1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:47:42 +00:00

Map pages in read-only ELF sections as non-writable.

This is so cool! :^) Now you'll crash if you try to write into your
.text or .rodata segments.
This commit is contained in:
Andreas Kling 2018-11-03 11:36:45 +01:00
parent aa6d06b47e
commit da13c9a264
4 changed files with 26 additions and 6 deletions

View file

@ -304,8 +304,8 @@ void MemoryManager::map_region_at_address(PageDirectory* page_directory, Region&
auto page_laddr = laddr.offset(i * PAGE_SIZE);
auto pte = ensurePTE(page_directory, page_laddr);
pte.setPhysicalPageBase(zone.m_pages[i].get());
pte.setPresent(true);
pte.setWritable(true);
pte.setPresent(true); // FIXME: Maybe we could use the is_readable flag here?
pte.setWritable(region.is_writable);
pte.setUserAllowed(user_allowed);
flushTLB(page_laddr);
#ifdef MM_DEBUG
@ -430,7 +430,7 @@ RetainPtr<Region> Region::clone()
// FIXME: Implement COW regions.
auto clone_zone = MM.createZone(zone->size());
auto clone_region = adopt(*new Region(linearAddress, size, move(clone_zone), String(name)));
auto clone_region = adopt(*new Region(linearAddress, size, move(clone_zone), String(name), is_readable, is_writable));
// FIXME: It would be cool to make the src_alias a read-only mapping.
byte* src_alias = MM.create_kernel_alias_for_region(*this);

View file

@ -38,7 +38,7 @@ private:
};
struct Region : public Retainable<Region> {
Region(LinearAddress, size_t, RetainPtr<Zone>&&, String&&);
Region(LinearAddress, size_t, RetainPtr<Zone>&&, String&&, bool r, bool w);
~Region();
RetainPtr<Region> clone();
@ -46,6 +46,8 @@ struct Region : public Retainable<Region> {
size_t size { 0 };
RetainPtr<Zone> zone;
String name;
bool is_readable { true };
bool is_writable { true };
};
#define MM MemoryManager::the()

View file

@ -147,7 +147,7 @@ Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name
auto zone = MM.createZone(size);
ASSERT(zone);
m_regions.append(adopt(*new Region(laddr, size, move(zone), move(name))));
m_regions.append(adopt(*new Region(laddr, size, move(zone), move(name), is_readable, is_writable)));
MM.mapRegion(*this, *m_regions.last());
return m_regions.last().ptr();
@ -1260,11 +1260,13 @@ Process* Process::kernelProcess()
return s_kernelProcess;
}
Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n)
Region::Region(LinearAddress a, size_t s, RetainPtr<Zone>&& z, String&& n, bool r, bool w)
: linearAddress(a)
, size(s)
, zone(move(z))
, name(move(n))
, is_readable(r)
, is_writable(w)
{
}

View file

@ -65,6 +65,18 @@ static int sh_fef(int, const char**)
return 0;
}
static int sh_wt(int, const char**)
{
const char* rodata_ptr = "foo";
printf("Writing to rodata=%p...\n", rodata_ptr);
*(char*)rodata_ptr = 0;
char* text_ptr = (char*)sh_fef;
printf("Writing to text=%p...\n", text_ptr);
*text_ptr = 0;
return 0;
}
static int sh_exit(int, const char**)
{
printf("Good-bye!\n");
@ -135,6 +147,10 @@ static bool handle_builtin(int argc, const char** argv, int& retval)
retval = sh_fef(argc, argv);
return true;
}
if (!strcmp(argv[0], "wt")) {
retval = sh_wt(argc, argv);
return true;
}
if (!strcmp(argv[0], "fork")) {
retval = sh_fork(argc, argv);
return true;