From a22eb6669d1de0c9b16518ee1e2b48e44a72f176 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 17 Jan 2020 14:09:45 +0100 Subject: [PATCH] =?UTF-8?q?[BUG]=C2=A0Stack=20smashed=20during=20paging=20?= =?UTF-8?q?init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kaleid/kernel/init/init.c | 6 ++++++ kaleid/kernel/init/ssp.c | 2 +- kaleid/kernel/mm/paging.c | 27 ++++++++++----------------- kaleid/kernel/mm/palloc.c | 34 +++++++++++++++++++--------------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 3a3144c..7febf67 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -81,9 +81,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) KeGetCpuInfos(); IoEnableKeyb(); + for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); + } + // Command line (kernel mode) ShStartShell(); + //KeCrashSystem(); // Exit ! PoShutdown(); + } diff --git a/kaleid/kernel/init/ssp.c b/kaleid/kernel/init/ssp.c index 958f1b2..bf2d70d 100644 --- a/kaleid/kernel/init/ssp.c +++ b/kaleid/kernel/init/ssp.c @@ -24,7 +24,7 @@ #include -ulong __stack_chk_guard = 0x447c0ffe4dbf9e55; +ulong __stack_chk_guard = 0xec0ffec0ffec0ffe; noreturn void __stack_chk_fail(void) { diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 6a9bd1d..af14533 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -34,8 +34,8 @@ //----------- -pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); -ulong *MmPhysicalPageTable; +static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +static ulong *MmPhysicalPageTable __attribute__((__aligned__(KPAGESIZE))); extern ulong _text; extern ulong _text_end; @@ -89,7 +89,7 @@ void MmInitPaging(void) memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - DebugLog("PhDirSize : %d\n", phDirSize/sizeof(ulong)); + DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable); for (curAddrPML4 = 0; curAddrPML4 < 512 * KPAGESIZE * 0x8000000; @@ -128,9 +128,6 @@ void MmInitPaging(void) continue; } - if (index == 0x447c0ffe4dbf9e55) - KeStartPanic("ERROR"); - MmPD = (pde_t *)malloc(512*sizeof(pde_t)); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -168,21 +165,18 @@ void MmInitPaging(void) index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; xedni = (curAddrPT / ((ulong)KPAGESIZE)); - if (curAddrPT == 0x973db000) - DebugLog("ERR : %p\n", &MmPhysicalPageTable[xedni]); - // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)curAddrPT; - //DebugLog("\tStack Guard at %p\n", curAddrPT); + DebugLog("\tStack Guard at %p\n", curAddrPT); } else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[1] = (ulong)curAddrPT; - //DebugLog("\tStack Guard at %p\n", curAddrPT); + DebugLog("\tStack Guard at %p\n", curAddrPT); } // SECTION .TEXT PROTECTION else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { @@ -207,12 +201,6 @@ void MmInitPaging(void) MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; } - else { - MmPT[index] = (ulong)0; - MmPhysicalPageTable[xedni] = (ulong)0; - } - - KeFlushTlbSingle(curAddrPT); } } } @@ -223,6 +211,11 @@ void MmInitPaging(void) MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); + + for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); + } + DebugLog("\n"); } // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 1e3bafe..1b663e7 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -249,24 +249,28 @@ error_t MmUnmapPageFrame(ulong id) error_t MmTestBusyPage(void) { - ulong tab[2000] = {0}; - int j = 0; - - for (int i = 0; i < 2000; i++) { - if (rand() %2) { - if (rand() %2) { - tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); - } else { - tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); - } - } else { - MmFreePageFrame(tab[rand() % (j+1)]); - } + for (ulong curAddrPT = 0x00971cb000; curAddrPT <= 0x00971fb000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); } - //printBusyPages(); + /* ulong tab[2000] = {0}; */ + /* int j = 0; */ - DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); + /* for (int i = 0; i < 2000; i++) { */ + /* if (rand() %2) { */ + /* if (rand() %2) { */ + /* tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); */ + /* } else { */ + /* tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); */ + /* } */ + /* } else { */ + /* MmFreePageFrame(tab[rand() % (j+1)]); */ + /* } */ + /* } */ + + /* //printBusyPages(); */ + + /* DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); */ return EOK; }