Skip to contentSkip to conversation

unwind_user: Implement .eh_frame handling

RFC PATCH In review Jens Remus 3 participants 58 messages Aug 21 – Sep 14

Subsystems (13)
Memory management x86 VFS MM core +9 more subsystems
lore
Share & tools
lore

Jens Remus

author
#0 lore raw

v2 cover unwind_user: Implement .eh_frame handling

This series adds support for parsing DWARF Call Frame Information (CFI) from the .eh_frame_hdr and .eh_frame sections of user space ELF files.

The code is based on the deferred unwind user work originally done for SFrame by Josh, Steven, and myself:

v4 : https://lore.kernel.org/all/[email protected]/
v10: https://lore.kernel.org/all/[email protected]/
v16: https://lore.kernel.org/all/[email protected]/

The goal is to make user space stack traces available in-kernel without requiring frame pointers and without copying large parts of the user stack for later processing.

Today, reliable user stack traces from the kernel generally requires frame pointers. Otherwise, profilers such as perf have to copy large amounts of user space stack into the kernel ring buffer and process it later. Frame pointers are simple and robust, but enabling them for all executables and libraries has a performance cost.

Another issue is that the frame layout can vary between compilers and architectures, and on architectures such as s390 there is no defined frame layout which allows reliable frame-pointer based stack tracing. The only way to perform user space profiling on there architectures is to copy the user space into the kernel buffer.

The .eh_frame section is already emitted by most toolchains on most architectures unless explicitly disabled. It contains DWARF CFI describing how to recover the caller state at any point in a function. The .eh_frame_hdr section provides a binary search table for looking up the Frame Description Entry (FDE) for a given instruction pointer (IP).

Because the .eh_frame_hdr and .eh_frame sections live in the ELF file, they need to be faulted in when used. This means that walking the user space stack requires being in a faultable context. As profilers like perf request a stack trace in interrupt or NMI context, the walking cannot be done when requested. This series reuses the deferred unwind user framework, that performed the actual user stack trace is later in a faultable context, before the task returns to user space.

This series implements .eh_frame[_hdr] support for the deferred unwind user code and enables it for x86-64 and s390.

It intentionally not implement a complete DWARF unwinder. It evaluates only the subset of DWARF CFI needed for stack tracing:

- Call Frame Address (CFA): Using rule from DWARF CFI.

- Stack pointer (SP): Using an implicit rule based on the CFA definition (SP = CFA for most architectures).

- Frame pointer (FP): Using rule from DWARF CFI.

- Return address (RA): Using rule from DWARF CFI.

Unsupported CFI instructions, unsupported expressions, invalid data, or user memory faults stop the stack tracing safely and results in a partial stack trace.

This series applies on top of v7.2 tag:

git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git v7.2

The to be stack-traced user space executables and libraries need to contain .eh_frame_hdr and .eh_frame sections as well as a GNU_EH_FRAME PHDR.

Namhyung Kim's related perf tools deferred callchain support can be used for testing, for example:

perf record --call-graph fp,defer ...
perf report
perf script

Why .eh_frame?

This series is not meant to replace or undermine the SFrame work. SFrame remains the simpler and more purpose-built format for user stack tracing. The motivation for .eh_frame support is pragmatic: .eh_frame is already widely deployed today.

- Availability and maturity: .eh_frame is already present in most ELF binaries for C++ exception handling. It has been used in production for decades for exception handling and debugger stack unwinding.

- Toolchain support: .eh_frame is supported across all major compilers and architectures today, whereas .sframe adoption is still emerging.

- Size: .sframe would be added in addition to existing .eh_frame[_hdr] rather than replacing it, increasing the ELF file size. [1]

Addressing historical DWARF concerns:

Using DWARF for kernel unwinding has a bad history. Previous attempts were complex, fragile, slow, and hard to maintain. Hand-written assembly and the complexity of the DWARF state machine were among the reasons the simpler ORC kernel unwind format was developed. [2,3,4]

However, this implementation for user space stack tracing differs from those problematic kernel unwinding attempts:

- It stack traces user space, not kernel.

- It runs in a deferred, faultable context, not in NMI, interrupt, or oops context.

- It may return partial stack traces. Bad CFI, unsupported operations, invalid user memory, or faults are allowed to terminate the unwind.

- It implements only the CFI subset needed for stack tracing, not a general DWARF unwinder.

- It does not include a general-purpose DWARF expression VM. Expression size is bounded. Only a small number of pattern-matched expressions is supported (e.g. DRAP and PLT expressions on x86). Unsupported expressions cause stack tracing to fail safely.

- All user memory access uses [unsafe_]get_user() with proper bounds checking and fault handling.

- Corruption detection with automatic section removal on invalid .eh_frame prevents further stack tracing attempts.

Limitations and future work:

- CIE version 1 support only and no DWARF64 support, as I have not run into either during my testing.

- Signal frames are not handled yet. An architecture hook could support unwinding through FDEs whose CIE augmentation contains 'S' (signal frame), similar to Glibc's SFrame backtrace() support. See also my "[RFC PATCH v1 0/5] s390: Signal frame user space unwinding". [5]

- x86-32, x86-x32, and 32-bit compat mode support not implemented yet.

- CIE caching would be useful. Reading an FDE requires reading its referenced CIE first to obtain the FDE encoding. Most .eh_frame sections have only a very small number of CIEs, often one default CIE shared by most FDEs and possibly one signal frame CIE. Caching the last CIE per section, together with the initial CFA, FP, and RA rules, would avoid repeated CIE parsing and initial CFI instruction processing.

[1]: https://lore.kernel.org/all/CAN30aBFVDxeoXApn_g_Hw0Ayhi4V=m7CcX8UDO6ZDTi6xA-3Pg@mail.gmail.com/
[2]: https://lwn.net/Articles/727553/
[3]: https://lkml.org/lkml/2012/2/10/356
[4]: https://lkml.org/lkml/2017/5/20/165
[5]: https://lore.kernel.org/all/[email protected]/

Patches 1-6 add base functionality to unwind user to support .eh_frame- based (or .sframe-based) unwinding. Patches originate from my latest .sframe patch series.

Patches 7-10 add the basic infrastructure for reading .eh_frame_hdr and .eh_frame sections and storing them in a per-mm maple tree.

Patches 11-14 wire up the eh_frame infrastructure to the unwind user framework and add error handling and debugging support.

Patch 15 duplicates registered .eh_frame_hdr section data on clone/fork.

Patch 16 improves .eh_frame DWARF CFI instruction processing.

Patch 17 enables architectures to implement selected DWARF expressions in CFI instructions.

Patches 18-21 enable .eh_frame unwinding on x86-64 with minimal DWARF expression support for DRAP and PLT expressions.

Patches 22-23 enable .eh_frame unwinding on s390.

Patch 24 adds a prctl() interface for (un)registering .eh_frame_hdr sections for shared libraries. I will send a related test-patch for Glibc separately.

Changes in RFC v2:
- Addressed most of Sashikos AI review feedback.
- Dropped patch that added support for linear .eh_frame search, as there is no good mean to limit it from reading outside of the .eh_frame section.
- Tweaked limits based on limited testing (still fairly arbitrary; needs more thought):
- Reduced state stack depth limit to 1.
- Added FDE length limit of 32.768 bytes.
- Added CFI instruction limit (CIE+FDE) of 16.384 instructions.

Note that I will be away from keyboard for two weeks. I'll reply to your (and Sashiko AI's) feedback afterwards.

Thanks and regards,
Jens

Jens Remus (23):
unwind_user: Add generic and arch-specific headers to MAINTAINERS
unwind_user: Stop when reaching an outermost frame
unwind_user: Enable archs that pass RA in a register
unwind_user: Flexible FP/RA recovery rules
unwind_user: Flexible CFA recovery rules
unwind_user: Enable archs that define CFA = SP_callsite + offset unwind_user/eh_frame: Add support for reading .eh_frame_hdr section unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple
tree
unwind_user/eh_frame: Add support for reading .eh_frame section unwind_user/eh_frame: Detect .eh_frame_hdr sections in executables unwind_user/eh_frame: Wire up unwind_user to eh_frame unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected
corruption
unwind_user/eh_frame: Show file name in debug output unwind_user/eh_frame: Add .eh_frame[_hdr] validation option unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section
data on clone/fork
unwind_user/eh_frame: Ignore DW_CFA_GNU_args_size unwind_user/eh_frame: Add support for DWARF expressions unwind_user/eh_frame/x86: Enable eh_frame unwinding on x86 unwind_user/eh_frame/x86: Handle PLT expressions unwind_user/eh_frame/x86: Handle DRAP expressions s390/ptrace: Provide frame_pointer() unwind_user/eh_frame/s390: Enable eh_frame unwinding on s390 unwind_user/eh_frame: Add prctl() interface for (un)registering
.eh_frame_hdr sections

Josh Poimboeuf (1):
x86/uaccess: Add unsafe_copy_from_user() implementation

 MAINTAINERS                                  |    5 +
 arch/Kconfig                                 |   26 +
 arch/s390/Kconfig                            |    1 +
 arch/s390/include/asm/ptrace.h               |    6 +
 arch/s390/include/asm/unwind_user.h          |   71 +
 arch/s390/include/asm/unwind_user_eh_frame.h |   24 +
 arch/x86/Kconfig                             |    1 +
 arch/x86/include/asm/mmu.h                   |    2 +-
 arch/x86/include/asm/uaccess.h               |   39 +-
 arch/x86/include/asm/unwind_user.h           |   77 +-
 arch/x86/include/asm/unwind_user_eh_frame.h  |  153 ++
 fs/binfmt_elf.c                              |   49 +-
 include/asm-generic/Kbuild                   |    1 +
 include/asm-generic/unwind_user_eh_frame.h   |   92 +
 include/linux/eh_frame.h                     |  105 ++
 include/linux/mm_types.h                     |    3 +
 include/linux/unwind_user.h                  |   20 +
 include/linux/unwind_user_eh_frame_types.h   |   41 +
 include/linux/unwind_user_types.h            |   51 +-
 include/uapi/linux/eh_frame.h                |   14 +
 include/uapi/linux/prctl.h                   |    4 +
 kernel/fork.c                                |   10 +
 kernel/sys.c                                 |   11 +
 kernel/unwind/Makefile                       |    3 +-
 kernel/unwind/eh_frame.c                     | 1657 ++++++++++++++++++
 kernel/unwind/eh_frame.h                     |   83 +
 kernel/unwind/eh_frame_debug.h               |   67 +
 kernel/unwind/user.c                         |  142 +-
 mm/init-mm.c                                 |    2 +
 mm/mmap.c                                    |    5 +
 30 files changed, 2724 insertions(+), 41 deletions(-)
 create mode 100644 arch/s390/include/asm/unwind_user.h
 create mode 100644 arch/s390/include/asm/unwind_user_eh_frame.h
 create mode 100644 arch/x86/include/asm/unwind_user_eh_frame.h
 create mode 100644 include/asm-generic/unwind_user_eh_frame.h
 create mode 100644 include/linux/eh_frame.h
 create mode 100644 include/linux/unwind_user_eh_frame_types.h
 create mode 100644 include/uapi/linux/eh_frame.h
 create mode 100644 kernel/unwind/eh_frame.c
 create mode 100644 kernel/unwind/eh_frame.h
 create mode 100644 kernel/unwind/eh_frame_debug.h

base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f

Signature · 1 line
2.53.0
author
#1 lore raw

RFC PATCH v2 1/24 unwind_user: Add generic and arch-specific headers to MAINTAINERS

Commit 71753c6ed2bf ("unwind_user: Add user space unwinding API with frame pointer support") introduced include/asm-generic/unwind_user.h without adding it to MAINTAINERS, as well as any future arch-specific versions such as the one added by commit 49cf34c0815f ("unwind_user/x86: Enable frame pointer unwinding on x86") which introduced arch/x86/include/asm/unwind_user.h.

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 MAINTAINERS | 2 ++
 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
index 8014b9f8253e..e5738a250d63 100644
@@ -28201,6 +28201,8 @@ USERSPACE STACK UNWINDING
 M:	Josh Poimboeuf <[email protected]>
 M:	Steven Rostedt <[email protected]>
 S:	Maintained
+F:	arch/*/include/asm/unwind_user.h
+F:	include/asm-generic/unwind_user.h
 F:	include/linux/unwind*.h
 F:	kernel/unwind/
 
Signature · 1 line
2.53.0
author
#2 lore raw

RFC PATCH v2 2/24 unwind_user: Stop when reaching an outermost frame

In reply to Jens Remus

Add an indication for an outermost frame to the unwind user frame structure and stop unwinding when reaching an outermost frame.

This will be used by unwind user sframe, as SFrame may represent an undefined return address as indication for an outermost frame.

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/unwind_user.h | 6 ++++--
 include/linux/unwind_user_types.h  | 1 +
 kernel/unwind/user.c               | 6 ++++++
 3 files changed, 11 insertions(+), 2 deletions(-)
arch/x86/include/asm/unwind_user.h +4 -2
index 6e469044e4de..2dfb5ef11e36 100644
@@ -23,13 +23,15 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 	.cfa_off	=  2*(ws),			\
 	.ra_off		= -1*(ws),			\
 	.fp_off		= -2*(ws),			\
-	.use_fp		= true,
+	.use_fp		= true,				\
+	.outermost	= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
 	.cfa_off	=  1*(ws),			\
 	.ra_off		= -1*(ws),			\
 	.fp_off		= 0,				\
-	.use_fp		= false,
+	.use_fp		= false,			\
+	.outermost	= false,
 
 static inline bool unwind_user_at_function_start(struct pt_regs *regs)
 {
include/linux/unwind_user_types.h +1 -0
index 412729a269bc..4fc3dd9b4e29 100644
@@ -30,6 +30,7 @@ struct unwind_user_frame {
 	s32 ra_off;
 	s32 fp_off;
 	bool use_fp;
+	bool outermost;
 };
 
 struct unwind_user_state {
kernel/unwind/user.c +6 -0
index 90ab3c1a205e..28091ece830c 100644
@@ -31,6 +31,12 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 {
 	unsigned long cfa, fp, ra;
 
+	/* Stop unwinding when reaching an outermost frame. */
+	if (frame->outermost) {
+		state->done = true;
+		return 0;
+	}
+
 	/* Get the Canonical Frame Address (CFA) */
 	if (frame->use_fp) {
 		if (state->fp < state->sp)
Signature · 1 line
2.53.0
#3 lore raw

Re: [RFC PATCH v2 02/24] unwind_user: Stop when reaching an outermost frame

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Add an indication for an outermost frame to the unwind user frame structure and stop unwinding when reaching an outermost frame.

This will be used by unwind user sframe, as SFrame may represent an undefined return address as indication for an outermost frame.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

RFC PATCH v2 3/24 unwind_user: Enable archs that pass RA in a register

In reply to Jens Remus

Not all architectures/ABIs pass the return address (RA) on the stack on function entry, like x86-64 does due to its CALL instruction pushing the RA onto the stack. Architectures/ABIs, such as s390, also do not require the RA to be saved on the stack in the function prologue. In particular, the RA may never be saved to the stack at all, such as in leaf functions. Unwinding must therefore not assume the presence of a RA saved on stack for the topmost frame.

Treat a RA offset from CFA of zero as indication that the RA is not saved (on the stack). For the topmost frame treat it as indication that the RA is in the link/RA register, such as on arm64 and s390, and obtain it from there. For non-topmost frames treat it as error, as the RA must be saved.

Additionally allow the SP to be unchanged in the topmost frame, for architectures where SP at function entry == SP at call site, such as arm64 and s390.

Note that treating a RA offset from CFA of zero as indication that the RA is not saved on the stack additionally allows for architectures, such as s390, where the frame pointer (FP) may be saved without the RA being saved as well. Provided that such architectures represent this in SFrame by encoding the "missing" RA offset using a padding RA offset with a value of zero.

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 include/linux/unwind_user.h | 10 ++++++++++
 kernel/unwind/user.c        | 20 ++++++++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)
include/linux/unwind_user.h +10 -0
index 64618618febd..941cef652435 100644
@@ -23,6 +23,16 @@ static inline bool unwind_user_at_function_start(struct pt_regs *regs)
 #define unwind_user_at_function_start unwind_user_at_function_start
 #endif
 
+#ifndef unwind_user_get_ra_reg
+static inline int unwind_user_get_ra_reg(unsigned long *val)
+{
+	pr_debug("%s (%d): %s() not implemented\n",
+		 current->comm, current->pid, __func__);
+	return -EINVAL;
+}
+#define unwind_user_get_ra_reg unwind_user_get_ra_reg
+#endif
+
 int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries);
 
 #endif /* _LINUX_UNWIND_USER_H */
kernel/unwind/user.c +16 -4
index 28091ece830c..f8a97d4167f0 100644
@@ -2,6 +2,9 @@
 /*
 * Generic interfaces for unwinding user space
 */
+
+#define pr_fmt(fmt)	"unwind_user: " fmt
+
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/sched/task_stack.h>
@@ -47,8 +50,12 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 	}
 	cfa += frame->cfa_off;
 
-	/* Make sure that stack is not going in wrong direction */
-	if (cfa <= state->sp)
+	/*
+	 * Make sure that stack is not going in wrong direction.  Allow SP
+	 * to be unchanged for the topmost frame, by subtracting topmost,
+	 * which is either 0 or 1.
+	 */
+	if (cfa <= state->sp - state->topmost)
 		return -EINVAL;
 
 	/* Make sure that the address is word aligned */
@@ -56,8 +63,13 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 		return -EINVAL;
 
 	/* Get the Return Address (RA) */
-	if (get_user_word(&ra, cfa, frame->ra_off, state->ws))
-		return -EINVAL;
+	if (frame->ra_off) {
+		if (get_user_word(&ra, cfa, frame->ra_off, state->ws))
+			return -EINVAL;
+	} else {
+		if (!state->topmost || unwind_user_get_ra_reg(&ra))
+			return -EINVAL;
+	}
 
 	/* Get the Frame Pointer (FP) */
 	if (frame->fp_off && get_user_word(&fp, cfa, frame->fp_off, state->ws))
Signature · 1 line
2.53.0
#5 lore raw

Re: [RFC PATCH v2 03/24] unwind_user: Enable archs that pass RA in a register

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 25 lines

Not all architectures/ABIs pass the return address (RA) on the stack on function entry, like x86-64 does due to its CALL instruction pushing the RA onto the stack. Architectures/ABIs, such as s390, also do not require the RA to be saved on the stack in the function prologue. In particular, the RA may never be saved to the stack at all, such as in leaf functions. Unwinding must therefore not assume the presence of a RA saved on stack for the topmost frame.

Treat a RA offset from CFA of zero as indication that the RA is not saved (on the stack). For the topmost frame treat it as indication that the RA is in the link/RA register, such as on arm64 and s390, and obtain it from there. For non-topmost frames treat it as error, as the RA must be saved.

Additionally allow the SP to be unchanged in the topmost frame, for architectures where SP at function entry == SP at call site, such as arm64 and s390.

Note that treating a RA offset from CFA of zero as indication that the RA is not saved on the stack additionally allows for architectures, such as s390, where the frame pointer (FP) may be saved without the RA being saved as well. Provided that such architectures represent this in SFrame by encoding the "missing" RA offset using a padding RA offset with a value of zero.

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#6 lore raw

RFC PATCH v2 4/24 unwind_user: Flexible FP/RA recovery rules

In reply to Jens Remus

To enable support for SFrame V3 flexible FDEs with a subsequent patch, add support for the following flexible frame pointer (FP) and return address (RA) recovery rules:

FP/RA = *(CFA + offset)
FP/RA = register + offset
FP/RA = *(register + offset)

Note that FP/RA recovery rules that use arbitrary register contents are only valid when in the topmost frame, as their contents are otherwise unknown.

This also enables unwinding of user space for architectures, such as s390, that may save the frame pointer (FP) and/or return address (RA) in other registers, for instance when in a leaf function.

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/unwind_user.h | 21 +++++++--
 include/linux/unwind_user.h        | 10 +++++
 include/linux/unwind_user_types.h  | 23 +++++++++-
 kernel/unwind/user.c               | 70 +++++++++++++++++++++++++++---
 4 files changed, 111 insertions(+), 13 deletions(-)
arch/x86/include/asm/unwind_user.h +17 -4
index 2dfb5ef11e36..9c3417be4283 100644
@@ -21,15 +21,26 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 
 #define ARCH_INIT_USER_FP_FRAME(ws)			\
 	.cfa_off	=  2*(ws),			\
-	.ra_off		= -1*(ws),			\
-	.fp_off		= -2*(ws),			\
+	.ra		= {				\
+		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.offset		= -1*(ws),		\
+			},				\
+	.fp		= {				\
+		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.offset		= -2*(ws),		\
+			},				\
 	.use_fp		= true,				\
 	.outermost	= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
 	.cfa_off	=  1*(ws),			\
-	.ra_off		= -1*(ws),			\
-	.fp_off		= 0,				\
+	.ra		= {				\
+		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.offset		= -1*(ws),		\
+			},				\
+	.fp		= {				\
+		.rule		= UNWIND_USER_RULE_RETAIN,\
+			},				\
 	.use_fp		= false,			\
 	.outermost	= false,
 
@@ -41,4 +52,6 @@ static inline bool unwind_user_at_function_start(struct pt_regs *regs)
 
 #endif /* CONFIG_HAVE_UNWIND_USER_FP */
 
+#include <asm-generic/unwind_user.h>
+
 #endif /* _ASM_X86_UNWIND_USER_H */
include/linux/unwind_user.h +10 -0
index 941cef652435..b2b1c2572fed 100644
@@ -33,6 +33,16 @@ static inline int unwind_user_get_ra_reg(unsigned long *val)
 #define unwind_user_get_ra_reg unwind_user_get_ra_reg
 #endif
 
+#ifndef unwind_user_get_reg
+static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
+{
+	pr_debug("%s (%d): %s(%u) not implemented\n",
+		 current->comm, current->pid, __func__, regnum);
+	return -EINVAL;
+}
+#define unwind_user_get_reg unwind_user_get_reg
+#endif
+
 int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries);
 
 #endif /* _LINUX_UNWIND_USER_H */
include/linux/unwind_user_types.h +21 -2
index 4fc3dd9b4e29..1b5db76e886a 100644
@@ -25,10 +25,29 @@ struct unwind_stacktrace {
 	unsigned long	*entries;
 };
 
+#define UNWIND_USER_RULE_DEREF			BIT(31)
+
+enum unwind_user_rule {
+	UNWIND_USER_RULE_RETAIN,		/* entity = entity */
+	UNWIND_USER_RULE_CFA_OFFSET,		/* entity = CFA + offset */
+	UNWIND_USER_RULE_REG_OFFSET,		/* entity = register + offset */
+	/* DEREF variants */
+	UNWIND_USER_RULE_CFA_OFFSET_DEREF =	/* entity = *(CFA + offset) */
+		UNWIND_USER_RULE_CFA_OFFSET | UNWIND_USER_RULE_DEREF,
+	UNWIND_USER_RULE_REG_OFFSET_DEREF =	/* entity = *(register + offset) */
+		UNWIND_USER_RULE_REG_OFFSET | UNWIND_USER_RULE_DEREF,
+};
+
+struct unwind_user_rule_data {
+	enum unwind_user_rule rule;
+	s32 offset;
+	unsigned int regnum;
+};
+
 struct unwind_user_frame {
 	s32 cfa_off;
-	s32 ra_off;
-	s32 fp_off;
+	struct unwind_user_rule_data ra;
+	struct unwind_user_rule_data fp;
 	bool use_fp;
 	bool outermost;
 };
kernel/unwind/user.c +63 -7

91 lines not on this page · raw message

Signature · 1 line
2.53.0
#7 lore raw

Re: [RFC PATCH v2 04/24] unwind_user: Flexible FP/RA recovery rules

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

To enable support for SFrame V3 flexible FDEs with a subsequent patch, add support for the following flexible frame pointer (FP) and return address (RA) recovery rules:

FP/RA = *(CFA + offset)
FP/RA = register + offset
FP/RA = *(register + offset)

Note that FP/RA recovery rules that use arbitrary register contents are only valid when in the topmost frame, as their contents are otherwise unknown.

Quoted 6 lines

This also enables unwinding of user space for architectures, such as s390, that may save the frame pointer (FP) and/or return address (RA) in other registers, for instance when in a leaf function.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#8 lore raw

RFC PATCH v2 5/24 unwind_user: Flexible CFA recovery rules

In reply to Jens Remus

To enable support for SFrame V3 flexible FDEs with a subsequent patch, add support for the following flexible Canonical Frame Address (CFA) recovery rules:

CFA = SP + offset
CFA = *(SP + offset)
CFA = FP + offset
CFA = *(FP + offset)
CFA = register + offset
CFA = *(register + offset)

Note that CFA recovery rules that use arbitrary register contents are only valid when in the topmost frame, as their contents are otherwise unknown.

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/unwind_user.h | 12 ++++++++----
 include/linux/unwind_user_types.h  | 22 ++++++++++++++++++++--
 kernel/unwind/user.c               | 24 ++++++++++++++++++++----
 3 files changed, 48 insertions(+), 10 deletions(-)
arch/x86/include/asm/unwind_user.h +8 -4
index 9c3417be4283..f38f7c5ff1de 100644
@@ -20,7 +20,10 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 #ifdef CONFIG_HAVE_UNWIND_USER_FP
 
 #define ARCH_INIT_USER_FP_FRAME(ws)			\
-	.cfa_off	=  2*(ws),			\
+	.cfa		= {				\
+		.rule		= UNWIND_USER_CFA_RULE_FP_OFFSET,\
+		.offset		=  2*(ws),		\
+			},				\
 	.ra		= {				\
 		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -1*(ws),		\
@@ -29,11 +32,13 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -2*(ws),		\
 			},				\
-	.use_fp		= true,				\
 	.outermost	= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
-	.cfa_off	=  1*(ws),			\
+	.cfa		= {				\
+		.rule		= UNWIND_USER_CFA_RULE_SP_OFFSET,\
+		.offset		=  1*(ws),		\
+			},				\
 	.ra		= {				\
 		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -1*(ws),		\
@@ -41,7 +46,6 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 	.fp		= {				\
 		.rule		= UNWIND_USER_RULE_RETAIN,\
 			},				\
-	.use_fp		= false,			\
 	.outermost	= false,
 
 static inline bool unwind_user_at_function_start(struct pt_regs *regs)
include/linux/unwind_user_types.h +20 -2
index 1b5db76e886a..670ac860ae76 100644
@@ -27,6 +27,25 @@ struct unwind_stacktrace {
 
 #define UNWIND_USER_RULE_DEREF			BIT(31)
 
+enum unwind_user_cfa_rule {
+	UNWIND_USER_CFA_RULE_SP_OFFSET,		/* CFA = SP + offset */
+	UNWIND_USER_CFA_RULE_FP_OFFSET,		/* CFA = FP + offset */
+	UNWIND_USER_CFA_RULE_REG_OFFSET,	/* CFA = register + offset */
+	/* DEREF variants */
+	UNWIND_USER_CFA_RULE_SP_OFFSET_DEREF =	/* CFA = *(SP + offset) */
+		UNWIND_USER_CFA_RULE_SP_OFFSET | UNWIND_USER_RULE_DEREF,
+	UNWIND_USER_CFA_RULE_FP_OFFSET_DEREF =	/* CFA = *(FP + offset) */
+		UNWIND_USER_CFA_RULE_FP_OFFSET | UNWIND_USER_RULE_DEREF,
+	UNWIND_USER_CFA_RULE_REG_OFFSET_DEREF =	/* CFA = *(register + offset) */
+		UNWIND_USER_CFA_RULE_REG_OFFSET | UNWIND_USER_RULE_DEREF,
+};
+
+struct unwind_user_cfa_rule_data {
+	enum unwind_user_cfa_rule rule;
+	s32 offset;
+	unsigned int regnum;
+};
+
 enum unwind_user_rule {
 	UNWIND_USER_RULE_RETAIN,		/* entity = entity */
 	UNWIND_USER_RULE_CFA_OFFSET,		/* entity = CFA + offset */
@@ -45,10 +64,9 @@ struct unwind_user_rule_data {
 };
 
 struct unwind_user_frame {
-	s32 cfa_off;
+	struct unwind_user_cfa_rule_data cfa;
 	struct unwind_user_rule_data ra;
 	struct unwind_user_rule_data fp;
-	bool use_fp;
 	bool outermost;
 };
 
kernel/unwind/user.c +20 -4
index f5d68e86d72b..9df5040df9b3 100644
@@ -52,14 +52,30 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 	}
 
 	/* Get the Canonical Frame Address (CFA) */
-	if (frame->use_fp) {
+	switch (frame->cfa.rule) {
+	case UNWIND_USER_CFA_RULE_SP_OFFSET:
+	case UNWIND_USER_CFA_RULE_SP_OFFSET_DEREF:
+		cfa = state->sp;
+		break;
+	case UNWIND_USER_CFA_RULE_FP_OFFSET:
+	case UNWIND_USER_CFA_RULE_FP_OFFSET_DEREF:
 		if (state->fp < state->sp)
 			return -EINVAL;
 		cfa = state->fp;
-	} else {
-		cfa = state->sp;
+		break;
+	case UNWIND_USER_CFA_RULE_REG_OFFSET:
+	case UNWIND_USER_CFA_RULE_REG_OFFSET_DEREF:
+		if (!state->topmost || unwind_user_get_reg(&cfa, frame->cfa.regnum))
+			return -EINVAL;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		return -EINVAL;
 	}
-	cfa += frame->cfa_off;
+	cfa += frame->cfa.offset;
+	if (frame->cfa.rule & UNWIND_USER_RULE_DEREF &&
+	    get_user_word(&cfa, cfa, 0, state->ws))
+		return -EINVAL;
 
 	/*
 	 * Make sure that stack is not going in wrong direction.  Allow SP
Signature · 1 line
2.53.0
#9 lore raw

Re: [RFC PATCH v2 05/24] unwind_user: Flexible CFA recovery rules

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

To enable support for SFrame V3 flexible FDEs with a subsequent patch, add support for the following flexible Canonical Frame Address (CFA) recovery rules:

CFA = SP + offset
CFA = *(SP + offset)
CFA = FP + offset
CFA = *(FP + offset)
CFA = register + offset
CFA = *(register + offset)

Quoted 6 lines

Note that CFA recovery rules that use arbitrary register contents are only valid when in the topmost frame, as their contents are otherwise unknown.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#10 lore raw

RFC PATCH v2 6/24 unwind_user: Enable archs that define CFA = SP_callsite + offset

In reply to Jens Remus

Most architectures define their CFA as the value of the stack pointer (SP) at the call site in the previous frame, as suggested by the DWARF standard. Therefore the SP at call site can be unwound using an implicitly assumed value offset from CFA rule with an offset of zero:

.cfi_val_offset <SP>, 0

As a result the SP at call site computes as follows:

SP = CFA

Enable unwinding of user space for architectures, such as s390, which define their CFA as the value of the SP at the call site in the previous frame with an offset. Do so by enabling architectures to override the default SP value offset from CFA of zero with an architecture-specific one:

.cfi_val_offset <SP>, offset

So that the SP at call site computes as follows:

SP = CFA + offset

---

Notes (jremus):
Cherry-picked from "[PATCH v4 00/12] s390: SFrame user space unwinding" series and adjusted to .eh_frame:
https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/unwind_user.h |  2 ++
 include/linux/unwind_user_types.h  |  1 +
 kernel/unwind/user.c               | 11 ++++++-----
 3 files changed, 9 insertions(+), 5 deletions(-)
arch/x86/include/asm/unwind_user.h +2 -0
index f38f7c5ff1de..c96645c824d1 100644
@@ -32,6 +32,7 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -2*(ws),		\
 			},				\
+	.sp_off		= 0,				\
 	.outermost	= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
@@ -46,6 +47,7 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 	.fp		= {				\
 		.rule		= UNWIND_USER_RULE_RETAIN,\
 			},				\
+	.sp_off		= 0,				\
 	.outermost	= false,
 
 static inline bool unwind_user_at_function_start(struct pt_regs *regs)
include/linux/unwind_user_types.h +1 -0
index 670ac860ae76..4da058096259 100644
@@ -67,6 +67,7 @@ struct unwind_user_frame {
 	struct unwind_user_cfa_rule_data cfa;
 	struct unwind_user_rule_data ra;
 	struct unwind_user_rule_data fp;
+	s32 sp_off;
 	bool outermost;
 };
 
kernel/unwind/user.c +6 -5
index 9df5040df9b3..830c620fe453 100644
@@ -43,7 +43,7 @@ get_user_word(unsigned long *word, unsigned long base, int off, unsigned int ws)
 static int unwind_user_next_common(struct unwind_user_state *state,
 				   const struct unwind_user_frame *frame)
 {
-	unsigned long cfa, fp, ra;
+	unsigned long cfa, sp, fp, ra;
 
 	/* Stop unwinding when reaching an outermost frame. */
 	if (frame->outermost) {
@@ -77,16 +77,17 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 	    get_user_word(&cfa, cfa, 0, state->ws))
 		return -EINVAL;
 
+	/* Get the Stack Pointer (SP) */
+	sp = cfa + frame->sp_off;
 	/*
 	 * Make sure that stack is not going in wrong direction.  Allow SP
 	 * to be unchanged for the topmost frame, by subtracting topmost,
 	 * which is either 0 or 1.
 	 */
-	if (cfa <= state->sp - state->topmost)
+	if (sp <= state->sp - state->topmost)
 		return -EINVAL;
-
 	/* Make sure that the address is word aligned */
-	if (cfa & (state->ws - 1))
+	if (sp & (state->ws - 1))
 		return -EINVAL;
 
 	/* Get the Return Address (RA) */
@@ -149,7 +150,7 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 		return -EINVAL;
 
 	state->ip = ra;
-	state->sp = cfa;
+	state->sp = sp;
 	state->fp = fp;
 	state->topmost = false;
 	return 0;
Signature · 1 line
2.53.0
#11 lore raw

Re: [RFC PATCH v2 06/24] unwind_user: Enable archs that define CFA = SP_callsite + offset

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Most architectures define their CFA as the value of the stack pointer (SP) at the call site in the previous frame, as suggested by the DWARF standard. Therefore the SP at call site can be unwound using an implicitly assumed value offset from CFA rule with an offset of zero:

.cfi_val_offset <SP>, 0

As a result the SP at call site computes as follows:

SP = CFA

Enable unwinding of user space for architectures, such as s390, which define their CFA as the value of the SP at the call site in the previous frame with an offset. Do so by enabling architectures to override the default SP value offset from CFA of zero with an architecture-specific one:

Quoted 7 lines

.cfi_val_offset <SP>, offset

So that the SP at call site computes as follows:

SP = CFA + offset

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author
#12 lore raw

RFC PATCH v2 7/24 unwind_user/eh_frame: Add support for reading .eh_frame_hdr section

In reply to Jens Remus

In preparation for unwinding user space stacks with .eh_frame, add basic unwind user eh_frame infrastructure and support for reading the .eh_frame_hdr section.

The .eh_frame_hdr section provides a binary search table for efficient lookup of Frame Description Entries (FDEs) in the .eh_frame section based on instruction pointer (IP).

eh_frame_add_section() reads the .eh_frame_hdr section and unconditionally returns an error, so it is not very useful yet. A subsequent patch will improve that.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---

Notes (jremus):
Changes in RFC v2:
- read_encoded_pointer(): Enable DW_EH_PE_absptr in combination with relative application flags (e.g. DW_EH_PE_pcrel). (Sashiko AI)
- eh_frame_read_header(): Fix binary search table bounds check. (Sashiko AI)
- UNSAFE_GET_USER_INC(): Improve overflow check.

 MAINTAINERS              |   1 +
 arch/Kconfig             |   3 +
 include/linux/eh_frame.h |  45 +++++
 kernel/unwind/Makefile   |   3 +-
 kernel/unwind/eh_frame.c | 366 +++++++++++++++++++++++++++++++++++++++
 kernel/unwind/eh_frame.h |  30 ++++
 6 files changed, 447 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/eh_frame.h
 create mode 100644 kernel/unwind/eh_frame.c
 create mode 100644 kernel/unwind/eh_frame.h
MAINTAINERS +1 -0
index e5738a250d63..f19f5bb87f00 100644
@@ -28203,6 +28203,7 @@ M:	Steven Rostedt <[email protected]>
 S:	Maintained
 F:	arch/*/include/asm/unwind_user.h
 F:	include/asm-generic/unwind_user.h
+F:	include/linux/eh_frame.h
 F:	include/linux/unwind*.h
 F:	kernel/unwind/
 
arch/Kconfig +3 -0
index fa7507ac8e13..60542d5e5731 100644
@@ -486,6 +486,9 @@ config HAVE_HARDLOCKUP_DETECTOR_ARCH
 config UNWIND_USER
 	bool
 
+config HAVE_UNWIND_USER_EH_FRAME
+	bool
+
 config HAVE_UNWIND_USER_FP
 	bool
 	select UNWIND_USER
include/linux/eh_frame.h new file +45 -0

45 lines not on this page · raw message

kernel/unwind/Makefile +2 -1
index eae37bea54fd..2d97e2625d8a 100644
@@ -1 +1,2 @@
- obj-$(CONFIG_UNWIND_USER)	+= user.o deferred.o
+ obj-$(CONFIG_UNWIND_USER)			+= user.o deferred.o
+ obj-$(CONFIG_HAVE_UNWIND_USER_EH_FRAME)	+= eh_frame.o
kernel/unwind/eh_frame.c new file +366 -0

366 lines not on this page · raw message

kernel/unwind/eh_frame.h new file +30 -0
new file mode 100644
index 000000000000..77eda5376dfb
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _EH_FRAME_H
+#define _EH_FRAME_H
+
+/* DWARF exception header pointer encodings */
+#define DW_EH_PE_omit			0xff
+/* Formats */
+#define DW_EH_PE_absptr			0x00
+#define DW_EH_PE_uleb128		0x01
+#define DW_EH_PE_udata2			0x02
+#define DW_EH_PE_udata4			0x03
+#define DW_EH_PE_udata8			0x04
+#define DW_EH_PE_sleb128		0x09
+#define DW_EH_PE_sdata2			0x0a
+#define DW_EH_PE_sdata4			0x0b
+#define DW_EH_PE_sdata8			0x0c
+/* Applications */
+#define DW_EH_PE_pcrel			0x10
+#define DW_EH_PE_textrel		0x20
+#define DW_EH_PE_datarel		0x30
+#define DW_EH_PE_funcrel		0x40
+#define DW_EH_PE_aligned		0x50
+/* Flags */
+#define DW_EH_PE_indirect		0x80
+
+/* Helpers for DWARF exception header pointer encodings */
+#define DW_EH_PE_format(encoding)	((encoding) & 0x0f)
+#define DW_EH_PE_application(encoding)	((encoding) & 0x70)
+
+#endif /* _EH_FRAME_H */
Signature · 1 line
2.53.0
#13 lore raw

Re: [RFC PATCH v2 07/24] unwind_user/eh_frame: Add support for reading .eh_frame_hdr section

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Incorrect pointer size assumption for 32-bit compat tasks using DW_EH_PE_absptr encoding. - [High]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Incorrect pointer size assumption for 32-bit compat tasks using DW_EH_PE_absptr encoding.
- [High] Bypassing speculative execution pointer masking by using an unmasked pointer within a scoped_user_read_access_size block.
- [Low] Mixing goto-based cleanup labels and scoped_guard declarations in the same function.
--

--- Patch [7]: [RFC PATCH v2 07/24] unwind_user/eh_frame: Add support for reading .eh_frame_hdr section --- commit d29ad7450daa52f7e2ed07991186d7ccf95beadb
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Add support for reading .eh_frame_hdr section

In preparation for unwinding user space stacks with .eh_frame, add basic unwind user eh_frame infrastructure and support for reading the .eh_frame_hdr section.

Quoted 6 lines
kernel/unwind/eh_frame.c new file +0 -0
new file mode 100644
index 0000000000000..f25a0923e315b
@@ -0,0 +1,366 @@

[ ... ]

kernel/unwind/eh_frame.c line 92

Quoted 7 lines
kernel/unwind/eh_frame.c new file +7 -0
+static __always_inline int encoded_pointer_size(u8 encoding)
+{
+	u8 format = DW_EH_PE_format(encoding);
+
+	switch (format) {
+	case DW_EH_PE_absptr:
+		return sizeof(unsigned long);

[Severity: High]
Does hardcoding sizeof(unsigned long) here cause issues for 32-bit compat tasks?

If a 64-bit kernel is profiling a 32-bit application, DW_EH_PE_absptr is expected to match the application's native 4-byte pointer size. Using sizeof(unsigned long) means the kernel will read 8 bytes, which could consume too much data and misalign the current pointer for the remainder of the unwinding process.

Quoted 3 lines
kernel/unwind/eh_frame.c new file +3 -0
+	case DW_EH_PE_udata2:
+	case DW_EH_PE_sdata2:
+		return 2;

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c new file +6 -0
+static __always_inline int read_encoded_pointer(struct eh_frame_section *sec,
+						unsigned long *addr,
+						unsigned long end,
+						u8 encoding,
+						unsigned long *value)
+{

[ ... ]

kernel/unwind/eh_frame.c line 155

Quoted 8 lines
kernel/unwind/eh_frame.c new file +8 -0
+	/* Read value based on format */
+	switch (format) {
+	case DW_EH_PE_absptr: {
+		unsigned long tmp;
+		UNSAFE_GET_USER_INC(tmp, cur, end, Efault);
+		result += tmp;
+		break;
+	}

[Severity: High]
Will this also read 8 bytes instead of 4 for a 32-bit task?

Similar to encoded_pointer_size(), this reads a full unsigned long for an absolute pointer. If this is a 64-bit kernel unwinding a 32-bit compat application, it seems this might read past the intended data boundaries and corrupt the address parsing.

Quoted 2 lines
kernel/unwind/eh_frame.c new file +2 -0
+	case DW_EH_PE_uleb128: {
+		unsigned long tmp;

[ ... ]

Quoted 5 lines
kernel/unwind/eh_frame.c new file +5 -0
+static int eh_frame_read_header(struct eh_frame_section *sec)
+{
+	struct mm_struct *mm = current->mm;
+	void __user *eh_frame_hdr = (void __user *)sec->eh_frame_hdr_start;
+	unsigned long cur = sec->eh_frame_hdr_start, end = sec->eh_frame_hdr_end;

[ ... ]

kernel/unwind/eh_frame.c line 249

Quoted 4 lines
kernel/unwind/eh_frame.c new file +4 -0
+	scoped_user_read_access_size(eh_frame_hdr, end - sec->eh_frame_hdr_start,
+				     Efault) {
+		/* Read version */
+		UNSAFE_GET_USER_INC(version, cur, end, Efault);

[Severity: High]
Does this bypass the speculative execution pointer masking provided by scoped_user_read_access_size()?

The block masks the eh_frame_hdr pointer, but inside the block, UNSAFE_GET_USER_INC() reads from the unmasked unsigned long cur variable:

#define UNSAFE_GET_USER_INC(to, from, end, label)
...

	unsafe_get_user(__to, (typeof(to) __user *)from, label);

Since cur is passed to unsafe_get_user() without being masked, it appears this defeats the Spectre V1 mitigation for architectures relying on can_do_masked_user_access().

Quoted 2 lines
kernel/unwind/eh_frame.c new file +2 -0
+		if (version != 1)
+			return -EINVAL;

[ ... ]

Quoted 8 lines
kernel/unwind/eh_frame.c new file +8 -0
+int eh_frame_add_section(unsigned long eh_frame_hdr_start,
+			 unsigned long eh_frame_hdr_end,
+			 unsigned long text_start,
+			 unsigned long text_end)
+{
+	struct mm_struct *mm = current->mm;
+	struct eh_frame_section *sec;
+	int ret;

[ ... ]

Quoted 4 lines
kernel/unwind/eh_frame.c new file +4 -0
+	scoped_guard(mmap_read_lock, mm) {
+		struct vm_area_struct *eh_frame_hdr_vma, *text_vma;
+
+		eh_frame_hdr_vma = vma_lookup(mm, eh_frame_hdr_start);

[ ... ]

kernel/unwind/eh_frame.c line 361

Quoted 11 lines
kernel/unwind/eh_frame.c new file +11 -0
+	ret = eh_frame_read_header(sec);
+	if (ret)
+		goto err_free;
+
+	/* TODO nowhere to store it yet - just free it and return an error */
+	ret = -ENOSYS;
+
+err_free:
+	free_section(sec);
+	return ret;
+}

[Severity: Low]
This isn't a bug, but should this function avoid mixing scoped_guard() and goto-based error handling?

The kernel cleanup guidelines recommend either fully converting to scope-based cleanup (for example, using __free() for the sec allocation) or strictly using goto-based cleanups without scoped_guard(). Mixing them in the same function can create confusing ownership semantics.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#14 lore raw

Re: [RFC PATCH v2 07/24] unwind_user/eh_frame: Add support for reading .eh_frame_hdr section

Quoted 5 lines · On 8/21/2026 10:06 PM, [email protected] wrote:

In preparation for unwinding user space stacks with .eh_frame, add basic unwind user eh_frame infrastructure and support for reading the .eh_frame_hdr section.

kernel/unwind/eh_frame.c

Quoted 1 line
kernel/unwind/eh_frame.c +0 -0

#include <linux/sched/task_stack.h>
#include <linux/unwind_user.h>

Quoted 10 lines
Quoted 1 line
kernel/unwind/eh_frame.c +0 -0
@@ -0,0 +1,366 @@

[ ... ]

kernel/unwind/eh_frame.c line 91

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+static __always_inline int encoded_pointer_size(u8 encoding)
+{
+	u8 format = DW_EH_PE_format(encoding);
+
+	switch (format) {
+	case DW_EH_PE_absptr:
		if (IS_ENABLED(CONFIG_COMPAT))
			return unwind_user_word_size(task_pt_regs(current));
Quoted 5 lines

kernel/unwind/eh_frame.c line 92

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+		return sizeof(unsigned long);

[Severity: High]
Does hardcoding sizeof(unsigned long) here cause issues for 32-bit compat tasks?

The word size from struct unwind_user_state field word_size cannot be used because it needs to be known at .eh_frame[_hdr] section registration time:

eh_frame_add_section() -> eh_frame_read_header() -> read_encoded_pointer()

Would it be ok to determine the word size each time the special case DW_EH_PE_absptr is handled in encoded_pointer_size() and read_encoded_pointer()? See also below.

Also I need to do some testing whether those actually occur in .eh_frame. If not it would be preferable to drop DW_EH_PE_absptr support.

Quoted 22 lines

If a 64-bit kernel is profiling a 32-bit application, DW_EH_PE_absptr is expected to match the application's native 4-byte pointer size. Using sizeof(unsigned long) means the kernel will read 8 bytes, which could consume too much data and misalign the current pointer for the remainder of the unwinding process.

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+	case DW_EH_PE_udata2:
+	case DW_EH_PE_sdata2:
+		return 2;

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+static __always_inline int read_encoded_pointer(struct eh_frame_section *sec,
+						unsigned long *addr,
+						unsigned long end,
+						u8 encoding,
+						unsigned long *value)
+{

[ ... ]

Quoted 6 lines

kernel/unwind/eh_frame.c line 153

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+	/* Read value based on format */
+	switch (format) {
+	case DW_EH_PE_absptr: {
+		unsigned long tmp;
+		UNSAFE_GET_USER_INC(tmp, cur, end, Efault);
+		result += tmp;

Replace above with:

		if (IS_ENABLED(CONFIG_COMPAT) &&
		    unwind_user_word_size(task_pt_regs(current)) == sizeof(int)) {
			unsigned int tmp32;
			UNSAFE_GET_USER_INC(tmp32, cur, end, Efault);
			result += tmp32;
		} else {
			unsigned long tmp;
			UNSAFE_GET_USER_INC(tmp, cur, end, Efault);
			result += tmp;
		}
Quoted 16 lines

kernel/unwind/eh_frame.c

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+		break;
+	}

[Severity: High]
Will this also read 8 bytes instead of 4 for a 32-bit task?

Similar to encoded_pointer_size(), this reads a full unsigned long for an absolute pointer. If this is a 64-bit kernel unwinding a 32-bit compat application, it seems this might read past the intended data boundaries and corrupt the address parsing.

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+	case DW_EH_PE_uleb128: {
+		unsigned long tmp;

[ ... ]

Quoted 4 lines

kernel/unwind/eh_frame.c line 232

Quoted 4 lines
kernel/unwind/eh_frame.c +4 -0
+static int eh_frame_read_header(struct eh_frame_section *sec)
+{
+	struct mm_struct *mm = current->mm;
+	void __user *eh_frame_hdr = (void __user *)sec->eh_frame_hdr_start;
	size_t size = sec->eh_frame_hdr_end - sec->eh_frame_hdr_start;
Quoted 1 line

kernel/unwind/eh_frame.c line 233

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+	unsigned long cur = sec->eh_frame_hdr_start, end = sec->eh_frame_hdr_end;

Move cur and end into scoped user access block below.

Quoted 5 lines

[ ... ]

kernel/unwind/eh_frame.c line 247

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+	scoped_user_read_access_size(eh_frame_hdr, end - sec->eh_frame_hdr_start,
+				     Efault) {
	scoped_user_read_access_size(eh_frame_hdr, size, Efault) {
		unsigned long cur = (unsigned long)eh_frame_hdr;
		unsigned long end = (unsigned long)eh_frame_hdr + size;
Quoted 2 lines

kernel/unwind/eh_frame.c line 249

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+		/* Read version */
+		UNSAFE_GET_USER_INC(version, cur, end, Efault);

Then whenever cur or end are expected to be unmasked pointers they need to be converted back:

- table_start = cur;
+ table_start = sec->eh_frame_hdr_start + (cur - (unsigned long)eh_frame_hdr);

...

- if (fde_count > (end - table_start) / entry_size)
+ if (fde_count > (sec->eh_frame_hdr_end - table_start) / entry_size)

			return -EINVAL;
Quoted 4 lines

[Severity: High]
Does this bypass the speculative execution pointer masking provided by scoped_user_read_access_size()?

True. I was not aware of that. While above seems to resolve this at a first glance there are some implications in read_encoded_pointer():

DW_EH_PE_datarel (relative to .eh_frame_hdr) and DW_EH_PE_textrel (relative to .text) return an unmasked pointer, which is ok, as the callers expect it to be an unmasked value.

DW_EH_PE_pcrel (relative to the field itself) also returns an unmasked pointer and now requires the unmasked cursor pointer to be provided as input as well.

Quoted 49 lines

The block masks the eh_frame_hdr pointer, but inside the block, UNSAFE_GET_USER_INC() reads from the unmasked unsigned long cur variable:

#define UNSAFE_GET_USER_INC(to, from, end, label)
...

	unsafe_get_user(__to, (typeof(to) __user *)from, label);

Since cur is passed to unsafe_get_user() without being masked, it appears this defeats the Spectre V1 mitigation for architectures relying on can_do_masked_user_access().

Quoted 11 lines
kernel/unwind/eh_frame.c +10 -0
+		if (version != 1)
+			return -EINVAL;
 
+int eh_frame_add_section(unsigned long eh_frame_hdr_start,
+			 unsigned long eh_frame_hdr_end,
+			 unsigned long text_start,
+			 unsigned long text_end)
+{
+	struct mm_struct *mm = current->mm;
+	struct eh_frame_section *sec;
+	int ret;

[ ... ]

Quoted 4 lines
kernel/unwind/eh_frame.c +4 -0
+	scoped_guard(mmap_read_lock, mm) {
+		struct vm_area_struct *eh_frame_hdr_vma, *text_vma;
+
+		eh_frame_hdr_vma = vma_lookup(mm, eh_frame_hdr_start);

[ ... ]

kernel/unwind/eh_frame.c line 361

Quoted 11 lines
kernel/unwind/eh_frame.c +11 -0
+	ret = eh_frame_read_header(sec);
+	if (ret)
+		goto err_free;
+
+	/* TODO nowhere to store it yet - just free it and return an error */
+	ret = -ENOSYS;
+
+err_free:
+	free_section(sec);
+	return ret;
+}

[Severity: Low]
This isn't a bug, but should this function avoid mixing scoped_guard() and goto-based error handling?

Quoted 4 lines

The kernel cleanup guidelines recommend either fully converting to scope-based cleanup (for example, using __free() for the sec allocation) or strictly using goto-based cleanups without scoped_guard(). Mixing them in the same function can create confusing ownership semantics.

Other than converting the code to use mmap_read_lock(mm) and mmap_read_unlock(mm) I don't see how the section cleanup logic could be converted to scope-based cleanup, as it must not be freed if it gets inserted into the mm->eh_frame_mt (in a subsequent patch). Sashiko probably misses this as in this incomplete state it looks as if it could be converted, as the section is always freed.

Thanks and regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#15 lore raw

RFC PATCH v2 8/24 unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple tree

In reply to Jens Remus

Associate .eh_frame_hdr sections with their mm by adding them to a per-mm maple tree which is indexed by the corresponding text address range. A single .eh_frame_hdr section can be associated with multiple text ranges.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---

Notes (jremus):
Changes in RFC v2:
- INIT_MM_EH_FRAME and mm_init_eh_frame(): Use MT_FLAGS_USE_RCU to enable concurrent eh_frame_mt read and write access. (Sashiko AI)
- eh_frame_remove_section(): Fix ABA problem by using the advanced maple tree API to hold the internal spinlock while iterating over the eh_frame_mt and removing the eh_frame section(s). (Sashiko AI)

 arch/x86/include/asm/mmu.h |  2 +-
 include/linux/eh_frame.h   | 22 ++++++++++++
 include/linux/mm_types.h   |  3 ++
 kernel/fork.c              | 10 ++++++
 kernel/unwind/eh_frame.c   | 70 ++++++++++++++++++++++++++++++++++++--
 mm/init-mm.c               |  2 ++
 6 files changed, 105 insertions(+), 4 deletions(-)
arch/x86/include/asm/mmu.h +1 -1
index 0fe9c569d171..227a32899a59 100644
@@ -87,7 +87,7 @@ typedef struct {
 	.context = {							\
 		.ctx_id = 1,						\
 		.lock = __MUTEX_INITIALIZER(mm.context.lock),		\
-	}
+	},
 
 void leave_mm(void);
 #define leave_mm leave_mm
include/linux/eh_frame.h +22 -0
index aaac2dd663d5..c4279ee77c13 100644
@@ -2,9 +2,14 @@
 #ifndef _LINUX_EH_FRAME_H
 #define _LINUX_EH_FRAME_H
 
+#include <linux/mm_types.h>
+#include <linux/srcu.h>
+
 #ifdef CONFIG_HAVE_UNWIND_USER_EH_FRAME
 
 struct eh_frame_section {
+	struct rcu_head	rcu;
+
 	unsigned long	eh_frame_hdr_start;
 	unsigned long	eh_frame_hdr_end;
 	unsigned long	text_start;
@@ -19,14 +24,29 @@ struct eh_frame_section {
 	u8		binary_search_table_enc;
 };
 
+#define EH_FRAME_MT_FLAGS (MT_FLAGS_USE_RCU)
+
+#define INIT_MM_EH_FRAME .eh_frame_mt = MTREE_INIT(eh_frame_mt, EH_FRAME_MT_FLAGS),
+extern void eh_frame_free_mm(struct mm_struct *mm);
+
 extern int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 				unsigned long eh_frame_hdr_end,
 				unsigned long text_start,
 				unsigned long text_end);
 extern int eh_frame_remove_section(unsigned long eh_frame_hdr_start);
 
+static inline bool current_has_eh_frame(void)
+{
+	struct mm_struct *mm = current->mm;
+
+	return mm && !mtree_empty(&mm->eh_frame_mt);
+}
+
 #else /* !CONFIG_HAVE_UNWIND_USER_EH_FRAME */
 
+#define INIT_MM_EH_FRAME
+static inline void eh_frame_free_mm(struct mm_struct *mm) {}
+
 static inline int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 				       unsigned long eh_frame_hdr_end,
 				       unsigned long text_start,
@@ -40,6 +60,8 @@ static inline int eh_frame_remove_section(unsigned long eh_frame_hdr_start)
 	return -ENOSYS;
 }
 
+static inline bool current_has_eh_frame(void) { return false; }
+
 #endif /* CONFIG_HAVE_UNWIND_USER_EH_FRAME */
 
 #endif /* _LINUX_EH_FRAME_H */
include/linux/mm_types.h +3 -0
index b18c2b2e7d2c..ac51d6212e86 100644
@@ -1404,6 +1404,9 @@ struct mm_struct {
 #ifdef CONFIG_MM_ID
 		mm_id_t mm_id;
 #endif /* CONFIG_MM_ID */
+#ifdef CONFIG_HAVE_UNWIND_USER_EH_FRAME
+		struct maple_tree eh_frame_mt;
+#endif
 	} __randomize_layout;
 
 	/*
kernel/fork.c +10 -0
index f0e2e131a9a5..dab67d2eb096 100644
@@ -111,6 +111,7 @@
 #include <linux/tick.h>
 #include <linux/unwind_deferred.h>
 #include <linux/pgalloc.h>
+#include <linux/eh_frame.h>
 #include <linux/uaccess.h>
 
 #include <asm/mmu_context.h>
@@ -738,6 +739,7 @@ void __mmdrop(struct mm_struct *mm)
 	mm_pasid_drop(mm);
 	mm_destroy_cid(mm);
 	percpu_counter_destroy_many(mm->rss_stat, NR_MM_COUNTERS);
+	eh_frame_free_mm(mm);
 
 	free_mm(mm);
 }
@@ -1082,6 +1084,13 @@ static void mmap_init_lock(struct mm_struct *mm)
 #endif
 }
 
+static void mm_init_eh_frame(struct mm_struct *mm)
+{
+#ifdef CONFIG_HAVE_UNWIND_USER_EH_FRAME
+	mt_init_flags(&mm->eh_frame_mt, EH_FRAME_MT_FLAGS);
+#endif
+}
+
 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
 {
 	mt_init_flags(&mm->mm_mt, MM_MT_FLAGS);
@@ -1109,6 +1118,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
 	mm->pmd_huge_pte = NULL;
 #endif
 	mm_init_uprobes_state(mm);
+	mm_init_eh_frame(mm);
 	hugetlb_count_init(mm);
 	futex_mm_init(mm);
 
kernel/unwind/eh_frame.c +67 -3

94 lines not on this page · raw message

mm/init-mm.c +2 -0
index 3e792aad7626..25c26341fc4f 100644
@@ -11,6 +11,7 @@
 #include <linux/atomic.h>
 #include <linux/user_namespace.h>
 #include <linux/iommu.h>
+#include <linux/eh_frame.h>
 #include <asm/mmu.h>
 
 #ifndef INIT_MM_CONTEXT
@@ -48,6 +49,7 @@ struct mm_struct init_mm = {
 #endif
 	.flexible_array	= MM_STRUCT_FLEXIBLE_ARRAY_INIT,
 	INIT_MM_CONTEXT(init_mm)
+	INIT_MM_EH_FRAME
 };
 
 void setup_initial_init_mm(void *start_code, void *end_code,
Signature · 1 line
2.53.0
#16 lore raw

Re: [RFC PATCH v2 08/24] unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple tree

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] If mas_erase() fails due to an Out-Of-Memory (OOM) condition, it leaves the entry in the tree…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] If mas_erase() fails due to an Out-Of-Memory (OOM) condition, it leaves the entry in the tree but __eh_frame_remove_section() still frees it via call_srcu().
- [Medium] Mixing goto-based error handling with scope-based cleanup helpers in the same function.
- [Low] efi_mm is missing the INIT_MM_EH_FRAME initializer.
--

--- Patch [8]: [RFC PATCH v2 08/24] unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple tree --- commit 428d78732477ec23211b2b4732a9923dae762c55
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple tree

This commit associates .eh_frame_hdr sections with their mm_struct by adding them to a per-mm maple tree. The tree is indexed by the corresponding text address ranges.

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
index f25a0923e315b..8cf19c5298fde 100644

[ ... ]

Quoted 7 lines
kernel/unwind/eh_frame.c +1 -2
@@ -352,15 +355,76 @@ int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 	if (ret)
 		goto err_free;
 
-	/* TODO nowhere to store it yet - just free it and return an error */
-	ret = -ENOSYS;
+	ret = mtree_insert_range(eh_frame_mt, sec->text_start, sec->text_end - 1,

kernel/unwind/eh_frame.c int eh_frame_add_section(unsigned long eh_frame_hdr_start, · line 368

Quoted 10 lines
kernel/unwind/eh_frame.c +8 -0
+				 sec, GFP_KERNEL_ACCOUNT);
+	if (ret) {
+		dbg("mtree_insert_range failed: text=%lx-%lx\n",
+		    sec->text_start, sec->text_end);
+		goto err_free;
+	}
+
+	return 0;
 
 err_free:

[Severity: Medium]
Is it intentional to mix goto-based error handling with scope-based cleanup helpers in the same function?

The eh_frame_add_section function uses scoped_guard for the mmap lock but retains a goto err_free label for allocation cleanup on failure.

The cleanup subsystem guidelines suggest that a function should either convert all resources to scope-based cleanup or convert none of them to avoid confusing ownership semantics.

Quoted 3 lines
kernel/unwind/eh_frame.c +0 -0
 	free_section(sec);
 	return ret;
 }

[ ... ]

kernel/unwind/eh_frame.c int eh_frame_add_section(unsigned long eh_frame_hdr_start, · line 388

Quoted 9 lines
kernel/unwind/eh_frame.c +9 -0
+static int __eh_frame_remove_section(struct ma_state *mas,
+				     struct eh_frame_section *sec)
+{
+	if (mas_erase(mas) != sec) {
+		dbg("mas_erase failed: text=%lx\n", sec->text_start);
+		return -EINVAL;
+	}
+
+	call_srcu(&eh_frame_srcu, &sec->rcu, eh_frame_free_srcu);

[Severity: Critical]
Can this cause a use-after-free if the maple tree erase operation fails?

If mas_erase fails due to memory exhaustion (for example, during tree rebalancing), it leaves the entry in the tree, sets mas_is_err(mas), and returns the pointer to the entry.

Because it returns the entry pointer, the condition mas_erase(mas) != sec will evaluate to false, skipping the error block.

The code would then proceed to call call_srcu and free the section while it remains linked in the maple tree, potentially causing a crash when the tree is traversed later. Should this check mas_is_err(mas) instead?

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+
+	return 0;
+}

[ ... ]

Quoted 4 lines
mm/init-mm.c +0 -0
index 3e792aad76261..25c26341fc4fb 100644

[ ... ]

mm/init-mm.c struct mm_struct init_mm = { · line 53

Quoted 6 lines
mm/init-mm.c +1 -0
@@ -48,6 +49,7 @@ struct mm_struct init_mm = {
 #endif
 	.flexible_array	= MM_STRUCT_FLEXIBLE_ARRAY_INIT,
 	INIT_MM_CONTEXT(init_mm)
+	INIT_MM_EH_FRAME
 };

[Severity: Low]
Does efi_mm in drivers/firmware/efi/efi.c also need this initializer?

When a new initializer like INIT_MM_EH_FRAME is added to init_mm, it usually needs to be added to all static mm_struct definitions.

Leaving it out means the eh_frame_mt maple tree inside efi_mm remains zero-initialized rather than properly initialized with MTREE_INIT, which might lead to lock splats or crashes if accessed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author
#17 lore raw

Re: [RFC PATCH v2 08/24] unwind_user/eh_frame: Store .eh_frame_hdr section data in per-mm maple tree

Quoted 9 lines · On 8/21/2026 10:13 PM, [email protected] wrote:

This commit associates .eh_frame_hdr sections with their mm_struct by adding them to a per-mm maple tree. The tree is indexed by the corresponding text address ranges.

kernel/unwind/eh_frame.c int eh_frame_add_section(unsigned long eh_frame_hdr_start, · line 382

Quoted 5 lines
kernel/unwind/eh_frame.c +3 -0
 
+static int __eh_frame_remove_section(struct ma_state *mas,
+				     struct eh_frame_section *sec)
+{
	struct eh_frame_section *erased;

	erased = mas_erase(mas);
Quoted 1 line

kernel/unwind/eh_frame.c int eh_frame_add_section(unsigned long eh_frame_hdr_start, · line 383

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+	if (mas_erase(mas) != sec) {
	if (mas_is_err(mas) || erased != sec) {
Quoted 8 lines

kernel/unwind/eh_frame.c int eh_frame_add_section(unsigned long eh_frame_hdr_start, · line 388

Quoted 5 lines
kernel/unwind/eh_frame.c +5 -0
+		dbg("mas_erase failed: text=%lx\n", sec->text_start);
+		return -EINVAL;
+	}
+
+	call_srcu(&eh_frame_srcu, &sec->rcu, eh_frame_free_srcu);

[Severity: Critical]
Can this cause a use-after-free if the maple tree erase operation fails?

Seems valid. Fixed in next version.

Quoted 18 lines

If mas_erase fails due to memory exhaustion (for example, during tree rebalancing), it leaves the entry in the tree, sets mas_is_err(mas), and returns the pointer to the entry.

Because it returns the entry pointer, the condition mas_erase(mas) != sec will evaluate to false, skipping the error block.

The code would then proceed to call call_srcu and free the section while it remains linked in the maple tree, potentially causing a crash when the tree is traversed later. Should this check mas_is_err(mas) instead?

Quoted 5 lines
kernel/unwind/eh_frame.c +3 -0
+
+	return 0;
+}
mm/init-mm.c +0 -0
Quoted 9 lines

mm/init-mm.c struct mm_struct init_mm = { · line 53

Quoted 6 lines
mm/init-mm.c +1 -0
@@ -48,6 +49,7 @@ struct mm_struct init_mm = {
 #endif
 	.flexible_array	= MM_STRUCT_FLEXIBLE_ARRAY_INIT,
 	INIT_MM_CONTEXT(init_mm)
+	INIT_MM_EH_FRAME
 };

[Severity: Low]
Does efi_mm in drivers/firmware/efi/efi.c also need this initializer?

Steven, Josh, I doubt that is required. Do you agree?

Otherwise tboot_mm in arch/x86/kernel/tboot.c would be affected as well.

Quoted 7 lines

When a new initializer like INIT_MM_EH_FRAME is added to init_mm, it usually needs to be added to all static mm_struct definitions.

Leaving it out means the eh_frame_mt maple tree inside efi_mm remains zero-initialized rather than properly initialized with MTREE_INIT, which might lead to lock splats or crashes if accessed.

Thanks and regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#18 lore raw

RFC PATCH v2 9/24 unwind_user/eh_frame: Add support for reading .eh_frame section

In reply to Jens Remus

In preparation for using .eh_frame to unwind user space stacks, add an eh_frame_find() interface for finding the .eh_frame[_hdr] information associated with a given text address.

The implementation parses DWARF Call Frame Information (CFI) from the .eh_frame section, including:
- Common Information Entries (CIEs) that define default unwinding rules,
- Frame Description Entries (FDEs) that describe unwinding for specific function address ranges, and
- DWARF CFI instructions that specify how to restore registers.

For performance, use user_read_access_begin() and the corresponding unsafe_*() accessors. Note that use of pr_debug() in uaccess-enabled regions would break noinstr validation, so there aren't any debug messages yet. That will be added in a subsequent commit.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---

Notes (jremus):
Changes in RFC v2:
- Reduce maximum state stack depth to 1 (EH_FRAME_MAX_STATE_STACK).
- Limit FDE length to 32768 (EH_FRAME_MAX_FDE_LENGTH).
- Limit number of CFI instructions processed to 16.384 (EH_FRAME_CFI_INSN_LIMIT) to prevent an infinite loop, in case of a logic error that would enable malicious .eh_frame to make the cursor go backwards (e.g. due to an overflow). (Sashiko AI) [cc1plus FDE with 13.888 CFI insns]
- __read_fde(): Do not allow function-relative encoding when reading PC begin (function start address) and PC range (function size) to fix use of uninitialized function start address. (Sashiko AI)
- read_encoded_pointer(): Drop support for function-relative encoding, as it is no longer used.
- DW_CFA_offset: Read offset into unsigned long. (Sashiko AI)
- Add support for DW_CFA_restore_extended.
- DW_CFA_def_cfa_{offset[_sf]|register}: Reject if CFA rule is not CFA_REG_OFFSET to ensure the unchanged part of the rule is initialized. (Sashiko AI)
- Improve CIE/FDE augmentation data length overflow checks.
- __do_cfi_insn(): Use check_mul_overflow() to check for overflow in multiplications with CIE data alignment factor. (Sashiko AI)
- DW_CFA_restore[_extended]: Restore to CIE state. (Sashiko AI)

 MAINTAINERS                                |   2 +
 include/asm-generic/Kbuild                 |   1 +
 include/asm-generic/unwind_user_eh_frame.h |  51 ++
 include/linux/eh_frame.h                   |   7 +
 kernel/unwind/eh_frame.c                   | 988 ++++++++++++++++++++-
 kernel/unwind/eh_frame.h                   |  38 +
 kernel/unwind/eh_frame_debug.h             |  19 +
 7 files changed, 1103 insertions(+), 3 deletions(-)
 create mode 100644 include/asm-generic/unwind_user_eh_frame.h
 create mode 100644 kernel/unwind/eh_frame_debug.h
MAINTAINERS +2 -0
index f19f5bb87f00..d5f46f26996f 100644
@@ -28202,7 +28202,9 @@ M:	Josh Poimboeuf <[email protected]>
 M:	Steven Rostedt <[email protected]>
 S:	Maintained
 F:	arch/*/include/asm/unwind_user.h
+F:	arch/*/include/asm/unwind_user_eh_frame.h
 F:	include/asm-generic/unwind_user.h
+F:	include/asm-generic/unwind_user_eh_frame.h
 F:	include/linux/eh_frame.h
 F:	include/linux/unwind*.h
 F:	kernel/unwind/
include/asm-generic/Kbuild +1 -0
index 15df9dcb42a5..bd031eb6adc4 100644
@@ -62,6 +62,7 @@ mandatory-y += topology.h
 mandatory-y += trace_clock.h
 mandatory-y += uaccess.h
 mandatory-y += unwind_user.h
+mandatory-y += unwind_user_eh_frame.h
 mandatory-y += vermagic.h
 mandatory-y += vga.h
 mandatory-y += video.h
include/asm-generic/unwind_user_eh_frame.h new file +51 -0

51 lines not on this page · raw message

include/linux/eh_frame.h +7 -0
index c4279ee77c13..740316b19df3 100644
@@ -4,6 +4,7 @@
 
 #include <linux/mm_types.h>
 #include <linux/srcu.h>
+#include <linux/unwind_user_types.h>
 
 #ifdef CONFIG_HAVE_UNWIND_USER_EH_FRAME
 
@@ -34,6 +35,7 @@ extern int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 				unsigned long text_start,
 				unsigned long text_end);
 extern int eh_frame_remove_section(unsigned long eh_frame_hdr_start);
+extern int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame);
 
 static inline bool current_has_eh_frame(void)
 {
@@ -60,6 +62,11 @@ static inline int eh_frame_remove_section(unsigned long eh_frame_hdr_start)
 	return -ENOSYS;
 }
 
+static inline int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
+{
+	return -ENOSYS;
+}
+
 static inline bool current_has_eh_frame(void) { return false; }
 
 #endif /* CONFIG_HAVE_UNWIND_USER_EH_FRAME */
kernel/unwind/eh_frame.c +985 -3

1,005 lines not on this page · raw message

kernel/unwind/eh_frame.h +38 -0
index 77eda5376dfb..9a0c71102742 100644
@@ -2,6 +2,38 @@
 #ifndef _EH_FRAME_H
 #define _EH_FRAME_H
 
+/* DWARF CFI opcodes */
+#define DW_CFA_advance_loc		0x40
+#define DW_CFA_offset			0x80
+#define DW_CFA_restore			0xc0
+#define DW_CFA_nop			0x00
+#define DW_CFA_set_loc			0x01
+#define DW_CFA_advance_loc1		0x02
+#define DW_CFA_advance_loc2		0x03
+#define DW_CFA_advance_loc4		0x04
+#define DW_CFA_offset_extended		0x05
+#define DW_CFA_restore_extended		0x06
+#define DW_CFA_undefined		0x07
+#define DW_CFA_same_value		0x08
+#define DW_CFA_register			0x09
+#define DW_CFA_remember_state		0x0a
+#define DW_CFA_restore_state		0x0b
+#define DW_CFA_def_cfa			0x0c
+#define DW_CFA_def_cfa_register		0x0d
+#define DW_CFA_def_cfa_offset		0x0e
+#define DW_CFA_def_cfa_expression	0x0f
+#define DW_CFA_expression		0x10
+#define DW_CFA_offset_extended_sf	0x11
+#define DW_CFA_def_cfa_sf		0x12
+#define DW_CFA_def_cfa_offset_sf	0x13
+#define DW_CFA_val_offset		0x14
+#define DW_CFA_val_offset_sf		0x15
+#define DW_CFA_val_expression		0x16
+
+/* Helpers for CFI opcodes */
+#define DW_CFA_opcode(insn)		((insn) & 0xc0)
+#define DW_CFA_operand(insn)		((insn) & 0x3f)
+
 /* DWARF exception header pointer encodings */
 #define DW_EH_PE_omit			0xff
 /* Formats */
@@ -27,4 +59,10 @@
 #define DW_EH_PE_format(encoding)	((encoding) & 0x0f)
 #define DW_EH_PE_application(encoding)	((encoding) & 0x70)
 
+/* CIE/FDE constants */
+#define EH_FRAME_CIE_ID			0
+#define EH_FRAME_DWARF64_LENGTH		0xffffffff
+#define EH_FRAME_CIE_MIN_LENGTH		13
+#define EH_FRAME_FDE_MIN_LENGTH		10
+
 #endif /* _EH_FRAME_H */
kernel/unwind/eh_frame_debug.h new file +19 -0
new file mode 100644
index 000000000000..5a3e4e7f065a
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _EH_FRAME_DEBUG_H
+#define _EH_FRAME_DEBUG_H
+
+#include <linux/eh_frame.h>
+#include "eh_frame.h"
+
+#ifdef CONFIG_DYNAMIC_DEBUG
+
+#define dbg(fmt, ...)							\
+	pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__)
+
+#else /* !CONFIG_DYNAMIC_DEBUG */
+
+#define dbg(args...)			no_printk(args)
+
+#endif /* !CONFIG_DYNAMIC_DEBUG */
+
+#endif /* _EH_FRAME_DEBUG_H */
Signature · 1 line
2.53.0
#19 lore raw

Re: [RFC PATCH v2 09/24] unwind_user/eh_frame: Add support for reading .eh_frame section

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] DW_CFA_restore and DW_CFA_restore_extended fail to restore the complete register rule state…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] DW_CFA_restore and DW_CFA_restore_extended fail to restore the complete register rule state (offset and register number).
- [High] Bypass of pointer masking mitigation inside scoped_user_read_access_size blocks leading to a Spectre V1 vulnerability.
- [Medium] Integer overflow during instruction pointer advancement in DW_CFA_advance_loc4.
--

--- Patch [9]: [RFC PATCH v2 09/24] unwind_user/eh_frame: Add support for reading .eh_frame section --- commit cd03be3d9017de830eb9301ff0da6dd6d08e69be
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Add support for reading .eh_frame section

In preparation for using .eh_frame to unwind user space stacks, add an eh_frame_find() interface for finding the .eh_frame[_hdr] information associated with a given text address.

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
index 8cf19c5298fde..21f40805b417f 100644

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+static __always_inline int __read_cie(struct eh_frame_section *sec,
+				      unsigned long cie_addr,
+				      struct eh_frame_cie *cie)
+{
+	void __user *cie_ptr = (void __user *)cie_addr;
+	unsigned long cur = cie_addr, end;

[ ... ]

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 328

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+	scoped_user_read_access_size(cie_ptr, 4 + length, Efault) {
+		/* Read CIE_ID (must be 0 for CIE; FDE otherwise) */
+		UNSAFE_GET_USER_INC(cie_id, cur, end, Efault);

[Severity: High]
Are we bypassing the pointer masking mitigation here?

The scoped_user_read_access_size() macro shadows the passed pointer (cie_ptr) with a masked version to prevent speculative kernel accesses. However, the memory read via UNSAFE_GET_USER_INC() uses the separate unmasked cur variable.

Could this allow speculative out-of-bounds reads to access kernel memory?

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+		if (cie_id != EH_FRAME_CIE_ID)
+			return -EINVAL;

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+static __always_inline int __read_fde(struct eh_frame_section *sec,
+				      unsigned long fde_addr,
+				      struct eh_frame_fde *fde)
+{
+	void __user *fde_ptr = (void __user *)fde_addr;
+	unsigned long cur = fde_addr, end;

[ ... ]

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 464

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+	scoped_user_read_access_size(fde_ptr, 4 + length, Efault) {
+		/* Read CIE pointer (offset from current position) */
+		UNSAFE_GET_USER_INC(cie_offset, cur, end, Efault);

[Severity: High]
Is this susceptible to the same pointer masking bypass as __read_cie()?

The unmasked cur variable is being used here instead of the masked fde_ptr.

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+		cie_addr = cur - 4 - cie_offset;
+		if (cie_addr + EH_FRAME_CIE_MIN_LENGTH > fde_addr)
+			return -EINVAL;
+		if (cie_addr < sec->eh_frame_start)
+			return -EINVAL;
+	}

[ ... ]

Quoted 8 lines
kernel/unwind/eh_frame.c +8 -0
+static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
+					 struct eh_frame_fde *fde,
+					 unsigned long *cur_ptr,
+					 unsigned long end,
+					 unsigned long *ip_ptr,
+					 unsigned long target_ip,
+					 struct eh_frame_cfi_context *ctx)
+{

[ ... ]

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 668

Quoted 12 lines
kernel/unwind/eh_frame.c +12 -0
+	case DW_CFA_restore: {
+		u8 reg = DW_CFA_operand(opcode);
+		int idx;
+
+		if (ctx->cie)
+			return -EINVAL;
+
+		idx = reg_to_index(reg);
+		if (idx >= 0)
+			ctx->state.reg_rule[idx] = ctx->cie_state.reg_rule[idx];
+		break;
+	}

[Severity: High]
Does this properly restore the complete register state?

It appears only the rule type is restored from ctx->cie_state, but the offset (reg_offset) and register number (reg_regnum) are not restored and will remain at their previously modified values.

[ ... ]

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 700

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+		case DW_CFA_advance_loc4: {
+			unsigned long offset;
+			u32 delta;
+
+			UNSAFE_GET_USER_INC(delta, cur, end, Efault);
+			offset = delta * fde->cie.code_align;

[Severity: Medium]
Can this calculation overflow?

The multiplication of delta (a u32) and fde->cie.code_align (an unsigned int) evaluates as a 32-bit unsigned integer before being assigned to the 64-bit unsigned long offset.

If the product exceeds 32 bits, the high bits would be truncated, which might cause the unwinder to desync from the actual PC.

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+			ip += offset;
+			break;
+		}

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+		case DW_CFA_restore_extended: {
+			unsigned long reg;
+			int idx;
+
+			if (ctx->cie)
+				return -EINVAL;

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 804

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+
+			ret = read_uleb128(&cur, end, &reg);
+			if (ret)
+				return ret;
+
+			idx = reg_to_index(reg);
+			if (idx >= 0)
+				ctx->state.reg_rule[idx] = ctx->cie_state.reg_rule[idx];
+			break;
+		}

[Severity: High]
This seems to have the same register state restoration issue as DW_CFA_restore. Should reg_offset and reg_regnum also be restored here alongside the rule type?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author
#20 lore raw

Re: [RFC PATCH v2 09/24] unwind_user/eh_frame: Add support for reading .eh_frame section

Quoted 15 lines · On 8/21/2026 10:16 PM, [email protected] wrote:

In preparation for using .eh_frame to unwind user space stacks, add an eh_frame_find() interface for finding the .eh_frame[_hdr] information associated with a given text address.

Quoted 8 lines
kernel/unwind/eh_frame.c +6 -0
 
+static __always_inline int __read_cie(struct eh_frame_section *sec,
+				      unsigned long cie_addr,
+				      struct eh_frame_cie *cie)
+{
+	void __user *cie_ptr = (void __user *)cie_addr;
+	unsigned long cur = cie_addr, end;

[ ... ]

Quoted 6 lines

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 328

Quoted 3 lines
kernel/unwind/eh_frame.c +3 -0
+	scoped_user_read_access_size(cie_ptr, 4 + length, Efault) {
+		/* Read CIE_ID (must be 0 for CIE; FDE otherwise) */
+		UNSAFE_GET_USER_INC(cie_id, cur, end, Efault);

[Severity: High]
Are we bypassing the pointer masking mitigation here?

True. Fixed in next version likewise as for eh_frame_read_header() in preceeding "[RFC PATCH v2 07/24] unwind_user/eh_frame: Add support for reading .eh_frame_hdr section".

Quoted 21 lines

The scoped_user_read_access_size() macro shadows the passed pointer (cie_ptr) with a masked version to prevent speculative kernel accesses. However, the memory read via UNSAFE_GET_USER_INC() uses the separate unmasked cur variable.

Could this allow speculative out-of-bounds reads to access kernel memory?

Quoted 11 lines
kernel/unwind/eh_frame.c +10 -0
+		if (cie_id != EH_FRAME_CIE_ID)
+			return -EINVAL;
 
+static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
+					 struct eh_frame_fde *fde,
+					 unsigned long *cur_ptr,
+					 unsigned long end,
+					 unsigned long *ip_ptr,
+					 unsigned long target_ip,
+					 struct eh_frame_cfi_context *ctx)
+{

[ ... ]

Quoted 9 lines

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 665

Quoted 9 lines
kernel/unwind/eh_frame.c +9 -0
+	case DW_CFA_restore: {
+		u8 reg = DW_CFA_operand(opcode);
+		int idx;
+
+		if (ctx->cie)
+			return -EINVAL;
+
+		idx = reg_to_index(reg);
+		if (idx >= 0)
		if (idx >= 0) {
Quoted 1 line

kernel/unwind/eh_frame.c

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+			ctx->state.reg_rule[idx] = ctx->cie_state.reg_rule[idx];
			ctx->state.reg_regnum[idx] = ctx->cie_state.reg_regnum[idx];
			ctx->state.reg_offset[idx] = ctx->cie_state.reg_offset[idx];
		}
Quoted 9 lines

kernel/unwind/eh_frame.c

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+		break;
+	}

[Severity: High]
Does this properly restore the complete register state?

It appears only the rule type is restored from ctx->cie_state, but the offset (reg_offset) and register number (reg_regnum) are not restored and will remain at their previously modified values.

Valid. I was in a rush to get all of the RFC v1 Sashiko AI review resolved and was used to the struct unwind_user_rule_data approach, where a single assignment is sufficient. I will do likewise here and introduce struct eh_frame_{cfa|reg}_rule_data:

struct eh_frame_cfa_rule_data {
	enum eh_frame_cfa_rule rule;
	unsigned long regnum;
	long offset;
};
struct eh_frame_reg_rule_data {
	enum eh_frame_reg_rule rule;
	unsigned long regnum;
	long offset;
};
struct eh_frame_state {
	struct eh_frame_cfa_rule_data cfa;
	struct eh_frame_reg_rule_data reg[NR_REGS];
};
struct eh_frame_cfi_context {
	struct eh_frame_state state;
	struct eh_frame_state state_stack[EH_FRAME_MAX_STATE_STACK];
	struct eh_frame_state cie_state;
	...

Another option would be to use struct unwind_user[_cfa]_rule_data. For that purpose those would need to be updated to represent the offset and register number as (unsigned) long. The question is how to deal with UNWIND_USER_CFA_RULE_{SP|FP}_OFFSET then. eh_frame could internally use UNWIND_USER_CFA_RULE_REG_OFFSET and only before returning the info to unwind user convert to UNWIND_USER_CFA_RULE_{SP|FP}_OFFSET if appropriate.

I will explore that in a future version.

Quoted 9 lines

[ ... ]

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 700

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+		case DW_CFA_advance_loc4: {
+			unsigned long offset;
+			u32 delta;
+
+			UNSAFE_GET_USER_INC(delta, cur, end, Efault);
+			offset = delta * fde->cie.code_align;
			if (check_mul_overflow(delta, fde->cie.code_align, &offset))
				return -EINVAL;
Quoted 3 lines

[Severity: Medium]
Can this calculation overflow?

Valid. Will add a check in next version.

Quoted 9 lines

The multiplication of delta (a u32) and fde->cie.code_align (an unsigned int) evaluates as a 32-bit unsigned integer before being assigned to the 64-bit unsigned long offset.

If the product exceeds 32 bits, the high bits would be truncated, which might cause the unwinder to desync from the actual PC.

kernel/unwind/eh_frame.c

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+			ip += offset;
			if (check_add_overflow(loc, offset, &loc))
				return -EINVAL;

Likewise for all other DW_CFA_advance_loc* flavors.

Quoted 17 lines
Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+			break;
+		}

[ ... ]

Quoted 12 lines
kernel/unwind/eh_frame.c +12 -0
+		case DW_CFA_restore_extended: {
+			unsigned long reg;
+			int idx;
+
+			if (ctx->cie)
+				return -EINVAL;
+
+			ret = read_uleb128(&cur, end, &reg);
+			if (ret)
+				return ret;
+
+			idx = reg_to_index(reg);
Quoted 8 lines

kernel/unwind/eh_frame.c static __always_inline int read_encoded_pointer(struct eh_frame_section *sec, · line 668

Quoted 4 lines
kernel/unwind/eh_frame.c +4 -0
+			if (idx >= 0)
+				ctx->state.reg_rule[idx] = ctx->cie_state.reg_rule[idx];
+			break;
+		}

[Severity: High]
This seems to have the same register state restoration issue as DW_CFA_restore. Should reg_offset and reg_regnum also be restored here alongside the rule type?

Same as for DW_CFA_restore above.

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#21 lore raw

RFC PATCH v2 10/24 unwind_user/eh_frame: Detect .eh_frame_hdr sections in executables

In reply to Jens Remus

When loading an ELF executable, automatically detect .eh_frame_hdr sections and associate them with the mm_struct.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---
 fs/binfmt_elf.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
fs/binfmt_elf.c +47 -2
index 16a56b6b3f6c..8c1cdd8318c5 100644
@@ -48,6 +48,7 @@
 #include <linux/uaccess.h>
 #include <uapi/linux/rseq.h>
 #include <linux/rseq.h>
+#include <linux/eh_frame.h>
 #include <asm/param.h>
 #include <asm/page.h>
 
@@ -637,6 +638,23 @@ static inline int make_prot(u32 p_flags, struct arch_elf_state *arch_state,
 	return arch_elf_adjust_prot(prot, arch_state, has_interp, is_interp);
 }
 
+static void elf_add_eh_frame(const struct elf_phdr *text,
+			     const struct elf_phdr *eh_frame,
+			     unsigned long base_addr)
+{
+	unsigned long eh_frame_start, eh_frame_end,
+		      text_start, text_end;
+
+	eh_frame_start = base_addr + eh_frame->p_vaddr;
+	eh_frame_end   = eh_frame_start + eh_frame->p_memsz;
+
+	text_start   = base_addr + text->p_vaddr;
+	text_end     = text_start + text->p_memsz;
+
+	/* Ignore return value, eh_frame[_hdr] section isn't critical */
+	eh_frame_add_section(eh_frame_start, eh_frame_end, text_start, text_end);
+}
+
 /* This is much more generalized than the library routine read function,
    so we keep this separate.  Technically the library read function
    is only provided so that we can read a.out libraries that have
@@ -647,7 +665,7 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 		unsigned long no_base, struct elf_phdr *interp_elf_phdata,
 		struct arch_elf_state *arch_state)
 {
-	struct elf_phdr *eppnt;
+	struct elf_phdr *eppnt, *eh_frame_phdr = NULL;
 	unsigned long load_addr = 0;
 	int load_addr_set = 0;
 	unsigned long error = ~0UL;
@@ -673,7 +691,8 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 
 	eppnt = interp_elf_phdata;
 	for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
-		if (eppnt->p_type == PT_LOAD) {
+		switch (eppnt->p_type) {
+		case PT_LOAD: {
 			int elf_type = MAP_PRIVATE;
 			int elf_prot = make_prot(eppnt->p_flags, arch_state,
 						 true, true);
@@ -712,6 +731,19 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 				error = -ENOMEM;
 				goto out;
 			}
+			break;
+		}
+		case PT_GNU_EH_FRAME:
+			eh_frame_phdr = eppnt;
+			break;
+		}
+	}
+
+	if (eh_frame_phdr) {
+		eppnt = interp_elf_phdata;
+		for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
+			if (eppnt->p_flags & PF_X && eppnt->p_type == PT_LOAD)
+				elf_add_eh_frame(eppnt, eh_frame_phdr, load_addr);
 		}
 	}
 
@@ -837,6 +869,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
 	unsigned long error;
 	struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL;
 	struct elf_phdr *elf_property_phdata = NULL;
+	struct elf_phdr *eh_frame_phdr = NULL;
 	unsigned long elf_brk;
 	bool brk_moved = false;
 	int retval, i;
@@ -945,6 +978,10 @@ static int load_elf_binary(struct linux_binprm *bprm)
 				executable_stack = EXSTACK_DISABLE_X;
 			break;
 
+		case PT_GNU_EH_FRAME:
+			eh_frame_phdr = elf_ppnt;
+			break;
+
 		case PT_LOPROC ... PT_HIPROC:
 			retval = arch_elf_pt_proc(elf_ex, elf_ppnt,
 						  bprm->file, false,
@@ -1242,6 +1279,14 @@ static int load_elf_binary(struct linux_binprm *bprm)
 			elf_brk = k;
 	}
 
+	if (eh_frame_phdr) {
+		for (i = 0, elf_ppnt = elf_phdata;
+		     i < elf_ex->e_phnum; i++, elf_ppnt++) {
+			if (elf_ppnt->p_flags & PF_X && elf_ppnt->p_type == PT_LOAD)
+				elf_add_eh_frame(elf_ppnt, eh_frame_phdr, load_bias);
+		}
+	}
+
 	e_entry = elf_ex->e_entry + load_bias;
 	phdr_addr += load_bias;
 	elf_brk += load_bias;
Signature · 1 line
2.53.0
#22 lore raw

Re: [RFC PATCH v2 10/24] unwind_user/eh_frame: Detect .eh_frame_hdr sections in executables

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

When loading an ELF executable, automatically detect .eh_frame_hdr sections and associate them with the mm_struct.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author
#23 lore raw

RFC PATCH v2 11/24 unwind_user/eh_frame: Wire up unwind_user to eh_frame

In reply to Jens Remus

Now that the eh_frame infrastructure is fully in place, make it work by hooking it up to the unwind_user interface.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---
 arch/Kconfig                      |  1 +
 include/linux/unwind_user_types.h |  4 +++-
 kernel/unwind/user.c              | 23 +++++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)
arch/Kconfig +1 -0
index 60542d5e5731..ea969811f798 100644
@@ -488,6 +488,7 @@ config UNWIND_USER
 
 config HAVE_UNWIND_USER_EH_FRAME
 	bool
+	select UNWIND_USER
 
 config HAVE_UNWIND_USER_FP
 	bool
include/linux/unwind_user_types.h +3 -1
index 4da058096259..88fc2fee8534 100644
@@ -9,7 +9,8 @@
  * available.
  */
 enum unwind_user_type_bits {
-	UNWIND_USER_TYPE_FP_BIT =		0,
+	UNWIND_USER_TYPE_EH_FRAME_BIT =		0,
+	UNWIND_USER_TYPE_FP_BIT =		1,
 
 	NR_UNWIND_USER_TYPE_BITS,
 };
@@ -17,6 +18,7 @@ enum unwind_user_type_bits {
 enum unwind_user_type {
 	/* Type "none" for the start of stack walk iteration. */
 	UNWIND_USER_TYPE_NONE =			0,
+	UNWIND_USER_TYPE_EH_FRAME =		BIT(UNWIND_USER_TYPE_EH_FRAME_BIT),
 	UNWIND_USER_TYPE_FP =			BIT(UNWIND_USER_TYPE_FP_BIT),
 };
 
kernel/unwind/user.c +23 -0
index 830c620fe453..85fc82252af1 100644
@@ -10,6 +10,7 @@
 #include <linux/sched/task_stack.h>
 #include <linux/unwind_user.h>
 #include <linux/uaccess.h>
+#include <linux/eh_frame.h>
 
 #ifdef CONFIG_DYNAMIC_DEBUG
 
@@ -173,6 +174,16 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 	return unwind_user_next_common(state, &fp_frame);
 }
 
+static int unwind_user_next_eh_frame(struct unwind_user_state *state)
+{
+	struct unwind_user_frame frame;
+
+	/* eh_frame expects the frame to be local storage */
+	if (eh_frame_find(state->ip, &frame))
+		return -ENOENT;
+	return unwind_user_next_common(state, &frame);
+}
+
 static int unwind_user_next(struct unwind_user_state *state)
 {
 	unsigned long iter_mask = state->available_types;
@@ -186,6 +197,16 @@ static int unwind_user_next(struct unwind_user_state *state)
 
 		state->current_type = type;
 		switch (type) {
+		case UNWIND_USER_TYPE_EH_FRAME:
+			switch (unwind_user_next_eh_frame(state)) {
+			case 0:
+				return 0;
+			case -ENOENT:
+				continue;	/* Try next method. */
+			default:
+				state->done = true;
+			}
+			break;
 		case UNWIND_USER_TYPE_FP:
 			if (!unwind_user_next_fp(state))
 				return 0;
@@ -214,6 +235,8 @@ static int unwind_user_start(struct unwind_user_state *state)
 		return -EINVAL;
 	}
 
+	if (current_has_eh_frame())
+		state->available_types |= UNWIND_USER_TYPE_EH_FRAME;
 	if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP))
 		state->available_types |= UNWIND_USER_TYPE_FP;
 
Signature · 1 line
2.53.0
#24 lore raw

Re: [RFC PATCH v2 11/24] unwind_user/eh_frame: Wire up unwind_user to eh_frame

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unadjusted instruction pointer passed to eh_frame_find() for return addresses. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unadjusted instruction pointer passed to eh_frame_find() for return addresses. --

--- Patch [11]: [RFC PATCH v2 11/24] unwind_user/eh_frame: Wire up unwind_user to eh_frame --- commit 242304f470b193c92dc61308cce6cc9e66841ecf
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Wire up unwind_user to eh_frame

This commit hooks up the eh_frame infrastructure to the unwind_user interface. It enables the unwinder to use eh_frame data for user space stack traces.

Quoted 4 lines
kernel/unwind/user.c +0 -0
index 830c620fe453c..85fc82252af15 100644

[ ... ]

kernel/unwind/user.c static int unwind_user_next_fp(struct unwind_user_state *state) · line 182

Quoted 10 lines
kernel/unwind/user.c +6 -0
@@ -173,6 +174,16 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 	return unwind_user_next_common(state, &fp_frame);
 }
 
+static int unwind_user_next_eh_frame(struct unwind_user_state *state)
+{
+	struct unwind_user_frame frame;
+
+	/* eh_frame expects the frame to be local storage */
+	if (eh_frame_find(state->ip, &frame))

[Severity: Medium]
Does this need to adjust state->ip for return addresses?

Since state->ip points to the instruction after the call, if a function call is the final instruction of a block or function, passing the unadjusted ip to eh_frame_find() might cause it to match the next function's FDE instead of the caller's.

Would it be better to pass an adjusted instruction pointer (like state->topmost ? state->ip : state->ip - 1) to ensure the correct unwind rules are retrieved during normal user space unwinding?

Quoted 4 lines
kernel/unwind/user.c +4 -0
+		return -ENOENT;
+	return unwind_user_next_common(state, &frame);
+}
+

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author
#25 lore raw

Re: [RFC PATCH v2 11/24] unwind_user/eh_frame: Wire up unwind_user to eh_frame

Quoted 20 lines · On 8/21/2026 10:07 PM, [email protected] wrote:

This commit hooks up the eh_frame infrastructure to the unwind_user interface. It enables the unwinder to use eh_frame data for user space stack traces.

kernel/unwind/user.c static int unwind_user_next_fp(struct unwind_user_state *state) · line 182

Quoted 12 lines
kernel/unwind/user.c +6 -0
@@ -173,6 +174,16 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 	return unwind_user_next_common(state, &fp_frame);
 }
 
+static int unwind_user_next_eh_frame(struct unwind_user_state *state)
+{
+	struct unwind_user_frame frame;
+
+	/* eh_frame expects the frame to be local storage */
+	if (eh_frame_find(state->ip, &frame))

[Severity: Medium]
Does this need to adjust state->ip for return addresses?

Quoted 8 lines

Since state->ip points to the instruction after the call, if a function call is the final instruction of a block or function, passing the unadjusted ip to eh_frame_find() might cause it to match the next function's FDE instead of the caller's.

Would it be better to pass an adjusted instruction pointer (like state->topmost ? state->ip : state->ip - 1) to ensure the correct unwind rules are retrieved during normal user space unwinding?

I have this on my list of improvements for unwind user for quite some time, as I have observed this on s390 (see below for details), but wanted to postpone as enhancement once .eh_frame or .sframe unwinding have landed.

Should unwind user only internally adust the IP used for the lookup (as suggested by Sashiko above)? Or should it adjust the RA returned by unwind_user_next_common(), so that the adjusted value surfaces in callchains?

perf does adjust the printed IPs for perf record --call-graph dwarf, as one can easily tell from the following output on s390. Instructions must be 2-byte aligned on s390 and they are not except for the topmost:

objdump 2802 515.787731: 100010 software/task-clock/P:

             3ff846904b0 _dl_relocate_object_no_relro+0x580 (/usr/lib/ld64.so.1)
             3ff84691bab _dl_relocate_object+0x1b (/usr/lib/ld64.so.1)
             3ff8469d7d3 dl_main+0x1b83 (/usr/lib/ld64.so.1)
             3ff8469a14b _dl_sysdep_start+0x1db (/usr/lib/ld64.so.1)
             3ff8469b903 _dl_start+0x723 (/usr/lib/ld64.so.1)
             3ff8469a733 _start+0x13 (/usr/lib/ld64.so.1)
Quoted 5 lines

kernel/unwind/user.c static int unwind_user_next_fp(struct unwind_user_state *state) · line 186

Quoted 5 lines
kernel/unwind/user.c +4 -0
+		return -ENOENT;
+	return unwind_user_next_common(state, &frame);
+}
+

On s390 I regularly observe the following case where above would prevent the callchain to erroneously end in internal_addseverity+0x0:

objdump 1060 26.324199: 100010 task-clock:ppp:

             3ff9ec908b4 dfs_traversal.part.0+0x34 (/usr/lib/ld64.so.1)
             3ff9ec90900 dfs_traversal.part.0+0x80 (/usr/lib/ld64.so.1)
             3ff9ec90e68 _dl_sort_maps+0x4b8 (/usr/lib/ld64.so.1)
             3ff9ec84aa2 _dl_fini+0x252 (/usr/lib/ld64.so.1)
             3ff9e856b06 __run_exit_handlers+0x176 (/usr/lib64/libc.so.6)
             3ff9e856c00 internal_addseverity+0x0 (/usr/lib64/libc.so.6)  <!!!

unwind user needs to determine the next IP as RA - 1, as the return address of a function with __attribute__((noreturn)), such as exit(), might point at the following function:

$ objdump -d /usr/lib64/libc.so.6
...
000000000004dee0 <exit>:
   4dee0:       eb ef f0 70 00 24       stmg    %r14,%r15,112(%r15)
   4dee6:       a7 59 00 01             lghi    %r5,1
   4deea:       e3 f0 ff 60 ff 71       lay     %r15,-160(%r15)
   4def0:       a7 49 00 01             lghi    %r4,1
   4def4:       c0 30 00 0d 9b a2       larl    %r3,201638 <__exit_funcs>
   4defa:       c0 e5 ff ff fe 9b       brasl   %r14,4dc30 <__run_exit_handlers>

000000000004df00 <internal_addseverity>:

   4df00:       eb 9f f0 48 00 24       stmg    %r9,%r15,72(%r15)
   4df06:       c0 10 00 0d 9b 9d       larl    %r1,201640 <severity_list>
   4df0c:       e3 f0 ff 60 ff 71       lay     %r15,-160(%r15)

...

Thanks and regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#26 lore raw

RFC PATCH v2 12/24 unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected corruption

In reply to Jens Remus

To avoid continued attempted use of a bad .eh_frame[_hdr] sections, remove them on demand when the first sign of corruption is detected.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---
 kernel/unwind/eh_frame.c | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/unwind/eh_frame.c +9 -0
index 21f40805b417..327849ea4a69 100644
@@ -1202,6 +1202,15 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	if (!ret)
 		ret = __find_frame_row(sec, &fde, ip, frame);
 
+	/*
+	 * Unregister .eh_frame[_hdr] in case of an error,
+	 * e.g. EINVAL (corrupted) or EFAULT (inaccessible).
+	 * Keep if ENOENT (not found) or EOPNOTSUPP (unsupported CFI).
+	 */
+	if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP))
+		if (eh_frame_remove_section(sec->eh_frame_hdr_start))
+			dbg("eh_frame_remove_section() failed\n");
+
 	return ret;
 }
 
Signature · 1 line
2.53.0
#27 lore raw

Re: [RFC PATCH v2 12/24] unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected corruption

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition allows the kernel to erroneously unregister a valid, newly loaded .eh_frame section due to address reuse.
- [High] The unwinder conflates actual memory corruption with kernel-imposed safety limits, permanently disabling unwinding for valid libraries.

Pre-existing issues:
- [Critical] Unchecked error from mas_erase() leads to freeing a maple tree section while it remains active in the tree, causing a Use-After-Free (UAF).
- [High] Lock drop and reacquire inside mas_erase() during mas_for_each() iteration causes a TOCTOU race and memory leak.
--

--- Patch [12]: [RFC PATCH v2 12/24] unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected corruption --- commit 14251b7471659003e9517d814ad704b284ed18df
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected corruption

This commit adds logic to unregister .eh_frame or .eh_frame_hdr sections on demand when corruption is detected during lookup. This prevents repeated attempts to parse bad unwind sections by removing them immediately upon an error return.

Quoted 7 lines
kernel/unwind/eh_frame.c +0 -0
index 21f40805b417f..327849ea4a692 100644
@@ -1202,6 +1202,15 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	if (!ret)
 		ret = __find_frame_row(sec, &fde, ip, frame);

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1213

Quoted 10 lines
kernel/unwind/eh_frame.c +9 -0
 
+	/*
+	 * Unregister .eh_frame[_hdr] in case of an error,
+	 * e.g. EINVAL (corrupted) or EFAULT (inaccessible).
+	 * Keep if ENOENT (not found) or EOPNOTSUPP (unsupported CFI).
+	 */
+	if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP))
+		if (eh_frame_remove_section(sec->eh_frame_hdr_start))
+			dbg("eh_frame_remove_section() failed\n");
+

[Severity: High]
Does this introduce a race condition with memory unmapping and mapping?

If a thread is delayed after retrieving sec in eh_frame_find(), another thread could unmap the library and map a new library at the exact same virtual address, registering it with the same eh_frame_hdr_start. When the first thread resumes and fails to parse against the new memory, passing the virtual address sec->eh_frame_hdr_start to eh_frame_remove_section() seems like it would unintentionally remove the newly loaded, valid library's section.

[Severity: High]
Is it safe to assume -EINVAL always means corruption?

Looking at __do_cfi_program(), it also returns -EINVAL when kernel limits are hit, such as exceeding EH_FRAME_CFI_INSN_LIMIT or EH_FRAME_MAX_CODE_ALIGN.

If a valid library simply has a complex function that hits these limits, wouldn't this code globally and permanently disable unwinding for it by removing its section?

[Severity: High]
This is a pre-existing issue, but looking at eh_frame_remove_section() which is called here, is there a memory leak if mas_erase() drops the lock?

eh_frame_remove_section() holds mtree_lock and iterates via mas_for_each(). It calls __eh_frame_remove_section(), which calls mas_erase().

If mas_erase() needs to allocate memory via mas_nomem(mas, GFP_KERNEL), it can drop the tree lock. If a concurrent thread inserts a new section covering the same range during this unlocked window, mas_erase() retries, erases the new section, and returns it.

__eh_frame_remove_section() then sees the returned entry doesn't match sec, returns -EINVAL, and skips call_srcu(), potentially leaking the new section.

[Severity: Critical]
This is a pre-existing issue, but looking at __eh_frame_remove_section() called by eh_frame_remove_section(), can a memory allocation failure lead to a use-after-free?

If mas_nomem() fails to allocate memory during mas_erase(), it sets an error state and aborts without removing the node, but still returns the original sec:

__eh_frame_remove_section() {
    ...
    if (mas_erase(mas) != sec) {
        return -EINVAL;
    }
    call_srcu(&eh_frame_srcu, &sec->rcu, eh_frame_free_srcu);
    ...
}

Because __eh_frame_remove_section() only checks if the return value does not equal sec, it misses the error state completely. It then calls call_srcu() to free the section while the node remains active in the maple tree. Could subsequent calls to eh_frame_find() load and access this freed memory?

Quoted 3 lines
kernel/unwind/eh_frame.c +0 -0
 	return ret;
 }
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
author
#28 lore raw

Re: [RFC PATCH v2 12/24] unwind_user/eh_frame: Remove .eh_frame[_hdr] section on detected corruption

Quoted 24 lines · On 8/21/2026 10:18 PM, [email protected] wrote:

This commit adds logic to unregister .eh_frame or .eh_frame_hdr sections on demand when corruption is detected during lookup. This prevents repeated attempts to parse bad unwind sections by removing them immediately upon an error return.

Quoted 5 lines
kernel/unwind/eh_frame.c +0 -0
@@ -1202,6 +1202,15 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	if (!ret)
 		ret = __find_frame_row(sec, &fde, ip, frame);

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1213

Quoted 10 lines
kernel/unwind/eh_frame.c +9 -0
 
+	/*
+	 * Unregister .eh_frame[_hdr] in case of an error,
+	 * e.g. EINVAL (corrupted) or EFAULT (inaccessible).
+	 * Keep if ENOENT (not found) or EOPNOTSUPP (unsupported CFI).
+	 */
+	if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP))
+		if (eh_frame_remove_section(sec->eh_frame_hdr_start))
+			dbg("eh_frame_remove_section() failed\n");
+

[Severity: High]
Does this introduce a race condition with memory unmapping and mapping?

Quoted 7 lines

If a thread is delayed after retrieving sec in eh_frame_find(), another thread could unmap the library and map a new library at the exact same virtual address, registering it with the same eh_frame_hdr_start. When the first thread resumes and fails to parse against the new memory, passing the virtual address sec->eh_frame_hdr_start to eh_frame_remove_section() seems like it would unintentionally remove the newly loaded, valid library's section.

I don't think that is possible. eh_frame_find() and likewise eh_frame_remove_section() both guard using eh_frame_srcu. So while one thread has a reference to a section that cannot be removed and replaced. I am missing something?

Quoted 11 lines

[Severity: High]
Is it safe to assume -EINVAL always means corruption?

Looking at __do_cfi_program(), it also returns -EINVAL when kernel limits are hit, such as exceeding EH_FRAME_CFI_INSN_LIMIT or EH_FRAME_MAX_CODE_ALIGN.

If a valid library simply has a complex function that hits these limits, wouldn't this code globally and permanently disable unwinding for it by removing its section?

Following are the used conventions:
- EINVAL: bad .eh_frame; section should be removed
- EOPNOTSUPP: unsupported .eh_frame
- ENOENT: no .eh_frame for IP

One can argue whether a CIE/FDE with too many CFI instructions should be considered bad or unsupported. For now I decided for bad, to prevent future unwind attempts in case of crafted .eh_frame.

Quoted 7 lines

[Severity: High]
This is a pre-existing issue, but looking at eh_frame_remove_section() which is called here, is there a memory leak if mas_erase() drops the lock?

eh_frame_remove_section() holds mtree_lock and iterates via mas_for_each(). It calls __eh_frame_remove_section(), which calls mas_erase().

Quoted 7 lines

If mas_erase() needs to allocate memory via mas_nomem(mas, GFP_KERNEL), it can drop the tree lock. If a concurrent thread inserts a new section covering the same range during this unlocked window, mas_erase() retries, erases the new section, and returns it.

__eh_frame_remove_section() then sees the returned entry doesn't match sec, returns -EINVAL, and skips call_srcu(), potentially leaking the new section.

Help! I fail to understand how the (internal) maple tree locking and its advanced API are then supposed to be used correctly to iterate over the items in the tree and remove some while doing so. Is that why other users use an external lock (MT_FLAGS_LOCK_EXTERN, MTREE_INIT_EXT())? See MM_MT_FLAGS.

Quoted 18 lines

[Severity: Critical]
This is a pre-existing issue, but looking at __eh_frame_remove_section() called by eh_frame_remove_section(), can a memory allocation failure lead to a use-after-free?

If mas_nomem() fails to allocate memory during mas_erase(), it sets an error state and aborts without removing the node, but still returns the original sec:

__eh_frame_remove_section() {
    ...
    if (mas_erase(mas) != sec) {
        return -EINVAL;
    }
    call_srcu(&eh_frame_srcu, &sec->rcu, eh_frame_free_srcu);
    ...
}
Quoted 5 lines

Because __eh_frame_remove_section() only checks if the return value does not equal sec, it misses the error state completely. It then calls call_srcu() to free the section while the node remains active in the maple tree. Could subsequent calls to eh_frame_find() load and access this freed memory?

This is resolved in the next version of "[RFC PATCH v2 08/24] unwind_user/
eh_frame: Store .eh_frame_hdr section data in per-mm maple tree". See my respective reply.

Quoted 5 lines

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1216

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
 	return ret;
 }
 

Thanks and regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#29 lore raw

RFC PATCH v2 13/24 unwind_user/eh_frame: Show file name in debug output

In reply to Jens Remus

When debugging eh_frame issues, the error messages aren't all that helpful without knowing what file a corresponding .eh_frame[_hdr] section belongs to. Prefix debug output strings with the file name.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

---
 include/linux/eh_frame.h       |  4 ++++
 kernel/unwind/eh_frame.c       | 13 ++++++++----
 kernel/unwind/eh_frame_debug.h | 37 ++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 4 deletions(-)
include/linux/eh_frame.h +4 -0
index 740316b19df3..70b4f9d12828 100644
@@ -11,6 +11,10 @@
 struct eh_frame_section {
 	struct rcu_head	rcu;
 
+#ifdef CONFIG_DYNAMIC_DEBUG
+	const char	*filename;
+#endif
+
 	unsigned long	eh_frame_hdr_start;
 	unsigned long	eh_frame_hdr_end;
 	unsigned long	text_start;
kernel/unwind/eh_frame.c +9 -4
index 327849ea4a69..96237a96d263 100644
@@ -1207,15 +1207,18 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	 * e.g. EINVAL (corrupted) or EFAULT (inaccessible).
 	 * Keep if ENOENT (not found) or EOPNOTSUPP (unsupported CFI).
 	 */
-	if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP))
+	if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP)) {
+		dbg_sec("removing bad .eh_frame[_hdr] section\n");
 		if (eh_frame_remove_section(sec->eh_frame_hdr_start))
 			dbg("eh_frame_remove_section() failed\n");
+	}
 
 	return ret;
 }
 
 static void free_section(struct eh_frame_section *sec)
 {
+	dbg_free(sec);
 	kfree(sec);
 }
 
@@ -1342,6 +1345,8 @@ int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 	sec->text_start		= text_start;
 	sec->text_end		= text_end;
 
+	dbg_init(sec);
+
 	ret = eh_frame_read_header(sec);
 	if (ret)
 		goto err_free;
@@ -1349,8 +1354,8 @@ int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 	ret = mtree_insert_range(eh_frame_mt, sec->text_start, sec->text_end - 1,
 				 sec, GFP_KERNEL_ACCOUNT);
 	if (ret) {
-		dbg("mtree_insert_range failed: text=%lx-%lx\n",
-		    sec->text_start, sec->text_end);
+		dbg_sec("mtree_insert_range failed: text=%lx-%lx\n",
+			sec->text_start, sec->text_end);
 		goto err_free;
 	}
 
@@ -1372,7 +1377,7 @@ static int __eh_frame_remove_section(struct ma_state *mas,
 				     struct eh_frame_section *sec)
 {
 	if (mas_erase(mas) != sec) {
-		dbg("mas_erase failed: text=%lx\n", sec->text_start);
+		dbg_sec("mas_erase failed: text=%lx\n", sec->text_start);
 		return -EINVAL;
 	}
 
kernel/unwind/eh_frame_debug.h +37 -0
index 5a3e4e7f065a..40a80861d4d4 100644
@@ -3,6 +3,7 @@
 #define _EH_FRAME_DEBUG_H
 
 #include <linux/eh_frame.h>
+#include <linux/mm.h>
 #include "eh_frame.h"
 
 #ifdef CONFIG_DYNAMIC_DEBUG
@@ -10,9 +11,45 @@
 #define dbg(fmt, ...)							\
 	pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__)
 
+#define dbg_sec(fmt, ...)						\
+	dbg("%s: " fmt, sec->filename, ##__VA_ARGS__)
+
+static inline void dbg_init(struct eh_frame_section *sec)
+{
+	struct mm_struct *mm = current->mm;
+	struct vm_area_struct *vma;
+	const char *name;
+
+	guard(mmap_read_lock)(mm);
+	vma = vma_lookup(mm, sec->eh_frame_hdr_start);
+	if (!vma)
+		sec->filename = kstrdup("(vma gone???)", GFP_KERNEL_ACCOUNT);
+	else if (vma_is_anonymous(vma))
+		 sec->filename = kstrdup("(anonymous)", GFP_KERNEL_ACCOUNT);
+	else if (vma->vm_file)
+		sec->filename = kstrdup_quotable_file(vma->vm_file, GFP_KERNEL_ACCOUNT);
+	else if (vma->vm_ops && vma->vm_ops->name && (name = vma->vm_ops->name(vma)))
+		sec->filename = kstrdup(name, GFP_KERNEL_ACCOUNT);
+	else if (arch_vma_name(vma))
+		sec->filename = kstrdup(arch_vma_name(vma), GFP_KERNEL_ACCOUNT);
+	else if (!vma->vm_mm)
+		sec->filename = kstrdup("(vdso)", GFP_KERNEL_ACCOUNT);
+	else
+		sec->filename = kstrdup("(vma unknown???)", GFP_KERNEL_ACCOUNT);
+}
+
+static inline void dbg_free(struct eh_frame_section *sec)
+{
+	kfree(sec->filename);
+}
+
 #else /* !CONFIG_DYNAMIC_DEBUG */
 
 #define dbg(args...)			no_printk(args)
+#define dbg_sec(args...)		no_printk(args)
+
+static inline void dbg_init(struct eh_frame_section *sec) {}
+static inline void dbg_free(struct eh_frame_section *sec) {}
 
 #endif /* !CONFIG_DYNAMIC_DEBUG */
 
Signature · 1 line
2.53.0
#30 lore raw

Re: [RFC PATCH v2 13/24] unwind_user/eh_frame: Show file name in debug output

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

When debugging eh_frame issues, the error messages aren't all that helpful without knowing what file a corresponding .eh_frame[_hdr] section belongs to. Prefix debug output strings with the file name.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe implementation.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
author
#31 lore raw

RFC PATCH v2 14/24 unwind_user/eh_frame: Add .eh_frame[_hdr] validation option

In reply to Jens Remus

Add a debug feature to validate all .eh_frame[_hdr] sections when first loading the file rather than on demand.

---

Notes (jremus):
FIXME: dbg*() with UACCESS enabled.

Changes in RFC v2:
- Fix compile error. (Sashiko AI)
- Correct loop counter type to prevent infinite loop. (Sashiko AI)

 arch/Kconfig                   | 22 +++++++++
 kernel/unwind/eh_frame.c       | 90 ++++++++++++++++++++++++++++++++++
 kernel/unwind/eh_frame_debug.h |  4 ++
 3 files changed, 116 insertions(+)
arch/Kconfig +22 -0
index ea969811f798..c06e5010a88a 100644
@@ -490,6 +490,28 @@ config HAVE_UNWIND_USER_EH_FRAME
 	bool
 	select UNWIND_USER
 
+config EH_FRAME_VALIDATION
+	bool "Enable .eh_frame[_hdr] section debugging"
+	depends on HAVE_UNWIND_USER_EH_FRAME
+	depends on DYNAMIC_DEBUG
+	help
+	  When adding an .eh_frame_hdr section for a test, validate the
+	  entire section and its referenced entire .eh_frame section
+	  immediately rather than on demand.
+
+	  This is a debug feature which is helpful for rooting out
+	  .eh_frame[_hdr] section issues.  If the .eh_frame[_hdr]
+	  section is corrupt, it will fail to load immediately, with
+	  more information provided in dynamic printks.
+
+	  This has a significant page cache footprint due to its reading
+	  of the entire .eh_frame[_hdr] sections for every loaded executable
+	  and shared library.  Also, it's done for all processes, even those
+	  which don't get stack traced by the kernel.  Not recommended for
+	  general use.
+
+	  If unsure, say N.
+
 config HAVE_UNWIND_USER_FP
 	bool
 	select UNWIND_USER
kernel/unwind/eh_frame.c +90 -0

102 lines not on this page · raw message

kernel/unwind/eh_frame_debug.h +4 -0
index 40a80861d4d4..bcb2d03ab9ab 100644
@@ -14,6 +14,9 @@
 #define dbg_sec(fmt, ...)						\
 	dbg("%s: " fmt, sec->filename, ##__VA_ARGS__)
 
+#define dbg_sec_ehfh(addr, fmt, ...)					\
+	dbg_sec(".eh_frame_hdr+%#lx: " fmt, ((addr) - sec->eh_frame_hdr_start), ##__VA_ARGS__)
+
 static inline void dbg_init(struct eh_frame_section *sec)
 {
 	struct mm_struct *mm = current->mm;
@@ -47,6 +50,7 @@ static inline void dbg_free(struct eh_frame_section *sec)
 
 #define dbg(args...)			no_printk(args)
 #define dbg_sec(args...)		no_printk(args)
+#define dbg_sec_ehfh(args...)		no_printk(args)
 
 static inline void dbg_init(struct eh_frame_section *sec) {}
 static inline void dbg_free(struct eh_frame_section *sec) {}
Signature · 1 line
2.53.0
#32 lore raw

Re: [RFC PATCH v2 14/24] unwind_user/eh_frame: Add .eh_frame[_hdr] validation option

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Calling printk (via dbg_sec and dbg_sec_ehfh) while user access (UACCESS) is enabled. - [High]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Calling printk (via dbg_sec and dbg_sec_ehfh) while user access (UACCESS) is enabled.
- [High] Missing cond_resched() in massive, unbounded .eh_frame validation loop.
- [High] Misuse of scoped_user_read_access_size completely bypasses pointer masking mitigations. --

--- Patch [14]: [RFC PATCH v2 14/24] unwind_user/eh_frame: Add .eh_frame[_hdr] validation option --- commit e134b713f5753b4c2aa710bb7ce4a784930e6ccd
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Add .eh_frame[_hdr] validation option

Add a debug feature to validate all .eh_frame[_hdr] sections when first loading the file rather than on demand.

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
index 96237a96d263e..b0be56e315080 100644

[ ... ]

Quoted 8 lines
kernel/unwind/eh_frame.c +4 -0
@@ -1216,6 +1216,92 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	return ret;
 }
 
+#ifdef CONFIG_EH_FRAME_VALIDATION
+
+static int eh_frame_validate_section(struct eh_frame_section *sec)
+{

[ ... ]

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1244

Quoted 4 lines
kernel/unwind/eh_frame.c +4 -0
+	table_start_ptr = (void __user *)sec->binary_search_table_start;
+	table_size = sec->binary_search_table_end - sec->binary_search_table_start;
+
+	for (i = 0; i < sec->fde_count; i++) {

[Severity: High]
Could this unbounded loop cause CPU stalls or trigger soft lockup warnings?

Since fde_count is controlled by the size of the mapped .eh_frame_hdr VMA, it can be extremely large for maliciously crafted ELF files, potentially stalling the CPU for seconds or minutes.

Because mmap_read_lock is released before this is called, would it be safer to add a cond_resched() in this loop?

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1256

Quoted 12 lines
kernel/unwind/eh_frame.c +12 -0
+		struct eh_frame_fde fde;
+		unsigned long cur;
+		unsigned long func_addr, fde_addr;
+		int ret;
+
+		cur = sec->binary_search_table_start + i * entry_size;
+
+		scoped_user_read_access_size(table_start_ptr, table_size, Efault) {
+			/* Read function start address from table */
+			ret = read_encoded_pointer(sec, &cur,
+						   sec->binary_search_table_end,
+						   table_enc, &func_addr);

[Severity: High]
Does this bypass the Spectre v1 mitigations provided by the scoped access macro?

The scoped_user_read_access_size() macro shadows the table_start_ptr variable to enforce usage of a masked pointer. However, the code ignores that masked pointer and instead uses the unmasked cur pointer derived outside the scope:

    cur = sec->binary_search_table_start + i * entry_size;

When read_encoded_pointer() uses this unmasked pointer inside unsafe_get_user(), the masking mitigations are bypassed, allowing potential arbitrary speculative reads.

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1258

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+			if (ret) {
+				dbg_sec_ehfh(cur, "table[%lu]: failed to read function start address\n", i);

[Severity: High]
Is it safe to call printk wrappers like dbg_sec_ehfh() or dbg_sec() while user access (UACCESS) is enabled?

Calling complex functions like printk inside a scoped_user_read_access_size() block can lead to deadlocks or kernel panics. It does internal locking, can sleep, and may clobber architecture-specific user access state like the SMAP AC flag.

Quoted 6 lines
kernel/unwind/eh_frame.c +6 -0
+				return ret;
+			}
+			if (i && func_addr <= prev_func_addr) {
+				dbg_sec(".eh_frame_hdr: table[%lu]: not sorted\n", i);
+				return -EINVAL;
+			}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author
#33 lore raw

Re: [RFC PATCH v2 14/24] unwind_user/eh_frame: Add .eh_frame[_hdr] validation option

Quoted 22 lines · On 8/21/2026 10:10 PM, [email protected] wrote:

Add a debug feature to validate all .eh_frame[_hdr] sections when first loading the file rather than on demand.

Quoted 10 lines
kernel/unwind/eh_frame.c +4 -0
@@ -1216,6 +1216,92 @@ int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame)
 	return ret;
 }
 
+#ifdef CONFIG_EH_FRAME_VALIDATION
+
+static int eh_frame_validate_section(struct eh_frame_section *sec)
+{

[ ... ]

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1244

Quoted 4 lines
kernel/unwind/eh_frame.c +4 -0
+	table_start_ptr = (void __user *)sec->binary_search_table_start;
+	table_size = sec->binary_search_table_end - sec->binary_search_table_start;
+
+	for (i = 0; i < sec->fde_count; i++) {

[Severity: High]
Could this unbounded loop cause CPU stalls or trigger soft lockup warnings?

Quoted 6 lines

Since fde_count is controlled by the size of the mapped .eh_frame_hdr VMA, it can be extremely large for maliciously crafted ELF files, potentially stalling the CPU for seconds or minutes.

Because mmap_read_lock is released before this is called, would it be safer to add a cond_resched() in this loop?

According to Steven this is not required.

Quoted 22 lines

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1256

Quoted 13 lines
kernel/unwind/eh_frame.c +12 -0
+		struct eh_frame_fde fde;
+		unsigned long cur;
+		unsigned long func_addr, fde_addr;
+		int ret;
+
+		cur = sec->binary_search_table_start + i * entry_size;
+
+		scoped_user_read_access_size(table_start_ptr, table_size, Efault) {
+			/* Read function start address from table */
+			ret = read_encoded_pointer(sec, &cur,
+						   sec->binary_search_table_end,
+						   table_enc, &func_addr);

[Severity: High]
Does this bypass the Spectre v1 mitigations provided by the scoped access macro?

The scoped_user_read_access_size() macro shadows the table_start_ptr variable to enforce usage of a masked pointer. However, the code ignores that masked pointer and instead uses the unmasked cur pointer derived outside the scope:

Quoted 5 lines
    cur = sec->binary_search_table_start + i * entry_size;

When read_encoded_pointer() uses this unmasked pointer inside unsafe_get_user(), the masking mitigations are bypassed, allowing potential arbitrary speculative reads.

True. Will resolve as part of resolving the next finding.

Quoted 8 lines

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1258

Quoted 3 lines
kernel/unwind/eh_frame.c +2 -0
+			if (ret) {
+				dbg_sec_ehfh(cur, "table[%lu]: failed to read function start address\n", i);

[Severity: High]
Is it safe to call printk wrappers like dbg_sec_ehfh() or dbg_sec() while user access (UACCESS) is enabled?

Quoted 4 lines

Calling complex functions like printk inside a scoped_user_read_access_size() block can lead to deadlocks or kernel panics. It does internal locking, can sleep, and may clobber architecture-specific user access state like the SMAP AC flag.

Will resolve by introducing and using a safe_read_encoded_pointer() variant, similar to the safe_read_{fde|fre}() in the .sframe series. This also gets rid of the scoped user access region and thus resolves the previous finding as well.

Quoted 8 lines

kernel/unwind/eh_frame.c int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame) · line 1264

Quoted 7 lines
kernel/unwind/eh_frame.c +6 -0
+				return ret;
+			}
+			if (i && func_addr <= prev_func_addr) {
+				dbg_sec(".eh_frame_hdr: table[%lu]: not sorted\n", i);
+				return -EINVAL;
+			}

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#34 lore raw

RFC PATCH v2 15/24 unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section data on clone/fork

In reply to Jens Remus

When duplicating a process' virtual memory mappings also duplicate all of its registered .eh_frame_hdr sections stored in the per-mm maple tree to enable stacktracing using eh_frame of the child process.

---
 include/linux/eh_frame.h       |  7 +++++
 kernel/unwind/eh_frame.c       | 48 ++++++++++++++++++++++++++++++++++
 kernel/unwind/eh_frame_debug.h |  7 +++++
 mm/mmap.c                      |  5 ++++
 4 files changed, 67 insertions(+)
include/linux/eh_frame.h +7 -0
index 70b4f9d12828..3c88ab2f7858 100644
@@ -32,6 +32,7 @@ struct eh_frame_section {
 #define EH_FRAME_MT_FLAGS (MT_FLAGS_USE_RCU)
 
 #define INIT_MM_EH_FRAME .eh_frame_mt = MTREE_INIT(eh_frame_mt, EH_FRAME_MT_FLAGS),
+extern int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm);
 extern void eh_frame_free_mm(struct mm_struct *mm);
 
 extern int eh_frame_add_section(unsigned long eh_frame_hdr_start,
@@ -51,6 +52,12 @@ static inline bool current_has_eh_frame(void)
 #else /* !CONFIG_HAVE_UNWIND_USER_EH_FRAME */
 
 #define INIT_MM_EH_FRAME
+
+static inline int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+	return 0;
+}
+
 static inline void eh_frame_free_mm(struct mm_struct *mm) {}
 
 static inline int eh_frame_add_section(unsigned long eh_frame_hdr_start,
kernel/unwind/eh_frame.c +48 -0

54 lines not on this page · raw message

kernel/unwind/eh_frame_debug.h +7 -0
index bcb2d03ab9ab..e72e011ba539 100644
@@ -41,6 +41,12 @@ static inline void dbg_init(struct eh_frame_section *sec)
 		sec->filename = kstrdup("(vma unknown???)", GFP_KERNEL_ACCOUNT);
 }
 
+static inline void dbg_dup(struct eh_frame_section *sec, struct eh_frame_section *oldsec)
+{
+	if (oldsec->filename)
+		sec->filename = kstrdup(oldsec->filename, GFP_KERNEL_ACCOUNT);
+}
+
 static inline void dbg_free(struct eh_frame_section *sec)
 {
 	kfree(sec->filename);
@@ -53,6 +59,7 @@ static inline void dbg_free(struct eh_frame_section *sec)
 #define dbg_sec_ehfh(args...)		no_printk(args)
 
 static inline void dbg_init(struct eh_frame_section *sec) {}
+static inline void dbg_dup(struct eh_frame_section *sec, struct eh_frame_section *oldsec) {}
 static inline void dbg_free(struct eh_frame_section *sec) {}
 
 #endif /* !CONFIG_DYNAMIC_DEBUG */
mm/mmap.c +5 -0
index 2311ae7c2ff4..7715a799810e 100644
@@ -48,6 +48,7 @@
 #include <linux/sched/mm.h>
 #include <linux/ksm.h>
 #include <linux/memfd.h>
+#include <linux/eh_frame.h>
 
 #include <linux/uaccess.h>
 #include <asm/cacheflush.h>
@@ -1844,6 +1845,9 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
 			goto loop_out;
 		}
 	}
+	retval = eh_frame_dup_mm(mm, oldmm);
+	if (retval)
+		goto loop_out;
 	/* a new mm has just been created */
 	retval = arch_dup_mmap(oldmm, mm);
 loop_out:
@@ -1893,6 +1897,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
 			vm_unacct_memory(charge);
 		}
 		__mt_destroy(&mm->mm_mt);
+		eh_frame_free_mm(mm);
 		/*
 		 * The mm_struct is going to exit, but the locks will be dropped
 		 * first.  Set the mm_struct as unstable is advisable as it is
Signature · 1 line
2.53.0
#35 lore raw

Re: [RFC PATCH v2 15/24] unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section data on clone/fork

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Blindly duplicating all eh_frame sections ignores VMA inheritance rules (e.g., VM_DONTCOPY)…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Blindly duplicating all eh_frame sections ignores VMA inheritance rules (e.g., VM_DONTCOPY), leading to stale unwinding data.
- [Medium] Mixing goto-based error handling with guard() cleanup in the same function.
--

--- Patch [15]: [RFC PATCH v2 15/24] unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section data on clone/fork --- commit cedc4c84b3351e03d89a9e7fc338f35818891c5a
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section data on clone/fork

When duplicating a process' virtual memory mappings also duplicate all of its registered .eh_frame_hdr sections stored in the per-mm maple tree to enable stacktracing using eh_frame of the child process.

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
index b0be56e315080..1ac7308814a16 100644

[ ... ]

Quoted 7 lines
kernel/unwind/eh_frame.c +3 -0
@@ -1501,6 +1501,54 @@ int eh_frame_remove_section(unsigned long eh_frame_hdr_start)
 	return 0;
 }
 
+static void __eh_frame_dup_section(struct eh_frame_section *sec,
+				   struct eh_frame_section *oldsec)
+{

[ ... ]

kernel/unwind/eh_frame.c int eh_frame_remove_section(unsigned long eh_frame_hdr_start) · line 1528

Quoted 9 lines
kernel/unwind/eh_frame.c +9 -0
+}
+
+int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+	struct eh_frame_section *sec, *oldsec;
+	unsigned long index = 0;
+	int ret;
+
+	guard(srcu)(&eh_frame_srcu);

[Severity: Medium]
Is it acceptable to mix goto-based error handling with guard() in the same function? The cleanup guidelines suggest converting all resources to scope-based cleanup or none to avoid confusing ownership semantics.

kernel/unwind/eh_frame.c

Quoted 2 lines
kernel/unwind/eh_frame.c +2 -0
+
+	mt_for_each(&oldmm->eh_frame_mt, oldsec, index, ULONG_MAX) {

[Severity: High]
Does this unconditionally duplicate all eh_frame sections from the parent without checking if the underlying VMA was actually copied during the fork?

If a process sets a region to VM_DONTCOPY or VM_WIPEONFORK before forking, the child won't inherit the VMA, but it appears it will still inherit the eh_frame section here. If the child process later maps new executable code at the same address, could the unwinder use the parent's stale rules?

Quoted 20 lines
kernel/unwind/eh_frame.c +20 -0
+		sec = kzalloc(sizeof(*sec), GFP_KERNEL_ACCOUNT);
+		if (!sec)
+			return -ENOMEM;
+
+		__eh_frame_dup_section(sec, oldsec);
+
+		ret = mtree_insert_range(&mm->eh_frame_mt,
+					 sec->text_start,
+					 sec->text_end - 1,
+					 sec, GFP_KERNEL_ACCOUNT);
+		if (ret)
+			goto err_free;
+	}
+
+	return 0;
+
+err_free:
+	free_section(sec);
+	return ret;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
author
#36 lore raw

Re: [RFC PATCH v2 15/24] unwind_user/eh_frame: Duplicate registered .eh_frame[_hdr] section data on clone/fork

Quoted 14 lines · On 8/21/2026 10:09 PM, [email protected] wrote:

When duplicating a process' virtual memory mappings also duplicate all of its registered .eh_frame_hdr sections stored in the per-mm maple tree to enable stacktracing using eh_frame of the child process.

Quoted 9 lines
kernel/unwind/eh_frame.c +3 -0
@@ -1501,6 +1501,54 @@ int eh_frame_remove_section(unsigned long eh_frame_hdr_start)
 	return 0;
 }
 
+static void __eh_frame_dup_section(struct eh_frame_section *sec,
+				   struct eh_frame_section *oldsec)
+{

[ ... ]

Quoted 14 lines

kernel/unwind/eh_frame.c int eh_frame_remove_section(unsigned long eh_frame_hdr_start) · line 1528

Quoted 9 lines
kernel/unwind/eh_frame.c +9 -0
+}
+
+int eh_frame_dup_mm(struct mm_struct *mm, struct mm_struct *oldmm)
+{
+	struct eh_frame_section *sec, *oldsec;
+	unsigned long index = 0;
+	int ret;
+
+	guard(srcu)(&eh_frame_srcu);

[Severity: Medium]
Is it acceptable to mix goto-based error handling with guard() in the same function? The cleanup guidelines suggest converting all resources to scope-based cleanup or none to avoid confusing ownership semantics.

I think it is acceptable.

Quoted 8 lines

kernel/unwind/eh_frame.c

Quoted 3 lines
kernel/unwind/eh_frame.c +2 -0
+
+	mt_for_each(&oldmm->eh_frame_mt, oldsec, index, ULONG_MAX) {

[Severity: High]
Does this unconditionally duplicate all eh_frame sections from the parent without checking if the underlying VMA was actually copied during the fork?

Quoted 4 lines

If a process sets a region to VM_DONTCOPY or VM_WIPEONFORK before forking, the child won't inherit the VMA, but it appears it will still inherit the eh_frame section here. If the child process later maps new executable code at the same address, could the unwinder use the parent's stale rules?

Steven, could you please advise?

Quoted 13 lines
Quoted 13 lines
kernel/unwind/eh_frame.c +12 -0
+		sec = kzalloc(sizeof(*sec), GFP_KERNEL_ACCOUNT);
+		if (!sec)
+			return -ENOMEM;
+
+		__eh_frame_dup_section(sec, oldsec);
+
+		ret = mtree_insert_range(&mm->eh_frame_mt,
+					 sec->text_start,
+					 sec->text_end - 1,
+					 sec, GFP_KERNEL_ACCOUNT);
+		if (ret)
+			goto err_free;
Quoted 9 lines

kernel/unwind/eh_frame.c int eh_frame_remove_section(unsigned long eh_frame_hdr_start) · line 1550

Quoted 8 lines
kernel/unwind/eh_frame.c +8 -0
+	}
+
+	return 0;
+
+err_free:
+	free_section(sec);
+	return ret;
+}

Thanks and regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#37 lore raw

RFC PATCH v2 16/24 unwind_user/eh_frame: Ignore DW_CFA_GNU_args_size

In reply to Jens Remus

The DW_CFA_GNU_args_size DWARF CFI instruction is used by GCC to track the size of arguments pushed on the stack for exception handling purposes. It is not needed for stack tracing.

---

Notes (jremus):
This patch could be squashed into "unwind_user/eh_frame: Add support for reading .eh_frame section". Leaving separate for review.

 kernel/unwind/eh_frame.c | 11 +++++++++++
 kernel/unwind/eh_frame.h |  1 +
 2 files changed, 12 insertions(+)
kernel/unwind/eh_frame.c +11 -0
index 1ac7308814a1..d185187b0c34 100644
@@ -980,6 +980,17 @@ static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
 			break;
 		}
 
+		case DW_CFA_GNU_args_size: {
+			unsigned long args_size;
+
+			ret = read_uleb128(&cur, end, &args_size);
+			if (ret)
+				return ret;
+
+			/* Ignore DW_CFA_GNU_args_size */
+			break;
+		}
+
 		case DW_CFA_remember_state:
 			if (ctx->stack_depth >= EH_FRAME_MAX_STATE_STACK)
 				return -EINVAL;
kernel/unwind/eh_frame.h +1 -0
index 9a0c71102742..a43560486283 100644
@@ -29,6 +29,7 @@
 #define DW_CFA_val_offset		0x14
 #define DW_CFA_val_offset_sf		0x15
 #define DW_CFA_val_expression		0x16
+#define DW_CFA_GNU_args_size		0x2e
 
 /* Helpers for CFI opcodes */
 #define DW_CFA_opcode(insn)		((insn) & 0xc0)
Signature · 1 line
2.53.0
#38 lore raw

Re: [RFC PATCH v2 16/24] unwind_user/eh_frame: Ignore DW_CFA_GNU_args_size

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

The DW_CFA_GNU_args_size DWARF CFI instruction is used by GCC to track the size of arguments pushed on the stack for exception handling purposes. It is not needed for stack tracing.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author
#39 lore raw

RFC PATCH v2 17/24 unwind_user/eh_frame: Add support for DWARF expressions

In reply to Jens Remus

Enable architectures to handle DWARF expressions in DWARF CFI instructions DW_CFA_def_cfa_expression, DW_CFA_expression, and DW_CFA_val_expression. Limit the maximum expression length to a reasonable size, while enabling architectures to override the limit.

Architectures are supposed to only handle specific known expressions or expression patterns, not to implement a stack-based expression evaluation machinery.

---

Notes (jremus):
Changes in RFC v2:
- Fix expression length overflow checks. (Sashiko AI)

 include/asm-generic/unwind_user_eh_frame.h | 41 ++++++++++++
 include/linux/unwind_user_eh_frame_types.h | 37 +++++++++++
 kernel/unwind/eh_frame.c                   | 72 +++++++++++-----------
 3 files changed, 114 insertions(+), 36 deletions(-)
 create mode 100644 include/linux/unwind_user_eh_frame_types.h
include/asm-generic/unwind_user_eh_frame.h +41 -0
index da238557605b..a3cd3b3b55c7 100644
@@ -2,6 +2,8 @@
 #ifndef _ASM_GENERIC_UNWIND_USER_EH_FRAME_H
 #define _ASM_GENERIC_UNWIND_USER_EH_FRAME_H
 
+#include <linux/unwind_user_eh_frame_types.h>
+
 #ifndef EH_FRAME_MAX_CIE_LENGTH
 #define EH_FRAME_MAX_CIE_LENGTH 128
 #endif
@@ -14,6 +16,10 @@
 #define EH_FRAME_MAX_AUGSTR_LENGTH 16
 #endif
 
+#ifndef EH_FRAME_MAX_EXPRESSION_LENGTH
+#define EH_FRAME_MAX_EXPRESSION_LENGTH 32
+#endif
+
 #ifndef EH_FRAME_MAX_STATE_STACK
 #define EH_FRAME_MAX_STATE_STACK 1
 #endif
@@ -47,5 +53,40 @@ static inline bool eh_frame_reject_sp_rule(void)
 #define eh_frame_reject_sp_rule eh_frame_reject_sp_rule
 #endif
 
+#ifndef eh_frame_do_def_cfa_expression
+static inline int eh_frame_do_def_cfa_expression(const char *expr,
+						 int size,
+						 unsigned long ip,
+						 struct eh_frame_reg_state *reg_state)
+{
+	return -EOPNOTSUPP;
+}
+#define eh_frame_do_def_cfa_expression eh_frame_do_def_cfa_expression
+#endif
+
+#ifndef eh_frame_do_expression
+static inline int eh_frame_do_expression(unsigned int reg,
+					 const char *expr,
+					 int size,
+					 unsigned long ip,
+					 struct eh_frame_reg_state *reg_state)
+{
+	return -EOPNOTSUPP;
+}
+#define eh_frame_do_expression eh_frame_do_expression
+#endif
+
+#ifndef eh_frame_do_val_expression
+static inline int eh_frame_do_val_expression(unsigned int reg,
+					     const char *expr,
+					     int size,
+					     unsigned long ip,
+					     struct eh_frame_reg_state *reg_state)
+{
+	return -EOPNOTSUPP;
+}
+#define eh_frame_do_val_expression eh_frame_do_val_expression
+#endif
+
 #endif /* _ASM_GENERIC_UNWIND_USER_EH_FRAME_H */
 
include/linux/unwind_user_eh_frame_types.h new file +37 -0
new file mode 100644
index 000000000000..e9f9d1abb76f
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_UNWIND_USER_EH_FRAME_TYPES_H
+#define _LINUX_UNWIND_USER_EH_FRAME_TYPES_H
+
+enum eh_frame_cfa_rule {
+	CFA_UNDEFINED,		/* unrecoverable */
+	CFA_REG_OFFSET,		/* CFA = reg + offset */
+};
+
+enum eh_frame_reg_rule {
+	REG_UNDEFINED_IMPLICIT,	/* reg = reg */
+	REG_UNDEFINED_EXPLICIT,	/* unrecoverable; RA: outermost frame */
+	REG_SAME_VALUE,		/* reg = reg; TODO: reset to CIE initial CFI */
+	REG_OFFSET,		/* reg = *(CFA + offset) */
+	REG_VAL_OFFSET,		/* reg = CFA + offset */
+	REG_REGISTER,		/* reg = other_reg */
+};
+
+enum eh_frame_reg_index {
+	FP_IDX,			/* frame pointer (FP) */
+	RA_IDX,			/* return address (RA) */
+	NR_REGS
+};
+
+struct eh_frame_reg_state {
+	/* CFA recovery rule */
+	enum eh_frame_cfa_rule cfa_rule;
+	unsigned long cfa_regnum;
+	long cfa_offset;
+
+	/* FP and RA recovery rules (SP uses implicit recovery) */
+	enum eh_frame_reg_rule reg_rule[NR_REGS];
+	unsigned long reg_regnum[NR_REGS];
+	long reg_offset[NR_REGS];
+};
+
+#endif /* _LINUX_UNWIND_USER_EH_FRAME_TYPES_H */
kernel/unwind/eh_frame.c +36 -36

99 lines not on this page · raw message

Signature · 1 line
2.53.0
#40 lore raw

Re: [RFC PATCH v2 17/24] unwind_user/eh_frame: Add support for DWARF expressions

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Unaligned stack buffer passed to unsafe_copy_from_user() causes undefined behavior --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Unaligned stack buffer passed to unsafe_copy_from_user() causes undefined behavior --

--- Patch [17]: [RFC PATCH v2 17/24] unwind_user/eh_frame: Add support for DWARF expressions --- commit 38e0b8657cdb2bd1e95dd3bc74b1aa71e0063c9a
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Add support for DWARF expressions

Enable architectures to handle DWARF expressions in DWARF CFI instructions DW_CFA_def_cfa_expression, DW_CFA_expression, and DW_CFA_val_expression. Limit the maximum expression length to a reasonable size, while enabling architectures to override the limit.

Quoted 12 lines
kernel/unwind/eh_frame.c +4 -0
index d185187b0c34e..a98804dbe5886 100644
@@ -803,6 +772,27 @@ static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
 			break;
 		}
 
+		case DW_CFA_def_cfa_expression: {
+			unsigned long expr_len;
+			char expr[EH_FRAME_MAX_EXPRESSION_LENGTH];
+

kernel/unwind/eh_frame.c static __always_inline int __do_cfi_insn(struct eh_frame_section *sec, · line 788

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+			ret = read_uleb128(&cur, end, &expr_len);
+			if (ret)
+				return ret;
+
+			if (cur + expr_len < cur || cur + expr_len > end)
+				return -EINVAL;
+
+			if (expr_len > sizeof(expr))
+				return -EOPNOTSUPP;
+			unsafe_copy_from_user(&expr, (void __user *)cur, expr_len, Efault);

[Severity: Low]
Does this unaligned stack buffer passed to unsafe_copy_from_user() cause undefined behavior?

Passing a 1-byte aligned char array to unsafe_copy_from_user() violates C alignment rules. In architecture-specific implementations, the macro explicitly casts the destination pointer to larger integer types (e.g., u64 *) to perform optimized word-sized memory assignments. Dereferencing an unaligned pointer is Undefined Behavior in C.

While x86 hardware gracefully handles unaligned memory accesses, the compiler's optimizer is legally allowed to assume u64 * is 8-byte aligned and could emit trapping instructions. Furthermore, if this generic .eh_frame code is later enabled on architectures with strict hardware alignment enforcement, it will immediately trigger alignment exceptions.

Could this buffer be declared with __aligned(8) or as an array of u64?

Quoted 7 lines
kernel/unwind/eh_frame.c +7 -0
+			ret = eh_frame_do_def_cfa_expression(expr, expr_len, target_ip, &ctx->state);
+			if (ret)
+				return ret;
+			cur += expr_len;
+			break;
+		}
+

[ ... ]

Quoted 6 lines
kernel/unwind/eh_frame.c +1 -1
@@ -970,12 +960,22 @@ static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
 			if (ret)
 				return ret;
 
-			if (cur + expr_len > end)
+			if (cur + expr_len < cur || cur + expr_len > end)

kernel/unwind/eh_frame.c static __always_inline int __do_cfi_insn(struct eh_frame_section *sec, · line 971

Quoted 10 lines
kernel/unwind/eh_frame.c +5 -2
 				return -EINVAL;
 
-			if (reg == EH_FRAME_REG_SP || reg == EH_FRAME_REG_FP || reg == EH_FRAME_REG_RA)
-				return -EOPNOTSUPP;
+			if (reg == EH_FRAME_REG_SP || reg == EH_FRAME_REG_FP || reg == EH_FRAME_REG_RA) {
+				char expr[EH_FRAME_MAX_EXPRESSION_LENGTH];
 
+				if (expr_len > sizeof(expr))
+					return -EOPNOTSUPP;
+				unsafe_copy_from_user(&expr, (void __user *)cur, expr_len, Efault);

[Severity: Low]
Is this the same alignment issue as above?

Could this buffer also be declared with __aligned(8) or as an array of u64?

Quoted 10 lines
kernel/unwind/eh_frame.c +7 -0
+				if (opcode == DW_CFA_expression)
+					ret = eh_frame_do_expression(reg, expr, expr_len, target_ip, &ctx->state);
+				else
+					ret = eh_frame_do_val_expression(reg, expr, expr_len, target_ip, &ctx->state);
+				if (ret)
+					return ret;
+			}
 			cur += expr_len;
 			break;
 		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author
#41 lore raw

Re: [RFC PATCH v2 17/24] unwind_user/eh_frame: Add support for DWARF expressions

Quoted 33 lines · On 8/21/2026 10:18 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Enable architectures to handle DWARF expressions in DWARF CFI instructions DW_CFA_def_cfa_expression, DW_CFA_expression, and DW_CFA_val_expression. Limit the maximum expression length to a reasonable size, while enabling architectures to override the limit.

Quoted 10 lines
kernel/unwind/eh_frame.c +4 -0
@@ -803,6 +772,27 @@ static __always_inline int __do_cfi_insn(struct eh_frame_section *sec,
 			break;
 		}
 
+		case DW_CFA_def_cfa_expression: {
+			unsigned long expr_len;
+			char expr[EH_FRAME_MAX_EXPRESSION_LENGTH];
+

kernel/unwind/eh_frame.c static __always_inline int __do_cfi_insn(struct eh_frame_section *sec, · line 788

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+			ret = read_uleb128(&cur, end, &expr_len);
+			if (ret)
+				return ret;
+
+			if (cur + expr_len < cur || cur + expr_len > end)
+				return -EINVAL;
+
+			if (expr_len > sizeof(expr))
+				return -EOPNOTSUPP;
+			unsafe_copy_from_user(&expr, (void __user *)cur, expr_len, Efault);

[Severity: Low]
Does this unaligned stack buffer passed to unsafe_copy_from_user() cause undefined behavior?

Quoted 5 lines

Passing a 1-byte aligned char array to unsafe_copy_from_user() violates C alignment rules. In architecture-specific implementations, the macro explicitly casts the destination pointer to larger integer types (e.g., u64 *) to perform optimized word-sized memory assignments. Dereferencing an unaligned pointer is Undefined Behavior in C.

AFAIK [unsafe_]copy_from_user() has no source or destination alignment requirements.

The reported concern appears to be specific to the proposed x86 implementation (see next patch "[RFC PATCH v2 18/24] x86/uaccess: Add unsafe_copy_from_user() implementation"), which casts the pointers to u64 *, u32 *, and so on.

Quoted 6 lines

While x86 hardware gracefully handles unaligned memory accesses, the compiler's optimizer is legally allowed to assume u64 * is 8-byte aligned and could emit trapping instructions. Furthermore, if this generic .eh_frame code is later enabled on architectures with strict hardware alignment enforcement, it will immediately trigger alignment exceptions.

This is currently enforced very early by "[RFC PATCH v2 07/24] unwind_user/eh_frame: Add support for reading .eh_frame_hdr section":

	/*
	 * Unaligned access to .eh_frame[_hdr] fields using
	 * unsafe_get_user() via UNSAFE_GET_USER_INC()
	 */
	BUILD_BUG_ON(!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS));
Quoted 2 lines

Could this buffer be declared with __aligned(8) or as an array of u64?

I don't think this makes sense.

Quoted 8 lines

kernel/unwind/eh_frame.c static __always_inline int __do_cfi_insn(struct eh_frame_section *sec, · line 795

Quoted 8 lines
kernel/unwind/eh_frame.c +7 -0
+			ret = eh_frame_do_def_cfa_expression(expr, expr_len, target_ip, &ctx->state);
+			if (ret)
+				return ret;
+			cur += expr_len;
+			break;
+		}
+

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#42 lore raw

RFC PATCH v2 18/24 x86/uaccess: Add unsafe_copy_from_user() implementation

In reply to Jens Remus

From: Josh Poimboeuf <[email protected]>

Add an x86 implementation of unsafe_copy_from_user() similar to the existing unsafe_copy_to_user().

---

Notes (jremus):
Cherry-picked from "[PATCH v16 00/20] unwind_deferred: Implement sframe handling" series:
https://lore.kernel.org/all/[email protected]/

 arch/x86/include/asm/uaccess.h | 39 +++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 10 deletions(-)
arch/x86/include/asm/uaccess.h +29 -10
index 3a0dd3c2b233..235886106f31 100644
@@ -598,7 +598,7 @@ _label:									\
  * We want the unsafe accessors to always be inlined and use
  * the error labels - thus the macro games.
  */
-#define unsafe_copy_loop(dst, src, len, type, label)				\
+#define unsafe_copy_to_user_loop(dst, src, len, type, label)			\
 	while (len >= sizeof(type)) {						\
 		unsafe_put_user(*(type *)(src),(type __user *)(dst),label);	\
 		dst += sizeof(type);						\
@@ -606,15 +606,34 @@ _label:									\
 		len -= sizeof(type);						\
 	}
 
-#define unsafe_copy_to_user(_dst,_src,_len,label)			\
-do {									\
-	char __user *__ucu_dst = (_dst);				\
-	const char *__ucu_src = (_src);					\
-	size_t __ucu_len = (_len);					\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label);	\
+#define unsafe_copy_to_user(_dst, _src, _len, label)				\
+do {										\
+	void __user *__dst = (_dst);						\
+	const void *__src = (_src);						\
+	size_t __len = (_len);							\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u8,  label);		\
+} while (0)
+
+#define unsafe_copy_from_user_loop(dst, src, len, type, label)			\
+	while (len >= sizeof(type)) {						\
+		unsafe_get_user(*(type *)(dst), (type __user *)(src), label);	\
+		dst += sizeof(type);						\
+		src += sizeof(type);						\
+		len -= sizeof(type);						\
+	}
+
+#define unsafe_copy_from_user(_dst, _src, _len, label)				\
+do {										\
+	void *__dst = (_dst);							\
+	const void __user *__src = (_src);					\
+	size_t __len = (_len);							\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u8,  label);		\
 } while (0)
 
 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
Signature · 1 line
2.53.0
#43 lore raw

Re: [RFC PATCH v2 18/24] x86/uaccess: Add unsafe_copy_from_user() implementation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Macro variable shadowing risk in `unsafe_copy_to_user` and `unsafe_copy_from_user`. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Macro variable shadowing risk in unsafe_copy_to_user and unsafe_copy_from_user. --

--- Patch [18]: [RFC PATCH v2 18/24] x86/uaccess: Add unsafe_copy_from_user() implementation --- commit 6d0ae2cda144449266a35cb0d45ee49364b75b97
Author: Josh Poimboeuf <[email protected]>

x86/uaccess: Add unsafe_copy_from_user() implementation

This commit adds an x86 implementation of unsafe_copy_from_user() similar to the existing unsafe_copy_to_user() macro. It also updates the existing unsafe_copy_to_user() macro to use shorter internal variable names.

Quoted 4 lines
arch/x86/include/asm/uaccess.h +0 -0
index 3a0dd3c2b233e..235886106f31e 100644

[ ... ]

Quoted 4 lines
arch/x86/include/asm/uaccess.h +0 -4
-#define unsafe_copy_to_user(_dst,_src,_len,label)			\
-do {									\
-	char __user *__ucu_dst = (_dst);				\
-	const char *__ucu_src = (_src);					\

arch/x86/include/asm/uaccess.h _label: \ · line 613

Quoted 10 lines
arch/x86/include/asm/uaccess.h +5 -5
-	size_t __ucu_len = (_len);					\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label);	\
+#define unsafe_copy_to_user(_dst, _src, _len, label)				\
+do {										\
+	void __user *__dst = (_dst);						\
+	const void *__src = (_src);						\
+	size_t __len = (_len);							\

[Severity: Medium]
Could this macro change cause dangerous variable shadowing?

By renaming the internal variables from __ucu_dst to generic names like __dst, __src, and __len, if a caller passes arguments with those exact names, the macro will expand to something like void __user *__dst = (__dst);. This results in self-initialization using uninitialized stack memory, which could lead to arbitrary memory reads or writes when used with SMAP disabled.

Quoted 9 lines
arch/x86/include/asm/uaccess.h +9 -0
+	unsafe_copy_to_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u8,  label);		\
+} while (0)
+
+#define unsafe_copy_from_user_loop(dst, src, len, type, label)			\
+	while (len >= sizeof(type)) {						\
+		unsafe_get_user(*(type *)(dst), (type __user *)(src), label);	\

arch/x86/include/asm/uaccess.h _label: \ · line 632

Quoted 10 lines
arch/x86/include/asm/uaccess.h +10 -0
+		dst += sizeof(type);						\
+		src += sizeof(type);						\
+		len -= sizeof(type);						\
+	}
+
+#define unsafe_copy_from_user(_dst, _src, _len, label)				\
+do {										\
+	void *__dst = (_dst);							\
+	const void __user *__src = (_src);					\
+	size_t __len = (_len);							\

[Severity: Medium]
Does this new macro suffer from the same variable shadowing risk?

Using generic internal names like __dst, __src, and __len might cause accidental self-initialization if a caller happens to use the same variable names for the arguments. Should we use more uniquely prefixed names here similar to the old __ucu_dst?

Quoted 5 lines
arch/x86/include/asm/uaccess.h +4 -0
+	unsafe_copy_from_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u8,  label);		\
 } while (0)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
author
#44 lore raw

Re: [RFC PATCH v2 18/24] x86/uaccess: Add unsafe_copy_from_user() implementation

Quoted 24 lines · On 8/21/2026 10:11 PM, [email protected] wrote:

This commit adds an x86 implementation of unsafe_copy_from_user() similar to the existing unsafe_copy_to_user() macro. It also updates the existing unsafe_copy_to_user() macro to use shorter internal variable names.

Quoted 6 lines
arch/x86/include/asm/uaccess.h +0 -4
 
-#define unsafe_copy_to_user(_dst,_src,_len,label)			\
-do {									\
-	char __user *__ucu_dst = (_dst);				\
-	const char *__ucu_src = (_src);					\

arch/x86/include/asm/uaccess.h _label: \ · line 613

Quoted 10 lines
arch/x86/include/asm/uaccess.h +5 -5
-	size_t __ucu_len = (_len);					\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label);	\
-	unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label);	\
+#define unsafe_copy_to_user(_dst, _src, _len, label)				\
+do {										\
+	void __user *__dst = (_dst);						\
+	const void *__src = (_src);						\
+	size_t __len = (_len);							\

[Severity: Medium]
Could this macro change cause dangerous variable shadowing?

Quoted 5 lines

By renaming the internal variables from __ucu_dst to generic names like __dst, __src, and __len, if a caller passes arguments with those exact names, the macro will expand to something like void __user *__dst = (__dst);. This results in self-initialization using uninitialized stack memory, which could lead to arbitrary memory reads or writes when used with SMAP disabled.

IIRC Steven advised to disregard Sashiko's concerns and leave Josh's commit as-is for now. Any objections?

Quoted 24 lines
Quoted 10 lines
arch/x86/include/asm/uaccess.h +9 -0
+	unsafe_copy_to_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_to_user_loop(__dst, __src, __len, u8,  label);		\
+} while (0)
+
+#define unsafe_copy_from_user_loop(dst, src, len, type, label)			\
+	while (len >= sizeof(type)) {						\
+		unsafe_get_user(*(type *)(dst), (type __user *)(src), label);	\

arch/x86/include/asm/uaccess.h _label: \ · line 632

Quoted 10 lines
arch/x86/include/asm/uaccess.h +10 -0
+		dst += sizeof(type);						\
+		src += sizeof(type);						\
+		len -= sizeof(type);						\
+	}
+
+#define unsafe_copy_from_user(_dst, _src, _len, label)				\
+do {										\
+	void *__dst = (_dst);							\
+	const void __user *__src = (_src);					\
+	size_t __len = (_len);							\

[Severity: Medium]
Does this new macro suffer from the same variable shadowing risk?

Quoted 4 lines

Using generic internal names like __dst, __src, and __len might cause accidental self-initialization if a caller happens to use the same variable names for the arguments. Should we use more uniquely prefixed names here similar to the old __ucu_dst?

Likewise.

Quoted 6 lines

arch/x86/include/asm/uaccess.h _label: \ · line 637

Quoted 6 lines
arch/x86/include/asm/uaccess.h +4 -0
+	unsafe_copy_from_user_loop(__dst, __src, __len, u64, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u32, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u16, label);		\
+	unsafe_copy_from_user_loop(__dst, __src, __len, u8,  label);		\
 } while (0)

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
author
#45 lore raw

RFC PATCH v2 19/24 unwind_user/eh_frame/x86: Enable eh_frame unwinding on x86

In reply to Jens Remus

The x86-64 .eh_frame implementation works well with GCC and Clang generated code. Enable it.

---
 arch/x86/Kconfig                            |  1 +
 arch/x86/include/asm/unwind_user.h          | 40 +++++++++++++++++++++
 arch/x86/include/asm/unwind_user_eh_frame.h | 22 ++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 arch/x86/include/asm/unwind_user_eh_frame.h
arch/x86/Kconfig +1 -0
index bdad90f210e4..fe7919915084 100644
@@ -300,6 +300,7 @@ config X86
 	select HAVE_SYSCALL_TRACEPOINTS
 	select HAVE_UACCESS_VALIDATION		if HAVE_OBJTOOL
 	select HAVE_UNSTABLE_SCHED_CLOCK
+	select HAVE_UNWIND_USER_EH_FRAME	if X86_64
 	select HAVE_UNWIND_USER_FP		if X86_64
 	select HAVE_USER_RETURN_NOTIFIER
 	select HAVE_GENERIC_VDSO
arch/x86/include/asm/unwind_user.h +40 -0
index c96645c824d1..df1966bcb0f6 100644
@@ -4,6 +4,7 @@
 
 #ifdef CONFIG_UNWIND_USER
 
+#include <linux/sched/task_stack.h>
 #include <asm/ptrace.h>
 #include <asm/uprobes.h>
 
@@ -15,6 +16,45 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 	return user_64bit_mode(regs) ? 8 : 4;
 }
 
+#ifdef CONFIG_X86_64
+
+static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
+{
+	struct pt_regs *regs = task_pt_regs(current);
+
+	/* TODO: eh_frame is currently limited to x86-64 */
+	if (!user_64bit_mode(regs))
+		return -EINVAL;
+
+	switch (regnum) {
+	/* DWARF register numbers 0..15 */
+	case  0: *val = regs->ax; break;
+	case  1: *val = regs->dx; break;
+	case  2: *val = regs->cx; break;
+	case  3: *val = regs->bx; break;
+	case  4: *val = regs->si; break;
+	case  5: *val = regs->di; break;
+	case  6: *val = regs->bp; break;
+	case  7: *val = regs->sp; break;
+	case  8: *val = regs->r8; break;
+	case  9: *val = regs->r9; break;
+	case 10: *val = regs->r10; break;
+	case 11: *val = regs->r11; break;
+	case 12: *val = regs->r12; break;
+	case 13: *val = regs->r13; break;
+	case 14: *val = regs->r14; break;
+	case 15: *val = regs->r15; break;
+	default:
+		pr_debug("%s (%d): %s(%u): unsupported register number\n",
+			 current->comm, current->pid, __func__, regnum);
+		return -EINVAL;
+	}
+	return 0;
+}
+#define unwind_user_get_reg unwind_user_get_reg
+
+#endif /* CONFIG_X86_64 */
+
 #endif /* CONFIG_UNWIND_USER */
 
 #ifdef CONFIG_HAVE_UNWIND_USER_FP
arch/x86/include/asm/unwind_user_eh_frame.h new file +22 -0
new file mode 100644
index 000000000000..fdccbda2fe4b
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_UNWIND_USER_EH_FRAME_H
+#define _ASM_X86_UNWIND_USER_EH_FRAME_H
+
+#ifdef CONFIG_X86_64
+
+#define EH_FRAME_REG_SP	7	/* designated stack pointer register */
+#define EH_FRAME_REG_FP	6	/* designated frame pointer register */
+#define EH_FRAME_REG_RA	16	/* (pseudo) return address register */
+
+/* Instructions must be 1-byte aligned */
+#define EH_FRAME_MAX_CODE_ALIGN 1
+
+/* Stack grows towards lower addresses and SP must be 8-byte aligned */
+#define EH_FRAME_MIN_DATA_ALIGN -8
+#define EH_FRAME_MAX_DATA_ALIGN -1
+
+#endif
+
+#include <asm-generic/unwind_user_eh_frame.h>
+
+#endif /* _ASM_X86_UNWIND_USER_EH_FRAME_H */
Signature · 1 line
2.53.0
#46 lore raw

Re: [RFC PATCH v2 19/24] unwind_user/eh_frame/x86: Enable eh_frame unwinding on x86

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Enabling `.eh_frame` unwinding on x86_64 breaks user stack traces for 32-bit compat binaries due…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Enabling .eh_frame unwinding on x86_64 breaks user stack traces for 32-bit compat binaries due to incorrect error handling in the generic unwind loop. --

--- Patch [19]: [RFC PATCH v2 19/24] unwind_user/eh_frame/x86: Enable eh_frame unwinding on x86 --- commit 26326eedb731dafef6946d5d887c188233f39043
Author: Jens Remus <[email protected]>

unwind_user/eh_frame/x86: Enable eh_frame unwinding on x86

This patch enables .eh_frame user stack unwinding for x86-64 by selecting HAVE_UNWIND_USER_EH_FRAME and providing the necessary register accessors.

Quoted 4 lines
arch/x86/include/asm/unwind_user.h +0 -0
index c96645c824d1b..df1966bcb0f6e 100644

[ ... ]

arch/x86/include/asm/unwind_user.h static inline int unwind_user_word_size(struct pt_regs *regs) · line 27

Quoted 13 lines
arch/x86/include/asm/unwind_user.h +9 -0
@@ -15,6 +16,45 @@ static inline int unwind_user_word_size(struct pt_regs *regs)
 	return user_64bit_mode(regs) ? 8 : 4;
 }
 
+#ifdef CONFIG_X86_64
+
+static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
+{
+	struct pt_regs *regs = task_pt_regs(current);
+
+	/* TODO: eh_frame is currently limited to x86-64 */
+	if (!user_64bit_mode(regs))
+		return -EINVAL;

[Severity: High]
Does returning -EINVAL here break the stack trace fallback mechanism for 32-bit compat tasks?

In kernel/unwind/user.c:unwind_user_next(), the generic unwinder only continues to the next method (like the frame pointer unwinder) if it receives -ENOENT:

kernel/unwind/user.c:unwind_user_next() {
...

        case UNWIND_USER_TYPE_EH_FRAME:
                switch (unwind_user_next_eh_frame(state)) {
                case 0:
                        return 0;
                case -ENOENT:
                        continue;       /* Try next method. */
                default:
                        state->done = true;
                }
                break;

...
}

By returning -EINVAL for 32-bit tasks, the unwinder hits the default case and aborts entirely by setting state->done = true. This truncates the user stack trace instead of safely falling back to the FP unwinder.

Would returning -ENOENT be more appropriate here when the current task is not 64-bit to ensure the fallback pipeline continues correctly?

Quoted 4 lines
arch/x86/include/asm/unwind_user.h +4 -0
+
+	switch (regnum) {
+	/* DWARF register numbers 0..15 */
+	case  0: *val = regs->ax; break;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
author
#47 lore raw

RFC PATCH v2 20/24 unwind_user/eh_frame/x86: Handle PLT expressions

In reply to Jens Remus

Add x86-specific support for handling the CFA expression found in GNU linker generated .eh_frame for PLT.

This enables unwinding through GNU linker generated PLT entries on i386 and x86-64, unless linker option --no-ld-generated-unwind-info is used.

---
 arch/x86/include/asm/unwind_user_eh_frame.h | 68 ++++++++++++++++++++-
 kernel/unwind/eh_frame.c                    |  3 +-
 kernel/unwind/eh_frame.h                    | 14 +++++
 3 files changed, 83 insertions(+), 2 deletions(-)
arch/x86/include/asm/unwind_user_eh_frame.h +67 -1
index fdccbda2fe4b..8268eb1a0ff5 100644
@@ -2,6 +2,8 @@
 #ifndef _ASM_X86_UNWIND_USER_EH_FRAME_H
 #define _ASM_X86_UNWIND_USER_EH_FRAME_H
 
+#include <linux/unwind_user_eh_frame_types.h>
+
 #ifdef CONFIG_X86_64
 
 #define EH_FRAME_REG_SP	7	/* designated stack pointer register */
@@ -15,7 +17,71 @@
 #define EH_FRAME_MIN_DATA_ALIGN -8
 #define EH_FRAME_MAX_DATA_ALIGN -1
 
-#endif
+#endif /* CONFIG_X86_64 */
+
+static inline int memcmp_masked(const void *s1, const void *s2,
+				const void *mask, size_t n)
+{
+	const unsigned char *p1 = s1, *p2 = s2, *m = mask;
+	int res = 0;
+
+	while (n--)
+		if ((res = (*p1++ ^ *p2++) & *m++))
+			break;
+
+	return res;
+}
+
+static inline int eh_frame_do_def_cfa_expression(const char *expr,
+						 int size,
+						 unsigned long ip,
+						 struct eh_frame_reg_state *reg_state)
+{
+	/*
+	 * PLT CFA expression:
+	 *
+	 * DW_OP_breg<SP> + <SP_offset>	// 4 (ESP) + 4 or 7 (RSP) + 8
+	 * DW_OP_breg<IP> + 0		// 8 (EIP) or 16 (RIP)
+	 * DW_OP_lit15
+	 * DW_OP_and
+	 * DW_OP_lit<N>
+	 * DW_OP_ge
+	 * DW_OP_lit<shift>		// 2 or 3
+	 * DW_OP_shl
+	 * DW_OP_plus
+	 *
+	 * CFA = (SP + offset) + (((IP & 0xf) >= N) << shift)
+	 */
+	static const char plt_expr[] = {0x00,0x00,0x00,0x00,0x3f,0x1a,0x30,0x2a,0x30,0x24,0x22};
+	static const char plt_mask[] = {0x00,0xf0,0x00,0xff,0xff,0xff,0xf0,0xff,0xf0,0xff,0xff};
+
+	if (size == sizeof(plt_expr) &&
+	    !memcmp_masked(expr, plt_expr, plt_mask, sizeof(plt_expr))) {
+		unsigned char sp_op = expr[0];
+		unsigned char sp_offset = expr[1] & 0x0f;
+		unsigned char ip_op = expr[2];
+		unsigned char n = DW_OP_lit_value(expr[6]);
+		unsigned char shift = DW_OP_lit_value(expr[8]);
+		unsigned char sp_reg, ip_reg;
+
+		if (!DW_OP_is_breg(sp_op) || !DW_OP_is_breg(ip_op))
+			return -EOPNOTSUPP;
+
+		sp_reg = DW_OP_breg_register(sp_op);
+		ip_reg = DW_OP_breg_register(ip_op);
+		if (sp_reg != EH_FRAME_REG_SP || ip_reg != EH_FRAME_REG_RA)
+			return -EOPNOTSUPP;
+
+		/* CFA = (SP + SP_offset) + (((IP & 0xf) >= N) << shift) */
+		reg_state->cfa_rule = CFA_REG_OFFSET;
+		reg_state->cfa_regnum = EH_FRAME_REG_SP;
+		reg_state->cfa_offset = sp_offset + (((ip & 15) >= n) << shift);
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+#define eh_frame_do_def_cfa_expression eh_frame_do_def_cfa_expression
 
 #include <asm-generic/unwind_user_eh_frame.h>
 
kernel/unwind/eh_frame.c +2 -1
index a98804dbe588..0a81ba1a0377 100644
@@ -15,11 +15,12 @@
 #include <linux/types.h>
 #include <linux/unwind_user_types.h>
 #include <linux/unwind_user_eh_frame_types.h>
-#include <asm/unwind_user_eh_frame.h>
 
 #include "eh_frame.h"
 #include "eh_frame_debug.h"
 
+#include <asm/unwind_user_eh_frame.h>
+
 struct eh_frame_cfi_context {
 	struct eh_frame_reg_state state;
 	struct eh_frame_reg_state stack[EH_FRAME_MAX_STATE_STACK];
kernel/unwind/eh_frame.h +14 -0
index a43560486283..2f87d1b32bbc 100644
@@ -60,6 +60,20 @@
 #define DW_EH_PE_format(encoding)	((encoding) & 0x0f)
 #define DW_EH_PE_application(encoding)	((encoding) & 0x70)
 
+/* DWARF expression operations */
+#define DW_OP_lit0	0x30
+/* ... */
+#define DW_OP_lit31	0x4f
+#define DW_OP_breg0	0x70
+/* ... */
+#define DW_OP_breg31	0x8f
+
+/* Helpers for DWARF expression operations */
+#define DW_OP_is_lit(op)		((op) >= DW_OP_lit0 && (op) <= DW_OP_lit31)
+#define DW_OP_is_breg(op)		((op) >= DW_OP_breg0 && (op) <= DW_OP_breg31)
+#define DW_OP_lit_value(op)		((op) - DW_OP_lit0)
+#define DW_OP_breg_register(op)		((op) - DW_OP_breg0)
+
 /* CIE/FDE constants */
 #define EH_FRAME_CIE_ID			0
 #define EH_FRAME_DWARF64_LENGTH		0xffffffff
Signature · 1 line
2.53.0
#48 lore raw

Re: [RFC PATCH v2 20/24] unwind_user/eh_frame/x86: Handle PLT expressions

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch claims to support i386 PLT unwinding but fundamentally breaks it by hardcoding x86-64…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch claims to support i386 PLT unwinding but fundamentally breaks it by hardcoding x86-64 DWARF register macros. --

--- Patch [20]: [RFC PATCH v2 20/24] unwind_user/eh_frame/x86: Handle PLT expressions --- commit 75d2af7b6df62f61cc44905f96f37d91b80e466a
Author: Jens Remus <[email protected]>

unwind_user/eh_frame/x86: Handle PLT expressions

This commit adds x86-specific support for handling the CFA expression found in GNU linker generated .eh_frame for PLT. It intends to enable unwinding through GNU linker generated PLT entries on both i386 and x86-64.

Quoted 4 lines
arch/x86/include/asm/unwind_user_eh_frame.h +0 -0
index fdccbda2fe4b1..8268eb1a0ff5a 100644

[ ... ]

Quoted 6 lines
arch/x86/include/asm/unwind_user_eh_frame.h +1 -1
@@ -15,7 +17,71 @@
 #define EH_FRAME_MIN_DATA_ALIGN -8
 #define EH_FRAME_MAX_DATA_ALIGN -1
 
-#endif
+#endif /* CONFIG_X86_64 */

[ ... ]

Quoted 5 lines
arch/x86/include/asm/unwind_user_eh_frame.h +5 -0
+static inline int eh_frame_do_def_cfa_expression(const char *expr,
+						 int size,
+						 unsigned long ip,
+						 struct eh_frame_reg_state *reg_state)
+{

[ ... ]

arch/x86/include/asm/unwind_user_eh_frame.h line 73

Quoted 7 lines
arch/x86/include/asm/unwind_user_eh_frame.h +7 -0
+		if (!DW_OP_is_breg(sp_op) || !DW_OP_is_breg(ip_op))
+			return -EOPNOTSUPP;
+
+		sp_reg = DW_OP_breg_register(sp_op);
+		ip_reg = DW_OP_breg_register(ip_op);
+		if (sp_reg != EH_FRAME_REG_SP || ip_reg != EH_FRAME_REG_RA)
+			return -EOPNOTSUPP;

[Severity: Medium]
The commit message mentions enabling unwinding for both i386 and x86-64, but does this check inadvertently break i386 support?

EH_FRAME_REG_SP and EH_FRAME_REG_RA are defined under CONFIG_X86_64 and correspond to x86-64 DWARF register numbers (7 and 16). For i386, the DWARF register numbers are different (ESP is 4, EIP is 8).

When unwinding an i386 PLT expression, such as for a 32-bit compat process running under a 64-bit kernel, will this check unconditionally reject the expression and return -EOPNOTSUPP because 4 does not equal 7?

Also, if this code is compiled natively for 32-bit, would this cause a build failure because the EH_FRAME_REG_SP and EH_FRAME_REG_RA macros are not defined outside of the CONFIG_X86_64 block?

Quoted 10 lines
arch/x86/include/asm/unwind_user_eh_frame.h +10 -0
+
+		/* CFA = (SP + SP_offset) + (((IP & 0xf) >= N) << shift) */
+		reg_state->cfa_rule = CFA_REG_OFFSET;
+		reg_state->cfa_regnum = EH_FRAME_REG_SP;
+		reg_state->cfa_offset = sp_offset + (((ip & 15) >= n) << shift);
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
author
#49 lore raw

RFC PATCH v2 21/24 unwind_user/eh_frame/x86: Handle DRAP expressions

In reply to Jens Remus

GCC uses DRAP (Dynamic Realign Argument Pointer) when the stack needs to be dynamically realigned (e.g. for aligned local variables). This uses DWARF expressions to describe how to unwind through such frames.

Add x86-specific handling for the CFA and FP expressions patterns:

1. CFA expression (DW_OP_breg<FP> + offset, DW_OP_deref):
- Semantics: CFA = *(FP + offset)
- Restores the CFA from the saved SP at FP-4 (i386) or FP-8 (x86-64).

2. FP expression (DW_OP_breg<FP> +0):
- Semantics: FP = *(FP + 0)
- Restores FP from DRAP location (FP+0).

The implementation uses mask-based pattern matching similar to the existing x86 PLT expression support.

---
 arch/x86/include/asm/unwind_user_eh_frame.h | 65 +++++++++++++++++++++
 include/linux/unwind_user_eh_frame_types.h  |  4 ++
 kernel/unwind/eh_frame.c                    | 57 ++++++++++++++----
 3 files changed, 115 insertions(+), 11 deletions(-)
arch/x86/include/asm/unwind_user_eh_frame.h +65 -0
index 8268eb1a0ff5..670740cdbd8c 100644
@@ -79,10 +79,75 @@ static inline int eh_frame_do_def_cfa_expression(const char *expr,
 		return 0;
 	}
 
+	/*
+	 * DRAP (Dynamic Realignment Pointer) CFA expression:
+	 *
+	 * DW_OP_breg<FP> + <FP_offset>	// 5 (EBP) - 4 or 6 (RBP) - 8
+	 * DW_OP_deref
+	 *
+	 * CFA = *(FP + FP_offset)
+	 */
+	static const char drap_expr[] = {0x70, 0x00, 0x06};
+	static const char drap_mask[] = {0xf0, 0x80, 0xff};
+
+	if (size == sizeof(drap_expr) &&
+	    !memcmp_masked(expr, drap_expr, drap_mask, sizeof(drap_expr))) {
+		unsigned char fp_reg = DW_OP_breg_register(expr[0]);
+		unsigned char fp_offset_byte = expr[1];
+		long fp_offset;
+
+		if (fp_reg != EH_FRAME_REG_FP)
+			return -EOPNOTSUPP;
+
+		fp_offset = (long)(fp_offset_byte);
+		if (fp_offset_byte & 0x40)
+			fp_offset |= -(1L << 7);	/* Sign extend */
+
+		/* CFA = *(FP + offset) */
+		reg_state->cfa_rule = CFA_REG_OFFSET_DEREF;
+		reg_state->cfa_regnum = EH_FRAME_REG_FP;
+		reg_state->cfa_offset = fp_offset;
+		return 0;
+	}
+
 	return -EOPNOTSUPP;
 }
 #define eh_frame_do_def_cfa_expression eh_frame_do_def_cfa_expression
 
+static inline int eh_frame_do_expression(unsigned int reg,
+					 const char *expr,
+					 int size,
+					 unsigned long ip,
+					 struct eh_frame_reg_state *reg_state)
+{
+	/*
+	 * DRAP (Dynamic Realignment Pointer) FP expression:
+	 *
+	 * DW_OP_breg<FP> +0	// 5 (EBP) or 6 (RBP)
+	 *
+	 * FP = *(FP + 0)
+	 */
+	static const char drap_fp_expr[] = {0x70, 0x00};
+	static const char drap_fp_mask[] = {0xf0, 0xff};
+
+	if (reg == EH_FRAME_REG_FP && size == sizeof(drap_fp_expr) &&
+	    !memcmp_masked(expr, drap_fp_expr, drap_fp_mask, sizeof(drap_fp_expr))) {
+		unsigned char fp_reg = DW_OP_breg_register(expr[0]);
+
+		if (fp_reg != EH_FRAME_REG_FP)
+			return -EOPNOTSUPP;
+
+		/* FP = *(FP + 0) */
+		reg_state->reg_rule[FP_IDX] = REG_REGISTER_OFFSET_DEREF;
+		reg_state->reg_regnum[FP_IDX] = EH_FRAME_REG_FP;
+		reg_state->reg_offset[FP_IDX] = 0;
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+#define eh_frame_do_expression eh_frame_do_expression
+
 #include <asm-generic/unwind_user_eh_frame.h>
 
 #endif /* _ASM_X86_UNWIND_USER_EH_FRAME_H */
include/linux/unwind_user_eh_frame_types.h +4 -0
index e9f9d1abb76f..9547e963bb5f 100644
@@ -5,6 +5,8 @@
 enum eh_frame_cfa_rule {
 	CFA_UNDEFINED,		/* unrecoverable */
 	CFA_REG_OFFSET,		/* CFA = reg + offset */
+	/* CFA expressions rules */
+	CFA_REG_OFFSET_DEREF,	/* CFA = *(reg + offset) */
 };
 
 enum eh_frame_reg_rule {
@@ -14,6 +16,8 @@ enum eh_frame_reg_rule {
 	REG_OFFSET,		/* reg = *(CFA + offset) */
 	REG_VAL_OFFSET,		/* reg = CFA + offset */
 	REG_REGISTER,		/* reg = other_reg */
+	/* expressions rules */
+	REG_REGISTER_OFFSET_DEREF,	/* reg = *(other_reg + offset) */
 };
 
 enum eh_frame_reg_index {
kernel/unwind/eh_frame.c +46 -11

76 lines not on this page · raw message

Signature · 1 line
2.53.0
#50 lore raw

Re: [RFC PATCH v2 21/24] unwind_user/eh_frame/x86: Handle DRAP expressions

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added DRAP DWARF expression handling for the Frame Pointer (FP) uses a generic…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added DRAP DWARF expression handling for the Frame Pointer (FP) uses a generic register rule that unconditionally fails for non-topmost frames, causing premature stack trace truncation. --

--- Patch [21]: [RFC PATCH v2 21/24] unwind_user/eh_frame/x86: Handle DRAP expressions --- commit a27cd1aa620d26189650492016f14beeb35a7207
Author: Jens Remus <[email protected]>

unwind_user/eh_frame/x86: Handle DRAP expressions

This commit adds x86-specific handling for DRAP CFA and FP expressions. It supports unwinding through frames with dynamically realigned stacks by parsing the corresponding DWARF expressions.

Quoted 3 lines
kernel/unwind/eh_frame.c +0 -0

[ ... ]

Quoted 4 lines
kernel/unwind/eh_frame.c +0 -0
@@ -1184,6 +1209,16 @@ static __always_inline int __find_frame_row(struct eh_frame_section *sec,
 		frame->fp.regnum = ctx.state.reg_regnum[FP_IDX];
 		frame->fp.offset = 0;
 		break;

kernel/unwind/eh_frame.c static __always_inline int __find_frame_row(struct eh_frame_section *sec, · line 1221

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+	case REG_REGISTER_OFFSET_DEREF:
+		if (ctx.state.reg_regnum[FP_IDX] > UINT_MAX)
+			return -EINVAL;
+		if (ctx.state.reg_offset[FP_IDX] < INT_MIN ||
+		    ctx.state.reg_offset[FP_IDX] > INT_MAX)
+			return -EOPNOTSUPP;
+		frame->fp.rule = UNWIND_USER_RULE_REG_OFFSET_DEREF;
+		frame->fp.regnum = ctx.state.reg_regnum[FP_IDX];
+		frame->fp.offset = ctx.state.reg_offset[FP_IDX];
+		break;

[Severity: Medium]
Will this generic register rule cause stack trace truncation for non-topmost frames?

Looking at unwind_user_next_common(), UNWIND_USER_RULE_REG_OFFSET_DEREF is unconditionally rejected unless it's the topmost frame:

kernel/unwind/user.c:unwind_user_next_common() {
    ...
    case UNWIND_USER_RULE_REG_OFFSET_DEREF:
        if (!state->topmost || unwind_user_get_reg(&fp, frame->fp.regnum))
            return -EINVAL;
    ...
}

Because EH_FRAME_REG_FP is tracked across frames, could this use a dedicated FP rule to avoid aborting the stack trace when encountering a DRAP frame that isn't the innermost frame?

Quoted 3 lines
kernel/unwind/eh_frame.c +0 -0
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
author
#51 lore raw

RFC PATCH v2 22/24 s390/ptrace: Provide frame_pointer()

In reply to Jens Remus

On s390 64-bit the s390x ELF ABI [1] designates register 11 as the "preferred" frame pointer (FP) register in user space.

[1]: s390x ELF ABI, https://github.com/IBM/s390x-abi/releases

---

Notes (jremus):
Cherry-picked from "[PATCH v4 00/12] s390: SFrame user space unwinding" series and adjusted to .eh_frame:
https://lore.kernel.org/all/[email protected]/

 arch/s390/include/asm/ptrace.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/s390/include/asm/ptrace.h +6 -0
index 495e310c3d6d..85765979a595 100644
@@ -255,6 +255,12 @@ static __always_inline unsigned long user_stack_pointer(const struct pt_regs *re
 	return regs->gprs[15];
 }
 
+static __always_inline unsigned long frame_pointer(const struct pt_regs *regs)
+{
+	/* Return ABI-designated "preferred" frame-pointer register value. */
+	return regs->gprs[11];
+}
+
 static __always_inline unsigned long regs_get_register(const struct pt_regs *regs,
 						       unsigned int offset)
 {
Signature · 1 line
2.53.0
#52 lore raw

Re: [RFC PATCH v2 22/24] s390/ptrace: Provide frame_pointer()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

On s390 64-bit the s390x ELF ABI [1] designates register 11 as the "preferred" frame pointer (FP) register in user space.

[1]: s390x ELF ABI, https://github.com/IBM/s390x-abi/releases

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
author
#53 lore raw

RFC PATCH v2 23/24 unwind_user/eh_frame/s390: Enable eh_frame unwinding on s390

In reply to Jens Remus

The unwind user eh_frame implementation works well with GCC and Clang generated code on s390. Enable it.

---
 arch/s390/Kconfig                            |  1 +
 arch/s390/include/asm/unwind_user.h          | 71 ++++++++++++++++++++
 arch/s390/include/asm/unwind_user_eh_frame.h | 24 +++++++
 3 files changed, 96 insertions(+)
 create mode 100644 arch/s390/include/asm/unwind_user.h
 create mode 100644 arch/s390/include/asm/unwind_user_eh_frame.h
arch/s390/Kconfig +1 -0
index 84404e6778d5..fa229ac92d5a 100644
@@ -251,6 +251,7 @@ config S390
 	select HAVE_SOFTIRQ_ON_OWN_STACK
 	select HAVE_STACKPROTECTOR if CC_HAS_STACKPROTECTOR_GLOBAL
 	select HAVE_SYSCALL_TRACEPOINTS
+	select HAVE_UNWIND_USER_EH_FRAME
 	select HAVE_VIRT_CPU_ACCOUNTING
 	select HAVE_VIRT_CPU_ACCOUNTING_IDLE
 	select HOTPLUG_SMT
arch/s390/include/asm/unwind_user.h new file +71 -0

71 lines not on this page · raw message

arch/s390/include/asm/unwind_user_eh_frame.h new file +24 -0
new file mode 100644
index 000000000000..842d9f7c3d80
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_S390_UNWIND_USER_EH_FRAME_H
+#define _ASM_S390_UNWIND_USER_EH_FRAME_H
+
+#define EH_FRAME_REG_SP	15	/* designated stack pointer register */
+#define EH_FRAME_REG_FP	11	/* "preferred" frame pointer register */
+#define EH_FRAME_REG_RA	14	/* designaged return address register */
+
+/* Instructions must be 2-byte aligned */
+#define EH_FRAME_MAX_CODE_ALIGN 2
+
+/* CFA is defined as SP at call site + 160, so that SP = CFA - 160 */
+#define EH_FRAME_SP_VAL_OFFSET -160
+
+/* SP may be saved on the stack or in a register */
+static inline bool eh_frame_reject_sp_rule(void)
+{
+	return false;
+}
+#define eh_frame_reject_sp_rule eh_frame_reject_sp_rule
+
+#include <asm-generic/unwind_user_eh_frame.h>
+
+#endif /* _ASM_S390_UNWIND_USER_EH_FRAME_H */
Signature · 1 line
2.53.0
#54 lore raw

Re: [RFC PATCH v2 23/24] unwind_user/eh_frame/s390: Enable eh_frame unwinding on s390

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

The unwind user eh_frame implementation works well with GCC and Clang generated code on s390. Enable it.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
author
#55 lore raw

RFC PATCH v2 24/24 unwind_user/eh_frame: Add prctl() interface for (un)registering .eh_frame_hdr sections

In reply to Jens Remus

The kernel does not have direct visibility to the ELF contents of shared libraries. Add prctl() interfaces to register and unregister .eh_frame_hdr sections that allow dynamic linkers to tell the kernel where to find these.

Both prctl's take a pointer to a new structure:

  struct eh_frame_setup {
	__u64	eh_frame_hdr_start;
	__u64	eh_frame_hdr_size;
	__u64	text_start;
	__u64	text_size;

};

and a size of the passed in structure. If the prctl's need to be extended, then the structure could be changes and the sizre of that structure will tell the kernel what it is the new version. If the kernel does not recognize the structure, it returns -EINVAL.

eh_frame_hdr_start - Virtual address of the .eh_frame_hdr section

  eh_frame_hdr_size  - Length of the .eh_frame_hdr section
  text_start         - Virtual address of the related text section
  text_size          - Length of the related text section

The unregister only needs the eh_frame_hdr_start and requires all of the remaining fields to be zero.

Based on Steven's patch "[PATCH v2] unwind: Add sframe_(un)register() system calls". [1]

[1]: https://lore.kernel.org/all/[email protected]/

---

Notes (jremus):
Changes in RFC v2:
- eh_frame_{register|unregister}(): Reject zero user_data or size.

 include/linux/eh_frame.h      | 20 +++++++++++++++
 include/uapi/linux/eh_frame.h | 14 +++++++++++
 include/uapi/linux/prctl.h    |  4 +++
 kernel/sys.c                  | 11 +++++++++
 kernel/unwind/eh_frame.c      | 46 +++++++++++++++++++++++++++++++++++
 5 files changed, 95 insertions(+)
 create mode 100644 include/uapi/linux/eh_frame.h
include/linux/eh_frame.h +20 -0
index 3c88ab2f7858..3a61ed0ae739 100644
@@ -6,6 +6,8 @@
 #include <linux/srcu.h>
 #include <linux/unwind_user_types.h>
 
+struct eh_frame_setup;
+
 #ifdef CONFIG_HAVE_UNWIND_USER_EH_FRAME
 
 struct eh_frame_section {
@@ -40,6 +42,12 @@ extern int eh_frame_add_section(unsigned long eh_frame_hdr_start,
 				unsigned long text_start,
 				unsigned long text_end);
 extern int eh_frame_remove_section(unsigned long eh_frame_hdr_start);
+
+extern int eh_frame_register(struct eh_frame_setup __user *user_data,
+			     __kernel_size_t size);
+extern int eh_frame_unregister(struct eh_frame_setup __user *user_data,
+			       __kernel_size_t size);
+
 extern int eh_frame_find(unsigned long ip, struct unwind_user_frame *frame);
 
 static inline bool current_has_eh_frame(void)
@@ -78,6 +86,18 @@ static inline int eh_frame_find(unsigned long ip, struct unwind_user_frame *fram
 	return -ENOSYS;
 }
 
+static inline int eh_frame_register(struct eh_frame_setup __user *user_data,
+				    __kernel_size_t size)
+{
+	return -EINVAL;
+}
+
+static inline int eh_frame_unregister(struct eh_frame_setup __user *user_data,
+				      __kernel_size_t size)
+{
+	return -EINVAL;
+}
+
 static inline bool current_has_eh_frame(void) { return false; }
 
 #endif /* CONFIG_HAVE_UNWIND_USER_EH_FRAME */
include/uapi/linux/eh_frame.h new file +14 -0
new file mode 100644
index 000000000000..8cca3792f323
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_LINUX_EH_FRAME_H
+#define _UAPI_LINUX_EH_FRAME_H
+
+#include <linux/types.h>
+
+struct eh_frame_setup {
+	__u64	eh_frame_hdr_start;
+	__u64	eh_frame_hdr_size;
+	__u64	text_start;
+	__u64	text_size;
+};
+
+#endif /* _UAPI_LINUX_EH_FRAME_H */
include/uapi/linux/prctl.h +4 -0
index b6ec6f693719..801a209bab3f 100644
@@ -416,4 +416,8 @@ struct prctl_mm_map {
 # define PR_CFI_DISABLE		_BITUL(1)
 # define PR_CFI_LOCK		_BITUL(2)
 
+/* EH_FRAME management */
+#define PR_REGISTER_EH_FRAME		82
+#define PR_UNREGISTER_EH_FRAME		83
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +11 -0
index df69bd71de03..ee67badf267c 100644
@@ -65,6 +65,7 @@
 #include <linux/rcupdate.h>
 #include <linux/uidgid.h>
 #include <linux/cred.h>
+#include <linux/eh_frame.h>
 
 #include <linux/nospec.h>
 
@@ -2907,6 +2908,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		if (arg3 & PR_CFI_LOCK && !(arg3 & PR_CFI_DISABLE))
 			error = arch_prctl_lock_branch_landing_pad_state(me);
 		break;
+	case PR_REGISTER_EH_FRAME:
+		if (arg4 || arg5)
+			return -EINVAL;
+		error = eh_frame_register((void __user *)arg2, arg3);
+		break;
+	case PR_UNREGISTER_EH_FRAME:
+		if (arg4 || arg5)
+			return -EINVAL;
+		error = eh_frame_unregister((void __user *)arg2, arg3);
+		break;
 	default:
 		trace_task_prctl_unknown(option, arg2, arg3, arg4, arg5);
 		error = -EINVAL;
kernel/unwind/eh_frame.c +46 -0

55 lines not on this page · raw message

Signature · 1 line
2.53.0
#56 lore raw

Re: [RFC PATCH v2 24/24] unwind_user/eh_frame: Add prctl() interface for (un)registering .eh_frame_hdr sections

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] The strict size != sizeof(data) check completely breaks UAPI backward and forward compatibility for extensible structs.
- [Medium] Silent truncation of the 64-bit eh_frame_hdr_start address to a 32-bit unsigned long during unregistration on 32-bit architectures.

Pre-existing issues:
- [High] If mas_erase fails due to an out-of-memory condition, it returns the target entry but leaves it in the maple tree. __eh_frame_remove_section fails to check mas_is_err(mas), freeing the entry while it remains accessible in the tree.
- [High] The unregister prctl triggers an unbounded iteration over the maple tree while holding a spinlock, allowing an unprivileged user to cause a CPU soft lockup.
--

--- Patch [24]: [RFC PATCH v2 24/24] unwind_user/eh_frame: Add prctl() interface for (un)registering .eh_frame_hdr sections --- commit 51adfd3b1e2795e1bc35c760119a8842b4e76983
Author: Jens Remus <[email protected]>

unwind_user/eh_frame: Add prctl() interface for (un)registering .eh_frame_hdr sections

This commit adds prctl interfaces to register and unregister .eh_frame_hdr sections. It allows dynamic linkers to notify the kernel about the location of these sections using a new eh_frame_setup structure.

Quoted 3 lines
kernel/unwind/eh_frame.c +0 -0

[ ... ]

Quoted 5 lines
kernel/unwind/eh_frame.c +1 -0
@@ -1609,3 +1610,48 @@ void eh_frame_free_mm(struct mm_struct *mm)
 
 	mtree_destroy(&mm->eh_frame_mt);
 }
+

kernel/unwind/eh_frame.c void eh_frame_free_mm(struct mm_struct *mm) · line 1623

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+int eh_frame_register(struct eh_frame_setup __user *user_data, __kernel_size_t size)
+{
+	struct eh_frame_setup data;
+	unsigned long eh_frame_hdr_end, text_end;
+
+	if (!user_data || !size)
+		return -EINVAL;
+
+	if (size != sizeof(data))
+		return -EINVAL;

[Severity: High]
Does this strict size check prevent future UAPI extensions? Since this is a new UAPI structure, rejecting sizes that do not exactly match the kernel's structure size prevents extending the structure later without breaking backward compatibility. Would it be better to use copy_struct_from_user() here to handle size mismatches by verifying trailing zeroes?

Quoted 14 lines
kernel/unwind/eh_frame.c +14 -0
+
+	if (copy_from_user(&data, user_data, sizeof(data)))
+		return -EFAULT;
+
+	if (check_add_overflow(data.eh_frame_hdr_start, data.eh_frame_hdr_size,
+			       &eh_frame_hdr_end))
+		return -EINVAL;
+
+	if (check_add_overflow(data.text_start, data.text_size, &text_end))
+		return -EINVAL;
+
+	return eh_frame_add_section(data.eh_frame_hdr_start, eh_frame_hdr_end,
+				    data.text_start, text_end);
+}

kernel/unwind/eh_frame.c void eh_frame_free_mm(struct mm_struct *mm) · line 1647

Quoted 10 lines
kernel/unwind/eh_frame.c +10 -0
+
+int eh_frame_unregister(struct eh_frame_setup __user *user_data, __kernel_size_t size)
+{
+	struct eh_frame_setup data;
+
+	if (!user_data || !size)
+		return -EINVAL;
+
+	if (size != sizeof(data))
+		return -EINVAL;

[Severity: High]
Is this strict size check similarly going to cause extensibility issues for the unregister path? Like the register function, could this use copy_struct_from_user() to safely allow future modifications?

kernel/unwind/eh_frame.c void eh_frame_free_mm(struct mm_struct *mm) · line 1656

Quoted 9 lines
kernel/unwind/eh_frame.c +9 -0
+
+	if (copy_from_user(&data, user_data, sizeof(data)))
+		return -EFAULT;
+
+	/* Unregister only uses eh_frame_hdr_start */
+	if (data.eh_frame_hdr_size || data.text_start || data.text_size)
+		return -EINVAL;
+
+	return eh_frame_remove_section(data.eh_frame_hdr_start);

[Severity: Medium]
Will data.eh_frame_hdr_start be silently truncated on 32-bit architectures? The user provides a 64-bit __u64 address, but eh_frame_remove_section() expects an unsigned long. If the upper 32 bits are set by userspace on a 32-bit architecture, this could truncate into a valid 32-bit address without any bounds validation.

[Severity: High]
This isn't a bug introduced by this patch, but does exposing this function to userspace via prctl allow an unprivileged user to cause a CPU soft lockup?

Looking at eh_frame_remove_section(), it performs an unbounded iteration over the maple tree while holding a spinlock:

kernel/unwind/eh_frame.c:eh_frame_remove_section() {
    ...
    mtree_lock(&mm->eh_frame_mt);
    mas_for_each(&mas, sec, ULONG_MAX) {
        if (sec->eh_frame_hdr_start == eh_frame_hdr_start) {
            found = true;
            ret |= __eh_frame_remove_section(&mas, sec);
        }
    }
    mtree_unlock(&mm->eh_frame_mt);
    ...
}

Could an attacker register a massive number of non-overlapping sections and then trigger a long unbounded loop with preemption disabled?

[Severity: High]
This is also a pre-existing issue, but the new prctl makes it trivially reachable by unprivileged userspace, which could lead to a use-after-free.

When __eh_frame_remove_section() attempts to remove a section:

kernel/unwind/eh_frame.c:__eh_frame_remove_section() {
    ...
    if (mas_erase(mas) != sec) {
        dbg_sec("mas_erase failed: text=%lx\n", sec->text_start);
        return -EINVAL;
    }

    call_srcu(&eh_frame_srcu, &sec->rcu, eh_frame_free_srcu);
    ...
}

If mas_erase() fails due to an out-of-memory condition, it can set an error state and return the target entry without removing it from the tree. Because the code does not check mas_is_err(mas), it frees the entry while it remains accessible in the maple tree. Should this verify mas_is_err() before freeing the section?

Quoted 1 line
kernel/unwind/eh_frame.c +1 -0
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#57 lore raw
In reply to Jens Remus
Quoted 197 lines · Jens Remus

This series adds support for parsing DWARF Call Frame Information (CFI) from the .eh_frame_hdr and .eh_frame sections of user space ELF files.

The code is based on the deferred unwind user work originally done for SFrame by Josh, Steven, and myself:

v4 : https://lore.kernel.org/all/[email protected]/
v10: https://lore.kernel.org/all/[email protected]/
v16: https://lore.kernel.org/all/[email protected]/

The goal is to make user space stack traces available in-kernel without requiring frame pointers and without copying large parts of the user stack for later processing.

Today, reliable user stack traces from the kernel generally requires frame pointers. Otherwise, profilers such as perf have to copy large amounts of user space stack into the kernel ring buffer and process it later. Frame pointers are simple and robust, but enabling them for all executables and libraries has a performance cost.

Another issue is that the frame layout can vary between compilers and architectures, and on architectures such as s390 there is no defined frame layout which allows reliable frame-pointer based stack tracing. The only way to perform user space profiling on there architectures is to copy the user space into the kernel buffer.

The .eh_frame section is already emitted by most toolchains on most architectures unless explicitly disabled. It contains DWARF CFI describing how to recover the caller state at any point in a function. The .eh_frame_hdr section provides a binary search table for looking up the Frame Description Entry (FDE) for a given instruction pointer (IP).

Because the .eh_frame_hdr and .eh_frame sections live in the ELF file, they need to be faulted in when used. This means that walking the user space stack requires being in a faultable context. As profilers like perf request a stack trace in interrupt or NMI context, the walking cannot be done when requested. This series reuses the deferred unwind user framework, that performed the actual user stack trace is later in a faultable context, before the task returns to user space.

This series implements .eh_frame[_hdr] support for the deferred unwind user code and enables it for x86-64 and s390.

It intentionally not implement a complete DWARF unwinder. It evaluates only the subset of DWARF CFI needed for stack tracing:

- Call Frame Address (CFA): Using rule from DWARF CFI.

- Stack pointer (SP): Using an implicit rule based on the CFA definition (SP = CFA for most architectures).

- Frame pointer (FP): Using rule from DWARF CFI.

- Return address (RA): Using rule from DWARF CFI.

Unsupported CFI instructions, unsupported expressions, invalid data, or user memory faults stop the stack tracing safely and results in a partial stack trace.

This series applies on top of v7.2 tag:

git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git v7.2

The to be stack-traced user space executables and libraries need to contain .eh_frame_hdr and .eh_frame sections as well as a GNU_EH_FRAME PHDR.

Namhyung Kim's related perf tools deferred callchain support can be used for testing, for example:

perf record --call-graph fp,defer ...
perf report
perf script

Why .eh_frame?

This series is not meant to replace or undermine the SFrame work. SFrame remains the simpler and more purpose-built format for user stack tracing. The motivation for .eh_frame support is pragmatic: .eh_frame is already widely deployed today.

- Availability and maturity: .eh_frame is already present in most ELF binaries for C++ exception handling. It has been used in production for decades for exception handling and debugger stack unwinding.

- Toolchain support: .eh_frame is supported across all major compilers and architectures today, whereas .sframe adoption is still emerging.

- Size: .sframe would be added in addition to existing .eh_frame[_hdr] rather than replacing it, increasing the ELF file size. [1]

Addressing historical DWARF concerns:

Using DWARF for kernel unwinding has a bad history. Previous attempts were complex, fragile, slow, and hard to maintain. Hand-written assembly and the complexity of the DWARF state machine were among the reasons the simpler ORC kernel unwind format was developed. [2,3,4]

However, this implementation for user space stack tracing differs from those problematic kernel unwinding attempts:

- It stack traces user space, not kernel.

- It runs in a deferred, faultable context, not in NMI, interrupt, or oops context.

- It may return partial stack traces. Bad CFI, unsupported operations, invalid user memory, or faults are allowed to terminate the unwind.

- It implements only the CFI subset needed for stack tracing, not a general DWARF unwinder.

- It does not include a general-purpose DWARF expression VM. Expression size is bounded. Only a small number of pattern-matched expressions is supported (e.g. DRAP and PLT expressions on x86). Unsupported expressions cause stack tracing to fail safely.

- All user memory access uses [unsafe_]get_user() with proper bounds checking and fault handling.

- Corruption detection with automatic section removal on invalid .eh_frame prevents further stack tracing attempts.

Limitations and future work:

- CIE version 1 support only and no DWARF64 support, as I have not run into either during my testing.

- Signal frames are not handled yet. An architecture hook could support unwinding through FDEs whose CIE augmentation contains 'S' (signal frame), similar to Glibc's SFrame backtrace() support. See also my "[RFC PATCH v1 0/5] s390: Signal frame user space unwinding". [5]

- x86-32, x86-x32, and 32-bit compat mode support not implemented yet.

- CIE caching would be useful. Reading an FDE requires reading its referenced CIE first to obtain the FDE encoding. Most .eh_frame sections have only a very small number of CIEs, often one default CIE shared by most FDEs and possibly one signal frame CIE. Caching the last CIE per section, together with the initial CFA, FP, and RA rules, would avoid repeated CIE parsing and initial CFI instruction processing.

[1]: https://lore.kernel.org/all/CAN30aBFVDxeoXApn_g_Hw0Ayhi4V=m7CcX8UDO6ZDTi6xA-3Pg@mail.gmail.com/
[2]: https://lwn.net/Articles/727553/
[3]: https://lkml.org/lkml/2012/2/10/356
[4]: https://lkml.org/lkml/2017/5/20/165
[5]: https://lore.kernel.org/all/[email protected]/

Patches 1-6 add base functionality to unwind user to support .eh_frame- based (or .sframe-based) unwinding. Patches originate from my latest .sframe patch series.

Patches 7-10 add the basic infrastructure for reading .eh_frame_hdr and .eh_frame sections and storing them in a per-mm maple tree.

Patches 11-14 wire up the eh_frame infrastructure to the unwind user framework and add error handling and debugging support.

Patch 15 duplicates registered .eh_frame_hdr section data on clone/fork.

Patch 16 improves .eh_frame DWARF CFI instruction processing.

Patch 17 enables architectures to implement selected DWARF expressions in CFI instructions.

Patches 18-21 enable .eh_frame unwinding on x86-64 with minimal DWARF expression support for DRAP and PLT expressions.

Patches 22-23 enable .eh_frame unwinding on s390.

Patch 24 adds a prctl() interface for (un)registering .eh_frame_hdr sections for shared libraries. I will send a related test-patch for Glibc separately.

Changes in RFC v2:
- Addressed most of Sashikos AI review feedback.
- Dropped patch that added support for linear .eh_frame search, as there is no good mean to limit it from reading outside of the .eh_frame section.
- Tweaked limits based on limited testing (still fairly arbitrary; needs more thought):
- Reduced state stack depth limit to 1.
- Added FDE length limit of 32.768 bytes.
- Added CFI instruction limit (CIE+FDE) of 16.384 instructions.

Quoted 6 lines

Note that I will be away from keyboard for two weeks. I'll reply to your (and Sashiko AI's) feedback afterwards.

Thanks and regards,
Jens

Thanks for pivoting to .eh_frame unwinding. This work is highly appreciated.

The moment deferred unwinding is implemented, the kernel space objection (https://lwn.net/Articles/728339/) no longer applies. Every argument for ORC (and derived formats like SFrame) -- that it must not crash during crash handling, that it is simple enough to be bug-free, and that it is fast enough for lockdep -- was formulated specifically to justify that kernel execution path.

On the userspace side, we are seeing major advances in compact unwinding:

* x86-64 (Alexis Engelke): Implemented a compact-unwind encoder in lld that encodes 100% of Clang (main) -O3 and 99.9% of GCC 15 -O3 functions in an -fno-exceptions libLLVM.so. This reduces .eh_frame_hdr
+ .eh_frame size by 87% for Clang (1.80 descriptors per FDE) and 71% for GCC (3.85 descriptors per FDE).
https://conf.gnu-tools-cauldron.org/prg26/talk/review/WV7MVNYXJA8XSMKRVBBV9GQJZZ3LW9KT
* AArch64 (Shoaib): Implemented a corresponding compact-unwind encoder for AArch64, used in Android apps.

Crucially, neither of these approaches generates unwind information in relocatable files.
Given the massive size reductions achieved by these compact unwind descriptors, I suspect there won't be a use case or remaining need for .sframe on either AArch64 or x86-64.