Commit Graph

1003 Commits

Author SHA1 Message Date
Leah Rowe
5d95d4dfe9 util/nvmutil: stricter return in pread
we were returning if verified is not off, but we
were not doing the check soon enough.

now it's clearer: just after either a reset,
or we found out offset doesn't match, we
return sooner.

otherwise, we read, and we verify again right
after. in the old code, we verified twice in
a row.

this is just more optimal, for error handling.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 02:01:20 +00:00
Leah Rowe
b365781a4c no, break instead
in the last patch, i return, which then avoids
resetting the offset.

prw is very careful not to return early.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:52:06 +00:00
Leah Rowe
c442225fa3 util/nvmutil pread: rw_over_nrw exit, not set
otherwise, if it's -1 and errno happens to be
EINTR or EAGAIN, we might loop on what is a
real error. this bug fixes that edge case.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:45:23 +00:00
Leah Rowe
2a0a903a5e nvmutil: stronger race-condition check on prw
do it per read, in the fallback pread/pwrite

per read/write that is

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:43:35 +00:00
Leah Rowe
53434e24a0 util/nvmutil: fix theoretical buffer overflow
i already guard offsets in io_args, but it's best
to be thorough here.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:14:29 +00:00
Leah Rowe
f509c87d04 util/nvmutil: prevent underflow in comparison
we already check before that rv is not negative,
and it starts at zero, but it's good to guard
it here just in case (for future re-factoring).

if rv is negative, it could convert (casted to
size_t) to a huge number (we don't want that).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:06:36 +00:00
Leah Rowe
7bd1e551d8 comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 01:01:10 +00:00
Leah Rowe
be2f4ce6cd util/nvmutil: extra race-condition check in prw
even with OFF_RESET, we still want some error checking.
if the check fails again immediately after, then it
suggests that another program really is modifying the
file, so we should stop.

the first check is done on the theory that another
program *was* working on it, but now isn't.

once again, this isn't perfect. use read pread/pwrite
if you need thread safety (and even then, you still
need to actually check your code)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:53:16 +00:00
Leah Rowe
d5c363d206 util/nvmutil: safer xstrxcmp() - overflow fix
if a points to a buffer shorter than maxlen,
and the string is not null-terminated early,
the loop may read may overflow

e.g.

char buf[3] = {'a', 'b', 'c'};
xstrxcmp(buf, "abc", 50);

this is undefined behaviour, and a bug. C allows
reading past arrays only if the memory exists,
but we can't guarantee that

to fix it, we check the condition for return,
namely NULL character, before using the character
again. This avoids reading further from a multiple
times so we exit as soon as we encounter NULL

this also avoids multiple reads from memory, though
a compiler would optimise that anyway

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:30:12 +00:00
Leah Rowe
36851df55f phrasing
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:26:51 +00:00
Leah Rowe
f0905cd7ec util/nvmutil: stricter rw_gbe_file_exact
we repeat these stteps later, but it's still good
to be exact here. these lower functions can
change.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:23:23 +00:00
Leah Rowe
2dd13e2859 cast
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:20:49 +00:00
Leah Rowe
6fc8bca77b comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:17:12 +00:00
Leah Rowe
d64d7878e4 util/nvmutil: stricter lseep_loop return offset
we currently reset just fine, but a partial success
where the previous offset is not the same as the
original should also be considered failure.

this patch therefore makes the return much stricter,
making the code return an error if this occurs,
which in nvmutil would then cause a program exit.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:13:45 +00:00
Leah Rowe
2bb67c9cd5 util/nvmutil: only use srand on fallback rand
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:08:14 +00:00
Leah Rowe
0d5d8204e0 nvmutil: add missing check to io_args
accidentally removed this in a rebase

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 00:02:56 +00:00
Leah Rowe
21bf1e644e util/nvmutil: add flock to pledge promises
otherwise, gbe.bin locking won't work!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:59:56 +00:00
Leah Rowe
cedcb2c68a util/nvmutil: tidy up io_args
i don't like it grouped together. do it
all separate, for clarity.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:48:27 +00:00
Leah Rowe
8875a712ae util/nvmutil: tidy up err()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:43:08 +00:00
Leah Rowe
23e7170897 util/nvmutil: re-add io_args()
unified arg check for prw and rw_file_exact

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:35:24 +00:00
Leah Rowe
67a7f16ba2 util/nvmutil: rw_file_exact: check inputs also
we check them in prw, but we used to rely
on prw because we called that first. no more.

it's correct to also check them here anyway,
in case i ever call another function here.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:29:11 +00:00
Leah Rowe
4db11cd4ed util/nvmutil: rw_over_nrw: err if nrw is zero
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:26:54 +00:00
Leah Rowe
9d7e990df4 util/nvmutil: rw: safer bound check
avoid pointer-range overflow arithmetic. this
patch doesn't change behaviour, but makes an
overflow impossible.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:23:01 +00:00
Leah Rowe
e97f64cb33 util/nvmutil rw: make off_reset a toggle
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:17:53 +00:00
Leah Rowe
0e7ac43f28 util/nvmutil: rw_file_exact: explicit casts
don't do it inside functions. some compilers may
be inconsistent, ditto several auditing tools.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 23:15:14 +00:00
Leah Rowe
7c79db20e3 util/nvmutil: fix potential overflow in rw
off is signed, so converting that to unsigned
is better than converting rc (unsigned)
to signed. i had the right idea, but got
it wrong in the earlier version. this
should fix potential overflow issues.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:50:06 +00:00
Leah Rowe
ef4e9dc895 util/nvmutil: rw file: guard rc before addition
otherwise, it could still overflow

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:47:16 +00:00
Leah Rowe
93603e1572 nvmutil: toggle for fd thread-safety err state
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:40:10 +00:00
Leah Rowe
0a4257f4ed util/nvmutil: clean up pwrite/pread case
some unused variables if enabled. hide them.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:25:34 +00:00
Leah Rowe
f229c722a9 util/nvmutil: partially mitigate fd offset race
our fallback pwrite/pread behaviour still does not
properly replicate the safety of real pwrite/pread

i intend to put this i/o code into a library for use
in other programs; nvmutil is single-threaded so
this change is largely redundant (but can't hurt)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:15:33 +00:00
Leah Rowe
d66ba5f781 util/nvmutil: tidy up rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 22:01:06 +00:00
Leah Rowe
1b0d348172 comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:57:06 +00:00
Leah Rowe
7b2761ee11 util/nvmutil: cleanup
remove ptr casts to ulong. size_t is better.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:45:57 +00:00
Leah Rowe
c6aba2acdb nope, use size_t
despite my cast, size_t is better for mem index

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:36:42 +00:00
Leah Rowe
46cd8b80c8 util/nvmutil: use ulong on ptr casts
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:33:22 +00:00
Leah Rowe
d6f3aadff1 util/nvmutil: remove rw_file_once
we don't need it anymore.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:30:20 +00:00
Leah Rowe
74d26d446e util/nvmutil: move looping logic to rw_file_exact
rw_file_once was doing what rw_file_exact should be
doing

_once does what it says: once

we were passing an offset (rc) to it that it was not
meaningfully using.

this makes the code now more robust, especially if
we later swap out or break _once - then we don't
get weird behaviour (if there is a regression).

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:25:24 +00:00
Leah Rowe
6bc7efe675 nvmutil: move increment logic to rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 21:06:20 +00:00
Leah Rowe
d61f9f7f88 nvmutil: fix redundant check
we already check not-zero in the next if

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 19:18:23 +00:00
Leah Rowe
6e21b2cd44 util/nvmutil: cast gbe file size on ptr cmp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 19:15:59 +00:00
Leah Rowe
58a80f8c3f util/nvmutil: even safer pointer comparison
we assert now that ulong is the size of a pointer,
therefore we know that it can fit a pointer reliably.

this code is written for c90 spec so lacks uintptr

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 19:13:38 +00:00
Leah Rowe
feb4db34a2 util/nvmutil: safer pointer comparison
technically we're never supposed to do arithmetic on
pointers (there's uintptr for that)

very anal fix

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 19:09:34 +00:00
Leah Rowe
48d17cae0f util/nvmutil: enable arc4random on linux
not available on older systems. can just pass
the relevant flag in the compiler:

HAVE_ARC4RANDOM_BUF=0 at build time if you need
the fallback.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:57:40 +00:00
Leah Rowe
b26a292d3e util/nvmutil: re-add arc4random
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:55:14 +00:00
Leah Rowe
430682d7e9 util/nvmutil: 5 retries, not 10
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:31:16 +00:00
Leah Rowe
3e0ec8cd5e util/nvmutil: check if gbe.bin is seekable
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:27:34 +00:00
Leah Rowe
e015e1103a util/nvmutil: check inode during post-verify
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:24:53 +00:00
Leah Rowe
cf5708957b util/nvmutil: warn about gbe.bin hard links
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:23:52 +00:00
Leah Rowe
a80c189011 util/nvmutil: guard file replacement attacks
i already also guard other toctuo attacks :)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:21:44 +00:00
Leah Rowe
ebffecf04b util/nvmutil: guard against unlinking file
if someone deletes gbe.bin while operating, nvmutil
will now abort

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-14 18:19:46 +00:00