From 0558e8040c0d9628858ddd85ce573b9c80941a1f Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Sat, 29 Jun 2024 23:45:47 +0200 Subject: Implement ipc_sanitize_packet as syscall. Signed-off-by: Amlal EL Mahrouss --- Kernel/HALKit/AMD64/HalKernelMain.cxx | 9 +++++++- Kernel/KernelKit/HError.hpp | 3 ++- Kernel/Sources/HError.cxx | 2 +- Kernel/Sources/Network/IPCEP.cxx | 40 +++++++++++++++++++++++++++++------ 4 files changed, 45 insertions(+), 9 deletions(-) (limited to 'Kernel') diff --git a/Kernel/HALKit/AMD64/HalKernelMain.cxx b/Kernel/HALKit/AMD64/HalKernelMain.cxx index 7d641153..d8c60e25 100644 --- a/Kernel/HALKit/AMD64/HalKernelMain.cxx +++ b/Kernel/HALKit/AMD64/HalKernelMain.cxx @@ -17,6 +17,7 @@ #include #include #include +#include #define KERNEL_INIT(X) X; \ NewOS::ke_stop(RUNTIME_CHECK_BOOTSTRAP); @@ -103,7 +104,7 @@ EXTERN_C void hal_init_platform( CONST NewOS::HAL::IDTLoader cIDT; cIDT.Load(idtBase); - // register the basic NAPI syscalls. + // Register the basic SCI functions. constexpr auto cSerialAlertInterrupt = 0x10; constexpr auto cTlsInterrupt = 0x11; @@ -123,6 +124,7 @@ EXTERN_C void hal_init_platform( constexpr auto cLPCSendMsg = 0x25; constexpr auto cLPCOpenMsg = 0x26; constexpr auto cLPCCloseMsg = 0x27; + constexpr auto cLPCSanitizeMsg = 0x28; kSyscalls[cSerialAlertInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx) -> void { const char* msg = (const char*)rdx; @@ -133,6 +135,10 @@ EXTERN_C void hal_init_platform( tls_check_syscall_impl(rdx); }; + kSyscalls[cLPCSanitizeMsg].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx) -> void { + NewOS::ipc_sanitize_packet(reinterpret_cast(rdx)); + }; + kSyscalls[cNewInterrupt].Leak().Leak()->fProc = [](NewOS::VoidPtr rdx) -> void { // get HAC struct. HeapAllocInfo* rdxInf = reinterpret_cast(rdx); @@ -198,6 +204,7 @@ EXTERN_C void hal_init_platform( kSyscalls[cLastExitInterrupt].Leak().Leak()->fHooked = true; kSyscalls[cShutdownInterrupt].Leak().Leak()->fHooked = true; kSyscalls[cRebootInterrupt].Leak().Leak()->fHooked = true; + kSyscalls[cLPCSanitizeMsg].Leak().Leak()->fHooked = true; NewOS::HAL::hal_system_get_cores(kHandoverHeader->f_HardwareTables.f_RsdPtr); diff --git a/Kernel/KernelKit/HError.hpp b/Kernel/KernelKit/HError.hpp index a61d84d4..468f9a12 100644 --- a/Kernel/KernelKit/HError.hpp +++ b/Kernel/KernelKit/HError.hpp @@ -46,8 +46,9 @@ namespace NewOS inline constexpr HError kErrorInvalidData = 56; inline constexpr HError kErrorAsync = 57; inline constexpr HError kErrorNonBlocking = 58; + inline constexpr HError kErrorIPC = 59; inline constexpr HError kErrorUnimplemented = 0; - Boolean ke_bug_check(void) noexcept; + Boolean err_bug_check(void) noexcept; } // namespace NewOS diff --git a/Kernel/Sources/HError.cxx b/Kernel/Sources/HError.cxx index 5ba02049..d38f2e07 100644 --- a/Kernel/Sources/HError.cxx +++ b/Kernel/Sources/HError.cxx @@ -11,7 +11,7 @@ namespace NewOS /// @brief Doea a system wide bug check. /// @param void no params. /// @return if error-free: true, otherwise false. - Boolean ke_bug_check(void) noexcept + Boolean err_bug_check(void) noexcept { /// TODO: return false; diff --git a/Kernel/Sources/Network/IPCEP.cxx b/Kernel/Sources/Network/IPCEP.cxx index 0cd9d778..9e198385 100644 --- a/Kernel/Sources/Network/IPCEP.cxx +++ b/Kernel/Sources/Network/IPCEP.cxx @@ -5,10 +5,14 @@ ------------------------------------------- */ #include +#include +#include using namespace NewOS; -Bool ipc_sanitize_packet(IPCEPMessageHeader* pckt) +/// @internal +/// @brief The internal sanitize function. +Bool __ipc_sanitize_packet(IPCEPMessageHeader* pckt) { if (!pckt) return false; @@ -19,25 +23,49 @@ Bool ipc_sanitize_packet(IPCEPMessageHeader* pckt) case Endian::kEndianBig: { if (pckt->IpcEndianess == eIPCEPLittleEndian) - return false; + goto _Fail; break; } case Endian::kEndianLittle: { if (pckt->IpcEndianess == eIPCEPBigEndian) - return false; + goto _Fail; break; } case Endian::kEndianMixed: break; default: - return false; + goto _Fail; } - if (pckt->IpcFrom == pckt->IpcTo) return false; - if (pckt->IpcPacketSize > cIPCEPMsgSize) return false; + if (pckt->IpcFrom == pckt->IpcTo) + { + goto _Fail; + } + if (pckt->IpcPacketSize > cIPCEPMsgSize) + { + goto _Fail; + } return pckt->IpcPacketSize > 1 && pckt->IpcHeaderMagic == cRemoteHeaderMagic; + +_Fail: + ErrLocal() = kErrorIPC; + return false; +} + +/// @brief Sanitize packet function +/// @retval true packet is correct. +/// @retval false packet is incorrect and process has crashed. +Bool ipc_sanitize_packet(IPCEPMessageHeader* pckt) +{ + if (!__ipc_sanitize_packet(pckt)) + { + ProcessScheduler::The().Leak().TheCurrent().Leak().Crash(); + return false; + } + + return true; } -- cgit v1.2.3