Monitor: don't use default modes when creating a file

Replace fopen() calls by open() with creation mode directly specified.
This fixes the potential security issue. Use octal values instead masks.

Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
This commit is contained in:
Mariusz Tkaczyk 2020-11-24 16:41:01 +01:00 committed by Jes Sorensen
parent b65c1f4a23
commit ca4b156b20
1 changed files with 12 additions and 5 deletions

View File

@ -305,8 +305,11 @@ static int make_daemon(char *pidfile)
if (!pidfile) if (!pidfile)
printf("%d\n", pid); printf("%d\n", pid);
else { else {
FILE *pid_file; FILE *pid_file = NULL;
pid_file=fopen(pidfile, "w"); int fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC,
0644);
if (fd >= 0)
pid_file = fdopen(fd, "w");
if (!pid_file) if (!pid_file)
perror("cannot create pid file"); perror("cannot create pid file");
else { else {
@ -368,13 +371,17 @@ static void write_autorebuild_pid()
{ {
char path[PATH_MAX]; char path[PATH_MAX];
int pid; int pid;
FILE *fp; FILE *fp = NULL;
sprintf(path, "%s/autorebuild.pid", MDMON_DIR); sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
if (mkdir(MDMON_DIR, S_IRWXU) < 0 && errno != EEXIST) { if (mkdir(MDMON_DIR, 0700) < 0 && errno != EEXIST) {
pr_err("Can't create autorebuild.pid file\n"); pr_err("Can't create autorebuild.pid file\n");
} else { } else {
fp = fopen(path, "w"); int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0700);
if (fd >= 0)
fp = fdopen(fd, "w");
if (!fp) if (!fp)
pr_err("Can't create autorebuild.pid file\n"); pr_err("Can't create autorebuild.pid file\n");
else { else {