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 <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-24 02:41:26 +00:00
parent a5eed39a76
commit 2c21a04741
3 changed files with 27 additions and 10 deletions

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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