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
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 or se_build_files modules.
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	return android.PathsForModuleSrc(ctx, b.properties.Srcs)
56}
57
58func (b *bugMap) GenerateAndroidBuildActions(ctx android.ModuleContext) {
59	if !b.SocSpecific() && !b.SystemExtSpecific() && !b.Platform() {
60		ctx.ModuleErrorf("Selinux bug_map can only be installed in system, system_ext and vendor partitions")
61	}
62
63	srcPaths := b.expandSeSources(ctx)
64	out := android.PathForModuleGen(ctx, b.Name())
65	ctx.Build(pctx, android.BuildParams{
66		Rule:        android.Cat,
67		Inputs:      srcPaths,
68		Output:      out,
69		Description: "Combining bug_map for " + b.Name(),
70	})
71
72	b.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
73	b.installSource = out
74	ctx.InstallFile(b.installPath, b.stem(), b.installSource)
75}
76
77func (b *bugMap) AndroidMkEntries() []android.AndroidMkEntries {
78	return []android.AndroidMkEntries{android.AndroidMkEntries{
79		Class:      "ETC",
80		OutputFile: android.OptionalPathForPath(b.installSource),
81		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
82			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
83				entries.SetPath("LOCAL_MODULE_PATH", b.installPath)
84				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.stem())
85			},
86		},
87	}}
88}
89