SELinux 权限

如何根据log 信息修改SELinux 权限

当出现 SELinux 权限不足时,可根据LOG 信息提示进行相应的修改。下面介绍如何根据 kernel log 来查看信息

1. disable selinux

在终端下

su
setenforce 0

该操作主要是为了禁用系统SELinux,让权限暂时通过。但是依然是会输出 SELinux 校验时的LOG。后面根据输出的LOG 进行相应的处理。

2. 查看kernel log

复现问题,导出 kernel log.

或者 adb shell su 之后输入 cat /dev/kmsg | grep avc 在控制台输出关键信息

kernel 日志的输出在 /dev/kmsg 节点下,avcSELinux 打印的信息

如:

5,4978,791423968,-;audit: type=1400 audit(1556096333.959:26): avc:  denied  { write } for  pid=5106 comm="Log_LogStrategy" name="Logs_Collector" dev="mmcblk0p40" ino=16321 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4979,791424019,-;audit: type=1400 audit(1556096333.959:27): avc:  denied  { add_name } for  pid=5106 comm="Log_LogStrategy" name="amefrktoukgokiph_20190424165853.zip" scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4980,791424176,-;audit: type=1400 audit(1556096333.959:28): avc:  denied  { create } for  pid=5106 comm="Log_LogStrategy" name="amefrktoukgokiph_20190424165853.zip" scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4981,791425437,-;audit: type=1400 audit(1556096333.959:29): avc:  denied  { write } for  pid=5106 comm="Log_LogStrategy" path="/data/Logs_Collector/amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4982,797691545,-;audit: type=1400 audit(1556096340.229:30): avc:  denied  { remove_name } for  pid=5086 comm="xtc.systemagent" name="amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
5,4983,797691614,-;audit: type=1400 audit(1556096340.229:31): avc:  denied  { rename } for  pid=5086 comm="xtc.systemagent" name="amefrktoukgokiph_20190424165853.zip" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1
5,4984,797693717,-;audit: type=1400 audit(1556096340.229:32): avc:  denied  { unlink } for  pid=5086 comm="xtc.systemagent" name="1556096340234" dev="mmcblk0p40" ino=16331 scontext=u:r:system_app:s0 tcontext=u:object_r:system_data_file:s0 tclass=file permissive=1

3. 修改te 文件

  • 3.1 找到对应的domain

如:

scontext=u:r:system_app:s0

domainsystem_app, 所以需要修改 system/sepolicy/system_app.te 文件

  • 3.2 找到 target 和 Object Class

如:

tcontext=u:object_r:system_data_file:s0 tclass=dir

targetsystem_data_file, object classdir

  • 3.3 找到缺失的权限

如:

denied  { add_name }

缺失的权限为: add_name

  • 3.4 最后整合整个缺失的权限,统一处理

最后针对上面的权限提示修改的 system/sepolicy/system_app.te 为,增加如下内容:

allow system_app system_data_file:dir { add_name remove_name rw_dir_perms };
allow system_app system_data_file:file { create rename unlink rw_file_perms };

格式(分号结尾)

allow scontext tcontext:tclass { perm };

4. 检查是否违反了 neverallow

可以先进行尝试编译,若有违反 neverallowSELinux 定义,会编译不通过,同时会提示哪个 te 文件中定义的 neverallow 冲突了。

如上面的修改会导致 system/sepolicy/app.te 文件中的 neverallow 规则有冲突。

原先冲突的 nerverallow 定义为:

# Write to system-owned parts of /data.
# This is the default type for anything under /data not otherwise
# specified in file_contexts.  Define a different type for portions
# that should be writable by apps.
neverallow { appdomain} system_data_file:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };

这里定义了不允许 system_data_filetarget 进行 create write unlink 等操作

此时解决方法是,需要过滤去除掉我们需要的 domain

进行修改如下:

neverallow { appdomain -system_app }
    system_data_file:dir_file_class_set
    { create write setattr relabelfrom relabelto append unlink link rename };

加上 -system_app 的排除语法。

5. 重编系统

至此根据 kernel log 信息提取权限完成,重新编译系统。

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

推荐阅读更多精彩内容