selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len
Stephen Smalley
maintainerPATCH 1/1 selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len
We should count the terminating NUL byte as part of the ctx_len. Otherwise, UBSAN logs a warning:
UBSAN: array-index-out-of-bounds in security/selinux/xfrm.c:99:14 index 60 is out of range for type 'char [*]'
The allocation itself is correct so there is no actual out of bounds indexing, just a warning.
--- security/selinux/xfrm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/xfrm.c +1 -1
@@ -94,7 +94,7 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp, ctx->ctx_doi =XFRM_SC_DOI_LSM ; ctx->ctx_alg =XFRM_SC_ALG_SELINUX ; - ctx->ctx_len = str_len; + ctx->ctx_len = str_len +1 ;memcpy (ctx->ctx_str, &uctx[1 ], str_len); ctx->ctx_str[str_len] ='\0' ; rc = security_context_to_sid(ctx->ctx_str, str_len,
Signature · 1 line
2.49.0
Quoted 8 lines · Stephen Smalley
We should count the terminating NUL byte as part of the
ctx_len. Otherwise, UBSAN logs a warning:UBSAN: array-index-out-of-bounds in security/selinux/xfrm.c:99:14 index 60 is out of range for type 'char [*]'The allocation itself is correct so there is no actual out of bounds indexing, just a warning.
Seems reasonable.
Should also fix not copying the NUL terminator in selinux_xfrm_policy_clone():
new_ctx = kmemdup(old_ctx, sizeof(*old_ctx) + old_ctx->ctx_len,
GFP_ATOMIC);Quoted 23 lines
Suggested-by Christian Göttsche <[email protected]>Link https://lore.kernel.org/selinux/CAEjxPJ6tA5+LxsGfOJokzdPeRomBHjKLBVR6zbrg+_w3ZZbM3A@mail.gmail.com/ open hereSigned-off-by Stephen Smalley <[email protected]>--- security/selinux/xfrm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)security/selinux/xfrm.c +1
-2@@ -94,7 +94,7 @@ static int selinux_xfrm_alloc_user(struct xfrm_sec_ctx **ctxp, ctx->ctx_doi =XFRM_SC_DOI_LSM ; ctx->ctx_alg =XFRM_SC_ALG_SELINUX ; - ctx->ctx_len = str_len; + ctx->ctx_len = str_len +1 ;memcpy (ctx->ctx_str, &uctx[1 ], str_len); ctx->ctx_str[str_len] ='\0' ; rc = security_context_to_sid(ctx->ctx_str, str_len, --2.49.0
Paul Moore
maintainerQuoted 9 lines · On Jun 13, 2025 Stephen Smalley <[email protected]> wrote:
We should count the terminating NUL byte as part of the
ctx_len. Otherwise, UBSAN logs a warning:UBSAN: array-index-out-of-bounds in security/selinux/xfrm.c:99:14 index 60 is out of range for type 'char [*]'The allocation itself is correct so there is no actual out of bounds indexing, just a warning.
Quoted 6 lines
Suggested-by Christian Göttsche <[email protected]>Link https://lore.kernel.org/selinux/CAEjxPJ6tA5+LxsGfOJokzdPeRomBHjKLBVR6zbrg+_w3ZZbM3A@mail.gmail.com/ open hereSigned-off-by Stephen Smalley <[email protected]>--- security/selinux/xfrm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
This looks good to me, thanks everyone.
I'm going to mark this for stable and merge it into selinux/stable-6.16, simply to help squash any UBSAN testing done on older kernels. If anyone has any concerns over that and would rather see it merged into selinux/dev, please send a comment to the list.
--
paul-moore.com