Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 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 | "io" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 20 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 25 | "android/soong/sysprop" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 28 | type selinuxContextsProperties struct { |
| 29 | // Filenames under sepolicy directories, which will be used to generate contexts file. |
| 30 | Srcs []string `android:"path"` |
| 31 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 32 | // Output file name. Defaults to module name |
| 33 | Stem *string |
| 34 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 35 | Product_variables struct { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 36 | Address_sanitize struct { |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 37 | Srcs []string `android:"path"` |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 41 | // Whether the comments in generated contexts file will be removed or not. |
| 42 | Remove_comment *bool |
| 43 | |
| 44 | // Whether the result context file is sorted with fc_sort or not. |
| 45 | Fc_sort *bool |
| 46 | |
| 47 | // Make this module available when building for recovery |
| 48 | Recovery_available *bool |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 51 | type seappProperties struct { |
| 52 | // Files containing neverallow rules. |
| 53 | Neverallow_files []string `android:"path"` |
| 54 | |
| 55 | // Precompiled sepolicy binary file which will be fed to checkseapp. |
| 56 | Sepolicy *string `android:"path"` |
| 57 | } |
| 58 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 59 | type selinuxContextsModule struct { |
| 60 | android.ModuleBase |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 61 | android.DefaultableModuleBase |
| 62 | flaggableModuleBase |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 63 | |
Jooyung Han | 804e234 | 2023-06-20 15:38:50 +0900 | [diff] [blame] | 64 | properties selinuxContextsProperties |
| 65 | seappProperties seappProperties |
| 66 | build func(ctx android.ModuleContext, inputs android.Paths) android.Path |
| 67 | deps func(ctx android.BottomUpMutatorContext) |
| 68 | outputPath android.Path |
| 69 | installPath android.InstallPath |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 70 | } |
| 71 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 72 | var _ flaggableModule = (*selinuxContextsModule)(nil) |
| 73 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 74 | var ( |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 75 | reuseContextsDepTag = dependencyTag{name: "reuseContexts"} |
| 76 | syspropLibraryDepTag = dependencyTag{name: "sysprop_library"} |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 77 | ) |
| 78 | |
| 79 | func init() { |
| 80 | pctx.HostBinToolVariable("fc_sort", "fc_sort") |
| 81 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 82 | android.RegisterModuleType("contexts_defaults", contextsDefaultsFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 83 | android.RegisterModuleType("file_contexts", fileFactory) |
| 84 | android.RegisterModuleType("hwservice_contexts", hwServiceFactory) |
| 85 | android.RegisterModuleType("property_contexts", propertyFactory) |
| 86 | android.RegisterModuleType("service_contexts", serviceFactory) |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 87 | android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 88 | android.RegisterModuleType("seapp_contexts", seappFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 89 | android.RegisterModuleType("vndservice_contexts", vndServiceFactory) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 90 | |
| 91 | android.RegisterModuleType("file_contexts_test", fileContextsTestFactory) |
| 92 | android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory) |
| 93 | android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory) |
| 94 | android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 95 | android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 96 | } |
| 97 | |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 98 | func (m *selinuxContextsModule) InstallInRoot() bool { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 99 | return m.InRecovery() |
| 100 | } |
| 101 | |
| 102 | func (m *selinuxContextsModule) InstallInRecovery() bool { |
| 103 | // ModuleBase.InRecovery() checks the image variant |
| 104 | return m.InRecovery() |
| 105 | } |
| 106 | |
| 107 | func (m *selinuxContextsModule) onlyInRecovery() bool { |
| 108 | // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property |
| 109 | return m.ModuleBase.InstallInRecovery() |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 112 | func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
Inseob Kim | bf7f4a4 | 2024-02-14 13:53:39 +0900 | [diff] [blame] | 113 | m.flagDeps(ctx) |
| 114 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 115 | if m.deps != nil { |
| 116 | m.deps(ctx) |
| 117 | } |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 118 | |
| 119 | if m.InRecovery() && !m.onlyInRecovery() { |
| 120 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 121 | {Mutator: "image", Variation: android.CoreVariation}, |
| 122 | }, reuseContextsDepTag, ctx.ModuleName()) |
| 123 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) { |
| 127 | for _, lib := range sysprop.SyspropLibraries(ctx.Config()) { |
| 128 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib) |
| 129 | } |
| 130 | } |
| 131 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 132 | func (m *selinuxContextsModule) stem() string { |
| 133 | return proptools.StringDefault(m.properties.Stem, m.Name()) |
| 134 | } |
| 135 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 136 | func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 137 | if m.InRecovery() { |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 138 | // Installing context files at the root of the recovery partition |
| 139 | m.installPath = android.PathForModuleInstall(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 140 | } else { |
| 141 | m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 142 | } |
| 143 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 144 | if m.InRecovery() && !m.onlyInRecovery() { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 145 | dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag) |
| 146 | |
| 147 | if reuseDeps, ok := dep.(*selinuxContextsModule); ok { |
| 148 | m.outputPath = reuseDeps.outputPath |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 149 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 150 | return |
| 151 | } |
| 152 | } |
| 153 | |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 154 | m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs)) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 155 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | func newModule() *selinuxContextsModule { |
| 159 | m := &selinuxContextsModule{} |
| 160 | m.AddProperties( |
| 161 | &m.properties, |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 162 | &m.seappProperties, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 163 | ) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 164 | initFlaggableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 165 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 166 | android.InitDefaultableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 167 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 168 | m.selinuxContextsHook(ctx) |
| 169 | }) |
| 170 | return m |
| 171 | } |
| 172 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 173 | type contextsDefaults struct { |
| 174 | android.ModuleBase |
| 175 | android.DefaultsModuleBase |
| 176 | } |
| 177 | |
| 178 | // contexts_defaults provides a set of properties that can be inherited by other contexts modules. |
| 179 | // (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a |
| 180 | // contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are |
| 181 | // erged (when possible) by prepending the default module's values to the depending module's values. |
| 182 | func contextsDefaultsFactory() android.Module { |
| 183 | m := &contextsDefaults{} |
| 184 | m.AddProperties( |
| 185 | &selinuxContextsProperties{}, |
| 186 | &seappProperties{}, |
Inseob Kim | bf7f4a4 | 2024-02-14 13:53:39 +0900 | [diff] [blame] | 187 | &flaggableModuleProperties{}, |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 188 | ) |
| 189 | android.InitDefaultsModule(m) |
| 190 | return m |
| 191 | } |
| 192 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 193 | func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) { |
| 194 | // TODO: clean this up to use build/soong/android/variable.go after b/79249983 |
| 195 | var srcs []string |
| 196 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 197 | for _, sanitize := range ctx.Config().SanitizeDevice() { |
| 198 | if sanitize == "address" { |
| 199 | srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...) |
| 200 | break |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | m.properties.Srcs = append(m.properties.Srcs, srcs...) |
| 205 | } |
| 206 | |
| 207 | func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData { |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 208 | nameSuffix := "" |
| 209 | if m.InRecovery() && !m.onlyInRecovery() { |
| 210 | nameSuffix = ".recovery" |
| 211 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 212 | return android.AndroidMkData{ |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 213 | Class: "ETC", |
| 214 | OutputFile: android.OptionalPathForPath(m.outputPath), |
| 215 | SubName: nameSuffix, |
| 216 | Extra: []android.AndroidMkExtraFunc{ |
| 217 | func(w io.Writer, outputFile android.Path) { |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 218 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 219 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem()) |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 220 | }, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 221 | }, |
| 222 | } |
| 223 | } |
| 224 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 225 | func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 226 | if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 227 | ctx.PropertyErrorf("recovery_available", |
| 228 | "doesn't make sense at the same time as `recovery: true`") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 232 | func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 233 | return !m.ModuleBase.InstallInRecovery() |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 237 | return false |
| 238 | } |
| 239 | |
| 240 | func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 241 | return false |
| 242 | } |
| 243 | |
Inseob Kim | 6cc75f4 | 2021-04-29 13:53:20 +0000 | [diff] [blame] | 244 | func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 245 | return false |
| 246 | } |
| 247 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 248 | func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 249 | return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available) |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 257 | } |
| 258 | |
| 259 | var _ android.ImageInterface = (*selinuxContextsModule)(nil) |
| 260 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 261 | func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 262 | builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 263 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 264 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 265 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 266 | newlineFile := pathForModuleOut(ctx, "newline") |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 267 | |
| 268 | rule.Command().Text("echo").FlagWithOutput("> ", newlineFile) |
| 269 | rule.Temporary(newlineFile) |
| 270 | |
| 271 | var inputsWithNewline android.Paths |
| 272 | for _, input := range inputs { |
| 273 | inputsWithNewline = append(inputsWithNewline, input, newlineFile) |
| 274 | } |
| 275 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 276 | flags := m.getBuildFlags(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 277 | rule.Command(). |
Dan Willemsen | 3c3e59b | 2019-06-19 10:52:50 -0700 | [diff] [blame] | 278 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 279 | Text("--fatal-warnings -s"). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 280 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame] | 281 | Flags(flagsToM4Macros(flags)). |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 282 | Inputs(inputsWithNewline). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 283 | FlagWithOutput("> ", builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 284 | |
| 285 | if proptools.Bool(m.properties.Remove_comment) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 286 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 287 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 288 | remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 289 | |
| 290 | rule.Command(). |
| 291 | Text("sed -e 's/#.*$//' -e '/^$/d'"). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 292 | Input(builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 293 | FlagWithOutput("> ", remove_comment_output) |
| 294 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 295 | builtContext = remove_comment_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if proptools.Bool(m.properties.Fc_sort) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 299 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 300 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 301 | sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 302 | |
| 303 | rule.Command(). |
| 304 | Tool(ctx.Config().HostToolPath(ctx, "fc_sort")). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 305 | FlagWithInput("-i ", builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 306 | FlagWithOutput("-o ", sorted_output) |
| 307 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 308 | builtContext = sorted_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 309 | } |
| 310 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 311 | ret := pathForModuleOut(ctx, m.stem()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 312 | rule.Temporary(builtContext) |
| 313 | rule.Command().Text("cp").Input(builtContext).Output(ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 314 | |
| 315 | rule.DeleteTemporaryFiles() |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 316 | rule.Build("selinux_contexts", "building contexts: "+m.Name()) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 317 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 318 | return ret |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 319 | } |
| 320 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 321 | func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | dfa4a48 | 2023-11-01 17:58:18 +0900 | [diff] [blame] | 322 | if m.properties.Remove_comment == nil { |
| 323 | m.properties.Remove_comment = proptools.BoolPtr(true) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 324 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 325 | return m.buildGeneralContexts(ctx, inputs) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | func fileFactory() android.Module { |
| 329 | m := newModule() |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 330 | m.build = m.buildFileContexts |
| 331 | return m |
| 332 | } |
| 333 | |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 334 | func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 335 | if m.properties.Remove_comment == nil { |
| 336 | m.properties.Remove_comment = proptools.BoolPtr(true) |
| 337 | } |
| 338 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 339 | return m.buildGeneralContexts(ctx, inputs) |
| 340 | } |
| 341 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 342 | func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, input android.Path) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 343 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 344 | ApiLevelR := android.ApiLevelOrPanic(ctx, "R") |
| 345 | |
| 346 | rule := android.NewRuleBuilder(pctx, ctx) |
| 347 | |
| 348 | // This list is from vts_treble_sys_prop_test. |
| 349 | allowedPropertyPrefixes := []string{ |
| 350 | "ctl.odm.", |
| 351 | "ctl.vendor.", |
| 352 | "ctl.start$odm.", |
| 353 | "ctl.start$vendor.", |
| 354 | "ctl.stop$odm.", |
| 355 | "ctl.stop$vendor.", |
| 356 | "init.svc.odm.", |
| 357 | "init.svc.vendor.", |
| 358 | "ro.boot.", |
| 359 | "ro.hardware.", |
| 360 | "ro.odm.", |
| 361 | "ro.vendor.", |
| 362 | "odm.", |
| 363 | "persist.odm.", |
| 364 | "persist.vendor.", |
| 365 | "vendor.", |
| 366 | } |
| 367 | |
| 368 | // persist.camera is also allowed for devices launching with R or eariler |
| 369 | if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) { |
| 370 | allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.") |
| 371 | } |
| 372 | |
| 373 | var allowedContextPrefixes []string |
| 374 | |
| 375 | if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) { |
| 376 | // This list is from vts_treble_sys_prop_test. |
| 377 | allowedContextPrefixes = []string{ |
| 378 | "vendor_", |
| 379 | "odm_", |
| 380 | } |
| 381 | } |
| 382 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 383 | cmd := rule.Command(). |
| 384 | BuiltTool("check_prop_prefix"). |
| 385 | FlagWithInput("--property-contexts ", input). |
| 386 | FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$' |
| 387 | FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 388 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 389 | if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() { |
| 390 | cmd.Flag("--strict") |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 391 | } |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 392 | |
Luca Stefani | 0b2d711 | 2023-11-16 17:42:52 +0100 | [diff] [blame] | 393 | out := pathForModuleOut(ctx, ctx.ModuleName()+"_namespace_checked") |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 394 | rule.Command().Text("cp -f").Input(input).Output(out) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 395 | rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName()) |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 396 | return out |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 397 | } |
| 398 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 399 | func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 400 | // vendor/odm properties are enforced for devices launching with Android Q or later. So, if |
| 401 | // vendor/odm, make sure that only vendor/odm properties exist. |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 402 | builtCtxFile := m.buildGeneralContexts(ctx, inputs) |
| 403 | |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 404 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 405 | ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q") |
| 406 | if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) { |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 407 | builtCtxFile = m.checkVendorPropertyNamespace(ctx, builtCtxFile) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 408 | } |
| 409 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 410 | var apiFiles android.Paths |
| 411 | ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) { |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 412 | i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath }) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 413 | if !ok { |
| 414 | panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName())) |
| 415 | } |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 416 | if api := i.CurrentSyspropApiFile(); api.Valid() { |
| 417 | apiFiles = append(apiFiles, api.Path()) |
| 418 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 419 | }) |
| 420 | |
| 421 | // check compatibility with sysprop_library |
| 422 | if len(apiFiles) > 0 { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 423 | out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked") |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 424 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 425 | |
| 426 | msg := `\n******************************\n` + |
| 427 | `API of sysprop_library doesn't match with property_contexts\n` + |
| 428 | `Please fix the breakage and rebuild.\n` + |
| 429 | `******************************\n` |
| 430 | |
| 431 | rule.Command(). |
| 432 | Text("( "). |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 433 | BuiltTool("sysprop_type_checker"). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 434 | FlagForEachInput("--api ", apiFiles). |
| 435 | FlagWithInput("--context ", builtCtxFile). |
| 436 | Text(" || ( echo").Flag("-e"). |
| 437 | Flag(`"` + msg + `"`). |
| 438 | Text("; exit 38) )") |
| 439 | |
| 440 | rule.Command().Text("cp -f").Input(builtCtxFile).Output(out) |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 441 | rule.Build("property_contexts_check_api", "checking API: "+m.Name()) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 442 | builtCtxFile = out |
| 443 | } |
| 444 | |
| 445 | return builtCtxFile |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 446 | } |
| 447 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 448 | func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool { |
| 449 | if !ctx.SocSpecific() && !ctx.DeviceSpecific() { |
| 450 | return false |
| 451 | } |
| 452 | |
| 453 | return ctx.DeviceConfig().CheckVendorSeappViolations() |
| 454 | } |
| 455 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 456 | func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 457 | neverallowFile := pathForModuleOut(ctx, "neverallow") |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 458 | ret := pathForModuleOut(ctx, "checkseapp", m.stem()) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 459 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 460 | // Step 1. Generate a M4 processed neverallow file |
| 461 | flags := m.getBuildFlags(ctx) |
| 462 | m4NeverallowFile := pathForModuleOut(ctx, "neverallow.m4out") |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 463 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 464 | rule.Command(). |
| 465 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 466 | Flag("--fatal-warnings"). |
| 467 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
| 468 | Flags(flagsToM4Macros(flags)). |
| 469 | Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)). |
| 470 | FlagWithOutput("> ", m4NeverallowFile) |
| 471 | |
| 472 | rule.Temporary(m4NeverallowFile) |
| 473 | rule.Command(). |
| 474 | Text("( grep"). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 475 | Flag("-ihe"). |
| 476 | Text("'^neverallow'"). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 477 | Input(m4NeverallowFile). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 478 | Text(">"). |
| 479 | Output(neverallowFile). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 480 | Text("|| true )") // to make ninja happy even when result is empty |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 481 | |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 482 | // Step 2. Generate a M4 processed contexts file |
| 483 | builtCtx := m.buildGeneralContexts(ctx, inputs) |
| 484 | |
| 485 | // Step 3. checkseapp |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 486 | rule.Temporary(neverallowFile) |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 487 | checkCmd := rule.Command().BuiltTool("checkseapp"). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 488 | FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))). |
| 489 | FlagWithOutput("-o ", ret). |
Inseob Kim | 085f22f | 2023-11-09 11:13:01 +0900 | [diff] [blame] | 490 | Input(builtCtx). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 491 | Input(neverallowFile) |
| 492 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 493 | if m.shouldCheckCoredomain(ctx) { |
| 494 | checkCmd.Flag("-c") // check coredomain for vendor contexts |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 497 | rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name()) |
| 498 | return ret |
| 499 | } |
| 500 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 501 | func hwServiceFactory() android.Module { |
| 502 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 503 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 504 | return m |
| 505 | } |
| 506 | |
| 507 | func propertyFactory() android.Module { |
| 508 | m := newModule() |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 509 | m.build = m.buildPropertyContexts |
| 510 | m.deps = m.propertyContextsDeps |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 511 | return m |
| 512 | } |
| 513 | |
| 514 | func serviceFactory() android.Module { |
| 515 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 516 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 517 | return m |
| 518 | } |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 519 | |
| 520 | func keystoreKeyFactory() android.Module { |
| 521 | m := newModule() |
| 522 | m.build = m.buildGeneralContexts |
| 523 | return m |
| 524 | } |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 525 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 526 | func seappFactory() android.Module { |
| 527 | m := newModule() |
| 528 | m.build = m.buildSeappContexts |
| 529 | return m |
| 530 | } |
| 531 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 532 | func vndServiceFactory() android.Module { |
| 533 | m := newModule() |
| 534 | m.build = m.buildGeneralContexts |
| 535 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 536 | if !ctx.SocSpecific() { |
| 537 | ctx.ModuleErrorf(m.Name(), "must set vendor: true") |
| 538 | return |
| 539 | } |
| 540 | }) |
| 541 | return m |
| 542 | } |
| 543 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 544 | var _ android.OutputFileProducer = (*selinuxContextsModule)(nil) |
| 545 | |
| 546 | // Implements android.OutputFileProducer |
| 547 | func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) { |
| 548 | if tag == "" { |
| 549 | return []android.Path{m.outputPath}, nil |
| 550 | } |
| 551 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 552 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 553 | |
| 554 | type contextsTestProperties struct { |
| 555 | // Contexts files to be tested. |
| 556 | Srcs []string `android:"path"` |
| 557 | |
| 558 | // Precompiled sepolicy binary to be tesed together. |
| 559 | Sepolicy *string `android:"path"` |
| 560 | } |
| 561 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 562 | type fileContextsTestProperties struct { |
| 563 | // Test data. File passed to `checkfc -t` to validate how contexts are resolved. |
| 564 | Test_data *string `android:"path"` |
| 565 | } |
| 566 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 567 | type contextsTestModule struct { |
| 568 | android.ModuleBase |
| 569 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 570 | // The type of context. |
| 571 | context contextType |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 572 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 573 | properties contextsTestProperties |
| 574 | fileProperties fileContextsTestProperties |
| 575 | testTimestamp android.OutputPath |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 578 | type contextType int |
| 579 | |
| 580 | const ( |
| 581 | FileContext contextType = iota |
| 582 | PropertyContext |
| 583 | ServiceContext |
| 584 | HwServiceContext |
| 585 | VndServiceContext |
| 586 | ) |
| 587 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 588 | // checkfc parses a context file and checks for syntax errors. |
| 589 | // If -s is specified, the service backend is used to verify binder services. |
| 590 | // If -l is specified, the service backend is used to verify hwbinder services. |
| 591 | // Otherwise, context_file is assumed to be a file_contexts file |
| 592 | // If -e is specified, then the context_file is allowed to be empty. |
| 593 | |
| 594 | // file_contexts_test tests given file_contexts files with checkfc. |
| 595 | func fileContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 596 | m := &contextsTestModule{context: FileContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 597 | m.AddProperties(&m.properties) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 598 | m.AddProperties(&m.fileProperties) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 599 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 600 | return m |
| 601 | } |
| 602 | |
| 603 | // property_contexts_test tests given property_contexts files with property_info_checker. |
| 604 | func propertyContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 605 | m := &contextsTestModule{context: PropertyContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 606 | m.AddProperties(&m.properties) |
| 607 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 608 | return m |
| 609 | } |
| 610 | |
| 611 | // hwservice_contexts_test tests given hwservice_contexts files with checkfc. |
| 612 | func hwserviceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 613 | m := &contextsTestModule{context: HwServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 614 | m.AddProperties(&m.properties) |
| 615 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 616 | return m |
| 617 | } |
| 618 | |
| 619 | // service_contexts_test tests given service_contexts files with checkfc. |
| 620 | func serviceContextsTestFactory() android.Module { |
| 621 | // checkfc -s: service_contexts test |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 622 | m := &contextsTestModule{context: ServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 623 | m.AddProperties(&m.properties) |
| 624 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 625 | return m |
| 626 | } |
| 627 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 628 | // vndservice_contexts_test tests given vndservice_contexts files with checkfc. |
| 629 | func vndServiceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 630 | m := &contextsTestModule{context: VndServiceContext} |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 631 | m.AddProperties(&m.properties) |
| 632 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 633 | return m |
| 634 | } |
| 635 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 636 | func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 637 | tool := "checkfc" |
| 638 | if m.context == PropertyContext { |
| 639 | tool = "property_info_checker" |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | if len(m.properties.Srcs) == 0 { |
| 643 | ctx.PropertyErrorf("srcs", "can't be empty") |
| 644 | return |
| 645 | } |
| 646 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 647 | validateWithPolicy := true |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 648 | if proptools.String(m.properties.Sepolicy) == "" { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 649 | if m.context == FileContext { |
| 650 | if proptools.String(m.fileProperties.Test_data) == "" { |
| 651 | ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided") |
| 652 | return |
| 653 | } |
| 654 | validateWithPolicy = false |
| 655 | } else { |
| 656 | ctx.PropertyErrorf("sepolicy", "can't be empty") |
| 657 | return |
| 658 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 661 | flags := []string(nil) |
| 662 | switch m.context { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 663 | case FileContext: |
| 664 | if !validateWithPolicy { |
| 665 | flags = []string{"-t"} |
| 666 | } |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 667 | case ServiceContext: |
| 668 | flags = []string{"-s" /* binder services */} |
| 669 | case HwServiceContext: |
| 670 | flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */} |
| 671 | case VndServiceContext: |
| 672 | flags = []string{"-e" /* allow empty */, "-v" /* vnd service */} |
| 673 | } |
| 674 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 675 | srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 676 | rule := android.NewRuleBuilder(pctx, ctx) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 677 | |
| 678 | if validateWithPolicy { |
| 679 | sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy)) |
| 680 | rule.Command().BuiltTool(tool). |
| 681 | Flags(flags). |
| 682 | Input(sepolicy). |
| 683 | Inputs(srcs) |
| 684 | } else { |
| 685 | test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data)) |
| 686 | rule.Command().BuiltTool(tool). |
| 687 | Flags(flags). |
| 688 | Inputs(srcs). |
| 689 | Input(test_data) |
| 690 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 691 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 692 | m.testTimestamp = pathForModuleOut(ctx, "timestamp") |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 693 | rule.Command().Text("touch").Output(m.testTimestamp) |
| 694 | rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName()) |
| 695 | } |
| 696 | |
| 697 | func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 698 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 699 | Class: "FAKE", |
| 700 | // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it. |
| 701 | // Without OutputFile this module won't be exported to Makefile. |
| 702 | OutputFile: android.OptionalPathForPath(m.testTimestamp), |
| 703 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 704 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 705 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 706 | entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String()) |
| 707 | }, |
| 708 | }, |
| 709 | }} |
| 710 | } |
| 711 | |
| 712 | // contextsTestModule implements ImageInterface to be able to include recovery_available contexts |
| 713 | // modules as its sources. |
| 714 | func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
| 715 | } |
| 716 | |
| 717 | func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 718 | return true |
| 719 | } |
| 720 | |
| 721 | func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 722 | return false |
| 723 | } |
| 724 | |
| 725 | func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 726 | return false |
| 727 | } |
| 728 | |
| 729 | func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 730 | return false |
| 731 | } |
| 732 | |
| 733 | func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 734 | return false |
| 735 | } |
| 736 | |
| 737 | func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 738 | return nil |
| 739 | } |
| 740 | |
| 741 | func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 742 | } |
| 743 | |
| 744 | var _ android.ImageInterface = (*contextsTestModule)(nil) |