aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorMarvin Borner2021-04-05 00:22:15 +0200
committerMarvin Borner2021-04-05 00:22:15 +0200
commitb476dc856431436ac0f3e0179f8a25302421a0cc (patch)
treeac89ab7533db5ebae796ec0f732271afff10c891 /libs
parent248c0d2fd007eeeb108ff1a88924e3ef8ff84ba4 (diff)
Added new randomization features
Diffstat (limited to 'libs')
-rw-r--r--libs/libc/crt/crt0.c22
-rw-r--r--libs/libc/inc/random.h1
-rw-r--r--libs/libc/inc/sys.h2
-rw-r--r--libs/libc/random.c32
-rw-r--r--libs/libc/sanitize.c4
-rw-r--r--libs/libc/sys.c5
6 files changed, 41 insertions, 25 deletions
diff --git a/libs/libc/crt/crt0.c b/libs/libc/crt/crt0.c
index 6c38474..3bd413f 100644
--- a/libs/libc/crt/crt0.c
+++ b/libs/libc/crt/crt0.c
@@ -1,18 +1,28 @@
// MIT License, Copyright (c) 2021 Marvin Borner
+#include <assert.h>
#include <def.h>
+#include <random.h>
#include <sys.h>
#ifdef USER
-extern int main(int, char **);
+extern u32 __stack_chk_guard;
+u32 __stack_chk_guard;
-void _start(void);
-void _start(void)
+int main(int, char **);
+
+int _start(int argc, char **argv);
+int _start(int argc, char **argv)
{
- exit(main(0, NULL));
- while (1)
- ;
+ u32 stamp = 0;
+ assert(read("/dev/rtc", &stamp, 0, sizeof(stamp)) == sizeof(stamp) && stamp);
+ srand(stamp);
+ rand_fill(&__stack_chk_guard, sizeof(__stack_chk_guard));
+
+ exit(main(argc, argv));
+
+ return 1;
}
#endif
diff --git a/libs/libc/inc/random.h b/libs/libc/inc/random.h
index 6360625..1b41df5 100644
--- a/libs/libc/inc/random.h
+++ b/libs/libc/inc/random.h
@@ -9,6 +9,7 @@
void srand(u32 seed);
u32 rand(void);
+void rand_fill(void *buf, u32 size);
char *randstr(u32 size);
#endif
diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h
index b435899..58d7969 100644
--- a/libs/libc/inc/sys.h
+++ b/libs/libc/inc/sys.h
@@ -28,7 +28,6 @@ enum sys {
SYS_EXIT, // Exit current process
SYS_BOOT, // Boot functions (e.g. reboot/shutdown)
SYS_YIELD, // Switch to next process
- SYS_TIME, // Get kernel time
/* SYS_NET_OPEN, // Open network socket */
/* SYS_NET_CLOSE, // Close network socket */
/* SYS_NET_CONNECT, // Connect to destination */
@@ -75,7 +74,6 @@ res poll(const char **files) NONNULL;
res exec(const char *path, ...) ATTR((nonnull(1))) SENTINEL;
res yield(void);
res boot(u32 cmd);
-u32 time(void);
res sys_alloc(u32 size, u32 *addr) NONNULL;
res sys_free(void *ptr) NONNULL;
diff --git a/libs/libc/random.c b/libs/libc/random.c
index 983357f..2cd7f93 100644
--- a/libs/libc/random.c
+++ b/libs/libc/random.c
@@ -15,36 +15,52 @@ void srand(u32 seed)
g_seed = seed;
}
-static u32 default_rand(void)
+static u32 rand_default(void)
{
g_seed = g_seed * 1103515245 + 12345;
return (g_seed >> 16) & 0x7FFF;
}
-u32 rand(void)
+static u32 rand_once(void)
{
#ifdef KERNEL
- u32 rd;
+ u32 val = 0;
if (cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED) {
__asm__ volatile("1:\n"
"rdseed %0\n"
"jnc 1b\n"
- : "=r"(rd));
+ : "=r"(val));
} else if (cpu_features.ecx & CPUID_FEAT_ECX_RDRND) {
__asm__ volatile("1:\n"
"rdrand %0\n"
"jnc 1b\n"
- : "=r"(rd));
+ : "=r"(val));
} else {
- rd = default_rand();
+ val = rand_default();
}
- return rd;
+ return val;
#else
- return default_rand();
+ return rand_default();
#endif
}
+u32 rand(void)
+{
+ u32 ret = 0;
+ ret |= (rand_once() & (0xffu << 0));
+ ret |= (rand_once() & (0xffu << 8));
+ ret |= (rand_once() & (0xffu << 16));
+ ret |= (rand_once() & (0xffu << 24));
+ return ret;
+}
+
+void rand_fill(void *buf, u32 size)
+{
+ for (u32 i = 0; i < size; i++)
+ ((u8 *)buf)[i] = rand_once() & 0xff;
+}
+
char *randstr(u32 size)
{
if (!size)
diff --git a/libs/libc/sanitize.c b/libs/libc/sanitize.c
index 415b790..d31c20c 100644
--- a/libs/libc/sanitize.c
+++ b/libs/libc/sanitize.c
@@ -8,10 +8,6 @@
* Stack protector
*/
-#define STACK_CHK_GUARD 0xdeadbeef
-
-u32 __stack_chk_guard = STACK_CHK_GUARD;
-
void __stack_chk_fail(void);
NORETURN void __stack_chk_fail(void)
{
diff --git a/libs/libc/sys.c b/libs/libc/sys.c
index 15a9669..139f523 100644
--- a/libs/libc/sys.c
+++ b/libs/libc/sys.c
@@ -170,11 +170,6 @@ res boot(u32 cmd)
return sys2(SYS_BOOT, SYS_BOOT_MAGIC, cmd);
}
-u32 time(void)
-{
- return (u32)sys0(SYS_TIME);
-}
-
/**
* At exit
*/