From 7a64bdbbcf14023370b90b22de73c2f61a6f84ac Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 4 Apr 2021 00:11:00 +0200 Subject: Implemented basic user program ASLR --- libs/libc/Makefile | 10 ++++++---- libs/libc/inc/random.h | 4 ++-- libs/libc/random.c | 48 +++++++++++++++++++----------------------------- libs/libgui/Makefile | 2 +- libs/libnet/Makefile | 2 +- libs/libtxt/Makefile | 2 +- 6 files changed, 30 insertions(+), 38 deletions(-) (limited to 'libs') diff --git a/libs/libc/Makefile b/libs/libc/Makefile index 24970d8..cc1b836 100644 --- a/libs/libc/Makefile +++ b/libs/libc/Makefile @@ -1,7 +1,8 @@ # MIT License, Copyright (c) 2020 Marvin Borner # TODO: Remove cpu from libc? -COBJS = sanitize.o \ +COBJS = crt/crt0.o \ + sanitize.o \ errno.o \ str.o \ alloc.o \ @@ -26,11 +27,12 @@ ASFLAGS = -f elf32 %.o: %.c @$(CC) -c $(CFLAGS) $< -o $@ -libc: CFLAGS += -DUSER +libc: CFLAGS += -pie -fPIE -fPIC -DUSER libc: $(COBJS) - @$(AS) $(ASFLAGS) crt/crt0.asm -o crt0.o @mkdir -p ../../build/ - @$(AR) rcs ../../build/libc.a crt0.o $+ + @$(AS) $(ASFLAGS) crt/crti.asm -o ../../build/crti.o + @$(AS) $(ASFLAGS) crt/crtn.asm -o ../../build/crtn.o + @$(AR) rcs ../../build/libc.a ../../build/crti.o ../../build/crti.o $+ libk: CFLAGS += -DKERNEL -ffreestanding -I../../kernel/inc/ $(CFLAGS_EXTRA) libk: $(COBJS) diff --git a/libs/libc/inc/random.h b/libs/libc/inc/random.h index a82524c..6360625 100644 --- a/libs/libc/inc/random.h +++ b/libs/libc/inc/random.h @@ -5,9 +5,9 @@ #include +#define rand_range(min, max) (rand() % ((max) + 1 - (min)) + (min)) + void srand(u32 seed); -u32 rdrand(void); -u32 rdseed(void); u32 rand(void); char *randstr(u32 size); diff --git a/libs/libc/random.c b/libs/libc/random.c index 6296407..983357f 100644 --- a/libs/libc/random.c +++ b/libs/libc/random.c @@ -15,46 +15,36 @@ void srand(u32 seed) g_seed = seed; } -u32 rdrand(void) +static u32 default_rand(void) { -#ifdef KERNEL - if (!(cpu_features.ecx & CPUID_FEAT_ECX_RDRND)) - return rand(); - - u32 rd; - __asm__ volatile("1:\n" - "rdrand %0\n" - "jnc 1b\n" - : "=r"(rd)); - return rd; -#else - return rand(); -#endif + g_seed = g_seed * 1103515245 + 12345; + return (g_seed >> 16) & 0x7FFF; } -u32 rdseed(void) +u32 rand(void) { #ifdef KERNEL - if (!(cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED)) - return rand(); - u32 rd; - __asm__ volatile("1:\n" - "rdseed %0\n" - "jnc 1b\n" - : "=r"(rd)); + if (cpu_extended_features.ebx & CPUID_EXT_FEAT_EBX_RDSEED) { + __asm__ volatile("1:\n" + "rdseed %0\n" + "jnc 1b\n" + : "=r"(rd)); + } else if (cpu_features.ecx & CPUID_FEAT_ECX_RDRND) { + __asm__ volatile("1:\n" + "rdrand %0\n" + "jnc 1b\n" + : "=r"(rd)); + } else { + rd = default_rand(); + } + return rd; #else - return rand(); + return default_rand(); #endif } -u32 rand(void) -{ - g_seed = g_seed * 1103515245 + 12345; - return (g_seed >> 16) & 0x7FFF; -} - char *randstr(u32 size) { if (!size) diff --git a/libs/libgui/Makefile b/libs/libgui/Makefile index e04b703..47e2556 100644 --- a/libs/libgui/Makefile +++ b/libs/libgui/Makefile @@ -10,7 +10,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -pie -fPIE -fPIC -DUSER all: libgui diff --git a/libs/libnet/Makefile b/libs/libnet/Makefile index 69c899d..7094875 100644 --- a/libs/libnet/Makefile +++ b/libs/libnet/Makefile @@ -5,7 +5,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -pie -fPIE -fPIC -DUSER all: libtxt diff --git a/libs/libtxt/Makefile b/libs/libtxt/Makefile index 6d06502..38fe01f 100644 --- a/libs/libtxt/Makefile +++ b/libs/libtxt/Makefile @@ -5,7 +5,7 @@ CC = ccache ../../cross/opt/bin/i686-elf-gcc LD = ccache ../../cross/opt/bin/i686-elf-ld AR = ccache ../../cross/opt/bin/i686-elf-ar -CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -DUSER +CFLAGS = $(CFLAGS_DEFAULT) -I../ -I../libc/inc/ -pie -fPIE -fPIC -DUSER all: libtxt -- cgit v1.2.3