From 55ffe634f9649b86cd15dd2bf1d14912734b4a37 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Mon, 11 Feb 2019 09:01:48 +0100 Subject: [PATCH] Enhancement: Check for Bad Thread Start Routine In this commit, I have introduced checks in the kthread_create() kernel call for bad thread start routine addresses. Note that this feature is not turned on by default, because in some targets the kernel may be linked with the user binary, thus resulting in error. Enhancement: Check for Bad Thread Argument In this commit, I have introduced checks in the kthread_create() kernel call for bad thread argument address. Note that this feature is not turned on by default, because in some targets the kernel may be linked with the user binary, thus resulting in error. Enhancement: Check for Bad Thread Join In this commit, I have introduced checks in the kthread_join() kernel call for bad thread return value address. Note that this feature is not turned on by default, because in some targets the kernel may be linked with the user binary, thus resulting in error. Enhancement: Check for Bad Thread Exit In this commit, I have introduced checks in the kthread_exit() kernel call for bad return value address. Note that this feature is not turned on by default, because in some targets the kernel may be linked with the user binary, thus resulting in error. --- include/nanvix/thread.h | 10 ++++++++++ src/kernel/sys/thread.c | 27 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/nanvix/thread.h b/include/nanvix/thread.h index 270b4fb30..a70aff4dd 100644 --- a/include/nanvix/thread.h +++ b/include/nanvix/thread.h @@ -56,6 +56,16 @@ #define THREAD_TERMINATED 4 /**< Terminated */ /**@}*/ + /** + * @name Features + */ + /**@{*/ + #define KERNEL_THREAD_BAD_START 0 /**< Check for bad thread start routine? */ + #define KERNEL_THREAD_BAD_ARG 0 /**< Check for bad thread argument? */ + #define KERNEL_THREAD_BAD_JOIN 0 /**< Check for bad thread join? */ + #define KERNEL_THREAD_BAD_EXIT 0 /**< Check for bad thread exit? */ + /**@}*/ + /** * @brief Thread. */ diff --git a/src/kernel/sys/thread.c b/src/kernel/sys/thread.c index b0feb52be..87c4fd331 100644 --- a/src/kernel/sys/thread.c +++ b/src/kernel/sys/thread.c @@ -23,6 +23,7 @@ */ #include +#include #include /** @@ -36,12 +37,24 @@ PUBLIC int sys_thread_get_id(void) /** * @see thread_create(). */ -PUBLIC int sys_thread_create(int *tid, void*(*start)(void*), void *arg) +PUBLIC int sys_thread_create(int *tid, void *(*start)(void*), void *arg) { /* Invalid start routine. */ if (start == NULL) return (-EINVAL); + /* Bad start routine. */ +#if (defined(KERNEL_THREAD_BAD_START) && (KERNEL_THREAD_BAD_START == 1)) + if (mm_is_kaddr(VADDR(start))) + return (-EINVAL); +#endif + + /* Bad argument. */ +#if (defined(KERNEL_THREAD_BAD_ARG) && (KERNEL_THREAD_BAD_ARG == 1)) + if ((arg != NULL) && (mm_is_kaddr(VADDR(arg)))) + return (-EINVAL); +#endif + return (thread_create(tid, start, arg)); } @@ -50,6 +63,12 @@ PUBLIC int sys_thread_create(int *tid, void*(*start)(void*), void *arg) */ PUBLIC int sys_thread_exit(void *retval) { + /* Bad exit. */ +#if (defined(KERNEL_THREAD_BAD_EXIT) && (KERNEL_THREAD_BAD_EXIT == 1)) + if ((retval != NULL) && (mm_is_kaddr(VADDR(retval)))) + return (-EINVAL); +#endif + thread_exit(retval); return (-EAGAIN); @@ -67,6 +86,12 @@ PUBLIC int sys_thread_join(int tid, void **retval) if (tid < 0) return (-EINVAL); + /* Bad join. */ +#if (defined(KERNEL_THREAD_BAD_JOIN) && (KERNEL_THREAD_BAD_JOIN == 1)) + if ((retval != NULL) && (mm_is_kaddr(VADDR(retval)))) + return (-EINVAL); +#endif + /* Cannot join itself. */ if (tid == thread_get_id(thread_get_curr())) return (-EINVAL);