mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-19 18:43:08 +02:00
util/nvmutil: Harden pledge promises
After reading a file, remove rpath. When removing rpath, also remove wpath if flags are not to O_RDONLY (read-only disk operation). When wpath is permitted, and a file was successfully written, remove wpath. In order to permit /dev/urandom access in rhex(), I call it as a void just before re-calling pledge. The rhex() function has been written in such a way that /dev/urandom only needs to be read *once*. Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
+11
-7
@@ -124,6 +124,11 @@ main(int argc, char *argv[])
|
|||||||
skipread[part ^ 1] = (cmd == &cmd_copy) |
|
skipread[part ^ 1] = (cmd == &cmd_copy) |
|
||||||
(cmd == &cmd_setchecksum) | (cmd == &cmd_brick);
|
(cmd == &cmd_setchecksum) | (cmd == &cmd_brick);
|
||||||
readGbeFile(&fd, FILENAME, flags, nr);
|
readGbeFile(&fd, FILENAME, flags, nr);
|
||||||
|
(void)rhex();
|
||||||
|
if (flags == O_RDONLY)
|
||||||
|
xpledge("stdio", NULL);
|
||||||
|
else
|
||||||
|
xpledge("stdio wpath", NULL);
|
||||||
if (strMac != NULL)
|
if (strMac != NULL)
|
||||||
cmd_setmac(strMac); /* nvm gbe.bin setmac */
|
cmd_setmac(strMac); /* nvm gbe.bin setmac */
|
||||||
else if (cmd != NULL)
|
else if (cmd != NULL)
|
||||||
@@ -219,18 +224,16 @@ hextonum(char ch)
|
|||||||
uint8_t
|
uint8_t
|
||||||
rhex(void)
|
rhex(void)
|
||||||
{
|
{
|
||||||
static int rfd = -1;
|
static int rfd = -1, n = 0;
|
||||||
static uint64_t rnum = 0;
|
static uint8_t rnum[16];
|
||||||
if (rnum == 0) {
|
if (!n) {
|
||||||
if (rfd == -1)
|
if (rfd == -1)
|
||||||
if ((rfd = open("/dev/urandom", O_RDONLY)) == -1)
|
if ((rfd = open("/dev/urandom", O_RDONLY)) == -1)
|
||||||
err(errno, "/dev/urandom");
|
err(errno, "/dev/urandom");
|
||||||
if (read(rfd, (uint8_t *) &rnum, 8) == -1)
|
if (read(rfd, (uint8_t *) &rnum, (n = 15) + 1) == -1)
|
||||||
err(errno, "/dev/urandom");
|
err(errno, "/dev/urandom");
|
||||||
}
|
}
|
||||||
uint8_t rval = (uint8_t) (rnum & 0xf);
|
return rnum[n--] & 0xf;
|
||||||
rnum >>= 4;
|
|
||||||
return rval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -366,6 +369,7 @@ next_part:
|
|||||||
}
|
}
|
||||||
if (close((*fd)))
|
if (close((*fd)))
|
||||||
err(errno, "%s", filename);
|
err(errno, "%s", filename);
|
||||||
|
xpledge("stdio", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
Reference in New Issue
Block a user