Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zero-len pkey_mprotect #293

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions libia2/ia2.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,24 @@ int protect_tls_pages(struct dl_phdr_info *info, size_t size, void *data) {
// it is followed by padding that ensures nothing else occupies the rest of
// its page.
if (untrusted_stackptr_addr >= start && untrusted_stackptr_addr < end) {
// Protect TLS region start to the beginning of the untrusted region.
int mprotect_err = pkey_mprotect(
(void *)start_round_down, untrusted_stackptr_addr - start_round_down,
PROT_READ | PROT_WRITE, search_args->pkey);
if (mprotect_err != 0) {
printf("pkey_mprotect failed: %s\n", strerror(errno));
exit(-1);
}
mprotect_err = pkey_mprotect((void *)untrusted_stackptr_addr + 0x1000,
end - (untrusted_stackptr_addr + 0x1000),
PROT_READ | PROT_WRITE, search_args->pkey);
if (mprotect_err != 0) {
printf("pkey_mprotect failed: %s\n", strerror(errno));
exit(-1);
uint64_t after_untrusted_region_start = untrusted_stackptr_addr + 0x1000;
uint64_t after_untrusted_region_len = end - after_untrusted_region_start;
if (after_untrusted_region_len > 0) {
mprotect_err = pkey_mprotect((void *)after_untrusted_region_start,
after_untrusted_region_len,
PROT_READ | PROT_WRITE, search_args->pkey);
if (mprotect_err != 0) {
printf("pkey_mprotect failed: %s\n", strerror(errno));
exit(-1);
}
}
} else {
int mprotect_err =
Expand Down
6 changes: 3 additions & 3 deletions runtime/track_memory_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ bool update_memory_map(struct memory_map *map, int event,
#define PKRU(pkey) (~((3 << (2 * pkey)) | 3))

unsigned char pkey_for_pkru(uint32_t pkru) {
#define CHECK(x) \
case PKRU(x): \
#define CHECK(x) \
case PKRU(x): \
return x;
switch (pkru) {
CHECK(0);
Expand Down Expand Up @@ -415,7 +415,7 @@ void track_memory_map(pid_t pid, struct memory_map *map) {

/* track effect of syscall on memory map */
if (!update_memory_map(map, event, &event_info)) {
fprintf(stderr, "could not update memory map! (operation=%d)\n", event);
fprintf(stderr, "could not update memory map! (operation=%s, rip=%p)\n", event_name(event), (void *)regs.rip);
return;
}
}
Expand Down