From 416046e2ad67508869ce472d8b8b0dd5578db0d7 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 29 May 2024 17:49:21 -0700 Subject: [PATCH] tftpd: call setgroups() before initgroups() Unconditionally call setgroups() before calling initgroups(). That way if initgroups() fails for some reason (e.g. it is unable to access /etc/groups or the equivalent) then at least the group list will be empty. Signed-off-by: H. Peter Anvin --- tftpd/tftpd.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index d9b3f46..2db7977 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -980,23 +980,20 @@ int main(int argc, char **argv) /etc/group still need to be accessible at this point. If we get EPERM, this is already a restricted process, e.g. using user namespaces on Linux. */ - setrv = -1; die = 0; +#ifdef HAVE_SETGROUPS + setrv = setgroups(0, NULL); + if (setrv && errno != EPERM) { + syslog(LOG_ERR, "cannot clear group list"); + die = EX_OSERR; + } +#endif #ifdef HAVE_INITGROUPS setrv = initgroups(user, pw->pw_gid); if (setrv && errno != EPERM) { syslog(LOG_ERR, "cannot set groups for user %s", user); die = EX_OSERR; } -#endif -#ifdef HAVE_SETGROUPS - if (setrv) { - setrv = setgroups(0, NULL); - if (setrv && errno != EPERM) { - syslog(LOG_ERR, "cannot clear group list"); - die = EX_OSERR; - } - } #endif if (die) exit(die);