From: Todd C. Miller Date: Fri, 6 May 2016 14:11:34 +0000 (-0600) Subject: Fix fd leak in open_io_fd() if gzdopen/fdopen fails. X-Git-Tag: SUDO_1_8_17^2~105 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4bfdf0c2b410bfbe2a2dfc08abe174de0dfd3a91;p=sudo Fix fd leak in open_io_fd() if gzdopen/fdopen fails. Coverity CID 104105 --- diff --git a/plugins/sudoers/iolog.c b/plugins/sudoers/iolog.c index cb2b8d453..5f00a98f1 100644 --- a/plugins/sudoers/iolog.c +++ b/plugins/sudoers/iolog.c @@ -325,13 +325,12 @@ mkdir_iopath(const char *iolog_path, char *pathbuf, size_t pathsize) static bool open_io_fd(char *pathbuf, size_t len, struct io_log_file *iol, bool docompress) { - int fd; debug_decl(open_io_fd, SUDOERS_DEBUG_UTIL) pathbuf[len] = '\0'; strlcat(pathbuf, iol->suffix, PATH_MAX); if (iol->enabled) { - fd = open(pathbuf, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR); + int fd = open(pathbuf, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR); if (fd != -1) { (void)fcntl(fd, F_SETFD, FD_CLOEXEC); #ifdef HAVE_ZLIB_H @@ -340,8 +339,12 @@ open_io_fd(char *pathbuf, size_t len, struct io_log_file *iol, bool docompress) else #endif iol->fd.f = fdopen(fd, "w"); + if (iol->fd.v == NULL) { + close(fd); + fd = -1; + } } - if (fd == -1 || iol->fd.v == NULL) { + if (fd == -1) { log_warning(SLOG_SEND_MAIL, N_("unable to create %s"), pathbuf); debug_return_bool(false); }