SELinux/SEAndroid 實例簡述(三)實例看SELinux/SEAndroid

  1. /***********************************
  2. * Author:劉江明
  3. * Environment:MTK Android 6.0
  4. * Date:2016年11月06日
  5. ***********************************/

基礎知識都已經學習完了,但是還不知道怎麼樣,下面從不同的場景,實現了幾個例子,可以參考學習一下

對於/extern/sepolicy的修改用如下方法編譯:

  1. 1. mmm external/sepolicy
  2. 2. make bootimg
不過對於mtk的android系統,不建議修改external/sepolicy,而是修改device/mediatek/common/sepolicy
在policy目錄下,make relabel可更新或創建標識映射

一. 添加一個系統服務的權限聲明
情景:定義一個init啓動的service,demo_service,對應的執行文件是/system/bin/demo.
  1. (1) 創建一個demo.te在/device/mediatke/common/sepolicy 目錄下
  2. 如果是Android4.4的源碼,在/device/mediatke/common/BoardConfig.mk BOARD_SEPOLICY_UNION 宏中新增demo.te
  3. 如果是Android5.0以上的編譯會自動包含了整個文件夾裏的文件,則不用在BoardConfig.mk中添加文件聲明
  4. (2) demo.te中添加:demo的域(domain)類型定義
  5. type demo, domain;
  6. (3) demo.te中添加:demo的可執行文件(客體)的類型定義
  7. type demo_exec, exec_type
  8. (4) demo.te中添加:init啓動service時類型轉換聲明,直接用一個宏,主要是用於把demo_exec(客體)轉換成demo(進程域)
  9. init_daemon_domain(demo)
  10. (5) 綁定執行檔 file_contexts 類型(安全上下文),由這個可執行文件啓動起來的進程都是demo域裏的
  11. /system/bin/demo u:object_r:demo_exec:s0
  12. (6) 根據demo需要訪問的文件以及設備,定義其它的權限在demo.te中.
上面的例子最大的疑點就是init_daemon_domain這個宏,我們來看看
下面所有的宏都在/external/sepolicy/te_macros文件裏定義,具體的定義,請自行查看
init_daemon_domain:
  1. init_daemon_domain宏的作用是:設置init轉換到daemon域,可執行文件是xxx_exec
  2. 上面的例子是這樣子使用的:init_daemon_domain(demo)
  3. $1=demo代換進去,相當於執行下面兩條語句
  4. domain_auto_trans(init, demo_exec, demo)
  5. 聲明tmpfs的讀寫和轉換權限,tmpfs是一些內存臨時文件
  6. tmpfs_domain(demo)
domain_auto_trans:
  1. domain_auto_trans的作用是:定義舊的域轉換成新的域的聲明,直接把上面的參數代入到domain_auto_trans裏相當於執行
  2. #這個宏應該就是上一篇文章裏提到的域轉換的權限聲明
  3. domain_trans(init,demo_exec,demo)
  4. #聲明init域執行demo_exec可執行文件,新的域轉換成demo域
  5. type_transition init demo_exec:process demo;
已經大致完成了一個服務的權限聲明,但是還有第(6)點沒有完成,如果該服務需要對底層設備文件的讀寫,該如何聲明權限?

二. 添加一個進程服務對內核文件節點的讀寫權限
直接上例程
  1. 情景:一個域(服務或者進程)需要訪問一個專屬的char device /dev/demo
  2. (1) 定義device 類型,創建一個device.te文件
  3. type demo_device dev_type;
  4. (2) 綁定demo device的上下文,在file_contexts
  5. /dev/demo u:object_r:demo_device:s0
  6. (3) 聲明demo進行 使用demo device 的權限 在demo.te
  7. allow demo demo_device:chr_file rw_file_perms;
rw_file_perms是一個集合的宏定義
  1. define(`r_file_perms', `{ getattr open read ioctl lock }')
  2. define(`w_file_perms', `{ open append write }')
  3. define(`rw_file_perms', `{ r_file_perms w_file_perms }')

三. 添加一個Socket訪問權限
這個類似於文件的權限聲明
情景:一個native service 通過init 創建一個socket 並綁定在 /dev/socket/demo,並且允許某些process 訪問.
  1. (1) 定義socket 的類型 file.te
  2. type demo_socketfile_type;
  3. (2) 綁定socket 的類型 file_contexts
  4. /dev/socket/demo_socket u:object_r:demo_socket:s0
  5. (3) 允許所有的process 訪問.
  6. # allow app connectto & write
  7. unix_socket_connect(appdomaindemodemo)

四. 添加一個進程服務對一個屬性的訪問權限
情景:允許一個進程域對一個屬性sys.powerctl進行設置 
  1. #聲明一個property_type類型powerctl_prop
  2. type powerctl_prop, property_type;
  3. #設置屬性的上下文,把屬性sys.powerctl的上下文設置成powerctl_prop類型
  4. sys.powerctl u:object_r:powerctl_prop:s0
  5. #允許adbd進程對powerctl_prop上下文的屬性進行設置
  6. #set_prop裏面做了兩個動作,允許進行對property的socket進行連接和寫入,允許進程設置屬性
  7. set_prop(adbd, powerctl_prop)


五. 添加一個服務的binder,對外提供服務
  1. type surfaceflinger_service, service_manager_type;
  2. type surfaceflinger, domain;
  3. SurfaceFlinger u:object_r:surfaceflinger_service:s0
  4. type surfaceflinger_exec, exec_type, file_type;
  5. /system/bin/surfaceflinger u:object_r:surfaceflinger_exec:s0
  6. #允許surfaceflinger讀,寫,調用Binder IPC
  7. binder_use(surfaceflinger)
  8. #允許surfaceflinger訪問,調用binderservicedomain的Binder IPC
  9. binder_call(surfaceflinger, binderservicedomain)
  10. binder_call(surfaceflinger, appdomain)
  11. binder_call(surfaceflinger, bootanim)
  12. #將surfaceflinger與binder service的屬性域binderservicedomain相連
  13. binder_service(surfaceflinger)

六. 添加一個APP的節點訪問權限
  1. 定義節點類型
  2. device/mediatek/common/sepolicy/file.te
  3. type demo_file, fs_type,sysfs_type;
  4. 定義文件上下文
  5. device/mediatek/common/sepolicy/file_contexts
  6. /dev/demo_file u:object_r:demo_file:s0
  7. 定義App權限
  8. ps -Z查看應用權限上下文,如Settingu:r:system_app:s0
  9. rw_file_perms是一組權限的集合,包含讀與寫權限
  10. device/mediatek/common/sepolicy/system_app.te
  11. allow system_app demo_file:file rw_file_perms;
  12. 定義服務權限,rw_file_perms是權限集,包含讀寫權限
  13. device/mediatek/common/sepolicy/system_server.te
  14. allow system_server demo_file:file rw_file_perms;

七. 添加一個APP的節點訪問權限2
  1. 定義文件類型
  2. device/mediatek/common/sepolicy/file.te
  3. type proc_mtk_demo, fs_type;
  4. 定義虛擬文件系統的文件安全上下文
  5. device/mediatek/common/sepolicy/genfs_contexts
  6. genfscon proc /mtk_demo/current_demo u:object_r:proc_mtk_demo:s0
  7. 給予shell的進程讀寫權限,由/system/bin/sh啓動的命令會與shell有同樣的進程權限域
  8. device/mediatek/common/sepolicy/shell.te
  9. allow shell proc_mtk_demo:file {open read write getattr};
  10. APP的讀取權限
  11. device/mediatek/common/sepolicy/system_app.te
  12. allow system_app proc_mtk_demo:file rw_file_perms;
使用方法
  1. App使用方法之一:
  2. String[] cmd = {
  3. "/system/bin/sh", "-c", "echo " + st + " > " + "/proc/mtk_demo/current_demo",
  4. };
  5. Runtime.getRuntime().exec(cmd);
  6. App使用方法之二,可以直接使用Java File來讀寫該文件:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章