From nobody Fri Dec 06 22:39:31 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y4mQr0xP5z5g1rD; Fri, 06 Dec 2024 22:39:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y4mQr0MTbz4DQZ; Fri, 6 Dec 2024 22:39:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733524772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lZAd3VE3QJZ/LFYb/m1nu9Jlrogc2uHU8H2qYIVYI/g=; b=D20CL8LULPBQHIy6iHoCU445BGf71IKA98zlDLLPCRsp5r3W3GouT6iYb9vVB9youe6ah3 rY7g+YoGuLLO/ssUBiB8I4EIFW5M08HTBa0ig08ShzP7by8s1FLq7TWLB0K1NsxbpKrlpy RAjkHUyaRSuJm42c/6uxlv/nJ+Rxb63fojSisZ2YcbAnGOjixS7ELe4yfu/E1i/dxDgu8H Lby7pelfZg/7v6RwW+GMwDKiksZtbgy0UZsuqug99s3BGh56/7V6tw7Enf7r87W9YAq/W3 dOfmOVveE0J97ChMCb1YWjDRHKRlvxBkTAWSUGEOjnlyAGrUjxhOUjvy5DcD9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733524772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lZAd3VE3QJZ/LFYb/m1nu9Jlrogc2uHU8H2qYIVYI/g=; b=E/poWZ6HbHJUcSScT5Zv2iBGM1gIoPazuAe1JVY7fR4cGTtyOhAp6HrE6O3iOFLA6zklHJ +BqxRvjzJNdef8HFzpJz5P/St+MbR+Z2pxyKjkK7OTRUM09Evm3B86fopNXD3Gk10rgX0K CLiwnGrxyzvyoEAbJvyIll3ayRb64kEVfeLkCyZYsSbcfukEE9XKCzz+sHjIV/8coVwUz6 RNfKA2zdgX+Q99FHe1bH8MWEEOu+ZGwBQiMB/PniKrSMgVuDzNOO3HbiMgXKlQ5ccls4fw QQG2bjdjk1ZT4yNxBOz1qchLJPt9HulSZwYm2GmlDSV/QOSqVNdd9LmYBmpKqw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733524772; a=rsa-sha256; cv=none; b=irPvt2bhBTqA3m4/e8odFC2rE1Y1yokNaSMNeeLPVO12U3mp+hEMZMPdfQED4Wa4NzZpHV zcWE2/u+5NJXfnZbAA6kRYJlhkuNt7KIMGbIXG2r7X3sc6SGhxw86G36o6GBiWdomdFrUi urIS1b70yXBEwf0jsE6f3xCJ3dUn+RkCbWyQ/I5AZ2efGxTrCz5WXPyjyi+LKpfBkFrIYO kYuQaNMDjEpO4mPzjgc4Q+Y++/lhPT/VfZCk/R3ETQ3tHr9vpqfLeweg7HSN3pjyZreYpn uGBw7BgJqNWp8Lv971Kan05OnAWXY7YWZnNAEXIbXp8omAL4upq1wOFe2J/KfA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y4mQq6fxDzx2j; Fri, 6 Dec 2024 22:39:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4B6MdVXo090892; Fri, 6 Dec 2024 22:39:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B6MdVJ4090889; Fri, 6 Dec 2024 22:39:31 GMT (envelope-from git) Date: Fri, 6 Dec 2024 22:39:31 GMT Message-Id: <202412062239.4B6MdVJ4090889@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: fda0403eb083 - main - rtld: Support multiple PT_GNU_RELRO program headers List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fda0403eb0839b29b0b271c69c5cb6bfc874a3b5 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=fda0403eb0839b29b0b271c69c5cb6bfc874a3b5 commit fda0403eb0839b29b0b271c69c5cb6bfc874a3b5 Author: John Baldwin AuthorDate: 2024-12-06 22:37:50 +0000 Commit: John Baldwin CommitDate: 2024-12-06 22:37:50 +0000 rtld: Support multiple PT_GNU_RELRO program headers Iterate over all the program headers in obj_remap_relro and remove the relro fields from Obj_Entry. Skip the call to obj_enforce_relro() in relocate_object() for the rtld object as well as the main program object. obj_enforce_relro() is called later when it safe to reference globals such as page_size. Reviewed by: kib Obtained from: CheriBSD Sponsored by: AFRL, DARPA Differential Revision: https://reviews.freebsd.org/D47884 --- libexec/rtld-elf/map_object.c | 12 ------------ libexec/rtld-elf/rtld.c | 37 ++++++++++++++++++++----------------- libexec/rtld-elf/rtld.h | 3 --- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/libexec/rtld-elf/map_object.c b/libexec/rtld-elf/map_object.c index 0b1500a15183..7dbab26f2c63 100644 --- a/libexec/rtld-elf/map_object.c +++ b/libexec/rtld-elf/map_object.c @@ -93,8 +93,6 @@ map_object(int fd, const char *path, const struct stat *sb) Elf_Addr bss_vlimit; caddr_t bss_addr; Elf_Word stack_flags; - Elf_Addr relro_page; - size_t relro_size; Elf_Addr note_start; Elf_Addr note_end; char *note_map; @@ -114,8 +112,6 @@ map_object(int fd, const char *path, const struct stat *sb) nsegs = -1; phdyn = phinterp = phtls = NULL; phdr_vaddr = 0; - relro_page = 0; - relro_size = 0; note_start = 0; note_end = 0; note_map = NULL; @@ -161,11 +157,6 @@ map_object(int fd, const char *path, const struct stat *sb) stack_flags = phdr->p_flags; break; - case PT_GNU_RELRO: - relro_page = phdr->p_vaddr; - relro_size = phdr->p_memsz; - break; - case PT_NOTE: if (phdr->p_offset > page_size || phdr->p_offset + phdr->p_filesz > page_size) { @@ -323,9 +314,6 @@ map_object(int fd, const char *path, const struct stat *sb) obj->tlsinit = mapbase + phtls->p_vaddr; } obj->stack_flags = stack_flags; - obj->relro_page = obj->relocbase + rtld_trunc_page(relro_page); - obj->relro_size = rtld_trunc_page(relro_page + relro_size) - - rtld_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 c585c47ce4da..7dc9d93dac26 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -1672,12 +1672,6 @@ digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry, const char *path) obj->stack_flags = ph->p_flags; break; - case PT_GNU_RELRO: - obj->relro_page = obj->relocbase + rtld_trunc_page(ph->p_vaddr); - obj->relro_size = rtld_trunc_page(ph->p_vaddr + ph->p_memsz) - - rtld_trunc_page(ph->p_vaddr); - break; - case PT_NOTE: note_start = (Elf_Addr)obj->relocbase + ph->p_vaddr; note_end = note_start + ph->p_filesz; @@ -2369,11 +2363,6 @@ parse_rtld_phdr(Obj_Entry *obj) case PT_GNU_STACK: obj->stack_flags = ph->p_flags; break; - case PT_GNU_RELRO: - obj->relro_page = obj->relocbase + - rtld_trunc_page(ph->p_vaddr); - obj->relro_size = rtld_round_page(ph->p_memsz); - break; case PT_NOTE: note_start = (Elf_Addr)obj->relocbase + ph->p_vaddr; note_end = note_start + ph->p_filesz; @@ -3328,7 +3317,7 @@ relocate_object(Obj_Entry *obj, bool bind_now, Obj_Entry *rtldobj, lockstate) == -1) return (-1); - if (!obj->mainprog && obj_enforce_relro(obj) == -1) + if (obj != rtldobj && !obj->mainprog && obj_enforce_relro(obj) == -1) return (-1); /* @@ -5909,12 +5898,26 @@ _rtld_is_dlopened(void *arg) static int obj_remap_relro(Obj_Entry *obj, int prot) { + const Elf_Phdr *ph; + caddr_t relro_page; + size_t relro_size; - if (obj->relro_size > 0 && mprotect(obj->relro_page, obj->relro_size, - prot) == -1) { - _rtld_error("%s: Cannot set relro protection to %#x: %s", - obj->path, prot, rtld_strerror(errno)); - return (-1); + for (ph = obj->phdr; (const char *)ph < (const char *)obj->phdr + + obj->phsize; ph++) { + switch (ph->p_type) { + case PT_GNU_RELRO: + relro_page = obj->relocbase + + rtld_trunc_page(ph->p_vaddr); + relro_size = + rtld_round_page(ph->p_vaddr + ph->p_memsz) - + rtld_trunc_page(ph->p_vaddr); + if (mprotect(relro_page, relro_size, prot) == -1) { + _rtld_error("%s: Cannot set relro protection to %#x: %s", + obj->path, prot, rtld_strerror(errno)); + return (-1); + } + break; + } } return (0); } diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h index 5527671d647e..383b8db2114c 100644 --- a/libexec/rtld-elf/rtld.h +++ b/libexec/rtld-elf/rtld.h @@ -165,9 +165,6 @@ typedef struct Struct_Obj_Entry { size_t tlsalign; /* Alignment of static TLS block */ size_t tlspoffset; /* p_offset of the static TLS block */ - caddr_t relro_page; - size_t relro_size; - /* Items from the dynamic section. */ Elf_Addr *pltgot; /* PLT or GOT, depending on architecture */ const Elf_Rel *rel; /* Relocation entries */