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

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


The kernel declares the unshare flags as unsigned long and fails with
EINVAL if any unknown flag is set, including bits 32 to 63.

Implement unshare in C, where INLINE_SYSCALL_CALL extends the argument
to the full register on these ABIs.

Checked on x86_64-linux-gnu.
---
 sysdeps/unix/sysv/linux/Makefile              |  1 +
 sysdeps/unix/sysv/linux/syscalls.list         |  1 -
 .../unix/sysv/linux/tst-syscall-upper-bits.c  | 15 +++++++++++
 sysdeps/unix/sysv/linux/unshare.c             | 26 +++++++++++++++++++
 4 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/unshare.c

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 4dbe7cf1078..ea500c14997 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -109,6 +109,7 @@ sysdep_routines += \
   timerfd_settime \
   umount \
   umount2 \
+  unshare \
   vmsplice \
   xmknod \
   xmknodat \
diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list
index 0db9478e5f4..2af3aec8cd0 100644
--- a/sysdeps/unix/sysv/linux/syscalls.list
+++ b/sysdeps/unix/sysv/linux/syscalls.list
@@ -68,7 +68,6 @@ sigaltstack	-	sigaltstack	i:PP	__sigaltstack	sigaltstack
 sysinfo		EXTRA	sysinfo		i:p	__sysinfo	sysinfo
 swapon		-	swapon		i:si	__swapon	swapon
 swapoff		-	swapoff		i:s	__swapoff	swapoff
-unshare		EXTRA	unshare		i:i	unshare
 uselib		EXTRA	uselib		i:s	__compat_uselib	uselib@GLIBC_2.0:GLIBC_2.23
 
 chown		-	chown		i:sii	__libc_chown	__chown chown
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
index f46b96b2362..b286e976bdf 100644
--- a/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
+++ b/sysdeps/unix/sysv/linux/tst-syscall-upper-bits.c
@@ -17,6 +17,7 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
+#include <sched.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -153,6 +154,19 @@ test_remap_file_pages (void)
   xclose (fd);
 }
 
+static int __attribute__ ((noipa))
+call_unshare (uint64_t flags)
+{
+  return unshare (flags);
+}
+
+/* The kernel rejects unknown flags.  */
+static void
+test_unshare (void)
+{
+  COMPARE_CALLS (unshare (0), call_unshare (DIRTY (0)));
+}
+
 static long int __attribute__ ((noipa))
 call_ptrace (uint64_t request, pid_t pid, void *addr, void *data)
 {
@@ -192,6 +206,7 @@ do_test (void)
   test_pkey_mprotect ();
   test_pkey_alloc ();
   test_remap_file_pages ();
+  test_unshare ();
   test_ptrace ();
 
   return 0;
diff --git a/sysdeps/unix/sysv/linux/unshare.c b/sysdeps/unix/sysv/linux/unshare.c
new file mode 100644
index 00000000000..1e7a322a862
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/unshare.c
@@ -0,0 +1,26 @@
+/* Disassociate parts of the process execution context.  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 <sched.h>
+#include <sysdep.h>
+
+int
+unshare (int flags)
+{
+  return INLINE_SYSCALL_CALL (unshare, flags);
+}
-- 
2.53.0



More information about the Libc-alpha mailing list