From 435c8fe2f8a92dd086a16c19eaafc2fcf7df4b29 Mon Sep 17 00:00:00 2001 From: Songchun Fan Date: Fri, 27 Jan 2023 16:23:23 -0800 Subject: [PATCH] [pm] prevent non-debuggable app downgrades via shell Downgrade can only be performed if the following conditions are met: 1. Downgrade is requested in the installFlags 2a. App is debuggable Or 2b: If app is not debuggable, the build is debuggable or the calling uid is system or root. This means shell can't downgrade a non-debuggable app on a non-debuggable build. BUG: 256202273 Test: manual Change-Id: I841878502eaa9574e98a38a77e73352fd9735677 --- .../java/com/android/server/pm/PackageInstallerService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java index 239853c857ccf..c48f6ea47e905 100644 --- a/services/core/java/com/android/server/pm/PackageInstallerService.java +++ b/services/core/java/com/android/server/pm/PackageInstallerService.java @@ -705,7 +705,7 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements } } - if (Build.IS_DEBUGGABLE || isCalledBySystemOrShell(callingUid)) { + if (Build.IS_DEBUGGABLE || isCalledBySystem(callingUid)) { params.installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE; } else { params.installFlags &= ~PackageManager.INSTALL_ALLOW_DOWNGRADE; @@ -906,6 +906,10 @@ public class PackageInstallerService extends IPackageInstaller.Stub implements return sessionId; } + private static boolean isCalledBySystem(int callingUid) { + return callingUid == Process.SYSTEM_UID || callingUid == Process.ROOT_UID; + } + private boolean isCalledBySystemOrShell(int callingUid) { return callingUid == Process.SYSTEM_UID || callingUid == Process.ROOT_UID || callingUid == Process.SHELL_UID;