mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-03-25 13:29:03 +02:00
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:
@@ -517,7 +517,7 @@ int world_writeable_and_sticky(const char *s,
|
|||||||
int same_dir(const char *a, const char *b);
|
int same_dir(const char *a, const char *b);
|
||||||
int tmpdir_policy(const char *path,
|
int tmpdir_policy(const char *path,
|
||||||
int *allow_noworld_unsticky);
|
int *allow_noworld_unsticky);
|
||||||
char *env_tmpdir(int always_sticky);
|
char *env_tmpdir(int always_sticky, char **tmpdir);
|
||||||
int secure_file(int *fd,
|
int secure_file(int *fd,
|
||||||
struct stat *st,
|
struct stat *st,
|
||||||
struct stat *expected,
|
struct stat *expected,
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ new_tmp_common(int *fd, char **path, int type)
|
|||||||
|
|
||||||
struct stat st_dir_initial;
|
struct stat st_dir_initial;
|
||||||
|
|
||||||
|
char *fail_dir = NULL;
|
||||||
|
|
||||||
if (path == NULL || fd == NULL) {
|
if (path == NULL || fd == NULL) {
|
||||||
errno = EFAULT;
|
errno = EFAULT;
|
||||||
goto err;
|
goto err;
|
||||||
@@ -89,9 +91,9 @@ new_tmp_common(int *fd, char **path, int type)
|
|||||||
|
|
||||||
#if defined(PERMIT_NON_STICKY_ALWAYS) && \
|
#if defined(PERMIT_NON_STICKY_ALWAYS) && \
|
||||||
((PERMIT_NON_STICKY_ALWAYS) > 0)
|
((PERMIT_NON_STICKY_ALWAYS) > 0)
|
||||||
tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS);
|
tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS, &fail_dir);
|
||||||
#else
|
#else
|
||||||
tmpdir = env_tmpdir(0);
|
tmpdir = env_tmpdir(0, &fail_dir);
|
||||||
#endif
|
#endif
|
||||||
if (tmpdir == NULL)
|
if (tmpdir == NULL)
|
||||||
goto err;
|
goto err;
|
||||||
@@ -176,6 +178,13 @@ err:
|
|||||||
*fd = -1;
|
*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;
|
errno = saved_errno;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -185,26 +194,31 @@ err:
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
char *
|
char *
|
||||||
env_tmpdir(int bypass_all_sticky_checks)
|
env_tmpdir(int bypass_all_sticky_checks, char **tmpdir)
|
||||||
{
|
{
|
||||||
char *t;
|
char *t;
|
||||||
int allow_noworld_unsticky;
|
int allow_noworld_unsticky;
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
|
|
||||||
|
char tmp[] = "/tmp";
|
||||||
|
char vartmp[] = "/var/tmp";
|
||||||
|
|
||||||
t = getenv("TMPDIR");
|
t = getenv("TMPDIR");
|
||||||
|
|
||||||
if (t != NULL && *t != '\0') {
|
if (t != NULL && *t != '\0') {
|
||||||
|
|
||||||
if (tmpdir_policy(t,
|
if (tmpdir_policy(t,
|
||||||
&allow_noworld_unsticky) < 0) {
|
&allow_noworld_unsticky) < 0) {
|
||||||
errno = EPERM;
|
if (tmpdir != NULL)
|
||||||
|
*tmpdir = t;
|
||||||
return NULL; /* errno already set */
|
return NULL; /* errno already set */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!world_writeable_and_sticky(t,
|
if (!world_writeable_and_sticky(t,
|
||||||
allow_noworld_unsticky,
|
allow_noworld_unsticky,
|
||||||
bypass_all_sticky_checks)) {
|
bypass_all_sticky_checks)) {
|
||||||
errno = EPERM;
|
if (tmpdir != NULL)
|
||||||
|
*tmpdir = t;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,6 +232,9 @@ env_tmpdir(int bypass_all_sticky_checks)
|
|||||||
allow_noworld_unsticky,
|
allow_noworld_unsticky,
|
||||||
bypass_all_sticky_checks)) {
|
bypass_all_sticky_checks)) {
|
||||||
|
|
||||||
|
if (tmpdir != NULL)
|
||||||
|
*tmpdir = tmp;
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return "/tmp";
|
return "/tmp";
|
||||||
}
|
}
|
||||||
@@ -226,13 +243,13 @@ env_tmpdir(int bypass_all_sticky_checks)
|
|||||||
allow_noworld_unsticky,
|
allow_noworld_unsticky,
|
||||||
bypass_all_sticky_checks)) {
|
bypass_all_sticky_checks)) {
|
||||||
|
|
||||||
|
if (tmpdir != NULL)
|
||||||
|
*tmpdir = vartmp;
|
||||||
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
return "/var/tmp";
|
return "/var/tmp";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errno == saved_errno)
|
|
||||||
errno = EPERM;
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (new_tmp_common(&fd, &s, type) < 0)
|
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 defined(__OpenBSD__) && defined(OpenBSD)
|
||||||
#if (OpenBSD) >= 509
|
#if (OpenBSD) >= 509
|
||||||
|
|||||||
Reference in New Issue
Block a user