NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: port-amd64/58366: KASLR broken
The following reply was made to PR port-amd64/58366; it has been noted by GNATS.
From: RVP <rvp%SDF.ORG@localhost>
To: Taylor R Campbell <campbell%mumble.net@localhost>
Cc: Harold Gutch <logix%foobar.franken.de@localhost>, gnats-bugs%NetBSD.org@localhost,
port-amd64-maintainer%NetBSD.org@localhost
Subject: Re: port-amd64/58366: KASLR broken
Date: Wed, 18 Nov 2026 00:14:06 +0000 (UTC)
On Sun, 30 Jun 2024, Taylor R Campbell wrote:
> [...]
> It looks like when ld splits sections, it sometimes chooses
> non-aligned splitting points and then reduces the alignment of the
> next section accordingly:
>
> section address size align
> .rodata 0x22c0 0x5cfe0 64
> .rodata.0 0x5f2a0 0xe2c80 32
>
> The starting address of .rodata is 64-byte-aligned, but its size is
> only 32-byte-aligned. The starting address of .rodata.0, which starts
> contiguously after .rodata in the virtual address space of the ELF
> file, is only 32-byte-aligned. And when we get to .rodata.4, it's
> gone down to only 8-byte alignment.
>
> So when the KASLR bootloader (`prekern') randomizes the address space,
> if it respects the requested alignment but roughly uniformly
> randomizes everything else, there's a roughly 1/2 probability that the
> .rodata.4 section will come out misaligned for PADDQ and the kernel
> will crash at boot.
> [...]
>
That's a great analysis!
> But the right fix is probably to convince ld to insert appropriate
> padding in the split sections so that the alignment can be maintained
> [...]
>
You can use SUBALIGN[1] to force an alignment on input sections:
```
--- src/sys/arch/amd64/conf/kern.ldscript.kaslr.orig 2018-01-21 11:21:40.000000000 +0000
+++ src/sys/arch/amd64/conf/kern.ldscript.kaslr 2026-11-08 04:26:50.878950027 +0000
@@ -31,7 +31,7 @@
__rodata_hotpatch_end = . ;
}
- .rodata :
+ .rodata : SUBALIGN(COHERENCY_UNIT)
{
*(.rodata)
*(.rodata.*)
```
I've done this to the .rodata.* sections and it's been working for me so far:
```
$ readelf -Wa /mnt/netbsd.KASLR | fgrep .rodata
[18] .rodata.hotpatch PROGBITS 0000000000000011 10e268 002382 00 A 0 0 1
[19] .rela.rodata.hotpatch RELA 0000000000000000 1af8228 005538 18 I 151 18 8
[20] .rodata PROGBITS 00000000000023c0 1105f0 05f640 00 A 0 0 64
[21] .rela.rodata RELA 0000000000000000 1afd760 01be10 18 I 151 20 8
[137] .rodata.0 PROGBITS 0000000000061a00 10ea928 0e2fc0 00 A 0 0 64
[138] .rela.rodata.0 RELA 0000000000000000 2620e88 003bd0 18 I 151 137 8
[139] .rodata.1 PROGBITS 00000000001449c0 11cd8e8 0f5bc0 00 A 0 0 64
[140] .rela.rodata.1 RELA 0000000000000000 2624a58 012390 18 I 151 139 8
[141] .rodata.2 PROGBITS 000000000023a580 12c34a8 108b40 00 A 0 0 64
[142] .rela.rodata.2 RELA 0000000000000000 2636de8 07a868 18 I 151 141 8
[143] .rodata.3 PROGBITS 00000000003430c0 13cbfe8 10cf40 00 A 0 0 64
[144] .rela.rodata.3 RELA 0000000000000000 26b1650 040560 18 I 151 143 8
[145] .rodata.4 PROGBITS 0000000000450000 14d8f28 1154c0 00 A 0 0 64
[146] .rodata.5 PROGBITS 00000000005654c0 15ee3e8 041b02 00 A 0 0 64
[...]
$
```
-RVP
[1]: https://sourceware.org/binutils/docs/ld/Forced-Input-Alignment.html
Home |
Main Index |
Thread Index |
Old Index