[PATCH 1/5] x86_64: Extend 32-bit syscall arguments to the full register (BZ 34917)
Adhemerval Zanella
adhemerval.zanella@linaro.org
Thu Nov 5 17:13:07 GMT 2026
The x86_64 psABI leaves the upper 32 bits of a register that holds a
32-bit argument unspecified, and the inline syscall macros keep each
argument in its own type. A C wrapper can issue the kernel whatever
its caller left in the upper half.
That is harmless for arguments the kernel declares as int, which
SYSCALL_DEFINE truncates, but the kernel declares some int argument
as long or unsigned long and uses the whole register:
- ptrace compares the request with PTRACE_TRACEME and the other
requests as a long, so ptrace (PTRACE_TRACEME, ...) fails with
ESRCH;
- pkey_mprotect with a valid pkey fails with EINVAL for any unknown
prot bit;
- mmap compares prot with PROT_EXEC to use an execute-only protection
key.
This fix by castting every argument to long int, as x32 already does
with int64_t and the aarch64 macros do with long.
Checked on x86_64-linux-gnu.
---
sysdeps/unix/sysv/linux/Makefile | 1 +
.../unix/sysv/linux/tst-syscall-upper-bits.c | 123 ++++++++++++++++++
sysdeps/unix/sysv/linux/x86_64/sysdep.h | 13 +-
3 files changed, 131 insertions(+), 6 deletions(-)
create mode 100644 sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 40f9cd87846..32bce0fad53 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -235,6 +235,7 @@ tests += \
tst-sigtimedwait \
tst-sync_file_range \
tst-syscall-restart \
+ tst-syscall-upper-bits \
tst-sysconf-iov_max \
tst-sysvmsg-linux \
tst-sysvsem-linux \
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
new file mode 100644
index 00000000000..7b3ae651b91
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
@@ -0,0 +1,123 @@
+/* Check that system call wrappers extend 32-bit arguments (BZ 34917).
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <support/check.h>
+#include <support/xunistd.h>
+
+/* Some ABI (x86_64, aarch64) leave the upper 32 bits of a register holding
+ a 32-bit argument unspecified. Linux declares some arguments that glibc
+ takes as int or unsigned int as long or unsigned long, and rejects stray
+ upper bits. So the wrapper must extend them. */
+
+#define DIRTY(v) \
+ ((uint64_t) (unsigned int) (v) | UINT64_C (0xdeadbeef00000000))
+
+/* Check that DIRTY, a call with garbage in the upper bits of its 32-bit
+ arguments, returns the same value and errno as CLEAN. This is for
+ calls whose outcome depends on the system, such as kernel support or
+ a seccomp filter. */
+#define COMPARE_CALLS(clean, dirty) \
+ do \
+ { \
+ errno = 0; \
+ long int ret = (clean); \
+ int err = errno; \
+ errno = 0; \
+ long int dret = (dirty); \
+ int derr = errno; \
+ if (dret != ret || derr != err) \
+ FAIL ("%s: returned %ld (errno %d), expected %ld (errno %d)", \
+ #dirty, dret, derr, ret, err); \
+ } \
+ while (0)
+
+static int __attribute__ ((noipa))
+call_pkey_mprotect (void *addr, size_t len, uint64_t prot, uint64_t pkey)
+{
+ return pkey_mprotect (addr, len, prot, pkey);
+}
+
+/* The kernel rejects unknown prot bits. */
+static void
+test_pkey_mprotect (void)
+{
+ int key = pkey_alloc (0, 0);
+ if (key < 0)
+ {
+ printf ("info: pkey_alloc failed: %m\n");
+ return;
+ }
+ size_t pagesize = getpagesize ();
+ void *p = xmmap (NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1);
+ TEST_COMPARE (call_pkey_mprotect (p, pagesize, DIRTY (PROT_READ),
+ DIRTY (key)), 0);
+ xmunmap (p, pagesize);
+ pkey_free (key);
+}
+
+static long int __attribute__ ((noipa))
+call_ptrace (uint64_t request, pid_t pid, void *addr, void *data)
+{
+ return ptrace ((enum __ptrace_request) request, pid, addr, data);
+}
+
+static int
+traceme (bool dirty)
+{
+ pid_t pid = xfork ();
+ if (pid == 0)
+ {
+ long int ret;
+ if (dirty)
+ ret = call_ptrace (DIRTY (PTRACE_TRACEME), 0, NULL, NULL);
+ else
+ ret = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
+ _exit (ret == 0 ? 0 : errno);
+ }
+ int status;
+ xwaitpid (pid, &status, 0);
+ TEST_VERIFY (WIFEXITED (status));
+ return WEXITSTATUS (status);
+}
+
+/* The kernel compares the request as a long. */
+static void
+test_ptrace (void)
+{
+ COMPARE_CALLS (traceme (false), traceme (true));
+}
+
+static int
+do_test (void)
+{
+ test_pkey_mprotect ();
+ test_ptrace ();
+
+ return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
index 104acff3a36..af5c9c5f8fe 100644
--- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
+++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
@@ -220,12 +220,13 @@
/* Registers clobbered by syscall. */
# define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx"
-/* NB: This also works when X is an array. For an array X, type of
- (X) - (X) is ptrdiff_t, which is signed, since size of ptrdiff_t
- == size of pointer, cast is a NOP. */
-#define TYPEFY1(X) __typeof__ ((X) - (X))
-/* Explicit cast the argument. */
-#define ARGIFY(X) ((TYPEFY1 (X)) (X))
+/* The psABI leaves the upper 32 bits of a register holding a 32-bit argument
+ unspecified, but the kernel reads the whole register for arguments it
+ declares as long or unsigned longi. Some of those are int in the C
+ interface (the ptrace request, for instance). Extend each argument to the
+ full register, which sign- or zero-extends integers according to their
+ signedness. This also works for pointers and arrays. */
+#define ARGIFY(X) ((long int) (X))
/* Create a variable 'name' based on type of variable 'X' to avoid
explicit types. */
#define TYPEFY(X, name) __typeof__ (ARGIFY (X)) name
--
2.53.0
More information about the Libc-alpha
mailing list