美文网首页
android selinux

android selinux

作者: tjy_2011 | 来源:发表于2018-08-21 20:54 被阅读0次

关键字: android, selinux, getenforce, setenforce, audit2allow

20180817 tjy

转载请注明出处

Android在4.3引入selinux, 当时工作上需要了解并解决一些selinux的问题, 这里总结一下涉及到的selinux的一些东西,不是普及性的文章, 只是记录和穿针引线的作用。

  1. logcat日志

如果某些可执行文件或者app或者文件访问的selinux没有设置好, 会在logcat中打印一些类似的日志, 这个时候需要对这些异常添加selinux权限了。

01-15 09:38:34.248    35    35 I kworker/0:1: type=1400 audit(0.0:23): avc: denied { search } for name="/" dev="mmcblk0p34" ino=2 scontext=u:r:kernel:s0 tcontext=u:object_r:persist_file:s0 tclass=dir permissive=1
01-15 09:38:38.468  1382  1382 I iptables-wrappe: type=1400 audit(0.0:26): avc: denied { read write } for path="socket:[18709]" dev="sockfs" ino=18709 scontext=u:r:netutils_wrapper:s0 tcontext=u:r:netmgrd:s0 tclass=netlink_socket permissive=1
01-15 09:38:47.128   410   410 I HwBinder:410_2: type=1400 audit(0.0:27): avc: denied { read } for name="class" dev="sysfs" ino=10 scontext=u:r:hal-server:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=1
01-15 09:38:47.128   410   410 I HwBinder:410_2: type=1400 audit(0.0:28): avc: denied { open } for path="/sys/class" dev="sysfs" ino=10 scontext=u:r:hal-server:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=1
01-15 09:38:48.768   880   880 I BootAnimation: type=1400 audit(0.0:29): avc: denied { dac_override } for capability=1 scontext=u:r:bootanim:s0 tcontext=u:r:bootanim:s0 tclass=capability permissive=1
01-15 09:38:54.208  3214  3214 I m.android.phone: type=1400 audit(0.0:33): avc: denied { open } for path="/dev/__properties__/u:object_r:lab_prop:s0" dev="tmpfs" ino=9703 scontext=u:r:radio:s0 tcontext=u:object_r:lab_prop:s0 tclass=file permissive=1
  1. permissive 和 enforce

如上面的日志所示, 日志以 permissive=1结尾,permissive=1 表示device处于permissive模式, 操作会被执行,但是logcat会打印这些日志, 一般用来调试。

如果手机当前处于enforce模式,则日志会以permissive=0结尾,日志里面描述的操作不会被执行,调试添加selinux权限后,需要在enforce模式下验证结果。

  1. setenforce 和 getenforce

setenforce 0 设置device进入 enforce模式。
setenforce 1 设置device进入 permissive模式。
getenforce 查看当前处于何种模式。

device-name:/system/bin # getenforce
getenforce
Permissive
device-name:/system/bin # setenforce 1
setenforce 1
device-name:/system/bin # getenforce
getenforce
Enforcing
device-name:/system/bin #
  1. file context

在 android代码中对可执行文件/一般文件/app/sysfs 定义了file context, 即它们的selinux域,

#from http://androidxref.com/8.1.0_r33/xref/device/google/dragon/sepolicy/file_contexts

# block devices
/dev/block/platform/700b0600\.sdhci/by-name/UDA       u:object_r:userdata_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/CAC       u:object_r:cache_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/MD1       u:object_r:metadata_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/APP       u:object_r:system_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/PST       u:object_r:frp_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/recovery  u:object_r:recovery_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/KERN-A    u:object_r:boot_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/KERN-B    u:object_r:boot_block_device:s0
/dev/block/platform/700b0600\.sdhci/by-name/MSC       u:object_r:misc_block_device:s0
/dev/block/zram0                                      u:object_r:swap_block_device:s0
/dev/block/mmcblk0rpmb                                u:object_r:rpmb_block_device:s0

# /dev
/dev/dri/card0                                  u:object_r:gpu_device:s0
/dev/dri/controlD64                             u:object_r:gpu_device:s0

# system executables
/system/bin/init_regions\.sh                    u:object_r:locale_exec:s0
/system/bin/tune-thermal-gov\.sh                u:object_r:thermal_gov_exec:s0
/system/bin/dump_bq25892\.sh                    u:object_r:dump_bq25892_exec:s0
/system/bin/touchfwup\.sh                       u:object_r:touch_fw_update_exec:s0
/system/bin/rmi4update                          u:object_r:rmi4update_exec:s0
/system/bin/fwtool                              u:object_r:fwtool_exec:s0

# vendor executables
/vendor/bin/hw/android\.hardware\.dumpstate@1\.0-service.dragon      u:object_r:hal_dumpstate_impl_exec:s0

# logs for touch firmware update
/data/misc/touchfwup(/.*)?                      u:object_r:touch_fw_update_log_file:s0

selinux域里面又定义了selinux的权限,权限包含read/write/...。

#from http://androidxref.com/8.1.0_r33/xref/device/google/dragon/sepolicy/fwtool.te

# permissions for /system/bin/fwtool
type fwtool, domain, device_domain_deprecated;
type fwtool_exec, exec_type, file_type;

init_daemon_domain(fwtool)

# access /dev/mtd/*
allow fwtool mtd_device:dir search;
allow fwtool mtd_device:chr_file rw_file_perms;
  1. ls -Z

对于可执行文件, 通过 ls -Z 命令可以查看其selinux域。

device-name:/system/bin # ls -Z
u:object_r:system_file:s0           acpi 
u:object_r:adbd_exec:s0             adbd 
u:object_r:system_file:s0           am   
u:object_r:system_file:s0           app_process
u:object_r:zygote_exec:s0           app_process32
u:object_r:system_file:s0           applypatch
u:object_r:system_file:s0           appops
u:object_r:system_file:s0           appwidget
u:object_r:system_file:s0           arping
u:object_r:system_file:s0           art  
u:object_r:atrace_exec:s0           atrace
u:object_r:audioserver_exec:s0      audioserver
u:object_r:system_file:s0           base64
u:object_r:system_file:s0           basename
u:object_r:system_file:s0           bcc  
u:object_r:blkid_exec:s0            blkid 
u:object_r:system_file:s0           blockdev
u:object_r:system_file:s0           bmgr 
u:object_r:bootanim_exec:s0         bootanimation
u:object_r:system_file:s0           bootctl
u:object_r:bootstat_exec:s0         bootstat
u:object_r:system_file:s0           brctl 
u:object_r:bt_logger_exec:s0        bt_logger
u:object_r:system_file:s0           bu   
u:object_r:system_file:s0           bunzip2
u:object_r:system_file:s0           bzcat 
u:object_r:system_file:s0           bzip2 
u:object_r:system_file:s0           cal  
u:object_r:system_file:s0           cat  
u:object_r:system_file:s0           chcon 
u:object_r:system_file:s0           chgrp 
u:object_r:system_file:s0           chmod 
u:object_r:system_file:s0           chown 
u:object_r:system_file:s0           chroot
u:object_r:system_file:s0           chrt 
...

那么问题来了,源码编译可执行文件的时候, 没有对这个可执行文件定义selinux域,然后添加了可执行文件的selinux域,编译boot.img(selinux域是放在boot.img里面的,而可执行文件一般放在system.img或者vendor.img),把boot.img更新到手机里面(这种情况即更新了selinux域的定义,但是没有更新可执行文件), 此时,更新的selinux域不会在这个可执行文件上面生效。
我记得当时在集成一个feature的时候犯了这个错误。

  1. audit2allow

面对logcat日志里面一对selinux异常,挨个写allow语句太麻烦了,可以试试audit2allow工具, 具体用法请咨询百度。

相关文章

网友评论

      本文标题:android selinux

      本文链接:https://www.haomeiwen.com/subject/amatiftx.html