当前位置: 首页 > news >正文

HQX SELinux 权限问题分析与解决

Google自Android 5.0起强制实施的SELinux安全子系统,通过最小权限原则显著提升了系统安全性,但这也导致开发过程中频繁出现权限拒绝问题。值得注意的是,即便设备已获取root权限,SELinux的强制访问控制机制仍会限制部分敏感操作。
本文将聚焦实战场景,通过典型错误日志解析,系统化讲解如何诊断并解决SELinux权限异常问题。

获取selinux配置

# getenforce
Enforcing

cmdline:

msmnile_gvmq:/ # cat /proc/cmdline
cgroup_disable=pressure androidboot.verifiedbootstate=orange androidboot.vbmeta.device=/dev/vbmeta androidboot.vbmeta.avb_version=1.1 androidboot.vbmeta.device_state=unlocked androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=3840 androidboot.vbmeta.digest=aaa6d414b8059e251f9aaf9cd950350c10d51acd494449855bcbbe925c58f052 androidboot.vbmeta.invalidate_on_error=yes androidboot.veritymode=enforcing console=hvc0,115200 debug user_debug=31 loglevel=9 print-fatal-signals=1 androidboot.console=ttyAMA0 androidboot.hardware=qcom androidboot.selinux=enforcing androidboot.memcg=1 init=/init swiotlb=4096 androidboot.usbcontroller=a600000.dwc3 androidboot.recover_usb=1 firmware_class.path=/vendor/firmware_mnt/image kpti=0 msm_cfg.cfg_sel=1 pcie_ports=compat androidboot.dtbo_idx=1 buildvariant=userdebug  androidboot.serialno=2e036b5d  androidboot.force_normal_boot=1  androidboot.fstab_suffix=ufs.qcom

Notes:
enforcing mode: 限制访问
permissive mode: 只审查权限,不限制

临时关闭selinux

# setenforce --help
usage: setenforce [enforcing|permissive|1|0]Sets whether SELinux is enforcing (1) or permissive (0).
setenforce 0 (临时关闭 SELinux 的限制访问模式)

永久关闭selinux

  1. BoardConfig
    File: device/rockchip/common/BoardConfig.mk
BOARD_SELINUX_ENFORCING ?= false
  1. selinux.cpp
    File: system/core/init/selinux.cpp
diff --git a/init/selinux.cpp b/init/selinux.cpp
index 5a0255acd..db22d7b61 100755
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -104,6 +104,7 @@ EnforcingStatus StatusFromCmdline() {}bool IsEnforcing() {
+    return false;if (ALLOW_PERMISSIVE_SELINUX) {return StatusFromCmdline() == SELINUX_ENFORCING;}

or:

diff --git a/init/selinux.cpp b/init/selinux.cpp
index 5a0255acd..42608a18b 100755
--- a/init/selinux.cpp
+++ b/init/selinux.cpp
@@ -476,6 +476,7 @@ void SelinuxInitialize() {bool kernel_enforcing = (security_getenforce() == 1);bool is_enforcing = IsEnforcing();
+       is_enforcing = 0;if (kernel_enforcing != is_enforcing) {if (security_setenforce(is_enforcing)) {PLOG(FATAL) << "security_setenforce(" << (is_enforcing ? "true" : "false")
  1. Boardconfig.mk
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -194,7 +194,7 @@ BOARD_VENDOR_KERNEL_MODULES += $(shell ls $(KERNEL_MODULES_OUT)/*.ko)TARGET_USES_ION := trueTARGET_USES_NEW_ION_API :=trueTARGET_USES_QCOM_BSP := false
-BOARD_KERNEL_CMDLINE := console=ttyMSM0,115200n8 androidboot.hardware=qcom androidboot.console=ttyMSM0 androidboot.memcg=1 lpm_levels.sleep_disabled=1 video
=vfb:640x400,bpp=32,memsize=3072000 msm_rtb.filter=0x237 service_locator.enable=1 swiotlb=4096 firmware_class.path=/vendor/firmware_mnt/image loop.max_part=7androidboot.usbcontroller=a600000.dwc3 androidboot.recover_usb=1 androidboot.selinux=enforcing hibernate=nocompress noswap_randomize pcie_ports=compat kpti=
0
+BOARD_KERNEL_CMDLINE := console=ttyMSM0,115200n8 androidboot.hardware=qcom androidboot.console=ttyMSM0 androidboot.memcg=1 lpm_levels.sleep_disabled=1 video
=vfb:640x400,bpp=32,memsize=3072000 msm_rtb.filter=0x237 service_locator.enable=1 swiotlb=4096 firmware_class.path=/vendor/firmware_mnt/image loop.max_part=7androidboot.usbcontroller=a600000.dwc3 androidboot.recover_usb=1 androidboot.selinux=permissive hibernate=nocompress noswap_randomize pcie_ports=compat kpti
=0

androidboot.selinux=enforcing --> androidboot.selinux=permissive

  1. system.build.tmpl

File: apps/qnx_ap/target/hypervisor/host/build_files/system.build.tmpl

-cmdline "console=ttyAMA0 earlycon=pl011,0x1c090000 debug user_debug=31 loglevel=9 print-fatal-signals=1 no_console_suspend androidboot.console=ttyAMA0       androidboot.hardware=qcom androidboot.selinux=enforcing androidboot.memcg=1 androidboot.bootdevice=/dev/disk/system_b swiotlb=2048 gvmip=192.168.1.3        androidboot.serialno="$env{SERIAL_NO\}" veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f rootwait skip_initramfs=1 ro init=/init root=/dev/dm-0       dm=\\\"system none ro,0 1 android-verity /dev/vda\\\""+cmdline "console=ttyAMA0 earlycon=pl011,0x1c090000 debug user_debug=31 loglevel=9 print-fatal-signals=1 no_console_suspend androidboot.console=ttyAMA0       androidboot.hardware=qcom androidboot.selinux=permissive androidboot.memcg=1 androidboot.bootdevice=/dev/disk/system_b swiotlb=2048 gvmip=192.168.1.3        androidboot.serialno="$env{SERIAL_NO\}" veritykeyid=id:7e4333f9bba00adfe0ede979e28ed1920492b40f rootwait skip_initramfs=1 ro init=/init root=/dev/dm-0       dm=\\\"system none ro,0 1 android-verity /dev/vda\\\""

androidboot.selinux=enforcing --> androidboot.selinux=permissive

  1. Kernel 中关闭selinux
    config
CONFIG_SECURITY_SELINUX=y

关闭以上配置。

可通过以下命令确实kernel中配置:

# zcat /proc/config.gz | grep SELINUX
CONFIG_SECURITY_SELINUX=y
  1. Others
    某些平台中,可能需要修改devicetree中chosen字段

增加SElinux权限

  1. 通过以下命令获取selinux
logcat | grep audit > selinux.txt
or:
dmesg | grep audit > selinux.txt
cat /proc/kmsg | grep audit 

Example:

type=1400 audit(0.0:29830): avc:  denied  { getattr } for property=ro.ril.ecclist pid=1 uid=0 gid=0 scontext=u:r:test_init:s0 tcontext=u:object_r:radio_prop:s0 tclass=property_service permissive=0
  1. Log解析
    avc: denied { 操作权限 } for pid=7201 comm=“进程名” scontext=u:r:源类型:s0 tcontext=u:r:目标类型:s0 tclass=访问类型 permissive=0

#============= 源类型 ==============
allow 源类型 目标类型:访问类型 操作权限;
完整的意思是: "源类型"进程对"目标类型"的"访问类型"缺少 “操作权限”

  1. 手动添加权限
    File path:
#============= test_init ==============
allow test_init radio_prop:property_service getattr;
  1. 工具添加权限
    使用 audit2allow 工具解析avc.txt。
    在aosp源码中执行如下命令:
source ./build/envsetup.sh
lunch msmnile_au-userdebug
audit2allow -i avc.txt 

解析出来如下图所示:

#============= test_init ==============
allow test_init automotive_display_service:dir getattr;
allow test_init hal_broadcastradio_default:dir getattr;
allow test_init network_stack:dir getattr;
allow test_init platform_app:dir getattr;
allow test_init secure_element:dir getattr;

上述步骤解析完了报出的权限问题,接下来需要把,这些权限添加到对应的 te 文件中,直接将上述的四条 allow xxx 拷贝到 te即可。
绝大多数的情况下 avc denied的报错不会一次性的全部暴露出来,要解决完一个,才会报下一个问题。
比如:一个进程需要读、写和打开的权限,但是一般情况下,可能会先报其中一个权限缺失,等你加了这个权限后,才会报另一个权限缺失,以此类推。

  1. te文件路径
    Path:
device/qcom/sepolicy_vndr;
device/qcom/sepolicy;
/system/sepolicy

尽量只修改device目录下的te文件,修改system目录下te会影响Google CTS测试。

http://www.dtcms.com/a/362774.html

相关文章:

  • 使用 Avidemux 去除视频的重复帧
  • 亚马逊美加站点物流新规解读:库存处理逻辑重构与卖家应对策略
  • 两台电脑通过网线直连共享数据,设置正确,却互相ping不通的解决方法
  • 探索 UniHttp:解锁 Xml 及 JavaBean 序列化的多种方式
  • ASP.NET Core上传文件到minio
  • 嵌入式硬件 - 51单片机1
  • JVM中产生OOM(内存溢出)的8种典型情况及解决方案
  • 自从不小心踢了一脚主机之后,电脑频繁蓝屏、死机、无法开机……
  • 鸿蒙Next开发指南:XComponent与Progress组件的深度解析与实践
  • 睿思芯科正式加入龙蜥社区,携手共建 RISC-V 服务器生态新标杆
  • react+taro的使用整理
  • 【JavaEE】(21)Spring AOP
  • 解密GTH时钟架构:一网打尽收发器时钟之谜
  • 火语言 RPA 界面应用生成:低代码逻辑下的功能设计与场景适配
  • PowerPoint和WPS演示如何循环放映PPT
  • 想找Gamma的平替?这几款AI PPT工具值得试试
  • 从技术架构到经济价值:低代码在企业开发中的成本节约潜力
  • LeetCode 925.长按键入
  • 哈希表-面试题01.02.判定是否互为字符重排-力扣(LeetCode)
  • 趣味学RUST基础篇(HashMap)
  • 二叉树的非递归遍历 | 秋招面试必备
  • Spring Bean
  • LLM面试50问:NLP/RAG/部署/对齐/安全/多模态全覆盖
  • R语言根据经纬度获得对应样本的省份
  • WPF依赖属性和依赖属性的包装器:
  • iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
  • 安卓学习 之 gradle下载失败的解决方法
  • Elasticsearch面试精讲 Day 5:倒排索引原理与实现
  • 跨越产业技术障碍、创新制造模式的智慧工业开源了
  • 【开题答辩全过程】以宠物生活社区为例,包含答辩的问题和答案