landlock: Add POSIX message queue scoping
Subsystems (4)
cover landlock: Add POSIX message queue scoping
Hi,
This series adds landlock support for scoping POSIX message queuesi [1].
Landlock already supports scoped IPC restrictions for signals and abstract UNIX sockets. These restrictions make it possible to prevent a sandboxed task from interacting with IPC objects outside of its Landlock domain, while still allowing communication within the same domain or with nested domains.
This series extends the same model to POSIX message queues with a new LANDLOCK_SCOPE_POSIX_MSG_QUEUE scope. When this scope is enforced, a task can only open POSIX message queues that were created by a task in the same landlock domain or in a nested domain.
The implementation tags mqueuefs inodes at creation time with the creator's landlock domain. This domain is kept alive for the lifetime of the inode and is checked when the queue is opened.
The series also exposes the mqueuefs magic number through the shared UAPI magic header, bumps the Landlock ABI, updates documentation, adds sandboxer support, and adds selftests.
The new behavior is:
- a task restricted with LANDLOCK_SCOPE_POSIX_MSG_QUEUE cannot open a queue created outside of its Landlock scope;
- a task can still open a queue created within its own Landlock domain;
- queues created outside of any Landlock domain are treated as outside the scope for a scoped opener.
[1] https://man7.org/linux/man-pages/man7/mq_overview.7.html
Oxana Kharitonova (6):
ipc: Move mqueue fs magic to uapi magic header
landlock: Scope POSIX message queue opens
landlock: Bump ABI for LANDLOCK_SCOPE_POSIX_MSG_QUEUE selftests/landlock: Test POSIX message queue scoping samples/landlock: Support POSIX message queue scoping
landlock: Document POSIX message queue scoping
Documentation/admin-guide/LSM/landlock.rst | 6 +- Documentation/userspace-api/landlock.rst | 11 +- include/uapi/linux/landlock.h | 7 +- include/uapi/linux/magic.h | 2 + ipc/mqueue.c | 2 +- samples/landlock/sandboxer.c | 16 +- security/landlock/audit.c | 9 + security/landlock/audit.h | 1 + security/landlock/fs.c | 35 +++ security/landlock/fs.h | 15 ++ security/landlock/limits.h | 2 +- security/landlock/ruleset.c | 1 - security/landlock/syscalls.c | 2 +- security/landlock/task.c | 43 ++++ security/landlock/task.h | 4 + tools/testing/selftests/landlock/base_test.c | 2 +- .../landlock/scoped_posix_msg_queue_test.c | 223 ++++++++++++++++++ .../testing/selftests/landlock/scoped_test.c | 2 +- 18 files changed, 371 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 1/6 ipc: Move mqueue fs magic to uapi magic header
Move MQUEUE_MAGIC from ipc/mqueue.c to include/uapi/linux/magic.h so it can be shared by code outside of ipc/mqueue.c without duplicating the constant.
--- include/uapi/linux/magic.h | 2 ++ ipc/mqueue.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)
include/uapi/linux/magic.h +2 -0
@@ -78,6 +78,8 @@#define V9FS_MAGIC 0x01021997 +#define MQUEUE_MAGIC 0x19800202 +#define BDEVFS_MAGIC 0x62646576#define DAXFS_MAGIC 0x64646178#define BINFMTFS_MAGIC 0x42494e4d
ipc/mqueue.c +1 -1
@@ -22,6 +22,7 @@#include <linux/sysctl.h> #include <linux/poll.h> #include <linux/mqueue.h> +#include <linux/magic.h> #include <linux/msg.h> #include <linux/skbuff.h> #include <linux/vmalloc.h>
@@ -47,7 +48,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.50.1 (Apple Git-155)
Günther Noack
reviewerRe: [PATCH 1/6] ipc: Move mqueue fs magic to uapi magic header
Hello!
Quoted 46 lines · Oxana Kharitonova
Move
MQUEUE_MAGICfromipc/mqueue.ctoinclude/uapi/linux/magic.hso it can be shared by code outside ofipc/mqueue.cwithout duplicating the constant.Signed-off-by Oxana Kharitonova <[email protected]>--- include/uapi/linux/magic.h | 2 ++ ipc/mqueue.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-)include/uapi/linux/magic.h +2
-0@@ -78,6 +78,8 @@#define V9FS_MAGIC 0x01021997 +#define MQUEUE_MAGIC 0x19800202 +#define BDEVFS_MAGIC 0x62646576#define DAXFS_MAGIC 0x64646178#define BINFMTFS_MAGIC 0x42494e4dipc/mqueue.c +1
-1@@ -22,6 +22,7 @@#include <linux/sysctl.h> #include <linux/poll.h> #include <linux/mqueue.h> +#include <linux/magic.h> #include <linux/msg.h> #include <linux/skbuff.h> #include <linux/vmalloc.h> @@ -47,7 +48,6 @@ struct mqueue_fs_context {bool newns;/* Set if newly created ipc namespace */ }; -#define MQUEUE_MAGIC 0x19800202#define DIRENT_SIZE 20#define FILENT_SIZE 80Signature · 1 line
2.50.1 (Apple Git-155)
The only reference to MQUEUE_MAGIC that I find in your patchset is from Landlock's kernel code -- For a value that gets serialized to disk, like for the other file systems, I think there is actually a legitimate use case for userspace to use that value, when dealing with raw images? But that's not the case here, I think?
Should this live in include/linux/ipc_namespace.h instead? Or even be exposed as a helper function that lets you check whether an inode is referring to an mqueue?
—Günther
Re: [PATCH 1/6] ipc: Move mqueue fs magic to uapi magic header
Quoted 6 lines · Günther Noack
Should this live in
include/linux/ipc_namespace.hinstead? Or even be exposed as a helper function that lets you check whether an inode is referring to an mqueue?—Günther
Hi Günther,
Thanks for the feedback. Agree, better to move it. I'll check how to do it better and include in v2.
PATCH 2/6 landlock: Scope POSIX message queue opens
In reply to Oxana KharitonovaAdd support for enforcing LANDLOCK_SCOPE_POSIX_MSG_QUEUE when opening POSIX message queues.
Tag mqueuefs inodes at instantiation time with the landlock domain of the task that created the queue. This domain is stored in the landlock inode security blob and kept alive until the inode security blob is released.
On file open, detect mqueuefs regular files and compare the queue creator's domain with the opener's scoped domain. Deny the open when the opener is restricted by LANDLOCK_SCOPE_POSIX_MSG_QUEUE and the queue was created outside of an allowed parent domain.
This makes POSIX message queues follow the same scoped-domain model as the existing landlock IPC restrictions, while keeping the queue creator domain tied to the lifetime of the queue inode.
--- include/uapi/linux/landlock.h | 1 + security/landlock/audit.c | 9 ++++++++ security/landlock/audit.h | 1 + security/landlock/fs.c | 35 ++++++++++++++++++++++++++++ security/landlock/fs.h | 15 ++++++++++++ security/landlock/limits.h | 2 +- security/landlock/ruleset.c | 1 - security/landlock/task.c | 43 +++++++++++++++++++++++++++++++++++ security/landlock/task.h | 4 ++++ 9 files changed, 109 insertions(+), 2 deletions(-)
include/uapi/linux/landlock.h +1 -0
@@ -491,6 +491,7 @@ struct landlock_net_port_attr {/* 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*/ #endif /* _UAPI_LINUX_LANDLOCK_H */
security/landlock/audit.c +9 -0
@@ -82,6 +82,10 @@ get_blocker(const enum landlock_request_type type,case LANDLOCK_REQUEST_SCOPE_SIGNAL :WARN_ON_ONCE (access_bit !=-1 );return "scope.signal" ; + +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : +WARN_ON_ONCE (access_bit !=-1 ); +return "scope.posix_msg_queue" ; }WARN_ON_ONCE (1 );
@@ -646,6 +650,11 @@ void landlock_log_denial(const struct landlock_cred_security *const subject, !!(quiet_mask &LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET );break ; +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : + quiet_applicable_to_access = + !!(quiet_mask & +LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); +break ;/* * Leave LANDLOCK_REQUEST_PTRACE and * LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY unhandled for now - they
security/landlock/audit.h +1 -0
@@ -21,6 +21,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, };/*
security/landlock/fs.c +35 -0
@@ -51,6 +51,7 @@#include "object.h" #include "ruleset.h" #include "setup.h" +#include "task.h" /* Underlying object management */
@@ -1268,6 +1269,33 @@ static void hook_inode_free_security_rcu(void *inode_security)*/ inode_sec = inode_security + landlock_blob_sizes.lbs_inode;WARN_ON_ONCE (inode_sec->object); + +landlock_put_ruleset_deferred (inode_sec->mq_domain); +} + +/* +* 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_ruleset *dom; + +if (!landlock_is_posix_mqueue_inode (inode)) +return ; + + dom =landlock_get_current_domain (); +if (!dom) +return ; + +if (WARN_ON_ONCE (landlock_inode (inode)->mq_domain)) +return ; + +landlock_get_ruleset (dom); +landlock_inode (inode)->mq_domain = dom; }/* Super-block hooks */
@@ -1756,6 +1784,12 @@ static int hook_file_open(struct file *const file)const struct landlock_cred_security *const subject =landlock_get_applicable_subject (file->f_cred, any_fs,NULL );struct landlock_request request = {}; +int err; + +/* POSIX message queue scoping is independent of FS access rights. */ + err =landlock_check_posix_mqueue_open (file); +if (err) +return err;if (!subject)return 0 ;
@@ -1979,6 +2013,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 (d_instantiate, hook_d_instantiate),LSM_HOOK_INIT (sb_delete, hook_sb_delete),LSM_HOOK_INIT (sb_mount, hook_sb_mount),
security/landlock/fs.h +15 -0
@@ -14,6 +14,7 @@#include <linux/fs.h> #include <linux/init.h> #include <linux/rcupdate.h> +#include <uapi/linux/magic.h> #include "access.h" #include "cred.h"
@@ -38,6 +39,14 @@ struct landlock_inode_security {* performed by get_inode_object(). */ struct landlock_object __rcu *object; +/** +* @mq_domain: Domain of the task that created POSIX message queue. +* Only set for mqueuefs inodes (i.e. s_magic == MQUEUE_MAGIC) at +* creation time by hook_d_instantiate(), never modified afterwards. +* Used to check LANDLOCK_SCOPE_POSIX_MSG_QUEUE against the +* accessing task's domain. +*/ +struct landlock_ruleset *mq_domain; };/**
@@ -141,6 +150,12 @@ landlock_inode(const struct inode *const inode)return inode->i_security + landlock_blob_sizes.lbs_inode; } +static inline bool +landlock_is_posix_mqueue_inode (const struct inode *const inode) +{ +return S_ISREG (inode->i_mode) && inode->i_sb->s_magic ==MQUEUE_MAGIC ; +} +static inline struct landlock_superblock_security *landlock_superblock (const struct super_block *const superblock) {
security/landlock/limits.h +1 -1
@@ -27,7 +27,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/ruleset.c +0 -1
@@ -520,7 +520,6 @@ static void free_ruleset_work(struct work_struct *const work)free_ruleset (ruleset); } -/* Only called by hook_cred_free(). */ void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset) {if (ruleset &&refcount_dec_and_test (&ruleset->usage)) {
security/landlock/task.c +43 -0
49 lines not on this page · raw message
security/landlock/task.h +4 -0
@@ -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 */
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH 2/6] landlock: Scope POSIX message queue opens
Quoted 162 lines · Oxana Kharitonova
Add support for enforcing
LANDLOCK_SCOPE_POSIX_MSG_QUEUEwhen opening POSIX message queues.Tag mqueuefs inodes at instantiation time with the landlock domain of the task that created the queue. This domain is stored in the landlock inode security blob and kept alive until the inode security blob is released.
On file open, detect mqueuefs regular files and compare the queue creator's domain with the opener's scoped domain. Deny the open when the opener is restricted by
LANDLOCK_SCOPE_POSIX_MSG_QUEUEand the queue was created outside of an allowed parent domain.This makes POSIX message queues follow the same scoped-domain model as the existing landlock IPC restrictions, while keeping the queue creator domain tied to the lifetime of the queue inode.
Signed-off-by Oxana Kharitonova <[email protected]>--- include/uapi/linux/landlock.h | 1 + security/landlock/audit.c | 9 ++++++++ security/landlock/audit.h | 1 + security/landlock/fs.c | 35 ++++++++++++++++++++++++++++ security/landlock/fs.h | 15 ++++++++++++ security/landlock/limits.h | 2 +- security/landlock/ruleset.c | 1 - security/landlock/task.c | 43 +++++++++++++++++++++++++++++++++++ security/landlock/task.h | 4 ++++ 9 files changed, 109 insertions(+), 2 deletions(-)include/uapi/linux/landlock.h +1
-0@@ -491,6 +491,7 @@ struct landlock_net_port_attr {/* 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*/ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/audit.c +9
-0@@ -82,6 +82,10 @@ get_blocker(const enum landlock_request_type type,case LANDLOCK_REQUEST_SCOPE_SIGNAL :WARN_ON_ONCE (access_bit !=-1 );return "scope.signal" ; + +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : +WARN_ON_ONCE (access_bit !=-1 ); +return "scope.posix_msg_queue" ; }WARN_ON_ONCE (1 );@@ -646,6 +650,11 @@ void landlock_log_denial(const struct landlock_cred_security *const subject, !!(quiet_mask &LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET );break ; +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : + quiet_applicable_to_access = + !!(quiet_mask & +LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); +break ;/* * Leave LANDLOCK_REQUEST_PTRACE and * LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY unhandled for now - they security/landlock/audit.h +1
-0@@ -21,6 +21,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, };/* security/landlock/fs.c +35
-0@@ -51,6 +51,7 @@#include "object.h" #include "ruleset.h" #include "setup.h" +#include "task.h" /* Underlying object management */ @@ -1268,6 +1269,33 @@ static void hook_inode_free_security_rcu(void *inode_security)*/ inode_sec = inode_security + landlock_blob_sizes.lbs_inode;WARN_ON_ONCE (inode_sec->object); + +landlock_put_ruleset_deferred (inode_sec->mq_domain); +} + +/* +* 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_ruleset *dom; + +if (!landlock_is_posix_mqueue_inode (inode)) +return ; + + dom =landlock_get_current_domain (); +if (!dom) +return ; + +if (WARN_ON_ONCE (landlock_inode (inode)->mq_domain)) +return ; + +landlock_get_ruleset (dom); +landlock_inode (inode)->mq_domain = dom; }/* Super-block hooks */ @@ -1756,6 +1784,12 @@ static int hook_file_open(struct file *const file)const struct landlock_cred_security *const subject =landlock_get_applicable_subject (file->f_cred, any_fs,NULL );struct landlock_request request = {}; +int err; + +/* POSIX message queue scoping is independent of FS access rights. */ + err =landlock_check_posix_mqueue_open (file); +if (err) +return err;if (!subject)return 0 ;@@ -1979,6 +2013,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 (d_instantiate, hook_d_instantiate),LSM_HOOK_INIT (sb_delete, hook_sb_delete),LSM_HOOK_INIT (sb_mount, hook_sb_mount),security/landlock/fs.h +1
-0@@ -14,6 +14,7 @@#include <linux/fs.h> #include <linux/init.h> #include <linux/rcupdate.h> +#include <uapi/linux/magic.h> #include "access.h" #include "cred.h" @@ -38,6 +39,14 @@ struct landlock_inode_security {* performed by get_inode_object().
security/landlock/fs.h struct landlock_inode_security { · line 49
Quoted 10 lines
security/landlock/fs.h +8
-0*/ struct landlock_object __rcu *object; +/** +* @mq_domain: Domain of the task that created POSIX message queue. +* Only set for mqueuefs inodes (i.e. s_magic == MQUEUE_MAGIC) at +* creation time by hook_d_instantiate(), never modified afterwards. +* Used to check LANDLOCK_SCOPE_POSIX_MSG_QUEUE against the +* accessing task's domain. +*/ +struct landlock_ruleset *mq_domain;
This seems like the right approach for posix mq.
They are NOT like SysV mq, they are opened like files with persistent handles. This follows the existing approach just like any other file and doesn't restrict after open.
...
But this doesn't restrict mq_unlink?
Shouldn't it handle that too? (otherwise scoped process could still unlink an mq created outside it's domain, then recreate one with the same name and bypass the restriction)
You may need to handle security_inode_unlink for this.
Justin
Quoted 114 lines
security/landlock/fs.h +6
-0};/** @@ -141,6 +150,12 @@ landlock_inode(const struct inode *const inode)return inode->i_security + landlock_blob_sizes.lbs_inode; } +static inline bool +landlock_is_posix_mqueue_inode (const struct inode *const inode) +{ +return S_ISREG (inode->i_mode) && inode->i_sb->s_magic ==MQUEUE_MAGIC ; +} +static inline struct landlock_superblock_security *landlock_superblock (const struct super_block *const superblock) {security/landlock/limits.h +1
-1@@ -27,7 +27,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/ruleset.c +0
-1@@ -520,7 +520,6 @@ static void free_ruleset_work(struct work_struct *const work)free_ruleset (ruleset); } -/* Only called by hook_cred_free(). */ void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset) {if (ruleset &&refcount_dec_and_test (&ruleset->usage)) {security/landlock/task.c +43
-0@@ -453,6 +453,49 @@ 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) +{ +const struct inode *const inode =file_inode (file); +const struct landlock_cred_security *subject; +size_t handle_layer; + +if (!landlock_is_posix_mqueue_inode (inode)) +return 0 ; + + subject =landlock_get_applicable_subject (file->f_cred, + posix_mqueue_scope, + &handle_layer); +if (!subject) +return 0 ; + +if (!domain_is_scoped (subject->domain, +landlock_inode (inode)->mq_domain, +LANDLOCK_SCOPE_POSIX_MSG_QUEUE )) +return 0 ; + +landlock_log_denial (subject, &(struct landlock_request ) { + .type =LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE , + .audit = { + .type =LSM_AUDIT_DATA_FILE , + .u.file = file, + }, + .layer_plus_one = handle_layer +1 , + }); +return -EPERM ; +} +static struct security_hook_list landlock_hooks[]__ro_after_init = {LSM_HOOK_INIT (ptrace_access_check, hook_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, hook_ptrace_traceme),security/landlock/task.h +4
-0@@ -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 */ Signature · 1 line
2.50.1 (Apple Git-155)
Mickaël Salaün
maintainerRe: [PATCH 2/6] landlock: Scope POSIX message queue opens
In reply to Oxana Kharitonovainclude/uapi/linux/landlock.h
Quoted 34 lines · Oxana Kharitonova
Add support for enforcing
LANDLOCK_SCOPE_POSIX_MSG_QUEUEwhen opening POSIX message queues.Tag mqueuefs inodes at instantiation time with the landlock domain of the task that created the queue. This domain is stored in the landlock inode security blob and kept alive until the inode security blob is released.
On file open, detect mqueuefs regular files and compare the queue creator's domain with the opener's scoped domain. Deny the open when the opener is restricted by
LANDLOCK_SCOPE_POSIX_MSG_QUEUEand the queue was created outside of an allowed parent domain.This makes POSIX message queues follow the same scoped-domain model as the existing landlock IPC restrictions, while keeping the queue creator domain tied to the lifetime of the queue inode.
Signed-off-by Oxana Kharitonova <[email protected]>--- include/uapi/linux/landlock.h | 1 + security/landlock/audit.c | 9 ++++++++ security/landlock/audit.h | 1 + security/landlock/fs.c | 35 ++++++++++++++++++++++++++++ security/landlock/fs.h | 15 ++++++++++++ security/landlock/limits.h | 2 +- security/landlock/ruleset.c | 1 - security/landlock/task.c | 43 +++++++++++++++++++++++++++++++++++ security/landlock/task.h | 4 ++++ 9 files changed, 109 insertions(+), 2 deletions(-)include/uapi/linux/landlock.h +0
-0
This file also contains the access right user doc which will automatically be included by Sphinx.
Quoted 24 lines
include/uapi/linux/landlock.h +1
-0@@ -491,6 +491,7 @@ struct landlock_net_port_attr {/* 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*/ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/audit.c +4
-0@@ -82,6 +82,10 @@ get_blocker(const enum landlock_request_type type,case LANDLOCK_REQUEST_SCOPE_SIGNAL :WARN_ON_ONCE (access_bit !=-1 );return "scope.signal" ; + +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : +WARN_ON_ONCE (access_bit !=-1 ); +return "scope.posix_msg_queue" ; }WARN_ON_ONCE (1 );
security/landlock/audit.c void landlock_log_denial(const struct landlock_cred_security *const subject, · line 656
Quoted 7 lines
security/landlock/audit.c +4
-0@@ -646,6 +650,11 @@ void landlock_log_denial(const struct landlock_cred_security *const subject, !!(quiet_mask &LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET );break ; +case LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE : + quiet_applicable_to_access = + !!(quiet_mask & +LANDLOCK_SCOPE_POSIX_MSG_QUEUE );
All the files must be formated with e.g.,
clang-format -i security/landlock/*.[ch]
There is currently an issue with the clang-format config so please ignore the RCU guard formatting.
Quoted 97 lines
security/landlock/audit.c +1
-0+break ;/* * Leave LANDLOCK_REQUEST_PTRACE and * LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY unhandled for now - they security/landlock/audit.h +1
-0@@ -21,6 +21,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, };/* security/landlock/fs.c +35
-0@@ -51,6 +51,7 @@#include "object.h" #include "ruleset.h" #include "setup.h" +#include "task.h" /* Underlying object management */ @@ -1268,6 +1269,33 @@ static void hook_inode_free_security_rcu(void *inode_security)*/ inode_sec = inode_security + landlock_blob_sizes.lbs_inode;WARN_ON_ONCE (inode_sec->object); + +landlock_put_ruleset_deferred (inode_sec->mq_domain); +} + +/* +* 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_ruleset *dom; + +if (!landlock_is_posix_mqueue_inode (inode)) +return ; + + dom =landlock_get_current_domain (); +if (!dom) +return ; + +if (WARN_ON_ONCE (landlock_inode (inode)->mq_domain)) +return ; + +landlock_get_ruleset (dom); +landlock_inode (inode)->mq_domain = dom; }/* Super-block hooks */ @@ -1756,6 +1784,12 @@ static int hook_file_open(struct file *const file)const struct landlock_cred_security *const subject =landlock_get_applicable_subject (file->f_cred, any_fs,NULL );struct landlock_request request = {}; +int err; + +/* POSIX message queue scoping is independent of FS access rights. */ + err =landlock_check_posix_mqueue_open (file); +if (err) +return err;if (!subject)return 0 ;@@ -1979,6 +2013,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 (d_instantiate, hook_d_instantiate),LSM_HOOK_INIT (sb_delete, hook_sb_delete),LSM_HOOK_INIT (sb_mount, hook_sb_mount),security/landlock/fs.h +1
-0@@ -14,6 +14,7 @@#include <linux/fs.h> #include <linux/init.h> #include <linux/rcupdate.h> +#include <uapi/linux/magic.h> #include "access.h" #include "cred.h" @@ -38,6 +39,14 @@ struct landlock_inode_security {* performed by get_inode_object().
security/landlock/fs.h struct landlock_inode_security { · line 49
Quoted 10 lines
security/landlock/fs.h +8
-0*/ struct landlock_object __rcu *object; +/** +* @mq_domain: Domain of the task that created POSIX message queue. +* Only set for mqueuefs inodes (i.e. s_magic == MQUEUE_MAGIC) at +* creation time by hook_d_instantiate(), never modified afterwards. +* Used to check LANDLOCK_SCOPE_POSIX_MSG_QUEUE against the +* accessing task's domain. +*/ +struct landlock_ruleset *mq_domain;
This would be a very-specific pointer just for mqueue. Instead, we should use the same mechanism as for identifying inodes with the Landlock object pointer (this mechanism was designed to generically manage weak references). This should also natively handle a few corner cases (e.g. unmounting the mqueue FS, which should have a test BTW).
Quoted 114 lines
security/landlock/fs.h +6
-0};/** @@ -141,6 +150,12 @@ landlock_inode(const struct inode *const inode)return inode->i_security + landlock_blob_sizes.lbs_inode; } +static inline bool +landlock_is_posix_mqueue_inode (const struct inode *const inode) +{ +return S_ISREG (inode->i_mode) && inode->i_sb->s_magic ==MQUEUE_MAGIC ; +} +static inline struct landlock_superblock_security *landlock_superblock (const struct super_block *const superblock) {security/landlock/limits.h +1
-1@@ -27,7 +27,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/ruleset.c +0
-1@@ -520,7 +520,6 @@ static void free_ruleset_work(struct work_struct *const work)free_ruleset (ruleset); } -/* Only called by hook_cred_free(). */ void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset) {if (ruleset &&refcount_dec_and_test (&ruleset->usage)) {security/landlock/task.c +43
-0@@ -453,6 +453,49 @@ 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) +{ +const struct inode *const inode =file_inode (file); +const struct landlock_cred_security *subject; +size_t handle_layer; + +if (!landlock_is_posix_mqueue_inode (inode)) +return 0 ; + + subject =landlock_get_applicable_subject (file->f_cred, + posix_mqueue_scope, + &handle_layer); +if (!subject) +return 0 ; + +if (!domain_is_scoped (subject->domain, +landlock_inode (inode)->mq_domain, +LANDLOCK_SCOPE_POSIX_MSG_QUEUE )) +return 0 ; + +landlock_log_denial (subject, &(struct landlock_request ) { + .type =LANDLOCK_REQUEST_SCOPE_POSIX_MSG_QUEUE , + .audit = { + .type =LSM_AUDIT_DATA_FILE , + .u.file = file, + }, + .layer_plus_one = handle_layer +1 , + }); +return -EPERM ; +} +static struct security_hook_list landlock_hooks[]__ro_after_init = {LSM_HOOK_INIT (ptrace_access_check, hook_ptrace_access_check),LSM_HOOK_INIT (ptrace_traceme, hook_ptrace_traceme),security/landlock/task.h +4
-0@@ -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 */ Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 3/6 landlock: Bump ABI for LANDLOCK_SCOPE_POSIX_MSG_QUEUE
In reply to Oxana KharitonovaIncrease the landlock ABI version so userspace can detect support for LANDLOCK_SCOPE_POSIX_MSG_QUEUE.
--- security/landlock/syscalls.c | 2 +- tools/testing/selftests/landlock/base_test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
security/landlock/syscalls.c +1 -1
@@ -169,7 +169,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 =10 ; +const int landlock_abi_version =11 ;/** * sys_landlock_create_ruleset - Create a new ruleset
tools/testing/selftests/landlock/base_test.c +1 -1
@@ -76,7 +76,7 @@ TEST(abi_version)const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs =LANDLOCK_ACCESS_FS_READ_FILE , }; -ASSERT_EQ (10 ,landlock_create_ruleset (NULL ,0 , +ASSERT_EQ (11 ,landlock_create_ruleset (NULL ,0 ,LANDLOCK_CREATE_RULESET_VERSION )); ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
Signature · 1 line
2.50.1 (Apple Git-155)
Mickaël Salaün
maintainerRe: [PATCH 3/6] landlock: Bump ABI for LANDLOCK_SCOPE_POSIX_MSG_QUEUE
Patches must be bisectable and self-sufficient, so this one must be squashed into the previous one that actually introduces the feature.
Quoted 39 lines · Oxana Kharitonova
Increase the landlock ABI version so userspace can detect support for
LANDLOCK_SCOPE_POSIX_MSG_QUEUE.Signed-off-by Oxana Kharitonova <[email protected]>--- security/landlock/syscalls.c | 2 +- tools/testing/selftests/landlock/base_test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)security/landlock/syscalls.c +1
-1@@ -169,7 +169,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 =10 ; +const int landlock_abi_version =11 ;/** * sys_landlock_create_ruleset - Create a new ruleset tools/testing/selftests/landlock/base_test.c +1
-1@@ -76,7 +76,7 @@ TEST(abi_version)const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs =LANDLOCK_ACCESS_FS_READ_FILE , }; -ASSERT_EQ (10 ,landlock_create_ruleset (NULL ,0 , +ASSERT_EQ (11 ,landlock_create_ruleset (NULL ,0 ,LANDLOCK_CREATE_RULESET_VERSION )); ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 4/6 selftests/landlock: Test POSIX message queue scoping
In reply to Oxana KharitonovaAdd tests for LANDLOCK_SCOPE_POSIX_MSG_QUEUE.
Verify that opening a queue created outside the current scoped domain is denied, and that opening a queue created within the same domain remains allowed.
--- .../landlock/scoped_posix_msg_queue_test.c | 223 ++++++++++++++++++ .../testing/selftests/landlock/scoped_test.c | 2 +- 2 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +223 -0
@@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Landlock tests - POSIX message queue scoping +* +* Copyright © 2024-2026 Microsoft Corporation +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <linux/landlock.h> +#include <mqueue.h> +#include <stddef.h> +#include <string.h> +#include <sys/prctl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "common.h" +#include "scoped_common.h" + +static void set_mq_name (char *const name,const size_t size) +{ +snprintf (name, size,"/selftests-landlock-mq-tid%d" ,sys_gettid ()); +} + +FIXTURE (scoped_domains) +{ +char mq_name[NAME_MAX ]; +}; + +#include "scoped_base_variants.h" + +FIXTURE_SETUP (scoped_domains) +{ +drop_caps (_metadata); + +set_mq_name (self->mq_name,sizeof (self->mq_name)); +/* Removes a possibly stale queue from a previous run. */ +mq_unlink (self->mq_name); +} + +FIXTURE_TEARDOWN (scoped_domains) +{ +} + +/* +* The parent creates the message queue, then the child tries +* to open it, with scoped domain(s) or no domain at all. +*/ +TEST_F(scoped_domains, open_parent_queue) +{ +pid_t child; +int status; +int pipe_parent[2 ], pipe_child[2 ]; +char buf; +mqd_t mq; + +ASSERT_EQ (0 ,pipe2 (pipe_parent,O_CLOEXEC )); +ASSERT_EQ (0 ,pipe2 (pipe_child,O_CLOEXEC )); + +if (variant->domain_both) +create_scoped_domain (_metadata,LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +mqd_t mq_child; + +EXPECT_EQ (0 ,close (pipe_parent[1 ])); +EXPECT_EQ (0 ,close (pipe_child[0 ])); + +if (variant->domain_child) +create_scoped_domain (_metadata, +LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + +/* Waits for the parent to create the queue. */ +ASSERT_EQ (1 ,read (pipe_parent[0 ], &buf,1 )); + + mq_child =mq_open (self->mq_name,O_RDWR ); +if (!variant->domain_child) { +EXPECT_LE (0 , mq_child); +if (mq_child >=0 ) +EXPECT_EQ (0 ,mq_close (mq_child)); + }else { +EXPECT_EQ (-1 , mq_child); +EXPECT_EQ (EPERM , errno); + } + +ASSERT_EQ (1 ,write (pipe_child[1 ],"." ,1 )); +_exit (_metadata->exit_code); +return ; + } +EXPECT_EQ (0 ,close (pipe_parent[0 ])); +EXPECT_EQ (0 ,close (pipe_child[1 ])); + +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); + +ASSERT_EQ (1 ,write (pipe_parent[1 ],"." ,1 )); +ASSERT_EQ (1 ,read (pipe_child[0 ], &buf,1 )); + +ASSERT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (0 ,mq_close (mq)); +EXPECT_EQ (0 ,mq_unlink (self->mq_name)); + +if (WIFSIGNALED (status) || !WIFEXITED (status) || +WEXITSTATUS (status) !=EXIT_SUCCESS ) + _metadata->exit_code =KSFT_FAIL ; +} + +/* +* The child creates the message queue, then the parent tries +* to open it, with scoped domain(s) or no domain at all. +*/ +TEST_F(scoped_domains, open_child_queue) +{ +pid_t child; +bool can_open_child_queue; +int status; +int pipe_parent[2 ], pipe_child[2 ]; +char buf; +mqd_t mq; + + can_open_child_queue = !variant->domain_parent; + +ASSERT_EQ (0 ,pipe2 (pipe_parent,O_CLOEXEC )); +ASSERT_EQ (0 ,pipe2 (pipe_child,O_CLOEXEC )); + +if (variant->domain_both) +create_scoped_domain (_metadata,LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +mqd_t mq_child; + +EXPECT_EQ (0 ,close (pipe_parent[1 ])); +EXPECT_EQ (0 ,close (pipe_child[0 ])); + +if (variant->domain_child) +create_scoped_domain (_metadata, +LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + +/* Waits for the parent to be in a domain, if any. */ +ASSERT_EQ (1 ,read (pipe_parent[0 ], &buf,1 )); + + mq_child =mq_open (self->mq_name,O_CREAT |O_RDWR ,0600 ,NULL ); +ASSERT_LE (0 , mq_child); + +ASSERT_EQ (1 ,write (pipe_child[1 ],"." ,1 )); + +ASSERT_EQ (1 ,read (pipe_parent[0 ], &buf,1 )); +EXPECT_EQ (0 ,mq_close (mq_child)); +EXPECT_EQ (0 ,mq_unlink (self->mq_name)); +_exit (_metadata->exit_code); +return ; + } +EXPECT_EQ (0 ,close (pipe_parent[0 ])); +EXPECT_EQ (0 ,close (pipe_child[1 ])); + +if (variant->domain_parent) +create_scoped_domain (_metadata,LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + +/* Signals that the parent is in a domain, if any. */ +ASSERT_EQ (1 ,write (pipe_parent[1 ],"." ,1 )); + +/* Waits for the child to create the queue. */ +ASSERT_EQ (1 ,read (pipe_child[0 ], &buf,1 )); + + mq =mq_open (self->mq_name,O_RDWR ); +if (can_open_child_queue) { +EXPECT_LE (0 , mq); +if (mq >=0 ) +EXPECT_EQ (0 ,mq_close (mq)); + }else { +EXPECT_EQ (-1 , mq); +EXPECT_EQ (EPERM , errno); + } + +/* Signals to the child that the open attempt is done. */ +ASSERT_EQ (1 ,write (pipe_parent[1 ],"." ,1 )); + +ASSERT_EQ (child,waitpid (child, &status,0 )); +if (WIFSIGNALED (status) || !WIFEXITED (status) || +WEXITSTATUS (status) !=EXIT_SUCCESS ) + _metadata->exit_code =KSFT_FAIL ; +} + +/* +* A process must always be able to open a queue it created within its own +* domain, whatever the enforced scope is. +*/ +TEST (create_and_open_same_domain) +{ +char mq_name[NAME_MAX ]; +mqd_t mq_create, mq_open_again; + +drop_caps (_metadata); +set_mq_name (mq_name,sizeof (mq_name)); +mq_unlink (mq_name); + +create_scoped_domain (_metadata,LANDLOCK_SCOPE_POSIX_MSG_QUEUE ); + + mq_create =mq_open (mq_name,O_CREAT |O_RDWR ,0600 ,NULL ); +ASSERT_LE (0 , mq_create); + + mq_open_again =mq_open (mq_name,O_RDWR ); +EXPECT_LE (0 , mq_open_again); +if (mq_open_again >=0 ) +EXPECT_EQ (0 ,mq_close (mq_open_again)); + +EXPECT_EQ (0 ,mq_close (mq_create)); +EXPECT_EQ (0 ,mq_unlink (mq_name)); +} + +TEST_HARNESS_MAIN
tools/testing/selftests/landlock/scoped_test.c +1 -1
@@ -12,7 +12,7 @@#include "common.h" -#define ACCESS_LAST LANDLOCK_SCOPE_SIGNAL +#define ACCESS_LAST LANDLOCK_SCOPE_POSIX_MSG_QUEUETEST (ruleset_with_unknown_scope) {
Signature · 1 line
2.50.1 (Apple Git-155)
Mickaël Salaün
maintainerRe: [PATCH 4/6] selftests/landlock: Test POSIX message queue scoping
These tests are good, but related audit (and quiet) tests are missing.
A test should check the mqueue unlink.
A test should check that once the sandboxed process that created an mqueue file exits, a new sibling or child sandboxed process cannot access this (stale) mqueue, but a parent sandboxed process should still be allowed to do so as well as for an unsandboxed process.
Quoted 19 lines · Oxana Kharitonova
Add tests for
LANDLOCK_SCOPE_POSIX_MSG_QUEUE.Verify that opening a queue created outside the current scoped domain is denied, and that opening a queue created within the same domain remains allowed.
Signed-off-by Oxana Kharitonova <[email protected]>--- .../landlock/scoped_posix_msg_queue_test.c | 223 ++++++++++++++++++ .../testing/selftests/landlock/scoped_test.c | 2 +- 2 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.ctools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +0
-0
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c line 5
Quoted 5 lines
tools/testing/selftests/landlock/scoped_posix_msg_queue_test.c new file +5
-0@@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Landlock tests - POSIX message queue scoping +* +* Copyright © 2024-2026 Microsoft Corporation
You need to update that.
PATCH 5/6 samples/landlock: Support POSIX message queue scoping
In reply to Oxana KharitonovaTeach the sandboxer sample to request LANDLOCK_SCOPE_POSIX_MSG_QUEUE through LL_SCOPED.
Add the "q" scope selector for POSIX message queues and document it in the sample help text. Also allow POSIX message queue denials to be quieted with "posix_msg_queue" through LL_QUIET_ACCESS.
--- samples/landlock/sandboxer.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
samples/landlock/sandboxer.c +14 -2
@@ -240,10 +240,12 @@ static bool check_ruleset_scope(const char *const env_var,bool error = false;bool abstract_scoping = false;bool signal_scoping = false; +bool posix_mqueue_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);
@@ -260,6 +262,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_mqueue_scoping) { + posix_mqueue_scoping = true; }else {fprintf (stderr,"Unknown or duplicate scope \"%s\"\n" , ipc_scoping_name);
@@ -276,6 +281,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_mqueue_scoping) + ruleset_attr->scoped &= ~LANDLOCK_SCOPE_POSIX_MSG_QUEUE ;unsetenv (env_var);return error;
@@ -354,6 +361,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);
@@ -400,6 +410,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"
@@ -416,6 +427,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\" "
@@ -423,7 +435,7 @@ static const char help[] =ENV_TCP_BIND_NAME "=\"9418\" " ENV_TCP_CONNECT_NAME "=\"80:443\" " ENV_UDP_CONNECT_SEND_NAME "=\"53\" " -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 "
Signature · 1 line
2.50.1 (Apple Git-155)
Mickaël Salaün
maintainerRe: [PATCH 5/6] samples/landlock: Support POSIX message queue scoping
Quoted 6 lines · Oxana Kharitonova
Teach the sandboxer sample to request
LANDLOCK_SCOPE_POSIX_MSG_QUEUEthroughLL_SCOPED.Add the "q" scope selector for POSIX message queues and document it in the sample help text. Also allow POSIX message queue denials to be quieted with "
posix_msg_queue" throughLL_QUIET_ACCESS.
This commit message should contains an example to prove with this sandboxer that the scope actually works, or at least to show how to use it.
Quoted 82 lines
Signed-off-by Oxana Kharitonova <[email protected]>--- samples/landlock/sandboxer.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)samples/landlock/sandboxer.c +14
-2@@ -240,10 +240,12 @@ static bool check_ruleset_scope(const char *const env_var,bool error = false;bool abstract_scoping = false;bool signal_scoping = false; +bool posix_mqueue_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);@@ -260,6 +262,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_mqueue_scoping) { + posix_mqueue_scoping = true; }else {fprintf (stderr,"Unknown or duplicate scope \"%s\"\n" , ipc_scoping_name);@@ -276,6 +281,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_mqueue_scoping) + ruleset_attr->scoped &= ~LANDLOCK_SCOPE_POSIX_MSG_QUEUE ;unsetenv (env_var);return error;@@ -354,6 +361,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);@@ -400,6 +410,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" @@ -416,6 +427,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\" " @@ -423,7 +435,7 @@ static const char help[] =ENV_TCP_BIND_NAME "=\"9418\" " ENV_TCP_CONNECT_NAME "=\"80:443\" " ENV_UDP_CONNECT_SEND_NAME "=\"53\" " -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 " Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 6/6 landlock: Document POSIX message queue scoping
In reply to Oxana KharitonovaDocument LANDLOCK_SCOPE_POSIX_MSG_QUEUE in the userspace API and UAPI kernel-doc.
--- Documentation/admin-guide/LSM/landlock.rst | 6 ++++-- Documentation/userspace-api/landlock.rst | 11 ++++++++++- include/uapi/linux/landlock.h | 6 +++++- 3 files changed, 19 insertions(+), 4 deletions(-)
Documentation/admin-guide/LSM/landlock.rst +4 -2
@@ -60,9 +60,11 @@ AUDIT_LANDLOCK_ACCESS - net.bind_udp - UDP port binding was denied - net.connect_send_udp - UDP connection and send was denied - **scope.*** - IPC scoping restrictions (ABI 6+): + **scope.*** - IPC scoping restrictions: - scope.abstract_unix_socket - Abstract UNIX socket connection denied - - scope.signal - Signal sending denied + (ABI 6+) + - scope.signal - Signal sending denied (ABI 6+) + - scope.posix_msg_queue - POSIX message queue opening denied (ABI 11+) Multiple blockers can appear in a single event (comma-separated) when multiple access rights are missing. For example, creating a regular file
Documentation/userspace-api/landlock.rst +10 -1
@@ -86,7 +86,8 @@ to be explicit about the denied-by-default access rights. LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP, .scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET | - LANDLOCK_SCOPE_SIGNAL, + LANDLOCK_SCOPE_SIGNAL | + LANDLOCK_SCOPE_POSIX_MSG_QUEUE, }; Because we may not know which kernel version an application will be executed
@@ -140,6 +141,10 @@ version, and only use the available subset of access rights: ruleset_attr.handled_access_net &= ~(LANDLOCK_ACCESS_NET_BIND_UDP | LANDLOCK_ACCESS_NET_CONNECT_SEND_UDP); + __attribute__((fallthrough)); + case 10: + /* Removes LANDLOCK_SCOPE_POSIX_MSG_QUEUE for ABI < 11 */ + ruleset_attr.scoped &= ~LANDLOCK_SCOPE_POSIX_MSG_QUEUE; } This enables the creation of an inclusive ruleset that will contain our rules.
@@ -420,6 +425,10 @@ The operations which can be scoped are: A :manpage:`sendto(2)` on a socket which was previously connected will not be restricted. This works for both datagram and stream sockets. +``LANDLOCK_SCOPE_POSIX_MSG_QUEUE`` + This limits opening POSIX message queues to queues created by a process in + the same or a nested Landlock domain. + IPC scoping does not support exceptions via :manpage:`landlock_add_rule(2)`. If an operation is scoped within a domain, no rules can be added to allow access to resources or processes outside of the scope.
include/uapi/linux/landlock.h +5 -1
@@ -478,7 +478,9 @@ struct landlock_net_port_attr {* Setting a flag for a ruleset will isolate the Landlock domain to forbid * connections to resources outside the domain. * -* This is supported since Landlock ABI version 6. +* This is supported since Landlock ABI version 6. The +* %LANDLOCK_SCOPE_POSIX_MSG_QUEUE scope is supported since Landlock ABI version +* 11. * * Scopes: *
@@ -487,6 +489,8 @@ struct landlock_net_port_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 created outside the domain. */ /* clang-format off */ #define LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET (1ULL << 0)
Signature · 1 line
2.50.1 (Apple Git-155)
Mickaël Salaün
maintainerHi Oxana,
Thanks for this patch series. Overall it looks good but you'll find some feedback to improve and send a v2, following Günther's and Justin's ones.
You should also Cc Christian and Al and ask for a review of the mqueue-specific patches (e.g. patch 1) for v2.
You'll find AI reviews here:
https://sashiko.dev/#/patchset/20260722122952.42149-1-oxana%40cloudflare.com
A test should create a domain that denies LANDLOCK_ACCESS_FS_READ_FILE and make sure the msgqueue scoping control stays the same.
The question about unlinking mqueue is a bit tricky. I don't think this should be restricted by an mqueue scope because that would mean that a sandboxed process creating an mqueue would not be able to remove the next time the same sandboxed program tries to clean up its files. So, we should check if such file are automatically removed if its creator exits. In any case, this should at least be documented in the user documentation.
The user doc should explain this new scope (see previous scope addition for examples).
tools/testing/selftests/landlock/config must be updated with CONFIG_POSIX_MQUEUE=y
Other questions/comments should either be answered to explain why they don't make sense or otherwise be implemented with v2.
Quoted 3 lines · Oxana Kharitonova
Hi,
This series adds landlock support for scoping POSIX message queuesi [1].
Nit: Landlock (in all docs, comments, and messages).
Quoted 63 lines
Landlock already supports scoped IPC restrictions for signals and abstract UNIX sockets. These restrictions make it possible to prevent a sandboxed task from interacting with IPC objects outside of its Landlock domain, while still allowing communication within the same domain or with nested domains.
This series extends the same model to POSIX message queues with a new
LANDLOCK_SCOPE_POSIX_MSG_QUEUEscope. When this scope is enforced, a task can only open POSIX message queues that were created by a task in the same landlock domain or in a nested domain.The implementation tags mqueuefs inodes at creation time with the creator's landlock domain. This domain is kept alive for the lifetime of the inode and is checked when the queue is opened.
The series also exposes the mqueuefs magic number through the shared UAPI magic header, bumps the Landlock ABI, updates documentation, adds sandboxer support, and adds selftests.
The new behavior is:
- a task restricted with
LANDLOCK_SCOPE_POSIX_MSG_QUEUEcannot open a queue created outside of its Landlock scope;
- a task can still open a queue created within its own Landlock domain;
- queues created outside of any Landlock domain are treated as outside the scope for a scoped opener.[1] https://man7.org/linux/man-pages/man7/mq_overview.7.html
Oxana Kharitonova (6):
ipc: Move mqueue fs magic to uapi magic header
landlock: Scope POSIX message queue opens
landlock: Bump ABI forLANDLOCK_SCOPE_POSIX_MSG_QUEUEselftests/landlock: Test POSIX message queue scopingsamples/landlock: Support POSIX message queue scoping
landlock: Document POSIX message queue scopingDocumentation/admin-guide/LSM/landlock.rst | 6 +- Documentation/userspace-api/landlock.rst | 11 +- include/uapi/linux/landlock.h | 7 +- include/uapi/linux/magic.h | 2 + ipc/mqueue.c | 2 +- samples/landlock/sandboxer.c | 16 +- security/landlock/audit.c | 9 + security/landlock/audit.h | 1 + security/landlock/fs.c | 35 +++ security/landlock/fs.h | 15 ++ security/landlock/limits.h | 2 +- security/landlock/ruleset.c | 1 - security/landlock/syscalls.c | 2 +- security/landlock/task.c | 43 ++++ security/landlock/task.h | 4 + tools/testing/selftests/landlock/base_test.c | 2 +- .../landlock/scoped_posix_msg_queue_test.c | 223 ++++++++++++++++++ .../testing/selftests/landlock/scoped_test.c | 2 +- 18 files changed, 371 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/landlock/scoped_posix_msg_queue_test.cSignature · 1 line
2.50.1 (Apple Git-155)
Quoted 7 lines · Mickaël Salaün
Hi Oxana,
Thanks for this patch series. Overall it looks good but you'll find some feedback to improve and send a v2, following Günther's and Justin's ones.
Thank you all for the reviews!
Just a quick heads-up: I'll be on vacation for the next few weeks, but I'll continue working on this once I'm back. Also, any further communication will come from my personal email, as I've left my current job.
Hello Oxana!
Quoted 21 lines · Oxana Kharitonova
This series adds landlock support for scoping POSIX message queuesi [1].
Landlock already supports scoped IPC restrictions for signals and abstract UNIX sockets. These restrictions make it possible to prevent a sandboxed task from interacting with IPC objects outside of its Landlock domain, while still allowing communication within the same domain or with nested domains.
This series extends the same model to POSIX message queues with a new
LANDLOCK_SCOPE_POSIX_MSG_QUEUEscope. When this scope is enforced, a task can only open POSIX message queues that were created by a task in the same landlock domain or in a nested domain.The implementation tags mqueuefs inodes at creation time with the creator's landlock domain. This domain is kept alive for the lifetime of the inode and is checked when the queue is opened.
The series also exposes the mqueuefs magic number through the shared UAPI magic header, bumps the Landlock ABI, updates documentation, adds sandboxer support, and adds selftests.
Quoted 7 lines
The new behavior is:
- a task restricted with
LANDLOCK_SCOPE_POSIX_MSG_QUEUEcannot open a queue created outside of its Landlock scope;
- a task can still open a queue created within its own Landlock domain;
- queues created outside of any Landlock domain are treated as outside the scope for a scoped opener.
Thank you for sending this!
I have a high level question about this patch set:
You are adding a check to hook_file_open(), but the existing hook_file_open() can already prevent mq_open().
The following experiment illustrates this:
* Restrict LANDLOCK_ACCESS_FS_READ_FILE and LANDLOCK_ACCESS_FS_WRITE_FILE
* Try to run mq_open("/foobar", O_CREAT...)
Depending on the installed PATH_BENEATH rules, you now see differing behaviour:
* If we allow READ_FILE and WRITE_FILE on nothing, mq_open() is *DENIED*.
* If we allow READ_FILE and WRITE_FILE on /, mq_open() is *DENIED*.
* If we allow READ_FILE and WRITE_FILE on a mounted /dev/mqueue, mq_open() is *ALLOWED*.
So it seems that the existing file system restrictions are already preventing POSIX message queues from being opened? And not only that -- since such Landlock policies are already quite common, it seems likely that many existing landlocked programs are already restricting opening of POSIX message queues today.
So, to clarify:
* What your patch set is adding is only that we are now additionally taking the Landlock domain scope into account?
* This distinction only makes a difference for landlocked programs that do not restrict READ_FILE/WRITE_FILE or that do restrict it and then allow-list READ_FILE/WRITE_FILE on a previously mounted /dev/mqueue.
Maybe this would be interesting to clarify a bit more prominently in the cover letter, because it reduces the applicability of this patchset?
Attached below is a LLM-generated (but double checked) test program which you can use to try out the creation of mqueues. (As first argument, use "-", "/" or "/dev/mqueue".)
What *might* still be interesting to restrict though: While mq_open() checks the READ_FILE and WRITE_FILE rights, it checks none of the LANDLOCK_ACCESS_FS_MAKE_* rights -- the message queue gets still created, even when the mq_open() is denied and returns with an error.
To expand on Justin's comment in [1] -- it feels that there are maybe still some gaps in the "lifecycle management" of these message queues that might be worth thinking systematically about, because both mq_unlink() and the creation of the message queue entries are currently apparently not restrictable yet? It makes me wonder whether hijacking of message queue names (creating same-named queues in other Landlock domains) is a problem then? Do you have thoughts on this?
Thanks,
–Günther
[1] https://lore.kernel.org/all/amKDYoOSvHMzVbKX@suesslenovo/
--- llmq.c
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/landlock.h>
#include <mqueue.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
static int ll_create(const struct landlock_ruleset_attr *a, size_t s, __u32 f) { return syscall(__NR_landlock_create_ruleset, a, s, f); }
static int ll_add(int fd, enum landlock_rule_type t, const void *a, __u32 f) { return syscall(__NR_landlock_add_rule, fd, t, a, f); }
static int ll_self(int fd, __u32 f)
{ return syscall(__NR_landlock_restrict_self, fd, f); }
#define RW (LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_WRITE_FILE)
int main(int argc, char **argv)
{
const char *grant = argv[1]; /* path to allow, or "-" for none */
struct landlock_ruleset_attr rsa = { .handled_access_fs = RW };
int rs = ll_create(&rsa, sizeof(rsa), 0);
mqd_t mq;
int f;
if (rs < 0) { perror("create_ruleset"); return 1; }
if (strcmp(grant, "-") != 0) {
struct landlock_path_beneath_attr pb = { .allowed_access = RW };
pb.parent_fd = open(grant, O_PATH | O_CLOEXEC);
if (pb.parent_fd < 0) { perror(grant); return 1; }
if (ll_add(rs, LANDLOCK_RULE_PATH_BENEATH, &pb, 0)) {
perror("add_rule"); return 1;
}
close(pb.parent_fd);
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) { perror("nnp"); return 1; }
if (ll_self(rs, 0)) { perror("restrict_self"); return 1; }
printf("granted=%-12s ", grant);
/* sanity: a normal file open, to prove the rule itself works */
f = open("/etc/hostname", O_RDONLY);
printf("open(/etc/hostname)=%-14s ",
f >= 0 ? "OK" : strerror(errno));
if (f >= 0) close(f);
mq = mq_open("/foobar", O_CREAT | O_RDWR, 0600, NULL);
printf("mq_open()=%s\n", mq != (mqd_t)-1 ? "OK" : strerror(errno));
if (mq != (mqd_t)-1) mq_close(mq);
return 0;
}---
Hi Günther!
Apologies for the delayed reply. I took some break.
Thank you very much for the detailed explanation and for the test program.
Quoted 17 lines · Günther Noack
Thank you for sending this!
I have a high level question about this patch set:
You are adding a check to
hook_file_open(), but the existinghook_file_open()can already preventmq_open().The following experiment illustrates this:
* RestrictLANDLOCK_ACCESS_FS_READ_FILEandLANDLOCK_ACCESS_FS_WRITE_FILE
* Try to runmq_open("/foobar", O_CREAT...)Depending on the installed
PATH_BENEATHrules, you now see differing behaviour:* If we allow
READ_FILEandWRITE_FILEon nothing,mq_open()is *DENIED*.
* If we allowREAD_FILEandWRITE_FILEon /,mq_open()is *DENIED*.
* If we allowREAD_FILEandWRITE_FILEon a mounted/dev/mqueue,mq_open()is *ALLOWED*.
Quoted 5 lines
So it seems that the existing file system restrictions are already preventing POSIX message queues from being opened? And not only that -- since such Landlock policies are already quite common, it seems likely that many existing landlocked programs are already restricting opening of POSIX message queues today.
The additional check in hook_file_open() adds a restriction for two sibling domains created under the same parent. If both siblings share filesystem access, but must not communicate with each other through POSIX message queues, the scope check prevents one sibling from opening a queue created by the other.
Although I can't say how practical this use case is. I see that it adds additional overhead for non-mqeuee files too, but it doesn't seem significant as it only checks the inode type and mqueuefs superblock magic.
If you think it's not worth it, I'm happy to remove it.
Quoted 9 lines
So, to clarify:
* What your patch set is adding is only that we are now additionally taking the Landlock domain scope into account?
* This distinction only makes a difference for landlocked programs that do not restrict READ_FILE/WRITE_FILE or that do restrict it and then allow-list READ_FILE/WRITE_FILE on a previously mounted/dev/mqueue.
Quoted 3 lines
Maybe this would be interesting to clarify a bit more prominently in the cover letter, because it reduces the applicability of this patchset?
Agree, I'll add more details in the cover letter.
Quoted 9 lines
Attached below is a LLM-generated (but double checked) test program which you can use to try out the creation of mqueues. (As first argument, use "-", "/" or "
/dev/mqueue".)What *might* still be interesting to restrict though: While
mq_open()checks theREAD_FILEandWRITE_FILErights, it checks none of the LANDLOCK_ACCESS_FS_MAKE_* rights -- the message queue gets still created, even when themq_open()is denied and returns with an error.
Quoted 7 lines
To expand on Justin's comment in [1] -- it feels that there are maybe still some gaps in the "lifecycle management" of these message queues that might be worth thinking systematically about, because both
mq_unlink()and the creation of the message queue entries are currently apparently not restrictable yet? It makes me wonder whether hijacking of message queue names (creating same-named queues in other Landlock domains) is a problem then? Do you have thoughts on this?
I need to think about it more and check how the mq_unlink works, taking into account the comment from Justin and Mickaël.
I'll follow up with a more complete answer.
Quoted 5 lines
Thanks,
–Günther[1] https://lore.kernel.org/all/amKDYoOSvHMzVbKX@suesslenovo/
Hi Günther!
As promissed, I'm following up with a more complete answer.
Quoted 5 lines · Oxana Kharitonova
Quoted 4 lines · Günther Noack
What *might* still be interesting to restrict though: While
mq_open()checks theREAD_FILEandWRITE_FILErights, it checks none of the LANDLOCK_ACCESS_FS_MAKE_* rights -- the message queue gets still created, even when themq_open()is denied and returns with an error.
I revisited my changes and noticed the gap you mentioned regarding mqueue creation. Thanks for highlighting it. I'll address it in v2.
Quoted 11 lines
Quoted 7 lines
To expand on Justin's comment in [1] -- it feels that there are maybe still some gaps in the "lifecycle management" of these message queues that might be worth thinking systematically about, because both
mq_unlink()and the creation of the message queue entries are currently apparently not restrictable yet? It makes me wonder whether hijacking of message queue names (creating same-named queues in other Landlock domains) is a problem then? Do you have thoughts on this?I need to think about it more and check how the
mq_unlinkworks, taking into account the comment from Justin and Mickaël.
I'll follow up with a more complete answer.
Regarding the lifecycle management of POSIX message queues, I haven’t found a good solution yet. I experimented with test programs, but the main constraints come from the POSIX message-queue semantics. A queue is independent of its creator’s lifetime and remains until mq_unlink() is called explicitly and all open descriptors are closed. Its name must be unique within the IPC namespace, but the same name can be reused after the existing queue has been unlinked.
If mq_unlink() is restricted for a process, a stale queue could remain and that process might be unable to clean it up. This seems to be the concern Mickaël raised in [2]. However, Landlock restrictions apply to the restricted process or domain, rather than globally to the queue, so another process might still be able to access or unlink it if its permissions allow. Therefore, I don’t currently see how to restrict this correctly through a Landlock policy, other than documenting the limitation. Let me know if you have other thoughts or I'm missing something.
Quoted 5 lines
Quoted 5 lines
Thanks,
–Günther[1] https://lore.kernel.org/all/amKDYoOSvHMzVbKX@suesslenovo/
Thanks,
Oxana