Skip to content

Commit

Permalink
Merge pull request #2495 from felixhandte/umask
Browse files Browse the repository at this point in the history
Use umask() to Constrain Created File Permissions
  • Loading branch information
felixhandte authored Feb 17, 2021
2 parents b2bff75 + a774c57 commit a2adc6d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
9 changes: 3 additions & 6 deletions programs/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,11 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
FIO_removeFile(dstFileName);
} }

{ FILE* const f = fopen( dstFileName, "wb" );
{ const int old_umask = UTIL_umask(0177); /* u-x,go-rwx */
FILE* const f = fopen( dstFileName, "wb" );
UTIL_umask(old_umask);
if (f == NULL) {
DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
} else if (srcFileName != NULL
&& strcmp (srcFileName, stdinmark)
&& strcmp(dstFileName, nulmark) ) {
/* reduce rights on newly created dst file while compression is ongoing */
UTIL_chmod(dstFileName, NULL, 00600);
}
return f;
}
Expand Down
9 changes: 9 additions & 0 deletions programs/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
return chmod(filename, permissions);
}

int UTIL_umask(int mode) {
#if PLATFORM_POSIX_VERSION > 0
return umask(mode);
#else
/* do nothing, fake return value */
return mode;
#endif
}

int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
{
int res = 0;
Expand Down
7 changes: 6 additions & 1 deletion programs/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {
#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
#include <stddef.h> /* size_t, ptrdiff_t */
#include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat, chmod */
#include <sys/stat.h> /* stat, chmod, umask */
#include "../lib/common/mem.h" /* U64 */


Expand Down Expand Up @@ -152,6 +152,11 @@ U64 UTIL_getFileSizeStat(const stat_t* statbuf);
*/
int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);

/**
* Wraps umask(). Does nothing when the platform doesn't have that concept.
*/
int UTIL_umask(int mode);

/*
* In the absence of a pre-existing stat result on the file in question, these
* functions will do a stat() call internally and then use that result to
Expand Down

0 comments on commit a2adc6d

Please sign in to comment.