summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util/libreboot-utils/include/common.h15
-rw-r--r--util/libreboot-utils/lib/io.c82
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c40
-rw-r--r--util/libreboot-utils/lib/state.c66
-rw-r--r--util/libreboot-utils/lib/string.c66
-rw-r--r--util/libreboot-utils/mkhtemp.c5
-rw-r--r--util/libreboot-utils/nvmutil.c3
7 files changed, 160 insertions, 117 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index b26448db..b78b0c9c 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -287,6 +287,11 @@ struct xfile {
unsigned char bufcmp[GBE_BUF_SIZE]; /* compare gbe/tmp/reads */
unsigned char pad[GBE_WORK_SIZE]; /* the file that wouldn't die */
+
+ /* we later rename in-place, using old fd. renameat() */
+ int dirfd;
+ char *base;
+ char *tmpbase;
};
/* Command table, MAC address, files
@@ -497,9 +502,9 @@ const char *getnvmprogname(void);
/* libc hardening
*/
-int new_tmpfile(int *fd, char **path);
-int new_tmpdir(int *fd, char **path);
-int new_tmp_common(int *fd, char **path, int type);
+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 mkhtemp_try_create(int dirfd,
struct stat *st_dir_initial,
char *fname_copy,
@@ -517,7 +522,8 @@ 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 **tmpdir);
+char *env_tmpdir(int always_sticky, char **tmpdir,
+ char *override_tmpdir);
int secure_file(int *fd,
struct stat *st,
struct stat *expected,
@@ -547,6 +553,7 @@ int mkdirat_on_eintr(int dirfd,
const char *pathname, mode_t mode);
int if_err(int condition, int errval);
int if_err_sys(int condition);
+char *lbgetprogname(char *argv0);
/* asserts */
diff --git a/util/libreboot-utils/lib/io.c b/util/libreboot-utils/lib/io.c
index cc38e5c7..295e15c0 100644
--- a/util/libreboot-utils/lib/io.c
+++ b/util/libreboot-utils/lib/io.c
@@ -413,7 +413,15 @@ gbe_mv(void)
int tmp_gbe_bin_exists;
char *dest_tmp;
- int dest_fd;
+ int dest_fd = -1;
+
+ char *dir = NULL;
+ char *base = NULL;
+ char *dest_name = NULL;
+
+ int dirfd = -1;
+
+ struct stat st_dir;
/* will be set 0 if it doesn't
*/
@@ -424,78 +432,12 @@ gbe_mv(void)
saved_errno = errno;
- rval = rename(f->tname, f->fname);
-
- if (rval > -1) {
-
- /* rename on same filesystem
- */
+ rval = fs_rename_at(f->dirfd, f->tmpbase,
+ f->dirfd, f->base);
+ if (rval > -1)
tmp_gbe_bin_exists = 0;
- if (fsync_dir(f->fname) < 0) {
- f->io_err_gbe_bin = 1;
- rval = -1;
- }
-
- goto ret_gbe_mv;
- }
-
- if (errno != EXDEV)
- goto ret_gbe_mv;
-
- /*
- * OR, cross-filesystem rename:
- */
-
- if ((rval = f->tmp_fd = open(f->tname,
- O_RDONLY | O_BINARY)) == -1)
- goto ret_gbe_mv;
-
- /* create replacement temp in target directory
- */
- if (new_tmpfile(&dest_fd, &f->fname) < 1)
- goto ret_gbe_mv;
- if (dest_tmp == NULL)
- goto ret_gbe_mv;
-
- /* copy data
- */
- rval = rw_file_exact(f->tmp_fd, f->bufcmp,
- f->gbe_file_size, 0, IO_PREAD,
- NO_LOOP_EAGAIN, LOOP_EINTR,
- MAX_ZERO_RW_RETRY, OFF_ERR);
-
- if (rval < 0)
- goto ret_gbe_mv;
-
- rval = rw_file_exact(dest_fd, f->bufcmp,
- f->gbe_file_size, 0, IO_PWRITE,
- NO_LOOP_EAGAIN, LOOP_EINTR,
- MAX_ZERO_RW_RETRY, OFF_ERR);
-
- if (rval < 0)
- goto ret_gbe_mv;
-
- if (fsync_on_eintr(dest_fd) == -1)
- goto ret_gbe_mv;
-
- if (close_on_eintr(dest_fd) == -1) {
- dest_fd = -1;
- goto ret_gbe_mv;
- }
- dest_fd = -1;
-
- if (rename(dest_tmp, f->fname) == -1)
- goto ret_gbe_mv;
-
- if (fsync_dir(f->fname) < 0) {
- f->io_err_gbe_bin = 1;
- goto ret_gbe_mv;
- }
-
- free_if_null(&dest_tmp);
-
ret_gbe_mv:
/* TODO: this whole section is bloat.
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index b30f6587..87d7c8dc 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -27,18 +27,21 @@
#include "../include/common.h"
+/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
int
-new_tmpfile(int *fd, char **path)
+new_tmpfile(int *fd, char **path, char *tmpdir)
{
- return new_tmp_common(fd, path, MKHTEMP_FILE);
+ return new_tmp_common(fd, path, MKHTEMP_FILE, tmpdir);
}
+/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
int
-new_tmpdir(int *fd, char **path)
+new_tmpdir(int *fd, char **path, char *tmpdir)
{
- return new_tmp_common(fd, path, MKHTEMP_DIR);
+ return new_tmp_common(fd, path, MKHTEMP_DIR, tmpdir);
}
+/* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */
/* WARNING:
* on error, *path (at **path) may be
NULL, or if the error pertains to
@@ -55,7 +58,8 @@ new_tmpdir(int *fd, char **path)
* default to /tmp or /var/tmp
*/
int
-new_tmp_common(int *fd, char **path, int type)
+new_tmp_common(int *fd, char **path, int type,
+ char *tmpdir)
{
#if defined(PATH_LEN) && \
(PATH_LEN) >= 256
@@ -66,7 +70,6 @@ new_tmp_common(int *fd, char **path, int type)
struct stat st;
char suffix[] = "tmp.XXXXXXXXXX";
- char *tmpdir = NULL;
size_t dirlen;
size_t destlen;
@@ -100,15 +103,25 @@ new_tmp_common(int *fd, char **path, int type)
* (on error, it will not be touched)
*/
-
*fd = -1;
+ if (tmpdir == NULL) { /* no user override */
+#if defined(PERMIT_NON_STICKY_ALWAYS) && \
+ ((PERMIT_NON_STICKY_ALWAYS) > 0)
+ tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS, &fail_dir, NULL);
+#else
+ tmpdir = env_tmpdir(0, &fail_dir, NULL);
+#endif
+ } else {
+
#if defined(PERMIT_NON_STICKY_ALWAYS) && \
((PERMIT_NON_STICKY_ALWAYS) > 0)
- tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS, &fail_dir);
+ tmpdir = env_tmpdir(PERMIT_NON_STICKY_ALWAYS, &fail_dir,
+ tmpdir);
#else
- tmpdir = env_tmpdir(0, &fail_dir);
+ tmpdir = env_tmpdir(0, &fail_dir, tmpdir);
#endif
+ }
if (tmpdir == NULL)
goto err;
@@ -189,7 +202,8 @@ err:
*/
char *
-env_tmpdir(int bypass_all_sticky_checks, char **tmpdir)
+env_tmpdir(int bypass_all_sticky_checks, char **tmpdir,
+ char *override_tmpdir)
{
char *t;
int allow_noworld_unsticky;
@@ -198,7 +212,11 @@ env_tmpdir(int bypass_all_sticky_checks, char **tmpdir)
char tmp[] = "/tmp";
char vartmp[] = "/var/tmp";
- t = getenv("TMPDIR");
+ /* tmpdir is a user override, if set */
+ if (override_tmpdir == NULL)
+ t = getenv("TMPDIR");
+ else
+ t = override_tmpdir;
if (t != NULL && *t != '\0') {
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index b7701f0d..e3cb0890 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -26,10 +26,16 @@ struct xstate *
xstart(int argc, char *argv[])
{
static int first_run = 1;
+ static char *dir = NULL;
+ static char *base = NULL;
+ char *realdir = NULL;
+ char *tmpdir = NULL;
+ char *tmpbase_local = NULL;
static struct xstate us = {
- /* DO NOT MESS THIS UP, OR THERE WILL BE DEMONS */
{
+ /* be careful when modifying xstate. you
+ * must set everything precisely */
{
CMD_DUMP, "dump", cmd_helper_dump, ARGC_3,
ARG_NOPART,
@@ -106,9 +112,35 @@ xstart(int argc, char *argv[])
us.f.tmp_fd = -1;
us.f.tname = NULL;
- if (new_tmpfile(&us.f.tmp_fd, &us.f.tname) < 0)
+ if ((realdir = realpath(us.f.fname, NULL)) == NULL)
+ err_no_cleanup(errno, "xstart: can't get realpath of %s",
+ us.f.fname);
+
+ if (fs_dirname_basename(realdir, &dir, &base, 0) < 0)
+ err_no_cleanup(errno, "xstart: don't know CWD of %s",
+ us.f.fname);
+
+ if ((us.f.base = strdup(base)) == NULL)
+ err_no_cleanup(errno, "strdup base");
+
+ us.f.dirfd = fs_open(dir,
+ O_RDONLY | O_DIRECTORY);
+ 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)
err_no_cleanup(errno, "%s", us.f.tname);
+ if (fs_dirname_basename(us.f.tname,
+ &tmpdir, &tmpbase_local, 0) < 0)
+ err_no_cleanup(errno, "tmp basename");
+
+ us.f.tmpbase = strdup(tmpbase_local);
+ if (us.f.tmpbase == NULL)
+ err_no_cleanup(errno, "strdup tmpbase");
+
+ free_if_null(&tmpdir);
+
if (us.f.tname == NULL)
err_no_cleanup(errno, "x->f.tname null");
if (*us.f.tname == '\0')
@@ -163,32 +195,6 @@ err(int nvm_errval, const char *msg, ...)
exit(EXIT_FAILURE);
}
-const char *
-getnvmprogname(void)
-{
- struct xstate *x = xstatus();
-
- const char *p;
- static char fallback[] = "nvmutil";
-
- char *rval = fallback;
-
- if (x != NULL) {
-
- if (x->argv0 != NULL && *x->argv0 != '\0')
- rval = x->argv0;
- else
- return fallback;
- }
-
- p = strrchr(rval, '/');
-
- if (p)
- return p + 1;
- else
- return rval;
-}
-
int
exit_cleanup(void)
{
@@ -211,6 +217,10 @@ exit_cleanup(void)
if (f->tname != NULL)
if (unlink(f->tname) == -1)
close_err = 1;
+
+ close_no_err(&f->dirfd);
+ free_if_null(&f->base);
+ free_if_null(&f->tmpbase);
}
if (saved_errno)
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c
index cb37c1ba..2f2be5f3 100644
--- a/util/libreboot-utils/lib/string.c
+++ b/util/libreboot-utils/lib/string.c
@@ -122,6 +122,8 @@ void
err_no_cleanup(int nvm_errval, const char *msg, ...)
{
va_list args;
+ int saved_errno = errno;
+ const char *p;
#if defined(__OpenBSD__) && defined(OpenBSD)
#if (OpenBSD) >= 509
@@ -129,11 +131,11 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
fprintf(stderr, "pledge failure during exit");
#endif
#endif
-
if (!errno)
- errno = ECANCELED;
+ saved_errno = errno = ECANCELED;
- fprintf(stderr, "nvmutil: ");
+ if ((p = getnvmprogname()) != NULL)
+ fprintf(stderr, "%s: ", p);
va_start(args, msg);
vfprintf(stderr, msg, args);
@@ -144,3 +146,61 @@ err_no_cleanup(int nvm_errval, const char *msg, ...)
exit(EXIT_FAILURE);
}
+const char *
+getnvmprogname(void)
+{
+ static char *rval = NULL;
+ static char *p;
+ static int setname = 0;
+
+ if (!setname) {
+ if ((rval = lbgetprogname(NULL)) == NULL)
+ return NULL;
+
+ p = strrchr(rval, '/');
+ if (p)
+ rval = p + 1;
+
+ setname = 1;
+ }
+
+ return rval;
+}
+
+/* singleton. if string not null,
+ sets the string. after set,
+ will not set anymore. either
+ way, returns the string
+ */
+char *
+lbgetprogname(char *argv0)
+{
+ static int setname = 0;
+ static char *progname = NULL;
+ size_t len;
+
+ if (!setname) {
+ if (if_err(argv0 == NULL || *argv0 == '\0', EFAULT) ||
+ slen(argv0, 4096, &len) < 0 ||
+ (progname = malloc(len + 1)) == NULL)
+ return NULL;
+
+ memcpy(progname, argv0, len + 1);
+ setname = 1;
+ }
+
+ return progname;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c
index 3e148a4a..c289c5b4 100644
--- a/util/libreboot-utils/mkhtemp.c
+++ b/util/libreboot-utils/mkhtemp.c
@@ -80,6 +80,9 @@ main(int argc, char *argv[])
size_t maxlen = 4096;
#endif
+ if (lbgetprogname(argv[0]) == NULL)
+ err_no_cleanup(errno, "could not set progname");
+
/* https://man.openbsd.org/pledge.2 */
#if defined(__OpenBSD__) && defined(OpenBSD)
#if (OpenBSD) >= 509
@@ -103,7 +106,7 @@ main(int argc, char *argv[])
}
}
- if (new_tmp_common(&fd, &s, type) < 0)
+ if (new_tmp_common(&fd, &s, type, NULL) < 0)
err_no_cleanup(errno, "%s", s);
#if defined(__OpenBSD__) && defined(OpenBSD)
diff --git a/util/libreboot-utils/nvmutil.c b/util/libreboot-utils/nvmutil.c
index fb45a97c..da7d40ec 100644
--- a/util/libreboot-utils/nvmutil.c
+++ b/util/libreboot-utils/nvmutil.c
@@ -35,6 +35,9 @@ main(int argc, char *argv[])
size_t c;
+ if (lbgetprogname(argv[0]) == NULL)
+ err_no_cleanup(errno, "could not set progname");
+
/* https://man.openbsd.org/pledge.2
https://man.openbsd.org/unveil.2 */
#if defined(__OpenBSD__) && defined(OpenBSD)