mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Nikolay Aleksandrov <razor@blackwall.org>,
	Ido Schimmel <idosch@nvidia.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	bridge@lists.linux.dev, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [BUG] net: bridge: mdb: duplicate sources make the RTM_NEWMDB error path delete a source twice
Date: Mon, 16 Nov 2026 21:10:10 -0800 (PST)	[thread overview]
Message-ID: <179489219555.3099318.10297266282803881244@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2312 bytes --]

Hi Nikolay, Ido,

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found that an RTM_NEWMDB request
with a repeated source address and a later invalid source hits the
WARN_ON_ONCE() in br_mdb_del_group_src(). An unprivileged user can do
this from its own user and network namespace:

  WARNING: net/bridge/br_mdb.c:901 at br_mdb_add_group_srcs+0x375/0x4a0, CPU#1: test/106
  CPU: 1 UID: 65534 PID: 106 Comm: test Not tainted 7.4.0-rc3 #1 PREEMPT(lazy) 
  RIP: 0010:br_mdb_add_group_srcs+0x375/0x4a0
  Call Trace:
   <TASK>
   br_mdb_add+0x7e2/0x1390
   rtnl_mdb_add+0x218/0x490
   rtnetlink_rcv_msg+0x2f7/0xb80
   netlink_rcv_skb+0x11f/0x350
   netlink_unicast+0x518/0x870

br_mdb_config_src_list_init() counts and parses the MDBE_ATTR_SRC_LIST
entries but does not reject duplicates. In br_mdb_add_group_srcs() the
second copy of a source finds the entry that the first copy created, so
both refer to the same struct net_bridge_group_src. When a later source
fails (here 0.0.0.0, which is_valid_mdb_source() accepts but
br_multicast_new_group_src() rejects), the error path deletes every
source added so far, duplicates included:

	err_del_group_srcs:
		for (i--; i >= 0; i--)
			br_mdb_del_group_src(pg, &cfg->src_entries[i]);

The first deletion frees the entry, and the second does not find it:

	ent = br_multicast_find_group_src(pg, &src->addr);
	if (WARN_ON_ONCE(!ent))
		return;

We have not seen memory corruption from this; the effect is the warning,
which panics a machine with panic_on_warn.

Rejecting duplicate source entries in br_mdb_config_src_list_init(), or
making the error path delete only the sources this request created,
should avoid it. We have not tested either change.

The reproducer runs as an unprivileged user (uid 65534) that creates a
user and network namespace, makes a bridge with a port, and sends
RTM_NEWMDB with NLM_F_REPLACE and the sources 10.0.0.1, 10.0.0.1 and
0.0.0.0. On an x86_64 QEMU guest with 2 vCPUs and KASAN (gcc 13.3,
config attached), it warns in every run (5 of 5).

Checked against v7.4-rc3 (aebb72744764); br_mdb.c is the same in
net-next (6ff4c5942a30). We found no earlier report on syzbot or on the
bridge list.

The reproducer (repro.c) and the kernel config are attached.

Thanks,
Kyuwon Cho

[-- Attachment #2: repro.c --]
[-- Type: text/plain, Size: 8776 bytes --]

// Reproducer for the is_valid_mdb_source (net/bridge/br_mdb.c) sibling of 4aa61c88b4e2.
// Sends RTM_NEWMDB / RTM_DELMDB with all-zeros sources, which the validator accepts.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#ifndef RTM_NEWMDB
#define RTM_NEWMDB 84
#endif
#ifndef RTM_DELMDB
#define RTM_DELMDB 85
#endif

#define AF_BRIDGE_ 7
#define MDBA_SET_ENTRY_ 1
#define MDBA_SET_ENTRY_ATTRS_ 2
#define MDBE_ATTR_SOURCE_ 1
#define MDBE_ATTR_SRC_LIST_ 2
#define MDBE_ATTR_GROUP_MODE_ 3
#define MDBE_SRC_LIST_ENTRY_ 1
#define MDBE_SRCATTR_ADDRESS_ 1
#define MCAST_INCLUDE_ 1
#define MDB_PERMANENT_ 1
#define VETH_INFO_PEER_ 1
#define IFF_UP_ 1

struct mdb_entry {               // layout of struct br_mdb_entry (28 bytes)
	uint32_t ifindex;
	uint8_t state;
	uint8_t flags;
	uint16_t vid;
	struct {
		union {
			uint32_t ip4;
			uint8_t ip6[16];
		} u;
		uint16_t proto;
	} addr;
};

struct br_port_msg_ {
	uint8_t family;
	uint8_t pad[3];
	int32_t ifindex;
};

static int nlfd = -1;
static uint32_t seq;

static size_t nla(char *b, size_t off, uint16_t type, const void *d, size_t len)
{
	struct nlattr *a = (void *)(b + off);

	a->nla_len = NLA_HDRLEN + len;
	a->nla_type = type;
	if (len)
		memcpy(b + off + NLA_HDRLEN, d, len);
	return off + NLA_ALIGN(NLA_HDRLEN + len);
}

static int nl_transact(struct nlmsghdr *nh)
{
	char rb[8192];
	struct sockaddr_nl sa = { .nl_family = AF_NETLINK };

	nh->nlmsg_seq = ++seq;
	if (sendto(nlfd, nh, nh->nlmsg_len, 0, (void *)&sa, sizeof(sa)) < 0)
		return -errno;
	for (;;) {
		ssize_t n = recv(nlfd, rb, sizeof(rb), 0);
		if (n < 0)
			return -errno;
		for (struct nlmsghdr *r = (void *)rb; NLMSG_OK(r, n);
		     r = NLMSG_NEXT(r, n)) {
			if (r->nlmsg_seq != nh->nlmsg_seq)
				continue;
			if (r->nlmsg_type == NLMSG_ERROR) {
				struct nlmsgerr *e = NLMSG_DATA(r);
				return e->error;
			}
		}
	}
}

static int link_new(const char *name, const char *kind, const char *peer)
{
	static char b[1024];
	struct nlmsghdr *nh = (void *)b;
	char info[512], pd[256], vd[384];
	size_t io, po, vo, off;

	memset(b, 0, sizeof(b));
	nh->nlmsg_type = RTM_NEWLINK;
	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
	off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	off = nla(b, off, IFLA_IFNAME, name, strlen(name) + 1);

	io = nla(info, 0, IFLA_INFO_KIND, kind, strlen(kind) + 1);
	if (peer) {
		memset(pd, 0, sizeof(pd));
		po = sizeof(struct ifinfomsg);
		po = nla(pd, po, IFLA_IFNAME, peer, strlen(peer) + 1);
		vo = nla(vd, 0, VETH_INFO_PEER_ | NLA_F_NESTED, pd, po);
		io = nla(info, io, IFLA_INFO_DATA | NLA_F_NESTED, vd, vo);
	}
	off = nla(b, off, IFLA_LINKINFO | NLA_F_NESTED, info, io);
	nh->nlmsg_len = off;
	return nl_transact(nh);
}

static int link_set(int ifindex, int up, int master)
{
	static char b[256];
	struct nlmsghdr *nh = (void *)b;
	struct ifinfomsg *ifi;
	size_t off;

	memset(b, 0, sizeof(b));
	nh->nlmsg_type = RTM_NEWLINK;
	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
	ifi = NLMSG_DATA(nh);
	ifi->ifi_index = ifindex;
	ifi->ifi_flags = up ? IFF_UP_ : 0;
	ifi->ifi_change = IFF_UP_;
	off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
	if (master) {
		uint32_t m = master;

		off = nla(b, off, IFLA_MASTER, &m, 4);
	}
	nh->nlmsg_len = off;
	return nl_transact(nh);
}

// Build and send RTM_NEWMDB/RTM_DELMDB for a (*,G) on port portifx.
//  srcattr_present: include MDBE_ATTR_SOURCE = srcattr (may be 0)
//  mode:            MDBE_ATTR_GROUP_MODE value, or -1 for none
//  nsrc:            MDBE_ATTR_SRC_LIST with nsrc IPv4 entries, or -1 for none
static int mdb_op(uint16_t type, uint16_t extra_flags, int brifx, int portifx,
		  uint32_t group, int srcattr_present, uint32_t srcattr,
		  int mode, int nsrc, const uint32_t *srcs)
{
	static char b[4096], at[2048], sl[1024], ent[64];
	struct nlmsghdr *nh = (void *)b;
	struct br_port_msg_ *bpm;
	struct mdb_entry e;
	size_t off, ao = 0;

	memset(b, 0, sizeof(b));
	nh->nlmsg_type = type;
	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | extra_flags;
	bpm = (void *)(b + NLMSG_HDRLEN);
	bpm->family = AF_BRIDGE_;
	bpm->ifindex = brifx;
	off = NLMSG_LENGTH(sizeof(*bpm));

	memset(&e, 0, sizeof(e));
	e.ifindex = portifx;
	e.state = MDB_PERMANENT_;
	e.addr.u.ip4 = group;
	e.addr.proto = htons(0x0800);
	off = nla(b, off, MDBA_SET_ENTRY_, &e, sizeof(e));

	if (srcattr_present) {
		uint32_t s = srcattr;

		ao = nla(at, ao, MDBE_ATTR_SOURCE_, &s, 4);
	}
	if (mode >= 0) {
		uint8_t m = mode;

		ao = nla(at, ao, MDBE_ATTR_GROUP_MODE_, &m, 1);
	}
	if (nsrc >= 0) {
		size_t so = 0;

		for (int i = 0; i < nsrc; i++) {
			size_t eo = nla(ent, 0, MDBE_SRCATTR_ADDRESS_, &srcs[i], 4);

			so = nla(sl, so, MDBE_SRC_LIST_ENTRY_ | NLA_F_NESTED, ent, eo);
		}
		ao = nla(at, ao, MDBE_ATTR_SRC_LIST_ | NLA_F_NESTED, sl, so);
	}
	off = nla(b, off, MDBA_SET_ENTRY_ATTRS_ | NLA_F_NESTED, at, ao);
	nh->nlmsg_len = off;
	return nl_transact(nh);
}

static uint32_t ip(const char *s)
{
	struct in_addr a;

	inet_pton(AF_INET, s, &a);
	return a.s_addr;
}

int main(void)
{
	int br, v0, v1, rc;
	uint32_t srcs[3];
	uid_t uid = getuid();
	gid_t gid = getgid();
	char map[64];
	int fd;

	if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0) {
		perror("unshare");
		return 1;
	}
	fd = open("/proc/self/setgroups", O_WRONLY);
	if (fd >= 0) { write(fd, "deny", 4); close(fd); }
	snprintf(map, sizeof(map), "0 %d 1", (int)uid);
	fd = open("/proc/self/uid_map", O_WRONLY);
	if (fd < 0 || write(fd, map, strlen(map)) < 0) perror("uid_map");
	if (fd >= 0) close(fd);
	snprintf(map, sizeof(map), "0 %d 1", (int)gid);
	fd = open("/proc/self/gid_map", O_WRONLY);
	if (fd < 0 || write(fd, map, strlen(map)) < 0) perror("gid_map");
	if (fd >= 0) close(fd);

	nlfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
	if (nlfd < 0) {
		perror("socket");
		return 1;
	}

	rc = link_new("br0", "bridge", NULL);
	printf("repro: link br0 rc=%d\n", rc);
	rc = link_new("v0", "veth", "v1");
	printf("repro: link veth v0/v1 rc=%d\n", rc);
	br = if_nametoindex("br0");
	v0 = if_nametoindex("v0");
	v1 = if_nametoindex("v1");
	printf("repro: ifindex br=%d v0=%d v1=%d\n", br, v0, v1);
	rc = link_set(v0, 1, br);
	printf("repro: v0 up+master rc=%d\n", rc);
	rc = link_set(v1, 1, 0);
	printf("repro: v1 up rc=%d\n", rc);
	rc = link_set(br, 1, 0);
	printf("repro: br0 up rc=%d\n", rc);

	// P1 control: one valid source, INCLUDE, new (*,G).
	srcs[0] = ip("10.0.0.1");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE, br, v0, ip("239.1.1.1"),
		    0, 0, MCAST_INCLUDE_, 1, srcs);
	printf("repro: P1 control add [10.0.0.1] rc=%d\n", rc);

	// P2: duplicate valid source then all-zeros. The zero source is rejected in
	// br_multicast_new_group_src -> ENOSPC, and br_mdb_add_group_srcs unwinds
	// both earlier entries, which are the same source (second unwind finds nothing).
	srcs[0] = ip("10.0.0.1");
	srcs[1] = ip("10.0.0.1");
	srcs[2] = ip("0.0.0.0");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE | NLM_F_REPLACE, br, v0,
		    ip("239.1.1.2"), 0, 0, MCAST_INCLUDE_, 3, srcs);
	printf("repro: P2 add [10.0.0.1,10.0.0.1,0.0.0.0] rc=%d\n", rc);

	// P3: single valid source then zero (single unwind, no duplicate).
	srcs[0] = ip("10.0.0.1");
	srcs[1] = ip("0.0.0.0");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE, br, v0, ip("239.1.1.3"),
		    0, 0, MCAST_INCLUDE_, 2, srcs);
	printf("repro: P3 add [10.0.0.1,0.0.0.0] rc=%d\n", rc);

	// P4: existing sources, then REPLACE with [10.0.0.1, 0.0.0.0]. Unwind
	// deletes the pre-existing 10.0.0.1 source entry.
	srcs[0] = ip("10.0.0.1");
	srcs[1] = ip("10.0.0.2");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE, br, v0, ip("239.1.1.4"),
		    0, 0, MCAST_INCLUDE_, 2, srcs);
	printf("repro: P4a add [10.0.0.1,10.0.0.2] rc=%d\n", rc);
	srcs[0] = ip("10.0.0.1");
	srcs[1] = ip("0.0.0.0");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE | NLM_F_REPLACE, br, v0,
		    ip("239.1.1.4"), 0, 0, MCAST_INCLUDE_, 2, srcs);
	printf("repro: P4b replace [10.0.0.1,0.0.0.0] rc=%d\n", rc);

	// P5: (*,G) with a zero MDBE_ATTR_SOURCE is the star-g key; delete it, then
	// delete again.
	srcs[0] = ip("10.0.0.9");
	rc = mdb_op(RTM_NEWMDB, NLM_F_CREATE, br, v0, ip("239.1.1.5"),
		    0, 0, MCAST_INCLUDE_, 1, srcs);
	printf("repro: P5a add star-g [10.0.0.9] rc=%d\n", rc);
	rc = mdb_op(RTM_DELMDB, 0, br, v0, ip("239.1.1.5"), 1, 0, -1, -1, NULL);
	printf("repro: P5b del with SOURCE=0 rc=%d\n", rc);
	rc = mdb_op(RTM_DELMDB, 0, br, v0, ip("239.1.1.5"), 1, 0, -1, -1, NULL);
	printf("repro: P5c del again rc=%d\n", rc);

	// Let deferred GC / RCU callbacks run and surface any use-after-free.
	sleep(3);
	printf("repro: done\n");
	return 0;
}

[-- Attachment #3: config.gz --]
[-- Type: application/gzip, Size: 37687 bytes --]

                 reply	other threads:[~2026-11-17  5:10 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=179489219555.3099318.10297266282803881244@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=bridge@lists.linux.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=razor@blackwall.org \
    /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®