Skip to content

Commit 5799772

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 1492364 commit 5799772

11 files changed

Lines changed: 237 additions & 17 deletions

File tree

apple/internal/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ bzl_library(
641641
visibility = [
642642
"//apple/internal:__subpackages__",
643643
],
644+
deps = [
645+
"@rules_cc//cc/common",
646+
],
644647
)
645648

646649
bzl_library(

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/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,
@@ -575,6 +580,7 @@ def _ios_app_clip_impl(ctx):
575580
suffix_default = ctx.attr._bundle_id_suffix_default,
576581
shared_capabilities = ctx.attr.shared_capabilities,
577582
)
583+
cc_toolchain = find_cpp_toolchain(ctx)
578584
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
579585
bundle_verification_targets = [struct(target = ext) for ext in ctx.attr.extensions]
580586
embeddable_targets = (
@@ -625,6 +631,10 @@ def _ios_app_clip_impl(ctx):
625631
apple_mac_toolchain_info = apple_mac_toolchain_info,
626632
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
627633
bundle_id = bundle_id,
634+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
635+
cc_toolchain = cc_toolchain,
636+
disabled_features = ctx.disabled_features,
637+
enabled_features = ctx.features,
628638
entitlements_file = ctx.file.entitlements,
629639
platform_prerequisites = platform_prerequisites,
630640
product_type = rule_descriptor.product_type,
@@ -1164,6 +1174,7 @@ def _ios_extension_impl(ctx):
11641174
suffix_default = ctx.attr._bundle_id_suffix_default,
11651175
shared_capabilities = ctx.attr.shared_capabilities,
11661176
)
1177+
cc_toolchain = find_cpp_toolchain(ctx)
11671178
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
11681179
features = features_support.compute_enabled_features(
11691180
requested_features = ctx.features,
@@ -1209,6 +1220,10 @@ def _ios_extension_impl(ctx):
12091220
apple_mac_toolchain_info = apple_mac_toolchain_info,
12101221
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
12111222
bundle_id = bundle_id,
1223+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1224+
cc_toolchain = cc_toolchain,
1225+
disabled_features = ctx.disabled_features,
1226+
enabled_features = ctx.features,
12121227
entitlements_file = ctx.file.entitlements,
12131228
platform_prerequisites = platform_prerequisites,
12141229
product_type = rule_descriptor.product_type,
@@ -1910,6 +1925,7 @@ def _ios_imessage_application_impl(ctx):
19101925
unsupported_features = ctx.disabled_features,
19111926
)
19121927
bundle_verification_targets = [struct(target = ctx.attr.extension)]
1928+
cc_toolchain = find_cpp_toolchain(ctx)
19131929
embeddable_targets = [ctx.attr.extension]
19141930
label = ctx.label
19151931
platform_prerequisites = platform_support.platform_prerequisites(
@@ -1949,6 +1965,10 @@ def _ios_imessage_application_impl(ctx):
19491965
apple_mac_toolchain_info = apple_mac_toolchain_info,
19501966
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
19511967
bundle_id = bundle_id,
1968+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1969+
cc_toolchain = cc_toolchain,
1970+
disabled_features = ctx.disabled_features,
1971+
enabled_features = ctx.features,
19521972
entitlements_file = ctx.file.entitlements,
19531973
platform_prerequisites = platform_prerequisites,
19541974
product_type = rule_descriptor.product_type,
@@ -2116,6 +2136,7 @@ def _ios_imessage_extension_impl(ctx):
21162136
shared_capabilities = ctx.attr.shared_capabilities,
21172137
)
21182138
executable_name = ctx.attr.executable_name
2139+
cc_toolchain = find_cpp_toolchain(ctx)
21192140
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
21202141
features = features_support.compute_enabled_features(
21212142
requested_features = ctx.features,
@@ -2159,6 +2180,10 @@ def _ios_imessage_extension_impl(ctx):
21592180
apple_mac_toolchain_info = apple_mac_toolchain_info,
21602181
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
21612182
bundle_id = bundle_id,
2183+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
2184+
cc_toolchain = cc_toolchain,
2185+
disabled_features = ctx.disabled_features,
2186+
enabled_features = ctx.features,
21622187
entitlements_file = ctx.file.entitlements,
21632188
platform_prerequisites = platform_prerequisites,
21642189
product_type = rule_descriptor.product_type,
@@ -2390,6 +2415,7 @@ def _ios_sticker_pack_extension_impl(ctx):
23902415
suffix_default = ctx.attr._bundle_id_suffix_default,
23912416
shared_capabilities = ctx.attr.shared_capabilities,
23922417
)
2418+
cc_toolchain = find_cpp_toolchain(ctx)
23932419
executable_name = ctx.attr.executable_name
23942420
features = features_support.compute_enabled_features(
23952421
requested_features = ctx.features,
@@ -2433,6 +2459,10 @@ def _ios_sticker_pack_extension_impl(ctx):
24332459
apple_mac_toolchain_info = apple_mac_toolchain_info,
24342460
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
24352461
bundle_id = bundle_id,
2462+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
2463+
cc_toolchain = cc_toolchain,
2464+
disabled_features = ctx.disabled_features,
2465+
enabled_features = ctx.features,
24362466
entitlements_file = ctx.file.entitlements,
24372467
platform_prerequisites = platform_prerequisites,
24382468
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,
@@ -500,6 +505,7 @@ def _macos_bundle_impl(ctx):
500505
suffix_default = ctx.attr._bundle_id_suffix_default,
501506
shared_capabilities = ctx.attr.shared_capabilities,
502507
)
508+
cc_toolchain = find_cpp_toolchain(ctx)
503509
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
504510
executable_name = ctx.attr.executable_name
505511
features = features_support.compute_enabled_features(
@@ -544,6 +550,10 @@ def _macos_bundle_impl(ctx):
544550
apple_mac_toolchain_info = apple_mac_toolchain_info,
545551
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
546552
bundle_id = bundle_id,
553+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
554+
cc_toolchain = cc_toolchain,
555+
disabled_features = ctx.disabled_features,
556+
enabled_features = ctx.features,
547557
entitlements_file = ctx.file.entitlements,
548558
platform_prerequisites = platform_prerequisites,
549559
product_type = rule_descriptor.product_type,
@@ -750,6 +760,7 @@ def _macos_extension_impl(ctx):
750760
suffix_default = ctx.attr._bundle_id_suffix_default,
751761
shared_capabilities = ctx.attr.shared_capabilities,
752762
)
763+
cc_toolchain = find_cpp_toolchain(ctx)
753764
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
754765
executable_name = ctx.attr.executable_name
755766
features = features_support.compute_enabled_features(
@@ -794,6 +805,10 @@ def _macos_extension_impl(ctx):
794805
apple_mac_toolchain_info = apple_mac_toolchain_info,
795806
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
796807
bundle_id = bundle_id,
808+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
809+
cc_toolchain = cc_toolchain,
810+
disabled_features = ctx.disabled_features,
811+
enabled_features = ctx.features,
797812
entitlements_file = ctx.file.entitlements,
798813
platform_prerequisites = platform_prerequisites,
799814
product_type = rule_descriptor.product_type,
@@ -1035,6 +1050,7 @@ def _macos_quick_look_plugin_impl(ctx):
10351050
suffix_default = ctx.attr._bundle_id_suffix_default,
10361051
shared_capabilities = ctx.attr.shared_capabilities,
10371052
)
1053+
cc_toolchain = find_cpp_toolchain(ctx)
10381054
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
10391055
executable_name = ctx.attr.executable_name
10401056
features = features_support.compute_enabled_features(
@@ -1078,6 +1094,10 @@ def _macos_quick_look_plugin_impl(ctx):
10781094
apple_mac_toolchain_info = apple_mac_toolchain_info,
10791095
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
10801096
bundle_id = bundle_id,
1097+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1098+
cc_toolchain = cc_toolchain,
1099+
disabled_features = ctx.disabled_features,
1100+
enabled_features = ctx.features,
10811101
entitlements_file = ctx.file.entitlements,
10821102
platform_prerequisites = platform_prerequisites,
10831103
product_type = rule_descriptor.product_type,
@@ -1287,6 +1307,7 @@ def _macos_kernel_extension_impl(ctx):
12871307
suffix_default = ctx.attr._bundle_id_suffix_default,
12881308
shared_capabilities = ctx.attr.shared_capabilities,
12891309
)
1310+
cc_toolchain = find_cpp_toolchain(ctx)
12901311
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
12911312
executable_name = ctx.attr.executable_name
12921313
features = features_support.compute_enabled_features(
@@ -1327,6 +1348,10 @@ def _macos_kernel_extension_impl(ctx):
13271348
apple_mac_toolchain_info = apple_mac_toolchain_info,
13281349
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
13291350
bundle_id = bundle_id,
1351+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1352+
cc_toolchain = cc_toolchain,
1353+
disabled_features = ctx.disabled_features,
1354+
enabled_features = ctx.features,
13301355
entitlements_file = ctx.file.entitlements,
13311356
platform_prerequisites = platform_prerequisites,
13321357
product_type = rule_descriptor.product_type,
@@ -1545,6 +1570,7 @@ def _macos_spotlight_importer_impl(ctx):
15451570
suffix_default = ctx.attr._bundle_id_suffix_default,
15461571
shared_capabilities = ctx.attr.shared_capabilities,
15471572
)
1573+
cc_toolchain = find_cpp_toolchain(ctx)
15481574
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
15491575
executable_name = ctx.attr.executable_name
15501576
features = features_support.compute_enabled_features(
@@ -1580,6 +1606,10 @@ def _macos_spotlight_importer_impl(ctx):
15801606
apple_mac_toolchain_info = apple_mac_toolchain_info,
15811607
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
15821608
bundle_id = bundle_id,
1609+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1610+
cc_toolchain = cc_toolchain,
1611+
disabled_features = ctx.disabled_features,
1612+
enabled_features = ctx.features,
15831613
entitlements_file = ctx.file.entitlements,
15841614
platform_prerequisites = platform_prerequisites,
15851615
product_type = rule_descriptor.product_type,
@@ -1788,6 +1818,7 @@ def _macos_xpc_service_impl(ctx):
17881818
suffix_default = ctx.attr._bundle_id_suffix_default,
17891819
shared_capabilities = ctx.attr.shared_capabilities,
17901820
)
1821+
cc_toolchain = find_cpp_toolchain(ctx)
17911822
cc_toolchain_forwarder = ctx.split_attr._cc_toolchain_forwarder
17921823
executable_name = ctx.attr.executable_name
17931824
features = features_support.compute_enabled_features(
@@ -1823,6 +1854,10 @@ def _macos_xpc_service_impl(ctx):
18231854
apple_mac_toolchain_info = apple_mac_toolchain_info,
18241855
apple_xplat_toolchain_info = apple_xplat_toolchain_info,
18251856
bundle_id = bundle_id,
1857+
cc_configured_features_init = features_support.make_cc_configured_features_init(ctx),
1858+
cc_toolchain = cc_toolchain,
1859+
disabled_features = ctx.disabled_features,
1860+
enabled_features = ctx.features,
18261861
entitlements_file = ctx.file.entitlements,
18271862
platform_prerequisites = platform_prerequisites,
18281863
product_type = rule_descriptor.product_type,

0 commit comments

Comments
 (0)