]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
tools/nolibc: add namespace functionality
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Mon, 28 Apr 2025 12:40:13 +0000 (14:40 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Wed, 21 May 2025 13:32:12 +0000 (15:32 +0200)
This is used in various selftests and will be handy when integrating
those with nolibc.

Not all configurations support namespaces, so skip the tests where
necessary.  Also if the tests are running without privileges.
Enable the namespace configuration for those architectures where it is not
enabled by default.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-12-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/Makefile
tools/include/nolibc/nolibc.h
tools/include/nolibc/sched.h [new file with mode: 0644]
tools/testing/selftests/nolibc/Makefile
tools/testing/selftests/nolibc/nolibc-test.c

index 58c4ede9c294fd06e038cc1fd5f2a59590788fa4..9ff78ae2152221444939e7098daa5485b7125f1e 100644 (file)
@@ -38,6 +38,7 @@ all_files := \
                math.h \
                nolibc.h \
                poll.h \
+               sched.h \
                signal.h \
                stackprotector.h \
                std.h \
index 37a55f703dfcde44bd938073f18c9a378eb65fcc..51c423a36b5957e2528e94d717d88e2383230cba 100644 (file)
 #include "sys/wait.h"
 #include "ctype.h"
 #include "elf.h"
+#include "sched.h"
 #include "signal.h"
 #include "unistd.h"
 #include "stdio.h"
diff --git a/tools/include/nolibc/sched.h b/tools/include/nolibc/sched.h
new file mode 100644 (file)
index 0000000..3222156
--- /dev/null
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * sched function definitions for NOLIBC
+ * Copyright (C) 2025 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_SCHED_H
+#define _NOLIBC_SCHED_H
+
+#include "sys.h"
+
+#include <linux/sched.h>
+
+/*
+ * int setns(int fd, int nstype);
+ */
+
+static __attribute__((unused))
+int sys_setns(int fd, int nstype)
+{
+       return my_syscall2(__NR_setns, fd, nstype);
+}
+
+static __attribute__((unused))
+int setns(int fd, int nstype)
+{
+       return __sysret(sys_setns(fd, nstype));
+}
+
+
+/*
+ * int unshare(int flags);
+ */
+
+static __attribute__((unused))
+int sys_unshare(int flags)
+{
+       return my_syscall1(__NR_unshare, flags);
+}
+
+static __attribute__((unused))
+int unshare(int flags)
+{
+       return __sysret(sys_unshare(flags));
+}
+
+#endif /* _NOLIBC_SCHED_H */
index 2671383045dbb51682fd62ac8c8df9c317157317..94176ffe46463548cc9bc787933b6cefa83d6502 100644 (file)
@@ -109,6 +109,8 @@ DEFCONFIG            = $(DEFCONFIG_$(XARCH))
 
 EXTRACONFIG_m68k      = -e CONFIG_BLK_DEV_INITRD
 EXTRACONFIG           = $(EXTRACONFIG_$(XARCH))
+EXTRACONFIG_arm       = -e CONFIG_NAMESPACES
+EXTRACONFIG_armthumb  = -e CONFIG_NAMESPACES
 
 # optional tests to run (default = all)
 TEST =
index 665d7631fbc6e9ecae469a4b143440ec0a43d4aa..d1157319b5d715dbc94ff9203b1d2a2e932aabcf 100644 (file)
@@ -1172,6 +1172,72 @@ int test_openat(void)
        return 0;
 }
 
+int test_namespace(void)
+{
+       int original_ns, new_ns, ret;
+       ino_t original_ns_ino;
+       struct stat stat_buf;
+
+       original_ns = open("/proc/self/ns/uts", O_RDONLY);
+       if (original_ns == -1)
+               return -1;
+
+       ret = fstat(original_ns, &stat_buf);
+       if (ret)
+               goto out;
+
+       original_ns_ino = stat_buf.st_ino;
+
+       ret = unshare(CLONE_NEWUTS);
+       if (ret)
+               goto out;
+
+       new_ns = open("/proc/self/ns/uts", O_RDONLY);
+       if (new_ns == -1) {
+               ret = new_ns;
+               goto out;
+       }
+
+       ret = fstat(new_ns, &stat_buf);
+       close(new_ns);
+       if (ret)
+               goto out;
+
+       if (stat_buf.st_ino == original_ns_ino) {
+               errno = EINVAL;
+               ret = -1;
+               goto out;
+       }
+
+       ret = setns(original_ns, CLONE_NEWUTS);
+       if (ret)
+               goto out;
+
+       new_ns = open("/proc/self/ns/uts", O_RDONLY);
+       if (new_ns == -1) {
+               ret = new_ns;
+               goto out;
+       }
+
+       ret = fstat(new_ns, &stat_buf);
+       if (ret)
+               goto out;
+
+       close(new_ns);
+
+       if (stat_buf.st_ino != original_ns_ino) {
+               errno = EINVAL;
+               ret = -1;
+               goto out;
+       }
+
+       ret = 0;
+
+out:
+       close(original_ns);
+       return ret;
+}
+
 /* Run syscall tests between IDs <min> and <max>.
  * Return 0 on success, non-zero on failure.
  */
@@ -1296,6 +1362,7 @@ int run_syscall(int min, int max)
                CASE_TEST(write_zero);        EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
                CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
                CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
+               CASE_TEST(namespace);         EXPECT_SYSZR(euid0 && proc, test_namespace()); break;
                case __LINE__:
                        return ret; /* must be last */
                /* note: do not set any defaults so as to permit holes above */