android selinux-170821

SELinux常见问题-sepolicy问题确认

如果怀疑是sepolicy权限导致的问题,首先需要确认是否真的是权限导致的。

确认方法是:关闭SELinux,查看问题是否存在。

关闭SELinux的方法:

1、运行时关闭,重启失效:

adb shell setenforce 0

2、开机阶段关闭:

1)修改kernel cmdline:

androidboot.selinux=permissive

2)修改init代码:

selinux_is_enforcing函数返回false

SELinux常见问题-denied log

在kernel log、event log或main log中,搜索denied 关键字:

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无法对安全上下文u:object_r:system_prop:s0的属性进行设置操作;

2、在system/sepolicy/中找到platform_app domain的定义,一般在platform_app.te中即(domain名字).te中,查看是否有对应的neverallow以及是否与要添加的权限冲突;

3、查看vendor/oppo/sepolicy/common/目录是否有platform_app.te文件,如没有则新建该文件新建文件的命名规则是:以scontext的内容为名,添加对应的权限。

a)allow platform_app system_prop:property_service { set };

b)system_prop由文件安全上下文获取,property_service由tclass获取,对应的操作就不用说了。这里提一点,在system/sepolicy/te_macros中定义了相关操作的宏,可按需要查看。

4、remake system/sepolicy后再$OUT/obj/ ETC/sepolicy_intermediates/policy.conf文件中保存了临时生成的安全策略,可查看自己的修改是否已经生效。

SELinux常见问题-mls

有时发现如下的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; 后,没有生效,这时候就是Mutil-Level Security(MLS)机制导致的操作被denied了。

配置:

type platform_app, domain, mlstrustedsubject; 即可

SELinux常见问题-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的控制在底层划分的更细,需要允许对应ioctlcmd操作。

具体方法为:

1、查找对应的ioctlcmd在ioctl_defines中的定义,如上文中的8bfc,对应的是SIOCIWFIRSTPRIV_1C

2、在对应的文件加入如下的配置:

allowxperm system_app self:udp_socket ioctl SIOCIWFIRSTPRIV_1C;

这样,在ioctl操作时,对应的ioctlcmd就会被允许了。

SELinux常见问题-新增app类型

当前系统中app的domain和文件类型默认的分配原则为:

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

2、platform签名 + 普通用户-> platform_app + app_data_file;

3、platform签名 + 特定用户 : 如bluetooth,对应的domain为bluetooth + bluetooth_data_file

4、三方应用 -> untrusted_app + app_data_file

根据特性需求,需要新增app类型:

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的应用,domain是theme_app,对应的文件类型是theme_app_data_file

SELinux常见问题-新增、修改文件类型

如某些需求,需要新增文件类型,只允许特定的domain访问,如存放字体文件的目录,只允许主题应用写入和删除,其他domain只有读的权限。

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 查看到的就是上面配置的文件类型:

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

SELinux常见问题-新增可执行程序和service

在/system/bin下新增可执行程序,以关键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在启动这个criticallog service时,会根据对应的exec类型设置进程的domain,可以通过 adb shell ps –Z | grep criticallog 来确认:

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

SELinux常见问题-新增service、无对应的bin

在init.rc中新增service,但没有对应的bin,一般通过sh脚本启动一些功能。这类service,需要在配置rc时,加上seclabel操作,之后,init启动service时,就会根据这个关键字解析出对应的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需要的默认权限在步骤2中,需要的时候拷贝、修改screen_record为对应的service即可。

SELinux常见问题-sepolicy-inject工具

常常有这样的困扰:selinux权限要加的太多,每次提示一条,都需要重新打包,很浪费时间!

现在有了sepolicy-inject工具,可以动态添加配置,不需要重复打包。

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

以下的denied信息,就可以用sepolicy-inject工具动态配置好,最终打包一次就可以了。

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

由于权限的机制是有一个访问被阻止了,整个进程都会推出,因此每次可能只打印出一条denied信息,为了一次打印出尽量多的信息,可以adb shell setenforce 0之后执行,可以看到denied信息中最后permissive=1,只打印信息,不会阻止。这样就可以尽量多的得到denied信息了。

最终,所有sepolicy-inject动态添加的allow配置,都会记录在/data/seinfo.txt文件,配置完成后,将这个文件的内容复制到对应的TE文件即可。

] [-l|--load]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 175,490评论 5 419
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 74,060评论 2 335
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 124,407评论 0 291
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 47,741评论 0 248
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 56,543评论 3 329
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 43,040评论 1 246
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 34,107评论 3 358
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 32,646评论 0 229
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 36,694评论 1 271
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 32,398评论 2 279
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 33,987评论 1 288
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 30,097评论 3 285
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 35,298评论 3 282
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 27,278评论 0 14
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 28,413评论 1 232
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 38,397评论 2 309
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 38,099评论 2 314

推荐阅读更多精彩内容