Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Binder IPC 的權限控制:clearCallingIdentity 與 restoreCallingIdentity 原理剖析

編輯:關於 Android 程式設計

讀 Android framework 原始碼時,你一定看過這兩個總是成對出現的方法:

// 清空遠端呼叫者的 uid 與 pid,改用本地行程的 uid/pid 取代
public static final native long clearCallingIdentity();

// 還原遠端呼叫者的 uid 與 pid,正好是上一個方法的反過程
public static final native void restoreCallingIdentity(long token);

它們看起來樸素,卻是 system_server 各項服務做權限管控的基礎。本文基於 Android 6.0 原始碼說明其原理與使用時機。

一、底層資料結構

每個線程都有一個獨立的 IPCThreadState 物件,其中記錄著目前線程的 pid 與 uid 兩個成員變數:

  • mCallingUid(簡稱 UID):保存 Binder IPC 呼叫方行程的 Uid。
  • mCallingPid(簡稱 PID):保存 Binder IPC 呼叫方行程的 Pid。

我們平時用 Binder.getCallingPid() 與 Binder.getCallingUid() 取用的,正是這兩個值。

二、實作原理

這兩個方法是 native 函式,透過 JNI 對應到位於 android_util_Binder.cpp 的實作,再轉進 IPCThreadState.cpp。

2.1 clearCallingIdentity

int64_t IPCThreadState::clearCallingIdentity() {
    int64_t token = ((int64_t)mCallingUid << 32) | mCallingPid;
    clearCaller();
    return token;
}

void IPCThreadState::clearCaller() {
    mCallingPid = getpid();   // 用當前行程 pid 覆蓋
    mCallingUid = getuid();   // 用當前行程 uid 覆蓋
}

關鍵在於位元運算:UID 與 PID 都是 32 位元的 int,這裡把 UID 左移 32 位當高半部、PID 放低半部,打包成一個 64 位元的 token 傳回給呼叫者保存。接著 clearCaller() 把 PID、UID 覆蓋成本地行程的值。

2.2 restoreCallingIdentity

static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token) {
    int uid = (int)(token >> 32);   // 右移 32 位取回 uid
    if (uid > 0 && uid < 999) {
        // Android 中不存在小於 999 的 uid,出現即視為異常
        jniThrowException(env, "java/lang/IllegalStateException", buf);
        return;
    }
    IPCThreadState::self()->restoreCallingIdentity(token);
}

void IPCThreadState::restoreCallingIdentity(int64_t token) {
    mCallingUid = (int)(token >> 32);
    mCallingPid = (int)token;
}

從 token 反解出 UID 與 PID 並寫回成員變數,正好是 clear 的逆運算。這裡還有一層防護:由於 Android 系統行程的 uid 都大於等於 1000,若解出的 uid 落在 0~999 之間,就代表 token 來源可疑,直接丟出 IllegalStateException。

三、為什麼需要這兩個方法

3.1 典型場景

考慮這樣的情境:線程 A 透過 Binder 遠端呼叫線程 B,而線程 B 在處理過程中又要呼叫自己這邊的另一個 Service 或 Activity。

逐步分析:

  1. A 呼叫 B 時:B 的 IPCThreadState 中,UID/PID 保存的是 A 的資訊。因此 B 可以在方法開頭呼叫 getCallingUid() 取得 A 的 uid,據此判斷「A 有沒有權限呼叫 B 的這個方法」。這是第一道權限檢查。
  2. B 呼叫自己的元件時:此時 B 是呼叫方,UID/PID 應該是 B 自己的值。所以必須先呼叫 clearCallingIdentity() 把它們覆蓋成本地的值,第二道權限檢查才能正確以 B 的身分進行。
  3. B 呼叫結束後:B 仍然身處 A 的呼叫上下文中,所以必須呼叫 restoreCallingIdentity() 把 UID/PID 還原成 A 的資訊,否則後續邏輯會誤判呼叫者身分。

一句話總結:在呼叫下一層元件之前執行 clearCallingIdentity(),呼叫結束之後執行 restoreCallingIdentity()。兩者必須成對出現。

3.2 用生活情境類比

如果上面的描述還是有點抽象,換個場景就好懂了。

情境:朋友請你幫忙,用你公司的員工內部價買一項產品。

這件事分成兩個階段,各自有不同的權限規則:

  • 第一階段(朋友 → 你):你並非有求必應,所以要先檢查對方身分。這對應的就是「A 呼叫 B」時的 uid 檢查——你記下對方的 Identity,通過才受理。
  • 第二階段(你 → 公司同事):公司內購只对员工开放,判斷依據是員工編號,而不是性別。此時若還拿著第一階段記下的資訊去比對,結果必然錯誤。clearCallingIdentity() 正是在這裡登場:先把身分清空、換成你自己的員工編號,公司那邊就能正確判定你有沒有內購資格。
  • 買完之後:你得把產品交回給朋友,所以要用 restoreCallingIdentity() 把身分還原成朋友那一組資訊,交付動作才對得上人。少了這一步,東西可能交錯對象。

這個類比也說明了為什麼兩者缺一不可:clear 少了,第二道權限檢查會用錯身分;restore 少了,呼叫者資訊會被汙染,影響後續所有依賴 uid 的判斷。

3.3 真實案例

這套模式最常出現在 system_server 行程的各子線程中,一般 App 應用較少用到。以 ActivityManagerService 的 attachApplication 為例:

@Override
public final void attachApplication(IApplicationThread thread) {
    synchronized (this) {
        // 取得遠端 Binder 呼叫方的 pid
        int callingPid = Binder.getCallingPid();
        // 清除遠端 uid/pid,並保存到 origId
        final long origId = Binder.clearCallingIdentity();
        attachApplicationLocked(thread, callingPid);
        // 以 origId 還原遠端 uid/pid
        Binder.restoreCallingIdentity(origId);
    }
}

程式結構非常清楚:getCallingPid() 先取出真正呼叫方的身分,接著 clear 之後才去執行同線程內的 attachApplicationLocked(),回來再用 origId 還原。這個「取身分 → 換身分 → 辦事 → 還原身分」的四段式寫法,幾乎是 framework 中權限控制的標準範式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved