summaryrefslogtreecommitdiff
path: root/util/libreboot-utils
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils')
-rw-r--r--util/libreboot-utils/include/common.h8
-rw-r--r--util/libreboot-utils/lib/rand.c42
-rw-r--r--util/libreboot-utils/lib/string.c35
-rw-r--r--util/libreboot-utils/lottery.c61
4 files changed, 65 insertions, 81 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index 0c8fbd3d..1bef0d50 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -22,6 +22,10 @@
#include <sys/syscall.h>
#endif
+#ifdef __OpenBSD__ /* for pledge */
+#include <unistd.h>
+#endif
+
#define items(x) (sizeof((x)) / sizeof((x)[0]))
/* system prototypes
@@ -369,6 +373,8 @@ void write_mac_part(size_t partnum);
/* string functions
*/
+int xunveilx(const char *path, const char *permissions);
+int xpledgex(const char *promises, const char *execpromises);
int slen(const char *scmp, size_t maxlen,
size_t *rval);
int scmp(const char *a, const char *b,
@@ -391,7 +397,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(char **buf);
+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 3a0a94bf..3155eec3 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -72,37 +72,20 @@
* or your program dies.
*/
-int
-win_lottery(char **buf) /* are u lucky? */
+void *
+rmalloc(size_t *rval)
{
- size_t size = 0;
- int rval;
-
- char *s1 = rmalloc(&size);
- char *s2 = rmalloc(&size);
-
- if (scmp(s1, s2, BUFSIZ + 2, &rval) >= 0 &&
- rval == 0)
- rval = 1; /* winner! */
- else
- rval = 0;
-
- (void) scat(s1, s2, BUFSIZ << 1, buf);
-
- free_if_null(&s1);
- free_if_null(&s2);
-
- return rval;
+ return if_err(rval == NULL, EFAULT) ?
+ NULL : mkrstr(*rval = rsize(BUFSIZ));
}
-void *
-rmalloc(size_t *rval)
+size_t
+rsize(size_t n)
{
- if (if_err(rval == NULL, EFAULT))
- return NULL;
+ size_t rval = SIZE_MAX;
+ for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval)));
- rset(rval, sizeof(*rval));
- return mkrstr(*rval %= BUFSIZ);
+ return rval % n;
}
char *
@@ -114,7 +97,7 @@ mkrstr(size_t n) /* emulates spkmodem-decode */
if (n == 0)
err_no_cleanup(0, EPERM, "mkrbuf: zero-byte request");
- if (n == SIZE_MAX)
+ if (n >= SIZE_MAX - 1)
err_no_cleanup(0, EOVERFLOW, "mkrbuf: overflow");
if (if_err((s = mkrbuf(n + 1)) == NULL, EFAULT))
@@ -132,11 +115,14 @@ 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");
+ if (n >= SIZE_MAX - 1)
+ err_no_cleanup(0, EOVERFLOW, "integer overflow in mkrbuf");
+
if ((buf = malloc(n)) == NULL)
err_no_cleanup(0, ENOMEM, "mkrbuf: malloc");
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c
index b639f0a4..c6e09752 100644
--- a/util/libreboot-utils/lib/string.c
+++ b/util/libreboot-utils/lib/string.c
@@ -321,15 +321,28 @@ lbgetprogname(char *argv0)
return progname;
}
+int
+xpledgex(const char *promises, const char *execpromises)
+{
+ int saved_errno = errno;
+ (void) promises, (void) execpromises, (void) saved_errno;
+#ifdef __OpenBSD__
+ if (pledge(promises, execpromises) == -1)
+ err_no_cleanup(0, errno, "pledge");
+#endif
+ errno = saved_errno;
+ return 0;
+}
-
-
-
-
-
-
-
-
-
-
-
+int
+xunveilx(const char *path, const char *permissions)
+{
+ int saved_errno = errno;
+ (void) path, (void) permissions, (void) saved_errno;
+#ifdef __OpenBSD__
+ if (pledge(promises, execpromises) == -1)
+ err_no_cleanup(0, errno, "pledge");
+#endif
+ errno = saved_errno;
+ return 0;
+}
diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c
index 8157d7a9..9b37d117 100644
--- a/util/libreboot-utils/lottery.c
+++ b/util/libreboot-utils/lottery.c
@@ -1,41 +1,20 @@
-/* SPDX-License-Identifier: MIT
- * Copyright (c) 2026 Leah Rowe <leah@libreboot.org>
- */
-
-#ifdef __OpenBSD__
-#include <sys/param.h> /* pledge(2) */
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "include/common.h"
-
-int
-main(int argc, char *argv[])
-{
- char *s1 = NULL;
- int rval = 0;
-
-#if defined(__OpenBSD__) && defined(OpenBSD)
-#if (OpenBSD) >= 509
- if (pledge("stdio", NULL) == -1)
- err_no_cleanup(0, errno, "openbsd won it");
-#endif
-#endif
- setvbuf(stdout, NULL, _IONBF, 0);
-
- if (win_lottery(&s1))
- rval = 1;
-
- if (s1 != NULL) {
- printf("%s\n\n", s1);
- free(s1);
- }
-
- printf("%s\n", rval ? "You won!" : "You lose! Sorry!");
- return rval? EXIT_SUCCESS : EXIT_FAILURE;
-}/*
-
- ( >:3 )
- /| |\
- / \ */
+/* SPDX-License-Identifier: MIT ( >:3 )
+ * Copyright (c) 2026 Leah Rowe <leah@libreboot.org> /| |\*/
+#include <stdio.h> /* / \ */
+#include <stdint.h>
+#include <string.h> /* (^.>) - are u lucky? */
+#include "include/common.h" /* \| /= */
+static int rigged(char **s, int spew) { /* l \ */
+ size_t size[2] = { rsize(SIZE_MAX >> 1), rsize(SIZE_MAX >> 1) };
+ spew &= size[1] = !(size[0] && size[0]==size[1] && size[0]<=SIZE_MAX &&
+ s != NULL) || memcmp(*s = mkrbuf(size[0] << 1), *s + size[0],
+ size[0]);
+ if (spew && !size[1]) printf("%s\n\n", *s);
+ return size[1];
+} /* (^:3) - it could be you! */
+int main(int argc, char **argv) { /* /| |l */
+ xpledgex("stdio", NULL); /* / \ */
+ printf("%s\n", (argc = rigged(argv, argc > 1))?"You lose!":"You win!");
+ return argc; /* (^.^)/ - come again soon! */
+} /* /| |
+ / \ */