關(guān)鍵字: android, selinux, getenforce, setenforce, audit2allow
20180817 tjy
轉(zhuǎn)載請注明出處
Android在4.3引入selinux, 當時工作上需要了解并解決一些selinux的問題, 這里總結(jié)一下涉及到的selinux的一些東西腻豌,不是普及性的文章焰檩, 只是記錄和穿針引線的作用。
- logcat日志
如果某些可執(zhí)行文件或者app或者文件訪問的selinux沒有設(shè)置好国旷, 會在logcat中打印一些類似的日志, 這個時候需要對這些異常添加selinux權(quán)限了。
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
- permissive 和 enforce
如上面的日志所示, 日志以 permissive=1結(jié)尾版姑,permissive=1 表示device處于permissive模式谎亩, 操作會被執(zhí)行,但是logcat會打印這些日志夭拌, 一般用來調(diào)試桶现。
如果手機當前處于enforce模式,則日志會以permissive=0結(jié)尾,日志里面描述的操作不會被執(zhí)行绵脯,調(diào)試添加selinux權(quán)限后份帐,需要在enforce模式下驗證結(jié)果噩凹。
- setenforce 和 getenforce
setenforce 0 設(shè)置device進入 enforce模式。
setenforce 1 設(shè)置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 #
- file context
在 android代碼中對可執(zhí)行文件/一般文件/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的權(quán)限,權(quán)限包含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;
- ls -Z
對于可執(zhí)行文件, 通過 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
...
那么問題來了,源碼編譯可執(zhí)行文件的時候贴铜, 沒有對這個可執(zhí)行文件定義selinux域轩褐,然后添加了可執(zhí)行文件的selinux域,編譯boot.img(selinux域是放在boot.img里面的,而可執(zhí)行文件一般放在system.img或者vendor.img),把boot.img更新到手機里面(這種情況即更新了selinux域的定義蓄髓,但是沒有更新可執(zhí)行文件)匾竿, 此時昵慌,更新的selinux域不會在這個可執(zhí)行文件上面生效淳蔼。
我記得當時在集成一個feature的時候犯了這個錯誤存皂。
- audit2allow
面對logcat日志里面一對selinux異常娩怎,挨個寫allow語句太麻煩了,可以試試audit2allow工具, 具體用法請咨詢百度袍啡。