blob: 73cd6e34529df33dc85fc6b94982cd76e5051baa [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()...)
Jeff Sharkey38338a92019-12-15 22:05:02 -070055 AddNeverAllowRules(createMediaProviderRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010056 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010057}
Steven Moreland65b3fd92017-12-06 14:18:35 -080058
Paul Duffin730f2a52019-06-27 14:08:51 +010059// Add a NeverAllow rule to the set of rules to apply.
60func AddNeverAllowRules(rules ...Rule) {
61 neverallows = append(neverallows, rules...)
62}
63
Paul Duffinc8111702019-07-22 12:13:55 +010064func createIncludeDirsRules() []Rule {
65 // The list of paths that cannot be referenced using include_dirs
66 paths := []string{
67 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000068 "art/libnativebridge",
69 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010070 "libcore",
71 "libnativehelper",
72 "external/apache-harmony",
73 "external/apache-xml",
74 "external/boringssl",
75 "external/bouncycastle",
76 "external/conscrypt",
77 "external/icu",
78 "external/okhttp",
79 "external/vixl",
80 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010081 }
82
83 // Create a composite matcher that will match if the value starts with any of the restricted
84 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
85 // XY.
86 rules := make([]Rule, 0, len(paths))
87 for _, path := range paths {
88 rule :=
89 NeverAllow().
90 WithMatcher("include_dirs", StartsWith(path+"/")).
91 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
92 " to use alternate mechanisms and so can no longer be used.")
93
94 rules = append(rules, rule)
95 }
96
97 return rules
98}
99
Paul Duffin730f2a52019-06-27 14:08:51 +0100100func createTrebleRules() []Rule {
101 return []Rule{
102 NeverAllow().
103 In("vendor", "device").
104 With("vndk.enabled", "true").
105 Without("vendor", "true").
Justin Yun98df0d12020-02-28 15:07:59 +0900106 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100107 Because("the VNDK can never contain a library that is device dependent."),
108 NeverAllow().
109 With("vndk.enabled", "true").
110 Without("vendor", "true").
111 Without("owner", "").
112 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800113
Neil Fullerdf5f3562018-10-21 17:19:10 +0100114 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100115 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800116 Without("name", "libhidlbase-combined-impl").
117 Without("name", "libhidlbase").
118 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100119 With("product_variables.enforce_vintf_manifest.cflags", "*").
120 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100121
122 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100123 NeverAllow().
124 Without("name", "libc_bionic_ndk").
125 With("product_variables.treble_linker_namespaces.cflags", "*").
126 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100127
128 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100129 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100130 }
131}
132
Paul Duffin730f2a52019-06-27 14:08:51 +0100133func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100134 var coreLibraryProjects = []string{
135 "libcore",
136 "external/apache-harmony",
137 "external/apache-xml",
138 "external/bouncycastle",
139 "external/conscrypt",
140 "external/icu",
141 "external/okhttp",
142 "external/wycheproof",
143 }
144
Paul Duffina3d09862019-06-11 13:40:47 +0100145 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
146 // Access to core library targets is restricted using visibility rules.
Paul Duffin730f2a52019-06-27 14:08:51 +0100147 rules := []Rule{
148 NeverAllow().
149 NotIn(coreLibraryProjects...).
150 With("sdk_version", "none"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100151 }
152
Neil Fullerdf5f3562018-10-21 17:19:10 +0100153 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800154}
155
Paul Duffin730f2a52019-06-27 14:08:51 +0100156func createMediaRules() []Rule {
157 return []Rule{
158 NeverAllow().
159 With("libs", "updatable-media").
160 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800161 }
162}
163
Jeff Sharkey38338a92019-12-15 22:05:02 -0700164func createMediaProviderRules() []Rule {
165 return []Rule{
166 NeverAllow().
167 With("libs", "framework-mediaprovider").
168 Because("framework-mediaprovider includes private APIs. Use framework_mediaprovider_stubs instead."),
169 }
170}
171
Paul Duffin730f2a52019-06-27 14:08:51 +0100172func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800173 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700174 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800175 "external/robolectric-shadows",
176 "framework/layoutlib",
177 }
178
Paul Duffin730f2a52019-06-27 14:08:51 +0100179 return []Rule{
180 NeverAllow().
181 NotIn(javaDeviceForHostProjectsWhitelist...).
182 ModuleType("java_device_for_host", "java_host_for_device").
183 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800184 }
185}
186
Steven Moreland65b3fd92017-12-06 14:18:35 -0800187func neverallowMutator(ctx BottomUpMutatorContext) {
188 m, ok := ctx.Module().(Module)
189 if !ok {
190 return
191 }
192
193 dir := ctx.ModuleDir() + "/"
194 properties := m.GetProperties()
195
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100196 osClass := ctx.Module().Target().Os.Class
197
Paul Duffin115445b2019-08-07 15:31:07 +0100198 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100199 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800200 if !n.appliesToPath(dir) {
201 continue
202 }
203
Colin Crossfd4f7432019-03-05 15:06:16 -0800204 if !n.appliesToModuleType(ctx.ModuleType()) {
205 continue
206 }
207
Steven Moreland65b3fd92017-12-06 14:18:35 -0800208 if !n.appliesToProperties(properties) {
209 continue
210 }
211
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100212 if !n.appliesToOsClass(osClass) {
213 continue
214 }
215
Paul Duffin35781882019-07-25 15:41:09 +0100216 if !n.appliesToDirectDeps(ctx) {
217 continue
218 }
219
Steven Moreland65b3fd92017-12-06 14:18:35 -0800220 ctx.ModuleErrorf("violates " + n.String())
221 }
222}
223
Paul Duffin73bf0542019-07-12 14:12:49 +0100224type ValueMatcher interface {
225 test(string) bool
226 String() string
227}
228
229type equalMatcher struct {
230 expected string
231}
232
233func (m *equalMatcher) test(value string) bool {
234 return m.expected == value
235}
236
237func (m *equalMatcher) String() string {
238 return "=" + m.expected
239}
240
241type anyMatcher struct {
242}
243
244func (m *anyMatcher) test(value string) bool {
245 return true
246}
247
248func (m *anyMatcher) String() string {
249 return "=*"
250}
251
252var anyMatcherInstance = &anyMatcher{}
253
Paul Duffinc8111702019-07-22 12:13:55 +0100254type startsWithMatcher struct {
255 prefix string
256}
257
258func (m *startsWithMatcher) test(value string) bool {
259 return strings.HasPrefix(value, m.prefix)
260}
261
262func (m *startsWithMatcher) String() string {
263 return ".starts-with(" + m.prefix + ")"
264}
265
Steven Moreland65b3fd92017-12-06 14:18:35 -0800266type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100267 fields []string // e.x.: Vndk.Enabled
268 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800269}
270
Paul Duffin730f2a52019-06-27 14:08:51 +0100271// A NeverAllow rule.
272type Rule interface {
273 In(path ...string) Rule
274
275 NotIn(path ...string) Rule
276
Paul Duffin35781882019-07-25 15:41:09 +0100277 InDirectDeps(deps ...string) Rule
278
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100279 WithOsClass(osClasses ...OsClass) Rule
280
Paul Duffin730f2a52019-06-27 14:08:51 +0100281 ModuleType(types ...string) Rule
282
283 NotModuleType(types ...string) Rule
284
285 With(properties, value string) Rule
286
Paul Duffinc8111702019-07-22 12:13:55 +0100287 WithMatcher(properties string, matcher ValueMatcher) Rule
288
Paul Duffin730f2a52019-06-27 14:08:51 +0100289 Without(properties, value string) Rule
290
Paul Duffinc8111702019-07-22 12:13:55 +0100291 WithoutMatcher(properties string, matcher ValueMatcher) Rule
292
Paul Duffin730f2a52019-06-27 14:08:51 +0100293 Because(reason string) Rule
294}
295
Steven Moreland65b3fd92017-12-06 14:18:35 -0800296type rule struct {
297 // User string for why this is a thing.
298 reason string
299
300 paths []string
301 unlessPaths []string
302
Paul Duffin35781882019-07-25 15:41:09 +0100303 directDeps map[string]bool
304
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100305 osClasses []OsClass
306
Colin Crossfd4f7432019-03-05 15:06:16 -0800307 moduleTypes []string
308 unlessModuleTypes []string
309
Steven Moreland65b3fd92017-12-06 14:18:35 -0800310 props []ruleProperty
311 unlessProps []ruleProperty
312}
313
Paul Duffin730f2a52019-06-27 14:08:51 +0100314// Create a new NeverAllow rule.
315func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100316 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800317}
Colin Crossfd4f7432019-03-05 15:06:16 -0800318
Paul Duffin730f2a52019-06-27 14:08:51 +0100319func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800320 r.paths = append(r.paths, cleanPaths(path)...)
321 return r
322}
Colin Crossfd4f7432019-03-05 15:06:16 -0800323
Paul Duffin730f2a52019-06-27 14:08:51 +0100324func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800325 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
326 return r
327}
Colin Crossfd4f7432019-03-05 15:06:16 -0800328
Paul Duffin35781882019-07-25 15:41:09 +0100329func (r *rule) InDirectDeps(deps ...string) Rule {
330 for _, d := range deps {
331 r.directDeps[d] = true
332 }
333 return r
334}
335
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100336func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
337 r.osClasses = append(r.osClasses, osClasses...)
338 return r
339}
340
Paul Duffin730f2a52019-06-27 14:08:51 +0100341func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800342 r.moduleTypes = append(r.moduleTypes, types...)
343 return r
344}
345
Paul Duffin730f2a52019-06-27 14:08:51 +0100346func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800347 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
348 return r
349}
350
Paul Duffin730f2a52019-06-27 14:08:51 +0100351func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100352 return r.WithMatcher(properties, selectMatcher(value))
353}
354
355func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800356 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100357 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100358 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800359 })
360 return r
361}
Colin Crossfd4f7432019-03-05 15:06:16 -0800362
Paul Duffin730f2a52019-06-27 14:08:51 +0100363func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100364 return r.WithoutMatcher(properties, selectMatcher(value))
365}
366
367func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800368 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100369 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100370 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800371 })
372 return r
373}
Colin Crossfd4f7432019-03-05 15:06:16 -0800374
Paul Duffin73bf0542019-07-12 14:12:49 +0100375func selectMatcher(expected string) ValueMatcher {
376 if expected == "*" {
377 return anyMatcherInstance
378 }
379 return &equalMatcher{expected: expected}
380}
381
Paul Duffin730f2a52019-06-27 14:08:51 +0100382func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800383 r.reason = reason
384 return r
385}
386
387func (r *rule) String() string {
388 s := "neverallow"
389 for _, v := range r.paths {
390 s += " dir:" + v + "*"
391 }
392 for _, v := range r.unlessPaths {
393 s += " -dir:" + v + "*"
394 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800395 for _, v := range r.moduleTypes {
396 s += " type:" + v
397 }
398 for _, v := range r.unlessModuleTypes {
399 s += " -type:" + v
400 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800401 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100402 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800403 }
404 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100405 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800406 }
Paul Duffin35781882019-07-25 15:41:09 +0100407 for k := range r.directDeps {
408 s += " deps:" + k
409 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100410 for _, v := range r.osClasses {
411 s += " os:" + v.String()
412 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800413 if len(r.reason) != 0 {
414 s += " which is restricted because " + r.reason
415 }
416 return s
417}
418
419func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800420 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
421 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800422 return includePath && !excludePath
423}
424
Paul Duffin35781882019-07-25 15:41:09 +0100425func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
426 if len(r.directDeps) == 0 {
427 return true
428 }
429
430 matches := false
431 ctx.VisitDirectDeps(func(m Module) {
432 if !matches {
433 name := ctx.OtherModuleName(m)
434 matches = r.directDeps[name]
435 }
436 })
437
438 return matches
439}
440
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100441func (r *rule) appliesToOsClass(osClass OsClass) bool {
442 if len(r.osClasses) == 0 {
443 return true
444 }
445
446 for _, c := range r.osClasses {
447 if c == osClass {
448 return true
449 }
450 }
451
452 return false
453}
454
Colin Crossfd4f7432019-03-05 15:06:16 -0800455func (r *rule) appliesToModuleType(moduleType string) bool {
456 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
457}
458
Steven Moreland65b3fd92017-12-06 14:18:35 -0800459func (r *rule) appliesToProperties(properties []interface{}) bool {
460 includeProps := hasAllProperties(properties, r.props)
461 excludeProps := hasAnyProperty(properties, r.unlessProps)
462 return includeProps && !excludeProps
463}
464
Paul Duffinc8111702019-07-22 12:13:55 +0100465func StartsWith(prefix string) ValueMatcher {
466 return &startsWithMatcher{prefix}
467}
468
Steven Moreland65b3fd92017-12-06 14:18:35 -0800469// assorted utils
470
471func cleanPaths(paths []string) []string {
472 res := make([]string, len(paths))
473 for i, v := range paths {
474 res[i] = filepath.Clean(v) + "/"
475 }
476 return res
477}
478
479func fieldNamesForProperties(propertyNames string) []string {
480 names := strings.Split(propertyNames, ".")
481 for i, v := range names {
482 names[i] = proptools.FieldNameForProperty(v)
483 }
484 return names
485}
486
Steven Moreland65b3fd92017-12-06 14:18:35 -0800487func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
488 for _, v := range props {
489 if hasProperty(properties, v) {
490 return true
491 }
492 }
493 return false
494}
495
496func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
497 for _, v := range props {
498 if !hasProperty(properties, v) {
499 return false
500 }
501 }
502 return true
503}
504
505func hasProperty(properties []interface{}, prop ruleProperty) bool {
506 for _, propertyStruct := range properties {
507 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
508 for _, v := range prop.fields {
509 if !propertiesValue.IsValid() {
510 break
511 }
512 propertiesValue = propertiesValue.FieldByName(v)
513 }
514 if !propertiesValue.IsValid() {
515 continue
516 }
517
Paul Duffin73bf0542019-07-12 14:12:49 +0100518 check := func(value string) bool {
519 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800520 }
521
522 if matchValue(propertiesValue, check) {
523 return true
524 }
525 }
526 return false
527}
528
529func matchValue(value reflect.Value, check func(string) bool) bool {
530 if !value.IsValid() {
531 return false
532 }
533
534 if value.Kind() == reflect.Ptr {
535 if value.IsNil() {
536 return check("")
537 }
538 value = value.Elem()
539 }
540
541 switch value.Kind() {
542 case reflect.String:
543 return check(value.String())
544 case reflect.Bool:
545 return check(strconv.FormatBool(value.Bool()))
546 case reflect.Int:
547 return check(strconv.FormatInt(value.Int(), 10))
548 case reflect.Slice:
549 slice, ok := value.Interface().([]string)
550 if !ok {
551 panic("Can only handle slice of string")
552 }
553 for _, v := range slice {
554 if check(v) {
555 return true
556 }
557 }
558 return false
559 }
560
561 panic("Can't handle type: " + value.Kind().String())
562}
Paul Duffin115445b2019-08-07 15:31:07 +0100563
564var neverallowRulesKey = NewOnceKey("neverallowRules")
565
566func neverallowRules(config Config) []Rule {
567 return config.Once(neverallowRulesKey, func() interface{} {
568 // No test rules were set by setTestNeverallowRules, use the global rules
569 return neverallows
570 }).([]Rule)
571}
572
573// Overrides the default neverallow rules for the supplied config.
574//
575// For testing only.
576func setTestNeverallowRules(config Config, testRules []Rule) {
577 config.Once(neverallowRulesKey, func() interface{} { return testRules })
578}