Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Nov 2024 20:10:53 +0000
From:      Jessica Clarke <jrtc27@freebsd.org>
To:        John Baldwin <jhb@FreeBSD.org>
Cc:        Andrew Turner <andrew@FreeBSD.org>, "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org>
Subject:   Re: git: e85eaa930862 - main - Have rtld query the page size from the kernel
Message-ID:  <768D45F9-2F02-4BA1-BFB7-51685486CFCC@freebsd.org>
In-Reply-To: <92a05dfe-683c-43d9-bd29-3110e89be275@FreeBSD.org>
References:  <202204071438.237Ecn2A012737@gitrepo.freebsd.org> <92a05dfe-683c-43d9-bd29-3110e89be275@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 13 Nov 2024, at 19:44, John Baldwin <jhb@FreeBSD.org> wrote:
>=20
> 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=3De85eaa930862d5b4dc917bc31e8d7254=
a693635d
>> commit e85eaa930862d5b4dc917bc31e8d7254a693635d
>> Author:     Andrew Turner <andrew@FreeBSD.org>
>> AuthorDate: 2022-04-04 15:05:40 +0000
>> Commit:     Andrew Turner <andrew@FreeBSD.org>
>> 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
>=20
> 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=E2=80=99s=

called relocate_objects for RTLD itself, but by hiding accesses to
page_size away in rtld_*_page that=E2=80=99s 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=E2=80=99t 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 !=3D rtldobj to
relocate_object=E2=80=99s case)

At least, that=E2=80=99s what I=E2=80=99ve 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=E2=80=99s 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.

Jess




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?768D45F9-2F02-4BA1-BFB7-51685486CFCC>