--- emacs22-22.2/lib-src/movemail.c.orig 2010-03-03 16:44:40.000000000 -0500 +++ emacs22-22.2/lib-src/movemail.c 2010-03-04 16:31:17.000000000 -0500 @@ -177,6 +177,10 @@ int nread; WAITTYPE status; int c, preserve_mail = 0; + uid_t real_gid, priv_gid; + + real_gid = getgid(); + priv_gid = getegid(); #ifndef MAIL_USE_SYSTEM_LOCK struct stat st; @@ -248,25 +252,6 @@ if (*outname == 0) fatal ("Destination file name is empty", 0, 0); - /* Check access to output file. */ - if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0) - pfatal_with_name (outname); - - /* Also check that outname's directory is writable to the real uid. */ - { - char *buf = (char *) xmalloc (strlen (outname) + 1); - char *p; - strcpy (buf, outname); - p = buf + strlen (buf); - while (p > buf && !IS_DIRECTORY_SEP (p[-1])) - *--p = 0; - if (p == buf) - *p++ = '.'; - if (access (buf, W_OK) != 0) - pfatal_with_name (buf); - free (buf); - } - #ifdef MAIL_USE_POP if (!strncmp (inname, "po:", 3)) { @@ -278,15 +263,12 @@ exit (status); } - setuid (getuid ()); + if ( setuid (getuid ()) < 0 ) + fatal ("Failed to drop privileges", 0, 0); + #endif /* MAIL_USE_POP */ #ifndef DISABLE_DIRECT_ACCESS - - /* Check access to input file. */ - if (access (inname, R_OK | W_OK) != 0) - pfatal_with_name (inname); - #ifndef MAIL_USE_MMDF #ifndef MAIL_USE_SYSTEM_LOCK #ifdef MAIL_USE_MAILLOCK @@ -373,7 +355,8 @@ long touched_lock, now; #endif - setuid (getuid ()); + if ( setuid (getuid ()) < 0 || setegid (real_gid) < 0 ) + fatal("Failed to drop privileges", 0, 0); #ifndef MAIL_USE_MMDF #ifdef MAIL_USE_SYSTEM_LOCK @@ -399,6 +382,9 @@ if (outdesc < 0) pfatal_with_name (outname); + if ( setegid(priv_gid) < 0 ) + fatal("Failed to regain privileges", 0, 0); + /* This label exists so we can retry locking after a delay, if it got EAGAIN or EBUSY. */ retry_lock: