From: Sven Schnelle <svens@linux.ibm.com>
Date: Fri, 6 Mar 2020 13:18:31 +0100
Subject: s390/ptrace: return -ENOSYS when invalid syscall is supplied
Git-commit: cd29fa798001075a554b978df3a64e6656c25794
Patch-mainline: v5.8-rc2
References: git-fixes

The current code returns the syscall number which an invalid
syscall number is supplied and tracing is enabled. This makes
the strace testsuite fail.

Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
[ ptesarik: The original patch is a bit confusing, as it changes
  regs->gprs[2] to regs->int_code & 0xffff, which is exactly what
  the only caller of this function does right before calling. I did
  not backport any of those changes, but only the relevant parts:
  - clear PIF_SYSCALL on secure computing failure,
  - set regs->gprs[2] to -ENOSYS if the syscall number is invalid. ]
Signed-off-by: Petr Tesarik <ptesarik@suse.com>
---
 arch/s390/kernel/ptrace.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

--- a/arch/s390/kernel/ptrace.c
+++ b/arch/s390/kernel/ptrace.c
@@ -851,14 +851,13 @@ asmlinkage long do_syscall_trace_enter(s
 		 * debugger stored an invalid system call number. Skip
 		 * the system call and the system call restart handling.
 		 */
-		clear_pt_regs_flag(regs, PIF_SYSCALL);
-		return -1;
+		goto skip;
 	}
 
 	/* Do the secure computing check after ptrace. */
 	if (secure_computing(NULL)) {
 		/* seccomp failures shouldn't expose any additional code. */
-		return -1;
+		goto skip;
 	}
 
 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
@@ -871,7 +870,12 @@ asmlinkage long do_syscall_trace_enter(s
 			    regs->gprs[3] &mask, regs->gprs[4] &mask,
 			    regs->gprs[5] &mask);
 
+	if ((signed long)regs->gprs[2] >= NR_syscalls)
+		regs->gprs[2] = -ENOSYS;
 	return regs->gprs[2];
+skip:
+	clear_pt_regs_flag(regs, PIF_SYSCALL);
+	return -1;
 }
 
 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
