createPackageContext
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมวดหมู่ OWASP: MASVS-CODE: คุณภาพโค้ด
ภาพรวม
public abstract Context createPackageContext (String packageName, int flags)
วิธีการ createPackageContext
ใช้เมื่อนักพัฒนาแอปต้องการสร้างบริบทสําหรับแอปพลิเคชันอื่นในแอปพลิเคชันของตนเอง
ตัวอย่างเช่น หากนักพัฒนาแอปต้องการรับทรัพยากรจากแอปพลิเคชันของบุคคลที่สามหรือเรียกใช้เมธอดจากแอปพลิเคชันดังกล่าว ก็ต้องใช้ createPackageContext
อย่างไรก็ตาม หากแอปพลิเคชันเรียกใช้ createPackageContext
ด้วย Flag CONTEXT_IGNORE_SECURITY
และ CONTEXT_INCLUDE_CODE
แล้วเรียกใช้ getClassLoader()
อาจส่งผลให้แอปพลิเคชันมีช่องโหว่ในการเรียกใช้โค้ดโดยแอปพลิเคชันที่เป็นอันตราย กรณีนี้อาจเกิดขึ้นได้ เช่น เมื่อผู้โจมตีแอบอ้างเป็นชื่อแพ็กเกจที่ยังไม่ได้อ้างสิทธิ์ (การแย่งชิงชื่อแพ็กเกจ) ซึ่งนักพัฒนาแอปคาดหวังว่าจะมีอยู่ในอุปกรณ์ของผู้ใช้
สรุปเกณฑ์ที่ต้องปฏิบัติตามเพื่อให้แอปพลิเคชันมีช่องโหว่ต่อการโจมตีประเภทนี้
แอปที่มีช่องโหว่:
แอปที่เป็นอันตราย
- อ้างสิทธิ์ชื่อแพ็กเกจที่แอปที่มีช่องโหว่ส่งให้กับ
createPackageContext
ได้
- ส่งออก android:appComponentFactory
ผลกระทบ
เมื่อแอปพลิเคชันใช้ createPackageContext ในลักษณะที่ไม่ปลอดภัย อาจทําให้แอปพลิเคชันที่เป็นอันตรายสามารถเรียกใช้โค้ดโดยไม่มีกฎเกณฑ์ในบริบทของแอปพลิเคชันที่ยังมีช่องโหว่
การลดปัญหา
อย่าเรียก createPackageContext
ด้วย CONTEXT_IGNORE_SECURITY
และ CONTEXT_INCLUDE_CODE
เว้นแต่จำเป็นจริงๆ
ในกรณีที่หลีกเลี่ยงไม่ได้ ให้ใช้กลไกเพื่อยืนยันตัวตนของแพ็กเกจที่คุณกำลังเรียกใช้ createPackageContext
(เช่น โดยการยืนยันลายเซ็นของแพ็กเกจ)
แหล่งข้อมูล
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","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 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)"]]