mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm
@ 2026-11-25  5:38 Andrii Tereshchenko
  2026-11-29  5:57 ` netdev-bot+sashiko
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-25  5:38 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Willem de Bruijn, netdev, ipsec, linux-kernel, syzkaller

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm
  2026-11-25  5:38 [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm Andrii Tereshchenko
@ 2026-11-29  5:57 ` netdev-bot+sashiko
  2026-11-30  2:05   ` Andrii Tereshchenko
  0 siblings, 1 reply; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29  5:57 UTC (permalink / raw)
  To: opensource
  Cc: steffen.klassert, herbert, davem, edumazet, kuba, pabeni, horms,
	willemb, netdev, ipsec, linux-kernel, syzkaller

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] On x86 and arm64, this patch changes which inbound digest_null
  SAs pass the policy template check, and the commit message does not…

--

xfrm: policy: avoid an undefined shift when testing the auth algorithm

This commit adds a range check to xfrm_state_ok() before the template's
32-bit aalgos mask is shifted by x->props.aalgo. With the check,
digest_null's ID (SADB_X_AALG_NULL, 251) no longer triggers a UBSAN
shift-out-of-bounds report. An auth algorithm ID that does not fit in the
mask is now treated as not set.

> 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))) ||

[Severity: Medium]
On x86 and arm64, does this change which inbound digest_null SAs pass the
template check?

Take a template installed through XFRM_MSG_NEWPOLICY with aalgos set to
0xffffffff and a restricted ealgos or calgos. copy_templates() in
net/xfrm/xfrm_user.c sets allalgs only when all three masks are ~0:

    t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);

In that case allalgs is 0. For AH/ESP/COMP the aalgos clause then decides
the result.

Before this patch, 1<<251 was a hardware shift. x86 SHL and arm64 LSL take
the count modulo 32, so the code tested bit 27. That bit is set in ~0, so
the digest_null SA matched.

With the new guard, x->props.aalgo < BITS_PER_TYPE(tmpl->aalgos) is false.
The clause is then false even though the template allows every auth
algorithm. xfrm_policy_ok() finds no matching template, and the inbound
packets are dropped as XfrmInTmplMismatch:

xfrm_policy_ok() {
    ...
	if (xfrm_state_ok(tmpl, sp->xvec[idx], family, if_id))
		return ++idx;
    ...
}

The output path doesn't look at aalgos or allalgs. The same policy would
still send with the digest_null SA but refuse its inbound packets.

After this change, no aalgos value can admit digest_null unless all three
masks are ~0. Could tmpl->aalgos == ~0U also be treated as allowing any
aalgo? That would still avoid the out-of-range shift and would keep the
"all auth algorithms" meaning.

The commit message says only that an out-of-range ID is treated "as not
set". The note about this behaviour change sits below the --- line, so it
won't reach git history:

  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.

The same section also says:

  No functional change is intended beyond the UBSAN report

That doesn't match the x86/arm64 behaviour described above. Since the
Fixes: tag makes an automatic stable backport likely, could the commit
message itself state the behaviour change, whichever semantic is chosen?

>  		 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
>  		!(x->props.mode != XFRM_MODE_TRANSPORT &&
>  		  xfrm_state_addr_cmp(tmpl, x, family)) &&

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/928a3d63-eb17-4192-8952-a9a74a89c5f9%40mid.andrii.net

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm
  2026-11-29  5:57 ` netdev-bot+sashiko
@ 2026-11-30  2:05   ` Andrii Tereshchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-30  2:05 UTC (permalink / raw)
  To: netdev-bot+sashiko
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Willem de Bruijn,
	netdev, ipsec, linux-kernel, syzkaller

Sashiko (netdev AI review) wrote:
> On x86 and arm64, does this change which inbound digest_null SAs pass the
> template check?
[...]
> Could tmpl->aalgos == ~0U also be treated as allowing any aalgo? That would
> still avoid the out-of-range shift and would keep the "all auth algorithms"
> meaning.

Yes, it does, in one case, and "no functional change" below the --- was
wrong. The test runs only when the template does not allow all
algorithms (allalgs is 0). For a digest_null SA (SADB_X_AALG_NULL,
251), the old code tests bit 27 of aalgos on x86, where the compiled
check is a bt on a 32-bit register (bit number modulo 32, and
251 = 7 * 32 + 27), and on arm64, where the shift count is also taken
modulo 32. On 32-bit ARM, PowerPC and s390 the shift gives 0. So on x86
and arm64 a template installed with XFRM_MSG_NEWPOLICY, or as a socket
policy, with bit 27 of aalgos set (in practice aalgos = ~0) and a
restricted ealgos or calgos accepted inbound digest_null SAs, and with
this patch it refuses them. Templates from iproute2 and PF_KEY have all
three masks at ~0 (allalgs) and do not reach the shift, before or
after.

I would rather not give ~0 a meaning of its own, though:

- aalgos is a mask of algorithm IDs, bit n for ID n, and nothing in the
  UAPI or the code says ~0 means more than IDs 0 to 31. The one "any
  algorithm" the code defines is allalgs, set in copy_templates() when
  all three masks are ~0.
- digest_null was admitted by an out-of-range shift, so by bit 27,
  which names no algorithm, and only on some architectures. A template
  with any other mask that has bit 27 set admitted it too.
- af_key.c has treated an ID outside the mask as not set since
  f398035f2dec ("[IPSEC]: Avoid undefined shift operation when testing
  algorithm ID") in 2007, and 32-bit ARM, PowerPC and s390 have always
  refused it here. The patch makes x86 and arm64 do the same.
- digest_null gives no integrity protection. When a template restricts
  the algorithms, refusing an SA that no bit of the mask names is the
  safer reading; a policy that wants any algorithm, digest_null
  included, sets all three masks to ~0.

Steffen, if you want the behaviour change in the git history, I'll send
a v2 with the same code and the note moved above the --- in place of
"no functional change".

Andrii Tereshchenko

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-30  2:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25  5:38 [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm Andrii Tereshchenko
2026-11-29  5:57 ` netdev-bot+sashiko
2026-11-30  2:05   ` Andrii Tereshchenko

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®