From: Paul Moore <paul@paul-moore.com>
To: "Arina Kharlamova" <arina.kharlamova@mbzuai.ac.ae>,
"Stephen Smalley" <stephen.smalley.work@gmail.com>,
"Ondrej Mosnáček" <omosnacek@gmail.com>,
"Kees Cook" <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: selinux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org, stable@vger.kernel.org,
Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
Subject: Re: [PATCH] selinux: reject a 65535-byte xfrm context that ctx_len cannot hold
Date: Mon, 23 Nov 2026 21:19:43 -0500 [thread overview]
Message-ID: <04e583c2e9045e6a8990d752a1a2d1eb@paul-moore.com> (raw)
In-Reply-To: <20261124-dev-v1-1-00b99ee684ec@mbzuai.ac.ae>
On Nov 23, 2026 Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae> wrote:
>
> Commit 86c8db86af43 ("selinux: fix selinux_xfrm_alloc_user() to set
> correct ctx_len") made ctx_len count the terminating NUL. ctx_len is a
> __u16 and selinux_xfrm_alloc_user() only rejects str_len >= PAGE_SIZE.
> With 64K pages, str_len = 65535 passes, and ctx_len = str_len + 1 wraps
> to 0 (kmalloc_flex() stores the same truncated value first).
>
> ctx_str[] is __counted_by(ctx_len), so the following memcpy() of
> str_len bytes is checked against a buffer of size 0. With
> CONFIG_FORTIFY_SOURCE this ends in __fortify_panic():
>
> memcpy: detected buffer overflow: 65535 byte write of buffer size 0
> kernel BUG at lib/string_helpers.c:1044!
> ...
> selinux_xfrm_state_alloc+0x18/0x24
> security_xfrm_state_alloc+0x3c/0x6c
>
> The memory written stays inside the allocation (65536 bytes for the
> string); without FORTIFY_SOURCE the context is stored with ctx_len 0.
>
> A user context of 65535 bytes can reach this hook through PF_KEY
> (af_key.c only limits sadb_x_ctx_len to PAGE_SIZE); the xfrm netlink
> path cannot, because there uctx->len (a __u16) includes the header.
>
> Reject a str_len that does not leave room for the NUL in ctx_len.
>
> Found while surveying callers of kmalloc_flex() whose __counted_by()
> counter is narrower than the count, with the help of an AI coding
> assistant (Claude); I reviewed the analysis and the fix. Tested on
> v7.4-rc2, arm64 with ARM64_64K_PAGES, FORTIFY_SOURCE and SELinux, under
> QEMU (GCC 15.2.0), with a KUnit case that passes a 65535-byte context to
> security_xfrm_state_alloc(): the unpatched kernel hits the BUG above,
> the patched one returns -ENOMEM. The PF_KEY path itself was not
> exercised.
>
> Fixes: 86c8db86af43 ("selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len")
> Cc: stable@vger.kernel.org
> Signed-off-by: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
> ---
> security/selinux/xfrm.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
Thanks, merged into selinux/stable-7.4.
--
paul-moore.com
prev parent reply other threads:[~2026-11-24 2:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 23:37 [PATCH] selinux: reject a 65535-byte xfrm context that ctx_len cannot hold Arina Kharlamova via B4 Relay
2026-11-23 23:37 ` Arina Kharlamova
2026-11-23 23:45 ` sashiko-bot
2026-11-24 2:19 ` Paul Moore [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=04e583c2e9045e6a8990d752a1a2d1eb@paul-moore.com \
--to=paul@paul-moore.com \
--cc=arina.kharlamova@mbzuai.ac.ae \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=omosnacek@gmail.com \
--cc=selinux@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=stephen.smalley.work@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.