[置頂] Android的IPC訪問控制設計與實現

3.3.1 IPC鉤子函數設計與實現

IPC Binder是Android最重要的進程間通信機制,因此,必須在此實施強制訪問控制。

1. 修改secuirty.h

打開終端shell,輸入指令“cd /android4.0/kernel/goldfish/include/linux/vim security.h”,找到結構security_operations,加入函數指針變量,如下所示:

/*

* 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定義結尾后,加入函數聲明,如下:

#ifdef HAVE_SMACK

int security_binder_transaction(structtask_struct *from, struct task_struct *to);

#endif

在LSM鉤子函數實現處加入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”,加入函數,如下所示:

#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”,加入函數“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

此鉤子函數用來判斷源進程from和目標進程to之間有沒有互相寫權限。最后在結構體security_operations smack_ops新增成員變量如下:

structsecurity_operations smack_ops = {

?? .name =????????????????????? "smack",

#ifdefHAVE_SMACK

?? .binder_transaction = smack_binder_transaction,

#endif

4. 重新編譯模擬器內核

編譯Android內核方法已經在第二章有所闡述,這里不再敘述。

3.3.2 ?

每個進程分為用戶空間和內核空間兩部分,不同進程的用戶空間是無法共享的,進程內核空間通過Android Binder來實現IPC。Binder驅動代碼位于“/android4.0/kernel/goldfish/driver/staging/android/bind.c”文件中,其中binder_transaction函數使用binder_transaction_data結構體的數據來執行Binder尋址、復制Binder IPC數據、生成及檢索Binder節點等操作。打開終端shell,輸入指令“cd /host/android4.0/kernel/goldfish/drivers/staging/android/vim binder.c”,找到該函數的定義,如下

static voidbinder_transaction(struct binder_proc *proc, struct binder_thread *thread, struct binder_transaction_data *tr, int reply);

在源進程和目標進程確定后,加入代碼,如下:

?????? if(security_binder_transaction(proc->tsk,target_proc->tsk)) {

?????? ??? return_error = BR_FAILED_REPLY;

?????? ??? goto err_invalid_target_handle;

?????? }

其中,target_proc->tsk指向目標進程的task_struct,proc->tsk指向源進程的task_struct,加入security_task_movememory用來判斷當前進程對源進程有沒有寫權限,security_binder_transaction用來判斷源進程對目標進程有沒有寫權限,這兩個函數均為LSM鉤子函數,由于內核已經裝載了smack模塊,因此它們是指向了smack內核的smack_task_movememory和smack_binder_transaction。加入上述代碼的目的是為了防止進程不經授權濫用IPC Binder進行通信,正如下圖所示:


?

如上圖所示,服務客戶端通過Binder調用Service Server的foo函數,服務客戶端將Binder IPC數據通過BinderDriver傳遞給Service Server,Binder Driver是源進程和目標進程通信的媒介,IPC檢查機制就是在Binder Driver中檢查源進程和目標進程之間有沒有互相“寫”的權限。在進程的安全標簽不是“_”的前提下,使用Smack可以實現對進程的控制。例如上層應用如果要想實現發短信的功能,與上層應用所對應的Linux進程是在BinderDriver中與radio進程進行互相通信,如果smack規則否定了上層應用對radio“寫”權限,那么上層應用不能實現發短信的功能。再如,上層應用要想實現訪問通訊錄的目的,上層應用也是在Binder Driver中與通訊錄進程進行通信,如果smack規則容許上層應用對通訊錄進程有“寫”的權限,那么上層應用才可以訪問通訊錄。惡意軟件可能繞過Android框架層的權限檢查機制,但它們繞不過內核的IPC檢查。


轉載于:https://www.cnblogs.com/pangblog/p/3265265.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377949.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377949.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377949.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TensorFlow在Anaconda環境下創建

一、我使用的是Anaconda自帶的Jupyter編譯器,詳細的安裝教程可以參考博文 二、之后打開Jupyter 三、進行測試 我的tensorflow使用的是2.0版本 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()a tf.constant([1.0,2.0],name"a") b tf.co…

leetcode 654. 構造最大二叉樹 思考分析

題目 給定一個不含重復元素的整數數組。一個以此數組構建的最大二叉樹定義如下: 二叉樹的根是數組中的最大元素。 左子樹是通過數組中最大值左邊部分構造出的最大二叉樹。 右子樹是通過數組中最大值右邊部分構造出的最大二叉樹。 通過給定的數組構建最大二叉樹&am…

Memcache的命令以及狀態監控

輸入telnet 127.0.0.1 11211(memcached默認端口為11211) stats :使用stats命令查看當前memcache服務器的狀態 pidmemcache服務器的進程IDuptime服務器已經運行的秒數time服務器當前的unix時間戳versionmemcache版本pointer_size當前操作系統 …

flush python_帶有示例的Python File flush()方法

flush python文件flush()方法 (File flush() Method) flush() method is an inbuilt method in Python, it is used to clear/flush the internal buffer, it is best practice while working with fila handling in Python, the internal buffer can be cleared before writin…

c++ 請拋棄匈牙利命名法 - 變量命名代碼風格的建議。

我只針對c碼農們講,其他語言不了解不過應該大同小異。曾幾何時翻開21天學通c系列等腦殘入門書,都以匈牙利命名法示人(DWORD dwXXX, int nXXX, string strXXX)。現在我可以負責任的告訴你,把類型名寫在前面屁用都沒有,對…

Pycharm更換anaconda環境空間

一、File—>Settings 或者直接快捷鍵 CtrlAltS 二、找到自己的項目—>Project Interpreter—>找到需要使用的anaconda環境空間 三、Add Local 四、G:\Anaconda3\envs\mask_rcnn\python.exe一般anaconda的envs文件夾下,找到你的環境空間名稱,…

android 應用demo截圖

ksoap2實現天氣預報 Frame 動畫 baidu map 轉載于:https://www.cnblogs.com/java20130726/archive/2011/11/28/3218328.html

leetcode 617. 合并二叉樹 思考分析

題目 給定兩個二叉樹,想象當你將它們中的一個覆蓋到另一個上時,兩個二叉樹的一些節點便會重疊。 你需要將他們合并為一個新的二叉樹。合并的規則是如果兩個節點重疊,那么將他們的值相加作為節點合并后的新值,否則不為 NULL 的節點…

python 示例_帶有示例的Python File write()方法

python 示例文件write()方法 (File write() Method) write() method is an inbuilt method in Python, it is used to write the content in the file. write()方法是Python中的內置方法,用于將內容寫入文件中。 Syntax: 句法: file_object.write(text…

如何關掉Microsoft Office Click-to-Run服務

很煩,一開電腦就出現 一、打開任務管理器(CtrlShiftEsc) 服務—>打開服務 二、找到Microsoft Office Click-to-Run Service 右擊,選擇屬性 三、禁用即可

[2013-08-19] nohup的使用

前幾天自擺了一個烏龍。 由于項目中用到memcache;在linux機器上安裝了該服務后,啟動并且通過 & 設置到后臺進程; 由于要指定某些服務端口,然后發現經常服務被“莫名其妙”地關閉了。我以為是別人手動關掉了,或者說…

友盟—安卓巴士【Android開發原創教程大賽】

Android開發原創教程大賽正式拉開序幕了,由國內最專業的移動開發者服務及統計平臺-友盟提供豐厚的話費獎品哦,為所有愛寫教程的開發者提供的一份獎勵。 活動時間:2011年11月29日——2011年12月21日 征集期:2011年…

leetcode 700. 二叉搜索樹中的搜索 思考分析

目錄題目1、不考慮BST性質,直接遞歸遍歷2、回顧BST性質3、利用BST性質進行遍歷4、簡單的迭代方法題目 給定二叉搜索樹(BST)的根節點和一個值。 你需要在BST中找到節點值等于給定值的節點。 返回以該節點為根的子樹。 如果節點不存在&#xf…

一、環境準備

一、前提已安裝好anaconda 二、打開cmd命令窗口 conda activate激活base環境 conda create -n pytorch python3.6創建一個名稱為pytorch的環境空間,其中使用的python版本為3.6 conda env list查看當前都有哪些環境 conda activate pytorch 激活剛安裝的環境 pip l…

Java技能優化集錦

Java技能優化集錦 1 通用篇 “通用篇”討論的問題適合于大多數Java應用。 1.1 不用new關鍵詞創建類的實例 用new關鍵詞創建類的實例時,構造函數鏈中的所有構造函數都會被自動調用。但如果一個對象實現了Cloneable接口,我們可以調用它的clon…

java treemap_Java TreeMap size()方法與示例

java treemapTreeMap類的size()方法 (TreeMap Class size() method) size() method is available in java.util package. size()方法在java.util包中可用。 size() method is used to return the size of this TreeMap or in other words, we can say it returns the number of…

LeetCode 98. 驗證二叉搜索樹 思考分析

題目 給定一個二叉樹,判斷其是否是一個有效的二叉搜索樹。 假設一個二叉搜索樹具有如下特征: 節點的左子樹只包含小于當前節點的數。 節點的右子樹只包含大于當前節點的數。 所有左子樹和右子樹自身必須也是二叉搜索樹。 1、利用BST性質:中序…

二、VC++環境的安裝

一、打開PyCharm 在Terminal中激活pytorch環境,conda activate pytorch 安裝pycocotools工具,pip install pycocotools 二、報錯需安裝VC 進入官網 安裝完成之后重啟電腦 三、再次安裝pycocotools 打開Pycharm,安裝pycocotools工具&am…

Android 網絡狀態的監控

1 http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html 2 轉載于:https://www.cnblogs.com/bavariama/p/3268450.html

深入淺出jQuery——jQuery歷史

Dean Edwards編寫的Packer:http://dean.edwards.name/packer/轉載于:https://www.cnblogs.com/mingle/archive/2011/12/01/2271155.html