From e0319f0116629f946fe286c5969f4a8a0dcfb7cb Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 25 Mar 2026 22:20:19 +0000 Subject: util/libreboot-utils: randomisation test to test the effectiveness of the rand function Signed-off-by: Leah Rowe --- util/libreboot-utils/.gitignore | 1 + util/libreboot-utils/Makefile | 26 +++++++++++++++--- util/libreboot-utils/include/common.h | 1 + util/libreboot-utils/lib/rand.c | 10 +++---- util/libreboot-utils/lottery.c | 51 +++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 util/libreboot-utils/lottery.c (limited to 'util/libreboot-utils') diff --git a/util/libreboot-utils/.gitignore b/util/libreboot-utils/.gitignore index fbf110f9..fbfbd130 100644 --- a/util/libreboot-utils/.gitignore +++ b/util/libreboot-utils/.gitignore @@ -1,6 +1,7 @@ /nvm /nvmutil /mkhtemp +/lottery *.bin *.o *.d diff --git a/util/libreboot-utils/Makefile b/util/libreboot-utils/Makefile index 692ebf0f..a48b8ef9 100644 --- a/util/libreboot-utils/Makefile +++ b/util/libreboot-utils/Makefile @@ -25,6 +25,7 @@ HELLFLAGS = $(STRICT) -Weverything PROG = nvmutil PROGMKH = mkhtemp +PROGLOT = lottery OBJS_NVMUTIL = \ obj/nvmutil.o \ @@ -48,11 +49,19 @@ OBJS_MKHTEMP = \ obj/lib/mkhtemp.o \ obj/lib/rand.o +OBJS_LOTTERY = \ + obj/lottery.o \ + obj/lib/file.o \ + obj/lib/string.o \ + obj/lib/num.o \ + obj/lib/mkhtemp.o \ + obj/lib/rand.o + # default mode CFLAGS_MODE = $(PORTABLE) CC_MODE = $(CC) -all: $(PROG) $(PROGMKH) +all: $(PROG) $(PROGMKH) $(PROGLOT) $(PROG): $(OBJS_NVMUTIL) $(CC_MODE) $(OBJS_NVMUTIL) -o $(PROG) $(LDFLAGS) @@ -60,9 +69,13 @@ $(PROG): $(OBJS_NVMUTIL) $(PROGMKH): $(OBJS_MKHTEMP) $(CC_MODE) $(OBJS_MKHTEMP) -o $(PROGMKH) $(LDFLAGS) +$(PROGLOT): $(OBJS_LOTTERY) + $(CC_MODE) $(OBJS_LOTTERY) -o $(PROGLOT) $(LDFLAGS) + # ensure obj directory exists $(OBJS_NVMUTIL): obj $(OBJS_MKHTEMP): obj +$(OBJS_LOTTERY): obj obj: mkdir obj || true @@ -76,6 +89,9 @@ obj/nvmutil.o: nvmutil.c obj/mkhtemp.o: mkhtemp.c $(CC_MODE) $(CFLAGS_MODE) -c mkhtemp.c -o obj/mkhtemp.o +obj/lottery.o: lottery.c + $(CC_MODE) $(CFLAGS_MODE) -c lottery.c -o obj/lottery.o + # library/helper objects obj/lib/state.o: lib/state.c @@ -113,19 +129,23 @@ obj/lib/rand.o: lib/rand.c # install -install: $(PROG) $(PROGMKH) +install: $(PROG) $(PROGMKH) $(PROGLOT) $(INSTALL) -d $(DESTDIR)$(PREFIX)/bin $(INSTALL) $(PROG) $(DESTDIR)$(PREFIX)/bin/$(PROG) chmod 755 $(DESTDIR)$(PREFIX)/bin/$(PROG) $(INSTALL) $(PROGMKH) $(DESTDIR)$(PREFIX)/bin/$(PROGMKH) chmod 755 $(DESTDIR)$(PREFIX)/bin/$(PROGMKH) + $(INSTALL) $(PROGLOT) $(DESTDIR)$(PREFIX)/bin/$(PROGLOT) + chmod 755 $(DESTDIR)$(PREFIX)/bin/$(PROGLOT) uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/$(PROG) rm -f $(DESTDIR)$(PREFIX)/bin/$(PROGMKH) + rm -f $(DESTDIR)$(PREFIX)/bin/$(PROGLOT) clean: - rm -f $(PROG) $(PROGMKH) $(OBJS_NVMUTIL) $(OBJS_MKHTEMP) + rm -f $(PROG) $(PROGMKH) $(OBJS_NVMUTIL) $(OBJS_MKHTEMP) \ + $(OBJS_LOTTERY) distclean: clean diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index 6d6d8d09..4668cffa 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -388,6 +388,7 @@ void *mkrbuf(size_t n); void rset(void *buf, size_t n); void *mkrbuf(size_t n); char *mkrstr(size_t n); +int win_lottery(void); /* Helper functions for command: dump */ diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 392ec2ba..29a756ed 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -100,17 +100,13 @@ win_lottery(void) win: free_if_null(&s1), free_if_null(&s2); - err_no_cleanup(0, ELOTTERY, - "Congratulations! you won the errno lottery"); - - exit(1); - return -1; + printf("Congratulations! you won the errno lottery!\n"); + return 1; err: /* the lottery won you */ free_if_null(&s1), free_if_null(&s2); err_no_cleanup(0, EFAULT, "lottery won you"); - exit(1); - return -1; + return 0; } char * diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c new file mode 100644 index 00000000..f673a109 --- /dev/null +++ b/util/libreboot-utils/lottery.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: MIT + * Copyright (c) 2026 Leah Rowe + */ + +#ifdef __OpenBSD__ +#include /* pledge(2) */ +#endif + +#include +#include "include/common.h" + +int +main(int argc, char *argv[]) +{ +#if defined(__OpenBSD__) && defined(OpenBSD) +#if (OpenBSD) >= 509 + if (pledge("stdio", NULL) == -1) + err_no_cleanup(0, errno, "openbsd won it"); +#endif +#endif + + if (win_lottery()) { + printf("You won!"); + return 0; + } + + return 1; +}/* + + + ( >:3 ) + /| |\ + / \ + + + + + + */ + + + + + + + + + + + + -- cgit v1.2.1