Commit Graph

3744 Commits

Author SHA1 Message Date
Leah Rowe b88c81869b 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-26 06:59:41 +00:00
Leah Rowe 5b4168c763 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-26 06:59:41 +00:00
Leah Rowe 8ccdde57e1 util/nvmutil: re-add arc4random
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 94632f3394 util/nvmutil: 5 retries, not 10
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 3c42608d6d util/nvmutil: check if gbe.bin is seekable
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 5f6038af5d util/nvmutil: check inode during post-verify
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 30e953dc8f util/nvmutil: warn about gbe.bin hard links
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b8246acf19 util/nvmutil: guard file replacement attacks
i already also guard other toctuo attacks :)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe e162d63ae6 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-26 06:59:41 +00:00
Leah Rowe 7eadaf2894 util/nvmutil: don't use /dev/urandom
too over engineered and cumbersome.

the new security in prw() makes it brittle,
and i'd rather not move checks outside of it.

the fallback rand is random enough.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 64389b696b util/nvmutil: fix file check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe c5c629d776 util/nvmutil: fix cast check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ec98e3143c util/nvmutil: more aggressive file checking
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b231e28c93 util/nvmutil: prevent overflow in rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 9ca9b54828 util/nvmutil: verify final offset in pwrite/pread
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b86f415a3e util/nvmutil: check regular file in rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe d34552b400 util/nvmutil: fix if (PWRITE)
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0612798ecf util/nvmutil: don't check o_append in prw
slow, per call. prw should be generic.

do it just for gbe files, once

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 026cbacbe2 util/nvmutil: configurable eintr/eagain
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 2eb045927b util/nvmutil: configurable retries/pread
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 72365f6447 util/nvmutil: support real pwrite/pread
build flag

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe c27dedfeea util/nvmutil: better commented I/O functions
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 33fa991141 util/nvmutil: make eintr/eagain setup clearer
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe caebecf6c6 nvmutil: fix rval in close_files
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0867507fc8 util/nvmutil: don't check write checksums on partial
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 3fba84ad95 util/nvmutil: don't recurse err/close_files
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 897c7490b5 util/nvmutil: restore pad before reading to it
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe bae8999b80 util/nvmutil: don't show checksum on bad pwrite
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 6c29a07ca0 util/nvmutil: always restore saved errno
the last lseek there is only there to reset
state, so its errors are irrelevant.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe a12f502af8 util/nvmutil: post-write verification report
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 2f80ac76ae util/nvmutil: optimise fsync / write check
write all at once, then sync all at once,
then verify all at once.

this increases the chancce that all data
gets written first, in the case of power
less, because fsync may take a while on
some systems.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 7c73218438 util/nvmutil: set EIO on bad memcmp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 3f17cd3ebc util/nvmutil: split up rw_gbe_file_part
the post-verification stage deserves a function

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe df18e65835 util/nvmutil: detect partial gbe rw
we already covered this in prw() which is
what ultimately gets called, but still.

it's logically correct not to check it here.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fe16b79537 util/nvmutil: verify gbe contents after writing
read it back and check. sync to disk first.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ce13599b99 util/nvmutil: consistent types
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 637d9a09c0 util/nvmutil: handle zero return in rw_file_exact
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0cceb58867 util/nvmutil: rename lseek_eintr
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 8a7d73c223 util/nvmutil: fix lseek eintr err check
it should be is equal, not not equal

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 100cb2014e util/nvmutil: rename err_eagain() to try_err()
makes more sense in code

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b345caa25c util/nvmutil: make EINTR configurable in prw()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 4a5a3bae45 util/nvmutil: tidy up prw()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fe959f9fb8 util/nvmutil: extra overflow check in prw
compliant posix systems should never meet this
check, but i put it here.

spec != implementation

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b60c42dcd0 util/nvmutil: extra overflow check in prw
do it at the very end

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 742b39be8c util/nvmutil: handle EAGAIN in prw()
the cat function can be greatly simplified

handle it conditionally, because not all
functions should use it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 7e8f375d9a util/nvmutil: split up rw_file_exact
move the gbe-specific parts out of it

what remains is a relatively generic
function; a very conservative implementation,
wrapping around libc functions but with
a few additional safety checks.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe e71f977468 util/nvmutil: add negative off check to prw
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe fb57cedded util/nvmutil: also do libc check on normal io
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 2a83a8ec43 util/nvmutil: properly reset lseek on error
don't return. set r instead. this will fall through
and return the same way, but with proper reset.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 935d082a51 util/nvmutil: move libc check to prw()
this still gets done from rw_once, but
it's generic enough that we want it in
our prw() wrapper function.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00