Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: shaos <shawnq1@163.com>
Cc: linux-security-module@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] landlock: restrict opening namespace file descriptors
Date: Thu, 26 Nov 2026 04:05:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-234719@kernel.org> (raw)
In-Reply-To: <179566442733.1993761.8053687674090289362@163.com>

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

      reply	other threads:[~2026-11-26  4:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  3:40 [PATCH] landlock: restrict opening namespace file descriptors shaos
2026-11-26  4:05 ` sashiko-bot [this message]

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=sashiko-outbox-234719@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shawnq1@163.com \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox