summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKAKit/src/ThreadLocalStorage.cc
blob: 59bfb694d0684ead3a7298c0886e0fd43d2332f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * ========================================================
 *
 * minoskrnl
 * Copyright (C) 2024, ELMH Group, all rights reserved., all rights reserved.
 *
 *  ========================================================
 */

#include <NewKit/KString.h>
#include <CFKit/Property.h>
#include <KernelKit/UserProcessScheduler.h>
#include <KernelKit/ThreadLocalStorage.h>

/***********************************************************************************/
/// @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;

	IEncoderObject encoder;
	const char*	   tib_as_bytes = encoder.AsBytes(tib_ptr);

	kcout << "Checking for a valid cookie inside the TIB...\r";

	return tib_as_bytes[0] == kCookieMag0 && tib_as_bytes[1] == kCookieMag1 &&
		   tib_as_bytes[2] == kCookieMag2;
}

/**
 * @brief System call implementation of the TLS check.
 * @param tib_ptr The TIB record.
 * @return
 */
EXTERN_C Bool tls_check_syscall_impl(Kernel::VoidPtr tib_ptr) noexcept
{
	if (!tib_ptr)
	{
		kcout << "Failing because of an invalid TIB...\r";
		return false;
	}

	THREAD_INFORMATION_BLOCK* tib = (THREAD_INFORMATION_BLOCK*)tib_ptr;

	if (!tls_check_tib(tib))
	{
		kcout << "Crashing because of an invalid TIB...\r";
		return false;
	}

	kcout << "Verification succeeded! staying alive...\r";
	return true;
}