1140 Commits

Author SHA1 Message Date
Leah Rowe
3871c02cb5 nvmutil: fix unveil call
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 20:40:42 +00:00
Leah Rowe
3a0460607d util/nvmutil: better getrandom safety
err if buf NULL, len -1

also getrandom may return fewer bytes, so
loop that too.

why can't linux be like bsd? bsd is:

arc4random_buf(buf, len);

no checks needed. it never errs.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 20:01:57 +00:00
Leah Rowe
2c211d385e fix variable name
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 19:16:32 +00:00
Leah Rowe
e55f7ea081 util/nvmutil: re-add fallback timer rand
for 1989

enabled via ifdef. not enabled by default.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 19:11:08 +00:00
Leah Rowe
0229e84539 util/nvmutil: remove arandom fallback on rand
openbsd 2.1 has arc4random, which we detect here.

arandom was apparently added much later, so this
is dead code. remove it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 18:36:59 +00:00
Leah Rowe
f8b07dba29 util/nvmutil: rand: use getrandom on newer linux
we still fall back to the old /dev/urandom read
on older linux, via runtime detection (ENOSYS).

getrandom is better, because it guarantees entropy
via blocking, and works even when /dev/urandom
is unavailable.

it has the same practical benefit as arc4random,
which i use on bsd. linux can have arc4random,
but not every linux libc has it, so it's better
to use getrandom on linux.

older linux will fall back to /dev/urandom

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 18:34:28 +00:00
Leah Rowe
7c66a788bd util/nvmutil: buffered urandom reads
also generally tidied the code and made
it more robust e.g. retries

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 18:04:33 +00:00
Leah Rowe
55f006318a tidy some comments
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 16:02:15 +00:00
Leah Rowe
7ad924a91f nvmutil: make tmpdir string much more random
more random characters

i added support for higher than the standard 6
characters so i can go nuts

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 08:50:09 +00:00
Leah Rowe
50300f846f util/nvmutil: hardened mkstemp
200 retries, not 100.

and open with O_NOFOLLOW and O_CLOEXEC

check X on mkstemp

support more than 6 X in mkstemp

make PATH_LEN 4096

1024 is a bit low

make default mkstemp length 4096

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 08:42:58 +00:00
Leah Rowe
4b35d9ac29 nvmutil: rename lseek_loop to lseek_on_eintr
that's what it does!

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:48:53 +00:00
Leah Rowe
1fbe972fa7 nvmutil: rename x_i_close to close_on_eintr
that's what it does. waits for eintr to stop firing

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:45:34 +00:00
Leah Rowe
dcf698b9a0 nvmutil: don't have finite eintr wait
this is technically incorrect. we don't control
faults in the hardware.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:43:43 +00:00
Leah Rowe
2eee2b5cac nvmutil: rename x_i_fsync to fsync_on_eintr
that's what it does. waits on eintr.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:41:04 +00:00
Leah Rowe
6d0bb47b82 util/rename: rename x_i_fsync
rename to fsync_on_eintr, because that's what it does

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:38:54 +00:00
Leah Rowe
846cb23585 nvmutil: remove memcmp/memcpy/strrchr/rename
i had this idea in my head of later porting this
to k&r c for fun. but screw it.

compiling on everything since 1989 is enough

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:37:53 +00:00
Leah Rowe
f1fda8b43e util/nvmutil: tidy up includes
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 07:22:03 +00:00
Leah Rowe
2ed8db3adc util/nvmutil: major cleanup
handle init in xstatus()

it's now a singleton design

also tidied up some other code

also removed todo.c. bloat.
will do all those anyway.

too much change. i just kept
touching the code until it
looked good

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-19 04:25:43 +00:00
Leah Rowe
6ccd54635f now remove the .empty files
but git still has these directories
in history now, so people should have
it now when cloning.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 14:27:53 +00:00
Leah Rowe
61a32316ed util/nvmutil: add obj dir to git
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 14:26:39 +00:00
Leah Rowe
fe00bebc06 util/nvmutil: add rule to create lib objdir
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 14:25:14 +00:00
Leah Rowe
594cc262f4 nvmutil: move lib files to lib/
only keep nvmutil.c in main

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 14:20:06 +00:00
Leah Rowe
4dbb1c9bf3 util/nvmutil: put objects in obj/
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 14:00:30 +00:00
Leah Rowe
bd7be7bb7e nvmutil makefile: use portable assignments
question mark respects environmental variables

but isn't portable

you can just pass as argument on the command line

question mark is more useful for build systems,
but i'm not really bothered. the old way works.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 13:40:20 +00:00
Leah Rowe
27371af4bc nvmutil: split nvmutil.c into multiple files
this is a big program now. act like it.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 13:37:06 +00:00
Leah Rowe
722ed03179 util/nvmutil: remove global variable x
make a singleton function instead

now there are technically no global variables,
so i can more easily start splitting this up
into multiple linked programs

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 07:17:40 +00:00
Leah Rowe
5c51352cf7 nvmutil: disable arc4random on obsd below 2.1
arc4random added in openbsd 2,1

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 05:18:23 +00:00
Leah Rowe
71da2f53cf util/nvmutil: initialise st in tmpdir
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 05:13:02 +00:00
Leah Rowe
3b389d4aec util/nvmutil: use strlen for tmpdir length
sizeof includes the null

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 05:08:54 +00:00
Leah Rowe
ee5ff03765 nvmutil tmpdir: check world-writeable / sticky bits
must be world writeable and not have sticky bits

a bit theoretical, but we're also reading TMPDIR,
which could be anything

due to how this is called, it defaults back to /tmp
if null is returned, so itt's safe

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:53:10 +00:00
Leah Rowe
4810284f12 nvmutil: fix modulo bias in mkstemp
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:45:53 +00:00
Leah Rowe
8d467ecea9 util/nvmutil: limit EAGAIN/EINTR retries
set it really high though, so it's still
basically reliably

an EINTR/EAGAIN storm could cause problems
in prw()

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:44:36 +00:00
Leah Rowe
3f2a6e749f util/nvmutil: use real fsync
that function i added was a load of crap. it
worked, but it was a bit dumb, and crap.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:39:50 +00:00
Leah Rowe
ef2d5ccdf4 nvmutil: don't disable blocking on random
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:36:04 +00:00
Leah Rowe
e48a8046d3 re-add arc4random in nvmutil
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:33:14 +00:00
Leah Rowe
1d1f721d85 util/nvmutil: remove randomness fallback
not secure. i'll just re-add arc4random

and use urandom as the fallback

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:26:10 +00:00
Leah Rowe
1ecea3247d nvmutil: don't read urandom fd if fd not open
yeah. obvious bug

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:21:18 +00:00
Leah Rowe
7d5384ebb0 nvmutil: new urandom fd every time (rlong)
otherwise, a stale descriptor could be manipulated
easily by an attacker over time

very theoretical to be honest

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:18:58 +00:00
Leah Rowe
fb9e660367 util/nvmutil: fix typo in unveil call
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:14:47 +00:00
Leah Rowe
410408cece util/nvmutil: fix rlong static variables
whoops

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:13:50 +00:00
Leah Rowe
6b41b901ce nvmutil: remove redundant srand call
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:06:23 +00:00
Leah Rowe
6be315cf04 nvmutil: remove redundant check
the actual cat function just writes to stdout

we need only check that the input is null, which
i've now done.

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 04:04:44 +00:00
Leah Rowe
0fd3858953 util/nvmutil: obsessively check null cmd
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 03:53:05 +00:00
Leah Rowe
982c1146b3 util/nvmutil: tidy up variables
where feasible, don't assign them at declaration

this is especially important for the next change
i'm working on

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 03:17:38 +00:00
Leah Rowe
9c450d8528 stricter S_ISREG check
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 01:52:43 +00:00
Leah Rowe
6262826627 nvmutil: even stronger double-run protection
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 00:29:33 +00:00
Leah Rowe
2723a9ccd8 util/nvmutil: guard against running twice
Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 00:16:01 +00:00
Leah Rowe
6def381e3c nvmutil: make commands check themselves
check yourself before you execute yourself

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-18 00:09:55 +00:00
Leah Rowe
ec96cb46fc util/nvmutil: check file flags properly
masking O_ACCMODE tells you which flag it is

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-17 22:26:17 +00:00
Leah Rowe
6fe909f9f7 util/nvmutil: tighter pledge and unveil
call it sooner. set new_state afterward.

i had to uncouple nv from some functions
for this, and i also added some extra
checks especially at exit, about whether
to touch nv (whether it is initialised)

Signed-off-by: Leah Rowe <leah@libreboot.org>
2026-03-17 22:21:16 +00:00