From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796056421; b=Qm5zkd+aPx0/unk9dlfYeaBbCkkA7JQU35zBK77K2q9w3Ap8/WlgE52NkwX3/biA3AB4 YV3UusBuIcLqlJBURMc0fY0R5qwNmyfrD8ZTJo2AE1KaCMhXRMDvsIGahRubnxXeVuf00 RuW5D0yJzNxKU4dQ1E1derMDEzRq7ZewNjlNXGZ3DDPaAE1OO5o5Wa3pvFWn2R97iD93X L6vh2M7iIwgOeNjQMBUpbUwGbz/wKaqer/Phvizk70ctXLgfuBVYDZTcJjjEeO//CcxNr OT4OVJn3Bbjnu/wWKbP8vyznmNEDG81fT7Ky7Gg6sxBQLmg0UtBJLuM0IensEnyKifQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796056421; 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=TPYGCi61+Xre17D0OEQ0oKyAD/z/DwBD/S53pRj/1bz70PI3Gcd09pK0SjIYUvgSzOTW JvKih/cGoPdZtN89CYrft3VEYoqsagxRs2lzEj29WWPTuQdbP73uIB7xN8qUERLgDJAoL fx7p6aEUGzHy4isMf2jTWN/C9mJr5kmEJbJge8IL1Dds1zKNaP9leTaiJTRGyNY3LcFQn 5nYgQfyZKy6d0CrSfQ9n84j+G0C/G+cpQkblx4KT0q/l/Zn9KT6U3223rh2Q2m8a/ecG8 v7DbiunFXFpplEcjrCdsDLlDO3asJ5jK4Jz2y1y5m6u4C/ey8Q/7bbsMkLpFvnk4tsg== 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.173 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 3B3C4606A332 for ; Mon, 30 Nov 2026 16:33:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3B3C4606A332 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-f173.google.com with SMTP id d2e1a72fcca58-89d6bd67045so4919381b3a.0 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=FqEK/lkkBWFW/I/DzO/9Rz6Abb6XP9Y6t1Wa2o+J0erHRllxUH9pcnnj6c+EPBSI+4 AByYZ96eKi40tkytYJaLeu+tcWe6enjFK/YjQ+aFfpY971Zyndud+ENdbHO4YmTFmBYX A/u8Na755TeUmLWtFlBhMi+Q/AuR1rPPqK1WzC7LADljx8izkR2JOw1w/Uul7WkQwDPU eBWZvEKXpjccfVs47aCkwGzBF2bw1BsXCRDf1kr6LgxKR9gralg6rCguQiD5qaQj+gaK hFxT+Z3UVn8EPL2AS7M7OP1o+irEnJL1HRv0MzU505mHGtJzHqymu3AIyP3/L1yF6z6c XVEg== X-Forwarded-Encrypted: i=1; AL+hErGgB8tU9ZFKp3L806VbXbS+JfJTHTUAbPiBLRwYs4hh0OEW89p8jVOwiksOVRLxDzjv10vG09Q=@vger.kernel.org X-Gm-Message-State: APuU4Z+TuGUMAxuBeAi6z3so0yDbdEoGqpfo376mjUOjRUBEtmc8F7vY aJ3LfcQ/ouTIs7/YIdBvD5Yji2LTtbPmmKZZ+u193DZ5Vvy9Cemgx8Vr X-Gm-Gg: AZFdTZecOT/u0kOoBBMq3utcpi7D3wXkConMafBEP1FkAlDu4zSIpnvx07ZG0maAp+a CrfqK8iWcq2BrtIx49hPI0xLTvSnn+26L8qNmCKSr9N3CDsw7QwobpgqIOrT+WjnAer8hCYrHpG 3SjjTt5rhHND5W6G6yXyvFIeDWWqeYdpiMip6Hra+ezIlHlR77v6XLAHrz1aZz7fiaBY5ZuU887 ccnKhWLFekeTLCIf5ur8grp1keSM9mIaVUpFeIkqL38LUnvGODZ+jj67BmfEquTaeu0u+Z/IZWl XZRWe7MAVYbjnCMbK2mavHNYoETO68nKTjCZvMHuMRz2+S1TwYz56G4lucky6Uw11Eh1aTdTsh9 u/QL0QNJ95sgUus/gU6HNFs2xzk4DQ0QRwon+oA/RVR+wk3bDfZeMwtILKFbi67hWJTxEJeZu+l Nam7EyTaqSRtCC4Im7J1KdMTFd/t03thucJPUMIVTtzTgA3/jV0p4e8KrwchAu4onmW5/P2LIjM DBEz+BsePo8cabBOEsb38ajEJ1fB3Zjsv5pkOiGEfHc2G/godFhWj8kDTg81y+ePH7Rebib2QZf 1aK8p2MYxPkPd+9KeRfakav7L5eeOGTU+zyTQgl5ocYltQEZToj3+8U= 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: netdev@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