1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-02 08:57:19 -04:00

Secure file saving: restore umask after _all_ failure conditions

The secure file saving code plays some shenanigans with the umask.
Previously, the code could fail to restore the old umask when certain libc
calls failed: malloc, mkstemp, fdopen, and fopen. This resulted in
unrelated code creating files with the wrong umode. Specifically, the
download code's automatic directory creation was creating directories
without the execute permission bit.

Thanks to Quiznos for reporting and helping to track the problem down.
This commit is contained in:
Miciah Dashiel Butler Masters 2006-10-01 14:57:47 +00:00 committed by Miciah Dashiel Butler Masters
parent 461787013b
commit 7f0fb0440c

View File

@ -69,9 +69,8 @@ enum secsave_errno secsave_errno = SS_ERR_NONE;
/* Open a file for writing in a secure way. It returns a pointer to a structure /* Open a file for writing in a secure way. It returns a pointer to a structure
* secure_save_info on success, or NULL on failure. */ * secure_save_info on success, or NULL on failure. */
static struct secure_save_info * static struct secure_save_info *
secure_open_umask(unsigned char *file_name, mode_t mask) secure_open_umask(unsigned char *file_name)
{ {
mode_t saved_mask;
struct stat st; struct stat st;
struct secure_save_info *ssi; struct secure_save_info *ssi;
@ -144,8 +143,6 @@ secure_open_umask(unsigned char *file_name, mode_t mask)
} }
} }
saved_mask = umask(mask);
if (ssi->secure_save) { if (ssi->secure_save) {
/* We use a random name for temporary file, mkstemp() opens /* We use a random name for temporary file, mkstemp() opens
* the file and return a file descriptor named fd, which is * the file and return a file descriptor named fd, which is
@ -187,8 +184,6 @@ secure_open_umask(unsigned char *file_name, mode_t mask)
} }
} }
umask(saved_mask);
return ssi; return ssi;
free_file_name: free_file_name:
@ -206,12 +201,20 @@ end:
struct secure_save_info * struct secure_save_info *
secure_open(unsigned char *file_name) secure_open(unsigned char *file_name)
{ {
struct secure_save_info *ssi;
mode_t saved_mask;
#ifdef CONFIG_OS_WIN32 #ifdef CONFIG_OS_WIN32
/* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */ /* There is neither S_IRWXG nor S_IRWXO under crossmingw32-gcc */
return secure_open_umask(file_name, 0177); const mode_t mask = 0177;
#else #else
return secure_open_umask(file_name, S_IXUSR | S_IRWXG | S_IRWXO); const mode_t mask = S_IXUSR | S_IRWXG | S_IRWXO;
#endif #endif
saved_mask = umask(mask);
ssi = secure_open_umask(file_name);
umask(saved_mask);
return ssi;
} }
/* Close a file opened with secure_open, and return 0 on success, errno /* Close a file opened with secure_open, and return 0 on success, errno