Skip to content

Commit

Permalink
sched: fix null pointer access in user space stack (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes authored Sep 5, 2024
1 parent 3801406 commit 6b934bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/platform/x86_64/sched/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion src/sched/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6b934bd

Please sign in to comment.