Ripped from Fedora. Get rid of a possible race condition: - close output file - bad man plays with output file - try to chmod output file Run the chmod on the file descriptor before we close it instead. --- gzip-1.3.5/gzip.c +++ gzip-1.3.5/gzip.c @@ -881,6 +881,20 @@ } close(ifd); + /* ofd ownership and permissions have to be set before close(ofd)*/ + if (!to_stdout) { + if (fchmod(ofd, istat.st_mode & 07777)) { + int e = errno; + WARN((stderr, "%s: ", progname)); + if (!quiet) { + errno = e; + perror(ofname); + } + } +#ifndef NO_CHOWN + fchown(ofd, istat.st_uid, istat.st_gid); /* Copy ownership */ +#endif + } if (!to_stdout && close(ofd)) { write_error(); } @@ -902,7 +916,7 @@ } fprintf(stderr, "\n"); } - /* Copy modes, times, ownership, and remove the input file */ + /* Copy times and remove the input file */ if (!to_stdout) { copy_stat(&istat); } @@ -1715,7 +1729,7 @@ /* ======================================================================== - * Copy modes, times, ownership from input file to output file. + * Copy times from input file to output file. * IN assertion: to_stdout is false. */ local void copy_stat(ifstat) @@ -1730,18 +1744,6 @@ } reset_times(ofname, ifstat); #endif - /* Copy the protection modes */ - if (chmod(ofname, ifstat->st_mode & 07777)) { - int e = errno; - WARN((stderr, "%s: ", progname)); - if (!quiet) { - errno = e; - perror(ofname); - } - } -#ifndef NO_CHOWN - chown(ofname, ifstat->st_uid, ifstat->st_gid); /* Copy ownership */ -#endif remove_ofname = 0; /* It's now safe to remove the input file: */ if (xunlink (ifname)) {