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" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | // "neverallow" rules for the build system. |
| 27 | // |
| 28 | // This allows things which aren't related to the build system and are enforced |
| 29 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 30 | // straightforward away disjoint from implementations and tests which should |
| 31 | // work regardless of these restrictions. |
| 32 | // |
| 33 | // A module is disallowed if all of the following are true: |
| 34 | // - it is in one of the "in" paths |
| 35 | // - it is not in one of the "notIn" paths |
| 36 | // - it has all "with" properties matched |
| 37 | // - - values are matched in their entirety |
| 38 | // - - nil is interpreted as an empty string |
| 39 | // - - nested properties are separated with a '.' |
| 40 | // - - if the property is a list, any of the values in the list being matches |
| 41 | // counts as a match |
| 42 | // - it has none of the "without" properties matched (same rules as above) |
| 43 | |
| 44 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
| 45 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 46 | } |
| 47 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 48 | var neverallows = createNeverAllows() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 49 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 50 | func createNeverAllows() []*rule { |
| 51 | rules := []*rule{} |
| 52 | rules = append(rules, createTrebleRules()...) |
| 53 | rules = append(rules, createLibcoreRules()...) |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 54 | rules = append(rules, createJavaDeviceForHostRules()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 55 | return rules |
| 56 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 57 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 58 | func createTrebleRules() []*rule { |
| 59 | return []*rule{ |
| 60 | neverallow(). |
| 61 | in("vendor", "device"). |
| 62 | with("vndk.enabled", "true"). |
| 63 | without("vendor", "true"). |
| 64 | because("the VNDK can never contain a library that is device dependent."), |
| 65 | neverallow(). |
| 66 | with("vndk.enabled", "true"). |
| 67 | without("vendor", "true"). |
| 68 | without("owner", ""). |
| 69 | because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 70 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 71 | // TODO(b/67974785): always enforce the manifest |
| 72 | neverallow(). |
| 73 | without("name", "libhidltransport"). |
| 74 | with("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 75 | because("manifest enforcement should be independent of ."), |
| 76 | |
| 77 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
| 78 | neverallow(). |
| 79 | without("name", "libc_bionic_ndk"). |
| 80 | with("product_variables.treble_linker_namespaces.cflags", "*"). |
| 81 | because("nothing should care if linker namespaces are enabled or not"), |
| 82 | |
| 83 | // Example: |
| 84 | // *neverallow().with("Srcs", "main.cpp")) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func createLibcoreRules() []*rule { |
| 89 | var coreLibraryProjects = []string{ |
| 90 | "libcore", |
| 91 | "external/apache-harmony", |
| 92 | "external/apache-xml", |
| 93 | "external/bouncycastle", |
| 94 | "external/conscrypt", |
| 95 | "external/icu", |
| 96 | "external/okhttp", |
| 97 | "external/wycheproof", |
| 98 | } |
| 99 | |
Paul Duffin | ff5a177 | 2019-04-30 13:13:39 +0100 | [diff] [blame^] | 100 | // Core library constraints. The no_standard_libs can only be used in core |
| 101 | // library projects. Access to core library targets is restricted using |
| 102 | // visibility rules. |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 103 | rules := []*rule{ |
| 104 | neverallow(). |
| 105 | notIn(append(coreLibraryProjects, "development")...). |
| 106 | with("no_standard_libs", "true"), |
| 107 | } |
| 108 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 109 | return rules |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 112 | func createJavaDeviceForHostRules() []*rule { |
| 113 | javaDeviceForHostProjectsWhitelist := []string{ |
Colin Cross | 97add50 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 114 | "external/guava", |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 115 | "external/robolectric-shadows", |
| 116 | "framework/layoutlib", |
| 117 | } |
| 118 | |
| 119 | return []*rule{ |
| 120 | neverallow(). |
| 121 | notIn(javaDeviceForHostProjectsWhitelist...). |
| 122 | moduleType("java_device_for_host", "java_host_for_device"). |
| 123 | because("java_device_for_host can only be used in whitelisted projects"), |
| 124 | } |
| 125 | } |
| 126 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 127 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 128 | m, ok := ctx.Module().(Module) |
| 129 | if !ok { |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | dir := ctx.ModuleDir() + "/" |
| 134 | properties := m.GetProperties() |
| 135 | |
| 136 | for _, n := range neverallows { |
| 137 | if !n.appliesToPath(dir) { |
| 138 | continue |
| 139 | } |
| 140 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 141 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 142 | continue |
| 143 | } |
| 144 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 145 | if !n.appliesToProperties(properties) { |
| 146 | continue |
| 147 | } |
| 148 | |
| 149 | ctx.ModuleErrorf("violates " + n.String()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | type ruleProperty struct { |
| 154 | fields []string // e.x.: Vndk.Enabled |
| 155 | value string // e.x.: true |
| 156 | } |
| 157 | |
| 158 | type rule struct { |
| 159 | // User string for why this is a thing. |
| 160 | reason string |
| 161 | |
| 162 | paths []string |
| 163 | unlessPaths []string |
| 164 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 165 | moduleTypes []string |
| 166 | unlessModuleTypes []string |
| 167 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 168 | props []ruleProperty |
| 169 | unlessProps []ruleProperty |
| 170 | } |
| 171 | |
| 172 | func neverallow() *rule { |
| 173 | return &rule{} |
| 174 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 175 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 176 | func (r *rule) in(path ...string) *rule { |
| 177 | r.paths = append(r.paths, cleanPaths(path)...) |
| 178 | return r |
| 179 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 180 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 181 | func (r *rule) notIn(path ...string) *rule { |
| 182 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 183 | return r |
| 184 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 185 | |
| 186 | func (r *rule) moduleType(types ...string) *rule { |
| 187 | r.moduleTypes = append(r.moduleTypes, types...) |
| 188 | return r |
| 189 | } |
| 190 | |
| 191 | func (r *rule) notModuleType(types ...string) *rule { |
| 192 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 193 | return r |
| 194 | } |
| 195 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 196 | func (r *rule) with(properties, value string) *rule { |
| 197 | r.props = append(r.props, ruleProperty{ |
| 198 | fields: fieldNamesForProperties(properties), |
| 199 | value: value, |
| 200 | }) |
| 201 | return r |
| 202 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 203 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 204 | func (r *rule) without(properties, value string) *rule { |
| 205 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
| 206 | fields: fieldNamesForProperties(properties), |
| 207 | value: value, |
| 208 | }) |
| 209 | return r |
| 210 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 211 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 212 | func (r *rule) because(reason string) *rule { |
| 213 | r.reason = reason |
| 214 | return r |
| 215 | } |
| 216 | |
| 217 | func (r *rule) String() string { |
| 218 | s := "neverallow" |
| 219 | for _, v := range r.paths { |
| 220 | s += " dir:" + v + "*" |
| 221 | } |
| 222 | for _, v := range r.unlessPaths { |
| 223 | s += " -dir:" + v + "*" |
| 224 | } |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 225 | for _, v := range r.moduleTypes { |
| 226 | s += " type:" + v |
| 227 | } |
| 228 | for _, v := range r.unlessModuleTypes { |
| 229 | s += " -type:" + v |
| 230 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 231 | for _, v := range r.props { |
| 232 | s += " " + strings.Join(v.fields, ".") + "=" + v.value |
| 233 | } |
| 234 | for _, v := range r.unlessProps { |
| 235 | s += " -" + strings.Join(v.fields, ".") + "=" + v.value |
| 236 | } |
| 237 | if len(r.reason) != 0 { |
| 238 | s += " which is restricted because " + r.reason |
| 239 | } |
| 240 | return s |
| 241 | } |
| 242 | |
| 243 | func (r *rule) appliesToPath(dir string) bool { |
| 244 | includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths) |
| 245 | excludePath := hasAnyPrefix(dir, r.unlessPaths) |
| 246 | return includePath && !excludePath |
| 247 | } |
| 248 | |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 249 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 250 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 251 | } |
| 252 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 253 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 254 | includeProps := hasAllProperties(properties, r.props) |
| 255 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 256 | return includeProps && !excludeProps |
| 257 | } |
| 258 | |
| 259 | // assorted utils |
| 260 | |
| 261 | func cleanPaths(paths []string) []string { |
| 262 | res := make([]string, len(paths)) |
| 263 | for i, v := range paths { |
| 264 | res[i] = filepath.Clean(v) + "/" |
| 265 | } |
| 266 | return res |
| 267 | } |
| 268 | |
| 269 | func fieldNamesForProperties(propertyNames string) []string { |
| 270 | names := strings.Split(propertyNames, ".") |
| 271 | for i, v := range names { |
| 272 | names[i] = proptools.FieldNameForProperty(v) |
| 273 | } |
| 274 | return names |
| 275 | } |
| 276 | |
| 277 | func hasAnyPrefix(s string, prefixes []string) bool { |
| 278 | for _, prefix := range prefixes { |
| 279 | if strings.HasPrefix(s, prefix) { |
| 280 | return true |
| 281 | } |
| 282 | } |
| 283 | return false |
| 284 | } |
| 285 | |
| 286 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 287 | for _, v := range props { |
| 288 | if hasProperty(properties, v) { |
| 289 | return true |
| 290 | } |
| 291 | } |
| 292 | return false |
| 293 | } |
| 294 | |
| 295 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 296 | for _, v := range props { |
| 297 | if !hasProperty(properties, v) { |
| 298 | return false |
| 299 | } |
| 300 | } |
| 301 | return true |
| 302 | } |
| 303 | |
| 304 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 305 | for _, propertyStruct := range properties { |
| 306 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 307 | for _, v := range prop.fields { |
| 308 | if !propertiesValue.IsValid() { |
| 309 | break |
| 310 | } |
| 311 | propertiesValue = propertiesValue.FieldByName(v) |
| 312 | } |
| 313 | if !propertiesValue.IsValid() { |
| 314 | continue |
| 315 | } |
| 316 | |
| 317 | check := func(v string) bool { |
| 318 | return prop.value == "*" || prop.value == v |
| 319 | } |
| 320 | |
| 321 | if matchValue(propertiesValue, check) { |
| 322 | return true |
| 323 | } |
| 324 | } |
| 325 | return false |
| 326 | } |
| 327 | |
| 328 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 329 | if !value.IsValid() { |
| 330 | return false |
| 331 | } |
| 332 | |
| 333 | if value.Kind() == reflect.Ptr { |
| 334 | if value.IsNil() { |
| 335 | return check("") |
| 336 | } |
| 337 | value = value.Elem() |
| 338 | } |
| 339 | |
| 340 | switch value.Kind() { |
| 341 | case reflect.String: |
| 342 | return check(value.String()) |
| 343 | case reflect.Bool: |
| 344 | return check(strconv.FormatBool(value.Bool())) |
| 345 | case reflect.Int: |
| 346 | return check(strconv.FormatInt(value.Int(), 10)) |
| 347 | case reflect.Slice: |
| 348 | slice, ok := value.Interface().([]string) |
| 349 | if !ok { |
| 350 | panic("Can only handle slice of string") |
| 351 | } |
| 352 | for _, v := range slice { |
| 353 | if check(v) { |
| 354 | return true |
| 355 | } |
| 356 | } |
| 357 | return false |
| 358 | } |
| 359 | |
| 360 | panic("Can't handle type: " + value.Kind().String()) |
| 361 | } |