Fix fd leak in do_logfile() if we fail to lock the log file.

Coverity CID 104115.
This commit is contained in:
Todd C. Miller
2016-05-06 09:12:39 -06:00
parent 7a5149d738
commit 0690793c25

View File

@@ -143,10 +143,11 @@ do_syslog(int pri, char *msg)
static bool static bool
do_logfile(const char *msg) do_logfile(const char *msg)
{ {
const char *timestr;
int len, oldlocale;
bool rval = false;
char *full_line; char *full_line;
mode_t oldmask; mode_t oldmask;
bool rval = false;
int len, oldlocale;
FILE *fp; FILE *fp;
debug_decl(do_logfile, SUDOERS_DEBUG_LOGGING) debug_decl(do_logfile, SUDOERS_DEBUG_LOGGING)
@@ -158,11 +159,15 @@ do_logfile(const char *msg)
if (fp == NULL) { if (fp == NULL) {
send_mail(_("unable to open log file: %s: %s"), send_mail(_("unable to open log file: %s: %s"),
def_logfile, strerror(errno)); def_logfile, strerror(errno));
} else if (!sudo_lock_file(fileno(fp), SUDO_LOCK)) { goto done;
}
if (!sudo_lock_file(fileno(fp), SUDO_LOCK)) {
send_mail(_("unable to lock log file: %s: %s"), send_mail(_("unable to lock log file: %s: %s"),
def_logfile, strerror(errno)); def_logfile, strerror(errno));
} else { goto done;
const char *timestr = get_timestr(time(NULL), def_log_year); }
timestr = get_timestr(time(NULL), def_log_year);
if (timestr == NULL) if (timestr == NULL)
timestr = "invalid date"; timestr = "invalid date";
if (def_log_host) { if (def_log_host) {
@@ -172,7 +177,10 @@ do_logfile(const char *msg)
len = asprintf(&full_line, "%s : %s : %s", len = asprintf(&full_line, "%s : %s : %s",
timestr, user_name, msg); timestr, user_name, msg);
} }
if (len != -1) { if (len == -1) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
if ((size_t)def_loglinelen < sizeof(LOG_INDENT)) { if ((size_t)def_loglinelen < sizeof(LOG_INDENT)) {
/* Don't pretty-print long log file lines (hard to grep). */ /* Don't pretty-print long log file lines (hard to grep). */
(void) fputs(full_line, fp); (void) fputs(full_line, fp);
@@ -185,11 +193,10 @@ do_logfile(const char *msg)
(void) fflush(fp); (void) fflush(fp);
if (!ferror(fp)) if (!ferror(fp))
rval = true; rval = true;
} else {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); done:
} if (fp != NULL)
(void) fclose(fp); (void) fclose(fp);
}
sudoers_setlocale(oldlocale, NULL); sudoers_setlocale(oldlocale, NULL);
debug_return_bool(rval); debug_return_bool(rval);