SElinux 读懂.te 定义自己的 .te

后面会持续补充

一、 .te 文件定义中的一些宏

1.1 unix_socket_connect( 1, 2, $3 )
这个其实是一个宏。它定义在 te_macros(android系统,mtk 和 qcom 下面都有) 的文件里面的:

         #####################################  android 系统 te_macros 文件中的定义
     # unix_socket_connect(clientdomain, socket, serverdomain)
     # Allow a local socket connection from clientdomain via
     # socket to serverdomain.
     #
     # Note: If you see denial records that distill to the
     # following allow rules:
     # allow clientdomain property_socket:sock_file write;
     # allow clientdomain init:unix_stream_socket connectto;
     # allow clientdomain something_prop:property_service set;
     #
     # This sequence is indicative of attempting to set a property.
     # use set_prop(sourcedomain, targetproperty)
     #
     define(`unix_socket_connect', `
     allow $1 $2_socket:sock_file write;
     allow $1 $3:unix_stream_socket connectto;
    ')


    #####################################  平台下 te_macros 的定义,(各有不同)
    # qmux_socket(clientdomain)
    # Allow client domain to connecto and send
    # via a local socket to the qmux domain.
    # Also allow the client domain to remove
    # its own socket.
    define(`qmux_socket', `
    allow $1 qmuxd_socket:dir create_dir_perms;
    unix_socket_connect($1, qmuxd, qmuxd)
    allow $1 qmuxd_socket:sock_file { read getattr write setattr create unlink };
    ')

    #####################################
    # netmgr_socket(clientdomain)
    # Allow client domain to connecto and send
    # via a local socket to the netmgrd domain.
    # Also allow the client domain to remove
    # its own socket.
    define(`netmgr_socket', `
    allow $1 netmgrd_socket:dir r_dir_perms;
    unix_socket_connect($1, netmgrd, netmgrd)
    allow $1 netmgrd_socket:sock_file { read getattr write };
    ')

1.2 init_daemon_domain($1)

#####################################  android 系统 te_macros 文件中的定义
    # init_daemon_domain(domain)
    # Set up a transition from init to the daemon domain
    # upon executing its binary.
    define(`init_daemon_domain', `
    domain_auto_trans(init, $1_exec, $1)
    tmpfs_domain($1)
    ')

1.3 appdomain app_domain($1)

  #####################################android 系统 te_macros 文件中的定义
    # app_domain(domain)
    # Allow a base set of permissions required for all apps.
    define(`app_domain', `
    typeattribute $1 appdomain;
    # Label ashmem objects with our own unique type.
    tmpfs_domain($1)
    # Map with PROT_EXEC.
    allow $1 $1_tmpfs:file execute;
    ')

二 、定义自己的 .te

2.1
这个就很简单了,照猫画虎, 我们可以查看 sepolicy 下的很多 .te文件进行查看
在 qcom 或者 mtk 的 sepolicy 下 创建文件

backup_service.te

文件头就照着其他文件拿过来,修改名称,定义我们自己的 type backup_service
说简单点,就是 我们自己定义一个进程类型, backup_service他属于域 domain,
然后下面我就们可以 去定义它的权限,允许他干什么, 不允许他干什么

# backup_service
type backup_service, domain;
type backup_service_exec, exec_type, file_type;

# Make transition from init to backup service domain
init_daemon_domain(backup_service)
unix_socket_connect(backup_service, property, init)


# 允许文件操作
allow .....

# 允许文件夹相关操作
allow ....

.....

当然我们现在仅仅定义一个这个 .te 文件只是初步,还运作不起来,
就跟我们写了一个 java代码 , int a=0 一样, 后面会文章会结合。

2.2 self
策略语言保留了一个关键字self,它用于AV规则中的目标区域,可以当做一个类型使用,如下面这两条规则是相等的:

  # 这两条规则是相等的   
  allow user_t user_t : process signal;   
  allow user_t self : process signal;  
------------------------------------------
    # 这两条规则   
    allow user_t user_t : process signal;  
    allow staff_t staff_t : process signal;   

    #等于下面这一条规则   
    allow {user_t staff_t} self : process signal;  

注意:你可能只会在AV规则的目标区域使用特殊类型self,特别要注意的是不能在AV规则的源区域使用self类型,另外,也不能声明一个类型或属性标识符叫做self。

allow domain domain : process signal; # 每个进程都能向它自己和其它进程发送signal  
allow domain self : process signal;   # 每个进程都能向它自己发送signal 

2.5 make bootimage
编译候,可以查看自己的规则是否编译进去,可以查看文件 android/out/target/product/项目名称/obj/ETC/sepolicy_intermediates/ 下的 policy.conf
这个里面可以查看到是否编译进去了,比如你 定义了一个type什么名称,在文件里搜索,查看能否看到。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值