blob: 463a978ed1c713b61d0ca92bdb6ad1a36f665dac [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 Kim2dac2672021-12-29 17:54:57 +090020 "os"
Inseob Kimb554e592019-04-15 20:10:46 +090021 "strings"
22
Inseob Kimcd616492020-03-24 23:06:40 +090023 "github.com/google/blueprint"
Inseob Kimb554e592019-04-15 20:10:46 +090024 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
Inseob Kimcd616492020-03-24 23:06:40 +090027 "android/soong/sysprop"
Inseob Kimb554e592019-04-15 20:10:46 +090028)
29
Inseob Kimb554e592019-04-15 20:10:46 +090030type selinuxContextsProperties struct {
31 // Filenames under sepolicy directories, which will be used to generate contexts file.
32 Srcs []string `android:"path"`
33
Yuntao Xu42e732c2021-11-18 22:33:02 +000034 // Output file name. Defaults to module name
35 Stem *string
36
Inseob Kimb554e592019-04-15 20:10:46 +090037 Product_variables struct {
Inseob Kimb554e592019-04-15 20:10:46 +090038 Address_sanitize struct {
Inseob Kim6d3d5a62021-12-21 20:55:32 +090039 Srcs []string `android:"path"`
Inseob Kimb554e592019-04-15 20:10:46 +090040 }
41 }
42
Inseob Kimb554e592019-04-15 20:10:46 +090043 // Whether the comments in generated contexts file will be removed or not.
44 Remove_comment *bool
45
46 // Whether the result context file is sorted with fc_sort or not.
47 Fc_sort *bool
48
49 // Make this module available when building for recovery
50 Recovery_available *bool
Inseob Kimb554e592019-04-15 20:10:46 +090051}
52
53type fileContextsProperties struct {
54 // flatten_apex can be used to specify additional sources of file_contexts.
55 // Apex paths, /system/apex/{apex_name}, will be amended to the paths of file_contexts
56 // entries.
57 Flatten_apex struct {
Inseob Kim6d3d5a62021-12-21 20:55:32 +090058 Srcs []string `android:"path"`
Inseob Kimb554e592019-04-15 20:10:46 +090059 }
60}
61
Inseob Kim2dac2672021-12-29 17:54:57 +090062type seappProperties struct {
63 // Files containing neverallow rules.
64 Neverallow_files []string `android:"path"`
65
66 // Precompiled sepolicy binary file which will be fed to checkseapp.
67 Sepolicy *string `android:"path"`
68}
69
Inseob Kimb554e592019-04-15 20:10:46 +090070type selinuxContextsModule struct {
71 android.ModuleBase
72
73 properties selinuxContextsProperties
74 fileContextsProperties fileContextsProperties
Inseob Kim2dac2672021-12-29 17:54:57 +090075 seappProperties seappProperties
Inseob Kimcd616492020-03-24 23:06:40 +090076 build func(ctx android.ModuleContext, inputs android.Paths) android.Path
77 deps func(ctx android.BottomUpMutatorContext)
78 outputPath android.Path
Colin Cross040f1512019-10-02 10:36:09 -070079 installPath android.InstallPath
Inseob Kimb554e592019-04-15 20:10:46 +090080}
81
82var (
Inseob Kimcd616492020-03-24 23:06:40 +090083 reuseContextsDepTag = dependencyTag{name: "reuseContexts"}
84 syspropLibraryDepTag = dependencyTag{name: "sysprop_library"}
Inseob Kimb554e592019-04-15 20:10:46 +090085)
86
87func init() {
88 pctx.HostBinToolVariable("fc_sort", "fc_sort")
89
90 android.RegisterModuleType("file_contexts", fileFactory)
91 android.RegisterModuleType("hwservice_contexts", hwServiceFactory)
92 android.RegisterModuleType("property_contexts", propertyFactory)
93 android.RegisterModuleType("service_contexts", serviceFactory)
Janis Danisevskisc40681f2020-07-25 13:02:29 -070094 android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory)
Inseob Kim2dac2672021-12-29 17:54:57 +090095 android.RegisterModuleType("seapp_contexts", seappFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +090096 android.RegisterModuleType("vndservice_contexts", vndServiceFactory)
Inseob Kimb5e23532022-02-16 02:26:11 +000097
98 android.RegisterModuleType("file_contexts_test", fileContextsTestFactory)
99 android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory)
100 android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory)
101 android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory)
Inseob Kimc7596c42022-02-25 11:45:41 +0900102 android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory)
Inseob Kimb554e592019-04-15 20:10:46 +0900103}
104
Colin Cross040f1512019-10-02 10:36:09 -0700105func (m *selinuxContextsModule) InstallInRoot() bool {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900106 return m.InRecovery()
107}
108
109func (m *selinuxContextsModule) InstallInRecovery() bool {
110 // ModuleBase.InRecovery() checks the image variant
111 return m.InRecovery()
112}
113
114func (m *selinuxContextsModule) onlyInRecovery() bool {
115 // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property
116 return m.ModuleBase.InstallInRecovery()
Colin Cross040f1512019-10-02 10:36:09 -0700117}
118
Inseob Kimcd616492020-03-24 23:06:40 +0900119func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
120 if m.deps != nil {
121 m.deps(ctx)
122 }
Inseob Kimfa6fe472021-01-12 13:40:27 +0900123
124 if m.InRecovery() && !m.onlyInRecovery() {
125 ctx.AddFarVariationDependencies([]blueprint.Variation{
126 {Mutator: "image", Variation: android.CoreVariation},
127 }, reuseContextsDepTag, ctx.ModuleName())
128 }
Inseob Kimcd616492020-03-24 23:06:40 +0900129}
130
131func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) {
132 for _, lib := range sysprop.SyspropLibraries(ctx.Config()) {
133 ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib)
134 }
135}
136
Yuntao Xu42e732c2021-11-18 22:33:02 +0000137func (m *selinuxContextsModule) stem() string {
138 return proptools.StringDefault(m.properties.Stem, m.Name())
139}
140
Inseob Kimb554e592019-04-15 20:10:46 +0900141func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900142 if m.InRecovery() {
Colin Cross040f1512019-10-02 10:36:09 -0700143 // Installing context files at the root of the recovery partition
144 m.installPath = android.PathForModuleInstall(ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900145 } else {
146 m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
147 }
148
Inseob Kimfa6fe472021-01-12 13:40:27 +0900149 if m.InRecovery() && !m.onlyInRecovery() {
Inseob Kimb554e592019-04-15 20:10:46 +0900150 dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag)
151
152 if reuseDeps, ok := dep.(*selinuxContextsModule); ok {
153 m.outputPath = reuseDeps.outputPath
Yuntao Xu42e732c2021-11-18 22:33:02 +0000154 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900155 return
156 }
157 }
158
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900159 m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs))
Yuntao Xu42e732c2021-11-18 22:33:02 +0000160 ctx.InstallFile(m.installPath, m.stem(), m.outputPath)
Inseob Kimb554e592019-04-15 20:10:46 +0900161}
162
163func newModule() *selinuxContextsModule {
164 m := &selinuxContextsModule{}
165 m.AddProperties(
166 &m.properties,
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900167 &m.fileContextsProperties,
Inseob Kim2dac2672021-12-29 17:54:57 +0900168 &m.seappProperties,
Inseob Kimb554e592019-04-15 20:10:46 +0900169 )
170 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
171 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
172 m.selinuxContextsHook(ctx)
173 })
174 return m
175}
176
177func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) {
178 // TODO: clean this up to use build/soong/android/variable.go after b/79249983
179 var srcs []string
180
Inseob Kimb554e592019-04-15 20:10:46 +0900181 for _, sanitize := range ctx.Config().SanitizeDevice() {
182 if sanitize == "address" {
183 srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...)
184 break
185 }
186 }
187
188 m.properties.Srcs = append(m.properties.Srcs, srcs...)
189}
190
191func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData {
Colin Crossf82aed02021-11-04 17:25:55 -0700192 nameSuffix := ""
193 if m.InRecovery() && !m.onlyInRecovery() {
194 nameSuffix = ".recovery"
195 }
Inseob Kimb554e592019-04-15 20:10:46 +0900196 return android.AndroidMkData{
Colin Crossf82aed02021-11-04 17:25:55 -0700197 Class: "ETC",
198 OutputFile: android.OptionalPathForPath(m.outputPath),
199 SubName: nameSuffix,
200 Extra: []android.AndroidMkExtraFunc{
201 func(w io.Writer, outputFile android.Path) {
Colin Cross6c7f9372022-01-11 19:35:43 -0800202 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String())
Yuntao Xu42e732c2021-11-18 22:33:02 +0000203 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem())
Colin Crossf82aed02021-11-04 17:25:55 -0700204 },
Inseob Kimb554e592019-04-15 20:10:46 +0900205 },
206 }
207}
208
Inseob Kimfa6fe472021-01-12 13:40:27 +0900209func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000210 if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() {
Inseob Kimfa6fe472021-01-12 13:40:27 +0900211 ctx.PropertyErrorf("recovery_available",
212 "doesn't make sense at the same time as `recovery: true`")
Inseob Kimb554e592019-04-15 20:10:46 +0900213 }
214}
215
Inseob Kimfa6fe472021-01-12 13:40:27 +0900216func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000217 return !m.ModuleBase.InstallInRecovery()
Inseob Kimfa6fe472021-01-12 13:40:27 +0900218}
219
220func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
221 return false
222}
223
224func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
225 return false
226}
227
Inseob Kim6cc75f42021-04-29 13:53:20 +0000228func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
229 return false
230}
231
Inseob Kimfa6fe472021-01-12 13:40:27 +0900232func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000233 return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available)
Inseob Kimfa6fe472021-01-12 13:40:27 +0900234}
235
236func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
237 return nil
238}
239
240func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
241}
242
243var _ android.ImageInterface = (*selinuxContextsModule)(nil)
244
Inseob Kimcd616492020-03-24 23:06:40 +0900245func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000246 builtContext := android.PathForModuleGen(ctx, ctx.ModuleName()+"_m4out")
Inseob Kimb554e592019-04-15 20:10:46 +0900247
Colin Cross242c8bc2020-11-16 17:58:17 -0800248 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900249
250 rule.Command().
Dan Willemsen3c3e59b2019-06-19 10:52:50 -0700251 Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
252 Text("--fatal-warnings -s").
Inseob Kimb554e592019-04-15 20:10:46 +0900253 FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()).
254 Inputs(inputs).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000255 FlagWithOutput("> ", builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900256
257 if proptools.Bool(m.properties.Remove_comment) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000258 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900259
260 remove_comment_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_remove_comment")
261
262 rule.Command().
263 Text("sed -e 's/#.*$//' -e '/^$/d'").
Yuntao Xu42e732c2021-11-18 22:33:02 +0000264 Input(builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900265 FlagWithOutput("> ", remove_comment_output)
266
Yuntao Xu42e732c2021-11-18 22:33:02 +0000267 builtContext = remove_comment_output
Inseob Kimb554e592019-04-15 20:10:46 +0900268 }
269
270 if proptools.Bool(m.properties.Fc_sort) {
Yuntao Xu42e732c2021-11-18 22:33:02 +0000271 rule.Temporary(builtContext)
Inseob Kimb554e592019-04-15 20:10:46 +0900272
273 sorted_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_sorted")
274
275 rule.Command().
276 Tool(ctx.Config().HostToolPath(ctx, "fc_sort")).
Yuntao Xu42e732c2021-11-18 22:33:02 +0000277 FlagWithInput("-i ", builtContext).
Inseob Kimb554e592019-04-15 20:10:46 +0900278 FlagWithOutput("-o ", sorted_output)
279
Yuntao Xu42e732c2021-11-18 22:33:02 +0000280 builtContext = sorted_output
Inseob Kimb554e592019-04-15 20:10:46 +0900281 }
282
Yuntao Xu42e732c2021-11-18 22:33:02 +0000283 ret := android.PathForModuleGen(ctx, m.stem())
284 rule.Temporary(builtContext)
285 rule.Command().Text("cp").Input(builtContext).Output(ret)
Inseob Kimb554e592019-04-15 20:10:46 +0900286
287 rule.DeleteTemporaryFiles()
Yuntao Xu42e732c2021-11-18 22:33:02 +0000288 rule.Build("selinux_contexts", "building contexts: "+m.Name())
Inseob Kimb554e592019-04-15 20:10:46 +0900289
Inseob Kimcd616492020-03-24 23:06:40 +0900290 return ret
Inseob Kimb554e592019-04-15 20:10:46 +0900291}
292
Inseob Kimcd616492020-03-24 23:06:40 +0900293func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimb554e592019-04-15 20:10:46 +0900294 if m.properties.Fc_sort == nil {
295 m.properties.Fc_sort = proptools.BoolPtr(true)
296 }
297
Colin Cross242c8bc2020-11-16 17:58:17 -0800298 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimb554e592019-04-15 20:10:46 +0900299
300 if ctx.Config().FlattenApex() {
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900301 for _, path := range android.PathsForModuleSrc(ctx, m.fileContextsProperties.Flatten_apex.Srcs) {
302 out := android.PathForModuleGen(ctx, "flattened_apex", path.Rel())
303 apex_path := "/system/apex/" + strings.Replace(
304 strings.TrimSuffix(path.Base(), "-file_contexts"),
305 ".", "\\\\.", -1)
Inseob Kimb554e592019-04-15 20:10:46 +0900306
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900307 rule.Command().
308 Text("awk '/object_r/{printf(\""+apex_path+"%s\\n\",$0)}'").
309 Input(path).
310 FlagWithOutput("> ", out)
Inseob Kimb554e592019-04-15 20:10:46 +0900311
Inseob Kim6d3d5a62021-12-21 20:55:32 +0900312 inputs = append(inputs, out)
Inseob Kimb554e592019-04-15 20:10:46 +0900313 }
314 }
315
Colin Cross242c8bc2020-11-16 17:58:17 -0800316 rule.Build(m.Name(), "flattened_apex_file_contexts")
Inseob Kimcd616492020-03-24 23:06:40 +0900317 return m.buildGeneralContexts(ctx, inputs)
Inseob Kimb554e592019-04-15 20:10:46 +0900318}
319
320func fileFactory() android.Module {
321 m := newModule()
Inseob Kimb554e592019-04-15 20:10:46 +0900322 m.build = m.buildFileContexts
323 return m
324}
325
Inseob Kimcd616492020-03-24 23:06:40 +0900326func (m *selinuxContextsModule) buildHwServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kimb554e592019-04-15 20:10:46 +0900327 if m.properties.Remove_comment == nil {
328 m.properties.Remove_comment = proptools.BoolPtr(true)
329 }
330
Inseob Kimcd616492020-03-24 23:06:40 +0900331 return m.buildGeneralContexts(ctx, inputs)
332}
333
Inseob Kim2bcc0452020-12-21 13:16:44 +0900334func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, inputs android.Paths) android.Paths {
335 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
336 ApiLevelR := android.ApiLevelOrPanic(ctx, "R")
337
338 rule := android.NewRuleBuilder(pctx, ctx)
339
340 // This list is from vts_treble_sys_prop_test.
341 allowedPropertyPrefixes := []string{
342 "ctl.odm.",
343 "ctl.vendor.",
344 "ctl.start$odm.",
345 "ctl.start$vendor.",
346 "ctl.stop$odm.",
347 "ctl.stop$vendor.",
348 "init.svc.odm.",
349 "init.svc.vendor.",
350 "ro.boot.",
351 "ro.hardware.",
352 "ro.odm.",
353 "ro.vendor.",
354 "odm.",
355 "persist.odm.",
356 "persist.vendor.",
357 "vendor.",
358 }
359
360 // persist.camera is also allowed for devices launching with R or eariler
361 if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) {
362 allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.")
363 }
364
365 var allowedContextPrefixes []string
366
367 if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) {
368 // This list is from vts_treble_sys_prop_test.
369 allowedContextPrefixes = []string{
370 "vendor_",
371 "odm_",
372 }
373 }
374
375 var ret android.Paths
376 for _, input := range inputs {
377 cmd := rule.Command().
378 BuiltTool("check_prop_prefix").
379 FlagWithInput("--property-contexts ", input).
380 FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$'
381 FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes)
382
383 if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() {
384 cmd.Flag("--strict")
385 }
386
387 out := android.PathForModuleGen(ctx, "namespace_checked").Join(ctx, input.String())
388 rule.Command().Text("cp -f").Input(input).Output(out)
389 ret = append(ret, out)
390 }
391 rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName())
392 return ret
393}
394
Inseob Kimcd616492020-03-24 23:06:40 +0900395func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
Inseob Kim2bcc0452020-12-21 13:16:44 +0900396 // vendor/odm properties are enforced for devices launching with Android Q or later. So, if
397 // vendor/odm, make sure that only vendor/odm properties exist.
398 shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel()
399 ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q")
400 if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) {
401 inputs = m.checkVendorPropertyNamespace(ctx, inputs)
402 }
403
Inseob Kimcd616492020-03-24 23:06:40 +0900404 builtCtxFile := m.buildGeneralContexts(ctx, inputs)
405
406 var apiFiles android.Paths
407 ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) {
Inseob Kim3a3539a2021-01-15 18:10:29 +0900408 i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath })
Inseob Kimcd616492020-03-24 23:06:40 +0900409 if !ok {
410 panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName()))
411 }
Inseob Kim3a3539a2021-01-15 18:10:29 +0900412 if api := i.CurrentSyspropApiFile(); api.Valid() {
413 apiFiles = append(apiFiles, api.Path())
414 }
Inseob Kimcd616492020-03-24 23:06:40 +0900415 })
416
417 // check compatibility with sysprop_library
418 if len(apiFiles) > 0 {
419 out := android.PathForModuleGen(ctx, ctx.ModuleName()+"_api_checked")
Colin Cross242c8bc2020-11-16 17:58:17 -0800420 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kimcd616492020-03-24 23:06:40 +0900421
422 msg := `\n******************************\n` +
423 `API of sysprop_library doesn't match with property_contexts\n` +
424 `Please fix the breakage and rebuild.\n` +
425 `******************************\n`
426
427 rule.Command().
428 Text("( ").
Colin Cross242c8bc2020-11-16 17:58:17 -0800429 BuiltTool("sysprop_type_checker").
Inseob Kimcd616492020-03-24 23:06:40 +0900430 FlagForEachInput("--api ", apiFiles).
431 FlagWithInput("--context ", builtCtxFile).
432 Text(" || ( echo").Flag("-e").
433 Flag(`"` + msg + `"`).
434 Text("; exit 38) )")
435
436 rule.Command().Text("cp -f").Input(builtCtxFile).Output(out)
Colin Cross242c8bc2020-11-16 17:58:17 -0800437 rule.Build("property_contexts_check_api", "checking API: "+m.Name())
Inseob Kimcd616492020-03-24 23:06:40 +0900438 builtCtxFile = out
439 }
440
441 return builtCtxFile
Inseob Kimb554e592019-04-15 20:10:46 +0900442}
443
Inseob Kim2dac2672021-12-29 17:54:57 +0900444func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path {
445 neverallowFile := android.PathForModuleGen(ctx, "neverallow")
446 ret := android.PathForModuleGen(ctx, m.stem())
447
448 rule := android.NewRuleBuilder(pctx, ctx)
449 rule.Command().Text("(grep").
450 Flag("-ihe").
451 Text("'^neverallow'").
452 Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)).
453 Text(os.DevNull). // to make grep happy even when Neverallow_files is empty
454 Text(">").
455 Output(neverallowFile).
456 Text("|| true)") // to make ninja happy even when result is empty
457
458 rule.Temporary(neverallowFile)
459 rule.Command().BuiltTool("checkseapp").
460 FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))).
461 FlagWithOutput("-o ", ret).
462 Inputs(inputs).
463 Input(neverallowFile)
464
465 rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name())
466 return ret
467}
468
Inseob Kimb554e592019-04-15 20:10:46 +0900469func hwServiceFactory() android.Module {
470 m := newModule()
471 m.build = m.buildHwServiceContexts
472 return m
473}
474
475func propertyFactory() android.Module {
476 m := newModule()
Inseob Kimcd616492020-03-24 23:06:40 +0900477 m.build = m.buildPropertyContexts
478 m.deps = m.propertyContextsDeps
Inseob Kimb554e592019-04-15 20:10:46 +0900479 return m
480}
481
482func serviceFactory() android.Module {
483 m := newModule()
484 m.build = m.buildGeneralContexts
485 return m
486}
Janis Danisevskisc40681f2020-07-25 13:02:29 -0700487
488func keystoreKeyFactory() android.Module {
489 m := newModule()
490 m.build = m.buildGeneralContexts
491 return m
492}
Yuntao Xu42e732c2021-11-18 22:33:02 +0000493
Inseob Kim2dac2672021-12-29 17:54:57 +0900494func seappFactory() android.Module {
495 m := newModule()
496 m.build = m.buildSeappContexts
497 return m
498}
499
Inseob Kimc7596c42022-02-25 11:45:41 +0900500func vndServiceFactory() android.Module {
501 m := newModule()
502 m.build = m.buildGeneralContexts
503 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
504 if !ctx.SocSpecific() {
505 ctx.ModuleErrorf(m.Name(), "must set vendor: true")
506 return
507 }
508 })
509 return m
510}
511
Yuntao Xu42e732c2021-11-18 22:33:02 +0000512var _ android.OutputFileProducer = (*selinuxContextsModule)(nil)
513
514// Implements android.OutputFileProducer
515func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) {
516 if tag == "" {
517 return []android.Path{m.outputPath}, nil
518 }
519 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
520}
Inseob Kimb5e23532022-02-16 02:26:11 +0000521
522type contextsTestProperties struct {
523 // Contexts files to be tested.
524 Srcs []string `android:"path"`
525
526 // Precompiled sepolicy binary to be tesed together.
527 Sepolicy *string `android:"path"`
528}
529
530type contextsTestModule struct {
531 android.ModuleBase
532
533 // Name of the test tool. "checkfc" or "property_info_checker"
534 tool string
535
536 // Additional flags to be passed to the tool.
537 flags []string
538
539 properties contextsTestProperties
540 testTimestamp android.ModuleOutPath
541}
542
543// checkfc parses a context file and checks for syntax errors.
544// If -s is specified, the service backend is used to verify binder services.
545// If -l is specified, the service backend is used to verify hwbinder services.
546// Otherwise, context_file is assumed to be a file_contexts file
547// If -e is specified, then the context_file is allowed to be empty.
548
549// file_contexts_test tests given file_contexts files with checkfc.
550func fileContextsTestFactory() android.Module {
551 m := &contextsTestModule{tool: "checkfc" /* no flags: file_contexts file check */}
552 m.AddProperties(&m.properties)
553 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
554 return m
555}
556
557// property_contexts_test tests given property_contexts files with property_info_checker.
558func propertyContextsTestFactory() android.Module {
559 m := &contextsTestModule{tool: "property_info_checker"}
560 m.AddProperties(&m.properties)
561 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
562 return m
563}
564
565// hwservice_contexts_test tests given hwservice_contexts files with checkfc.
566func hwserviceContextsTestFactory() android.Module {
567 m := &contextsTestModule{tool: "checkfc", flags: []string{"-e" /* allow empty */, "-l" /* hwbinder services */}}
568 m.AddProperties(&m.properties)
569 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
570 return m
571}
572
573// service_contexts_test tests given service_contexts files with checkfc.
574func serviceContextsTestFactory() android.Module {
575 // checkfc -s: service_contexts test
576 m := &contextsTestModule{tool: "checkfc", flags: []string{"-s" /* binder services */}}
577 m.AddProperties(&m.properties)
578 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
579 return m
580}
581
Inseob Kimc7596c42022-02-25 11:45:41 +0900582// vndservice_contexts_test tests given vndservice_contexts files with checkfc.
583func vndServiceContextsTestFactory() android.Module {
584 m := &contextsTestModule{tool: "checkfc", flags: []string{"-e" /* allow empty */, "-v" /* vnd service */}}
585 m.AddProperties(&m.properties)
586 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
587 return m
588}
589
Inseob Kimb5e23532022-02-16 02:26:11 +0000590func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
591 tool := m.tool
592 if tool != "checkfc" && tool != "property_info_checker" {
593 panic(fmt.Errorf("%q: unknown tool name: %q", ctx.ModuleName(), tool))
594 }
595
596 if len(m.properties.Srcs) == 0 {
597 ctx.PropertyErrorf("srcs", "can't be empty")
598 return
599 }
600
601 if proptools.String(m.properties.Sepolicy) == "" {
602 ctx.PropertyErrorf("sepolicy", "can't be empty")
603 return
604 }
605
606 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
607 sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy))
608
609 rule := android.NewRuleBuilder(pctx, ctx)
610 rule.Command().BuiltTool(tool).
611 Flags(m.flags).
612 Input(sepolicy).
613 Inputs(srcs)
614
615 m.testTimestamp = android.PathForModuleOut(ctx, "timestamp")
616 rule.Command().Text("touch").Output(m.testTimestamp)
617 rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName())
618}
619
620func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries {
621 return []android.AndroidMkEntries{android.AndroidMkEntries{
622 Class: "FAKE",
623 // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it.
624 // Without OutputFile this module won't be exported to Makefile.
625 OutputFile: android.OptionalPathForPath(m.testTimestamp),
626 Include: "$(BUILD_PHONY_PACKAGE)",
627 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
628 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
629 entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String())
630 },
631 },
632 }}
633}
634
635// contextsTestModule implements ImageInterface to be able to include recovery_available contexts
636// modules as its sources.
637func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
638}
639
640func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
641 return true
642}
643
644func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
645 return false
646}
647
648func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
649 return false
650}
651
652func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
653 return false
654}
655
656func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
657 return false
658}
659
660func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
661 return nil
662}
663
664func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
665}
666
667var _ android.ImageInterface = (*contextsTestModule)(nil)