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