本文轉載自: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什么名稱,在文件里搜索,查看能否看到。