mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Mickaël Salaün" <mic@digikod.net>
To: "Günther Noack" <gnoack@google.com>
Cc: Ada Smith <adasmithgt@gmail.com>,
	Hungyu Lin <dennylin0707@gmail.com>,
	 James Morris <jmorris@namei.org>,
	Josef Bacik <josef@toxicpanda.com>,
	 Paul Moore <paul@paul-moore.com>,
	Serge Hallyn <serge@hallyn.com>,
	 Serge Hallyn <sergeh@kernel.org>,
	kernel-team@cloudflare.com, linux-kernel@vger.kernel.org,
	 linux-security-module@vger.kernel.org
Subject: Re: [PATCH v2 2/6] landlock: Mediate CAP_SYS_RAWIO for mappings below mmap_min_addr
Date: Wed, 18 Nov 2026 18:00:27 +0100	[thread overview]
Message-ID: <20261118.Phu0OoSh4ahY@digikod.net> (raw)
In-Reply-To: <avxjfHybQBcATFKV@google.com>

On Tue, Nov 17, 2026 at 03:23:56PM +0100, Günther Noack wrote:
> On Tue, Nov 17, 2026 at 11:11:52AM +0100, Mickaël Salaün wrote:
> > Until now, a process in a domain that denies CAP_SYS_RAWIO could still
> > map memory below the vm.mmap_min_addr limit if it held this capability.
> > cap_mmap_addr() is commoncap's mmap_addr hook, and it checks
> > CAP_SYS_RAWIO with cap_capable(), not security_capable(), so the check
> > never reaches hook_capable().  Mapping the lowest pages is what this
> > limit protects against, because it helps to exploit kernel NULL pointer
> > dereferences.
> > 
> > Add a Landlock mmap_addr hook that checks CAP_SYS_RAWIO against the
> > domain under the same condition as cap_mmap_addr().  With the same
> > vm.mmap_min_addr value, the hook only denies mappings for which
> > commoncap required the capability.  security_mmap_addr() stops at
> > commoncap's error, so processes without the capability get the same
> > result as before.  This also covers the x86 vm86(2) and vm86old(2)
> > system calls, which use the same check for address 0.
> > 
> > Making cap_mmap_addr() call capable() would also fix this, but it would
> > send every low mapping through the capable hook of all LSMs (SELinux,
> > AppArmor, SafeSetID, BPF LSM programs), adding capability checks that
> > their policies never had to handle.  For instance, SELinux checks
> > mmap_zero in its own mmap_addr hook and keeps it separate from the
> > capability on purpose.  LSMs with their own policy for low mappings
> > implement it in this hook, so Landlock does the same.
> > 
> > Cc: Günther Noack <gnoack@google.com>
> > Cc: Paul Moore <paul@paul-moore.com>
> > Cc: Serge Hallyn <serge@hallyn.com>
> > Acked-by: Serge Hallyn (AMD) <sergeh@kernel.org>
> > Fixes: 97c8386f1d74 ("landlock: Enforce capability restrictions")
> > Signed-off-by: Mickaël Salaün <mic@digikod.net>
> > ---
> > 
> > Changes since v1:
> > https://patch.msgid.link/20261110202436.2408244-3-mic@digikod.net
> > - Add Serge Hallyn's Acked-by.
> > ---
> >  security/landlock/cap.c | 22 ++++++++++++++++++++++
> >  1 file changed, 22 insertions(+)
> > 
> > diff --git a/security/landlock/cap.c b/security/landlock/cap.c
> > index fa59118e3486..10e1ca772b9c 100644
> > --- a/security/landlock/cap.c
> > +++ b/security/landlock/cap.c
> > @@ -9,6 +9,7 @@
> >  #include <linux/cred.h>
> >  #include <linux/lsm_audit.h>
> >  #include <linux/lsm_hooks.h>
> > +#include <linux/security.h>
> >  #include <uapi/linux/landlock.h>
> >  
> >  #include "cap.h"
> > @@ -90,8 +91,29 @@ static int hook_capable(const struct cred *cred, struct user_namespace *ns,
> >  	return check_cap(cred, cap, opts);
> >  }
> >  
> > +/**
> > + * hook_mmap_addr - Restrict mappings below dac_mmap_min_addr
> > + *
> > + * @addr: Address of the new mapping.
> > + *
> > + * cap_mmap_addr() requires CAP_SYS_RAWIO to map below dac_mmap_min_addr, but
> > + * calls cap_capable() directly, which skips hook_capable().  Check the same
> > + * condition against the domain.  commoncap runs first, so this only sees
> > + * mappings that commoncap allowed.
> > + *
> > + * Return: 0 if allowed, -EPERM if CAP_SYS_RAWIO use is denied.
> > + */
> > +static int hook_mmap_addr(unsigned long addr)
> > +{
> > +	if (addr >= dac_mmap_min_addr)
> > +		return 0;
> > +
> > +	return check_cap(current_cred(), CAP_SYS_RAWIO, CAP_OPT_NONE);
> > +}
> > +
> >  static struct security_hook_list landlock_hooks[] __ro_after_init = {
> >  	LSM_HOOK_INIT(capable, hook_capable),
> > +	LSM_HOOK_INIT(mmap_addr, hook_mmap_addr),
> >  };
> >  
> >  __init void landlock_add_cap_hooks(void)
> > -- 
> > 2.56.0
> > 
> 
> Reviewed-by: Günther Noack <gnoack@google.com>
> 
> Looks good; I compared this against the commoncap implementation of
> the same hook, and the logic checks out.
> 
> 
> Minor nit, in case you end up sending a V3; I found the commit message
> a bit wordy.

Indeed, I'll send a v3 with your feedback.

> 
> It makes intuitively sense that the commoncap LSM does not want to
> invoke functions which themselves call into LSM hooks, even if it's
> just a safeguard to prevent accidental recursion or deep call stacks.
> 
> So for the few cases where commoncap checks capabilities directly, we
> need to do the equivalent checks in the same LSM hooks in Landlock.
> That means, we replicate the logic from the commoncap hook if
> necessary and treat it like a capability use that came in through the
> normal hook.  This is similar to what SELinux already does as well.
> 
> What I would have found useful to discuss in this commit message would
> have been the story of PF_SUPERPRIV:
> 
> commoncap sets PF_SUPERPRIV when granting the capability use, and
> Landlock does not undo that when it rejects the capability use.  In my
> understanding this is OK because the flag only gets used in "legacy
> accounting" use cases and the consequences are benign.  The
> interaction with SELinux is already the same as well - it does not
> unset that flag.

Good catch!  I think it should not be addressed by this series but I
just sent a fix for that:
https://lore.kernel.org/r/20261118165603.2573289-1-mic@digikod.net

> 
> —Günther
> 

  reply	other threads:[~2026-11-18 17:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:11 [PATCH v2 0/6] Fix Landlock mediation of commoncap's capability checks Mickaël Salaün
2026-11-17 10:11 ` [PATCH v2 1/6] landlock: Factor out capability denial checks Mickaël Salaün
2026-11-17 13:56   ` Günther Noack
2026-11-17 10:11 ` [PATCH v2 2/6] landlock: Mediate CAP_SYS_RAWIO for mappings below mmap_min_addr Mickaël Salaün
2026-11-17 14:23   ` Günther Noack
2026-11-18 17:00     ` Mickaël Salaün [this message]
2026-11-17 10:11 ` [PATCH v2 3/6] landlock: Mediate CAP_SETPCAP for inheritable capabilities Mickaël Salaün
2026-11-17 15:41   ` Günther Noack
2026-11-18 17:01     ` Mickaël Salaün
2026-11-17 10:11 ` [PATCH v2 4/6] landlock: Mediate CAP_SYS_ADMIN for the admin memory reserve Mickaël Salaün
2026-11-17 15:43   ` Günther Noack
2026-11-17 10:11 ` [PATCH v2 5/6] selftests/landlock: Test capability checks outside the capable hook Mickaël Salaün
2026-11-17 16:11   ` Günther Noack
2026-11-18 17:11     ` Mickaël Salaün
2026-11-17 10:11 ` [PATCH v2 6/6] landlock: Fix capability restriction documentation Mickaël Salaün
2026-11-17 16:25   ` Günther Noack
2026-11-18 17:11     ` Mickaël Salaün

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=20261118.Phu0OoSh4ahY@digikod.net \
    --to=mic@digikod.net \
    --cc=adasmithgt@gmail.com \
    --cc=dennylin0707@gmail.com \
    --cc=gnoack@google.com \
    --cc=jmorris@namei.org \
    --cc=josef@toxicpanda.com \
    --cc=kernel-team@cloudflare.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=serge@hallyn.com \
    --cc=sergeh@kernel.org \
    /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

all inboxes | Powered by JetHome®