From c9f833abf1d76ea194b82caafa06a0627790ad97 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Thu, 12 Aug 2021 05:45:15 +0300 Subject: [PATCH] rtld: Round down relro_size lld rounds up p_memsz(PT_GNU_RELRO) to satisfy common-page-size. If the page size is smaller than common-page-size, rounding up relro_size may incorrectly make some RW pages read-only. GNU ld, gold, and ld.lld ensures p_vaddr+p_memsz is a multiple of common-page-size. While max-page-size >= system the page size, common-page-size can be smaller than the system page size. Submitted by: MaskRay MFC after: 1 week Differential revision: https://reviews.freebsd.org/D31498 --- libexec/rtld-elf/map_object.c | 3 ++- libexec/rtld-elf/rtld.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libexec/rtld-elf/map_object.c b/libexec/rtld-elf/map_object.c index 273e477fbda5..b725fe93b8f6 100644 --- a/libexec/rtld-elf/map_object.c +++ b/libexec/rtld-elf/map_object.c @@ -325,7 +325,8 @@ map_object(int fd, const char *path, const struct stat *sb) } obj->stack_flags = stack_flags; obj->relro_page = obj->relocbase + trunc_page(relro_page); - obj->relro_size = round_page(relro_size); + obj->relro_size = trunc_page(relro_page + relro_size) - + trunc_page(relro_page); if (note_start < note_end) digest_notes(obj, note_start, note_end); if (note_map != NULL) diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index f60872f12c52..eaad89339d07 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -1557,7 +1557,8 @@ digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path) case PT_GNU_RELRO: obj->relro_page = obj->relocbase + trunc_page(ph->p_vaddr); - obj->relro_size = round_page(ph->p_memsz); + obj->relro_size = trunc_page(ph->p_vaddr + ph->p_memsz) - + trunc_page(ph->p_vaddr); break; case PT_NOTE: