android-selinux 原理之三

基礎(chǔ)知識(shí)都已經(jīng)學(xué)習(xí)完了,但是還不知道怎么樣,下面從不同的場(chǎng)景,實(shí)現(xiàn)了幾個(gè)例子,可以參考學(xué)習(xí)一下

對(duì)于/extern/sepolicy的修改用如下方法編譯:

  1. mmm external/sepolicy
  2. make sepolicy
    不過(guò)對(duì)于mtk的android系統(tǒng),不建議修改external/sepolicy,而是修改device/mediatek/common/sepolicy
    在policy目錄下,make relabel可更新或創(chuàng)建標(biāo)識(shí)映射

一. 添加一個(gè)系統(tǒng)服務(wù)的權(quán)限聲明

情景:定義一個(gè)init啟動(dòng)的service,demo_service,對(duì)應(yīng)的執(zhí)行文件是/system/bin/demo.

(1) 創(chuàng)建一個(gè)demo.te在/device/mediatke/common/sepolicy 目錄下
如果是Android4.4的源碼,在/device/mediatke/common/BoardConfig.mk 的BOARD_SEPOLICY_UNION 宏中新增demo.te
如果是Android5.0以上的編譯會(huì)自動(dòng)包含了整個(gè)文件夾里的文件,則不用在BoardConfig.mk中添加文件聲明
(2) 在demo.te中添加:demo的域(domain)類型定義
type demo, domain;
(3) 在demo.te中添加:demo的可執(zhí)行文件(客體)的類型定義
type demo_exec, exec_type
(4) 在demo.te中添加:init啟動(dòng)service時(shí)類型轉(zhuǎn)換聲明,直接用一個(gè)宏,主要是用于把demo_exec(客體)轉(zhuǎn)換成demo(進(jìn)程域) 
init_daemon_domain(demo)
(5) 綁定執(zhí)行檔 file_contexts 類型(安全上下文),由這個(gè)可執(zhí)行文件啟動(dòng)起來(lái)的進(jìn)程都是demo域里的
/system/bin/demo u:object_r:demo_exec:s0
(6) 根據(jù)demo需要訪問(wèn)的文件以及設(shè)備,定義其它的權(quán)限在demo.te中.

下面所有的宏都在/external/sepolicy/te_macros文件里定義
init_daemon_domain:

init_daemon_domain宏的作用是:設(shè)置init轉(zhuǎn)換到daemon域,可執(zhí)行文件是xxx_exec
上面的例子是這樣子使用的:init_daemon_domain(demo)
把$1=demo代換進(jìn)去,相當(dāng)于執(zhí)行下面兩條語(yǔ)句
domain_auto_trans(init, demo_exec, demo)
聲明tmpfs的讀寫和轉(zhuǎn)換權(quán)限,tmpfs是一些內(nèi)存臨時(shí)文件
tmpfs_domain(demo)

domain_auto_trans:

domain_auto_trans的作用是:定義舊的域轉(zhuǎn)換成新的域的聲明,直接把上面的參數(shù)代入到domain_auto_trans里相當(dāng)于執(zhí)行
#這個(gè)宏應(yīng)該就是上一篇文章里提到的域轉(zhuǎn)換的權(quán)限聲明
domain_trans(init,demo_exec,demo)
#聲明init域執(zhí)行demo_exec可執(zhí)行文件,新的域轉(zhuǎn)換成demo域
type_transition init demo_exec:process demo;

二. 添加一個(gè)進(jìn)程服務(wù)對(duì)內(nèi)核文件節(jié)點(diǎn)的讀寫權(quán)限

情景:一個(gè)域(服務(wù)或者進(jìn)程)需要訪問(wèn)一個(gè)專屬的char device /dev/demo
(1) 定義device 類型,創(chuàng)建一個(gè)device.te文件
type demo_device dev_type;
(2) 綁定demo device的上下文,在file_contexts
/dev/demo u:object_r:demo_device:s0
(3) 聲明demo進(jìn)行 使用demo device 的權(quán)限 在demo.te
allow demo demo_device:chr_file rw_file_perms;

rw_file_perms是一個(gè)集合的宏定義

define(`r_file_perms', `{ getattr open read ioctl lock }')
define(`w_file_perms', `{ open append write }')
define(`rw_file_perms', `{ r_file_perms w_file_perms }')

三. 添加一個(gè)Socket訪問(wèn)權(quán)限
這個(gè)類似于文件的權(quán)限聲明
情景:一個(gè)native service 通過(guò)init 創(chuàng)建一個(gè)socket 并綁定在 /dev/socket/demo,并且允許某些process 訪問(wèn).

(1) 定義socket 的類型 file.te
type demo_socket,file_type;
(2) 綁定socket 的類型 file_contexts
/dev/socket/demo_socket u:object_r:demo_socket:s0
(3) 允許所有的process 訪問(wèn).
# allow app connectto & write
unix_socket_connect(appdomain,demo,demo)

四. 添加一個(gè)進(jìn)程服務(wù)對(duì)一個(gè)屬性的訪問(wèn)權(quán)限

情景:允許一個(gè)進(jìn)程域?qū)σ粋€(gè)屬性sys.powerctl進(jìn)行設(shè)置

#聲明一個(gè)property_type類型powerctl_prop
type powerctl_prop, property_type;
#設(shè)置屬性的上下文,把屬性sys.powerctl的上下文設(shè)置成powerctl_prop類型
sys.powerctl u:object_r:powerctl_prop:s0
#允許adbd進(jìn)程對(duì)powerctl_prop上下文的屬性進(jìn)行設(shè)置
#set_prop里面做了兩個(gè)動(dòng)作,允許進(jìn)行對(duì)property的socket進(jìn)行連接和寫入,允許進(jìn)程設(shè)置屬性
set_prop(adbd, powerctl_prop)

五. 添加一個(gè)服務(wù)的binder,對(duì)外提供服務(wù)

type surfaceflinger_service, service_manager_type;
type surfaceflinger, domain;
SurfaceFlinger u:object_r:surfaceflinger_service:s0
type surfaceflinger_exec, exec_type, file_type;
/system/bin/surfaceflinger u:object_r:surfaceflinger_exec:s0
#允許surfaceflinger讀,寫,調(diào)用Binder IPC
binder_use(surfaceflinger)
#允許surfaceflinger訪問(wèn),調(diào)用binderservicedomain的Binder IPC
binder_call(surfaceflinger, binderservicedomain)
binder_call(surfaceflinger, appdomain)
binder_call(surfaceflinger, bootanim)
#將surfaceflinger與binder service的屬性域binderservicedomain相連
binder_service(surfaceflinger)

六. 添加一個(gè)APP的節(jié)點(diǎn)訪問(wèn)權(quán)限

定義節(jié)點(diǎn)類型
device/mediatek/common/sepolicy/file.te
type demo_file, fs_type,sysfs_type;
定義文件上下文
device/mediatek/common/sepolicy/file_contexts
/dev/demo_file u:object_r:demo_file:s0
定義App權(quán)限
ps -Z查看應(yīng)用權(quán)限上下文,如Setting是u:r:system_app:s0
rw_file_perms是一組權(quán)限的集合,包含讀與寫權(quán)限
device/mediatek/common/sepolicy/system_app.te
allow system_app demo_file:file rw_file_perms;
定義服務(wù)權(quán)限,rw_file_perms是權(quán)限集,包含讀寫權(quán)限
device/mediatek/common/sepolicy/system_server.te
allow system_server demo_file:file rw_file_perms;

七. 添加一個(gè)APP的節(jié)點(diǎn)訪問(wèn)權(quán)限2

定義文件類型
device/mediatek/common/sepolicy/file.te
type proc_mtk_demo, fs_type;
定義虛擬文件系統(tǒng)的文件安全上下文
device/mediatek/common/sepolicy/genfs_contexts
genfscon proc /mtk_demo/current_demo u:object_r:proc_mtk_demo:s0
給予shell的進(jìn)程讀寫權(quán)限,由/system/bin/sh啟動(dòng)的命令會(huì)與shell有同樣的進(jìn)程權(quán)限域
device/mediatek/common/sepolicy/shell.te
allow shell proc_mtk_demo:file {open read write getattr};
APP的讀取權(quán)限
device/mediatek/common/sepolicy/system_app.te
allow system_app proc_mtk_demo:file rw_file_perms;

使用方法

App使用方法之一:
String[] cmd = {
"/system/bin/sh", "-c", "echo " + st + " > " + "/proc/mtk_demo/current_demo",
};
Runtime.getRuntime().exec(cmd);
App使用方法之二,可以直接使用Java File來(lái)讀寫該文件:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容