Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH] selftests/landlock: Test io_uring requests against the submitter's domain
@ 2026-11-28 21:31 Christopher Lusk via B4 Relay
  2026-11-28 21:37 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher Lusk via B4 Relay @ 2026-11-28 21:31 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: Günther Noack, Shuah Khan, linux-security-module,
	linux-kselftest, linux-kernel, Christopher Lusk

From: Christopher Lusk <clusk@northecho.dev>

No Landlock test covers requests submitted through io_uring.  Add one
that opens a file the ruleset allows and a file it denies with
IORING_OP_OPENAT, on a ring set up after the restriction, with the
request run inline, on an io-wq worker (IOSQE_ASYNC), and on the SQ
thread (IORING_SETUP_SQPOLL).

Signed-off-by: Christopher Lusk <clusk@northecho.dev>
---
Passes 3/3 on the Landlock next branch and v7.4-rc4; a kprobe confirms
the opens run on the io-wq worker and the SQ thread.  Without the
restriction, all three fail.  It only covers rings set up after the
restriction.  It skips on ENOSYS or EPERM, for kernels without io_uring
or with it disabled.

Drafted with LLM assistance; I reviewed, tested and edited it.
---
 tools/testing/selftests/landlock/config          |   1 +
 tools/testing/selftests/landlock/io_uring_test.c | 169 +++++++++++++++++++++++
 2 files changed, 170 insertions(+)

diff --git a/tools/testing/selftests/landlock/config b/tools/testing/selftests/landlock/config
index 5568d7a4b2d7..c8adb413ed34 100644
--- a/tools/testing/selftests/landlock/config
+++ b/tools/testing/selftests/landlock/config
@@ -5,6 +5,7 @@ CONFIG_CGROUP_SCHED=y
 CONFIG_ENABLE_DEFAULT_TRACERS=y
 CONFIG_FTRACE=y
 CONFIG_INET=y
+CONFIG_IO_URING=y
 CONFIG_IPC_NS=y
 CONFIG_IPV6=y
 CONFIG_KEYS=y
diff --git a/tools/testing/selftests/landlock/io_uring_test.c b/tools/testing/selftests/landlock/io_uring_test.c
new file mode 100644
index 000000000000..725b3e693bea
--- /dev/null
+++ b/tools/testing/selftests/landlock/io_uring_test.c
@@ -0,0 +1,169 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Landlock tests - io_uring
+ *
+ * Copyright © 2026 Christopher Lusk <clusk@northecho.dev>
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/landlock.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+
+#include "../../../include/io_uring/mini_liburing.h"
+
+#include "common.h"
+
+static const char dir_allowed[] = TMP_DIR "/allowed";
+static const char file_allowed[] = TMP_DIR "/allowed/f";
+static const char dir_denied[] = TMP_DIR "/denied";
+static const char file_denied[] = TMP_DIR "/denied/f";
+
+static void create_file(struct __test_metadata *const _metadata,
+			const char *const path)
+{
+	const int fd = open(path, O_CREAT | O_WRONLY | O_CLOEXEC, 0600);
+
+	ASSERT_LE(0, fd);
+	ASSERT_EQ(0, close(fd));
+}
+
+/* Allows LANDLOCK_ACCESS_FS_READ_FILE beneath dir_allowed only. */
+static void restrict_reads(struct __test_metadata *const _metadata)
+{
+	const struct landlock_ruleset_attr ruleset_attr = {
+		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
+	};
+	struct landlock_path_beneath_attr path_beneath = {
+		.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE,
+	};
+	int ruleset_fd;
+
+	ruleset_fd =
+		landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0);
+	ASSERT_LE(0, ruleset_fd);
+	path_beneath.parent_fd = open(dir_allowed, O_PATH | O_CLOEXEC);
+	ASSERT_LE(0, path_beneath.parent_fd);
+	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_PATH_BENEATH,
+				       &path_beneath, 0));
+	ASSERT_EQ(0, close(path_beneath.parent_fd));
+	ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
+	ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0));
+	ASSERT_EQ(0, close(ruleset_fd));
+}
+
+/* Submits an IORING_OP_OPENAT for @path and returns its CQE result in @res. */
+static void ring_openat(struct __test_metadata *const _metadata,
+			struct io_uring *const ring, const __u8 sqe_flags,
+			const char *const path, int *const res)
+{
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+
+	sqe = io_uring_get_sqe(ring);
+	ASSERT_NE(NULL, sqe);
+	memset(sqe, 0, sizeof(*sqe));
+	sqe->opcode = IORING_OP_OPENAT;
+	sqe->flags = sqe_flags;
+	sqe->fd = AT_FDCWD;
+	sqe->addr = (unsigned long)path;
+	sqe->open_flags = O_RDONLY | O_CLOEXEC;
+	ASSERT_EQ(1, io_uring_submit(ring));
+	/* mini_liburing does not wake an idle SQ thread. */
+	if ((ring->flags & IORING_SETUP_SQPOLL) &&
+	    (*ring->sq.kflags & IORING_SQ_NEED_WAKEUP))
+		ASSERT_LE(0, io_uring_enter(ring->ring_fd, 0, 0,
+					    IORING_ENTER_SQ_WAKEUP, NULL));
+	ASSERT_EQ(0, io_uring_wait_cqe(ring, &cqe));
+	*res = cqe->res;
+	io_uring_cqe_seen(ring);
+	if (*res >= 0)
+		EXPECT_EQ(0, close(*res));
+}
+
+/* clang-format off */
+FIXTURE(uring) {};
+/* clang-format on */
+
+FIXTURE_VARIANT(uring)
+{
+	const unsigned int setup_flags;
+	const __u8 sqe_flags;
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(uring, inline) {
+	/* clang-format on */
+	.setup_flags = 0,
+	.sqe_flags = 0,
+};
+
+/* Runs the request on an io-wq worker. */
+/* clang-format off */
+FIXTURE_VARIANT_ADD(uring, async) {
+	/* clang-format on */
+	.setup_flags = 0,
+	.sqe_flags = IOSQE_ASYNC,
+};
+
+/* Runs the request on the ring's SQ thread. */
+/* clang-format off */
+FIXTURE_VARIANT_ADD(uring, sqpoll) {
+	/* clang-format on */
+	.setup_flags = IORING_SETUP_SQPOLL,
+	.sqe_flags = 0,
+};
+
+FIXTURE_SETUP(uring)
+{
+	umask(0077);
+	ASSERT_EQ(0, mkdir(TMP_DIR, 0700));
+	ASSERT_EQ(0, mkdir(dir_allowed, 0700));
+	ASSERT_EQ(0, mkdir(dir_denied, 0700));
+	create_file(_metadata, file_allowed);
+	create_file(_metadata, file_denied);
+}
+
+FIXTURE_TEARDOWN(uring)
+{
+	EXPECT_EQ(0, unlink(file_allowed));
+	EXPECT_EQ(0, unlink(file_denied));
+	EXPECT_EQ(0, rmdir(dir_allowed));
+	EXPECT_EQ(0, rmdir(dir_denied));
+	EXPECT_EQ(0, rmdir(TMP_DIR));
+}
+
+/*
+ * A request on a ring that a sandboxed task sets up runs with that task's
+ * credentials, so the task's Landlock domain applies to it, wherever the
+ * request runs.
+ */
+TEST_F(uring, openat_after_restriction)
+{
+	struct io_uring_params params = {
+		.flags = variant->setup_flags,
+		.sq_thread_idle = 1000,
+	};
+	struct io_uring ring;
+	int ret, res;
+
+	restrict_reads(_metadata);
+	ASSERT_EQ(-1, open(file_denied, O_RDONLY | O_CLOEXEC));
+	ASSERT_EQ(EACCES, errno);
+
+	ret = io_uring_queue_init_params(8, &ring, &params);
+	if (ret == -1 && (errno == ENOSYS || errno == EPERM))
+		SKIP(return, "io_uring is not available");
+	ASSERT_EQ(0, ret);
+
+	ring_openat(_metadata, &ring, variant->sqe_flags, file_allowed, &res);
+	EXPECT_LE(0, res);
+	ring_openat(_metadata, &ring, variant->sqe_flags, file_denied, &res);
+	EXPECT_EQ(-EACCES, res);
+	io_uring_queue_exit(&ring);
+}
+
+TEST_HARNESS_MAIN

---
base-commit: bb872036147b78498276986ac54634fbc609efa3
change-id: 20261128-b4-io-uring-b07b3f8c4e3e

Best regards,
-- 
Christopher Lusk <clusk@northecho.dev>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 21:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 21:31 [PATCH] selftests/landlock: Test io_uring requests against the submitter's domain Christopher Lusk via B4 Relay
2026-11-28 21:37 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox