我是靠谱客的博主 活力电源,最近开发中收集的这篇文章主要介绍SeLinux权限配置心得总结 ,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

1、编译命令:
make selinux_policy
或者
cd system/sepolicy
mm

2、编译后生成策略文件的路径:
需要替换验证的文件,替换后要修改读写执行权限跟之前一样:
outtargetproductplatformrootplat_*
outtargetproductplatformrootnonplat_*
outtargetproductplatformvendoretcselinuxprecompiled_sepolicy
outtargetproductplatformvendoretcselinuxprecompiled_sepolicy.plat_and_mapping.sha256
outtargetproductplatformvendoretcselinuxnonplat_sepolicy.cil
具体操作如下:
adb root
adb remount
adb push plat_service_contexts /
adb push plat_seapp_contexts /
adb push plat_seapp_contexts /
adb push plat_hwservice_contexts /
adb push plat_file_contexts /

adb push nonplat_service_contexts /
adb push nonplat_seapp_contexts /
adb push nonplat_property_contexts /
adb push nonplat_hwservice_contexts /
adb push nonplat_file_contexts /

adb push precompiled_sepolicy /vendor/etc/selinux
adb push precompiled_sepolicy.plat_and_mapping.sha256 /vendor/etc/selinux
adb push nonplat_sepolicy.cil /vendor/etc/selinux

adb push sepolicy /
adb shell chmod -R 644 /plat_*
adb shell chmod -R 644 /sepolicy
adb shell chmod -R 644 /nonplat_*
adb shell chmod -R 644 /vendor/etc/selinux/precompiled_sepolicy
adb shell chmod -R 644 /vendor/etc/selinux/precompiled_sepolicy.plat_and_mapping.sha256
adb shell chmod -R 644 /vendor/etc/selinux/nonplat_sepolicy.cil
adb shell sync
adb reboot

生成所有allow语句的策略文件:out/target/product/g6sa/obj/ETC/nonplat_sepolicy.cil_intermediates/nonplat_policy.conf

3、临时修改安全上下文:chcon u:object_r:sec_ota_data_file:s0 sn_num.prop

4、使配置的文件生效:restorecon -Rv 文件/目录, eg:
restorecon -Rv /data_fota

5、在init.rc中配置文件生效:
restorecon_recursive /dev/socket/RVC_IPC
在对应的init.te文件中添加:
allow init socket_device:sock_file relabelfrom; 
socket_device是RVC_IPC当前的安全上下文

elabelfrom    从现有类型改变安全上下文        SELinux特定许可
relabelto        改变新类型的安全上下文        SELinux特定许可

6、临时打开/关闭selinux权限:setenforce 1 //1:打开, 0:关闭

7、查看selinux的权限:getenforce  //Enforcing: 打开 Permissive:关闭

8、挂载分区时配置安全上下文:
mount  /dev/block/mmcblk0p21 /data_fota -o rw,default,context=u:object_r:sv_db_file:s0
或者
mount  /dev/block/platform/soc/ee140000.sd/by-name/fotadata /data_fota -o rw,context=u:object_r:sv_db_file:s0

9、查看某个文件的安全上下文:ls -Z /dev/socket/RVC_IPC

10、查看某个进程的安全上下文:ps -efZ | grep com.baidu.car.radio

11、针对dir缺少的任何权限,建议赋予create_dir_perms,基本涵盖对dir的所有权限,比如:
{ open search write read rename create rmdir getattr }等等。
针对file缺少的任何权限,建议赋予rwx_file_perms,基本涵盖对file的所有权限,比如:
包含{ open read write open execute getattr create ioctl }等等。

12、根据log信息自动生成te文件,log中只有avc相关的打印:
external/selinux/prebuilts/bin/audit2allow -i abc.txt > abc.te

13、给file添加所有权限:
allow system_app sv_db_file:file { rw_file_perms create setattr relabelfrom relabelto unlink link rename execute quotaon mounton };
file具体定义如下:
system/sepolicy/prebuilts/api/26.0/private/access_vectors
common file
{
    ioctl
    read
    write
    create
    getattr
    setattr
    lock
    relabelfrom
    relabelto
    append
    map
    unlink
    link
    rename
    execute
    quotaon
    mounton
}

14、给dir添加所有的权限
allow system_app sv_db_file:dir { rw_dir_perms create setattr relabelfrom relabelto unlink link rename execute quotaon mounton reparent rmdir audit_access execmod };
dir具体定义如下:
system/sepolicy/prebuilts/api/26.0/private/access_vectors
class dir
inherits file
{
    add_name
    remove_name
    reparent
    search
    rmdir
    open
    audit_access
    execmod
}

15、那么第三方APP,在SElinux下,如何获得对一个内核节点的访问权限, 分下面三个步骤:
a.在 system/sepolicy/public/file_contexts 文件中添加: 
/dev/fm1388 u:object_r:fm1388_device:s0 
其中“fm1388”是我的设备节点名称,可自行更改,“fm1388_device”是自定义设备名

b.在 system/sepolicy/public/device.te 文件中添加: 
type fm1388_device, dev_type, mlstrustedobject; 
其中“fm1388_device”需要跟上面自定义的设备名一致 
 注: 
mlstrustedsubject:这一attribute包含了所有能越过MLS检查的主体domain。 
mlstrustedobject:这一attribute包含了所有能越过MLS检查的客体type。

c.在 system/sepolicy/public/untrusted_app.te 文件中添加: 
allow untrusted_app fm1388_device:chr_file rw_file_perms; 
修改完后重新编译系统,最好将设备节点的权限也改为777(chmod 777 /dev/fm1388) 
 如果不想每次重启都需要修改权限,在 /system/core/rootdir 目录下的 ueventd.rc 文件添加修改权限即可

16、添加权限后的neverallowed冲突
实例
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};
修改Sepolicy后出现“Error While Expanding policy”
在系统添加某个*.te或在te文件中添加某个selinux权限后,build会出现如下error:
genfscon proc /driver/thermal u:object_r:proc_thermal:s0
libsepol.report_failure: neverallow on line 429 of system/sepolicy/private/app.te (or line 21317 of policy.conf) violated by allow system_app system_file:file { write };
libsepol.report_failure: neverallow on line 406 of system/sepolicy/public/domain.te (or line 8484 of policy.conf) violated by allow system_app system_file:file { write };
libsepol.check_assertions: 2 neverallow failures occurred
Error while expanding policy 

这是因为在/system/sepolicy/private/app.te和system/sepolicy/public/domain.te文件中添加了一些neverallow rules,导致编译检查的时候出现错误。
neverallow appdomain system_file:dir_file_class_set {create write setattr relabelfrom relabelto append unlink link rename};
只需要在上面的规则中去掉添加的allow xx system_file:file { write };中的xx,具体方式是在nerverallow中用{}里用-xx排除某个,即不需要有此规则:
neverallow {appdomain -system_app} system_file:dir_file_class_set {create write setattr relabelfrom relabelto append unlink link rename};

17、策略文件类型定义:
   a、te文件
      给某个进程定义一个特定的domain,然后这个进程所有操作需要的权限都在这个te文件中定义:
      foo.te,foo为damain名称
      
   b、file_contexts:
      给某个文件设置domain,所有试图访问这个文件的进程都需要有对这个domain的访问权限才能
   成功操作,否则将会返回失败
   /system/bin/foo   u:object_r:foo_exec:s0

   c、genfs_contexts:
      给内存以及不支持xattr扩展的文件系统中的文件设置domain
      genfscon fuseblk    / u:object_r:fuseblk:s0
   
   d、property_contexts:
      给system property设置相应的domain
      foo. u:object_r:foo_prop:s0
      
   e、service_contexts:
      给某个service设置domain
      fooservice    u:object_r:foo_service:s0

   f、hwservice_contexts:
      给某个hal service设置domain
      android.hardware.foo::IFoo    u:object_r:hal_foo_hwservice:s0
   
   g、seapp_contexts:
      给具有某个uid的app设置相应的domain:
      user=foo domain=platform_app type=app_data_file levelFrom=user
      将uid为foo的apk设置为platform_app

18、配置最简单的SELinux domain:
  a、定义一个te文件foo.te,内容如下:
      type foo, domain;
      type foo_exec, exec_type, file_type;     
      init_daemon_domain(foo);
     特别说明:init_daemon_domain是在system/sepolicy/public/te_macros里面定义的
   b、给可执行程序foo配置domain,在file_contexts文件中添加:
      /system/bin/foo   u:object_r:foo_exec:s0

19、配置私有文件访问  
   程序往往会创建一些私有的文件供自己存储数据,可以按如下步骤添加私有文件配置:
   a、在共用目录中创建一个自己的私有目录/data/foo
  
   b、file.te中添加新的文件类型domain:
      type foo_data_file, file_type, data_file_type, core_data_file_type;

   c、将foo目录设置为对应的domain,在file_contexts文件中添加:
      /data/foo(/.*)?   u:object_r:foo_data_file:s0

   d、配置目录访问权限foo.te:
      #配置/data/foo文件权限
      allow foo system_data_file:dir create_dir_perms;
      type_transition foo system_data_file:dir foo_data_file;
      #配置/data/foo/*文件权限
      allow foo foo_data_file:dir create_dir_perms;
      allow foo foo_data_file:file create_file_perms;

20、添加新的service 
   a、添加service domain, service.te:
      type foo_service, service_manager_type;
  
   b、将service名称对应到新domain上, service_contexts:
      fooservice    u:object_r:foo_service:s0;

   c、添加往servicemanager中新加service的权限, foo.te:
      type foo, domain;
      type foo_exec, exec_type, file_type;     
      init_daemon_domain(foo);
      binder_call(foo, binderservicedomain);
      binder_call(foo, appdomain);
      binder_use(foo);
      binder_service(foo);
      add_service(foo, foo_service);

21、策略文件位置:
  a、android策略文件定义:
        system/sepolicy     
  b、BoardConfig.mk中定义新的sepolicy目录
      BOARD_SEPOLICY_DIRS += <root>/device/manufacturer/device-name/sepolicy
  c、内核中配置selinux开启/关闭模式:
     在Boardconfig.mk里面对BOARD_KERNEL_CMDLINE进行添加androidboot.selinux=permissive即     可关闭selinux
      关闭:androidboot.selinux=permissive
      开启:androidboot.selinux=enforcing

22、参考的网址:
https://www.jianshu.com/p/88a92d101532
https://www.jianshu.com/p/e95cd0c17adc
https://blog.csdn.net/Innost/article/details/19299937#/notebooks/29547787/notes/_blank
//类型跟属性的关系,有时候找不到类型可以用相关联的属性代替
https://blog.csdn.net/u014135607/article/details/78309481?utm_medium=distribute.pc_relevant.none-task-blog-title-7&spm=1001.2101.3001.4242
//SELinux策略语言--客体类别和许可
https://www.bbsmax.com/A/q4zVkoBXJK/

     
      

最后

以上就是活力电源为你收集整理的SeLinux权限配置心得总结 的全部内容,希望文章能够帮你解决SeLinux权限配置心得总结 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部