3931 Commits

Author SHA1 Message Date
Leah Rowe
f05776f5ff 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-16 16:57:53 +00:00
Leah Rowe
4437e41b38 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-16 16:54:17 +00:00
Leah Rowe
849a73012e 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-16 16:52:28 +00:00
Leah Rowe
fb4f263005 Revert "util/nvmutil: don't use fsync()"
This reverts commit bdb43afac6.
2026-03-16 16:39:59 +00:00
Leah Rowe
9ef92b3b25 util/nvmutil: longer string in mkstemp
have A-Z too, for more randomness

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:38:47 +00:00
Leah Rowe
7674550083 util/nvmutil: don't use mktemp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:36:07 +00:00
Leah Rowe
1cc7c91b2c 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-16 16:32:23 +00:00
Leah Rowe
9b82c8fe11 util/nvmutil: portable memcpy/memcmp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:27:56 +00:00
Leah Rowe
806993621c 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-16 16:21:07 +00:00
Leah Rowe
a261bab075 util/nvmutil: more secure tmpdir()
use stat instead of access (race conditions)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:19:27 +00:00
Leah Rowe
bc2cf24993 util/nvmutil: fix O_NONBLOCK fallback
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:16:58 +00:00
Leah Rowe
f62ac24d8f 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-16 16:13:50 +00:00
Leah Rowe
e5e107d8cd util/nvmutil: more reliable TMPDIR handling
more portable

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:10:25 +00:00
Leah Rowe
bdb43afac6 util/nvmutil: don't use fsync()
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:05:45 +00:00
Leah Rowe
d345712172 util/nvmutil: more portable functtions
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 16:00:49 +00:00
Leah Rowe
7d5ada2272 util/nvmutil: add portable malloc extern
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:50:52 +00:00
Leah Rowe
6744867e4e 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-16 15:46:19 +00:00
Leah Rowe
7a66c78016 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-16 15:43:06 +00:00
Leah Rowe
d2d2fb778a util/nvmutil: typeset size_t explicitly
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:40:05 +00:00
Leah Rowe
440d6ce5ba util/nvmutil: run-time CHAR_BIT test
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:36:10 +00:00
Leah Rowe
f3956a2482 util/nvmutil: lower default PATH_LEN
older unix needed lower

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:34:28 +00:00
Leah Rowe
37387a8e2d util/nvmutil: portable struct timeval
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:33:02 +00:00
Leah Rowe
36344f84b5 util/nvmutil: portable S_ISREG
very old libc doesn't have it

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:23:20 +00:00
Leah Rowe
06ff9d27e8 mktemp prototype
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:22:28 +00:00
Leah Rowe
139f19f867 util/nvmutil: portable gettimeofday
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:22:23 +00:00
Leah Rowe
4ecce163d6 util/nvmutil: use portable mkstemp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:15:53 +00:00
Leah Rowe
68ef78e439 util/nvmutil: better urandom portability
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-16 15:10:49 +00:00
Leah Rowe
e2d8734150 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-16 15:00:08 +00:00
Leah Rowe
a74f184437 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-16 14:47:55 +00:00
Leah Rowe
975aefeb80 add -I. to nvmutil makefile
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 23:28:44 +00:00
Leah Rowe
68b6851075 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-15 23:28:02 +00:00
Leah Rowe
a3cd10be4a also remove -Wextra
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 21:32:22 +00:00
Leah Rowe
8636ca41f4 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-15 21:30:16 +00:00
Leah Rowe
1912c82f97 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-15 21:09:33 +00:00
Leah Rowe
1f205662a9 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-15 20:53:06 +00:00
Leah Rowe
0855088209 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-15 20:23:56 +00:00
Leah Rowe
99b50d74c6 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-15 20:20:19 +00:00
Leah Rowe
eb139b32ef 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-15 20:01:36 +00:00
Leah Rowe
df741bcd38 nvmutil: use O_CLOEXEC on gbe files
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 02:22:54 +00:00
Leah Rowe
f03ca35497 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-15 02:14:23 +00:00
Leah Rowe
fdf2e68163 set errno
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-15 02:07:12 +00:00
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