فهرست منبع

Protect the first 4 KB of memory.

This makes null pointers crashy, tremendously useful :^)
Andreas Kling 6 سال پیش
والد
کامیت
d5ec18027e
2فایلهای تغییر یافته به همراه18 افزوده شده و 1 حذف شده
  1. 17 1
      Kernel/MemoryManager.cpp
  2. 1 0
      Kernel/MemoryManager.h

+ 17 - 1
Kernel/MemoryManager.cpp

@@ -38,7 +38,10 @@ void MemoryManager::initializePaging()
     kprintf("MM: Page table zero @ %p\n", m_pageTableZero);
     kprintf("MM: Page table one @ %p\n", m_pageTableOne);
 
-    identityMap(LinearAddress(0), 4 * MB);
+    // Make null dereferences crash.
+    protectMap(LinearAddress(0), 4 * KB);
+
+    identityMap(LinearAddress(4096), 4 * MB);
  
     // Put pages between 4MB and 16MB in the page freelist.
     for (size_t i = (4 * MB) + 1024; i < (16 * MB); i += PAGE_SIZE) {
@@ -79,6 +82,19 @@ auto MemoryManager::ensurePTE(LinearAddress linearAddress) -> PageTableEntry
     return PageTableEntry(&pde.pageTableBase()[pageTableIndex]);
 }
 
+void MemoryManager::protectMap(LinearAddress linearAddress, size_t length)
+{
+    // FIXME: ASSERT(linearAddress is 4KB aligned);
+    for (dword offset = 0; offset < length; offset += 4096) {
+        auto pteAddress = linearAddress.offset(offset);
+        auto pte = ensurePTE(pteAddress);
+        pte.setPhysicalPageBase(pteAddress.get());
+        pte.setUserAllowed(false);
+        pte.setPresent(false);
+        pte.setWritable(false);
+    }
+}
+
 void MemoryManager::identityMap(LinearAddress linearAddress, size_t length)
 {
     // FIXME: ASSERT(linearAddress is 4KB aligned);

+ 1 - 0
Kernel/MemoryManager.h

@@ -58,6 +58,7 @@ private:
 
     void initializePaging();
 
+    void protectMap(LinearAddress, size_t length);
     void identityMap(LinearAddress, size_t length);
 
     Vector<PhysicalAddress> allocatePhysicalPages(size_t count);