Skip to contentSkip to conversation

selinux: fix selinux_xfrm_alloc_user() to set correct ctx_len

PATCH In review Stephen Smalley 3 participants 3 messages Jun 13, 2025 – Jun 16, 2025 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 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
index 90ec4ef1b082..61d56b0c2be1 100644
@@ -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
#1 lore raw
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
---
 security/selinux/xfrm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/xfrm.c +1 -2
index 90ec4ef1b082..61d56b0c2be1 100644
@@ -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

maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 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
---
 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