Skip to content

Commit 0c0cf3d

Browse files
pchelkin91davem330
authored andcommitted
net: macsec: fix double free of percpu stats
Inside macsec_add_dev() we free percpu macsec->secy.tx_sc.stats and macsec->stats on some of the memory allocation failure paths. However, the net_device is already registered to that moment: in macsec_newlink(), just before calling macsec_add_dev(). This means that during unregister process its priv_destructor - macsec_free_netdev() - will be called and will free the stats again. Remove freeing percpu stats inside macsec_add_dev() because macsec_free_netdev() will correctly free the already allocated ones. The pointers to unallocated stats stay NULL, and free_percpu() treats that correctly. Found by Linux Verification Center (linuxtesting.org) with Syzkaller. Fixes: 0a28bfd ("net/macsec: Add MACsec skb_metadata_dst Tx Data path support") Fixes: c09440f ("macsec: introduce IEEE 802.1AE driver") Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru> Reviewed-by: Sabrina Dubroca <sd@queasysnail.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9eed321 commit 0c0cf3d

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

drivers/net/macsec.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,17 +3997,15 @@ static int macsec_add_dev(struct net_device *dev, sci_t sci, u8 icv_len)
39973997
return -ENOMEM;
39983998

39993999
secy->tx_sc.stats = netdev_alloc_pcpu_stats(struct pcpu_tx_sc_stats);
4000-
if (!secy->tx_sc.stats) {
4001-
free_percpu(macsec->stats);
4000+
if (!secy->tx_sc.stats)
40024001
return -ENOMEM;
4003-
}
40044002

40054003
secy->tx_sc.md_dst = metadata_dst_alloc(0, METADATA_MACSEC, GFP_KERNEL);
4006-
if (!secy->tx_sc.md_dst) {
4007-
free_percpu(secy->tx_sc.stats);
4008-
free_percpu(macsec->stats);
4004+
if (!secy->tx_sc.md_dst)
4005+
/* macsec and secy percpu stats will be freed when unregistering
4006+
* net_device in macsec_free_netdev()
4007+
*/
40094008
return -ENOMEM;
4010-
}
40114009

40124010
if (sci == MACSEC_UNDEF_SCI)
40134011
sci = dev_to_sci(dev, MACSEC_PORT_ES);

0 commit comments

Comments
 (0)