summaryrefslogtreecommitdiffhomepage
path: root/dev/zka/src/Network/IPC.cc
blob: 3f69bc3cd997fcbeed46dcda726cd66c083915ff (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* -------------------------------------------

	Copyright ZKA Web Services Co.

------------------------------------------- */

#include <NetworkKit/IPC.h>
#include <KernelKit/LPC.h>
#include <KernelKit/UserProcessScheduler.h>

using namespace Kernel;

/// @internal
/// @brief The internal sanitize function.
Bool ipc_int_sanitize_packet(IPC_MESSAGE_STRUCT* pckt)
{
	auto endian = cDeduceEndian(pckt, ((Char*)pckt)[0]);

	switch (endian)
	{
	case Endian::kEndianBig: {
		if (pckt->IpcEndianess == eIPCEPLittleEndian)
			goto ipc_check_failed;

		break;
	}
	case Endian::kEndianLittle: {
		if (pckt->IpcEndianess == eIPCEPBigEndian)
			goto ipc_check_failed;

		break;
	}
	case Endian::kEndianMixed: {
		if (pckt->IpcEndianess == eIPCEPMixedEndian)
			goto ipc_check_failed;

		break;
	}
	default:
		goto ipc_check_failed;
	}

	if (pckt->IpcFrom == pckt->IpcTo ||
		pckt->IpcPacketSize > cXPCOMMsgSize)
	{
		goto ipc_check_failed;
	}

	return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == cXPCOMHeaderMagic;

ipc_check_failed:
	ErrLocal() = kErrorIPC;
	return false;
}

namespace Kernel
{
	/// @brief Sanitize packet function
	/// @retval true packet is correct.
	/// @retval false packet is incorrect and process has crashed.
	Bool ipc_sanitize_packet(IPC_MESSAGE_STRUCT* pckt)
	{
		if (!pckt ||
			!ipc_int_sanitize_packet(pckt))
		{
			UserProcessScheduler::The().CurrentProcess().Leak().Crash();
			return false;
		}

		return true;
	}

	/// @brief Construct packet function
	/// @retval true packet is correct.
	/// @retval false packet is incorrect and process has crashed.
	Bool ipc_construct_packet(_Output IPC_MESSAGE_STRUCT** pckt_in)
	{
		// don't do anything if it's valid already.
		if (*pckt_in)
			return true;

		// crash process if the packet pointer of pointer is NULL.
		if (!pckt_in)
		{
			UserProcessScheduler::The().CurrentProcess().Leak().Crash();
			return false;
		}

		*pckt_in = new IPC_MESSAGE_STRUCT();

		MUST_PASS((*pckt_in));

		if (*pckt_in)
		{
			auto endian = cDeduceEndian((*pckt_in), ((Char*)(*pckt_in))[0]);

			(*pckt_in)->IpcHeaderMagic = cXPCOMHeaderMagic;

			(*pckt_in)->IpcEndianess  = static_cast<UInt8>(endian);
			(*pckt_in)->IpcPacketSize = sizeof(IPC_MESSAGE_STRUCT);

			(*pckt_in)->IpcTo.UserProcessID	  = 0;
			(*pckt_in)->IpcTo.UserProcessTeam = 0;

			(*pckt_in)->IpcFrom.UserProcessID	= Kernel::UserProcessScheduler::The().CurrentProcess().Leak().ProcessId;
			(*pckt_in)->IpcFrom.UserProcessTeam = Kernel::UserProcessScheduler::The().CurrentTeam().mTeamId;

			return true;
		}

		UserProcessScheduler::The().CurrentProcess().Leak().Crash();
		return false;
	}
} // namespace Kernel