From nobody Tue Nov 19 06:30:20 2024 X-Original-To: dev-commits-src-all@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 4XsvkZ2TZnz5dfQF; Tue, 19 Nov 2024 06:30:30 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (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 mx1.freebsd.org (Postfix) with ESMTPS id 4XsvkY2myFz4MSD; Tue, 19 Nov 2024 06:30:29 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; dkim=none; spf=softfail (mx1.freebsd.org: 2001:470:d5e7:1::1 is neither permitted nor denied by domain of kostikbel@gmail.com) smtp.mailfrom=kostikbel@gmail.com; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=gmail.com (policy=none) Received: from tom.home (kib@localhost [127.0.0.1] (may be forged)) by kib.kiev.ua (8.18.1/8.18.1) with ESMTP id 4AJ6UKLh065190; Tue, 19 Nov 2024 08:30:23 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 4AJ6UKLh065190 Received: (from kostik@localhost) by tom.home (8.18.1/8.18.1/Submit) id 4AJ6UKLZ065189; Tue, 19 Nov 2024 08:30:20 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 19 Nov 2024 08:30:20 +0200 From: Konstantin Belousov To: John Baldwin Cc: Jessica Clarke , Andrew Turner , "src-committers@freebsd.org" , "dev-commits-src-all@freebsd.org" , "dev-commits-src-main@freebsd.org" Subject: Re: git: e85eaa930862 - main - Have rtld query the page size from the kernel Message-ID: References: <202204071438.237Ecn2A012737@gitrepo.freebsd.org> <92a05dfe-683c-43d9-bd29-3110e89be275@FreeBSD.org> <768D45F9-2F02-4BA1-BFB7-51685486CFCC@freebsd.org> <4d81d34f-4749-4911-b302-eca9166e4be7@FreeBSD.org> List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4d81d34f-4749-4911-b302-eca9166e4be7@FreeBSD.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on tom.home X-Spamd-Result: default: False [-2.98 / 15.00]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.98)[-0.977]; MIME_GOOD(-0.10)[text/plain]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : No valid SPF, No valid DKIM,none]; TO_DN_EQ_ADDR_SOME(0.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; MIME_TRACE(0.00)[0:+]; MISSING_XM_UA(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; R_DKIM_NA(0.00)[]; MLMMJ_DEST(0.00)[dev-commits-src-all@freebsd.org,dev-commits-src-main@freebsd.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; HAS_XAW(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; RCVD_TLS_LAST(0.00)[]; RCPT_COUNT_FIVE(0.00)[6] X-Rspamd-Queue-Id: 4XsvkY2myFz4MSD X-Spamd-Bar: -- On Thu, Nov 14, 2024 at 09:47:30AM -0500, John Baldwin wrote: > On 11/13/24 12:10, Jessica Clarke wrote: > > On 13 Nov 2024, at 19:44, John Baldwin wrote: > > > > > > On 4/7/22 07:38, Andrew Turner wrote: > > > > The branch main has been updated by andrew: > > > > URL: https://cgit.FreeBSD.org/src/commit/?id=e85eaa930862d5b4dc917bc31e8d7254a693635d > > > > commit e85eaa930862d5b4dc917bc31e8d7254a693635d > > > > Author: Andrew Turner > > > > AuthorDate: 2022-04-04 15:05:40 +0000 > > > > Commit: Andrew Turner > > > > CommitDate: 2022-04-07 14:37:37 +0000 > > > > Have rtld query the page size from the kernel > > > > To allow for a dynamic page size on arm64 have the runtime linker > > > > query the kernel for the currentl page size. > > > > Reviewed by: kib > > > > Sponsored by: The FreeBSD Foundation > > > > Differential Revision: https://reviews.freebsd.org/D34765 > > > > > > This broke relro handling for rtld. The reason is that init_pagesizes() is > > > called after parsing the program headers for rltd in init_rtld(). As a result, > > > page_size is 0 when rtld_round_page() is called so the relro_size is 0. The > > > RTLD_INIT_EARLY_PAGESIZES case was for ia64, and in the early case it's probably > > > not safe to call sysctl? If it is safe to call sysctl, we could just always > > > init pagesizes early? > > > > It looks like there are a few things going on: > > > > 1. relocate_object calls obj_enforce_relro if !obj->mainprog, so will > > try to enforce RELRO for RTLD itself whilst page_size is 0 > > > > 2. init_rtld later calls obj_enforce_relro for obj_rtld, after > > page_size has been initialised > > > > 3. init_rtld is careful to avoid using global variables until it’s > > called relocate_objects for RTLD itself, but by hiding accesses to > > page_size away in rtld_*_page that’s no longer true (definitely not > > true in the case of text relocations, for example, though whether it > > also occurs for other cases we care more about I don’t know) > > > > So I think there are a couple of things to fix: > > > > 1. Stop accessing page_size prior to relocate_objects returning for > > RTLD itself > > > > 2. Stop enforcing RELRO twice for RTLD (e.g. add && obj != rtldobj to > > relocate_object’s case) > > > > At least, that’s what I’ve inferred from reading the code. > > > > Though, to be honest, things might be rather nicer if we just made > > .rtld_start responsible for relocating RTLD itself prior to calling > > init_rtld, that’s what we have to do for CHERI, as do arm, powerpc and > > powerpc64, and it means you can use globals from the start in init_rtld. > > I've done 2) locally which fixed my immediate issue. Can you provide some more info please? obj_enforce_relro() only acts if obj_relro_size is > 0, which requires the call to parse_rtld_phdr() to fill it. This is indeed called after the rtld is relocated by relocate_object(s), but so it was before the Andrew' change. I can see a problem if plt is mapped ro and requires relro to remap it rw before relocation, but I do not believe this is the case for rtld and any static linker. And what is the arch? The RTLD_INIT_PAGESIZES_EARLY symbol indeed should be eliminated, I have the patch. But let's sort out the page size issue as well. > > I agree though that having all arches implement rtld_relocate_nonplt_self() > which is called before rtld() would be nicer. > > -- > John Baldwin