mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 17:35:06 -05:00
util: add function for dropping root privileges
Share the code for dropping supplementary groups and setting effective, saved, and real user UID/GID between system drivers.
This commit is contained in:
20
util.c
20
util.c
@@ -1118,6 +1118,26 @@ UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid)
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
UTI_DropRoot(uid_t uid, gid_t gid)
|
||||
{
|
||||
/* Drop supplementary groups */
|
||||
if (setgroups(0, NULL))
|
||||
LOG_FATAL(LOGF_Util, "setgroups() failed : %s", strerror(errno));
|
||||
|
||||
/* Set effective, saved and real group ID */
|
||||
if (setgid(gid))
|
||||
LOG_FATAL(LOGF_Util, "setgid(%d) failed : %s", gid, strerror(errno));
|
||||
|
||||
/* Set effective, saved and real user ID */
|
||||
if (setuid(uid))
|
||||
LOG_FATAL(LOGF_Util, "setuid(%d) failed : %s", uid, strerror(errno));
|
||||
|
||||
DEBUG_LOG(LOGF_Util, "Dropped root privileges: UID %d GID %d", uid, gid);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
#define DEV_URANDOM "/dev/urandom"
|
||||
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user