Re: [PATCH ipsec] xfrm: policy: avoid an undefined shift when testing the auth algorithm

From: Andrii Tereshchenko

Date: Sun Nov 29 2026 - 21:05:43 EST


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