mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] wifi: cfg80211: CSA onto a hidden SSID group warns in cfg80211_update_assoc_bss_entry() and leaves freed beacon IEs
@ 2026-11-22 22:11 Kyuwon Cho
  2026-11-22 22:11 ` [PATCH wireless] wifi: cfg80211: unlink hidden SSID members of the merged BSS on CSA Kyuwon Cho
  0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-11-22 22:11 UTC (permalink / raw)
  To: Johannes Berg; +Cc: Sergey Matyukevich, linux-wireless, linux-kernel

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

Hi Johannes,

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found that when a station
follows a channel switch to a channel where its AP already has a hidden
SSID group in the scan list, cfg80211_update_assoc_bss_entry() warns:

  WARNING: net/wireless/scan.c:3537 at cfg80211_update_assoc_bss_entry+0x53a/0x7c0, CPU#0: kworker/u8:2/44
  Workqueue: events_unbound cfg80211_wiphy_work
  RIP: 0010:cfg80211_update_assoc_bss_entry (net/wireless/scan.c:3537)
  Call Trace:
   <TASK>
   cfg80211_ch_switch_notify (net/wireless/nl80211.c:23295)
   ieee80211_sta_process_chanswitch (net/mac80211/mlme.c:3328)
   ieee80211_rx_mgmt_beacon (net/mac80211/mlme.c:8381)
   ieee80211_sta_rx_queued_frame (net/mac80211/mlme.c:11909)
   ieee80211_iface_work (net/mac80211/iface.c:1805 net/mac80211/iface.c:1862)
   cfg80211_wiphy_work (net/wireless/core.c:609)
   process_one_work (kernel/workqueue.c:3495)
   worker_thread (kernel/workqueue.c:3578 kernel/workqueue.c:3659)
   kthread (kernel/kthread.c:436)
   ret_from_fork (arch/x86/kernel/process.c:158)
   ret_from_fork_asm (arch/x86/entry/entry_64.S:245)
   </TASK>

and a later scan dump can read the group's beacon IEs after they were
freed:

  BUG: KASAN: slab-use-after-free in nl80211_send_bss.constprop.0 (net/wireless/nl80211.c:12959)
  Read of size 1 at addr ff1100001428e61c by task test/115
  Call Trace:
   <TASK>
   nl80211_send_bss.constprop.0 (net/wireless/nl80211.c:12959)
   nl80211_dump_scan (net/wireless/nl80211.c:13105)
   genl_dumpit (net/netlink/genetlink.c:1026)
   netlink_dump (net/netlink/af_netlink.c:2360)
   __netlink_dump_start (net/netlink/af_netlink.c:2475)
   genl_family_rcv_msg_dumpit (net/netlink/genetlink.c:1075)
   genl_rcv_msg (net/netlink/genetlink.c:1191 net/netlink/genetlink.c:1209)
   netlink_rcv_skb (net/netlink/af_netlink.c:2585)
   genl_rcv (net/netlink/genetlink.c:1218)
   netlink_unicast (net/netlink/af_netlink.c:1336 net/netlink/af_netlink.c:1362)
   netlink_sendmsg (net/netlink/af_netlink.c:1921)
   __sys_sendto (net/socket.c:2283)
   do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
   </TASK>

The entry on the new channel that matches the associated BSS gives its
IEs to cbss and is then unlinked:

	if (new) {
		/* to save time, update IEs for transmitting bss only */
		cfg80211_update_known_bss(rdev, cbss, new, false);
		new->pub.proberesp_ies = NULL;
		new->pub.beacon_ies = NULL;
		...
		WARN_ON(atomic_read(&new->hold));
		if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
			rdev->bss_generation++;
	}

When new is the beacon entry of a hidden SSID group,
__cfg80211_unlink_bss() refuses to remove it while the group has probe
response entries. new stays on the list, so cfg80211_rehash_bss() of
cbss also warns in rb_insert_bss(), and the probe response entries
still share the beacon IEs that cfg80211_update_known_bss() just gave
to cbss. Once cbss is freed, bss_free() frees those IEs, and the next
GET_SCAN dump reads them through a probe response entry.
cc45f313d855 ("wifi: cfg80211: preserve hidden-group beacon IE
ownership") covers the case where new is a member of such a group; this
is the case where new is the group's beacon entry.

The reproducer runs as root and drives two mac80211_hwsim radios over
nl80211: an AP interface sends hidden SSID beacons and probe responses
on channel 1 while the station scans, the station associates with the
same AP on channel 6, and the AP announces a channel switch to channel
1. The WARN fires on every run. The use-after-free also needs the
station to disconnect, a flush scan that expires the old entry and a
GET_SCAN dump, and shows up in some runs.

Checked against v7.4-rc3 (aebb72744764); the code is the same in the
wireless tree and in wireless-next.

A fix follows as a reply. The reproducer (repro.c) and the kernel
config are attached.

Thanks,
Kyuwon Cho

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

/*
 * repro.c: cfg80211_update_hidden_bsses / cfg80211_update_assoc_bss_entry sibling of 26e84445f02c.
 *
 * Everything goes through nl80211 (generic netlink) and ioctl. The AP interface (hap0) does all
 * of the on-air work: beacons, probe responses (NL80211_CMD_FRAME), auth/assoc answers, and CSA
 * countdown beacons (NL80211_CMD_SET_BEACON with a CSA IE).
 *
 *  A  AP on ch1 with a hidden zero-SSID beacon; the station (phy1) scans ch1 -> owner O1.
 *     The AP sends hidnet probe responses while the station scans -> member M1 in O1's group,
 *     sharing O1's beacon_ies blob B.
 *  B  AP restarted on ch6 (hidden zero-SSID); the station scans ch6 -> entry O6.
 *  C  station associates to the AP on ch6 (userspace AP answers auth/assoc). cbss = O6.
 *  D  AP keeps running on ch6 and beacons a CSA IE (new channel 1, count 4..0). The station
 *     follows to ch1 and cfg80211_ch_switch_notify -> cfg80211_update_assoc_bss_entry(cbss=O6,
 *     new=O1): O6 takes B, O1 loses beacon_ies but stays linked (it has a member), and M1 still
 *     points at B.
 *  E  AP stops, station disconnects (drops the cbss ref). AP restarts on ch1 with non-hidden
 *     "hidnet" beacons: these refresh only M1 (exact SSID, early-return branch, no repoint).
 *     A FLUSH scan expires O6 (ts older than the scan) -> bss_free(O6) -> kfree_rcu(B).
 *     GET_SCAN dump reads M1->beacon_ies (nl80211_send_bss) -> use after free expected.
 */
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <linux/genetlink.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/nl80211.h>
#include <linux/sockios.h>

#ifndef NLA_F_NESTED
#define NLA_F_NESTED (1 << 15)
#endif
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
#define ALIGN4(x) (((x) + 3) & ~3)
#define REQ_ACK (NLM_F_REQUEST | NLM_F_ACK)

static const uint8_t AP_MAC[6] = {0x02, 0xaa, 0x00, 0x00, 0x00, 0x01};
static const uint8_t STA_MAC[6] = {0x02, 0xaa, 0x00, 0x00, 0x00, 0x02};
static const uint8_t BCAST[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static const uint8_t RATES[8] = {0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24};

static int nlfam, mlme_gid;
static int ctl_fd = -1, apfd = -1, evfd = -1;
static int AP_IDX, STA_IDX;
static int auth_rsp_sent, assoc_rsp_sent;

static uint8_t ch_of(uint32_t freq)
{
	return (uint8_t)((freq - 2407) / 5);
}

/* ---------- netlink message building ---------- */
static unsigned char mb[8192];
static int ml;
static unsigned char rb[8192];
static int rep_len;

static void msg_begin(uint8_t cmd, uint16_t type, uint16_t flags)
{
	memset(mb, 0, 20);
	struct nlmsghdr *nh = (void *)mb;
	nh->nlmsg_type = type;
	nh->nlmsg_flags = flags;
	struct genlmsghdr *gh = (void *)(mb + 16);
	gh->cmd = cmd;
	gh->version = 1;
	ml = 20;
}

static void msg_add(uint16_t type, const void *data, int dlen)
{
	struct nlattr *a = (void *)(mb + ml);
	a->nla_type = type;
	a->nla_len = 4 + dlen;
	if (dlen)
		memcpy(mb + ml + 4, data, dlen);
	ml += ALIGN4(4 + dlen);
}

static void msg_u32(uint16_t type, uint32_t v)
{
	msg_add(type, &v, 4);
}

static int nest_begin(uint16_t type)
{
	int off = ml;
	struct nlattr *a = (void *)(mb + ml);

	a->nla_type = type | NLA_F_NESTED;
	a->nla_len = 4;
	ml += 4;
	return off;
}

static void nest_end(int off)
{
	struct nlattr *a = (void *)(mb + off);

	a->nla_len = ml - off;
}

static int nl_send(int fd)
{
	struct nlmsghdr *nh = (void *)mb;
	struct sockaddr_nl sa;

	nh->nlmsg_len = ml;
	memset(&sa, 0, sizeof(sa));
	sa.nl_family = AF_NETLINK;
	if (sendto(fd, mb, ml, 0, (void *)&sa, sizeof(sa)) != ml)
		return -errno;
	return 0;
}

/* Send the built message and wait for its ack. The first data reply is kept in rb. */
static int nl_xact(int fd)
{
	static unsigned char in[16384];
	int r, n;

	rep_len = 0;
	r = nl_send(fd);
	if (r)
		return r;
	for (;;) {
		n = recv(fd, in, sizeof(in), 0);
		if (n < 0)
			return -errno;
		for (struct nlmsghdr *nh = (void *)in; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) {
			if (nh->nlmsg_type == NLMSG_ERROR) {
				struct nlmsgerr *e = NLMSG_DATA(nh);
				return e->error;
			}
			if (nh->nlmsg_type == NLMSG_DONE)
				return 0;
			if (rep_len == 0 && nh->nlmsg_len <= sizeof(rb)) {
				memcpy(rb, nh, nh->nlmsg_len);
				rep_len = nh->nlmsg_len;
			}
		}
	}
}

/* ---------- attribute iteration ---------- */
struct it {
	const unsigned char *p;
	int left;
};

static int it_next(struct it *s, uint16_t *type, const unsigned char **data, int *dlen)
{
	const struct nlattr *n;
	int adv;

	if (s->left < 4)
		return 0;
	n = (const void *)s->p;
	if (n->nla_len < 4 || n->nla_len > s->left)
		return 0;
	*type = n->nla_type & 0x3fff;
	*data = s->p + 4;
	*dlen = n->nla_len - 4;
	adv = ALIGN4(n->nla_len);
	if (adv > s->left)
		adv = s->left;
	s->p += adv;
	s->left -= adv;
	return 1;
}

static const unsigned char *attr_find(const unsigned char *p, int len, uint16_t type, int *dlen)
{
	struct it s = {p, len};
	uint16_t t;
	const unsigned char *d;
	int dl;

	while (it_next(&s, &t, &d, &dl))
		if (t == type) {
			*dlen = dl;
			return d;
		}
	*dlen = 0;
	return NULL;
}

static long ms_since(const struct timespec *t0)
{
	struct timespec t;

	clock_gettime(CLOCK_MONOTONIC, &t);
	return (t.tv_sec - t0->tv_sec) * 1000L + (t.tv_nsec - t0->tv_nsec) / 1000000L;
}

static int nl_open(void)
{
	struct sockaddr_nl sa;
	struct timeval tv = {5, 0};
	int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_GENERIC);

	if (fd < 0) {
		perror("socket netlink");
		exit(1);
	}
	memset(&sa, 0, sizeof(sa));
	sa.nl_family = AF_NETLINK;
	if (bind(fd, (void *)&sa, sizeof(sa))) {
		perror("bind netlink");
		exit(1);
	}
	setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
	return fd;
}

static int get_family(void)
{
	const unsigned char *g;
	int l, gl;
	struct it top;
	uint16_t ty, ty2;
	const unsigned char *d, *d2;
	int dl, dl2;
	int rc;

	msg_begin(CTRL_CMD_GETFAMILY, GENL_ID_CTRL, REQ_ACK);
	msg_add(CTRL_ATTR_FAMILY_NAME, "nl80211", 8);
	rc = nl_xact(ctl_fd);
	if (rc) {
		printf("getfamily rc=%d\n", rc);
		return -1;
	}
	g = attr_find(rb + 20, rep_len - 20, CTRL_ATTR_FAMILY_ID, &l);
	nlfam = g ? *(const uint16_t *)g : 0;
	g = attr_find(rb + 20, rep_len - 20, CTRL_ATTR_MCAST_GROUPS, &gl);
	if (g) {
		top.p = g;
		top.left = gl;
		while (it_next(&top, &ty, &d, &dl)) {
			struct it grp = {d, dl};
			const unsigned char *nm = NULL;
			int nml = 0;
			uint32_t gid = 0;

			while (it_next(&grp, &ty2, &d2, &dl2)) {
				if (ty2 == CTRL_ATTR_MCAST_GRP_NAME) {
					nm = d2;
					nml = dl2;
				}
				if (ty2 == CTRL_ATTR_MCAST_GRP_ID && dl2 >= 4)
					memcpy(&gid, d2, 4);
			}
			if (nm && nml >= 5 && !strncmp((const char *)nm, "mlme", 4))
				mlme_gid = gid;
		}
	}
	return nlfam ? 0 : -1;
}

static int new_iface(uint32_t wiphy, uint32_t type, const char *name)
{
	int rc, l;
	const unsigned char *p;
	uint32_t idx = 0;

	msg_begin(NL80211_CMD_NEW_INTERFACE, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_WIPHY, wiphy);
	msg_u32(NL80211_ATTR_IFTYPE, type);
	msg_add(NL80211_ATTR_IFNAME, name, strlen(name) + 1);
	rc = nl_xact(ctl_fd);
	p = attr_find(rb + 20, rep_len - 20, NL80211_ATTR_IFINDEX, &l);
	if (p && l >= 4)
		memcpy(&idx, p, 4);
	printf("new_iface %s wiphy=%u type=%u rc=%d ifindex=%u\n", name, wiphy, type, rc, idx);
	return (int)idx;
}

/* ---------- frame builders ---------- */
static int mk_hdr(unsigned char *f, uint16_t fc, const uint8_t *da, const uint8_t *sa,
		  const uint8_t *bssid)
{
	memset(f, 0, 24);
	f[0] = fc & 0xff;
	f[1] = fc >> 8;
	memcpy(f + 4, da, 6);
	memcpy(f + 10, sa, 6);
	memcpy(f + 16, bssid, 6);
	return 24;
}

static int ie_put(unsigned char *p, int id, const void *d, int dl)
{
	p[0] = id;
	p[1] = dl;
	if (dl)
		memcpy(p + 2, d, dl);
	return 2 + dl;
}

static int beacon_fixed(unsigned char *p)
{
	memset(p, 0, 8);
	p[8] = 100;	/* interval 100 TU */
	p[9] = 0;
	p[10] = 0x01;	/* ESS */
	p[11] = 0;
	return 12;
}

/* Beacon from AP_MAC on channel ch. slen 0 = hidden (zero-length SSID element).
 * csa_count >= 0 adds a CSA IE (mode 1, new channel 1, count csa_count). */
static int mk_beacon(unsigned char *f, uint8_t ch, const char *ssid, int slen, int csa_count)
{
	int n = mk_hdr(f, 0x0080, BCAST, AP_MAC, AP_MAC);
	uint8_t csa[3];

	n += beacon_fixed(f + n);
	n += ie_put(f + n, 0, ssid, slen);
	n += ie_put(f + n, 1, RATES, 8);
	n += ie_put(f + n, 3, &ch, 1);
	if (csa_count >= 0) {
		csa[0] = 1;
		csa[1] = 1;
		csa[2] = (uint8_t)csa_count;
		n += ie_put(f + n, 37, csa, 3);
	}
	return n;
}

/* Probe response with SSID "hidnet" from AP_MAC to the station, channel ch. */
static int mk_probe(unsigned char *f, uint8_t ch)
{
	int n = mk_hdr(f, 0x0050, STA_MAC, AP_MAC, AP_MAC);

	n += beacon_fixed(f + n);
	n += ie_put(f + n, 0, "hidnet", 6);
	n += ie_put(f + n, 1, RATES, 8);
	n += ie_put(f + n, 3, &ch, 1);
	return n;
}

static int mk_auth_rsp(unsigned char *f, const uint8_t *sta)
{
	int n = mk_hdr(f, 0x00b0, sta, AP_MAC, AP_MAC);

	f[24] = 0;	/* open system */
	f[25] = 0;
	f[26] = 2;	/* seq 2 */
	f[27] = 0;
	f[28] = 0;	/* status success */
	f[29] = 0;
	return n + 6;
}

static int mk_assoc_rsp(unsigned char *f, const uint8_t *sta)
{
	int n = mk_hdr(f, 0x0010, sta, AP_MAC, AP_MAC);

	f[24] = 0x01;	/* capability */
	f[25] = 0x00;
	f[26] = 0;	/* status success */
	f[27] = 0;
	f[28] = 0x01;	/* AID 1 with the two top bits set */
	f[29] = 0xc0;
	n += 6;
	n += ie_put(f + n, 1, RATES, 8);
	return n;
}

/* ---------- nl80211 operations ---------- */
static int send_ap_frame(const unsigned char *f, int fl, uint32_t freq, const char *tag)
{
	int rc;

	msg_begin(NL80211_CMD_FRAME, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, AP_IDX);
	msg_u32(NL80211_ATTR_WIPHY_FREQ, freq);
	msg_add(NL80211_ATTR_FRAME, f, fl);
	rc = nl_xact(ctl_fd);
	if (tag)
		printf("ap tx %s len=%d rc=%d\n", tag, fl, rc);
	return rc;
}

/* Answer the station's auth and assoc requests as the AP (AP mode does not do it itself). */
static void serve_ap(int ms, int *done_flag)
{
	struct timespec t0;
	static unsigned char eb[16384];

	clock_gettime(CLOCK_MONOTONIC, &t0);
	while (!*done_flag && ms_since(&t0) < ms) {
		struct pollfd p = {apfd, POLLIN, 0};
		int n;

		if (poll(&p, 1, 100) <= 0)
			continue;
		n = recv(apfd, eb, sizeof(eb), 0);
		if (n <= 0)
			continue;
		for (struct nlmsghdr *nh = (void *)eb; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) {
			struct genlmsghdr *gh = (void *)((unsigned char *)nh + 16);
			const unsigned char *fr;
			int fl;
			uint16_t fc;
			unsigned char rsp[128];
			int rl = 0;

			if (gh->cmd != NL80211_CMD_FRAME)
				continue;
			fr = attr_find((unsigned char *)nh + 20, nh->nlmsg_len - 20,
				       NL80211_ATTR_FRAME, &fl);
			if (!fr || fl < 28)
				continue;
			fc = fr[0] | (fr[1] << 8);
			printf("ap rx fc=0x%04x len=%d algo/seq=%u/%u\n", fc, fl,
			       fr[24] | (fr[25] << 8), fr[26] | (fr[27] << 8));
			if (fc == 0x00b0 && !auth_rsp_sent && (fr[26] | (fr[27] << 8)) == 1) {
				rl = mk_auth_rsp(rsp, fr + 10);
				send_ap_frame(rsp, rl, 2437, "auth");
				auth_rsp_sent = 1;
			} else if (fc == 0x0000 && !assoc_rsp_sent) {
				rl = mk_assoc_rsp(rsp, fr + 10);
				send_ap_frame(rsp, rl, 2437, "assoc");
				assoc_rsp_sent = 1;
			}
		}
	}
}

static int trigger_scan(uint32_t freq, int flush)
{
	int off;

	msg_begin(NL80211_CMD_TRIGGER_SCAN, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, STA_IDX);
	off = nest_begin(NL80211_ATTR_SCAN_FREQUENCIES);
	msg_u32(1, freq);
	nest_end(off);
	if (flush)
		msg_u32(NL80211_ATTR_SCAN_FLAGS, NL80211_SCAN_FLAG_FLUSH);
	return nl_xact(ctl_fd);
}

static int start_ap(uint32_t freq, const char *ssid, int hidden)
{
	unsigned char head[256];
	int hl, rc;
	uint8_t ch = ch_of(freq);

	hl = mk_beacon(head, ch, ssid, hidden ? 0 : (int)strlen(ssid), -1);
	msg_begin(NL80211_CMD_START_AP, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, AP_IDX);
	msg_add(NL80211_ATTR_BEACON_HEAD, head, hl);
	msg_u32(NL80211_ATTR_BEACON_INTERVAL, 100);
	msg_u32(NL80211_ATTR_DTIM_PERIOD, 2);
	msg_u32(NL80211_ATTR_WIPHY_FREQ, freq);
	msg_u32(NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_20_NOHT);
	msg_add(NL80211_ATTR_SSID, ssid, strlen(ssid));
	if (hidden)
		msg_u32(NL80211_ATTR_HIDDEN_SSID, NL80211_HIDDEN_SSID_ZERO_LEN);
	rc = nl_xact(ctl_fd);
	printf("start_ap %u ssid=%s hidden=%d rc=%d\n", freq, ssid, hidden, rc);
	return rc;
}

/* AP channel switch to new_freq with a 5-beacon countdown. The countdown beacons use a
 * non-hidden "hidnet" head on head_ch; the beacons after the switch use a hidden zero-SSID head
 * (post_hidden) or a non-hidden "hidnet" head on the new channel. */
static int chan_switch(uint32_t new_freq, uint8_t head_ch, int post_hidden)
{
	unsigned char head_csa[256], head_after[256], tail[8];
	int hl_csa, hl_after, rc, o;
	uint16_t off = 4;	/* counter byte inside the CSA IE tail: 37,3,mode,new,count */

	hl_csa = mk_beacon(head_csa, head_ch, "hidnet", 6, -1);
	if (post_hidden)
		hl_after = mk_beacon(head_after, ch_of(new_freq), NULL, 0, -1);
	else
		hl_after = mk_beacon(head_after, ch_of(new_freq), "hidnet", 6, -1);
	tail[0] = 37;
	tail[1] = 3;
	tail[2] = 1;			/* mode */
	tail[3] = ch_of(new_freq);	/* new channel */
	tail[4] = 5;			/* count */

	msg_begin(NL80211_CMD_CHANNEL_SWITCH, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, AP_IDX);
	msg_u32(NL80211_ATTR_WIPHY_FREQ, new_freq);
	msg_u32(NL80211_ATTR_CHANNEL_WIDTH, NL80211_CHAN_WIDTH_20_NOHT);
	msg_u32(NL80211_ATTR_CH_SWITCH_COUNT, 5);
	msg_add(NL80211_ATTR_BEACON_HEAD, head_after, hl_after);
	o = nest_begin(NL80211_ATTR_CSA_IES);
	msg_add(NL80211_ATTR_BEACON_HEAD, head_csa, hl_csa);
	msg_add(NL80211_ATTR_BEACON_TAIL, tail, 5);
	msg_add(NL80211_ATTR_CNTDWN_OFFS_BEACON, &off, 2);
	nest_end(o);
	rc = nl_xact(ctl_fd);
	printf("channel_switch %u rc=%d\n", new_freq, rc);
	return rc;
}

static int reg_frame(uint16_t fc)
{
	int rc;

	msg_begin(NL80211_CMD_REGISTER_FRAME, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, AP_IDX);
	msg_add(NL80211_ATTR_FRAME_TYPE, &fc, 2);
	msg_add(NL80211_ATTR_FRAME_MATCH, NULL, 0);
	rc = nl_xact(apfd);
	printf("register_frame 0x%04x rc=%d\n", fc, rc);
	return rc;
}

static int do_auth(void)
{
	msg_begin(NL80211_CMD_AUTHENTICATE, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, STA_IDX);
	msg_add(NL80211_ATTR_MAC, AP_MAC, 6);
	msg_u32(NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
	msg_add(NL80211_ATTR_SSID, NULL, 0);
	msg_u32(NL80211_ATTR_WIPHY_FREQ, 2437);
	return nl_xact(ctl_fd);
}

static int do_assoc(void)
{
	msg_begin(NL80211_CMD_ASSOCIATE, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, STA_IDX);
	msg_add(NL80211_ATTR_MAC, AP_MAC, 6);
	msg_add(NL80211_ATTR_SSID, NULL, 0);
	msg_u32(NL80211_ATTR_WIPHY_FREQ, 2437);
	return nl_xact(ctl_fd);
}

static int stop_ap(void)
{
	msg_begin(NL80211_CMD_STOP_AP, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, AP_IDX);
	return nl_xact(ctl_fd);
}

static int disconnect(void)
{
	uint16_t reason = 3;

	msg_begin(NL80211_CMD_DISCONNECT, nlfam, REQ_ACK);
	msg_u32(NL80211_ATTR_IFINDEX, STA_IDX);
	msg_add(NL80211_ATTR_REASON_CODE, &reason, 2);
	return nl_xact(ctl_fd);
}

/* Dump the station's scan cache and print each BSS (bssid and frequency). */
static void dump_scan(const char *tag)
{
	static unsigned char dbuf[65536];
	int n, cnt = 0, done = 0;

	msg_begin(NL80211_CMD_GET_SCAN, nlfam, NLM_F_REQUEST | NLM_F_DUMP);
	msg_u32(NL80211_ATTR_IFINDEX, STA_IDX);
	if (nl_send(ctl_fd)) {
		printf("%s: dump send failed\n", tag);
		return;
	}
	while (!done) {
		n = recv(ctl_fd, dbuf, sizeof(dbuf), 0);
		if (n <= 0)
			break;
		for (struct nlmsghdr *nh = (void *)dbuf; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) {
			const unsigned char *at, *bss, *bid, *fq;
			int alen, bl, bbl = 0, ffl = 0;
			uint32_t freq = 0;

			if (nh->nlmsg_type == NLMSG_DONE || nh->nlmsg_type == NLMSG_ERROR) {
				done = 1;
				break;
			}
			at = (unsigned char *)nh + 20;
			alen = nh->nlmsg_len - 20;
			bss = attr_find(at, alen, NL80211_ATTR_BSS, &bl);
			if (!bss)
				continue;
			cnt++;
			bid = attr_find(bss, bl, NL80211_BSS_BSSID, &bbl);
			fq = attr_find(bss, bl, NL80211_BSS_FREQUENCY, &ffl);
			if (fq && ffl >= 4)
				memcpy(&freq, fq, 4);
			{
				int sl = 0, ssl = 0, bil = 0, st_l = 0, seen_l = 0;
				const unsigned char *ies, *st, *seen, *bi;
				const unsigned char *ssid = NULL;
				int ssid_len = -1;
				uint32_t status = 0, seen_ms = 0;

				ies = attr_find(bss, bl, NL80211_BSS_INFORMATION_ELEMENTS, &sl);
				if (ies)
					for (int k = 0; k + 2 <= sl && ies[k] == 0 && k + 2 + ies[k + 1] <= sl; ) {
						ssid = ies + k + 2;
						ssid_len = ies[k + 1];
						break;
					}
				st = attr_find(bss, bl, NL80211_BSS_STATUS, &st_l);
				if (st && st_l >= 4)
					memcpy(&status, st, 4);
				seen = attr_find(bss, bl, NL80211_BSS_SEEN_MS_AGO, &seen_l);
				if (seen && seen_l >= 4)
					memcpy(&seen_ms, seen, 4);
				bi = attr_find(bss, bl, NL80211_BSS_BEACON_IES, &bil);
				printf("%s: bss %d bssid=%02x:%02x:%02x:%02x:%02x:%02x freq=%u ssid_len=%d"
				       " status=%u presp=%d beacon_ies=%d(len %d) seen_ms=%u\n",
				       tag, cnt,
				       bid && bbl >= 6 ? bid[0] : 0, bid && bbl >= 6 ? bid[1] : 0,
				       bid && bbl >= 6 ? bid[2] : 0, bid && bbl >= 6 ? bid[3] : 0,
				       bid && bbl >= 6 ? bid[4] : 0, bid && bbl >= 6 ? bid[5] : 0,
				       freq, ssid_len, status,
				       attr_find(bss, bl, NL80211_BSS_PRESP_DATA, &sl) != NULL,
				       bi != NULL, bil, seen_ms);
				(void)ssid;
			}
		}
	}
	printf("%s: %d entries\n", tag, cnt);
}

static void drain_mlme(void)
{
	static unsigned char eb[16384];

	for (;;) {
		struct pollfd p = {evfd, POLLIN, 0};
		int n;

		if (poll(&p, 1, 0) <= 0)
			return;
		n = recv(evfd, eb, sizeof(eb), 0);
		if (n <= 0)
			return;
		for (struct nlmsghdr *nh = (void *)eb; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) {
			struct genlmsghdr *gh = (void *)((unsigned char *)nh + 16);
			const unsigned char *st;
			int sl = 0;

			if (nh->nlmsg_type != nlfam)
				continue;
			st = attr_find((unsigned char *)nh + 20, nh->nlmsg_len - 20,
				       NL80211_ATTR_STATUS_CODE, &sl);
			printf("mlme event cmd=%u status=%d\n", gh->cmd,
			       st && sl >= 2 ? st[0] | (st[1] << 8) : -1);
		}
	}
}

/* Send probe responses from the AP on freq for ms milliseconds. */
static void ap_probe_burst(int ms, uint32_t freq)
{
	struct timespec t0;
	unsigned char f[256];
	int n;

	clock_gettime(CLOCK_MONOTONIC, &t0);
	do {
		n = mk_probe(f, freq == 2437 ? 6 : 1);
		send_ap_frame(f, n, freq, NULL);
		usleep(20000);
	} while (ms_since(&t0) < ms);
}

int main(void)
{
	int rc;

	setvbuf(stdout, NULL, _IONBF, 0);
	printf("repro: start\n");
	ctl_fd = nl_open();
	apfd = nl_open();
	evfd = nl_open();
	if (get_family()) {
		printf("repro: no nl80211 family\n");
		return 0;
	}
	if (mlme_gid)
		setsockopt(evfd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &mlme_gid, sizeof(mlme_gid));
	printf("repro: nl80211 family=%d mlme group=%d\n", nlfam, mlme_gid);

	AP_IDX = new_iface(0, NL80211_IFTYPE_AP, "hap0");
	STA_IDX = new_iface(1, NL80211_IFTYPE_STATION, "hsta0");
	{
		/* MACs and link state for the two interfaces */
		int s = socket(AF_INET, SOCK_DGRAM, 0);
		struct ifreq ifr;
		const char *names[2] = {"hap0", "hsta0"};
		const uint8_t *macs[2] = {AP_MAC, STA_MAC};

		for (int i = 0; i < 2; i++) {
			memset(&ifr, 0, sizeof(ifr));
			strncpy(ifr.ifr_name, names[i], IFNAMSIZ - 1);
			ifr.ifr_hwaddr.sa_family = 1;	/* ARPHRD_ETHER */
			memcpy(ifr.ifr_hwaddr.sa_data, macs[i], 6);
			if (ioctl(s, SIOCSIFHWADDR, &ifr))
				printf("set mac %s errno=%d\n", names[i], errno);
			if (ioctl(s, SIOCGIFFLAGS, &ifr))
				printf("getflags %s errno=%d\n", names[i], errno);
			ifr.ifr_flags |= IFF_UP;
			if (ioctl(s, SIOCSIFFLAGS, &ifr))
				printf("setflags %s errno=%d\n", names[i], errno);
		}
		close(s);
	}

	/* Stage A: owner O1 (hidden zero-SSID AP beacons on ch1) and member M1 (hidnet probe responses). */
	printf("stage A: owner and member on ch1\n");
	rc = start_ap(2412, "hidnet", 1);
	reg_frame(0x00b0);
	reg_frame(0x0000);
	usleep(300000);
	for (int r = 0; r < 8; r++) {
		trigger_scan(2412, 0);
		ap_probe_burst(250, 2412);
		usleep(50000);
	}
	dump_scan("A");

	/* Stage A2: owner O11 (hidden zero-SSID AP beacons on ch11), used by the second CSA. */
	printf("stage A2: owner on ch11\n");
	stop_ap();
	rc = start_ap(2462, "hidnet", 1);
	reg_frame(0x00b0);
	reg_frame(0x0000);
	usleep(300000);
	for (int r = 0; r < 6; r++) {
		trigger_scan(2462, 0);
		usleep(300000);
	}
	dump_scan("A2");

	/* Stage B: AP restarts on ch6, station scans ch6 for entry O6. */
	printf("stage B: AP on ch6\n");
	stop_ap();
	rc = start_ap(2437, "hidnet", 1);
	reg_frame(0x00b0);
	reg_frame(0x0000);
	usleep(300000);
	for (int r = 0; r < 6; r++) {
		trigger_scan(2437, 0);
		usleep(300000);
	}
	dump_scan("B");

	/* Stage C: associate to the hidden AP on ch6. */
	printf("stage C: associate\n");
	auth_rsp_sent = assoc_rsp_sent = 0;
	rc = do_auth();
	printf("auth rc=%d\n", rc);
	serve_ap(4000, &auth_rsp_sent);
	usleep(300000);
	rc = do_assoc();
	printf("assoc rc=%d\n", rc);
	serve_ap(4000, &assoc_rsp_sent);
	usleep(1000000);
	drain_mlme();
	dump_scan("C");

	/* Stage D: CSA countdown from the AP (SET_BEACON with a CSA IE announcing ch1). */
	printf("stage D: CSA to ch1\n");
	rc = chan_switch(2412, 6, 0);
	usleep(2000000);
	drain_mlme();
	dump_scan("D");

	/* Stage D2: second CSA, to ch11 where O11 is. The leaked cbss (O6) takes O11's IEs and
	 * kfree_rcu()s its own old blob B, which M1 still points at. */
	printf("stage D2: second CSA to ch11\n");
	usleep(1000000);
	rc = chan_switch(2462, 1, 1);
	usleep(2000000);
	drain_mlme();
	dump_scan("D2");
	usleep(1000000);
	dump_scan("D3");

	printf("repro: done\n");
	return 0;
}

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-22 22:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 22:11 [BUG] wifi: cfg80211: CSA onto a hidden SSID group warns in cfg80211_update_assoc_bss_entry() and leaves freed beacon IEs Kyuwon Cho
2026-11-22 22:11 ` [PATCH wireless] wifi: cfg80211: unlink hidden SSID members of the merged BSS on CSA Kyuwon Cho

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®