[置顶] Android的IPC访问控制设计与实现
3.3.1 IPC鉤子函數(shù)設(shè)計(jì)與實(shí)現(xiàn)
IPC Binder是Android最重要的進(jìn)程間通信機(jī)制,因此,必須在此實(shí)施強(qiáng)制訪問(wèn)控制。
1. 修改secuirty.h
打開終端shell,輸入指令“cd /android4.0/kernel/goldfish/include/linux/vim security.h”,找到結(jié)構(gòu)security_operations,加入函數(shù)指針變量,如下所示:
/*
* This is the main security structure.
*/
struct security_operations {
?? charname[SECURITY_NAME_MAX + 1];
#ifdef HAVE_SMACK
?? /*
?? ?* to add a binder hook
?? ?* */
?? int(*binder_transaction) (struct task_struct *from, struct task_struct *to);
#endif
隨后在security_operations定義結(jié)尾后,加入函數(shù)聲明,如下:
#ifdef HAVE_SMACK
int security_binder_transaction(structtask_struct *from, struct task_struct *to);
#endif
在LSM鉤子函數(shù)實(shí)現(xiàn)處加入security_binder_transaction的定義,如下:
#ifdef HAVE_SMACK
static inline intsecurity_binder_transaction(struct task_struct *from, struct task_struct *to) {
???return 0;
}
#endif
2. 修改security.c
打開終端shell,輸入指令“cd /android4.0/kernel/goldfish/security/vim security.c”,加入函數(shù),如下所示:
#ifdefHAVE_SMACK
intsecurity_binder_transaction(struct task_struct *from, struct task_struct *to) {
??? returnsecurity_ops->binder_transaction(from, to);
}
#endif
3. 修改smack_lsm.c
打開終端shell,輸入指令“cd /android4.0/kernel/goldfish/security/smack/vim smack_lsm.c”,加入函數(shù)“smack_binder_transaction”,如下所示:
#ifdefHAVE_SMACK
/*
?* smack_binder_transaction - to check bindertransaction between two tasks
?* */
static intsmack_binder_transaction(struct task_struct *from, struct task_struct *to) {
??? int rc1, rc2;
??? /*
???? * ask the two task must have writepermission to each other
???? * */
??? rc1 = smk_access(task_security(from),task_security(to), MAY_WRITE);
??? rc2 = smk_access(task_security(to),task_security(from), MAY_WRITE);
?
??? return rc1 == 0 && rc2 == 0 ? 0:1;
}
#endif
此鉤子函數(shù)用來(lái)判斷源進(jìn)程from和目標(biāo)進(jìn)程to之間有沒有互相寫權(quán)限。最后在結(jié)構(gòu)體security_operations smack_ops新增成員變量如下:
structsecurity_operations smack_ops = {
?? .name =????????????????????? "smack",
#ifdefHAVE_SMACK
?? .binder_transaction = smack_binder_transaction,
#endif
4. 重新編譯模擬器內(nèi)核
編譯Android內(nèi)核方法已經(jīng)在第二章有所闡述,這里不再敘述。
3.3.2 ?
每個(gè)進(jìn)程分為用戶空間和內(nèi)核空間兩部分,不同進(jìn)程的用戶空間是無(wú)法共享的,進(jìn)程內(nèi)核空間通過(guò)Android Binder來(lái)實(shí)現(xiàn)IPC。Binder驅(qū)動(dòng)代碼位于“/android4.0/kernel/goldfish/driver/staging/android/bind.c”文件中,其中binder_transaction函數(shù)使用binder_transaction_data結(jié)構(gòu)體的數(shù)據(jù)來(lái)執(zhí)行Binder尋址、復(fù)制Binder IPC數(shù)據(jù)、生成及檢索Binder節(jié)點(diǎn)等操作。打開終端shell,輸入指令“cd /host/android4.0/kernel/goldfish/drivers/staging/android/vim binder.c”,找到該函數(shù)的定義,如下
static voidbinder_transaction(struct binder_proc *proc, struct binder_thread *thread, struct binder_transaction_data *tr, int reply);
在源進(jìn)程和目標(biāo)進(jìn)程確定后,加入代碼,如下:
?????? if(security_binder_transaction(proc->tsk,target_proc->tsk)) {
?????? ??? return_error = BR_FAILED_REPLY;
?????? ??? goto err_invalid_target_handle;
?????? }
其中,target_proc->tsk指向目標(biāo)進(jìn)程的task_struct,proc->tsk指向源進(jìn)程的task_struct,加入security_task_movememory用來(lái)判斷當(dāng)前進(jìn)程對(duì)源進(jìn)程有沒有寫權(quán)限,security_binder_transaction用來(lái)判斷源進(jìn)程對(duì)目標(biāo)進(jìn)程有沒有寫權(quán)限,這兩個(gè)函數(shù)均為L(zhǎng)SM鉤子函數(shù),由于內(nèi)核已經(jīng)裝載了smack模塊,因此它們是指向了smack內(nèi)核的smack_task_movememory和smack_binder_transaction。加入上述代碼的目的是為了防止進(jìn)程不經(jīng)授權(quán)濫用IPC Binder進(jìn)行通信,正如下圖所示:
?
如上圖所示,服務(wù)客戶端通過(guò)Binder調(diào)用Service Server的foo函數(shù),服務(wù)客戶端將Binder IPC數(shù)據(jù)通過(guò)BinderDriver傳遞給Service Server,Binder Driver是源進(jìn)程和目標(biāo)進(jìn)程通信的媒介,IPC檢查機(jī)制就是在Binder Driver中檢查源進(jìn)程和目標(biāo)進(jìn)程之間有沒有互相“寫”的權(quán)限。在進(jìn)程的安全標(biāo)簽不是“_”的前提下,使用Smack可以實(shí)現(xiàn)對(duì)進(jìn)程的控制。例如上層應(yīng)用如果要想實(shí)現(xiàn)發(fā)短信的功能,與上層應(yīng)用所對(duì)應(yīng)的Linux進(jìn)程是在BinderDriver中與radio進(jìn)程進(jìn)行互相通信,如果smack規(guī)則否定了上層應(yīng)用對(duì)radio“寫”權(quán)限,那么上層應(yīng)用不能實(shí)現(xiàn)發(fā)短信的功能。再如,上層應(yīng)用要想實(shí)現(xiàn)訪問(wèn)通訊錄的目的,上層應(yīng)用也是在Binder Driver中與通訊錄進(jìn)程進(jìn)行通信,如果smack規(guī)則容許上層應(yīng)用對(duì)通訊錄進(jìn)程有“寫”的權(quán)限,那么上層應(yīng)用才可以訪問(wèn)通訊錄。惡意軟件可能繞過(guò)Android框架層的權(quán)限檢查機(jī)制,但它們繞不過(guò)內(nèi)核的IPC檢查。
轉(zhuǎn)載于:https://www.cnblogs.com/pangblog/p/3265265.html
總結(jié)
以上是生活随笔為你收集整理的[置顶] Android的IPC访问控制设计与实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 北京环球影城优速通几点开售
- 下一篇: 米其林的轮胎多少钱啊?