summaryrefslogtreecommitdiff
path: root/util/nvmutil/nvmutil.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/nvmutil/nvmutil.c')
-rw-r--r--util/nvmutil/nvmutil.c485
1 files changed, 297 insertions, 188 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c
index 2aa47d19..01a5e36c 100644
--- a/util/nvmutil/nvmutil.c
+++ b/util/nvmutil/nvmutil.c
@@ -22,7 +22,7 @@
* Make most of nvmutil a *library* for re-use
*
* TODO: gettimeofday not posible - use portable functions.
- * TODO: uint32_t fallback: modify the program instead
+ * TODO: ux fallback: modify the program instead
* to run on 16-bit systems: smaller buffers, and do
* operations byte-based instead of word-based.
*
@@ -135,11 +135,12 @@ CFLAGS += -fstack-protector-strong
CFLAGS += -fno-common
CFLAGS += -D_FORTIFY_SOURCE=2
CFLAGS += -fPIE
-*/
-#ifndef _XOPEN_SOURCE
-#define _XOPEN_SOURCE 500
-#endif
+also consider:
+-fstack-clash-protection
+-Wl,-z,relro
+-Wl,-z,now
+*/
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
@@ -156,32 +157,23 @@ CFLAGS += -fPIE
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
-#if defined(__has_include)
-#if __has_include(<stdint.h>)
-#include <stdint.h>
-#else
-typedef unsigned char uint8_t;
-typedef unsigned short uint16_t;
-typedef unsigned int uint32_t;
-#endif
-#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
-#include <stdint.h>
-#else
-typedef unsigned char uint8_t;
-typedef unsigned short uint16_t;
-typedef unsigned int uint32_t;
-#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
+typedef unsigned char u8;
+typedef unsigned short ushort;
+typedef unsigned int uint;
+
/* type asserts */
typedef char static_assert_char_is_8_bits[(CHAR_BIT == 8) ? 1 : -1];
-typedef char static_assert_uint8_is_1[(sizeof(uint8_t) == 1) ? 1 : -1];
-typedef char static_assert_uint16_is_2[(sizeof(uint16_t) == 2) ? 1 : -1];
-typedef char static_assert_uint32_is_4[(sizeof(uint32_t) == 4) ? 1 : -1];
+typedef char static_assert_char_is_1[(sizeof(char) == 1) ? 1 : -1];
+typedef char static_assert_uint8_is_1[(sizeof(u8) == 1) ? 1 : -1];
+typedef char static_assert_uint16_is_2[(sizeof(ushort) >= 2) ? 1 : -1];
+typedef char static_assert_short_is_2[(sizeof(short) >= 2) ? 1 : -1];
+typedef char static_assert_uint32_is_4[(sizeof(uint) >= 4) ? 1 : -1];
typedef char static_assert_int_ge_32[(sizeof(int) >= 4) ? 1 : -1];
typedef char static_assert_twos_complement[
((-1 & 3) == 3) ? 1 : -1
@@ -226,8 +218,8 @@ 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
+#ifndef O_NOFOLLOW
+#define O_NOFOLLOW 0
#endif
/*
@@ -253,6 +245,7 @@ static int xstrxcmp(const char *a, const char *b, size_t maxlen);
*/
static void open_dev_urandom(void);
static void open_gbe_file(void);
+static void lock_gbe_file(void);
static void xopen(int *fd, const char *path, int flags, struct stat *st);
/*
@@ -271,7 +264,7 @@ static int good_checksum(size_t partnum);
*/
static void run_cmd(size_t c);
static void check_command_num(size_t c);
-static uint8_t valid_command(size_t c);
+static u8 valid_command(size_t c);
/*
* Helper functions for command: setmac
@@ -282,9 +275,9 @@ 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 uint16_t hextonum(char ch_s);
-static uint16_t rhex(void);
-static uint16_t fallback_rand(void);
+static ushort hextonum(char ch_s);
+static ushort rhex(void);
+static ushort fallback_rand(void);
static unsigned long entropy_jitter(void);
static void write_mac_part(size_t partnum);
@@ -300,7 +293,7 @@ static void hexdump(size_t partnum);
* cat, cat16 and cat128
*/
static void cmd_helper_cat(void);
-static void gbe_cat_buf(uint8_t *b);
+static void gbe_cat_buf(u8 *b);
/*
* After command processing, write
@@ -312,14 +305,14 @@ static void gbe_cat_buf(uint8_t *b);
static void write_gbe_file(void);
static void override_part_modified(void);
static void set_checksum(size_t part);
-static uint16_t calculated_checksum(size_t p);
+static ushort calculated_checksum(size_t p);
/*
* Helper functions for accessing
* the NVM area during operation.
*/
-static uint16_t nvm_word(size_t pos16, size_t part);
-static void set_nvm_word(size_t pos16, size_t part, uint16_t val16);
+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);
@@ -330,21 +323,23 @@ static void check_bin(size_t a, const char *a_name);
*/
static void rw_gbe_file_part(size_t p, int rw_type,
const char *rw_type_str);
-static uint8_t *gbe_mem_offset(size_t part, const char *f_op);
+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,
const char *d_type, off_t nsize, off_t ncmp);
-static ssize_t rw_file_exact(int fd, uint8_t *mem, size_t len,
+static ssize_t rw_gbe_file_exact(int fd, u8 *mem, size_t nrw,
off_t off, int rw_type);
-static ssize_t rw_file_once(int fd, uint8_t *mem, size_t len,
- off_t off, int rw_type, size_t rc);
-static ssize_t do_rw(int fd,
- uint8_t *mem, size_t len, off_t off, int rw_type);
+static ssize_t rw_file_exact(int fd, u8 *mem, size_t len,
+ off_t off, int rw_type, int loop_eagain, int loop_eintr);
+static ssize_t rw_file_once(int fd, u8 *mem, size_t len,
+ off_t off, int rw_type, size_t rc, int loop_eagain,
+ int loop_eintr);
static ssize_t prw(int fd, void *mem, size_t nrw,
- off_t off, int rw_type);
-static off_t lseek_eintr(int fd, off_t off, int whence);
-static int io_args(int fd, void *mem, size_t nrw,
- off_t off, int rw_type);
+ off_t off, int rw_type, int loop_eagain, int loop_eintr);
+static int rw_over_nrw(ssize_t r, size_t nrw);
+static off_t lseek_loop(int fd, off_t off,
+ int whence, int loop_eagain, int loop_eintr);
+static int try_err(int loop_err, int errval);
/*
* Error handling and cleanup
@@ -352,7 +347,7 @@ static int io_args(int fd, void *mem, size_t nrw,
static void err(int nvm_errval, const char *msg, ...);
static void close_files(void);
static const char *getnvmprogname(void);
-static void usage(uint8_t usage_exit);
+static void usage(u8 usage_exit);
/*
* Sizes in bytes:
@@ -383,6 +378,7 @@ static void usage(uint8_t usage_exit);
#define NVM_CHECKSUM_WORD (NVM_WORDS - 1)
#define NUM_RANDOM_BYTES 12
+static u8 rnum[NUM_RANDOM_BYTES];
/*
* Portable macro based on BSD nitems.
@@ -402,17 +398,17 @@ static const char *rname = NULL;
*
* The code will handle this properly.
*/
-static uint8_t buf[GBE_FILE_SIZE];
-static uint8_t pad[GBE_PART_SIZE]; /* the file that wouldn't die */
+static u8 buf[GBE_FILE_SIZE];
+static u8 pad[GBE_PART_SIZE]; /* the file that wouldn't die */
-static uint16_t mac_buf[3];
+static ushort mac_buf[3];
static off_t gbe_file_size;
static int urandom_fd = -1;
static int gbe_fd = -1;
static size_t part;
-static uint8_t part_modified[2];
-static uint8_t part_valid[2];
+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;
@@ -489,11 +485,11 @@ struct commands {
const char *str;
void (*run)(void);
int argc;
- uint8_t invert;
- uint8_t set_modified;
- uint8_t arg_part;
- uint8_t chksum_read;
- uint8_t chksum_write;
+ u8 invert;
+ u8 set_modified;
+ u8 arg_part;
+ u8 chksum_read;
+ u8 chksum_write;
size_t rw_size; /* within the 4KB GbE part */
int flags; /* e.g. O_RDWR or O_RDONLY */
};
@@ -652,6 +648,7 @@ main(int argc, char *argv[])
open_dev_urandom();
open_gbe_file();
+ lock_gbe_file();
#ifdef NVMUTIL_PLEDGE
if (pledge("stdio", NULL) == -1)
@@ -694,7 +691,7 @@ sanitize_command_list(void)
static void
sanitize_command_index(size_t c)
{
- uint8_t mod_type;
+ u8 mod_type;
size_t gbe_rw_size;
check_command_num(c);
@@ -775,7 +772,7 @@ set_cmd(int argc, char *argv[])
static void
set_cmd_args(int argc, char *argv[])
{
- uint8_t arg_part;
+ u8 arg_part;
if (!valid_command(cmd_index) || argc < 3)
usage(1);
@@ -865,7 +862,8 @@ open_gbe_file(void)
{
struct stat gbe_st;
- xopen(&gbe_fd, fname, command[cmd_index].flags | O_BINARY, &gbe_st);
+ xopen(&gbe_fd, fname,
+ command[cmd_index].flags | O_BINARY | O_NOFOLLOW, &gbe_st);
gbe_file_size = gbe_st.st_size;
@@ -880,6 +878,24 @@ open_gbe_file(void)
}
static void
+lock_gbe_file(void)
+{
+ struct flock fl;
+
+ memset(&fl, 0, sizeof(fl));
+
+ if (command[cmd_index].flags == O_RDONLY)
+ fl.l_type = F_RDLCK;
+ else
+ fl.l_type = F_WRLCK;
+
+ fl.l_whence = SEEK_SET;
+
+ if (fcntl(gbe_fd, F_SETLK, &fl) == -1)
+ err(errno, "file is locked by another process");
+}
+
+static void
xopen(int *fd_ptr, const char *path, int flags, struct stat *st)
{
if ((*fd_ptr = open(path, flags)) == -1)
@@ -896,7 +912,7 @@ static void
read_gbe_file(void)
{
size_t p;
- uint8_t do_read[2] = {1, 1};
+ u8 do_read[2] = {1, 1};
/*
* Commands specifying a partnum only
@@ -916,10 +932,10 @@ read_checksums(void)
{
size_t p;
size_t skip_part;
- uint8_t invert;
- uint8_t arg_part;
- uint8_t num_invalid;
- uint8_t max_invalid;
+ u8 invert;
+ u8 arg_part;
+ u8 num_invalid;
+ u8 max_invalid;
part_valid[0] = 0;
part_valid[1] = 0;
@@ -965,8 +981,8 @@ read_checksums(void)
static int
good_checksum(size_t partnum)
{
- uint16_t expected_checksum = calculated_checksum(partnum);
- uint16_t current_checksum = nvm_word(NVM_CHECKSUM_WORD, partnum);
+ ushort expected_checksum = calculated_checksum(partnum);
+ ushort current_checksum = nvm_word(NVM_CHECKSUM_WORD, partnum);
if (current_checksum == expected_checksum)
return 1;
@@ -990,7 +1006,7 @@ check_command_num(size_t c)
(unsigned long)c);
}
-static uint8_t
+static u8
valid_command(size_t c)
{
if (c >= N_COMMANDS)
@@ -1084,7 +1100,7 @@ set_mac_nib(size_t mac_str_pos,
size_t mac_byte_pos, size_t mac_nib_pos)
{
char mac_ch;
- uint16_t hex_num;
+ ushort hex_num;
mac_ch = mac_str[mac_str_pos + mac_nib_pos];
@@ -1109,7 +1125,7 @@ set_mac_nib(size_t mac_str_pos,
| ((mac_nib_pos ^ 1) << 2)); /* left or right nib? */
}
-static uint16_t
+static ushort
hextonum(char ch_s)
{
unsigned char ch = (unsigned char)ch_s;
@@ -1128,25 +1144,24 @@ hextonum(char ch_s)
return 16; /* invalid character */
}
-static uint16_t
+static ushort
rhex(void)
{
static size_t n = 0;
- static uint8_t rnum[NUM_RANDOM_BYTES];
if (use_prng)
return fallback_rand();
if (!n) {
n = sizeof(rnum);
- if (rw_file_exact(urandom_fd, rnum, n, 0, IO_READ) == -1)
+ if (rw_file_exact(urandom_fd, rnum, n, 0, IO_READ, 0, 1) == -1)
err(errno, "Randomisation failed");
}
- return (uint16_t)(rnum[--n] & 0xf);
+ return (ushort)(rnum[--n] & 0xf);
}
-static uint16_t
+static ushort
fallback_rand(void)
{
struct timeval tv;
@@ -1170,7 +1185,7 @@ fallback_rand(void)
mix ^= (unsigned long)&tv;
mix ^= (unsigned long)&counter;
- return (uint16_t)(mix & 0xf);
+ return (ushort)(mix & 0xf);
}
static unsigned long
@@ -1245,7 +1260,7 @@ static void
print_mac_from_nvm(size_t partnum)
{
size_t c;
- uint16_t val16;
+ ushort val16;
for (c = 0; c < 3; c++) {
val16 = nvm_word(c, partnum);
@@ -1264,7 +1279,7 @@ hexdump(size_t partnum)
{
size_t c;
size_t row;
- uint16_t val16;
+ ushort val16;
for (row = 0; row < 8; row++) {
printf("%08lx ", (unsigned long)((size_t)row << 4));
@@ -1305,36 +1320,21 @@ cmd_helper_cat(void)
}
static void
-gbe_cat_buf(uint8_t *b)
+gbe_cat_buf(u8 *b)
{
- ssize_t rval;
-
- while (1) {
- rval = rw_file_exact(STDOUT_FILENO, b,
- GBE_PART_SIZE, 0, IO_WRITE);
-
- if (rval >= 0) {
- /*
- * A partial write is especially
- * fatal, as it should already be
- * prevented in rw_file_exact().
- */
- if ((size_t)rval != GBE_PART_SIZE)
- err(EIO, "stdout: cat: Partial write");
- break;
- }
-
- if (errno != EAGAIN)
- err(errno, "stdout: cat");
- }
+ if (rw_file_exact(STDOUT_FILENO, b,
+ GBE_PART_SIZE, 0, IO_WRITE, 1, 1) < 0)
+ err(errno, "stdout: cat");
}
static void
write_gbe_file(void)
{
+ struct stat gbe_st;
+
size_t p;
size_t partnum;
- uint8_t update_checksum;
+ u8 update_checksum;
if (command[cmd_index].flags == O_RDONLY)
return;
@@ -1342,6 +1342,15 @@ write_gbe_file(void)
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_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;
@@ -1359,7 +1368,7 @@ write_gbe_file(void)
static void
override_part_modified(void)
{
- uint8_t mod_type = command[cmd_index].set_modified;
+ u8 mod_type = command[cmd_index].set_modified;
switch (mod_type) {
case SET_MOD_0:
@@ -1390,16 +1399,16 @@ set_checksum(size_t p)
set_nvm_word(NVM_CHECKSUM_WORD, p, calculated_checksum(p));
}
-static uint16_t
+static ushort
calculated_checksum(size_t p)
{
size_t c;
- uint32_t val16 = 0;
+ uint val16 = 0;
for (c = 0; c < NVM_CHECKSUM_WORD; c++)
- val16 += (uint32_t)nvm_word(c, p);
+ val16 += (uint)nvm_word(c, p);
- return (uint16_t)((NVM_CHECKSUM - val16) & 0xffff);
+ return (ushort)((NVM_CHECKSUM - val16) & 0xffff);
}
/*
@@ -1410,7 +1419,7 @@ calculated_checksum(size_t p)
* file, but we assume otherwise and adapt accordingly.
*/
-static uint16_t
+static ushort
nvm_word(size_t pos16, size_t p)
{
size_t pos;
@@ -1418,20 +1427,20 @@ nvm_word(size_t pos16, size_t p)
check_nvm_bound(pos16, p);
pos = (pos16 << 1) + (p * GBE_PART_SIZE);
- return (uint16_t)buf[pos] |
- ((uint16_t)buf[pos + 1] << 8);
+ return (ushort)buf[pos] |
+ ((ushort)buf[pos + 1] << 8);
}
static void
-set_nvm_word(size_t pos16, size_t p, uint16_t val16)
+set_nvm_word(size_t pos16, size_t p, ushort val16)
{
size_t pos;
check_nvm_bound(pos16, p);
pos = (pos16 << 1) + (p * GBE_PART_SIZE);
- buf[pos] = (uint8_t)(val16 & 0xff);
- buf[pos + 1] = (uint8_t)(val16 >> 8);
+ buf[pos] = (u8)(val16 & 0xff);
+ buf[pos + 1] = (u8)(val16 >> 8);
set_part_modified(p);
}
@@ -1472,9 +1481,9 @@ rw_gbe_file_part(size_t p, int rw_type,
const char *rw_type_str)
{
size_t gbe_rw_size = command[cmd_index].rw_size;
- uint8_t invert = command[cmd_index].invert;
+ u8 invert = command[cmd_index].invert;
- uint8_t *mem_offset;
+ u8 *mem_offset;
if (rw_type < IO_PREAD || rw_type > IO_PWRITE)
err(errno, "%s: %s: part %lu: invalid rw_type, %d",
@@ -1489,7 +1498,7 @@ rw_gbe_file_part(size_t p, int rw_type,
*/
mem_offset = gbe_mem_offset(p ^ invert, rw_type_str);
- if (rw_file_exact(gbe_fd, mem_offset,
+ if (rw_gbe_file_exact(gbe_fd, mem_offset,
gbe_rw_size, gbe_file_offset(p, rw_type_str),
rw_type) == -1)
err(errno, "%s: %s: part %lu",
@@ -1501,13 +1510,13 @@ rw_gbe_file_part(size_t p, int rw_type,
* but used to check Gbe bounds in memory,
* and it is *also* used during file I/O.
*/
-static uint8_t *
+static u8 *
gbe_mem_offset(size_t p, const char *f_op)
{
off_t gbe_off = gbe_x_offset(p, f_op, "mem",
GBE_PART_SIZE, GBE_FILE_SIZE);
- return (uint8_t *)(buf + gbe_off);
+ return (u8 *)(buf + gbe_off);
}
/*
@@ -1547,6 +1556,34 @@ 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,
+ off_t off, int rw_type)
+{
+ if (mem == NULL)
+ goto err_rw_gbe_file_exact;
+
+ if (mem != (void *)pad
+ && mem != (void *)rnum
+ && (mem < buf || mem >= (buf + GBE_FILE_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))
+ goto err_rw_gbe_file_exact;
+
+ if (nrw > GBE_PART_SIZE)
+ goto err_rw_gbe_file_exact;
+
+ return rw_file_exact(fd, mem, nrw, off, rw_type, 0, 1);
+
+err_rw_gbe_file_exact:
+ errno = EIO;
+ return -1;
+}
+
/*
* Read or write the exact contents of a file,
* along with a buffer, (if applicable) offset,
@@ -1565,20 +1602,26 @@ gbe_x_offset(size_t p, const char *f_op, const char *d_type,
* means that EOF is also considered fatal.
*/
static ssize_t
-rw_file_exact(int fd, uint8_t *mem, size_t nrw,
- off_t off, int rw_type)
+rw_file_exact(int fd, u8 *mem, size_t nrw,
+ off_t off, int rw_type, int loop_eagain,
+ int loop_eintr)
{
ssize_t rv;
size_t rc;
- if (io_args(fd, mem, nrw, off, rw_type) == -1) {
- errno = EIO;
- return -1;
- }
-
for (rc = 0, rv = 0; rc < nrw; ) {
- if ((rv = rw_file_once(fd, mem, nrw, off, rw_type, rc)) <= 0)
+ if ((rv = rw_file_once(fd, mem, nrw, off, rw_type, rc,
+ loop_eagain, loop_eintr)) < 0)
return -1;
+
+ /* rw_file_once never returns
+ zero, but it's still logically
+ incorrect not to handle it here */
+
+ if (rv == 0) {
+ errno = EIO;
+ return -1;
+ }
rc += (size_t)rv;
}
@@ -1587,31 +1630,38 @@ rw_file_exact(int fd, uint8_t *mem, size_t nrw,
}
/*
- * May not return all requested bytes (len).
+ * Helper function for rw_file_exact, that
+ * also does extra error handling pertaining
+ * to GbE file offsets.
+ *
+ * May not return all requested bytes (nrw).
* Use rw_file_exact for guaranteed length.
+ *
+ * This function will never return zero.
+ * It will only return below (error),
+ * or above (success). On error, -1 is
+ * returned and errno is set accordingly.
*/
static ssize_t
-rw_file_once(int fd, uint8_t *mem, size_t nrw,
- off_t off, int rw_type, size_t rc)
+rw_file_once(int fd, u8 *mem, size_t nrw,
+ off_t off, int rw_type, size_t rc,
+ int loop_eagain, int loop_eintr)
{
ssize_t rv;
size_t retries_on_zero = 0;
size_t max_retries = 10;
- if (io_args(fd, mem, nrw, off, rw_type) == -1)
+ if (mem == NULL)
goto err_rw_file_once;
read_again:
- rv = do_rw(fd, mem + rc, nrw - rc, off + rc, rw_type);
-
- if (rv < 0 && errno == EINTR)
- goto read_again;
+ rv = prw(fd, mem + rc, nrw - rc, off + rc, rw_type,
+ loop_eagain, loop_eintr);
if (rv < 0)
return -1;
- if ((size_t)rv > SSIZE_MAX /* theoretical buggy libc */
- || (size_t)rv > (nrw - rc))/* don't overflow */
+ if ((size_t)rv > (nrw - rc))/* don't overflow */
goto err_rw_file_once;
if (rv != 0)
@@ -1625,28 +1675,9 @@ err_rw_file_once:
return -1;
}
-static ssize_t
-do_rw(int fd, uint8_t *mem,
- size_t nrw, off_t off, int rw_type)
-{
- if (io_args(fd, mem, nrw, off, rw_type) == -1)
- goto err_do_rw;
-
- if (rw_type == IO_READ)
- return read(fd, mem, nrw);
-
- if (rw_type == IO_WRITE)
- return write(fd, mem, nrw);
-
- if (rw_type == IO_PREAD || rw_type == IO_PWRITE)
- return prw(fd, mem, nrw, off, rw_type);
-
-err_do_rw:
- errno = EIO;
- return -1;
-}
-
/*
+ * prw() - portable read-write
+ *
* This implements a portable analog of pwrite()
* and pread() - note that this version is not
* thread-safe (race conditions are possible on
@@ -1654,25 +1685,64 @@ err_do_rw:
*
* This limitation is acceptable, since nvmutil is
* single-threaded. Portability is the main goal.
+ *
+ * A fallback is provided for regular read/write.
+ * rw_type can be IO_READ, IO_WRITE, IO_PREAD
+ * or IO_PWRITE
+ *
+ * loop_eagain does a retry loop on EAGAIN if set
+ * loop_eintr does a retry loop on EINTR if set
+ *
+ * Unlike the bare syscalls, prw() does security
+ * checks e.g. checks NULL strings, checks bounds,
+ * also mitigates a few theoretical libc bugs.
+ * It is designed for extremely safe single-threaded
+ * I/O on applications that need it.
*/
+
static ssize_t
prw(int fd, void *mem, size_t nrw,
- off_t off, int rw_type)
+ off_t off, int rw_type,
+ int loop_eagain, int loop_eintr)
{
off_t off_orig;
ssize_t r;
int saved_errno;
- int prw_type;
int flags;
+ int positional_rw;
- if (io_args(fd, mem, nrw, off, rw_type) == -1)
+ if (mem == NULL)
goto err_prw;
- prw_type = rw_type ^ IO_PREAD;
-
- if ((unsigned int)prw_type > IO_WRITE)
+ if (fd < 0
+ || off < 0
+ || !nrw /* prevent zero read request */
+ || nrw > (size_t)SSIZE_MAX /* prevent overflow */
+ || (unsigned int)rw_type > IO_PWRITE)
goto err_prw;
+ r = -1;
+
+ if (rw_type >= IO_PREAD)
+ positional_rw = 1; /* pread/pwrite */
+ else
+ positional_rw = 0; /* read/write */
+
+try_rw_again:
+
+ if (!positional_rw) {
+ if (rw_type == IO_WRITE)
+ r = write(fd, mem, nrw);
+ else if (rw_type == IO_READ)
+ r = read(fd, mem, nrw);
+
+ if (r == -1 && (errno == try_err(loop_eintr, EINTR)
+ || errno == try_err(loop_eagain, EAGAIN)))
+ goto try_rw_again;
+
+ return rw_over_nrw(r, nrw);
+ }
+
flags = fcntl(fd, F_GETFL);
if (flags == -1)
return -1;
@@ -1686,70 +1756,109 @@ prw(int fd, void *mem, size_t nrw,
if (flags & O_APPEND)
goto err_prw;
- if ((off_orig = lseek_eintr(fd, (off_t)0, SEEK_CUR)) == (off_t)-1)
- return -1;
-
- if (lseek_eintr(fd, off, SEEK_SET) == (off_t)-1)
- return -1;
+ if ((off_orig = lseek_loop(fd, (off_t)0, SEEK_CUR,
+ loop_eagain, loop_eintr)) == (off_t)-1)
+ r = -1;
+ else if (lseek_loop(fd, off, SEEK_SET,
+ loop_eagain, loop_eintr) == (off_t)-1)
+ r = -1;
do {
- r = do_rw(fd, mem, nrw, off, prw_type);
- } while (r < 0 && errno == EINTR);
+ if (rw_type == IO_PREAD)
+ r = read(fd, mem, nrw);
+ else if (rw_type == IO_PWRITE)
+ r = write(fd, mem, nrw);
+
+ r = rw_over_nrw(r, nrw);
+ } while (r == -1 &&
+ (errno == try_err(loop_eintr, EINTR)
+ || errno == try_err(loop_eagain, EAGAIN)));
saved_errno = errno;
- if (lseek_eintr(fd, off_orig, SEEK_SET) == (off_t)-1) {
+ if (lseek_loop(fd, off_orig, SEEK_SET,
+ loop_eagain, loop_eintr) == (off_t)-1) {
if (r < 0)
errno = saved_errno;
-
return -1;
}
errno = saved_errno;
- return r;
+ return rw_over_nrw(r, nrw);
err_prw:
errno = EIO;
return -1;
}
+/*
+ * POSIX can say whatever it wants.
+ * specification != implementation
+ */
static int
-io_args(int fd, void *mem, size_t nrw,
- off_t off, int rw_type)
+rw_over_nrw(ssize_t r, size_t nrw)
{
- if (off > 0
- && off != GBE_PART_SIZE)
- goto err_io_args;
+ if (r == -1)
+ return r;
- if (nrw != GBE_PART_SIZE &&
- nrw != NVM_SIZE &&
- nrw != NUM_RANDOM_BYTES)
- goto err_io_args;
+ if ((size_t)r > SSIZE_MAX) {
+ /*
+ * Theoretical buggy libc
+ * check. Extremely academic.
+ *
+ * Specifications never
+ * allow this return value
+ * to exceed SSIZE_MAX, but
+ * spec != implementation
+ *
+ * Check this after using
+ * [p]read() or [p]write()
+ */
+ goto err_rw_over_nrw;
+ }
- if (fd < 0
- || !nrw /* prevent zero read request */
- || nrw > (size_t)SSIZE_MAX /* prevent overflow */
- || (unsigned int)rw_type > IO_PWRITE)
- goto err_io_args;
+ /*
+ * Theoretical buggy libc:
+ * Should never return a number of
+ * bytes above the requested length.
+ */
+ if ((size_t)r > nrw)
+ goto err_rw_over_nrw;
- return 0;
+ return r;
+
+err_rw_over_nrw:
-err_io_args:
errno = EIO;
return -1;
}
static off_t
-lseek_eintr(int fd, off_t off, int whence)
+lseek_loop(int fd, off_t off, int whence,
+ int loop_eagain, int loop_eintr)
{
- off_t old;
+ off_t old = -1;
do {
old = lseek(fd, off, whence);
- } while (old == (off_t)-1 && errno == EINTR);
+ } while (old == (off_t)-1 && (
+ errno == try_err(loop_eintr, EINTR) ||
+ errno == try_err(loop_eagain, EAGAIN)));
return old;
}
+static int
+try_err(int loop_err, int errval)
+{
+ if (loop_err)
+ return errval;
+
+ /* errno is never negative,
+ so functions checking it
+ can use it accordingly */
+ return -1;
+}
+
static void
err(int nvm_errval, const char *msg, ...)
{
@@ -1807,7 +1916,7 @@ getnvmprogname(void)
}
static void
-usage(uint8_t usage_exit)
+usage(u8 usage_exit)
{
const char *util = getnvmprogname();