android7.1增加一个开机自启动的bin应用遇到的权限问题
android7.1增加一個開機自啟動的bin應用遇到的權限問題
?
1.????增加開機自啟動的bin應用
1.1??增加的源代碼
新建external\study,編譯生成study,編譯方式有兩種
(1)??mmm external/study
(2)??make systemimage,在 device/qcom/common/base.mk下增加LIBCAMERA += study
?
1.2??增加開機自啟動study
device/qcom/msm8937_64/init.target.rc的
on boot
…
service study /system/bin/study
???class main
???user? root
oneshot
第1個study是service名稱,第2個study是可執行應用名稱。
?
1.3??增加study的權限
(1)??device/qcom/sepolicy/common/file_contexts下增加study
# System files
…
stem/bin/study?????????????????????? u:object_r: study_exec:s0
file_contexts文件保存系統中所有文件的安全上下文定義,每行前半部分是文件的路徑,后面是它的安全上下文的定義(study_exec)。
?
安全上下文的格式:USER:ROLE:TYPE[LEVEL],在安全上下文規則中最主要的定義是type,這里是study_exec。TYPE是定義主體和客體所屬的類型,對于進程而言,它的類型也稱為domian。
?
(2)??在device/qcom/sepolicy/common/或是其他sepolicy下增加study.te文件。
Te文件屬于類型強制規則文件(TypeEnforcement),它主要由類型定義和規則定義兩部分組成
?
#定義了study和study_exec兩種類型,study用在進程的安全上下文中,study_exec用在文件的安全上下文中。屬性domain表示域,屬性是android預先在system\sepolicy\attributes中定義好,當然我們也可以定義,比如在device\qcom\sepolicy\common\attributes中。
?
#無論是主題還是客體的類型定義,都是通過type語句來完成,通常主體的type具有domian屬性,因此,我們也把主體的type稱為domain,將domain設置為study的屬性,表明zygote是用來描述進程的安全上下文的。
type study, domain;
#表明類型study_exec具有屬性exec_type和file_type,即它是用來描述文件的安全上下文的
type study_exec, exec_type, file_type;
?
init_daemon_domain(study)
allow study study:capability dac_override;
allow study rootfs:lnk_file { read getattr};
allow study system_data_file:dir {opengetattr read write add_name};
allow study system_data_file:file {opengetattr create read write};
allow study shell_exec:file {read openexecute execute_no_trans rx_file_perms};
allow study toolbox_exec:file {getattrexecute read open execute_no_trans};
這些內容是根據avc denied的log增加的。
?
1)????init_daemon_domain(study)
程執行一個type為zygote_exec的文件時,將該子進程的domain設置為zygote,而不是繼承父進程的domain。并且給與zygote這個domain,所有定義在tmpfs_domain宏中的權限。
# init_daemon_domain(domain) # Set up a transition from init to thedaemon domain # upon executing its binary. define(`init_daemon_domain', ` domain_auto_trans(init, $1_exec, $1) tmpfs_domain($1) ')init_daemon_domain(study)是一個宏,聲明當一個domain為init的進程創建一個子進程執行一個type為study_exec的文件時,將該子進程的domain設置為study,而不是繼承父進程的domain。并且給與study這個domain,所有定義在tmpfs_domain宏中的權限。
?
2)????allow語句
比如allow study system_data_file:file {opengetattr create read write};
rule_name source_type target_type : classperm_set
?
source_type:通常是某種屬性為domain的類型(type),代表主體。
Target_type(目標類型):允許訪問的客體的類型,目標類型可以同時指定多個,
Class(客體類別):允許訪問的客戶的目標類型可能會涵蓋比較廣的范圍,客體類別可以對客體目標類型進行限制和明確化,例如這里目標類型是system_data_file可以代表文件(file)、目錄(dir)及鏈接(lnk_file),通過file對它進行了限制,因此,在這條規則中只代表文件。
?
增加的內容需要編譯生成新的boot.img和生成study,其中增加的study.te文件在make bootimage編譯的時候生成在out\target\product\msm8937_64\root\sepolicy中,而file_contexts文件匯總生成為out\target\product\msm8937_64\root\file_contexts.bin,file_contexts.bin文件,file_contexts.bin 和 file_contexts可互轉,見鏈接:
https://blog.cofface.com/archives/2255.html
?
sepolicy可用二進制工具bless查找里面的內容。
?
2.????adb方式更新boot.img和study---OK
用fastboot燒錄boot.img和adb push study到/system/bin下驗證正常。
通過ls -z study方式得到結果:
u:object_r:study_exec:s0system/bin/study
?
3.????升級包update.zip方式更新boot.img和study---權限問題
更新后提示init: Service study does not have aSELinux domain defined.
通過ls -z study方式得到結果:
u:object_r:system_file:s0 system/bin/study
可見用adb的方式study的類型是study_exec,升級包的方式是system_file,為什么是system_file呢?見文件system\sepolicy\file.te下面的內容
# Default type for anything under /system.
type system_file, file_type;
?
為了解決init: Service study does not have aSELinux domain defined.問題,需要在升級包的META-INF\com\google\android\updater-script文件增加下面的內容:
set_metadata("/system/bin/study","uid", 0, "gid", 2000, "mode", 0755,"capabilities", 0x0, "selabel", "u:object_r:study_exec:s0");
總結
以上是生活随笔為你收集整理的android7.1增加一个开机自启动的bin应用遇到的权限问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (android)system ui 内
- 下一篇: Android Studio MAT内存