blob: e1e8956d884178c5c8424873384a31fcc5d1099b [file] [log] [blame]
Inseob Kimd5816612021-09-15 03:01:05 +00001// 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 (
Inseob Kimd5816612021-09-15 03:01:05 +000018 "sort"
19
20 "android/soong/android"
21)
22
Inseob Kim36d9d392023-09-04 17:40:03 +090023var currentCilTag = dependencyTag{name: "current_cil"}
24var prebuiltCilTag = dependencyTag{name: "prebuilt_cil"}
25
Inseob Kimd5816612021-09-15 03:01:05 +000026func init() {
27 ctx := android.InitRegistrationContext
LaMont Jones3ee89842023-05-16 16:59:17 +000028 ctx.RegisterParallelSingletonModuleType("se_freeze_test", freezeTestFactory)
Inseob Kimd5816612021-09-15 03:01:05 +000029}
30
31// se_freeze_test compares the plat sepolicy with the prebuilt sepolicy. Additional directories can
32// be specified via Makefile variables: SEPOLICY_FREEZE_TEST_EXTRA_DIRS and
33// SEPOLICY_FREEZE_TEST_EXTRA_PREBUILT_DIRS.
34func freezeTestFactory() android.SingletonModule {
35 f := &freezeTestModule{}
36 android.InitAndroidModule(f)
Inseob Kim36d9d392023-09-04 17:40:03 +090037 android.AddLoadHook(f, func(ctx android.LoadHookContext) {
38 f.loadHook(ctx)
39 })
Inseob Kimd5816612021-09-15 03:01:05 +000040 return f
41}
42
43type freezeTestModule struct {
44 android.SingletonModuleBase
45 freezeTestTimestamp android.ModuleOutPath
46}
47
Inseob Kim3e34b722023-12-11 18:15:42 +090048func (f *freezeTestModule) shouldRunTest(ctx android.EarlyModuleContext) bool {
49 val, _ := ctx.Config().GetBuildFlag("RELEASE_BOARD_API_LEVEL_FROZEN")
50 return val == "true"
Inseob Kim36d9d392023-09-04 17:40:03 +090051}
52
53func (f *freezeTestModule) loadHook(ctx android.LoadHookContext) {
Inseob Kimd5816612021-09-15 03:01:05 +000054 extraDirs := ctx.DeviceConfig().SepolicyFreezeTestExtraDirs()
55 extraPrebuiltDirs := ctx.DeviceConfig().SepolicyFreezeTestExtraPrebuiltDirs()
Inseob Kimd5816612021-09-15 03:01:05 +000056
Inseob Kim3e34b722023-12-11 18:15:42 +090057 if !f.shouldRunTest(ctx) {
Inseob Kimd5816612021-09-15 03:01:05 +000058 if len(extraDirs) > 0 || len(extraPrebuiltDirs) > 0 {
59 ctx.ModuleErrorf("SEPOLICY_FREEZE_TEST_EXTRA_DIRS or SEPOLICY_FREEZE_TEST_EXTRA_PREBUILT_DIRS cannot be set before system/sepolicy freezes.")
60 return
61 }
62
Inseob Kimd5816612021-09-15 03:01:05 +000063 return
64 }
65
66 if len(extraDirs) != len(extraPrebuiltDirs) {
67 ctx.ModuleErrorf("SEPOLICY_FREEZE_TEST_EXTRA_DIRS and SEPOLICY_FREEZE_TEST_EXTRA_PREBUILT_DIRS must have the same number of directories.")
68 return
69 }
Inseob Kim36d9d392023-09-04 17:40:03 +090070}
Inseob Kimd5816612021-09-15 03:01:05 +000071
Inseob Kim36d9d392023-09-04 17:40:03 +090072func (f *freezeTestModule) prebuiltCilModuleName(ctx android.EarlyModuleContext) string {
73 return ctx.DeviceConfig().PlatformSepolicyVersion() + "_plat_pub_policy.cil"
74}
Inseob Kimd5816612021-09-15 03:01:05 +000075
Inseob Kim36d9d392023-09-04 17:40:03 +090076func (f *freezeTestModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Inseob Kim3e34b722023-12-11 18:15:42 +090077 if !f.shouldRunTest(ctx) {
Inseob Kim36d9d392023-09-04 17:40:03 +090078 return
79 }
80
81 ctx.AddDependency(f, currentCilTag, "base_plat_pub_policy.cil")
82 ctx.AddDependency(f, prebuiltCilTag, f.prebuiltCilModuleName(ctx))
83}
84
85func (f *freezeTestModule) GenerateSingletonBuildActions(ctx android.SingletonContext) {
86 // does nothing; se_freeze_test is a singeton because two freeze test modules don't make sense.
87}
88
89func (f *freezeTestModule) outputFileOfDep(ctx android.ModuleContext, depTag dependencyTag) android.Path {
90 deps := ctx.GetDirectDepsWithTag(depTag)
91 if len(deps) != 1 {
92 ctx.ModuleErrorf("%d deps having tag %q; expected only one dep", len(deps), depTag)
93 return nil
94 }
95
96 dep := deps[0]
97 outputFileProducer, ok := dep.(android.OutputFileProducer)
98 if !ok {
99 ctx.ModuleErrorf("module %q is not an output file producer", dep.String())
100 return nil
101 }
102
103 output, err := outputFileProducer.OutputFiles("")
104 if err != nil {
105 ctx.ModuleErrorf("module %q failed to produce output: %w", dep.String(), err)
106 return nil
107 }
108 if len(output) != 1 {
109 ctx.ModuleErrorf("module %q produced %d outputs; expected only one output", dep.String(), len(output))
110 return nil
111 }
112
113 return output[0]
114}
115
116func (f *freezeTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
117 f.freezeTestTimestamp = android.PathForModuleOut(ctx, "freeze_test")
118
Inseob Kim3e34b722023-12-11 18:15:42 +0900119 if !f.shouldRunTest(ctx) {
Inseob Kim36d9d392023-09-04 17:40:03 +0900120 // we still build a rule to prevent possible regression
121 android.WriteFileRule(ctx, f.freezeTestTimestamp, ";; no freeze tests needed before system/sepolicy freezes")
122 return
123 }
124
125 // Freeze test 1: compare ToT sepolicy and prebuilt sepolicy
126 currentCil := f.outputFileOfDep(ctx, currentCilTag)
127 prebuiltCil := f.outputFileOfDep(ctx, prebuiltCilTag)
128 if ctx.Failed() {
129 return
130 }
131
132 rule := android.NewRuleBuilder(pctx, ctx)
133 rule.Command().BuiltTool("sepolicy_freeze_test").
134 FlagWithInput("-c ", currentCil).
135 FlagWithInput("-p ", prebuiltCil)
136
137 // Freeze test 2: compare extra directories
138 // We don't know the exact structure of extra directories, so just directly compare them
139 extraDirs := ctx.DeviceConfig().SepolicyFreezeTestExtraDirs()
140 extraPrebuiltDirs := ctx.DeviceConfig().SepolicyFreezeTestExtraPrebuiltDirs()
Inseob Kimd5816612021-09-15 03:01:05 +0000141
142 var implicits []string
Inseob Kim36d9d392023-09-04 17:40:03 +0900143 for _, dir := range append(extraDirs, extraPrebuiltDirs...) {
Inseob Kimd5816612021-09-15 03:01:05 +0000144 glob, err := ctx.GlobWithDeps(dir+"/**/*", []string{"bug_map"} /* exclude */)
145 if err != nil {
146 ctx.ModuleErrorf("failed to glob sepolicy dir %q: %s", dir, err.Error())
147 return
148 }
149 implicits = append(implicits, glob...)
150 }
151 sort.Strings(implicits)
152
Inseob Kim36d9d392023-09-04 17:40:03 +0900153 for idx, _ := range extraDirs {
Inseob Kimd5816612021-09-15 03:01:05 +0000154 rule.Command().Text("diff").
155 Flag("-r").
156 Flag("-q").
157 FlagWithArg("-x ", "bug_map"). // exclude
Inseob Kim36d9d392023-09-04 17:40:03 +0900158 Text(extraDirs[idx]).
159 Text(extraPrebuiltDirs[idx])
Inseob Kimd5816612021-09-15 03:01:05 +0000160 }
161
162 rule.Command().Text("touch").
163 Output(f.freezeTestTimestamp).
164 Implicits(android.PathsForSource(ctx, implicits))
165
166 rule.Build("sepolicy_freeze_test", "sepolicy_freeze_test")
167}
168
169func (f *freezeTestModule) AndroidMkEntries() []android.AndroidMkEntries {
170 return []android.AndroidMkEntries{android.AndroidMkEntries{
171 Class: "FAKE",
172 // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
173 // Without OutputFile this module won't be exported to Makefile.
174 OutputFile: android.OptionalPathForPath(f.freezeTestTimestamp),
175 Include: "$(BUILD_PHONY_PACKAGE)",
176 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
177 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
178 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", f.freezeTestTimestamp.String())
179 },
180 },
181 }}
182}