Android SELinux权限

Android 使用安全增强型 Linux (SELinux) 对所有进程强制执行强制访问控制 (MAC),其中包括以 Root/超级用户权限运行的进程(Linux 功能)。

工作模式

宽容模式 - 仅记录但不强制执行 SELinux 安全政策。
强制模式 - 强制执行并记录安全政策。如果失败,则显示为 EPERM 错误。
可以通过getenfoce来获取当前工作模式:

console:/ # getenforce
Enforcing

还可以通过setenforce 0来设置当前模式为Permissive:

console:/ # setenforce 0
console:/ # getenforce
Permissive

也可以通过setenforce 1来设置当前模式为Enforcing:

console:/ # setenforce 1
console:/ # getenforce
Enforcing

SELinux安全上下文

SELinux 管理过程中,进程是否可以正确地访问文件资源,取决于它们的安全上下文。进程和文件都有自己的安全上下文,SELinux 会为进程和文件添加安全信息标签,安全上下文的格式为:user:role:type:sensitivity[:categories],通常可以忽略上下文的 user、role 和 sensitivity 字段。当运行 SELinux 后,所有这些信息都将作为访问控制的依据。

  1. 查看文件和目录的安全上下文,执行命令如下:
1|console:/ # ls -Z
u:object_r:cgroup:s0                acct             u:object_r:tmpfs:s0                 mnt
u:object_r:apex_mnt_dir:s0          apex             u:object_r:vendor_file:s0           odm
u:object_r:rootfs:s0                bin              u:object_r:vendor_file:s0           odm_dlkm
u:object_r:rootfs:s0                bugreports       u:object_r:oemfs:s0                 oem
u:object_r:cache_file:s0            cache            u:object_r:postinstall_mnt_dir:s0   postinstall
u:object_r:configfs:s0              config           u:object_r:proc:s0                  proc
u:object_r:rootfs:s0                d                u:object_r:system_file:s0           product
u:object_r:system_data_root_file:s0 data             u:object_r:rootfs:s0                sdcard
u:object_r:mirror_data_file:s0      data_mirror      u:object_r:tmpfs:s0                 second_stage_resources
u:object_r:tmpfs:s0                 debug_ramdisk    u:object_r:mnt_user_file:s0         storage
u:object_r:device:s0                dev              u:object_r:sysfs:s0                 sys
u:object_r:rootfs:s0                etc              u:object_r:system_file:s0           system
u:object_r:init_exec:s0             init             u:object_r:system_dlkm_file:s0      system_dlkm
u:object_r:rootfs:s0                init.environ.rc  u:object_r:system_file:s0           system_ext
u:object_r:linkerconfig_file:s0     linkerconfig     u:object_r:vendor_file:s0           vendor
u:object_r:metadata_file:s0         metadata         u:object_r:vendor_file:s0   
  1. 查看进程的安全上下文,执行命令如下:
console:/ # ps -Z -e |grep smartdevice
u:r:mt_platform_app:s0         system        1512   384    1232712  75756 do_epoll_wait       0 S com.smartdevice.global.service
u:r:system_app:s0              system        2081   384    1330484 186356 do_epoll_wait       0 S com.smartdevice.dailyshortcut
u:r:system_app:s0              system        2123   384    1193112  55872 do_epoll_wait       0 S com.smartdevice.leanbacklauncher.customizer
u:r:system_app:s0              system        3157   384    1302756 119640 do_epoll_wait       0 S com.smartdevice.dtv.icast
u:r:untrusted_app_32:s0:c54,c256,c512,c768 u0_a54 3186 384 1883096  63176 do_epoll_wait       0 S com.smartdevice.tv.aircast
u:r:mt_platform_app:s0         system        4268   384    1230384  94636 do_epoll_wait       0 S com.smartdevice.factory
u:r:system_app:s0              system        8659   384    1195700  57920 do_epoll_wait       0 S com.smartdevice.bleconnectservice
u:r:system_app:s0              system       11512   384    1262884 129512 do_epoll_wait       0 S com.smartdevice.ipcontrol

进程SContext的type字段代表该进程所属的Domain。
3. 查看属性的安全上下文,执行命令如下:

console:/mnt/vendor # getprop -Z persist.sys.locale
u:object_r:locale_prop:s0

安全策略

SELinux安全机制又称为是基于TE(Type Enforcement)策略的安全机制。所有安全策略都存放在.te结尾的文件中,一般放在 /system/sepolicy/private/,厂商定制的一般放在/device/xxx/sepolicy/下。
语句格式为:

allow source target:class permissions;
//source - 规则主题的类型(或属性),即进程的组,也就是进程的domain。
//目标 - 对象的类型(或属性),即进程所要操作的文件的type。
//类 - 要访问的对象(例如,文件、套接字)的类型,system/sepolicy/private/security_classes中有class的定义,常见的有file,dir等。
//权限 - 要执行的操作(或一组操作,例如读取、写入),常见的有read , write, ioctrol, create, getattr, getattr等。

示例:

allow system_app vendor_configs_file:file { read getattr open };
get_prop(system_app, vendor_default_prop) 其实是一个宏,展开如下:
allow system_app vendor_default_prop:file r_file_perms; 

错误修改

常见错误

出现违反SELinux安全策略的错误时一般会有如下log输出:

type=1400 audit(0.0:16187): avc:  denied  { read getattr open } for  name="vendor" dev="tmpfs" ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:vendor_configs_file:s0 tclass=file permissive=0

按照规则 allow scontex tcontex:tclass action 来改即可:

allow system_app vendor_configs_file:file { read getattr open };

违反规则的同时又neverallow问题修改

Google 在system/sepolicy中有使用相关neverallow 规则, 对SELinux Policy 的更新进行了限制, 以防止开发者过度开放权限,从而引发安全问题。并且通过CTS Test 检测开发者是否有违反相关的规则.
如:

type=1400 audit(0.0:2928): avc:  denied  { search } for  name="vendor" dev="tmpfs" ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:mnt_vendor_file:s0 tclass=dir permissive=0

按照之前规则配置:

allow system_app mnt_vendor_file:dir { search };

编译出现报错:

FAILED: out/soong/.intermediates/system/sepolicy/sepolicy.recovery/android_common/sepolicy
out/host/linux-x86/bin/secilc -m -M true -G -c 30 out/soong/.intermediates/system/sepolicy/recovery_sepolicy.cil/android_common/recovery_sepolicy.cil -o out/soong/.intermediates/system/sepolicy/sepolicy.recovery/android_common/sepolicy_policy -f /dev/null && cp -f out/soong/.intermediates/system/sepolicy/sepolicy.recovery/android_common/sepolicy_policy out/soong/.intermediates/system/sepolicy/sepolicy.recovery/android_common/sepolicy && rm -f out/soong/.intermediates/system/sepolicy/sepolicy.recovery/android_common/sepolicy_policy # hash of input list: 187605db6ee3f7580bafd9adbd0101d2c2a0d02f423bb7efa74ee537c43d35ce
neverallow check failed at out/soong/.intermediates/system/sepolicy/recovery_sepolicy.cil/android_common/recovery_sepolicy.cil:11171 from system/sepolicy/public/domain.te:1233
  (neverallow base_typeattr_281 mnt_vendor_file (dir (ioctl read write create getattr setattr lock relabelfrom relabelto append map unlink link rename execute quotaon mounton audit_access open execmod watch watch_mount watch_sb watch_with_perm watch_reads add_name remove_name reparent search rmdir)))
    <root>
    allow at out/soong/.intermediates/system/sepolicy/recovery_sepolicy.cil/android_common/recovery_sepolicy.cil:33799
      (allow system_app mnt_vendor_file (dir (search)))

Failed to generate binary
Failed to build policydb

通过查看system/sepolicy/public/domain.te:

# Platform must not have access to /mnt/vendor.
neverallow {
  coredomain
  -init
  -ueventd
  -vold
  -system_writes_mnt_vendor_violators
} mnt_vendor_file:dir *;

domain.te文件中,明确domain中是没有对mnt_vendor_file的读写权限。

  • 方法一:将 system_app 添加到上述neverallow的domain例外中,但后续可能会有cts问题,不建议使用。
  • 方法二:将要操作的文件定义为其他type,然后允许system_app来读写这个新type的文件。
    1. file.te新增mt_config_file SELinux type:
type mt_config_file, file_type;
  1. 绑定文件到SELinux type, file_contexts文件添加:
/mnt/vendor/config(/.*)?                                u:object_r:mt_config_file:s0
//这个含义代表/mnt/vendor/config下所有的文件或者目录的标签都是mt_config_file,
//如果在mnt/vendor/config的子目录下,创建一个目录或者文件,并不给它打标签的话,那么这个新创建的文件或者目录会和父目录的标签一致
  1. 申请权限:
allow system_app mt_config_file:dir search;
  1. 发现还是无法访问,再修改权限域,seapp_contexts新建权限域:
user=system seinfo=platform name=pkgNmae domain=mt_platform_app type=mt_tvapk_app_data_file
  1. 最后新建mt_platform_app.te,申请权限:
allow mt_platform_app mt_config_file:dir search;

自定义Prop 错误

在mk中增加Prop,

PRODUCT_PRODUCT_PROPERTIES += \
    ro.vendor.firmware.version=1.0.0

通过SystemProperties去获取时发现出现Access denied finding property "ro.vendor.firmware.version"问题。
对于vendor下property权限,类似file权限

  1. attributes.te定义type:
attribute mtk_core_property_type;
  1. 在property.te 中添加:
vendor_public_prop(vendor_fw_version_prop)
typeattribute vendor_fw_version_prop                        mtk_core_property_type;
  1. 在property_contexts中添加:
ro.vendor.firmware.version                              u:object_r:vendor_fw_version_prop:s0
  1. system_app.te添加权限
allow system_app vendor_fw_version_prop:file { map getattr open };
get_prop(system_app, vendor_fw_version_prop)

其他

App的上下文 seapp_contexts 文件,可以直接修改,push手机上。重启后生效。 属性的上下文
property_contexts 文件, 可以直接修改,push手机上。重启后生效。 服务的上下文 service_contexts
文件,可以直接修改,push手机上。重启后生效。 虚拟文件上下文 genfs_contexts 文件,通过chcon命令进行修改。
讲下文件上下文 file_contexts 文件的修改与测试。restorecon 命令可使文件上下文selinux政策生效。
restorecon -R

命令行:
修改某个文件(夹)的selinux lable:

chcon  <安全上下文> 文件
chcon -R  <安全上下文> 目录

示例:

chcon u:object_r:system_data_file:s0 epopdata.json

修改前:

console:/mnt/vendor/persist/config/EPOP # ls -Z
u:object_r:mt_persist_file:s0 EPOP.zip  u:object_r:mt_persist_file:s0 epopdata.json
on -R -t u:object_r:system_data_file:s0 epopdata.json 

修改后:

console:/mnt/vendor/persist/config/EPOP # ls -Z
u:object_r:mt_persist_file:s0  EPOP.zip  u:object_r:system_data_file:s0 epopdata.json

修改 /vendor/tvconfig/config/app 下文件上下文示例:

  1. 首先修改vendor_file_contexts 文件:
把 /vendor/tvconfig/(.*)?                                 u:object_r:vendor_configs_file:s0
修改为:/vendor/tvconfig/(.*)?                                 u:object_r:vendor_configs_file_new:s0
  1. 执行命令:
console:/vendor/tvconfig/config/app # restorecon config.json -v
SELinux: Loaded file context from:
                /system/etc/selinux/plat_file_contexts
                /system_ext/etc/selinux/system_ext_file_contexts
                /product/etc/selinux/product_file_contexts
                /vendor/etc/selinux/vendor_file_contexts
SELinux:  Relabeling /vendor/tvconfig/config/app/config.json from u:object_r:vendor_configs_file:s0 to u:object_r:vendor_configs_file_new:s0.

TE文件说明:

可用于加标签的文件名含义对应的声明文件名
file_contexts给文件/目录/节点 新增标签file.te
genfs_contexts给节点新增标签,与上一个不同的是,不用执行restorecon操作file.te
hwservice_contexts给hal服务新增标签hwservice.te
property_contexts给prop服务新增标签property.te
seapp_contexts给APP新增标签untrusted_app,te app.te,system_app.te…等
service_contexts给系统服务新增标签service.te
### AndroidSELinux 权限配置教程 #### 1. SELinux 基础概念 SELinux 是一种强制访问控制 (MAC) 安全机制,它补充了传统的基于用户 ID 和组 ID 的自主访问控制 (DAC)[^2]。在 Android 系统中,SELinux 提供了一种更细粒度的安全模型来保护设备免受潜在威胁。 当遇到 `permission denied` 错误时,通常是因为某些操作违反了 SELinux 的策略规则。这可能涉及文件、目录或其他资源的访问尝试被拒绝的情况[^5]。 --- #### 2. 配置 SELinux 权限时的关键步骤 ##### 2.1 查看日志并定位问题 要解决 SELinux 导致的权限问题,第一步是查看系统日志以获取详细的错误信息。可以通过以下命令捕获相关日志: ```bash dmesg | grep avc ``` 这些日志记录了哪些操作因 SELinux 被阻止以及具体的上下文信息。例如,如果某个二进制文件无法执行,则可能是由于其安全标签不匹配或缺少必要的权限[^4]。 ##### 2.2 使用 audit2allow 工具分析和生成新规则 为了快速修复缺失的权限,可以利用工具 **audit2allow** 自动生成所需的 policy 文件片段。具体流程如下所示: 1. 收集 AVC 拒绝消息; 2. 运行下面这条指令来自动生成对应的 rule: ```bash cat /path/to/denied_log.txt | audit2allow -M mymodule ``` 3. 将生成好的 module 加载至当前运行环境之中测试效果; 需要注意的是,这种方法虽然便捷高效,但也有可能引入新的安全隐患因此需谨慎对待新增加的内容是否合理合法合规[^1]。 ##### 2.3 手动调整 SEPolicy 规则 对于复杂场景或者不适合自动化的修改需求来说,还需要手动编辑 sepolicy 文件来进行精确控制。一般情况下需要遵循以下几个原则: - 添加适当类型的转换规则(type_transition); - 授权特定主体(subject) 对象(object) 实施动作(action); 示例代码展示如何允许某服务读取指定路径下的数据文件夹内容: ```sepol allow myservicedomain file:dir {read getattr}; allow myservicedomain file:file {open read write append lock ioctl}; ``` 完成编写之后记得重新编译整个 selinux framework 并刷入目标设备验证最终成果[^3]. --- #### 3. 特殊情况处理 – 关闭 Enforcing Mode 作为最后手段,在调试阶段也可以考虑暂时禁用 enforce mode 来绕过所有限制条件从而确认是否有其他因素干扰正常工作流。不过强烈建议仅用于短期排查目的而非长期解决方案因为这样会使系统暴露于极大风险之下失去应有的安全保障功能. 临时关闭方法如下: ```bash setenforce 0 ``` 永久更改则需要修改 `/sepolicy` 或者通过 OTA 更新推送更新后的版本给终端用户应用生效. --- ### 总结 通过对上述几个方面的深入探讨可以看出, 正确合理的设置 android 下面得 selinux 参数是一项既技术性强又责任重大的任务. 不但要求开发者具备扎实的操作系统理论基础还要熟悉实际产品架构特点才能做到游刃有余妥善处置各类突发状况.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值