From 342245fa2121d6e6ec96e9f4a380811d5151a148 Mon Sep 17 00:00:00 2001 From: Ariel Abreu Date: Thu, 3 Mar 2022 00:09:29 -0500 Subject: [PATCH] dtape getters: Return `NULL` if input is `NULL` Found via UBSAN --- duct-tape/internal-include/darlingserver/duct-tape/task.h | 3 +++ duct-tape/internal-include/darlingserver/duct-tape/thread.h | 6 ++++++ duct-tape/src/thread.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/duct-tape/internal-include/darlingserver/duct-tape/task.h b/duct-tape/internal-include/darlingserver/duct-tape/task.h index 4c1f342..6eda66b 100644 --- a/duct-tape/internal-include/darlingserver/duct-tape/task.h +++ b/duct-tape/internal-include/darlingserver/duct-tape/task.h @@ -18,6 +18,9 @@ struct dtape_task { __attribute__((always_inline)) static dtape_task_t* dtape_task_for_xnu_task(task_t xnu_task) { + if (!xnu_task) { + return NULL; + } return (dtape_task_t*)((char*)xnu_task - offsetof(dtape_task_t, xnu_task)); }; diff --git a/duct-tape/internal-include/darlingserver/duct-tape/thread.h b/duct-tape/internal-include/darlingserver/duct-tape/thread.h index 23c61ee..f1f79cf 100644 --- a/duct-tape/internal-include/darlingserver/duct-tape/thread.h +++ b/duct-tape/internal-include/darlingserver/duct-tape/thread.h @@ -42,11 +42,17 @@ struct dtape_thread { __attribute__((always_inline)) static dtape_thread_t* dtape_thread_for_xnu_thread(thread_t xnu_thread) { + if (!xnu_thread) { + return NULL; + } return (dtape_thread_t*)((char*)xnu_thread - offsetof(dtape_thread_t, xnu_thread)); }; __attribute__((always_inline)) static dtape_task_t* dtape_task_for_thread(dtape_thread_t* thread) { + if (!thread) { + return NULL; + } return dtape_task_for_xnu_task(thread->xnu_thread.task); }; diff --git a/duct-tape/src/thread.c b/duct-tape/src/thread.c index 07d19ad..5426e70 100644 --- a/duct-tape/src/thread.c +++ b/duct-tape/src/thread.c @@ -414,7 +414,7 @@ void dtape_thread_dying(dtape_thread_t* thread) { thread_t current_thread(void) { dtape_thread_t* thread = dtape_hooks->current_thread(); - return &thread->xnu_thread; + return thread ? &thread->xnu_thread : NULL; }; void (thread_reference)(thread_t thread) {