diff options
Diffstat (limited to 'util/nvmutil')
| -rw-r--r-- | util/nvmutil/Makefile | 6 | ||||
| -rw-r--r-- | util/nvmutil/nvmutil.c | 1584 |
2 files changed, 1160 insertions, 430 deletions
diff --git a/util/nvmutil/Makefile b/util/nvmutil/Makefile index 719e1c1e..e58c654b 100644 --- a/util/nvmutil/Makefile +++ b/util/nvmutil/Makefile @@ -3,7 +3,11 @@ # Copyright (c) 2023 Riku Viitanen <riku.viitanen@protonmail.com> CC?=cc -CFLAGS?=-Os -Wall -Wextra -Werror -pedantic -std=c90 +CSTD?=-std=c90 +WERROR?=-Werror +CWARN?=-Wall -Wextra -pedantic +COPT?=-Os +CFLAGS?=-I. $(COPT) $(CWARN) $(CSTD) LDFLAGS?= DESTDIR?= PREFIX?=/usr/local diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 4cc35505..25953de5 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -15,24 +15,29 @@ * -Os -Wall -Wextra -Werror -pedantic -std=c90 */ -#define OFF_ERR 0 -#ifndef OFF_RESET -#define OFF_RESET 1 -#endif - /* - * NOTE: older Linux lacked arc4random. - * added in glibc 2.36. Just pass HAVE_ARC4RANDOM_BUF=0 - * at build time if you need old Linux / other libc. + * In practise, most people + * aren't going to use very + * long names, so even on old + * systems with weaker limits, + * it's OK to set this higher. + * + * 4096 is a good, conservative + * default these days. */ -#if defined(__OpenBSD__) || defined(__FreeBSD__) || \ - defined(__NetBSD__) || defined(__APPLE__) || \ - defined(__linux__) -#ifndef HAVE_ARC4RANDOM_BUF -#define HAVE_ARC4RANDOM_BUF 1 +#ifndef PATH_LEN +#ifdef PATH_MAX +#define PATH_LEN (PATH_MAX) +#else +#define PATH_LEN 1024 #endif #endif +#define OFF_ERR 0 +#ifndef OFF_RESET +#define OFF_RESET 1 +#endif + /* * I/O config (build-time) * @@ -216,7 +221,6 @@ also consider: #include <sys/param.h> #endif #include <sys/types.h> -#include <sys/time.h> #include <sys/stat.h> #include <errno.h> @@ -254,8 +258,8 @@ typedef char static_assert_twos_complement[ typedef char assert_ulong_ptr[ (sizeof(ulong) >= sizeof(void *)) ? 1 : -1 ]; -typedef char assert_size_t_ptr[ - (sizeof(size_t) >= sizeof(void *)) ? 1 : -1 +typedef char assert_unsigned_long_ptr[ + (sizeof(unsigned long) >= sizeof(void *)) ? 1 : -1 ]; /* @@ -300,29 +304,41 @@ typedef char static_assert_off_t_is_32[(sizeof(off_t) >= 4) ? 1 : -1]; #define O_BINARY 0 #endif +#ifndef O_NONBLOCK +#define O_NONBLOCK 0 +#endif + +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + #ifndef O_NOFOLLOW #define O_NOFOLLOW 0 #endif +#ifndef FD_CLOEXEC +#define FD_CLOEXEC 0 +#endif + /* * Sanitize command tables. */ static void sanitize_command_list(void); -static void sanitize_command_index(size_t c); +static void sanitize_command_index(unsigned long c); /* * Argument handling (user input) */ static void set_cmd(int argc, char *argv[]); static void set_cmd_args(int argc, char *argv[]); -static size_t conv_argv_part_num(const char *part_str); -static int xstrxcmp(const char *a, const char *b, size_t maxlen); +static unsigned long conv_argv_part_num(const char *part_str); +static int xstrxcmp(const char *a, const char *b, unsigned long maxlen); /* * Prep files for reading */ static void open_gbe_file(void); -static void lock_gbe_file(void); +static int lock_file(int fd); static void xopen(int *fd, const char *path, int flags, struct stat *st); /* @@ -331,48 +347,65 @@ static void xopen(int *fd, const char *path, int flags, struct stat *st); * * After this, we can run commands. */ -static void read_gbe_file(void); +static void copy_gbe(void); static void read_checksums(void); -static int good_checksum(size_t partnum); +static int good_checksum(unsigned long partnum); /* * Execute user command on GbE data. * These are stubs that call helpers. */ -static void run_cmd(size_t c); -static void check_command_num(size_t c); -static u8 valid_command(size_t c); +static void run_cmd(unsigned long c); +static void check_command_num(unsigned long c); +static u8 valid_command(unsigned long c); + +/* + * portable timeval + */ +struct x_st_timeval { + long tv_sec; + long tv_usec; +}; /* * Helper functions for command: setmac */ static void cmd_helper_setmac(void); static void parse_mac_string(void); -static size_t xstrxlen(const char *scmp, size_t maxlen); -static void set_mac_byte(size_t mac_byte_pos); -static void set_mac_nib(size_t mac_str_pos, - size_t mac_byte_pos, size_t mac_nib_pos); +static unsigned long xstrxlen(const char *scmp, unsigned long maxlen); +static void set_mac_byte(unsigned long mac_byte_pos); +static void set_mac_nib(unsigned long mac_str_pos, + unsigned long mac_byte_pos, unsigned long mac_nib_pos); static ushort hextonum(char ch_s); static ushort rhex(void); -#if !defined(HAVE_ARC4RANDOM_BUF) || \ - (HAVE_ARC4RANDOM_BUF) < 1 +static ushort read_urandom(void); static ulong entropy_jitter(void); -#endif -static void write_mac_part(size_t partnum); +static int x_i_gettimeofday(struct x_st_timeval *tv, void *tz); +static void write_mac_part(unsigned long partnum); /* * Helper functions for command: dump */ static void cmd_helper_dump(void); -static void print_mac_from_nvm(size_t partnum); -static void hexdump(size_t partnum); +static void print_mac_from_nvm(unsigned long partnum); +static void hexdump(unsigned long partnum); + +/* + * Helper functions for command: swap + */ +static void cmd_helper_swap(void); + +/* + * Helper functions for command: copy + */ +static void cmd_helper_copy(void); /* * Helper functions for commands: * cat, cat16 and cat128 */ static void cmd_helper_cat(void); -static void gbe_cat_buf(u8 *b); +static void cat_buf(u8 *b); /* * After command processing, write @@ -382,44 +415,46 @@ static void gbe_cat_buf(u8 *b); * below for the actual functions. */ static void write_gbe_file(void); -static void override_part_modified(void); -static void set_checksum(size_t part); -static ushort calculated_checksum(size_t p); +static void set_checksum(unsigned long part); +static ushort calculated_checksum(unsigned long p); /* * Helper functions for accessing * the NVM area during operation. */ -static ushort nvm_word(size_t pos16, size_t part); -static void set_nvm_word(size_t pos16, size_t part, ushort val16); -static void set_part_modified(size_t p); -static void check_nvm_bound(size_t pos16, size_t part); -static void check_bin(size_t a, const char *a_name); +static ushort nvm_word(unsigned long pos16, unsigned long part); +static void set_nvm_word(unsigned long pos16, unsigned long part, ushort val16); +static void set_part_modified(unsigned long p); +static void check_nvm_bound(unsigned long pos16, unsigned long part); +static void check_bin(unsigned long a, const char *a_name); /* * Helper functions for stub functions * that handle GbE file reads/writes. */ -static void rw_gbe_file_part(size_t p, int rw_type, +static void rw_gbe_file_part(unsigned long p, int rw_type, const char *rw_type_str); -static void check_written_part(size_t p); +static void write_to_gbe_bin(void); +static int gbe_mv(void); +static void check_written_part(unsigned long p); static void report_io_err_rw(void); -static u8 *gbe_mem_offset(size_t part, const char *f_op); -static off_t gbe_file_offset(size_t part, const char *f_op); -static off_t gbe_x_offset(size_t part, const char *f_op, +static int fsync_dir(const char *path); +static u8 *gbe_mem_offset(unsigned long part, const char *f_op); +static off_t gbe_file_offset(unsigned long part, const char *f_op); +static off_t gbe_x_offset(unsigned long part, const char *f_op, const char *d_type, off_t nsize, off_t ncmp); -static ssize_t rw_gbe_file_exact(int fd, u8 *mem, size_t nrw, +static long rw_gbe_file_exact(int fd, u8 *mem, unsigned long nrw, off_t off, int rw_type); -static ssize_t rw_file_exact(int fd, u8 *mem, size_t len, +static long rw_file_exact(int fd, u8 *mem, unsigned long len, off_t off, int rw_type, int loop_eagain, int loop_eintr, - size_t max_retries, int off_reset); -static ssize_t prw(int fd, void *mem, size_t nrw, + unsigned long max_retries, int off_reset); +static long prw(int fd, void *mem, unsigned long nrw, off_t off, int rw_type, int loop_eagain, int loop_eintr, int off_reset); -static int io_args(int fd, void *mem, size_t nrw, +static int io_args(int fd, void *mem, unsigned long nrw, off_t off, int rw_type); static int check_file(int fd, struct stat *st); -static ssize_t rw_over_nrw(ssize_t r, size_t nrw); +static long rw_over_nrw(long r, unsigned long nrw); #if !defined(HAVE_REAL_PREAD_PWRITE) || \ HAVE_REAL_PREAD_PWRITE < 1 static off_t lseek_loop(int fd, off_t off, @@ -430,10 +465,19 @@ static int try_err(int loop_err, int errval); /* * Error handling and cleanup */ +static void usage(void); static void err(int nvm_errval, const char *msg, ...); -static int close_files(void); +static int exit_cleanup(void); static const char *getnvmprogname(void); -static void usage(int usage_exit); + +/* + * a special kind of hell + */ +static char *new_tmpfile(int *fd, int local, const char *path); +static int x_i_mkstemp(char *template); +static char *x_c_strrchr(const char *s, int c); +static int x_i_rename(const char *src, const char *dst); +static char *x_c_tmpdir(void); /* * Sizes in bytes: @@ -444,6 +488,8 @@ static void usage(int usage_exit); #define SIZE_16KB (16 * SIZE_1KB) #define SIZE_128KB (128 * SIZE_1KB) +#define GBE_BUF_SIZE (SIZE_128KB) + /* * First 128 bytes of a GbE part contains * the regular NVM (Non-Volatile-Memory) @@ -456,8 +502,8 @@ static void usage(int usage_exit); * There is a second 4KB part with the same * rules, and it *should* be identical. */ -#define GBE_FILE_SIZE SIZE_8KB /* for buf */ -#define GBE_PART_SIZE (GBE_FILE_SIZE >> 1) +#define GBE_WORK_SIZE (SIZE_8KB) +#define GBE_PART_SIZE (GBE_WORK_SIZE >> 1) #define NVM_CHECKSUM 0xBABA #define NVM_SIZE 128 #define NVM_WORDS (NVM_SIZE >> 1) @@ -478,35 +524,30 @@ static void usage(int usage_exit); * * The code will handle this properly. */ -static u8 real_buf[GBE_FILE_SIZE]; -static u8 pad[GBE_FILE_SIZE]; /* the file that wouldn't die */ +static u8 real_buf[GBE_BUF_SIZE]; +static u8 bufcmp[GBE_BUF_SIZE]; /* compare gbe/tmp/reads */ +static u8 pad[GBE_WORK_SIZE]; /* the file that wouldn't die */ static u8 *buf = real_buf; static ushort mac_buf[3]; static off_t gbe_file_size; +static off_t gbe_tmp_size; static int gbe_fd = -1; -static size_t part; +static unsigned long part; static u8 part_modified[2]; static u8 part_valid[2]; static const char rmac[] = "xx:xx:xx:xx:xx:xx"; -static const char *mac_str; -static const char *fname; +static const char *mac_str = rmac; +static const char *fname = NULL; static const char *argv0; -#ifndef SSIZE_MAX -#define SSIZE_MAX ((ssize_t)(~((size_t)1 << (sizeof(ssize_t)*CHAR_BIT-1)))) +#ifndef X_LONG_MAX +#define X_LONG_MAX ((long)(~((long)1 << (sizeof(long)*CHAR_BIT-1)))) #endif /* - * Use these for .invert in command[]: - * If set to 1: read/write inverter (p0->p1, p1->p0) - */ -#define PART_INVERT 1 -#define NO_INVERT 0 - -/* * Use these for .argc in command[]: */ #define ARGC_3 3 @@ -536,18 +577,6 @@ enum { CMD_CAT128 }; -/* - * If set, a given part will always be written. - */ -enum { - SET_MOD_OFF, /* don't manually set part modified */ - SET_MOD_0, /* set part 0 modified */ - SET_MOD_1, /* set part 1 modified */ - SET_MOD_N, /* set user-specified part modified */ - /* affected by command[].invert */ - SET_MOD_BOTH /* set both parts modified */ -}; - enum { ARG_NOPART, ARG_PART @@ -564,16 +593,14 @@ enum { }; struct commands { - size_t chk; + unsigned long chk; const char *str; void (*run)(void); int argc; - u8 invert; - u8 set_modified; u8 arg_part; u8 chksum_read; u8 chksum_write; - size_t rw_size; /* within the 4KB GbE part */ + unsigned long rw_size; /* within the 4KB GbE part */ int flags; /* e.g. O_RDWR or O_RDONLY */ }; @@ -582,50 +609,36 @@ struct commands { */ static const struct commands command[] = { { CMD_DUMP, "dump", cmd_helper_dump, ARGC_3, - NO_INVERT, SET_MOD_OFF, ARG_NOPART, SKIP_CHECKSUM_READ, SKIP_CHECKSUM_WRITE, NVM_SIZE, O_RDONLY }, { CMD_SETMAC, "setmac", cmd_helper_setmac, ARGC_3, - NO_INVERT, SET_MOD_OFF, ARG_NOPART, CHECKSUM_READ, CHECKSUM_WRITE, NVM_SIZE, O_RDWR }, - /* - * OPTIMISATION: Read inverted, so no copying is needed. - */ - { CMD_SWAP, "swap", NULL, ARGC_3, - PART_INVERT, SET_MOD_BOTH, + { CMD_SWAP, "swap", cmd_helper_swap, ARGC_3, ARG_NOPART, CHECKSUM_READ, SKIP_CHECKSUM_WRITE, GBE_PART_SIZE, O_RDWR }, - /* - * OPTIMISATION: Read inverted, so no copying is needed. - * The non-target part will not be read. - */ - { CMD_COPY, "copy", NULL, ARGC_4, - PART_INVERT, SET_MOD_N, + { CMD_COPY, "copy", cmd_helper_copy, ARGC_4, ARG_PART, CHECKSUM_READ, SKIP_CHECKSUM_WRITE, GBE_PART_SIZE, O_RDWR }, { CMD_CAT, "cat", cmd_helper_cat, ARGC_3, - NO_INVERT, SET_MOD_OFF, ARG_NOPART, CHECKSUM_READ, SKIP_CHECKSUM_WRITE, GBE_PART_SIZE, O_RDONLY }, { CMD_CAT16, "cat16", cmd_helper_cat, ARGC_3, - NO_INVERT, SET_MOD_OFF, ARG_NOPART, CHECKSUM_READ, SKIP_CHECKSUM_WRITE, GBE_PART_SIZE, O_RDONLY }, { CMD_CAT128, "cat128", cmd_helper_cat, ARGC_3, - NO_INVERT, SET_MOD_OFF, ARG_NOPART, CHECKSUM_READ, SKIP_CHECKSUM_WRITE, GBE_PART_SIZE, O_RDONLY }, @@ -638,7 +651,7 @@ static const struct commands command[] = { /* * Index in command[], will be set later */ -static size_t cmd_index = CMD_NULL; +static unsigned long cmd_index = CMD_NULL; /* * asserts (variables/defines sanity check) @@ -649,6 +662,7 @@ typedef char assert_read[(IO_READ==0)?1:-1]; typedef char assert_write[(IO_WRITE==1)?1:-1]; typedef char assert_pread[(IO_PREAD==2)?1:-1]; typedef char assert_pwrite[(IO_PWRITE==3)?1:-1]; +typedef char assert_pathlen[(PATH_LEN>=256)?1:-1]; /* commands */ typedef char assert_cmd_dump[(CMD_DUMP==0)?1:-1]; typedef char assert_cmd_setmac[(CMD_SETMAC==1)?1:-1]; @@ -657,12 +671,6 @@ typedef char assert_cmd_copy[(CMD_COPY==3)?1:-1]; typedef char assert_cmd_cat[(CMD_CAT==4)?1:-1]; typedef char assert_cmd_cat16[(CMD_CAT16==5)?1:-1]; typedef char assert_cmd_cat128[(CMD_CAT128==6)?1:-1]; -/* mod_type */ -typedef char assert_mod_off[(SET_MOD_OFF==0)?1:-1]; -typedef char assert_mod_0[(SET_MOD_0==1)?1:-1]; -typedef char assert_mod_1[(SET_MOD_1==2)?1:-1]; -typedef char assert_mod_n[(SET_MOD_N==3)?1:-1]; -typedef char assert_mod_both[(SET_MOD_BOTH==4)?1:-1]; /* bool */ typedef char bool_arg_nopart[(ARG_NOPART==0)?1:-1]; typedef char bool_arg_part[(ARG_PART==1)?1:-1]; @@ -670,8 +678,6 @@ typedef char bool_skip_checksum_read[(SKIP_CHECKSUM_READ==0)?1:-1]; typedef char bool_checksum_read[(CHECKSUM_READ==1)?1:-1]; typedef char bool_skip_checksum_write[(SKIP_CHECKSUM_WRITE==0)?1:-1]; typedef char bool_checksum_write[(CHECKSUM_WRITE==1)?1:-1]; -typedef char bool_no_invert[(NO_INVERT==0)?1:-1]; -typedef char bool_part_invert[(PART_INVERT==1)?1:-1]; typedef char bool_loop_eintr[(LOOP_EINTR==1||LOOP_EINTR==0)?1:-1]; typedef char bool_loop_eagain[(LOOP_EAGAIN==1||LOOP_EAGAIN==0)?1:-1]; typedef char bool_no_loop_eintr[(NO_LOOP_EINTR==0)?1:-1]; @@ -679,7 +685,8 @@ typedef char bool_no_loop_eagain[(NO_LOOP_EAGAIN==0)?1:-1]; typedef char bool_off_err[(OFF_ERR==0)?1:-1]; typedef char bool_off_reset[(OFF_RESET==0||OFF_RESET==1)?1:-1]; -static int io_err_gbe = 0; +static int io_err_gbe = 0; /* intermediary write (verification) */ +static int io_err_gbe_bin = 0; /* final write (real file) */ static int rw_check_err_read[] = {0, 0}; static int rw_check_partial_read[] = {0, 0}; static int rw_check_bad_part[] = {0, 0}; @@ -689,28 +696,63 @@ static int post_rw_checksum[] = {0, 0}; static dev_t gbe_dev; static ino_t gbe_ino; -#if defined(HAVE_ARC4RANDOM_BUF) && \ - (HAVE_ARC4RANDOM_BUF) > 0 -void arc4random_buf(void *buf, size_t n); +static dev_t tmp_dev; +static ino_t tmp_ino; + +/* + * No need to declare feature + * macros. I jus declare the + * prototypes. Should be safe + * on most Unices and compilers + */ +char *mktemp(char *template); +int fchmod(int fd, mode_t mode); +void sync(void); +int syncfs(int fd); + +static int tmp_fd = -1; +static char *tname = NULL; + +#ifndef S_ISREG +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) #endif +extern void *malloc(); + int main(int argc, char *argv[]) { argv0 = argv[0]; if (argc < 3) - usage(1); + usage(); + + if (CHAR_BIT != 8) + err(EINVAL, "Unsupported char size"); fname = argv[1]; +#ifdef NVMUTIL_UNVEIL + /* + * if global tmp is a different filesystem, + * unveil would trap on final file rename + * and we can't know the path in advance + */ + tname = new_tmpfile(&tmp_fd, 1, NULL); +#else + tname = new_tmpfile(&tmp_fd, 0, NULL); +#endif + + if (tname == NULL) + err(errno, "Can't create tmpfile"); + #ifdef NVMUTIL_PLEDGE #ifdef NVMUTIL_UNVEIL - if (pledge("stdio flock rpath wpath unveil", NULL) == -1) - err(errno, "pledge"); - if (unveil("/dev/null", "r") == -1) - err(errno, "unveil /dev/null"); + if (pledge("stdio flock rpath wpath cpath unveil", NULL) == -1) + err(errno, "pledge, unveil"); + if (unveil("/dev/urandom", "r") == -1) + err(errno, "unveil: /dev/urandom"); #else - if (pledge("stdio flock rpath wpath", NULL) == -1) + if (pledge("stdio flock rpath wpath cpath", NULL) == -1) err(errno, "pledge"); #endif #endif @@ -720,77 +762,49 @@ main(int argc, char *argv[]) set_cmd(argc, argv); set_cmd_args(argc, argv); -#ifdef NVMUTIL_PLEDGE #ifdef NVMUTIL_UNVEIL if (command[cmd_index].flags == O_RDONLY) { if (unveil(fname, "r") == -1) - err(errno, "%s: unveil ro", fname); - if (unveil(NULL, NULL) == -1) - err(errno, "unveil block (ro)"); - if (pledge("stdio flock rpath", NULL) == -1) - err(errno, "pledge ro (kill unveil)"); + err(errno, "%s: unveil r", fname); } else { - if (unveil(fname, "rw") == -1) + if (unveil(fname, "rwc") == -1) err(errno, "%s: unveil rw", fname); - if (unveil(NULL, NULL) == -1) - err(errno, "unveil block (rw)"); - if (pledge("stdio flock rpath wpath", NULL) == -1) - err(errno, "pledge rw (kill unveil)"); - } -#else - if (command[cmd_index].flags == O_RDONLY) { - if (pledge("stdio flock rpath", NULL) == -1) - err(errno, "pledge ro"); } -#endif + + if (unveil(tname, "rwc") == -1) + err(errno, "%s: unveil rwc", tname); + + if (unveil(NULL, NULL) == -1) + err(errno, "unveil block (rw)"); + + if (pledge("stdio flock rpath wpath cpath", NULL) == -1) + err(errno, "pledge (kill unveil)"); #endif -#if !defined(HAVE_ARC4RANDOM_BUF) || \ - (HAVE_ARC4RANDOM_BUF) < 1 srand((uint)(time(NULL) ^ getpid())); -#endif open_gbe_file(); - lock_gbe_file(); -#ifdef NVMUTIL_PLEDGE - if (pledge("stdio", NULL) == -1) - err(errno, "pledge stdio (main)"); -#endif + memset(buf, 0, GBE_BUF_SIZE); + memset(bufcmp, 0, GBE_BUF_SIZE); - /* - * Used by CMD_CAT, for padding - */ - memset(pad, 0xff, sizeof(pad)); + copy_gbe(); - read_gbe_file(); read_checksums(); run_cmd(cmd_index); - if (command[cmd_index].flags == O_RDWR) { - - write_gbe_file(); + if (command[cmd_index].flags == O_RDWR) + write_to_gbe_bin(); - /* - * We may otherwise read from - * cache, so we must sync. - */ - if (fsync(gbe_fd) == -1) - err(errno, "%s: fsync (pre-verification)", - fname); - - check_written_part(0); - check_written_part(1); - - report_io_err_rw(); + if (exit_cleanup() == -1) + err(EIO, "%s: close", fname); - if (io_err_gbe) - err(EIO, "%s: bad write", fname); - } + if (io_err_gbe_bin) + err(EIO, "%s: error writing final file"); - if (close_files() == -1) - err(EIO, "%s: close", fname); + if (tname != NULL) + free(tname); return EXIT_SUCCESS; } @@ -801,7 +815,7 @@ main(int argc, char *argv[]) static void sanitize_command_list(void) { - size_t c; + unsigned long c; for (c = 0; c < N_COMMANDS; c++) sanitize_command_index(c); @@ -811,10 +825,9 @@ sanitize_command_list(void) * TODO: specific config checks per command */ static void -sanitize_command_index(size_t c) +sanitize_command_index(unsigned long c) { - u8 mod_type; - size_t gbe_rw_size; + unsigned long gbe_rw_size; check_command_num(c); @@ -835,19 +848,10 @@ sanitize_command_index(size_t c) (ulong)c, command[c].str); } - mod_type = command[c].set_modified; - switch (mod_type) { - case SET_MOD_0: - case SET_MOD_1: - case SET_MOD_N: - case SET_MOD_BOTH: - case SET_MOD_OFF: - break; - default: - err(EINVAL, "Unsupported set_mod type: %u", mod_type); - } + if (command[c].run == NULL) + err(EINVAL, "cmd index %lu: cmd ptr null", + (ulong)c); - check_bin(command[c].invert, "cmd.invert"); check_bin(command[c].arg_part, "cmd.arg_part"); check_bin(command[c].chksum_read, "cmd.chksum_read"); check_bin(command[c].chksum_write, "cmd.chksum_write"); @@ -897,7 +901,7 @@ set_cmd_args(int argc, char *argv[]) u8 arg_part; if (!valid_command(cmd_index) || argc < 3) - usage(1); + usage(); arg_part = command[cmd_index].arg_part; @@ -915,7 +919,7 @@ set_cmd_args(int argc, char *argv[]) part = conv_argv_part_num(argv[3]); } -static size_t +static unsigned long conv_argv_part_num(const char *part_str) { u8 ch; @@ -928,7 +932,7 @@ conv_argv_part_num(const char *part_str) if (ch < '0' || ch > '1') err(EINVAL, "Bad part number (%c)", ch); - return (size_t)(ch - '0'); + return (unsigned long)(ch - '0'); } /* @@ -936,9 +940,9 @@ conv_argv_part_num(const char *part_str) * strings. Even stricter than strncmp(). */ static int -xstrxcmp(const char *a, const char *b, size_t maxlen) +xstrxcmp(const char *a, const char *b, unsigned long maxlen) { - size_t i; + unsigned long i; if (a == NULL || b == NULL) err(EINVAL, "NULL input to xstrxcmp"); @@ -979,7 +983,8 @@ open_gbe_file(void) int flags; xopen(&gbe_fd, fname, - command[cmd_index].flags | O_BINARY | O_NOFOLLOW, &gbe_st); + command[cmd_index].flags | O_BINARY | + O_NOFOLLOW | O_CLOEXEC, &gbe_st); /* inode will be checked later on write */ gbe_dev = gbe_st.st_dev; @@ -1004,7 +1009,7 @@ open_gbe_file(void) * which would therefore break pread/pwrite */ if (flags & O_APPEND) - err(EIO, "%s: O_APPEND flag"); + err(EIO, "%s: O_APPEND flag", fname); gbe_file_size = gbe_st.st_size; @@ -1016,10 +1021,13 @@ open_gbe_file(void) default: err(EINVAL, "File size must be 8KB, 16KB or 128KB"); } + + if (lock_file(gbe_fd) == -1) + err(errno, "%s: can't lock", fname); } -static void -lock_gbe_file(void) +static int +lock_file(int fd) { struct flock fl; @@ -1032,8 +1040,10 @@ lock_gbe_file(void) fl.l_whence = SEEK_SET; - if (fcntl(gbe_fd, F_SETLK, &fl) == -1) - err(errno, "file is locked by another process"); + if (fcntl(fd, F_SETLK, &fl) == -1) + return -1; + + return 0; } static void @@ -1043,7 +1053,7 @@ xopen(int *fd_ptr, const char *path, int flags, struct stat *st) err(errno, "%s", path); if (fstat(*fd_ptr, st) == -1) - err(errno, "%s", path); + err(errno, "%s: stat", path); if (!S_ISREG(st->st_mode)) err(errno, "%s: not a regular file", path); @@ -1052,31 +1062,89 @@ xopen(int *fd_ptr, const char *path, int flags, struct stat *st) err(errno, "%s: file not seekable", path); } +/* + * We copy the entire gbe file + * to the tmpfile, and then we + * work on that. We copy back + * afterward. this is the copy. + * + * we copy to tmpfile even on + * read-only commands, for the + * double-read verification, + * which also benefits cmd_cat. + */ static void -read_gbe_file(void) +copy_gbe(void) { - size_t p; - u8 do_read[2] = {1, 1}; + long r; + struct stat st; + + /* read main file */ + r = rw_file_exact(gbe_fd, buf, gbe_file_size, + 0, IO_PREAD, NO_LOOP_EAGAIN, LOOP_EINTR, + MAX_ZERO_RW_RETRY, OFF_ERR); + + if (r < 0) + err(errno, "%s: read failed", fname); + + /* copy to tmpfile */ + + r = rw_file_exact(tmp_fd, buf, gbe_file_size, + 0, IO_PWRITE, NO_LOOP_EAGAIN, LOOP_EINTR, + MAX_ZERO_RW_RETRY, OFF_ERR); + + if (r < 0) + err(errno, "%s: %s: copy failed", + fname, tname); /* - * Commands specifying a partnum only - * need the given GbE part to be read. + * file size comparison */ - if (command[cmd_index].arg_part) - do_read[part ^ 1] = 0; - for (p = 0; p < 2; p++) { - if (do_read[p]) - rw_gbe_file_part(p, IO_PREAD, "pread"); - } + if (fstat(tmp_fd, &st) == -1) + err(errno, "%s: stat", tname); + + gbe_tmp_size = st.st_size; + + if (gbe_tmp_size != gbe_file_size) + err(EIO, "%s: %s: not the same size", fname, tname); + + sync(); + + r = rw_file_exact(tmp_fd, bufcmp, gbe_file_size, + 0, IO_PREAD, NO_LOOP_EAGAIN, LOOP_EINTR, + MAX_ZERO_RW_RETRY, OFF_ERR); + + if (r < 0) + err(errno, "%s: read failed (cmp)", tname); + + if (memcmp(buf, bufcmp, gbe_file_size) != 0) + err(errno, "%s: %s: read contents differ (pre-test)", + fname, tname); + + /* + regular operations post-read operate only on the first + 8KB, because each GbE part is the first 4KB of each + half of the file. + + we no longer care about anything past 8KB, until we get + to writing, at which point we will flush the buffer + again + */ + + if (gbe_file_size == SIZE_8KB) + return; + + memcpy(buf + (unsigned long)GBE_PART_SIZE, + buf + (unsigned long)(gbe_file_size >> 1), + (unsigned long)GBE_PART_SIZE); } static void read_checksums(void) { - size_t p; - size_t skip_part; - u8 invert; + unsigned long p; + unsigned long skip_part; u8 arg_part; u8 num_invalid; u8 max_invalid; @@ -1090,7 +1158,6 @@ read_checksums(void) num_invalid = 0; max_invalid = 2; - invert = command[cmd_index].invert; arg_part = command[cmd_index].arg_part; if (arg_part) max_invalid = 1; @@ -1099,7 +1166,7 @@ read_checksums(void) * Skip verification on this part, * but only when arg_part is set. */ - skip_part = part ^ 1 ^ invert; + skip_part = part ^ 1; for (p = 0; p < 2; p++) { /* @@ -1123,7 +1190,7 @@ read_checksums(void) } static int -good_checksum(size_t partnum) +good_checksum(unsigned long partnum) { ushort expected_checksum = calculated_checksum(partnum); ushort current_checksum = nvm_word(NVM_CHECKSUM_WORD, partnum); @@ -1135,15 +1202,18 @@ good_checksum(size_t partnum) } static void -run_cmd(size_t c) +run_cmd(unsigned long c) { check_command_num(c); - if (command[c].run != NULL) - command[c].run(); + + if (command[c].run == NULL) + err(EINVAL, "Command %lu: null ptr", (ulong)c); + + command[c].run(); } static void -check_command_num(size_t c) +check_command_num(unsigned long c) { if (!valid_command(c)) err(EINVAL, "Invalid run_cmd arg: %lu", @@ -1151,7 +1221,7 @@ check_command_num(size_t c) } static u8 -valid_command(size_t c) +valid_command(unsigned long c) { if (c >= N_COMMANDS) return 0; @@ -1166,7 +1236,7 @@ valid_command(size_t c) static void cmd_helper_setmac(void) { - size_t partnum; + unsigned long partnum; printf("MAC address to be written: %s\n", mac_str); parse_mac_string(); @@ -1178,7 +1248,7 @@ cmd_helper_setmac(void) static void parse_mac_string(void) { - size_t mac_byte; + unsigned long mac_byte; if (xstrxlen(mac_str, 18) != 17) err(EINVAL, "MAC address is the wrong length"); @@ -1201,10 +1271,10 @@ parse_mac_string(void) * strnlen() was standardized in POSIX.1-2008 and is not * available on some older systems, so we provide our own. */ -static size_t -xstrxlen(const char *scmp, size_t maxlen) +static unsigned long +xstrxlen(const char *scmp, unsigned long maxlen) { - size_t xstr_index; + unsigned long xstr_index; if (scmp == NULL) err(EINVAL, "NULL input to xstrxlen"); @@ -1223,10 +1293,10 @@ xstrxlen(const char *scmp, size_t maxlen) } static void -set_mac_byte(size_t mac_byte_pos) +set_mac_byte(unsigned long mac_byte_pos) { - size_t mac_str_pos = mac_byte_pos * 3; - size_t mac_nib_pos; + unsigned long mac_str_pos = mac_byte_pos * 3; + unsigned long mac_nib_pos; char separator; if (mac_str_pos < 15) { @@ -1240,8 +1310,8 @@ set_mac_byte(size_t mac_byte_pos) } static void -set_mac_nib(size_t mac_str_pos, - size_t mac_byte_pos, size_t mac_nib_pos) +set_mac_nib(unsigned long mac_str_pos, + unsigned long mac_byte_pos, unsigned long mac_nib_pos) { char mac_ch; ushort hex_num; @@ -1288,30 +1358,23 @@ hextonum(char ch_s) return 16; /* invalid character */ } -#if defined(HAVE_ARC4RANDOM_BUF) && \ - (HAVE_ARC4RANDOM_BUF) > 0 -static ushort -rhex(void) -{ - static u8 num[12]; - static size_t n = 0; - - if (!n) { - n = 12; - arc4random_buf(num, 12); - } - - return num[--n] & 0xf; -} -#else static ushort rhex(void) { - struct timeval tv; + struct x_st_timeval tv; ulong mix; static ulong counter = 0; + ushort r; + + /* Read /dev/urandom + * if possible */ + r = read_urandom(); + if (r < 16) + return r; + + /* Fallback */ - gettimeofday(&tv, NULL); + x_i_gettimeofday(&tv, NULL); mix = (ulong)tv.tv_sec ^ (ulong)tv.tv_usec @@ -1331,18 +1394,54 @@ rhex(void) return (ushort)(mix & 0xf); } +static ushort +read_urandom(void) +{ + static int fd = -1; + static long n = -1; + + static u8 r[256]; + + if (fd < 0) { + + fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK); + if (fd < 0) /* older openbsd */ + fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK); + if (fd < 0) /* super old unix (could block) */ + fd = open("/dev/random", O_RDONLY | O_NONBLOCK); + + if (fd < 0) + return 16; + } + + if (n < 0) { + + n = rw_file_exact(fd, r, 256, 0, IO_READ, + LOOP_EAGAIN, LOOP_EINTR, 2, OFF_ERR); + + if (n == 0) + n = -1; + if (n < 0) + return 16; + + --n; + } + + return r[n--] & 0xf; +} + static ulong entropy_jitter(void) { - struct timeval a, b; + struct x_st_timeval a, b; ulong mix = 0; long mix_diff; int i; for (i = 0; i < 8; i++) { - gettimeofday(&a, NULL); + x_i_gettimeofday(&a, NULL); getpid(); - gettimeofday(&b, NULL); + x_i_gettimeofday(&b, NULL); /* * prevent negative numbers to prevent overflow, @@ -1358,12 +1457,28 @@ entropy_jitter(void) return mix; } -#endif + + + +static int +x_i_gettimeofday(struct x_st_timeval *tv, void *tz) +{ + time_t t; + + (void)tz; + + t = time(NULL); + + tv->tv_sec = t; + tv->tv_usec = (long)clock() % 1000000; + + return 0; +} static void -write_mac_part(size_t partnum) +write_mac_part(unsigned long partnum) { - size_t w; + unsigned long w; check_bin(partnum, "part number"); if (!part_valid[partnum]) @@ -1380,7 +1495,7 @@ write_mac_part(size_t partnum) static void cmd_helper_dump(void) { - size_t partnum; + unsigned long partnum; part_valid[0] = good_checksum(0); part_valid[1] = good_checksum(1); @@ -1401,9 +1516,9 @@ cmd_helper_dump(void) } static void -print_mac_from_nvm(size_t partnum) +print_mac_from_nvm(unsigned long partnum) { - size_t c; + unsigned long c; ushort val16; for (c = 0; c < 3; c++) { @@ -1419,14 +1534,14 @@ print_mac_from_nvm(size_t partnum) } static void -hexdump(size_t partnum) +hexdump(unsigned long partnum) { - size_t c; - size_t row; + unsigned long c; + unsigned long row; ushort val16; for (row = 0; row < 8; row++) { - printf("%08lx ", (ulong)((size_t)row << 4)); + printf("%08lx ", (ulong)((unsigned long)row << 4)); for (c = 0; c < 8; c++) { val16 = nvm_word((row << 3) + c, partnum); if (c == 4) @@ -1440,31 +1555,73 @@ hexdump(size_t partnum) } static void -cmd_helper_cat(void) +cmd_helper_swap(void) +{ + memcpy( + buf + (unsigned long)GBE_WORK_SIZE, + buf, + GBE_PART_SIZE); + + memcpy( + buf, + buf + (unsigned long)GBE_PART_SIZE, + GBE_PART_SIZE); + + memcpy( + buf + (unsigned long)GBE_PART_SIZE, + buf + (unsigned long)GBE_WORK_SIZE, + GBE_PART_SIZE); + + set_part_modified(0); + set_part_modified(1); +} + +static void +cmd_helper_copy(void) { - size_t p; - size_t ff; - size_t n = 0; + memcpy( + buf + (unsigned long)((part ^ 1) * GBE_PART_SIZE), + buf + (unsigned long)(part * GBE_PART_SIZE), + GBE_PART_SIZE); + + set_part_modified(part ^ 1); +} - if (cmd_index == CMD_CAT16) - n = 1; - else if (cmd_index == CMD_CAT128) - n = 15; - else if (cmd_index != CMD_CAT) - err(EINVAL, "cmd_helper_cat called erroneously"); +static void +cmd_helper_cat(void) +{ + unsigned long p = 0; + unsigned long ff = 0; + unsigned long nff = 0; fflush(NULL); + memset(pad, 0xff, GBE_PART_SIZE); + + switch (cmd_index) { + case CMD_CAT: + nff = 0; + break; + case CMD_CAT16: + nff = 1; + break; + case CMD_CAT128: + nff = 15; + break; + default: + err(EINVAL, "erroneous call to cat"); + } + for (p = 0; p < 2; p++) { - gbe_cat_buf(buf + (size_t)(p * GBE_PART_SIZE)); + cat_buf(bufcmp + (unsigned long)(p * (gbe_file_size >> 1))); - for (ff = 0; ff < n; ff++) - gbe_cat_buf(pad); + for (ff = 0; ff < nff; ff++) + cat_buf(pad); } } static void -gbe_cat_buf(u8 *b) +cat_buf(u8 *b) { if (rw_file_exact(STDOUT_FILENO, b, GBE_PART_SIZE, 0, IO_WRITE, LOOP_EAGAIN, LOOP_EINTR, @@ -1476,81 +1633,56 @@ static void write_gbe_file(void) { struct stat gbe_st; + struct stat tmp_st; - size_t p; - size_t partnum; + unsigned long p; u8 update_checksum; if (command[cmd_index].flags == O_RDONLY) return; - update_checksum = command[cmd_index].chksum_write; - - override_part_modified(); - if (fstat(gbe_fd, &gbe_st) == -1) err(errno, "%s: re-check", fname); - if (gbe_st.st_dev != gbe_dev || gbe_st.st_ino != gbe_ino) err(EIO, "%s: file replaced while open", fname); - if (gbe_st.st_size != gbe_file_size) err(errno, "%s: file size changed before write", fname); - if (!S_ISREG(gbe_st.st_mode)) err(errno, "%s: file type changed before write", fname); - for (p = 0; p < 2; p++) { - partnum = p ^ command[cmd_index].invert; + if (fstat(tmp_fd, &tmp_st) == -1) + err(errno, "%s: re-check", tname); + if (tmp_st.st_dev != tmp_dev || tmp_st.st_ino != tmp_ino) + err(EIO, "%s: file replaced while open", tname); + if (tmp_st.st_size != gbe_file_size) + err(errno, "%s: file size changed before write", tname); + if (!S_ISREG(tmp_st.st_mode)) + err(errno, "%s: file type changed before write", tname); - if (!part_modified[partnum]) + update_checksum = command[cmd_index].chksum_write; + + for (p = 0; p < 2; p++) { + if (!part_modified[p]) continue; if (update_checksum) - set_checksum(partnum); - - rw_gbe_file_part(partnum, IO_PWRITE, "pwrite"); - } -} - -static void -override_part_modified(void) -{ - u8 mod_type = command[cmd_index].set_modified; + set_checksum(p); - switch (mod_type) { - case SET_MOD_0: - set_part_modified(0); - break; - case SET_MOD_1: - set_part_modified(1); - break; - case SET_MOD_N: - set_part_modified(part ^ command[cmd_index].invert); - break; - case SET_MOD_BOTH: - set_part_modified(0); - set_part_modified(1); - break; - case SET_MOD_OFF: - break; - default: - err(EINVAL, "Unsupported set_mod type: %u", - mod_type); + rw_gbe_file_part(p, IO_PWRITE, "pwrite"); } } static void -set_checksum(size_t p) +set_checksum(unsigned long p) { check_bin(p, "part number"); set_nvm_word(NVM_CHECKSUM_WORD, p, calculated_checksum(p)); } static ushort -calculated_checksum(size_t p) +calculated_checksum(unsigned long p) { - size_t c; + unsigned long c; uint val16 = 0; for (c = 0; c < NVM_CHECKSUM_WORD; c++) @@ -1568,9 +1700,9 @@ calculated_checksum(size_t p) */ static ushort -nvm_word(size_t pos16, size_t p) +nvm_word(unsigned long pos16, unsigned long p) { - size_t pos; + unsigned long pos; check_nvm_bound(pos16, p); pos = (pos16 << 1) + (p * GBE_PART_SIZE); @@ -1580,9 +1712,9 @@ nvm_word(size_t pos16, size_t p) } static void -set_nvm_word(size_t pos16, size_t p, ushort val16) +set_nvm_word(unsigned long pos16, unsigned long p, ushort val16) { - size_t pos; + unsigned long pos; check_nvm_bound(pos16, p); pos = (pos16 << 1) + (p * GBE_PART_SIZE); @@ -1594,14 +1726,14 @@ set_nvm_word(size_t pos16, size_t p, ushort val16) } static void -set_part_modified(size_t p) +set_part_modified(unsigned long p) { check_bin(p, "part number"); part_modified[p] = 1; } static void -check_nvm_bound(size_t c, size_t p) +check_nvm_bound(unsigned long c, unsigned long p) { /* * NVM_SIZE assumed as the limit, because this @@ -1617,7 +1749,7 @@ check_nvm_bound(size_t c, size_t p) } static void -check_bin(size_t a, const char *a_name) +check_bin(unsigned long a, const char *a_name) { if (a > 1) err(EINVAL, "%s must be 0 or 1, but is %lu", @@ -1625,12 +1757,11 @@ check_bin(size_t a, const char *a_name) } static void -rw_gbe_file_part(size_t p, int rw_type, +rw_gbe_file_part(unsigned long p, int rw_type, const char *rw_type_str) { - ssize_t r; - size_t gbe_rw_size = command[cmd_index].rw_size; - u8 invert = command[cmd_index].invert; + long r; + unsigned long gbe_rw_size = command[cmd_index].rw_size; u8 *mem_offset; off_t file_offset; @@ -1639,33 +1770,106 @@ rw_gbe_file_part(size_t p, int rw_type, err(errno, "%s: %s: part %lu: invalid rw_type, %d", fname, rw_type_str, (ulong)p, rw_type); - if (rw_type == IO_PWRITE) - invert = 0; - - /* - * Inverted reads are used by copy/swap. - * E.g. read from p0 (file) to p1 (mem). - */ - mem_offset = gbe_mem_offset(p ^ invert, rw_type_str); + mem_offset = gbe_mem_offset(p, rw_type_str); file_offset = (off_t)gbe_file_offset(p, rw_type_str); - r = rw_gbe_file_exact(gbe_fd, mem_offset, + r = rw_gbe_file_exact(tmp_fd, mem_offset, gbe_rw_size, file_offset, rw_type); if (r == -1) err(errno, "%s: %s: part %lu", fname, rw_type_str, (ulong)p); - if ((size_t)r != gbe_rw_size) + if ((unsigned long)r != gbe_rw_size) err(EIO, "%s: partial %s: part %lu", fname, rw_type_str, (ulong)p); } static void -check_written_part(size_t p) +write_to_gbe_bin(void) { - ssize_t r; - size_t gbe_rw_size; + int saved_errno; + int mv; + + if (command[cmd_index].flags != O_RDWR) + return; + + write_gbe_file(); + + sync(); + + check_written_part(0); + check_written_part(1); + + report_io_err_rw(); + + if (io_err_gbe) + err(EIO, "%s: bad write", fname); + + /* + * success! + * now just rename the tmpfile + */ + + saved_errno = errno; + + if (close(tmp_fd) == -1) { + fprintf(stderr, "FAIL: %s: close\n", tname); + io_err_gbe_bin = 1; + } + + if (close(gbe_fd) == -1) { + fprintf(stderr, "FAIL: %s: close\n", fname); + io_err_gbe_bin = 1; + } + + errno = saved_errno; + + tmp_fd = -1; + gbe_fd = -1; + + if (!io_err_gbe_bin) { + + mv = gbe_mv(); + + if (mv < 0) { + io_err_gbe_bin = 1; + fprintf(stderr, "%s: %s\n", + fname, strerror(errno)); + } else { + /* + * tmpfile removed + * by the rename + */ + + if (tname != NULL) + free(tname); + + tname = NULL; + } + } + + /* + * finally: + * must sync to disk! + * very nearly done + */ + + if (!io_err_gbe_bin) + return; + + fprintf(stderr, "FAIL (rename): %s: skipping fsync\n", + fname); + if (errno) + fprintf(stderr, + "errno %d: %s\n", errno, strerror(errno)); +} + +static void +check_written_part(unsigned long p) +{ + long r; + unsigned long gbe_rw_size; u8 *mem_offset; off_t file_offset; u8 *buf_restore; @@ -1684,16 +1888,20 @@ check_written_part(size_t p) if (fstat(gbe_fd, &st) == -1) err(errno, "%s: fstat (post-write)", fname); - if (st.st_dev != gbe_dev || st.st_ino != gbe_ino) err(EIO, "%s: file changed during write", fname); - r = rw_gbe_file_exact(gbe_fd, pad, + if (fstat(tmp_fd, &st) == -1) + err(errno, "%s: fstat (post-write)", tname); + if (st.st_dev != tmp_dev || st.st_ino != tmp_ino) + err(EIO, "%s: file changed during write", tname); + + r = rw_gbe_file_exact(tmp_fd, pad, gbe_rw_size, file_offset, IO_PREAD); if (r == -1) rw_check_err_read[p] = io_err_gbe = 1; - else if ((size_t)r != gbe_rw_size) + else if ((unsigned long)r != gbe_rw_size) rw_check_partial_read[p] = io_err_gbe = 1; else if (memcmp(mem_offset, pad, gbe_rw_size) != 0) rw_check_bad_part[p] = io_err_gbe = 1; @@ -1716,7 +1924,7 @@ check_written_part(size_t p) static void report_io_err_rw(void) { - size_t p; + unsigned long p; if (!io_err_gbe) return; @@ -1766,18 +1974,221 @@ report_io_err_rw(void) } } +static int +gbe_mv(void) +{ + int r; + int saved_errno; + int tmp_gbe_bin_exists = 1; + + char *dest_tmp = NULL; + int dest_fd = -1; + + saved_errno = errno; + + r = x_i_rename(tname, fname); + + if (r > -1) { + /* + * same filesystem + */ + + tmp_gbe_bin_exists = 0; + + if (fsync_dir(fname) < 0) + r = -1; + + goto ret_gbe_mv; + } + + if (errno != EXDEV) + goto ret_gbe_mv; + + /* cross-filesystem rename */ + + if ((r = tmp_fd = open(tname, + O_RDONLY | O_BINARY)) == -1) + goto ret_gbe_mv; + + /* create replacement temp in target directory */ + dest_tmp = new_tmpfile(&dest_fd, 1, fname); + if (dest_tmp == NULL) + goto ret_gbe_mv; + + /* copy data */ + + r = rw_file_exact(tmp_fd, bufcmp, + gbe_file_size, 0, IO_PREAD, + NO_LOOP_EAGAIN, LOOP_EINTR, + MAX_ZERO_RW_RETRY, OFF_ERR); + + if (r < 0) + goto ret_gbe_mv; + + r = rw_file_exact(dest_fd, bufcmp, + gbe_file_size, 0, IO_PWRITE, + NO_LOOP_EAGAIN, LOOP_EINTR, + MAX_ZERO_RW_RETRY, OFF_ERR); + + if (r < 0) + goto ret_gbe_mv; + + sync(); + + if (close(dest_fd) == -1) + goto ret_gbe_mv; + + if (x_i_rename(dest_tmp, fname) == -1) + goto ret_gbe_mv; + + if (fsync_dir(fname) < 0) + goto ret_gbe_mv; + + free(dest_tmp); + dest_tmp = NULL; + +ret_gbe_mv: + + if (gbe_fd > -1) { + if (close(gbe_fd) < 0) + r = -1; + if (fsync_dir(fname) < 0) + r = -1; + gbe_fd = -1; + } + + if (tmp_fd > -1) { + if (close(tmp_fd) < 0) + r = -1; + + tmp_fd = -1; + } + + /* + * before this function is called, + * tmp_fd may have been moved + */ + if (tmp_gbe_bin_exists) { + if (unlink(tname) < 0) + r = -1; + else + tmp_gbe_bin_exists = 0; + } + + if (r < 0) { + /* + * if nothing set errno, + * we assume EIO, or we + * use what was set + */ + if (errno == saved_errno) + errno = EIO; + } else { + errno = saved_errno; + } + + return r; +} + +/* + * Ensure x_i_rename() is durable by syncing the + * directory containing the target file. + */ +static int +fsync_dir(const char *path) +{ +#if defined(PATH_LEN) && \ + (PATH_LEN) >= 256 + unsigned long maxlen = PATH_LEN; +#else + unsigned long maxlen = 1024; +#endif + unsigned long pathlen; +/* char dirbuf[maxlen]; */ + char *dirbuf = NULL; + char *slash; + int dfd = -1; + + struct stat st; + + int saved_errno = errno; + + pathlen = xstrxlen(path, maxlen); + + if (pathlen >= maxlen) { + fprintf(stderr, "Path too long for fsync_parent_dir\n"); + goto err_fsync_dir; + } + + dirbuf = malloc(pathlen + 1); + if (dirbuf == NULL) + goto err_fsync_dir; + + memcpy(dirbuf, path, pathlen + 1); + slash = x_c_strrchr(dirbuf, '/'); + + if (slash != NULL) { + *slash = '\0'; + if (*dirbuf == '\0') + strcpy(dirbuf, "/"); + } else { + strcpy(dirbuf, "."); + } + + dfd = open(dirbuf, O_RDONLY); + if (dfd == -1) + goto err_fsync_dir; + + if (fstat(dfd, &st) < 0) + goto err_fsync_dir; + + if (!S_ISDIR(st.st_mode)) { + fprintf(stderr, "%s: not a directory\n", dirbuf); + goto err_fsync_dir; + } + + sync(); + + if (close(dfd) == -1) + goto err_fsync_dir; + + if (dirbuf != NULL) + free(dirbuf); + + errno = saved_errno; + return 0; + +err_fsync_dir: + if (!errno) + errno = EIO; + + if (errno != saved_errno) + fprintf(stderr, "%s: %s\n", fname, strerror(errno)); + + if (dirbuf != NULL) + free(dirbuf); + + if (dfd > -1) + close(dfd); + + io_err_gbe_bin = 1; + errno = saved_errno; + + return -1; +} + /* * This one is similar to gbe_file_offset, * but used to check Gbe bounds in memory, * and it is *also* used during file I/O. */ static u8 * -gbe_mem_offset(size_t p, const char *f_op) +gbe_mem_offset(unsigned long p, const char *f_op) { off_t gbe_off = gbe_x_offset(p, f_op, "mem", - GBE_PART_SIZE, GBE_FILE_SIZE); + GBE_PART_SIZE, GBE_WORK_SIZE); - return (u8 *)(buf + (size_t)gbe_off); + return (u8 *)(buf + (unsigned long)gbe_off); } /* @@ -1788,7 +2199,7 @@ gbe_mem_offset(size_t p, const char *f_op) * This check is called, to ensure just that. */ static off_t -gbe_file_offset(size_t p, const char *f_op) +gbe_file_offset(unsigned long p, const char *f_op) { off_t gbe_file_half_size = gbe_file_size >> 1; @@ -1797,7 +2208,7 @@ gbe_file_offset(size_t p, const char *f_op) } static off_t -gbe_x_offset(size_t p, const char *f_op, const char *d_type, +gbe_x_offset(unsigned long p, const char *f_op, const char *d_type, off_t nsize, off_t ncmp) { off_t off; @@ -1817,35 +2228,35 @@ gbe_x_offset(size_t p, const char *f_op, const char *d_type, return off; } -static ssize_t -rw_gbe_file_exact(int fd, u8 *mem, size_t nrw, +static long +rw_gbe_file_exact(int fd, u8 *mem, unsigned long nrw, off_t off, int rw_type) { - size_t mem_addr; - size_t buf_addr; - ssize_t r; + unsigned long mem_addr; + unsigned long buf_addr; + long r; if (io_args(fd, mem, nrw, off, rw_type) == -1) return -1; - mem_addr = (size_t)(void *)mem; - buf_addr = (size_t)(void *)buf; + mem_addr = (unsigned long)(void *)mem; + buf_addr = (unsigned long)(void *)buf; if (mem != (void *)pad) { if (mem_addr < buf_addr) goto err_rw_gbe_file_exact; - if ((mem_addr - buf_addr) >= (size_t)GBE_FILE_SIZE) + if ((mem_addr - buf_addr) >= (unsigned long)GBE_WORK_SIZE) goto err_rw_gbe_file_exact; } if (off < 0 || off >= gbe_file_size) goto err_rw_gbe_file_exact; - if (nrw > (size_t)(gbe_file_size - off)) + if (nrw > (unsigned long)(gbe_file_size - off)) goto err_rw_gbe_file_exact; - if (nrw > (size_t)GBE_PART_SIZE) + if (nrw > (unsigned long)GBE_PART_SIZE) goto err_rw_gbe_file_exact; r = rw_file_exact(fd, mem, nrw, off, rw_type, @@ -1889,17 +2300,17 @@ err_rw_gbe_file_exact: * times upon zero-return, to recover, * otherwise it will return an error. */ -static ssize_t -rw_file_exact(int fd, u8 *mem, size_t nrw, +static long +rw_file_exact(int fd, u8 *mem, unsigned long nrw, off_t off, int rw_type, int loop_eagain, - int loop_eintr, size_t max_retries, + int loop_eintr, unsigned long max_retries, int off_reset) { - ssize_t rv = 0; - ssize_t rc = 0; - size_t retries_on_zero = 0; + long rv = 0; + long rc = 0; + unsigned long retries_on_zero = 0; off_t off_cur; - size_t nrw_cur; + unsigned long nrw_cur; void *mem_cur; if (io_args(fd, mem, nrw, off, rw_type) == -1) @@ -1908,18 +2319,18 @@ rw_file_exact(int fd, u8 *mem, size_t nrw, while (1) { /* Prevent theoretical overflow */ - if (rv >= 0 && (size_t)rv > (nrw - rc)) + if (rv >= 0 && (unsigned long)rv > (nrw - rc)) goto err_rw_file_exact; rc += rv; - if ((size_t)rc >= nrw) + if ((unsigned long)rc >= nrw) break; - mem_cur = (void *)(mem + (size_t)rc); - nrw_cur = (size_t)(nrw - (size_t)rc); + mem_cur = (void *)(mem + (unsigned long)rc); + nrw_cur = (unsigned long)(nrw - (unsigned long)rc); if (off < 0) goto err_rw_file_exact; - off_cur = (off_t)((size_t)off + (size_t)rc); + off_cur = off + (off_t)rc; rv = prw(fd, mem_cur, nrw_cur, off_cur, rw_type, loop_eagain, loop_eintr, @@ -1937,7 +2348,7 @@ rw_file_exact(int fd, u8 *mem, size_t nrw, retries_on_zero = 0; } - if ((size_t)rc != nrw) + if ((unsigned long)rc != nrw) goto err_rw_file_exact; return rw_over_nrw(rc, nrw); @@ -1989,13 +2400,13 @@ err_rw_file_exact: * we reset and continue, and pray for the worst. */ -static ssize_t -prw(int fd, void *mem, size_t nrw, +static long +prw(int fd, void *mem, unsigned long nrw, off_t off, int rw_type, int loop_eagain, int loop_eintr, int off_reset) { - ssize_t r; + long r; int positional_rw; struct stat st; #if !defined(HAVE_REAL_PREAD_PWRITE) || \ @@ -2086,19 +2497,6 @@ real_pread_pwrite: loop_eagain, loop_eintr); do { - if (off != verified) - goto err_prw; - - if (rw_type == IO_PREAD) - r = read(fd, mem, nrw); - else if (rw_type == IO_PWRITE) - r = write(fd, mem, nrw); - - if (rw_over_nrw(r, nrw) == -1) { - errno = EIO; - break; - } - /* * Verify again before I/O * (even with OFF_ERR) @@ -2119,6 +2517,19 @@ real_pread_pwrite: verified = lseek_loop(fd, (off_t)0, SEEK_CUR, loop_eagain, loop_eintr); + if (off != verified) + goto err_prw; + + if (rw_type == IO_PREAD) + r = read(fd, mem, nrw); + else if (rw_type == IO_PWRITE) + r = write(fd, mem, nrw); + + if (rw_over_nrw(r, nrw) == -1) { + errno = EIO; + break; + } + } while (r == -1 && (errno == try_err(loop_eintr, EINTR) || errno == try_err(loop_eagain, EAGAIN))); @@ -2145,7 +2556,7 @@ err_prw: } static int -io_args(int fd, void *mem, size_t nrw, +io_args(int fd, void *mem, unsigned long nrw, off_t off, int rw_type) { /* obviously */ @@ -2165,11 +2576,11 @@ io_args(int fd, void *mem, size_t nrw, goto err_io_args; /* prevent overflow */ - if (nrw > (size_t)SSIZE_MAX) + if (nrw > (unsigned long)X_LONG_MAX) goto err_io_args; /* prevent overflow */ - if (((size_t)off + nrw) < (size_t)off) + if (((unsigned long)off + nrw) < (unsigned long)off) goto err_io_args; if (rw_type > IO_PWRITE) @@ -2204,8 +2615,8 @@ err_is_file: * POSIX can say whatever it wants. * specification != implementation */ -static ssize_t -rw_over_nrw(ssize_t r, size_t nrw) +static long +rw_over_nrw(long r, unsigned long nrw) { /* * If a byte length of zero @@ -2218,14 +2629,14 @@ rw_over_nrw(ssize_t r, size_t nrw) if (r == -1) return r; - if ((size_t)r > SSIZE_MAX) { + if ((unsigned long)r > X_LONG_MAX) { /* * Theoretical buggy libc * check. Extremely academic. * * Specifications never * allow this return value - * to exceed SSIZE_MAX, but + * to exceed SSIZE_T, but * spec != implementation * * Check this after using @@ -2239,7 +2650,7 @@ rw_over_nrw(ssize_t r, size_t nrw) * Should never return a number of * bytes above the requested length. */ - if ((size_t)r > nrw) + if ((unsigned long)r > nrw) goto err_rw_over_nrw; return r; @@ -2292,14 +2703,37 @@ try_err(int loop_err, int errval) } static void +usage(void) +{ + const char *util = getnvmprogname(); + + fprintf(stderr, + "Modify Intel GbE NVM images e.g. set MAC\n" + "USAGE:\n" + "\t%s FILE dump\n" + "\t%s FILE setmac [MAC]\n" + "\t%s FILE swap\n" + "\t%s FILE copy 0|1\n" + "\t%s FILE cat\n" + "\t%s FILE cat16\n" + "\t%s FILE cat128\n", + util, util, util, util, + util, util, util); + + err(EINVAL, "Too few arguments"); +} + +static void err(int nvm_errval, const char *msg, ...) { va_list args; if (errno == 0) errno = nvm_errval; + if (!errno) + errno = ECANCELED; - (void)close_files(); + (void)exit_cleanup(); fprintf(stderr, "%s: ", getnvmprogname()); @@ -2310,25 +2744,41 @@ err(int nvm_errval, const char *msg, ...) fprintf(stderr, ": %s", strerror(errno)); fprintf(stderr, "\n"); + + if (tname != NULL) + free(tname); + exit(EXIT_FAILURE); } static int -close_files(void) +exit_cleanup(void) { - int close_err_gbe = 0; + int close_err = 0; int saved_errno = errno; if (gbe_fd > -1) { if (close(gbe_fd) == -1) - close_err_gbe = errno; + close_err = 1; gbe_fd = -1; } + if (tmp_fd > -1) { + if (close(tmp_fd) == -1) + close_err = 1; + } + + if (tname != NULL) { + if (unlink(tname) == -1) + close_err = 1; + } + + tmp_fd = -1; + if (saved_errno) errno = saved_errno; - if (close_err_gbe) + if (close_err) return -1; return 0; @@ -2342,7 +2792,7 @@ getnvmprogname(void) if (argv0 == NULL || *argv0 == '\0') return ""; - p = strrchr(argv0, '/'); + p = x_c_strrchr(argv0, '/'); if (p) return p + 1; @@ -2350,28 +2800,304 @@ getnvmprogname(void) return argv0; } -static void -usage(int usage_exit) +/* + * create new tmpfile path + * + * ON SUCCESS: + * + * returns ptr to path string on success + * ALSO: the int at *fd will be set, + * indicating the file descriptor + * + * ON ERROR: + * + * return NULL (*fd not touched) + * + * malloc() may set errno, but you should + * not rely on errno from this function + * + * local: if non-zero, then only a file + * name will be given, relative to + * the current file name. for this, + * the 3rd argument (path) must be non-null + * + * if local is zero, then 3rd arg (path) + * is irrelevant and can be NULL + */ +static char * +new_tmpfile(int *fd, int local, const char *path) { - const char *util = getnvmprogname(); + unsigned long maxlen; + struct stat st; -#ifdef NVMUTIL_PLEDGE - if (pledge("stdio", NULL) == -1) - err(errno, "pledge"); + /* + * please do not modify the + * strings or I will get mad + */ + char tmp_none[] = ""; + char tmp_default[] = "/tmp"; + char default_tmpname[] = "tmpXXXXXX"; + char *tmpname; + + char *base = NULL; + char *dest = NULL; + + unsigned long tmpdir_len = 0; + unsigned long tmpname_len = 0; + unsigned long tmppath_len = 0; + + int fd_tmp = -1; + int flags; + + /* + * 256 is the most + * conservative path + * size limit (posix), + * but 4096 is modern + * + * set PATH_LEN as you + * wish, at build time + */ + +#if defined(PATH_LEN) && \ + (PATH_LEN) >= 256 + maxlen = PATH_LEN; +#else + maxlen = 1024; #endif - fprintf(stderr, - "Modify Intel GbE NVM images e.g. set MAC\n" - "USAGE:\n" - "\t%s FILE dump\n" - "\t%s FILE setmac [MAC]\n" - "\t%s FILE swap\n" - "\t%s FILE copy 0|1\n" - "\t%s FILE cat\n" - "\t%s FILE cat16\n" - "\t%s FILE cat128\n", - util, util, util, util, - util, util, util); - if (usage_exit) - err(EINVAL, "Too few arguments"); + tmpname = default_tmpname; + if (local) { + if (path == NULL) + goto err_new_tmpfile; + if (*path == '\0') + goto err_new_tmpfile; + + if (stat(path, &st) == -1) + goto err_new_tmpfile; + + if (!S_ISREG(st.st_mode)) + goto err_new_tmpfile; + + tmpname = (char *)path; + } + + if (local) { + base = tmp_none; + + /* + * appended to filename for tmp: + */ + tmpdir_len = sizeof(default_tmpname); + } else { + base = x_c_tmpdir(); + + if (base == NULL) + base = tmp_default; + if (*base == '\0') + base = tmp_default; + + tmpdir_len = xstrxlen(base, maxlen); + } + + tmpname_len = xstrxlen(tmpname, maxlen); + + tmppath_len = tmpdir_len + tmpname_len; + ++tmppath_len; /* for '/' or '.' */ + + /* + * max length -1 of maxlen + * for termination + */ + if (tmpdir_len > maxlen - tmpname_len - 1) + goto err_new_tmpfile; + + /* +1 for NULL */ + dest = malloc(tmppath_len + 1); + if (dest == NULL) + goto err_new_tmpfile; + + if (local) { + + *dest = '.'; /* hidden file */ + + memcpy(dest + (unsigned long)1, tmpname, tmpname_len); + + memcpy(dest + (unsigned long)1 + tmpname_len, + default_tmpname, tmpdir_len); + } else { + + memcpy(dest, base, tmpdir_len); + + dest[tmpdir_len] = '/'; + + memcpy(dest + tmpdir_len + 1, tmpname, tmpname_len); + } + + dest[tmppath_len] = '\0'; + + fd_tmp = x_i_mkstemp(dest); + if (fd_tmp == -1) + goto err_new_tmpfile; + + if (fchmod(fd_tmp, 0600) == -1) + goto err_new_tmpfile; + + if (lock_file(fd_tmp) == -1) + goto err_new_tmpfile; + + if (fstat(fd_tmp, &st) == -1) + goto err_new_tmpfile; + + /* + * Extremely defensive + * likely pointless checks + */ + + /* check if it's a file */ + if (!S_ISREG(st.st_mode)) + goto err_new_tmpfile; + + /* check if it's seekable */ + if (lseek(fd_tmp, 0, SEEK_CUR) == (off_t)-1) + goto err_new_tmpfile; + + /* inode will be checked later on write */ + tmp_dev = st.st_dev; + tmp_ino = st.st_ino; + + /* tmpfile has >1 hardlinks */ + if (st.st_nlink > 1) + goto err_new_tmpfile; + + /* tmpfile unlinked while opened */ + if (st.st_nlink == 0) + goto err_new_tmpfile; + + flags = fcntl(fd_tmp, F_GETFL); + + if (flags == -1) + goto err_new_tmpfile; + + /* + * O_APPEND would permit offsets + * to be ignored, which breaks + * positional read/write + */ + if (flags & O_APPEND) + goto err_new_tmpfile; + + *fd = fd_tmp; + + return dest; + +err_new_tmpfile: + + if (dest != NULL) + free(dest); + + if (fd_tmp > -1) + close(fd_tmp); + + return NULL; +} + +/* + * portable mkstemp + */ +static int +x_i_mkstemp(char *template) +{ + int fd; + int i; + + for (i = 0; i < 10; i++) { + if (mktemp(template) == NULL) + return -1; + + fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) + return fd; + } + + errno = EEXIST; + return -1; +} + +static char * +x_c_strrchr(const char *s, int c) +{ + const char *p = NULL; + + while (*s) { + if (*s == (char)c) + p = s; + s++; + } + + if (c == '\0') + return (char *)s; + + return (char *)p; +} + +static int +x_i_rename(const char *src, const char *dst) +{ + int sfd, dfd; + ssize_t r; + char buf[8192]; + + sfd = open(src, O_RDONLY); + if (sfd < 0) + return -1; + + dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (dfd < 0) { + close(sfd); + return -1; + } + + while ((r = read(sfd, buf, sizeof(buf))) > 0) { + ssize_t w = write(dfd, buf, r); + if (w != r) { + close(sfd); + close(dfd); + return -1; + } + } + + if (r < 0) { + close(sfd); + close(dfd); + return -1; + } + + sync(); + + close(sfd); + close(dfd); + + if (unlink(src) < 0) + return -1; + + return 0; +} + +static char * +x_c_tmpdir(void) +{ + char *t; + + t = getenv("TMPDIR"); + if (t && *t) + return t; + + if (access("/tmp", W_OK) == 0) + return "/tmp"; + + if (access("/var/tmp", W_OK) == 0) + return "/var/tmp"; + + return "."; } |
