zoukankan      html  css  js  c++  java
  • SELinux权限学习笔记

    1. 权限修改

    方法1: adb在线修改seLinux

    $ getenforce;     //获取当前seLinux状态,Enforcing(表示已打开),Permissive(表示已关闭)
    $ setenforce 1;   //打开seLinux
    $ setenforce 0;   //关闭seLinux

    方法2: 从kernel中彻底关闭

    修改LINUX/android/kernel/arch/arm64/configs/xxx_defconfig文件, 去掉CONFIG_SECURITY_SELINUX=y 的配置项

    方法3: sepolicy中添加权限

    修改依据: 通过指令cat /proc/kmsg | grep denied,或者kernel的Log中定位到标志性log。

    修改步骤: 找相应的源类型.te文件,此文件可能的存放路径 (其中源类型见下方的标志性log格式) :

    LINUX/android/external/sepolicy
    LINUX/android/device/qcom/sepolicy/common

    标志性log 格式:

    avc: denied  { 操作权限  }  for pid=7201  comm=“进程名”  scontext=u:r:源类型:s0  tcontext=u:r:目标类型:s0  tclass=访问类型 permissive=0

    在相应源类型.te文件,添加如下格式的一行语句:(结尾别忘了分号)

    格式:allow  源类型 目标类型:访问类型 {操作权限};

    实例:

    Kernel Log:

    avc: denied {getattr read} for pid=7201 comm="xxx.xxx" scontext=u:r:system_app:s0 tcontext=u:r:shell_data_file:s0 tclass=dir permissive=0

    修改方案

    在system_app.te文件中,添加下面语句:

    allow system_app shell_data_file:dir{getattr read};

    scontext: start context,发起方
    tcontext:target context, 接受方

    若发起方访问接受方没有权限,一般权限配置在发起方,但是由权限接受方模块负责人配置。

    2. 将selinux报错信息转换为配置

    # audit2allow -i selicx.txt

    selicx.txt中存放的是“avc: denied”报错打印,audit2allow不但会给出权限配置,而且还会给出配置的位置。

    3.audit2why工具

    ~/tmp/selinux$ cat se_error.txt 
    avc: denied { write } for comm="init" name="iostats" dev="sysfs" ino=67139 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0
    avc: denied { write } for comm="init" name="scheduler" dev="sysfs" ino=67122 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0
    ~/tmp/selinux$ 
    ~/tmp/selinux$ audit2why -i se_error.txt 
    avc: denied { write } for comm="init" name="scheduler" dev="sysfs" ino=67122 scontext=u:r:init:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0
    
            Was caused by:
                    Missing type enforcement (TE) allow rule.
    
                    You can use audit2allow to generate a loadable module to allow this access.

    解释avc报错原因。

    4.查看文件的selinux属性

    /sys/class/wakeup # ls -la -Z
    total 0
    drwxr-xr-x   2 root root u:object_r:sysfs_wakeup:s0  0 1970-01-01 08:00 .  //selinux配置使用sysfs_wakeup
    drwxr-xr-x 112 root root u:object_r:sysfs:s0         0 1970-01-01 08:00 .. //selinux配置使用sysfs
    lrwxrwxrwx   1 root root u:object_r:sysfs:s0         0 2020-08-28 20:04 wakeup0 -> ../../devices/platform/.../c440000.qcom,spmi:qcom,pm8150@0:qcom,power-on@800/wakeup/wakeup0

    5.file_context文件是设置节点或文件的selinux属性类型的,.te文件是设置访问权限的。通俗的理解,file_context是定义属性的类型,.te文件是使用属性类型。

    6.看selinux报的是哪个文件的权限错误

    As an example , 
    In the following path ino , 82228 / 69931 / 49391 
    08-03 00:22:47.555 684 684 W Binder:684_2: type=1400 audit(0.0:3709): avc: denied { read } for name="wakeup58" dev="sysfs" ino=82228 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0 
    08-03 00:22:47.555 684 684 W Binder:684_2: type=1400 audit(0.0:3710): avc: denied { read } for name="wakeup48" dev="sysfs" ino=69931 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0 
    
    We have to see which path its pointing to usingfind /sys -inum <number that is showing in the denials >this will change in each run where running with the right number will give you the path it was trying for . 

    举例:

    log:
    01-01 00:00:53.725  1373  1373 W vendor-oplus-ha: type=1400 audit(0.0:1866): avc: denied { read } for name="u:object_r:system_prop:s0" dev="tmpfs" ino=13478 scontext=u:r:usage_hidl:s0 tcontext=u:object_r:system_prop:s0 tclass=file permissive=0
    130|OP4AF7:/ # find /sys/ -inum 13478
    /sys/firmware/devicetree/base/soc/xxx/port@4/endpoint/slave-mode

    7.se context 属性是 per inode, 链接的节点相当于新的节点了, 取决于是否对此节点是否赋予新的context. 

    TODO: 解释剩余命令的作用

    ~/tmp/selinux$ audi [tab]
    audispd audit2allow audit2why auditctl auditd

    TODO: 优秀博文

    https://blog.csdn.net/yanjun821126/article/details/80828908
    https://blog.csdn.net/shichaog/article/details/53728893
    https://blog.csdn.net/xct841990555/article/details/82714322
    https://blog.csdn.net/w2064004678/article/details/105515244/

    selinux属性模糊配置:
    https://blog.csdn.net/iaMay_____/article/details/80344592

    参考:

    SELinux 权限问题: http://gityuan.com/2015/06/13/SEAndroid-permission/

  • 相关阅读:
    【深度学习Deep Learning】资料大全
    在谷歌中缓存下载视频离线观看,js代码
    asp.net mvc 中Html.ValidationSummary显示html
    asp.net网站访问时不能显示页面
    k8s install kubeadm网络原因访问不了谷哥and gpg: no valid OpenPGP data found. 解决办法
    火绒杀毒软件更安静
    Linux使用mount挂载Windows共享文件夹
    spark学习
    https://blog.csdn.net/tangdong3415/article/details/53432166
    正则表达式
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/12264337.html
Copyright © 2011-2022 走看看