summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/mkhtemp.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils/lib/mkhtemp.c')
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c53
1 files changed, 12 insertions, 41 deletions
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index 2fb2f01a..191d657c 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -886,53 +886,30 @@ err:
int
mkhtemp_fill_random(char *p, size_t xc)
{
- size_t chx = 0;
- int rand_failures = 0;
-
- size_t r;
-
- int saved_rand_error = 0;
static char ch[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ size_t chx = 0;
+ size_t r;
+
/* clamp rand to prevent modulo bias
- * (reduced risk of entropy leak)
*/
size_t limit = ((size_t)-1) - (((size_t)-1) % (sizeof(ch) - 1));
-
int saved_errno = errno;
- if (p == NULL) {
- errno = EFAULT;
- goto err_mkhtemp_fill_random;
- }
+ if (if_err(p == NULL, EFAULT))
+ return -1;
for (chx = 0; chx < xc; chx++) {
- do {
- saved_rand_error = errno;
- rand_failures = 0;
retry_rand:
- errno = 0;
-
- /* on bsd: uses arc4random
- on linux: uses getrandom
- on OLD linux: /dev/urandom
- on old/other unix: /dev/urandom
- */
- r = rlong();
-
- if (errno > 0) {
- if (++rand_failures <= 8)
- goto retry_rand;
-
- goto err_mkhtemp_fill_random;
- }
-
- rand_failures = 0;
- errno = saved_rand_error;
-
- } while (r >= limit);
+ /* on bsd: uses arc4random
+ on linux: uses getrandom
+ *never returns error*
+ */
+ r = rlong(); /* always returns successful */
+ if (r >= limit)
+ goto retry_rand;
p[chx] = ch[r % (sizeof(ch) - 1)];
}
@@ -940,12 +917,6 @@ retry_rand:
errno = saved_errno;
return 0;
-err_mkhtemp_fill_random:
-
- if (errno == saved_errno)
- errno = ECANCELED;
-
- return -1;
}
/* WARNING: **ONCE** per file.