No description
Find a file
Eric Biggers 9a5992336e Restrict creating per-user encrypted directories
Creating a per-user encrypted directory such as /data/system_ce/0 and
the subdirectories in it too early has been a recurring bug.  Typically,
individual services in system_server are to blame; system_server has
permission to create these directories, and it's easy to write
"mkdirs()" instead of "mkdir()".  Such bugs are very bad, as they
prevent these directories from being encrypted, as encryption policies
can only be set on empty directories.  Due to recent changes, a factory
reset is now forced in such cases, which helps detect these bugs;
however, it would be much better to prevent them in the first place.

This CL locks down the ability to create these directories to just vold
and init, or to just vold when possible.  This is done by assigning new
types to the directories that contain these directories, and then only
allowing the needed domains to write to these parent directories.  This
is similar to what https://r.android.com/1117297 did for /data itself.

Three new types are used instead of just one, since these directories
had three different types already (system_data_file, media_rw_data_file,
vendor_data_file), and this allows the policy to be a bit more precise.

A significant limitation is that /data/user/0 is currently being created
by init during early boot.  Therefore, this CL doesn't help much for
/data/user/0, though it helps a lot for the other directories.  As the
next step, I'll try to eliminate the /data/user/0 quirk.  Anyway, this
CL is needed regardless of whether we're able to do that.

Test: Booted cuttlefish.  Ran 'sm partition disk:253,32 private', then
      created and deleted a user.  Used 'ls -lZ' to check the relevant
      SELinux labels on both internal and adoptable storage.  Also did
      similar tests on raven, with the addition of going through the
      setup wizard and using an app that creates media files.  No
      relevant SELinux denials seen during any of this.
Bug: 156305599
Change-Id: I1fbdd180f56dd2fe4703763936f5850cef8ab0ba
2022-05-05 04:12:46 +00:00
apex Add file contexts for OnDevicePersonalization. 2022-03-11 08:31:41 +00:00
build Replace se_filegroup to se_build_files 2022-04-23 01:47:40 +09:00
com.android.sepolicy Support for APEX updatable sepolicy 2022-01-11 12:52:09 +01:00
compat Add 33.0 mapping files 2022-05-02 14:12:28 +09:00
contexts Build vndservice_contexts with Android.bp 2022-03-02 17:26:44 +09:00
microdroid Allow microdroid_manager to set dev.bootcomplete 2022-05-02 10:33:49 -07:00
prebuilts/api platform/system/sepolicy - SEPolicy Prebuilts for Tiramisu 2022-05-02 13:24:45 +09:00
private Restrict creating per-user encrypted directories 2022-05-05 04:12:46 +00:00
public Restrict creating per-user encrypted directories 2022-05-05 04:12:46 +00:00
reqd_mask property_context: split into platform and non-platform components. 2017-01-29 21:09:11 +00:00
tests Allow policy tests to support space in file names 2022-02-02 15:12:43 +01:00
tools Merge "Revert^2 "Migrate contexts tests to Android.bp"" 2022-02-16 04:23:47 +00:00
vendor Add sensor multi-HAL AIDL sepolicy 2022-04-22 01:49:26 +00:00
.gitignore Only maintain maps between current and previous selinux versions. 2018-10-02 15:10:13 -07:00
Android.bp Add 33.0 mapping files 2022-05-02 14:12:28 +09:00
Android.mk Build vndservice_contexts with Android.bp 2022-03-02 17:26:44 +09:00
CleanSpec.mk Renames nonplat_* to vendor_* 2018-02-05 18:21:55 +08:00
definitions.mk Revert "Revert "Add a neverallow for debugfs mounting"" 2021-05-02 21:41:53 -07:00
mac_permissions.mk Move sdk_sandbox sepolicy to AOSP. 2022-03-17 10:22:33 +01:00
METADATA Move comment to license_note 2022-03-03 14:58:45 -08:00
MODULE_LICENSE_PUBLIC_DOMAIN Update MODULE_LICENSE 2015-05-22 10:31:21 -07:00
NOTICE Public domain notice 2012-06-19 07:29:55 -04:00
OWNERS Remove self from OWNERS 2022-01-06 10:01:26 -08:00
policy_version.mk Android.bp: set sepolicy version for use by init 2019-03-14 17:49:14 +00:00
PREUPLOAD.cfg Enable gofmt in preupload hooks 2019-04-03 11:38:17 -07:00
README.md Convert README to README.md 2022-04-07 22:44:42 +09:00
TEST_MAPPING Add ComposHostTestCases to system/sepolicy/TEST_MAPPING 2021-10-13 16:42:27 -07:00
treble_sepolicy_tests_for_release.mk Non-module targets. 2022-03-31 19:25:33 -07:00

Android SEPolicy

This directory contains the core Android SELinux policy configuration. It defines the domains and types for the AOSP services and apps common to all devices. Device-specific policy should be placed under a separate device/<vendor>/<board>/sepolicy subdirectory and linked into the policy build as described below.

Policy Generation

Additional, per device, policy files can be added into the policy build. These files should have each line including the final line terminated by a newline character (0x0A). This will allow files to be concatenated and processed whenever the m4(1) macro processor is called by the build process. Adding the newline will also make the intermediate text files easier to read when debugging build failures. The sets of file, service and property contexts files will automatically have a newline inserted between each file as these are common failure points.

These device policy files can be configured through the use of the BOARD_VENDOR_SEPOLICY_DIRS variable. This variable should be set in the BoardConfig.mk file in the device or vendor directories.

BOARD_VENDOR_SEPOLICY_DIRS contains a list of directories to search for additional policy files. Order matters in this list. For example, if you have 2 instances of widget.te files in the BOARD_VENDOR_SEPOLICY_DIRS search path, then the first one found (at the first search dir containing the file) will be concatenated first. Reviewing out/target/product/<device>/obj/ETC/vendor_sepolicy.conf_intermediates/vendor_sepolicy.conf will help sort out ordering issues.

Example BoardConfig.mk Usage: From the Tuna device BoardConfig.mk, device/samsung/tuna/BoardConfig.mk

BOARD_VENDOR_SEPOLICY_DIRS += device/samsung/tuna/sepolicy

Alongside vendor sepolicy dirs, OEMs can also amend the public and private policy of the product and system_ext partitions:

SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS += device/acme/roadrunner-sepolicy/systemext/public
SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS += device/acme/roadrunner-sepolicy/systemext/private
PRODUCT_PUBLIC_SEPOLICY_DIRS += device/acme/roadrunner-sepolicy/product/public
PRODUCT_PRIVATE_SEPOLICY_DIRS += device/acme/roadrunner-sepolicy/product/private

The old BOARD_PLAT_PUBLIC_SEPOLICY_DIR and BOARD_PLAT_PRIVATE_SEPOLICY_DIR variables have been deprecated in favour of SYSTEM_EXT_*.

Additionally, OEMs can specify BOARD_SEPOLICY_M4DEFS to pass arbitrary m4 definitions during the build. A definition consists of a string in the form of macro-name=value. Spaces must NOT be present. This is useful for building modular policies, policy generation, conditional file paths, etc. It is supported in the following file types:

  • All *.te and SELinux policy files as passed to checkpolicy
  • file_contexts
  • service_contexts
  • property_contexts
  • keys.conf

Example BoardConfig.mk Usage:

BOARD_SEPOLICY_M4DEFS += btmodule=foomatic \
                        btdevice=/dev/gps

SPECIFIC POLICY FILE INFORMATION

mac_permissions.xml

The mac_permissions.xml file is used for controlling the mmac solutions as well as mapping a public base16 signing key with an arbitrary seinfo string. Details of the files contents can be found in a comment at the top of that file. The seinfo string, previously mentioned, is the same string that is referenced in seapp_contexts.

It is important to note the final processed version of this file is stripped of comments and whitespace. This is to preserve space on the system.img. If one wishes to view it in a more human friendly format, the tidy or xmllint command will assist you.

insertkeys.py

Is a helper script for mapping arbitrary tags in the signature stanzas of mac_permissions.xml to public keys found in pem files. This script takes a mac_permissions.xml file(s) and configuration file in order to operate. Details of the configuration file (keys.conf) can be found in the subsection keys.conf. This tool is also responsible for stripping the comments and whitespace during processing.

keys.conf

The keys.conf file is used for controlling the mapping of "tags" found in the mac_permissions.xml signature stanzas with actual public keys found in pem files. The configuration file is processed via m4.

The script allows for mapping any string contained in TARGET_BUILD_VARIANT with specific path to a pem file. Typically TARGET_BUILD_VARIANT is either user, eng or userdebug. Additionally, one can specify "ALL" 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 @. The option arguments can also use environment variables via the familiar $VARIABLE syntax. This is often useful for setting a location to ones release keys.

Often times, one will need to integrate an application that was signed by a separate organization and may need to extract the pem file for the insertkeys/keys.conf tools. Extraction of the public key in the pem format is possible via openssl. First you need to unzip the apk, once it is unzipped, cd into the META_INF directory and then execute

openssl pkcs7 -inform DER -in CERT.RSA -out CERT.pem -outform PEM  -print_certs

On some occasions CERT.RSA has a different name, and you will need to adjust for that. After extracting the pem, you can rename it, and configure keys.conf and mac_permissions.xml to pick up the change. You MUST open the generated pem file in a text editor and strip out anything outside the opening and closing scissor lines. Failure to do so WILL cause a compile time issue thrown by insertkeys.py

NOTE: The pem files are base64 encoded and PackageManagerService, mac_permissions.xml and setool all use base16 encodings.