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