From 2268718ee2b23936f6db88eff7af03762713c169 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Wed, 17 Nov 2010 16:55:42 -0800 Subject: [PATCH] check the return value of setuid and friends Under some circumstances, setuid() and family can fail, returning a non-zero value. (see "man setuid" for details). If this happens, we want to ensure that init doesn't spawn a process which has root privileges when it's not suppose to. Change-Id: Idd03f2c8f82a7eaf6e696b5bcfe308e51ea58b52 --- init/init.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/init/init.c b/init/init.c index cd129c3e0..7aef38721 100755 --- a/init/init.c +++ b/init/init.c @@ -243,13 +243,22 @@ void service_start(struct service *svc, const char *dynamic_args) /* as requested, set our gid, supplemental gids, and uid */ if (svc->gid) { - setgid(svc->gid); + if (setgid(svc->gid) != 0) { + ERROR("setgid failed: %s\n", strerror(errno)); + _exit(127); + } } if (svc->nr_supp_gids) { - setgroups(svc->nr_supp_gids, svc->supp_gids); + if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { + ERROR("setgroups failed: %s\n", strerror(errno)); + _exit(127); + } } if (svc->uid) { - setuid(svc->uid); + if (setuid(svc->uid) != 0) { + ERROR("setuid failed: %s\n", strerror(errno)); + _exit(127); + } } if (!dynamic_args) {