blob: 1282b9041e73b993982a273fefa3b10c5fc5c79c [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) {
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900113 m.flagDeps(ctx)
114
Inseob Kimcd616492020-03-24 23:06:40 +0900115 if m.deps != nil {
116 m.deps(ctx)
117 }
Inseob Kimfa6fe472021-01-12 13:40:27 +0900118
119 if m.InRecovery() && !m.onlyInRecovery() {
120 ctx.AddFarVariationDependencies([]blueprint.Variation{
121 {Mutator: "image", Variation: android.CoreVariation},
122 }, reuseContextsDepTag, ctx.ModuleName())
123 }
Inseob Kimcd616492020-03-24 23:06:40 +0900124}
125
126func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) {
127 for _, lib := range sysprop.SyspropLibraries(ctx.Config()) {
128 ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib)
129 }
130}
131
Yuntao Xu42e732c2021-11-18 22:33:02 +0000132func (m *selinuxContextsModule) stem() string {
133 return proptools.StringDefault(m.properties.Stem, m.Name())
134}
135
Inseob Kimb554e592019-04-15 20:10:46 +0900136func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900137 if m.InRecovery() {
Colin Cross040f1512019-10-02 10:36:09 -0700138 // Installing context files at the root of the recovery partition
139 m.installPath = android.PathForModuleInstall(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900140 } else {
141 m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
142 }
143
Inseob Kimfa6fe472021-01-12 13:40:27 +0900144 if m.InRecovery() && !m.onlyInRecovery() {
Inseob Kimb554e592019-04-15 20:10:46 +0900145 dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag)
146
147 if reuseDeps, ok := dep.(*selinuxContextsModule); ok {
148 m.outputPath = reuseDeps.outputPath
Yuntao Xu42e732c2021-11-18 22:33:02 +0000149 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900150 return
151 }
152 }
153
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900154 m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs))
Yuntao Xu42e732c2021-11-18 22:33:02 +0000155 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900156}
157
158func newModule() *selinuxContextsModule {
159 m := &selinuxContextsModule{}
160 m.AddProperties(
161 &m.properties,
Inseob Kim2dac2672021-12-29 17:54:57 +0900162 &m.seappProperties,
Inseob Kimb554e592019-04-15 20:10:46 +0900163 )
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900164 initFlaggableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900165 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900166 android.InitDefaultableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900167 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
168 m.selinuxContextsHook(ctx)
169 })
170 return m
171}
172
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900173type contextsDefaults struct {
174 android.ModuleBase
175 android.DefaultsModuleBase
176}
177
178// contexts_defaults provides a set of properties that can be inherited by other contexts modules.
179// (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a
180// contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are
181// erged (when possible) by prepending the default module's values to the depending module's values.
182func contextsDefaultsFactory() android.Module {
183 m := &contextsDefaults{}
184 m.AddProperties(
185 &selinuxContextsProperties{},
186 &seappProperties{},
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900187 &flaggableModuleProperties{},
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900188 )
189 android.InitDefaultsModule(m)
190 return m
191}
192
Inseob Kimb554e592019-04-15 20:10:46 +0900193func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) {
194 // TODO: clean this up to use build/soong/android/variable.go after b/79249983
195 var srcs []string
196
Inseob Kimb554e592019-04-15 20:10:46 +0900197 for _, sanitize := range ctx.Config().SanitizeDevice() {
198 if sanitize == "address" {
199 srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...)
200 break
201 }
202 }
203
204 m.properties.Srcs = append(m.properties.Srcs, srcs...)
205}
206
207func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData {
Colin Crossf82aed02021-11-04 17:25:55 -0700208 nameSuffix := ""
209 if m.InRecovery() && !m.onlyInRecovery() {
210 nameSuffix = ".recovery"
211 }
Inseob Kimb554e592019-04-15 20:10:46 +0900212 return android.AndroidMkData{
Colin Crossf82aed02021-11-04 17:25:55 -0700213 Class: "ETC",
214 OutputFile: android.OptionalPathForPath(m.outputPath),
215 SubName: nameSuffix,
216 Extra: []android.AndroidMkExtraFunc{
217 func(w io.Writer, outputFile android.Path) {
Colin Cross6c7f9372022-01-11 19:35:43 -0800218 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000219 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem())
Colin Crossf82aed02021-11-04 17:25:55 -0700220 },
Inseob Kimb554e592019-04-15 20:10:46 +0900221 },
222 }
223}
224
Inseob Kimfa6fe472021-01-12 13:40:27 +0900225func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000226 if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900227 ctx.PropertyErrorf("recovery_available",
228 "doesn't make sense at the same time as `recovery: true`")
Inseob Kimb554e592019-04-15 20:10:46 +0900229 }
230}
231
Inseob Kimfa6fe472021-01-12 13:40:27 +0900232func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000233 return !m.ModuleBase.InstallInRecovery()
Inseob Kimfa6fe472021-01-12 13:40:27 +0900234}
235
236func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
237 return false
238}
239
240func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
241 return false
242}
243
Inseob Kim6cc75f42021-04-29 13:53:20 +0000244func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
245 return false
246}
247
Inseob Kimfa6fe472021-01-12 13:40:27 +0900248func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000249 return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available)
Inseob Kimfa6fe472021-01-12 13:40:27 +0900250}
251
252func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
253 return nil
254}
255
256func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
257}
258
259var _ android.ImageInterface = (*selinuxContextsModule)(nil)
260
Inseob Kimcd616492020-03-24 23:06:40 +0900261func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900262 builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out")
Inseob Kimb554e592019-04-15 20:10:46 +0900263
Colin Cross242c8bc2020-11-16 17:58:17 -0800264 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900265
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900266 newlineFile := pathForModuleOut(ctx, "newline")
Inseob Kim35e9d412023-01-04 15:27:32 +0900267
268 rule.Command().Text("echo").FlagWithOutput("> ", newlineFile)
269 rule.Temporary(newlineFile)
270
271 var inputsWithNewline android.Paths
272 for _, input := range inputs {
273 inputsWithNewline = append(inputsWithNewline, input, newlineFile)
274 }
275
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900276 flags := m.getBuildFlags(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900277 rule.Command().
Dan Willemsen3c3e59b2019-06-19 10:52:50 -0700278 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
279 Text("--fatal-warnings -s").
Inseob Kimb554e592019-04-15 20:10:46 +0900280 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900281 Flags(flagsToM4Macros(flags)).
Inseob Kim35e9d412023-01-04 15:27:32 +0900282 Inputs(inputsWithNewline).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000283 FlagWithOutput("> ", builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900284
285 if proptools.Bool(m.properties.Remove_comment) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000286 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900287
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900288 remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment")
Inseob Kimb554e592019-04-15 20:10:46 +0900289
290 rule.Command().
291 Text("sed -e 's/#.*$//' -e '/^$/d'").
Yuntao Xu42e732c2021-11-18 22:33:02 +0000292 Input(builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900293 FlagWithOutput("> ", remove_comment_output)
294
Yuntao Xu42e732c2021-11-18 22:33:02 +0000295 builtContext = remove_comment_output
Inseob Kimb554e592019-04-15 20:10:46 +0900296 }
297
298 if proptools.Bool(m.properties.Fc_sort) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000299 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900300
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900301 sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted")
Inseob Kimb554e592019-04-15 20:10:46 +0900302
303 rule.Command().
304 Tool(ctx.Config().HostToolPath(ctx, "fc_sort")).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000305 FlagWithInput("-i ", builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900306 FlagWithOutput("-o ", sorted_output)
307
Yuntao Xu42e732c2021-11-18 22:33:02 +0000308 builtContext = sorted_output
Inseob Kimb554e592019-04-15 20:10:46 +0900309 }
310
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900311 ret := pathForModuleOut(ctx, m.stem())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000312 rule.Temporary(builtContext)
313 rule.Command().Text("cp").Input(builtContext).Output(ret)
Inseob Kimb554e592019-04-15 20:10:46 +0900314
315 rule.DeleteTemporaryFiles()
Yuntao Xu42e732c2021-11-18 22:33:02 +0000316 rule.Build("selinux_contexts", "building contexts: "+m.Name())
Inseob Kimb554e592019-04-15 20:10:46 +0900317
Inseob Kimcd616492020-03-24 23:06:40 +0900318 return ret
Inseob Kimb554e592019-04-15 20:10:46 +0900319}
320
Inseob Kimcd616492020-03-24 23:06:40 +0900321func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimdfa4a482023-11-01 17:58:18 +0900322 if m.properties.Remove_comment == nil {
323 m.properties.Remove_comment = proptools.BoolPtr(true)
Inseob Kimb554e592019-04-15 20:10:46 +0900324 }
Inseob Kimcd616492020-03-24 23:06:40 +0900325 return m.buildGeneralContexts(ctx, inputs)
Inseob Kimb554e592019-04-15 20:10:46 +0900326}
327
328func fileFactory() android.Module {
329 m := newModule()
Inseob Kimb554e592019-04-15 20:10:46 +0900330 m.build = m.buildFileContexts
331 return m
332}
333
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000334func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimb554e592019-04-15 20:10:46 +0900335 if m.properties.Remove_comment == nil {
336 m.properties.Remove_comment = proptools.BoolPtr(true)
337 }
338
Inseob Kimcd616492020-03-24 23:06:40 +0900339 return m.buildGeneralContexts(ctx, inputs)
340}
341
Inseob Kim085f22f2023-11-09 11:13:01 +0900342func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, input android.Path) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900343 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
344 ApiLevelR := android.ApiLevelOrPanic(ctx, "R")
345
346 rule := android.NewRuleBuilder(pctx, ctx)
347
348 // This list is from vts_treble_sys_prop_test.
349 allowedPropertyPrefixes := []string{
350 "ctl.odm.",
351 "ctl.vendor.",
352 "ctl.start$odm.",
353 "ctl.start$vendor.",
354 "ctl.stop$odm.",
355 "ctl.stop$vendor.",
356 "init.svc.odm.",
357 "init.svc.vendor.",
358 "ro.boot.",
359 "ro.hardware.",
360 "ro.odm.",
361 "ro.vendor.",
362 "odm.",
363 "persist.odm.",
364 "persist.vendor.",
365 "vendor.",
366 }
367
368 // persist.camera is also allowed for devices launching with R or eariler
369 if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) {
370 allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.")
371 }
372
373 var allowedContextPrefixes []string
374
375 if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) {
376 // This list is from vts_treble_sys_prop_test.
377 allowedContextPrefixes = []string{
378 "vendor_",
379 "odm_",
380 }
381 }
382
Inseob Kim085f22f2023-11-09 11:13:01 +0900383 cmd := rule.Command().
384 BuiltTool("check_prop_prefix").
385 FlagWithInput("--property-contexts ", input).
386 FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$'
387 FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900388
Inseob Kim085f22f2023-11-09 11:13:01 +0900389 if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() {
390 cmd.Flag("--strict")
Inseob Kim2bcc0452020-12-21 13:16:44 +0900391 }
Inseob Kim085f22f2023-11-09 11:13:01 +0900392
Luca Stefani0b2d7112023-11-16 17:42:52 +0100393 out := pathForModuleOut(ctx, ctx.ModuleName()+"_namespace_checked")
Inseob Kim085f22f2023-11-09 11:13:01 +0900394 rule.Command().Text("cp -f").Input(input).Output(out)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900395 rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName())
Inseob Kim085f22f2023-11-09 11:13:01 +0900396 return out
Inseob Kim2bcc0452020-12-21 13:16:44 +0900397}
398
Inseob Kimcd616492020-03-24 23:06:40 +0900399func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900400 // vendor/odm properties are enforced for devices launching with Android Q or later. So, if
401 // vendor/odm, make sure that only vendor/odm properties exist.
Inseob Kim085f22f2023-11-09 11:13:01 +0900402 builtCtxFile := m.buildGeneralContexts(ctx, inputs)
403
Inseob Kim2bcc0452020-12-21 13:16:44 +0900404 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
405 ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q")
406 if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) {
Inseob Kim085f22f2023-11-09 11:13:01 +0900407 builtCtxFile = m.checkVendorPropertyNamespace(ctx, builtCtxFile)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900408 }
409
Inseob Kimcd616492020-03-24 23:06:40 +0900410 var apiFiles android.Paths
411 ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) {
Inseob Kim3a3539a2021-01-15 18:10:29 +0900412 i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath })
Inseob Kimcd616492020-03-24 23:06:40 +0900413 if !ok {
414 panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName()))
415 }
Inseob Kim3a3539a2021-01-15 18:10:29 +0900416 if api := i.CurrentSyspropApiFile(); api.Valid() {
417 apiFiles = append(apiFiles, api.Path())
418 }
Inseob Kimcd616492020-03-24 23:06:40 +0900419 })
420
421 // check compatibility with sysprop_library
422 if len(apiFiles) > 0 {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900423 out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked")
Colin Cross242c8bc2020-11-16 17:58:17 -0800424 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimcd616492020-03-24 23:06:40 +0900425
426 msg := `\n******************************\n` +
427 `API of sysprop_library doesn't match with property_contexts\n` +
428 `Please fix the breakage and rebuild.\n` +
429 `******************************\n`
430
431 rule.Command().
432 Text("( ").
Colin Cross242c8bc2020-11-16 17:58:17 -0800433 BuiltTool("sysprop_type_checker").
Inseob Kimcd616492020-03-24 23:06:40 +0900434 FlagForEachInput("--api ", apiFiles).
435 FlagWithInput("--context ", builtCtxFile).
436 Text(" || ( echo").Flag("-e").
437 Flag(`"` + msg + `"`).
438 Text("; exit 38) )")
439
440 rule.Command().Text("cp -f").Input(builtCtxFile).Output(out)
Colin Cross242c8bc2020-11-16 17:58:17 -0800441 rule.Build("property_contexts_check_api", "checking API: "+m.Name())
Inseob Kimcd616492020-03-24 23:06:40 +0900442 builtCtxFile = out
443 }
444
445 return builtCtxFile
Inseob Kimb554e592019-04-15 20:10:46 +0900446}
447
Inseob Kim06518b12023-08-25 21:20:08 +0900448func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool {
449 if !ctx.SocSpecific() && !ctx.DeviceSpecific() {
450 return false
451 }
452
453 return ctx.DeviceConfig().CheckVendorSeappViolations()
454}
455
Inseob Kim2dac2672021-12-29 17:54:57 +0900456func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900457 neverallowFile := pathForModuleOut(ctx, "neverallow")
Inseob Kim085f22f2023-11-09 11:13:01 +0900458 ret := pathForModuleOut(ctx, "checkseapp", m.stem())
Inseob Kim2dac2672021-12-29 17:54:57 +0900459
Inseob Kim085f22f2023-11-09 11:13:01 +0900460 // Step 1. Generate a M4 processed neverallow file
461 flags := m.getBuildFlags(ctx)
462 m4NeverallowFile := pathForModuleOut(ctx, "neverallow.m4out")
Inseob Kim2dac2672021-12-29 17:54:57 +0900463 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim085f22f2023-11-09 11:13:01 +0900464 rule.Command().
465 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
466 Flag("--fatal-warnings").
467 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
468 Flags(flagsToM4Macros(flags)).
469 Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)).
470 FlagWithOutput("> ", m4NeverallowFile)
471
472 rule.Temporary(m4NeverallowFile)
473 rule.Command().
474 Text("( grep").
Inseob Kim2dac2672021-12-29 17:54:57 +0900475 Flag("-ihe").
476 Text("'^neverallow'").
Inseob Kim085f22f2023-11-09 11:13:01 +0900477 Input(m4NeverallowFile).
Inseob Kim2dac2672021-12-29 17:54:57 +0900478 Text(">").
479 Output(neverallowFile).
Inseob Kim085f22f2023-11-09 11:13:01 +0900480 Text("|| true )") // to make ninja happy even when result is empty
Inseob Kim2dac2672021-12-29 17:54:57 +0900481
Inseob Kim085f22f2023-11-09 11:13:01 +0900482 // Step 2. Generate a M4 processed contexts file
483 builtCtx := m.buildGeneralContexts(ctx, inputs)
484
485 // Step 3. checkseapp
Inseob Kim2dac2672021-12-29 17:54:57 +0900486 rule.Temporary(neverallowFile)
Inseob Kimd7d36092023-06-26 20:48:48 +0900487 checkCmd := rule.Command().BuiltTool("checkseapp").
Inseob Kim2dac2672021-12-29 17:54:57 +0900488 FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))).
489 FlagWithOutput("-o ", ret).
Inseob Kim085f22f2023-11-09 11:13:01 +0900490 Input(builtCtx).
Inseob Kim2dac2672021-12-29 17:54:57 +0900491 Input(neverallowFile)
492
Inseob Kim06518b12023-08-25 21:20:08 +0900493 if m.shouldCheckCoredomain(ctx) {
494 checkCmd.Flag("-c") // check coredomain for vendor contexts
Inseob Kimd7d36092023-06-26 20:48:48 +0900495 }
496
Inseob Kim2dac2672021-12-29 17:54:57 +0900497 rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name())
498 return ret
499}
500
Inseob Kimb554e592019-04-15 20:10:46 +0900501func hwServiceFactory() android.Module {
502 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000503 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900504 return m
505}
506
507func propertyFactory() android.Module {
508 m := newModule()
Inseob Kimcd616492020-03-24 23:06:40 +0900509 m.build = m.buildPropertyContexts
510 m.deps = m.propertyContextsDeps
Inseob Kimb554e592019-04-15 20:10:46 +0900511 return m
512}
513
514func serviceFactory() android.Module {
515 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000516 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900517 return m
518}
Janis Danisevskisc40681f2020-07-25 13:02:29 -0700519
520func keystoreKeyFactory() android.Module {
521 m := newModule()
522 m.build = m.buildGeneralContexts
523 return m
524}
Yuntao Xu42e732c2021-11-18 22:33:02 +0000525
Inseob Kim2dac2672021-12-29 17:54:57 +0900526func seappFactory() android.Module {
527 m := newModule()
528 m.build = m.buildSeappContexts
529 return m
530}
531
Inseob Kimc7596c42022-02-25 11:45:41 +0900532func vndServiceFactory() android.Module {
533 m := newModule()
534 m.build = m.buildGeneralContexts
535 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
536 if !ctx.SocSpecific() {
537 ctx.ModuleErrorf(m.Name(), "must set vendor: true")
538 return
539 }
540 })
541 return m
542}
543
Yuntao Xu42e732c2021-11-18 22:33:02 +0000544var _ android.OutputFileProducer = (*selinuxContextsModule)(nil)
545
546// Implements android.OutputFileProducer
547func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) {
548 if tag == "" {
549 return []android.Path{m.outputPath}, nil
550 }
551 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
552}
Inseob Kimb5e23532022-02-16 02:26:11 +0000553
554type contextsTestProperties struct {
555 // Contexts files to be tested.
556 Srcs []string `android:"path"`
557
558 // Precompiled sepolicy binary to be tesed together.
559 Sepolicy *string `android:"path"`
560}
561
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100562type fileContextsTestProperties struct {
563 // Test data. File passed to `checkfc -t` to validate how contexts are resolved.
564 Test_data *string `android:"path"`
565}
566
Inseob Kimb5e23532022-02-16 02:26:11 +0000567type contextsTestModule struct {
568 android.ModuleBase
569
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100570 // The type of context.
571 context contextType
Inseob Kimb5e23532022-02-16 02:26:11 +0000572
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100573 properties contextsTestProperties
574 fileProperties fileContextsTestProperties
575 testTimestamp android.OutputPath
Inseob Kimb5e23532022-02-16 02:26:11 +0000576}
577
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100578type contextType int
579
580const (
581 FileContext contextType = iota
582 PropertyContext
583 ServiceContext
584 HwServiceContext
585 VndServiceContext
586)
587
Inseob Kimb5e23532022-02-16 02:26:11 +0000588// checkfc parses a context file and checks for syntax errors.
589// If -s is specified, the service backend is used to verify binder services.
590// If -l is specified, the service backend is used to verify hwbinder services.
591// Otherwise, context_file is assumed to be a file_contexts file
592// If -e is specified, then the context_file is allowed to be empty.
593
594// file_contexts_test tests given file_contexts files with checkfc.
595func fileContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100596 m := &contextsTestModule{context: FileContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000597 m.AddProperties(&m.properties)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100598 m.AddProperties(&m.fileProperties)
Inseob Kimb5e23532022-02-16 02:26:11 +0000599 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
600 return m
601}
602
603// property_contexts_test tests given property_contexts files with property_info_checker.
604func propertyContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100605 m := &contextsTestModule{context: PropertyContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000606 m.AddProperties(&m.properties)
607 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
608 return m
609}
610
611// hwservice_contexts_test tests given hwservice_contexts files with checkfc.
612func hwserviceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100613 m := &contextsTestModule{context: HwServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000614 m.AddProperties(&m.properties)
615 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
616 return m
617}
618
619// service_contexts_test tests given service_contexts files with checkfc.
620func serviceContextsTestFactory() android.Module {
621 // checkfc -s: service_contexts test
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100622 m := &contextsTestModule{context: ServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000623 m.AddProperties(&m.properties)
624 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
625 return m
626}
627
Inseob Kimc7596c42022-02-25 11:45:41 +0900628// vndservice_contexts_test tests given vndservice_contexts files with checkfc.
629func vndServiceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100630 m := &contextsTestModule{context: VndServiceContext}
Inseob Kimc7596c42022-02-25 11:45:41 +0900631 m.AddProperties(&m.properties)
632 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
633 return m
634}
635
Inseob Kimb5e23532022-02-16 02:26:11 +0000636func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100637 tool := "checkfc"
638 if m.context == PropertyContext {
639 tool = "property_info_checker"
Inseob Kimb5e23532022-02-16 02:26:11 +0000640 }
641
642 if len(m.properties.Srcs) == 0 {
643 ctx.PropertyErrorf("srcs", "can't be empty")
644 return
645 }
646
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100647 validateWithPolicy := true
Inseob Kimb5e23532022-02-16 02:26:11 +0000648 if proptools.String(m.properties.Sepolicy) == "" {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100649 if m.context == FileContext {
650 if proptools.String(m.fileProperties.Test_data) == "" {
651 ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided")
652 return
653 }
654 validateWithPolicy = false
655 } else {
656 ctx.PropertyErrorf("sepolicy", "can't be empty")
657 return
658 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000659 }
660
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100661 flags := []string(nil)
662 switch m.context {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100663 case FileContext:
664 if !validateWithPolicy {
665 flags = []string{"-t"}
666 }
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100667 case ServiceContext:
668 flags = []string{"-s" /* binder services */}
669 case HwServiceContext:
670 flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */}
671 case VndServiceContext:
672 flags = []string{"-e" /* allow empty */, "-v" /* vnd service */}
673 }
674
Inseob Kimb5e23532022-02-16 02:26:11 +0000675 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
Inseob Kimb5e23532022-02-16 02:26:11 +0000676 rule := android.NewRuleBuilder(pctx, ctx)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100677
678 if validateWithPolicy {
679 sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy))
680 rule.Command().BuiltTool(tool).
681 Flags(flags).
682 Input(sepolicy).
683 Inputs(srcs)
684 } else {
685 test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data))
686 rule.Command().BuiltTool(tool).
687 Flags(flags).
688 Inputs(srcs).
689 Input(test_data)
690 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000691
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900692 m.testTimestamp = pathForModuleOut(ctx, "timestamp")
Inseob Kimb5e23532022-02-16 02:26:11 +0000693 rule.Command().Text("touch").Output(m.testTimestamp)
694 rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName())
695}
696
697func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries {
698 return []android.AndroidMkEntries{android.AndroidMkEntries{
699 Class: "FAKE",
700 // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
701 // Without OutputFile this module won't be exported to Makefile.
702 OutputFile: android.OptionalPathForPath(m.testTimestamp),
703 Include: "$(BUILD_PHONY_PACKAGE)",
704 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
705 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
706 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String())
707 },
708 },
709 }}
710}
711
712// contextsTestModule implements ImageInterface to be able to include recovery_available contexts
713// modules as its sources.
714func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
715}
716
717func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
718 return true
719}
720
721func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
722 return false
723}
724
725func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
726 return false
727}
728
729func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
730 return false
731}
732
733func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
734 return false
735}
736
737func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
738 return nil
739}
740
741func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
742}
743
744var _ android.ImageInterface = (*contextsTestModule)(nil)