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" |
| 20 | "strings" |
| 21 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 26 | "android/soong/sysprop" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 29 | type selinuxContextsProperties struct { |
| 30 | // Filenames under sepolicy directories, which will be used to generate contexts file. |
| 31 | Srcs []string `android:"path"` |
| 32 | |
| 33 | Product_variables struct { |
| 34 | Debuggable struct { |
| 35 | Srcs []string |
| 36 | } |
| 37 | |
| 38 | Address_sanitize struct { |
| 39 | Srcs []string |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Whether reqd_mask directory is included to sepolicy directories or not. |
| 44 | Reqd_mask *bool |
| 45 | |
| 46 | // Whether the comments in generated contexts file will be removed or not. |
| 47 | Remove_comment *bool |
| 48 | |
| 49 | // Whether the result context file is sorted with fc_sort or not. |
| 50 | Fc_sort *bool |
| 51 | |
| 52 | // Make this module available when building for recovery |
| 53 | Recovery_available *bool |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type fileContextsProperties struct { |
| 57 | // flatten_apex can be used to specify additional sources of file_contexts. |
| 58 | // Apex paths, /system/apex/{apex_name}, will be amended to the paths of file_contexts |
| 59 | // entries. |
| 60 | Flatten_apex struct { |
| 61 | Srcs []string |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | type selinuxContextsModule struct { |
| 66 | android.ModuleBase |
| 67 | |
| 68 | properties selinuxContextsProperties |
| 69 | fileContextsProperties fileContextsProperties |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 70 | build func(ctx android.ModuleContext, inputs android.Paths) android.Path |
| 71 | deps func(ctx android.BottomUpMutatorContext) |
| 72 | outputPath android.Path |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 73 | installPath android.InstallPath |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | var ( |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 77 | reuseContextsDepTag = dependencyTag{name: "reuseContexts"} |
| 78 | syspropLibraryDepTag = dependencyTag{name: "sysprop_library"} |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 79 | ) |
| 80 | |
| 81 | func init() { |
| 82 | pctx.HostBinToolVariable("fc_sort", "fc_sort") |
| 83 | |
| 84 | android.RegisterModuleType("file_contexts", fileFactory) |
| 85 | android.RegisterModuleType("hwservice_contexts", hwServiceFactory) |
| 86 | android.RegisterModuleType("property_contexts", propertyFactory) |
| 87 | android.RegisterModuleType("service_contexts", serviceFactory) |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 88 | android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 91 | func (m *selinuxContextsModule) InstallInRoot() bool { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 92 | return m.InRecovery() |
| 93 | } |
| 94 | |
| 95 | func (m *selinuxContextsModule) InstallInRecovery() bool { |
| 96 | // ModuleBase.InRecovery() checks the image variant |
| 97 | return m.InRecovery() |
| 98 | } |
| 99 | |
| 100 | func (m *selinuxContextsModule) onlyInRecovery() bool { |
| 101 | // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property |
| 102 | return m.ModuleBase.InstallInRecovery() |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 105 | func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 106 | if m.deps != nil { |
| 107 | m.deps(ctx) |
| 108 | } |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 109 | |
| 110 | if m.InRecovery() && !m.onlyInRecovery() { |
| 111 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 112 | {Mutator: "image", Variation: android.CoreVariation}, |
| 113 | }, reuseContextsDepTag, ctx.ModuleName()) |
| 114 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) { |
| 118 | for _, lib := range sysprop.SyspropLibraries(ctx.Config()) { |
| 119 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib) |
| 120 | } |
| 121 | } |
| 122 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 123 | func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 124 | if m.InRecovery() { |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 125 | // Installing context files at the root of the recovery partition |
| 126 | m.installPath = android.PathForModuleInstall(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 127 | } else { |
| 128 | m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 129 | } |
| 130 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 131 | if m.InRecovery() && !m.onlyInRecovery() { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 132 | dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag) |
| 133 | |
| 134 | if reuseDeps, ok := dep.(*selinuxContextsModule); ok { |
| 135 | m.outputPath = reuseDeps.outputPath |
| 136 | ctx.InstallFile(m.installPath, m.Name(), m.outputPath) |
| 137 | return |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | var inputs android.Paths |
| 142 | |
Paul Duffin | 532bde1 | 2021-07-09 22:53:03 +0100 | [diff] [blame^] | 143 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 144 | depTag := ctx.OtherModuleDependencyTag(dep) |
| 145 | if !android.IsSourceDepTagWithOutputTag(depTag, "") { |
| 146 | return |
| 147 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 148 | segroup, ok := dep.(*fileGroup) |
| 149 | if !ok { |
| 150 | ctx.ModuleErrorf("srcs dependency %q is not an selinux filegroup", |
| 151 | ctx.OtherModuleName(dep)) |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | if ctx.ProductSpecific() { |
| 156 | inputs = append(inputs, segroup.ProductPrivateSrcs()...) |
| 157 | } else if ctx.SocSpecific() { |
Inseob Kim | 8ada8a7 | 2020-11-09 20:58:58 +0900 | [diff] [blame] | 158 | if ctx.DeviceConfig().BoardSepolicyVers() == ctx.DeviceConfig().PlatformSepolicyVersion() { |
| 159 | inputs = append(inputs, segroup.SystemVendorSrcs()...) |
| 160 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 161 | inputs = append(inputs, segroup.VendorSrcs()...) |
| 162 | } else if ctx.DeviceSpecific() { |
| 163 | inputs = append(inputs, segroup.OdmSrcs()...) |
Bowgo Tsai | 86a048d | 2019-09-09 22:04:06 +0800 | [diff] [blame] | 164 | } else if ctx.SystemExtSpecific() { |
| 165 | inputs = append(inputs, segroup.SystemExtPrivateSrcs()...) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 166 | } else { |
| 167 | inputs = append(inputs, segroup.SystemPrivateSrcs()...) |
Felix | 342b58a | 2020-03-02 16:13:12 +0100 | [diff] [blame] | 168 | inputs = append(inputs, segroup.SystemPublicSrcs()...) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if proptools.Bool(m.properties.Reqd_mask) { |
Inseob Kim | 8ada8a7 | 2020-11-09 20:58:58 +0900 | [diff] [blame] | 172 | if ctx.SocSpecific() || ctx.DeviceSpecific() { |
| 173 | inputs = append(inputs, segroup.VendorReqdMaskSrcs()...) |
| 174 | } else { |
| 175 | inputs = append(inputs, segroup.SystemReqdMaskSrcs()...) |
| 176 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 177 | } |
| 178 | }) |
| 179 | |
| 180 | for _, src := range m.properties.Srcs { |
| 181 | // Module sources are handled above with VisitDirectDepsWithTag |
| 182 | if android.SrcIsModule(src) == "" { |
| 183 | inputs = append(inputs, android.PathForModuleSrc(ctx, src)) |
| 184 | } |
| 185 | } |
| 186 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 187 | m.outputPath = m.build(ctx, inputs) |
| 188 | ctx.InstallFile(m.installPath, ctx.ModuleName(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func newModule() *selinuxContextsModule { |
| 192 | m := &selinuxContextsModule{} |
| 193 | m.AddProperties( |
| 194 | &m.properties, |
| 195 | ) |
| 196 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 197 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 198 | m.selinuxContextsHook(ctx) |
| 199 | }) |
| 200 | return m |
| 201 | } |
| 202 | |
| 203 | func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) { |
| 204 | // TODO: clean this up to use build/soong/android/variable.go after b/79249983 |
| 205 | var srcs []string |
| 206 | |
| 207 | if ctx.Config().Debuggable() { |
| 208 | srcs = append(srcs, m.properties.Product_variables.Debuggable.Srcs...) |
| 209 | } |
| 210 | |
| 211 | for _, sanitize := range ctx.Config().SanitizeDevice() { |
| 212 | if sanitize == "address" { |
| 213 | srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...) |
| 214 | break |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | m.properties.Srcs = append(m.properties.Srcs, srcs...) |
| 219 | } |
| 220 | |
| 221 | func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData { |
| 222 | return android.AndroidMkData{ |
| 223 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 224 | nameSuffix := "" |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 225 | if m.InRecovery() && !m.onlyInRecovery() { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 226 | nameSuffix = ".recovery" |
| 227 | } |
| 228 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 229 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 230 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix) |
Bob Badour | 4eeb6a2 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 231 | data.Entries.WriteLicenseVariables(w) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 232 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
| 233 | if m.Owner() != "" { |
| 234 | fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", m.Owner()) |
| 235 | } |
| 236 | fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional") |
| 237 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", m.outputPath.String()) |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 238 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.ToMakePath().String()) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 239 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name) |
| 240 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 241 | }, |
| 242 | } |
| 243 | } |
| 244 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 245 | func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
| 246 | if proptools.Bool(m.properties.Recovery_available) && m.InstallInRecovery() { |
| 247 | ctx.PropertyErrorf("recovery_available", |
| 248 | "doesn't make sense at the same time as `recovery: true`") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 252 | func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 253 | return !m.InstallInRecovery() |
| 254 | } |
| 255 | |
| 256 | func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 257 | return false |
| 258 | } |
| 259 | |
| 260 | func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 261 | return false |
| 262 | } |
| 263 | |
Inseob Kim | 6cc75f4 | 2021-04-29 13:53:20 +0000 | [diff] [blame] | 264 | func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 265 | return false |
| 266 | } |
| 267 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 268 | func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 269 | return m.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available) |
| 270 | } |
| 271 | |
| 272 | func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 273 | return nil |
| 274 | } |
| 275 | |
| 276 | func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 277 | } |
| 278 | |
| 279 | var _ android.ImageInterface = (*selinuxContextsModule)(nil) |
| 280 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 281 | func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
| 282 | ret := android.PathForModuleGen(ctx, ctx.ModuleName()+"_m4out") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 283 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 284 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 285 | |
| 286 | rule.Command(). |
Dan Willemsen | 3c3e59b | 2019-06-19 10:52:50 -0700 | [diff] [blame] | 287 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 288 | Text("--fatal-warnings -s"). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 289 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
| 290 | Inputs(inputs). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 291 | FlagWithOutput("> ", ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 292 | |
| 293 | if proptools.Bool(m.properties.Remove_comment) { |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 294 | rule.Temporary(ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 295 | |
| 296 | remove_comment_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_remove_comment") |
| 297 | |
| 298 | rule.Command(). |
| 299 | Text("sed -e 's/#.*$//' -e '/^$/d'"). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 300 | Input(ret). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 301 | FlagWithOutput("> ", remove_comment_output) |
| 302 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 303 | ret = remove_comment_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if proptools.Bool(m.properties.Fc_sort) { |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 307 | rule.Temporary(ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 308 | |
| 309 | sorted_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_sorted") |
| 310 | |
| 311 | rule.Command(). |
| 312 | Tool(ctx.Config().HostToolPath(ctx, "fc_sort")). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 313 | FlagWithInput("-i ", ret). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 314 | FlagWithOutput("-o ", sorted_output) |
| 315 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 316 | ret = sorted_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 319 | rule.Build("selinux_contexts", "building contexts: "+m.Name()) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 320 | |
| 321 | rule.DeleteTemporaryFiles() |
| 322 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 323 | return ret |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 324 | } |
| 325 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 326 | func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 327 | if m.properties.Fc_sort == nil { |
| 328 | m.properties.Fc_sort = proptools.BoolPtr(true) |
| 329 | } |
| 330 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 331 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 332 | |
| 333 | if ctx.Config().FlattenApex() { |
| 334 | for _, src := range m.fileContextsProperties.Flatten_apex.Srcs { |
| 335 | if m := android.SrcIsModule(src); m != "" { |
| 336 | ctx.ModuleErrorf( |
| 337 | "Module srcs dependency %q is not supported for flatten_apex.srcs", m) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 338 | return nil |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 339 | } |
| 340 | for _, path := range android.PathsForModuleSrcExcludes(ctx, []string{src}, nil) { |
| 341 | out := android.PathForModuleGen(ctx, "flattened_apex", path.Rel()) |
| 342 | apex_path := "/system/apex/" + strings.Replace( |
| 343 | strings.TrimSuffix(path.Base(), "-file_contexts"), |
| 344 | ".", "\\\\.", -1) |
| 345 | |
| 346 | rule.Command(). |
| 347 | Text("awk '/object_r/{printf(\""+apex_path+"%s\\n\",$0)}'"). |
| 348 | Input(path). |
| 349 | FlagWithOutput("> ", out) |
| 350 | |
| 351 | inputs = append(inputs, out) |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 356 | rule.Build(m.Name(), "flattened_apex_file_contexts") |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 357 | return m.buildGeneralContexts(ctx, inputs) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | func fileFactory() android.Module { |
| 361 | m := newModule() |
| 362 | m.AddProperties(&m.fileContextsProperties) |
| 363 | m.build = m.buildFileContexts |
| 364 | return m |
| 365 | } |
| 366 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 367 | func (m *selinuxContextsModule) buildHwServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 368 | if m.properties.Remove_comment == nil { |
| 369 | m.properties.Remove_comment = proptools.BoolPtr(true) |
| 370 | } |
| 371 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 372 | return m.buildGeneralContexts(ctx, inputs) |
| 373 | } |
| 374 | |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 375 | func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, inputs android.Paths) android.Paths { |
| 376 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 377 | ApiLevelR := android.ApiLevelOrPanic(ctx, "R") |
| 378 | |
| 379 | rule := android.NewRuleBuilder(pctx, ctx) |
| 380 | |
| 381 | // This list is from vts_treble_sys_prop_test. |
| 382 | allowedPropertyPrefixes := []string{ |
| 383 | "ctl.odm.", |
| 384 | "ctl.vendor.", |
| 385 | "ctl.start$odm.", |
| 386 | "ctl.start$vendor.", |
| 387 | "ctl.stop$odm.", |
| 388 | "ctl.stop$vendor.", |
| 389 | "init.svc.odm.", |
| 390 | "init.svc.vendor.", |
| 391 | "ro.boot.", |
| 392 | "ro.hardware.", |
| 393 | "ro.odm.", |
| 394 | "ro.vendor.", |
| 395 | "odm.", |
| 396 | "persist.odm.", |
| 397 | "persist.vendor.", |
| 398 | "vendor.", |
| 399 | } |
| 400 | |
| 401 | // persist.camera is also allowed for devices launching with R or eariler |
| 402 | if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) { |
| 403 | allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.") |
| 404 | } |
| 405 | |
| 406 | var allowedContextPrefixes []string |
| 407 | |
| 408 | if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) { |
| 409 | // This list is from vts_treble_sys_prop_test. |
| 410 | allowedContextPrefixes = []string{ |
| 411 | "vendor_", |
| 412 | "odm_", |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | var ret android.Paths |
| 417 | for _, input := range inputs { |
| 418 | cmd := rule.Command(). |
| 419 | BuiltTool("check_prop_prefix"). |
| 420 | FlagWithInput("--property-contexts ", input). |
| 421 | FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$' |
| 422 | FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes) |
| 423 | |
| 424 | if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() { |
| 425 | cmd.Flag("--strict") |
| 426 | } |
| 427 | |
| 428 | out := android.PathForModuleGen(ctx, "namespace_checked").Join(ctx, input.String()) |
| 429 | rule.Command().Text("cp -f").Input(input).Output(out) |
| 430 | ret = append(ret, out) |
| 431 | } |
| 432 | rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName()) |
| 433 | return ret |
| 434 | } |
| 435 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 436 | func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 437 | // vendor/odm properties are enforced for devices launching with Android Q or later. So, if |
| 438 | // vendor/odm, make sure that only vendor/odm properties exist. |
| 439 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 440 | ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q") |
| 441 | if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) { |
| 442 | inputs = m.checkVendorPropertyNamespace(ctx, inputs) |
| 443 | } |
| 444 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 445 | builtCtxFile := m.buildGeneralContexts(ctx, inputs) |
| 446 | |
| 447 | var apiFiles android.Paths |
| 448 | ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) { |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 449 | i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath }) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 450 | if !ok { |
| 451 | panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName())) |
| 452 | } |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 453 | if api := i.CurrentSyspropApiFile(); api.Valid() { |
| 454 | apiFiles = append(apiFiles, api.Path()) |
| 455 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 456 | }) |
| 457 | |
| 458 | // check compatibility with sysprop_library |
| 459 | if len(apiFiles) > 0 { |
| 460 | out := android.PathForModuleGen(ctx, ctx.ModuleName()+"_api_checked") |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 461 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 462 | |
| 463 | msg := `\n******************************\n` + |
| 464 | `API of sysprop_library doesn't match with property_contexts\n` + |
| 465 | `Please fix the breakage and rebuild.\n` + |
| 466 | `******************************\n` |
| 467 | |
| 468 | rule.Command(). |
| 469 | Text("( "). |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 470 | BuiltTool("sysprop_type_checker"). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 471 | FlagForEachInput("--api ", apiFiles). |
| 472 | FlagWithInput("--context ", builtCtxFile). |
| 473 | Text(" || ( echo").Flag("-e"). |
| 474 | Flag(`"` + msg + `"`). |
| 475 | Text("; exit 38) )") |
| 476 | |
| 477 | rule.Command().Text("cp -f").Input(builtCtxFile).Output(out) |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 478 | rule.Build("property_contexts_check_api", "checking API: "+m.Name()) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 479 | builtCtxFile = out |
| 480 | } |
| 481 | |
| 482 | return builtCtxFile |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | func hwServiceFactory() android.Module { |
| 486 | m := newModule() |
| 487 | m.build = m.buildHwServiceContexts |
| 488 | return m |
| 489 | } |
| 490 | |
| 491 | func propertyFactory() android.Module { |
| 492 | m := newModule() |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 493 | m.build = m.buildPropertyContexts |
| 494 | m.deps = m.propertyContextsDeps |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 495 | return m |
| 496 | } |
| 497 | |
| 498 | func serviceFactory() android.Module { |
| 499 | m := newModule() |
| 500 | m.build = m.buildGeneralContexts |
| 501 | return m |
| 502 | } |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 503 | |
| 504 | func keystoreKeyFactory() android.Module { |
| 505 | m := newModule() |
| 506 | m.build = m.buildGeneralContexts |
| 507 | return m |
| 508 | } |