[PATCH 3/5] linux: Implement pkey_alloc in C (BZ 34917)

Adhemerval Zanella adhemerval.zanella@linaro.org
Thu Nov 5 17:13:09 GMT 2026


The kernel declares the pkey_alloc flags and access rights as unsigned
long, and fails with EINVAL if the flags are nonzero or the access
rights have an unknown bit, including bits 32 to 63.

Implement pkey_alloc in C, where INLINE_SYSCALL_CALL zero-extends the
arguments to the full register on these ABIs.

Checked on x86_64-linux-gnu and aarch64-linux-gnu.
---
 sysdeps/unix/sysv/linux/Makefile              |  1 +
 sysdeps/unix/sysv/linux/pkey_alloc.c          | 28 ++++++++++++++++
 sysdeps/unix/sysv/linux/syscalls.list         |  1 -
 .../unix/sysv/linux/tst-syscall-upper-bits.c  | 32 +++++++++++++++++++
 4 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/pkey_alloc.c

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 32bce0fad53..4dbe7cf1078 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -85,6 +85,7 @@ sysdep_routines += \
   mremap \
   open_by_handle_at \
   personality \
+  pkey_alloc \
   pkey_get \
   pkey_mprotect \
   pkey_set \
diff --git a/sysdeps/unix/sysv/linux/pkey_alloc.c b/sysdeps/unix/sysv/linux/pkey_alloc.c
new file mode 100644
index 00000000000..ea57a5f72de
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/pkey_alloc.c
@@ -0,0 +1,28 @@
+/* Allocate a memory protection key.  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
+__pkey_alloc (unsigned int flags, unsigned int access_restrictions)
+{
+  return INLINE_SYSCALL_CALL (pkey_alloc, flags, access_restrictions);
+}
+libc_hidden_def (__pkey_alloc)
+weak_alias (__pkey_alloc, pkey_alloc)
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index 424bf43868f..d328416ca98 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -103,7 +103,6 @@ name_to_handle_at EXTRA	name_to_handle_at i:isppi name_to_handle_at
 setns		EXTRA	setns		i:ii	setns
 
 memfd_create    EXTRA	memfd_create	i:si    memfd_create
-pkey_alloc	EXTRA	pkey_alloc	i:ii	__pkey_alloc	pkey_alloc
 pkey_free	EXTRA	pkey_free	i:i	__pkey_free	pkey_free
 gettid          EXTRA   gettid          Ei:     __gettid	gettid
 tgkill          EXTRA   tgkill          i:iii   __tgkill	tgkill
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
index 52ca91989dc..3dba1e65c8a 100644
--- a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
+++ b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
@@ -98,6 +98,37 @@ test_mprotect (void)
   xmunmap (p, pagesize);
 }
 
+static int __attribute__ ((noipa))
+call_pkey_alloc (uint64_t flags, uint64_t access_restrictions)
+{
+  return pkey_alloc (flags, access_restrictions);
+}
+
+/* The kernel rejects nonzero flags even if protection keys are not
+   supported.  */
+static void
+test_pkey_alloc (void)
+{
+  errno = 0;
+  int key = pkey_alloc (0, 0);
+  int err = errno;
+  errno = 0;
+  int dkey = call_pkey_alloc (DIRTY (0), DIRTY (0));
+  int derr = errno;
+  if (key < 0)
+    {
+      TEST_COMPARE (dkey, -1);
+      TEST_COMPARE (derr, err);
+    }
+  else
+    {
+      TEST_VERIFY (dkey >= 0);
+      pkey_free (key);
+      if (dkey >= 0)
+	pkey_free (dkey);
+    }
+}
+
 static long int __attribute__ ((noipa))
 call_ptrace (uint64_t request, pid_t pid, void *addr, void *data)
 {
@@ -135,6 +166,7 @@ do_test (void)
 {
   test_mprotect ();
   test_pkey_mprotect ();
+  test_pkey_alloc ();
   test_ptrace ();
 
   return 0;
-- 
2.53.0



More information about the Libc-alpha mailing list