mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrii Tereshchenko <opensource@andrii.net>
To: Steffen Klassert <steffen.klassert@secunet.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Willem de Bruijn <willemb@google.com>,
	netdev@vger.kernel.org, ipsec@lists.linux.dev,
	linux-kernel@vger.kernel.org, syzkaller@googlegroups.com
Subject: [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm
Date: Tue, 24 Nov 2026 21:38:35 -0800	[thread overview]
Message-ID: <928a3d63-eb17-4192-8952-a9a74a89c5f9@mid.andrii.net> (raw)

xfrm_state_ok() tests the template's 32-bit aalgos mask with
1 << x->props.aalgo, but digest_null's ID, SADB_X_AALG_NULL, is 251.
A digest_null SA checked against a template that does not allow all
algorithms then triggers a UBSAN shift-out-of-bounds report, which
gets in the way of running with CONFIG_UBSAN_SHIFT.

Fix this by treating an ID that does not fit in the mask as not set,
as aalg_tmpl_set() in af_key.c does.

Made with an AI coding assistant, reviewed by the submitter.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
Found with syzkaller (our own run, not syzbot).
Validated: reproduced on ipsec master with CONFIG_UBSAN_SHIFT, without
kernel changes or fault injection:

  UBSAN: shift-out-of-bounds in net/xfrm/xfrm_policy.c:3399:39
  shift exponent 251 is too large for 32-bit type 'int'

Tested: with the patch the reproducer runs without a report and the
packet is still refused (XfrmInTmplMismatch); with a template that
allows all algorithms it is still delivered.

No functional change is intended beyond the UBSAN report, and this is
not a security fix.

History, from the pre-git tree [1]:
- 6c55c29fabc2 ("[IPSEC]: Add transform engine and AH
  implementation.", 2002) wrote the test, against a single algos mask;
  4a618fbe0113 ("[NET]: IPSEC updates.", 2002) split it into aalgos.
- 3bab27b68626 ("[IPSEC]: Clean up key manager algorithm handling.",
  2003) added digest_null and SADB_X_AALG_NULL = 251, a KAME value
  (Jun-ichiro itojun Hagino, tech-net, 2000-07-16 [2]).
- af_key.c guarded its copy of the test in f398035f2dec ("[IPSEC]:
  Avoid undefined shift operation when testing algorithm ID", 2007).

Until now the result for ID 251 depended on the CPU: x86 and arm64
mask the shift count and test bit 27; 32-bit ARM, PowerPC and s390
give 0, which this patch makes the rule everywhere. ealgos is not
tested here, so SERPENT (252) and TWOFISH (253) don't hit this.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git
[2] https://mail-index.netbsd.org/tech-net/2000/07/16/0000.html

 net/xfrm/xfrm_policy.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index d2e938d4db9aa..b980d915a24a6 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -3396,7 +3396,9 @@ xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
 		(x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
 		(x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
 		x->props.mode == tmpl->mode &&
-		(tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
+		(tmpl->allalgs ||
+		 (x->props.aalgo < BITS_PER_TYPE(tmpl->aalgos) &&
+		  (tmpl->aalgos & (1U << x->props.aalgo))) ||
 		 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
 		!(x->props.mode != XFRM_MODE_TRANSPORT &&
 		  xfrm_state_addr_cmp(tmpl, x, family)) &&

base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
-- 
2.53.0


             reply	other threads:[~2026-11-25  5:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  5:38 Andrii Tereshchenko [this message]
2026-11-29  5:57 ` netdev-bot+sashiko

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=928a3d63-eb17-4192-8952-a9a74a89c5f9@mid.andrii.net \
    --to=opensource@andrii.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    --cc=syzkaller@googlegroups.com \
    --cc=willemb@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®