Skip to content

Commit ba1d5b7

Browse files
committed
sudoers_io_open_local: Close iolog_dir_fd and iolog_files[] on error
Found by the ZeroPath AI Security Engineer <https://zeropath.com>
1 parent 658bbc4 commit ba1d5b7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

plugins/sudoers/iolog.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ sudoers_io_open_local(struct timespec *start_time)
684684
{
685685
const struct sudoers_context *ctx = sudoers_get_context();
686686
struct eventlog *evlog = iolog_details.evlog;
687-
int i, ret = -1;
687+
int i;
688688
debug_decl(sudoers_io_open_local, SUDOERS_DEBUG_PLUGIN);
689689

690690
/* If no I/O log path defined we need to figure it out ourselves. */
@@ -696,15 +696,15 @@ sudoers_io_open_local(struct timespec *start_time)
696696
log_warning(ctx, SLOG_SEND_MAIL,
697697
N_("unable to update sequence file"));
698698
warned = true;
699-
goto done;
699+
goto bad;
700700
}
701701
len = asprintf(&evlog->iolog_path, "%s/%c%c/%c%c/%c%c",
702702
_PATH_SUDO_IO_LOGDIR,
703703
evlog->sessid[0], evlog->sessid[1], evlog->sessid[2],
704704
evlog->sessid[3], evlog->sessid[4], evlog->sessid[5]);
705705
if (len == -1) {
706706
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
707-
goto done;
707+
goto bad;
708708
}
709709
}
710710

@@ -715,23 +715,23 @@ sudoers_io_open_local(struct timespec *start_time)
715715
if (!iolog_mkpath(evlog->iolog_path)) {
716716
log_warning(ctx, SLOG_SEND_MAIL, "%s", evlog->iolog_path);
717717
warned = true;
718-
goto done;
718+
goto bad;
719719
}
720720

721721
iolog_dir_fd =
722722
iolog_openat(AT_FDCWD, evlog->iolog_path, O_RDONLY|O_DIRECTORY);
723723
if (iolog_dir_fd == -1) {
724724
log_warning(ctx, SLOG_SEND_MAIL, "%s", evlog->iolog_path);
725725
warned = true;
726-
goto done;
726+
goto bad;
727727
}
728728

729729
/* Write log file with user and command details. */
730730
if (!iolog_write_info_file(iolog_dir_fd, iolog_details.evlog)) {
731731
log_warningx(ctx, SLOG_SEND_MAIL,
732732
N_("unable to write to I/O log file: %s"), strerror(errno));
733733
warned = true;
734-
goto done;
734+
goto bad;
735735
}
736736

737737
/* Create the timing and I/O log files. */
@@ -740,14 +740,24 @@ sudoers_io_open_local(struct timespec *start_time)
740740
log_warning(ctx, SLOG_SEND_MAIL, N_("unable to create %s/%s"),
741741
evlog->iolog_path, iolog_fd_to_name(i));
742742
warned = true;
743-
goto done;
743+
goto bad;
744744
}
745745
}
746746

747-
ret = true;
747+
debug_return_int(true);
748748

749-
done:
750-
debug_return_int(ret);
749+
bad:
750+
if (iolog_dir_fd != -1) {
751+
for (i = 0; i < IOFD_MAX; i++) {
752+
if (iolog_files[i].fd.v == NULL)
753+
continue;
754+
iolog_close(&iolog_files[i], NULL);
755+
iolog_files[i].fd.v = NULL;
756+
}
757+
close(iolog_dir_fd);
758+
iolog_dir_fd = -1;
759+
}
760+
debug_return_int(-1);
751761
}
752762

753763
#ifdef SUDOERS_LOG_CLIENT

0 commit comments

Comments
 (0)