Skip to contentSkip to conversation

Landlock: Restrict POSIX message queue creation and access

PATCH In review Oxana Kharitonova 4 participants 13 messages Nov 2 – Nov 6

Subsystems (6)
Documentation VFS Kselftest Tracing +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover Landlock: Restrict POSIX message queue creation and access

Hi,

Christian and Al, could you please review the mqueue-specific changes, particularly the shared MQUEUE_MAGIC definition in patch 1?

Mickaël, Günther, and Justin, thank you for your feedback. I have revised the changes and split the series into two parts: patches 1 and 2 enforce the existing MAKE_REG permission, while patches 3 and 4 introduce and expose the new scope. The first two patches can be applied without the latter two.

The new scope adds control based on the queue creator's Landlock domain, allowing sandboxes with shared filesystem access to isolate their queues. However, I am unsure how useful this additional isolation would be in practice.

The scope cannot replace the existing filesystem permission checks without changing behavior, because applications may already rely on them to restrict queue access. Both mechanisms therefore remain in place. When READ_FILE is handled and queue scoping is enabled, the checks combine as follows for opening an existing queue read-only, assuming other access controls permit the operation:

Filesystem read access | Queue scope | Expected result
-----------------------+-------------+----------------

Denied                 | Allowed     | EACCES
Denied                 | Denied      | EPERM
Allowed                | Allowed     | Success
Allowed                | Denied      | EPERM

Selftests cover all four combinations.

I would welcome feedback on whether this domain-based isolation provides enough practical value to justify the additional logic needed to track creator domains and manage their lifetimes.

Queue creation
==============

POSIX message queues are represented by files in the mqueue filesystem. Landlock's existing READ_FILE and WRITE_FILE restrictions can deny mq_open(), but these checks occur after queue creation. Consequently, mq_open() with O_CREAT may create a queue before access is denied, leaving the queue behind despite the failed call.

Patch 2 adds an inode_create hook to enforce MAKE_REG before queue creation. When this right is handled, creation requires an applicable rule allowing MAKE_REG on the queue's parent directory. Opening an existing queue does not require MAKE_REG.

Queue scoping
=============

Patch 3 adds LANDLOCK_SCOPE_POSIX_MSG_QUEUE through Landlock ABI 13. It restricts opening queues to those created within the domain enabling the scope or its descendants.

The restriction applies to mq_open() and opens through a mounted mqueue filesystem. Existing, inherited, and passed descriptors remain usable. Filesystem read/write restrictions continue to apply alongside the scope.

The implementation includes audit logging, audit quieting, and a dedicated denial trace event. Patch 4 exposes the scope and quieting options through the sandboxer sample.

Unlink and queue lifetime
========================

A queue's lifetime is independent of its creator. While its IPC namespace exists, the queue remains until its name is unlinked and all open descriptors are closed.

The scope leaves mq_unlink() unrestricted, subject to other access controls, so that an application can clean up stale queues from previous runs.

A process permitted to unlink and recreate a queue can replace its name, including from another Landlock domain. Existing descriptors continue to refer to the original queue, while subsequent opens refer to the replacement and undergo its scope check. The scope therefore does not guarantee the identity or availability of queues by name. These limitations are documented.

Changes since v1
================

- Separate MAKE_REG enforcement from the proposed queue scope.
- Store the creator-domain reference in landlock_object rather than directly in landlock_inode_security.
- Add an inode_create hook to enforce MAKE_REG before queue creation.
- Expand documentation of filesystem permissions, scoping, descriptor reuse, and unlink/name-replacement limitations.
- Extend selftest coverage, including audit logging and quieting, path-based opens, inherited descriptors, and descriptor passing.
- Enable CONFIG_POSIX_MQUEUE=y in the Landlock selftest configuration.

Oxana Kharitonova (4):
ipc: Move MQUEUE_MAGIC to a shared header
Landlock: Enforce MAKE_REG for POSIX message queue creation
Landlock: Add POSIX message queue scoping samples/landlock: Add POSIX message queue scoping to sandboxer

 Documentation/trace/events-landlock.rst       |   1 +
 Documentation/userspace-api/landlock.rst      |  80 +-
 include/linux/ipc_namespace.h                 |   2 +
 include/linux/landlock.h                      |   3 +-
 include/trace/events/landlock.h               |  46 +
 include/uapi/linux/landlock.h                 |   5 +
 ipc/mqueue.c                                  |   1 -
 samples/landlock/sandboxer.c                  |  26 +-
 security/landlock/audit.c                     |   5 +
 security/landlock/fs.c                        | 133 ++-
 security/landlock/fs.h                        |  12 +
 security/landlock/limits.h                    |   2 +-
 security/landlock/log.c                       |   4 +
 security/landlock/log.h                       |   1 +
 security/landlock/object.c                    |   2 +
 security/landlock/object.h                    |   7 +
 security/landlock/syscalls.c                  |   2 +-
 security/landlock/task.c                      |  66 ++
 security/landlock/task.h                      |   4 +
 security/landlock/trace.c                     |   7 +
 tools/testing/selftests/landlock/Makefile     |   4 +-
 tools/testing/selftests/landlock/audit_test.c | 114 +++
 tools/testing/selftests/landlock/base_test.c  |  40 +-
 tools/testing/selftests/landlock/config       |   1 +
 .../selftests/landlock/posix_mqueue_test.c    | 233 +++++
 .../landlock/scoped_posix_msg_queue_test.c    | 846 ++++++++++++++++++
 .../testing/selftests/landlock/scoped_test.c  |   2 +-
 27 files changed, 1634 insertions(+), 15 deletions(-)
 create mode 100644 tools/testing/selftests/landlock/posix_mqueue_test.c
 create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 ipc: Move MQUEUE_MAGIC to a shared header

Move MQUEUE_MAGIC from ipc/mqueue.c to include/linux/ipc_namespace.h so it can be shared by code outside of ipc/mqueue.c without duplicating the constant.

---
 include/linux/ipc_namespace.h | 2 ++
 ipc/mqueue.c                  | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/ipc_namespace.h +2 -0
index 12faca29bbb9..8a15ffe38e9a 100644
@@ -15,6 +15,8 @@
 
 struct user_namespace;
 
+#define MQUEUE_MAGIC 0x19800202
+
 struct ipc_ids {
 	int in_use;
 	unsigned short seq;
ipc/mqueue.c +0 -1
index d1dd36a651b0..0f00e4efcb60 100644
@@ -47,7 +47,6 @@ struct mqueue_fs_context {
 	bool			 newns;	/* Set if newly created ipc namespace */
 };
 
-#define MQUEUE_MAGIC	0x19800202
 #define DIRENT_SIZE	20
 #define FILENT_SIZE	80
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 2/4 Landlock: Enforce MAKE_REG for POSIX message queue creation

In reply to Oxana Kharitonova

POSIX message queues are represented by files in the mqueue filesystem. Landlock's existing READ_FILE and WRITE_FILE restrictions can deny mq_open() through hook_file_open(). However, these checks occur after queue creation, so mq_open() with O_CREAT may create a queue before access is denied, leaving the queue behind despite the failed call.

Add an inode_create hook to enforce LANDLOCK_ACCESS_FS_MAKE_REG before queue creation. Use the mqueue mount associated with the current IPC namespace to construct the parent path. Opening an existing queue does not require MAKE_REG.

Add selftests covering denied creation without leaving a queue behind, explicitly allowed creation, opening existing queues when MAKE_REG is denied, and existing read/write restrictions. Enable POSIX message queues in the selftest configuration and link the tests with librt.

Document the creation and opening checks, including that a queue may still remain if creation is allowed but the subsequent open is denied.

---
 Documentation/userspace-api/landlock.rst      |  24 ++
 include/uapi/linux/landlock.h                 |   1 +
 security/landlock/fs.c                        |  31 +++
 security/landlock/fs.h                        |   6 +
 tools/testing/selftests/landlock/Makefile     |   4 +-
 tools/testing/selftests/landlock/config       |   1 +
 .../selftests/landlock/posix_mqueue_test.c    | 233 ++++++++++++++++++
 7 files changed, 298 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/landlock/posix_mqueue_test.c
Documentation/userspace-api/landlock.rst +24 -0
index 3d45b956b640..1fb91884cb8a 100644
@@ -458,6 +458,30 @@ OverlayFS layer will not restrict the resulted merged hierarchy, and vice versa.
 Landlock users should then only think about file hierarchies they want to allow
 access to, regardless of the underlying filesystem.
 
+POSIX message queues
+--------------------
+
+POSIX message queues are represented by regular files in the mqueue
+filesystem.  Opening a queue with :manpage:`mq_open(3)` is subject to
+``LANDLOCK_ACCESS_FS_READ_FILE`` and ``LANDLOCK_ACCESS_FS_WRITE_FILE``,
+according to the requested access mode, when these rights are handled by an
+enforced ruleset.  These checks also apply to opening queue files through a
+mounted mqueue filesystem with :manpage:`open(2)`.
+
+Creating a new queue with :manpage:`mq_open(3)` and ``O_CREAT`` is checked
+separately.  When an enforced ruleset handles
+``LANDLOCK_ACCESS_FS_MAKE_REG``, creation is denied unless an applicable
+filesystem rule allows this right on the queue's parent directory.  Such a
+rule can refer to the root of the mqueue filesystem for the calling process's
+IPC namespace, commonly mounted at ``/dev/mqueue``.  Opening an existing queue,
+including with ``O_CREAT``, does not require this right.
+
+The creation check occurs before the file-open check.  If creation is allowed
+but the subsequent read or write access check denies the open, the newly
+created queue remains even though :manpage:`mq_open(3)` fails.  In particular,
+handling only ``LANDLOCK_ACCESS_FS_READ_FILE`` or
+``LANDLOCK_ACCESS_FS_WRITE_FILE`` does not prevent queue creation.
+
 Inheritance
 -----------
 
include/uapi/linux/landlock.h +1 -0
index 24510e5d8479..04a18824c700 100644
@@ -450,6 +450,7 @@ struct landlock_capability_attr {
  *   device.
  * - %LANDLOCK_ACCESS_FS_MAKE_DIR: Create (or rename) a directory.
  * - %LANDLOCK_ACCESS_FS_MAKE_REG: Create (or rename or link) a regular file.
+ *   This also controls POSIX message queue creation with mq_open(3).
  *   This also guards the creation of whiteout objects as used in OverlayFS.
  * - %LANDLOCK_ACCESS_FS_MAKE_SOCK: Create (or rename or link) a UNIX domain
  *   socket.
security/landlock/fs.c +31 -0
index 2ab67bcad198..d5dcb19d5eb1 100644
@@ -29,6 +29,7 @@
 #include <linux/mount.h>
 #include <linux/namei.h>
 #include <linux/net.h>
+#include <linux/nsproxy.h>
 #include <linux/path.h>
 #include <linux/pid.h>
 #include <linux/rcupdate.h>
@@ -1676,6 +1677,35 @@ static int hook_path_truncate(const struct path *const path)
 	return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE);
 }
 
+/*
+ * Unlike regular file creation, mq_open(2) calls vfs_mkobj() directly and
+ * therefore does not trigger the path_mknod hook.  Use the mqueuefs mount
+ * associated with the current IPC namespace to recover the parent path.
+ */
+static int hook_inode_create(struct inode *const dir,
+			     struct dentry *const dentry, const umode_t)
+{
+	struct ipc_namespace *ipc_ns;
+	struct vfsmount *mnt;
+	struct path parent;
+
+	if (!landlock_is_posix_mqueue_fs(dir))
+		return 0;
+
+	if (!current->nsproxy)
+		return 0;
+	ipc_ns = current->nsproxy->ipc_ns;
+	if (!ipc_ns)
+		return 0;
+	mnt = ipc_ns->mq_mnt;
+	if (WARN_ON_ONCE(!mnt || mnt->mnt_sb != dir->i_sb))
+		return 0;
+
+	parent.mnt = mnt;
+	parent.dentry = dentry->d_parent;
+	return current_check_access_path(&parent, LANDLOCK_ACCESS_FS_MAKE_REG);
+}
+
 /**
  * unmask_scoped_access - Remove access right bits in @masks in all layers
  *                        where @client and @server have the same domain
@@ -2084,6 +2114,7 @@ static void hook_file_free_security(struct file *file)
 
 static struct security_hook_list landlock_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(inode_free_security_rcu, hook_inode_free_security_rcu),
+	LSM_HOOK_INIT(inode_create, hook_inode_create),
 
 	LSM_HOOK_INIT(sb_delete, hook_sb_delete),
 	LSM_HOOK_INIT(sb_mount, hook_sb_mount),
security/landlock/fs.h +6 -0
index 50b3ddb8ac1c..05e36ef1acf7 100644
@@ -14,6 +14,7 @@
 #include <linux/cleanup.h>
 #include <linux/fs.h>
 #include <linux/init.h>
+#include <linux/ipc_namespace.h>
 #include <linux/rcupdate.h>
 
 #include "access.h"
@@ -144,6 +145,11 @@ landlock_inode(const struct inode *const inode)
 	return inode->i_security + landlock_blob_sizes.lbs_inode;
 }
 
+static inline bool landlock_is_posix_mqueue_fs(const struct inode *const inode)
+{
+	return inode->i_sb->s_magic == MQUEUE_MAGIC;
+}
+
 static inline struct landlock_superblock_security *
 landlock_superblock(const struct super_block *const superblock)
 {
tools/testing/selftests/landlock/Makefile +2 -2
index fc43225d319a..80da63f4251d 100644
@@ -18,11 +18,11 @@ TEST_GEN_PROGS_EXTENDED := \
 	wait-pipe-sandbox
 
 # Short targets:
-$(TEST_GEN_PROGS): LDLIBS += -lcap -lpthread
+$(TEST_GEN_PROGS): LDLIBS += -lcap -lpthread -lrt
 $(TEST_GEN_PROGS_EXTENDED): LDFLAGS += -static
 
 include ../lib.mk
 
 # Targets with $(OUTPUT)/ prefix:
-$(TEST_GEN_PROGS): LDLIBS += -lcap -lpthread
+$(TEST_GEN_PROGS): LDLIBS += -lcap -lpthread -lrt
 $(TEST_GEN_PROGS_EXTENDED): LDFLAGS += -static
tools/testing/selftests/landlock/config +1 -0
index 5568d7a4b2d7..7761916db48b 100644
@@ -14,6 +14,7 @@ CONFIG_NET=y
 CONFIG_NET_NS=y
 CONFIG_OVERLAY_FS=y
 CONFIG_PID_NS=y
+CONFIG_POSIX_MQUEUE=y
 CONFIG_PROC_FS=y
 CONFIG_SECURITY=y
 CONFIG_SECURITY_LANDLOCK=y
tools/testing/selftests/landlock/posix_mqueue_test.c new file +233 -0

233 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 3/4 Landlock: Add POSIX message queue scoping

In reply to Oxana Kharitonova

Filesystem access rights control POSIX message queue creation and opening, but do not distinguish queues by their creator's Landlock domain. Sandboxes sharing access to the mqueue filesystem may therefore open each other's queues when other access controls permit it.

Add LANDLOCK_SCOPE_POSIX_MSG_QUEUE to restrict opening queues to those created within the domain enabling the scope or its descendants. Associate queues with their creator's domain and check this boundary before filesystem access checks when opening a queue.

Apply the scope to mq_open() and opens through a mounted mqueue filesystem. Leave existing, inherited, and passed descriptors usable. Do not restrict mq_unlink(), allowing applications to clean up stale queues, subject to other access controls. Document the resulting limitations around unlinking and queue name reuse.

Expose the scope through ABI 13, with audit logging, audit quieting, and a dedicated denial trace event.

Add selftests covering domain relationships, queue lifetime, filesystem permission interactions, path-based opens, descriptor inheritance and passing, UAPI validation, and audit logging and quieting.

---
 Documentation/trace/events-landlock.rst       |   1 +
 Documentation/userspace-api/landlock.rst      |  56 +-
 include/linux/landlock.h                      |   3 +-
 include/trace/events/landlock.h               |  46 +
 include/uapi/linux/landlock.h                 |   4 +
 security/landlock/audit.c                     |   5 +
 security/landlock/fs.c                        | 102 ++-
 security/landlock/fs.h                        |   6 +
 security/landlock/limits.h                    |   2 +-
 security/landlock/log.c                       |   4 +
 security/landlock/log.h                       |   1 +
 security/landlock/object.c                    |   2 +
 security/landlock/object.h                    |   7 +
 security/landlock/syscalls.c                  |   2 +-
 security/landlock/task.c                      |  66 ++
 security/landlock/task.h                      |   4 +
 security/landlock/trace.c                     |   7 +
 tools/testing/selftests/landlock/audit_test.c | 114 +++
 tools/testing/selftests/landlock/base_test.c  |  40 +-
 .../landlock/scoped_posix_msg_queue_test.c    | 846 ++++++++++++++++++
 .../testing/selftests/landlock/scoped_test.c  |   2 +-
 21 files changed, 1312 insertions(+), 8 deletions(-)
 create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c
Documentation/trace/events-landlock.rst +1 -0
index 9ccf2769a2d5..1f55c67650ed 100644
@@ -46,6 +46,7 @@ Landlock trace events are organized in four categories:
 - ``landlock_deny_scope_signal``: signal delivery denied
 - ``landlock_deny_scope_abstract_unix_socket``: abstract unix socket
   access denied
+- ``landlock_deny_scope_posix_msg_queue``: POSIX message queue open denied
 - ``landlock_deny_permission_namespace``: namespace use denied
 - ``landlock_deny_permission_capability``: capability use denied
 
Documentation/userspace-api/landlock.rst +55 -1

86 lines not on this page · raw message

include/linux/landlock.h +2 -1
index 0ee4880988ba..17501fd05b7a 100644
@@ -53,7 +53,8 @@
 #define _LANDLOCK_SCOPE_NAMES \
 	_LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET, \
 			     "abstract_unix_socket"), \
-	_LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_SIGNAL, "signal")
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_SIGNAL, "signal"), \
+	_LANDLOCK_NAME_ENTRY(LANDLOCK_SCOPE_POSIX_MSG_QUEUE, "posix_msg_queue")
 
 #define _LANDLOCK_PERMISSION_NAMESPACE_NAME "namespace"
 #define _LANDLOCK_PERMISSION_CAPABILITY_NAME "capability"
include/trace/events/landlock.h +46 -0

52 lines not on this page · raw message

include/uapi/linux/landlock.h +4 -0
index 04a18824c700..a011667dcfd1 100644
@@ -588,10 +588,14 @@ struct landlock_capability_attr {
  *   related Landlock domain (e.g., a parent domain or a non-sandboxed process).
  * - %LANDLOCK_SCOPE_SIGNAL: Restrict a sandboxed process from sending a signal
  *   to another process outside the domain.
+ * - %LANDLOCK_SCOPE_POSIX_MSG_QUEUE: Restrict a sandboxed process from opening
+ *   a POSIX message queue outside the domain.  Support added in Landlock ABI
+ *   version 13.
  */
 /* clang-format off */
 #define LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET		(1ULL << 0)
 #define LANDLOCK_SCOPE_SIGNAL		                (1ULL << 1)
+#define LANDLOCK_SCOPE_POSIX_MSG_QUEUE		        (1ULL << 2)
 /* clang-format on*/
 
 /**
security/landlock/audit.c +5 -0
index be49ef47b4ea..df9104b2939d 100644
@@ -80,6 +80,10 @@ get_blocker(const enum landlock_request_type type,
 		WARN_ON_ONCE(access_bit != -1);
 		return scope_strings[BIT_INDEX(LANDLOCK_SCOPE_SIGNAL)];
 
+	case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE:
+		WARN_ON_ONCE(access_bit != -1);
+		return scope_strings[BIT_INDEX(LANDLOCK_SCOPE_POSIX_MSG_QUEUE)];
+
 	case LANDLOCK_REQUEST_NAMESPACE:
 	case LANDLOCK_REQUEST_CAPABILITY:
 		if (WARN_ON_ONCE(access_bit >= ARRAY_SIZE(permission_strings)))
@@ -113,6 +117,7 @@ blocker_prefix(const enum landlock_request_type type)
 
 	case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET:
 	case LANDLOCK_REQUEST_SCOPE_SIGNAL:
+	case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE:
 		return "scope.";
 
 	case LANDLOCK_REQUEST_NAMESPACE:
security/landlock/fs.c +100 -2

150 lines not on this page · raw message

security/landlock/fs.h +6 -0
index 05e36ef1acf7..85025030659c 100644
@@ -150,6 +150,12 @@ static inline bool landlock_is_posix_mqueue_fs(const struct inode *const inode)
 	return inode->i_sb->s_magic == MQUEUE_MAGIC;
 }
 
+static inline bool
+landlock_is_posix_mqueue_inode(const struct inode *const inode)
+{
+	return S_ISREG(inode->i_mode) && landlock_is_posix_mqueue_fs(inode);
+}
+
 static inline struct landlock_superblock_security *
 landlock_superblock(const struct super_block *const superblock)
 {
security/landlock/limits.h +1 -1
index ddd300e10689..469ddcb4a65f 100644
@@ -29,7 +29,7 @@
 #define LANDLOCK_MASK_ACCESS_NET	((LANDLOCK_LAST_ACCESS_NET << 1) - 1)
 #define LANDLOCK_NUM_ACCESS_NET		__const_hweight64(LANDLOCK_MASK_ACCESS_NET)
 
-#define LANDLOCK_LAST_SCOPE		LANDLOCK_SCOPE_SIGNAL
+#define LANDLOCK_LAST_SCOPE		LANDLOCK_SCOPE_POSIX_MSG_QUEUE
 #define LANDLOCK_MASK_SCOPE		((LANDLOCK_LAST_SCOPE << 1) - 1)
 #define LANDLOCK_NUM_SCOPE		__const_hweight64(LANDLOCK_MASK_SCOPE)
 
security/landlock/log.c +4 -0
index 43efbb95725c..d2e0b5de3ce3 100644
@@ -406,6 +406,7 @@ static bool is_valid_request(const struct landlock_request *const request)
 	case LANDLOCK_REQUEST_NET_ACCESS:
 	case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET:
 	case LANDLOCK_REQUEST_SCOPE_SIGNAL:
+	case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE:
 		if (WARN_ON_ONCE(request->permission))
 			return false;
 		break;
@@ -485,6 +486,9 @@ is_denial_quieted(const struct landlock_request *const request,
 	case LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET:
 		return !!(youngest_denied->quiet_access.scope &
 			  LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET);
+	case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE:
+		return !!(youngest_denied->quiet_access.scope &
+			  LANDLOCK_SCOPE_POSIX_MSG_QUEUE);
 	case LANDLOCK_REQUEST_NAMESPACE:
 		return !!(youngest_denied->quiet_permission.ns_types &
 			  landlock_ns_type_to_bit(request->audit.u.ns.ns_type));
security/landlock/log.h +1 -0
index 596feeeb7aa6..8eec738d3ca4 100644
@@ -25,6 +25,7 @@ enum landlock_request_type {
 	LANDLOCK_REQUEST_NET_ACCESS,
 	LANDLOCK_REQUEST_SCOPE_ABSTRACT_UNIX_SOCKET,
 	LANDLOCK_REQUEST_SCOPE_SIGNAL,
+	LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE,
 	LANDLOCK_REQUEST_NAMESPACE,
 	LANDLOCK_REQUEST_CAPABILITY,
 };
security/landlock/object.c +2 -0
index 0d6e159ef8b5..bea45c855a91 100644
@@ -15,6 +15,7 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 
+#include "domain.h"
 #include "object.h"
 
 struct landlock_object *
@@ -62,6 +63,7 @@ void landlock_put_object(struct landlock_object *const object)
 		 * @object->underobj to @object (if it still exists).
 		 */
 		object->underops->release(object);
+		landlock_put_domain_deferred(object->domain);
 		kfree_rcu(object, rcu_free);
 	}
 }
security/landlock/object.h +7 -0
index 5f28c35e8aa8..3518ee36ab71 100644
@@ -14,6 +14,7 @@
 #include <linux/spinlock.h>
 
 struct landlock_object;
+struct landlock_domain;
 
 /**
  * struct landlock_object_underops - Operations on an underlying object
@@ -74,6 +75,12 @@ struct landlock_object {
 		 */
 		const struct landlock_object_underops *underops;
 	};
+	/**
+	 * @domain: Domain associated with a POSIX message queue object.  While
+	 * non-NULL, the object owns both a reference to this domain and the usage
+	 * reference retained when the queue was tagged.  Protected by @lock.
+	 */
+	struct landlock_domain *domain;
 };
 
 struct landlock_object *
security/landlock/syscalls.c +1 -1
index fb5af318b69b..11d3a2abcec1 100644
@@ -191,7 +191,7 @@ static const struct file_operations ruleset_fops = {
  * If the change involves a fix that requires userspace awareness, also update
  * the errata documentation in Documentation/userspace-api/landlock.rst .
  */
-const int landlock_abi_version = 12;
+const int landlock_abi_version = 13;
 
 /**
  * sys_landlock_create_ruleset - Create a new ruleset
security/landlock/task.c +66 -0

72 lines not on this page · raw message

security/landlock/task.h +4 -0
index 7c00360219a2..0b031dc17bbf 100644
@@ -9,6 +9,10 @@
 #ifndef _SECURITY_LANDLOCK_TASK_H
 #define _SECURITY_LANDLOCK_TASK_H
 
+#include <linux/fs.h>
+
 __init void landlock_add_task_hooks(void);
 
+int landlock_check_posix_mqueue_open(struct file *const file);
+
 #endif /* _SECURITY_LANDLOCK_TASK_H */
security/landlock/trace.c +7 -0
index 995c6b948e16..1d2bb0c3175b 100644
@@ -246,6 +246,13 @@ void landlock_trace_denial(
 				request->other_domain_id,
 				request->audit.u.net->sk);
 		break;
+	case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE:
+		if (trace_landlock_deny_scope_posix_msg_queue_enabled())
+			trace_landlock_deny_scope_posix_msg_queue(
+				youngest_denied, same_exec, logged,
+				request->other_domain_id,
+				request->audit.u.file);
+		break;
 	default:
 		WARN_ONCE(1, "Unhandled Landlock request type %d",
 			  request->type);
tools/testing/selftests/landlock/audit_test.c +114 -0

124 lines not on this page · raw message

tools/testing/selftests/landlock/base_test.c +39 -1
index 89c0d96607de..1f361c6fe3ee 100644
@@ -76,7 +76,7 @@ TEST(abi_version)
 	const struct landlock_ruleset_attr ruleset_attr = {
 		.handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE,
 	};
-	ASSERT_EQ(12, landlock_create_ruleset(NULL, 0,
+	ASSERT_EQ(13, landlock_create_ruleset(NULL, 0,
 					      LANDLOCK_CREATE_RULESET_VERSION));
 
 	ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
@@ -755,4 +755,42 @@ TEST(invalid_quiet_bits_2)
 	ASSERT_EQ(EINVAL, errno);
 }
 
+/* Checks the UAPI validation of LANDLOCK_SCOPE_POSIX_MSG_QUEUE. */
+TEST(posix_mqueue_scope_uapi)
+{
+	const struct landlock_ruleset_attr scoped = {
+		.scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+	};
+	const struct landlock_ruleset_attr quiet = {
+		.scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+		.quiet_scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+	};
+	const struct landlock_ruleset_attr quiet_without_scope = {
+		.scoped = LANDLOCK_SCOPE_SIGNAL,
+		.quiet_scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+	};
+	const struct landlock_ruleset_attr unknown_scope = {
+		.scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE << 1,
+	};
+	int ruleset_fd;
+
+	drop_caps(_metadata);
+
+	ruleset_fd = landlock_create_ruleset(&scoped, sizeof(scoped), 0);
+	ASSERT_LE(0, ruleset_fd);
+	EXPECT_EQ(0, close(ruleset_fd));
+
+	ruleset_fd = landlock_create_ruleset(&quiet, sizeof(quiet), 0);
+	ASSERT_LE(0, ruleset_fd);
+	EXPECT_EQ(0, close(ruleset_fd));
+
+	EXPECT_EQ(-1, landlock_create_ruleset(&quiet_without_scope,
+					      sizeof(quiet_without_scope), 0));
+	EXPECT_EQ(EINVAL, errno);
+
+	EXPECT_EQ(-1, landlock_create_ruleset(&unknown_scope,
+					      sizeof(unknown_scope), 0));
+	EXPECT_EQ(EINVAL, errno);
+}
+
 TEST_HARNESS_MAIN
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +846 -0

846 lines not on this page · raw message

tools/testing/selftests/landlock/scoped_test.c +1 -1
index b90f76ed0d9c..c530baa50948 100644
@@ -12,7 +12,7 @@
 
 #include "common.h"
 
-#define ACCESS_LAST LANDLOCK_SCOPE_SIGNAL
+#define ACCESS_LAST LANDLOCK_SCOPE_POSIX_MSG_QUEUE
 
 TEST(ruleset_with_unknown_scope)
 {
Signature · 1 line
2.43.0
#11 lore raw

PATCH v2 4/4 samples/landlock: Add POSIX message queue scoping to sandboxer

In reply to Oxana Kharitonova

Add "q" to LL_SCOPED to enable LANDLOCK_SCOPE_POSIX_MSG_QUEUE and "posix_msg_queue" to LL_QUIET_ACCESS to quiet related audit denials. Update the supported ABI version to 13 and disable the new scope when running on older kernels.

For example, on a kernel supporting Landlock ABI 13:

  LL_FS_RO="/" LL_FS_RW="" LL_SCOPED="q" \
    ./samples/landlock/sandboxer /bin/sh

The resulting shell and its children cannot open POSIX message queues created outside their Landlock domain. They can create queues and open queues created within that domain.

---
 samples/landlock/sandboxer.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)
samples/landlock/sandboxer.c +22 -4
index 1362f08ff14b..e8e05c4d6f63 100644
@@ -441,10 +441,12 @@ static bool check_ruleset_scope(const char *const env_var,
 	bool error = false;
 	bool abstract_scoping = false;
 	bool signal_scoping = false;
+	bool posix_msg_queue_scoping = false;
 
 	/* Scoping is not supported by Landlock ABI */
 	if (!(ruleset_attr->scoped &
-	      (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL)))
+	      (LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | LANDLOCK_SCOPE_SIGNAL |
+	       LANDLOCK_SCOPE_POSIX_MSG_QUEUE)))
 		goto out_unset;
 
 	env_type_scope = getenv(env_var);
@@ -461,6 +463,9 @@ static bool check_ruleset_scope(const char *const env_var,
 		} else if (strcmp("s", ipc_scoping_name) == 0 &&
 			   !signal_scoping) {
 			signal_scoping = true;
+		} else if (strcmp("q", ipc_scoping_name) == 0 &&
+			   !posix_msg_queue_scoping) {
+			posix_msg_queue_scoping = true;
 		} else {
 			fprintf(stderr, "Unknown or duplicate scope \"%s\"\n",
 				ipc_scoping_name);
@@ -477,6 +482,8 @@ static bool check_ruleset_scope(const char *const env_var,
 		ruleset_attr->scoped &= ~LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
 	if (!signal_scoping)
 		ruleset_attr->scoped &= ~LANDLOCK_SCOPE_SIGNAL;
+	if (!posix_msg_queue_scoping)
+		ruleset_attr->scoped &= ~LANDLOCK_SCOPE_POSIX_MSG_QUEUE;
 
 	unsetenv(env_var);
 	return error;
@@ -555,6 +562,9 @@ static int add_quiet_access(const char *const env_var,
 				LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET;
 		else if (strcmp(str_access, "signal") == 0)
 			ruleset_attr->quiet_scoped |= LANDLOCK_SCOPE_SIGNAL;
+		else if (strcmp(str_access, "posix_msg_queue") == 0)
+			ruleset_attr->quiet_scoped |=
+				LANDLOCK_SCOPE_POSIX_MSG_QUEUE;
 		else {
 			fprintf(stderr, "Unknown quiet access \"%s\"\n",
 				str_access);
@@ -570,7 +580,7 @@ static int add_quiet_access(const char *const env_var,
 	return 0;
 }
 
-#define LANDLOCK_ABI_LAST 12
+#define LANDLOCK_ABI_LAST 13
 
 #define XSTR(s) #s
 #define STR(s) XSTR(s)
@@ -618,6 +628,7 @@ static const char help[] =
 	"* " ENV_SCOPED_NAME ": actions denied on the outside of the landlock domain\n"
 	"  - \"a\" to restrict opening abstract unix sockets\n"
 	"  - \"s\" to restrict sending signals\n"
+	"  - \"q\" to restrict opening POSIX message queues\n"
 	"\n"
 	"A sandboxer should not log denied access requests to avoid spamming logs, "
 	"but to test audit we can set " ENV_FORCE_LOG_NAME "=1\n"
@@ -634,6 +645,7 @@ static const char help[] =
 	"  - \"udp_connect\" to quiet udp connect / send denials\n"
 	"  - \"abstract_unix_socket\" to quiet abstract unix socket denials\n"
 	"  - \"signal\" to quiet signal denials\n"
+	"  - \"posix_msg_queue\" to quiet POSIX message queue denials\n"
 	"\n"
 	"Example:\n"
 	ENV_FS_RO_NAME "=\"${PATH}:/lib:/usr:/proc:/etc:/dev/urandom\" "
@@ -643,7 +655,7 @@ static const char help[] =
 	ENV_UDP_CONNECT_SEND_NAME "=\"53\" "
 	ENV_NS_NAME "=\"user:uts:net\" "
 	ENV_CAP_NAME "=\"" CAP_VALUE_EXAMPLE "\" "
-	ENV_SCOPED_NAME "=\"a:s\" "
+	ENV_SCOPED_NAME "=\"a:s:q\" "
 	"%1$s bash -i\n"
 	"\n"
 	"This sandboxer can use Landlock features up to ABI version "
@@ -667,7 +679,8 @@ int main(const int argc, char *const argv[], char *const *const envp)
 				      LANDLOCK_ACCESS_NET_BIND_UDP |
 				      LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP,
 		.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET |
-			  LANDLOCK_SCOPE_SIGNAL,
+			  LANDLOCK_SCOPE_SIGNAL |
+			  LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
 		.quiet_access_fs = 0,
 		.quiet_access_net = 0,
 		.quiet_scoped = 0,
@@ -780,6 +793,11 @@ int main(const int argc, char *const argv[], char *const *const envp)
 		ruleset_attr.handled_permissions &=
 			~(LANDLOCK_PERMISSION_NAMESPACE_USE |
 			  LANDLOCK_PERMISSION_CAPABILITY_USE);
+		__attribute__((fallthrough));
+	case 12:
+		/* Removes POSIX message queue scoping for ABI < 13 */
+		ruleset_attr.scoped &= ~LANDLOCK_SCOPE_POSIX_MSG_QUEUE;
+
 		/* Must be printed for any ABI < LANDLOCK_ABI_LAST. */
 		fprintf(stderr,
 			"Hint: You should update the running kernel "
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/4] ipc: Move MQUEUE_MAGIC to a shared header

In reply to Oxana Kharitonova
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines

Move MQUEUE_MAGIC from ipc/mqueue.c to include/linux/ipc_namespace.h so it can be shared by code outside of ipc/mqueue.c without duplicating the constant.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#5 lore raw

Re: [PATCH v2 2/4] Landlock: Enforce MAKE_REG for POSIX message queue creation

In reply to Oxana Kharitonova
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] landlock: incompatible hook_inode_create() signature omits mnt_idmap - [High] landlock…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] landlock: incompatible hook_inode_create() signature omits mnt_idmap
- [High] landlock: user-triggerable WARN_ON_ONCE() in hook_inode_create()
- [High] landlock: hook_inode_create() breaks VFS create on mounted mqueuefs
- [Low] landlock: unnamed parameter in hook_inode_create() breaks gnu11 build --

commit 8666d4a55fba496ff4bfd048b949b769a8d57ec6
Author: Oxana Kharitonova <[email protected]>

Landlock: Enforce MAKE_REG for POSIX message queue creation

This commit adds an inode_create LSM hook in Landlock to enforce LANDLOCK_ACCESS_FS_MAKE_REG when creating POSIX message queues before the queue is created in the mqueue filesystem. It also documents the behavior and adds selftests for POSIX message queue creation and opening.

Quoted 4 lines
security/landlock/fs.c +0 -0
index 2ab67bcad1983..d5dcb19d5eb19 100644

[ ... ]

security/landlock/fs.c static int hook_path_truncate(const struct path *const path) · line 1686

Quoted 11 lines
security/landlock/fs.c +7 -0
@@ -1676,6 +1677,35 @@ static int hook_path_truncate(const struct path *const path)
 	return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE);
 }
 
+/*
+ * Unlike regular file creation, mq_open(2) calls vfs_mkobj() directly and
+ * therefore does not trigger the path_mknod hook.  Use the mqueuefs mount
+ * associated with the current IPC namespace to recover the parent path.
+ */
+static int hook_inode_create(struct inode *const dir,
+			     struct dentry *const dentry, const umode_t)

[Severity: High]
Does hook_inode_create() in security/landlock/fs.c have the wrong function signature for the inode_create LSM hook?

In include/linux/lsm_hook_defs.h, inode_create is declared with four parameters, starting with const struct mnt_idmap *idmap:

LSM_HOOK(int, 0, inode_create, const struct mnt_idmap *idmap,
struct inode *dir, struct dentry *dentry, umode_t mode)

Because hook_inode_create() omits the leading idmap parameter, registering it via LSM_HOOK_INIT(inode_create, hook_inode_create) in landlock_hooks fails to build with -Werror=incompatible-pointer-types when CONFIG_SECURITY_LANDLOCK is enabled (and would cause the mnt_idmap pointer to be passed as dir at runtime).

[Severity: Low]
Also in hook_inode_create() in security/landlock/fs.c, the trailing const umode_t parameter omits the parameter name.

Because the kernel builds with -std=gnu11 and supports GCC 8.1 and later, while unnamed parameters in function definitions are a C23 feature only supported starting in GCC 11, won't this fail to compile on GCC 8 through 10 with a "parameter name omitted" error?

Quoted 6 lines
security/landlock/fs.c +6 -0
+{
+	struct ipc_namespace *ipc_ns;
+	struct vfsmount *mnt;
+	struct path parent;
+
+	if (!landlock_is_posix_mqueue_fs(dir))

security/landlock/fs.c static int hook_path_truncate(const struct path *const path) · line 1702

Quoted 10 lines
security/landlock/fs.c +10 -0
+		return 0;
+
+	if (!current->nsproxy)
+		return 0;
+	ipc_ns = current->nsproxy->ipc_ns;
+	if (!ipc_ns)
+		return 0;
+	mnt = ipc_ns->mq_mnt;
+	if (WARN_ON_ONCE(!mnt || mnt->mnt_sb != dir->i_sb))
+		return 0;

[Severity: High]
Can an unprivileged user trigger this WARN_ON_ONCE() in hook_inode_create() (and bypass the check) by creating a file on a mounted mqueue filesystem from another IPC namespace?

Although mq_open(2) calls vfs_mkobj(), mqueue_dir_inode_operations in ipc/mqueue.c also defines .create = mqueue_create:

static const struct inode_operations mqueue_dir_inode_operations = {
.lookup = simple_lookup,
.create = mqueue_create,
.unlink = mqueue_unlink,
};

This means VFS creation paths on a mounted mqueue filesystem also reach security_inode_create() -> hook_inode_create():

    openat(O_CREAT) -> do_filp_open() -> path_openat() -> lookup_open()
      -> may_o_create() -> security_inode_create()

    mknodat(S_IFREG) -> filename_mknodat() -> vfs_create()
      -> security_inode_create()

If a process unshares into a new IPC namespace (for example via unshare(CLONE_NEWUSER | CLONE_NEWIPC)) while retaining access to a mounted mqueue filesystem from a previous IPC namespace (such as /dev/mqueue or an open directory fd), dir->i_sb belongs to the mount's IPC namespace whereas current->nsproxy->ipc_ns->mq_mnt->mnt_sb belongs to the new IPC namespace. Calling open() with O_CREAT or mknod() with S_IFREG on that mount will then cause mnt->mnt_sb != dir->i_sb and fire WARN_ON_ONCE().

security/landlock/fs.c static int hook_path_truncate(const struct path *const path) · line 1707

Quoted 5 lines
security/landlock/fs.c +5 -0
+
+	parent.mnt = mnt;
+	parent.dentry = dentry->d_parent;
+	return current_check_access_path(&parent, LANDLOCK_ACCESS_FS_MAKE_REG);
+}

[Severity: High]
When a Landlock-restricted task creates a queue via open(O_CREAT) or mknod(S_IFREG) on a mounted mqueue filesystem (such as /dev/mqueue), does constructing parent with ipc_ns->mq_mnt in hook_inode_create() break rules attached to ancestor directories like /dev or /?

During open(O_CREAT) or mknod(S_IFREG) on /dev/mqueue/q, the VFS first checks security_path_mknod() with the real user mount path, which walks up across the /dev/mqueue mountpoint to /dev and / via follow_up() in is_access_to_paths_allowed():

    jump_up:
        if (walker_path.dentry == walker_path.mnt->mnt_root) {
            if (follow_up(&walker_path)) {
                /* Ignores hidden mount points. */
                goto jump_up;
            } else {
                break;
            }
        }

However, may_o_create() and vfs_create() then call security_inode_create() -> hook_inode_create(), which replaces the mount with the internal kernel mount ipc_ns->mq_mnt. Because ipc_ns->mq_mnt is unattached, follow_up() returns false at mnt_root and stops the walk before reaching /dev or /, causing current_check_access_path() to deny the creation with -EACCES unless a rule was added directly on /dev/mqueue itself.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH v2 3/4] Landlock: Add POSIX message queue scoping

In reply to Oxana Kharitonova
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] landlock: premature creator domain clearing in hook_inode_unlink() - [Medium] landlock: ignored…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] landlock: premature creator domain clearing in hook_inode_unlink()
- [Medium] landlock: ignored allocation failure in hook_d_instantiate() leaves queue untagged
- [Medium] selftests/landlock: missing ENOSYS skip checks when CONFIG_POSIX_MQUEUE=n
- [Low] landlock: build failure in landlock_check_posix_mqueue_open() without tracepoints
- [Low] Documentation: landlock: section title underline too short in landlock.rst --

commit 672099a186033eeb6307163bb1baca6370e0b662
Author: Oxana Kharitonova <[email protected]>

Landlock: Add POSIX message queue scoping

This commit introduces Posix message queue scoping in Landlock (ABI 13) to restrict opening queues to those created within the scoping domain or its descendants. It also adds audit logging, a denial trace event, documentation, and selftests.

Quoted 4 lines
Documentation/userspace-api/landlock.rst +0 -0
index 1fb91884cb8af..fa65fa4aad02e 100644

[ ... ]

Documentation/userspace-api/landlock.rst fd-reference acquisition (:manpage:`open_tree(2)`, :manpage:`fsmount(2)`) with · line 1125

Quoted 6 lines
Documentation/userspace-api/landlock.rst +2 -0
@@ -1075,6 +1121,14 @@ fd-reference acquisition (:manpage:`open_tree(2)`, :manpage:`fsmount(2)`) with
 the new ``LANDLOCK_PERMISSION_NAMESPACE_USE`` permission flag and
 ``LANDLOCK_RULE_NAMESPACE`` rule type.
 
+POSIX message queue scoping (ABI < 13)
+-------------------------------------

[Severity: Low]
This isn't a bug, but the hyphen underline here is 37 characters while the heading above it is 38 characters. Should an extra hyphen be added so htmldocs builds do not emit a title underline warning?

Quoted 28 lines
security/landlock/fs.c +13 -0
index d5dcb19d5eb19..e9c52332dfcde 100644
@@ -53,6 +53,7 @@
 #include "object.h"
 #include "ruleset.h"
 #include "setup.h"
+#include "task.h"
 
 #include <trace/events/landlock.h>
 
@@ -1374,6 +1375,80 @@ static void hook_inode_free_security_rcu(void *inode_security)
 	WARN_ON_ONCE(inode_sec->object);
 }
 
+/*
+ * Tag a newly created POSIX message queue with its creator's domain.
+ *
+ * This is the earliest reachable point with both the creator's context and a
+ * fully initialized inode.
+ */
+static void hook_d_instantiate(struct dentry *const dentry,
+			       struct inode *const inode)
+{
+	struct landlock_domain *dom;
+	struct landlock_object *object;
+

security/landlock/fs.c static void hook_inode_free_security_rcu(void *inode_security) · line 1399

Quoted 10 lines
security/landlock/fs.c +10 -0
+	if (!landlock_is_posix_mqueue_inode(inode))
+		return;
+
+	dom = landlock_get_current_domain();
+	if (!dom)
+		return;
+
+	object = get_inode_object(inode);
+	if (IS_ERR(object))
+		return;

[Severity: Medium]
What happens if get_inode_object(inode) fails to allocate memory in hook_d_instantiate() when a task in a scoped domain creates a queue via mq_open()?

Because security_d_instantiate() is a void hook called from:

mqueue_create_attr()
d_make_persistent()
security_d_instantiate()
hook_d_instantiate()

the allocation failure is ignored and the new queue is linked into mqueuefs with no creator domain attached. Immediately afterwards, mqueue_file_open() calls dentry_open() -> hook_file_open() -> landlock_check_posix_mqueue_open(), which sees creator_domain == NULL, treats the queue as created outside any Landlock domain, and denies the creator's own mq_open() with EPERM while leaving the untagged queue behind in the namespace.

Could the object allocation or tagging be performed in a fallible creation hook (or preallocated before d_instantiate) so that allocation failure aborts queue creation cleanly instead of leaving an untagged queue?

Quoted 41 lines
security/landlock/fs.c +41 -0
+
+	spin_lock(&object->lock);
+	if (WARN_ON_ONCE(object->domain)) {
+		spin_unlock(&object->lock);
+		landlock_put_object(object);
+		return;
+	}
+
+	landlock_get_domain(dom);
+	object->domain = dom;
+	spin_unlock(&object->lock);
+}
+
+/*
+ * Drop the reference retained by hook_d_instantiate() for a POSIX message
+ * queue once its sole name is unlinked.
+ */
+static int hook_inode_unlink(struct inode *const dir,
+			     struct dentry *const dentry)
+{
+	const struct inode *const inode = d_backing_inode(dentry);
+	struct landlock_domain *domain;
+	struct landlock_object *object;
+
+	if (!landlock_is_posix_mqueue_inode(inode))
+		return 0;
+
+	rcu_read_lock();
+	object = rcu_dereference(landlock_inode(inode)->object);
+	/* The temporary reference protects against concurrent object release. */
+	if (!object || !refcount_inc_not_zero(&object->usage)) {
+		rcu_read_unlock();
+		return 0;
+	}
+	rcu_read_unlock();
+
+	spin_lock(&object->lock);
+	domain = object->domain;
+	object->domain = NULL;
+	spin_unlock(&object->lock);
+

security/landlock/fs.c static void hook_inode_free_security_rcu(void *inode_security) · line 1450

Quoted 10 lines
security/landlock/fs.c +10 -0
+	/* Drop the temporary reference acquired by refcount_inc_not_zero(). */
+	landlock_put_object(object);
+	if (domain) {
+		landlock_put_domain_deferred(domain);
+		/* Drops the reference retained by hook_d_instantiate(). */
+		landlock_put_object(object);
+	}
+
+	return 0;
+}

[Severity: High]
Can clearing object->domain inside hook_inode_unlink() strip the creator domain from a queue that remains linked or open?

In fs/namei.c:vfs_unlink(), security_inode_unlink() is a pre-unlink permission check:

    vfs_unlink()
      security_inode_unlink(dir, dentry)
      try_break_deleg(dir, ...)
      try_break_deleg(target, ...)
      dir->i_op->unlink(dir, dentry)

If Landlock runs before another stacked security module (such as SELinux, AppArmor, Smack, or BPF LSM in the default lsm order) and that subsequent module denies the unlink, or if try_break_deleg() fails, hook_inode_unlink() has already cleared object->domain = NULL and dropped the retained references, even though vfs_unlink() aborts and the queue remains linked in mqueuefs.

Similarly, even when vfs_unlink() succeeds, the queue inode stays alive while open file descriptors exist and can be reopened by tasks in the creator's domain via /proc/<pid>/fd/<n>.

Because landlock_check_posix_mqueue_open() treats creator_domain == NULL as an unsandboxed creator, any subsequent open of that queue by the creator's own scoped domain (or a scoped ancestor domain) will be denied with EPERM. Should the creator domain stay attached for the lifetime of the inode rather than being cleared in hook_inode_unlink()?

Quoted 22 lines
security/landlock/task.c +14 -0
index d9eae86fc5529..0daa7edff2d7e 100644
@@ -522,6 +522,72 @@ static int hook_file_send_sigiotask(struct task_struct *tsk,
 	return -EPERM;
 }
 
+static const struct access_masks posix_mqueue_scope = {
+	.scope = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+};
+
+/**
+ * landlock_check_posix_mqueue_open - Deny opening a POSIX message queue
+ * created by a task from a different (non-ancestor) domain
+ *
+ * @file: The mqueuefs file being opened.
+ *
+ * Return: -EPERM if the open must be denied, 0 otherwise.
+ */
+int landlock_check_posix_mqueue_open(struct file *const file)
+{

[ ... ]

security/landlock/task.c static int hook_file_send_sigiotask(struct task_struct *tsk, · line 585

Quoted 9 lines
security/landlock/task.c +9 -0
+	landlock_log_denial(subject, &(struct landlock_request) {
+		.type = LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE,
+		.other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,
+		.audit = {
+			.type = LSM_AUDIT_DATA_FILE,
+			.u.file = file,
+		},
+		.layer_plus_one = handle_layer + 1,
+	});

[Severity: Low]
Does security/landlock/task.c build when tracepoints are disabled?

In security/landlock/log.h, other_domain_id in struct landlock_request is guarded by:

    #ifdef CONFIG_TRACEPOINTS
    	union {
    		u64 other_domain_id;
    ...

and in security/landlock/domain.h, creator_domain->hierarchy->id is guarded by #ifdef CONFIG_SECURITY_LANDLOCK_LOG. Because landlock_log_denial() is a static inline function when logging is disabled, the compound literal is still type-checked by the compiler. Should the .other_domain_id initializer here be wrapped in #ifdef CONFIG_TRACEPOINTS like in hook_unix_stream_connect() and hook_unix_may_send()?

Quoted 4 lines
tools/testing/selftests/landlock/audit_test.c +0 -0
index 4bea8c880a4d3..a15c1e5d277f3 100644

[ ... ]

Quoted 29 lines
tools/testing/selftests/landlock/audit_test.c +29 -0
+/* Checks audit logging and quieting of a denied POSIX message queue open. */
+TEST_F(posix_mqueue_audit, open)
+{
+	const struct landlock_ruleset_attr ruleset_attr = {
+		.scoped = LANDLOCK_SCOPE_POSIX_MSG_QUEUE,
+		.quiet_scoped = variant->quiet_scoped,
+	};
+	char mq_name[NAME_MAX], log_match[PATH_MAX];
+	int pipe_parent[2], pipe_child[2], ruleset_fd, status;
+	struct audit_records records;
+	char buf;
+	pid_t child;
+	mqd_t mq;
+
+	ASSERT_LT(0,
+		  snprintf(mq_name, sizeof(mq_name),
+			   "/selftests-landlock-audit-mq-tid%d", sys_gettid()));
+	mq_unlink(mq_name);
+	ASSERT_EQ(0, pipe2(pipe_parent, O_CLOEXEC));
+	ASSERT_EQ(0, pipe2(pipe_child, O_CLOEXEC));
+
+	child = fork();
+	ASSERT_LE(0, child);
+	if (child == 0) {
+		EXPECT_EQ(0, close(pipe_parent[1]));
+		EXPECT_EQ(0, close(pipe_child[0]));
+
+		mq = mq_open(mq_name, O_CREAT | O_EXCL | O_RDWR, 0600, NULL);
+		ASSERT_LE(0, mq);

[ ... ]

Quoted 5 lines
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +0 -0
new file mode 100644
index 0000000000000..4475746081bc3

[ ... ]

tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c line 105

Quoted 5 lines
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +5 -0
+	if (variant->domain_parent)
+		create_scoped_domain(_metadata, LANDLOCK_SCOPE_POSIX_MSG_QUEUE);
+
+	mq = mq_open(self->mq_name, O_CREAT | O_RDWR, 0600, NULL);
+	ASSERT_LE(0, mq);

[Severity: Medium]
In tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c, the later tests check whether mq_open() failed with errno == ENOSYS and call SKIP() when Posix message queue support is not enabled in the kernel, but the first eight tests (open_parent_queue, open_child_queue, open_existing_queue_with_create, unlink_and_recreate_in_sibling_domain, open_queue_after_creator_exits, open_queue_from_child_after_creator_exits, posix_mqueue_ipc_namespace_lifetime, and create_and_open_same_domain) along with TEST_F(posix_mqueue_audit, open) in tools/testing/selftests/landlock/audit_test.c assert that mq_open() succeeds directly.

Should those tests also skip when mq_open() fails with ENOSYS so they do not fail on kernels built without Posix message queue support?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#12 lore raw

Re: [PATCH v2 4/4] samples/landlock: Add POSIX message queue scoping to sandboxer

In reply to Oxana Kharitonova
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] landlock: user-triggerable WARN_ON_ONCE and wrong mount in hook_inode_create() - [Medium]…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] landlock: user-triggerable WARN_ON_ONCE and wrong mount in hook_inode_create()
- [Medium] samples/landlock: commit message example with LL_FS_RW="" denies mq_open()
- [Medium] landlock: hook_inode_unlink() clears object->domain before unlink succeeds
- [Low] landlock: omitted parameter name in hook_inode_create() breaks GCC < 11 build --

commit fb37ae1137756d3c9de1fe75208b0f3f51d1b11c
Author: Oxana Kharitonova <[email protected]>

samples/landlock: Add POSIX message queue scoping to sandboxer

This series adds LANDLOCK_ACCESS_FS_MAKE_REG enforcement for POSIX message queue creation, introduces LANDLOCK_SCOPE_POSIX_MSG_QUEUE in ABI 13, and updates the Landlock sample sandboxer to support the new scope and audit quieting option.

Quoted 9 lines

samples/landlock: Add POSIX message queue scoping to sandboxer

Add "q" to LL_SCOPED to enable LANDLOCK_SCOPE_POSIX_MSG_QUEUE and "posix_msg_queue" to LL_QUIET_ACCESS to quiet related audit denials. Update the supported ABI version to 13 and disable the new scope when running on older kernels.

For example, on a kernel supporting Landlock ABI 13:

Quoted 8 lines

LL_FS_RO="/" LL_FS_RW="" LL_SCOPED="q" \
./samples/landlock/sandboxer /bin/sh

The resulting shell and its children cannot open POSIX message queues created outside their Landlock domain. They can create queues and open queues created within that domain.

[Severity: Medium]
Does the example command in the commit message actually allow the shell and its children to create or open POSIX message queues?

In samples/landlock/sandboxer.c:main(), ruleset_attr.handled_access_fs is unconditionally initialized to access_fs_rw:

	__u64 access_fs_ro = ACCESS_FS_ROUGHLY_READ,
	      access_fs_rw = ACCESS_FS_ROUGHLY_READ | ACCESS_FS_ROUGHLY_WRITE;

	struct landlock_ruleset_attr ruleset_attr = {
		.handled_access_fs = access_fs_rw,

When LL_FS_RW="" is passed, populate_ruleset_fs() returns early without adding any read-write rules:

	if (num_paths == 1 && path_list[0][0] == '\0') {
		/*
		 * Allows to not use all possible restrictions (e.g. use
		 * LL_FS_RO without LL_FS_RW).
		 */
		ret = 0;
		goto out_free_name;
	}

This leaves LANDLOCK_ACCESS_FS_MAKE_REG and LANDLOCK_ACCESS_FS_WRITE_FILE handled and denied everywhere. Creating a queue via mq_open() with O_CREAT invokes hook_inode_create(), which checks LANDLOCK_ACCESS_FS_MAKE_REG on ipc_ns->mq_mnt and denies creation with EACCES.

In addition, do_mq_open() and hook_inode_create() operate on current->nsproxy->ipc_ns->mq_mnt, which is an internal SB_KERNMOUNT mount where path traversal in is_access_to_paths_allowed() stops at mq_mnt->mnt_root (follow_up() returns false) and never reaches "/". Because of that, LL_FS_RO="/" does not grant LANDLOCK_ACCESS_FS_READ_FILE to mq_open() either; wouldn't the mqueue filesystem for the current IPC namespace (such as /dev/mqueue) need to be mounted and included in LL_FS_RW for this example to work as described?

Quoted 4 lines
security/landlock/fs.c +0 -0
index 2ab67bcad1983..e9c52332dfcde 100644

[ ... ]

Quoted 28 lines
security/landlock/fs.c +28 -0
+/*
+ * Drop the reference retained by hook_d_instantiate() for a POSIX message
+ * queue once its sole name is unlinked.
+ */
+static int hook_inode_unlink(struct inode *const dir,
+			     struct dentry *const dentry)
+{
+	const struct inode *const inode = d_backing_inode(dentry);
+	struct landlock_domain *domain;
+	struct landlock_object *object;
+
+	if (!landlock_is_posix_mqueue_inode(inode))
+		return 0;
+
+	rcu_read_lock();
+	object = rcu_dereference(landlock_inode(inode)->object);
+	/* The temporary reference protects against concurrent object release. */
+	if (!object || !refcount_inc_not_zero(&object->usage)) {
+		rcu_read_unlock();
+		return 0;
+	}
+	rcu_read_unlock();
+
+	spin_lock(&object->lock);
+	domain = object->domain;
+	object->domain = NULL;
+	spin_unlock(&object->lock);
+

security/landlock/fs.c

Quoted 10 lines
security/landlock/fs.c +10 -0
+	/* Drop the temporary reference acquired by refcount_inc_not_zero(). */
+	landlock_put_object(object);
+	if (domain) {
+		landlock_put_domain_deferred(domain);
+		/* Drops the reference retained by hook_d_instantiate(). */
+		landlock_put_object(object);
+	}
+
+	return 0;
+}

[Severity: Medium]
Can clearing object->domain in hook_inode_unlink() strip the creator domain from a queue before the unlink actually succeeds, or while the queue inode is still alive?

In vfs_unlink(), security_inode_unlink(dir, dentry) is a pre-operation permission hook invoked before try_break_deleg() and dir->i_op->unlink(). Because Landlock is ordered before SELinux, Smack, AppArmor, and BPF LSM in CONFIG_LSM, hook_inode_unlink() runs first, sets object->domain = NULL, and drops the domain and object references retained by hook_d_instantiate().

If a subsequent stacked LSM hook or delegation break fails, vfs_unlink() aborts and the queue remains linked in mqueuefs with object->domain == NULL. Any later mq_open() or open() from the creator's domain with LANDLOCK_SCOPE_POSIX_MSG_QUEUE enabled will see creator_domain == NULL in landlock_check_posix_mqueue_open() and fail with -EPERM. The same -EPERM also occurs if an unlinked queue with an open file descriptor is reopened by the creator's domain via /proc/self/fd/<fd>.

Should the domain reference instead be released when the underlying inode is evicted or released rather than inside the pre-unlink permission hook?

[ ... ]

security/landlock/fs.c

Quoted 7 lines
security/landlock/fs.c +7 -0
+/*
+ * Unlike regular file creation, mq_open(2) calls vfs_mkobj() directly and
+ * therefore does not trigger the path_mknod hook.  Use the mqueuefs mount
+ * associated with the current IPC namespace to recover the parent path.
+ */
+static int hook_inode_create(struct inode *const dir,
+			     struct dentry *const dentry, const umode_t)

[Severity: Low]
Does omitting the parameter name for const umode_t in hook_inode_create() break builds with older supported GCC versions?

The kernel compiles C sources with -std=gnu11 and supports GCC >= 8.1.0, whereas unnamed parameters in function definitions are a C23 feature only supported by GCC 11 and later (older versions fail with "error: parameter name omitted").

Quoted 11 lines
security/landlock/fs.c +11 -0
+{
+	struct ipc_namespace *ipc_ns;
+	struct vfsmount *mnt;
+	struct path parent;
+
+	if (!landlock_is_posix_mqueue_fs(dir))
+		return 0;
+
+	if (!current->nsproxy)
+		return 0;
+	ipc_ns = current->nsproxy->ipc_ns;

security/landlock/fs.c

Quoted 10 lines
security/landlock/fs.c +10 -0
+	if (!ipc_ns)
+		return 0;
+	mnt = ipc_ns->mq_mnt;
+	if (WARN_ON_ONCE(!mnt || mnt->mnt_sb != dir->i_sb))
+		return 0;
+
+	parent.mnt = mnt;
+	parent.dentry = dentry->d_parent;
+	return current_check_access_path(&parent, LANDLOCK_ACCESS_FS_MAKE_REG);
+}

[Severity: High]
Can hook_inode_create() also be reached via VFS path-based creation on a mounted mqueue filesystem, triggering the WARN_ON_ONCE() from unprivileged userspace or checking the wrong mount?

In ipc/mqueue.c, mqueue_dir_inode_operations sets .create = mqueue_create, so calling open(..., O_CREAT, ...) or mknod(..., S_IFREG, ...) on a mounted mqueue filesystem (such as /dev/mqueue, which has mode 01777) calls security_path_mknod() followed by security_inode_create() -> hook_inode_create().

This appears to cause two regressions:

1. A mounted mqueue superblock belongs to the ipc_namespace active at mount time (sb->s_fs_info), which need not match current->nsproxy->ipc_ns. An unprivileged user can call unshare(CLONE_NEWUSER | CLONE_NEWIPC) while keeping access to the previous IPC namespace's /dev/mqueue mount and call open("/dev/mqueue/foo", O_CREAT, 0600). In hook_inode_create(), current->nsproxy->ipc_ns->mq_mnt->mnt_sb != dir->i_sb, triggering the WARN_ON_ONCE() and returning 0 without enforcing LANDLOCK_ACCESS_FS_MAKE_REG.

2. Even within the same IPC namespace, if a sandboxed process creates a queue via open("/dev/mqueue/foo", O_CREAT, 0600) or mknod() and its ruleset grants LANDLOCK_ACCESS_FS_MAKE_REG on an ancestor directory such as "/" or "/dev" rather than directly on the /dev/mqueue root inode, hook_path_mknod() succeeds by walking up across the mountpoint via follow_up(). However, hook_inode_create() then checks parent.mnt = ipc_ns->mq_mnt (an internal SB_KERNMOUNT mount where follow_up() stops at the root) and denies the creation with -EACCES.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw

Re: [PATCH v2 2/4] Landlock: Enforce MAKE_REG for POSIX message queue creation

In reply to Oxana Kharitonova
kernel test robot noticed the following build warnings:

Hi Oxana,

kernel test robot noticed the following build warnings:

[auto build test WARNING on shuah-kselftest/next]
[also build test WARNING on linus/master next-20261030]
[cannot apply to shuah-kselftest/fixes trace/for-next brauner-vfs/vfs.all v7.3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oxana-Kharitonova/ipc-Move-MQUEUE_MAGIC-to-a-shared-header/20261102-144557
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link:    https://lore.kernel.org/r/20261102144708.920232-3-webprosto%40gmail.com
patch subject: [PATCH v2 2/4] Landlock: Enforce MAKE_REG for POSIX message queue creation
config: s390-defconfig (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> security/landlock/fs.c:1686:51: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
    1686 |                              struct dentry *const dentry, const umode_t)
         |                                                                        ^
   security/landlock/fs.c:2117:30: error: incompatible function pointer types initializing 'int (*)(const struct mnt_idmap *, struct inode *, struct dentry *, umode_t)' (aka 'int (*)(const struct mnt_idmap *, struct inode *, struct dentry *, unsigned short)') with an expression of type 'int (struct inode *const, struct dentry *const, const umode_t)' (aka 'int (struct inode *const, struct dentry *const, const unsigned short)') [-Wincompatible-function-pointer-types]
    2117 |         LSM_HOOK_INIT(inode_create, hook_inode_create),
         |                                     ^~~~~~~~~~~~~~~~~
   include/linux/lsm_hooks.h:141:21: note: expanded from macro 'LSM_HOOK_INIT'
     141 |                 .hook = { .NAME = HOOK }                \
         |                                   ^~~~

1 warning and 1 error generated.

vim +1686 security/landlock/fs.c

1679
1680 /*
1681 * Unlike regular file creation, mq_open(2) calls vfs_mkobj() directly and
1682 * therefore does not trigger the path_mknod hook. Use the mqueuefs mount
1683 * associated with the current IPC namespace to recover the parent path.
1684 */
1685 static int hook_inode_create(struct inode *const dir,

> 1686				     struct dentry *const dentry, const umode_t)

1687 {
1688 struct ipc_namespace *ipc_ns;
1689 struct vfsmount *mnt;
1690 struct path parent;
1691
1692 if (!landlock_is_posix_mqueue_fs(dir))
1693 return 0;
1694
1695 if (!current->nsproxy)
1696 return 0;
1697 ipc_ns = current->nsproxy->ipc_ns;
1698 if (!ipc_ns)
1699 return 0;
1700 mnt = ipc_ns->mq_mnt;
1701 if (WARN_ON_ONCE(!mnt || mnt->mnt_sb != dir->i_sb))
1702 return 0;
1703
1704 parent.mnt = mnt;
1705 parent.dentry = dentry->d_parent;
1706 return current_check_access_path(&parent, LANDLOCK_ACCESS_FS_MAKE_REG);
1707 }
1708

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#9 lore raw

Re: [PATCH v2 3/4] Landlock: Add POSIX message queue scoping

In reply to Oxana Kharitonova
kernel test robot noticed the following build errors:

Hi Oxana,

kernel test robot noticed the following build errors:

[auto build test ERROR on shuah-kselftest/next]
[also build test ERROR on linus/master next-20261030]
[cannot apply to shuah-kselftest/fixes trace/for-next brauner-vfs/vfs.all v7.3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oxana-Kharitonova/ipc-Move-MQUEUE_MAGIC-to-a-shared-header/20261102-144557
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link:    https://lore.kernel.org/r/20261102144708.920232-4-webprosto%40gmail.com
patch subject: [PATCH v2 3/4] Landlock: Add POSIX message queue scoping
config: nios2-allmodconfig (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

security/landlock/task.c: In function 'landlock_check_posix_mqueue_open':

>> security/landlock/task.c:579:18: error: 'struct landlock_request' has no member named 'other_domain_id'
     579 |                 .other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,
         |                  ^~~~~~~~~~~~~~~

vim +579 security/landlock/task.c

528
529 /**
530 * landlock_check_posix_mqueue_open - Deny opening a POSIX message queue
531 * created by a task from a different (non-ancestor) domain
532 *
533 * @file: The mqueuefs file being opened.
534 *
535 * Return: -EPERM if the open must be denied, 0 otherwise.
536 */
537 int landlock_check_posix_mqueue_open(struct file *const file)
538 {
539 const struct inode *const inode = file_inode(file);
540 const struct landlock_cred_security *subject;
541 struct landlock_object *object;
542 struct landlock_domain *creator_domain = NULL;
543 bool is_scoped;
544 size_t handle_layer;
545
546 if (!landlock_is_posix_mqueue_inode(inode))
547 return 0;
548
549 subject = landlock_get_applicable_subject(
550 file->f_cred, posix_mqueue_scope, &handle_layer);
551 if (!subject)
552 return 0;
553
554 rcu_read_lock();
555 object = rcu_dereference(landlock_inode(inode)->object);
556 /* The temporary reference protects against concurrent object release. */
557 if (object && !refcount_inc_not_zero(&object->usage))
558 object = NULL;
559 rcu_read_unlock();
560
561 if (object) {
562 spin_lock(&object->lock);
563 creator_domain = object->domain;
564 landlock_get_domain(creator_domain);
565 spin_unlock(&object->lock);
566 }
567
568 /* Untagged queues were created outside any Landlock domain. */
569 is_scoped = domain_is_scoped(subject->domain, creator_domain,
570 LANDLOCK_SCOPE_POSIX_MSG_QUEUE);
571 if (!is_scoped) {
572 landlock_put_domain_deferred(creator_domain);
573 landlock_put_object(object);
574 return 0;
575 }
576
577 landlock_log_denial(subject, &(struct landlock_request) {
578 .type = LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE,

 > 579			.other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,

580 .audit = {
581 .type = LSM_AUDIT_DATA_FILE,
582 .u.file = file,
583 },
584 .layer_plus_one = handle_layer + 1,
585 });
586 landlock_put_domain_deferred(creator_domain);
587 landlock_put_object(object);
588 return -EPERM;
589 }
590

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#10 lore raw

Re: [PATCH v2 3/4] Landlock: Add POSIX message queue scoping

In reply to Oxana Kharitonova
kernel test robot noticed the following build errors:

Hi Oxana,

kernel test robot noticed the following build errors:

[auto build test ERROR on shuah-kselftest/next]
[also build test ERROR on linus/master next-20261030]
[cannot apply to shuah-kselftest/fixes trace/for-next brauner-vfs/vfs.all v7.3] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oxana-Kharitonova/ipc-Move-MQUEUE_MAGIC-to-a-shared-header/20261102-144557
base:   https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
patch link:    https://lore.kernel.org/r/20261102144708.920232-4-webprosto%40gmail.com
patch subject: [PATCH v2 3/4] Landlock: Add POSIX message queue scoping
config: nios2-randconfig-1000-20261103 (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

security/landlock/task.c: In function 'landlock_check_posix_mqueue_open':
security/landlock/task.c:579:18: error: 'struct landlock_request' has no member named 'other_domain_id'

     579 |                 .other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,
         |                  ^~~~~~~~~~~~~~~
>> security/landlock/task.c:579:78: error: 'struct landlock_hierarchy' has no member named 'id'
     579 |                 .other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,
         |                                                                              ^~

vim +579 security/landlock/task.c

528
529 /**
530 * landlock_check_posix_mqueue_open - Deny opening a POSIX message queue
531 * created by a task from a different (non-ancestor) domain
532 *
533 * @file: The mqueuefs file being opened.
534 *
535 * Return: -EPERM if the open must be denied, 0 otherwise.
536 */
537 int landlock_check_posix_mqueue_open(struct file *const file)
538 {
539 const struct inode *const inode = file_inode(file);
540 const struct landlock_cred_security *subject;
541 struct landlock_object *object;
542 struct landlock_domain *creator_domain = NULL;
543 bool is_scoped;
544 size_t handle_layer;
545
546 if (!landlock_is_posix_mqueue_inode(inode))
547 return 0;
548
549 subject = landlock_get_applicable_subject(
550 file->f_cred, posix_mqueue_scope, &handle_layer);
551 if (!subject)
552 return 0;
553
554 rcu_read_lock();
555 object = rcu_dereference(landlock_inode(inode)->object);
556 /* The temporary reference protects against concurrent object release. */
557 if (object && !refcount_inc_not_zero(&object->usage))
558 object = NULL;
559 rcu_read_unlock();
560
561 if (object) {
562 spin_lock(&object->lock);
563 creator_domain = object->domain;
564 landlock_get_domain(creator_domain);
565 spin_unlock(&object->lock);
566 }
567
568 /* Untagged queues were created outside any Landlock domain. */
569 is_scoped = domain_is_scoped(subject->domain, creator_domain,
570 LANDLOCK_SCOPE_POSIX_MSG_QUEUE);
571 if (!is_scoped) {
572 landlock_put_domain_deferred(creator_domain);
573 landlock_put_object(object);
574 return 0;
575 }
576
577 landlock_log_denial(subject, &(struct landlock_request) {
578 .type = LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE,

 > 579			.other_domain_id = creator_domain ? creator_domain->hierarchy->id : 0,

580 .audit = {
581 .type = LSM_AUDIT_DATA_FILE,
582 .u.file = file,
583 },
584 .layer_plus_one = handle_layer + 1,
585 });
586 landlock_put_domain_deferred(creator_domain);
587 landlock_put_object(object);
588 return -EPERM;
589 }
590

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#3 lore raw

Re: [PATCH v2 1/4] ipc: Move MQUEUE_MAGIC to a shared header

In reply to Oxana Kharitonova
Quoted 15 lines · Oxana Kharitonova

Move MQUEUE_MAGIC from ipc/mqueue.c to include/linux/ipc_namespace.h so it can be shared by code outside of ipc/mqueue.c without duplicating the constant.

---
 include/linux/ipc_namespace.h | 2 ++
 ipc/mqueue.c                  | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/ipc_namespace.h +0 -0
index 12faca29bbb9..8a15ffe38e9a 100644

include/linux/ipc_namespace.h line 18

Quoted 4 lines
include/linux/ipc_namespace.h +1 -0
@@ -15,6 +15,8 @@
 
 struct user_namespace;
 
+#define MQUEUE_MAGIC 0x19800202

Should go into include/uapi/linux/magic.h ?