From f8825fafc810e831cccd3a8ad61c064f55d4fa45 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 2 Jul 2021 20:47:27 -0700 Subject: [PATCH] Fix gid not being set in ueventd when device nodes already exist. The chown() call will fix the uid for pre-existing nodes, but not the gid. This fix ensures the correct gid is set if needed. Bug: 187738549 Test: manual test modifying ueventd.rc Change-Id: I0fadd745a7c57a089fed9afc2572ace597a05396 --- init/devices.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/init/devices.cpp b/init/devices.cpp index ce6298a9d..56c66239e 100644 --- a/init/devices.cpp +++ b/init/devices.cpp @@ -264,6 +264,8 @@ void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, i setfscreatecon(secontext.c_str()); } + gid_t new_group = -1; + dev_t dev = makedev(major, minor); /* Temporarily change egid to avoid race condition setting the gid of the * device node. Unforunately changing the euid would prevent creation of @@ -291,10 +293,21 @@ void DeviceHandler::MakeDevice(const std::string& path, bool block, int major, i PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device"; } + + struct stat s; + if (stat(path.c_str(), &s) == 0) { + if (gid != s.st_gid) { + new_group = gid; + } + } else { + PLOG(ERROR) << "Cannot stat " << path; + } } out: - chown(path.c_str(), uid, -1); + if (chown(path.c_str(), uid, new_group) < 0) { + PLOG(ERROR) << "Cannot chown " << path << " " << uid << " " << new_group; + } if (setegid(AID_ROOT)) { PLOG(FATAL) << "setegid(AID_ROOT) failed"; }