blob: 9d574c98ebce5b9ae4a30f664c99802e5ec71f8f [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"
20 "strconv"
21
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25)
26
27const (
28 // TODO: sync with Android.mk
29 MlsSens = 1
30 MlsCats = 1024
31 PolicyVers = 30
32)
33
34func init() {
35 android.RegisterModuleType("se_policy_conf", policyConfFactory)
Inseob Kimdf1a0de2021-03-17 19:05:02 +090036 android.RegisterModuleType("se_policy_cil", policyCilFactory)
Inseob Kim7e8bd1e2021-03-17 18:59:43 +090037}
38
39type policyConfProperties struct {
40 // Name of the output. Default is {module_name}
41 Stem *string
42
43 // Policy files to be compiled to cil file.
44 Srcs []string `android:"path"`
45
46 // Target build variant (user / userdebug / eng). Default follows the current lunch target
47 Build_variant *string
48
49 // Whether to exclude build test or not. Default is false
50 Exclude_build_test *bool
51
52 // Whether to include asan specific policies or not. Default follows the current lunch target
53 With_asan *bool
54
55 // Whether to build CTS specific policy or not. Default is false
56 Cts *bool
57
58 // Whether this module is directly installable to one of the partitions. Default is true
59 Installable *bool
60}
61
62type policyConf struct {
63 android.ModuleBase
64
65 properties policyConfProperties
66
67 installSource android.Path
68 installPath android.InstallPath
69}
70
71// se_policy_conf merges collection of policy files into a policy.conf file to be processed by
72// checkpolicy.
73func policyConfFactory() android.Module {
74 c := &policyConf{}
75 c.AddProperties(&c.properties)
76 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
77 return c
78}
79
80func (c *policyConf) installable() bool {
81 return proptools.BoolDefault(c.properties.Installable, true)
82}
83
84func (c *policyConf) stem() string {
85 return proptools.StringDefault(c.properties.Stem, c.Name())
86}
87
88func (c *policyConf) buildVariant(ctx android.ModuleContext) string {
89 if variant := proptools.String(c.properties.Build_variant); variant != "" {
90 return variant
91 }
92 if ctx.Config().Eng() {
93 return "eng"
94 }
95 if ctx.Config().Debuggable() {
96 return "userdebug"
97 }
98 return "user"
99}
100
101func (c *policyConf) cts() bool {
102 return proptools.Bool(c.properties.Cts)
103}
104
105func (c *policyConf) withAsan(ctx android.ModuleContext) string {
106 isAsanDevice := android.InList("address", ctx.Config().SanitizeDevice())
107 return strconv.FormatBool(proptools.BoolDefault(c.properties.With_asan, isAsanDevice))
108}
109
110func (c *policyConf) sepolicySplit(ctx android.ModuleContext) string {
111 if c.cts() {
112 return "cts"
113 }
114 return strconv.FormatBool(ctx.DeviceConfig().SepolicySplit())
115}
116
117func (c *policyConf) compatibleProperty(ctx android.ModuleContext) string {
118 if c.cts() {
119 return "cts"
120 }
121 return "true"
122}
123
124func (c *policyConf) trebleSyspropNeverallow(ctx android.ModuleContext) string {
125 if c.cts() {
126 return "cts"
127 }
128 return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenTrebleSyspropNeverallow())
129}
130
131func (c *policyConf) enforceSyspropOwner(ctx android.ModuleContext) string {
132 if c.cts() {
133 return "cts"
134 }
135 return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenEnforceSyspropOwner())
136}
137
138func (c *policyConf) transformPolicyToConf(ctx android.ModuleContext) android.OutputPath {
139 conf := android.PathForModuleOut(ctx, "conf").OutputPath
140 rule := android.NewRuleBuilder(pctx, ctx)
141 rule.Command().Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")).
142 Flag("--fatal-warnings").
143 FlagForEachArg("-D ", ctx.DeviceConfig().SepolicyM4Defs()).
144 FlagWithArg("-D mls_num_sens=", strconv.Itoa(MlsSens)).
145 FlagWithArg("-D mls_num_cats=", strconv.Itoa(MlsCats)).
146 FlagWithArg("-D target_arch=", ctx.DeviceConfig().DeviceArch()).
147 FlagWithArg("-D target_with_asan=", c.withAsan(ctx)).
Inseob Kim4360c192021-03-23 20:52:53 +0900148 FlagWithArg("-D target_with_dexpreopt=", strconv.FormatBool(ctx.DeviceConfig().WithDexpreopt())).
Inseob Kim7e8bd1e2021-03-17 18:59:43 +0900149 FlagWithArg("-D target_with_native_coverage=", strconv.FormatBool(ctx.DeviceConfig().ClangCoverageEnabled() || ctx.DeviceConfig().GcovCoverageEnabled())).
150 FlagWithArg("-D target_build_variant=", c.buildVariant(ctx)).
151 FlagWithArg("-D target_full_treble=", c.sepolicySplit(ctx)).
152 FlagWithArg("-D target_compatible_property=", c.compatibleProperty(ctx)).
153 FlagWithArg("-D target_treble_sysprop_neverallow=", c.trebleSyspropNeverallow(ctx)).
154 FlagWithArg("-D target_enforce_sysprop_owner=", c.enforceSyspropOwner(ctx)).
155 FlagWithArg("-D target_exclude_build_test=", strconv.FormatBool(proptools.Bool(c.properties.Exclude_build_test))).
156 FlagWithArg("-D target_requires_insecure_execmem_for_swiftshader=", strconv.FormatBool(ctx.DeviceConfig().RequiresInsecureExecmemForSwiftshader())).
157 Flag("-s").
158 Inputs(android.PathsForModuleSrc(ctx, c.properties.Srcs)).
159 Text("> ").Output(conf)
160
161 rule.Build("conf", "Transform policy to conf: "+ctx.ModuleName())
162 return conf
163}
164
165func (c *policyConf) DepsMutator(ctx android.BottomUpMutatorContext) {
166 // do nothing
167}
168
169func (c *policyConf) GenerateAndroidBuildActions(ctx android.ModuleContext) {
170 c.installSource = c.transformPolicyToConf(ctx)
171 c.installPath = android.PathForModuleInstall(ctx, "etc")
172 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
173
174 if !c.installable() {
175 c.SkipInstall()
176 }
177}
178
179func (c *policyConf) AndroidMkEntries() []android.AndroidMkEntries {
180 return []android.AndroidMkEntries{android.AndroidMkEntries{
181 OutputFile: android.OptionalPathForPath(c.installSource),
182 Class: "ETC",
183 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
184 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
185 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.installable())
186 entries.SetPath("LOCAL_MODULE_PATH", c.installPath.ToMakePath())
187 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
188 },
189 },
190 }}
191}
192
193func (c *policyConf) OutputFiles(tag string) (android.Paths, error) {
194 if tag == "" {
195 return android.Paths{c.installSource}, nil
196 }
197 return nil, fmt.Errorf("Unknown tag %q", tag)
198}
199
200var _ android.OutputFileProducer = (*policyConf)(nil)
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900201
202type policyCilProperties struct {
203 // Name of the output. Default is {module_name}
204 Stem *string
205
206 // Policy file to be compiled to cil file.
207 Src *string `android:"path"`
208
209 // Additional cil files to be added in the end of the output. This is to support workarounds
210 // which are not supported by the policy language.
211 Additional_cil_files []string `android:"path"`
212
213 // Cil files to be filtered out by the filter_out tool of "build_sepolicy". Used to build
214 // exported policies
215 Filter_out []string `android:"path"`
216
217 // Whether to remove line markers (denoted by ;;) out of compiled cil files. Defaults to false
218 Remove_line_marker *bool
219
220 // Whether to run secilc to check compiled policy or not. Defaults to true
221 Secilc_check *bool
222
223 // Whether to ignore neverallow when running secilc check. Defaults to
224 // SELINUX_IGNORE_NEVERALLOWS.
225 Ignore_neverallow *bool
226
227 // Whether this module is directly installable to one of the partitions. Default is true
228 Installable *bool
229}
230
231type policyCil struct {
232 android.ModuleBase
233
234 properties policyCilProperties
235
236 installSource android.Path
237 installPath android.InstallPath
238}
239
240// se_policy_cil compiles a policy.conf file to a cil file with checkpolicy, and optionally runs
241// secilc to check the output cil file. Affected by SELINUX_IGNORE_NEVERALLOWS.
242func policyCilFactory() android.Module {
243 c := &policyCil{}
244 c.AddProperties(&c.properties)
245 android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
246 return c
247}
248
249func (c *policyCil) Installable() bool {
250 return proptools.BoolDefault(c.properties.Installable, true)
251}
252
253func (c *policyCil) stem() string {
254 return proptools.StringDefault(c.properties.Stem, c.Name())
255}
256
257func (c *policyCil) compileConfToCil(ctx android.ModuleContext, conf android.Path) android.OutputPath {
258 cil := android.PathForModuleOut(ctx, c.stem()).OutputPath
259 rule := android.NewRuleBuilder(pctx, ctx)
260 rule.Command().BuiltTool("checkpolicy").
261 Flag("-C"). // Write CIL
262 Flag("-M"). // Enable MLS
263 FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
264 FlagWithOutput("-o ", cil).
265 Input(conf)
266
267 if len(c.properties.Additional_cil_files) > 0 {
268 rule.Command().Text("cat").
269 Inputs(android.PathsForModuleSrc(ctx, c.properties.Additional_cil_files)).
270 Text(">> ").Output(cil)
271 }
272
273 if len(c.properties.Filter_out) > 0 {
274 rule.Command().BuiltTool("build_sepolicy").
275 Text("filter_out").
276 Flag("-f").
277 Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)).
278 FlagWithOutput("-t ", cil)
279 }
280
281 if proptools.Bool(c.properties.Remove_line_marker) {
282 rule.Command().Text("grep -v").
283 Text(proptools.ShellEscape(";;")).
284 Text(cil.String()).
285 Text(">").
286 Text(cil.String() + ".tmp").
287 Text("&& mv").
288 Text(cil.String() + ".tmp").
289 Text(cil.String())
290 }
291
292 if proptools.BoolDefault(c.properties.Secilc_check, true) {
293 secilcCmd := rule.Command().BuiltTool("secilc").
294 Flag("-m"). // Multiple decls
295 FlagWithArg("-M ", "true"). // Enable MLS
296 Flag("-G"). // expand and remove auto generated attributes
297 FlagWithArg("-c ", strconv.Itoa(PolicyVers)).
298 Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)). // Also add cil files which are filtered out
299 Text(cil.String()).
300 FlagWithArg("-o ", os.DevNull).
301 FlagWithArg("-f ", os.DevNull)
302
303 if proptools.BoolDefault(c.properties.Ignore_neverallow, ctx.Config().SelinuxIgnoreNeverallows()) {
304 secilcCmd.Flag("-N")
305 }
306 }
307
308 rule.Build("cil", "Building cil for "+ctx.ModuleName())
309 return cil
310}
311
312func (c *policyCil) GenerateAndroidBuildActions(ctx android.ModuleContext) {
313 if proptools.String(c.properties.Src) == "" {
314 ctx.PropertyErrorf("src", "must be specified")
315 return
316 }
317 conf := android.PathForModuleSrc(ctx, *c.properties.Src)
318 cil := c.compileConfToCil(ctx, conf)
319
Inseob Kim6cc75f42021-04-29 13:53:20 +0000320 if c.InstallInDebugRamdisk() {
321 // for userdebug_plat_sepolicy.cil
322 c.installPath = android.PathForModuleInstall(ctx)
323 } else {
324 c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
325 }
Inseob Kimdf1a0de2021-03-17 19:05:02 +0900326 c.installSource = cil
327 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
328
329 if !c.Installable() {
330 c.SkipInstall()
331 }
332}
333
334func (c *policyCil) AndroidMkEntries() []android.AndroidMkEntries {
335 return []android.AndroidMkEntries{android.AndroidMkEntries{
336 OutputFile: android.OptionalPathForPath(c.installSource),
337 Class: "ETC",
338 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
339 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
340 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.Installable())
341 entries.SetPath("LOCAL_MODULE_PATH", c.installPath.ToMakePath())
342 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
343 },
344 },
345 }}
346}
347
348func (c *policyCil) OutputFiles(tag string) (android.Paths, error) {
349 if tag == "" {
350 return android.Paths{c.installSource}, nil
351 }
352 return nil, fmt.Errorf("Unknown tag %q", tag)
353}
354
355var _ android.OutputFileProducer = (*policyCil)(nil)