Skip to content

Commit 70b2228

Browse files
committed
apply text read+execute permissions, only after post linker does its thing. If there are no delayed relocations then we can apply it early on after the module image has been created
1 parent d56057e commit 70b2228

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

shiva_module.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,18 @@ set_linker_mode(struct shiva_module *linker)
26142614
}
26152615
return;
26162616
}
2617+
static bool
2618+
apply_memory_protection(struct shiva_module *linker)
2619+
{
2620+
if (mprotect(linker->text_mem,
2621+
ELF_PAGEALIGN(linker->text_size, PAGE_SIZE),
2622+
PROT_READ|PROT_EXEC) < 0) {
2623+
perror("mprotect");
2624+
return false;
2625+
}
2626+
return true;
2627+
}
2628+
26172629
/*
26182630
* NOTE: const char *path: path to the ELF module
26192631
*/
@@ -2715,6 +2727,12 @@ shiva_module_loader(struct shiva_ctx *ctx, const char *path, struct shiva_module
27152727
shiva_debug("Failed to resolve PLTGOT entries\n");
27162728
return false;
27172729
}
2730+
if ((linker->flags & SHIVA_MODULE_F_DELAYED_RELOCS) == 0) {
2731+
if (apply_memory_protection(linker) == false) {
2732+
shiva_debug("Failed to apply module segment memory protection\n");
2733+
return false;
2734+
}
2735+
}
27182736

27192737
/*
27202738
* If we are linking a Shiva module, then we pass control to the

shiva_post_linker.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ shiva_post_linker(void)
5353
shiva_debug("Transfering control to %#lx\n", ctx_global->ulexec.entry_point);
5454
test_mark();
5555

56+
/*
57+
* Mark the text segment as writable now that there won't
58+
* be any final fixups in the modules .text.
59+
*/
60+
if (mprotect(ctx_global->module.runtime->text_mem,
61+
ELF_PAGEALIGN(ctx_global->module.runtime->text_size,
62+
PAGE_SIZE),
63+
PROT_READ|PROT_EXEC) < 0) {
64+
perror("mprotect");
65+
return false;
66+
}
67+
5668
__asm__ __volatile__ ("mov x21, %0" :: "r"(ctx_global->ulexec.entry_point));
5769
return;
5870
}

0 commit comments

Comments
 (0)