From 55cf5a255a4b65bc898e5287fa9d939768997dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Mon, 12 Apr 2021 14:07:14 +0200 Subject: [PATCH] zed: set O_CLOEXEC on persistent fds, remove closefrom() from pre-exec MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also don't dup /dev/null over stdio if daemonised Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia ZiemiaƄska Closes #11891 --- cmd/zed/zed_conf.c | 11 ++++------- cmd/zed/zed_event.c | 2 +- cmd/zed/zed_exec.c | 9 ++++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/cmd/zed/zed_conf.c b/cmd/zed/zed_conf.c index b66b67b530..b95108fd2f 100644 --- a/cmd/zed/zed_conf.c +++ b/cmd/zed/zed_conf.c @@ -425,8 +425,6 @@ zed_conf_scan_dir(struct zed_conf *zcp) int zed_conf_write_pid(struct zed_conf *zcp) { - const mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - const mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char buf[PATH_MAX]; int n; char *p; @@ -454,7 +452,7 @@ zed_conf_write_pid(struct zed_conf *zcp) if (p) *p = '\0'; - if ((mkdirp(buf, dirmode) < 0) && (errno != EEXIST)) { + if ((mkdirp(buf, 0755) < 0) && (errno != EEXIST)) { zed_log_msg(LOG_ERR, "Failed to create directory \"%s\": %s", buf, strerror(errno)); goto err; @@ -464,7 +462,7 @@ zed_conf_write_pid(struct zed_conf *zcp) */ mask = umask(0); umask(mask | 022); - zcp->pid_fd = open(zcp->pid_file, (O_RDWR | O_CREAT), filemode); + zcp->pid_fd = open(zcp->pid_file, O_RDWR | O_CREAT | O_CLOEXEC, 0644); umask(mask); if (zcp->pid_fd < 0) { zed_log_msg(LOG_ERR, "Failed to open PID file \"%s\": %s", @@ -529,7 +527,6 @@ int zed_conf_open_state(struct zed_conf *zcp) { char dirbuf[PATH_MAX]; - mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; int n; char *p; int rv; @@ -551,7 +548,7 @@ zed_conf_open_state(struct zed_conf *zcp) if (p) *p = '\0'; - if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) { + if ((mkdirp(dirbuf, 0755) < 0) && (errno != EEXIST)) { zed_log_msg(LOG_WARNING, "Failed to create directory \"%s\": %s", dirbuf, strerror(errno)); @@ -569,7 +566,7 @@ zed_conf_open_state(struct zed_conf *zcp) (void) unlink(zcp->state_file); zcp->state_fd = open(zcp->state_file, - (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); + O_RDWR | O_CREAT | O_CLOEXEC, 0644); if (zcp->state_fd < 0) { zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s", zcp->state_file, strerror(errno)); diff --git a/cmd/zed/zed_event.c b/cmd/zed/zed_event.c index 5e28bb22ce..232b88a138 100644 --- a/cmd/zed/zed_event.c +++ b/cmd/zed/zed_event.c @@ -54,7 +54,7 @@ zed_event_init(struct zed_conf *zcp) zed_log_die("Failed to initialize libzfs"); } - zcp->zevent_fd = open(ZFS_DEV, O_RDWR); + zcp->zevent_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); if (zcp->zevent_fd < 0) { if (zcp->do_idle) return (-1); diff --git a/cmd/zed/zed_exec.c b/cmd/zed/zed_exec.c index b1756c5386..8c8452ca7a 100644 --- a/cmd/zed/zed_exec.c +++ b/cmd/zed/zed_exec.c @@ -27,7 +27,6 @@ #include #include #include "zed_exec.h" -#include "zed_file.h" #include "zed_log.h" #include "zed_strings.h" @@ -116,7 +115,7 @@ _zed_exec_create_env(zed_strings_t *zsp) */ static void _zed_exec_fork_child(uint64_t eid, const char *dir, const char *prog, - char *env[], int zfd) + char *env[], int zfd, boolean_t in_foreground) { char path[PATH_MAX]; int n; @@ -154,13 +153,13 @@ _zed_exec_fork_child(uint64_t eid, const char *dir, const char *prog, (void) sigprocmask(SIG_SETMASK, &mask, NULL); (void) umask(022); - if ((fd = open("/dev/null", O_RDWR)) != -1) { + if (in_foreground && /* we're already devnulled if daemonised */ + (fd = open("/dev/null", O_RDWR | O_CLOEXEC)) != -1) { (void) dup2(fd, STDIN_FILENO); (void) dup2(fd, STDOUT_FILENO); (void) dup2(fd, STDERR_FILENO); } (void) dup2(zfd, ZEVENT_FILENO); - zed_file_close_from(ZEVENT_FILENO + 1); execle(path, prog, NULL, env); _exit(127); } @@ -359,7 +358,7 @@ zed_exec_process(uint64_t eid, const char *class, const char *subclass, n = strlen(*csp); if ((strncmp(z, *csp, n) == 0) && !isalpha(z[n])) _zed_exec_fork_child(eid, zcp->zedlet_dir, - z, e, zcp->zevent_fd); + z, e, zcp->zevent_fd, zcp->do_foreground); } } free(e);