Handle a missing run_time in an ExitMessage.

It is now possible to pass a NULL run_time to eventlog_exit().
This commit is contained in:
Todd C. Miller
2021-10-18 15:34:48 -06:00
parent f73bc6dee3
commit 77c339858d
2 changed files with 23 additions and 15 deletions

View File

@@ -900,10 +900,12 @@ format_json(int event_type, struct eventlog_args *args,
} }
/* Log event time from client */ /* Log event time from client */
if (!json_add_timestamp(&json, time_str, args->event_time, format_timestamp)) { if (args->event_time != NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, if (!json_add_timestamp(&json, time_str, args->event_time, format_timestamp)) {
"unable format timestamp"); sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
goto bad; "unable format timestamp");
goto bad;
}
} }
if (event_type == EVLOG_EXIT) { if (event_type == EVLOG_EXIT) {
@@ -1136,7 +1138,6 @@ do_logfile_sudo(const char *logline, const struct eventlog *evlog,
char *full_line, timebuf[8192], *timestr = NULL; char *full_line, timebuf[8192], *timestr = NULL;
const char *timefmt = evl_conf->time_fmt; const char *timefmt = evl_conf->time_fmt;
const char *logfile = evl_conf->logpath; const char *logfile = evl_conf->logpath;
time_t tv_sec = event_time->tv_sec;
struct tm tm; struct tm tm;
bool ret = false; bool ret = false;
FILE *fp; FILE *fp;
@@ -1152,12 +1153,15 @@ do_logfile_sudo(const char *logline, const struct eventlog *evlog,
goto done; goto done;
} }
if (localtime_r(&tv_sec, &tm) != NULL) { if (event_time != NULL) {
/* strftime() does not guarantee to NUL-terminate so we must check. */ time_t tv_sec = event_time->tv_sec;
timebuf[sizeof(timebuf) - 1] = '\0'; if (localtime_r(&tv_sec, &tm) != NULL) {
if (strftime(timebuf, sizeof(timebuf), timefmt, &tm) != 0 && /* strftime() does not guarantee to NUL-terminate so we must check. */
timebuf[sizeof(timebuf) - 1] == '\0') { timebuf[sizeof(timebuf) - 1] = '\0';
timestr = timebuf; if (strftime(timebuf, sizeof(timebuf), timefmt, &tm) != 0 &&
timebuf[sizeof(timebuf) - 1] == '\0') {
timestr = timebuf;
}
} }
} }
len = asprintf(&full_line, "%s : %s : %s", len = asprintf(&full_line, "%s : %s : %s",

View File

@@ -264,12 +264,16 @@ bool
store_exit_local(ExitMessage *msg, uint8_t *buf, size_t len, store_exit_local(ExitMessage *msg, uint8_t *buf, size_t len,
struct connection_closure *closure) struct connection_closure *closure)
{ {
struct timespec ts, *run_time = NULL;
const char *signame = NULL; const char *signame = NULL;
struct timespec run_time = { msg->run_time->tv_sec, msg->run_time->tv_nsec };
int flags = 0; int flags = 0;
mode_t mode;
debug_decl(store_exit_local, SUDO_DEBUG_UTIL); debug_decl(store_exit_local, SUDO_DEBUG_UTIL);
if (msg->run_time != NULL) {
ts.tv_sec = msg->run_time->tv_sec;
ts.tv_nsec = msg->run_time->tv_nsec;
run_time = &ts;
}
if (msg->signal != NULL && msg->signal[0] != '\0') { if (msg->signal != NULL && msg->signal[0] != '\0') {
signame = msg->signal; signame = msg->signal;
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO, sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
@@ -280,7 +284,7 @@ store_exit_local(ExitMessage *msg, uint8_t *buf, size_t len,
"command exited with %d", msg->exit_value); "command exited with %d", msg->exit_value);
} }
if (logsrvd_conf_log_exit()) { if (logsrvd_conf_log_exit()) {
if (!eventlog_exit(closure->evlog, flags, &run_time, msg->exit_value, if (!eventlog_exit(closure->evlog, flags, run_time, msg->exit_value,
signame, msg->dumped_core)) { signame, msg->dumped_core)) {
closure->errstr = _("error logging exit event"); closure->errstr = _("error logging exit event");
debug_return_bool(false); debug_return_bool(false);
@@ -289,7 +293,7 @@ store_exit_local(ExitMessage *msg, uint8_t *buf, size_t len,
if (closure->log_io) { if (closure->log_io) {
/* Clear write bits from I/O timing file to indicate completion. */ /* Clear write bits from I/O timing file to indicate completion. */
mode = logsrvd_conf_iolog_mode(); mode_t mode = logsrvd_conf_iolog_mode();
CLR(mode, S_IWUSR|S_IWGRP|S_IWOTH); CLR(mode, S_IWUSR|S_IWGRP|S_IWOTH);
if (fchmodat(closure->iolog_dir_fd, "timing", mode, 0) == -1) { if (fchmodat(closure->iolog_dir_fd, "timing", mode, 0) == -1) {
sudo_warn("chmod 0%o %s/%s", (unsigned int)mode, "timing", sudo_warn("chmod 0%o %s/%s", (unsigned int)mode, "timing",