From bfc115311e2f7d38612d64fe35569687167248a4 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 7 Nov 2023 14:44:24 -0800 Subject: [PATCH] run-as: reduce the scope of changed egid. There's no bug here that I can see, but having a larger than necessary scope just seems like a bad idea in case anyone adds anything later. Also switch to modern struct initialization syntax. Bug: http://b/308990322 Test: treehugger Change-Id: If2576f902de62e8c24d4a643c224df8ea6cff5d5 --- run-as/run-as.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/run-as/run-as.cpp b/run-as/run-as.cpp index cc92c68d0..9ebe799e1 100644 --- a/run-as/run-as.cpp +++ b/run-as/run-as.cpp @@ -191,14 +191,13 @@ int main(int argc, char* argv[]) { } // Retrieve package information from system, switching egid so we can read the file. + pkg_info info = {.name = pkgname}; gid_t old_egid = getegid(); if (setegid(AID_PACKAGE_INFO) == -1) error(1, errno, "setegid(AID_PACKAGE_INFO) failed"); - pkg_info info; - memset(&info, 0, sizeof(info)); - info.name = pkgname; if (!packagelist_parse(packagelist_parse_callback, &info)) { error(1, errno, "packagelist_parse failed"); } + if (setegid(old_egid) == -1) error(1, errno, "couldn't restore egid"); // Handle a multi-user data path if (userId > 0) { @@ -211,7 +210,6 @@ int main(int argc, char* argv[]) { if (info.uid == 0) { error(1, 0, "unknown package: %s", pkgname); } - if (setegid(old_egid) == -1) error(1, errno, "couldn't restore egid"); // Verify that user id is not too big. if ((UID_MAX - info.uid) / AID_USER_OFFSET < (uid_t)userId) {