android selinux-170821

SELinux常見(jiàn)問(wèn)題-sepolicy問(wèn)題確認(rèn)

如果懷疑是sepolicy權(quán)限導(dǎo)致的問(wèn)題,首先需要確認(rèn)是否真的是權(quán)限導(dǎo)致的。

確認(rèn)方法是:關(guān)閉SELinux,查看問(wèn)題是否存在。

關(guān)閉SELinux的方法:

1、運(yùn)行時(shí)關(guān)閉,重啟失效:

adb shell setenforce 0

2、開(kāi)機(jī)階段關(guān)閉:

1)修改kernel cmdline:

androidboot.selinux=permissive

2)修改init代碼:

selinux_is_enforcing函數(shù)返回false

SELinux常見(jiàn)問(wèn)題-denied log

在kernel log、event log或main log中,搜索denied 關(guān)鍵字:

08-19 14:34:40.044160 1 1 E [ 27.054100].(1)[1:init]init: avc: denied { set } for property=sys.oppo.reboot pid=1608 uid=10013 gid=10013 scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:system_prop:s0 tclass=property_service permissive=0

1、查看denied信息,顯示platform_app domain無(wú)法對(duì)安全上下文u:object_r:system_prop:s0的屬性進(jìn)行設(shè)置操作;

2、在system/sepolicy/中找到platform_app domain的定義,一般在platform_app.te中即(domain名字).te中,查看是否有對(duì)應(yīng)的neverallow以及是否與要添加的權(quán)限沖突;

3、查看vendor/oppo/sepolicy/common/目錄是否有platform_app.te文件,如沒(méi)有則新建該文件新建文件的命名規(guī)則是:以scontext的內(nèi)容為名,添加對(duì)應(yīng)的權(quán)限。

a)allow platform_app system_prop:property_service { set };

b)system_prop由文件安全上下文獲取,property_service由tclass獲取,對(duì)應(yīng)的操作就不用說(shuō)了。這里提一點(diǎn),在system/sepolicy/te_macros中定義了相關(guān)操作的宏,可按需要查看。

4、remake system/sepolicy后再$OUT/obj/ ETC/sepolicy_intermediates/policy.conf文件中保存了臨時(shí)生成的安全策略,可查看自己的修改是否已經(jīng)生效。

SELinux常見(jiàn)問(wèn)題-mls

有時(shí)發(fā)現(xiàn)如下的denied log:

07-13 10:03:38.042 5587 5587 W ndroid.incallui: type=1400 audit(0.0:4215): avc: denied { add_name } for name="Recordings" scontext=u:r:platform_app:s0:c512,c768 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=0

在配置 allow platform_app system_data_file:dir add_name; 后,沒(méi)有生效,這時(shí)候就是Mutil-Level Security(MLS)機(jī)制導(dǎo)致的操作被denied了。

配置:

type platform_app, domain, mlstrustedsubject; 即可

SELinux常見(jiàn)問(wèn)題-ioctl

08-07 10:49:20.149 4795 4795 I iwpriv : type=1400 audit(0.0:3457): avc: denied { ioctl } for path="socket:[1607798]" dev="sockfs" ino=1607798 ioctlcmd=8bfc scontext=u:r:system_app:s0 tcontext=u:r:system_app:s0 tclass=udp_socket permissive=1

在setenfoce 0和配置allow system_app self:udp_socket ioctl; 后操作依然被denied,這是由于ioctl的控制在底層劃分的更細(xì),需要允許對(duì)應(yīng)ioctlcmd操作。

具體方法為:

1、查找對(duì)應(yīng)的ioctlcmd在ioctl_defines中的定義,如上文中的8bfc,對(duì)應(yīng)的是SIOCIWFIRSTPRIV_1C

2、在對(duì)應(yīng)的文件加入如下的配置:

allowxperm system_app self:udp_socket ioctl SIOCIWFIRSTPRIV_1C;

這樣,在ioctl操作時(shí),對(duì)應(yīng)的ioctlcmd就會(huì)被允許了。

SELinux常見(jiàn)問(wèn)題-新增app類(lèi)型

當(dāng)前系統(tǒng)中app的domain和文件類(lèi)型默認(rèn)的分配原則為:

1、platform簽名 + share systemuid -> system_app + system_app_data_file;

2、platform簽名 + 普通用戶-> platform_app + app_data_file;

3、platform簽名 + 特定用戶 : 如bluetooth,對(duì)應(yīng)的domain為bluetooth + bluetooth_data_file

4、三方應(yīng)用 -> untrusted_app + app_data_file

根據(jù)特性需求,需要新增app類(lèi)型:

1、system/sepolicy/mac_permissions.xml

2、seapp_contexts

user=_app seinfo=theme name=com.nearme.themespace* domain=theme_app type=theme_app_data_file levelFrom=user

3、file.te

type theme_app_data_file, file_type, data_file_type;

4、theme_app.te

type theme_app, domain, mlstrustedsubject;

這樣,簽名是@THEME(定義在system/sepolicy/keys.conf文件)和包名是comthemespace的應(yīng)用,domain是theme_app,對(duì)應(yīng)的文件類(lèi)型是theme_app_data_file

SELinux常見(jiàn)問(wèn)題-新增、修改文件類(lèi)型

如某些需求,需要新增文件類(lèi)型,只允許特定的domain訪問(wèn),如存放字體文件的目錄,只允許主題應(yīng)用寫(xiě)入和刪除,其他domain只有讀的權(quán)限。

1、file_contexts

/data/system/font(/.*)? u:object_r:font_data_file:s0

2、file.te

type font_data_file, file_type, data_file_type;

3、system/core/rootdir/init.rc

在restorecon_recursive /data之前加入:

mkdir /data/system/font 0777 system system

chmod 0777 /data/system/font

這樣,adb shell ls -l -Z /data/system/font 查看到的就是上面配置的文件類(lèi)型:

-rwxrwxrwx 1 u0_a18 u0_a18 u:object_r:font_data_file:s0:c512,c768 3553188 2017-08-19 16:40 Regular.ttf

SELinux常見(jiàn)問(wèn)題-新增可執(zhí)行程序和service

在/system/bin下新增可執(zhí)行程序,以關(guān)鍵log的bin為例

1、file_contexts

/system/bin/criticallog u:object_r:criticallog_exec:s0

2、critical.te

type criticallog, domain;

type criticallog_exec, exec_type, file_type;

3、system/core/rootdir/init.rc

service criticallog /system/bin/criticallog

這樣,init在啟動(dòng)這個(gè)criticallog service時(shí),會(huì)根據(jù)對(duì)應(yīng)的exec類(lèi)型設(shè)置進(jìn)程的domain,可以通過(guò) adb shell ps –Z | grep criticallog 來(lái)確認(rèn):

u:r:criticallog:s0 system 484 1 17484 4220 binder_thr 72b6e2eaa8 S /system/bin/criticallog

SELinux常見(jiàn)問(wèn)題-新增service、無(wú)對(duì)應(yīng)的bin

在init.rc中新增service,但沒(méi)有對(duì)應(yīng)的bin,一般通過(guò)sh腳本啟動(dòng)一些功能。這類(lèi)service,需要在配置rc時(shí),加上seclabel操作,之后,init啟動(dòng)service時(shí),就會(huì)根據(jù)這個(gè)關(guān)鍵字解析出對(duì)應(yīng)的context。以screen_record service為例

1、system/core/rootdir/init.rc

service screen_record /system/bin/sh /autochmod.sh "screen_record”

seclabel u:r:screen_record:s0

2、screen_record.te

type screen_record, domain;

type screen_record_exec, exec_type, file_type;

init_daemon_domain(screen_record)

binder_use(screen_record)

allow screen_record shell_exec:file {read open execute_no_trans execute getattr entrypoint};

allow screen_record rootfs:lnk_file {getattr};

allow screen_record rootfs:file {read open};

allow screen_record system_file:file { read open execute_no_trans };

allow screen_record toolbox_exec:file { read open execute_no_trans execute getattr};

allow screen_record self:capability dac_override;

allow screen_record self:process execmem;

如上,新增service需要的默認(rèn)權(quán)限在步驟2中,需要的時(shí)候拷貝、修改screen_record為對(duì)應(yīng)的service即可。

SELinux常見(jiàn)問(wèn)題-sepolicy-inject工具

常常有這樣的困擾:selinux權(quán)限要加的太多,每次提示一條,都需要重新打包,很浪費(fèi)時(shí)間!

現(xiàn)在有了sepolicy-inject工具,可以動(dòng)態(tài)添加配置,不需要重復(fù)打包。

sepolicy-inject -s? -t? -c? -p [,,,...] [-P ] [-o

以下的denied信息,就可以用sepolicy-inject工具動(dòng)態(tài)配置好,最終打包一次就可以了。

01-01 08:01:57.902000 353 353 I auditd : type=1400 audit(0.0:2476): avc: denied { getattr } for comm="servicemanager" scontext=u:r:servicemanager:s0 tcontext=u:r:write_on:s0 tclass=process permissive=1

sepolicy-inject –s servicemanager –t write_on –c process –p getattr -l

由于權(quán)限的機(jī)制是有一個(gè)訪問(wèn)被阻止了,整個(gè)進(jìn)程都會(huì)推出,因此每次可能只打印出一條denied信息,為了一次打印出盡量多的信息,可以adb shell setenforce 0之后執(zhí)行,可以看到denied信息中最后permissive=1,只打印信息,不會(huì)阻止。這樣就可以盡量多的得到denied信息了。

最終,所有sepolicy-inject動(dòng)態(tài)添加的allow配置,都會(huì)記錄在/data/seinfo.txt文件,配置完成后,將這個(gè)文件的內(nèi)容復(fù)制到對(duì)應(yīng)的TE文件即可。

] [-l|--load]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容