tun: flush uc and mc addresses in TUNSETLINK
Subsystems (3)
Eric Dumazet
maintainerPATCH 1/1 tun: flush uc and mc addresses in TUNSETLINK
TUNSETLINK changes dev->type and dev->addr_len, but leaves dev->uc and dev->mc untouched.
Entries added with the old addr_len stay in lists and rb-trees that are now searched and dumped with the new addr_len.
When addr_len grows (eg ARPHRD_NONE -> ARPHRD_IP6GRE), dumps like /proc/net/dev_mcast and RTM_GETMULTICAST expose bytes of netdev_hw_addr that were never initialized.
bonding and team already call dev_uc_flush() and dev_mc_flush() between NETDEV_PRE_TYPE_CHANGE and the type change. Do the same in tun, but only when addr_len changes: if it does not, existing entries are still valid and there is no reason to drop them.
---
Cc: Henry Martin <[email protected]>
Cc: Willem de Bruijn <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: Andrew Lunn <[email protected]>
--- drivers/net/tun.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/net/tun.c +8 -1
@@ -3379,6 +3379,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, (unsigned short)arg == ARPHRD_MCTP) { ret = -EINVAL; } else { +unsigned char addr_len =tun_get_addr_len (arg); + ret =call_netdevice_notifiers (NETDEV_PRE_TYPE_CHANGE , tun->dev); ret =notifier_to_errno (ret);
@@ -3387,8 +3389,13 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, "Refused to change device type\n");break ; } +/* Addresses were added with the old dev->addr_len. */ +if (addr_len != tun->dev->addr_len) { +dev_uc_flush (tun->dev); +dev_mc_flush (tun->dev); + } tun->dev->type = (int ) arg; - tun->dev->addr_len =tun_get_addr_len (tun->dev->type); + tun->dev->addr_len = addr_len;netif_info (tun, drv, tun->dev,"linktype set to %d\n" , tun->dev->type); call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
Signature · 1 line
2.53.0
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Eric Dumazet
maintainerQuoted 9 lines · Le mer. 18 nov. 2026 à 13:58, <[email protected]> a écrit :
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
The issue was reported by Henry Martin, see the Link: tag:
https://lore.kernel.org/netdev/[email protected]/
I wrote this patch after reviewing his, by code inspection:
TUNSETLINK is missing the dev_uc_flush()/dev_mc_flush() calls that bonding and team perform when they change the device type.
Quoted 5 lines
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Henry reported triggering it on v7.4-rc3: after TUNSETLINK from ARPHRD_NONE to ARPHRD_IP6GRE, RTM_GETMULTICAST and /proc/net/dev_mcast exposed uninitialized bytes of netdev_hw_addr, including kernel heap pointers. Details are in his v2:
https://lore.kernel.org/netdev/[email protected]/
There is no splat, this is an information leak.
Quoted 9 lines
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages. --
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Quoted 10 lines · Eric Dumazet
TUNSETLINK changes
dev->typeanddev->addr_len, but leavesdev->ucanddev->mcuntouched.Entries added with the old
addr_lenstay in lists and rb-trees that are now searched and dumped with the newaddr_len.When
addr_lengrows (egARPHRD_NONE->ARPHRD_IP6GRE), dumps like/proc/net/dev_mcastandRTM_GETMULTICASTexpose bytes ofnetdev_hw_addrthat were never initialized.
Quoted 8 lines
bonding and team already call
dev_uc_flush()anddev_mc_flush()betweenNETDEV_PRE_TYPE_CHANGEand the type change. Do the same in tun, but only whenaddr_lenchanges: if it does not, existing entries are still valid and there is no reason to drop them.Fixes cca8ea3b05c9 ("net: tun: set tun->dev->addr_len during TUNSETLINK processing")Signed-off-by Eric Dumazet <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix's own claim is still reachable through a race, so uninitialized kernel memory can still leak.
- [Medium] The new dev_uc_flush()/dev_mc_flush() in TUNSETLINK (drivers/net/tun.c:3393-3396) frees every netdev_hw_addr in…
--
tun: flush uc and mc addresses in TUNSETLINK
When the new link type changes dev->addr_len, this patch makes TUNSETLINK flush dev->uc and dev->mc. Entries created with the old addr_len would otherwise be searched and dumped with the new length. This follows what bonding and team do between NETDEV_PRE_TYPE_CHANGE and the type change.
Quoted 14 lines
drivers/net/tun.c +2
-0@@ -3379,6 +3379,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, (unsigned short)arg == ARPHRD_MCTP) { ret = -EINVAL; } else { +unsigned char addr_len =tun_get_addr_len (arg); + ret =call_netdevice_notifiers (NETDEV_PRE_TYPE_CHANGE , tun->dev); ret =notifier_to_errno (ret);
drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3396
Quoted 8 lines
drivers/net/tun.c +5
-0@@ -3387,8 +3389,13 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, "Refused to change device type\n");break ; } +/* Addresses were added with the old dev->addr_len. */ +if (addr_len != tun->dev->addr_len) { +dev_uc_flush (tun->dev); +dev_mc_flush (tun->dev); + }
[Severity: Medium]
Can this flush break the reference bookkeeping of holders other than IPv4 and IPv6?
dev_uc_flush() and dev_mc_flush() free every netdev_hw_addr no matter what its refcount, sync_cnt or global_use is. Only IPv4 and addrconf (ipv6_mc_unmap()/ipv6_mc_remap()) give back and re-take their references across PRE/POST_TYPE_CHANGE.
Upper devices that sync into a tap do not veto NETDEV_PRE_TYPE_CHANGE on the lower device. Two examples are bonding, via bond_set_rx_mode() -> dev_uc_sync()/dev_mc_sync(), and macsec, via dev_uc_add(real_dev, dev->dev_addr) in macsec_dev_open() and the syncs in macsec_dev_set_rx_mode(). After the flush, the upper device's entries still have sync_cnt == 1 and the extra refcount.
On bond failover, bond_hw_addr_swap() -> bond_hw_addr_flush() -> dev_mc_unsync() reaches:
net/core/dev_addr_lists.c:__hw_addr_unsync_one() {
err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
false, true);
if (err)
return;
ha->sync_cnt--;
}This returns early on -ENOENT. __hw_addr_sync() then skips the entry for the new active slave, because sync_cnt is still non-zero:
if (!ha->sync_cnt) {
err = __hw_addr_sync_one(to_list, ha, addr_len);
...Would the bond's uc/mc addresses then never be synced to the new active slave?
The same applies to AF_PACKET PACKET_ADD_MEMBERSHIP. The packet_mclist entry survives the flush. If addr_len later returns to its original value (for example ETHER -> NONE -> ETHER), packet_dev_mc() passes its check on close or drop:
if (i->alen != dev->addr_len) return -EINVAL; ... return dev_mc_del(dev, i->addr);
Can this plain delete in __hw_addr_del_entry() decrement, or even free, an entry that now belongs to another holder (SIOCADDMULTI, IPv6, or an upper device's sync)?
bonding and team only flush their own master lists, and only when they have no lower ports, so no downstream sync state exists at that point. tun can be a lower device and has no such check. Should it handle this case differently?
drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3398
Quoted 3 lines
drivers/net/tun.c +1
-1tun->dev->type = (int ) arg; - tun->dev->addr_len =tun_get_addr_len (tun->dev->type); + tun->dev->addr_len = addr_len;
[Severity: High]
Is the uninitialized-byte exposure described in the commit message still reachable through a race here?
dev_uc_flush() and dev_mc_flush() each take and drop netif_addr_lock_bh. The dev->type and dev->addr_len stores come afterwards, as plain stores outside that lock. __dev_mc_add() reads dev->addr_len under the addr lock:
err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
IPV6_ADD_MEMBERSHIP does not take RTNL. do_ipv6_setsockopt() only does sockopt_lock_sock(), and the join path is:
__ipv6_sock_mc_join()
ip6_mc_find_dev()
__ipv6_dev_mc_inc() /* holds only idev->mc_lock */
igmp6_group_added()
ndisc_mc_map(&mc->mca_addr, buf, dev, 0) /* uses dev->type */
dev_mc_add(dev, buf)igmp6_group_added() does this before its IFF_UP check, so it also runs on the down device that TUNSETLINK requires.
Take a tap switching from ARPHRD_ETHER (addr_len 6) to ARPHRD_IP6GRE (addr_len 16):
TUNSETLINK IPV6_ADD_MEMBERSHIPPRE_TYPE_CHANGE (ipv6_mc_unmap)dev_mc_flush()
mutex_lock(&idev->mc_lock)
ndisc_mc_map() fills 6 bytes
__dev_mc_add() reads addr_len == 6
__hw_addr_create(): kmalloc(),
memcpy() of 6 bytesdev->addr_len = 16POST_TYPE_CHANGE (ipv6_mc_remap)
igmp6_group_added() skips the
group, MAF_LOADED already set
The entry survives. Afterwards, dev_mc_seq_show() (/proc/net/dev_mcast) and dev_mc_fill_addr() (RTM_GETMULTICAST) copy dev->addr_len bytes of ha->addr to user space:
nla_put(skb, IFA_MULTICAST, dev->addr_len, ha->addr) ||
Since __hw_addr_create() does not zero the allocation, wouldn't 10 of those 16 bytes be uninitialized slab memory?
There is also a stack variant. idev->mc_lock is a mutex, so the joining thread can be preempted after ndisc_mc_map() has written only 6 bytes of the on-stack buf[MAX_ADDR_LEN]. If TUNSETLINK then flushes and stores addr_len = 16, its POST remap blocks on mc_lock. When the join resumes, dev_mc_add() copies 16 bytes from buf into a long-lived netdev_hw_addr, 10 of them uninitialized stack.
Would the flush and the dev->type/dev->addr_len update need to be atomic under netif_addr_lock_bh? Would mappers that build the address from dev->type also need to agree with the addr_len used for the copy, for example by zero-initializing buf in igmp6_group_added() or re-checking under the lock?
Quoted 3 lines
drivers/net/tun.c +0
-0netif_info (tun, drv, tun->dev,"linktype set to %d\n" , tun->dev->type); call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118125443.1137177-1-edumazet%40kernel.org
Eric Dumazet
maintainerQuoted 98 lines · Le jeu. 19 nov. 2026 à 23:17, <[email protected]> a écrit :
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix's own claim is still reachable through a race, so uninitialized kernel memory can still leak.
- [Medium] The new dev_uc_flush()/dev_mc_flush() in TUNSETLINK (drivers/net/tun.c:3393-3396) frees everynetdev_hw_addrin…--
tun: flush uc and mc addresses in TUNSETLINK
When the new link type changes
dev->addr_len, this patch makes TUNSETLINK flushdev->ucanddev->mc. Entries created with the oldaddr_lenwould otherwise be searched and dumped with the new length. This follows what bonding and team do betweenNETDEV_PRE_TYPE_CHANGEand the type change.Quoted 14 lines
drivers/net/tun.c +2
-0@@ -3379,6 +3379,8 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, (unsigned short)arg == ARPHRD_MCTP) { ret = -EINVAL; } else { +unsigned char addr_len =tun_get_addr_len (arg); + ret =call_netdevice_notifiers (NETDEV_PRE_TYPE_CHANGE , tun->dev); ret =notifier_to_errno (ret);drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3396
Quoted 8 lines
drivers/net/tun.c +5
-0@@ -3387,8 +3389,13 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd, "Refused to change device type\n");break ; } +/* Addresses were added with the old dev->addr_len. */ +if (addr_len != tun->dev->addr_len) { +dev_uc_flush (tun->dev); +dev_mc_flush (tun->dev); + }[Severity: Medium]
Can this flush break the reference bookkeeping of holders other than IPv4 and IPv6?
dev_uc_flush()anddev_mc_flush()free everynetdev_hw_addrno matter what its refcount,sync_cntorglobal_useis. Only IPv4 and addrconf (ipv6_mc_unmap()/ipv6_mc_remap()) give back and re-take their references across PRE/POST_TYPE_CHANGE.Upper devices that sync into a tap do not veto
NETDEV_PRE_TYPE_CHANGEon the lower device. Two examples are bonding, viabond_set_rx_mode()-> dev_uc_sync()/dev_mc_sync(), and macsec, viadev_uc_add(real_dev, dev->dev_addr)inmacsec_dev_open()and the syncs inmacsec_dev_set_rx_mode(). After the flush, the upper device's entries still havesync_cnt== 1 and the extra refcount.On bond failover,
bond_hw_addr_swap()->bond_hw_addr_flush()->dev_mc_unsync()reaches:net/core/dev_addr_lists.c:__hw_addr_unsync_one() { err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type, false, true); if (err) return; ha->sync_cnt--; }This returns early on
-ENOENT.__hw_addr_sync()then skips the entry for the new active slave, becausesync_cntis still non-zero:if (!ha->sync_cnt) { err = __hw_addr_sync_one(to_list, ha, addr_len); ...Would the bond's uc/mc addresses then never be synced to the new active slave?
The same applies to
AF_PACKETPACKET_ADD_MEMBERSHIP. Thepacket_mclistentry survives the flush. Ifaddr_lenlater returns to its original value (for example ETHER -> NONE -> ETHER),packet_dev_mc()passes its check on close or drop:if (i->alen != dev->addr_len) return -EINVAL; ... return dev_mc_del(dev, i->addr);
Quoted 8 lines
Can this plain delete in
__hw_addr_del_entry()decrement, or even free, an entry that now belongs to another holder (SIOCADDMULTI, IPv6, or an upper device's sync)?bonding and team only flush their own master lists, and only when they have no lower ports, so no downstream sync state exists at that point. tun can be a lower device and has no such check. Should it handle this case differently?
This is not new with this patch.
Without the flush, the entries synced by the upper device stay in the tap lists, but dev_uc_unsync()/dev_mc_unsync() then look them up with the new addr_len of the tap. When addr_len changed, the lookup compares the wrong number of bytes, so it either fails (and sync_cnt on the upper device is stuck the same way) or matches an unrelated entry.
Changing the link type of a tap that has upper devices makes no sense
anyway: bonding and team check the type when the port is added, and team already vetoes NETDEV_PRE_TYPE_CHANGE on its ports.
I think the right fix is to refuse TUNSETLINK when the device has upper devices. This is a behavior change, I will send it as a separate patch.
The AF_PACKET case needs addr_len to change and then come back to its original value while the socket keeps its membership. bonding and team have the same limitation, I do not plan to address it here.
Quoted 61 lines
drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3398
Quoted 4 lines
drivers/net/tun.c +1
-1tun->dev->type = (int ) arg; - tun->dev->addr_len =tun_get_addr_len (tun->dev->type); + tun->dev->addr_len = addr_len;[Severity: High]
Is the uninitialized-byte exposure described in the commit message still reachable through a race here?
dev_uc_flush()anddev_mc_flush()each take and dropnetif_addr_lock_bh. Thedev->typeanddev->addr_lenstores come afterwards, as plain stores outside that lock.__dev_mc_add()readsdev->addr_lenunder the addr lock:err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
IPV6_ADD_MEMBERSHIPdoes not take RTNL.do_ipv6_setsockopt()only doessockopt_lock_sock(), and the join path is:
__ipv6_sock_mc_join()
ip6_mc_find_dev()
__ipv6_dev_mc_inc()/* holds onlyidev->mc_lock*/igmp6_group_added() ndisc_mc_map(&mc->mca_addr, buf, dev, 0) /* uses dev->type */ dev_mc_add(dev, buf)
igmp6_group_added()does this before itsIFF_UPcheck, so it also runs on the down device that TUNSETLINK requires.Take a tap switching from
ARPHRD_ETHER(addr_len6) toARPHRD_IP6GRE(addr_len16):TUNSETLINK
IPV6_ADD_MEMBERSHIPPRE_TYPE_CHANGE(ipv6_mc_unmap)dev_mc_flush()mutex_lock(&idev->mc_lock) ndisc_mc_map() fills 6 bytes __dev_mc_add() reads addr_len == 6 __hw_addr_create(): kmalloc(), memcpy() of 6 bytes
dev->addr_len= 16POST_TYPE_CHANGE(ipv6_mc_remap)
igmp6_group_added()skips the
group,MAF_LOADEDalready setThe entry survives. Afterwards,
dev_mc_seq_show()(/proc/net/dev_mcast) anddev_mc_fill_addr()(RTM_GETMULTICAST) copydev->addr_lenbytes ofha->addrto user space:nla_put(skb, IFA_MULTICAST, dev->addr_len, ha->addr) ||Since
__hw_addr_create()does not zero the allocation, wouldn't 10 of those 16 bytes be uninitialized slab memory?There is also a stack variant.
idev->mc_lockis a mutex, so the joining thread can be preempted afterndisc_mc_map()has written only 6 bytes of the on-stack buf[MAX_ADDR_LEN]. If TUNSETLINK then flushes and storesaddr_len= 16, its POST remap blocks onmc_lock. When the join resumes,dev_mc_add()copies 16 bytes from buf into a long-livednetdev_hw_addr, 10 of them uninitialized stack.
Quoted 5 lines
Would the flush and the dev->type/dev->addr_len update need to be atomic under
netif_addr_lock_bh? Would mappers that build the address fromdev->typealso need to agree with theaddr_lenused for the copy, for example by zero-initializing buf inigmp6_group_added()or re-checking under the lock?
The race is real, but it is not specific to tun, and not added by this patch.
Since commit 1767bb2d47b7 ("ipv6: mcast: Don't hold RTNL for IPV6_ADD_MEMBERSHIP and MCAST_JOIN_GROUP"), igmp6_group_added() reads dev->type and dev->addr_len without RTNL, while every driver changing them (tun, bonding, team, qmi_wwan, hdlc) only holds RTNL.
Doing the flush and the stores under netif_addr_lock_bh() in tun would not close it: ndisc_mc_map() reads dev->type before dev_mc_add() takes that lock.
The heap variant is closed by Henry's patch, which zeroes the tail of ha->addr in __hw_addr_create(), whatever the path or the race:
https://lore.kernel.org/netdev/[email protected]/
The stack variant (buf[] partially filled by ndisc_mc_map(), then copied with a larger addr_len) needs a fix in net/ipv6/mcast.c. I will send a patch zero-initializing buf in igmp6_group_added().
IPv4 is not affected, the IGMP paths still run under RTNL.
So I am not planning a respin of this patch: it removes the stale entries in the non racy case, and the two remaining points are addressed by separate patches.
Quoted 7 lines
drivers/net/tun.c static long __tun_chr_ioctl(struct file *file, unsigned int cmd, · line 3401
Quoted 4 lines
drivers/net/tun.c +0
-0netif_info (tun, drv, tun->dev,"linktype set to %d\n" , tun->dev->type); call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118125443.1137177-1-edumazet%40kernel.org
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Wed, 18 Nov 2026 13:54:43 +0100 you wrote:
TUNSETLINK changes
dev->typeanddev->addr_len, but leavesdev->ucanddev->mcuntouched.Entries added with the old
addr_lenstay in lists and rb-trees that are now searched and dumped with the newaddr_len.When
addr_lengrows (egARPHRD_NONE->ARPHRD_IP6GRE), dumps like/proc/net/dev_mcastandRTM_GETMULTICASTexpose bytes ofnetdev_hw_addrthat were never initialized.[...]
Here is the summary with links:
- [net] tun: flush uc and mc addresses in TUNSETLINK
https://git.kernel.org/netdev/net/c/5677cf0e039b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html