# \[PATCH v3 2/2\] selftests/ptrace: check that PTRACE\_SET\_SYSCALL\_INFO rejects invalid error values

- From: Renzo Davoli \<hidden\>
- Date: 2026-11-15 15:27:18 
- List: linux-api
- Page: https://ratatoskr.run/linux-api/2026/11/17939979
- Thread: https://ratatoskr.run/linux-api/2026/11/17939978/t.md

```text
At every exit stop of the set_syscall_info test, try to set with
is_error set a few values outside [-MAX_ERRNO, -1], check that
PTRACE_SET_SYSCALL_INFO fails with ERANGE and that the return value
of the system call is left untouched.

Signed-off-by: Renzo Davoli <redacted>
---
 .../selftests/ptrace/set_syscall_info.c       | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/tools/testing/selftests/ptrace/set_syscall_info.c b/tools/testing/selftests/ptrace/set_syscall_info.c
index 1cc411a41cd6..366ef0ec228f 100644
--- a/tools/testing/selftests/ptrace/set_syscall_info.c
+++ b/tools/testing/selftests/ptrace/set_syscall_info.c
@@ -126,6 +126,54 @@ check_psi_exit(struct __test_metadata *_metadata,
 	}
 }
 
+/* Error values that PTRACE_SET_SYSCALL_INFO must reject with ERANGE. */
+static const long long bad_error_rval[] = {
+	0,
+	1,
+	ESRCH,
+	-4095 - 1,	/* -MAX_ERRNO - 1 */
+	-2147483647LL - 1,
+#if __SIZEOF_LONG__ > 4
+	0x100000000LL,
+	-0x100000000LL - ESRCH,
+#endif
+};
+
+static void
+check_psi_exit_bad_rval(struct __test_metadata *_metadata,
+			const struct ptrace_syscall_info *orig,
+			const struct si_exit *exp_exit)
+{
+	struct ptrace_syscall_info info;
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(bad_error_rval); ++i) {
+		info = *orig;
+		info.exit.is_error = 1;
+		info.exit.rval = bad_error_rval[i];
+		ASSERT_EQ(-1, sys_ptrace(PTRACE_SET_SYSCALL_INFO,
+					 tracee_pid, sizeof(info),
+					 (uintptr_t) &info)) {
+			LOG_KILL_TRACEE("PTRACE_SET_SYSCALL_INFO: error rval %lld accepted",
+					bad_error_rval[i]);
+		}
+		ASSERT_EQ(ERANGE, errno) {
+			LOG_KILL_TRACEE("PTRACE_SET_SYSCALL_INFO: error rval %lld: %m",
+					bad_error_rval[i]);
+		}
+
+		/* the return value must be left untouched */
+		memset(&info, 0, sizeof(info));
+		info.op = 0xff;
+		ASSERT_LT(0, sys_ptrace(PTRACE_GET_SYSCALL_INFO, tracee_pid,
+					sizeof(info), (uintptr_t) &info)) {
+			LOG_KILL_TRACEE("PTRACE_GET_SYSCALL_INFO: %m");
+		}
+		check_psi_exit(_metadata, &info, exp_exit,
+			       "PTRACE_SET_SYSCALL_INFO with bad error rval");
+	}
+}
+
 TEST(set_syscall_info)
 {
 	const pid_t tracer_pid = getpid();
@@ -476,6 +524,9 @@ TEST(set_syscall_info)
 				check_psi_exit(_metadata, &info, exp_exit,
 						"PTRACE_GET_SYSCALL_INFO #1");
 
+				/* invalid error values must be rejected */
+				check_psi_exit_bad_rval(_metadata, &info, exp_exit);
+
 				/* apply the changes */
 				info.exit.is_error = set_exit->is_error;
 				info.exit.rval = set_exit->rval;
-- 
2.47.3

```
