backport of https://github.com/jjk-jacky/pam_rundir/pull/8

diff --git a/pam_rundir.c b/pam_rundir.c
index d36584d..f92c73e 100644
--- a/pam_rundir.c
+++ b/pam_rundir.c
@@ -404,8 +404,8 @@
         secbits = prctl (PR_GET_SECUREBITS);
         if (secbits != -1)
             prctl (PR_SET_SECUREBITS, (unsigned long) secbits | SECBIT_NO_SETUID_FIXUP);
-        /* set euid so if we do create the dir, it is own by the user */
-        if (seteuid (pw->pw_uid) < 0)
+        /* set euid and egid so if we do create the dir, it is owned by the user */
+        if (seteuid (pw->pw_uid) < 0 || setegid (pw->pw_gid) < 0)
         {
             r = -1;
             goto done;
@@ -422,7 +422,7 @@
             pam_putenv (pamh, buf);
         }
         /* restore */
-        if (seteuid (0) < 0)
+        if (seteuid (0) < 0 || setegid (0) < 0)
         {
             r = -1;
             goto done;
