Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 18 | "fmt" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 19 | "path/filepath" |
| 20 | "reflect" |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 21 | "regexp" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 22 | "strconv" |
| 23 | "strings" |
| 24 | |
| 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
| 28 | // "neverallow" rules for the build system. |
| 29 | // |
| 30 | // This allows things which aren't related to the build system and are enforced |
Joe Onorato | b4638c1 | 2021-10-27 15:47:06 -0700 | [diff] [blame] | 31 | // against assumptions, in progress code refactors, or policy to be expressed in a |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 32 | // straightforward away disjoint from implementations and tests which should |
| 33 | // work regardless of these restrictions. |
| 34 | // |
| 35 | // A module is disallowed if all of the following are true: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 36 | // - it is in one of the "In" paths |
| 37 | // - it is not in one of the "NotIn" paths |
| 38 | // - it has all "With" properties matched |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 39 | // - - values are matched in their entirety |
| 40 | // - - nil is interpreted as an empty string |
| 41 | // - - nested properties are separated with a '.' |
| 42 | // - - if the property is a list, any of the values in the list being matches |
| 43 | // counts as a match |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 44 | // - it has none of the "Without" properties matched (same rules as above) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 45 | |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 46 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 47 | ctx.BottomUp("neverallow", neverallowMutator) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 50 | var neverallows = []Rule{} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 51 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 52 | func init() { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 53 | AddNeverAllowRules(createIncludeDirsRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 54 | AddNeverAllowRules(createTrebleRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 55 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 56 | AddNeverAllowRules(createCcSdkVariantRules()...) |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 57 | AddNeverAllowRules(createUncompressDexRules()...) |
Nelson Li | 4380820 | 2024-11-20 09:05:53 +0000 | [diff] [blame] | 58 | AddNeverAllowRules(createInstallInRootAllowingRules()...) |
Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 59 | AddNeverAllowRules(createProhibitFrameworkAccessRules()...) |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 60 | AddNeverAllowRules(createCcStubsRule()) |
Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 61 | AddNeverAllowRules(createProhibitHeaderOnlyRule()) |
Steven Moreland | 0db999c | 2024-09-03 22:06:24 +0000 | [diff] [blame] | 62 | AddNeverAllowRules(createLimitNdkExportRule()...) |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 63 | AddNeverAllowRules(createLimitDirgroupRule()...) |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 64 | AddNeverAllowRules(createFilesystemIsAutoGeneratedRule()) |
Luca Stefani | 4102975 | 2024-10-12 17:55:31 +0200 | [diff] [blame] | 65 | AddNeverAllowRules(createKotlinPluginRule()...) |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 66 | AddNeverAllowRules(createPrebuiltEtcBpDefineRule()) |
Spandan Das | e2f98da | 2024-11-18 19:22:39 +0000 | [diff] [blame] | 67 | AddNeverAllowRules(createAutogenRroBpDefineRule()) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 68 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 69 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 70 | // Add a NeverAllow rule to the set of rules to apply. |
| 71 | func AddNeverAllowRules(rules ...Rule) { |
| 72 | neverallows = append(neverallows, rules...) |
| 73 | } |
| 74 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 75 | var ( |
| 76 | neverallowNotInIncludeDir = []string{ |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 77 | "art", |
Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 78 | "art/libnativebridge", |
| 79 | "art/libnativeloader", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 80 | "libcore", |
| 81 | "libnativehelper", |
| 82 | "external/apache-harmony", |
| 83 | "external/apache-xml", |
| 84 | "external/boringssl", |
| 85 | "external/bouncycastle", |
| 86 | "external/conscrypt", |
| 87 | "external/icu", |
| 88 | "external/okhttp", |
| 89 | "external/vixl", |
| 90 | "external/wycheproof", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 91 | } |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 92 | neverallowNoUseIncludeDir = []string{ |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 93 | "frameworks/av/apex", |
| 94 | "frameworks/av/tools", |
| 95 | "frameworks/native/cmds", |
| 96 | "system/apex", |
| 97 | "system/bpf", |
| 98 | "system/gatekeeper", |
| 99 | "system/hwservicemanager", |
| 100 | "system/libbase", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 101 | "system/libfmq", |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 102 | "system/libvintf", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 103 | } |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 104 | ) |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 105 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 106 | func createIncludeDirsRules() []Rule { |
| 107 | rules := make([]Rule, 0, len(neverallowNotInIncludeDir)+len(neverallowNoUseIncludeDir)) |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 108 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 109 | for _, path := range neverallowNotInIncludeDir { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 110 | rule := |
| 111 | NeverAllow(). |
| 112 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 113 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 114 | " to use alternate mechanisms and so can no longer be used.") |
| 115 | |
| 116 | rules = append(rules, rule) |
| 117 | } |
| 118 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 119 | for _, path := range neverallowNoUseIncludeDir { |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 120 | rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). |
| 121 | Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + |
| 122 | " to use alternate mechanisms and so can no longer be used.") |
| 123 | rules = append(rules, rule) |
| 124 | } |
| 125 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 126 | return rules |
| 127 | } |
| 128 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 129 | func createTrebleRules() []Rule { |
| 130 | return []Rule{ |
| 131 | NeverAllow(). |
| 132 | In("vendor", "device"). |
| 133 | With("vndk.enabled", "true"). |
| 134 | Without("vendor", "true"). |
Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 135 | Without("product_specific", "true"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 136 | Because("the VNDK can never contain a library that is device dependent."), |
| 137 | NeverAllow(). |
| 138 | With("vndk.enabled", "true"). |
| 139 | Without("vendor", "true"). |
| 140 | Without("owner", ""). |
| 141 | Because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 142 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 143 | // TODO(b/67974785): always enforce the manifest |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 144 | NeverAllow(). |
Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 145 | Without("name", "libhidlbase-combined-impl"). |
| 146 | Without("name", "libhidlbase"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 147 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 148 | Because("manifest enforcement should be independent of ."), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 149 | |
| 150 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 151 | NeverAllow(). |
| 152 | Without("name", "libc_bionic_ndk"). |
| 153 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 154 | Because("nothing should care if linker namespaces are enabled or not"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 155 | |
| 156 | // Example: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 157 | // *NeverAllow().with("Srcs", "main.cpp")) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 161 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 162 | javaDeviceForHostProjectsAllowedList := []string{ |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 163 | "development/build", |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 164 | "external/guava", |
Steve Elliott | 8053f82 | 2022-10-18 17:09:28 -0400 | [diff] [blame] | 165 | "external/kotlinx.coroutines", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 166 | "external/robolectric-shadows", |
Rex Hoffman | 54641d2 | 2022-08-25 17:29:50 +0000 | [diff] [blame] | 167 | "external/robolectric", |
Makoto Onuki | b66bba3 | 2023-11-11 00:06:09 +0000 | [diff] [blame] | 168 | "frameworks/base/ravenwood", |
| 169 | "frameworks/base/tools/hoststubgen", |
Jerome Gaillard | 655ee02 | 2021-09-23 11:38:08 +0000 | [diff] [blame] | 170 | "frameworks/layoutlib", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 173 | return []Rule{ |
| 174 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 175 | NotIn(javaDeviceForHostProjectsAllowedList...). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 176 | ModuleType("java_device_for_host", "java_host_for_device"). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 177 | Because("java_device_for_host can only be used in allowed projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 181 | func createCcSdkVariantRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 182 | sdkVersionOnlyAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 183 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 184 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 185 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 186 | // the APEX modules contain the SDK variant and the platform variant still exists. |
Anton Hansson | 4b8e64b | 2020-05-27 18:25:23 +0100 | [diff] [blame] | 187 | "packages/modules/SdkExtensions/derive_sdk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 188 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 189 | "prebuilts/ndk", |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 190 | "frameworks/native/libs/binder/ndk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 191 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 192 | "tools/test/graphicsbenchmark/functional_tests/java", |
Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 193 | "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", |
Chang Li | 66d3cb7 | 2021-06-18 14:04:50 +0000 | [diff] [blame] | 194 | "external/libtextclassifier/native", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 197 | platformVariantPropertiesAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 198 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 199 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 200 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 201 | // for the platform variant. Allow them to use the platform variant |
| 202 | // property to set shared_libs. |
| 203 | "prebuilts/ndk", |
| 204 | "frameworks/rs", |
| 205 | } |
| 206 | |
| 207 | return []Rule{ |
| 208 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 209 | NotIn(sdkVersionOnlyAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 210 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 211 | Because("sdk_variant_only can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 212 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 213 | NotIn(platformVariantPropertiesAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 214 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 215 | Because("platform variant properties can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 219 | func createCcStubsRule() Rule { |
| 220 | ccStubsImplementationInstallableProjectsAllowedList := []string{ |
Jiyong Park | f6736c7 | 2024-07-22 11:22:35 +0900 | [diff] [blame] | 221 | "packages/modules/Virtualization/libs/libvm_payload", |
Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return NeverAllow(). |
| 225 | NotIn(ccStubsImplementationInstallableProjectsAllowedList...). |
| 226 | WithMatcher("stubs.implementation_installable", isSetMatcherInstance). |
| 227 | Because("implementation_installable can only be used in allowed projects.") |
| 228 | } |
| 229 | |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 230 | func createUncompressDexRules() []Rule { |
| 231 | return []Rule{ |
| 232 | NeverAllow(). |
| 233 | NotIn("art"). |
| 234 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 235 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 236 | } |
| 237 | } |
| 238 | |
Nelson Li | 4380820 | 2024-11-20 09:05:53 +0000 | [diff] [blame] | 239 | func createInstallInRootAllowingRules() []Rule { |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 240 | return []Rule{ |
| 241 | NeverAllow(). |
Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 242 | Without("name", "init_first_stage_defaults"). |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 243 | Without("name", "init_first_stage"). |
Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 244 | Without("name", "init_first_stage.microdroid"). |
Nelson Li | 4380820 | 2024-11-20 09:05:53 +0000 | [diff] [blame] | 245 | Without("name", "librecovery_ui_ext"). |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 246 | With("install_in_root", "true"). |
Inseob Kim | be6a66d | 2024-07-11 15:43:44 +0900 | [diff] [blame] | 247 | NotModuleType("prebuilt_root"). |
Jihoon Kang | 320ca7c | 2024-12-03 18:14:50 +0000 | [diff] [blame] | 248 | NotModuleType("prebuilt_vendor"). |
| 249 | NotModuleType("prebuilt_sbin"). |
| 250 | NotModuleType("prebuilt_system"). |
| 251 | NotModuleType("prebuilt_first_stage_ramdisk"). |
Jihoon Kang | 96f38d2 | 2024-12-03 23:17:55 +0000 | [diff] [blame] | 252 | NotModuleType("prebuilt_res"). |
Jihoon Kang | cc1c4aa | 2025-03-12 22:14:39 +0000 | [diff] [blame] | 253 | NotModuleType("prebuilt_any"). |
Nelson Li | 4380820 | 2024-11-20 09:05:53 +0000 | [diff] [blame] | 254 | Because("install_in_root is only for init_first_stage or librecovery_ui_ext."), |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 258 | func createProhibitFrameworkAccessRules() []Rule { |
| 259 | return []Rule{ |
| 260 | NeverAllow(). |
| 261 | With("libs", "framework"). |
| 262 | WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")). |
| 263 | Because("framework can't be used when building against SDK"), |
| 264 | } |
| 265 | } |
| 266 | |
Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 267 | func createProhibitHeaderOnlyRule() Rule { |
| 268 | return NeverAllow(). |
| 269 | Without("name", "framework-minus-apex-headers"). |
| 270 | With("headers_only", "true"). |
| 271 | Because("headers_only can only be used for generating framework-minus-apex headers for non-updatable modules") |
| 272 | } |
| 273 | |
Steven Moreland | 0db999c | 2024-09-03 22:06:24 +0000 | [diff] [blame] | 274 | func createLimitNdkExportRule() []Rule { |
| 275 | reason := "If the headers you're trying to export are meant to be a part of the NDK, they should be exposed by an ndk_headers module. If the headers shouldn't be a part of the NDK, the headers should instead be exposed from a separate `cc_library_headers` which consumers depend on." |
| 276 | // DO NOT ADD HERE - please consult danalbert@ |
| 277 | // b/357711733 |
| 278 | return []Rule{ |
| 279 | NeverAllow(). |
| 280 | NotIn("frameworks/native/libs/binder/ndk"). |
| 281 | ModuleType("ndk_library"). |
| 282 | WithMatcher("export_header_libs", isSetMatcherInstance).Because(reason), |
| 283 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_generated_headers", isSetMatcherInstance).Because(reason), |
| 284 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_include_dirs", isSetMatcherInstance).Because(reason), |
| 285 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_shared_lib_headers", isSetMatcherInstance).Because(reason), |
| 286 | NeverAllow().ModuleType("ndk_library").WithMatcher("export_static_lib_headers", isSetMatcherInstance).Because(reason), |
| 287 | } |
| 288 | } |
| 289 | |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 290 | func createLimitDirgroupRule() []Rule { |
Inseob Kim | 7195b06 | 2024-11-29 15:40:49 +0900 | [diff] [blame] | 291 | reason := "dirgroup module and dir_srcs / keep_gendir property of genrule is allowed only to Trusty build rule." |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 292 | return []Rule{ |
| 293 | NeverAllow(). |
| 294 | ModuleType("dirgroup"). |
| 295 | WithMatcher("visibility", NotInList([]string{"//trusty/vendor/google/aosp/scripts"})).Because(reason), |
| 296 | NeverAllow(). |
| 297 | ModuleType("dirgroup"). |
| 298 | Without("visibility", "//trusty/vendor/google/aosp/scripts").Because(reason), |
| 299 | NeverAllow(). |
| 300 | ModuleType("genrule"). |
Inseob Kim | e8b1ac4 | 2024-10-24 09:27:44 +0000 | [diff] [blame] | 301 | Without("name", "trusty-arm64.lk.elf.gen"). |
Alice Wang | 38aed8d | 2024-11-27 08:18:59 +0000 | [diff] [blame] | 302 | Without("name", "trusty-arm64-virt-test-debug.lk.elf.gen"). |
Inseob Kim | e8b1ac4 | 2024-10-24 09:27:44 +0000 | [diff] [blame] | 303 | Without("name", "trusty-x86_64.lk.elf.gen"). |
| 304 | Without("name", "trusty-x86_64-test.lk.elf.gen"). |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 305 | WithMatcher("dir_srcs", isSetMatcherInstance).Because(reason), |
Inseob Kim | 7195b06 | 2024-11-29 15:40:49 +0900 | [diff] [blame] | 306 | NeverAllow(). |
| 307 | ModuleType("genrule"). |
| 308 | Without("name", "trusty-arm64.lk.elf.gen"). |
| 309 | Without("name", "trusty-arm64-virt-test-debug.lk.elf.gen"). |
| 310 | Without("name", "trusty-x86_64.lk.elf.gen"). |
| 311 | Without("name", "trusty-x86_64-test.lk.elf.gen"). |
| 312 | With("keep_gendir", "true").Because(reason), |
Inseob Kim | 76e1985 | 2024-10-10 17:57:22 +0900 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Jihoon Kang | 0d545b8 | 2024-10-11 00:21:57 +0000 | [diff] [blame] | 316 | func createFilesystemIsAutoGeneratedRule() Rule { |
| 317 | return NeverAllow(). |
| 318 | NotIn("build/soong/fsgen"). |
| 319 | ModuleType("filesystem", "android_system_image"). |
| 320 | WithMatcher("is_auto_generated", isSetMatcherInstance). |
| 321 | Because("is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory") |
| 322 | } |
| 323 | |
Luca Stefani | 4102975 | 2024-10-12 17:55:31 +0200 | [diff] [blame] | 324 | func createKotlinPluginRule() []Rule { |
| 325 | kotlinPluginProjectsAllowedList := []string{ |
| 326 | // TODO: Migrate compose plugin to the bundled compiler plugin |
| 327 | // Actual path prebuilts/sdk/current/androidx/m2repository/androidx/compose/compiler/compiler-hosted |
| 328 | "prebuilts/sdk/current/androidx", |
| 329 | "external/kotlinc", |
| 330 | } |
| 331 | |
| 332 | return []Rule{ |
| 333 | NeverAllow(). |
| 334 | NotIn(kotlinPluginProjectsAllowedList...). |
| 335 | ModuleType("kotlin_plugin"). |
| 336 | Because("kotlin_plugin can only be used in allowed projects"), |
| 337 | } |
| 338 | } |
| 339 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 340 | // These module types are introduced to convert PRODUCT_COPY_FILES to Soong, |
| 341 | // and is only intended to be used by filesystem_creator. |
| 342 | func createPrebuiltEtcBpDefineRule() Rule { |
| 343 | return NeverAllow(). |
| 344 | ModuleType( |
Jihoon Kang | cc1c4aa | 2025-03-12 22:14:39 +0000 | [diff] [blame] | 345 | "prebuilt_any", |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 346 | "prebuilt_usr_srec", |
| 347 | "prebuilt_priv_app", |
| 348 | "prebuilt_rfs", |
| 349 | "prebuilt_framework", |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 350 | "prebuilt_wlc_upt", |
| 351 | "prebuilt_odm", |
Jihoon Kang | 2e2b744 | 2024-11-05 00:26:20 +0000 | [diff] [blame] | 352 | "prebuilt_vendor_dlkm", |
| 353 | "prebuilt_bt_firmware", |
Jihoon Kang | dca2f2b | 2024-11-06 18:43:19 +0000 | [diff] [blame] | 354 | "prebuilt_tvservice", |
| 355 | "prebuilt_optee", |
Jihoon Kang | ecf76dd | 2024-11-12 05:24:46 +0000 | [diff] [blame] | 356 | "prebuilt_tvconfig", |
Jihoon Kang | 3ca07a1 | 2024-12-02 19:14:30 +0000 | [diff] [blame] | 357 | "prebuilt_vendor", |
Jihoon Kang | 320ca7c | 2024-12-03 18:14:50 +0000 | [diff] [blame] | 358 | "prebuilt_sbin", |
| 359 | "prebuilt_system", |
| 360 | "prebuilt_first_stage_ramdisk", |
Michael Bestas | e6b299a | 2025-03-08 20:44:34 +0200 | [diff] [blame] | 361 | "prebuilt_radio", |
Luca Stefani | 5daba74 | 2025-03-09 12:41:06 +0100 | [diff] [blame] | 362 | "prebuilt_gpu", |
Artem Borisov | 81732c2 | 2025-03-11 23:19:26 +0300 | [diff] [blame] | 363 | "prebuilt_vendor_overlay", |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 364 | ). |
| 365 | DefinedInBpFile(). |
| 366 | Because("module type not allowed to be defined in bp file") |
| 367 | } |
| 368 | |
Spandan Das | e2f98da | 2024-11-18 19:22:39 +0000 | [diff] [blame] | 369 | func createAutogenRroBpDefineRule() Rule { |
| 370 | return NeverAllow(). |
| 371 | ModuleType( |
| 372 | "autogen_runtime_resource_overlay", |
| 373 | ). |
| 374 | DefinedInBpFile(). |
| 375 | Because("Module type will be autogenerated by soong. Use runtime_resource_overlay instead") |
| 376 | } |
| 377 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 378 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 379 | m, ok := ctx.Module().(Module) |
| 380 | if !ok { |
| 381 | return |
| 382 | } |
| 383 | |
| 384 | dir := ctx.ModuleDir() + "/" |
| 385 | properties := m.GetProperties() |
| 386 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 387 | osClass := ctx.Module().Target().Os.Class |
| 388 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 389 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 390 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 391 | if !n.appliesToPath(dir) { |
| 392 | continue |
| 393 | } |
| 394 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 395 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 396 | continue |
| 397 | } |
| 398 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 399 | if !n.appliesToProperties(ctx, properties) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 400 | continue |
| 401 | } |
| 402 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 403 | if !n.appliesToOsClass(osClass) { |
| 404 | continue |
| 405 | } |
| 406 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 407 | if !n.appliesToDirectDeps(ctx) { |
| 408 | continue |
| 409 | } |
| 410 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 411 | if !n.appliesToBpDefinedModule(ctx) { |
| 412 | continue |
| 413 | } |
| 414 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 415 | ctx.ModuleErrorf("violates " + n.String()) |
| 416 | } |
| 417 | } |
| 418 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 419 | type ValueMatcher interface { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 420 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 421 | String() string |
| 422 | } |
| 423 | |
| 424 | type equalMatcher struct { |
| 425 | expected string |
| 426 | } |
| 427 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 428 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 429 | return m.expected == value |
| 430 | } |
| 431 | |
| 432 | func (m *equalMatcher) String() string { |
| 433 | return "=" + m.expected |
| 434 | } |
| 435 | |
| 436 | type anyMatcher struct { |
| 437 | } |
| 438 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 439 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 440 | return true |
| 441 | } |
| 442 | |
| 443 | func (m *anyMatcher) String() string { |
| 444 | return "=*" |
| 445 | } |
| 446 | |
| 447 | var anyMatcherInstance = &anyMatcher{} |
| 448 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 449 | type startsWithMatcher struct { |
| 450 | prefix string |
| 451 | } |
| 452 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 453 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 454 | return strings.HasPrefix(value, m.prefix) |
| 455 | } |
| 456 | |
| 457 | func (m *startsWithMatcher) String() string { |
| 458 | return ".starts-with(" + m.prefix + ")" |
| 459 | } |
| 460 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 461 | type regexMatcher struct { |
| 462 | re *regexp.Regexp |
| 463 | } |
| 464 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 465 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 466 | return m.re.MatchString(value) |
| 467 | } |
| 468 | |
| 469 | func (m *regexMatcher) String() string { |
| 470 | return ".regexp(" + m.re.String() + ")" |
| 471 | } |
| 472 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 473 | type notInListMatcher struct { |
| 474 | allowed []string |
| 475 | } |
| 476 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 477 | func (m *notInListMatcher) Test(value string) bool { |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 478 | return !InList(value, m.allowed) |
| 479 | } |
| 480 | |
| 481 | func (m *notInListMatcher) String() string { |
| 482 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 483 | } |
| 484 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 485 | type isSetMatcher struct{} |
| 486 | |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 487 | func (m *isSetMatcher) Test(value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 488 | return value != "" |
| 489 | } |
| 490 | |
| 491 | func (m *isSetMatcher) String() string { |
| 492 | return ".is-set" |
| 493 | } |
| 494 | |
| 495 | var isSetMatcherInstance = &isSetMatcher{} |
| 496 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 497 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 498 | fields []string // e.x.: Vndk.Enabled |
| 499 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 502 | func (r *ruleProperty) String() string { |
| 503 | return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher) |
| 504 | } |
| 505 | |
| 506 | type ruleProperties []ruleProperty |
| 507 | |
| 508 | func (r ruleProperties) String() string { |
| 509 | var s []string |
| 510 | for _, r := range r { |
| 511 | s = append(s, r.String()) |
| 512 | } |
| 513 | return strings.Join(s, " ") |
| 514 | } |
| 515 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 516 | // A NeverAllow rule. |
| 517 | type Rule interface { |
| 518 | In(path ...string) Rule |
| 519 | |
| 520 | NotIn(path ...string) Rule |
| 521 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 522 | InDirectDeps(deps ...string) Rule |
| 523 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 524 | WithOsClass(osClasses ...OsClass) Rule |
| 525 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 526 | ModuleType(types ...string) Rule |
| 527 | |
| 528 | NotModuleType(types ...string) Rule |
| 529 | |
| 530 | With(properties, value string) Rule |
| 531 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 532 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 533 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 534 | Without(properties, value string) Rule |
| 535 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 536 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 537 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 538 | DefinedInBpFile() Rule |
| 539 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 540 | Because(reason string) Rule |
| 541 | } |
| 542 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 543 | type rule struct { |
| 544 | // User string for why this is a thing. |
| 545 | reason string |
| 546 | |
| 547 | paths []string |
| 548 | unlessPaths []string |
| 549 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 550 | directDeps map[string]bool |
| 551 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 552 | osClasses []OsClass |
| 553 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 554 | moduleTypes []string |
| 555 | unlessModuleTypes []string |
| 556 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 557 | props ruleProperties |
| 558 | unlessProps ruleProperties |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 559 | |
| 560 | onlyBootclasspathJar bool |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 561 | |
| 562 | definedInBp bool |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 565 | // Create a new NeverAllow rule. |
| 566 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 567 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 568 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 569 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 570 | // In adds path(s) where this rule applies. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 571 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 572 | r.paths = append(r.paths, cleanPaths(path)...) |
| 573 | return r |
| 574 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 575 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 576 | // NotIn adds path(s) to that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 577 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 578 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 579 | return r |
| 580 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 581 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 582 | // InDirectDeps adds dep(s) that are not allowed with this rule. |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 583 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 584 | for _, d := range deps { |
| 585 | r.directDeps[d] = true |
| 586 | } |
| 587 | return r |
| 588 | } |
| 589 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 590 | // WithOsClass adds osClass(es) that this rule applies to. |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 591 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 592 | r.osClasses = append(r.osClasses, osClasses...) |
| 593 | return r |
| 594 | } |
| 595 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 596 | // ModuleType adds type(s) that this rule applies to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 597 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 598 | r.moduleTypes = append(r.moduleTypes, types...) |
| 599 | return r |
| 600 | } |
| 601 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 602 | // NotModuleType adds type(s) that this rule does not apply to.. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 603 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 604 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 605 | return r |
| 606 | } |
| 607 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 608 | // With specifies property/value combinations that are restricted for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 609 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 610 | return r.WithMatcher(properties, selectMatcher(value)) |
| 611 | } |
| 612 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 613 | // WithMatcher specifies property/matcher combinations that are restricted for this rule. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 614 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 615 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 616 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 617 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 618 | }) |
| 619 | return r |
| 620 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 621 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 622 | // Without specifies property/value combinations that this rule does not apply to. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 623 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 624 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 625 | } |
| 626 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 627 | // Without specifies property/matcher combinations that this rule does not apply to. |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 628 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 629 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 630 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 631 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 632 | }) |
| 633 | return r |
| 634 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 635 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 636 | // DefinedInBpFile specifies that this rule applies to modules that are defined |
| 637 | // in bp files, and does not apply to modules that are auto generated by other modules. |
| 638 | func (r *rule) DefinedInBpFile() Rule { |
| 639 | r.definedInBp = true |
| 640 | return r |
| 641 | } |
| 642 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 643 | func selectMatcher(expected string) ValueMatcher { |
| 644 | if expected == "*" { |
| 645 | return anyMatcherInstance |
| 646 | } |
| 647 | return &equalMatcher{expected: expected} |
| 648 | } |
| 649 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 650 | // Because specifies a reason for this rule. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 651 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 652 | r.reason = reason |
| 653 | return r |
| 654 | } |
| 655 | |
| 656 | func (r *rule) String() string { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 657 | s := []string{"neverallow requirements. Not allowed:"} |
| 658 | if len(r.paths) > 0 { |
| 659 | s = append(s, fmt.Sprintf("in dirs: %q", r.paths)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 660 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 661 | if len(r.moduleTypes) > 0 { |
| 662 | s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 663 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 664 | if len(r.props) > 0 { |
| 665 | s = append(s, fmt.Sprintf("properties matching: %s", r.props)) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 666 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 667 | if len(r.directDeps) > 0 { |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 668 | s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps))) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 669 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 670 | if len(r.osClasses) > 0 { |
| 671 | s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses)) |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 672 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 673 | if len(r.unlessPaths) > 0 { |
| 674 | s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths)) |
| 675 | } |
| 676 | if len(r.unlessModuleTypes) > 0 { |
| 677 | s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes)) |
| 678 | } |
| 679 | if len(r.unlessProps) > 0 { |
| 680 | s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps)) |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 681 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 682 | if len(r.reason) != 0 { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 683 | s = append(s, " which is restricted because "+r.reason) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 684 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 685 | if len(s) == 1 { |
| 686 | s[0] = "neverallow requirements (empty)" |
| 687 | } |
| 688 | return strings.Join(s, "\n\t") |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 692 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 693 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 694 | return includePath && !excludePath |
| 695 | } |
| 696 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 697 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 698 | if len(r.directDeps) == 0 { |
| 699 | return true |
| 700 | } |
| 701 | |
| 702 | matches := false |
| 703 | ctx.VisitDirectDeps(func(m Module) { |
| 704 | if !matches { |
| 705 | name := ctx.OtherModuleName(m) |
| 706 | matches = r.directDeps[name] |
| 707 | } |
| 708 | }) |
| 709 | |
| 710 | return matches |
| 711 | } |
| 712 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 713 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 714 | if len(r.osClasses) == 0 { |
| 715 | return true |
| 716 | } |
| 717 | |
| 718 | for _, c := range r.osClasses { |
| 719 | if c == osClass { |
| 720 | return true |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return false |
| 725 | } |
| 726 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 727 | func (r *rule) appliesToModuleType(moduleType string) bool { |
Jihoon Kang | d546507 | 2024-12-03 00:53:05 +0000 | [diff] [blame] | 728 | // Remove prefix for auto-generated modules |
| 729 | moduleType = strings.TrimSuffix(moduleType, "__loadHookModule") |
| 730 | moduleType = strings.TrimSuffix(moduleType, "__bottomUpMutatorModule") |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 731 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 732 | } |
| 733 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 734 | func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []interface{}) bool { |
| 735 | includeProps := hasAllProperties(ctx, properties, r.props) |
| 736 | excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 737 | return includeProps && !excludeProps |
| 738 | } |
| 739 | |
Jihoon Kang | 2a7bf75 | 2024-11-01 21:21:25 +0000 | [diff] [blame] | 740 | func (r *rule) appliesToBpDefinedModule(ctx BottomUpMutatorContext) bool { |
| 741 | if !r.definedInBp { |
| 742 | return true |
| 743 | } |
| 744 | return !ctx.OtherModuleIsAutoGenerated(ctx.Module()) == r.definedInBp |
| 745 | } |
| 746 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 747 | func StartsWith(prefix string) ValueMatcher { |
| 748 | return &startsWithMatcher{prefix} |
| 749 | } |
| 750 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 751 | func Regexp(re string) ValueMatcher { |
| 752 | r, err := regexp.Compile(re) |
| 753 | if err != nil { |
| 754 | panic(err) |
| 755 | } |
| 756 | return ®exMatcher{r} |
| 757 | } |
| 758 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 759 | func NotInList(allowed []string) ValueMatcher { |
| 760 | return ¬InListMatcher{allowed} |
| 761 | } |
| 762 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 763 | // assorted utils |
| 764 | |
| 765 | func cleanPaths(paths []string) []string { |
| 766 | res := make([]string, len(paths)) |
| 767 | for i, v := range paths { |
| 768 | res[i] = filepath.Clean(v) + "/" |
| 769 | } |
| 770 | return res |
| 771 | } |
| 772 | |
| 773 | func fieldNamesForProperties(propertyNames string) []string { |
| 774 | names := strings.Split(propertyNames, ".") |
| 775 | for i, v := range names { |
| 776 | names[i] = proptools.FieldNameForProperty(v) |
| 777 | } |
| 778 | return names |
| 779 | } |
| 780 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 781 | func hasAnyProperty(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 782 | for _, v := range props { |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 783 | if hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 784 | return true |
| 785 | } |
| 786 | } |
| 787 | return false |
| 788 | } |
| 789 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 790 | func hasAllProperties(ctx BottomUpMutatorContext, properties []interface{}, props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 791 | for _, v := range props { |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 792 | if !hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 793 | return false |
| 794 | } |
| 795 | } |
| 796 | return true |
| 797 | } |
| 798 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 799 | func hasProperty(ctx BottomUpMutatorContext, properties []interface{}, prop ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 800 | for _, propertyStruct := range properties { |
| 801 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 802 | for _, v := range prop.fields { |
| 803 | if !propertiesValue.IsValid() { |
| 804 | break |
| 805 | } |
| 806 | propertiesValue = propertiesValue.FieldByName(v) |
| 807 | } |
| 808 | if !propertiesValue.IsValid() { |
| 809 | continue |
| 810 | } |
| 811 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 812 | check := func(value string) bool { |
Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 813 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 816 | if matchValue(ctx, propertiesValue, check) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 817 | return true |
| 818 | } |
| 819 | } |
| 820 | return false |
| 821 | } |
| 822 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 823 | func matchValue(ctx BottomUpMutatorContext, value reflect.Value, check func(string) bool) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 824 | if !value.IsValid() { |
| 825 | return false |
| 826 | } |
| 827 | |
| 828 | if value.Kind() == reflect.Ptr { |
| 829 | if value.IsNil() { |
| 830 | return check("") |
| 831 | } |
| 832 | value = value.Elem() |
| 833 | } |
| 834 | |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 835 | switch v := value.Interface().(type) { |
| 836 | case string: |
| 837 | return check(v) |
| 838 | case bool: |
| 839 | return check(strconv.FormatBool(v)) |
| 840 | case int: |
| 841 | return check(strconv.FormatInt((int64)(v), 10)) |
| 842 | case []string: |
| 843 | for _, v := range v { |
| 844 | if check(v) { |
| 845 | return true |
| 846 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 847 | } |
Cole Faust | 588f62f | 2024-08-27 15:47:06 -0700 | [diff] [blame] | 848 | return false |
| 849 | case proptools.Configurable[string]: |
| 850 | return check(v.GetOrDefault(ctx, "")) |
| 851 | case proptools.Configurable[bool]: |
| 852 | return check(strconv.FormatBool(v.GetOrDefault(ctx, false))) |
| 853 | case proptools.Configurable[[]string]: |
| 854 | for _, v := range v.GetOrDefault(ctx, nil) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 855 | if check(v) { |
| 856 | return true |
| 857 | } |
| 858 | } |
| 859 | return false |
| 860 | } |
| 861 | |
| 862 | panic("Can't handle type: " + value.Kind().String()) |
| 863 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 864 | |
| 865 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 866 | |
| 867 | func neverallowRules(config Config) []Rule { |
| 868 | return config.Once(neverallowRulesKey, func() interface{} { |
| 869 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 870 | return neverallows |
| 871 | }).([]Rule) |
| 872 | } |
| 873 | |
| 874 | // Overrides the default neverallow rules for the supplied config. |
| 875 | // |
| 876 | // For testing only. |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 877 | func setTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 878 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 879 | } |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 880 | |
| 881 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 882 | // |
| 883 | // If the supplied rules are nil then the default rules are used. |
| 884 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 885 | return GroupFixturePreparers( |
| 886 | FixtureModifyConfig(func(config Config) { |
| 887 | if testRules != nil { |
| 888 | setTestNeverallowRules(config, testRules) |
| 889 | } |
| 890 | }), |
| 891 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 892 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 893 | }), |
| 894 | ) |
| 895 | } |