selinux: reject a 65535-byte xfrm context that ctx_len cannot hold
Subsystems (2)
PATCH 1/1 selinux: reject a 65535-byte xfrm context that ctx_len cannot hold
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.
--- security/selinux/xfrm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
security/selinux/xfrm.c +2 -1
@@ -85,7 +85,8 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp,return -EINVAL ; str_len = uctx->ctx_len; -if (str_len >=PAGE_SIZE ) +/* ctx->ctx_len below includes the NUL and has to fit its type. */ +if (str_len >=PAGE_SIZE || str_len >=type_max (ctx->ctx_len))return -ENOMEM ; ctx =kmalloc_flex (*ctx, ctx_str, str_len +1 , gfp);
---
base-commit: 347b0c5bacbe94bc99f7683e3c9269c77a0b9c92
change-id: 20261124-dev-32673cc83656
Best regards,
Signature · 1 line
Arina Kharlamova <[email protected]>
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
From: Arina Kharlamova <[email protected]>
Commit
86c8db86af43("selinux: fixselinux_xfrm_alloc_user()to set correctctx_len") madectx_lencount the terminating NUL.ctx_lenis a__u16andselinux_xfrm_alloc_user()only rejectsstr_len>=PAGE_SIZE. With 64K pages,str_len= 65535 passes, andctx_len=str_len+ 1 wraps to 0 (kmalloc_flex()stores the same truncated value first).ctx_str[] is
__counted_by(ctx_len), so the followingmemcpy()ofstr_lenbytes is checked against a buffer of size 0. WithCONFIG_FORTIFY_SOURCEthis ends in__fortify_panic():memcpy: detected buffer overflow: 65535 byte write of buffer size 0 kernel BUG at
lib/string_helpers.c:1044!
...
[ ... ]
exercised.
Quoted 3 lines
Fixes 86c8db86af43 ("selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len")Signed-off-by Arina Kharlamova <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Paul Moore
maintainerQuoted 35 lines · On Nov 23, 2026 Arina Kharlamova <[email protected]> wrote:
Commit
86c8db86af43("selinux: fixselinux_xfrm_alloc_user()to set correctctx_len") madectx_lencount the terminating NUL.ctx_lenis a__u16andselinux_xfrm_alloc_user()only rejectsstr_len>=PAGE_SIZE. With 64K pages,str_len= 65535 passes, andctx_len=str_len+ 1 wraps to 0 (kmalloc_flex()stores the same truncated value first).ctx_str[] is
__counted_by(ctx_len), so the followingmemcpy()ofstr_lenbytes is checked against a buffer of size 0. WithCONFIG_FORTIFY_SOURCEthis 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/0x6cThe memory written stays inside the allocation (65536 bytes for the string); without
FORTIFY_SOURCEthe context is stored withctx_len0.A user context of 65535 bytes can reach this hook through
PF_KEY(af_key.conly limitssadb_x_ctx_lentoPAGE_SIZE); the xfrm netlink path cannot, because thereuctx->len(a__u16) includes the header.Reject a
str_lenthat does not leave room for the NUL inctx_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 withARM64_64K_PAGES,FORTIFY_SOURCEand SELinux, under QEMU (GCC 15.2.0), with a KUnit case that passes a 65535-byte context tosecurity_xfrm_state_alloc(): the unpatched kernel hits the BUG above, the patched one returns-ENOMEM. ThePF_KEYpath itself was not exercised.
Quoted 6 lines
Fixes 86c8db86af43 ("selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len")Signed-off-by Arina Kharlamova <[email protected]>--- security/selinux/xfrm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Thanks, merged into selinux/stable-7.4.
--
paul-moore.com