blob: 8fcfb8a6e79b34094fc33ca493724db25793b86f [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").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900105 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100106 Because("the VNDK can never contain a library that is device dependent."),
107 NeverAllow().
108 With("vndk.enabled", "true").
109 Without("vendor", "true").
110 Without("owner", "").
111 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800112
Neil Fullerdf5f3562018-10-21 17:19:10 +0100113 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100114 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800115 Without("name", "libhidlbase-combined-impl").
116 Without("name", "libhidlbase").
117 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100118 With("product_variables.enforce_vintf_manifest.cflags", "*").
119 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100120
121 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100122 NeverAllow().
123 Without("name", "libc_bionic_ndk").
124 With("product_variables.treble_linker_namespaces.cflags", "*").
125 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100126
127 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100128 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100129 }
130}
131
Paul Duffin730f2a52019-06-27 14:08:51 +0100132func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100133 var coreLibraryProjects = []string{
134 "libcore",
135 "external/apache-harmony",
136 "external/apache-xml",
137 "external/bouncycastle",
138 "external/conscrypt",
139 "external/icu",
140 "external/okhttp",
141 "external/wycheproof",
142 }
143
Paul Duffina3d09862019-06-11 13:40:47 +0100144 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
145 // Access to core library targets is restricted using visibility rules.
Paul Duffin730f2a52019-06-27 14:08:51 +0100146 rules := []Rule{
147 NeverAllow().
148 NotIn(coreLibraryProjects...).
149 With("sdk_version", "none"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100150 }
151
Neil Fullerdf5f3562018-10-21 17:19:10 +0100152 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800153}
154
Paul Duffin730f2a52019-06-27 14:08:51 +0100155func createMediaRules() []Rule {
156 return []Rule{
157 NeverAllow().
158 With("libs", "updatable-media").
159 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800160 }
161}
162
Paul Duffin730f2a52019-06-27 14:08:51 +0100163func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800164 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700165 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800166 "external/robolectric-shadows",
167 "framework/layoutlib",
168 }
169
Paul Duffin730f2a52019-06-27 14:08:51 +0100170 return []Rule{
171 NeverAllow().
172 NotIn(javaDeviceForHostProjectsWhitelist...).
173 ModuleType("java_device_for_host", "java_host_for_device").
174 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800175 }
176}
177
Steven Moreland65b3fd92017-12-06 14:18:35 -0800178func neverallowMutator(ctx BottomUpMutatorContext) {
179 m, ok := ctx.Module().(Module)
180 if !ok {
181 return
182 }
183
184 dir := ctx.ModuleDir() + "/"
185 properties := m.GetProperties()
186
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100187 osClass := ctx.Module().Target().Os.Class
188
Paul Duffin115445b2019-08-07 15:31:07 +0100189 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100190 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800191 if !n.appliesToPath(dir) {
192 continue
193 }
194
Colin Crossfd4f7432019-03-05 15:06:16 -0800195 if !n.appliesToModuleType(ctx.ModuleType()) {
196 continue
197 }
198
Steven Moreland65b3fd92017-12-06 14:18:35 -0800199 if !n.appliesToProperties(properties) {
200 continue
201 }
202
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100203 if !n.appliesToOsClass(osClass) {
204 continue
205 }
206
Paul Duffin35781882019-07-25 15:41:09 +0100207 if !n.appliesToDirectDeps(ctx) {
208 continue
209 }
210
Steven Moreland65b3fd92017-12-06 14:18:35 -0800211 ctx.ModuleErrorf("violates " + n.String())
212 }
213}
214
Paul Duffin73bf0542019-07-12 14:12:49 +0100215type ValueMatcher interface {
216 test(string) bool
217 String() string
218}
219
220type equalMatcher struct {
221 expected string
222}
223
224func (m *equalMatcher) test(value string) bool {
225 return m.expected == value
226}
227
228func (m *equalMatcher) String() string {
229 return "=" + m.expected
230}
231
232type anyMatcher struct {
233}
234
235func (m *anyMatcher) test(value string) bool {
236 return true
237}
238
239func (m *anyMatcher) String() string {
240 return "=*"
241}
242
243var anyMatcherInstance = &anyMatcher{}
244
Paul Duffinc8111702019-07-22 12:13:55 +0100245type startsWithMatcher struct {
246 prefix string
247}
248
249func (m *startsWithMatcher) test(value string) bool {
250 return strings.HasPrefix(value, m.prefix)
251}
252
253func (m *startsWithMatcher) String() string {
254 return ".starts-with(" + m.prefix + ")"
255}
256
Steven Moreland65b3fd92017-12-06 14:18:35 -0800257type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100258 fields []string // e.x.: Vndk.Enabled
259 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800260}
261
Paul Duffin730f2a52019-06-27 14:08:51 +0100262// A NeverAllow rule.
263type Rule interface {
264 In(path ...string) Rule
265
266 NotIn(path ...string) Rule
267
Paul Duffin35781882019-07-25 15:41:09 +0100268 InDirectDeps(deps ...string) Rule
269
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100270 WithOsClass(osClasses ...OsClass) Rule
271
Paul Duffin730f2a52019-06-27 14:08:51 +0100272 ModuleType(types ...string) Rule
273
274 NotModuleType(types ...string) Rule
275
276 With(properties, value string) Rule
277
Paul Duffinc8111702019-07-22 12:13:55 +0100278 WithMatcher(properties string, matcher ValueMatcher) Rule
279
Paul Duffin730f2a52019-06-27 14:08:51 +0100280 Without(properties, value string) Rule
281
Paul Duffinc8111702019-07-22 12:13:55 +0100282 WithoutMatcher(properties string, matcher ValueMatcher) Rule
283
Paul Duffin730f2a52019-06-27 14:08:51 +0100284 Because(reason string) Rule
285}
286
Steven Moreland65b3fd92017-12-06 14:18:35 -0800287type rule struct {
288 // User string for why this is a thing.
289 reason string
290
291 paths []string
292 unlessPaths []string
293
Paul Duffin35781882019-07-25 15:41:09 +0100294 directDeps map[string]bool
295
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100296 osClasses []OsClass
297
Colin Crossfd4f7432019-03-05 15:06:16 -0800298 moduleTypes []string
299 unlessModuleTypes []string
300
Steven Moreland65b3fd92017-12-06 14:18:35 -0800301 props []ruleProperty
302 unlessProps []ruleProperty
303}
304
Paul Duffin730f2a52019-06-27 14:08:51 +0100305// Create a new NeverAllow rule.
306func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100307 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800308}
Colin Crossfd4f7432019-03-05 15:06:16 -0800309
Paul Duffin730f2a52019-06-27 14:08:51 +0100310func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800311 r.paths = append(r.paths, cleanPaths(path)...)
312 return r
313}
Colin Crossfd4f7432019-03-05 15:06:16 -0800314
Paul Duffin730f2a52019-06-27 14:08:51 +0100315func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800316 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
317 return r
318}
Colin Crossfd4f7432019-03-05 15:06:16 -0800319
Paul Duffin35781882019-07-25 15:41:09 +0100320func (r *rule) InDirectDeps(deps ...string) Rule {
321 for _, d := range deps {
322 r.directDeps[d] = true
323 }
324 return r
325}
326
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100327func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
328 r.osClasses = append(r.osClasses, osClasses...)
329 return r
330}
331
Paul Duffin730f2a52019-06-27 14:08:51 +0100332func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800333 r.moduleTypes = append(r.moduleTypes, types...)
334 return r
335}
336
Paul Duffin730f2a52019-06-27 14:08:51 +0100337func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800338 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
339 return r
340}
341
Paul Duffin730f2a52019-06-27 14:08:51 +0100342func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100343 return r.WithMatcher(properties, selectMatcher(value))
344}
345
346func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800347 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100348 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100349 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800350 })
351 return r
352}
Colin Crossfd4f7432019-03-05 15:06:16 -0800353
Paul Duffin730f2a52019-06-27 14:08:51 +0100354func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100355 return r.WithoutMatcher(properties, selectMatcher(value))
356}
357
358func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800359 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100360 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100361 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800362 })
363 return r
364}
Colin Crossfd4f7432019-03-05 15:06:16 -0800365
Paul Duffin73bf0542019-07-12 14:12:49 +0100366func selectMatcher(expected string) ValueMatcher {
367 if expected == "*" {
368 return anyMatcherInstance
369 }
370 return &equalMatcher{expected: expected}
371}
372
Paul Duffin730f2a52019-06-27 14:08:51 +0100373func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800374 r.reason = reason
375 return r
376}
377
378func (r *rule) String() string {
379 s := "neverallow"
380 for _, v := range r.paths {
381 s += " dir:" + v + "*"
382 }
383 for _, v := range r.unlessPaths {
384 s += " -dir:" + v + "*"
385 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800386 for _, v := range r.moduleTypes {
387 s += " type:" + v
388 }
389 for _, v := range r.unlessModuleTypes {
390 s += " -type:" + v
391 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800392 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100393 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800394 }
395 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100396 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800397 }
Paul Duffin35781882019-07-25 15:41:09 +0100398 for k := range r.directDeps {
399 s += " deps:" + k
400 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100401 for _, v := range r.osClasses {
402 s += " os:" + v.String()
403 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800404 if len(r.reason) != 0 {
405 s += " which is restricted because " + r.reason
406 }
407 return s
408}
409
410func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800411 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
412 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800413 return includePath && !excludePath
414}
415
Paul Duffin35781882019-07-25 15:41:09 +0100416func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
417 if len(r.directDeps) == 0 {
418 return true
419 }
420
421 matches := false
422 ctx.VisitDirectDeps(func(m Module) {
423 if !matches {
424 name := ctx.OtherModuleName(m)
425 matches = r.directDeps[name]
426 }
427 })
428
429 return matches
430}
431
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100432func (r *rule) appliesToOsClass(osClass OsClass) bool {
433 if len(r.osClasses) == 0 {
434 return true
435 }
436
437 for _, c := range r.osClasses {
438 if c == osClass {
439 return true
440 }
441 }
442
443 return false
444}
445
Colin Crossfd4f7432019-03-05 15:06:16 -0800446func (r *rule) appliesToModuleType(moduleType string) bool {
447 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
448}
449
Steven Moreland65b3fd92017-12-06 14:18:35 -0800450func (r *rule) appliesToProperties(properties []interface{}) bool {
451 includeProps := hasAllProperties(properties, r.props)
452 excludeProps := hasAnyProperty(properties, r.unlessProps)
453 return includeProps && !excludeProps
454}
455
Paul Duffinc8111702019-07-22 12:13:55 +0100456func StartsWith(prefix string) ValueMatcher {
457 return &startsWithMatcher{prefix}
458}
459
Steven Moreland65b3fd92017-12-06 14:18:35 -0800460// assorted utils
461
462func cleanPaths(paths []string) []string {
463 res := make([]string, len(paths))
464 for i, v := range paths {
465 res[i] = filepath.Clean(v) + "/"
466 }
467 return res
468}
469
470func fieldNamesForProperties(propertyNames string) []string {
471 names := strings.Split(propertyNames, ".")
472 for i, v := range names {
473 names[i] = proptools.FieldNameForProperty(v)
474 }
475 return names
476}
477
Steven Moreland65b3fd92017-12-06 14:18:35 -0800478func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
479 for _, v := range props {
480 if hasProperty(properties, v) {
481 return true
482 }
483 }
484 return false
485}
486
487func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
488 for _, v := range props {
489 if !hasProperty(properties, v) {
490 return false
491 }
492 }
493 return true
494}
495
496func hasProperty(properties []interface{}, prop ruleProperty) bool {
497 for _, propertyStruct := range properties {
498 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
499 for _, v := range prop.fields {
500 if !propertiesValue.IsValid() {
501 break
502 }
503 propertiesValue = propertiesValue.FieldByName(v)
504 }
505 if !propertiesValue.IsValid() {
506 continue
507 }
508
Paul Duffin73bf0542019-07-12 14:12:49 +0100509 check := func(value string) bool {
510 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800511 }
512
513 if matchValue(propertiesValue, check) {
514 return true
515 }
516 }
517 return false
518}
519
520func matchValue(value reflect.Value, check func(string) bool) bool {
521 if !value.IsValid() {
522 return false
523 }
524
525 if value.Kind() == reflect.Ptr {
526 if value.IsNil() {
527 return check("")
528 }
529 value = value.Elem()
530 }
531
532 switch value.Kind() {
533 case reflect.String:
534 return check(value.String())
535 case reflect.Bool:
536 return check(strconv.FormatBool(value.Bool()))
537 case reflect.Int:
538 return check(strconv.FormatInt(value.Int(), 10))
539 case reflect.Slice:
540 slice, ok := value.Interface().([]string)
541 if !ok {
542 panic("Can only handle slice of string")
543 }
544 for _, v := range slice {
545 if check(v) {
546 return true
547 }
548 }
549 return false
550 }
551
552 panic("Can't handle type: " + value.Kind().String())
553}
Paul Duffin115445b2019-08-07 15:31:07 +0100554
555var neverallowRulesKey = NewOnceKey("neverallowRules")
556
557func neverallowRules(config Config) []Rule {
558 return config.Once(neverallowRulesKey, func() interface{} {
559 // No test rules were set by setTestNeverallowRules, use the global rules
560 return neverallows
561 }).([]Rule)
562}
563
564// Overrides the default neverallow rules for the supplied config.
565//
566// For testing only.
567func setTestNeverallowRules(config Config, testRules []Rule) {
568 config.Once(neverallowRulesKey, func() interface{} { return testRules })
569}