From 7b4bd3577a31d0f0adc7371840642791ae1567f4 Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 24 Jan 2025 10:38:36 +0100 Subject: ADD: Open version, with important changes kept out. Signed-off-by: Amlal --- dev/Kernel/src/ThreadLocalStorage.cc | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 dev/Kernel/src/ThreadLocalStorage.cc (limited to 'dev/Kernel/src/ThreadLocalStorage.cc') diff --git a/dev/Kernel/src/ThreadLocalStorage.cc b/dev/Kernel/src/ThreadLocalStorage.cc new file mode 100644 index 00000000..8bcfe616 --- /dev/null +++ b/dev/Kernel/src/ThreadLocalStorage.cc @@ -0,0 +1,67 @@ +/* + * ======================================================== + * + * minoskrnl + * Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved., all rights reserved. + * + * ======================================================== + */ + +#include +#include +#include +#include + +/***********************************************************************************/ +/// @bugs: 0 +/// @file ThreadLocalStorage.cc +/// @brief Process Thread Local Storage. +/***********************************************************************************/ + +using namespace Kernel; + +/** + * @brief Checks for cookie inside the TIB. + * @param tib_ptr the TIB to check. + * @return if the cookie is enabled, true; false otherwise + */ + +Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* tib_ptr) +{ + if (!tib_ptr || + !tib_ptr->Record) + return false; + + ICodec encoder; + const Char* tib_as_bytes = encoder.AsBytes(tib_ptr); + + kcout << "TLS: Validating the TIB...\r"; + + return tib_as_bytes[kCookieMag0Idx] == kCookieMag0 && tib_as_bytes[kCookieMag1Idx] == kCookieMag1 && + tib_as_bytes[kCookieMag2Idx] == kCookieMag2; +} + +/** + * @brief System call implementation of the TLS check. + * @param tib_ptr The TIB record. + * @return if the TIB record is valid or not. + */ +EXTERN_C Bool tls_check_syscall_impl(Kernel::VoidPtr tib_ptr) noexcept +{ + if (!tib_ptr) + { + kcout << "TLS: Failing because of an invalid TIB...\r"; + return false; + } + + THREAD_INFORMATION_BLOCK* tib = reinterpret_cast(tib_ptr); + + if (!tls_check_tib(tib)) + { + kcout << "TLS: Failing because of an invalid TIB...\r"; + return false; + } + + kcout << "TLS Passed checked.\r"; + return true; +} -- cgit v1.2.3