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