Skip to contentSkip to conversation

powerpc/ptrace: Add offsetof check for exit_flags in pt_regs_check()

PATCH In review Mukesh Kumar Chaurasiya (IBM) 3 participants 3 messages Jul 3 – Aug 14 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/ptrace: Add offsetof check for exit_flags in pt_regs_check()

pt_regs_check() verifies that fields shared between struct pt_regs and struct user_pt_regs sit at the same offset, to catch any accidental layout divergence between the kernel and uapi structures.

Add the missing check for exit_flags, following the same pattern as the existing checks for result, dsisr, dar and the other shared fields.

Fixes d7a6797e0bc1 ("powerpc: add exit_flags field in pt_regs")
---
 arch/powerpc/kernel/ptrace/ptrace.c | 2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/kernel/ptrace/ptrace.c +2 -0
index 316d4f5ead8e..fb3fec73f66a 100644
@@ -235,6 +235,8 @@ void __init pt_regs_check(void)
 		     offsetof(struct user_pt_regs, dsisr));
 	BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
 		     offsetof(struct user_pt_regs, result));
+	BUILD_BUG_ON(offsetof(struct pt_regs, exit_flags) !=
+		     offsetof(struct user_pt_regs, exit_flags));
 
 	BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
 
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Mukesh Kumar Chaurasiya (IBM)

pt_regs_check() verifies that fields shared between struct pt_regs and struct user_pt_regs sit at the same offset, to catch any accidental layout divergence between the kernel and uapi structures.

Add the missing check for exit_flags, following the same pattern as the existing checks for result, dsisr, dar and the other shared fields.

Fixes d7a6797e0bc1 ("powerpc: add exit_flags field in pt_regs")

You may want to add below in order to get this patch backported:

Quoted 10 lines
---
 arch/powerpc/kernel/ptrace/ptrace.c | 2 ++
 1 file changed, 2 insertions(+)
arch/powerpc/kernel/ptrace/ptrace.c +0 -0
index 316d4f5ead8e..fb3fec73f66a 100644

arch/powerpc/kernel/ptrace/ptrace.c void __init pt_regs_check(void) · line 239

Quoted 5 lines
arch/powerpc/kernel/ptrace/ptrace.c +2 -0
@@ -235,6 +235,8 @@ void __init pt_regs_check(void)
 		     offsetof(struct user_pt_regs, dsisr));
 	BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
 		     offsetof(struct user_pt_regs, result));
+	BUILD_BUG_ON(offsetof(struct pt_regs, exit_flags) !=
+		     offsetof(struct user_pt_regs, exit_flags));

At first, it appeared to be redundant as we already check

	CHECK_REG(PT_EXIT_FLAGS, exit_flags);

which expands to:

	#define CHECK_REG(_pt, _reg) \
		BUILD_BUG_ON(_pt != (offsetof(struct user_pt_regs, _reg) / \
				     sizeof(unsigned long)));

But looking closely, it turns out to be non-redundant. CHECK_REG only touches struct user_pt_regs — it validates the PT_EXIT_FLAGS constant against the uapi layout. It says nothing about struct pt_regs.

struct pt_regs (asm/ptrace.h) embeds struct user_pt_regs in a union alongside an anonymous struct that re-declares all the same fields with kernel-internal aliases (dear/dar, esr/dsisr). exit_flags lives in that anonymous struct. A field accidentally inserted there between result and exit_flags would silently break the union aliasing without CHECK_REG catching it. The new BUILD_BUG_ON closes that gap, consistent with how result, dar and dsisr are already protected in the same block.

Looks good to me.

Thanks,
Amit

Quoted 7 lines
arch/powerpc/kernel/ptrace/ptrace.c +0 -0
 	BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
 
Signature · 1 line
2.55.0
#2 lore raw

Le 03/07/2026 à 10:55, Amit Machhiwal a écrit :

Quoted 9 lines · Mukesh Kumar Chaurasiya (IBM)
Quoted 8 lines

pt_regs_check() verifies that fields shared between struct pt_regs and struct user_pt_regs sit at the same offset, to catch any accidental layout divergence between the kernel and uapi structures.

Add the missing check for exit_flags, following the same pattern as the existing checks for result, dsisr, dar and the other shared fields.

Fixes d7a6797e0bc1 ("powerpc: add exit_flags field in pt_regs")

This commit was almost reverted by commit 263e5159e00a ("powerpc: Fix exit_flags field placement in pt_regs for ptrace")

Dropping this patch.

Christophe

Quoted 57 lines

You may want to add below in order to get this patch backported:

Quoted 10 lines
---
  arch/powerpc/kernel/ptrace/ptrace.c | 2 ++
  1 file changed, 2 insertions(+)
arch/powerpc/kernel/ptrace/ptrace.c +0 -0
index 316d4f5ead8e..fb3fec73f66a 100644

arch/powerpc/kernel/ptrace/ptrace.c void __init pt_regs_check(void) · line 239

Quoted 5 lines
arch/powerpc/kernel/ptrace/ptrace.c +2 -0
@@ -235,6 +235,8 @@ void __init pt_regs_check(void)
  		     offsetof(struct user_pt_regs, dsisr));
  	BUILD_BUG_ON(offsetof(struct pt_regs, result) !=
  		     offsetof(struct user_pt_regs, result));
+	BUILD_BUG_ON(offsetof(struct pt_regs, exit_flags) !=
+		     offsetof(struct user_pt_regs, exit_flags));

At first, it appeared to be redundant as we already check

	CHECK_REG(PT_EXIT_FLAGS, exit_flags);

which expands to:

	#define CHECK_REG(_pt, _reg) \
		BUILD_BUG_ON(_pt != (offsetof(struct user_pt_regs, _reg) / \
				     sizeof(unsigned long)));

But looking closely, it turns out to be non-redundant. CHECK_REG only touches struct user_pt_regs — it validates the PT_EXIT_FLAGS constant against the uapi layout. It says nothing about struct pt_regs.

struct pt_regs (asm/ptrace.h) embeds struct user_pt_regs in a union alongside an anonymous struct that re-declares all the same fields with kernel-internal aliases (dear/dar, esr/dsisr). exit_flags lives in that anonymous struct. A field accidentally inserted there between result and exit_flags would silently break the union aliasing without CHECK_REG catching it. The new BUILD_BUG_ON closes that gap, consistent with how result, dar and dsisr are already protected in the same block.

Looks good to me.

Thanks,
Amit

Quoted 7 lines
arch/powerpc/kernel/ptrace/ptrace.c +0 -0
  	BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs));
 
Signature · 1 line
2.55.0