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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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)"]]