Yi-Yo Chiang | 2c18965 | 2021-11-08 19:30:04 +0800 | [diff] [blame] | 1 | // Copyright 2021 The Android Open Source Project |
| 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 | |
| 15 | package selinux |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint/proptools" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | android.RegisterModuleType("se_bug_map", bugMapFactory) |
| 25 | } |
| 26 | |
| 27 | // se_bug_map collects and installs selinux denial bug tracking information to be loaded by auditd. |
| 28 | func bugMapFactory() android.Module { |
| 29 | c := &bugMap{} |
| 30 | c.AddProperties(&c.properties) |
| 31 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 32 | return c |
| 33 | } |
| 34 | |
| 35 | type bugMap struct { |
| 36 | android.ModuleBase |
| 37 | properties bugMapProperties |
| 38 | installSource android.Path |
| 39 | installPath android.InstallPath |
| 40 | } |
| 41 | |
| 42 | type bugMapProperties struct { |
| 43 | // List of source files. Can reference se_filegroup type modules with the ":module" syntax. |
| 44 | Srcs []string `android:"path"` |
| 45 | |
| 46 | // Output file name. Defaults to module name if unspecified. |
| 47 | Stem *string |
| 48 | } |
| 49 | |
| 50 | func (b *bugMap) stem() string { |
| 51 | return proptools.StringDefault(b.properties.Stem, b.Name()) |
| 52 | } |
| 53 | |
| 54 | func (b *bugMap) expandSeSources(ctx android.ModuleContext) android.Paths { |
| 55 | srcPaths := make(android.Paths, 0, len(b.properties.Srcs)) |
| 56 | for _, src := range b.properties.Srcs { |
| 57 | if m := android.SrcIsModule(src); m != "" { |
| 58 | module := android.GetModuleFromPathDep(ctx, m, "") |
| 59 | if module == nil { |
| 60 | // Error would have been handled by ExtractSourcesDeps |
| 61 | continue |
| 62 | } |
| 63 | if fg, ok := module.(*fileGroup); ok { |
| 64 | if b.SocSpecific() { |
| 65 | srcPaths = append(srcPaths, fg.VendorSrcs()...) |
| 66 | srcPaths = append(srcPaths, fg.SystemVendorSrcs()...) |
| 67 | } else if b.SystemExtSpecific() { |
| 68 | srcPaths = append(srcPaths, fg.SystemExtPrivateSrcs()...) |
| 69 | } else { |
| 70 | srcPaths = append(srcPaths, fg.SystemPrivateSrcs()...) |
| 71 | } |
| 72 | } else { |
| 73 | ctx.PropertyErrorf("srcs", "%q is not an se_filegroup", m) |
| 74 | } |
| 75 | } else { |
| 76 | srcPaths = append(srcPaths, android.PathForModuleSrc(ctx, src)) |
| 77 | } |
| 78 | } |
| 79 | return android.FirstUniquePaths(srcPaths) |
| 80 | } |
| 81 | |
| 82 | func (b *bugMap) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 83 | if !b.SocSpecific() && !b.SystemExtSpecific() && !b.Platform() { |
| 84 | ctx.ModuleErrorf("Selinux bug_map can only be installed in system, system_ext and vendor partitions") |
| 85 | } |
| 86 | |
| 87 | srcPaths := b.expandSeSources(ctx) |
| 88 | out := android.PathForModuleGen(ctx, b.Name()) |
| 89 | ctx.Build(pctx, android.BuildParams{ |
| 90 | Rule: android.Cat, |
| 91 | Inputs: srcPaths, |
| 92 | Output: out, |
| 93 | Description: "Combining bug_map for " + b.Name(), |
| 94 | }) |
| 95 | |
| 96 | b.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 97 | b.installSource = out |
| 98 | ctx.InstallFile(b.installPath, b.stem(), b.installSource) |
| 99 | } |
| 100 | |
| 101 | func (b *bugMap) AndroidMkEntries() []android.AndroidMkEntries { |
| 102 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 103 | Class: "ETC", |
| 104 | OutputFile: android.OptionalPathForPath(b.installSource), |
| 105 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 106 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 107 | entries.SetPath("LOCAL_MODULE_PATH", b.installPath) |
Yi-Yo Chiang | 2c18965 | 2021-11-08 19:30:04 +0800 | [diff] [blame] | 108 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.stem()) |
| 109 | }, |
| 110 | }, |
| 111 | }} |
| 112 | } |