使用 SELinux 上下文在 rootfs (ramdisk) 上标记文件

Label files on rootfs (ramdisk) with SELinux context

SELinux 标签如何应用于 rootfs 上的文件,即直接驻留在 / 中但不属于 systemdata ... 分区的文件(Android 7.1)?在 adb 上调用 ls -lZ / 会产生(摘录):

-rwxr-x---   1 root   root   u:object_r:init_exec:s0         806748 1970-01-01 01:00 init
drwxr-xr-x  14 root   root   u:object_r:cgroup:s0                 0 2019-03-14 14:01 acct
-rw-r--r--   1 root   root   u:object_r:rootfs:s0             99904 1970-01-01 01:00 file_contexts.bin

根据我的研究,rootfs (GPIO+GZ) 不支持 SElinux context labeling (no xattr),所以所有的文件应该有相同的 context,显然不是这样。我已将自定义文件 /foo 添加到继承默认上下文 u:object_r:rootfs:s0.

的 ramdisk

作为测试,我在 system/sepolicy/file_contexts 中添加了一条规则:

/foo   u:object_r:init_exec:s0

为我的文件提供与 /init 相同的上下文。这确实有任何影响(但 grep /foo /file_contexts.bin 显示 /file_contexts.bin 已更新)。如何标记 /foo

我自己发现的。首先,在 file_contexts file, which gets compiled and put into the rootfs as /file_contexts.bin. The /init binary then performs some hardcoded calls to restorecon to apply these labels to some files and directories. Further calls to restorecon are made from the init*.rc 个文件中声明文件上下文(对于所有其他文件)以应用更多文件标签。

因此,要向 rootfs 上的自定义文件添加标签,需要在 file_contexts 文件中定义它,并且需要将对 restorecon 的调用放入 [=16] =] 源代码或 init*.rc 个文件之一。