blob: e542c3af4f44d3ac1e4c315de5bcdc48a6a2a249 [file] [log] [blame]
Inseob Kimb554e592019-04-15 20:10:46 +09001// Copyright (C) 2019 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 "fmt"
19 "io"
Inseob Kimb554e592019-04-15 20:10:46 +090020
Inseob Kimcd616492020-03-24 23:06:40 +090021 "github.com/google/blueprint"
Inseob Kimb554e592019-04-15 20:10:46 +090022 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
Inseob Kimcd616492020-03-24 23:06:40 +090025 "android/soong/sysprop"
Inseob Kimb554e592019-04-15 20:10:46 +090026)
27
Inseob Kimb554e592019-04-15 20:10:46 +090028type selinuxContextsProperties struct {
29 // Filenames under sepolicy directories, which will be used to generate contexts file.
30 Srcs []string `android:"path"`
31
Yuntao Xu42e732c2021-11-18 22:33:02 +000032 // Output file name. Defaults to module name
33 Stem *string
34
Inseob Kimb554e592019-04-15 20:10:46 +090035 Product_variables struct {
Inseob Kimb554e592019-04-15 20:10:46 +090036 Address_sanitize struct {
Inseob Kim6d3d5a62021-12-21 20:55:32 +090037 Srcs []string `android:"path"`
Inseob Kimb554e592019-04-15 20:10:46 +090038 }
39 }
40
Inseob Kimb554e592019-04-15 20:10:46 +090041 // Whether the comments in generated contexts file will be removed or not.
42 Remove_comment *bool
43
44 // Whether the result context file is sorted with fc_sort or not.
45 Fc_sort *bool
46
47 // Make this module available when building for recovery
48 Recovery_available *bool
Inseob Kimb554e592019-04-15 20:10:46 +090049}
50
Inseob Kim2dac2672021-12-29 17:54:57 +090051type seappProperties struct {
52 // Files containing neverallow rules.
53 Neverallow_files []string `android:"path"`
54
55 // Precompiled sepolicy binary file which will be fed to checkseapp.
56 Sepolicy *string `android:"path"`
57}
58
Inseob Kimb554e592019-04-15 20:10:46 +090059type selinuxContextsModule struct {
60 android.ModuleBase
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090061 android.DefaultableModuleBase
62 flaggableModuleBase
Inseob Kimb554e592019-04-15 20:10:46 +090063
Jooyung Han804e2342023-06-20 15:38:50 +090064 properties selinuxContextsProperties
65 seappProperties seappProperties
66 build func(ctx android.ModuleContext, inputs android.Paths) android.Path
67 deps func(ctx android.BottomUpMutatorContext)
68 outputPath android.Path
69 installPath android.InstallPath
Inseob Kimb554e592019-04-15 20:10:46 +090070}
71
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090072var _ flaggableModule = (*selinuxContextsModule)(nil)
73
Inseob Kimb554e592019-04-15 20:10:46 +090074var (
Inseob Kimcd616492020-03-24 23:06:40 +090075 reuseContextsDepTag = dependencyTag{name: "reuseContexts"}
76 syspropLibraryDepTag = dependencyTag{name: "sysprop_library"}
Inseob Kimb554e592019-04-15 20:10:46 +090077)
78
79func init() {
80 pctx.HostBinToolVariable("fc_sort", "fc_sort")
81
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090082 android.RegisterModuleType("contexts_defaults", contextsDefaultsFactory)
Inseob Kimb554e592019-04-15 20:10:46 +090083 android.RegisterModuleType("file_contexts", fileFactory)
84 android.RegisterModuleType("hwservice_contexts", hwServiceFactory)
85 android.RegisterModuleType("property_contexts", propertyFactory)
86 android.RegisterModuleType("service_contexts", serviceFactory)
Janis Danisevskisc40681f2020-07-25 13:02:29 -070087 android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory)
Inseob Kim2dac2672021-12-29 17:54:57 +090088 android.RegisterModuleType("seapp_contexts", seappFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +090089 android.RegisterModuleType("vndservice_contexts", vndServiceFactory)
Inseob Kimb5e23532022-02-16 02:26:11 +000090
91 android.RegisterModuleType("file_contexts_test", fileContextsTestFactory)
92 android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory)
93 android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory)
94 android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +090095 android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory)
Inseob Kimb554e592019-04-15 20:10:46 +090096}
97
Colin Cross040f1512019-10-02 10:36:09 -070098func (m *selinuxContextsModule) InstallInRoot() bool {
Inseob Kimfa6fe472021-01-12 13:40:27 +090099 return m.InRecovery()
100}
101
102func (m *selinuxContextsModule) InstallInRecovery() bool {
103 // ModuleBase.InRecovery() checks the image variant
104 return m.InRecovery()
105}
106
107func (m *selinuxContextsModule) onlyInRecovery() bool {
108 // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property
109 return m.ModuleBase.InstallInRecovery()
Colin Cross040f1512019-10-02 10:36:09 -0700110}
111
Inseob Kimcd616492020-03-24 23:06:40 +0900112func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
113 if m.deps != nil {
114 m.deps(ctx)
115 }
Inseob Kimfa6fe472021-01-12 13:40:27 +0900116
117 if m.InRecovery() && !m.onlyInRecovery() {
118 ctx.AddFarVariationDependencies([]blueprint.Variation{
119 {Mutator: "image", Variation: android.CoreVariation},
120 }, reuseContextsDepTag, ctx.ModuleName())
121 }
Inseob Kimcd616492020-03-24 23:06:40 +0900122}
123
124func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) {
125 for _, lib := range sysprop.SyspropLibraries(ctx.Config()) {
126 ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib)
127 }
128}
129
Yuntao Xu42e732c2021-11-18 22:33:02 +0000130func (m *selinuxContextsModule) stem() string {
131 return proptools.StringDefault(m.properties.Stem, m.Name())
132}
133
Inseob Kimb554e592019-04-15 20:10:46 +0900134func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900135 if m.InRecovery() {
Colin Cross040f1512019-10-02 10:36:09 -0700136 // Installing context files at the root of the recovery partition
137 m.installPath = android.PathForModuleInstall(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900138 } else {
139 m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
140 }
141
Inseob Kimfa6fe472021-01-12 13:40:27 +0900142 if m.InRecovery() && !m.onlyInRecovery() {
Inseob Kimb554e592019-04-15 20:10:46 +0900143 dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag)
144
145 if reuseDeps, ok := dep.(*selinuxContextsModule); ok {
146 m.outputPath = reuseDeps.outputPath
Yuntao Xu42e732c2021-11-18 22:33:02 +0000147 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900148 return
149 }
150 }
151
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900152 m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs))
Yuntao Xu42e732c2021-11-18 22:33:02 +0000153 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900154}
155
156func newModule() *selinuxContextsModule {
157 m := &selinuxContextsModule{}
158 m.AddProperties(
159 &m.properties,
Inseob Kim2dac2672021-12-29 17:54:57 +0900160 &m.seappProperties,
Inseob Kimb554e592019-04-15 20:10:46 +0900161 )
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900162 initFlaggableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900163 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900164 android.InitDefaultableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900165 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
166 m.selinuxContextsHook(ctx)
167 })
168 return m
169}
170
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900171type contextsDefaults struct {
172 android.ModuleBase
173 android.DefaultsModuleBase
174}
175
176// contexts_defaults provides a set of properties that can be inherited by other contexts modules.
177// (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a
178// contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are
179// erged (when possible) by prepending the default module's values to the depending module's values.
180func contextsDefaultsFactory() android.Module {
181 m := &contextsDefaults{}
182 m.AddProperties(
183 &selinuxContextsProperties{},
184 &seappProperties{},
185 &flagsProperties{},
186 )
187 android.InitDefaultsModule(m)
188 return m
189}
190
Inseob Kimb554e592019-04-15 20:10:46 +0900191func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) {
192 // TODO: clean this up to use build/soong/android/variable.go after b/79249983
193 var srcs []string
194
Inseob Kimb554e592019-04-15 20:10:46 +0900195 for _, sanitize := range ctx.Config().SanitizeDevice() {
196 if sanitize == "address" {
197 srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...)
198 break
199 }
200 }
201
202 m.properties.Srcs = append(m.properties.Srcs, srcs...)
203}
204
205func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData {
Colin Crossf82aed02021-11-04 17:25:55 -0700206 nameSuffix := ""
207 if m.InRecovery() && !m.onlyInRecovery() {
208 nameSuffix = ".recovery"
209 }
Inseob Kimb554e592019-04-15 20:10:46 +0900210 return android.AndroidMkData{
Colin Crossf82aed02021-11-04 17:25:55 -0700211 Class: "ETC",
212 OutputFile: android.OptionalPathForPath(m.outputPath),
213 SubName: nameSuffix,
214 Extra: []android.AndroidMkExtraFunc{
215 func(w io.Writer, outputFile android.Path) {
Colin Cross6c7f9372022-01-11 19:35:43 -0800216 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000217 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem())
Colin Crossf82aed02021-11-04 17:25:55 -0700218 },
Inseob Kimb554e592019-04-15 20:10:46 +0900219 },
220 }
221}
222
Inseob Kimfa6fe472021-01-12 13:40:27 +0900223func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000224 if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900225 ctx.PropertyErrorf("recovery_available",
226 "doesn't make sense at the same time as `recovery: true`")
Inseob Kimb554e592019-04-15 20:10:46 +0900227 }
228}
229
Inseob Kimfa6fe472021-01-12 13:40:27 +0900230func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000231 return !m.ModuleBase.InstallInRecovery()
Inseob Kimfa6fe472021-01-12 13:40:27 +0900232}
233
234func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
235 return false
236}
237
238func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
239 return false
240}
241
Inseob Kim6cc75f42021-04-29 13:53:20 +0000242func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
243 return false
244}
245
Inseob Kimfa6fe472021-01-12 13:40:27 +0900246func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000247 return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available)
Inseob Kimfa6fe472021-01-12 13:40:27 +0900248}
249
250func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
251 return nil
252}
253
254func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
255}
256
257var _ android.ImageInterface = (*selinuxContextsModule)(nil)
258
Inseob Kimcd616492020-03-24 23:06:40 +0900259func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900260 builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out")
Inseob Kimb554e592019-04-15 20:10:46 +0900261
Colin Cross242c8bc2020-11-16 17:58:17 -0800262 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900263
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900264 newlineFile := pathForModuleOut(ctx, "newline")
Inseob Kim35e9d412023-01-04 15:27:32 +0900265
266 rule.Command().Text("echo").FlagWithOutput("> ", newlineFile)
267 rule.Temporary(newlineFile)
268
269 var inputsWithNewline android.Paths
270 for _, input := range inputs {
271 inputsWithNewline = append(inputsWithNewline, input, newlineFile)
272 }
273
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900274 flags := m.getBuildFlags(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900275 rule.Command().
Dan Willemsen3c3e59b2019-06-19 10:52:50 -0700276 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
277 Text("--fatal-warnings -s").
Inseob Kimb554e592019-04-15 20:10:46 +0900278 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900279 Flags(flagsToM4Macros(flags)).
Inseob Kim35e9d412023-01-04 15:27:32 +0900280 Inputs(inputsWithNewline).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000281 FlagWithOutput("> ", builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900282
283 if proptools.Bool(m.properties.Remove_comment) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000284 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900285
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900286 remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment")
Inseob Kimb554e592019-04-15 20:10:46 +0900287
288 rule.Command().
289 Text("sed -e 's/#.*$//' -e '/^$/d'").
Yuntao Xu42e732c2021-11-18 22:33:02 +0000290 Input(builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900291 FlagWithOutput("> ", remove_comment_output)
292
Yuntao Xu42e732c2021-11-18 22:33:02 +0000293 builtContext = remove_comment_output
Inseob Kimb554e592019-04-15 20:10:46 +0900294 }
295
296 if proptools.Bool(m.properties.Fc_sort) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000297 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900298
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900299 sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted")
Inseob Kimb554e592019-04-15 20:10:46 +0900300
301 rule.Command().
302 Tool(ctx.Config().HostToolPath(ctx, "fc_sort")).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000303 FlagWithInput("-i ", builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900304 FlagWithOutput("-o ", sorted_output)
305
Yuntao Xu42e732c2021-11-18 22:33:02 +0000306 builtContext = sorted_output
Inseob Kimb554e592019-04-15 20:10:46 +0900307 }
308
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900309 ret := pathForModuleOut(ctx, m.stem())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000310 rule.Temporary(builtContext)
311 rule.Command().Text("cp").Input(builtContext).Output(ret)
Inseob Kimb554e592019-04-15 20:10:46 +0900312
313 rule.DeleteTemporaryFiles()
Yuntao Xu42e732c2021-11-18 22:33:02 +0000314 rule.Build("selinux_contexts", "building contexts: "+m.Name())
Inseob Kimb554e592019-04-15 20:10:46 +0900315
Inseob Kimcd616492020-03-24 23:06:40 +0900316 return ret
Inseob Kimb554e592019-04-15 20:10:46 +0900317}
318
Inseob Kimcd616492020-03-24 23:06:40 +0900319func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimdfa4a482023-11-01 17:58:18 +0900320 if m.properties.Remove_comment == nil {
321 m.properties.Remove_comment = proptools.BoolPtr(true)
Inseob Kimb554e592019-04-15 20:10:46 +0900322 }
Inseob Kimcd616492020-03-24 23:06:40 +0900323 return m.buildGeneralContexts(ctx, inputs)
Inseob Kimb554e592019-04-15 20:10:46 +0900324}
325
326func fileFactory() android.Module {
327 m := newModule()
Inseob Kimb554e592019-04-15 20:10:46 +0900328 m.build = m.buildFileContexts
329 return m
330}
331
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000332func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimb554e592019-04-15 20:10:46 +0900333 if m.properties.Remove_comment == nil {
334 m.properties.Remove_comment = proptools.BoolPtr(true)
335 }
336
Inseob Kimcd616492020-03-24 23:06:40 +0900337 return m.buildGeneralContexts(ctx, inputs)
338}
339
Inseob Kim085f22f2023-11-09 11:13:01 +0900340func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, input android.Path) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900341 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
342 ApiLevelR := android.ApiLevelOrPanic(ctx, "R")
343
344 rule := android.NewRuleBuilder(pctx, ctx)
345
346 // This list is from vts_treble_sys_prop_test.
347 allowedPropertyPrefixes := []string{
348 "ctl.odm.",
349 "ctl.vendor.",
350 "ctl.start$odm.",
351 "ctl.start$vendor.",
352 "ctl.stop$odm.",
353 "ctl.stop$vendor.",
354 "init.svc.odm.",
355 "init.svc.vendor.",
356 "ro.boot.",
357 "ro.hardware.",
358 "ro.odm.",
359 "ro.vendor.",
360 "odm.",
361 "persist.odm.",
362 "persist.vendor.",
363 "vendor.",
364 }
365
366 // persist.camera is also allowed for devices launching with R or eariler
367 if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) {
368 allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.")
369 }
370
371 var allowedContextPrefixes []string
372
373 if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) {
374 // This list is from vts_treble_sys_prop_test.
375 allowedContextPrefixes = []string{
376 "vendor_",
377 "odm_",
378 }
379 }
380
Inseob Kim085f22f2023-11-09 11:13:01 +0900381 cmd := rule.Command().
382 BuiltTool("check_prop_prefix").
383 FlagWithInput("--property-contexts ", input).
384 FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$'
385 FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900386
Inseob Kim085f22f2023-11-09 11:13:01 +0900387 if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() {
388 cmd.Flag("--strict")
Inseob Kim2bcc0452020-12-21 13:16:44 +0900389 }
Inseob Kim085f22f2023-11-09 11:13:01 +0900390
391 out := pathForModuleOut(ctx, "namespace_checked").Join(ctx, input.String())
392 rule.Command().Text("cp -f").Input(input).Output(out)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900393 rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName())
Inseob Kim085f22f2023-11-09 11:13:01 +0900394 return out
Inseob Kim2bcc0452020-12-21 13:16:44 +0900395}
396
Inseob Kimcd616492020-03-24 23:06:40 +0900397func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900398 // vendor/odm properties are enforced for devices launching with Android Q or later. So, if
399 // vendor/odm, make sure that only vendor/odm properties exist.
Inseob Kim085f22f2023-11-09 11:13:01 +0900400 builtCtxFile := m.buildGeneralContexts(ctx, inputs)
401
Inseob Kim2bcc0452020-12-21 13:16:44 +0900402 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
403 ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q")
404 if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) {
Inseob Kim085f22f2023-11-09 11:13:01 +0900405 builtCtxFile = m.checkVendorPropertyNamespace(ctx, builtCtxFile)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900406 }
407
Inseob Kimcd616492020-03-24 23:06:40 +0900408 var apiFiles android.Paths
409 ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) {
Inseob Kim3a3539a2021-01-15 18:10:29 +0900410 i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath })
Inseob Kimcd616492020-03-24 23:06:40 +0900411 if !ok {
412 panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName()))
413 }
Inseob Kim3a3539a2021-01-15 18:10:29 +0900414 if api := i.CurrentSyspropApiFile(); api.Valid() {
415 apiFiles = append(apiFiles, api.Path())
416 }
Inseob Kimcd616492020-03-24 23:06:40 +0900417 })
418
419 // check compatibility with sysprop_library
420 if len(apiFiles) > 0 {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900421 out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked")
Colin Cross242c8bc2020-11-16 17:58:17 -0800422 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimcd616492020-03-24 23:06:40 +0900423
424 msg := `\n******************************\n` +
425 `API of sysprop_library doesn't match with property_contexts\n` +
426 `Please fix the breakage and rebuild.\n` +
427 `******************************\n`
428
429 rule.Command().
430 Text("( ").
Colin Cross242c8bc2020-11-16 17:58:17 -0800431 BuiltTool("sysprop_type_checker").
Inseob Kimcd616492020-03-24 23:06:40 +0900432 FlagForEachInput("--api ", apiFiles).
433 FlagWithInput("--context ", builtCtxFile).
434 Text(" || ( echo").Flag("-e").
435 Flag(`"` + msg + `"`).
436 Text("; exit 38) )")
437
438 rule.Command().Text("cp -f").Input(builtCtxFile).Output(out)
Colin Cross242c8bc2020-11-16 17:58:17 -0800439 rule.Build("property_contexts_check_api", "checking API: "+m.Name())
Inseob Kimcd616492020-03-24 23:06:40 +0900440 builtCtxFile = out
441 }
442
443 return builtCtxFile
Inseob Kimb554e592019-04-15 20:10:46 +0900444}
445
Inseob Kim06518b12023-08-25 21:20:08 +0900446func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool {
447 if !ctx.SocSpecific() && !ctx.DeviceSpecific() {
448 return false
449 }
450
451 return ctx.DeviceConfig().CheckVendorSeappViolations()
452}
453
Inseob Kim2dac2672021-12-29 17:54:57 +0900454func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900455 neverallowFile := pathForModuleOut(ctx, "neverallow")
Inseob Kim085f22f2023-11-09 11:13:01 +0900456 ret := pathForModuleOut(ctx, "checkseapp", m.stem())
Inseob Kim2dac2672021-12-29 17:54:57 +0900457
Inseob Kim085f22f2023-11-09 11:13:01 +0900458 // Step 1. Generate a M4 processed neverallow file
459 flags := m.getBuildFlags(ctx)
460 m4NeverallowFile := pathForModuleOut(ctx, "neverallow.m4out")
Inseob Kim2dac2672021-12-29 17:54:57 +0900461 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim085f22f2023-11-09 11:13:01 +0900462 rule.Command().
463 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
464 Flag("--fatal-warnings").
465 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
466 Flags(flagsToM4Macros(flags)).
467 Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)).
468 FlagWithOutput("> ", m4NeverallowFile)
469
470 rule.Temporary(m4NeverallowFile)
471 rule.Command().
472 Text("( grep").
Inseob Kim2dac2672021-12-29 17:54:57 +0900473 Flag("-ihe").
474 Text("'^neverallow'").
Inseob Kim085f22f2023-11-09 11:13:01 +0900475 Input(m4NeverallowFile).
Inseob Kim2dac2672021-12-29 17:54:57 +0900476 Text(">").
477 Output(neverallowFile).
Inseob Kim085f22f2023-11-09 11:13:01 +0900478 Text("|| true )") // to make ninja happy even when result is empty
Inseob Kim2dac2672021-12-29 17:54:57 +0900479
Inseob Kim085f22f2023-11-09 11:13:01 +0900480 // Step 2. Generate a M4 processed contexts file
481 builtCtx := m.buildGeneralContexts(ctx, inputs)
482
483 // Step 3. checkseapp
Inseob Kim2dac2672021-12-29 17:54:57 +0900484 rule.Temporary(neverallowFile)
Inseob Kimd7d36092023-06-26 20:48:48 +0900485 checkCmd := rule.Command().BuiltTool("checkseapp").
Inseob Kim2dac2672021-12-29 17:54:57 +0900486 FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))).
487 FlagWithOutput("-o ", ret).
Inseob Kim085f22f2023-11-09 11:13:01 +0900488 Input(builtCtx).
Inseob Kim2dac2672021-12-29 17:54:57 +0900489 Input(neverallowFile)
490
Inseob Kim06518b12023-08-25 21:20:08 +0900491 if m.shouldCheckCoredomain(ctx) {
492 checkCmd.Flag("-c") // check coredomain for vendor contexts
Inseob Kimd7d36092023-06-26 20:48:48 +0900493 }
494
Inseob Kim2dac2672021-12-29 17:54:57 +0900495 rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name())
496 return ret
497}
498
Inseob Kimb554e592019-04-15 20:10:46 +0900499func hwServiceFactory() android.Module {
500 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000501 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900502 return m
503}
504
505func propertyFactory() android.Module {
506 m := newModule()
Inseob Kimcd616492020-03-24 23:06:40 +0900507 m.build = m.buildPropertyContexts
508 m.deps = m.propertyContextsDeps
Inseob Kimb554e592019-04-15 20:10:46 +0900509 return m
510}
511
512func serviceFactory() android.Module {
513 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000514 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900515 return m
516}
Janis Danisevskisc40681f2020-07-25 13:02:29 -0700517
518func keystoreKeyFactory() android.Module {
519 m := newModule()
520 m.build = m.buildGeneralContexts
521 return m
522}
Yuntao Xu42e732c2021-11-18 22:33:02 +0000523
Inseob Kim2dac2672021-12-29 17:54:57 +0900524func seappFactory() android.Module {
525 m := newModule()
526 m.build = m.buildSeappContexts
527 return m
528}
529
Inseob Kimc7596c42022-02-25 11:45:41 +0900530func vndServiceFactory() android.Module {
531 m := newModule()
532 m.build = m.buildGeneralContexts
533 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
534 if !ctx.SocSpecific() {
535 ctx.ModuleErrorf(m.Name(), "must set vendor: true")
536 return
537 }
538 })
539 return m
540}
541
Yuntao Xu42e732c2021-11-18 22:33:02 +0000542var _ android.OutputFileProducer = (*selinuxContextsModule)(nil)
543
544// Implements android.OutputFileProducer
545func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) {
546 if tag == "" {
547 return []android.Path{m.outputPath}, nil
548 }
549 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
550}
Inseob Kimb5e23532022-02-16 02:26:11 +0000551
552type contextsTestProperties struct {
553 // Contexts files to be tested.
554 Srcs []string `android:"path"`
555
556 // Precompiled sepolicy binary to be tesed together.
557 Sepolicy *string `android:"path"`
558}
559
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100560type fileContextsTestProperties struct {
561 // Test data. File passed to `checkfc -t` to validate how contexts are resolved.
562 Test_data *string `android:"path"`
563}
564
Inseob Kimb5e23532022-02-16 02:26:11 +0000565type contextsTestModule struct {
566 android.ModuleBase
567
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100568 // The type of context.
569 context contextType
Inseob Kimb5e23532022-02-16 02:26:11 +0000570
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100571 properties contextsTestProperties
572 fileProperties fileContextsTestProperties
573 testTimestamp android.OutputPath
Inseob Kimb5e23532022-02-16 02:26:11 +0000574}
575
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100576type contextType int
577
578const (
579 FileContext contextType = iota
580 PropertyContext
581 ServiceContext
582 HwServiceContext
583 VndServiceContext
584)
585
Inseob Kimb5e23532022-02-16 02:26:11 +0000586// checkfc parses a context file and checks for syntax errors.
587// If -s is specified, the service backend is used to verify binder services.
588// If -l is specified, the service backend is used to verify hwbinder services.
589// Otherwise, context_file is assumed to be a file_contexts file
590// If -e is specified, then the context_file is allowed to be empty.
591
592// file_contexts_test tests given file_contexts files with checkfc.
593func fileContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100594 m := &contextsTestModule{context: FileContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000595 m.AddProperties(&m.properties)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100596 m.AddProperties(&m.fileProperties)
Inseob Kimb5e23532022-02-16 02:26:11 +0000597 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
598 return m
599}
600
601// property_contexts_test tests given property_contexts files with property_info_checker.
602func propertyContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100603 m := &contextsTestModule{context: PropertyContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000604 m.AddProperties(&m.properties)
605 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
606 return m
607}
608
609// hwservice_contexts_test tests given hwservice_contexts files with checkfc.
610func hwserviceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100611 m := &contextsTestModule{context: HwServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000612 m.AddProperties(&m.properties)
613 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
614 return m
615}
616
617// service_contexts_test tests given service_contexts files with checkfc.
618func serviceContextsTestFactory() android.Module {
619 // checkfc -s: service_contexts test
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100620 m := &contextsTestModule{context: ServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000621 m.AddProperties(&m.properties)
622 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
623 return m
624}
625
Inseob Kimc7596c42022-02-25 11:45:41 +0900626// vndservice_contexts_test tests given vndservice_contexts files with checkfc.
627func vndServiceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100628 m := &contextsTestModule{context: VndServiceContext}
Inseob Kimc7596c42022-02-25 11:45:41 +0900629 m.AddProperties(&m.properties)
630 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
631 return m
632}
633
Inseob Kimb5e23532022-02-16 02:26:11 +0000634func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100635 tool := "checkfc"
636 if m.context == PropertyContext {
637 tool = "property_info_checker"
Inseob Kimb5e23532022-02-16 02:26:11 +0000638 }
639
640 if len(m.properties.Srcs) == 0 {
641 ctx.PropertyErrorf("srcs", "can't be empty")
642 return
643 }
644
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100645 validateWithPolicy := true
Inseob Kimb5e23532022-02-16 02:26:11 +0000646 if proptools.String(m.properties.Sepolicy) == "" {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100647 if m.context == FileContext {
648 if proptools.String(m.fileProperties.Test_data) == "" {
649 ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided")
650 return
651 }
652 validateWithPolicy = false
653 } else {
654 ctx.PropertyErrorf("sepolicy", "can't be empty")
655 return
656 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000657 }
658
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100659 flags := []string(nil)
660 switch m.context {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100661 case FileContext:
662 if !validateWithPolicy {
663 flags = []string{"-t"}
664 }
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100665 case ServiceContext:
666 flags = []string{"-s" /* binder services */}
667 case HwServiceContext:
668 flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */}
669 case VndServiceContext:
670 flags = []string{"-e" /* allow empty */, "-v" /* vnd service */}
671 }
672
Inseob Kimb5e23532022-02-16 02:26:11 +0000673 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
Inseob Kimb5e23532022-02-16 02:26:11 +0000674 rule := android.NewRuleBuilder(pctx, ctx)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100675
676 if validateWithPolicy {
677 sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy))
678 rule.Command().BuiltTool(tool).
679 Flags(flags).
680 Input(sepolicy).
681 Inputs(srcs)
682 } else {
683 test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data))
684 rule.Command().BuiltTool(tool).
685 Flags(flags).
686 Inputs(srcs).
687 Input(test_data)
688 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000689
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900690 m.testTimestamp = pathForModuleOut(ctx, "timestamp")
Inseob Kimb5e23532022-02-16 02:26:11 +0000691 rule.Command().Text("touch").Output(m.testTimestamp)
692 rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName())
693}
694
695func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries {
696 return []android.AndroidMkEntries{android.AndroidMkEntries{
697 Class: "FAKE",
698 // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
699 // Without OutputFile this module won't be exported to Makefile.
700 OutputFile: android.OptionalPathForPath(m.testTimestamp),
701 Include: "$(BUILD_PHONY_PACKAGE)",
702 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
703 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
704 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String())
705 },
706 },
707 }}
708}
709
710// contextsTestModule implements ImageInterface to be able to include recovery_available contexts
711// modules as its sources.
712func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
713}
714
715func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
716 return true
717}
718
719func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
720 return false
721}
722
723func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
724 return false
725}
726
727func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
728 return false
729}
730
731func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
732 return false
733}
734
735func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
736 return nil
737}
738
739func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
740}
741
742var _ android.ImageInterface = (*contextsTestModule)(nil)