From bbb9690935278752df21c2650d73ba9234ecce62 Mon Sep 17 00:00:00 2001 From: Yi-Yo Chiang Date: Thu, 25 Jan 2024 17:24:31 +0800 Subject: [PATCH] remount: Auto remount option (-R) also tries to gain root When doing an auto remount & reboot command and is running as the SHELL uid, just try to gain root on behalf of the user and retry the remount command. If "gain root" failed, then print the message to tell the user to run "adb root" and retry. Bug: 322285923 Test: adb unroot && adb remount -vR Test: adb unroot && adb shell remount -vR Change-Id: If8e04dc602573c73178c108ef4944f0a985b590e --- fs_mgr/fs_mgr_remount.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/fs_mgr/fs_mgr_remount.cpp b/fs_mgr/fs_mgr_remount.cpp index 733ba2f20..79c0b6df0 100644 --- a/fs_mgr/fs_mgr_remount.cpp +++ b/fs_mgr/fs_mgr_remount.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include "fs_mgr_overlayfs_control.h" #include "fs_mgr_overlayfs_mount.h" @@ -608,7 +609,19 @@ int main(int argc, char* argv[]) { } // Make sure we are root. - if (::getuid() != 0) { + if (const auto uid = ::getuid(); uid != AID_ROOT) { + // If requesting auto reboot, also try to auto gain root. + if (auto_reboot && uid == AID_SHELL && access("/system/xbin/su", F_OK) == 0) { + std::vector args{const_cast("/system/xbin/su"), + const_cast("root")}; + for (int i = 0; i < argc; ++i) { + args.push_back(argv[i]); + } + args.push_back(nullptr); + LOG(INFO) << "Attempting to gain root with \"su root\""; + execv(args[0], args.data()); + PLOG(ERROR) << "Failed to execute \"su root\""; + } LOG(ERROR) << "Not running as root. Try \"adb root\" first."; return EXIT_FAILURE; }