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 ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 20 | "regexp" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | // "neverallow" rules for the build system. |
| 28 | // |
| 29 | // This allows things which aren't related to the build system and are enforced |
| 30 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 31 | // straightforward away disjoint from implementations and tests which should |
| 32 | // work regardless of these restrictions. |
| 33 | // |
| 34 | // A module is disallowed if all of the following are true: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 35 | // - it is in one of the "In" paths |
| 36 | // - it is not in one of the "NotIn" paths |
| 37 | // - it has all "With" properties matched |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 38 | // - - values are matched in their entirety |
| 39 | // - - nil is interpreted as an empty string |
| 40 | // - - nested properties are separated with a '.' |
| 41 | // - - if the property is a list, any of the values in the list being matches |
| 42 | // counts as a match |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 43 | // - it has none of the "Without" properties matched (same rules as above) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 44 | |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 45 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 46 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 47 | } |
| 48 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 49 | var neverallows = []Rule{} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 50 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 51 | func init() { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 52 | AddNeverAllowRules(createIncludeDirsRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 53 | AddNeverAllowRules(createTrebleRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 54 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 55 | AddNeverAllowRules(createCcSdkVariantRules()...) |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 56 | AddNeverAllowRules(createUncompressDexRules()...) |
Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 57 | AddNeverAllowRules(createMakefileGoalRules()...) |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame^] | 58 | AddNeverAllowRules(createInitFirstStageRules()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 59 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 60 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 61 | // Add a NeverAllow rule to the set of rules to apply. |
| 62 | func AddNeverAllowRules(rules ...Rule) { |
| 63 | neverallows = append(neverallows, rules...) |
| 64 | } |
| 65 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 66 | func createIncludeDirsRules() []Rule { |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 67 | notInIncludeDir := []string{ |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 68 | "art", |
Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 69 | "art/libnativebridge", |
| 70 | "art/libnativeloader", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 71 | "libcore", |
| 72 | "libnativehelper", |
| 73 | "external/apache-harmony", |
| 74 | "external/apache-xml", |
| 75 | "external/boringssl", |
| 76 | "external/bouncycastle", |
| 77 | "external/conscrypt", |
| 78 | "external/icu", |
| 79 | "external/okhttp", |
| 80 | "external/vixl", |
| 81 | "external/wycheproof", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 82 | } |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 83 | noUseIncludeDir := []string{ |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 84 | "frameworks/av/apex", |
| 85 | "frameworks/av/tools", |
| 86 | "frameworks/native/cmds", |
| 87 | "system/apex", |
| 88 | "system/bpf", |
| 89 | "system/gatekeeper", |
| 90 | "system/hwservicemanager", |
| 91 | "system/libbase", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 92 | "system/libfmq", |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 93 | "system/libvintf", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 94 | } |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 95 | |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 96 | rules := make([]Rule, 0, len(notInIncludeDir)+len(noUseIncludeDir)) |
| 97 | |
| 98 | for _, path := range notInIncludeDir { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 99 | rule := |
| 100 | NeverAllow(). |
| 101 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 102 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 103 | " to use alternate mechanisms and so can no longer be used.") |
| 104 | |
| 105 | rules = append(rules, rule) |
| 106 | } |
| 107 | |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 108 | for _, path := range noUseIncludeDir { |
| 109 | rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). |
| 110 | Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + |
| 111 | " to use alternate mechanisms and so can no longer be used.") |
| 112 | rules = append(rules, rule) |
| 113 | } |
| 114 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 115 | return rules |
| 116 | } |
| 117 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 118 | func createTrebleRules() []Rule { |
| 119 | return []Rule{ |
| 120 | NeverAllow(). |
| 121 | In("vendor", "device"). |
| 122 | With("vndk.enabled", "true"). |
| 123 | Without("vendor", "true"). |
Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 124 | Without("product_specific", "true"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 125 | Because("the VNDK can never contain a library that is device dependent."), |
| 126 | NeverAllow(). |
| 127 | With("vndk.enabled", "true"). |
| 128 | Without("vendor", "true"). |
| 129 | Without("owner", ""). |
| 130 | Because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 131 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 132 | // TODO(b/67974785): always enforce the manifest |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 133 | NeverAllow(). |
Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 134 | Without("name", "libhidlbase-combined-impl"). |
| 135 | Without("name", "libhidlbase"). |
| 136 | Without("name", "libhidlbase_pgo"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 137 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 138 | Because("manifest enforcement should be independent of ."), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 139 | |
| 140 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 141 | NeverAllow(). |
| 142 | Without("name", "libc_bionic_ndk"). |
| 143 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 144 | Because("nothing should care if linker namespaces are enabled or not"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 145 | |
| 146 | // Example: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 147 | // *NeverAllow().with("Srcs", "main.cpp")) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 151 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 152 | javaDeviceForHostProjectsAllowedList := []string{ |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 153 | "external/guava", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 154 | "external/robolectric-shadows", |
| 155 | "framework/layoutlib", |
| 156 | } |
| 157 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 158 | return []Rule{ |
| 159 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 160 | NotIn(javaDeviceForHostProjectsAllowedList...). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 161 | ModuleType("java_device_for_host", "java_host_for_device"). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 162 | Because("java_device_for_host can only be used in allowed projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 166 | func createCcSdkVariantRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 167 | sdkVersionOnlyAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 168 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 169 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 170 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 171 | // 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] | 172 | "packages/modules/SdkExtensions/derive_sdk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 173 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 174 | "prebuilts/ndk", |
| 175 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 176 | "tools/test/graphicsbenchmark/functional_tests/java", |
Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 177 | "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 180 | platformVariantPropertiesAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 181 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 182 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 183 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 184 | // for the platform variant. Allow them to use the platform variant |
| 185 | // property to set shared_libs. |
| 186 | "prebuilts/ndk", |
| 187 | "frameworks/rs", |
| 188 | } |
| 189 | |
| 190 | return []Rule{ |
| 191 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 192 | NotIn(sdkVersionOnlyAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 193 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 194 | Because("sdk_variant_only can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 195 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 196 | NotIn(platformVariantPropertiesAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 197 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 198 | Because("platform variant properties can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 202 | func createUncompressDexRules() []Rule { |
| 203 | return []Rule{ |
| 204 | NeverAllow(). |
| 205 | NotIn("art"). |
| 206 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 207 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 208 | } |
| 209 | } |
| 210 | |
Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 211 | func createMakefileGoalRules() []Rule { |
| 212 | return []Rule{ |
| 213 | NeverAllow(). |
| 214 | ModuleType("makefile_goal"). |
| 215 | WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")). |
| 216 | Because("Only boot images may be imported as a makefile goal."), |
| 217 | } |
| 218 | } |
| 219 | |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame^] | 220 | func createInitFirstStageRules() []Rule { |
| 221 | return []Rule{ |
| 222 | NeverAllow(). |
| 223 | Without("name", "init_first_stage"). |
| 224 | With("install_in_root", "true"). |
| 225 | Because("install_in_root is only for init_first_stage."), |
| 226 | } |
| 227 | } |
| 228 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 229 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 230 | m, ok := ctx.Module().(Module) |
| 231 | if !ok { |
| 232 | return |
| 233 | } |
| 234 | |
| 235 | dir := ctx.ModuleDir() + "/" |
| 236 | properties := m.GetProperties() |
| 237 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 238 | osClass := ctx.Module().Target().Os.Class |
| 239 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 240 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 241 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 242 | if !n.appliesToPath(dir) { |
| 243 | continue |
| 244 | } |
| 245 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 246 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 247 | continue |
| 248 | } |
| 249 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 250 | if !n.appliesToProperties(properties) { |
| 251 | continue |
| 252 | } |
| 253 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 254 | if !n.appliesToOsClass(osClass) { |
| 255 | continue |
| 256 | } |
| 257 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 258 | if !n.appliesToDirectDeps(ctx) { |
| 259 | continue |
| 260 | } |
| 261 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 262 | if !n.appliesToBootclasspathJar(ctx) { |
| 263 | continue |
| 264 | } |
| 265 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 266 | ctx.ModuleErrorf("violates " + n.String()) |
| 267 | } |
| 268 | } |
| 269 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 270 | type ValueMatcher interface { |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 271 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 272 | String() string |
| 273 | } |
| 274 | |
| 275 | type equalMatcher struct { |
| 276 | expected string |
| 277 | } |
| 278 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 279 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 280 | return m.expected == value |
| 281 | } |
| 282 | |
| 283 | func (m *equalMatcher) String() string { |
| 284 | return "=" + m.expected |
| 285 | } |
| 286 | |
| 287 | type anyMatcher struct { |
| 288 | } |
| 289 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 290 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 291 | return true |
| 292 | } |
| 293 | |
| 294 | func (m *anyMatcher) String() string { |
| 295 | return "=*" |
| 296 | } |
| 297 | |
| 298 | var anyMatcherInstance = &anyMatcher{} |
| 299 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 300 | type startsWithMatcher struct { |
| 301 | prefix string |
| 302 | } |
| 303 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 304 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 305 | return strings.HasPrefix(value, m.prefix) |
| 306 | } |
| 307 | |
| 308 | func (m *startsWithMatcher) String() string { |
| 309 | return ".starts-with(" + m.prefix + ")" |
| 310 | } |
| 311 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 312 | type regexMatcher struct { |
| 313 | re *regexp.Regexp |
| 314 | } |
| 315 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 316 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 317 | return m.re.MatchString(value) |
| 318 | } |
| 319 | |
| 320 | func (m *regexMatcher) String() string { |
| 321 | return ".regexp(" + m.re.String() + ")" |
| 322 | } |
| 323 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 324 | type notInListMatcher struct { |
| 325 | allowed []string |
| 326 | } |
| 327 | |
| 328 | func (m *notInListMatcher) Test(value string) bool { |
| 329 | return !InList(value, m.allowed) |
| 330 | } |
| 331 | |
| 332 | func (m *notInListMatcher) String() string { |
| 333 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 334 | } |
| 335 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 336 | type isSetMatcher struct{} |
| 337 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 338 | func (m *isSetMatcher) Test(value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 339 | return value != "" |
| 340 | } |
| 341 | |
| 342 | func (m *isSetMatcher) String() string { |
| 343 | return ".is-set" |
| 344 | } |
| 345 | |
| 346 | var isSetMatcherInstance = &isSetMatcher{} |
| 347 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 348 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 349 | fields []string // e.x.: Vndk.Enabled |
| 350 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 353 | // A NeverAllow rule. |
| 354 | type Rule interface { |
| 355 | In(path ...string) Rule |
| 356 | |
| 357 | NotIn(path ...string) Rule |
| 358 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 359 | InDirectDeps(deps ...string) Rule |
| 360 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 361 | WithOsClass(osClasses ...OsClass) Rule |
| 362 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 363 | ModuleType(types ...string) Rule |
| 364 | |
| 365 | NotModuleType(types ...string) Rule |
| 366 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 367 | BootclasspathJar() Rule |
| 368 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 369 | With(properties, value string) Rule |
| 370 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 371 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 372 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 373 | Without(properties, value string) Rule |
| 374 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 375 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 376 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 377 | Because(reason string) Rule |
| 378 | } |
| 379 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 380 | type rule struct { |
| 381 | // User string for why this is a thing. |
| 382 | reason string |
| 383 | |
| 384 | paths []string |
| 385 | unlessPaths []string |
| 386 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 387 | directDeps map[string]bool |
| 388 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 389 | osClasses []OsClass |
| 390 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 391 | moduleTypes []string |
| 392 | unlessModuleTypes []string |
| 393 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 394 | props []ruleProperty |
| 395 | unlessProps []ruleProperty |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 396 | |
| 397 | onlyBootclasspathJar bool |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 400 | // Create a new NeverAllow rule. |
| 401 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 402 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 403 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 404 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 405 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 406 | r.paths = append(r.paths, cleanPaths(path)...) |
| 407 | return r |
| 408 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 409 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 410 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 411 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 412 | return r |
| 413 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 414 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 415 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 416 | for _, d := range deps { |
| 417 | r.directDeps[d] = true |
| 418 | } |
| 419 | return r |
| 420 | } |
| 421 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 422 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 423 | r.osClasses = append(r.osClasses, osClasses...) |
| 424 | return r |
| 425 | } |
| 426 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 427 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 428 | r.moduleTypes = append(r.moduleTypes, types...) |
| 429 | return r |
| 430 | } |
| 431 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 432 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 433 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 434 | return r |
| 435 | } |
| 436 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 437 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 438 | return r.WithMatcher(properties, selectMatcher(value)) |
| 439 | } |
| 440 | |
| 441 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 442 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 443 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 444 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 445 | }) |
| 446 | return r |
| 447 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 448 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 449 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 450 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 451 | } |
| 452 | |
| 453 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 454 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 455 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 456 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 457 | }) |
| 458 | return r |
| 459 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 460 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 461 | func selectMatcher(expected string) ValueMatcher { |
| 462 | if expected == "*" { |
| 463 | return anyMatcherInstance |
| 464 | } |
| 465 | return &equalMatcher{expected: expected} |
| 466 | } |
| 467 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 468 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 469 | r.reason = reason |
| 470 | return r |
| 471 | } |
| 472 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 473 | func (r *rule) BootclasspathJar() Rule { |
| 474 | r.onlyBootclasspathJar = true |
| 475 | return r |
| 476 | } |
| 477 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 478 | func (r *rule) String() string { |
| 479 | s := "neverallow" |
| 480 | for _, v := range r.paths { |
| 481 | s += " dir:" + v + "*" |
| 482 | } |
| 483 | for _, v := range r.unlessPaths { |
| 484 | s += " -dir:" + v + "*" |
| 485 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 486 | for _, v := range r.moduleTypes { |
| 487 | s += " type:" + v |
| 488 | } |
| 489 | for _, v := range r.unlessModuleTypes { |
| 490 | s += " -type:" + v |
| 491 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 492 | for _, v := range r.props { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 493 | s += " " + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 494 | } |
| 495 | for _, v := range r.unlessProps { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 496 | s += " -" + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 497 | } |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 498 | for k := range r.directDeps { |
| 499 | s += " deps:" + k |
| 500 | } |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 501 | for _, v := range r.osClasses { |
| 502 | s += " os:" + v.String() |
| 503 | } |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 504 | if r.onlyBootclasspathJar { |
| 505 | s += " inBcp" |
| 506 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 507 | if len(r.reason) != 0 { |
| 508 | s += " which is restricted because " + r.reason |
| 509 | } |
| 510 | return s |
| 511 | } |
| 512 | |
| 513 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 514 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 515 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 516 | return includePath && !excludePath |
| 517 | } |
| 518 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 519 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 520 | if len(r.directDeps) == 0 { |
| 521 | return true |
| 522 | } |
| 523 | |
| 524 | matches := false |
| 525 | ctx.VisitDirectDeps(func(m Module) { |
| 526 | if !matches { |
| 527 | name := ctx.OtherModuleName(m) |
| 528 | matches = r.directDeps[name] |
| 529 | } |
| 530 | }) |
| 531 | |
| 532 | return matches |
| 533 | } |
| 534 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 535 | func (r *rule) appliesToBootclasspathJar(ctx BottomUpMutatorContext) bool { |
| 536 | if !r.onlyBootclasspathJar { |
| 537 | return true |
| 538 | } |
| 539 | |
| 540 | return InList(ctx.ModuleName(), ctx.Config().BootJars()) |
| 541 | } |
| 542 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 543 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 544 | if len(r.osClasses) == 0 { |
| 545 | return true |
| 546 | } |
| 547 | |
| 548 | for _, c := range r.osClasses { |
| 549 | if c == osClass { |
| 550 | return true |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return false |
| 555 | } |
| 556 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 557 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 558 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 559 | } |
| 560 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 561 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 562 | includeProps := hasAllProperties(properties, r.props) |
| 563 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 564 | return includeProps && !excludeProps |
| 565 | } |
| 566 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 567 | func StartsWith(prefix string) ValueMatcher { |
| 568 | return &startsWithMatcher{prefix} |
| 569 | } |
| 570 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 571 | func Regexp(re string) ValueMatcher { |
| 572 | r, err := regexp.Compile(re) |
| 573 | if err != nil { |
| 574 | panic(err) |
| 575 | } |
| 576 | return ®exMatcher{r} |
| 577 | } |
| 578 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 579 | func NotInList(allowed []string) ValueMatcher { |
| 580 | return ¬InListMatcher{allowed} |
| 581 | } |
| 582 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 583 | // assorted utils |
| 584 | |
| 585 | func cleanPaths(paths []string) []string { |
| 586 | res := make([]string, len(paths)) |
| 587 | for i, v := range paths { |
| 588 | res[i] = filepath.Clean(v) + "/" |
| 589 | } |
| 590 | return res |
| 591 | } |
| 592 | |
| 593 | func fieldNamesForProperties(propertyNames string) []string { |
| 594 | names := strings.Split(propertyNames, ".") |
| 595 | for i, v := range names { |
| 596 | names[i] = proptools.FieldNameForProperty(v) |
| 597 | } |
| 598 | return names |
| 599 | } |
| 600 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 601 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 602 | for _, v := range props { |
| 603 | if hasProperty(properties, v) { |
| 604 | return true |
| 605 | } |
| 606 | } |
| 607 | return false |
| 608 | } |
| 609 | |
| 610 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 611 | for _, v := range props { |
| 612 | if !hasProperty(properties, v) { |
| 613 | return false |
| 614 | } |
| 615 | } |
| 616 | return true |
| 617 | } |
| 618 | |
| 619 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 620 | for _, propertyStruct := range properties { |
| 621 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 622 | for _, v := range prop.fields { |
| 623 | if !propertiesValue.IsValid() { |
| 624 | break |
| 625 | } |
| 626 | propertiesValue = propertiesValue.FieldByName(v) |
| 627 | } |
| 628 | if !propertiesValue.IsValid() { |
| 629 | continue |
| 630 | } |
| 631 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 632 | check := func(value string) bool { |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 633 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if matchValue(propertiesValue, check) { |
| 637 | return true |
| 638 | } |
| 639 | } |
| 640 | return false |
| 641 | } |
| 642 | |
| 643 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 644 | if !value.IsValid() { |
| 645 | return false |
| 646 | } |
| 647 | |
| 648 | if value.Kind() == reflect.Ptr { |
| 649 | if value.IsNil() { |
| 650 | return check("") |
| 651 | } |
| 652 | value = value.Elem() |
| 653 | } |
| 654 | |
| 655 | switch value.Kind() { |
| 656 | case reflect.String: |
| 657 | return check(value.String()) |
| 658 | case reflect.Bool: |
| 659 | return check(strconv.FormatBool(value.Bool())) |
| 660 | case reflect.Int: |
| 661 | return check(strconv.FormatInt(value.Int(), 10)) |
| 662 | case reflect.Slice: |
| 663 | slice, ok := value.Interface().([]string) |
| 664 | if !ok { |
| 665 | panic("Can only handle slice of string") |
| 666 | } |
| 667 | for _, v := range slice { |
| 668 | if check(v) { |
| 669 | return true |
| 670 | } |
| 671 | } |
| 672 | return false |
| 673 | } |
| 674 | |
| 675 | panic("Can't handle type: " + value.Kind().String()) |
| 676 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 677 | |
| 678 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 679 | |
| 680 | func neverallowRules(config Config) []Rule { |
| 681 | return config.Once(neverallowRulesKey, func() interface{} { |
| 682 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 683 | return neverallows |
| 684 | }).([]Rule) |
| 685 | } |
| 686 | |
| 687 | // Overrides the default neverallow rules for the supplied config. |
| 688 | // |
| 689 | // For testing only. |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 690 | func setTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 691 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 692 | } |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 693 | |
| 694 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 695 | // |
| 696 | // If the supplied rules are nil then the default rules are used. |
| 697 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 698 | return GroupFixturePreparers( |
| 699 | FixtureModifyConfig(func(config Config) { |
| 700 | if testRules != nil { |
| 701 | setTestNeverallowRules(config, testRules) |
| 702 | } |
| 703 | }), |
| 704 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 705 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 706 | }), |
| 707 | ) |
| 708 | } |