From 23d04dde2496ffcd527a0bb5bc81faf4c85dbd9e Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 21 Sep 2021 19:09:21 -0600 Subject: [PATCH] iolog_nextid(): make iolog_dir argument const. We make a copy of the directory so there's no real reason that parameter can't be const. --- include/sudo_iolog.h | 2 +- lib/iolog/iolog_nextid.c | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/sudo_iolog.h b/include/sudo_iolog.h index 16e0c3a97..e405e9665 100644 --- a/include/sudo_iolog.h +++ b/include/sudo_iolog.h @@ -111,7 +111,7 @@ bool iolog_close(struct iolog_file *iol, const char **errstr); bool iolog_eof(struct iolog_file *iol); bool iolog_mkdtemp(char *path); bool iolog_mkpath(char *path); -bool iolog_nextid(char *iolog_dir, char sessid[7]); +bool iolog_nextid(const char *iolog_dir, char sessid[7]); bool iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode); bool iolog_write_info_file(int dfd, struct eventlog *evlog); char *iolog_gets(struct iolog_file *iol, char *buf, size_t nbytes, const char **errsttr); diff --git a/lib/iolog/iolog_nextid.c b/lib/iolog/iolog_nextid.c index 5ca0ca691..8977a35f6 100644 --- a/lib/iolog/iolog_nextid.c +++ b/lib/iolog/iolog_nextid.c @@ -50,11 +50,12 @@ * Uses file locking to avoid sequence number collisions. */ bool -iolog_nextid(char *iolog_dir, char sessid[7]) +iolog_nextid(const char *iolog_dir, char sessid[7]) { char buf[32], *ep; - int i, len, fd = -1; + int i, fd = -1; unsigned long id = 0; + size_t len; ssize_t nread; bool ret = false; char pathbuf[PATH_MAX]; @@ -66,14 +67,21 @@ iolog_nextid(char *iolog_dir, char sessid[7]) /* * Create I/O log directory if it doesn't already exist. */ - if (!iolog_mkdirs(iolog_dir)) + len = strlcpy(pathbuf, iolog_dir, sizeof(pathbuf)); + if (len >= sizeof(pathbuf)) { + errno = ENAMETOOLONG; + sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, + "%s: %s", __func__, iolog_dir); + goto done; + } + if (!iolog_mkdirs(pathbuf)) goto done; /* * Open sequence file */ - len = snprintf(pathbuf, sizeof(pathbuf), "%s/seq", iolog_dir); - if (len < 0 || len >= ssizeof(pathbuf)) { + len = strlcat(pathbuf, "/seq", sizeof(pathbuf)); + if (len >= sizeof(pathbuf)) { errno = ENAMETOOLONG; sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO, "%s: %s/seq", __func__, iolog_dir);