Policy File Configuration Details

This section details the specific SE for Android policy configuration files (i.e. those not used by 'standard' Linux based SELinux) as they were in April '13. Where those files are used to compute contexts using the SE for Android libselinux functions, those functions are also described with examples.

As this project is continually being enhanced, it is recommended that the official project wiki is checked for the latest enhancements at SEforAndroid

SELinux MAC Files

seapp_contexts File

This file is loaded and sorted into memory automatically on first use of one of the following SE for Android libselinux functions that are called by the SE for Android enabled services:

Computing a Directory Context

To compute an app package directory context the selinux_android_setfilecon2 function is called that will use the parameters passed, plus the contents of the seapp_contexts file. The function parameters are:

property_contexts File

This file holds property names and their contexts that will be applied by SELinux when applications are loaded. The property names reflect the 'white list' of Android property entries that are also built into the system (see system/core/init/property_service.c and init.c) however there are also additional property entries for applications that require specific contexts to be set.

The build process supports additional seapp_contexts files to allow devices to specify their specific entries as described in the Building the Policy section.

When selabel_open(3) is called specifying this file it will be read into memory and sorted using qsort(3), subsequent calls using selabel_lookup(3) will then retrieve the appropriate context.

Each line within the property contexts file is as follows:

property_key context

Where:

property_key

The key used to obtain the context that may contain '*' for wildcard matching.

Install MMAC File

The mac_permissions.xml file is used to configure install-time MMAC policy and provides two main functions:

x.509 certificate to seinfo string mapping so that Zygote spawns the application in the correct domain. See the Computing a Process Context section for how this is achieved using information also contained in the seapp_contexts file.

Install-time MMAC permission checking.

It is important to note that all third party apps will be subject to the <default> install MAC policy defined in this file. This is a requirement of AOSP in that all third party apps must be treated alike (i.e. distinctions can only be made between system apps and third party apps and among the system apps, not between two different third party apps). The current <default> package policy is as follows:

The build process supports additional mac_permissions.xml files to allow devices to specify their specific entries as described in the Building the Policy section.

Signature Entries

The <signer signature= entry may have the public base16 signing key present in the string or it may have an entry starting with @, then a keyword as shown that allows the key to be extracted from a pem file as discussed in the insertkeys.py section:

If a base16 key is required, it can be extracted from a package using the setool utility as described in the setool section.

Policy Rules

The following rules have been extracted from the source mac_permissions.xml file:

A signature is a hex encoded X.509 certificate and is required for each signer tag.

A <signer signature="" > element may have multiple child elements:

allow-permission : produces a set of maximal allowed permissions (whitelist).

deny-permission : produces a blacklist of permissions to deny.

allow-all : a wildcard tag that will allow every permission requested.

package : a complex tag which itself defines allow, deny, and wildcard sub elements for a specific package name protected by the signature.

Zero or more global <package name=""> tags are allowed. These tags allow a policy to be set outside any signature for specific package names.

Unknown tags at any level are skipped.

Zero or more signer tags are allowed.

Zero or more package tags are allowed per signer tag.

A <package name=""> tag may not contain another <package name=""> tag. If found, it's skipped.

A <default> tag is allowed that can contain install policy for all apps not signed with a previously listed cert and not having a per package global policy.

When multiple sub elements appear for a tag the following logic is used to ultimately determine the type of enforcement:

A blacklist is used if at least one deny-permission tag is found

A whitelist is used if not a blacklist and at least one allow-permission tag is found

A wildcard (accept all permission) policy is used if not a blacklist and not a whitelist and at least one allow-all tag is present.

If a <package name=""> sub element is found then that sub element's policy is used according to the above logic and overrides any signature global policy type.

In order for a policy stanza to be enforced at least one of the above situations must apply. Meaning, empty signer, default or package tags will not be accepted.

Each signer / default / global package tag is allowed to contain one <seinfo value=""/> tag. This tag represents additional information that each application can use in setting a SELinux security context on the eventual process. Any <seinfo value=""/> tag found as a child of a <package name=""> tag which is protected (sub element of signer or the default tag) is ignored. It's possible that multiple seinfo tags are relevant for one application. In the event that this happens, the seinfo tag that will be applied is the one for which the corresponding policy stanza is used in the policy decision.

Strict enforcing of any xml stanza is not enforced in most cases. This mainly applies to duplicate tags which are allowed. In the event that a tag already exists, the original tag is replaced.

There are also no checks on the validity of permission names. Although valid android permissions are expected, nothing prevents unknowns.

Enforcement decisions:

All signatures used to sign an application are checked for policy according to signer tags. Only one of the signature policies has to pass however.

In the event that none of the signature policies pass, or none even match, then a global package policy is sought. If found, this policy mediates the install.

Intent MAC Files

There are two configuration files to support intent MAC, they are:

intent_mac.xml - This defines the intents that will be allowed between each source and destination. The source and destination may be package names or <type> entries defined in the mmac_types.xml file that will allow intents to be managed on a package, signature and/or permissions basis. There is also an optional <allow-all> section.

Allow-all Entry

The entries are defined below with:

The src and dst entries would normally be defined as package names to allow all intents to flow, however they may be 'type names' that are further defined in the mmac_types.xml file. The notes below describe how either of these methods can be implemented.

The srcctx entry if defined will be matched against the app process source context.

mmac_types.xml File

The build process supports additional mmac_types.xml files to allow devices to specify their specific entries as described in the Building the Policy section that will also expand any signatures.

The entries are defined below with:

The <type name="" /> entry defines the 'type name' entry that will be referenced by the intent_mac.xml file src="" and dst="" entries. The entry will define signatures, package names and or permissions.

The <signature value="" /> entries may be the key as extracted by setool (but it will need to be copied from the output produced by setool), or use the "@.." show in the example below, this will then be generated as a part of the build process by insertkeys.py.

The srcctx= entry if defined will be matched against the app process source context.

Policy Build Tools

This section covers the policy build tools located at external/sepolicy/tools. They are checkfc, checkseapp and insertkeys.py. There is also setool that is not used as part of the build process but generates mac_permissions.xml entries from packages.

checkfc

The checkfc utility is used during the build process to validate the file_contexts and property_contexts files against policy. If validation fails checkfc will exit with an error.

checkseapp

The checkseapp utility is used during the build process to validate the seapp_contexts file against policy. If validation fails checkseapp will exit with an error. checkseapp also consolidates matching entries and outputs the valid file stripped of comments.

insertkeys.py

The insertkeys.py utility is used during the build process to insert signing keys into multiple mac_permissions.xml and mmac_types.xml files. The keys are obtained from pem files and the entries to be replaced start with an @ followed by a keyword. The external/sepolicy/keys.conf file contains corresponding entries that allow mapping of pem files to signatures as discussed in the keys.conf section.

Note that pem files are base64 encoded however the Android Package Manager Service uses base16 encodings. Therefore insertkeys.py and setool will generate base16 encodings for the mac_permissions.xml and mmac_types.xml files.

insertkeys.py will also strip the files of comments and whitespace to preserve space on the system.img. To view the output file in a more human friendly format the tidy or xmllint command can be used.

insertkeys.py -h will give the following output:

Usage: insertkeys.py [options] CONFIG_FILE MAC_PERMISSIONS_FILE [MAC_PERMISSIONS_FILE...]
This tool allows one to configure an automatic inclusion of signing keys into the mac_permission.xml file(s) from the pem files.
If multiple mac_permission.xml files are included then they are unioned to produce a final version.
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Print internal operations to stdout
-o FILE, --output=FILE Specify an output file, default is stdout
-c DIR, --cwd=DIR Specify a root (CWD) directory to run this from, itchdirs' AFTER loading the config file
-t TARGET_BUILD_VARIANT, --target-build-variant=TARGET_BUILD_VARIANT
Specify the TARGET_BUILD_VARIANT, defaults to eng

An example command that takes two mac_permission.xml files inserts the signatures and concatenates them into a single file is:

keys.conf

The keys.conf file is used by insertkeys.py for mapping the "@..." tags in mac_permissions.xml and mmac_types.xml signature entries with public keys found in pem files. The configuration file can be used in BOARD_SEPOLICY_UNION and BOARD_SEPOLICY_REPLACE variables and is processed via m4 macros.

insertkeys.py allows for mapping any string contained in TARGET_BUILD_VARIANT with a specific path to a pem file. Typically TARGET_BUILD_VARIANT is either user, eng or userdebug. Additionally "ALL" may be specified to map a path to any string specified in TARGET_BUILD_VARIANT. All tags are matched verbatim and all options are matched lowercase. The options are "tolowered" automatically for the user, it is convention to specify tags and options in all uppercase and tags start with @.

An example keys.conf file is as follows:

#
# Maps an arbitrary tag [TAGNAME] with the string contents found in
# TARGET_BUILD_VARAINT. Common convention is to start TAGNAME with an @ and
# name it after the base file name of the pem file.
#
# Each tag (section) then allows one to specify any string found in
# TARGET_BUILD_VARIANT. Typically this is user, eng, and userdebug. Another
# option is to use ALL which will match ANY TARGET_BUILD_VARAINT string.
#
[@PLATFORM]
ALL : build/target/product/security/platform.x509.pem
[@MEDIA]
ALL : build/target/product/security/media.x509.pem
[@SHARED]
ALL : build/target/product/security/shared.x509.pem
# Example of ALL TARGET_BUILD_VARIANTS
[@RELEASE]
ENG : build/target/product/security/testkey.x509.pem
USER : build/target/product/security/testkey.x509.pem
USERDEBUG : build/target/product/security/testkey.x509.pem

setool

The setool utility is not used during the build process and is intended only to produce 'starter' entries for the mac_permissions.xml file.

The entries produced will generally have to be modified because currently:

If multiple packages are given each with the same signature, a separate entry will be generated for each package. If added to the mac_permissions.xml file only the first entry will be used by the Install-time MMAC process (therefore the packages need to be consolidated into on entry).

setool will generate (--build) entries for third party apps, however it should not as they will be ignored by the install-time MMAC.

setool will check (--policy) third party apps against the full mac_permissions.xml policy, however it should not, it should only check them against the <default> entries only.

Having said that, setool is still useful as it will generate the signature entries and extract the permissions.

setool --help will give the following output:

Usage: setool [flags] <--build|--policy> <apks>
Tool to help build and verify MMAC install policies.
apks List of apks to analyze, space separated. All supplied apks must
be absolute paths or relative to --apkdir (which defaults to the current directory).
--build Generate an MMAC style policy stanza. The resulting stanza can then be used as an entry in the mac_permissions.xml file.
whitelist
Policy entry that contains a white listing of all permissions. The stanza will contain the app's package tag within its
signer tag.
keys
Print a valid signer tag which contains the hex encoded X.509 cert of the app.
--policy Determine if supplied apks pass the supplied policy.
Flags:
--help Prints this message and exits.
--apkdir Directory to search for supplied apks (default to current directory).
--verbose Increase the amount of debug statements.
--outfile Dump all output to the given file (defaults to stdout).
--seinfo Create an seinfo tag for all generated policy stanzas.

The following are examples of using setool, however, note that they will not produce a valid policy for third party apps.

Assuming in Android project directory ($PREFIX) and there is a "demo" directory present, run setool to extract the package info and generate white list entries from a package (any package will suffice for the example):