blob: 8f1f1e0d4fd7b0e067b5e395e74b05aa6832b393 [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",
67 "libcore",
68 "libnativehelper",
69 "external/apache-harmony",
70 "external/apache-xml",
71 "external/boringssl",
72 "external/bouncycastle",
73 "external/conscrypt",
74 "external/icu",
75 "external/okhttp",
76 "external/vixl",
77 "external/wycheproof",
78 "system/core/libnativebridge",
79 "system/core/libnativehelper",
80 }
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().
114 Without("name", "libhidltransport-impl-internal").
115 With("product_variables.enforce_vintf_manifest.cflags", "*").
116 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100117
118 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100119 NeverAllow().
120 Without("name", "libc_bionic_ndk").
121 With("product_variables.treble_linker_namespaces.cflags", "*").
122 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100123
124 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100125 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100126 }
127}
128
Paul Duffin730f2a52019-06-27 14:08:51 +0100129func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100130 var coreLibraryProjects = []string{
131 "libcore",
132 "external/apache-harmony",
133 "external/apache-xml",
134 "external/bouncycastle",
135 "external/conscrypt",
136 "external/icu",
137 "external/okhttp",
138 "external/wycheproof",
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100139
140 // Not really a core library but still needs access to same capabilities.
141 "development",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100142 }
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 Duffin730f2a52019-06-27 14:08:51 +0100187 for _, r := range neverallows {
188 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800189 if !n.appliesToPath(dir) {
190 continue
191 }
192
Colin Crossfd4f7432019-03-05 15:06:16 -0800193 if !n.appliesToModuleType(ctx.ModuleType()) {
194 continue
195 }
196
Steven Moreland65b3fd92017-12-06 14:18:35 -0800197 if !n.appliesToProperties(properties) {
198 continue
199 }
200
201 ctx.ModuleErrorf("violates " + n.String())
202 }
203}
204
Paul Duffin73bf0542019-07-12 14:12:49 +0100205type ValueMatcher interface {
206 test(string) bool
207 String() string
208}
209
210type equalMatcher struct {
211 expected string
212}
213
214func (m *equalMatcher) test(value string) bool {
215 return m.expected == value
216}
217
218func (m *equalMatcher) String() string {
219 return "=" + m.expected
220}
221
222type anyMatcher struct {
223}
224
225func (m *anyMatcher) test(value string) bool {
226 return true
227}
228
229func (m *anyMatcher) String() string {
230 return "=*"
231}
232
233var anyMatcherInstance = &anyMatcher{}
234
Paul Duffinc8111702019-07-22 12:13:55 +0100235type startsWithMatcher struct {
236 prefix string
237}
238
239func (m *startsWithMatcher) test(value string) bool {
240 return strings.HasPrefix(value, m.prefix)
241}
242
243func (m *startsWithMatcher) String() string {
244 return ".starts-with(" + m.prefix + ")"
245}
246
Steven Moreland65b3fd92017-12-06 14:18:35 -0800247type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100248 fields []string // e.x.: Vndk.Enabled
249 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800250}
251
Paul Duffin730f2a52019-06-27 14:08:51 +0100252// A NeverAllow rule.
253type Rule interface {
254 In(path ...string) Rule
255
256 NotIn(path ...string) Rule
257
258 ModuleType(types ...string) Rule
259
260 NotModuleType(types ...string) Rule
261
262 With(properties, value string) Rule
263
Paul Duffinc8111702019-07-22 12:13:55 +0100264 WithMatcher(properties string, matcher ValueMatcher) Rule
265
Paul Duffin730f2a52019-06-27 14:08:51 +0100266 Without(properties, value string) Rule
267
Paul Duffinc8111702019-07-22 12:13:55 +0100268 WithoutMatcher(properties string, matcher ValueMatcher) Rule
269
Paul Duffin730f2a52019-06-27 14:08:51 +0100270 Because(reason string) Rule
271}
272
Steven Moreland65b3fd92017-12-06 14:18:35 -0800273type rule struct {
274 // User string for why this is a thing.
275 reason string
276
277 paths []string
278 unlessPaths []string
279
Colin Crossfd4f7432019-03-05 15:06:16 -0800280 moduleTypes []string
281 unlessModuleTypes []string
282
Steven Moreland65b3fd92017-12-06 14:18:35 -0800283 props []ruleProperty
284 unlessProps []ruleProperty
285}
286
Paul Duffin730f2a52019-06-27 14:08:51 +0100287// Create a new NeverAllow rule.
288func NeverAllow() Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800289 return &rule{}
290}
Colin Crossfd4f7432019-03-05 15:06:16 -0800291
Paul Duffin730f2a52019-06-27 14:08:51 +0100292func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800293 r.paths = append(r.paths, cleanPaths(path)...)
294 return r
295}
Colin Crossfd4f7432019-03-05 15:06:16 -0800296
Paul Duffin730f2a52019-06-27 14:08:51 +0100297func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800298 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
299 return r
300}
Colin Crossfd4f7432019-03-05 15:06:16 -0800301
Paul Duffin730f2a52019-06-27 14:08:51 +0100302func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800303 r.moduleTypes = append(r.moduleTypes, types...)
304 return r
305}
306
Paul Duffin730f2a52019-06-27 14:08:51 +0100307func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800308 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
309 return r
310}
311
Paul Duffin730f2a52019-06-27 14:08:51 +0100312func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100313 return r.WithMatcher(properties, selectMatcher(value))
314}
315
316func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800317 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100318 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100319 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800320 })
321 return r
322}
Colin Crossfd4f7432019-03-05 15:06:16 -0800323
Paul Duffin730f2a52019-06-27 14:08:51 +0100324func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100325 return r.WithoutMatcher(properties, selectMatcher(value))
326}
327
328func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800329 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100330 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100331 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800332 })
333 return r
334}
Colin Crossfd4f7432019-03-05 15:06:16 -0800335
Paul Duffin73bf0542019-07-12 14:12:49 +0100336func selectMatcher(expected string) ValueMatcher {
337 if expected == "*" {
338 return anyMatcherInstance
339 }
340 return &equalMatcher{expected: expected}
341}
342
Paul Duffin730f2a52019-06-27 14:08:51 +0100343func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800344 r.reason = reason
345 return r
346}
347
348func (r *rule) String() string {
349 s := "neverallow"
350 for _, v := range r.paths {
351 s += " dir:" + v + "*"
352 }
353 for _, v := range r.unlessPaths {
354 s += " -dir:" + v + "*"
355 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800356 for _, v := range r.moduleTypes {
357 s += " type:" + v
358 }
359 for _, v := range r.unlessModuleTypes {
360 s += " -type:" + v
361 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800362 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100363 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800364 }
365 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100366 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800367 }
368 if len(r.reason) != 0 {
369 s += " which is restricted because " + r.reason
370 }
371 return s
372}
373
374func (r *rule) appliesToPath(dir string) bool {
375 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
376 excludePath := hasAnyPrefix(dir, r.unlessPaths)
377 return includePath && !excludePath
378}
379
Colin Crossfd4f7432019-03-05 15:06:16 -0800380func (r *rule) appliesToModuleType(moduleType string) bool {
381 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
382}
383
Steven Moreland65b3fd92017-12-06 14:18:35 -0800384func (r *rule) appliesToProperties(properties []interface{}) bool {
385 includeProps := hasAllProperties(properties, r.props)
386 excludeProps := hasAnyProperty(properties, r.unlessProps)
387 return includeProps && !excludeProps
388}
389
Paul Duffinc8111702019-07-22 12:13:55 +0100390func StartsWith(prefix string) ValueMatcher {
391 return &startsWithMatcher{prefix}
392}
393
Steven Moreland65b3fd92017-12-06 14:18:35 -0800394// assorted utils
395
396func cleanPaths(paths []string) []string {
397 res := make([]string, len(paths))
398 for i, v := range paths {
399 res[i] = filepath.Clean(v) + "/"
400 }
401 return res
402}
403
404func fieldNamesForProperties(propertyNames string) []string {
405 names := strings.Split(propertyNames, ".")
406 for i, v := range names {
407 names[i] = proptools.FieldNameForProperty(v)
408 }
409 return names
410}
411
412func hasAnyPrefix(s string, prefixes []string) bool {
413 for _, prefix := range prefixes {
414 if strings.HasPrefix(s, prefix) {
415 return true
416 }
417 }
418 return false
419}
420
421func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
422 for _, v := range props {
423 if hasProperty(properties, v) {
424 return true
425 }
426 }
427 return false
428}
429
430func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
431 for _, v := range props {
432 if !hasProperty(properties, v) {
433 return false
434 }
435 }
436 return true
437}
438
439func hasProperty(properties []interface{}, prop ruleProperty) bool {
440 for _, propertyStruct := range properties {
441 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
442 for _, v := range prop.fields {
443 if !propertiesValue.IsValid() {
444 break
445 }
446 propertiesValue = propertiesValue.FieldByName(v)
447 }
448 if !propertiesValue.IsValid() {
449 continue
450 }
451
Paul Duffin73bf0542019-07-12 14:12:49 +0100452 check := func(value string) bool {
453 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800454 }
455
456 if matchValue(propertiesValue, check) {
457 return true
458 }
459 }
460 return false
461}
462
463func matchValue(value reflect.Value, check func(string) bool) bool {
464 if !value.IsValid() {
465 return false
466 }
467
468 if value.Kind() == reflect.Ptr {
469 if value.IsNil() {
470 return check("")
471 }
472 value = value.Elem()
473 }
474
475 switch value.Kind() {
476 case reflect.String:
477 return check(value.String())
478 case reflect.Bool:
479 return check(strconv.FormatBool(value.Bool()))
480 case reflect.Int:
481 return check(strconv.FormatInt(value.Int(), 10))
482 case reflect.Slice:
483 slice, ok := value.Interface().([]string)
484 if !ok {
485 panic("Can only handle slice of string")
486 }
487 for _, v := range slice {
488 if check(v) {
489 return true
490 }
491 }
492 return false
493 }
494
495 panic("Can't handle type: " + value.Kind().String())
496}