Android selinux策略配置
2021-06-28 本文已影响0人
后知晚觉
功能需求描述
在android的keystore服务中获取调用者的包名。
代码实现:
#include <binder/IPCThreadState.h>
//......
pid_t pid = android::IPCThreadState::self()->getCallingPid();
ALOGI("calling pid: %d", pid);
char path[64] = { 0 };
sprintf(path, "/proc/%d/cmdline", pid);
FILE *cmdline = fopen(path, "r");
if (cmdline) {
char application_id[64] = { 0 };
fread(application_id, sizeof(application_id), 1, cmdline);
ALOGI("application id %s", application_id);
fclose(cmdline);
}
selinux异常
代码在keystore进程中运行时,会抛出selinux avc异常:
05-31 08:28:34.889 7420 7420 W keystore: type=1400 audit(0.0:177): avc: denied { getattr } for path="/proc/9671/cmdline" dev="proc" ino=128198 scontext=u:r:keystore:s0 tcontext=u:r:untrusted_app:s0:c141,c256,c512,c768 tclass=file permissive=0
异常分析:
keystore在对untrusted_app的file做getattr操作时抛出异常。
sepolicy编写
# system/sepolicy/public/keystore.te system/sepolicy/prebuilts/api/30.0/public/keystore.te
# 两个文件都增加一行:
allow keystore untrusted_app:file getattr;
添加上面一行可以满足功能需求描述中要求。
如果安全策略与其他策略文件冲突,编译系统在编译时会对策略文件做检测,如果存在冲突的情况,还需要额外修改以下文件:
system/sepolicy/prebuilts/api/30.0/private/coredomain.te
system/sepolicy/prebuilts/api/30.0/public/domain.te
system/sepolicy/private/coredomain.te
system/sepolicy/public/domain.te
有时存在system domain下引用不到vendor domain中变量的情况,如果涉及到vendor相关资源的权限,可能还需要在vendor路径下新增策略文件:
device/qcom/sepolicy_vndr/generic/vendor/common/keystore.te
编译
$ source build/envsetup.sh
$ lunch xxxxx
$ mmm system/sepolicy -j2
push到设备中验证
$ adb push out/target/product/xxxxxx/system/etc/selinux/* /system/etc/selinux/
$ adb shell sync
$ adb reboot
$ adb wait-for-device
$ adb logcat | grep keystore
如果涉及到vendor相关的策略改动,还需要执行下面的命令:
$ adb push out/target/product/xxxxxx/vendor/etc/selinux/* /vendor/etc/selinux/
设备重启后,触发功能代码执行,观察log,看是否有avc异常抛出。