android selinux

關(guān)鍵字: android, selinux, getenforce, setenforce, audit2allow

20180817 tjy

轉(zhuǎn)載請注明出處

Android在4.3引入selinux, 當(dāng)時工作上需要了解并解決一些selinux的問題, 這里總結(jié)一下涉及到的selinux的一些東西,不是普及性的文章, 只是記錄和穿針引線的作用。

  1. 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
  1. permissive 和 enforce

如上面的日志所示, 日志以 permissive=1結(jié)尾,permissive=1 表示device處于permissive模式, 操作會被執(zhí)行,但是logcat會打印這些日志, 一般用來調(diào)試。

如果手機(jī)當(dāng)前處于enforce模式,則日志會以permissive=0結(jié)尾,日志里面描述的操作不會被執(zhí)行,調(diào)試添加selinux權(quán)限后,需要在enforce模式下驗證結(jié)果。

  1. setenforce 和 getenforce

setenforce 0 設(shè)置device進(jìn)入 enforce模式。
setenforce 1 設(shè)置device進(jìn)入 permissive模式。
getenforce 查看當(dāng)前處于何種模式。

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代碼中對可執(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;
  1. 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更新到手機(jī)里面(這種情況即更新了selinux域的定義,但是沒有更新可執(zhí)行文件), 此時,更新的selinux域不會在這個可執(zhí)行文件上面生效。
我記得當(dāng)時在集成一個feature的時候犯了這個錯誤。

  1. audit2allow

面對logcat日志里面一對selinux異常,挨個寫allow語句太麻煩了,可以試試audit2allow工具, 具體用法請咨詢百度。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容