From 8f19140b9baf96a976af018e6dee952a02ec413a Mon Sep 17 00:00:00 2001 From: jewelcodes Date: Thu, 5 Sep 2024 13:55:13 -0400 Subject: [PATCH] sched: fix null pointer access in user space stack --- src/platform/x86_64/sched/context.c | 5 ++--- src/sched/exec.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/platform/x86_64/sched/context.c b/src/platform/x86_64/sched/context.c index f8a3dca..72e1af6 100644 --- a/src/platform/x86_64/sched/context.c +++ b/src/platform/x86_64/sched/context.c @@ -138,18 +138,17 @@ int platformSetContext(Thread *t, uintptr_t entry, uintptr_t highest, const char while(base % PAGE_SIZE) { base++; } - uintptr_t limit = base + PAGE_SIZE + PLATFORM_THREAD_STACK; size_t pages = (PLATFORM_THREAD_STACK+PAGE_SIZE-1)/PAGE_SIZE; pages++; - uintptr_t stack = vmmAllocate(base, limit, pages, VMM_WRITE | VMM_USER); + uintptr_t stack = vmmAllocate(base, USER_LIMIT_ADDRESS, pages, VMM_WRITE | VMM_USER); if(!stack) return -1; memset((void *)stack, 0, PLATFORM_THREAD_STACK + PAGE_SIZE); stack += PLATFORM_THREAD_STACK; ctx->regs.rsp = stack; - t->highest = stack; // requisite to sbrk() someday + t->highest = stack + PAGE_SIZE; // requisite to sbrk() someday return 0; } diff --git a/src/sched/exec.c b/src/sched/exec.c index 4f73f81..228245c 100644 --- a/src/sched/exec.c +++ b/src/sched/exec.c @@ -82,7 +82,16 @@ pid_t execveMemory(const void *ptr, const char **argv, const char **envp) { uint64_t highest; uint64_t entry = loadELF(ptr, &highest); - platformSetContext(process->threads[0], entry, highest, argv, envp); + if(platformSetContext(process->threads[0], entry, highest, argv, envp)) { + threadUseContext(getTid()); + free(process->threads[0]->context); + free(process->threads[0]); + free(process->threads); + free(process); + setScheduling(true); + schedRelease(); + return 0; + } KDEBUG("created new process with pid %d\n", pid);