Skip to content

Commit

Permalink
Only maintain maps between current and previous selinux versions.
Browse files Browse the repository at this point in the history
New maintenance scheme for mapping files:
Say, V is the current SELinux platform version, then at any point in time we
only maintain (V->V-1) mapping. (V->V-n) map is constructed from top (V->V-n+1)
and bottom (V-n+1->V-n) without changes to previously maintained mapping files.

Caveats:
- 26.0.cil doesn't technically represent 27.0->26.0 map, but rather
current->26.0. We'll fully migrate to the scheme with future releases.

Bug: 67510052
Test: adding new public type only requires changing the latest compat map
Change-Id: Iab5564e887ef2c8004cb493505dd56c6220c61f8
  • Loading branch information
Tri Vo committed Oct 2, 2018
1 parent aabee5f commit 438684b
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*.*~
33 changes: 24 additions & 9 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,36 @@ se_filegroup {

se_cil_compat_map {
name: "26.0.cil",
srcs: [
":26.0.board.compat.map",
],
bottom_half: [":26.0.board.compat.map"],
top_half: "27.0.cil",
}

se_cil_compat_map {
name: "27.0.cil",
srcs: [
":27.0.board.compat.map",
],
bottom_half: [":27.0.board.compat.map"],
top_half: "28.0.cil",
}

se_cil_compat_map {
name: "28.0.cil",
srcs: [
":28.0.board.compat.map",
],
bottom_half: [":28.0.board.compat.map"],
// top_half: "29.0.cil",
}

se_cil_compat_map {
name: "26.0.ignore.cil",
bottom_half: ["private/compat/26.0/26.0.ignore.cil"],
top_half: "27.0.ignore.cil",
}

se_cil_compat_map {
name: "27.0.ignore.cil",
bottom_half: ["private/compat/27.0/27.0.ignore.cil"],
top_half: "28.0.ignore.cil",
}

se_cil_compat_map {
name: "28.0.ignore.cil",
bottom_half: ["private/compat/28.0/28.0.ignore.cil"],
// top_half: "29.0.ignore.cil",
}
98 changes: 85 additions & 13 deletions build/soong/cil_compat_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@ import (
"android/soong/android"
"fmt"
"io"

"github.com/google/blueprint/proptools"
"github.com/google/blueprint"
)

var (
pctx = android.NewPackageContext("android/soong/selinux")

combine_maps = pctx.HostBinToolVariable("combine_maps", "combine_maps")
combineMapsCmd = "${combine_maps} -t ${topHalf} -b ${bottomHalf} -o $out"
combineMapsRule = pctx.StaticRule(
"combineMapsRule",
blueprint.RuleParams{
Command: combineMapsCmd,
CommandDeps: []string{"${combine_maps}"},
},
"topHalf",
"bottomHalf",
)

String = proptools.String
TopHalfDepTag = dependencyTag{name: "top"}
)

func init() {
Expand All @@ -40,18 +58,43 @@ func cilCompatMapFactory() android.Module {
}

type cilCompatMapProperties struct {
// list of source (.cil) files used to build an sepolicy compatibility mapping
// file. srcs 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.
Srcs []string
// 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.
Bottom_half []string
}

type cilCompatMap struct {
android.ModuleBase
properties cilCompatMapProperties
// (.intermediate) module output path as installation source.
installSource android.OptionalPath
installSource android.Path
}

type CilCompatMapGenerator interface {
GeneratedMapFile() android.Path
}

type dependencyTag struct {
blueprint.BaseDependencyTag
name string
}

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
}

func expandSeSources(ctx android.ModuleContext, srcFiles []string) android.Paths {
Expand Down Expand Up @@ -81,33 +124,62 @@ func expandSeSources(ctx android.ModuleContext, srcFiles []string) android.Paths
}

func (c *cilCompatMap) GenerateAndroidBuildActions(ctx android.ModuleContext) {
srcFiles := expandSeSources(ctx, c.properties.Srcs)
srcFiles := expandSeSources(ctx, c.properties.Bottom_half)

for _, src := range srcFiles {
if src.Ext() != ".cil" {
ctx.PropertyErrorf("srcs", "%s has to be a .cil file.", src.String())
ctx.PropertyErrorf("bottom_half", "%s has to be a .cil file.", src.String())
}
}

out := android.PathForModuleGen(ctx, c.Name())
bottomHalf := android.PathForModuleGen(ctx, "bottom_half")
ctx.Build(pctx, android.BuildParams{
Rule: android.Cat,
Output: out,
Output: bottomHalf,
Inputs: srcFiles,
})
c.installSource = android.OptionalPathForPath(out)

topHalf := expandTopHalf(ctx)
if (topHalf.Valid()) {
out := android.PathForModuleGen(ctx, c.Name())
ctx.ModuleBuild(pctx, android.ModuleBuildParams{
Rule: combineMapsRule,
Output: out,
Implicits: []android.Path{
topHalf.Path(),
bottomHalf,
},
Args: map[string]string{
"topHalf": topHalf.String(),
"bottomHalf": bottomHalf.String(),
},
})
c.installSource = out
} else {
c.installSource = bottomHalf
}
}

func (c *cilCompatMap) DepsMutator(ctx android.BottomUpMutatorContext) {
android.ExtractSourcesDeps(ctx, c.properties.Srcs)
android.ExtractSourcesDeps(ctx, c.properties.Bottom_half)
if (c.properties.Top_half != nil) {
ctx.AddDependency(c, TopHalfDepTag, String(c.properties.Top_half))
}
}

func (c *cilCompatMap) AndroidMk() android.AndroidMkData {
ret := android.AndroidMkData{
OutputFile: c.installSource,
OutputFile: android.OptionalPathForPath(c.installSource),
Class: "ETC",
}
ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/selinux/mapping")
})
return ret
}

var _ CilCompatMapGenerator = (*cilCompatMap)(nil)

func (c *cilCompatMap) GeneratedMapFile() android.Path {
return c.installSource
}
11 changes: 7 additions & 4 deletions private/compat/26.0/26.0.ignore.cil
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
;; new_objects - a collection of types that have been introduced that have no
;; analogue in older policy. Thus, we do not need to map these types to
;; previous ones. Add here to pass checkapi tests.
(type new_objects)
(typeattribute new_objects)
(typeattributeset new_objects
( activity_task_service
( new_objects
activity_task_service
adb_service
adbd_exec
app_binding_service
Expand Down Expand Up @@ -182,8 +184,9 @@
;; private_objects - a collection of types that were labeled differently in
;; older policy, but that should not remain accessible to vendor policy.
;; Thus, these types are also not mapped, but recorded for checkapi tests
(type priv_objects)
(typeattribute priv_objects)
(typeattributeset priv_objects
( adbd_tmpfs
untrusted_app_27_tmpfs
))
( priv_objects
adbd_tmpfs
untrusted_app_27_tmpfs))
9 changes: 7 additions & 2 deletions private/compat/27.0/27.0.ignore.cil
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
;; new_objects - a collection of types that have been introduced that have no
;; analogue in older policy. Thus, we do not need to map these types to
;; previous ones. Add here to pass checkapi tests.
(type new_objects)
(typeattribute new_objects)
(typeattributeset new_objects
( activity_task_service
( new_objects
activity_task_service
adb_service
app_binding_service
atrace
Expand Down Expand Up @@ -160,5 +162,8 @@
;; private_objects - a collection of types that were labeled differently in
;; older policy, but that should not remain accessible to vendor policy.
;; Thus, these types are also not mapped, but recorded for checkapi tests
(type priv_objects)
(typeattribute priv_objects)
(typeattributeset priv_objects (untrusted_app_27_tmpfs))
(typeattributeset priv_objects
( priv_objects
untrusted_app_27_tmpfs))
4 changes: 3 additions & 1 deletion private/compat/28.0/28.0.ignore.cil
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
;; new_objects - a collection of types that have been introduced that have no
;; analogue in older policy. Thus, we do not need to map these types to
;; previous ones. Add here to pass checkapi tests.
(type new_objects)
(typeattribute new_objects)
(typeattributeset new_objects
( activity_task_service
( new_objects
activity_task_service
adb_service
app_binding_service
biometric_service
Expand Down
8 changes: 8 additions & 0 deletions tests/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ python_binary_host {
required: ["libsepolwrap"],
defaults: ["py2_only"],
}

python_binary_host {
name: "combine_maps",
srcs: [
"combine_maps.py",
"mini_parser.py",
],
}
66 changes: 66 additions & 0 deletions tests/combine_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 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.

"""Tool to combine SEPolicy mapping file.
Say, x, y, z are platform SEPolicy versions such that x > y > z. Then given two
mapping files from x to y (top) and y to z (bottom), it's possible to construct
a mapping file from x to z. We do the following to combine two maps.
1. Add all new types declarations from top to bottom.
2. Say, a new type "bar" in top is mapped like this "foo_V_v<-bar", then we map
"bar" to whatever "foo" is mapped to in the bottom map. We do this for all new
types in the top map.
More generally, we can correctly construct x->z from x->y' and y"->z as long as
y">y'.
This file contains the implementation of combining two mapping files.
"""
import argparse
import re
from mini_parser import MiniCilParser

def Combine(top, bottom):
bottom.types.update(top.types)

for top_ta in top.typeattributesets:
top_type_set = top.typeattributesets[top_ta]
if len(top_type_set) == 1:
continue

m = re.match(r"(\w+)_\d+_\d+", top_ta)
# Typeattributes in V.v.cil have _V_v suffix, but not in V.v.ignore.cil
bottom_type = m.group(1) if m else top_ta

for bottom_ta in bottom.rTypeattributesets[bottom_type]:
bottom.typeattributesets[bottom_ta].update(top_type_set)

return bottom

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--top-map", dest="top_map",
required=True, help="top map file")
parser.add_argument("-b", "--bottom-map", dest="bottom_map",
required=True, help="bottom map file")
parser.add_argument("-o", "--output-file", dest="output_file",
required=True, help="output map file")
args = parser.parse_args()

top_map_cil = MiniCilParser(args.top_map)
bottom_map_cil = MiniCilParser(args.bottom_map)
result = Combine(top_map_cil, bottom_map_cil)

with open(args.output_file, "w") as output:
output.write(result.unparse())
Loading

0 comments on commit 438684b

Please sign in to comment.