Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package selinux |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "os" |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 20 | "sort" |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 21 | "strconv" |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 22 | "strings" |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | |
| 26 | "android/soong/android" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | // TODO: sync with Android.mk |
| 31 | MlsSens = 1 |
| 32 | MlsCats = 1024 |
| 33 | PolicyVers = 30 |
| 34 | ) |
| 35 | |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 36 | // This order should be kept. checkpolicy syntax requires it. |
| 37 | var policyConfOrder = []string{ |
| 38 | "security_classes", |
| 39 | "initial_sids", |
| 40 | "access_vectors", |
| 41 | "global_macros", |
| 42 | "neverallow_macros", |
| 43 | "mls_macros", |
| 44 | "mls_decl", |
| 45 | "mls", |
| 46 | "policy_capabilities", |
| 47 | "te_macros", |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 48 | "ioctl_defines", |
| 49 | "ioctl_macros", |
Inseob Kim | 1e79634 | 2022-06-09 11:26:35 +0900 | [diff] [blame] | 50 | "attributes|*.te", |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 51 | "roles_decl", |
| 52 | "roles", |
| 53 | "users", |
| 54 | "initial_sid_contexts", |
| 55 | "fs_use", |
| 56 | "genfs_contexts", |
| 57 | "port_contexts", |
| 58 | } |
| 59 | |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 60 | func init() { |
| 61 | android.RegisterModuleType("se_policy_conf", policyConfFactory) |
Inseob Kim | df1a0de | 2021-03-17 19:05:02 +0900 | [diff] [blame] | 62 | android.RegisterModuleType("se_policy_cil", policyCilFactory) |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 63 | android.RegisterModuleType("se_policy_binary", policyBinaryFactory) |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | type 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 Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 85 | // Whether to build recovery specific policy or not. Default is false |
| 86 | Target_recovery *bool |
| 87 | |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 88 | // Whether this module is directly installable to one of the partitions. Default is true |
| 89 | Installable *bool |
Inseob Kim | 6e384f3 | 2022-03-10 13:15:05 +0900 | [diff] [blame] | 90 | |
| 91 | // Desired number of MLS categories. Defaults to 1024 |
| 92 | Mls_cats *int64 |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | type policyConf struct { |
| 96 | android.ModuleBase |
| 97 | |
| 98 | properties policyConfProperties |
| 99 | |
| 100 | installSource android.Path |
| 101 | installPath android.InstallPath |
| 102 | } |
| 103 | |
| 104 | // se_policy_conf merges collection of policy files into a policy.conf file to be processed by |
| 105 | // checkpolicy. |
| 106 | func policyConfFactory() android.Module { |
| 107 | c := &policyConf{} |
| 108 | c.AddProperties(&c.properties) |
| 109 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 110 | return c |
| 111 | } |
| 112 | |
| 113 | func (c *policyConf) installable() bool { |
| 114 | return proptools.BoolDefault(c.properties.Installable, true) |
| 115 | } |
| 116 | |
| 117 | func (c *policyConf) stem() string { |
| 118 | return proptools.StringDefault(c.properties.Stem, c.Name()) |
| 119 | } |
| 120 | |
| 121 | func (c *policyConf) buildVariant(ctx android.ModuleContext) string { |
| 122 | if variant := proptools.String(c.properties.Build_variant); variant != "" { |
| 123 | return variant |
| 124 | } |
| 125 | if ctx.Config().Eng() { |
| 126 | return "eng" |
| 127 | } |
| 128 | if ctx.Config().Debuggable() { |
| 129 | return "userdebug" |
| 130 | } |
| 131 | return "user" |
| 132 | } |
| 133 | |
| 134 | func (c *policyConf) cts() bool { |
| 135 | return proptools.Bool(c.properties.Cts) |
| 136 | } |
| 137 | |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 138 | func (c *policyConf) isTargetRecovery() bool { |
| 139 | return proptools.Bool(c.properties.Target_recovery) |
| 140 | } |
| 141 | |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 142 | func (c *policyConf) withAsan(ctx android.ModuleContext) string { |
| 143 | isAsanDevice := android.InList("address", ctx.Config().SanitizeDevice()) |
| 144 | return strconv.FormatBool(proptools.BoolDefault(c.properties.With_asan, isAsanDevice)) |
| 145 | } |
| 146 | |
| 147 | func (c *policyConf) sepolicySplit(ctx android.ModuleContext) string { |
| 148 | if c.cts() { |
| 149 | return "cts" |
| 150 | } |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 151 | if c.isTargetRecovery() { |
| 152 | return "false" |
| 153 | } |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 154 | return strconv.FormatBool(ctx.DeviceConfig().SepolicySplit()) |
| 155 | } |
| 156 | |
| 157 | func (c *policyConf) compatibleProperty(ctx android.ModuleContext) string { |
| 158 | if c.cts() { |
| 159 | return "cts" |
| 160 | } |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 161 | if c.isTargetRecovery() { |
| 162 | return "false" |
| 163 | } |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 164 | return "true" |
| 165 | } |
| 166 | |
| 167 | func (c *policyConf) trebleSyspropNeverallow(ctx android.ModuleContext) string { |
| 168 | if c.cts() { |
| 169 | return "cts" |
| 170 | } |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 171 | if c.isTargetRecovery() { |
| 172 | return "false" |
| 173 | } |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 174 | return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenTrebleSyspropNeverallow()) |
| 175 | } |
| 176 | |
| 177 | func (c *policyConf) enforceSyspropOwner(ctx android.ModuleContext) string { |
| 178 | if c.cts() { |
| 179 | return "cts" |
| 180 | } |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 181 | if c.isTargetRecovery() { |
| 182 | return "false" |
| 183 | } |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 184 | return strconv.FormatBool(!ctx.DeviceConfig().BuildBrokenEnforceSyspropOwner()) |
| 185 | } |
| 186 | |
Hridya Valsaraju | a885dd8 | 2021-04-26 16:32:17 -0700 | [diff] [blame] | 187 | func (c *policyConf) enforceDebugfsRestrictions(ctx android.ModuleContext) string { |
| 188 | if c.cts() { |
| 189 | return "cts" |
| 190 | } |
| 191 | return strconv.FormatBool(ctx.DeviceConfig().BuildDebugfsRestrictionsEnabled()) |
| 192 | } |
| 193 | |
Inseob Kim | 6e384f3 | 2022-03-10 13:15:05 +0900 | [diff] [blame] | 194 | func (c *policyConf) mlsCats() int { |
| 195 | return proptools.IntDefault(c.properties.Mls_cats, MlsCats) |
| 196 | } |
| 197 | |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 198 | func findPolicyConfOrder(name string) int { |
| 199 | for idx, pattern := range policyConfOrder { |
Inseob Kim | 1e79634 | 2022-06-09 11:26:35 +0900 | [diff] [blame] | 200 | // We could use regexp but it seems like an overkill |
| 201 | if pattern == "attributes|*.te" && (name == "attributes" || strings.HasSuffix(name, ".te")) { |
| 202 | return idx |
| 203 | } else if pattern == name { |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 204 | return idx |
| 205 | } |
| 206 | } |
| 207 | // name is not matched |
| 208 | return len(policyConfOrder) |
| 209 | } |
| 210 | |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 211 | func (c *policyConf) transformPolicyToConf(ctx android.ModuleContext) android.OutputPath { |
Inseob Kim | 6c5fa54 | 2022-02-09 23:27:04 +0900 | [diff] [blame] | 212 | conf := android.PathForModuleOut(ctx, c.stem()).OutputPath |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 213 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 214 | |
| 215 | srcs := android.PathsForModuleSrc(ctx, c.properties.Srcs) |
| 216 | sort.SliceStable(srcs, func(x, y int) bool { |
| 217 | return findPolicyConfOrder(srcs[x].Base()) < findPolicyConfOrder(srcs[y].Base()) |
| 218 | }) |
| 219 | |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 220 | rule.Command().Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 221 | Flag("--fatal-warnings"). |
| 222 | FlagForEachArg("-D ", ctx.DeviceConfig().SepolicyM4Defs()). |
| 223 | FlagWithArg("-D mls_num_sens=", strconv.Itoa(MlsSens)). |
Inseob Kim | 6e384f3 | 2022-03-10 13:15:05 +0900 | [diff] [blame] | 224 | FlagWithArg("-D mls_num_cats=", strconv.Itoa(c.mlsCats())). |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 225 | FlagWithArg("-D target_arch=", ctx.DeviceConfig().DeviceArch()). |
| 226 | FlagWithArg("-D target_with_asan=", c.withAsan(ctx)). |
Inseob Kim | 4360c19 | 2021-03-23 20:52:53 +0900 | [diff] [blame] | 227 | FlagWithArg("-D target_with_dexpreopt=", strconv.FormatBool(ctx.DeviceConfig().WithDexpreopt())). |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 228 | FlagWithArg("-D target_with_native_coverage=", strconv.FormatBool(ctx.DeviceConfig().ClangCoverageEnabled() || ctx.DeviceConfig().GcovCoverageEnabled())). |
| 229 | FlagWithArg("-D target_build_variant=", c.buildVariant(ctx)). |
| 230 | FlagWithArg("-D target_full_treble=", c.sepolicySplit(ctx)). |
| 231 | FlagWithArg("-D target_compatible_property=", c.compatibleProperty(ctx)). |
| 232 | FlagWithArg("-D target_treble_sysprop_neverallow=", c.trebleSyspropNeverallow(ctx)). |
| 233 | FlagWithArg("-D target_enforce_sysprop_owner=", c.enforceSyspropOwner(ctx)). |
| 234 | FlagWithArg("-D target_exclude_build_test=", strconv.FormatBool(proptools.Bool(c.properties.Exclude_build_test))). |
| 235 | FlagWithArg("-D target_requires_insecure_execmem_for_swiftshader=", strconv.FormatBool(ctx.DeviceConfig().RequiresInsecureExecmemForSwiftshader())). |
Hridya Valsaraju | a885dd8 | 2021-04-26 16:32:17 -0700 | [diff] [blame] | 236 | FlagWithArg("-D target_enforce_debugfs_restriction=", c.enforceDebugfsRestrictions(ctx)). |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 237 | FlagWithArg("-D target_recovery=", strconv.FormatBool(c.isTargetRecovery())). |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 238 | Flag("-s"). |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 239 | Inputs(srcs). |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 240 | Text("> ").Output(conf) |
| 241 | |
| 242 | rule.Build("conf", "Transform policy to conf: "+ctx.ModuleName()) |
| 243 | return conf |
| 244 | } |
| 245 | |
| 246 | func (c *policyConf) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 247 | // do nothing |
| 248 | } |
| 249 | |
| 250 | func (c *policyConf) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 251 | if !c.installable() { |
| 252 | c.SkipInstall() |
| 253 | } |
Inseob Kim | 31db274 | 2021-06-08 10:31:09 +0900 | [diff] [blame] | 254 | |
| 255 | c.installSource = c.transformPolicyToConf(ctx) |
| 256 | c.installPath = android.PathForModuleInstall(ctx, "etc") |
| 257 | ctx.InstallFile(c.installPath, c.stem(), c.installSource) |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | func (c *policyConf) AndroidMkEntries() []android.AndroidMkEntries { |
| 261 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 262 | OutputFile: android.OptionalPathForPath(c.installSource), |
| 263 | Class: "ETC", |
| 264 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 265 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 266 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.installable()) |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 267 | entries.SetPath("LOCAL_MODULE_PATH", c.installPath) |
Inseob Kim | 7e8bd1e | 2021-03-17 18:59:43 +0900 | [diff] [blame] | 268 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem()) |
| 269 | }, |
| 270 | }, |
| 271 | }} |
| 272 | } |
| 273 | |
| 274 | func (c *policyConf) OutputFiles(tag string) (android.Paths, error) { |
| 275 | if tag == "" { |
| 276 | return android.Paths{c.installSource}, nil |
| 277 | } |
| 278 | return nil, fmt.Errorf("Unknown tag %q", tag) |
| 279 | } |
| 280 | |
| 281 | var _ android.OutputFileProducer = (*policyConf)(nil) |
Inseob Kim | df1a0de | 2021-03-17 19:05:02 +0900 | [diff] [blame] | 282 | |
| 283 | type policyCilProperties struct { |
| 284 | // Name of the output. Default is {module_name} |
| 285 | Stem *string |
| 286 | |
| 287 | // Policy file to be compiled to cil file. |
| 288 | Src *string `android:"path"` |
| 289 | |
| 290 | // Additional cil files to be added in the end of the output. This is to support workarounds |
| 291 | // which are not supported by the policy language. |
| 292 | Additional_cil_files []string `android:"path"` |
| 293 | |
| 294 | // Cil files to be filtered out by the filter_out tool of "build_sepolicy". Used to build |
| 295 | // exported policies |
| 296 | Filter_out []string `android:"path"` |
| 297 | |
| 298 | // Whether to remove line markers (denoted by ;;) out of compiled cil files. Defaults to false |
| 299 | Remove_line_marker *bool |
| 300 | |
| 301 | // Whether to run secilc to check compiled policy or not. Defaults to true |
| 302 | Secilc_check *bool |
| 303 | |
| 304 | // Whether to ignore neverallow when running secilc check. Defaults to |
| 305 | // SELINUX_IGNORE_NEVERALLOWS. |
| 306 | Ignore_neverallow *bool |
| 307 | |
| 308 | // Whether this module is directly installable to one of the partitions. Default is true |
| 309 | Installable *bool |
| 310 | } |
| 311 | |
| 312 | type policyCil struct { |
| 313 | android.ModuleBase |
| 314 | |
| 315 | properties policyCilProperties |
| 316 | |
| 317 | installSource android.Path |
| 318 | installPath android.InstallPath |
| 319 | } |
| 320 | |
| 321 | // se_policy_cil compiles a policy.conf file to a cil file with checkpolicy, and optionally runs |
| 322 | // secilc to check the output cil file. Affected by SELINUX_IGNORE_NEVERALLOWS. |
| 323 | func policyCilFactory() android.Module { |
| 324 | c := &policyCil{} |
| 325 | c.AddProperties(&c.properties) |
| 326 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 327 | return c |
| 328 | } |
| 329 | |
| 330 | func (c *policyCil) Installable() bool { |
| 331 | return proptools.BoolDefault(c.properties.Installable, true) |
| 332 | } |
| 333 | |
| 334 | func (c *policyCil) stem() string { |
| 335 | return proptools.StringDefault(c.properties.Stem, c.Name()) |
| 336 | } |
| 337 | |
| 338 | func (c *policyCil) compileConfToCil(ctx android.ModuleContext, conf android.Path) android.OutputPath { |
| 339 | cil := android.PathForModuleOut(ctx, c.stem()).OutputPath |
| 340 | rule := android.NewRuleBuilder(pctx, ctx) |
| 341 | rule.Command().BuiltTool("checkpolicy"). |
| 342 | Flag("-C"). // Write CIL |
| 343 | Flag("-M"). // Enable MLS |
| 344 | FlagWithArg("-c ", strconv.Itoa(PolicyVers)). |
| 345 | FlagWithOutput("-o ", cil). |
| 346 | Input(conf) |
| 347 | |
| 348 | if len(c.properties.Additional_cil_files) > 0 { |
| 349 | rule.Command().Text("cat"). |
| 350 | Inputs(android.PathsForModuleSrc(ctx, c.properties.Additional_cil_files)). |
| 351 | Text(">> ").Output(cil) |
| 352 | } |
| 353 | |
| 354 | if len(c.properties.Filter_out) > 0 { |
| 355 | rule.Command().BuiltTool("build_sepolicy"). |
| 356 | Text("filter_out"). |
| 357 | Flag("-f"). |
| 358 | Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)). |
| 359 | FlagWithOutput("-t ", cil) |
| 360 | } |
| 361 | |
| 362 | if proptools.Bool(c.properties.Remove_line_marker) { |
| 363 | rule.Command().Text("grep -v"). |
| 364 | Text(proptools.ShellEscape(";;")). |
| 365 | Text(cil.String()). |
| 366 | Text(">"). |
| 367 | Text(cil.String() + ".tmp"). |
| 368 | Text("&& mv"). |
| 369 | Text(cil.String() + ".tmp"). |
| 370 | Text(cil.String()) |
| 371 | } |
| 372 | |
| 373 | if proptools.BoolDefault(c.properties.Secilc_check, true) { |
| 374 | secilcCmd := rule.Command().BuiltTool("secilc"). |
| 375 | Flag("-m"). // Multiple decls |
| 376 | FlagWithArg("-M ", "true"). // Enable MLS |
| 377 | Flag("-G"). // expand and remove auto generated attributes |
| 378 | FlagWithArg("-c ", strconv.Itoa(PolicyVers)). |
| 379 | Inputs(android.PathsForModuleSrc(ctx, c.properties.Filter_out)). // Also add cil files which are filtered out |
| 380 | Text(cil.String()). |
| 381 | FlagWithArg("-o ", os.DevNull). |
| 382 | FlagWithArg("-f ", os.DevNull) |
| 383 | |
| 384 | if proptools.BoolDefault(c.properties.Ignore_neverallow, ctx.Config().SelinuxIgnoreNeverallows()) { |
| 385 | secilcCmd.Flag("-N") |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | rule.Build("cil", "Building cil for "+ctx.ModuleName()) |
| 390 | return cil |
| 391 | } |
| 392 | |
| 393 | func (c *policyCil) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 394 | if proptools.String(c.properties.Src) == "" { |
| 395 | ctx.PropertyErrorf("src", "must be specified") |
| 396 | return |
| 397 | } |
| 398 | conf := android.PathForModuleSrc(ctx, *c.properties.Src) |
| 399 | cil := c.compileConfToCil(ctx, conf) |
| 400 | |
Inseob Kim | 31db274 | 2021-06-08 10:31:09 +0900 | [diff] [blame] | 401 | if !c.Installable() { |
| 402 | c.SkipInstall() |
| 403 | } |
| 404 | |
Inseob Kim | 6cc75f4 | 2021-04-29 13:53:20 +0000 | [diff] [blame] | 405 | if c.InstallInDebugRamdisk() { |
| 406 | // for userdebug_plat_sepolicy.cil |
| 407 | c.installPath = android.PathForModuleInstall(ctx) |
| 408 | } else { |
| 409 | c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 410 | } |
Inseob Kim | df1a0de | 2021-03-17 19:05:02 +0900 | [diff] [blame] | 411 | c.installSource = cil |
| 412 | ctx.InstallFile(c.installPath, c.stem(), c.installSource) |
Inseob Kim | df1a0de | 2021-03-17 19:05:02 +0900 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | func (c *policyCil) AndroidMkEntries() []android.AndroidMkEntries { |
| 416 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 417 | OutputFile: android.OptionalPathForPath(c.installSource), |
| 418 | Class: "ETC", |
| 419 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 420 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 421 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.Installable()) |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 422 | entries.SetPath("LOCAL_MODULE_PATH", c.installPath) |
Inseob Kim | df1a0de | 2021-03-17 19:05:02 +0900 | [diff] [blame] | 423 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem()) |
| 424 | }, |
| 425 | }, |
| 426 | }} |
| 427 | } |
| 428 | |
| 429 | func (c *policyCil) OutputFiles(tag string) (android.Paths, error) { |
| 430 | if tag == "" { |
| 431 | return android.Paths{c.installSource}, nil |
| 432 | } |
| 433 | return nil, fmt.Errorf("Unknown tag %q", tag) |
| 434 | } |
| 435 | |
| 436 | var _ android.OutputFileProducer = (*policyCil)(nil) |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 437 | |
| 438 | type policyBinaryProperties struct { |
| 439 | // Name of the output. Default is {module_name} |
| 440 | Stem *string |
| 441 | |
| 442 | // Cil files to be compiled. |
| 443 | Srcs []string `android:"path"` |
| 444 | |
| 445 | // Whether to ignore neverallow when running secilc check. Defaults to |
| 446 | // SELINUX_IGNORE_NEVERALLOWS. |
| 447 | Ignore_neverallow *bool |
| 448 | |
| 449 | // Whether this module is directly installable to one of the partitions. Default is true |
| 450 | Installable *bool |
| 451 | } |
| 452 | |
| 453 | type policyBinary struct { |
| 454 | android.ModuleBase |
| 455 | |
| 456 | properties policyBinaryProperties |
| 457 | |
| 458 | installSource android.Path |
| 459 | installPath android.InstallPath |
| 460 | } |
| 461 | |
| 462 | // se_policy_binary compiles cil files to a binary sepolicy file with secilc. Usually sources of |
| 463 | // se_policy_binary come from outputs of se_policy_cil modules. |
| 464 | func policyBinaryFactory() android.Module { |
| 465 | c := &policyBinary{} |
| 466 | c.AddProperties(&c.properties) |
| 467 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 468 | return c |
| 469 | } |
| 470 | |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 471 | func (c *policyBinary) InstallInRoot() bool { |
| 472 | return c.InstallInRecovery() |
| 473 | } |
| 474 | |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 475 | func (c *policyBinary) Installable() bool { |
| 476 | return proptools.BoolDefault(c.properties.Installable, true) |
| 477 | } |
| 478 | |
| 479 | func (c *policyBinary) stem() string { |
| 480 | return proptools.StringDefault(c.properties.Stem, c.Name()) |
| 481 | } |
| 482 | |
| 483 | func (c *policyBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 484 | if len(c.properties.Srcs) == 0 { |
| 485 | ctx.PropertyErrorf("srcs", "must be specified") |
| 486 | return |
| 487 | } |
Inseob Kim | 3d5f925 | 2021-12-21 20:42:35 +0900 | [diff] [blame] | 488 | bin := android.PathForModuleOut(ctx, c.stem()+"_policy") |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 489 | rule := android.NewRuleBuilder(pctx, ctx) |
| 490 | secilcCmd := rule.Command().BuiltTool("secilc"). |
| 491 | Flag("-m"). // Multiple decls |
| 492 | FlagWithArg("-M ", "true"). // Enable MLS |
| 493 | Flag("-G"). // expand and remove auto generated attributes |
| 494 | FlagWithArg("-c ", strconv.Itoa(PolicyVers)). |
| 495 | Inputs(android.PathsForModuleSrc(ctx, c.properties.Srcs)). |
| 496 | FlagWithOutput("-o ", bin). |
| 497 | FlagWithArg("-f ", os.DevNull) |
| 498 | |
| 499 | if proptools.BoolDefault(c.properties.Ignore_neverallow, ctx.Config().SelinuxIgnoreNeverallows()) { |
| 500 | secilcCmd.Flag("-N") |
| 501 | } |
Inseob Kim | 3d5f925 | 2021-12-21 20:42:35 +0900 | [diff] [blame] | 502 | rule.Temporary(bin) |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 503 | |
Inseob Kim | 3d5f925 | 2021-12-21 20:42:35 +0900 | [diff] [blame] | 504 | // permissive check is performed only in user build (not debuggable). |
| 505 | if !ctx.Config().Debuggable() { |
| 506 | permissiveDomains := android.PathForModuleOut(ctx, c.stem()+"_permissive") |
| 507 | rule.Command().BuiltTool("sepolicy-analyze"). |
| 508 | Input(bin). |
| 509 | Text("permissive"). |
| 510 | Text(" > "). |
| 511 | Output(permissiveDomains) |
| 512 | rule.Temporary(permissiveDomains) |
| 513 | |
| 514 | msg := `==========\n` + |
| 515 | `ERROR: permissive domains not allowed in user builds\n` + |
| 516 | `List of invalid domains:` |
| 517 | |
| 518 | rule.Command().Text("if test"). |
| 519 | FlagWithInput("-s ", permissiveDomains). |
| 520 | Text("; then echo"). |
| 521 | Flag("-e"). |
| 522 | Text(`"` + msg + `"`). |
| 523 | Text("&& cat "). |
| 524 | Input(permissiveDomains). |
| 525 | Text("; exit 1; fi") |
| 526 | } |
| 527 | |
| 528 | out := android.PathForModuleOut(ctx, c.stem()) |
| 529 | rule.Command().Text("cp"). |
| 530 | Flag("-f"). |
| 531 | Input(bin). |
| 532 | Output(out) |
| 533 | |
| 534 | rule.DeleteTemporaryFiles() |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 535 | rule.Build("secilc", "Compiling cil files for "+ctx.ModuleName()) |
| 536 | |
| 537 | if !c.Installable() { |
| 538 | c.SkipInstall() |
| 539 | } |
| 540 | |
Inseob Kim | 5bbcd68 | 2021-12-28 14:57:03 +0900 | [diff] [blame] | 541 | if c.InstallInRecovery() { |
| 542 | // install in root |
| 543 | c.installPath = android.PathForModuleInstall(ctx) |
| 544 | } else { |
| 545 | c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 546 | } |
Inseob Kim | 3d5f925 | 2021-12-21 20:42:35 +0900 | [diff] [blame] | 547 | c.installSource = out |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 548 | ctx.InstallFile(c.installPath, c.stem(), c.installSource) |
| 549 | } |
| 550 | |
| 551 | func (c *policyBinary) AndroidMkEntries() []android.AndroidMkEntries { |
| 552 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 553 | OutputFile: android.OptionalPathForPath(c.installSource), |
| 554 | Class: "ETC", |
| 555 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 556 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 557 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", !c.Installable()) |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 558 | entries.SetPath("LOCAL_MODULE_PATH", c.installPath) |
Inseob Kim | b9d0511 | 2021-09-27 13:13:46 +0000 | [diff] [blame] | 559 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem()) |
| 560 | }, |
| 561 | }, |
| 562 | }} |
| 563 | } |
| 564 | |
| 565 | func (c *policyBinary) OutputFiles(tag string) (android.Paths, error) { |
| 566 | if tag == "" { |
| 567 | return android.Paths{c.installSource}, nil |
| 568 | } |
| 569 | return nil, fmt.Errorf("Unknown tag %q", tag) |
| 570 | } |
| 571 | |
| 572 | var _ android.OutputFileProducer = (*policyBinary)(nil) |