libreboot-utils: much stricter open() handling

abort on error, and do EINTR looping

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-28 08:09:14 +00:00
parent 03dd3c2894
commit 49cac232d8
3 changed files with 36 additions and 5 deletions
+1
View File
@@ -545,6 +545,7 @@ int fs_rename_at(int olddirfd, const char *old,
int newdirfd, const char *new); int newdirfd, const char *new);
int fs_open(const char *path, int flags); int fs_open(const char *path, int flags);
void free_and_set_null(char **buf); void free_and_set_null(char **buf);
void open_on_eintr(char *path, int *fd, int flags, mode_t mode);
struct filesystem *rootfs(void); struct filesystem *rootfs(void);
int fs_resolve_at(int dirfd, const char *path, int flags); int fs_resolve_at(int dirfd, const char *path, int flags);
int fs_next_component(const char **p, int fs_next_component(const char **p,
+34 -2
View File
@@ -589,6 +589,36 @@ free_and_set_null(char **buf)
*buf = NULL; *buf = NULL;
} }
void
open_on_eintr(char *path,
int *fd, int flags, mode_t mode)
{
int r = -1;
int saved_errno = errno;
if (path == NULL)
err_exit(EINVAL, "open_on_eintr: null path");
if (fd == NULL)
err_exit(EFAULT, "%s: open_on_eintr: null fd ptr", path);
if (*fd >= 0)
err_exit(EBADF, "%s: open_on_eintr: file already open", path);
do {
r = open(path, flags, mode);
} while (r == -1 && (
errno == EINTR || errno == EAGAIN ||
errno == EWOULDBLOCK || errno == ETXTBSY));
if (r < 0)
err_exit(errno, "%s: open_on_eintr: could not close", path);
*fd = r;
errno = saved_errno;
}
void void
close_on_eintr(int *fd) close_on_eintr(int *fd)
{ {
@@ -672,8 +702,10 @@ rootfs(void)
if (!fs_initialised) { if (!fs_initialised) {
global_fs.rootfd = global_fs.rootfd = -1;
open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
open_on_eintr("/", &global_fs.rootfd,
O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0400);
if (global_fs.rootfd < 0) if (global_fs.rootfd < 0)
return NULL; return NULL;
+1 -3
View File
@@ -142,9 +142,7 @@ rset(void *buf, size_t n)
#if defined(USE_URANDOM) && \ #if defined(USE_URANDOM) && \
((USE_URANDOM) > 0) ((USE_URANDOM) > 0)
int fd = -1; int fd = -1;
open_on_eintr("/dev/urandom", &fd, O_RDONLY, 0400);
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
goto err;
retry_rand: retry_rand:
if ((rc = read(fd, (unsigned char *)buf + off, n - off)) < 0) { if ((rc = read(fd, (unsigned char *)buf + off, n - off)) < 0) {
#elif defined(__linux__) #elif defined(__linux__)