blob: fd1cd3422b519287000e4a8ca126195854cad5ec [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
Thiébaud Weksteenfe008ad2024-09-17 12:06:12 +100049
50 // Board api level of policy files. Set "current" for RELEASE_BOARD_API_LEVEL, or a direct
51 // version string (e.g. "202404"). Defaults to "current"
52 Board_api_level *string
Inseob Kimb554e592019-04-15 20:10:46 +090053}
54
Inseob Kim2dac2672021-12-29 17:54:57 +090055type seappProperties struct {
56 // Files containing neverallow rules.
57 Neverallow_files []string `android:"path"`
58
59 // Precompiled sepolicy binary file which will be fed to checkseapp.
60 Sepolicy *string `android:"path"`
61}
62
Inseob Kimb554e592019-04-15 20:10:46 +090063type selinuxContextsModule struct {
64 android.ModuleBase
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090065 android.DefaultableModuleBase
66 flaggableModuleBase
Inseob Kimb554e592019-04-15 20:10:46 +090067
Jooyung Han804e2342023-06-20 15:38:50 +090068 properties selinuxContextsProperties
69 seappProperties seappProperties
70 build func(ctx android.ModuleContext, inputs android.Paths) android.Path
71 deps func(ctx android.BottomUpMutatorContext)
72 outputPath android.Path
73 installPath android.InstallPath
Inseob Kimb554e592019-04-15 20:10:46 +090074}
75
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090076var _ flaggableModule = (*selinuxContextsModule)(nil)
77
Inseob Kimb554e592019-04-15 20:10:46 +090078var (
Inseob Kimcd616492020-03-24 23:06:40 +090079 reuseContextsDepTag = dependencyTag{name: "reuseContexts"}
80 syspropLibraryDepTag = dependencyTag{name: "sysprop_library"}
Inseob Kimb554e592019-04-15 20:10:46 +090081)
82
83func init() {
84 pctx.HostBinToolVariable("fc_sort", "fc_sort")
85
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090086 android.RegisterModuleType("contexts_defaults", contextsDefaultsFactory)
Inseob Kimb554e592019-04-15 20:10:46 +090087 android.RegisterModuleType("file_contexts", fileFactory)
88 android.RegisterModuleType("hwservice_contexts", hwServiceFactory)
89 android.RegisterModuleType("property_contexts", propertyFactory)
90 android.RegisterModuleType("service_contexts", serviceFactory)
Janis Danisevskisc40681f2020-07-25 13:02:29 -070091 android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory)
Inseob Kim2dac2672021-12-29 17:54:57 +090092 android.RegisterModuleType("seapp_contexts", seappFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +090093 android.RegisterModuleType("vndservice_contexts", vndServiceFactory)
Inseob Kimb5e23532022-02-16 02:26:11 +000094
95 android.RegisterModuleType("file_contexts_test", fileContextsTestFactory)
96 android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory)
97 android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory)
98 android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +090099 android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory)
Inseob Kimb554e592019-04-15 20:10:46 +0900100}
101
Colin Cross040f1512019-10-02 10:36:09 -0700102func (m *selinuxContextsModule) InstallInRoot() bool {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900103 return m.InRecovery()
104}
105
106func (m *selinuxContextsModule) InstallInRecovery() bool {
107 // ModuleBase.InRecovery() checks the image variant
108 return m.InRecovery()
109}
110
111func (m *selinuxContextsModule) onlyInRecovery() bool {
112 // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property
113 return m.ModuleBase.InstallInRecovery()
Colin Cross040f1512019-10-02 10:36:09 -0700114}
115
Inseob Kimcd616492020-03-24 23:06:40 +0900116func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900117 m.flagDeps(ctx)
118
Inseob Kimcd616492020-03-24 23:06:40 +0900119 if m.deps != nil {
120 m.deps(ctx)
121 }
Inseob Kimfa6fe472021-01-12 13:40:27 +0900122
123 if m.InRecovery() && !m.onlyInRecovery() {
124 ctx.AddFarVariationDependencies([]blueprint.Variation{
125 {Mutator: "image", Variation: android.CoreVariation},
126 }, reuseContextsDepTag, ctx.ModuleName())
127 }
Inseob Kimcd616492020-03-24 23:06:40 +0900128}
129
130func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) {
131 for _, lib := range sysprop.SyspropLibraries(ctx.Config()) {
132 ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib)
133 }
134}
135
Yuntao Xu42e732c2021-11-18 22:33:02 +0000136func (m *selinuxContextsModule) stem() string {
137 return proptools.StringDefault(m.properties.Stem, m.Name())
138}
139
Inseob Kimb554e592019-04-15 20:10:46 +0900140func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900141 if m.InRecovery() {
Colin Cross040f1512019-10-02 10:36:09 -0700142 // Installing context files at the root of the recovery partition
143 m.installPath = android.PathForModuleInstall(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900144 } else {
145 m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
146 }
147
Inseob Kimfa6fe472021-01-12 13:40:27 +0900148 if m.InRecovery() && !m.onlyInRecovery() {
Inseob Kimb554e592019-04-15 20:10:46 +0900149 dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag)
150
151 if reuseDeps, ok := dep.(*selinuxContextsModule); ok {
152 m.outputPath = reuseDeps.outputPath
Yuntao Xu42e732c2021-11-18 22:33:02 +0000153 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900154 return
155 }
156 }
157
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900158 m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs))
Yuntao Xu42e732c2021-11-18 22:33:02 +0000159 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
mrziwangdc268a72024-06-06 14:42:10 -0700160
161 ctx.SetOutputFiles([]android.Path{m.outputPath}, "")
Inseob Kimb554e592019-04-15 20:10:46 +0900162}
163
164func newModule() *selinuxContextsModule {
165 m := &selinuxContextsModule{}
166 m.AddProperties(
167 &m.properties,
Inseob Kim2dac2672021-12-29 17:54:57 +0900168 &m.seappProperties,
Inseob Kimb554e592019-04-15 20:10:46 +0900169 )
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900170 initFlaggableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900171 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900172 android.InitDefaultableModule(m)
Inseob Kimb554e592019-04-15 20:10:46 +0900173 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
174 m.selinuxContextsHook(ctx)
175 })
176 return m
177}
178
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900179type contextsDefaults struct {
180 android.ModuleBase
181 android.DefaultsModuleBase
182}
183
184// contexts_defaults provides a set of properties that can be inherited by other contexts modules.
185// (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a
186// contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are
187// erged (when possible) by prepending the default module's values to the depending module's values.
188func contextsDefaultsFactory() android.Module {
189 m := &contextsDefaults{}
190 m.AddProperties(
191 &selinuxContextsProperties{},
192 &seappProperties{},
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900193 &flaggableModuleProperties{},
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900194 )
195 android.InitDefaultsModule(m)
196 return m
197}
198
Inseob Kimb554e592019-04-15 20:10:46 +0900199func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) {
200 // TODO: clean this up to use build/soong/android/variable.go after b/79249983
201 var srcs []string
202
Inseob Kimb554e592019-04-15 20:10:46 +0900203 for _, sanitize := range ctx.Config().SanitizeDevice() {
204 if sanitize == "address" {
205 srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...)
206 break
207 }
208 }
209
210 m.properties.Srcs = append(m.properties.Srcs, srcs...)
211}
212
213func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData {
Colin Crossf82aed02021-11-04 17:25:55 -0700214 nameSuffix := ""
215 if m.InRecovery() && !m.onlyInRecovery() {
216 nameSuffix = ".recovery"
217 }
Inseob Kimb554e592019-04-15 20:10:46 +0900218 return android.AndroidMkData{
Colin Crossf82aed02021-11-04 17:25:55 -0700219 Class: "ETC",
220 OutputFile: android.OptionalPathForPath(m.outputPath),
221 SubName: nameSuffix,
222 Extra: []android.AndroidMkExtraFunc{
223 func(w io.Writer, outputFile android.Path) {
Colin Cross6c7f9372022-01-11 19:35:43 -0800224 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000225 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem())
Colin Crossf82aed02021-11-04 17:25:55 -0700226 },
Inseob Kimb554e592019-04-15 20:10:46 +0900227 },
228 }
229}
230
Inseob Kimfa6fe472021-01-12 13:40:27 +0900231func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000232 if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900233 ctx.PropertyErrorf("recovery_available",
234 "doesn't make sense at the same time as `recovery: true`")
Inseob Kimb554e592019-04-15 20:10:46 +0900235 }
236}
237
Jihoon Kang03f4d9d2024-06-21 06:36:33 +0000238func (m *selinuxContextsModule) VendorVariantNeeded(ctx android.BaseModuleContext) bool {
239 return false
240}
241
242func (m *selinuxContextsModule) ProductVariantNeeded(ctx android.BaseModuleContext) bool {
243 return false
244}
245
Inseob Kimfa6fe472021-01-12 13:40:27 +0900246func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000247 return !m.ModuleBase.InstallInRecovery()
Inseob Kimfa6fe472021-01-12 13:40:27 +0900248}
249
250func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
251 return false
252}
253
254func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
255 return false
256}
257
Inseob Kim6cc75f42021-04-29 13:53:20 +0000258func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
259 return false
260}
261
Inseob Kimfa6fe472021-01-12 13:40:27 +0900262func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000263 return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available)
Inseob Kimfa6fe472021-01-12 13:40:27 +0900264}
265
266func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
267 return nil
268}
269
Jihoon Kang8298ae52024-06-13 21:30:15 +0000270func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string) {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900271}
272
273var _ android.ImageInterface = (*selinuxContextsModule)(nil)
274
Inseob Kimcd616492020-03-24 23:06:40 +0900275func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900276 builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out")
Inseob Kimb554e592019-04-15 20:10:46 +0900277
Colin Cross242c8bc2020-11-16 17:58:17 -0800278 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900279
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900280 newlineFile := pathForModuleOut(ctx, "newline")
Inseob Kim35e9d412023-01-04 15:27:32 +0900281
282 rule.Command().Text("echo").FlagWithOutput("> ", newlineFile)
283 rule.Temporary(newlineFile)
284
285 var inputsWithNewline android.Paths
286 for _, input := range inputs {
287 inputsWithNewline = append(inputsWithNewline, input, newlineFile)
288 }
289
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900290 flags := m.getBuildFlags(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900291 rule.Command().
Dan Willemsen3c3e59b2019-06-19 10:52:50 -0700292 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
293 Text("--fatal-warnings -s").
Inseob Kimb554e592019-04-15 20:10:46 +0900294 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
Thiébaud Weksteenfe008ad2024-09-17 12:06:12 +1000295 Flag(boardApiLevelToM4Macro(ctx, m.properties.Board_api_level)).
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900296 Flags(flagsToM4Macros(flags)).
Inseob Kim35e9d412023-01-04 15:27:32 +0900297 Inputs(inputsWithNewline).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000298 FlagWithOutput("> ", builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900299
300 if proptools.Bool(m.properties.Remove_comment) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000301 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900302
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900303 remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment")
Inseob Kimb554e592019-04-15 20:10:46 +0900304
305 rule.Command().
306 Text("sed -e 's/#.*$//' -e '/^$/d'").
Yuntao Xu42e732c2021-11-18 22:33:02 +0000307 Input(builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900308 FlagWithOutput("> ", remove_comment_output)
309
Yuntao Xu42e732c2021-11-18 22:33:02 +0000310 builtContext = remove_comment_output
Inseob Kimb554e592019-04-15 20:10:46 +0900311 }
312
313 if proptools.Bool(m.properties.Fc_sort) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000314 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900315
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900316 sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted")
Inseob Kimb554e592019-04-15 20:10:46 +0900317
318 rule.Command().
319 Tool(ctx.Config().HostToolPath(ctx, "fc_sort")).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000320 FlagWithInput("-i ", builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900321 FlagWithOutput("-o ", sorted_output)
322
Yuntao Xu42e732c2021-11-18 22:33:02 +0000323 builtContext = sorted_output
Inseob Kimb554e592019-04-15 20:10:46 +0900324 }
325
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900326 ret := pathForModuleOut(ctx, m.stem())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000327 rule.Temporary(builtContext)
328 rule.Command().Text("cp").Input(builtContext).Output(ret)
Inseob Kimb554e592019-04-15 20:10:46 +0900329
330 rule.DeleteTemporaryFiles()
Yuntao Xu42e732c2021-11-18 22:33:02 +0000331 rule.Build("selinux_contexts", "building contexts: "+m.Name())
Inseob Kimb554e592019-04-15 20:10:46 +0900332
Inseob Kimcd616492020-03-24 23:06:40 +0900333 return ret
Inseob Kimb554e592019-04-15 20:10:46 +0900334}
335
Inseob Kimcd616492020-03-24 23:06:40 +0900336func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimdfa4a482023-11-01 17:58:18 +0900337 if m.properties.Remove_comment == nil {
338 m.properties.Remove_comment = proptools.BoolPtr(true)
Inseob Kimb554e592019-04-15 20:10:46 +0900339 }
Inseob Kimcd616492020-03-24 23:06:40 +0900340 return m.buildGeneralContexts(ctx, inputs)
Inseob Kimb554e592019-04-15 20:10:46 +0900341}
342
343func fileFactory() android.Module {
344 m := newModule()
Inseob Kimb554e592019-04-15 20:10:46 +0900345 m.build = m.buildFileContexts
346 return m
347}
348
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000349func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimb554e592019-04-15 20:10:46 +0900350 if m.properties.Remove_comment == nil {
351 m.properties.Remove_comment = proptools.BoolPtr(true)
352 }
353
Inseob Kimcd616492020-03-24 23:06:40 +0900354 return m.buildGeneralContexts(ctx, inputs)
355}
356
Inseob Kim085f22f2023-11-09 11:13:01 +0900357func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, input android.Path) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900358 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
359 ApiLevelR := android.ApiLevelOrPanic(ctx, "R")
360
361 rule := android.NewRuleBuilder(pctx, ctx)
362
363 // This list is from vts_treble_sys_prop_test.
364 allowedPropertyPrefixes := []string{
365 "ctl.odm.",
366 "ctl.vendor.",
367 "ctl.start$odm.",
368 "ctl.start$vendor.",
369 "ctl.stop$odm.",
370 "ctl.stop$vendor.",
371 "init.svc.odm.",
372 "init.svc.vendor.",
373 "ro.boot.",
374 "ro.hardware.",
375 "ro.odm.",
376 "ro.vendor.",
377 "odm.",
378 "persist.odm.",
379 "persist.vendor.",
380 "vendor.",
381 }
382
383 // persist.camera is also allowed for devices launching with R or eariler
384 if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) {
385 allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.")
386 }
387
388 var allowedContextPrefixes []string
389
390 if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) {
391 // This list is from vts_treble_sys_prop_test.
392 allowedContextPrefixes = []string{
393 "vendor_",
394 "odm_",
395 }
396 }
397
Inseob Kim085f22f2023-11-09 11:13:01 +0900398 cmd := rule.Command().
399 BuiltTool("check_prop_prefix").
400 FlagWithInput("--property-contexts ", input).
401 FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$'
402 FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900403
Inseob Kim085f22f2023-11-09 11:13:01 +0900404 if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() {
405 cmd.Flag("--strict")
Inseob Kim2bcc0452020-12-21 13:16:44 +0900406 }
Inseob Kim085f22f2023-11-09 11:13:01 +0900407
Luca Stefani0b2d7112023-11-16 17:42:52 +0100408 out := pathForModuleOut(ctx, ctx.ModuleName()+"_namespace_checked")
Inseob Kim085f22f2023-11-09 11:13:01 +0900409 rule.Command().Text("cp -f").Input(input).Output(out)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900410 rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName())
Inseob Kim085f22f2023-11-09 11:13:01 +0900411 return out
Inseob Kim2bcc0452020-12-21 13:16:44 +0900412}
413
Inseob Kimcd616492020-03-24 23:06:40 +0900414func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900415 // vendor/odm properties are enforced for devices launching with Android Q or later. So, if
416 // vendor/odm, make sure that only vendor/odm properties exist.
Inseob Kim085f22f2023-11-09 11:13:01 +0900417 builtCtxFile := m.buildGeneralContexts(ctx, inputs)
418
Inseob Kim2bcc0452020-12-21 13:16:44 +0900419 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
420 ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q")
421 if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) {
Inseob Kim085f22f2023-11-09 11:13:01 +0900422 builtCtxFile = m.checkVendorPropertyNamespace(ctx, builtCtxFile)
Inseob Kim2bcc0452020-12-21 13:16:44 +0900423 }
424
Inseob Kimcd616492020-03-24 23:06:40 +0900425 var apiFiles android.Paths
426 ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) {
Inseob Kim3a3539a2021-01-15 18:10:29 +0900427 i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath })
Inseob Kimcd616492020-03-24 23:06:40 +0900428 if !ok {
429 panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName()))
430 }
Inseob Kim3a3539a2021-01-15 18:10:29 +0900431 if api := i.CurrentSyspropApiFile(); api.Valid() {
432 apiFiles = append(apiFiles, api.Path())
433 }
Inseob Kimcd616492020-03-24 23:06:40 +0900434 })
435
436 // check compatibility with sysprop_library
437 if len(apiFiles) > 0 {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900438 out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked")
Colin Cross242c8bc2020-11-16 17:58:17 -0800439 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimcd616492020-03-24 23:06:40 +0900440
441 msg := `\n******************************\n` +
442 `API of sysprop_library doesn't match with property_contexts\n` +
443 `Please fix the breakage and rebuild.\n` +
444 `******************************\n`
445
446 rule.Command().
447 Text("( ").
Colin Cross242c8bc2020-11-16 17:58:17 -0800448 BuiltTool("sysprop_type_checker").
Inseob Kimcd616492020-03-24 23:06:40 +0900449 FlagForEachInput("--api ", apiFiles).
450 FlagWithInput("--context ", builtCtxFile).
451 Text(" || ( echo").Flag("-e").
452 Flag(`"` + msg + `"`).
453 Text("; exit 38) )")
454
455 rule.Command().Text("cp -f").Input(builtCtxFile).Output(out)
Colin Cross242c8bc2020-11-16 17:58:17 -0800456 rule.Build("property_contexts_check_api", "checking API: "+m.Name())
Inseob Kimcd616492020-03-24 23:06:40 +0900457 builtCtxFile = out
458 }
459
460 return builtCtxFile
Inseob Kimb554e592019-04-15 20:10:46 +0900461}
462
Inseob Kim06518b12023-08-25 21:20:08 +0900463func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool {
464 if !ctx.SocSpecific() && !ctx.DeviceSpecific() {
465 return false
466 }
467
468 return ctx.DeviceConfig().CheckVendorSeappViolations()
469}
470
Inseob Kim2dac2672021-12-29 17:54:57 +0900471func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900472 neverallowFile := pathForModuleOut(ctx, "neverallow")
Inseob Kim085f22f2023-11-09 11:13:01 +0900473 ret := pathForModuleOut(ctx, "checkseapp", m.stem())
Inseob Kim2dac2672021-12-29 17:54:57 +0900474
Inseob Kim085f22f2023-11-09 11:13:01 +0900475 // Step 1. Generate a M4 processed neverallow file
476 flags := m.getBuildFlags(ctx)
477 m4NeverallowFile := pathForModuleOut(ctx, "neverallow.m4out")
Inseob Kim2dac2672021-12-29 17:54:57 +0900478 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim085f22f2023-11-09 11:13:01 +0900479 rule.Command().
480 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
481 Flag("--fatal-warnings").
482 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
483 Flags(flagsToM4Macros(flags)).
484 Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)).
485 FlagWithOutput("> ", m4NeverallowFile)
486
487 rule.Temporary(m4NeverallowFile)
488 rule.Command().
489 Text("( grep").
Inseob Kim2dac2672021-12-29 17:54:57 +0900490 Flag("-ihe").
491 Text("'^neverallow'").
Inseob Kim085f22f2023-11-09 11:13:01 +0900492 Input(m4NeverallowFile).
Inseob Kim2dac2672021-12-29 17:54:57 +0900493 Text(">").
494 Output(neverallowFile).
Inseob Kim085f22f2023-11-09 11:13:01 +0900495 Text("|| true )") // to make ninja happy even when result is empty
Inseob Kim2dac2672021-12-29 17:54:57 +0900496
Inseob Kim085f22f2023-11-09 11:13:01 +0900497 // Step 2. Generate a M4 processed contexts file
498 builtCtx := m.buildGeneralContexts(ctx, inputs)
499
500 // Step 3. checkseapp
Inseob Kim2dac2672021-12-29 17:54:57 +0900501 rule.Temporary(neverallowFile)
Inseob Kimd7d36092023-06-26 20:48:48 +0900502 checkCmd := rule.Command().BuiltTool("checkseapp").
Inseob Kim2dac2672021-12-29 17:54:57 +0900503 FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))).
504 FlagWithOutput("-o ", ret).
Inseob Kim085f22f2023-11-09 11:13:01 +0900505 Input(builtCtx).
Inseob Kim2dac2672021-12-29 17:54:57 +0900506 Input(neverallowFile)
507
Inseob Kim06518b12023-08-25 21:20:08 +0900508 if m.shouldCheckCoredomain(ctx) {
509 checkCmd.Flag("-c") // check coredomain for vendor contexts
Inseob Kimd7d36092023-06-26 20:48:48 +0900510 }
511
Inseob Kim2dac2672021-12-29 17:54:57 +0900512 rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name())
513 return ret
514}
515
Inseob Kimb554e592019-04-15 20:10:46 +0900516func hwServiceFactory() android.Module {
517 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000518 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900519 return m
520}
521
522func propertyFactory() android.Module {
523 m := newModule()
Inseob Kimcd616492020-03-24 23:06:40 +0900524 m.build = m.buildPropertyContexts
525 m.deps = m.propertyContextsDeps
Inseob Kimb554e592019-04-15 20:10:46 +0900526 return m
527}
528
529func serviceFactory() android.Module {
530 m := newModule()
Thiébaud Weksteen74482f52023-04-26 13:46:59 +1000531 m.build = m.buildServiceContexts
Inseob Kimb554e592019-04-15 20:10:46 +0900532 return m
533}
Janis Danisevskisc40681f2020-07-25 13:02:29 -0700534
535func keystoreKeyFactory() android.Module {
536 m := newModule()
537 m.build = m.buildGeneralContexts
538 return m
539}
Yuntao Xu42e732c2021-11-18 22:33:02 +0000540
Inseob Kim2dac2672021-12-29 17:54:57 +0900541func seappFactory() android.Module {
542 m := newModule()
543 m.build = m.buildSeappContexts
544 return m
545}
546
Inseob Kimc7596c42022-02-25 11:45:41 +0900547func vndServiceFactory() android.Module {
548 m := newModule()
549 m.build = m.buildGeneralContexts
550 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
551 if !ctx.SocSpecific() {
552 ctx.ModuleErrorf(m.Name(), "must set vendor: true")
553 return
554 }
555 })
556 return m
557}
558
Inseob Kimb5e23532022-02-16 02:26:11 +0000559type contextsTestProperties struct {
560 // Contexts files to be tested.
561 Srcs []string `android:"path"`
562
563 // Precompiled sepolicy binary to be tesed together.
564 Sepolicy *string `android:"path"`
565}
566
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100567type fileContextsTestProperties struct {
568 // Test data. File passed to `checkfc -t` to validate how contexts are resolved.
569 Test_data *string `android:"path"`
570}
571
Inseob Kimb5e23532022-02-16 02:26:11 +0000572type contextsTestModule struct {
573 android.ModuleBase
574
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100575 // The type of context.
576 context contextType
Inseob Kimb5e23532022-02-16 02:26:11 +0000577
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100578 properties contextsTestProperties
579 fileProperties fileContextsTestProperties
580 testTimestamp android.OutputPath
Inseob Kimb5e23532022-02-16 02:26:11 +0000581}
582
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100583type contextType int
584
585const (
586 FileContext contextType = iota
587 PropertyContext
588 ServiceContext
589 HwServiceContext
590 VndServiceContext
591)
592
Inseob Kimb5e23532022-02-16 02:26:11 +0000593// checkfc parses a context file and checks for syntax errors.
594// If -s is specified, the service backend is used to verify binder services.
595// If -l is specified, the service backend is used to verify hwbinder services.
596// Otherwise, context_file is assumed to be a file_contexts file
597// If -e is specified, then the context_file is allowed to be empty.
598
599// file_contexts_test tests given file_contexts files with checkfc.
600func fileContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100601 m := &contextsTestModule{context: FileContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000602 m.AddProperties(&m.properties)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100603 m.AddProperties(&m.fileProperties)
Inseob Kimb5e23532022-02-16 02:26:11 +0000604 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
605 return m
606}
607
608// property_contexts_test tests given property_contexts files with property_info_checker.
609func propertyContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100610 m := &contextsTestModule{context: PropertyContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000611 m.AddProperties(&m.properties)
612 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
613 return m
614}
615
616// hwservice_contexts_test tests given hwservice_contexts files with checkfc.
617func hwserviceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100618 m := &contextsTestModule{context: HwServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000619 m.AddProperties(&m.properties)
620 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
621 return m
622}
623
624// service_contexts_test tests given service_contexts files with checkfc.
625func serviceContextsTestFactory() android.Module {
626 // checkfc -s: service_contexts test
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100627 m := &contextsTestModule{context: ServiceContext}
Inseob Kimb5e23532022-02-16 02:26:11 +0000628 m.AddProperties(&m.properties)
629 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
630 return m
631}
632
Inseob Kimc7596c42022-02-25 11:45:41 +0900633// vndservice_contexts_test tests given vndservice_contexts files with checkfc.
634func vndServiceContextsTestFactory() android.Module {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100635 m := &contextsTestModule{context: VndServiceContext}
Inseob Kimc7596c42022-02-25 11:45:41 +0900636 m.AddProperties(&m.properties)
637 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
638 return m
639}
640
Inseob Kimb5e23532022-02-16 02:26:11 +0000641func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100642 tool := "checkfc"
643 if m.context == PropertyContext {
644 tool = "property_info_checker"
Inseob Kimb5e23532022-02-16 02:26:11 +0000645 }
646
647 if len(m.properties.Srcs) == 0 {
648 ctx.PropertyErrorf("srcs", "can't be empty")
649 return
650 }
651
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100652 validateWithPolicy := true
Inseob Kimb5e23532022-02-16 02:26:11 +0000653 if proptools.String(m.properties.Sepolicy) == "" {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100654 if m.context == FileContext {
655 if proptools.String(m.fileProperties.Test_data) == "" {
656 ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided")
657 return
658 }
659 validateWithPolicy = false
660 } else {
661 ctx.PropertyErrorf("sepolicy", "can't be empty")
662 return
663 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000664 }
665
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100666 flags := []string(nil)
667 switch m.context {
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100668 case FileContext:
669 if !validateWithPolicy {
670 flags = []string{"-t"}
671 }
Thiébaud Weksteena69e14f2023-10-20 13:31:19 +1100672 case ServiceContext:
673 flags = []string{"-s" /* binder services */}
674 case HwServiceContext:
675 flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */}
676 case VndServiceContext:
677 flags = []string{"-e" /* allow empty */, "-v" /* vnd service */}
678 }
679
Inseob Kimb5e23532022-02-16 02:26:11 +0000680 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
Inseob Kimb5e23532022-02-16 02:26:11 +0000681 rule := android.NewRuleBuilder(pctx, ctx)
Thiébaud Weksteenb6e74302023-10-20 15:36:15 +1100682
683 if validateWithPolicy {
684 sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy))
685 rule.Command().BuiltTool(tool).
686 Flags(flags).
687 Input(sepolicy).
688 Inputs(srcs)
689 } else {
690 test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data))
691 rule.Command().BuiltTool(tool).
692 Flags(flags).
693 Inputs(srcs).
694 Input(test_data)
695 }
Inseob Kimb5e23532022-02-16 02:26:11 +0000696
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900697 m.testTimestamp = pathForModuleOut(ctx, "timestamp")
Inseob Kimb5e23532022-02-16 02:26:11 +0000698 rule.Command().Text("touch").Output(m.testTimestamp)
699 rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName())
700}
701
702func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries {
703 return []android.AndroidMkEntries{android.AndroidMkEntries{
704 Class: "FAKE",
705 // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
706 // Without OutputFile this module won't be exported to Makefile.
707 OutputFile: android.OptionalPathForPath(m.testTimestamp),
708 Include: "$(BUILD_PHONY_PACKAGE)",
709 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
710 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
711 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String())
712 },
713 },
714 }}
715}
716
717// contextsTestModule implements ImageInterface to be able to include recovery_available contexts
718// modules as its sources.
719func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
720}
721
Jihoon Kang03f4d9d2024-06-21 06:36:33 +0000722func (m *contextsTestModule) VendorVariantNeeded(ctx android.BaseModuleContext) bool {
723 return false
724}
725
726func (m *contextsTestModule) ProductVariantNeeded(ctx android.BaseModuleContext) bool {
727 return false
728}
729
Inseob Kimb5e23532022-02-16 02:26:11 +0000730func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
731 return true
732}
733
734func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
735 return false
736}
737
738func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
739 return false
740}
741
742func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
743 return false
744}
745
746func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
747 return false
748}
749
750func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
751 return nil
752}
753
Jihoon Kang8298ae52024-06-13 21:30:15 +0000754func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string) {
Inseob Kimb5e23532022-02-16 02:26:11 +0000755}
756
757var _ android.ImageInterface = (*contextsTestModule)(nil)