Skip to content

Commit 6f47836

Browse files
nglevinluispadron
authored andcommitted
Add facilities to check that cc toolchain features for enhanced security aren't being disabled when they're declared as enabled on the top level rule.
Automated testing is limited to what we're able to catch with Starlark analysis tests, i.e. the cases where we fail through the transition can't be effectively modelled in that type of test without bringing down the entire test suite Cherry-pick: d594354
1 parent a5bcde1 commit 6f47836

13 files changed

Lines changed: 253 additions & 25 deletions

apple/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ bzl_library(
101101
"//apple/internal:platform_support",
102102
"//apple/internal:providers",
103103
"@bazel_skylib//lib:dicts",
104-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
105104
"@build_bazel_apple_support//lib:apple_support",
106105
"@rules_cc//cc/common",
107106
],

apple/internal/BUILD

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ bzl_library(
7070
"@bazel_skylib//lib:partial",
7171
"@bazel_skylib//lib:paths",
7272
"@bazel_skylib//lib:sets",
73-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
7473
"@build_bazel_rules_swift//swift",
7574
],
7675
)
@@ -125,7 +124,6 @@ bzl_library(
125124
"//apple/internal/aspects:swift_usage_aspect",
126125
"@bazel_skylib//lib:dicts",
127126
"@bazel_skylib//lib:paths",
128-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
129127
"@build_bazel_apple_support//lib:apple_support",
130128
"@build_bazel_rules_swift//swift",
131129
],
@@ -336,7 +334,6 @@ bzl_library(
336334
"//apple/internal/utils:clang_rt_dylibs",
337335
"//apple/internal/utils:main_thread_checker_dylibs",
338336
"@bazel_skylib//lib:collections",
339-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
340337
"@build_bazel_apple_support//lib:apple_support",
341338
"@build_bazel_rules_swift//swift",
342339
"@build_bazel_rules_swift//swift:providers",
@@ -609,7 +606,6 @@ bzl_library(
609606
"//apple/internal/testing:apple_test_bundle_support",
610607
"//apple/internal/testing:apple_test_rule_support",
611608
"@bazel_skylib//lib:dicts",
612-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
613609
"@build_bazel_apple_support//lib:apple_support",
614610
],
615611
)
@@ -643,6 +639,9 @@ bzl_library(
643639
visibility = [
644640
"//apple/internal:__subpackages__",
645641
],
642+
deps = [
643+
"@rules_cc//cc/common",
644+
],
646645
)
647646

648647
bzl_library(
@@ -735,7 +734,6 @@ bzl_library(
735734
"//apple/internal/aspects:resource_aspect_hint",
736735
"//apple/internal/utils:clang_rt_dylibs",
737736
"//apple/internal/utils:main_thread_checker_dylibs",
738-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
739737
"@build_bazel_apple_support//lib:apple_support",
740738
"@build_bazel_rules_swift//swift",
741739
"@build_bazel_rules_swift//swift:providers",
@@ -776,7 +774,6 @@ bzl_library(
776774
"//apple/internal/utils:clang_rt_dylibs",
777775
"//apple/internal/utils:main_thread_checker_dylibs",
778776
"@bazel_skylib//lib:sets",
779-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
780777
"@build_bazel_apple_support//lib:apple_support",
781778
],
782779
)
@@ -813,7 +810,6 @@ bzl_library(
813810
"//apple/internal/utils:clang_rt_dylibs",
814811
"//apple/internal/utils:main_thread_checker_dylibs",
815812
"@bazel_skylib//lib:sets",
816-
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
817813
"@build_bazel_apple_support//lib:apple_support",
818814
],
819815
)

apple/internal/entitlements_support.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ def _process_entitlements(
200200
apple_mac_toolchain_info,
201201
apple_xplat_toolchain_info,
202202
bundle_id,
203+
cc_configured_features_init,
204+
cc_toolchain,
205+
disabled_features,
206+
enabled_features,
203207
entitlements_file,
204208
platform_prerequisites,
205209
product_type,
@@ -232,6 +236,11 @@ def _process_entitlements(
232236
apple_xplat_toolchain_info: The `struct` of tools from the shared Apple
233237
cross platform toolchain.
234238
bundle_id: The bundle identifier.
239+
cc_configured_features_init: The function to initialize the feature configuration for a
240+
given cc_toolchain.
241+
cc_toolchain: A cc_toolchain as found from the rule context's toolchains.
242+
disabled_features: The features requested to be disabled for the target.
243+
enabled_features: The features requested for the target.
235244
entitlements_file: The `File` containing the unprocessed entitlements
236245
(or `None` if none were provided).
237246
platform_prerequisites: The platform prerequisites.
@@ -276,7 +285,19 @@ def _process_entitlements(
276285
if secure_features:
277286
if not apple_xplat_toolchain_info.build_settings.enable_wip_features:
278287
fail("secure_features are still a work in progress and not yet supported in the rules.")
288+
289+
# Calculate the effective set of Crosstool features for this target, as we do want to double
290+
# check that the secure features are supported and enabled.
291+
feature_configuration = cc_configured_features_init(
292+
cc_toolchain = cc_toolchain,
293+
requested_features = enabled_features,
294+
unsupported_features = disabled_features,
295+
)
296+
297+
# Retrieve the entitlements required by the requested secure features, if there are any.
279298
secure_features_entitlements = secure_features_support.entitlements_from_secure_features(
299+
feature_configuration = feature_configuration,
300+
rule_label = rule_label,
280301
secure_features = secure_features,
281302
xcode_version = platform_prerequisites.xcode_version_config.xcode_version(),
282303
)

apple/internal/features_support.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Support macros to assist in detecting build features."""
1616

1717
load("@bazel_skylib//lib:new_sets.bzl", "sets")
18+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
1819

1920
def _compute_enabled_features(*, requested_features, unsupported_features):
2021
"""Returns a list of features for the given build.
@@ -33,6 +34,21 @@ def _compute_enabled_features(*, requested_features, unsupported_features):
3334
)
3435
return sets.to_list(enabled_features_set)
3536

37+
def _make_cc_configured_features_init(ctx):
38+
"""Captures the rule ctx for a deferred `cc_common.configure_features(...)` call.
39+
40+
Args:
41+
ctx: The rule context, expected to be captured directly in the rule context and NOT within a
42+
partial or helper method.
43+
44+
Returns:
45+
A lambda that has the captured instance of the rule context, which will always set that rule
46+
context as the `ctx` argument of `cc_common.configure_features(...)` and will forward any
47+
arguments it is given to `cc_common.configure_features(...)`.
48+
"""
49+
return lambda *args, **kwargs: cc_common.configure_features(ctx = ctx, *args, **kwargs)
50+
3651
features_support = struct(
3752
compute_enabled_features = _compute_enabled_features,
53+
make_cc_configured_features_init = _make_cc_configured_features_init,
3854
)

apple/internal/ios_rules.bzl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def _ios_application_impl(ctx):
180180
shared_capabilities = ctx.attr.shared_capabilities,
181181
)
182182
bundle_verification_targets = [struct(target = ext) for ext in ctx.attr.extensions]
183+
cc_toolchain = find_cpp_toolchain(ctx)
183184
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
184185
embeddable_targets = (
185186
ctx.attr.frameworks +
@@ -234,6 +235,10 @@ def _ios_application_impl(ctx):
234235
apple_mac_toolchain_info = apple_mac_toolchain_info,
235236
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
236237
bundle_id = bundle_id,
238+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
239+
cc_toolchain = cc_toolchain,
240+
disabled_features = ctx.disabled_features,
241+
enabled_features = ctx.features,
237242
entitlements_file = ctx.file.entitlements,
238243
platform_prerequisites = platform_prerequisites,
239244
product_type = rule_descriptor.product_type,
@@ -578,6 +583,7 @@ def _ios_app_clip_impl(ctx):
578583
suffix_default = ctx.attr._bundle_id_suffix_default,
579584
shared_capabilities = ctx.attr.shared_capabilities,
580585
)
586+
cc_toolchain = find_cpp_toolchain(ctx)
581587
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
582588
bundle_verification_targets = [struct(target = ext) for ext in ctx.attr.extensions]
583589
embeddable_targets = (
@@ -628,6 +634,10 @@ def _ios_app_clip_impl(ctx):
628634
apple_mac_toolchain_info = apple_mac_toolchain_info,
629635
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
630636
bundle_id = bundle_id,
637+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
638+
cc_toolchain = cc_toolchain,
639+
disabled_features = ctx.disabled_features,
640+
enabled_features = ctx.features,
631641
entitlements_file = ctx.file.entitlements,
632642
platform_prerequisites = platform_prerequisites,
633643
product_type = rule_descriptor.product_type,
@@ -1158,6 +1168,7 @@ def _ios_extension_impl(ctx):
11581168
suffix_default = ctx.attr._bundle_id_suffix_default,
11591169
shared_capabilities = ctx.attr.shared_capabilities,
11601170
)
1171+
cc_toolchain = find_cpp_toolchain(ctx)
11611172
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
11621173
features = features_support.compute_enabled_features(
11631174
requested_features = ctx.features,
@@ -1203,6 +1214,10 @@ def _ios_extension_impl(ctx):
12031214
apple_mac_toolchain_info = apple_mac_toolchain_info,
12041215
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
12051216
bundle_id = bundle_id,
1217+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1218+
cc_toolchain = cc_toolchain,
1219+
disabled_features = ctx.disabled_features,
1220+
enabled_features = ctx.features,
12061221
entitlements_file = ctx.file.entitlements,
12071222
platform_prerequisites = platform_prerequisites,
12081223
product_type = rule_descriptor.product_type,
@@ -1907,6 +1922,7 @@ def _ios_imessage_application_impl(ctx):
19071922
unsupported_features = ctx.disabled_features,
19081923
)
19091924
bundle_verification_targets = [struct(target = ctx.attr.extension)]
1925+
cc_toolchain = find_cpp_toolchain(ctx)
19101926
embeddable_targets = [ctx.attr.extension]
19111927
label = ctx.label
19121928
platform_prerequisites = platform_support.platform_prerequisites(
@@ -1946,6 +1962,10 @@ def _ios_imessage_application_impl(ctx):
19461962
apple_mac_toolchain_info = apple_mac_toolchain_info,
19471963
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
19481964
bundle_id = bundle_id,
1965+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1966+
cc_toolchain = cc_toolchain,
1967+
disabled_features = ctx.disabled_features,
1968+
enabled_features = ctx.features,
19491969
entitlements_file = ctx.file.entitlements,
19501970
platform_prerequisites = platform_prerequisites,
19511971
product_type = rule_descriptor.product_type,
@@ -2113,6 +2133,7 @@ def _ios_imessage_extension_impl(ctx):
21132133
shared_capabilities = ctx.attr.shared_capabilities,
21142134
)
21152135
executable_name = ctx.attr.executable_name
2136+
cc_toolchain = find_cpp_toolchain(ctx)
21162137
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
21172138
features = features_support.compute_enabled_features(
21182139
requested_features = ctx.features,
@@ -2156,6 +2177,10 @@ def _ios_imessage_extension_impl(ctx):
21562177
apple_mac_toolchain_info = apple_mac_toolchain_info,
21572178
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
21582179
bundle_id = bundle_id,
2180+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
2181+
cc_toolchain = cc_toolchain,
2182+
disabled_features = ctx.disabled_features,
2183+
enabled_features = ctx.features,
21592184
entitlements_file = ctx.file.entitlements,
21602185
platform_prerequisites = platform_prerequisites,
21612186
product_type = rule_descriptor.product_type,
@@ -2387,6 +2412,7 @@ def _ios_sticker_pack_extension_impl(ctx):
23872412
suffix_default = ctx.attr._bundle_id_suffix_default,
23882413
shared_capabilities = ctx.attr.shared_capabilities,
23892414
)
2415+
cc_toolchain = find_cpp_toolchain(ctx)
23902416
executable_name = ctx.attr.executable_name
23912417
features = features_support.compute_enabled_features(
23922418
requested_features = ctx.features,
@@ -2430,6 +2456,10 @@ def _ios_sticker_pack_extension_impl(ctx):
24302456
apple_mac_toolchain_info = apple_mac_toolchain_info,
24312457
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
24322458
bundle_id = bundle_id,
2459+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
2460+
cc_toolchain = cc_toolchain,
2461+
disabled_features = ctx.disabled_features,
2462+
enabled_features = ctx.features,
24332463
entitlements_file = ctx.file.entitlements,
24342464
platform_prerequisites = platform_prerequisites,
24352465
product_type = rule_descriptor.product_type,

apple/internal/macos_rules.bzl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def _macos_application_impl(ctx):
188188
shared_capabilities = ctx.attr.shared_capabilities,
189189
)
190190
bundle_verification_targets = [struct(target = ext) for ext in verification_targets]
191+
cc_toolchain = find_cpp_toolchain(ctx)
191192
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
192193
executable_name = ctx.attr.executable_name
193194
features = features_support.compute_enabled_features(
@@ -232,6 +233,10 @@ def _macos_application_impl(ctx):
232233
apple_mac_toolchain_info = apple_mac_toolchain_info,
233234
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
234235
bundle_id = bundle_id,
236+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
237+
cc_toolchain = cc_toolchain,
238+
disabled_features = ctx.disabled_features,
239+
enabled_features = ctx.features,
235240
entitlements_file = ctx.file.entitlements,
236241
platform_prerequisites = platform_prerequisites,
237242
product_type = rule_descriptor.product_type,
@@ -503,6 +508,7 @@ def _macos_bundle_impl(ctx):
503508
suffix_default = ctx.attr._bundle_id_suffix_default,
504509
shared_capabilities = ctx.attr.shared_capabilities,
505510
)
511+
cc_toolchain = find_cpp_toolchain(ctx)
506512
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
507513
executable_name = ctx.attr.executable_name
508514
features = features_support.compute_enabled_features(
@@ -547,6 +553,10 @@ def _macos_bundle_impl(ctx):
547553
apple_mac_toolchain_info = apple_mac_toolchain_info,
548554
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
549555
bundle_id = bundle_id,
556+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
557+
cc_toolchain = cc_toolchain,
558+
disabled_features = ctx.disabled_features,
559+
enabled_features = ctx.features,
550560
entitlements_file = ctx.file.entitlements,
551561
platform_prerequisites = platform_prerequisites,
552562
product_type = rule_descriptor.product_type,
@@ -753,6 +763,7 @@ def _macos_extension_impl(ctx):
753763
suffix_default = ctx.attr._bundle_id_suffix_default,
754764
shared_capabilities = ctx.attr.shared_capabilities,
755765
)
766+
cc_toolchain = find_cpp_toolchain(ctx)
756767
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
757768
executable_name = ctx.attr.executable_name
758769
features = features_support.compute_enabled_features(
@@ -797,6 +808,10 @@ def _macos_extension_impl(ctx):
797808
apple_mac_toolchain_info = apple_mac_toolchain_info,
798809
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
799810
bundle_id = bundle_id,
811+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
812+
cc_toolchain = cc_toolchain,
813+
disabled_features = ctx.disabled_features,
814+
enabled_features = ctx.features,
800815
entitlements_file = ctx.file.entitlements,
801816
platform_prerequisites = platform_prerequisites,
802817
product_type = rule_descriptor.product_type,
@@ -1038,6 +1053,7 @@ def _macos_quick_look_plugin_impl(ctx):
10381053
suffix_default = ctx.attr._bundle_id_suffix_default,
10391054
shared_capabilities = ctx.attr.shared_capabilities,
10401055
)
1056+
cc_toolchain = find_cpp_toolchain(ctx)
10411057
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
10421058
executable_name = ctx.attr.executable_name
10431059
features = features_support.compute_enabled_features(
@@ -1081,6 +1097,10 @@ def _macos_quick_look_plugin_impl(ctx):
10811097
apple_mac_toolchain_info = apple_mac_toolchain_info,
10821098
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
10831099
bundle_id = bundle_id,
1100+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1101+
cc_toolchain = cc_toolchain,
1102+
disabled_features = ctx.disabled_features,
1103+
enabled_features = ctx.features,
10841104
entitlements_file = ctx.file.entitlements,
10851105
platform_prerequisites = platform_prerequisites,
10861106
product_type = rule_descriptor.product_type,
@@ -1290,6 +1310,7 @@ def _macos_kernel_extension_impl(ctx):
12901310
suffix_default = ctx.attr._bundle_id_suffix_default,
12911311
shared_capabilities = ctx.attr.shared_capabilities,
12921312
)
1313+
cc_toolchain = find_cpp_toolchain(ctx)
12931314
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
12941315
executable_name = ctx.attr.executable_name
12951316
features = features_support.compute_enabled_features(
@@ -1330,6 +1351,10 @@ def _macos_kernel_extension_impl(ctx):
13301351
apple_mac_toolchain_info = apple_mac_toolchain_info,
13311352
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
13321353
bundle_id = bundle_id,
1354+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1355+
cc_toolchain = cc_toolchain,
1356+
disabled_features = ctx.disabled_features,
1357+
enabled_features = ctx.features,
13331358
entitlements_file = ctx.file.entitlements,
13341359
platform_prerequisites = platform_prerequisites,
13351360
product_type = rule_descriptor.product_type,
@@ -1548,6 +1573,7 @@ def _macos_spotlight_importer_impl(ctx):
15481573
suffix_default = ctx.attr._bundle_id_suffix_default,
15491574
shared_capabilities = ctx.attr.shared_capabilities,
15501575
)
1576+
cc_toolchain = find_cpp_toolchain(ctx)
15511577
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
15521578
executable_name = ctx.attr.executable_name
15531579
features = features_support.compute_enabled_features(
@@ -1583,6 +1609,10 @@ def _macos_spotlight_importer_impl(ctx):
15831609
apple_mac_toolchain_info = apple_mac_toolchain_info,
15841610
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
15851611
bundle_id = bundle_id,
1612+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1613+
cc_toolchain = cc_toolchain,
1614+
disabled_features = ctx.disabled_features,
1615+
enabled_features = ctx.features,
15861616
entitlements_file = ctx.file.entitlements,
15871617
platform_prerequisites = platform_prerequisites,
15881618
product_type = rule_descriptor.product_type,
@@ -1791,6 +1821,7 @@ def _macos_xpc_service_impl(ctx):
17911821
suffix_default = ctx.attr._bundle_id_suffix_default,
17921822
shared_capabilities = ctx.attr.shared_capabilities,
17931823
)
1824+
cc_toolchain = find_cpp_toolchain(ctx)
17941825
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
17951826
executable_name = ctx.attr.executable_name
17961827
features = features_support.compute_enabled_features(
@@ -1826,6 +1857,10 @@ def _macos_xpc_service_impl(ctx):
18261857
apple_mac_toolchain_info = apple_mac_toolchain_info,
18271858
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
18281859
bundle_id = bundle_id,
1860+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1861+
cc_toolchain = cc_toolchain,
1862+
disabled_features = ctx.disabled_features,
1863+
enabled_features = ctx.features,
18291864
entitlements_file = ctx.file.entitlements,
18301865
platform_prerequisites = platform_prerequisites,
18311866
product_type = rule_descriptor.product_type,

0 commit comments

Comments
 (0)