summaryrefslogtreecommitdiffhomepage
path: root/Private/Source
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@el-mahrouss-logic.com>2024-03-24 09:36:47 +0100
committerAmlal El Mahrouss <amlal@el-mahrouss-logic.com>2024-03-24 09:36:47 +0100
commitc1c1c7cb8ec2518b746804ecc2be9e0613fe685f (patch)
tree2c43f6637c9de551ad695a4d149dd66e0d6a18b7 /Private/Source
parent09383c793fe953da6441902b4f66b1382df46738 (diff)
Kernel: Add CRC32 check inside the kernel's heap,
Handover: Rename f_VendorTable to f_RsdPtr and f_SmBios. Signed-off-by: Amlal El Mahrouss <amlal@el-mahrouss-logic.com>
Diffstat (limited to 'Private/Source')
-rw-r--r--Private/Source/Crc32.cxx10
-rw-r--r--Private/Source/KernelCheck.cxx2
-rw-r--r--Private/Source/KernelHeap.cxx40
3 files changed, 37 insertions, 15 deletions
diff --git a/Private/Source/Crc32.cxx b/Private/Source/Crc32.cxx
index 53de6d13..eab12c37 100644
--- a/Private/Source/Crc32.cxx
+++ b/Private/Source/Crc32.cxx
@@ -7,9 +7,10 @@
#include <NewKit/Crc32.hpp>
// @file CRC32.cpp
-// @brief Checksum implementation.
+// @brief Check sequence implementation.
namespace HCore {
+/// @brief The CRC32 table.
UInt kCrcTbl[kCrcCnt] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
@@ -55,13 +56,16 @@ UInt kCrcTbl[kCrcCnt] = {
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
-/// @brief calculate CRC32 of pointer.
+/// @brief Calculate CRC32 of p
+/// @param p the data to compute.
+/// @param len the length of the data.
+/// @return the CRC32.
UInt ke_calculate_crc32(const Char *p, UInt len) noexcept {
UInt crc = 0xffffffff;
while (len-- != 0) crc = kCrcTbl[((UInt8)crc ^ *(p++))] ^ (crc >> 8);
- // return (~crc); also works
+ // return (~crc); also works, does the same thing.
return (crc ^ 0xffffffff);
}
} // namespace HCore
diff --git a/Private/Source/KernelCheck.cxx b/Private/Source/KernelCheck.cxx
index bf7ac939..dcf8a06d 100644
--- a/Private/Source/KernelCheck.cxx
+++ b/Private/Source/KernelCheck.cxx
@@ -37,7 +37,7 @@ void ke_stop(const HCore::Int &id) {
}
case RUNTIME_CHECK_POINTER: {
kcout << "*** CAUSE: RUNTIME_CHECK_POINTER *** \r\n";
- kcout << "*** WHAT: HEAP ERROR, UNSTABLE STATE. *** \r\n";
+ kcout << "*** WHAT: HEAP CRC32 ERROR, UNSTABLE STATE. *** \r\n";
break;
}
case RUNTIME_CHECK_BAD_BEHAVIOR: {
diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx
index 03e5f250..09f62004 100644
--- a/Private/Source/KernelHeap.cxx
+++ b/Private/Source/KernelHeap.cxx
@@ -5,10 +5,10 @@
------------------------------------------- */
#include <KernelKit/DebugOutput.hpp>
-#include <KernelKit/KernelHeap.hpp>
-#include <NewKit/PageManager.hpp>
#include <KernelKit/HError.hpp>
+#include <KernelKit/KernelHeap.hpp>
#include <NewKit/Crc32.hpp>
+#include <NewKit/PageManager.hpp>
//! @file KernelHeap.cxx
//! @brief Kernel allocator.
@@ -60,14 +60,14 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) {
}
/// @brief Declare pointer as free.
-/// @param ptr the pointer.
+/// @param heapPtr the pointer.
/// @return
-Int32 ke_delete_ke_heap(VoidPtr ptr) {
+Int32 ke_delete_ke_heap(VoidPtr heapPtr) {
if (kHeapCount < 1) return -kErrorInternal;
Detail::HeapInformationBlockPtr virtualAddress =
- reinterpret_cast<Detail::HeapInformationBlockPtr>((UIntPtr)ptr -
- sizeof(Detail::HeapInformationBlock));
+ reinterpret_cast<Detail::HeapInformationBlockPtr>(
+ (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock));
if (virtualAddress && virtualAddress->hMagic == kHeapMagic) {
if (virtualAddress->hCRC32 != 0) {
@@ -91,17 +91,35 @@ Int32 ke_delete_ke_heap(VoidPtr ptr) {
}
/// @brief Check if pointer is a valid kernel pointer.
-/// @param ptr the pointer
+/// @param heapPtr the pointer
/// @return if it exists.
-Boolean ke_is_valid_ptr(VoidPtr ptr) {
+Boolean ke_is_valid_heap(VoidPtr heapPtr) {
if (kHeapCount < 1) return false;
- if (ptr) {
+ if (heapPtr) {
+ Detail::HeapInformationBlockPtr virtualAddress =
+ reinterpret_cast<Detail::HeapInformationBlockPtr>(
+ (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock));
+
+ if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/// @brief Protect the heap pointer with a CRC32.
+/// @param heapPtr
+/// @return
+Boolean ke_protect_ke_heap(VoidPtr heapPtr) {
+ if (heapPtr) {
Detail::HeapInformationBlockPtr virtualAddress =
- reinterpret_cast<Detail::HeapInformationBlockPtr>((UIntPtr)ptr -
- sizeof(Detail::HeapInformationBlock));
+ reinterpret_cast<Detail::HeapInformationBlockPtr>(
+ (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock));
if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) {
+ virtualAddress->hCRC32 = ke_calculate_crc32((Char*)heapPtr, virtualAddress->hSizeAddress);
return true;
}
}