您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android5.1系統(tǒng)如何通過包名給應(yīng)用開放系統(tǒng)權(quán)限,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
常用的給應(yīng)用開放系統(tǒng)權(quán)限的方法是直接將該應(yīng)用做成系統(tǒng)應(yīng)用(即在AndroidManifest.xml文件里加上:android:sharedUserId="android.uid.system"),但是這種做法限制了應(yīng)用本身的自由,也就是說經(jīng)過系統(tǒng)簽名后的系統(tǒng)應(yīng)用只能夠在對(duì)應(yīng)的Android平臺(tái)上安裝使用,無(wú)法向其他應(yīng)用一樣放到Android市場(chǎng)里兼容所有的Android設(shè)備。
現(xiàn)在此介紹一種通過修改Android平臺(tái)系統(tǒng)層代碼,根據(jù)指定的應(yīng)用包名給對(duì)應(yīng)的應(yīng)用在該平臺(tái)上開放系統(tǒng)權(quán)限,這樣既不應(yīng)用應(yīng)用的兼容性,又解決了應(yīng)用想調(diào)用一些系統(tǒng)層接口而沒有權(quán)限的矛盾。
該方法的核心是:在ActivityManagerService的startProcessLocked
接口中把uid和gid都改為0.
需要開放系統(tǒng)權(quán)限的包名:
1、net.forclass.fcstudent
2、com.ckl.launcher
3、com.creative.fcstudent
修改步驟:
1、應(yīng)用安裝在設(shè)備上之后,點(diǎn)擊啟動(dòng)必定會(huì)調(diào)用ActivityManagerService的startProcessLocked接口來開啟一個(gè)新的進(jìn)程,而給應(yīng)用開放系統(tǒng)權(quán)限目的其實(shí)就是使應(yīng)用能夠成為超級(jí)應(yīng)用,運(yùn)行在系統(tǒng)進(jìn)程中,這樣我們只需要在startProcessLocked接口里面將應(yīng)用的uid修改為0即可。
ActivityManagerService.java (frameworks\base\services\core\java\com\android\server\am) final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName, boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) { // modified by haming patch begin, configure system permission for some special application. if ("net.forclass.fcstudent".equals(info.packageName) || "com.ckl.launcher".equals(info.packageName) || "com.creative.fcstudent".equals(info.packageName) || "com.hampoo.hampoointerfacetestdemo".equals(info.packageName)){ info.uid = 0; } // modified by haming patch end. long startTime = SystemClock.elapsedRealtime(); ProcessRecord app; ...... // 此處省略好多行 checkTime(startTime, "startProcess: stepping in to startProcess"); startProcessLocked( // 再次調(diào)用startProcessLocked重載方法 app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); checkTime(startTime, "startProcess: done starting proc!"); return (app.pid != 0) ? app : null; }在重載方法startProcessLocked(ProcessRecord app, String hostingType,String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs)里面將應(yīng)用的gid也改為0: private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) { long startTime = SystemClock.elapsedRealtime(); ...... // 此處省略好多行 try { int uid = app.uid; int[] gids = null; int mountExternal = Zygote.MOUNT_EXTERNAL_NONE; if (!app.isolated) { ...... // 此處省略好多行 // modified by haming patch begin, configure system permission for some special application. if ("net.forclass.fcstudent".equals(app.info.packageName) || "com.ckl.launcher".equals(app.info.packageName) || "com.creative.fcstudent".equals(app.info.packageName) || "com.hampoo.hampoointerfacetestdemo".equals(app.info.packageName)){ SystemProperties.set("sys.permission.enable", "true"); // 設(shè)置一個(gè)系統(tǒng)屬性,在Zygote進(jìn)行判斷是否拋出異常 gids[0] = 0; gids[1] = 0; } else { gids[0] = UserHandle.getSharedAppGid(UserHandle.getAppId(uid)); gids[1] = UserHandle.getUserGid(UserHandle.getUserId(uid)); } // modified by haming patch end. } ...... // 此處省略好多行 } catch (RuntimeException e) { // XXX do better error recovery. app.setPid(0); mBatteryStatsService.noteProcessFinish(app.processName, app.info.uid); if (app.isolated) { mBatteryStatsService.removeIsolatedUid(app.uid, app.info.uid); } Slog.e(TAG, "Failure starting process " + app.processName, e); } }
2、ZygoteConnection.java里的applyUidSecurityPolicy(Arguments args, Credentials peer, String peerSecurityContext)接口會(huì)對(duì)進(jìn)程id進(jìn)行判斷,如果小于Process.SYSTEM_UID(1000)則認(rèn)為是非法,而zygote是具有root權(quán)限的唯一server,所有在判斷之后就可以通過讀取前面設(shè)定的系統(tǒng)屬性“sys.permission.enable”的值來決定是否拋出異常。
ZygoteConnection.java (frameworks\base\core\java\com\android\internal\os) private static void applyUidSecurityPolicy(Arguments args, Credentials peer, String peerSecurityContext) throws ZygoteSecurityException { int peerUid = peer.getUid(); if (peerUid == 0) { // Root can do what it wants } else if (peerUid == Process.SYSTEM_UID ) { // System UID is restricted, except in factory test mode String factoryTest = SystemProperties.get("ro.factorytest"); boolean uidRestricted; /* In normal operation, SYSTEM_UID can only specify a restricted * set of UIDs. In factory test mode, SYSTEM_UID may specify any uid. */ uidRestricted = !(factoryTest.equals("1") || factoryTest.equals("2")); // modified by haming patch begin, configure system permission for some special application. if (uidRestricted && args.uidSpecified && (args.uid < Process.SYSTEM_UID)) { if (!SystemProperties.getBoolean("sys.permission.enable", false)){ throw new ZygoteSecurityException( "System UID may not launch process with UID < " + Process.SYSTEM_UID); } else { SystemProperties.set("sys.permission.enable", "false"); } } // modified by haming patch end. } else { // Everything else if (args.uidSpecified || args.gidSpecified || args.gids != null) { throw new ZygoteSecurityException( "App UIDs may not specify uid's or gid's"); } } ...... // 此處省略好多行 }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android5.1系統(tǒng)如何通過包名給應(yīng)用開放系統(tǒng)權(quán)限”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。