From d1ba9bae0358d87b8dc73d89b7df956722c26997 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Thu, 26 Mar 2026 11:41:37 +0000 Subject: further cleanup Signed-off-by: Leah Rowe --- util/libreboot-utils/include/common.h | 3 +- util/libreboot-utils/lib/rand.c | 60 ++++++----------------------------- util/libreboot-utils/lottery.c | 35 ++++++++++++-------- 3 files changed, 31 insertions(+), 67 deletions(-) (limited to 'util') diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index 71f28fad..7f063a8c 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -391,8 +391,7 @@ void *rmalloc(size_t *size); /* don't ever use this */ void rset(void *buf, size_t n); void *mkrbuf(size_t n); char *mkrstr(size_t n); -int win_lottery(void); -size_t rsize(void); +size_t rsize(size_t n); /* Helper functions for command: dump */ diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 58cb211e..3155eec3 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -72,62 +72,20 @@ * or your program dies. */ -#define MAX_ALLOC (2 << 16) - -int -win_lottery(void) /* are u lucky? */ +void * +rmalloc(size_t *rval) { - size_t size = rsize(); - size_t size2 = rsize(); - char *s = NULL; - - if (size && - size == size2 && - size <= MAX_ALLOC << 1) { - - if (!memcmp(s = mkrbuf(size << 1), - s + size, size)) - size2 = 1; /* winner! */ - else - size2 = 0; - } else { - return 0; - } - - free_if_null(&s); - return (int)size2; + return if_err(rval == NULL, EFAULT) ? + NULL : mkrstr(*rval = rsize(BUFSIZ)); } size_t -rsize(void) -{ - size_t rval = 0; - - /* clamp rand to prevent modulo bias */ - size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC); - - do { - rset(&rval, sizeof(rval)); - } while (rval >= limit); - - return rval % MAX_ALLOC; -} - - -void * -rmalloc(size_t *rval) +rsize(size_t n) { - /* clamp rand to prevent modulo bias */ - size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC); - - if (if_err(rval == NULL, EFAULT)) - return NULL; - - do { - rset(rval, sizeof(*rval)); - } while (*rval >= limit || *rval == 0); + size_t rval = SIZE_MAX; + for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval))); - return mkrstr(*rval %= MAX_ALLOC); + return rval % n; } char * @@ -157,7 +115,7 @@ mkrstr(size_t n) /* emulates spkmodem-decode */ void * mkrbuf(size_t n) { - void *buf; + void *buf = ""; if (n == 0) err_no_cleanup(0, EPERM, "mkrbuf: zero-byte request"); diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c index 4c3b0f70..dcede791 100644 --- a/util/libreboot-utils/lottery.c +++ b/util/libreboot-utils/lottery.c @@ -2,31 +2,38 @@ * Copyright (c) 2026 Leah Rowe */ -#ifdef __OpenBSD__ -#include /* pledge(2) */ -#endif - #include +#include + #include "include/common.h" +#define MAX_ALLOC (1 << 17) + +static int rigged(char **s); int main(int argc, char *argv[]) { - int lucky; -#if defined(__OpenBSD__) && defined(OpenBSD) -#if (OpenBSD) >= 509 + char *s = "You lose!"; + int lucky = rigged(&s); + +#ifdef __OpenBSD__ if (pledge("stdio", NULL) == -1) - err_no_cleanup(0, errno, "openbsd won it"); + err_no_cleanup(0, errno, "openbsd wins"); #endif -#endif - setvbuf(stdout, NULL, _IONBF, 0); + printf("%s\n", s); + return lucky; +} - lucky = win_lottery(); +static int +rigged(char **s) /* are u lucky? */ +{ + size_t size[2] = { rsize(MAX_ALLOC), rsize(MAX_ALLOC) }; - printf("%s\n", lucky ? "You won!" : "You lose! Sorry!"); - return lucky ? EXIT_SUCCESS : EXIT_FAILURE; + return !(size[0] && size[0] == size[1] && size[0] <= MAX_ALLOC << 1 && + s != NULL) || memcmp(*s = mkrbuf(size[0] << 1), *s + size[0], + size[0]); }/* ( >:3 ) - /| |\ + /| |\ it could be you! / \ */ -- cgit v1.2.1