From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796056420; b=wDf8JBgZcoZk8d5freQ1ZAYfPJOWvl3sLOIWOH1R1vbtjhXIk07hAtUyO/HvPf4TADKQ ZZW8Std4hfdQRO1RHJ3oWsxyI/YtTnrradKauShYOw3gxLwgad+jP5scw1BYF7U23nRyx WSF1x/XM0MsA+8jsF8ijO5NCTNbawES3lP0wA8nGwzHQNGUw2399ZpiIeZT0PxwW2atPy bckM1LEZJJ10RjJL14oWnUDx2eagtSZt1LXmW5aR+4SVbv9oohG81wieZqpwBw1g6otJd zqp2vpSuKhtYi+VYT2FKQij9B3voKxwgVBkLn9mhdNfeofOapzUjrlUSQWj+oauqsFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796056420; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=GGfO0ZA8rL4h6k1idTbB3N6xt1dPa1dwOyHqV6jefKA=; b=n7omTHptcYU4FIkrb3cv/DVrPo5dvBHKy8PLHXu3ZpWVXujVys3JM4rkGbLUwRBa6kZo yA0jIXIhxM6tCfzI+jC85qmHDpZJfZnzEoW9fp4KEOfF+5+jHzpf9IumZG9RClWYJMe3w QGO5L8hfecOpaB4swuZhcb4Tk+Duw839vaa+9AeAaGCOLTgwWtz79yqP68o58KwSRI1Pn Q6L9BPcIrp6ilUw9dvk9xqaoy+NdD+xiYGOU7PTlcFa+ySZa40EeNQMybUbQCGj4VeMtR MKHSR1kO6jIsPvKRTFueFh3mBozel+suQHX3RTbYLQKNuYAwvDdknoQm4X+HpJVuQWA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=OAeKoA9C; arc=none smtp.remote-ip=209.85.210.171 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 416C1606A336 for ; Mon, 30 Nov 2026 16:33:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 416C1606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=OAeKoA9C Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8a0880a46e7so6337177b3a.1 for ; Mon, 30 Nov 2026 08:33:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796056418; x=1796661218; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=GGfO0ZA8rL4h6k1idTbB3N6xt1dPa1dwOyHqV6jefKA=; b=OAeKoA9CZqxGDEbmwIyMRzARH0biY/4ljk+WNO2jBIVJl4OQRU1W4nQ48HP9qCy0mu 8ClkPZ9OoFG5dmqbASbi7u0J0+RNwQiURLOuU4vLECANy12OueLxtSk1JAHosZvqSPqc L1YX+ql/U0yu6T4nYMryk/drqnn+yfv4rEQ4BmEJXzDBLGnFhr3KgknX9sNIPnrAP79I mwj6A84OM5AKmh2F+yjvU+Jxd1DGNN1anoXdgrK1KiDgyM++pE5NCbH8Ufb8czkhTJUY jo4yoi98dDdaFxY+vXcioWrc8k0KixLGrQINa4i4SPPElhRSBIDWSKw3Uyst4+Fg+kqy auvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796056418; x=1796661218; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GGfO0ZA8rL4h6k1idTbB3N6xt1dPa1dwOyHqV6jefKA=; b=dlbLL1YGJDTnHZ8JAUpv16BFYk3AYp6wK+Pe6HVGpBNSug+TaoIRon248QZ+b9Qo86 XLYb/LRQGN987g3h4Wsr5cfvU2dmJKM9PC4usaj4BQUFzW/ZJlaac+rCjyA2aefnaDyx CNHZMZwrQqdVMCqNwIG0v/xp6IS+Qbb7bHwepJnMraWq2Om4RUvPjcO64d+0/0LAKPZM Ru7+5Jbd/sArR15xq6csJlANeeMeysiH3puR7T2T+hwffxYOaLplPMVYBmulKeobQ3PT ncIpTGDtRu40ENSqFmYTcfB5EwgA99Vq+uOsAdJwjWI224u19e6xWTQIi/GynozwJbFb PdgQ== X-Forwarded-Encrypted: i=1; AL+hErEF1C8yWZTxPsLMJVVD9wyDukDJREBr4EOPiooL4lJggRbiOGz8vWKrjInfE41525Bx8kxp0Re6CnMtgxQ=@vger.kernel.org X-Gm-Message-State: APuU4Z/oVb7BLVwsK5KeI63F0Zp5UfRz8iHrJBOIX6gnHGSXBWOZvuKM ahafMLnmBzXFHeJ+3BqcZ/ooSXcnnL099h0nGJQ0hDztHlnkxLfkJ3i7 X-Gm-Gg: AZFdTZdFiWvODaV2c5Tc7rBHHe0HOX2agHfLlY7r2xGYc8KFtgchapuVS0kZEZCfZZ7 IaLuLghdh+PkgZ8tUL3x7FzRHCnRxZEKb4g+f2ZQpfizPt9pRvHK57AJWBAsAwLPqxcHxGhGNgi JPT3SUK/rXzjvTiERMeGQJTZ+zu5fF27zpcA9kucHhCQSZHotLxUG7YmBunk9dUHftK9/WdZWqX gO9cMU5wpAk6dHKaO0+hg1Da9E81JnGGV+2kauc4PA+/n9D0ULrs+Hu/FI3LqAJGIicEgvbJVPF U5w0cH1LtdKgl/DHSA/4Xz+NZZmyi0iH5SihQH19ecxWs+GI6yTguGSZZx/2Mzv9YBYOcCqnKQ1 NYlCggQRmBoNjsuWWR/iOGFTuJldLpipDLGE78zwdYWp1omkseVm0NUTLZ5XThuNY7baqYvl6Bs 9i7jT3FVl4yTjKj3zSLx7NABh+bqevTprUMg+GL+u4E157NSG14oF7M8EN9YjQ8UqlSk9SpYIe3 oSCVPP348nEIFCNMD0qVWDXhcu6xctcfwXn0LnUt5v54vQEx0PE0/F2JYB36oJNFWadJq09HkTb hhRTnhiTndzFYxi6pt/bAE3/wT9k0rKuyP5Ocd3XKUmM9FmQK3//xCA= X-Received: by 2002:a05:6a00:13a2:b0:8b6:ac2e:a340 with SMTP id d2e1a72fcca58-8c7dd306849mr25018737b3a.16.1796056418058; Mon, 30 Nov 2026 08:33:38 -0800 (PST) Received: from dl380p.taila420a2.ts.net ([240b:10:ff26:df00:7436:b499:3aa7:ccac]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c872e45756sm18201395b3a.27.2026.11.30.08.33.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 08:33:37 -0800 (PST) From: Yasuaki Torimaru 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 Message-ID: <20261130163321.2863669-1-yasuakitorimaru@gmail.com> X-Mailer: git-send-email 2.54.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 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 --- 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