X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Fuser-util.c;h=893790f78f861ac7affb9158223195634246601c;hp=0d199cf18da7dbfbce93fbae2206cba66aa8d4a9;hb=1e2c11636861da69c5637e59afe207a11dd4f386;hpb=4d1a9dbea12127dd8fd8c0ee9a53b544ef92f6bf diff --git a/src/basic/user-util.c b/src/basic/user-util.c index 0d199cf18..893790f78 100644 --- a/src/basic/user-util.c +++ b/src/basic/user-util.c @@ -463,9 +463,11 @@ int get_shell(char **_s) { #endif // 0 int reset_uid_gid(void) { + int r; - if (setgroups(0, NULL) < 0) - return -errno; + r = maybe_setgroups(0, NULL); + if (r < 0) + return r; if (setresgid(0, 0, 0) < 0) return -errno; @@ -608,3 +610,32 @@ bool valid_home(const char *p) { return true; } + +int maybe_setgroups(size_t size, const gid_t *list) { + int r; + + /* Check if setgroups is allowed before we try to drop all the auxiliary groups */ + if (size == 0) { /* Dropping all aux groups? */ + _cleanup_free_ char *setgroups_content = NULL; + bool can_setgroups; + + r = read_one_line_file("/proc/self/setgroups", &setgroups_content); + if (r == -ENOENT) + /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */ + can_setgroups = true; + else if (r < 0) + return r; + else + can_setgroups = streq(setgroups_content, "allow"); + + if (!can_setgroups) { + log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'"); + return 0; + } + } + + if (setgroups(size, list) < 0) + return -errno; + + return 0; +}