1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:27:35 +00:00

Kernel+LibC+crash: Add mprotect() syscall

This patch adds the mprotect() syscall to allow changing the protection
flags for memory regions. We don't do any region splitting/merging yet,
so this only works on whole mmap() regions.

Added a "crash -r" flag to verify that we crash when you attempt to
write to read-only memory. :^)
This commit is contained in:
Andreas Kling 2019-08-12 19:33:24 +02:00
parent 4917445e2e
commit 7d6689055f
7 changed files with 36 additions and 1 deletions

View file

@ -1,6 +1,7 @@
#include <AK/AKString.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
static void print_usage_and_exit()
{
@ -20,6 +21,7 @@ int main(int argc, char** argv)
WriteToFreedMemory,
ReadFromUninitializedMallocMemory,
ReadFromFreedMemory,
WriteToReadonlyMemory,
};
Mode mode = SegmentationViolation;
@ -42,6 +44,8 @@ int main(int argc, char** argv)
mode = WriteToUninitializedMallocMemory;
else if (String(argv[1]) == "-F")
mode = WriteToFreedMemory;
else if (String(argv[1]) == "-r")
mode = WriteToReadonlyMemory;
else
print_usage_and_exit();
@ -97,6 +101,16 @@ int main(int argc, char** argv)
ASSERT_NOT_REACHED();
}
if (mode == WriteToReadonlyMemory) {
auto* ptr = (u8*)mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
ASSERT(ptr != MAP_FAILED);
*ptr = 'x'; // This should work fine.
int rc = mprotect(ptr, 4096, PROT_READ);
ASSERT(rc == 0);
ASSERT(*ptr == 'x');
*ptr = 'y'; // This should crash!
}
ASSERT_NOT_REACHED();
return 0;
}