Re: [PATCH v2 4/6] landlock: Mediate CAP_SYS_ADMIN for the admin memory reserve
From: Günther Noack
Date: Tue Nov 17 2026 - 10:43:54 EST
On Tue, Nov 17, 2026 at 11:11:54AM +0100, Mickaël Salaün wrote:
> With vm.overcommit_memory set to 2, __vm_enough_memory() keeps the
> vm.admin_reserve_kbytes reserve for callers whose LSMs all agree that
> they hold CAP_SYS_ADMIN. cap_vm_enough_memory() checks it with
> cap_capable(), so a process in a domain that denies CAP_SYS_ADMIN could
> still use this reserve, which is meant to let an administrator recover
> from memory exhaustion.
>
> Add a Landlock vm_enough_memory hook that withholds the reserve when the
> domain denies CAP_SYS_ADMIN. Like commoncap and SELinux, do not audit
> this check: it only selects a limit, it runs for every accounted
> mapping, and withholding the reserve can only make an allocation fail
> with -ENOMEM. As with the previous hooks, LSMs implement their own
> policy in this hook: SELinux already checks CAP_SYS_ADMIN in its own
> vm_enough_memory hook, so making commoncap call security_capable() would
> make SELinux check it twice.
>
> Cc: Günther Noack <gnoack@xxxxxxxxxx>
> Cc: Paul Moore <paul@xxxxxxxxxxxxxx>
> Cc: Serge Hallyn <serge@xxxxxxxxxx>
> Acked-by: Serge Hallyn (AMD) <sergeh@xxxxxxxxxx>
> Fixes: 97c8386f1d74 ("landlock: Enforce capability restrictions")
> Signed-off-by: Mickaël Salaün <mic@xxxxxxxxxxx>
> ---
>
> Changes since v1:
> https://patch.msgid.link/20261110202436.2408244-5-mic@xxxxxxxxxxx
> - 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 c89e8d76db8c..c9a900888f3b 100644
> --- a/security/landlock/cap.c
> +++ b/security/landlock/cap.c
> @@ -140,10 +140,32 @@ static int hook_mmap_addr(unsigned long addr)
> return check_cap(current_cred(), CAP_SYS_RAWIO, CAP_OPT_NONE);
> }
>
> +/**
> + * hook_vm_enough_memory - Restrict the admin memory reserve
> + *
> + * @mm: Address space being charged (unused).
> + * @pages: Number of pages being charged (unused).
> + *
> + * cap_vm_enough_memory() lets CAP_SYS_ADMIN use the vm.admin_reserve_kbytes
> + * reserve, but calls cap_capable() directly, which skips hook_capable(). Check
> + * the same capability against the domain. commoncap runs first, so this only
> + * sees tasks that commoncap let use the reserve. Like commoncap, do not log
> + * denials: this only selects a memory limit.
> + *
> + * Return: 0 if the domain does not withhold the reserve, -EPERM if
> + * CAP_SYS_ADMIN use is denied. An error does not fail the allocation by
> + * itself, but without the reserve, the allocation may fail with -ENOMEM.
> + */
> +static int hook_vm_enough_memory(struct mm_struct *mm, long pages)
> +{
> + return check_cap(current_cred(), CAP_SYS_ADMIN, CAP_OPT_NOAUDIT);
> +}
> +
> static struct security_hook_list landlock_hooks[] __ro_after_init = {
> LSM_HOOK_INIT(capable, hook_capable),
> LSM_HOOK_INIT(capset, hook_capset),
> LSM_HOOK_INIT(mmap_addr, hook_mmap_addr),
> + LSM_HOOK_INIT(vm_enough_memory, hook_vm_enough_memory),
> };
>
> __init void landlock_add_cap_hooks(void)
> --
> 2.56.0
>
Reviewed-by: Günther Noack <gnoack@xxxxxxxxxx>