From d7f2a75240490e89407561b845d597455e5d72df Mon Sep 17 00:00:00 2001 From: Songchun Fan Date: Fri, 11 Mar 2022 00:32:10 +0000 Subject: [PATCH] [pm] restrict max number of components at parsing Apps with too many components can cause the system to be unresponsive. Adding a max number restriction in the parsing so that such apps will be rejected during installation. The current max number is chosen based on the stats of Play apps. Test: manual $ adb install app-debug.apk Performing Streamed Install adb: failed to install /usr/local/google/home/schfan/Downloads/app-debug.apk: Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED: Failed parse during installPackageLI: /data/app/vmdl1065528371.tmp/base.apk (at Binary XML file line #300021): Total number of components has exceeded the maximum number: 30000] BUG: 214397059 Change-Id: Iae22f66db8434533efffa8db2ed72e0d23534100 --- .../pm/pkg/parsing/ParsingPackageUtils.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java index 3eaca9dddcc41..5fe828cc10d0e 100644 --- a/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java +++ b/services/core/java/com/android/server/pm/pkg/parsing/ParsingPackageUtils.java @@ -239,6 +239,11 @@ public class ParsingPackageUtils { public static final int PARSE_CHATTY = 1 << 31; + /** The total maximum number of activities, services, providers and activity-aliases */ + private static final int MAX_NUM_COMPONENTS = 30000; + private static final String MAX_NUM_COMPONENTS_ERR_MSG = + "Total number of components has exceeded the maximum number: " + MAX_NUM_COMPONENTS; + @IntDef(flag = true, prefix = { "PARSE_" }, value = { PARSE_CHATTY, PARSE_COLLECT_CERTIFICATES, @@ -834,11 +839,20 @@ public class ParsingPackageUtils { if (result.isError()) { return input.error(result); } + + if (hasTooManyComponents(pkg)) { + return input.error(MAX_NUM_COMPONENTS_ERR_MSG); + } } return input.success(pkg); } + private static boolean hasTooManyComponents(ParsingPackage pkg) { + return pkg.getActivities().size() + pkg.getServices().size() + pkg.getProviders().size() + > MAX_NUM_COMPONENTS; + } + /** * For parsing non-MainComponents. Main ones have an order and some special handling which is * done directly in {@link #parseSplitApplication(ParseInput, ParsingPackage, Resources, @@ -2125,6 +2139,9 @@ public class ParsingPackageUtils { if (result.isError()) { return input.error(result); } + if (hasTooManyComponents(pkg)) { + return input.error(MAX_NUM_COMPONENTS_ERR_MSG); + } } if (TextUtils.isEmpty(pkg.getStaticSharedLibName()) && TextUtils.isEmpty(