^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright © 2019 Alexey Dobriyan <adobriyan@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Permission to use, copy, modify, and distribute this software for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * purpose with or without fee is hereby granted, provided that the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * copyright notice and this permission notice appear in all copies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Test that setns(CLONE_NEWNET) points to new /proc/net content even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * if old one is in dcache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * FIXME /proc/net/unix is under CONFIG_UNIX which can be disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #undef NDEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static pid_t pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void f(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (pid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kill(pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int fd[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) char _ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int nsfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) atexit(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* Check for priviledges and syscall availability straight away. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (unshare(CLONE_NEWNET) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (errno == ENOSYS || errno == EPERM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Distinguisher between two otherwise empty net namespaces. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (socket(AF_UNIX, SOCK_STREAM, 0) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (pipe(fd) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (pid == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (pid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (unshare(CLONE_NEWNET) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (write(fd[1], &_, 1) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pause();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (read(fd[0], &_, 1) != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) char buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) snprintf(buf, sizeof(buf), "/proc/%u/ns/net", pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) nsfd = open(buf, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (nsfd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Reliably pin dentry into dcache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) (void)open("/proc/net/unix", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (setns(nsfd, CLONE_NEWNET) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kill(pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) char buf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ssize_t rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fd = open("/proc/net/unix", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define S "Num RefCount Protocol Flags Type St Inode Path\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rv = read(fd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) assert(rv == strlen(S));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) assert(memcmp(buf, S, strlen(S)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }