createPackageContext
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
OWASP 類別:MASVS-CODE:程式碼品質
總覽
public abstract Context createPackageContext (String packageName, int flags)
開發人員想要在自己的應用程式中為其他應用程式建立結構定義時,會使用 createPackageContext
方法。
舉例來說,如果開發人員想要從第三方應用程式取得資源,或呼叫其中的方法,就會使用 createPackageContext
。
不過,如果應用程式使用 CONTEXT_IGNORE_SECURITY
和 CONTEXT_INCLUDE_CODE
標記呼叫 createPackageContext
,然後再呼叫 getClassLoader()
,就可能導致應用程式容易遭到惡意應用程式執行程式碼。舉例來說,如果攻擊者冒用開發人員預期會在使用者裝置上出現的未宣告套件名稱 (套件佔用),就可能發生這種情況。
以下是應用程式必須符合的條件摘要,才能遭受這類攻擊:
易受攻擊的應用程式:
惡意應用程式:
影響
如果應用程式以不安全的方式使用 createPackageContext,可能會導致惡意應用程式在有安全漏洞的應用程式環境中執行任意程式碼。
因應措施
除非絕對必要,否則請勿使用 CONTEXT_IGNORE_SECURITY
和 CONTEXT_INCLUDE_CODE
呼叫 createPackageContext
。
如果無法避免這種情況,請務必實作驗證機制,以驗證您執行 createPackageContext
的套件身分 (例如驗證套件的簽名)。
資源
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# createPackageContext\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\n public abstract Context createPackageContext (String packageName, int flags)\n\nThe method [`createPackageContext`](/reference/android/content/Context#createPackageContext(java.lang.String,%20int)) is used when a developer wants to create\na context for another application in their own application.\n\nFor example, if developers want to get resources from a 3rd-party application or\ncall methods from it, they would use `createPackageContext`.\n\nHowever, if an application calls `createPackageContext` with the\n[`CONTEXT_IGNORE_SECURITY`](/reference/android/content/Context#CONTEXT_IGNORE_SECURITY) and [`CONTEXT_INCLUDE_CODE`](/reference/android/content/Context#CONTEXT_INCLUDE_CODE) flags, and then\ncalls [`getClassLoader()`](/reference/android/content/Context#getClassLoader()), this could result in making the application\nvulnerable to code execution by a malicious application. This can occur, for\nexample, when an attacker impersonates an unclaimed package name (package\nsquatting) that the developer had expected to be present on the user's device.\n\nTo summarize the criteria that have to be met to make an application vulnerable\nto this kind of attack:\n\nVulnerable App:\n\n- Calls [`createPackageContext`](/reference/android/content/Context#createPackageContext(java.lang.String,%20int)) with [`CONTEXT_IGNORE_SECURITY`](/reference/android/content/Context#CONTEXT_IGNORE_SECURITY) and [`CONTEXT_INCLUDE_CODE`](/reference/android/content/Context#CONTEXT_INCLUDE_CODE).\n- Calls [`getClassLoader()`](/reference/android/content/Context#getClassLoader()) on the retrieved context.\n\nMalicious App:\n\n- Is able to claim the package name that the vulnerable app passes to [`createPackageContext`](/reference/android/content/Context#createPackageContext(java.lang.String,%20int)).\n- Exports android:appComponentFactory.\n\nImpact\n------\n\nWhen createPackageContext is used in an insecure way by an application, this can\nlead to a malicious application being able to gain arbitrary code execution in\nthe context of the vulnerable application.\n\nMitigations\n-----------\n\nDon't call [`createPackageContext`](/reference/android/content/Context#createPackageContext(java.lang.String,%20int)) with\n[`CONTEXT_IGNORE_SECURITY`](/reference/android/content/Context#CONTEXT_IGNORE_SECURITY) and [`CONTEXT_INCLUDE_CODE`](/reference/android/content/Context#CONTEXT_INCLUDE_CODE) unless absolutely necessary.\n\nIn cases where this is unavoidable, make sure to implement a mechanism to verify\nthe identity of the package you are executing [`createPackageContext`](/reference/android/content/Context#createPackageContext(java.lang.String,%20int)) on (e.g. by verifying the package's signature).\n\nResources\n---------\n\n- [createPackageContext Documentation](/reference/android/content/Context#createPackageContext(java.lang.String,%20int))\n- [OverSecured blog post on createPackageContext code execution](https://blog.oversecured.com/Android-arbitrary-code-execution-via-third-party-package-contexts)"]]