* [PATCH ipsec] xfrm/compat: pass on the translated length of a socket policy
@ 2026-11-24 3:29 Andrii Tereshchenko
2026-11-25 20:04 ` Andrii Tereshchenko
0 siblings, 1 reply; 2+ messages in thread
From: Andrii Tereshchenko @ 2026-11-24 3:29 UTC (permalink / raw)
To: Steffen Klassert, Herbert Xu, David S. Miller
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Dmitry Safonov, Sabrina Dubroca, netdev, ipsec, linux-kernel,
stable
xfrm_user_policy() compiles a 32-bit task's translated socket policy
with the untranslated length, 4 bytes short, so the policy loses its
last template: an ESP-required socket policy then sends in clear
text. Have the translator return the translated length.
Made with an AI coding assistant, reviewed by the submitter.
Fixes: 96392ee5a13b ("xfrm/compat: Translate 32-bit user_policy from sockptr")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
The 64-bit struct xfrm_userpolicy_info has 4 bytes of tail padding
that the 32-bit one lacks (168 and 164 bytes on x86-64), and
xfrm_compile_policy() counts the templates as (len - sizeof(*p)) /
sizeof(*ut). With the 32-bit length one template counts as none, so a
32-bit program's ESP-required socket policy requires nothing: its
datagrams leave in clear text, with no SA and no acquire. For the same
reason a policy without templates, which IKE daemons set on their own
sockets, is refused.
The BUILD_BUG_ON()s check what the translator's memcpy()s rely on:
4 = sizeof(struct xfrm_userpolicy_info) -
sizeof(struct compat_xfrm_userpolicy_info), the fields before the
padding at the same offsets in both layouts, and templates that need
no 8-byte alignment.
Found with an AI coding assistant. It reproduces on an unmodified
kernel (ipsec master), without kernel changes or fault injection, and
no longer with this change. Tested with the reproducer, a selftest,
two boots each way, and with the xfrm_policy and xfrm_state
selftests. The reproducer is available on request; the selftest
follows once stable has the fix.
include/net/xfrm.h | 2 +-
net/xfrm/xfrm_compat.c | 18 ++++++++++++++----
net/xfrm/xfrm_state.c | 2 +-
3 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 445223a8dc6f1..455b9f986ea5b 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -2331,7 +2331,7 @@ struct xfrm_translator {
struct netlink_ext_ack *extack);
/* Translate 32-bit user_policy from sockptr */
- int (*xlate_user_policy_sockptr)(u8 **pdata32, int optlen);
+ int (*xlate_user_policy_sockptr)(u8 **pdata32, int *optlen);
struct module *owner;
};
diff --git a/net/xfrm/xfrm_compat.c b/net/xfrm/xfrm_compat.c
index 670e3512fc090..af5cb36d691ea 100644
--- a/net/xfrm/xfrm_compat.c
+++ b/net/xfrm/xfrm_compat.c
@@ -642,16 +642,25 @@ static struct nlmsghdr *xfrm_user_rcv_msg_compat(const struct nlmsghdr *h32,
return h64;
}
-static int xfrm_user_policy_compat(u8 **pdata32, int optlen)
+static int xfrm_user_policy_compat(u8 **pdata32, int *optlen)
{
struct compat_xfrm_userpolicy_info *p = (void *)*pdata32;
u8 *src_templates, *dst_templates;
u8 *data64;
- if (optlen < sizeof(*p))
+ /* The 64-bit layout only adds tail padding, and the templates
+ * after it are the same in both layouts.
+ */
+ BUILD_BUG_ON(sizeof(struct xfrm_userpolicy_info) -
+ sizeof(struct compat_xfrm_userpolicy_info) != 4);
+ BUILD_BUG_ON(offsetof(struct xfrm_userpolicy_info, share) !=
+ offsetof(struct compat_xfrm_userpolicy_info, share));
+ BUILD_BUG_ON(__alignof__(struct xfrm_user_tmpl) > 4);
+
+ if (*optlen < sizeof(*p))
return -EINVAL;
- data64 = kmalloc_track_caller(optlen + 4, GFP_USER | __GFP_NOWARN);
+ data64 = kmalloc_track_caller(*optlen + 4, GFP_USER | __GFP_NOWARN);
if (!data64)
return -ENOMEM;
@@ -660,10 +669,11 @@ static int xfrm_user_policy_compat(u8 **pdata32, int optlen)
src_templates = *pdata32 + sizeof(*p);
dst_templates = data64 + sizeof(*p) + 4;
- memcpy(dst_templates, src_templates, optlen - sizeof(*p));
+ memcpy(dst_templates, src_templates, *optlen - sizeof(*p));
kfree(*pdata32);
*pdata32 = data64;
+ *optlen += 4;
return 0;
}
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index f4a4ddaeb9b37..eb984ed118884 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -3115,7 +3115,7 @@ int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
return -EOPNOTSUPP;
}
- err = xtr->xlate_user_policy_sockptr(&data, optlen);
+ err = xtr->xlate_user_policy_sockptr(&data, &optlen);
xfrm_put_translator(xtr);
if (err) {
kfree(data);
base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH ipsec] xfrm/compat: pass on the translated length of a socket policy
2026-11-24 3:29 [PATCH ipsec] xfrm/compat: pass on the translated length of a socket policy Andrii Tereshchenko
@ 2026-11-25 20:04 ` Andrii Tereshchenko
0 siblings, 0 replies; 2+ messages in thread
From: Andrii Tereshchenko @ 2026-11-25 20:04 UTC (permalink / raw)
To: Steffen Klassert, Herbert Xu, David S. Miller
Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Willem de Bruijn, Dmitry Safonov, Sabrina Dubroca, netdev, ipsec,
linux-kernel, stable
The netdev AI review [1] raised two older issues, outside this patch.
Adding Willem de Bruijn, whom I missed in the Cc.
Sashiko (netdev AI review) wrote:
> This is a pre-existing issue and was not introduced by this patch. Does
> this translation also run for x32 tasks, whose layout already matches the
> 64-bit one?
[...]
> Would compat_need_64bit_alignment_fixup() be the better gate here? On x86
> it is in_ia32_syscall, and net/ethtool/ioctl.c already uses it.
Yes, confirmed on ipsec master with X86_X32_ABI=y: x32 requests are
translated as i386 ones, so their SA and socket policy requests are
refused and a NEWPOLICY loses its template, leaving its traffic in
clear text. Gating on compat_need_64bit_alignment_fixup() fixes it; the
fix will come with the other xfrm compat fixes. x32 readers of dumps
and notifications still get the i386 layout, which needs a separate
af_netlink change.
Sashiko (netdev AI review) wrote:
> This is a pre-existing issue, but should this translation be limited to
> IP_XFRM_POLICY and IPV6_XFRM_POLICY?
[...]
> As a result, a compat task's PF_KEY socket policy shorter than 164 bytes
> is refused before any key manager sees it:
[...]
> The gap came in with 96392ee5a13b. That commit replaced the
> in_compat_syscall() -EOPNOTSUPP refusal for all optnames with the
> xfrm-format translator. Could an optname check be added to this fix?
Yes, it should. I have that fix ready as a separate patch, which runs
the translator only for IP_XFRM_POLICY and IPV6_XFRM_POLICY, and will
post it with the other xfrm compat fixes rather than fold it in here,
as it is a different bug.
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/5249741d-fa84-4d54-bf77-45f48dbbb169%40mid.andrii.net
Andrii Tereshchenko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 20:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 3:29 [PATCH ipsec] xfrm/compat: pass on the translated length of a socket policy Andrii Tereshchenko
2026-11-25 20:04 ` 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®