Commit Graph

3828 Commits

Author SHA1 Message Date
Leah Rowe 96dde65d16 util/nvmutil: proper /dev/fd search in fchmod
some systems may not even have it

works with /dev/fd (bsd/mac etc)

works with linux (/proc/self/fd)

and falls back on super old systems
that have neither

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 120241f1cf util/nvmutil: tidy up makefile options
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe c46a634288 nope. put -Werror in the makefile
not compiling without it is a bug

don't let the default exclude it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 71ce22a938 nvmutil makefile: define WERROR but blank it
settting it to -Werror is wrong, should set
it not -Werror.

however, put the WERROR variable in the make
command. that way, i could test with

make WERROR=-Werror

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe af40114be7 util/nvmutil: portable fchmod
and with that, now the code compiles on gcc
with -std=c90 -pedantic

with -Werror and -Wall -Wextra

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe a0fd201b31 Revert "util/nvmutil: don't use fsync()"
This reverts commit bdb43afac6edef21a15f99b8c3beac01be8b86f7.
2026-03-26 06:59:41 +00:00
Leah Rowe e6b53e8aed util/nvmutil: longer string in mkstemp
have A-Z too, for more randomness

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ad5a4771bf util/nvmutil: don't use mktemp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe f365969d34 util/nvmutil: don't declare libc prototypes
i no longer use -Werror

these can actually conflict on some weird
systems, so better just remove them

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 5927c175df util/nvmutil: portable memcpy/memcmp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 612783bc24 util/nvmutil: more portable close()
close may set errno to EINTR, which could
cause weird edge case behaviour in our
prw() functtion

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 26d8807b79 util/nvmutil: more secure tmpdir()
use stat instead of access (race conditions)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 1ce06b01e0 util/nvmutil: fix O_NONBLOCK fallback
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 15cbafe20b util/nvmutil: more secure mkstemp
try a few more times until success

explicitly return EEXIST when needed

we try multiple times and check more
thoroughly if a file exists, thus
reducing the risk of race conditions

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 24c4e715e6 util/nvmutil: more reliable TMPDIR handling
more portable

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0e0fcd5a81 util/nvmutil: don't use fsync()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 4233f76fee util/nvmutil: more portable functtions
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 64835e67fd util/nvmutil: add portable malloc extern
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0043446f60 util/nvmutil: don't use size_t/ssize_t
not portable. some old systems don't have it,
or handle it very poorly

unsigned long is a reasonable way to refer
to indexes inside pointters

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe a8fe2c79db util/nvmutil: don't use SSIZE_MAX (not portable)
some old systems don't have ssize_t or size_t

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 6c37d2aa4b util/nvmutil: typeset size_t explicitly
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ae43dc391f util/nvmutil: run-time CHAR_BIT test
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ab806d2546 util/nvmutil: lower default PATH_LEN
older unix needed lower

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 9bdd91131d util/nvmutil: portable struct timeval
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ea71010f85 util/nvmutil: portable S_ISREG
very old libc doesn't have it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 6a898dff72 mktemp prototype
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe ff458943c3 util/nvmutil: portable gettimeofday
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe e9d90e8aca util/nvmutil: use portable mkstemp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 2a6a63fef3 util/nvmutil: better urandom portability
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 609076d98b util/nvmutil: use local tmpfile on openbsd
if the global file is created on a different file
system than the gbe file, unveil would trigger an
abort trap, since we rely on created a second
temporary file, whose path we can't know ahead
of time.

i could get rid of unveil, or unveil a directory,
but neither is acceptable. just use localtmp on
openbsd. a temporary file is created next to
the gbe file, in the same directory.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 381b7e3abb util/nvmutil: /dev/random fallback
now the custom fallback code is very unlikely
to ever actually be used, on any system,
except really old systems.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe d7dea02fba add -I. to nvmutil makefile
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 5b8ea71bbd re-add strict flags
i need these. can always turn them off
when running make if you need to

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe e795e42f66 also remove -Wextra
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 7f14693e6c util/nvmutil: more portable Makefile
-Werror removed, for older compilers

actual warnings still there

-std is configurable now

e.g.

make CSTD=-c90
make CSTD=-c99

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 824fade5e9 util/nvmuttil: don't use arc4random
i have urandom again. it's enough

the fallback rand implementation
is used if needed

now i don't have to worry about any
weird version of unix from 1992 and
deal with weird hacks. in fact, with
this change, my code will probably
compile on irix now

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 04b215dd6d util/nvmutil: re-enable urandom reads
i had to loosen the pledges for the new i/o
framework, which needs more permissions

as a result, i can now open urandom in
this function statically, rather than
in nvmutil's control logic

and because of that, it's less buggy now

arc4random is disabled on linux by default,
because it's not universally available
on all libc, and only since about 2022
in some glibc versions

better for portability to let linux users
justt use urandom

the new logic is different. now it falls
back to rand per-byte, but in practise
it almost never will.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe b4b194f94c util/nvmutil: remove arc4random on linux
linux only had it since 2022.

lots of people will complain if i leave this enabled.

not all libc have it either

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 5d8b19fb75 util/nvmutil: fix cat
i simplified it in the last commits, but i sttill
need this loop to properly handle parts

otherwise yeah, all it's doing is copying a file
verbatim. duh.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe af7ab6f786 util/nvmutil: double-verify r/w using tmp files
we now read twice, verify the two, to make sure
one read isn't faulty

we operate on a tmp file, then rename back. this
reduces the risk of power cuts corrupting data

we properly verify the contents that we wrote
back

inspired largely by flashprog. i wanted to have
an insanely over-engineered and extremely safe
tool that edits intel gbe nvm files

and now i have one. the only one in existence.

i'm basically writing my own libc code at this
point, to be honest. i'll probably start puttting
these functions in libraries

e.g. that tmpfile generator

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 1556ca7173 nvmutil: use O_CLOEXEC on gbe files
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 0d6bb8d747 util/nvmutil: fix verified first, in prw loop
yes, because otherwise if the offset is still
wrong, we allow junk to be written. bad!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 31835eb8e3 set errno
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00
Leah Rowe 4b667f0d19 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-26 06:59:41 +00:00
Leah Rowe 572f00e7b7 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-26 06:59:41 +00:00
Leah Rowe d890a6b138 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-26 06:59:41 +00:00
Leah Rowe 7f192b4344 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-26 06:59:41 +00:00
Leah Rowe a050e5a563 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-26 06:59:41 +00:00
Leah Rowe 8d81e8f68b 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-26 06:59:41 +00:00
Leah Rowe d148985e61 comment
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-26 06:59:41 +00:00