From nobody Thu Nov 14 14:47:30 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 4Xq30M2LzRz5clMk; Thu, 14 Nov 2024 14:47:31 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (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 "smtp.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Xq30M1s8Bz4Zfv; Thu, 14 Nov 2024 14:47:31 +0000 (UTC) (envelope-from jhb@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731595651; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nnSM/DtVM8pmYnNWl9tmZEoD/sm16dSJUaNFzb5OHQI=; b=uBLHJQ0qQjfsQzK5qdKg/zgyhQdGy3tAAfgHG8jeBx5HEGoGaHVoxsm06ZL8OvNjA0qRGj jaj8GiIjwMCp6Hm7ys9FGa5wj2csAUVyXXyms0eG5O8xtQZEdKY9VtgU6hvgsOupAZJkTP UCC9PE/eyPuXqnuU55qfOwobsl2naW7R/QxTKzj+oOsZz9M61zK2N5o8DP2CDqR1TNFrEL Rukm8+3Rj0yA2t7RQHCwZyW9ZOGuefK+uQthGho3vJ1CAWGGa1YYnTaeOYkImmUUxeuCLx NGBUv11tOwiALnBmLEOtOomThTtsD4t71haP67R7WOqdYxPj3rWmDcoiiBP+xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1731595651; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nnSM/DtVM8pmYnNWl9tmZEoD/sm16dSJUaNFzb5OHQI=; b=BeJgyjpy2ksYUBnK9LeWM3nWELSHqnAkZRoDGOI9ZjdakrX9SY8EMsoGEmFHzrzgQhpW5z AH396AK9ULXMJQsJPfV15jOWSKAMKuQWaJAjgwpBh8M+ae7zSwg+6UUQstkCnKO0CgA5wi AKDw8R5TsdZ0bckY+hqTRspsunfb3M/zI9n0HXrf+Dsf66lRaMabNT4fLO6ofQGMzkW+mv wG54eHCvCyxX89RcnIe/wa0ppRWYd/VTGUV5UcRnJ7BUL+VgtjCMIDp9pRcm6MpbGPgsAv mKvUWLXDWh+tX+wJi8C4pFl5HWZ8xs0FrsLvy4wLDwp/8KVOAhzrWVSFGpB2Bg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1731595651; a=rsa-sha256; cv=none; b=TDc73akLOEDqbZxFrct9F/mFmDW2b50kJ0sTinHF/hDYg0qm1OOsYeIOj4CRURDxbrh6Gw XlAvjeB+IjXPRqxsrtKi9ToApDFg+kLL/h0UokkSsiZ8O8D3GqegEWlfCFEpjB3u8249ra nBGwjI/HJlPjUxc39ekBV+4xUV7n0m+8DMUM+f6QO5vVez7U9wwlrVVScyJjkTFfsKPa4w orOAdwi8RQ9AqqTtspZurT092OPqNNg65M/oCkXfZw7/sRvW4YYqXxtZwDqGwHqQ22bq1O QiwbZwXTQ83wBGAQYkblY+7pBfpEhHOhSMkMA/8ewMq4vbDSByx3kkvC8HPlOA== Received: from [IPV6:2601:5c0:4200:b830:e12f:83c9:e3d1:29c8] (unknown [IPv6:2601:5c0:4200:b830:e12f:83c9:e3d1:29c8]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: jhb) by smtp.freebsd.org (Postfix) with ESMTPSA id 4Xq30L6lvxzj1K; Thu, 14 Nov 2024 14:47:30 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Message-ID: <4d81d34f-4749-4911-b302-eca9166e4be7@FreeBSD.org> Date: Thu, 14 Nov 2024 09:47:30 -0500 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 User-Agent: Mozilla Thunderbird Subject: Re: git: e85eaa930862 - main - Have rtld query the page size from the kernel Content-Language: en-US To: Jessica Clarke Cc: Andrew Turner , "src-committers@freebsd.org" , "dev-commits-src-all@freebsd.org" , "dev-commits-src-main@freebsd.org" References: <202204071438.237Ecn2A012737@gitrepo.freebsd.org> <92a05dfe-683c-43d9-bd29-3110e89be275@FreeBSD.org> <768D45F9-2F02-4BA1-BFB7-51685486CFCC@freebsd.org> From: John Baldwin In-Reply-To: <768D45F9-2F02-4BA1-BFB7-51685486CFCC@freebsd.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. I agree though that having all arches implement rtld_relocate_nonplt_self() which is called before rtld() would be nicer. -- John Baldwin