diff options
author | Anton Kling <anton@kling.gg> | 2023-11-10 15:19:49 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-11-10 15:19:49 +0100 |
commit | 0cb4afef6da5488a128e5aaece435e9aa5f5797e (patch) | |
tree | b77f05fa03878bad5542c31e42f19f4c99034149 /kernel | |
parent | 8ffef83741948964171ca111fd7c90534515ae87 (diff) |
Kernel/Memory: Fill new allocations with random data.
This should make it easier to spot uninitalized memory being used.
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/arch/i386/mmu.c | 3 | ||||
-rw-r--r-- | kernel/cpu/syscall.c | 2 | ||||
-rw-r--r-- | kernel/fs/ext2.c | 2 | ||||
-rw-r--r-- | kernel/kmalloc.c | 27 |
4 files changed, 19 insertions, 15 deletions
diff --git a/kernel/arch/i386/mmu.c b/kernel/arch/i386/mmu.c index 9f20ce0..53aa5dd 100644 --- a/kernel/arch/i386/mmu.c +++ b/kernel/arch/i386/mmu.c @@ -3,6 +3,7 @@ #include <log.h> #include <math.h> #include <mmu.h> +#include <random.h> #define INDEX_FROM_BIT(a) (a / (32)) #define OFFSET_FROM_BIT(a) (a % (32)) @@ -35,6 +36,7 @@ void *ksbrk(size_t s) { // If there is no active pagedirectory we // just assume that the memory is // already mapped. + get_random((void *)rc, data_end - rc); return (void *)rc; } // Determine whether we are approaching a unallocated table @@ -57,6 +59,7 @@ void *ksbrk(size_t s) { assert(((uintptr_t)rc % PAGE_SIZE) == 0); memset((void *)rc, 0x00, s); + get_random((void *)rc, data_end - rc); return (void *)rc; } diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c index 6b4f437..8e17ae1 100644 --- a/kernel/cpu/syscall.c +++ b/kernel/cpu/syscall.c @@ -18,10 +18,10 @@ #include <scalls/recvfrom.h> #include <scalls/sendto.h> #include <scalls/shm.h> +#include <scalls/sigaction.h> #include <scalls/socket.h> #include <scalls/stat.h> #include <scalls/uptime.h> -#include <scalls/sigaction.h> #include <stdint.h> #include <string.h> diff --git a/kernel/fs/ext2.c b/kernel/fs/ext2.c index 0c226cf..6bde98b 100644 --- a/kernel/fs/ext2.c +++ b/kernel/fs/ext2.c @@ -58,7 +58,7 @@ void cached_read_block(uint32_t block, void *address, size_t size, struct BLOCK_CACHE *c = &cache[free_found]; c->block_num = block; read_lba(block * block_byte_size / 512, c->block, 1024, 0); - return cached_read_block(block, address, size, offset); + cached_read_block(block, address, size, offset); } void ext2_read_block(uint32_t block, void *address, size_t size, diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index 34c466d..75bc74b 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -2,7 +2,8 @@ #include <kmalloc.h> #include <ksbrk.h> #include <math.h> -#define NEW_ALLOC_SIZE 0x30000 +#include <random.h> +#define NEW_ALLOC_SIZE 0x20000 #define IS_FREE (1 << 0) #define IS_FINAL (1 << 1) @@ -142,6 +143,7 @@ void *kmalloc(size_t s) { free_entry->flags = 0; free_entry->n = new_entry; free_entry->magic = 0xdde51ab9410268b1; + get_random((void *)rc, s); return rc; } @@ -215,16 +217,15 @@ void *kcalloc(size_t nelem, size_t elsize) { } void kfree(void *p) { - /* -if (!p) -return; -// FIXME: This assumes that p is at the start of a allocated area. -// Could this be avoided in a simple way? -MallocHeader *h = (MallocHeader *)((uintptr_t)p - sizeof(MallocHeader)); -assert(h->magic == 0xdde51ab9410268b1); -if (h->flags & IS_FREE) -return; - -h->flags |= IS_FREE; -merge_headers(h);*/ + if (!p) + return; + // FIXME: This assumes that p is at the start of a allocated area. + // Could this be avoided in a simple way? + MallocHeader *h = (MallocHeader *)((uintptr_t)p - sizeof(MallocHeader)); + assert(h->magic == 0xdde51ab9410268b1); + if (h->flags & IS_FREE) + return; + + h->flags |= IS_FREE; + merge_headers(h); } |