summaryrefslogtreecommitdiff
path: root/userland
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-11-23 17:01:55 +0100
committerAnton Kling <anton@kling.gg>2023-11-23 17:01:55 +0100
commit6757803629574fe46e9047be8567e0c7e0522973 (patch)
treecf31247efa27997b0b43bfed559ffbb42bfb8f3e /userland
parentb8da260dd01175529feb57d5a73570f469decb08 (diff)
Kernel/LibC: Add randomfill() syscall.
This syscall aims to fill the given buffer with cryptographically secure random data. If the syscall returns and does not cause a page fault it will **always** have filled the buffer with random data and never gives back any error value.
Diffstat (limited to 'userland')
-rw-r--r--userland/libc/Makefile2
-rw-r--r--userland/libc/include/sys/random.h3
-rw-r--r--userland/libc/include/syscall.h1
-rw-r--r--userland/libc/sys/random/randomfill.c6
-rw-r--r--userland/test/test.c21
5 files changed, 32 insertions, 1 deletions
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index 146492b..a47d7bf 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -2,7 +2,7 @@ CC="i686-sb-gcc"
AR="i686-sb-ar"
AS="i686-sb-as"
CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type
-OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o math/min.o math/max.o systemcall.o
+OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o math/min.o math/max.o systemcall.o sys/random/randomfill.o
all: libc.a
%.o: %.c
diff --git a/userland/libc/include/sys/random.h b/userland/libc/include/sys/random.h
new file mode 100644
index 0000000..0be3dd8
--- /dev/null
+++ b/userland/libc/include/sys/random.h
@@ -0,0 +1,3 @@
+#include <stdint.h>
+
+void randomfill(void *buffer, uint32_t size);
diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h
index c09eafb..7a3b50d 100644
--- a/userland/libc/include/syscall.h
+++ b/userland/libc/include/syscall.h
@@ -39,6 +39,7 @@
#define SYS_CHDIR 31
#define SYS_GETCWD 32
#define SYS_ISATTY 33
+#define SYS_RANDOMFILL 34
int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx,
uint32_t esi, uint32_t edi);
diff --git a/userland/libc/sys/random/randomfill.c b/userland/libc/sys/random/randomfill.c
new file mode 100644
index 0000000..18ec04d
--- /dev/null
+++ b/userland/libc/sys/random/randomfill.c
@@ -0,0 +1,6 @@
+#include <sys/random.h>
+#include <syscall.h>
+
+void randomfill(void *buffer, uint32_t size) {
+ syscall(SYS_RANDOMFILL, buffer, size, 0, 0, 0);
+}
diff --git a/userland/test/test.c b/userland/test/test.c
index a8c20dc..940854c 100644
--- a/userland/test/test.c
+++ b/userland/test/test.c
@@ -735,6 +735,26 @@ void realpath_test(void) {
dbgln("realpath TEST PASSED");
}
+void randomfill_test(void) {
+ dbgln("randomfill TEST");
+ {
+ char buffer[4096];
+ memset(buffer, 0, 4096);
+ randomfill(buffer, 4096);
+ int i;
+ for (i = 0; i < 4096; i++) {
+ if (0 != buffer[i])
+ break;
+ }
+ assert(4096 != i);
+ char buffer2[4096];
+ memcpy(buffer2, buffer, 4096);
+ randomfill(buffer, 4096);
+ assert(0 != memcmp(buffer, buffer2, 4096));
+ }
+ dbgln("randomfill TEST PASSED");
+}
+
int main(void) {
dbgln("START");
malloc_test();
@@ -774,6 +794,7 @@ int main(void) {
dirname_test();
getline_test();
realpath_test();
+ randomfill_test();
// TODO: Add mkstemp
return 0;
}