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