Dmitry Belyavskiy aa1b33
diff --git a/misc.c b/misc.c
Dmitry Belyavskiy aa1b33
index b8d1040d..0134d694 100644
Dmitry Belyavskiy aa1b33
--- a/misc.c
Dmitry Belyavskiy aa1b33
+++ b/misc.c
Dmitry Belyavskiy aa1b33
@@ -56,6 +56,7 @@
Dmitry Belyavskiy aa1b33
 #ifdef HAVE_PATHS_H
Dmitry Belyavskiy aa1b33
 # include <paths.h>
Dmitry Belyavskiy aa1b33
 #include <pwd.h>
Dmitry Belyavskiy aa1b33
+#include <grp.h>
Dmitry Belyavskiy aa1b33
 #endif
Dmitry Belyavskiy aa1b33
 #ifdef SSH_TUN_OPENBSD
Dmitry Belyavskiy aa1b33
 #include <net/if.h>
Dmitry Belyavskiy aa1b33
@@ -2695,6 +2696,12 @@ subprocess(const char *tag, const char *command,
Dmitry Belyavskiy aa1b33
 		}
Dmitry Belyavskiy aa1b33
 		closefrom(STDERR_FILENO + 1);
Dmitry Belyavskiy aa1b33
 
Dmitry Belyavskiy aa1b33
+		if (geteuid() == 0 &&
Dmitry Belyavskiy aa1b33
+		    initgroups(pw->pw_name, pw->pw_gid) == -1) {
Dmitry Belyavskiy aa1b33
+			error("%s: initgroups(%s, %u): %s", tag,
Dmitry Belyavskiy aa1b33
+			    pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
Dmitry Belyavskiy aa1b33
+			_exit(1);
Dmitry Belyavskiy aa1b33
+		}
Dmitry Belyavskiy aa1b33
 		if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) {
Dmitry Belyavskiy aa1b33
 			error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
Dmitry Belyavskiy aa1b33
 			    strerror(errno));