Android ContentProvider調(diào)用報(bào)錯(cuò)"Bad call: specified package xxx under uid 10032 but it is really 10001"及Binder權(quán)限問題分析

問題:

項(xiàng)目中有一下情況:進(jìn)程A調(diào)用另一進(jìn)程的B ContentProvider,B在該此次query中需要在query另一個(gè) C ContentProvider:

    class BContentProvider extends ContentProvider {
        Context mContext;
        ...
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            ...
            try {
                // query C ContentProvider:
                Cursor cursor = mContext.getContentResolver().query(...);
                if (cursor != null) {
                    try {
                        //do something;
                    } finally {
                        cursor.close();
                    }
                }
                Cursor cursor = mContext.getContentResolver().query(...);
            ...
        ...
            }
        }
    }

在這種情況下饥臂,系統(tǒng)拋出Exception如下:

1-11 16:04:51.867  2633  3557 W AppOps  : Bad call: specified package com.providers.xxx under uid 10032 but it is really 10001
01-11 16:04:51.867  2633  3557 W AppOps  : java.lang.RuntimeException: here
01-11 16:04:51.867  2633  3557 W AppOps  :  at com.android.server.AppOpsService.getOpsRawLocked(AppOpsService.java:1399)
01-11 16:04:51.867  2633  3557 W AppOps  :  at com.android.server.AppOpsService.noteOperationUnchecked(AppOpsService.java:1115)
01-11 16:04:51.867  2633  3557 W AppOps  :  at com.android.server.AppOpsService.noteProxyOperation(AppOpsService.java:1093)
01-11 16:04:51.867  2633  3557 W AppOps  :  at com.android.internal.app.IAppOpsService$Stub.onTransact(IAppOpsService.java:157)
01-11 16:04:51.867  2633  3557 W AppOps  :  at android.os.BinderInjector.onTransact(BinderInjector.java:30)
01-11 16:04:51.867  2633  3557 W AppOps  :  at android.os.Binder.execTransact(Binder.java:569)
01-11 16:04:51.868  4659  6791 E DatabaseUtils: Writing exception to parcel
01-11 16:04:51.868  4659  6791 E DatabaseUtils: java.lang.SecurityException: Proxy package com.providers.xxx from uid 10001 or calling package com.providers.xxx from uid 10032 not allowed to perform READ_PROVIDER_C
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.app.AppOpsManager.noteProxyOp(AppOpsManager.java:1834)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProvider.checkPermissionAndAppOp(ContentProvider.java:538)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:560)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:483)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProvider$Transport.query(ContentProvider.java:212)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentResolver.query(ContentResolver.java:532)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentResolver.query(ContentResolver.java:473)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at com.android.providers.xxx.BDatabaseHelper.query(BDatabaseHelper.java:7238)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at 
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.os.BinderInjector.onTransact(BinderInjector.java:30)
01-11 16:04:51.868  4659  6791 E DatabaseUtils:     at android.os.Binder.execTransact(Binder.java:569)

分析:

由于錯(cuò)誤log首先反應(yīng)了沒有C ContentProvider的權(quán)限,但檢查A應(yīng)用是有C的讀寫權(quán)限的瀑晒。所以排除了A的權(quán)限問題论笔。
繼續(xù)分析:
通過log可以看到確實(shí)是ContentProvider在做權(quán)限檢查時(shí)出錯(cuò)。通過log中對(duì)應(yīng)的源碼進(jìn)行分析:
首先可以看到ContentProvider.query()的時(shí)候做了權(quán)限檢查懂昂,注意介时,傳入的enforceReadPermission()的callingPkg是調(diào)用方的包名,以上面為例,就是B的包名沸柔。

ContentProvider.query():

        @Override
        public Cursor query(String callingPkg, Uri uri, @Nullable String[] projection,
                @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal) {
            validateIncomingUri(uri);
            uri = maybeGetUriWithoutUserId(uri);
            if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {

enforceReadPermission()調(diào)用了.checkPermissionAndAppOp()方法循衰,ContentProvider.checkPermissionAndAppOp()調(diào)用了AppOpsManager.noteProxyOp()去做檢查出了異常。

AppOpsManager.noteProxyOp():

    public int noteProxyOp(int op, String proxiedPackageName) {
        int mode = noteProxyOpNoThrow(op, proxiedPackageName);
        if (mode == MODE_ERRORED) {
            throw new SecurityException("Proxy package " + mContext.getOpPackageName()
                    + " from uid " + Process.myUid() + " or calling package "
                    + proxiedPackageName + " from uid " + Binder.getCallingUid()
                    + " not allowed to perform " + sOpNames[op]);
        }
        return mode;
    }

noteProxyOpNoThrow()又做了什么呢褐澎?
AppOpsManager.noteProxyOpNoThrow():

    /**
     * Like {@link #noteProxyOp(int, String)} but instead
     * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}.
     * @hide
     */
    public int noteProxyOpNoThrow(int op, String proxiedPackageName) {
        try {
            return mService.noteProxyOperation(op, mContext.getOpPackageName(),
                    Binder.getCallingUid(), proxiedPackageName);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

可見noteProxyOpNoThrow()是通過binder調(diào)用到了AppOpsService.noteProxyOperation()方法会钝,注意,這里傳入的是AppOpsService.noteProxyOperation()的后兩個(gè)參數(shù)為Binder.getCallingUid()和之前層層傳入的調(diào)用方的包名工三,也就是上面例子的B的包名迁酸。

下面,繼續(xù)看binder另一側(cè)的AppOpsService.noteProxyOperation()方法,我們結(jié)合log中AppOps的輸出log:

AppOpsService.noteProxyOperation():

    @Override
    public int noteProxyOperation(int code, String proxyPackageName,
            int proxiedUid, String proxiedPackageName) {
        verifyIncomingOp(code);
        final int proxyUid = Binder.getCallingUid();
        String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName);
        if (resolveProxyPackageName == null) {
            return AppOpsManager.MODE_IGNORED;
        }
        final int proxyMode = noteOperationUnchecked(code, proxyUid,
                resolveProxyPackageName, -1, null);
        if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
            return proxyMode;
        }
        String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName);
        if (resolveProxiedPackageName == null) {
            return AppOpsManager.MODE_IGNORED;
        }
        return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName,
                proxyMode, resolveProxyPackageName);
    }

AppOpsService.noteOperationUnchecked():

   private int noteOperationUnchecked(int code, int uid, String packageName,
            int proxyUid, String proxyPackageName) {
        Op op = null;
        Op switchOp = null;
        int switchCode;
        int resultMode = AppOpsManager.MODE_ALLOWED;
        synchronized (this) {
            Ops ops = getOpsRawLocked(uid, packageName, true);
          ...
         }
    ...
}

AppOpsService.getOpsRawLocked():

    private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
        ...
        Ops ops = uidState.pkgOps.get(packageName);
        if (ops == null) {
            if (!edit) {
                return null;
            }
            boolean isPrivileged = false;
            // This is the first time we have seen this package name under this uid,
            // so let's make sure it is valid.
            if (uid != 0) {
                final long ident = Binder.clearCallingIdentity();
                try {
                    int pkgUid = -1;
                    try {
                        ApplicationInfo appInfo = ActivityThread.getPackageManager()
                                .getApplicationInfo(packageName,
                                        PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
                                        UserHandle.getUserId(uid));
                        if (appInfo != null) {
                            pkgUid = appInfo.uid;
                            isPrivileged = (appInfo.privateFlags
                                    & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
                        }
                        ...
                    }
                    ...
                    if (pkgUid != uid) {
                        // Oops!  The package name is not valid for the uid they are calling
                        // under.  Abort.
                        RuntimeException ex = new RuntimeException("here");
                        ex.fillInStackTrace();
                        Slog.w(TAG, "Bad call: specified package " + packageName
                                + " under uid " + uid + " but it is really " + pkgUid, ex);
                        return null;
                    }
                } finally {
                    Binder.restoreCallingIdentity(ident);
                }
            }
            ops = new Ops(packageName, uidState, isPrivileged);
            uidState.pkgOps.put(packageName, ops);
        }
        return ops;
    }

這里主要的操作就是將傳入的uid和包名進(jìn)行判斷:比對(duì)該包對(duì)應(yīng)的uid和傳入的uid比較俭正,如果不一致就報(bào)錯(cuò)奸鬓。錯(cuò)誤信息和log中的一致:

Bad call: specified package com.providers.xxx under uid 10032 but it is really 10001

上文提到了,這個(gè)包名是傳入的ContentProvider的調(diào)用方的包名掸读,也就是例子中的B的包名串远。而uid是在AppOpsManager中通過Binder.getCallingUid()獲得的。log中顯示儿惫,此uid并不是B的uid澡罚,而是其上游調(diào)用者A的uid。
為什么在C中調(diào)用Binder.getCallingUid()得到的是A進(jìn)程的呢肾请?我找到了袁輝輝大神的一片博客: Binder IPC的權(quán)限控制

“線程B通過Binder調(diào)用當(dāng)前線程的某個(gè)組件:此時(shí)線程B是線程B某個(gè)組件的調(diào)用端留搔,則mCallingUid和mCallingPid應(yīng)該保存當(dāng)前線程B的PID和UID,故需要調(diào)用clearCallingIdentity()方法完成這個(gè)功能铛铁。當(dāng)線程B調(diào)用完某個(gè)組件催式,由于線程B仍然處于線程A的被調(diào)用端,因此mCallingUid和mCallingPid需要恢復(fù)成線程A的UID和PID避归,這是調(diào)用restoreCallingIdentity()即可完成荣月。”

Binder的機(jī)制就是這么設(shè)計(jì)的梳毙,所以需要在B進(jìn)行下一次Binder調(diào)用(也就是query ContentProvider)之前調(diào)用clearCallingIdentity()來將B的
PID和UID附給mCallingUid和mCallingPid哺窄。Binder調(diào)用結(jié)束后在restoreCallingIdentity()來將其恢復(fù)成其原本調(diào)用方的PID和UID。這樣在C里就會(huì)用B的相關(guān)信息進(jìn)行權(quán)限校驗(yàn)账锹,在AppOpsService.getOpsRawLocked()萌业,UID和包名都是B的,是一致的奸柬,就不會(huì)報(bào)錯(cuò)生年。

解決辦法:

其實(shí)上文也已經(jīng)提到了,參考 Binder IPC的權(quán)限控制廓奕,在B進(jìn)行Query前后分別調(diào)用clearCallingIdentity()
//作用是清空遠(yuǎn)程調(diào)用端的uid和pid抱婉,用當(dāng)前本地進(jìn)程的uid和pid替代档叔,這樣在之后的調(diào)用方去進(jìn)行權(quán)限校驗(yàn)時(shí)會(huì)以B的信息為主,不會(huì)出現(xiàn)包名和UID不一致的情況蒸绩。
最后修改過的調(diào)用方式如下:

        long token = Binder.clearCallingIdentity();
        try {
            Cursor cursor = mContext.getContentResolver().query(...);
            if (cursor != null) {
                try {
                    //do something;
                } finally {
                    cursor.close();
                }
            }
        } finally {
            Binder.restoreCallingIdentity(token);
        }

總結(jié):

1.ContentProvider是用Binder實(shí)現(xiàn)的衙四,查詢的過程其實(shí)就是一次Binder調(diào)用,所以想深入了解ContentProvider一定要會(huì)一些Binder相關(guān)的知識(shí)患亿。
2.ContentProvider在接受一次查詢前會(huì)調(diào)用AppOpsManager(其會(huì)通過Binder再由AppOpsService完成)進(jìn)行權(quán)限校驗(yàn)传蹈,其中會(huì)校驗(yàn)調(diào)用方的UID和包名是否一致,其相關(guān)功能可見文章: Android 權(quán)限管理 —— AppOps步藕。
2.Binder調(diào)用時(shí)候可以通過Binder.getCallingPid()和Binder.getCallingUid()來獲取調(diào)用方的PID和UID惦界,而如果A通過Binder調(diào)用B,B又Binder調(diào)用了C咙冗,那么在C中Binder.getCallingPid()和Binder.getCallingUid()得到的是A的PID和UID沾歪,這種情況下需要在B調(diào)用C的前后用Binder.clearCallingIdentity()和Binder.restoreCallingIdentity()使其帶上B的PID和UID,從而在C中進(jìn)行權(quán)限校驗(yàn)時(shí)候用B的信息進(jìn)行校驗(yàn)乞娄,當(dāng)然這也符合邏輯,B調(diào)用的C显歧,應(yīng)該B需要有相應(yīng)權(quán)限仪或。
3.Binder.clearCallingIdentity()和Binder.restoreCallingIdentity()的實(shí)現(xiàn)原理 Binder IPC的權(quán)限控制也有介紹,是通過移位實(shí)現(xiàn)的士骤。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末范删,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子拷肌,更是在濱河造成了極大的恐慌到旦,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巨缘,死亡現(xiàn)場(chǎng)離奇詭異添忘,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)若锁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門搁骑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人又固,你說我怎么就攤上這事仲器。” “怎么了仰冠?”我有些...
    開封第一講書人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵乏冀,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我洋只,道長(zhǎng)辆沦,這世上最難降的妖魔是什么昼捍? 我笑而不...
    開封第一講書人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮众辨,結(jié)果婚禮上端三,老公的妹妹穿的比我還像新娘。我一直安慰自己鹃彻,他們只是感情好郊闯,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蛛株,像睡著了一般团赁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谨履,一...
    開封第一講書人閱讀 49,166評(píng)論 1 284
  • 那天欢摄,我揣著相機(jī)與錄音,去河邊找鬼笋粟。 笑死怀挠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的害捕。 我是一名探鬼主播绿淋,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼尝盼!你這毒婦竟也來了吞滞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤盾沫,失蹤者是張志新(化名)和其女友劉穎裁赠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赴精,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡佩捞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蕾哟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片失尖。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖渐苏,靈堂內(nèi)的尸體忽然破棺而出掀潮,到底是詐尸還是另有隱情,我是刑警寧澤琼富,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布仪吧,位于F島的核電站,受9級(jí)特大地震影響鞠眉,放射性物質(zhì)發(fā)生泄漏薯鼠。R本人自食惡果不足惜择诈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望出皇。 院中可真熱鬧羞芍,春花似錦、人聲如沸郊艘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纱注。三九已至畏浆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間狞贱,已是汗流浹背刻获。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留瞎嬉,地道東北人蝎毡。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像氧枣,于是被迫代替她去往敵國(guó)和親沐兵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344