2018-03-22 19:35:02 +01:00
|
|
|
// Copyright (C) 2018 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package selinux
|
|
|
|
|
|
|
|
// This file contains "se_cil_compat_map" module type used to build and install
|
|
|
|
// sepolicy backwards compatibility mapping files.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-09-30 02:47:10 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2019-04-03 01:22:59 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2018-03-22 19:35:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-03 01:22:59 +02:00
|
|
|
combine_maps = pctx.HostBinToolVariable("combine_maps", "combine_maps")
|
|
|
|
combineMapsCmd = "${combine_maps} -t ${topHalf} -b ${bottomHalf} -o $out"
|
2018-09-30 02:47:10 +02:00
|
|
|
combineMapsRule = pctx.StaticRule(
|
|
|
|
"combineMapsRule",
|
|
|
|
blueprint.RuleParams{
|
2019-04-03 01:22:59 +02:00
|
|
|
Command: combineMapsCmd,
|
2018-09-30 02:47:10 +02:00
|
|
|
CommandDeps: []string{"${combine_maps}"},
|
|
|
|
},
|
|
|
|
"topHalf",
|
|
|
|
"bottomHalf",
|
|
|
|
)
|
|
|
|
|
2019-04-03 01:22:59 +02:00
|
|
|
String = proptools.String
|
2018-09-30 02:47:10 +02:00
|
|
|
TopHalfDepTag = dependencyTag{name: "top"}
|
2018-03-22 19:35:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("se_cil_compat_map", cilCompatMapFactory)
|
2019-04-03 01:22:59 +02:00
|
|
|
pctx.Import("android/soong/android")
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cilCompatMapFactory() android.Module {
|
|
|
|
c := &cilCompatMap{}
|
|
|
|
c.AddProperties(&c.properties)
|
2019-10-11 01:29:40 +02:00
|
|
|
android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
|
2018-03-22 19:35:02 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cilCompatMapProperties struct {
|
2018-09-30 02:47:10 +02:00
|
|
|
// se_cil_compat_map module representing a compatibility mapping file for
|
|
|
|
// platform versions (x->y). Bottom half represents a mapping (y->z).
|
|
|
|
// Together the halves are used to generate a (x->z) mapping.
|
|
|
|
Top_half *string
|
|
|
|
// list of source (.cil) files used to build an the bottom half of sepolicy
|
|
|
|
// compatibility mapping file. bottom_half may reference the outputs of
|
|
|
|
// other modules that produce source files like genrule or filegroup using
|
|
|
|
// the syntax ":module". srcs has to be non-empty.
|
2021-07-09 23:53:03 +02:00
|
|
|
Bottom_half []string `android:"path"`
|
2019-10-11 01:29:40 +02:00
|
|
|
// name of the output
|
|
|
|
Stem *string
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type cilCompatMap struct {
|
|
|
|
android.ModuleBase
|
|
|
|
properties cilCompatMapProperties
|
|
|
|
// (.intermediate) module output path as installation source.
|
2018-09-30 02:47:10 +02:00
|
|
|
installSource android.Path
|
2019-10-11 01:29:40 +02:00
|
|
|
installPath android.InstallPath
|
2018-09-30 02:47:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CilCompatMapGenerator interface {
|
|
|
|
GeneratedMapFile() android.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandTopHalf(ctx android.ModuleContext) android.OptionalPath {
|
|
|
|
var topHalf android.OptionalPath
|
|
|
|
ctx.VisitDirectDeps(func(dep android.Module) {
|
|
|
|
depTag := ctx.OtherModuleDependencyTag(dep)
|
|
|
|
switch depTag {
|
|
|
|
case TopHalfDepTag:
|
|
|
|
topHalf = android.OptionalPathForPath(dep.(CilCompatMapGenerator).GeneratedMapFile())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return topHalf
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 05:03:58 +02:00
|
|
|
func expandSeSources(ctx android.ModuleContext, srcFiles []string) android.Paths {
|
|
|
|
expandedSrcFiles := make(android.Paths, 0, len(srcFiles))
|
|
|
|
for _, s := range srcFiles {
|
|
|
|
if m := android.SrcIsModule(s); m != "" {
|
2021-07-09 23:53:03 +02:00
|
|
|
module := android.GetModuleFromPathDep(ctx, m, "")
|
2018-03-26 05:03:58 +02:00
|
|
|
if module == nil {
|
|
|
|
// Error will have been handled by ExtractSourcesDeps
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fg, ok := module.(*fileGroup); ok {
|
2019-10-11 01:29:40 +02:00
|
|
|
if ctx.ProductSpecific() {
|
|
|
|
expandedSrcFiles = append(expandedSrcFiles, fg.ProductPrivateSrcs()...)
|
|
|
|
} else if ctx.SystemExtSpecific() {
|
|
|
|
expandedSrcFiles = append(expandedSrcFiles, fg.SystemExtPrivateSrcs()...)
|
|
|
|
} else {
|
|
|
|
expandedSrcFiles = append(expandedSrcFiles, fg.SystemPrivateSrcs()...)
|
|
|
|
}
|
2018-03-26 05:03:58 +02:00
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("srcs dependency %q is not an selinux filegroup", m)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p := android.PathForModuleSrc(ctx, s)
|
|
|
|
expandedSrcFiles = append(expandedSrcFiles, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expandedSrcFiles
|
|
|
|
}
|
|
|
|
|
2018-03-22 19:35:02 +01:00
|
|
|
func (c *cilCompatMap) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-10-11 01:29:40 +02:00
|
|
|
c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping")
|
|
|
|
|
2018-09-30 02:47:10 +02:00
|
|
|
srcFiles := expandSeSources(ctx, c.properties.Bottom_half)
|
|
|
|
|
2018-03-22 19:35:02 +01:00
|
|
|
for _, src := range srcFiles {
|
|
|
|
if src.Ext() != ".cil" {
|
2018-09-30 02:47:10 +02:00
|
|
|
ctx.PropertyErrorf("bottom_half", "%s has to be a .cil file.", src.String())
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 02:47:10 +02:00
|
|
|
bottomHalf := android.PathForModuleGen(ctx, "bottom_half")
|
2018-03-22 19:35:02 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Cat,
|
2018-09-30 02:47:10 +02:00
|
|
|
Output: bottomHalf,
|
2018-03-22 19:35:02 +01:00
|
|
|
Inputs: srcFiles,
|
|
|
|
})
|
2018-09-30 02:47:10 +02:00
|
|
|
|
|
|
|
topHalf := expandTopHalf(ctx)
|
2019-04-03 01:22:59 +02:00
|
|
|
if topHalf.Valid() {
|
2018-09-30 02:47:10 +02:00
|
|
|
out := android.PathForModuleGen(ctx, c.Name())
|
|
|
|
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
|
2019-04-03 01:22:59 +02:00
|
|
|
Rule: combineMapsRule,
|
2018-09-30 02:47:10 +02:00
|
|
|
Output: out,
|
|
|
|
Implicits: []android.Path{
|
|
|
|
topHalf.Path(),
|
|
|
|
bottomHalf,
|
|
|
|
},
|
|
|
|
Args: map[string]string{
|
2019-04-03 01:22:59 +02:00
|
|
|
"topHalf": topHalf.String(),
|
2018-09-30 02:47:10 +02:00
|
|
|
"bottomHalf": bottomHalf.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
c.installSource = out
|
|
|
|
} else {
|
|
|
|
c.installSource = bottomHalf
|
|
|
|
}
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cilCompatMap) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2019-04-03 01:22:59 +02:00
|
|
|
if c.properties.Top_half != nil {
|
2018-09-30 02:47:10 +02:00
|
|
|
ctx.AddDependency(c, TopHalfDepTag, String(c.properties.Top_half))
|
|
|
|
}
|
2018-03-22 19:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cilCompatMap) AndroidMk() android.AndroidMkData {
|
|
|
|
ret := android.AndroidMkData{
|
2018-09-30 02:47:10 +02:00
|
|
|
OutputFile: android.OptionalPathForPath(c.installSource),
|
2018-03-22 19:35:02 +01:00
|
|
|
Class: "ETC",
|
|
|
|
}
|
|
|
|
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
|
2019-10-11 01:29:40 +02:00
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", c.installPath.ToMakePath().String())
|
|
|
|
if c.properties.Stem != nil {
|
|
|
|
fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", String(c.properties.Stem))
|
|
|
|
}
|
2018-03-22 19:35:02 +01:00
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
2018-09-30 02:47:10 +02:00
|
|
|
|
|
|
|
var _ CilCompatMapGenerator = (*cilCompatMap)(nil)
|
|
|
|
|
|
|
|
func (c *cilCompatMap) GeneratedMapFile() android.Path {
|
|
|
|
return c.installSource
|
|
|
|
}
|