linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for H. Peter Anvin (Intel)" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "H. Peter Anvin (Intel)" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: x86/entry] x86/entry/64: Sign-extend system calls on entry to int
Date: Thu, 20 May 2021 13:23:31 -0000	[thread overview]
Message-ID: <162151701102.29796.14343051250816101090.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20210518191303.4135296-5-hpa@zytor.com>

The following commit has been merged into the x86/entry branch of tip:

Commit-ID:     0595494891723a1dcca5eaa8eeca8ab54ad953b9
Gitweb:        https://git.kernel.org/tip/0595494891723a1dcca5eaa8eeca8ab54ad953b9
Author:        H. Peter Anvin (Intel) <hpa@zytor.com>
AuthorDate:    Tue, 18 May 2021 12:13:01 -07:00
Committer:     Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Thu, 20 May 2021 15:19:49 +02:00

x86/entry/64: Sign-extend system calls on entry to int

Right now, *some* code will treat e.g. 0x0000000100000001 as a system
call and some will not. Some of the code, notably in ptrace, will
treat 0x000000018000000 as a system call and some will not. Finally,
right now, e.g. 335 for x86-64 will force the exit code to be set to
-ENOSYS even if poked by ptrace, but 548 will not, because there is an
observable difference between an out of range system call and a system
call number that falls outside the range of the table.

This is visible to the user: for example, the syscall_numbering_64
test fails if run under strace, because as strace uses ptrace, it ends
up clobbering the upper half of the 64-bit system call number.

The architecture independent code all assumes that a system call is "int"
that the value -1 specifically and not just any negative value is used for
a non-system call. This is the case on x86 as well when arch-independent
code is involved. The arch-independent API is defined/documented (but not
*implemented*!) in <asm-generic/syscall.h>.

This is an ABI change, but is in fact a revert to the original x86-64
ABI. The original assembly entry code would zero-extend the system call
number;

Use sign extend to be explicit that this is treated as a signed number
(although in practice it makes no difference, of course) and to avoid
people getting the idea of "optimizing" it, as has happened on at least
two(!) separate occasions.

Do not store the extended value into regs->orig_ax, however: on x86-64, the
ABI is that the callee is responsible for extending parameters, so only
examining the lower 32 bits is fully consistent with any "int" argument to
any system call, e.g. regs->di for write(2). The full value of %rax on
entry to the kernel is thus still available.

[ tglx: Add a comment to the ASM code ]

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20210518191303.4135296-5-hpa@zytor.com

---
 arch/x86/entry/entry_64.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 1d9db15..a5f02d0 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -108,7 +108,8 @@ SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
 
 	/* IRQs are off. */
 	movq	%rsp, %rdi
-	movq	%rax, %rsi
+	/* Sign extend the lower 32bit as syscall numbers are treated as int */
+	movslq	%eax, %rsi
 	call	do_syscall_64		/* returns with IRQs disabled */
 
 	/*

  reply	other threads:[~2021-05-20 13:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-18 19:12 [PATCH v4 0/6] x86/syscall: use int for x86-64 system calls H. Peter Anvin
2021-05-18 19:12 ` [PATCH v4 1/6] x86/syscall: update and extend selftest syscall_numbering_64 H. Peter Anvin
2021-05-20 13:23   ` [tip: x86/entry] selftests/x86/syscall: Update and extend syscall_numbering_64 tip-bot2 for H. Peter Anvin (Intel)
2021-05-18 19:12 ` [PATCH v4 2/6] x86/syscall: simplify message reporting in syscall_numbering.c H. Peter Anvin
2021-05-20 13:23   ` [tip: x86/entry] selftests/x86/syscall: Simplify message reporting in syscall_numbering tip-bot2 for H. Peter Anvin (Intel)
2021-05-18 19:13 ` [PATCH v4 3/6] x86/syscall: add tests under ptrace to syscall_numbering.c H. Peter Anvin
2021-05-20 13:23   ` [tip: x86/entry] selftests/x86/syscall: Add tests under ptrace to syscall_numbering_64 tip-bot2 for H. Peter Anvin (Intel)
2021-05-18 19:13 ` [PATCH v4 4/6] x86/syscall: sign-extend system calls on entry to int H. Peter Anvin
2021-05-20 13:23   ` tip-bot2 for H. Peter Anvin (Intel) [this message]
2021-05-18 19:13 ` [PATCH v4 5/6] x86/syscall: treat out of range and gap system calls the same H. Peter Anvin
2021-05-20 13:23   ` [tip: x86/entry] x86/entry: Treat " tip-bot2 for H. Peter Anvin (Intel)
2021-05-18 19:13 ` [PATCH v4 6/6] x86/syscall: use int everywhere for system call numbers H. Peter Anvin
2021-05-20  8:53   ` Thomas Gleixner
2021-05-21 21:36     ` H. Peter Anvin
2021-05-22 13:19       ` David Laight
2021-05-25  8:13   ` [tip: x86/entry] x86/entry: Use " tip-bot2 for H. Peter Anvin (Intel)
2021-05-19 11:29 ` [PATCH v4 0/6] x86/syscall: use int for x86-64 system calls Ingo Molnar
2021-05-19 16:17   ` H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=162151701102.29796.14343051250816101090.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).