SElinux 读懂.te 定义自己的 .te【转】

本文转载自:https://blog.csdn.net/kongbaidepao/article/details/61417291

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

1.1 unix_socket_connect(1,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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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;
    ')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二 、定义自己的 .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 ....

.....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

当然我们现在仅仅定义一个这个 .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;  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

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

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

posted @ 2018-07-23 10:31  请给我倒杯茶  阅读(3523)  评论(0编辑  收藏  举报