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>
This commit is contained in:
Leah Rowe
2026-03-16 16:12:02 +00:00
parent 24c4e715e6
commit 15cbafe20b
+11 -4
View File
@@ -3009,12 +3009,19 @@ static int
x_i_mkstemp(char *template) x_i_mkstemp(char *template)
{ {
int fd; int fd;
int i;
if (mktemp(template) == NULL) for (i = 0; i < 10; i++) {
return -1; if (mktemp(template) == NULL)
return -1;
fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600); fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
return fd; if (fd >= 0)
return fd;
}
errno = EEXIST;
return -1;
} }
static char * static char *