summaryrefslogtreecommitdiff
path: root/util/libreboot-utils
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils')
-rw-r--r--util/libreboot-utils/include/common.h9
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c29
-rw-r--r--util/libreboot-utils/lib/state.c2
-rw-r--r--util/libreboot-utils/mkhtemp.c41
4 files changed, 55 insertions, 26 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index 620e95b9..b028e437 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -502,9 +502,12 @@ const char *getnvmprogname(void);
/* libc hardening
*/
-int new_tmpfile(int *fd, char **path, char *tmpdir);
-int new_tmpdir(int *fd, char **path, char *tmpdir);
-int new_tmp_common(int *fd, char **path, int type, char *tmpdir);
+int new_tmpfile(int *fd, char **path, char *tmpdir,
+ const char *template);
+int new_tmpdir(int *fd, char **path, char *tmpdir,
+ const char *template);
+int new_tmp_common(int *fd, char **path, int type,
+ char *tmpdir, const char *template);
int mkhtemp_try_create(int dirfd,
struct stat *st_dir_initial,
char *fname_copy,
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index cd4a9cde..6b4898fd 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -35,16 +35,20 @@
/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
int
-new_tmpfile(int *fd, char **path, char *tmpdir)
+new_tmpfile(int *fd, char **path, char *tmpdir,
+ const char *template)
{
- return new_tmp_common(fd, path, MKHTEMP_FILE, tmpdir);
+ return new_tmp_common(fd, path, MKHTEMP_FILE,
+ tmpdir, template);
}
/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
int
-new_tmpdir(int *fd, char **path, char *tmpdir)
+new_tmpdir(int *fd, char **path, char *tmpdir,
+ const char *template)
{
- return new_tmp_common(fd, path, MKHTEMP_DIR, tmpdir);
+ return new_tmp_common(fd, path, MKHTEMP_DIR,
+ tmpdir, template);
}
/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
@@ -65,7 +69,7 @@ new_tmpdir(int *fd, char **path, char *tmpdir)
*/
int
new_tmp_common(int *fd, char **path, int type,
- char *tmpdir)
+ char *tmpdir, const char *template)
{
#if defined(PATH_LEN) && \
(PATH_LEN) >= 256
@@ -75,7 +79,8 @@ new_tmp_common(int *fd, char **path, int type,
#endif
struct stat st;
- char suffix[] = "tmp.XXXXXXXXXX";
+ const char *suffix;
+ size_t suffix_len;
size_t dirlen;
size_t destlen;
@@ -138,10 +143,18 @@ new_tmp_common(int *fd, char **path, int type,
if (*tmpdir != '/')
goto err;
+ if (template != NULL)
+ suffix = template;
+ else
+ suffix = "tmp.XXXXXXXXXX";
+
+ if (slen(suffix, maxlen, &suffix_len) < 0)
+ goto err;
+
/* sizeof adds an extra byte, useful
* because we also want '.' or '/'
*/
- destlen = dirlen + sizeof(suffix);
+ destlen = dirlen + 1 + suffix_len;
if (destlen > maxlen - 1) {
errno = EOVERFLOW;
goto err;
@@ -155,7 +168,7 @@ new_tmp_common(int *fd, char **path, int type,
memcpy(dest, tmpdir, dirlen);
*(dest + dirlen) = '/';
- memcpy(dest + dirlen + 1, suffix, sizeof(suffix) - 1);
+ memcpy(dest + dirlen + 1, suffix, suffix_len);
*(dest + destlen) = '\0';
fname = dest + dirlen + 1;
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index e3cb0890..a1120906 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -128,7 +128,7 @@ xstart(int argc, char *argv[])
if (us.f.dirfd < 0)
err_no_cleanup(errno, "%s: open dir", dir);
- if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir) < 0)
+ if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir, NULL) < 0)
err_no_cleanup(errno, "%s", us.f.tname);
if (fs_dirname_basename(us.f.tname,
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c
index 3fa4819f..4408f763 100644
--- a/util/libreboot-utils/mkhtemp.c
+++ b/util/libreboot-utils/mkhtemp.c
@@ -67,27 +67,26 @@
int
main(int argc, char *argv[])
{
- const char usage_str[] = "usage: %s [-d] [-p dir] [template]";
-
- char *tmpdir = NULL;
- char *template = NULL;
-
- char *s = NULL;
- int fd = -1;
- char c;
- int type = MKHTEMP_FILE;
- size_t len;
-
- char *rp;
-
#if defined (PATH_LEN) && \
(PATH_LEN) >= 256
size_t maxlen = PATH_LEN;
#else
size_t maxlen = 4096;
#endif
+ size_t len;
+ size_t tlen;
+ size_t xc = 0;
+ char *tmpdir = NULL;
+ char *template = NULL;
+ char *p;
+ char *s = NULL;
+ char *rp;
char resolved[maxlen];
+ char c;
+
+ int fd = -1;
+ int type = MKHTEMP_FILE;
if (lbgetprogname(argv[0]) == NULL)
err_no_cleanup(errno, "could not set progname");
@@ -123,6 +122,19 @@ main(int argc, char *argv[])
err_no_cleanup(EINVAL,
"usage: mkhtemp [-d] [-p dir] [template]\n");
+ /* custom template e.g. foo.XXXXXXXXXXXXXXXXXXXXX */
+ if (template != NULL) {
+ if (slen(template, maxlen, &tlen) < 0)
+ err_no_cleanup(EINVAL,
+ "invalid template");
+
+ for (p = template + tlen;
+ p > template && *--p == 'X'; xc++);
+
+ if (xc < 6)
+ err_no_cleanup(EINVAL,
+ "template must end in at least 6 X");
+ }
/* user supplied -p PATH - WARNING:
* this permits symlinks, but only here,
@@ -140,7 +152,8 @@ main(int argc, char *argv[])
tmpdir = resolved;
}
- if (new_tmp_common(&fd, &s, type, tmpdir) < 0)
+ if (new_tmp_common(&fd, &s, type,
+ tmpdir, template) < 0)
err_no_cleanup(errno, "%s", s);
#if defined(__OpenBSD__) && defined(OpenBSD)