From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72CE336C9C1 for ; Fri, 3 Jul 2026 08:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783066273; cv=none; b=KwH3gBYNxsc5amvC2WF2IC8weMnrkytLWe17CIIxkR/I/zblyclOdAnSp2FCd0CcZH83gx+GuFP63QRbsBkRVdLjNA8dMwuuj8daCou6e9qtzOQzaq5tTuAE2tkPjAqgZ/oMzOOe1zJZJRoOajQijdUZ0QBz+VPbWWFxsQ7SKl8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783066273; c=relaxed/simple; bh=zvRD3DOjx8MMufzkpasS9AbdbY5fNbrdY1hF8jHD8KQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=iSPV0/ALUR0kImnLp8gVb8IMEXBlf9f1b5K8uhd7AunBt7v0wGF1YhADqksk/yRWQiJ4WYA6BeHn2C8pml0HmSY4/BBJowlCKPav6lS60yyYBD71MuOkdrAq86Sexbo7bSpmvfOCqkE3RKvl1hUjVJ8VxNQ6Q0mbYorbnVw1Rtg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=d+fVb1Ke; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d+fVb1Ke" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-3811f512167so270725a91.3 for ; Fri, 03 Jul 2026 01:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783066272; x=1783671072; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=XonqW6TTEmhnxGieIs2nXvN/Lqh826qWlC2WpFQO6i8=; b=d+fVb1KeYkaVTYdJ9FJctQVZG0a/cu4LNtMc1lvvf30IMhwJyO1wWwnw6t38IoJ1ec 5QageLMbep2e41n9Vhuwa8AnsSRlWWEoFTP5gitwRvqKrnfXT0m1nvWD7V7s2PABcjbQ XsKCaP41pH0dQ88pvJv736VFg2LqttK2jOO+xxXjCn01DkdIUPJFculsG0RdFS3cc4Yg CH1YrA+uF2Iw/Pt6eT1au9BuORQFGSKlwyEw1nUD/m3dI6sGDZ/huynkfeynsRwytNvn 53NRzdtWKoiyG+KnQU5vTpqx+0RTGhUU+QgYu+C0w+JMhUBsaIdSlvPZxY+3yab8/WAP FJOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783066272; x=1783671072; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=XonqW6TTEmhnxGieIs2nXvN/Lqh826qWlC2WpFQO6i8=; b=LrAPitD86ZlfszvolIEFALkMZmxlaZ1lw3CHC8N2IWHKKdIWdo0mGnFdUv4yJsqBOH J8YDqNAp2ASMxzDP1jXuUWetNexxP6qPAGcStRGAa+njnBvBcJhm7/b6n5zGxK77gudr Rt7IBpFSvITYHAbICrg8qNPAO8hiN5pHDDHVZCCGBw9FkPtHkOLvX/vHCq1nS/I32WDS OLjCTmyh3o+eAVZ5G2e/qlElOmq5kmVJ1LzyJJ6bQeWgzHAgG8GlVdyMIpscxrAvxK3T fRQqgYnXauy8P9duDF/WRMcgNg64esifx5W3Ehsa7VprwrgzOrj7jRNiUq00My5ujxq2 u42w== X-Forwarded-Encrypted: i=1; AFNElJ9EBoMY7ivHn+7pWV0ibJkMOmrBufFd1UeEusgLmRmA4fTE1mzawQjAhkVNnSSatwoAMruj3PWTRjx4Zjw=@vger.kernel.org X-Gm-Message-State: AOJu0YyQiQXEL4iZARKjV83yG8uQuAHjUfWT4mVEO9T+Ixyd1ZkMDxcD epWXXRdTudS9UcaLniJHEOdoBixg/gVax9eEEKHeFMjAP5bt46fmjX/+ X-Gm-Gg: AfdE7cmi5rZ9FaifQ4LQIR35DYWc4GrRvXsSuZ7bOtvHAqMEKi/HEY2eBVXmaV1KYnv zpFhhb/XpKT3eXR3fY+rHHqsOHm935nfhrqsq5rz5mF3Y3TTBtF7H9DBvKjv5I482zLmqfv9HJ8 e/cDMmLvsEGrndQM7H6iCuW2wJAE1+rzd5WGREhGiQgdRzgiG+ct8ragQvyIIDvH/uMzCuPhdPT vlj1jDO9fwqkHdXhT+FTOuP/r3jgVJres3XWKIAYNkJoDP3hrEq2/TwBGmTafP7cV3QsrTEjBoW ERp6/siezDcpm6gfenff0sJE4mMiC0SfOGVubR8tv38ig7kJJpDLFwMcJhfxtQam/roL6nhEq8W Avf0NJ3LYSy4sxJS5hKIqnctzeRRncrAMvlzzE91nBPJcwuDQhZ9/CA96r/teOy7fnvyFln8c0k n8jjji75YLgj2AyvTJujPp4IdEGE6Kp9rExz6F7B8wmU8FQZ0AcHAw/ir9kJWeDRHzVw== X-Received: by 2002:a05:6a21:48d:b0:3bf:9aa9:b2a4 with SMTP id adf61e73a8af0-3bfed29d324mr10405838637.20.1783066271547; Fri, 03 Jul 2026 01:11:11 -0700 (PDT) Received: from li-1a3e774c-28e4-11b2-a85c-acc9f2883e29.ibm.com.com ([106.51.160.236]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-30f0bb80f5csm25932409eec.15.2026.07.03.01.11.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jul 2026 01:11:11 -0700 (PDT) From: "Mukesh Kumar Chaurasiya (IBM)" To: oleg@redhat.com, maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, sshegde@linux.ibm.com, mkchauras@gmail.com, mchauras@linux.ibm.com, thuth@redhat.com, segher@kernel.crashing.org, ryan.roberts@arm.com, ruanjinjie@huawei.com, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Michal=20Such=C3=A1nek?= Subject: [PATCH V3] powerpc/syscall: Fix syscall skip handling for seccomp and ptrace Date: Fri, 3 Jul 2026 13:41:00 +0530 Message-ID: <20260703081100.1681924-1-mkchauras@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After enabling GENERIC_ENTRY on PowerPC, syscall_enter_from_user_mode() returns -1 as a sentinel to signal that seccomp or ptrace has intercepted the syscall and already set a return value via syscall_set_return_value(). system_call_exception() was not handling this sentinel, and since -1UL is >= NR_syscalls, the code fell into the out-of-range path and returned -ENOSYS, overwriting the errno already placed in regs->gpr[3]. The naive fix of checking r0 == -1L before the NR_syscalls bounds check is ambiguous: a user legitimately calling syscall(-1) also produces r0 == -1L, and a tracer intercepting such a call would have its injected return value silently discarded. Fix this properly by introducing regs->entry_flags, a kernel-internal field in struct pt_regs (consuming one slot of the existing __pt_regs_pad so the ABI is preserved), with SYSCALL_ENTRY_RET_SET as an out-of-band flag. syscall_set_return_value() sets this flag whenever seccomp or ptrace injects a return value. system_call_exception() zeros entry_flags before calling syscall_enter_from_user_mode(), then checks and clears the flag afterwards: if set, it returns regs->gpr[3] directly regardless of what syscall number the user originally requested. This handles all seccomp actions correctly: - SECCOMP_RET_ERRNO, SECCOMP_RET_TRACE (no tracer), SECCOMP_RET_USER_NOTIF: all call syscall_set_return_value(), flag is set, injected value returned. - SECCOMP_RET_TRAP, SECCOMP_RET_KILL: call syscall_rollback() and deliver a signal; flag is not set, but the process is dying so the return value is irrelevant. The fix covers both ppc32 and ppc64 with no #ifdefs. Fixes: bee25f97ad24 ("powerpc: Enable GENERIC_ENTRY feature") Reported-by: Michal Suchánek Closes: https://lore.kernel.org/all/ajpp-_XnbF3UTM_E@kunlun.suse.cz/ Signed-off-by: Mukesh Kumar Chaurasiya (IBM) --- v2 -> v3: - Last fix is not working for -1 syscall. Fixed that with this. v2: https://lore.kernel.org/all/20260629182946.419552-1-mkchauras@gmail.com v1 -> v2: - Fix issues in the previous fix (Michal) v1: https://lore.kernel.org/all/20260624171520.772408-1-mkchauras@gmail.com arch/powerpc/include/asm/ptrace.h | 22 +++++++++++++++++++++- arch/powerpc/include/asm/syscall.h | 6 ++++++ arch/powerpc/include/uapi/asm/ptrace.h | 6 ++++-- arch/powerpc/kernel/ptrace/ptrace.c | 2 ++ arch/powerpc/kernel/syscall.c | 18 ++++++++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/include/asm/ptrace.h b/arch/powerpc/include/asm/ptrace.h index fdeb97421785..1a53d5cfa8db 100644 --- a/arch/powerpc/include/asm/ptrace.h +++ b/arch/powerpc/include/asm/ptrace.h @@ -54,8 +54,9 @@ struct pt_regs }; unsigned long result; unsigned long exit_flags; + unsigned long entry_flags; /* Maintain 16 byte interrupt stack alignment */ - unsigned long __pt_regs_pad[3]; + unsigned long __pt_regs_pad[2]; }; }; #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP) @@ -233,6 +234,25 @@ static inline unsigned long frame_pointer(struct pt_regs *regs) #define current_pt_regs() \ ((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1) +/* + * SYSCALL_ENTRY_RET_SET: seccomp or ptrace called syscall_set_return_value() + * and wants the syscall skipped; regs->gpr[3] already holds the return value. + */ +#define SYSCALL_ENTRY_RET_SET BIT(0) + +static inline void set_syscall_entry_ret(struct pt_regs *regs) +{ + regs->entry_flags |= SYSCALL_ENTRY_RET_SET; +} + +static inline bool test_and_clear_syscall_entry_ret(struct pt_regs *regs) +{ + bool set = !!(regs->entry_flags & SYSCALL_ENTRY_RET_SET); + + regs->entry_flags &= ~SYSCALL_ENTRY_RET_SET; + return set; +} + /* * The 4 low bits (0xf) are available as flags to overload the trap word, * because interrupt vectors have minimum alignment of 0x10. TRAP_FLAGS_MASK diff --git a/arch/powerpc/include/asm/syscall.h b/arch/powerpc/include/asm/syscall.h index 834fcc4f7b54..9ae79326abe3 100644 --- a/arch/powerpc/include/asm/syscall.h +++ b/arch/powerpc/include/asm/syscall.h @@ -98,6 +98,12 @@ static inline void syscall_set_return_value(struct task_struct *task, regs->gpr[3] = val; } } + /* + * Mark that a return value has been explicitly set by seccomp or + * ptrace so that system_call_exception() can skip the syscall + * unconditionally, even when the user requested syscall(-1). + */ + set_syscall_entry_ret(regs); } static inline void syscall_get_arguments(struct task_struct *task, diff --git a/arch/powerpc/include/uapi/asm/ptrace.h b/arch/powerpc/include/uapi/asm/ptrace.h index a393b7f2760a..2f2a43414fe6 100644 --- a/arch/powerpc/include/uapi/asm/ptrace.h +++ b/arch/powerpc/include/uapi/asm/ptrace.h @@ -56,7 +56,8 @@ struct pt_regs unsigned long dsisr; /* on 4xx/Book-E used for ESR */ unsigned long result; /* Result of a system call */ unsigned long exit_flags; /* System call exit flags */ - unsigned long __pt_regs_pad[3]; /* Maintain 16 byte interrupt stack alignment */ + unsigned long entry_flags; /* System call entry flags */ + unsigned long __pt_regs_pad[2]; /* Maintain 16 byte interrupt stack alignment */ }; #endif /* __ASSEMBLER__ */ @@ -117,7 +118,8 @@ struct pt_regs #define PT_DSISR 42 #define PT_RESULT 43 #define PT_EXIT_FLAGS 44 -#define PT_PAD 47 /* 3 times */ +#define PT_ENTRY_FLAGS 45 +#define PT_PAD 46 /* 2 times */ #define PT_DSCR 48 #define PT_REGS_COUNT 48 diff --git a/arch/powerpc/kernel/ptrace/ptrace.c b/arch/powerpc/kernel/ptrace/ptrace.c index 316d4f5ead8e..440d00690cf2 100644 --- a/arch/powerpc/kernel/ptrace/ptrace.c +++ b/arch/powerpc/kernel/ptrace/ptrace.c @@ -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, entry_flags) != + offsetof(struct user_pt_regs, entry_flags)); BUILD_BUG_ON(sizeof(struct user_pt_regs) > sizeof(struct pt_regs)); diff --git a/arch/powerpc/kernel/syscall.c b/arch/powerpc/kernel/syscall.c index a9da2af6efa8..c7458aae199d 100644 --- a/arch/powerpc/kernel/syscall.c +++ b/arch/powerpc/kernel/syscall.c @@ -19,9 +19,27 @@ notrace long system_call_exception(struct pt_regs *regs, unsigned long r0) long ret; syscall_fn f; + /* + * Zero entry_flags before syscall_enter_from_user_mode() so that + * syscall_set_return_value() can set SYSCALL_ENTRY_RET_SET as an + * unambiguous out-of-band signal. The field is not initialised by + * the entry assembly. + */ + regs->entry_flags = 0; add_random_kstack_offset(); r0 = syscall_enter_from_user_mode(regs, r0); + /* + * Seccomp or ptrace may have set a return value and requested that + * the syscall be skipped. syscall_set_return_value() sets + * SYSCALL_ENTRY_RET_SET in regs->entry_flags as an + * unambiguous out-of-band signal. This avoids the ambiguity of + * using r0 == -1 as the skip sentinel when the user themselves + * called syscall(-1). + */ + if (unlikely(test_and_clear_syscall_entry_ret(regs))) + return regs->gpr[3]; + if (unlikely(r0 >= NR_syscalls)) { if (unlikely(trap_is_unsupported_scv(regs))) { /* Unsupported scv vector */ -- 2.55.0