List /proc/net/dev_snmp6 of a new network namespace while a tun device
comes and goes:

- with a buffer that holds the dots and "lo", add a device whose name
  sorts first, finish the listing: "lo" is listed once

- with that device added and a buffer that holds the dots and it,
  remove it, finish the listing: "lo" is still listed

- with that device added, stall getdents64() on a userfaultfd page
  while it writes the device's entry, remove the device, finish the
  listing: "lo" is still listed

All three fail without the fix, the first lists "lo" twice, the other
two not at all.

Signed-off-by: Christian Brauner (Amutable) <[email protected]>
---
 tools/testing/selftests/proc/.gitignore           |   1 +
 tools/testing/selftests/proc/Makefile             |   1 +
 tools/testing/selftests/proc/proc-readdir-churn.c | 236 ++++++++++++++++++++++
 3 files changed, 238 insertions(+)

diff --git a/tools/testing/selftests/proc/.gitignore 
b/tools/testing/selftests/proc/.gitignore
index 9c9735570abf..8520c7878ea2 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -8,6 +8,7 @@
 /proc-maps-race
 /proc-multiple-procfs
 /proc-net-dev-lseek
+/proc-readdir-churn
 /proc-empty-vm
 /proc-pid-vm
 /proc-self-map-files-001
diff --git a/tools/testing/selftests/proc/Makefile 
b/tools/testing/selftests/proc/Makefile
index a7de2bb6d8be..e0c0d313f4d0 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -11,6 +11,7 @@ TEST_GEN_PROGS += proc-2-is-kthread
 TEST_GEN_PROGS += proc-loadavg-001
 TEST_GEN_PROGS += proc-maps-race
 TEST_GEN_PROGS += proc-net-dev-lseek
+TEST_GEN_PROGS += proc-readdir-churn
 TEST_GEN_PROGS += proc-empty-vm
 TEST_GEN_PROGS += proc-pid-vm
 TEST_GEN_PROGS += proc-self-map-files-001
diff --git a/tools/testing/selftests/proc/proc-readdir-churn.c 
b/tools/testing/selftests/proc/proc-readdir-churn.c
new file mode 100644
index 000000000000..223b8a6698ee
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-readdir-churn.c
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A listing of a /proc directory that takes more than one getdents64()
+ * must not show an entry twice or skip one when an entry is added or
+ * removed in between, and a listing must go on after an entry that is
+ * removed while it is being emitted. /proc/net/dev_snmp6/<dev> of a tun
+ * device comes and goes with the device.
+ */
+#define _GNU_SOURCE
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+#include <linux/userfaultfd.h>
+#include <poll.h>
+#include <pthread.h>
+#include <sched.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define SNMP6_DIR      "/proc/net/dev_snmp6"
+#define TUN_NAME       "a0"            /* sorts before "lo" */
+#define TUN_PATH       SNMP6_DIR "/" TUN_NAME
+#define SMALL_BUF      80              /* ".", ".." and one short name */
+#define NAMES          64
+
+static int tun_open(const char *name)
+{
+       struct ifreq ifr = {};
+       int fd;
+
+       fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
+       if (fd < 0)
+               return -1;
+       strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
+       ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
+       if (ioctl(fd, TUNSETIFF, &ifr)) {
+               close(fd);
+               return -1;
+       }
+       return fd;
+}
+
+/* one getdents64() into @buf, the names appended to @names */
+static int list(int fd, char *buf, size_t size, char names[][NAME_MAX + 1],
+               int *n)
+{
+       long ret, off;
+       int got = 0;
+
+       ret = syscall(__NR_getdents64, fd, buf, size);
+       if (ret < 0)
+               return -errno;
+       for (off = 0; off < ret;) {
+               struct dirent64 *d = (void *)(buf + off);
+
+               if (*n < NAMES)
+                       strcpy(names[(*n)++], d->d_name);
+               got++;
+               off += d->d_reclen;
+       }
+       return got;
+}
+
+static int count(char names[][NAME_MAX + 1], int n, const char *name)
+{
+       int i, c = 0;
+
+       for (i = 0; i < n; i++)
+               if (!strcmp(names[i], name))
+                       c++;
+       return c;
+}
+
+/* removes the device once getdents64() faults on its entry, then resolves */
+struct stall {
+       int uffd;
+       int tun;
+       unsigned long addr;
+       unsigned long len;
+       int removed;
+};
+
+static void *stall_resolve(void *arg)
+{
+       struct stall *s = arg;
+       struct pollfd pfd = { .fd = s->uffd, .events = POLLIN };
+       struct uffdio_zeropage zero = {
+               .range = { .start = s->addr, .len = s->len },
+       };
+       struct uffd_msg msg;
+
+       if (poll(&pfd, 1, 10000) != 1)
+               return NULL;
+       if (read(s->uffd, &msg, sizeof(msg)) != sizeof(msg) ||
+           msg.event != UFFD_EVENT_PAGEFAULT)
+               return NULL;
+       close(s->tun);
+       s->removed = access(TUN_PATH, F_OK) == -1;
+       ioctl(s->uffd, UFFDIO_ZEROPAGE, &zero);
+       return NULL;
+}
+
+FIXTURE(proc_readdir) {
+       int dir;
+       int n;
+       char names[NAMES][NAME_MAX + 1];
+       char buf[65536];
+};
+
+FIXTURE_SETUP(proc_readdir)
+{
+       if (unshare(CLONE_NEWNET))
+               SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno));
+       if (access("/dev/net/tun", F_OK))
+               SKIP(return, "/dev/net/tun: %s", strerror(errno));
+       self->dir = open(SNMP6_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+       if (self->dir < 0)
+               SKIP(return, SNMP6_DIR ": %s", strerror(errno));
+       self->n = 0;
+}
+
+FIXTURE_TEARDOWN(proc_readdir)
+{
+       close(self->dir);
+}
+
+/* a device added in front of the position doesn't repeat an entry */
+TEST_F(proc_readdir, add_in_front)
+{
+       int tun;
+
+       /* ".", ".." and "lo" */
+       ASSERT_EQ(list(self->dir, self->buf, SMALL_BUF, self->names, &self->n), 
3);
+       ASSERT_EQ(count(self->names, self->n, "lo"), 1);
+
+       tun = tun_open(TUN_NAME);
+       ASSERT_GE(tun, 0);
+       ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+
+       while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+                   &self->n) > 0)
+               ;
+       EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+       close(tun);
+}
+
+/* a device removed in front of the position doesn't skip an entry */
+TEST_F(proc_readdir, remove_in_front)
+{
+       int tun;
+
+       tun = tun_open(TUN_NAME);
+       ASSERT_GE(tun, 0);
+       ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+
+       /* ".", ".." and the device, "lo" doesn't fit */
+       ASSERT_EQ(list(self->dir, self->buf, SMALL_BUF, self->names, &self->n), 
3);
+       ASSERT_EQ(count(self->names, self->n, TUN_NAME), 1);
+
+       /* closing the device unregisters it and removes its entry */
+       close(tun);
+       ASSERT_EQ(access(TUN_PATH, F_OK), -1);
+
+       while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+                   &self->n) > 0)
+               ;
+       EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+}
+
+/* the entry being emitted is removed meanwhile, the listing goes on */
+TEST_F(proc_readdir, remove_current)
+{
+       struct uffdio_api api = { .api = UFFD_API };
+       struct uffdio_register reg = {};
+       long page = sysconf(_SC_PAGESIZE);
+       struct stall s = {};
+       pthread_t thread;
+       char *map;
+       long ret, off;
+
+       s.uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+       if (s.uffd < 0)
+               SKIP(return, "userfaultfd: %s", strerror(errno));
+       ASSERT_EQ(ioctl(s.uffd, UFFDIO_API, &api), 0);
+
+       /* the first page is there, the second one faults */
+       map = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE,
+                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+       ASSERT_NE(map, MAP_FAILED);
+       map[0] = 0;
+       s.addr = (unsigned long)map + page;
+       s.len = page;
+       reg.range.start = s.addr;
+       reg.range.len = s.len;
+       reg.mode = UFFDIO_REGISTER_MODE_MISSING;
+       ASSERT_EQ(ioctl(s.uffd, UFFDIO_REGISTER, &reg), 0);
+
+       s.tun = tun_open(TUN_NAME);
+       ASSERT_GE(s.tun, 0);
+       ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+       ASSERT_EQ(pthread_create(&thread, NULL, stall_resolve, &s), 0);
+
+       /*
+        * ".." ends right before the second page, so writing the record of
+        * the device stalls until it has been removed.
+        */
+       ret = syscall(__NR_getdents64, self->dir, map + page - 48, 48 + page);
+       ASSERT_EQ(pthread_join(thread, NULL), 0);
+       ASSERT_TRUE(s.removed);
+       ASSERT_GT(ret, 0);
+       for (off = 0; off < ret;) {
+               struct dirent64 *d = (void *)(map + page - 48 + off);
+
+               if (self->n < NAMES)
+                       strcpy(self->names[self->n++], d->d_name);
+               off += d->d_reclen;
+       }
+       ASSERT_EQ(count(self->names, self->n, TUN_NAME), 1);
+
+       while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+                   &self->n) > 0)
+               ;
+       EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+       munmap(map, 2 * page);
+       close(s.uffd);
+}
+
+TEST_HARNESS_MAIN

-- 
2.53.0


Reply via email to