LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: CGEL <cgel.zte@gmail.com>
To: christian.brauner@ubuntu.com, dbueso@suse.de
Cc: cgel.zte@gmail.com, jamorris@linux.microsoft.com,
	keescook@chromium.org, ktkhai@virtuozzo.com, legion@kernel.org,
	linux-kernel@vger.kernel.org, ran.xiaokai@zte.com.cn,
	varad.gautam@suse.com
Subject: [PATCH V2] tests: add mqueue sysctl tests for user namespace
Date: Fri, 27 Aug 2021 02:50:27 -0700	[thread overview]
Message-ID: <20210827095027.5662-1-ran.xiaokai@zte.com.cn> (raw)
In-Reply-To: <20210824120523.s5qnzt643yvgugpv@wittgenstein>

From: Ran Xiaokai <ran.xiaokai@zte.com.cn>

when a ipc namespace is created in a user namespace, the mqueue sysctl
files should be writalbe to the owner of the user namespace. Even the
owner is not a global privileged user.

v2
  - add .gitignore change
  - move this test codes to the existing mqueue directory
  - rename test file name
  - use mkdtemp() creating mountpoint
v1
  - initial patch

Signed-off-by: Ran Xiaokai <ran.xiaokai@zte.com.cn>
---
 tools/testing/selftests/mqueue/.gitignore        |   1 +
 tools/testing/selftests/mqueue/Makefile          |   2 +-
 tools/testing/selftests/mqueue/mq_sysctl_tests.c | 175 +++++++++++++++++++++++
 3 files changed, 177 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/mqueue/mq_sysctl_tests.c

diff --git a/tools/testing/selftests/mqueue/.gitignore b/tools/testing/selftests/mqueue/.gitignore
index 72ad8ca..226fe58 100644
--- a/tools/testing/selftests/mqueue/.gitignore
+++ b/tools/testing/selftests/mqueue/.gitignore
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 mq_open_tests
 mq_perf_tests
+mq_sysctl_tests
diff --git a/tools/testing/selftests/mqueue/Makefile b/tools/testing/selftests/mqueue/Makefile
index 8a58055..31becad 100644
--- a/tools/testing/selftests/mqueue/Makefile
+++ b/tools/testing/selftests/mqueue/Makefile
@@ -2,6 +2,6 @@
 CFLAGS += -O2
 LDLIBS = -lrt -lpthread -lpopt
 
-TEST_GEN_PROGS := mq_open_tests mq_perf_tests
+TEST_GEN_PROGS := mq_open_tests mq_perf_tests mq_sysctl_tests
 
 include ../lib.mk
diff --git a/tools/testing/selftests/mqueue/mq_sysctl_tests.c b/tools/testing/selftests/mqueue/mq_sysctl_tests.c
new file mode 100644
index 0000000..931a915
--- /dev/null
+++ b/tools/testing/selftests/mqueue/mq_sysctl_tests.c
@@ -0,0 +1,175 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sched.h>
+#include <sys/mount.h>
+#include <mqueue.h>
+#include <sys/wait.h>
+#include <string.h>
+
+#include "../kselftest_harness.h"
+
+static ssize_t write_nointr(int fd, const void *buf, size_t count)
+{
+	ssize_t ret;
+
+	do {
+		ret = write(fd, buf, count);
+	} while (ret < 0 && errno == EINTR);
+
+	return ret;
+}
+
+static int write_file(const char *path, const void *buf, size_t count)
+{
+	int fd;
+	ssize_t ret;
+
+	fd = open(path, O_WRONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW);
+	if (fd < 0)
+		return -1;
+
+	ret = write_nointr(fd, buf, count);
+	close(fd);
+	if (ret < 0 || (size_t)ret != count)
+		return -1;
+
+	return 0;
+}
+
+static int create_and_enter_userns(void)
+{
+	uid_t uid;
+	gid_t gid;
+	char map[100];
+
+	uid = getuid();
+	gid = getgid();
+
+	if (unshare(CLONE_NEWUSER))
+		return -1;
+
+	if (write_file("/proc/self/setgroups", "deny", sizeof("deny") - 1) &&
+				errno != ENOENT)
+		return -1;
+
+	snprintf(map, sizeof(map), "0 %d 1", uid);
+	if (write_file("/proc/self/uid_map", map, strlen(map)))
+		return -1;
+
+	snprintf(map, sizeof(map), "0 %d 1", gid);
+	if (write_file("/proc/self/gid_map", map, strlen(map)))
+		return -1;
+
+	if (setgid(0))
+		return -1;
+
+	if (setuid(0))
+		return -1;
+
+	return 0;
+}
+
+static int prepare_unpriv_mountns(void)
+{
+	if (create_and_enter_userns())
+		return -1;
+
+	if (unshare(CLONE_NEWNS))
+		return -1;
+
+	if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0))
+		return -1;
+
+	if (unshare(CLONE_NEWIPC))
+		return -1;
+
+	return 0;
+}
+
+static int wait_for_pid(pid_t pid)
+{
+	int status, ret;
+
+again:
+	ret = waitpid(pid, &status, 0);
+	if (ret == -1) {
+		if (errno == EINTR)
+			goto again;
+
+		return -1;
+	}
+
+	if (!WIFEXITED(status))
+		return -1;
+
+	return WEXITSTATUS(status);
+}
+
+static int get_mq_queues_max(void)
+{
+	int fd;
+	char buf[16];
+	int val = -1;
+
+	fd = open("/proc/sys/fs/mqueue/queues_max", O_RDONLY);
+	if (fd >= 0) {
+		if (read(fd, buf, sizeof(buf)) > 0)
+			val = atoi(buf);
+
+		close(fd);
+		return val;
+	}
+	return val;
+}
+
+TEST(mqueue_sysctl)
+{
+	pid_t pid;
+	int qmax1, qmax2;
+	int dirfd;
+	char tmpdir[] = "/mqueue_sysctl_XXXXXX";
+
+	if (!mkdtemp(tmpdir))
+		SKIP(return, "create temp dir failed");
+
+	/* read and stash the original sysctl value */
+	qmax1 = get_mq_queues_max();
+	ASSERT_GE(qmax1, 0);
+
+	pid = fork();
+	ASSERT_GE(pid, 0);
+
+	if (pid == 0) {
+		ASSERT_EQ(prepare_unpriv_mountns(), 0);
+
+		ASSERT_EQ(mount("none", tmpdir, "mqueue", MS_NOATIME, NULL), 0);
+
+		/* modify the sysctl value in new ipc namesapce */
+		ASSERT_EQ(write_file("/proc/sys/fs/mqueue/queues_max", "1", 1), 0);
+
+		ASSERT_GE(mq_open("/new_ns1",  O_RDWR | O_CREAT, 0644, NULL), 0);
+
+		/* mq_open() should fail as exceeding of queues_max */
+		ASSERT_EQ(mq_open("/new_ns2",  O_RDWR | O_CREAT, 0644, NULL), -1);
+
+		ASSERT_EQ(mq_unlink("/new_ns1"), 0);
+		ASSERT_EQ(umount(tmpdir), 0);
+
+		exit(0);
+	}
+
+	ASSERT_EQ(wait_for_pid(pid), 0);
+
+	qmax2 = get_mq_queues_max();
+	ASSERT_EQ(qmax1, qmax2);
+
+	remove(tmpdir);
+}
+
+TEST_HARNESS_MAIN
-- 
2.15.2


  reply	other threads:[~2021-08-27  9:50 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-29  3:06 [PATCH] ipc: add set_ownership() and permissions() callbacks for posix mqueue sysctl cgel.zte
2021-07-29 14:53 ` Christian Brauner
2021-08-03 10:31   ` CGEL
2021-08-03 14:01     ` Christian Brauner
2021-08-11 15:51       ` CGEL
2021-08-23  3:29       ` [PATCH] tests: add mqueue sysctl tests for user namespace Ran Xiaokai
2021-08-23 15:26         ` Davidlohr Bueso
2021-08-24 12:05         ` Christian Brauner
2021-08-27  9:50           ` CGEL [this message]
2021-08-27 10:12           ` [PATCH V2] ipc: add set_ownership() and permissions() callbacks for posix mqueue sysctl CGEL
2021-09-13 14:40             ` Christian Brauner
2021-09-13 19:42               ` Davidlohr Bueso
2021-09-16  1:49               ` CGEL
2021-10-04 10:53                 ` Christian Brauner
2021-12-01  7:14                   ` CGEL
2021-12-01 12:53                     ` Christian Brauner
2022-04-06  7:59                       ` cgel.zte
2021-07-30 15:09 ` [PATCH] " Davidlohr Bueso
2021-08-03 10:34   ` CGEL

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210827095027.5662-1-ran.xiaokai@zte.com.cn \
    --to=cgel.zte@gmail.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=dbueso@suse.de \
    --cc=jamorris@linux.microsoft.com \
    --cc=keescook@chromium.org \
    --cc=ktkhai@virtuozzo.com \
    --cc=legion@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ran.xiaokai@zte.com.cn \
    --cc=varad.gautam@suse.com \
    --subject='Re: [PATCH V2] tests: add mqueue sysctl tests for user namespace' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).