blob: 91c63472717d3f7abf83d2900b28b335ea011fe1 [file] [log] [blame]
Yi-Yo Chiang2c189652021-11-08 19:30:04 +08001// 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
15package selinux
16
17import (
18 "github.com/google/blueprint/proptools"
19
20 "android/soong/android"
21)
22
23func 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.
28func bugMapFactory() android.Module {
29 c := &bugMap{}
30 c.AddProperties(&c.properties)
31 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
32 return c
33}
34
35type bugMap struct {
36 android.ModuleBase
37 properties bugMapProperties
38 installSource android.Path
39 installPath android.InstallPath
40}
41
42type 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
50func (b *bugMap) stem() string {
51 return proptools.StringDefault(b.properties.Stem, b.Name())
52}
53
54func (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
82func (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
101func (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) {
107 entries.SetPath("LOCAL_MODULE_PATH", b.installPath.ToMakePath())
108 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.stem())
109 },
110 },
111 }}
112}