blob: 23b645413d5cfe08b4b95f4b8798b173bc36a5aa [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 Duffinaebc02a2019-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 Duffinaebc02a2019-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 Duffinaebc02a2019-06-27 14:08:51 +010048var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080049
Paul Duffinaebc02a2019-06-27 14:08:51 +010050func init() {
Paul Duffin2ac2bef2019-07-16 14:18:22 +010051 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffinaebc02a2019-06-27 14:08:51 +010052 AddNeverAllowRules(createTrebleRules()...)
53 AddNeverAllowRules(createLibcoreRules()...)
54 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010055}
Steven Moreland65b3fd92017-12-06 14:18:35 -080056
Paul Duffinaebc02a2019-06-27 14:08:51 +010057// Add a NeverAllow rule to the set of rules to apply.
58func AddNeverAllowRules(rules ...Rule) {
59 neverallows = append(neverallows, rules...)
60}
61
Paul Duffin2ac2bef2019-07-16 14:18:22 +010062func createIncludeDirsRules() []Rule {
63 // The list of paths that cannot be referenced using include_dirs
64 paths := []string{
65 "art",
66 "libcore",
67 "libnativehelper",
68 "external/apache-harmony",
69 "external/apache-xml",
70 "external/boringssl",
71 "external/bouncycastle",
72 "external/conscrypt",
73 "external/icu",
74 "external/okhttp",
75 "external/vixl",
76 "external/wycheproof",
77 "system/core/libnativebridge",
78 "system/core/libnativehelper",
79 }
80
81 // Create a composite matcher that will match if the value starts with any of the restricted
82 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
83 // XY.
84 rules := make([]Rule, 0, len(paths))
85 for _, path := range paths {
86 rule :=
87 NeverAllow().
88 WithMatcher("include_dirs", StartsWith(path+"/")).
89 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
90 " to use alternate mechanisms and so can no longer be used.")
91
92 rules = append(rules, rule)
93 }
94
95 return rules
96}
97
Paul Duffinaebc02a2019-06-27 14:08:51 +010098func createTrebleRules() []Rule {
99 return []Rule{
100 NeverAllow().
101 In("vendor", "device").
102 With("vndk.enabled", "true").
103 Without("vendor", "true").
104 Because("the VNDK can never contain a library that is device dependent."),
105 NeverAllow().
106 With("vndk.enabled", "true").
107 Without("vendor", "true").
108 Without("owner", "").
109 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800110
Neil Fullerdf5f3562018-10-21 17:19:10 +0100111 // TODO(b/67974785): always enforce the manifest
Paul Duffinaebc02a2019-06-27 14:08:51 +0100112 NeverAllow().
113 Without("name", "libhidltransport").
114 With("product_variables.enforce_vintf_manifest.cflags", "*").
115 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100116
117 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffinaebc02a2019-06-27 14:08:51 +0100118 NeverAllow().
119 Without("name", "libc_bionic_ndk").
120 With("product_variables.treble_linker_namespaces.cflags", "*").
121 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100122
123 // Example:
Paul Duffinaebc02a2019-06-27 14:08:51 +0100124 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100125 }
126}
127
Paul Duffinaebc02a2019-06-27 14:08:51 +0100128func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100129 var coreLibraryProjects = []string{
130 "libcore",
131 "external/apache-harmony",
132 "external/apache-xml",
133 "external/bouncycastle",
134 "external/conscrypt",
135 "external/icu",
136 "external/okhttp",
137 "external/wycheproof",
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100138
139 // Not really a core library but still needs access to same capabilities.
140 "development",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100141 }
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 Duffinaebc02a2019-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 Duffinaebc02a2019-06-27 14:08:51 +0100154func createJavaDeviceForHostRules() []Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800155 javaDeviceForHostProjectsWhitelist := []string{
Colin Cross97add502019-04-11 14:07:38 -0700156 "external/guava",
Colin Crossc35c5f92019-03-05 15:06:16 -0800157 "external/robolectric-shadows",
158 "framework/layoutlib",
159 }
160
Paul Duffinaebc02a2019-06-27 14:08:51 +0100161 return []Rule{
162 NeverAllow().
163 NotIn(javaDeviceForHostProjectsWhitelist...).
164 ModuleType("java_device_for_host", "java_host_for_device").
165 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossc35c5f92019-03-05 15:06:16 -0800166 }
167}
168
Steven Moreland65b3fd92017-12-06 14:18:35 -0800169func neverallowMutator(ctx BottomUpMutatorContext) {
170 m, ok := ctx.Module().(Module)
171 if !ok {
172 return
173 }
174
175 dir := ctx.ModuleDir() + "/"
176 properties := m.GetProperties()
177
Paul Duffinaebc02a2019-06-27 14:08:51 +0100178 for _, r := range neverallows {
179 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800180 if !n.appliesToPath(dir) {
181 continue
182 }
183
Colin Crossc35c5f92019-03-05 15:06:16 -0800184 if !n.appliesToModuleType(ctx.ModuleType()) {
185 continue
186 }
187
Steven Moreland65b3fd92017-12-06 14:18:35 -0800188 if !n.appliesToProperties(properties) {
189 continue
190 }
191
192 ctx.ModuleErrorf("violates " + n.String())
193 }
194}
195
Paul Duffin73bf0542019-07-12 14:12:49 +0100196type ValueMatcher interface {
197 test(string) bool
198 String() string
199}
200
201type equalMatcher struct {
202 expected string
203}
204
205func (m *equalMatcher) test(value string) bool {
206 return m.expected == value
207}
208
209func (m *equalMatcher) String() string {
210 return "=" + m.expected
211}
212
213type anyMatcher struct {
214}
215
216func (m *anyMatcher) test(value string) bool {
217 return true
218}
219
220func (m *anyMatcher) String() string {
221 return "=*"
222}
223
224var anyMatcherInstance = &anyMatcher{}
225
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100226type startsWithMatcher struct {
227 prefix string
228}
229
230func (m *startsWithMatcher) test(value string) bool {
231 return strings.HasPrefix(value, m.prefix)
232}
233
234func (m *startsWithMatcher) String() string {
235 return ".starts-with(" + m.prefix + ")"
236}
237
Steven Moreland65b3fd92017-12-06 14:18:35 -0800238type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100239 fields []string // e.x.: Vndk.Enabled
240 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800241}
242
Paul Duffinaebc02a2019-06-27 14:08:51 +0100243// A NeverAllow rule.
244type Rule interface {
245 In(path ...string) Rule
246
247 NotIn(path ...string) Rule
248
249 ModuleType(types ...string) Rule
250
251 NotModuleType(types ...string) Rule
252
253 With(properties, value string) Rule
254
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100255 WithMatcher(properties string, matcher ValueMatcher) Rule
256
Paul Duffinaebc02a2019-06-27 14:08:51 +0100257 Without(properties, value string) Rule
258
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100259 WithoutMatcher(properties string, matcher ValueMatcher) Rule
260
Paul Duffinaebc02a2019-06-27 14:08:51 +0100261 Because(reason string) Rule
262}
263
Steven Moreland65b3fd92017-12-06 14:18:35 -0800264type rule struct {
265 // User string for why this is a thing.
266 reason string
267
268 paths []string
269 unlessPaths []string
270
Colin Crossc35c5f92019-03-05 15:06:16 -0800271 moduleTypes []string
272 unlessModuleTypes []string
273
Steven Moreland65b3fd92017-12-06 14:18:35 -0800274 props []ruleProperty
275 unlessProps []ruleProperty
276}
277
Paul Duffinaebc02a2019-06-27 14:08:51 +0100278// Create a new NeverAllow rule.
279func NeverAllow() Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800280 return &rule{}
281}
Colin Crossc35c5f92019-03-05 15:06:16 -0800282
Paul Duffinaebc02a2019-06-27 14:08:51 +0100283func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800284 r.paths = append(r.paths, cleanPaths(path)...)
285 return r
286}
Colin Crossc35c5f92019-03-05 15:06:16 -0800287
Paul Duffinaebc02a2019-06-27 14:08:51 +0100288func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800289 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
290 return r
291}
Colin Crossc35c5f92019-03-05 15:06:16 -0800292
Paul Duffinaebc02a2019-06-27 14:08:51 +0100293func (r *rule) ModuleType(types ...string) Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800294 r.moduleTypes = append(r.moduleTypes, types...)
295 return r
296}
297
Paul Duffinaebc02a2019-06-27 14:08:51 +0100298func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossc35c5f92019-03-05 15:06:16 -0800299 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
300 return r
301}
302
Paul Duffinaebc02a2019-06-27 14:08:51 +0100303func (r *rule) With(properties, value string) Rule {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100304 return r.WithMatcher(properties, selectMatcher(value))
305}
306
307func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800308 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100309 fields: fieldNamesForProperties(properties),
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100310 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800311 })
312 return r
313}
Colin Crossc35c5f92019-03-05 15:06:16 -0800314
Paul Duffinaebc02a2019-06-27 14:08:51 +0100315func (r *rule) Without(properties, value string) Rule {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100316 return r.WithoutMatcher(properties, selectMatcher(value))
317}
318
319func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800320 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100321 fields: fieldNamesForProperties(properties),
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100322 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800323 })
324 return r
325}
Colin Crossc35c5f92019-03-05 15:06:16 -0800326
Paul Duffin73bf0542019-07-12 14:12:49 +0100327func selectMatcher(expected string) ValueMatcher {
328 if expected == "*" {
329 return anyMatcherInstance
330 }
331 return &equalMatcher{expected: expected}
332}
333
Paul Duffinaebc02a2019-06-27 14:08:51 +0100334func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800335 r.reason = reason
336 return r
337}
338
339func (r *rule) String() string {
340 s := "neverallow"
341 for _, v := range r.paths {
342 s += " dir:" + v + "*"
343 }
344 for _, v := range r.unlessPaths {
345 s += " -dir:" + v + "*"
346 }
Colin Crossc35c5f92019-03-05 15:06:16 -0800347 for _, v := range r.moduleTypes {
348 s += " type:" + v
349 }
350 for _, v := range r.unlessModuleTypes {
351 s += " -type:" + v
352 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800353 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100354 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800355 }
356 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100357 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800358 }
359 if len(r.reason) != 0 {
360 s += " which is restricted because " + r.reason
361 }
362 return s
363}
364
365func (r *rule) appliesToPath(dir string) bool {
366 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
367 excludePath := hasAnyPrefix(dir, r.unlessPaths)
368 return includePath && !excludePath
369}
370
Colin Crossc35c5f92019-03-05 15:06:16 -0800371func (r *rule) appliesToModuleType(moduleType string) bool {
372 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
373}
374
Steven Moreland65b3fd92017-12-06 14:18:35 -0800375func (r *rule) appliesToProperties(properties []interface{}) bool {
376 includeProps := hasAllProperties(properties, r.props)
377 excludeProps := hasAnyProperty(properties, r.unlessProps)
378 return includeProps && !excludeProps
379}
380
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100381func StartsWith(prefix string) ValueMatcher {
382 return &startsWithMatcher{prefix}
383}
384
Steven Moreland65b3fd92017-12-06 14:18:35 -0800385// assorted utils
386
387func cleanPaths(paths []string) []string {
388 res := make([]string, len(paths))
389 for i, v := range paths {
390 res[i] = filepath.Clean(v) + "/"
391 }
392 return res
393}
394
395func fieldNamesForProperties(propertyNames string) []string {
396 names := strings.Split(propertyNames, ".")
397 for i, v := range names {
398 names[i] = proptools.FieldNameForProperty(v)
399 }
400 return names
401}
402
403func hasAnyPrefix(s string, prefixes []string) bool {
404 for _, prefix := range prefixes {
405 if strings.HasPrefix(s, prefix) {
406 return true
407 }
408 }
409 return false
410}
411
412func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
413 for _, v := range props {
414 if hasProperty(properties, v) {
415 return true
416 }
417 }
418 return false
419}
420
421func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
422 for _, v := range props {
423 if !hasProperty(properties, v) {
424 return false
425 }
426 }
427 return true
428}
429
430func hasProperty(properties []interface{}, prop ruleProperty) bool {
431 for _, propertyStruct := range properties {
432 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
433 for _, v := range prop.fields {
434 if !propertiesValue.IsValid() {
435 break
436 }
437 propertiesValue = propertiesValue.FieldByName(v)
438 }
439 if !propertiesValue.IsValid() {
440 continue
441 }
442
Paul Duffin73bf0542019-07-12 14:12:49 +0100443 check := func(value string) bool {
444 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800445 }
446
447 if matchValue(propertiesValue, check) {
448 return true
449 }
450 }
451 return false
452}
453
454func matchValue(value reflect.Value, check func(string) bool) bool {
455 if !value.IsValid() {
456 return false
457 }
458
459 if value.Kind() == reflect.Ptr {
460 if value.IsNil() {
461 return check("")
462 }
463 value = value.Elem()
464 }
465
466 switch value.Kind() {
467 case reflect.String:
468 return check(value.String())
469 case reflect.Bool:
470 return check(strconv.FormatBool(value.Bool()))
471 case reflect.Int:
472 return check(strconv.FormatInt(value.Int(), 10))
473 case reflect.Slice:
474 slice, ok := value.Interface().([]string)
475 if !ok {
476 panic("Can only handle slice of string")
477 }
478 for _, v := range slice {
479 if check(v) {
480 return true
481 }
482 }
483 return false
484 }
485
486 panic("Can't handle type: " + value.Kind().String())
487}