createPackageContext
Stay organized with collections
Save and categorize content based on your preferences.
OWASP category: MASVS-CODE: Code Quality
Overview
public abstract Context createPackageContext (String packageName, int flags)
The method createPackageContext
is used when a developer wants to create
a context for another application in their own application.
For example, if developers want to get resources from a 3rd-party application or
call methods from it, they would use createPackageContext
.
However, if an application calls createPackageContext
with the
CONTEXT_IGNORE_SECURITY
and CONTEXT_INCLUDE_CODE
flags, and then
calls getClassLoader()
, this could result in making the application
vulnerable to code execution by a malicious application. This can occur, for
example, when an attacker impersonates an unclaimed package name (package
squatting) that the developer had expected to be present on the user's device.
To summarize the criteria that have to be met to make an application vulnerable
to this kind of attack:
Vulnerable App:
Malicious App:
- Is able to claim the package name that the vulnerable app passes to
createPackageContext
.
- Exports android:appComponentFactory.
Impact
When createPackageContext is used in an insecure way by an application, this can
lead to a malicious application being able to gain arbitrary code execution in
the context of the vulnerable application.
Mitigations
Don't call createPackageContext
with
CONTEXT_IGNORE_SECURITY
and CONTEXT_INCLUDE_CODE
unless absolutely necessary.
In cases where this is unavoidable, make sure to implement a mechanism to verify
the identity of the package you are executing createPackageContext
on (e.g. by verifying the package's signature).
Resources
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-09-24 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-24 UTC."],[],[],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)"]]