[PATCH v2 3/3] sparc64: Randomize the load address of PIE executables

From: Stian Halseth

Date: Sun Oct 18 2026 - 08:27:52 EST


On sparc64 every PIE executable (ET_DYN with PT_INTERP) is loaded at
exactly ELF_ET_DYN_BASE, with its brk heap at a fixed offset after it.
The stack and the mmap base are randomized; the program image and brk
are not.

load_elf_binary() adds arch_mmap_rnd() to the load bias and calls
arch_randomize_brk(), but without ARCH_HAS_ELF_RANDOMIZE both are
no-ops. sparc64 already has a suitable mmap_rnd() for the mmap base.
Rename it to arch_mmap_rnd(), add arch_randomize_brk() with the ranges
x86 and the generic version use, and select ARCH_HAS_ELF_RANDOMIZE.

Closes: https://github.com/sparclinux/issues/issues/119
Tested-by: Jan Engelhardt <jengelh@xxxxxxx>
Signed-off-by: Stian Halseth <stian@xxxxxx>
---
.../features/vm/ELF-ASLR/arch-support.txt | 2 +-
arch/sparc/Kconfig | 1 +
arch/sparc/kernel/sys_sparc_64.c | 14 ++++++++++++--
3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/Documentation/features/vm/ELF-ASLR/arch-support.txt b/Documentation/features/vm/ELF-ASLR/arch-support.txt
index 57406c0d53531..d4a34c9fbae86 100644
--- a/Documentation/features/vm/ELF-ASLR/arch-support.txt
+++ b/Documentation/features/vm/ELF-ASLR/arch-support.txt
@@ -24,7 +24,7 @@
| riscv: | ok |
| s390: | ok |
| sh: | TODO |
- | sparc: | TODO |
+ | sparc: | ok |
| um: | TODO |
| x86: | ok |
| xtensa: | TODO |
diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
index ab77d3f2536e1..82c989b9e7831 100644
--- a/arch/sparc/Kconfig
+++ b/arch/sparc/Kconfig
@@ -71,6 +71,7 @@ config SPARC64
def_bool 64BIT
select ALTERNATE_USER_ADDRESS_SPACE
select ARCH_HAS_DELAY_TIMER
+ select ARCH_HAS_ELF_RANDOMIZE
select HAVE_FUNCTION_TRACER
select HAVE_FUNCTION_GRAPH_TRACER
select HAVE_KRETPROBES
diff --git a/arch/sparc/kernel/sys_sparc_64.c b/arch/sparc/kernel/sys_sparc_64.c
index 087595fa43081..2948d5769e768 100644
--- a/arch/sparc/kernel/sys_sparc_64.c
+++ b/arch/sparc/kernel/sys_sparc_64.c
@@ -26,6 +26,8 @@
#include <linux/ipc.h>
#include <linux/personality.h>
#include <linux/random.h>
+#include <linux/elf-randomize.h>
+#include <linux/sizes.h>
#include <linux/export.h>
#include <linux/context_tracking.h>
#include <linux/timex.h>
@@ -280,7 +282,7 @@ unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, u
EXPORT_SYMBOL(get_fb_unmapped_area);

/* Essentially the same as PowerPC. */
-static unsigned long mmap_rnd(void)
+unsigned long arch_mmap_rnd(void)
{
unsigned long rnd = 0UL;

@@ -294,9 +296,17 @@ static unsigned long mmap_rnd(void)
return rnd << PAGE_SHIFT;
}

+unsigned long arch_randomize_brk(struct mm_struct *mm)
+{
+ if (test_thread_flag(TIF_32BIT))
+ return randomize_page(mm->brk, SZ_32M);
+
+ return randomize_page(mm->brk, SZ_1G);
+}
+
void arch_pick_mmap_layout(struct mm_struct *mm, const struct rlimit *rlim_stack)
{
- unsigned long random_factor = mmap_rnd();
+ unsigned long random_factor = arch_mmap_rnd();
unsigned long gap;

/*
--
2.43.0