From 2eed956396560e528749ba50c7581fe2500b7e2c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 31 Oct 2014 06:57:17 -0600 Subject: [PATCH] Use a static buffer for sudo_debug_execve2() if possible. --- lib/util/sudo_debug.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/util/sudo_debug.c b/lib/util/sudo_debug.c index ae693f088..e82380081 100644 --- a/lib/util/sudo_debug.c +++ b/lib/util/sudo_debug.c @@ -674,7 +674,7 @@ sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *con struct sudo_debug_instance *instance; struct sudo_debug_output *output; char * const *av; - char *buf, *cp; + char *cp, static_buf[4096], *buf = static_buf; size_t plen; if (sudo_debug_last_instance == -1) @@ -706,7 +706,6 @@ sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *con if (subsys > instance->max_subsystem) goto out; - /* XXX - use static buffer if possible */ SLIST_FOREACH(output, &instance->outputs, entries) { bool log_envp = false; @@ -733,9 +732,11 @@ sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *con buflen += strlen(*av) + 1; buflen--; } - buf = malloc(buflen + 1); - if (buf == NULL) - goto out; + if (buflen >= (int)sizeof(static_buf)) { + buf = malloc(buflen + 1); + if (buf == NULL) + goto out; + } /* Copy prefix and command. */ memcpy(buf, EXEC_PREFIX, sizeof(EXEC_PREFIX) - 1); @@ -771,7 +772,10 @@ sudo_debug_execve2_v1(int level, const char *path, char *const argv[], char *con *cp = '\0'; sudo_debug_write(output->fd, buf, buflen, 0); - free(buf); + if (buf != static_buf) { + sudo_efree(buf); + buf = static_buf; + } } out: errno = saved_errno; -- 2.40.0