mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yasuaki Torimaru <yasuakitorimaru@gmail.com>
To: steffen.klassert@secunet.com, herbert@gondor.apana.org.au
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, netdev@vger.kernel.org,
	ipsec@lists.linux.dev, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH ipsec] xfrm: account each XFRMA_MIGRATE attribute in xfrm_migrate_msgsize()
Date: Tue,  1 Dec 2026 01:33:21 +0900	[thread overview]
Message-ID: <20261130163321.2863669-1-yasuakitorimaru@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-30 16:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130163321.2863669-1-yasuakitorimaru@gmail.com \
    --to=yasuakitorimaru@gmail.com \
    --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=stable@vger.kernel.org \
    --cc=steffen.klassert@secunet.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®