Android 使用 audit2allow 工具添加SELinux权限

53 篇文章 2 订阅
4 篇文章 0 订阅

Andorid 官网相关SELinux说明:
SELinux 概念
Android 中的安全增强型 Linux
验证 SELinux

开发应用报错,提示

avc: denied { write } for comm="com.test" name="/" dev="dm-5" ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_root_file:s0 tclass=dir permissive=0

那就是缺少 SELinux 权限,那就加上。

在 Android aosp 源码上,需要先执行过 source build/envsetuplunch 命令,
如果不执行 ,会提示

ANDROID_HOST_OUT not set. Have you run lunch?

所以,得先执行。执行后,在根目录就可以直接使用 audit2allow 工具了。

audit2allow

audit2allow 工具路径是 external/selinux/prebuilts/bin/audit2allow

把 avc 的 log 写入到 avc_log.txt 中,

注意,抓到的 log 可能是这样的,

09-28 09:30:06.221 5734 5734 W Thread-20: type=1400 audit(0.0:2346): avc: denied { write } for comm=“com.test” name=“/” dev=“dm-5” ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_root_file:s0 tclass=dir permissive=0

写入文件时要去掉前面的部分,只保留从 avc: denied 开头的部分,即
avc: denied { write } for comm=“com.test” name=“/” dev=“dm-5” ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_root_file:s0 tclass=dir permissive=0

文件名可以自己修改,把 avc_log.txt 拷贝到根目录,

然后执行 audit2allow -i avc_log.txt ,就会得到如下结果

#============= system_app ==============
allow system_app system_data_root_file:dir write;

如果没有执行出结果,可以把 avc_log.txt 中的内容复制多行后再执行

执行 get_build_var BOARD_SEPOLICY_DIRS 找到写 Selinux 权限的目录,然后根据目录找对应的 te 文件(因为权限是写在 te 文件里的),然后写入这个结果。

结果可能有好几个,写入到任意一个就行。
不过还是建议遵循归类原则。
比如,我们要加的是 system_app 的权限,就找已经加过 system_app 相关的 te ;要加的是 vendor_app 的权限,就找已经加过 vendor_app 相关的 te。

本例是加到了 device/xxx/common/sepolicy/system_app.te 中.

修改后报错,

out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/policy.conf ) && (out/host/linux-x86/bin/sepolicy-analyze out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp neverallow -w -f out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/policy_2.conf ||       ( echo \"\" 1>&2;         echo \"sepolicy-analyze failed. This is most likely due to the use\" 1>&2;      echo \"of an expanded attribute in a neverallow assertion. Please fix\" 1>&2;          echo \"the policy.\" 1>&2;       exit 1 ) ) && (touch out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp ) && (mv out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows.tmp out/target/product/xxx/obj/FAKE/sepolicy_neverallows_intermediates/sepolicy_neverallows )"
libsepol.report_failure: neverallow on line 634 of system/sepolicy/public/init.te (or line 21764 of policy.conf) violated by allow system_app system_data_root_file:dir { write };
libsepol.check_assertions: 1 neverallow failures occurred
Error while expanding policy

按照提示修改 system/sepolicy/public/init.te

-neverallow { domain -init -toolbox -vendor_init -vold } system_data_root_file:dir { write add_name remove_name };
+neverallow { domain -init -toolbox -vendor_init -vold } system_data_root_file:dir { add_name remove_name };

然后任选如下任一方式单编:

  • 到 system/sepolicy/ 下执行 mma -j12 编译,适用于只修改了这个目录的情况。
  • 在根目录执行 make -j12 sepolicy推荐做法,修改了原生 sepolicy 和厂商 sepolicy 都可以这样验证。

单编通过的话说明语法没问题,也没有触发 neverallow 。然后再全编译验证即可。

编译问题

Match operation ‘empty’ is not valid

编译的时候碰到了一个奇怪的问题,

Could not read line from 'out/target/product/xxx/vendor/etc/selinux/vendor_property_contexts': Match operation 'empty' is not valid: must be either 'prefix' or 'exact'

百度搜索之后找到答案:
是因为 te,context 文件结尾必须要有空行。检查 context 文件后,在末尾加上空行就好了。

x_x_perms

添加权限是,可能需要同时加多个权限,如,

allow system_app hidraw_device:chr_file { read open getattr};

挨个加是没问题的,简便方法是通过宏定义 x_x_perms 全部加上,如,

allow system_app hidraw_device:chr_file rw_file_perms;

rw_file_perms 的定义在 system/sepolicy/prebuilts/api/28.0/public/global_macros

#####################################
# Common groupings of permissions.
#
define(`x_file_perms', `{ getattr execute execute_no_trans map }')
define(`r_file_perms', `{ getattr open read ioctl lock map }')
define(`w_file_perms', `{ open append write lock map }')
define(`rx_file_perms', `{ r_file_perms x_file_perms }')
define(`ra_file_perms', `{ r_file_perms append }')
define(`rw_file_perms', `{ r_file_perms w_file_perms }')
define(`rwx_file_perms', `{ rw_file_perms x_file_perms }')
define(`create_file_perms', `{ create rename setattr unlink rw_file_perms }')

define(`r_dir_perms', `{ open getattr read search ioctl lock }')
define(`w_dir_perms', `{ open search write add_name remove_name lock }')
define(`ra_dir_perms', `{ r_dir_perms add_name write }')
define(`rw_dir_perms', `{ r_dir_perms w_dir_perms }')
define(`create_dir_perms', `{ create reparent rename rmdir setattr rw_dir_perms }')

define(`r_ipc_perms', `{ getattr read associate unix_read }')
define(`w_ipc_perms', `{ write unix_write }')
define(`rw_ipc_perms', `{ r_ipc_perms w_ipc_perms }')
define(`create_ipc_perms', `{ create setattr destroy rw_ipc_perms }')
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要从日志中查看SELinux权限导致的网络无法使用问题,可以按照以下步骤进行操作: 1. 打开终端,以root用户身份登录系统。 2. 使用以下命令查看SELinux日志: ``` grep AVC /var/log/audit/audit.log ``` 这个命令将会输出所有包含 "AVC" 的日志记录,这些记录表示SELinux原子策略的决策。 3. 在输出中查找与网络相关的日志记录。例如,你可以搜索 "network"、"port"、"socket"、"httpd" 或 "ftp" 等关键词,以查找与网络相关的日志记录。 4. 了解SELinux的决策。在日志记录中,你会看到一些以 "AVC" 开头的记录,其中包含了SELinux的决策信息。例如: ``` type=AVC msg=audit(1436929977.620:289): avc: denied { name_connect } for pid=3211 comm="httpd" dest=80 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket ``` 这条记录表示,一个进程(httpd)尝试连接到端口80,但是由于SELinux的策略,这个连接被拒绝了。在这个例子中,原因是 httpd 进程的安全上下文(scontext)是 "unconfined_u:system_r:httpd_t:s0",而目标端口的安全上下文(tcontext)是 "system_u:object_r:http_port_t:s0"。由于这两个安全上下文不匹配,SELinux拒绝了这个连接。 5. 修改SELinux的策略。如果你在日志中发现了与网络相关的SELinux拒绝记录,那么你需要针对这些记录来修改SELinux的策略。例如,在上面的例子中,你需要将 httpd 进程的安全上下文与目标端口的安全上下文进行匹配。你可以使用 `chcon` 命令来修改文件或目录的安全上下文,使用 `semanage` 命令来修改SELinux策略。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值