Commit 67c422ca authored by SantiM's avatar SantiM Committed by Ben Gamari
Browse files

rts/linker/{SymbolExtras,elf_got}.c: map code as read-only

protect mmaped addresses from writes after being initially manipulated

Test Plan: ./validate

Reviewers: bgamari, erikd, simonmar

Reviewed By: bgamari

Subscribers: angerman, carlostome, rwbarton, thomie, carter

GHC Trac Issues: #14069

Differential Revision: https://phabricator.haskell.org/D4817
parent c7b1e93b
...@@ -51,8 +51,9 @@ int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first ) ...@@ -51,8 +51,9 @@ int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
n = roundUpToPage(oc->fileSize); n = roundUpToPage(oc->fileSize);
/* Keep image and symbol_extras contiguous */ /* Keep image and symbol_extras contiguous */
void *new = mmapForLinker(n + (sizeof(SymbolExtra) * count),
MAP_ANONYMOUS, -1, 0); size_t allocated_size = n + (sizeof(SymbolExtra) * count);
void *new = mmapForLinker(allocated_size, MAP_ANONYMOUS, -1, 0);
if (new) { if (new) {
memcpy(new, oc->image, oc->fileSize); memcpy(new, oc->image, oc->fileSize);
if (oc->imageMapped) { if (oc->imageMapped) {
...@@ -62,6 +63,9 @@ int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first ) ...@@ -62,6 +63,9 @@ int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
oc->imageMapped = true; oc->imageMapped = true;
oc->fileSize = n + (sizeof(SymbolExtra) * count); oc->fileSize = n + (sizeof(SymbolExtra) * count);
oc->symbol_extras = (SymbolExtra *) (oc->image + n); oc->symbol_extras = (SymbolExtra *) (oc->image + n);
if(mprotect(new, allocated_size, PROT_READ | PROT_EXEC) != 0) {
sysErrorBelch("unable to protect memory");
}
} }
else { else {
oc->symbol_extras = NULL; oc->symbol_extras = NULL;
......
...@@ -62,6 +62,9 @@ makeGot(ObjectCode * oc) { ...@@ -62,6 +62,9 @@ makeGot(ObjectCode * oc) {
symTab->symbols[i].got_addr symTab->symbols[i].got_addr
= (uint8_t *)oc->info->got_start = (uint8_t *)oc->info->got_start
+ (slot++ * sizeof(void*)); + (slot++ * sizeof(void*));
if(mprotect(mem, oc->info->got_size, PROT_READ) != 0) {
sysErrorBelch("unable to protect memory");
}
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment