From 2cc603b00a87e02a81cbf9d79645bd1e3efc277c Mon Sep 17 00:00:00 2001 From: Mohammad Samiul Islam Date: Fri, 16 Oct 2020 19:18:43 +0100 Subject: [PATCH] Prevent exceptions during staged install from crashing system server An unhandled exception during staged install at boot time will cause system server to crash and restart. Upon restart, staged install will resume again and it will cause system server to crash again. Thus creating a loop. Instead of allowing the exception to crash the system server, we now catch it and fail the corresponding staged session with appropriate message. Bug: 170784748 Test: manual Test: verified that without this fix, any unhandled exception sends system server into a crash loop Change-Id: If79073a9a40c70e8223acbc47a3da394eb54fb43 Merged-In: I9abec5d2401af95ecb095fa3c45960d2f15d4e74 (cherry picked from commit 1c3e99b6c5a13ec2d7a8d3e618ce459fec0de7b7) --- .../com/android/server/pm/StagingManager.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/services/core/java/com/android/server/pm/StagingManager.java b/services/core/java/com/android/server/pm/StagingManager.java index 96dd001008786..5adab3e3f56d5 100644 --- a/services/core/java/com/android/server/pm/StagingManager.java +++ b/services/core/java/com/android/server/pm/StagingManager.java @@ -353,22 +353,9 @@ public class StagingManager { } // The APEX part of the session is activated, proceed with the installation of APKs. if (!installApksInSession(session, /* preReboot */ false)) { - session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, - "Staged installation of APKs failed. Check logcat messages for" - + "more information."); - - if (!hasApex) { - return; - } - - if (!mApexManager.abortActiveSession()) { - Slog.e(TAG, "Failed to abort APEXd session"); - } else { - Slog.e(TAG, - "Successfully aborted apexd session. Rebooting device in order to revert " - + "to the previous state of APEXd."); - mPowerManager.reboot(null); - } + onInstallationFailure(session, new PackageManagerException( + SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, "Staged installation of APKs " + + "failed. Check logcat messages for more information.")); return; } @@ -378,6 +365,21 @@ public class StagingManager { } } + void onInstallationFailure(PackageInstallerSession session, PackageManagerException e) { + session.setStagedSessionFailed(e.error, e.getMessage()); + if (!sessionContainsApex(session)) { + return; + } + if (!mApexManager.abortActiveSession()) { + Slog.e(TAG, "Failed to abort APEXd session"); + } else { + Slog.e(TAG, + "Successfully aborted apexd session. Rebooting device in order to revert " + + "to the previous state of APEXd."); + mPowerManager.reboot(null); + } + } + private List findAPKsInDir(File stageDir) { List ret = new ArrayList<>(); if (stageDir != null && stageDir.exists()) { @@ -671,7 +673,15 @@ public class StagingManager { } else { // Session had already being marked ready. Start the checks to verify if there is any // follow-up work. - resumeSession(session); + try { + resumeSession(session); + } catch (Exception e) { + Slog.e(TAG, "Staged install failed due to unhandled exception", e); + onInstallationFailure(session, new PackageManagerException( + SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, + "Staged install failed due to unhandled exception: " + e)); + + } } }