Netdev List
 help / color / mirror / Atom feed
* [PATCH ipsec] xfrm: account each XFRMA_MIGRATE attribute in xfrm_migrate_msgsize()
@ 2026-11-30 16:33 Yasuaki Torimaru
  0 siblings, 0 replies; only message in thread
From: Yasuaki Torimaru @ 2026-11-30 16:33 UTC (permalink / raw)
  To: steffen.klassert, herbert
  Cc: davem, edumazet, kuba, pabeni, horms, netdev, ipsec, linux-kernel,
	stable

xfrm_migrate_msgsize() sizes the migrate entries as a single
XFRMA_MIGRATE attribute holding num_migrate structures, but
build_migrate() emits one XFRMA_MIGRATE attribute per entry through
copy_to_user_migrate().  The estimate is short by one attribute header
for every entry after the first: 4 * (num_migrate - 1) bytes, up to 20
bytes for XFRM_MAX_DEPTH entries.

The slack in the skb head usually hides this.  When the skb gets less
than 4 * (num_migrate - 1) bytes of tailroom beyond the requested size,
the last nla_put() in build_migrate() fails with -EMSGSIZE and
xfrm_send_migrate() hits BUG_ON(err < 0).

On v7.4-rc4 with x86_64_defconfig plus CONFIG_XFRM_MIGRATE=y (and
CONFIG_XFRM_SUB_POLICY not set), an XFRM_MSG_MIGRATE request with three
entries, XFRMA_KMADDRESS and XFRMA_ENCAP asks for 384 bytes, gets 384
bytes of tailroom and writes 392:

  kernel BUG at net/xfrm/xfrm_user.c:3670!
  Oops: invalid opcode: 0000 [#1] SMP PTI
  CPU: 0 UID: 1000 PID: 71 Comm: migrate_poc Not tainted 7.4.0-rc4 #4 PREEMPT(lazy)
  RIP: 0010:xfrm_send_migrate+0x382/0x3b0
  Call Trace:
   <TASK>
   km_migrate+0x90/0xe0
   xfrm_migrate+0x472/0x790
   xfrm_do_migrate+0x29b/0x380
   xfrm_user_rcv_msg+0x19d/0x3d0
   netlink_rcv_skb+0x57/0x110
   xfrm_netlink_rcv+0x2c/0x40
   netlink_unicast+0x278/0x3a0
   netlink_sendmsg+0x21d/0x460

The request needs CAP_NET_ADMIN over the network namespace only; the
trace above is from uid 1000 after unshare(CLONE_NEWUSER | CLONE_NEWNET).
The caller first adds a policy with one transport mode ESP template; no
state is required.  SADB_X_MIGRATE on PF_KEY ends up in the same
xfrm_send_migrate() through km_migrate(), without an encap template.

Whether the BUG_ON() is reached depends on the tailroom that
nlmsg_new() actually gets, which follows the cache line size,
NET_SKB_PAD, sizeof(struct skb_shared_info) and MAX_HEADER
(CONFIG_WLAN, CONFIG_MAC80211_MESH, CONFIG_HYPERV_NET=y and the IP
tunnel options), and on CONFIG_XFRM_SUB_POLICY, which adds
XFRMA_POLICY_TYPE.  On x86_64 the BUG triggers with three entries,
XFRMA_KMADDRESS and XFRMA_ENCAP when MAX_HEADER is 144 and
CONFIG_XFRM_SUB_POLICY is not set, and with two entries,
XFRMA_KMADDRESS and XFRMA_ENCAP when MAX_HEADER is 80 and
CONFIG_XFRM_SUB_POLICY=y; the x86_64 distribution kernels tried do not
hit it.  The unmodified distribution kernels Fedora 45
7.3.6-300.fc45.ppc64le, Fedora 45 7.3.6-300.fc45.s390x and AlmaLinux 9
5.14.0-741.27.1.el9_9.ppc64le hit it from an unprivileged user
namespace, with five entries and XFRMA_ENCAP (Fedora ppc64le, s390x)
or six entries, XFRMA_KMADDRESS and XFRMA_ENCAP (Fedora and AlmaLinux
ppc64le).  AlmaLinux 9 sets CONFIG_PANIC_ON_OOPS=y and panics.

Count each entry as its own attribute.

This was found by an AI code review agent (Claude Code,
claude-mythos-5-2) while reviewing the BUG_ON(err < 0) sites in
xfrm_user.c.  It was not hit on a production
system, and there is no syzbot report.  It was confirmed in QEMU with a
userspace reproducer that adds the policy and sends XFRM_MSG_MIGRATE;
reproducing it needs no kernel modification or error injection.  With
the fix applied on x86_64, all 24 combinations of 1..6 entries with and
without XFRMA_KMADDRESS and XFRMA_ENCAP succeed with and without
CONFIG_XFRM_SUB_POLICY.

Fixes: 5c79de6e79cd ("[XFRM]: User interface for handling XFRM_MSG_MIGRATE")
Cc: stable@vger.kernel.org
Signed-off-by: Yasuaki Torimaru <yasuakitorimaru@gmail.com>
---
Testing, all in QEMU (the reproducer is available on request):

- v7.4-rc4 (4f069ab1ec24), x86_64_defconfig + XFRM_MIGRATE, without the
  patch: BUG as above, as root and as uid 1000 in a new user namespace.
  The same boot runs a one-entry request first, which succeeds.
- Same kernel with the patch: all 24 combinations succeed as root, and
  the three-entry case succeeds from the user namespace.
- Same kernel with XFRM_SUB_POLICY=y: all 24 combinations succeed.  A
  debug printk showed 384 bytes of tailroom for requests up to 384
  bytes and 704 above that.
- XFRM_SUB_POLICY=y with CONFIG_WLAN not set: BUG with two entries,
  XFRMA_KMADDRESS and XFRMA_ENCAP, as root and as uid 1000; with the
  patch, all 24 combinations and the uid 1000 cases succeed.
- Unmodified distribution kernels with their own xfrm_user, 24
  combinations each: Fedora 45 7.3.6-300.fc45.ppc64le hits the BUG with
  (5 entries, ENCAP) and (6 entries, KMADDRESS, ENCAP); Fedora 45
  7.3.6-300.fc45.s390x with (5 entries, ENCAP); AlmaLinux 9
  5.14.0-741.27.1.el9_9.ppc64le with (6 entries, KMADDRESS, ENCAP),
  followed by a panic.  Each also from uid 1000 in a new user namespace.
  The x86_64 kernels Debian 7.3.6+deb14, Arch 7.3.6-arch1-1, openSUSE
  Tumbleweed 7.3.5-2-default, Fedora 7.3.6-300.fc45, AlmaLinux
  5.14.0-741.27.1.el9_9 and 6.12.0-264.21.1.el10_3 did not hit it.
  Ubuntu 7.0.0-44 and 6.8.0-151 have XFRM_MIGRATE disabled.
- PF_KEY SADB_X_MIGRATE on the Fedora ppc64le kernel: 12 combinations
  without BUG, each delivering an XFRM_MSG_MIGRATE without encap.

Not tested: the patch on ppc64le and s390x, stable trees, and the
ppc64el and s390x kernels of Debian, openSUSE and Ubuntu.

checkpatch.pl is clean.

Tool use: Claude Code with claude-mythos-5-2 found the problem and wrote
the reproducer, the test scripts, this change and the first draft of
this changelog; claude-opus-5-5 revised the changelog after review.  I
reviewed the change and the results.

 net/xfrm/xfrm_user.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 217f65faa2bd3..8e8c162c94858 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -3603,7 +3603,7 @@ static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
-	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
+	      + nla_total_size(sizeof(struct xfrm_user_migrate)) * num_migrate
 	      + userpolicy_type_attrsize();
 }
 

base-commit: 1213df0bc5993e479706c5eba5830243d778f164
-- 
2.54.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 16:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:33 [PATCH ipsec] xfrm: account each XFRMA_MIGRATE attribute in xfrm_migrate_msgsize() Yasuaki Torimaru

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox