diff options
author | Ian Moffett <ian@osmora.org> | 2025-09-20 21:32:01 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-09-20 21:32:01 -0400 |
commit | ebc26c8259160193dde5b0baecf7230c8388fd29 (patch) | |
tree | 0e110b8fd4c8e6c28a1d2fda4af35b804719746b | |
parent | e0504467b11ca122d76d7a5bf0dc66451ac8f293 (diff) |
kern: proc: Add address checking routine
Add routine to check if an address is valid within a process's address
space. More work will need to be done with this but this introduces a
great starting point
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | src/sys/include/sys/proc.h | 13 | ||||
-rw-r--r-- | src/sys/os/os_proc.c | 23 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/sys/include/sys/proc.h b/src/sys/include/sys/proc.h index 2ff2876..6a356ef 100644 --- a/src/sys/include/sys/proc.h +++ b/src/sys/include/sys/proc.h @@ -147,6 +147,19 @@ int md_proc_kill(struct proc *procp, int flags); int md_set_ip(struct proc *procp, uintptr_t ip); /* + * Check that a virtual address is within the bounds of + * a process. + * + * @proc: Process the address should be within + * @addr: Virtual address to check + * @len: Length of memory referenced by 'addr' + * + * Returns zero if the address is within the process bounds, + * otherwise a less than zero value on failure. + */ +int proc_check_addr(struct proc *proc, uintptr_t addr, size_t len); + +/* * Put the current process into a halt loop * until the next one runs. */ diff --git a/src/sys/os/os_proc.c b/src/sys/os/os_proc.c index 3a42496..f21fc69 100644 --- a/src/sys/os/os_proc.c +++ b/src/sys/os/os_proc.c @@ -139,3 +139,26 @@ proc_kill(struct proc *procp, int status) proc_clear_ranges(procp); return md_proc_kill(procp, 0); } + + +/* + * Check that an address is within the bounds of a + * process. + */ +int +proc_check_addr(struct proc *proc, uintptr_t addr, size_t len) +{ + uintptr_t stack_base; + uintptr_t stack_end; + + /* Within the bounds of the stack? */ + stack_base = STACK_TOP - STACK_LEN; + if (addr >= stack_base && addr <= STACK_TOP) { + return 0; + } + if ((stack_base + len) < stack_end) { + return 0; + } + + return -EFAULT; +} |