Skip to content

Commit

Permalink
Enhancement: Check for Bad Thread Start Routine
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ppenna committed Feb 11, 2019
1 parent 1b1c385 commit 55ffe63
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/nanvix/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
27 changes: 26 additions & 1 deletion src/kernel/sys/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#include <nanvix/const.h>
#include <nanvix/mm.h>
#include <nanvix/thread.h>

/**
Expand All @@ -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));
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 55ffe63

Please sign in to comment.