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