[PATCH 2/5] linux: Implement mprotect in C (BZ 34917)
Adhemerval Zanella
adhemerval.zanella@linaro.org
Thu Nov 5 17:13:08 GMT 2026
On x86_64, x32, and aarch64 the upper half of a register holding a 32-bit
argument is unspecified, so a valid call can fail, for instance:
__attribute__ ((noipa)) int
f (void *addr, size_t len, long int prot)
{
return mprotect (addr, len, prot);
}
f (addr, len, PROT_READ | ~0xffffffffL)
returns -1 with EINVAL.
Implement mprotect in C, where INLINE_SYSCALL_CALL extends the argument
to the full register on these ABIs.
Checked on x86_64-linux-gnu.
---
sysdeps/unix/syscalls.list | 1 -
sysdeps/unix/sysv/linux/mprotect.c | 28 +++++++++++++++++++
.../unix/sysv/linux/tst-syscall-upper-bits.c | 20 +++++++++++++
3 files changed, 48 insertions(+), 1 deletion(-)
create mode 100644 sysdeps/unix/sysv/linux/mprotect.c
diff --git a/sysdeps/unix/syscalls.list b/sysdeps/unix/syscalls.list
index 67c82783a1d..4b9f31e4c17 100644
--- a/sysdeps/unix/syscalls.list
+++ b/sysdeps/unix/syscalls.list
@@ -39,7 +39,6 @@ lseek - lseek i:iii __libc_lseek __lseek lseek
madvise - madvise i:pUi __madvise madvise
mkdir - mkdir i:si __mkdir mkdir
mmap - mmap b:aUiiii __mmap mmap
-mprotect - mprotect i:aUi __mprotect mprotect
munmap - munmap i:aU __munmap munmap
open - open Ci:siv __libc_open __open open
profil - profil i:piii __profil profil
diff --git a/sysdeps/unix/sysv/linux/mprotect.c b/sysdeps/unix/sysv/linux/mprotect.c
new file mode 100644
index 00000000000..b1170a03223
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mprotect.c
@@ -0,0 +1,28 @@
+/* Change memory protection. Linux version.
+ 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 <sys/mman.h>
+#include <sysdep.h>
+
+int
+__mprotect (void *addr, size_t len, int prot)
+{
+ return INLINE_SYSCALL_CALL (mprotect, addr, len, prot);
+}
+libc_hidden_def (__mprotect)
+weak_alias (__mprotect, mprotect)
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
index 7b3ae651b91..52ca91989dc 100644
--- a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
+++ b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
@@ -79,6 +79,25 @@ test_pkey_mprotect (void)
pkey_free (key);
}
+static int __attribute__ ((noipa))
+call_mprotect (void *addr, size_t len, uint64_t prot)
+{
+ return mprotect (addr, len, prot);
+}
+
+/* The kernel rejects unknown prot bits. */
+static void
+test_mprotect (void)
+{
+ size_t pagesize = getpagesize ();
+ void *p = xmmap (NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1);
+ TEST_COMPARE (call_mprotect (p, pagesize, DIRTY (PROT_READ)), 0);
+ TEST_COMPARE (call_mprotect (p, pagesize, DIRTY (PROT_READ | PROT_WRITE)),
+ 0);
+ xmunmap (p, pagesize);
+}
+
static long int __attribute__ ((noipa))
call_ptrace (uint64_t request, pid_t pid, void *addr, void *data)
{
@@ -114,6 +133,7 @@ test_ptrace (void)
static int
do_test (void)
{
+ test_mprotect ();
test_pkey_mprotect ();
test_ptrace ();
--
2.53.0
More information about the Libc-alpha
mailing list