Description: fix unsafe race condition in opening output files. Author: Colin Phipps Origin: Debian: https://salsa.debian.org/debian/bzip2/blob/6baf99d2eb446f50f7986868b7d57e6f4fd9b459/debian/patches/bzip2recover-race-open-output.diff https://sources.debian.org/src/bzip2/1.0.6-9/debian/patches/bzip2recover-race-open-output.diff/ --- a/bzip2recover.c +++ b/bzip2recover.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include /* This program records bit locations in the file to be recovered. @@ -269,6 +271,19 @@ name[n-1] == '2'); } +/*---------------------------------------------*/ +/* Open an output file safely with O_EXCL and good permissions */ +FILE* fopen_output( Char* name, const char* mode ) +{ + FILE *fp; + int fh; + + fh = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600); + if (fh == -1) return NULL; + fp = fdopen(fh, mode); + if (fp == NULL) close(fh); + return fp; +} /*---------------------------------------------------*/ /*--- ---*/ @@ -486,7 +501,7 @@ fprintf ( stderr, " writing block %d to `%s' ...\n", wrBlock+1, outFileName ); - outFile = fopen ( outFileName, "wb" ); + outFile = fopen_output ( outFileName, "wb" ); if (outFile == NULL) { fprintf ( stderr, "%s: can't write `%s'\n", progName, outFileName );