From nobody Wed Apr 15 13:15:49 2026 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 351E2309DB5 for ; Wed, 4 Mar 2026 07:43:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772610218; cv=none; b=M/tIY/nvSu4s5Ybvi04UFul+dZXJVDd++WsyHflJ4Qwpg4Sd6+y4RZ1b5wVCY3JX+ExrGoPhOsRUPceJ3RYzQ18KLGrQmtjHhtnhKQE5LM6yDb62ygGjDBjN+BqRGjsUZcDk42gm6CIG7WN8SaYZ/KDQ8DIYRfv7XRT24a44sgY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772610218; c=relaxed/simple; bh=ds9fEzCzgnOh8aZtFA8qphSeg7jiatW+uffLqHndLZk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j2yNUwDclgHARVr3bgKzKFbdqCDSS59l4bLGJy9eu0UE/7mnvRF3RaeTchwVUu9fU2XMeATSrp/DTetp2La27V4Yy8nhrGlJuaiCmxlJiF4cuLwoWk+JMhQOQ55KGaZvz3e96HXPgeA+UnyKE45Inj4oMcsBYfSqlbN58HQfM0Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=hPttfdbf; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="hPttfdbf" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1772610214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gkKYESAH5YFAD03Jj76NpTW6HeKRoN5KgHV8fqdKUwk=; b=hPttfdbfoMJn5mkbkpzGib/ixwWVH0nVRqwVGlyIaVXN9oRVzmBs5c5HBelUUTKaec9iba Z4Jz4Xq/vaUOzJG9K2wyfuWbtmATt2rQlm63ShEphIYdfmjN+vIRmhWudq7wFzXodtx9iD 4n9V8PUb0fIoIDas7XPnB0O36Oqu4Hg= From: Jiayuan Chen To: jv@jvosburgh.net, netdev@vger.kernel.org Cc: jiayuan.chen@linux.dev, jiayuan.chen@shopee.com, syzbot+80e046b8da2820b6ba73@syzkaller.appspotmail.com, Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Stanislav Fomichev , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Hao Luo , Jiri Olsa , Shuah Khan , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Jussi Maki , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev Subject: [PATCH net v4 1/2] bonding: fix null-ptr-deref in bond_rr_gen_slave_id() Date: Wed, 4 Mar 2026 15:42:57 +0800 Message-ID: <20260304074301.35482-2-jiayuan.chen@linux.dev> In-Reply-To: <20260304074301.35482-1-jiayuan.chen@linux.dev> References: <20260304074301.35482-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Jiayuan Chen bond_rr_gen_slave_id() dereferences bond->rr_tx_counter without a NULL check. rr_tx_counter is a per-CPU counter only allocated in bond_open() when the bond mode is round-robin. If the bond device was never brought up, rr_tx_counter remains NULL, causing a null-ptr-deref. The XDP redirect path can reach this code even when the bond is not up: bpf_master_redirect_enabled_key is a global static key, so when any bond device has native XDP attached, the XDP_TX -> xdp_master_redirect() interception is enabled for all bond slaves system-wide. This allows the path xdp_master_redirect() -> bond_xdp_get_xmit_slave() -> bond_xdp_xmit_roundrobin_slave_get() -> bond_rr_gen_slave_id() to be reached on a bond that was never opened. Fix this by allocating rr_tx_counter unconditionally in bond_init() (ndo_init), which is called by register_netdevice() and covers both device creation paths (bond_create() and bond_newlink()). This also handles the case where bond mode is changed to round-robin after device creation. The conditional allocation in bond_open() is removed. Since bond_destructor() already unconditionally calls free_percpu(bond->rr_tx_counter), the lifecycle is clean: allocate at ndo_init, free at destructor. Note: rr_tx_counter is only used by round-robin mode, so this deliberately allocates a per-cpu u32 that goes unused for other modes. Conditional allocation (e.g., in bond_option_mode_set) was considered but rejected: the XDP path can race with mode changes on a downed bond, and adding memory barriers to the XDP hot path is not justified for saving 4 bytes per CPU. Fixes: 879af96ffd72 ("net, core: Add support for XDP redirection to slave d= evice") Reported-by: syzbot+80e046b8da2820b6ba73@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/698f84c6.a70a0220.2c38d7.00cc.GAE@googl= e.com/T/ Signed-off-by: Jiayuan Chen --- drivers/net/bonding/bond_main.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_mai= n.c index 78cff904cdc3..55b5c7a6cb5f 100644 --- a/drivers/net/bonding/bond_main.c +++ b/drivers/net/bonding/bond_main.c @@ -4279,12 +4279,6 @@ static int bond_open(struct net_device *bond_dev) struct list_head *iter; struct slave *slave; =20 - if (BOND_MODE(bond) =3D=3D BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) { - bond->rr_tx_counter =3D alloc_percpu(u32); - if (!bond->rr_tx_counter) - return -ENOMEM; - } - /* reset slave->backup and slave->inactive */ if (bond_has_slaves(bond)) { bond_for_each_slave(bond, slave, iter) { @@ -6411,6 +6405,19 @@ static int bond_init(struct net_device *bond_dev) if (!bond->wq) return -ENOMEM; =20 + /* rr_tx_counter is only used in round-robin mode, but we allocate + * it unconditionally because the XDP redirect path + * (xdp_master_redirect -> bond_xdp_get_xmit_slave) can reach here + * even when the bond is not up, and deferring allocation to + * bond_open or bond_option_mode_set would require memory barriers + * on the XDP hot path. The cost is a per-cpu u32 per bond device. + */ + bond->rr_tx_counter =3D alloc_percpu(u32); + if (!bond->rr_tx_counter) { + destroy_workqueue(bond->wq); + return -ENOMEM; + } + bond->notifier_ctx =3D false; =20 spin_lock_init(&bond->stats_lock); --=20 2.43.0 From nobody Wed Apr 15 13:15:49 2026 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C90D1382366 for ; Wed, 4 Mar 2026 07:43:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772610227; cv=none; b=pWISfPlpbL1bnBUsPNeh8WBzLrcAWKUwFxmEcSsd2bFv99IcUjXh4Hzd7yLX40SD2ayBjf/n4lLEG6ngkuqamyRKvnC3reT2xxfYmEsGbaIizQ92wXtNTqNO0N+lCKAqE+G48y/G7UWSDw93Yvl8geUBPsFvau6zyl9IfXc2Na4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772610227; c=relaxed/simple; bh=/nWFWGm1LWFw2+LWBT5eLedzo5nO+2W6C3bxSAB4nQw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XMpYoA7XCsMSByzxKd7vCfCOg0NFNb9Cj5LhbnOegTrAlGCGL359nWLOLI5+qu+0nOhJEVSEFWQQf1CtAprF0y/IGM1E/sO9CRxRDEZz4/1oEgTZz/2Vwpwtn2BJvLC3d/wWPfWD+v4wZzIpyjU1II3+VwHCK7wrY56wtZsDq9M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=UcoZDpUg; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="UcoZDpUg" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1772610223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OozEdu1pfaLvelgpBknkr6XecjF2fZSlQnkaPUUUzxY=; b=UcoZDpUg7fPzwB8PKIahbAswNCu/jblwcid44mW/Y20eRXHwOMxMowaGMvIlJBAGfJ8x09 CtDbSlLqoypeFN2BlZtFE2FdO0ux0jmnzU4nQEoUswuBDsiRKreENFTU2lE8karDwWKbA2 BaV+SDaSBgAk2R4uYeE7+KiWDhYhOL4= From: Jiayuan Chen To: jv@jvosburgh.net, netdev@vger.kernel.org Cc: jiayuan.chen@linux.dev, jiayuan.chen@shopee.com, Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Stanislav Fomichev , Andrii Nakryiko , Eduard Zingerman , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Hao Luo , Jiri Olsa , Shuah Khan , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Jussi Maki , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev Subject: [PATCH net v4 2/2] selftests/bpf: add test for xdp_master_redirect with bond not up Date: Wed, 4 Mar 2026 15:42:58 +0800 Message-ID: <20260304074301.35482-3-jiayuan.chen@linux.dev> In-Reply-To: <20260304074301.35482-1-jiayuan.chen@linux.dev> References: <20260304074301.35482-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Jiayuan Chen Add a selftest that reproduces the null-ptr-deref in bond_rr_gen_slave_id() when XDP redirect targets a bond device in round-robin mode that was never brought up. The test verifies the fix by ensuring no crash occurs. Test setup: - bond0: active-backup mode, UP, with native XDP (enables bpf_master_redirect_enabled_key globally) - bond1: round-robin mode, never UP - veth1: slave of bond1, with generic XDP (XDP_TX) - BPF_PROG_TEST_RUN with live frames triggers the redirect path Signed-off-by: Jiayuan Chen --- .../selftests/bpf/prog_tests/xdp_bonding.c | 101 +++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_bonding.c b/tools/t= esting/selftests/bpf/prog_tests/xdp_bonding.c index fb952703653e..a5b15e464018 100644 --- a/tools/testing/selftests/bpf/prog_tests/xdp_bonding.c +++ b/tools/testing/selftests/bpf/prog_tests/xdp_bonding.c @@ -191,13 +191,18 @@ static int bonding_setup(struct skeletons *skeletons,= int mode, int xmit_policy, return -1; } =20 -static void bonding_cleanup(struct skeletons *skeletons) +static void link_cleanup(struct skeletons *skeletons) { - restore_root_netns(); while (skeletons->nlinks) { skeletons->nlinks--; bpf_link__destroy(skeletons->links[skeletons->nlinks]); } +} + +static void bonding_cleanup(struct skeletons *skeletons) +{ + restore_root_netns(); + link_cleanup(skeletons); ASSERT_OK(system("ip link delete bond1"), "delete bond1"); ASSERT_OK(system("ip link delete veth1_1"), "delete veth1_1"); ASSERT_OK(system("ip link delete veth1_2"), "delete veth1_2"); @@ -493,6 +498,95 @@ static void test_xdp_bonding_nested(struct skeletons *= skeletons) system("ip link del bond_nest2"); } =20 +/* + * Test that XDP redirect via xdp_master_redirect() does not crash when + * the bond master device is not up. When bond is in round-robin mode but + * never opened, rr_tx_counter is NULL. + */ +static void test_xdp_bonding_redirect_no_up(struct skeletons *skeletons) +{ + struct nstoken *nstoken =3D NULL; + int xdp_pass_fd, xdp_tx_fd; + int veth1_ifindex; + int err; + char pkt[ETH_HLEN + 1]; + struct xdp_md ctx_in =3D {}; + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in =3D &pkt, + .data_size_in =3D sizeof(pkt), + .ctx_in =3D &ctx_in, + .ctx_size_in =3D sizeof(ctx_in), + .flags =3D BPF_F_TEST_XDP_LIVE_FRAMES, + .repeat =3D 1, + .batch_size =3D 1, + ); + + /* We can't use bonding_setup() because bond will be active */ + SYS(out, "ip netns add ns_rr_no_up"); + nstoken =3D open_netns("ns_rr_no_up"); + if (!ASSERT_OK_PTR(nstoken, "open ns_rr_no_up")) + goto out; + + /* bond0: active-backup, UP with slave veth0. + * Attaching native XDP to bond0 enables bpf_master_redirect_enabled_key + * globally. + */ + SYS(out, "ip link add bond0 type bond mode active-backup"); + SYS(out, "ip link add veth0 type veth peer name veth0p"); + SYS(out, "ip link set veth0 master bond0"); + SYS(out, "ip link set bond0 up"); + SYS(out, "ip link set veth0p up"); + + /* bond1: round-robin, never UP -> rr_tx_counter stays NULL */ + SYS(out, "ip link add bond1 type bond mode balance-rr"); + SYS(out, "ip link add veth1 type veth peer name veth1p"); + SYS(out, "ip link set veth1 master bond1"); + + veth1_ifindex =3D if_nametoindex("veth1"); + if (!ASSERT_GT(veth1_ifindex, 0, "veth1_ifindex")) + goto out; + + /* Attach native XDP to bond0 -> enables global redirect key */ + if (xdp_attach(skeletons, skeletons->xdp_tx->progs.xdp_tx, "bond0")) + goto out; + + /* Attach generic XDP (XDP_TX) to veth1. + * When packets arrive at veth1 via netif_receive_skb, do_xdp_generic() + * runs this program. XDP_TX + bond slave triggers xdp_master_redirect(). + */ + xdp_tx_fd =3D bpf_program__fd(skeletons->xdp_tx->progs.xdp_tx); + if (!ASSERT_GE(xdp_tx_fd, 0, "xdp_tx prog_fd")) + goto out; + + err =3D bpf_xdp_attach(veth1_ifindex, xdp_tx_fd, + XDP_FLAGS_SKB_MODE, NULL); + if (!ASSERT_OK(err, "attach generic XDP to veth1")) + goto out; + + /* Run BPF_PROG_TEST_RUN with XDP_PASS live frames on veth1. + * XDP_PASS frames become SKBs with skb->dev =3D veth1, entering + * netif_receive_skb -> do_xdp_generic -> xdp_master_redirect. + * Without the fix, bond_rr_gen_slave_id() dereferences NULL + * rr_tx_counter and crashes. + */ + xdp_pass_fd =3D bpf_program__fd(skeletons->xdp_dummy->progs.xdp_dummy_pro= g); + if (!ASSERT_GE(xdp_pass_fd, 0, "xdp_pass prog_fd")) + goto out; + + memset(pkt, 0, sizeof(pkt)); + ctx_in.data_end =3D sizeof(pkt); + ctx_in.ingress_ifindex =3D veth1_ifindex; + + err =3D bpf_prog_test_run_opts(xdp_pass_fd, &opts); + ASSERT_OK(err, "xdp_pass test_run should not crash"); + +out: + link_cleanup(skeletons); + close_netns(nstoken); + SYS_NOFAIL("ip netns del ns_rr_no_up"); +} + static void test_xdp_bonding_features(struct skeletons *skeletons) { LIBBPF_OPTS(bpf_xdp_query_opts, query_opts); @@ -680,6 +774,9 @@ void serial_test_xdp_bonding(void) if (test__start_subtest("xdp_bonding_redirect_multi")) test_xdp_bonding_redirect_multi(&skeletons); =20 + if (test__start_subtest("xdp_bonding_redirect_no_up")) + test_xdp_bonding_redirect_no_up(&skeletons); + out: xdp_dummy__destroy(skeletons.xdp_dummy); xdp_tx__destroy(skeletons.xdp_tx); --=20 2.43.0