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