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 | |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | coreMode = "core" |
| 29 | recoveryMode = "recovery" |
| 30 | ) |
| 31 | |
| 32 | type selinuxContextsProperties struct { |
| 33 | // Filenames under sepolicy directories, which will be used to generate contexts file. |
| 34 | Srcs []string `android:"path"` |
| 35 | |
| 36 | Product_variables struct { |
| 37 | Debuggable struct { |
| 38 | Srcs []string |
| 39 | } |
| 40 | |
| 41 | Address_sanitize struct { |
| 42 | Srcs []string |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Whether reqd_mask directory is included to sepolicy directories or not. |
| 47 | Reqd_mask *bool |
| 48 | |
| 49 | // Whether the comments in generated contexts file will be removed or not. |
| 50 | Remove_comment *bool |
| 51 | |
| 52 | // Whether the result context file is sorted with fc_sort or not. |
| 53 | Fc_sort *bool |
| 54 | |
| 55 | // Make this module available when building for recovery |
| 56 | Recovery_available *bool |
| 57 | |
| 58 | InRecovery bool `blueprint:"mutated"` |
| 59 | } |
| 60 | |
| 61 | type fileContextsProperties struct { |
| 62 | // flatten_apex can be used to specify additional sources of file_contexts. |
| 63 | // Apex paths, /system/apex/{apex_name}, will be amended to the paths of file_contexts |
| 64 | // entries. |
| 65 | Flatten_apex struct { |
| 66 | Srcs []string |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | type selinuxContextsModule struct { |
| 71 | android.ModuleBase |
| 72 | |
| 73 | properties selinuxContextsProperties |
| 74 | fileContextsProperties fileContextsProperties |
| 75 | build func(ctx android.ModuleContext, inputs android.Paths) |
| 76 | outputPath android.ModuleGenPath |
| 77 | installPath android.OutputPath |
| 78 | } |
| 79 | |
| 80 | var ( |
| 81 | reuseContextsDepTag = dependencyTag{name: "reuseContexts"} |
| 82 | ) |
| 83 | |
| 84 | func init() { |
| 85 | pctx.HostBinToolVariable("fc_sort", "fc_sort") |
| 86 | |
| 87 | android.RegisterModuleType("file_contexts", fileFactory) |
| 88 | android.RegisterModuleType("hwservice_contexts", hwServiceFactory) |
| 89 | android.RegisterModuleType("property_contexts", propertyFactory) |
| 90 | android.RegisterModuleType("service_contexts", serviceFactory) |
| 91 | |
| 92 | android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 93 | ctx.BottomUp("selinux_contexts", selinuxContextsMutator).Parallel() |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | func (m *selinuxContextsModule) inRecovery() bool { |
| 98 | return m.properties.InRecovery || m.ModuleBase.InstallInRecovery() |
| 99 | } |
| 100 | |
| 101 | func (m *selinuxContextsModule) onlyInRecovery() bool { |
| 102 | return m.ModuleBase.InstallInRecovery() |
| 103 | } |
| 104 | |
| 105 | func (m *selinuxContextsModule) InstallInRecovery() bool { |
| 106 | return m.inRecovery() |
| 107 | } |
| 108 | |
| 109 | func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 110 | if m.InstallInRecovery() { |
| 111 | // Workaround for installing context files at the root of the recovery partition |
| 112 | m.installPath = android.PathForOutput(ctx, |
| 113 | "target", "product", ctx.Config().DeviceName(), "recovery", "root") |
| 114 | } else { |
| 115 | m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 116 | } |
| 117 | |
| 118 | if m.inRecovery() && !m.onlyInRecovery() { |
| 119 | dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag) |
| 120 | |
| 121 | if reuseDeps, ok := dep.(*selinuxContextsModule); ok { |
| 122 | m.outputPath = reuseDeps.outputPath |
| 123 | ctx.InstallFile(m.installPath, m.Name(), m.outputPath) |
| 124 | return |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | var inputs android.Paths |
| 129 | |
| 130 | ctx.VisitDirectDepsWithTag(android.SourceDepTag, func(dep android.Module) { |
| 131 | segroup, ok := dep.(*fileGroup) |
| 132 | if !ok { |
| 133 | ctx.ModuleErrorf("srcs dependency %q is not an selinux filegroup", |
| 134 | ctx.OtherModuleName(dep)) |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | if ctx.ProductSpecific() { |
| 139 | inputs = append(inputs, segroup.ProductPrivateSrcs()...) |
| 140 | } else if ctx.SocSpecific() { |
| 141 | inputs = append(inputs, segroup.SystemVendorSrcs()...) |
| 142 | inputs = append(inputs, segroup.VendorSrcs()...) |
| 143 | } else if ctx.DeviceSpecific() { |
| 144 | inputs = append(inputs, segroup.OdmSrcs()...) |
| 145 | } else { |
| 146 | inputs = append(inputs, segroup.SystemPrivateSrcs()...) |
| 147 | inputs = append(inputs, segroup.SystemExtPrivateSrcs()...) |
| 148 | |
| 149 | if ctx.Config().ProductCompatibleProperty() { |
| 150 | inputs = append(inputs, segroup.SystemPublicSrcs()...) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if proptools.Bool(m.properties.Reqd_mask) { |
| 155 | inputs = append(inputs, segroup.SystemReqdMaskSrcs()...) |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | for _, src := range m.properties.Srcs { |
| 160 | // Module sources are handled above with VisitDirectDepsWithTag |
| 161 | if android.SrcIsModule(src) == "" { |
| 162 | inputs = append(inputs, android.PathForModuleSrc(ctx, src)) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | m.build(ctx, inputs) |
| 167 | } |
| 168 | |
| 169 | func newModule() *selinuxContextsModule { |
| 170 | m := &selinuxContextsModule{} |
| 171 | m.AddProperties( |
| 172 | &m.properties, |
| 173 | ) |
| 174 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 175 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 176 | m.selinuxContextsHook(ctx) |
| 177 | }) |
| 178 | return m |
| 179 | } |
| 180 | |
| 181 | func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) { |
| 182 | // TODO: clean this up to use build/soong/android/variable.go after b/79249983 |
| 183 | var srcs []string |
| 184 | |
| 185 | if ctx.Config().Debuggable() { |
| 186 | srcs = append(srcs, m.properties.Product_variables.Debuggable.Srcs...) |
| 187 | } |
| 188 | |
| 189 | for _, sanitize := range ctx.Config().SanitizeDevice() { |
| 190 | if sanitize == "address" { |
| 191 | srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...) |
| 192 | break |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | m.properties.Srcs = append(m.properties.Srcs, srcs...) |
| 197 | } |
| 198 | |
| 199 | func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData { |
| 200 | return android.AndroidMkData{ |
| 201 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 202 | nameSuffix := "" |
| 203 | if m.inRecovery() && !m.onlyInRecovery() { |
| 204 | nameSuffix = ".recovery" |
| 205 | } |
| 206 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
| 207 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 208 | fmt.Fprintln(w, "LOCAL_MODULE :=", name+nameSuffix) |
| 209 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS := ETC") |
| 210 | if m.Owner() != "" { |
| 211 | fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", m.Owner()) |
| 212 | } |
| 213 | fmt.Fprintln(w, "LOCAL_MODULE_TAGS := optional") |
| 214 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", m.outputPath.String()) |
| 215 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(OUT_DIR)/"+m.installPath.RelPathString()) |
| 216 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", name) |
| 217 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 218 | }, |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func selinuxContextsMutator(ctx android.BottomUpMutatorContext) { |
| 223 | m, ok := ctx.Module().(*selinuxContextsModule) |
| 224 | if !ok { |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | var coreVariantNeeded bool = true |
| 229 | var recoveryVariantNeeded bool = false |
| 230 | if proptools.Bool(m.properties.Recovery_available) { |
| 231 | recoveryVariantNeeded = true |
| 232 | } |
| 233 | |
| 234 | if m.ModuleBase.InstallInRecovery() { |
| 235 | recoveryVariantNeeded = true |
| 236 | coreVariantNeeded = false |
| 237 | } |
| 238 | |
| 239 | var variants []string |
| 240 | if coreVariantNeeded { |
| 241 | variants = append(variants, coreMode) |
| 242 | } |
| 243 | if recoveryVariantNeeded { |
| 244 | variants = append(variants, recoveryMode) |
| 245 | } |
| 246 | mod := ctx.CreateVariations(variants...) |
| 247 | |
| 248 | for i, v := range variants { |
| 249 | if v == recoveryMode { |
| 250 | m := mod[i].(*selinuxContextsModule) |
| 251 | m.properties.InRecovery = true |
| 252 | |
| 253 | if coreVariantNeeded { |
| 254 | ctx.AddInterVariantDependency(reuseContextsDepTag, m, mod[i-1]) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) { |
| 261 | m.outputPath = android.PathForModuleGen(ctx, ctx.ModuleName()+"_m4out") |
| 262 | |
| 263 | rule := android.NewRuleBuilder() |
| 264 | |
| 265 | rule.Command(). |
| 266 | Text("m4 --fatal-warnings -s"). |
| 267 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
| 268 | Inputs(inputs). |
| 269 | FlagWithOutput("> ", m.outputPath) |
| 270 | |
| 271 | if proptools.Bool(m.properties.Remove_comment) { |
| 272 | rule.Temporary(m.outputPath) |
| 273 | |
| 274 | remove_comment_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_remove_comment") |
| 275 | |
| 276 | rule.Command(). |
| 277 | Text("sed -e 's/#.*$//' -e '/^$/d'"). |
| 278 | Input(m.outputPath). |
| 279 | FlagWithOutput("> ", remove_comment_output) |
| 280 | |
| 281 | m.outputPath = remove_comment_output |
| 282 | } |
| 283 | |
| 284 | if proptools.Bool(m.properties.Fc_sort) { |
| 285 | rule.Temporary(m.outputPath) |
| 286 | |
| 287 | sorted_output := android.PathForModuleGen(ctx, ctx.ModuleName()+"_sorted") |
| 288 | |
| 289 | rule.Command(). |
| 290 | Tool(ctx.Config().HostToolPath(ctx, "fc_sort")). |
| 291 | FlagWithInput("-i ", m.outputPath). |
| 292 | FlagWithOutput("-o ", sorted_output) |
| 293 | |
| 294 | m.outputPath = sorted_output |
| 295 | } |
| 296 | |
| 297 | rule.Build(pctx, ctx, "selinux_contexts", m.Name()) |
| 298 | |
| 299 | rule.DeleteTemporaryFiles() |
| 300 | |
| 301 | ctx.InstallFile(m.installPath, ctx.ModuleName(), m.outputPath) |
| 302 | } |
| 303 | |
| 304 | func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) { |
| 305 | if m.properties.Fc_sort == nil { |
| 306 | m.properties.Fc_sort = proptools.BoolPtr(true) |
| 307 | } |
| 308 | |
| 309 | rule := android.NewRuleBuilder() |
| 310 | |
| 311 | if ctx.Config().FlattenApex() { |
| 312 | for _, src := range m.fileContextsProperties.Flatten_apex.Srcs { |
| 313 | if m := android.SrcIsModule(src); m != "" { |
| 314 | ctx.ModuleErrorf( |
| 315 | "Module srcs dependency %q is not supported for flatten_apex.srcs", m) |
| 316 | return |
| 317 | } |
| 318 | for _, path := range android.PathsForModuleSrcExcludes(ctx, []string{src}, nil) { |
| 319 | out := android.PathForModuleGen(ctx, "flattened_apex", path.Rel()) |
| 320 | apex_path := "/system/apex/" + strings.Replace( |
| 321 | strings.TrimSuffix(path.Base(), "-file_contexts"), |
| 322 | ".", "\\\\.", -1) |
| 323 | |
| 324 | rule.Command(). |
| 325 | Text("awk '/object_r/{printf(\""+apex_path+"%s\\n\",$0)}'"). |
| 326 | Input(path). |
| 327 | FlagWithOutput("> ", out) |
| 328 | |
| 329 | inputs = append(inputs, out) |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | rule.Build(pctx, ctx, m.Name(), "flattened_apex_file_contexts") |
| 335 | m.buildGeneralContexts(ctx, inputs) |
| 336 | } |
| 337 | |
| 338 | func fileFactory() android.Module { |
| 339 | m := newModule() |
| 340 | m.AddProperties(&m.fileContextsProperties) |
| 341 | m.build = m.buildFileContexts |
| 342 | return m |
| 343 | } |
| 344 | |
| 345 | func (m *selinuxContextsModule) buildHwServiceContexts(ctx android.ModuleContext, inputs android.Paths) { |
| 346 | if m.properties.Remove_comment == nil { |
| 347 | m.properties.Remove_comment = proptools.BoolPtr(true) |
| 348 | } |
| 349 | |
| 350 | m.buildGeneralContexts(ctx, inputs) |
| 351 | } |
| 352 | |
| 353 | func hwServiceFactory() android.Module { |
| 354 | m := newModule() |
| 355 | m.build = m.buildHwServiceContexts |
| 356 | return m |
| 357 | } |
| 358 | |
| 359 | func propertyFactory() android.Module { |
| 360 | m := newModule() |
| 361 | m.build = m.buildGeneralContexts |
| 362 | return m |
| 363 | } |
| 364 | |
| 365 | func serviceFactory() android.Module { |
| 366 | m := newModule() |
| 367 | m.build = m.buildGeneralContexts |
| 368 | return m |
| 369 | } |