blob: be9d34e03f974e99a67ba374da9da4936b6cc2a9 [file] [log] [blame]
Inseob Kim7e8bd1e2021-03-17 18:59:43 +09001// Copyright (C) 2021 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package selinux
16
17import (
18 "fmt"
19 "os"
Inseob Kim0a707fa2021-12-09 23:35:11 +090020 "sort"
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090021 "strconv"
Inseob Kim0a707fa2021-12-09 23:35:11 +090022 "strings"
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090023
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27)
28
29const (
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090030 MlsSens = 1
31 MlsCats = 1024
32 PolicyVers = 30
33)
34
Inseob Kim0a707fa2021-12-09 23:35:11 +090035// This order should be kept. checkpolicy syntax requires it.
36var policyConfOrder = []string{
37 "security_classes",
38 "initial_sids",
39 "access_vectors",
40 "global_macros",
41 "neverallow_macros",
42 "mls_macros",
43 "mls_decl",
44 "mls",
45 "policy_capabilities",
46 "te_macros",
Inseob Kim0a707fa2021-12-09 23:35:11 +090047 "ioctl_defines",
48 "ioctl_macros",
Inseob Kim1e796342022-06-09 11:26:35 +090049 "attributes|*.te",
Inseob Kim0a707fa2021-12-09 23:35:11 +090050 "roles_decl",
51 "roles",
52 "users",
53 "initial_sid_contexts",
54 "fs_use",
55 "genfs_contexts",
56 "port_contexts",
57}
58
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090059func init() {
60 android.RegisterModuleType("se_policy_conf", policyConfFactory)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +090061 android.RegisterModuleType("se_policy_conf_defaults", policyConfDefaultFactory)
Inseob Kimdf1a0de2021-03-17 19:05:02 +090062 android.RegisterModuleType("se_policy_cil", policyCilFactory)
Inseob Kimb9d05112021-09-27 13:13:46 +000063 android.RegisterModuleType("se_policy_binary", policyBinaryFactory)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090064}
65
66type policyConfProperties struct {
67 // Name of the output. Default is {module_name}
68 Stem *string
69
70 // Policy files to be compiled to cil file.
71 Srcs []string `android:"path"`
72
73 // Target build variant (user / userdebug / eng). Default follows the current lunch target
74 Build_variant *string
75
76 // Whether to exclude build test or not. Default is false
77 Exclude_build_test *bool
78
79 // Whether to include asan specific policies or not. Default follows the current lunch target
80 With_asan *bool
81
82 // Whether to build CTS specific policy or not. Default is false
83 Cts *bool
84
Inseob Kim5bbcd682021-12-28 14:57:03 +090085 // Whether to build recovery specific policy or not. Default is false
86 Target_recovery *bool
87
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090088 // Whether this module is directly installable to one of the partitions. Default is true
89 Installable *bool
Inseob Kim6e384f32022-03-10 13:15:05 +090090
91 // Desired number of MLS categories. Defaults to 1024
92 Mls_cats *int64
Inseob Kim8697fc82024-04-16 14:45:32 +090093
94 // Whether to turn on board_api_level guard or not. Defaults to false
95 Board_api_level_guard *bool
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090096}
97
98type policyConf struct {
99 android.ModuleBase
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900100 android.DefaultableModuleBase
101 flaggableModuleBase
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900102
103 properties policyConfProperties
104
105 installSource android.Path
106 installPath android.InstallPath
107}
108
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900109var _ flaggableModule = (*policyConf)(nil)
110
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900111// se_policy_conf merges collection of policy files into a policy.conf file to be processed by
112// checkpolicy.
113func policyConfFactory() android.Module {
114 c := &policyConf{}
115 c.AddProperties(&c.properties)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900116 initFlaggableModule(c)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900117 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900118 android.InitDefaultableModule(c)
119 return c
120}
121
122type policyConfDefaults struct {
123 android.ModuleBase
124 android.DefaultsModuleBase
125}
126
127// se_policy_conf_defaults provides a set of properties that can be inherited by other
128// se_policy_conf_defaults modules. A module can use the properties from a se_policy_conf_defaults
129// using `defaults: ["<:default_module_name>"]`. Properties of both modules are merged (when
130// possible) by prepending the default module's values to the depending module's values.
131func policyConfDefaultFactory() android.Module {
132 c := &policyConfDefaults{}
133 c.AddProperties(
134 &policyConfProperties{},
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900135 &flaggableModuleProperties{},
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900136 )
137 android.InitDefaultsModule(c)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900138 return c
139}
140
141func (c *policyConf) installable() bool {
142 return proptools.BoolDefault(c.properties.Installable, true)
143}
144
145func (c *policyConf) stem() string {
146 return proptools.StringDefault(c.properties.Stem, c.Name())
147}
148
149func (c *policyConf) buildVariant(ctx android.ModuleContext) string {
150 if variant := proptools.String(c.properties.Build_variant); variant != "" {
151 return variant
152 }
153 if ctx.Config().Eng() {
154 return "eng"
155 }
156 if ctx.Config().Debuggable() {
157 return "userdebug"
158 }
159 return "user"
160}
161
162func (c *policyConf) cts() bool {
163 return proptools.Bool(c.properties.Cts)
164}
165
Inseob Kim5bbcd682021-12-28 14:57:03 +0900166func (c *policyConf) isTargetRecovery() bool {
167 return proptools.Bool(c.properties.Target_recovery)
168}
169
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900170func (c *policyConf) withAsan(ctx android.ModuleContext) string {
171 isAsanDevice := android.InList("address", ctx.Config().SanitizeDevice())
172 return strconv.FormatBool(proptools.BoolDefault(c.properties.With_asan, isAsanDevice))
173}
174
175func (c *policyConf) sepolicySplit(ctx android.ModuleContext) string {
176 if c.cts() {
177 return "cts"
178 }
Inseob Kim5bbcd682021-12-28 14:57:03 +0900179 if c.isTargetRecovery() {
180 return "false"
181 }
Steven Moreland721f5af2023-05-31 21:54:51 +0000182 return strconv.FormatBool(true)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900183}
184
185func (c *policyConf) compatibleProperty(ctx android.ModuleContext) string {
186 if c.cts() {
187 return "cts"
188 }
Inseob Kim5bbcd682021-12-28 14:57:03 +0900189 if c.isTargetRecovery() {
190 return "false"
191 }
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900192 return "true"
193}
194
195func (c *policyConf) trebleSyspropNeverallow(ctx android.ModuleContext) string {
196 if c.cts() {
197 return "cts"
198 }
Inseob Kim5bbcd682021-12-28 14:57:03 +0900199 if c.isTargetRecovery() {
200 return "false"
201 }
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900202 return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenTrebleSyspropNeverallow())
203}
204
205func (c *policyConf) enforceSyspropOwner(ctx android.ModuleContext) string {
206 if c.cts() {
207 return "cts"
208 }
Inseob Kim5bbcd682021-12-28 14:57:03 +0900209 if c.isTargetRecovery() {
210 return "false"
211 }
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900212 return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenEnforceSyspropOwner())
213}
214
Hridya Valsarajua885dd82021-04-26 16:32:17 -0700215func (c *policyConf) enforceDebugfsRestrictions(ctx android.ModuleContext) string {
216 if c.cts() {
217 return "cts"
218 }
219 return strconv.FormatBool(ctx.DeviceConfig().BuildDebugfsRestrictionsEnabled())
220}
221
Inseob Kim6e384f32022-03-10 13:15:05 +0900222func (c *policyConf) mlsCats() int {
223 return proptools.IntDefault(c.properties.Mls_cats, MlsCats)
224}
225
Inseob Kim8697fc82024-04-16 14:45:32 +0900226func (c *policyConf) boardApiLevel(ctx android.ModuleContext) string {
227 if proptools.Bool(c.properties.Board_api_level_guard) {
228 return ctx.Config().VendorApiLevel()
229 }
230 // aribtrary value greater than any other vendor API levels
231 return "1000000"
232}
233
Inseob Kim0a707fa2021-12-09 23:35:11 +0900234func findPolicyConfOrder(name string) int {
235 for idx, pattern := range policyConfOrder {
Inseob Kim1e796342022-06-09 11:26:35 +0900236 // We could use regexp but it seems like an overkill
237 if pattern == "attributes|*.te" && (name == "attributes" || strings.HasSuffix(name, ".te")) {
238 return idx
239 } else if pattern == name {
Inseob Kim0a707fa2021-12-09 23:35:11 +0900240 return idx
241 }
242 }
243 // name is not matched
244 return len(policyConfOrder)
245}
246
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900247func (c *policyConf) transformPolicyToConf(ctx android.ModuleContext) android.OutputPath {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900248 conf := pathForModuleOut(ctx, c.stem())
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900249 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim0a707fa2021-12-09 23:35:11 +0900250
251 srcs := android.PathsForModuleSrc(ctx, c.properties.Srcs)
252 sort.SliceStable(srcs, func(x, y int) bool {
253 return findPolicyConfOrder(srcs[x].Base()) < findPolicyConfOrder(srcs[y].Base())
254 })
255
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900256 flags := c.getBuildFlags(ctx)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900257 rule.Command().Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
258 Flag("--fatal-warnings").
259 FlagForEachArg("-D ", ctx.DeviceConfig().SepolicyM4Defs()).
260 FlagWithArg("-D mls_num_sens=", strconv.Itoa(MlsSens)).
Inseob Kim6e384f32022-03-10 13:15:05 +0900261 FlagWithArg("-D mls_num_cats=", strconv.Itoa(c.mlsCats())).
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900262 FlagWithArg("-D target_arch=", ctx.DeviceConfig().DeviceArch()).
263 FlagWithArg("-D target_with_asan=", c.withAsan(ctx)).
Inseob Kim4360c192021-03-23 20:52:53 +0900264 FlagWithArg("-D target_with_dexpreopt=", strconv.FormatBool(ctx.DeviceConfig().WithDexpreopt())).
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900265 FlagWithArg("-D target_with_native_coverage=", strconv.FormatBool(ctx.DeviceConfig().ClangCoverageEnabled() || ctx.DeviceConfig().GcovCoverageEnabled())).
266 FlagWithArg("-D target_build_variant=", c.buildVariant(ctx)).
267 FlagWithArg("-D target_full_treble=", c.sepolicySplit(ctx)).
268 FlagWithArg("-D target_compatible_property=", c.compatibleProperty(ctx)).
269 FlagWithArg("-D target_treble_sysprop_neverallow=", c.trebleSyspropNeverallow(ctx)).
270 FlagWithArg("-D target_enforce_sysprop_owner=", c.enforceSyspropOwner(ctx)).
271 FlagWithArg("-D target_exclude_build_test=", strconv.FormatBool(proptools.Bool(c.properties.Exclude_build_test))).
272 FlagWithArg("-D target_requires_insecure_execmem_for_swiftshader=", strconv.FormatBool(ctx.DeviceConfig().RequiresInsecureExecmemForSwiftshader())).
Hridya Valsarajua885dd82021-04-26 16:32:17 -0700273 FlagWithArg("-D target_enforce_debugfs_restriction=", c.enforceDebugfsRestrictions(ctx)).
Inseob Kim5bbcd682021-12-28 14:57:03 +0900274 FlagWithArg("-D target_recovery=", strconv.FormatBool(c.isTargetRecovery())).
Inseob Kim8697fc82024-04-16 14:45:32 +0900275 FlagWithArg("-D target_board_api_level=", c.boardApiLevel(ctx)).
Inseob Kim6cd0ddd2023-10-25 23:48:16 +0900276 Flags(flagsToM4Macros(flags)).
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900277 Flag("-s").
Inseob Kim0a707fa2021-12-09 23:35:11 +0900278 Inputs(srcs).
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900279 Text("> ").Output(conf)
280
281 rule.Build("conf", "Transform policy to conf: "+ctx.ModuleName())
282 return conf
283}
284
Inseob Kimbf7f4a42024-02-14 13:53:39 +0900285func (c *policyConf) DepsMutator(ctx android.BottomUpMutatorContext) {
286 c.flagDeps(ctx)
287}
288
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900289func (c *policyConf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900290 if !c.installable() {
291 c.SkipInstall()
292 }
Inseob Kim31db2742021-06-08 10:31:09 +0900293
294 c.installSource = c.transformPolicyToConf(ctx)
295 c.installPath = android.PathForModuleInstall(ctx, "etc")
296 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900297}
298
299func (c *policyConf) AndroidMkEntries() []android.AndroidMkEntries {
300 return []android.AndroidMkEntries{android.AndroidMkEntries{
301 OutputFile: android.OptionalPathForPath(c.installSource),
302 Class: "ETC",
303 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
304 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
305 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.installable())
Colin Cross6c7f9372022-01-11 19:35:43 -0800306 entries.SetPath("LOCAL_MODULE_PATH", c.installPath)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900307 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
308 },
309 },
310 }}
311}
312
313func (c *policyConf) OutputFiles(tag string) (android.Paths, error) {
314 if tag == "" {
315 return android.Paths{c.installSource}, nil
316 }
317 return nil, fmt.Errorf("Unknown tag %q", tag)
318}
319
320var _ android.OutputFileProducer = (*policyConf)(nil)
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900321
322type policyCilProperties struct {
323 // Name of the output. Default is {module_name}
324 Stem *string
325
326 // Policy file to be compiled to cil file.
327 Src *string `android:"path"`
328
Sandro143988d2022-08-05 11:38:56 +0000329 // If true, the input policy file is a binary policy that will be decompiled to a cil file.
330 // Defaults to false.
331 Decompile_binary *bool
332
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900333 // Additional cil files to be added in the end of the output. This is to support workarounds
334 // which are not supported by the policy language.
335 Additional_cil_files []string `android:"path"`
336
337 // Cil files to be filtered out by the filter_out tool of "build_sepolicy". Used to build
338 // exported policies
339 Filter_out []string `android:"path"`
340
341 // Whether to remove line markers (denoted by ;;) out of compiled cil files. Defaults to false
342 Remove_line_marker *bool
343
344 // Whether to run secilc to check compiled policy or not. Defaults to true
345 Secilc_check *bool
346
347 // Whether to ignore neverallow when running secilc check. Defaults to
348 // SELINUX_IGNORE_NEVERALLOWS.
349 Ignore_neverallow *bool
350
351 // Whether this module is directly installable to one of the partitions. Default is true
352 Installable *bool
353}
354
355type policyCil struct {
356 android.ModuleBase
357
358 properties policyCilProperties
359
360 installSource android.Path
361 installPath android.InstallPath
362}
363
364// se_policy_cil compiles a policy.conf file to a cil file with checkpolicy, and optionally runs
365// secilc to check the output cil file. Affected by SELINUX_IGNORE_NEVERALLOWS.
366func policyCilFactory() android.Module {
367 c := &policyCil{}
368 c.AddProperties(&c.properties)
369 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
370 return c
371}
372
373func (c *policyCil) Installable() bool {
374 return proptools.BoolDefault(c.properties.Installable, true)
375}
376
377func (c *policyCil) stem() string {
378 return proptools.StringDefault(c.properties.Stem, c.Name())
379}
380
381func (c *policyCil) compileConfToCil(ctx android.ModuleContext, conf android.Path) android.OutputPath {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900382 cil := pathForModuleOut(ctx, c.stem())
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900383 rule := android.NewRuleBuilder(pctx, ctx)
Sandro143988d2022-08-05 11:38:56 +0000384 checkpolicyCmd := rule.Command().BuiltTool("checkpolicy").
Lokesh Gidra1269a172022-08-01 17:20:38 +0000385 Flag("-C"). // Write CIL
386 Flag("-M"). // Enable MLS
387 FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
388 FlagWithOutput("-o ", cil).
389 Input(conf)
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900390
Sandro143988d2022-08-05 11:38:56 +0000391 if proptools.Bool(c.properties.Decompile_binary) {
392 checkpolicyCmd.Flag("-b") // Read binary
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900393 }
394
395 if len(c.properties.Filter_out) > 0 {
396 rule.Command().BuiltTool("build_sepolicy").
397 Text("filter_out").
398 Flag("-f").
399 Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)).
400 FlagWithOutput("-t ", cil)
401 }
402
Sandro143988d2022-08-05 11:38:56 +0000403 if len(c.properties.Additional_cil_files) > 0 {
404 rule.Command().Text("cat").
405 Inputs(android.PathsForModuleSrc(ctx, c.properties.Additional_cil_files)).
406 Text(">> ").Output(cil)
407 }
408
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900409 if proptools.Bool(c.properties.Remove_line_marker) {
410 rule.Command().Text("grep -v").
411 Text(proptools.ShellEscape(";;")).
412 Text(cil.String()).
413 Text(">").
414 Text(cil.String() + ".tmp").
415 Text("&& mv").
416 Text(cil.String() + ".tmp").
417 Text(cil.String())
418 }
419
420 if proptools.BoolDefault(c.properties.Secilc_check, true) {
421 secilcCmd := rule.Command().BuiltTool("secilc").
422 Flag("-m"). // Multiple decls
423 FlagWithArg("-M ", "true"). // Enable MLS
424 Flag("-G"). // expand and remove auto generated attributes
425 FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
426 Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)). // Also add cil files which are filtered out
427 Text(cil.String()).
428 FlagWithArg("-o ", os.DevNull).
429 FlagWithArg("-f ", os.DevNull)
430
431 if proptools.BoolDefault(c.properties.Ignore_neverallow, ctx.Config().SelinuxIgnoreNeverallows()) {
432 secilcCmd.Flag("-N")
433 }
434 }
435
436 rule.Build("cil", "Building cil for "+ctx.ModuleName())
437 return cil
438}
439
440func (c *policyCil) GenerateAndroidBuildActions(ctx android.ModuleContext) {
441 if proptools.String(c.properties.Src) == "" {
442 ctx.PropertyErrorf("src", "must be specified")
443 return
444 }
445 conf := android.PathForModuleSrc(ctx, *c.properties.Src)
446 cil := c.compileConfToCil(ctx, conf)
447
Inseob Kim31db2742021-06-08 10:31:09 +0900448 if !c.Installable() {
449 c.SkipInstall()
450 }
451
Inseob Kim6cc75f42021-04-29 13:53:20 +0000452 if c.InstallInDebugRamdisk() {
453 // for userdebug_plat_sepolicy.cil
454 c.installPath = android.PathForModuleInstall(ctx)
455 } else {
456 c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
457 }
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900458 c.installSource = cil
459 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900460}
461
462func (c *policyCil) AndroidMkEntries() []android.AndroidMkEntries {
463 return []android.AndroidMkEntries{android.AndroidMkEntries{
464 OutputFile: android.OptionalPathForPath(c.installSource),
465 Class: "ETC",
466 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
467 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
468 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.Installable())
Colin Cross6c7f9372022-01-11 19:35:43 -0800469 entries.SetPath("LOCAL_MODULE_PATH", c.installPath)
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900470 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
471 },
472 },
473 }}
474}
475
476func (c *policyCil) OutputFiles(tag string) (android.Paths, error) {
477 if tag == "" {
478 return android.Paths{c.installSource}, nil
479 }
480 return nil, fmt.Errorf("Unknown tag %q", tag)
481}
482
483var _ android.OutputFileProducer = (*policyCil)(nil)
Inseob Kimb9d05112021-09-27 13:13:46 +0000484
485type policyBinaryProperties struct {
486 // Name of the output. Default is {module_name}
487 Stem *string
488
489 // Cil files to be compiled.
490 Srcs []string `android:"path"`
491
492 // Whether to ignore neverallow when running secilc check. Defaults to
493 // SELINUX_IGNORE_NEVERALLOWS.
494 Ignore_neverallow *bool
495
496 // Whether this module is directly installable to one of the partitions. Default is true
497 Installable *bool
Jiyong Parkef567212022-12-05 14:06:47 +0900498
499 // List of domains that are allowed to be in permissive mode on user builds.
500 Permissive_domains_on_user_builds []string
Inseob Kimb9d05112021-09-27 13:13:46 +0000501}
502
503type policyBinary struct {
504 android.ModuleBase
505
506 properties policyBinaryProperties
507
508 installSource android.Path
509 installPath android.InstallPath
510}
511
512// se_policy_binary compiles cil files to a binary sepolicy file with secilc. Usually sources of
513// se_policy_binary come from outputs of se_policy_cil modules.
514func policyBinaryFactory() android.Module {
515 c := &policyBinary{}
516 c.AddProperties(&c.properties)
517 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
518 return c
519}
520
Inseob Kim5bbcd682021-12-28 14:57:03 +0900521func (c *policyBinary) InstallInRoot() bool {
522 return c.InstallInRecovery()
523}
524
Inseob Kimb9d05112021-09-27 13:13:46 +0000525func (c *policyBinary) Installable() bool {
526 return proptools.BoolDefault(c.properties.Installable, true)
527}
528
529func (c *policyBinary) stem() string {
530 return proptools.StringDefault(c.properties.Stem, c.Name())
531}
532
533func (c *policyBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
534 if len(c.properties.Srcs) == 0 {
535 ctx.PropertyErrorf("srcs", "must be specified")
536 return
537 }
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900538 bin := pathForModuleOut(ctx, c.stem()+"_policy")
Inseob Kimb9d05112021-09-27 13:13:46 +0000539 rule := android.NewRuleBuilder(pctx, ctx)
540 secilcCmd := rule.Command().BuiltTool("secilc").
541 Flag("-m"). // Multiple decls
542 FlagWithArg("-M ", "true"). // Enable MLS
543 Flag("-G"). // expand and remove auto generated attributes
544 FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
545 Inputs(android.PathsForModuleSrc(ctx, c.properties.Srcs)).
546 FlagWithOutput("-o ", bin).
547 FlagWithArg("-f ", os.DevNull)
548
549 if proptools.BoolDefault(c.properties.Ignore_neverallow, ctx.Config().SelinuxIgnoreNeverallows()) {
550 secilcCmd.Flag("-N")
551 }
Inseob Kim3d5f9252021-12-21 20:42:35 +0900552 rule.Temporary(bin)
Inseob Kimb9d05112021-09-27 13:13:46 +0000553
Inseob Kim3d5f9252021-12-21 20:42:35 +0900554 // permissive check is performed only in user build (not debuggable).
555 if !ctx.Config().Debuggable() {
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900556 permissiveDomains := pathForModuleOut(ctx, c.stem()+"_permissive")
Jiyong Parkef567212022-12-05 14:06:47 +0900557 cmd := rule.Command().BuiltTool("sepolicy-analyze").
Inseob Kim3d5f9252021-12-21 20:42:35 +0900558 Input(bin).
Jiyong Parkef567212022-12-05 14:06:47 +0900559 Text("permissive")
560 // Filter-out domains listed in permissive_domains_on_user_builds
561 allowedDomains := c.properties.Permissive_domains_on_user_builds
562 if len(allowedDomains) != 0 {
563 cmd.Text("| { grep -Fxv")
564 for _, d := range allowedDomains {
565 cmd.FlagWithArg("-e ", proptools.ShellEscape(d))
566 }
567 cmd.Text(" || true; }") // no match doesn't fail the cmd
568 }
569 cmd.Text(" > ").Output(permissiveDomains)
Inseob Kim3d5f9252021-12-21 20:42:35 +0900570 rule.Temporary(permissiveDomains)
571
572 msg := `==========\n` +
573 `ERROR: permissive domains not allowed in user builds\n` +
574 `List of invalid domains:`
575
576 rule.Command().Text("if test").
577 FlagWithInput("-s ", permissiveDomains).
578 Text("; then echo").
579 Flag("-e").
580 Text(`"` + msg + `"`).
581 Text("&& cat ").
582 Input(permissiveDomains).
583 Text("; exit 1; fi")
584 }
585
Inseob Kim6c6f53b2023-04-26 11:03:35 +0900586 out := pathForModuleOut(ctx, c.stem())
Inseob Kim3d5f9252021-12-21 20:42:35 +0900587 rule.Command().Text("cp").
588 Flag("-f").
589 Input(bin).
590 Output(out)
591
592 rule.DeleteTemporaryFiles()
Inseob Kimb9d05112021-09-27 13:13:46 +0000593 rule.Build("secilc", "Compiling cil files for "+ctx.ModuleName())
594
595 if !c.Installable() {
596 c.SkipInstall()
597 }
598
Inseob Kim5bbcd682021-12-28 14:57:03 +0900599 if c.InstallInRecovery() {
600 // install in root
601 c.installPath = android.PathForModuleInstall(ctx)
602 } else {
603 c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
604 }
Inseob Kim3d5f9252021-12-21 20:42:35 +0900605 c.installSource = out
Inseob Kimb9d05112021-09-27 13:13:46 +0000606 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
607}
608
609func (c *policyBinary) AndroidMkEntries() []android.AndroidMkEntries {
610 return []android.AndroidMkEntries{android.AndroidMkEntries{
611 OutputFile: android.OptionalPathForPath(c.installSource),
612 Class: "ETC",
613 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
614 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
615 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.Installable())
Colin Cross6c7f9372022-01-11 19:35:43 -0800616 entries.SetPath("LOCAL_MODULE_PATH", c.installPath)
Inseob Kimb9d05112021-09-27 13:13:46 +0000617 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
618 },
619 },
620 }}
621}
622
623func (c *policyBinary) OutputFiles(tag string) (android.Paths, error) {
624 if tag == "" {
625 return android.Paths{c.installSource}, nil
626 }
627 return nil, fmt.Errorf("Unknown tag %q", tag)
628}
629
630var _ android.OutputFileProducer = (*policyBinary)(nil)