util/nvmutil: guard file replacement attacks

i already also guard other toctuo attacks :)

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-14 18:21:44 +00:00
parent ebffecf04b
commit a80c189011

View File

@@ -652,6 +652,9 @@ static int rw_check_bad_part[] = {0, 0};
static int post_rw_checksum[] = {0, 0};
static dev_t gbe_dev;
static ino_t gbe_ino;
int
main(int argc, char *argv[])
{
@@ -930,6 +933,10 @@ open_gbe_file(void)
xopen(&gbe_fd, fname,
command[cmd_index].flags | O_BINARY | O_NOFOLLOW, &gbe_st);
/* inode will be checked later on write */
gbe_dev = gbe_st.st_dev;
gbe_ino = gbe_st.st_ino;
if (gbe_st.st_nlink == 0)
err(EIO, "%s: file unlinked while open", fname);
@@ -1411,6 +1418,9 @@ write_gbe_file(void)
if (fstat(gbe_fd, &gbe_st) == -1)
err(errno, "%s: re-check", fname);
if (gbe_st.st_dev != gbe_dev || gbe_st.st_ino != gbe_ino)
err(EIO, "%s: file replaced while open", fname);
if (gbe_st.st_size != gbe_file_size)
err(errno, "%s: file size changed before write", fname);