配置 Android selinux 以挂载 tmpfs 文件系统



我正在尝试在Android AOSP上挂载一个大小有限的tmpfs,以便在RAM中缓存一些HLS视频。视频不断重写,因此无法写入会磨损的闪存。我不知道如何获得正确的权限,有什么建议吗?

另外我应该使用什么域?我想限制此目录,以便只有我的应用程序可以使用它,但这不是绝对要求。 我的应用是普通应用,没有特权。

hls.te

type hlsfs, fs_type;
# Allow file access in /hls
allow domain hlsfs:file create_file_perms;
allow domain hlsfs:dir rw_dir_perms;
allow init hlsfs:dir { create_dir_perms relabelto };

file_contexts

#HLS cache
/data/hls(/.*)?              u:object_r:hlsfs:s0

init.rc

on init
mkdir /data/hls 0777 system system
on early-fs
mount tmpfs tmpfs /data/hls mode=777,size=60M,context=u:object_r:hlsfs:s0

错误消息,第一个错误是MKDIR,第二个错误是装载。

root@bpi-m64-hdmi:/ # dmesg | grep hls
[    2.218541] type=1400 audit(1499777970.690:4): avc:  denied  { associate } for  pid=1 comm="init" name="hls" scontext=u:object_r:hlsfs:s0 tcontext=u:object_r:rootfs:s0 tclass=filesystem permissive=1
[   11.285712] type=1400 audit(1499777979.750:6): avc: denied { relabelto } for pid=1 comm="init" name="hls" dev="dm-1" ino=170689 scontext=u:r:init:s0 tcontext=u:object_r:hlsfs:s0 tclass=dir permissive=0

Android 默认的 selinux 政策在这里 https://android.googlesource.com/platform/system/sepolicy/+/android-n-preview-2/

更新:

我在这方面取得了很大的进展。首先,把它放在/data 中是一个坏主意,因为有许多现有的规则阻止了这一点。所以我把它移到了/cache中。

hls.te

type hlsfs, contextmount_type, fs_type;
# Allow file access in /hls
permissive hlsfs;
#allow domain hlsfs:file create_file_perms;
#allow domain hlsfs:dir rw_dir_perms;

file_contexts

#HLS cache
/cache/hls(/.*)?              u:object_r:hlsfs:s0

init.rc

on fs
mkdir /cache/hls 0777 system system
mount tmpfs tmpfs /cache/hls mode=777,size=60M,context=u:object_r:hlsfs:s0

init.te

# allow hls tmpfs to be mounted in cache dir
allow init cache_file:dir mounton;

通过这些更改,我可以挂载 tmpfs 目录。接下来,我需要确保我的应用程序可以使用它。 我还需要确保应用程序无法在 tmp 目录中执行任何内容。

更新2:

应用无法访问缓存目录。因此,让我们尝试另一种方案...

这个方案几乎是正确的。我可以挂载 tmpfs 目录,我的应用程序可以访问它。但是除非我更改所有者"chown media_rw media_rw/data/media/0/hls",否则该应用程序无法访问它。更改所有者会触发核心 Android 规则中的拒绝错误。我将 init 设置为允许以绕过错误。那么,我该如何解决目录的需要呢?我尝试将权限设置为 777 并使用 gid=1023/uid=1023 进行挂载。都无济于事。

hls.te

# Allow file access in /hls
permissive hlsfs;

file_contexts

#HLS cache
/data/media/0/hls(/.*)?              u:object_r:hlsfs:s0

init.rc

on property:sys.boot_completed=1
mkdir /data/media/0/hls 0777 media_rw media_rw
mount tmpfs tmpfs /data/media/0/hls mode=777,size=60M,context=u:object_r:hlsfs:s0
chown media_rw media_rw /data/media/0/hls

init.te

# allow hls tmpfs to be mounted in /data/media dir
allow init media_rw_data_file:dir {setattr mounton};
allow init hlsfs:dir {create setattr relabelto mounton};
allow init labeledfs:filesystem associate;
permissive init;

错误信息

07-14 23:02:33.010 1-1/? I/init: type=1400 audit(0.0:5): avc: denied { relabelto } for scontext=u:r:init:s0 tcontext=u:object_r:hlsfs:s0 tclass=filesystem permissive=1

我相信我现在有这个工作。我的最后一个错误是试图为 tmp 目录制定自定义策略。自定义策略导致重新标记错误。

现在我在getExternalStorageDirectory((下有一个工作目录,它是基于tmpfs的,限制为60MB。

init.te

# allow hls tmpfs to be mounted in /data/media dir
allow init media_rw_data_file:dir {setattr mounton};
allow init labeledfs:filesystem associate;

init.rc

on property:sys.boot_completed=1
mkdir /data/media/0/hls 0777 media_rw media_rw
mount tmpfs tmpfs /data/media/0/hls size=60M

sdcardd.te

allow sdcardd tmpfs:dir create_dir_perms;
allow sdcardd tmpfs:file create_file_perms;

相关内容

最新更新