From 2c21a04741eed260570c7ed3ded08c639e65a689 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 24 Mar 2026 02:41:26 +0000 Subject: util/mkhtemp: show path on error accessing it a bit naughty the way i do it, but it works. without this, the message gets clobbered by EINVAL due to a bad call to vprintf in the err function. in this way, we ensure that there is a path, and thus the errno does not get clobbered. i also removed the EPERM setting in the env_tmpdir function, which also clobbered errno. with this fix, if TMPDIR is set but invalid, it should now show the error reliably. Signed-off-by: Leah Rowe --- util/libreboot-utils/include/common.h | 2 +- util/libreboot-utils/lib/mkhtemp.c | 33 +++++++++++++++++++++++++-------- util/libreboot-utils/mkhtemp.c | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'util/libreboot-utils') diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index 5d6405bc..fa07da7f 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -517,7 +517,7 @@ int world_writeable_and_sticky(const char *s, int same_dir(const char *a, const char *b); int tmpdir_policy(const char *path, int *allow_noworld_unsticky); -char *env_tmpdir(int always_sticky); +char *env_tmpdir(int always_sticky, char **tmpdir); int secure_file(int *fd, struct stat *st, struct stat *expected, diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c index 8d58dcc5..e1ca0a10 100644 --- a/util/libreboot-utils/lib/mkhtemp.c +++ b/util/libreboot-utils/lib/mkhtemp.c @@ -63,6 +63,8 @@ new_tmp_common(int *fd, char **path, int type) struct stat st_dir_initial; + char *fail_dir = NULL; + if (path == NULL || fd == NULL) { errno = EFAULT; goto err; @@ -89,9 +91,9 @@ new_tmp_common(int *fd, char **path, int type) #if defined(PERMIT_NON_STICKY_ALWAYS) && \ ((PERMIT_NON_STICKY_ALWAYS) > 0) - tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS); + tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS, &fail_dir); #else - tmpdir = env_tmpdir(0); + tmpdir = env_tmpdir(0, &fail_dir); #endif if (tmpdir == NULL) goto err; @@ -176,6 +178,13 @@ err: *fd = -1; } + /* where a TMPDIR isn't found, and we err, + * we pass this back through for the + * error message + */ + if (fail_dir != NULL) + *path = fail_dir; + errno = saved_errno; return -1; } @@ -185,26 +194,31 @@ err: */ char * -env_tmpdir(int bypass_all_sticky_checks) +env_tmpdir(int bypass_all_sticky_checks, char **tmpdir) { char *t; int allow_noworld_unsticky; int saved_errno = errno; + char tmp[] = "/tmp"; + char vartmp[] = "/var/tmp"; + t = getenv("TMPDIR"); if (t != NULL && *t != '\0') { if (tmpdir_policy(t, &allow_noworld_unsticky) < 0) { - errno = EPERM; + if (tmpdir != NULL) + *tmpdir = t; return NULL; /* errno already set */ } if (!world_writeable_and_sticky(t, allow_noworld_unsticky, bypass_all_sticky_checks)) { - errno = EPERM; + if (tmpdir != NULL) + *tmpdir = t; return NULL; } @@ -218,6 +232,9 @@ env_tmpdir(int bypass_all_sticky_checks) allow_noworld_unsticky, bypass_all_sticky_checks)) { + if (tmpdir != NULL) + *tmpdir = tmp; + errno = saved_errno; return "/tmp"; } @@ -226,13 +243,13 @@ env_tmpdir(int bypass_all_sticky_checks) allow_noworld_unsticky, bypass_all_sticky_checks)) { + if (tmpdir != NULL) + *tmpdir = vartmp; + errno = saved_errno; return "/var/tmp"; } - if (errno == saved_errno) - errno = EPERM; - return NULL; } diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c index 5e7fcc0f..3e148a4a 100644 --- a/util/libreboot-utils/mkhtemp.c +++ b/util/libreboot-utils/mkhtemp.c @@ -104,7 +104,7 @@ main(int argc, char *argv[]) } if (new_tmp_common(&fd, &s, type) < 0) - err_no_cleanup(errno, NULL); + err_no_cleanup(errno, "%s", s); #if defined(__OpenBSD__) && defined(OpenBSD) #if (OpenBSD) >= 509 -- cgit v1.2.1