Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] landlock: restrict opening namespace file descriptors
@ 2026-11-26  3:40 shaos
  2026-11-26  4:05 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: shaos @ 2026-11-26  3:40 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: Günther Noack, Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers, Paul Moore,
	James Morris, Serge E. Hallyn, linux-security-module, linux-doc,
	linux-kernel, linux-trace-kernel, stable

Hello,

Landlock checks NAMESPACE_USE when a task creates a namespace or
installs one with setns(), but does not check acquisition of a file
descriptor for an existing namespace. A sandboxed task can use
SIOCGSKNS on a retained socket to obtain a descriptor for a network
namespace denied by its Landlock rules, then pass that descriptor to
RTM_SETLINK to move an interface into that namespace.

The reproducer uses a task with CAP_NET_ADMIN in its own user namespace
and a retained socket reference to the target namespace. It demonstrates
a Landlock namespace-use policy bypass; it does not demonstrate
privilege escalation beyond that capability.

This change checks nsfs file opens from Landlock's file_open hook. It
covers proc namespace links and namespace ioctls that create an nsfs
file. It keeps the current namespace implicitly accessible, consistent
with the documented behavior, and records fd acquisition distinctly in
audit and trace events.

HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
Git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
Source version: Linux 7.4.0-rc4
Compiler:       gcc 11.4.0, GNU ld 2.38
Test runtime:   QEMU 6.2.0, x86-64 TCG

The PoC, complete configuration, console logs, and initramfs helpers
are in this unlisted Gist:

https://gist.github.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc

Console output:
https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/console-output.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/kernel-config.txt
PoC source:
https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/poc.c

The reproduction script builds the specified kernel, builds the PoC and
initramfs, and runs the baseline, deny, allow, and invalid-fd cases:

    git clone https://gist.github.com/b2c0b1e1951692b79d2bc2f9421abfbc.git repro
    git -C repro checkout d2278bbd001f2c62b611fc0a5325cc4c8f7451b0
    chmod +x repro/*.sh
    ./repro/reproduce.sh

Two vulnerable boots allowed SIOCGSKNS and RTM_SETLINK despite the deny
rule: the move request was acknowledged and the interface appeared in
the denied namespace. With this patch, opening the current namespace
still succeeds, setns() and unshare() remain denied, SIOCGSKNS returns
EPERM, and the interface remains in its original namespace. Baseline,
explicit allow-rule, and invalid-fd controls pass.

Fixes: 15dd86021b57 ("landlock: Enforce namespace use restrictions")
Cc: stable@vger.kernel.org
Signed-off-by: shaos <shawnq1@163.com>

---
 Documentation/userspace-api/landlock.rst |  3 +++
 include/trace/events/landlock.h          | 12 ++++++----
 security/landlock/audit.c                |  4 ++++
 security/landlock/fs.c                   | 11 +++++++++
 security/landlock/log.c                  |  2 ++
 security/landlock/log.h                  |  1 +
 security/landlock/ns.c                   | 30 ++++++++++++++++--------
 security/landlock/ns.h                   |  4 ++++
 security/landlock/trace.c                |  1 +
 9 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst
index 7cc5fde482623..b1e78eef17bac 100644
--- a/Documentation/userspace-api/landlock.rst
+++ b/Documentation/userspace-api/landlock.rst
@@ -598,6 +598,9 @@ that references it.  The covered syscall paths are:
   descriptor referring to a newly-created mount namespace.
 * :manpage:`fsmount(2)` (default): the caller obtains a file descriptor
   referring to a newly-created anonymous mount namespace.
+* Opening an nsfs file descriptor, including through :manpage:`open(2)` on
+  ``/proc/<pid>/ns/`` or :manpage:`ioctl(2)` operations such as ``SIOCGSKNS``
+  and ``NS_GET_USERNS``.
 
 Anonymous mount namespaces (created by ``open_tree(OPEN_TREE_CLONE)`` and the
 default :manpage:`fsmount(2)`) are intentionally covered by the bit even though
diff --git a/include/trace/events/landlock.h b/include/trace/events/landlock.h
index 09e90e4a9cd7b..ae385ffb3a360 100644
--- a/include/trace/events/landlock.h
+++ b/include/trace/events/landlock.h
@@ -40,6 +40,7 @@ TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_FS_ACCESS);
 TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NET_ACCESS);
 TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_CREATE);
 TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_ENTER);
+TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_ACQUIRE);
 TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_CAPABILITY);
 
 #ifdef CREATE_TRACE_POINTS
@@ -993,8 +994,8 @@ TRACE_EVENT(landlock_deny_access_net,
  * @namespace_id: Namespace ID, not directly visible from user space when
  *                creation was denied.
  *
- * Emitted when a Landlock domain denies namespace creation or entry.  The
- * namespace_op field distinguishes these operations.
+ * Emitted when a Landlock domain denies namespace creation, entry, or fd
+ * acquisition. The namespace_op field distinguishes these operations.
  */
 TRACE_EVENT(landlock_deny_permission_namespace,
 
@@ -1028,14 +1029,17 @@ TRACE_EVENT(landlock_deny_permission_namespace,
 	TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s namespace_op=%s namespace_type=0x%x namespace_id=%llu",
 		  __entry->domain_id, __entry->same_exec, __entry->logged,
 		  (__entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_CREATE ||
-		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER) ?
+		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER ||
+		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ACQUIRE) ?
 			  __print_flags(__entry->blockers_access, "|",
 					_LANDLOCK_PERMISSION_BLOCKER_NAMES) :
 			  "unknown",
 		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_CREATE ?
 			  "create" :
 		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER ?
-			  "enter" : "unknown",
+			  "enter" :
+		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ACQUIRE ?
+			  "acquire" : "unknown",
 		  __entry->namespace_type, __entry->namespace_id)
 );
 
diff --git a/security/landlock/audit.c b/security/landlock/audit.c
index 6d9e54f5deedf..ddd653b686d72 100644
--- a/security/landlock/audit.c
+++ b/security/landlock/audit.c
@@ -82,6 +82,7 @@ get_blocker(const enum landlock_request_type type,
 
 	case LANDLOCK_REQUEST_NAMESPACE_CREATE:
 	case LANDLOCK_REQUEST_NAMESPACE_ENTER:
+	case LANDLOCK_REQUEST_NAMESPACE_ACQUIRE:
 	case LANDLOCK_REQUEST_CAPABILITY:
 		if (WARN_ON_ONCE(access_bit >= ARRAY_SIZE(permission_strings)))
 			return "unknown";
@@ -118,6 +119,7 @@ blocker_prefix(const enum landlock_request_type type)
 
 	case LANDLOCK_REQUEST_NAMESPACE_CREATE:
 	case LANDLOCK_REQUEST_NAMESPACE_ENTER:
+	case LANDLOCK_REQUEST_NAMESPACE_ACQUIRE:
 		return _LANDLOCK_PERMISSION_NAMESPACE_NAME ".";
 
 	case LANDLOCK_REQUEST_CAPABILITY:
@@ -218,6 +220,8 @@ void landlock_audit_denial(const struct landlock_request *const request,
 		audit_log_format(ab, " namespace_op=create");
 	else if (request->type == LANDLOCK_REQUEST_NAMESPACE_ENTER)
 		audit_log_format(ab, " namespace_op=enter");
+	else if (request->type == LANDLOCK_REQUEST_NAMESPACE_ACQUIRE)
+		audit_log_format(ab, " namespace_op=acquire");
 	audit_log_lsm_data(ab, &request->audit);
 	audit_log_end(ab);
 
diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 2ab67bcad1983..97b30fbffa48e 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -31,6 +31,8 @@
 #include <linux/net.h>
 #include <linux/path.h>
 #include <linux/pid.h>
+#include <linux/proc_fs.h>
+#include <linux/proc_ns.h>
 #include <linux/rcupdate.h>
 #include <linux/sched/signal.h>
 #include <linux/spinlock.h>
@@ -49,6 +51,7 @@
 #include "fs.h"
 #include "limits.h"
 #include "log.h"
+#include "ns.h"
 #include "object.h"
 #include "ruleset.h"
 #include "setup.h"
@@ -1861,6 +1864,14 @@ 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;
+
+	if (proc_ns_file(file)) {
+		err = landlock_check_namespace_fd(file->f_cred,
+						  get_proc_ns(file_inode(file)));
+		if (err)
+			return err;
+	}
 
 	if (!subject)
 		return 0;
diff --git a/security/landlock/log.c b/security/landlock/log.c
index c132b343f0ee0..fbcfa962cca32 100644
--- a/security/landlock/log.c
+++ b/security/landlock/log.c
@@ -390,6 +390,7 @@ static bool is_valid_request(const struct landlock_request *const request)
 	switch (request->type) {
 	case LANDLOCK_REQUEST_NAMESPACE_CREATE:
 	case LANDLOCK_REQUEST_NAMESPACE_ENTER:
+	case LANDLOCK_REQUEST_NAMESPACE_ACQUIRE:
 		if (WARN_ON_ONCE(request->permission !=
 				 LANDLOCK_PERMISSION_NAMESPACE_USE) ||
 		    WARN_ON_ONCE(request->audit.type != LSM_AUDIT_DATA_NS))
@@ -488,6 +489,7 @@ is_denial_quieted(const struct landlock_request *const request,
 			  LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET);
 	case LANDLOCK_REQUEST_NAMESPACE_CREATE:
 	case LANDLOCK_REQUEST_NAMESPACE_ENTER:
+	case LANDLOCK_REQUEST_NAMESPACE_ACQUIRE:
 		return !!(youngest_denied->quiet_permission.ns_types &
 			  landlock_ns_type_to_bit(request->audit.u.ns.ns_type));
 	case LANDLOCK_REQUEST_CAPABILITY:
diff --git a/security/landlock/log.h b/security/landlock/log.h
index 5ca87870c7b78..dee55270a7b89 100644
--- a/security/landlock/log.h
+++ b/security/landlock/log.h
@@ -29,6 +29,7 @@ enum landlock_request_type {
 	LANDLOCK_REQUEST_SCOPE_SIGNAL,
 	LANDLOCK_REQUEST_NAMESPACE_CREATE,
 	LANDLOCK_REQUEST_NAMESPACE_ENTER,
+	LANDLOCK_REQUEST_NAMESPACE_ACQUIRE,
 	LANDLOCK_REQUEST_CAPABILITY,
 };
 
diff --git a/security/landlock/ns.c b/security/landlock/ns.c
index 3d443599b16f4..1ef41ac4f977b 100644
--- a/security/landlock/ns.c
+++ b/security/landlock/ns.c
@@ -89,25 +89,28 @@ static const struct access_masks ns_permission = {
 /**
  * check_ns_type - Check namespace use permission
  *
- * @ns: The namespace being allocated or installed.
+ * @ns: The namespace being created, installed, or opened.
+ * @cred: Credentials whose Landlock domain is checked.
  * @request_type: Namespace operation reported on denial.
  *
- * Shared check for namespace_init (creation via clone(2), unshare(2),
- * open_tree(2), or fsmount(2)) and namespace_install (use via setns(2)): denies
- * when the namespace type is not in the domain's allowed set.  At allocation
- * time @ns->ns_id is already set even though the namespace is not reachable
- * from user space yet.
+ * Shared check for namespace creation, installation, and nsfs file opening.
+ * Namespaces in which the current task is already a member remain accessible.
+ * At allocation time @ns->ns_id is already set even though the namespace is
+ * not reachable from user space yet.
  *
  * Return: 0 if allowed, -EPERM if denied.
  */
 static int check_ns_type(struct ns_common *const ns,
+			 const struct cred *const cred,
 			 const enum landlock_request_type request_type)
 {
 	const struct landlock_cred_security *subject;
 	size_t denied_layer;
 
-	subject = landlock_get_applicable_subject(current_cred(), ns_permission,
-						  NULL);
+	if (is_current_namespace(ns))
+		return 0;
+
+	subject = landlock_get_applicable_subject(cred, ns_permission, NULL);
 	if (!subject)
 		return 0;
 
@@ -132,13 +135,20 @@ static int check_ns_type(struct ns_common *const ns,
 
 static int hook_namespace_init(struct ns_common *const ns)
 {
-	return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_CREATE);
+	return check_ns_type(ns, current_cred(),
+			     LANDLOCK_REQUEST_NAMESPACE_CREATE);
 }
 
 static int hook_namespace_install(const struct nsset *const nsset,
 				  struct ns_common *const ns)
 {
-	return check_ns_type(ns, LANDLOCK_REQUEST_NAMESPACE_ENTER);
+	return check_ns_type(ns, current_cred(),
+			     LANDLOCK_REQUEST_NAMESPACE_ENTER);
+}
+
+int landlock_check_namespace_fd(const struct cred *cred, struct ns_common *ns)
+{
+	return check_ns_type(ns, cred, LANDLOCK_REQUEST_NAMESPACE_ACQUIRE);
 }
 
 static struct security_hook_list landlock_hooks[] __ro_after_init = {
diff --git a/security/landlock/ns.h b/security/landlock/ns.h
index 46d3bd17479d2..cdf6dc2f7326e 100644
--- a/security/landlock/ns.h
+++ b/security/landlock/ns.h
@@ -10,8 +10,12 @@
 
 #include <linux/types.h>
 
+struct cred;
+struct ns_common;
+
 u64 landlock_ns_type_to_bit(u32 ns_type);
 u64 landlock_ns_types_to_bits(u64 ns_types);
+int landlock_check_namespace_fd(const struct cred *cred, struct ns_common *ns);
 
 __init void landlock_add_ns_hooks(void);
 
diff --git a/security/landlock/trace.c b/security/landlock/trace.c
index a47fb10dec459..9d71b31e9c27a 100644
--- a/security/landlock/trace.c
+++ b/security/landlock/trace.c
@@ -83,6 +83,7 @@ void landlock_trace_denial(
 	switch (request->type) {
 	case LANDLOCK_REQUEST_NAMESPACE_CREATE:
 	case LANDLOCK_REQUEST_NAMESPACE_ENTER:
+	case LANDLOCK_REQUEST_NAMESPACE_ACQUIRE:
 		if (trace_landlock_deny_permission_namespace_enabled()) {
 			const struct landlock_blockers blockers = {
 				.access = missing,
-- 
2.34.1


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

* Re: [PATCH] landlock: restrict opening namespace file descriptors
  2026-11-26  3:40 [PATCH] landlock: restrict opening namespace file descriptors shaos
@ 2026-11-26  4:05 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26  4:05 UTC (permalink / raw)
  To: shaos; +Cc: linux-security-module, linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] landlock: check_ns_type() current-ns check bypasses setns()
  enforcement
- [Medium] landlock: hook_file_open() checks nsfs permission before fs
  path access
- [Low] landlock: missing doc, UAPI comment, and test regex updates for
  acquire op
- [Low] landlock: commit message has bug-report boilerplate and wrong
  setns() claim
--

commit 54561b92150f0ba42829d19391fa02878f218068
Author: shaos <shawnq1@163.com>

> landlock: restrict opening namespace file descriptors
>
> Hello,
>
> Landlock checks NAMESPACE_USE when a task creates a namespace or
> installs one with setns(), but does not check acquisition of a file
> descriptor for an existing namespace. A sandboxed task can use
> SIOCGSKNS on a retained socket to obtain a descriptor for a network
> namespace denied by its Landlock rules, then pass that descriptor to
> RTM_SETLINK to move an interface into that namespace.
>
> The reproducer uses a task with CAP_NET_ADMIN in its own user namespace
> and a retained socket reference to the target namespace. It demonstrates
> a Landlock namespace-use policy bypass; it does not demonstrate
> privilege escalation beyond that capability.
>
> This change checks nsfs file opens from Landlock's file_open hook. It
> covers proc namespace links and namespace ioctls that create an nsfs
> file. It keeps the current namespace implicitly accessible, consistent
> with the documented behavior, and records fd acquisition distinctly in
> audit and trace events.
>
> HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
> Git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
> Source version: Linux 7.4.0-rc4
> Compiler:       gcc 11.4.0, GNU ld 2.38
> Test runtime:   QEMU 6.2.0, x86-64 TCG
>
> The PoC, complete configuration, console logs, and initramfs helpers
> are in this unlisted Gist:
>
> https://gist.github.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc
>
> Console output:
> https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/console-output.txt
> Kernel config:
> https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/kernel-config.txt
> PoC source:
> https://gist.githubusercontent.com/shaos26/b2c0b1e1951692b79d2bc2f9421abfbc/raw/d2278bbd001f2c62b611fc0a5325cc4c8f7451b0/poc.c
>
> The reproduction script builds the specified kernel, builds the PoC and
> initramfs, and runs the baseline, deny, allow, and invalid-fd cases:
>
>     git clone https://gist.github.com/b2c0b1e1951692b79d2bc2f9421abfbc.git repro
>     git -C repro checkout d2278bbd001f2c62b611fc0a5325cc4c8f7451b0
>     chmod +x repro/*.sh
>     ./repro/reproduce.sh
>
> Two vulnerable boots allowed SIOCGSKNS and RTM_SETLINK despite the deny
> rule: the move request was acknowledged and the interface appeared in
> the denied namespace. With this patch, opening the current namespace
> still succeeds, setns() and unshare() remain denied, SIOCGSKNS returns
> EPERM, and the interface remains in its original namespace. Baseline,
> explicit allow-rule, and invalid-fd controls pass.
>
> Fixes: 15dd86021b57 ("landlock: Enforce namespace use restrictions")
> Cc: stable@vger.kernel.org
> Signed-off-by: shaos <shawnq1@163.com>

[Severity: Low]
This isn't a code bug, but the greeting, test environment table, external
gist URLs, and git clone instructions belong below the "---" tearline rather
than in the permanent commit changelog.  Also, the claim that setns() on the
current namespace remains denied does not match the check_ns_type() change
below.

[ ... ]

> diff --git a/include/trace/events/landlock.h b/include/trace/events/landlock.h
> index 09e90e4a9cd7b..ae385ffb3a360 100644
> --- a/include/trace/events/landlock.h
> +++ b/include/trace/events/landlock.h
> @@ -40,6 +40,7 @@ TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_FS_ACCESS);
>  TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NET_ACCESS);
>  TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_CREATE);
>  TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_ENTER);
> +TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_NAMESPACE_ACQUIRE);
>  TRACE_DEFINE_ENUM(LANDLOCK_REQUEST_CAPABILITY);
>  
>  #ifdef CREATE_TRACE_POINTS
> @@ -993,8 +994,8 @@ TRACE_EVENT(landlock_deny_access_net,
>   * @namespace_id: Namespace ID, not directly visible from user space when
>   *                creation was denied.
>   *
> - * Emitted when a Landlock domain denies namespace creation or entry.  The
> - * namespace_op field distinguishes these operations.
> + * Emitted when a Landlock domain denies namespace creation, entry, or fd
> + * acquisition. The namespace_op field distinguishes these operations.
>   */
>  TRACE_EVENT(landlock_deny_permission_namespace,
>  
> @@ -1028,14 +1029,17 @@ TRACE_EVENT(landlock_deny_permission_namespace,
>  	TP_printk("domain=%llx same_exec=%d logged=%d blockers=%s namespace_op=%s namespace_type=0x%x namespace_id=%llu",
>  		  __entry->domain_id, __entry->same_exec, __entry->logged,
>  		  (__entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_CREATE ||
> -		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER) ?
> +		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER ||
> +		   __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ACQUIRE) ?
>  			  __print_flags(__entry->blockers_access, "|",
>  					_LANDLOCK_PERMISSION_BLOCKER_NAMES) :
>  			  "unknown",
>  		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_CREATE ?
>  			  "create" :
>  		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ENTER ?
> -			  "enter" : "unknown",
> +			  "enter" :
> +		  __entry->blockers_type == LANDLOCK_REQUEST_NAMESPACE_ACQUIRE ?
> +			  "acquire" : "unknown",
>  		  __entry->namespace_type, __entry->namespace_id)
>  );

[Severity: Low]
Should REGEX_DENY_PERMISSION_NAMESPACE in
tools/testing/selftests/landlock/trace.h, along with
Documentation/admin-guide/LSM/landlock.rst,
Documentation/trace/events-landlock.rst, and the
LANDLOCK_PERMISSION_NAMESPACE_USE comment in include/uapi/linux/landlock.h,
also be updated for the new namespace_op=acquire value?  As is,
REGEX_DENY_PERMISSION_NAMESPACE only matches "create" or "enter" and will
fail to match acquire denial trace events.

[ ... ]

> diff --git a/security/landlock/fs.c b/security/landlock/fs.c
> index 2ab67bcad1983..97b30fbffa48e 100644
> --- a/security/landlock/fs.c
> +++ b/security/landlock/fs.c

[ ... ]

> @@ -1861,6 +1864,14 @@ 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;
> +
> +	if (proc_ns_file(file)) {
> +		err = landlock_check_namespace_fd(file->f_cred,
> +						  get_proc_ns(file_inode(file)));
> +		if (err)
> +			return err;
> +	}
>  
>  	if (!subject)
>  		return 0;

[Severity: Medium]
Should hook_file_open() run landlock_check_namespace_fd() only after the
filesystem path access check succeeds?

When an nsfs file is bind-mounted into the filesystem (for example under
/run/netns/<name>) at a path where the domain denies
LANDLOCK_ACCESS_FS_READ_FILE, running landlock_check_namespace_fd() first
causes open() to fail with -EPERM instead of -EACCES and emits a
namespace_op=acquire audit and trace denial exposing the target namespace's
ns_type and ns_id from a path the task was not allowed to access.

[ ... ]

> diff --git a/security/landlock/ns.c b/security/landlock/ns.c
> index 3d443599b16f4..1ef41ac4f977b 100644
> --- a/security/landlock/ns.c
> +++ b/security/landlock/ns.c
> @@ -89,25 +89,28 @@ static const struct access_masks ns_permission = {
>  /**
>   * check_ns_type - Check namespace use permission
>   *
> - * @ns: The namespace being allocated or installed.
> + * @ns: The namespace being created, installed, or opened.
> + * @cred: Credentials whose Landlock domain is checked.
>   * @request_type: Namespace operation reported on denial.
>   *
> - * Shared check for namespace_init (creation via clone(2), unshare(2),
> - * open_tree(2), or fsmount(2)) and namespace_install (use via setns(2)): denies
> - * when the namespace type is not in the domain's allowed set.  At allocation
> - * time @ns->ns_id is already set even though the namespace is not reachable
> - * from user space yet.
> + * Shared check for namespace creation, installation, and nsfs file opening.
> + * Namespaces in which the current task is already a member remain accessible.
> + * At allocation time @ns->ns_id is already set even though the namespace is
> + * not reachable from user space yet.
>   *
>   * Return: 0 if allowed, -EPERM if denied.
>   */
>  static int check_ns_type(struct ns_common *const ns,
> +			 const struct cred *const cred,
>  			 const enum landlock_request_type request_type)
>  {
>  	const struct landlock_cred_security *subject;
>  	size_t denied_layer;
>  
> -	subject = landlock_get_applicable_subject(current_cred(), ns_permission,
> -						  NULL);
> +	if (is_current_namespace(ns))
> +		return 0;
> +
> +	subject = landlock_get_applicable_subject(cred, ns_permission, NULL);
>  	if (!subject)
>  		return 0;
>  
> @@ -132,13 +135,20 @@ static int check_ns_type(struct ns_common *const ns,
>  
>  static int hook_namespace_init(struct ns_common *const ns)
>  {
> -	return check_ns_type(ns, current_cred(),
> -			     LANDLOCK_REQUEST_NAMESPACE_CREATE);
>  }
>  
>  static int hook_namespace_install(const struct nsset *const nsset,
>  				  struct ns_common *const ns)
>  {
> -	return check_ns_type(ns, current_cred(),
> -			     LANDLOCK_REQUEST_NAMESPACE_ENTER);
>  }
>  
> +int landlock_check_namespace_fd(const struct cred *cred, struct ns_common *ns)
> +{
> +	return check_ns_type(ns, cred, LANDLOCK_REQUEST_NAMESPACE_ACQUIRE);
> +}

[Severity: High]
Doesn't placing the is_current_namespace(ns) check inside check_ns_type()
also exempt hook_namespace_install() when setns(2) targets a namespace the
task is already in?

Re-entering the current namespace via setns(2) is not a no-op (for example,
mntns_install() resets fs->pwd and fs->root to the mount namespace root),
and the setns denial and audit/trace tests in
tools/testing/selftests/landlock/ns_test.c open /proc/self/ns/<type> and
expect setns(2) to fail with EPERM.  Scoping is_current_namespace(ns) to
landlock_check_namespace_fd() (after checking
landlock_get_applicable_subject() so unsandboxed tasks avoid the check)
would preserve setns(2) enforcement.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179566442733.1993761.8053687674090289362@163.com?part=1

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

end of thread, other threads:[~2026-11-26  4:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  3:40 [PATCH] landlock: restrict opening namespace file descriptors shaos
2026-11-26  4:05 ` sashiko-bot

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