blob: 0cb20296c097082e2aedb5953b1d34fd781ba2e0 [file] [log] [blame]
Steven Moreland65b3fd92017-12-06 14:18:35 -08001// 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
15package android
16
17import (
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:
Paul Duffin730f2a52019-06-27 14:08:51 +010034// - 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
Steven Moreland65b3fd92017-12-06 14:18:35 -080037// - - 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
Paul Duffin730f2a52019-06-27 14:08:51 +010042// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080043
44func registerNeverallowMutator(ctx RegisterMutatorsContext) {
45 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
46}
47
Paul Duffin730f2a52019-06-27 14:08:51 +010048var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080049
Paul Duffin730f2a52019-06-27 14:08:51 +010050func init() {
Paul Duffinc8111702019-07-22 12:13:55 +010051 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010052 AddNeverAllowRules(createTrebleRules()...)
53 AddNeverAllowRules(createLibcoreRules()...)
54 AddNeverAllowRules(createMediaRules()...)
55 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010056}
Steven Moreland65b3fd92017-12-06 14:18:35 -080057
Paul Duffin730f2a52019-06-27 14:08:51 +010058// Add a NeverAllow rule to the set of rules to apply.
59func AddNeverAllowRules(rules ...Rule) {
60 neverallows = append(neverallows, rules...)
61}
62
Paul Duffinc8111702019-07-22 12:13:55 +010063func createIncludeDirsRules() []Rule {
64 // The list of paths that cannot be referenced using include_dirs
65 paths := []string{
66 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000067 "art/libnativebridge",
68 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010069 "libcore",
70 "libnativehelper",
71 "external/apache-harmony",
72 "external/apache-xml",
73 "external/boringssl",
74 "external/bouncycastle",
75 "external/conscrypt",
76 "external/icu",
77 "external/okhttp",
78 "external/vixl",
79 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010080 }
81
82 // Create a composite matcher that will match if the value starts with any of the restricted
83 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
84 // XY.
85 rules := make([]Rule, 0, len(paths))
86 for _, path := range paths {
87 rule :=
88 NeverAllow().
89 WithMatcher("include_dirs", StartsWith(path+"/")).
90 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
91 " to use alternate mechanisms and so can no longer be used.")
92
93 rules = append(rules, rule)
94 }
95
96 return rules
97}
98
Paul Duffin730f2a52019-06-27 14:08:51 +010099func createTrebleRules() []Rule {
100 return []Rule{
101 NeverAllow().
102 In("vendor", "device").
103 With("vndk.enabled", "true").
104 Without("vendor", "true").
105 Because("the VNDK can never contain a library that is device dependent."),
106 NeverAllow().
107 With("vndk.enabled", "true").
108 Without("vendor", "true").
109 Without("owner", "").
110 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800111
Neil Fullerdf5f3562018-10-21 17:19:10 +0100112 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100113 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800114 Without("name", "libhidlbase-combined-impl").
115 Without("name", "libhidlbase").
116 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100117 With("product_variables.enforce_vintf_manifest.cflags", "*").
118 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100119
120 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100121 NeverAllow().
122 Without("name", "libc_bionic_ndk").
123 With("product_variables.treble_linker_namespaces.cflags", "*").
124 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100125
126 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100127 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100128 }
129}
130
Paul Duffin730f2a52019-06-27 14:08:51 +0100131func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100132 var coreLibraryProjects = []string{
133 "libcore",
134 "external/apache-harmony",
135 "external/apache-xml",
136 "external/bouncycastle",
137 "external/conscrypt",
138 "external/icu",
139 "external/okhttp",
140 "external/wycheproof",
141 }
142
Paul Duffina3d09862019-06-11 13:40:47 +0100143 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
144 // Access to core library targets is restricted using visibility rules.
Paul Duffin730f2a52019-06-27 14:08:51 +0100145 rules := []Rule{
146 NeverAllow().
147 NotIn(coreLibraryProjects...).
148 With("sdk_version", "none"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100149 }
150
Neil Fullerdf5f3562018-10-21 17:19:10 +0100151 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800152}
153
Paul Duffin730f2a52019-06-27 14:08:51 +0100154func createMediaRules() []Rule {
155 return []Rule{
156 NeverAllow().
157 With("libs", "updatable-media").
158 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800159 }
160}
161
Paul Duffin730f2a52019-06-27 14:08:51 +0100162func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800163 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700164 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800165 "external/robolectric-shadows",
166 "framework/layoutlib",
167 }
168
Paul Duffin730f2a52019-06-27 14:08:51 +0100169 return []Rule{
170 NeverAllow().
171 NotIn(javaDeviceForHostProjectsWhitelist...).
172 ModuleType("java_device_for_host", "java_host_for_device").
173 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800174 }
175}
176
Steven Moreland65b3fd92017-12-06 14:18:35 -0800177func neverallowMutator(ctx BottomUpMutatorContext) {
178 m, ok := ctx.Module().(Module)
179 if !ok {
180 return
181 }
182
183 dir := ctx.ModuleDir() + "/"
184 properties := m.GetProperties()
185
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100186 osClass := ctx.Module().Target().Os.Class
187
Paul Duffin115445b2019-08-07 15:31:07 +0100188 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100189 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800190 if !n.appliesToPath(dir) {
191 continue
192 }
193
Colin Crossfd4f7432019-03-05 15:06:16 -0800194 if !n.appliesToModuleType(ctx.ModuleType()) {
195 continue
196 }
197
Steven Moreland65b3fd92017-12-06 14:18:35 -0800198 if !n.appliesToProperties(properties) {
199 continue
200 }
201
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100202 if !n.appliesToOsClass(osClass) {
203 continue
204 }
205
Paul Duffin35781882019-07-25 15:41:09 +0100206 if !n.appliesToDirectDeps(ctx) {
207 continue
208 }
209
Steven Moreland65b3fd92017-12-06 14:18:35 -0800210 ctx.ModuleErrorf("violates " + n.String())
211 }
212}
213
Paul Duffin73bf0542019-07-12 14:12:49 +0100214type ValueMatcher interface {
215 test(string) bool
216 String() string
217}
218
219type equalMatcher struct {
220 expected string
221}
222
223func (m *equalMatcher) test(value string) bool {
224 return m.expected == value
225}
226
227func (m *equalMatcher) String() string {
228 return "=" + m.expected
229}
230
231type anyMatcher struct {
232}
233
234func (m *anyMatcher) test(value string) bool {
235 return true
236}
237
238func (m *anyMatcher) String() string {
239 return "=*"
240}
241
242var anyMatcherInstance = &anyMatcher{}
243
Paul Duffinc8111702019-07-22 12:13:55 +0100244type startsWithMatcher struct {
245 prefix string
246}
247
248func (m *startsWithMatcher) test(value string) bool {
249 return strings.HasPrefix(value, m.prefix)
250}
251
252func (m *startsWithMatcher) String() string {
253 return ".starts-with(" + m.prefix + ")"
254}
255
Steven Moreland65b3fd92017-12-06 14:18:35 -0800256type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100257 fields []string // e.x.: Vndk.Enabled
258 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800259}
260
Paul Duffin730f2a52019-06-27 14:08:51 +0100261// A NeverAllow rule.
262type Rule interface {
263 In(path ...string) Rule
264
265 NotIn(path ...string) Rule
266
Paul Duffin35781882019-07-25 15:41:09 +0100267 InDirectDeps(deps ...string) Rule
268
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100269 WithOsClass(osClasses ...OsClass) Rule
270
Paul Duffin730f2a52019-06-27 14:08:51 +0100271 ModuleType(types ...string) Rule
272
273 NotModuleType(types ...string) Rule
274
275 With(properties, value string) Rule
276
Paul Duffinc8111702019-07-22 12:13:55 +0100277 WithMatcher(properties string, matcher ValueMatcher) Rule
278
Paul Duffin730f2a52019-06-27 14:08:51 +0100279 Without(properties, value string) Rule
280
Paul Duffinc8111702019-07-22 12:13:55 +0100281 WithoutMatcher(properties string, matcher ValueMatcher) Rule
282
Paul Duffin730f2a52019-06-27 14:08:51 +0100283 Because(reason string) Rule
284}
285
Steven Moreland65b3fd92017-12-06 14:18:35 -0800286type rule struct {
287 // User string for why this is a thing.
288 reason string
289
290 paths []string
291 unlessPaths []string
292
Paul Duffin35781882019-07-25 15:41:09 +0100293 directDeps map[string]bool
294
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100295 osClasses []OsClass
296
Colin Crossfd4f7432019-03-05 15:06:16 -0800297 moduleTypes []string
298 unlessModuleTypes []string
299
Steven Moreland65b3fd92017-12-06 14:18:35 -0800300 props []ruleProperty
301 unlessProps []ruleProperty
302}
303
Paul Duffin730f2a52019-06-27 14:08:51 +0100304// Create a new NeverAllow rule.
305func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100306 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800307}
Colin Crossfd4f7432019-03-05 15:06:16 -0800308
Paul Duffin730f2a52019-06-27 14:08:51 +0100309func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800310 r.paths = append(r.paths, cleanPaths(path)...)
311 return r
312}
Colin Crossfd4f7432019-03-05 15:06:16 -0800313
Paul Duffin730f2a52019-06-27 14:08:51 +0100314func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800315 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
316 return r
317}
Colin Crossfd4f7432019-03-05 15:06:16 -0800318
Paul Duffin35781882019-07-25 15:41:09 +0100319func (r *rule) InDirectDeps(deps ...string) Rule {
320 for _, d := range deps {
321 r.directDeps[d] = true
322 }
323 return r
324}
325
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100326func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
327 r.osClasses = append(r.osClasses, osClasses...)
328 return r
329}
330
Paul Duffin730f2a52019-06-27 14:08:51 +0100331func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800332 r.moduleTypes = append(r.moduleTypes, types...)
333 return r
334}
335
Paul Duffin730f2a52019-06-27 14:08:51 +0100336func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800337 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
338 return r
339}
340
Paul Duffin730f2a52019-06-27 14:08:51 +0100341func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100342 return r.WithMatcher(properties, selectMatcher(value))
343}
344
345func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800346 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100347 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100348 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800349 })
350 return r
351}
Colin Crossfd4f7432019-03-05 15:06:16 -0800352
Paul Duffin730f2a52019-06-27 14:08:51 +0100353func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100354 return r.WithoutMatcher(properties, selectMatcher(value))
355}
356
357func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800358 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100359 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100360 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800361 })
362 return r
363}
Colin Crossfd4f7432019-03-05 15:06:16 -0800364
Paul Duffin73bf0542019-07-12 14:12:49 +0100365func selectMatcher(expected string) ValueMatcher {
366 if expected == "*" {
367 return anyMatcherInstance
368 }
369 return &equalMatcher{expected: expected}
370}
371
Paul Duffin730f2a52019-06-27 14:08:51 +0100372func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800373 r.reason = reason
374 return r
375}
376
377func (r *rule) String() string {
378 s := "neverallow"
379 for _, v := range r.paths {
380 s += " dir:" + v + "*"
381 }
382 for _, v := range r.unlessPaths {
383 s += " -dir:" + v + "*"
384 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800385 for _, v := range r.moduleTypes {
386 s += " type:" + v
387 }
388 for _, v := range r.unlessModuleTypes {
389 s += " -type:" + v
390 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800391 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100392 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800393 }
394 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100395 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800396 }
Paul Duffin35781882019-07-25 15:41:09 +0100397 for k := range r.directDeps {
398 s += " deps:" + k
399 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100400 for _, v := range r.osClasses {
401 s += " os:" + v.String()
402 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800403 if len(r.reason) != 0 {
404 s += " which is restricted because " + r.reason
405 }
406 return s
407}
408
409func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800410 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
411 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800412 return includePath && !excludePath
413}
414
Paul Duffin35781882019-07-25 15:41:09 +0100415func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
416 if len(r.directDeps) == 0 {
417 return true
418 }
419
420 matches := false
421 ctx.VisitDirectDeps(func(m Module) {
422 if !matches {
423 name := ctx.OtherModuleName(m)
424 matches = r.directDeps[name]
425 }
426 })
427
428 return matches
429}
430
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100431func (r *rule) appliesToOsClass(osClass OsClass) bool {
432 if len(r.osClasses) == 0 {
433 return true
434 }
435
436 for _, c := range r.osClasses {
437 if c == osClass {
438 return true
439 }
440 }
441
442 return false
443}
444
Colin Crossfd4f7432019-03-05 15:06:16 -0800445func (r *rule) appliesToModuleType(moduleType string) bool {
446 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
447}
448
Steven Moreland65b3fd92017-12-06 14:18:35 -0800449func (r *rule) appliesToProperties(properties []interface{}) bool {
450 includeProps := hasAllProperties(properties, r.props)
451 excludeProps := hasAnyProperty(properties, r.unlessProps)
452 return includeProps && !excludeProps
453}
454
Paul Duffinc8111702019-07-22 12:13:55 +0100455func StartsWith(prefix string) ValueMatcher {
456 return &startsWithMatcher{prefix}
457}
458
Steven Moreland65b3fd92017-12-06 14:18:35 -0800459// assorted utils
460
461func cleanPaths(paths []string) []string {
462 res := make([]string, len(paths))
463 for i, v := range paths {
464 res[i] = filepath.Clean(v) + "/"
465 }
466 return res
467}
468
469func fieldNamesForProperties(propertyNames string) []string {
470 names := strings.Split(propertyNames, ".")
471 for i, v := range names {
472 names[i] = proptools.FieldNameForProperty(v)
473 }
474 return names
475}
476
Steven Moreland65b3fd92017-12-06 14:18:35 -0800477func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
478 for _, v := range props {
479 if hasProperty(properties, v) {
480 return true
481 }
482 }
483 return false
484}
485
486func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
487 for _, v := range props {
488 if !hasProperty(properties, v) {
489 return false
490 }
491 }
492 return true
493}
494
495func hasProperty(properties []interface{}, prop ruleProperty) bool {
496 for _, propertyStruct := range properties {
497 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
498 for _, v := range prop.fields {
499 if !propertiesValue.IsValid() {
500 break
501 }
502 propertiesValue = propertiesValue.FieldByName(v)
503 }
504 if !propertiesValue.IsValid() {
505 continue
506 }
507
Paul Duffin73bf0542019-07-12 14:12:49 +0100508 check := func(value string) bool {
509 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800510 }
511
512 if matchValue(propertiesValue, check) {
513 return true
514 }
515 }
516 return false
517}
518
519func matchValue(value reflect.Value, check func(string) bool) bool {
520 if !value.IsValid() {
521 return false
522 }
523
524 if value.Kind() == reflect.Ptr {
525 if value.IsNil() {
526 return check("")
527 }
528 value = value.Elem()
529 }
530
531 switch value.Kind() {
532 case reflect.String:
533 return check(value.String())
534 case reflect.Bool:
535 return check(strconv.FormatBool(value.Bool()))
536 case reflect.Int:
537 return check(strconv.FormatInt(value.Int(), 10))
538 case reflect.Slice:
539 slice, ok := value.Interface().([]string)
540 if !ok {
541 panic("Can only handle slice of string")
542 }
543 for _, v := range slice {
544 if check(v) {
545 return true
546 }
547 }
548 return false
549 }
550
551 panic("Can't handle type: " + value.Kind().String())
552}
Paul Duffin115445b2019-08-07 15:31:07 +0100553
554var neverallowRulesKey = NewOnceKey("neverallowRules")
555
556func neverallowRules(config Config) []Rule {
557 return config.Once(neverallowRulesKey, func() interface{} {
558 // No test rules were set by setTestNeverallowRules, use the global rules
559 return neverallows
560 }).([]Rule)
561}
562
563// Overrides the default neverallow rules for the supplied config.
564//
565// For testing only.
566func setTestNeverallowRules(config Config, testRules []Rule) {
567 config.Once(neverallowRulesKey, func() interface{} { return testRules })
568}