Skip to content

Commit

Permalink
Fix time-of-check time-of-use race condition
Browse files Browse the repository at this point in the history
Permission checking/setting shall be before opening a file.
  • Loading branch information
arkq committed Aug 3, 2021
1 parent 218236c commit 8f98d06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: CI - Build and Test
on: [push]
on:
push:
pull_request:
branches: [ master ]
jobs:
build:
strategy:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/codeql-analysis.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: CodeQL Analysis
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
Expand Down
14 changes: 9 additions & 5 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#if HAVE_SYS_INOTIFY_H
# include <sys/inotify.h>
#endif
Expand Down Expand Up @@ -129,14 +130,17 @@ int cmusfm_config_read(const char *fname, struct cmusfm_config *conf) {
/* Write cmusfm configuration to the file. */
int cmusfm_config_write(const char *fname, struct cmusfm_config *conf) {

int fd;
FILE *f;

/* create configuration file (truncate previous one) */
if ((f = fopen(fname, "w")) == NULL)
/* Create configuration file (truncate previous one) and set
* access mode to protect session key from exposure. */
if ((fd = creat(fname, S_IRUSR | S_IWUSR)) == -1)
return -1;

/* protect session key from exposure */
chmod(fname, S_IWUSR | S_IRUSR);
if ((f = fdopen(fd, "w")) == NULL) {
close(fd);
return -1;
}

fprintf(f, "# authentication\n");
fprintf(f, "%s = \"%s\"\n", CMCONF_USER_NAME, conf->user_name);
Expand Down

0 comments on commit 8f98d06

Please sign in to comment.