From bf2c51426dc4c8b528e6f2efff4aaca8c9430317 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 29 Oct 2022 17:27:05 +0900 Subject: [PATCH] kernel: proc->group may have exited Handling of thread parents needs to be better... the initial thread of a process can disappear, and with pid reuse it could even be replaced by a different process... this needs to be cleaned up more. --- kernel/arch/x86_64/idt.c | 8 +++++--- kernel/sys/syscall.c | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel/arch/x86_64/idt.c b/kernel/arch/x86_64/idt.c index ba4ee88b..62bae8d6 100644 --- a/kernel/arch/x86_64/idt.c +++ b/kernel/arch/x86_64/idt.c @@ -357,7 +357,7 @@ void arch_dump_traceback(void) { * * @param fromAddr The low address to map, should be page aligned. */ -static void map_more_stack(uintptr_t fromAddr) { +static int map_more_stack(uintptr_t fromAddr) { volatile process_t * volatile proc = this_core->current_process; /* Is this thread the process leader? */ @@ -365,6 +365,8 @@ static void map_more_stack(uintptr_t fromAddr) { proc = process_from_pid(proc->group); } + if (!proc) return 0; + /* Make sure nothing else is going to mess with this process's page tables */ spin_lock(proc->image.lock); @@ -378,6 +380,7 @@ static void map_more_stack(uintptr_t fromAddr) { proc->image.userstack = fromAddr; spin_unlock(proc->image.lock); + return 1; } /** @@ -520,8 +523,7 @@ static void _page_fault(struct regs * r) { /* Quietly map more stack if it was a viable stack address. */ if (faulting_address < 0x800000000000 && faulting_address > 0x700000000000) { - map_more_stack(faulting_address & 0xFFFFffffFFFFf000); - return; + if (map_more_stack(faulting_address & 0xFFFFffffFFFFf000)) return; } /* Otherwise, segfault the current process. */ diff --git a/kernel/sys/syscall.c b/kernel/sys/syscall.c index e012a371..f9b4159d 100644 --- a/kernel/sys/syscall.c +++ b/kernel/sys/syscall.c @@ -55,6 +55,7 @@ long sys_sbrk(ssize_t size) { if (proc->group != 0) { proc = process_from_pid(proc->group); } + if (!proc) return -EINVAL; spin_lock(proc->image.lock); uintptr_t out = proc->image.heap; for (uintptr_t i = out; i < out + size; i += 0x1000) { @@ -135,6 +136,7 @@ long sys_sysfunc(long fn, char ** args) { if (!args[0]) return -EFAULT; volatile process_t * volatile proc = this_core->current_process; if (proc->group != 0) proc = process_from_pid(proc->group); + if (!proc) return -EFAULT; spin_lock(proc->image.lock); proc->image.heap = (uintptr_t)args[0]; spin_unlock(proc->image.lock); @@ -149,6 +151,7 @@ long sys_sysfunc(long fn, char ** args) { if (!args) return -EFAULT; volatile process_t * volatile proc = this_core->current_process; if (proc->group != 0) proc = process_from_pid(proc->group); + if (!proc) return -EFAULT; spin_lock(proc->image.lock); /* Align inputs */ uintptr_t start = ((uintptr_t)args[0]) & 0xFFFFffffFFFFf000UL;