Android7.0的靜默安裝失敗問題研究

Android7.0的靜默安裝失敗問題研究

最近遇到了在Android7.0上靜默安裝失敗的問題谢揪。應(yīng)用程序放到系統(tǒng)分區(qū)(/system/priv-app/)執(zhí)行pm命令實(shí)現(xiàn)靜默安裝的方式在其他版本上都可以實(shí)現(xiàn)靜默安裝统台,在7.0上就安裝失敗,報(bào)這個(gè)異常:

java.lang.SecurityException: 
    Permission Denial: runInstallCreate from pm command asks to run as user -1 but is calling from user 0; 
    this requires android.permission.INTERACT_ACROSS_USERS_FULL

至于安裝失敗肯定是pm instll的執(zhí)行邏輯做了修改,我們直接從Pm的源碼開始研究钓简。
網(wǎng)上關(guān)于7.0靜默安裝的資料確實(shí)很少油啤,沒辦法了只能先去查看源碼看為啥報(bào)這個(gè)異常了。手頭上也沒有7.0的源碼坯钦,只能去下載了预皇,20個(gè)G的源碼下載了三個(gè)小時(shí)左右。7.0源碼下載婉刀,清華大學(xué)Android源碼鏡像站吟温,在解壓的aosp目錄下面執(zhí)行repo sync即可得到完整目錄。
看frameworks的突颊,源碼查看工具我用的是understand鲁豪,全局搜索runInstallCreate from pm command asks to run as user沒搜到,說明這句話是組裝成的洋丐,所以拆開搜索呈昔,全局搜索asks to run as user,這次搜到了友绝。該方法在UserController類中

int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll,
            int allowMode, String name, String callerPackage) {
        //測試發(fā)現(xiàn)這個(gè)getUserId的返回值應(yīng)該為0
        final int callingUserId = UserHandle.getUserId(callingUid);
        if (callingUserId == userId) {
            return userId;
        }

        // Note that we may be accessing mCurrentUserId outside of a lock...
        // shouldn't be a big deal, if this is being called outside
        // of a locked context there is intrinsically a race with
        // the value the caller will receive and someone else changing it.
        // We assume that USER_CURRENT_OR_SELF will use the current user; later
        // we will switch to the calling user if access to the current user fails.
        int targetUserId = unsafeConvertIncomingUserLocked(userId);

        if (callingUid != 0 && callingUid != SYSTEM_UID) {
            final boolean allow;
            if (mService.checkComponentPermission(INTERACT_ACROSS_USERS_FULL, callingPid,
                    callingUid, -1, true) == PackageManager.PERMISSION_GRANTED) {
                // If the caller has this permission, they always pass go.  And collect $200.
                allow = true;
            } else if (allowMode == ALLOW_FULL_ONLY) {
                // We require full access, sucks to be you.
                allow = false;
            } else if (mService.checkComponentPermission(INTERACT_ACROSS_USERS, callingPid,
                    callingUid, -1, true) != PackageManager.PERMISSION_GRANTED) {
                // If the caller does not have either permission, they are always doomed.
                allow = false;
            } else if (allowMode == ALLOW_NON_FULL) {
                // We are blanket allowing non-full access, you lucky caller!
                allow = true;
            } else if (allowMode == ALLOW_NON_FULL_IN_PROFILE) {
                // We may or may not allow this depending on whether the two users are
                // in the same profile.
                allow = isSameProfileGroup(callingUserId, targetUserId);
            } else {
                throw new IllegalArgumentException("Unknown mode: " + allowMode);
            }
            if (!allow) {
                if (userId == UserHandle.USER_CURRENT_OR_SELF) {
                    // In this case, they would like to just execute as their
                    // owner user instead of failing.
                    targetUserId = callingUserId;
                } else {
                    StringBuilder builder = new StringBuilder(128);
                    builder.append("Permission Denial: ");
                    builder.append(name);
                    if (callerPackage != null) {
                        builder.append(" from ");
                        builder.append(callerPackage);
                    }
                    builder.append(" asks to run as user ");
                    builder.append(userId);
                    builder.append(" but is calling from user ");
                    builder.append(UserHandle.getUserId(callingUid));
                    builder.append("; this requires ");
                    builder.append(INTERACT_ACROSS_USERS_FULL);
                    if (allowMode != ALLOW_FULL_ONLY) {
                        builder.append(" or ");
                        builder.append(INTERACT_ACROSS_USERS);
                    }
                    String msg = builder.toString();
                    Slog.w(TAG, msg);
                    throw new SecurityException(msg);
                }
            }
        }
        if (!allowAll && targetUserId < 0) {
            throw new IllegalArgumentException(
                    "Call does not support special user #" + targetUserId);
        }
        // Check shell permission
        if (callingUid == Process.SHELL_UID && targetUserId >= UserHandle.USER_SYSTEM) {
            if (hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId)) {
                throw new SecurityException("Shell does not have permission to access user "
                        + targetUserId + "\n " + Debug.getCallers(3));
            }
        }
        return targetUserId;
    }

研究發(fā)現(xiàn)callingUserId = UserHandle.getUserId(callingUid)的返回值為0堤尾,如果userId的值和callingUserId的值相同就不會(huì)執(zhí)行下面的方法,也就不會(huì)拋出異常了迁客。那我們就繼續(xù)追蹤看看userId是什么郭宝。這次我們Pm的源碼開始,pm install就是在Pm這個(gè)類中執(zhí)行的掷漱,調(diào)用的是runInstall方法粘室,runInstall的源碼就不貼出來了,我們這里只關(guān)心兩個(gè)方法
final InstallParams params = makeInstallParams();和final int sessionId = doCreateSession(params.sessionParams,params.installerPackageName, params.userId);
makeInstallParams的源碼:該方法是解析命令中的參數(shù)的卜范,這里只看主要的

private InstallParams makeInstallParams() {
        final SessionParams sessionParams = new SessionParams(SessionParams.MODE_FULL_INSTALL);
        final InstallParams params = new InstallParams();
        params.sessionParams = sessionParams;
        String opt;
        while ((opt = nextOption()) != null) {
            switch (opt) {
               ....
                case "-i":
                    params.installerPackageName = nextArg();
                    if (params.installerPackageName == null) {
                        throw new IllegalArgumentException("Missing installer package");
                    }
                    break;
               ...
               ...
                case "--user":
                    params.userId = UserHandle.parseUserArg(nextOptionData());
                    break;
                ....
                default:
                    throw new IllegalArgumentException("Unknown option " + opt);
            }
        }
        return params;
    }

doCreateSession方法中回去調(diào)用userId = translateUserId(userId, "runInstallCreate");
translateUserId中回去調(diào)用ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, logContext, "pm command");看ActivityManagerService中的handleIncomingUser方法

@Override
    public int handleIncomingUser(int callingPid, int callingUid, int userId, boolean allowAll,
            boolean requireFull, String name, String callerPackage) {
        return mUserController.handleIncomingUser(callingPid, callingUid, userId, allowAll,
                requireFull ? ALLOW_FULL_ONLY : ALLOW_NON_FULL, name, callerPackage);
    }

找到了衔统,就是在這里調(diào)用的mUserController.handleIncomingUser方法,拋出的異常。這個(gè)userId就是我們調(diào)用doCreateSession方法傳入的params.userId锦爵,我們繼續(xù)看makeInstallParams的源碼舱殿,params.userId是--user指定的。
我們在代碼中執(zhí)行Runtime.getRuntime().exec("pm install --user 0 apkpath")险掀,log又拋出一個(gè)空指針異常沪袭。
調(diào)用鏈:doCreateSession--->mInstaller.createSession(params, installerPackageName, userId)-->createSessionInternal-->mAppOps.checkPackage(callingUid, installerPackageName);
checkPackageName會(huì)判斷installerPackageName是不是為空,為空就拋出異常樟氢。installerPackageNameye也是調(diào)用doCreateSession時(shí)傳入params.installerPackageName 該值也是pm命令指定的冈绊,為當(dāng)前調(diào)用執(zhí)行pm命令的包名
所以修改pm命令為:Runtime.getRuntime().exec("pm install -i 包名 --user 0 apkpath"),靜默安裝成功2嚎小K佬!
到這里就結(jié)束了霸妹,后面的源碼沒有貼出來十电,但是寫出了調(diào)用過程,大家可以自己去追蹤一下源碼叹螟!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市台盯,隨后出現(xiàn)的幾起案子罢绽,更是在濱河造成了極大的恐慌,老刑警劉巖静盅,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件良价,死亡現(xiàn)場離奇詭異,居然都是意外死亡蒿叠,警方通過查閱死者的電腦和手機(jī)明垢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來市咽,“玉大人痊银,你說我怎么就攤上這事∈┮铮” “怎么了溯革?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長谷醉。 經(jīng)常有香客問我致稀,道長,這世上最難降的妖魔是什么俱尼? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任抖单,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘矛绘。我一直安慰自己耍休,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布蔑歌。 她就那樣靜靜地躺著羹应,像睡著了一般。 火紅的嫁衣襯著肌膚如雪次屠。 梳的紋絲不亂的頭發(fā)上园匹,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機(jī)與錄音劫灶,去河邊找鬼裸违。 笑死,一個(gè)胖子當(dāng)著我的面吹牛本昏,可吹牛的內(nèi)容都是我干的供汛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涌穆,長吁一口氣:“原來是場噩夢啊……” “哼怔昨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起宿稀,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤趁舀,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后祝沸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矮烹,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年罩锐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了奉狈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡涩惑,死狀恐怖仁期,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情境氢,我是刑警寧澤蟀拷,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站萍聊,受9級特大地震影響问芬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜寿桨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一此衅、第九天 我趴在偏房一處隱蔽的房頂上張望强戴。 院中可真熱鬧,春花似錦挡鞍、人聲如沸骑歹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽道媚。三九已至,卻和暖如春翘县,著一層夾襖步出監(jiān)牢的瞬間最域,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工锈麸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镀脂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓忘伞,卻偏偏與公主長得像薄翅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子氓奈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容