net: fix skb use after free in netpoll_send_skb_on_dev
Subsystems (2)
PATCH 1/1 net: fix skb use after free in netpoll_send_skb_on_dev
After commit baeababb5b85d5c4e6c917efe2a1504179438d3b
("tun: return NET_XMIT_DROP for dropped packets"),
when tun_net_xmit drop packets, it will free skb and return NET_XMIT_DROP, netpoll_send_skb_on_dev will run into two use after free cases:
1. retry netpoll_start_xmit with freed skb;
2. queue freed skb in npinfo->txq.
hit the first case with following kernel log:
[ 117.864773] kernel BUG at mm/slub.c:306! [ 117.864773] invalid opcode: 0000 [#1] SMP PTI [ 117.864774] CPU: 3 PID: 2627 Comm: loop_printmsg Kdump: loaded Tainted: P OE 5.3.0-050300rc5-generic #201908182231 [ 117.864775] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014 [ 117.864775] RIP: 0010:kmem_cache_free+0x28d/0x2b0 [ 117.864781] Call Trace: [ 117.864781] ? tun_net_xmit+0x21c/0x460 [ 117.864781] kfree_skbmem+0x4e/0x60 [ 117.864782] kfree_skb+0x3a/0xa0 [ 117.864782] tun_net_xmit+0x21c/0x460 [ 117.864782] netpoll_start_xmit+0x11d/0x1b0 [ 117.864788] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864789] __br_forward+0x1b9/0x1e0 [bridge] [ 117.864789] ? skb_clone+0x53/0xd0 [ 117.864790] ? __skb_clone+0x2e/0x120 [ 117.864790] deliver_clone+0x37/0x50 [bridge] [ 117.864790] maybe_deliver+0x89/0xc0 [bridge] [ 117.864791] br_flood+0x6c/0x130 [bridge] [ 117.864791] br_dev_xmit+0x315/0x3c0 [bridge] [ 117.864792] netpoll_start_xmit+0x11d/0x1b0 [ 117.864792] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864792] netpoll_send_udp+0x2c6/0x3e8 [ 117.864793] write_msg+0xd9/0xf0 [netconsole] [ 117.864793] console_unlock+0x386/0x4e0 [ 117.864793] vprintk_emit+0x17e/0x280 [ 117.864794] vprintk_default+0x29/0x50 [ 117.864794] vprintk_func+0x4c/0xbc [ 117.864794] printk+0x58/0x6f [ 117.864795] loop_fun+0x24/0x41 [printmsg_loop] [ 117.864795] kthread+0x104/0x140 [ 117.864795] ? 0xffffffffc05b1000 [ 117.864796] ? kthread_park+0x80/0x80 [ 117.864796] ret_from_fork+0x35/0x40
--- net/core/netpoll.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/netpoll.c +2 -2
@@ -335,7 +335,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,HARD_TX_UNLOCK (dev, txq); -if (status ==NETDEV_TX_OK ) +if (status ==NETDEV_TX_OK || status ==NET_XMIT_DROP )break ; }
@@ -352,7 +352,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, } -if (status !=NETDEV_TX_OK ) { +if (status !=NETDEV_TX_OK && status !=NET_XMIT_DROP ) {skb_queue_tail (&npinfo->txq, skb);schedule_delayed_work (&npinfo->tx_work,0 ); }
Signature · 1 line
2.7.4
David Miller
maintainerFrom: Feng Sun <[email protected]>
Date: Sat, 24 Aug 2019 00:32:00 +0800
Quoted 4 lines
After commit
baeababb5b85d5c4e6c917efe2a1504179438d3b
("tun: returnNET_XMIT_DROPfor dropped packets"),
whentun_net_xmitdrop packets, it will free skb and returnNET_XMIT_DROP,netpoll_send_skb_on_devwill run into two use after free cases:
I don't know what to do here.
Really, the intention of the design is that the only valid ->ndo_start_xmit() values are those with macro names fitting the pattern NETDEV_TX_*, which means only NETDEV_TX_OK and NETDEV_TX_BUSY are valid.
NET_XMIT_* values are for qdisc ->enqueue() methods.
Note, particularly, that when ->ndo_start_xmit() values are propagated through ->enqueue() calls they get masked out with NET_XMIT_MASK.
However, I see that most of the code doing enqueueing and invocation of ->ndo_start_xmit() use the dev_xmit_complete() helper to check this condition.
So probably that is what netpoll should be using as well.
PATCH v2 1/1 net: fix skb use after free in netpoll
In reply to Feng SunAfter commit baeababb5b85d5c4e6c917efe2a1504179438d3b
("tun: return NET_XMIT_DROP for dropped packets"),
when tun_net_xmit drop packets, it will free skb and return NET_XMIT_DROP, netpoll_send_skb_on_dev will run into following use after free cases:
1. retry netpoll_start_xmit with freed skb;
2. queue freed skb in npinfo->txq.queue_process will also run into use after free case.
hit netpoll_send_skb_on_dev first case with following kernel log:
[ 117.864773] kernel BUG at mm/slub.c:306! [ 117.864773] invalid opcode: 0000 [#1] SMP PTI [ 117.864774] CPU: 3 PID: 2627 Comm: loop_printmsg Kdump: loaded Tainted: P OE 5.3.0-050300rc5-generic #201908182231 [ 117.864775] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014 [ 117.864775] RIP: 0010:kmem_cache_free+0x28d/0x2b0 [ 117.864781] Call Trace: [ 117.864781] ? tun_net_xmit+0x21c/0x460 [ 117.864781] kfree_skbmem+0x4e/0x60 [ 117.864782] kfree_skb+0x3a/0xa0 [ 117.864782] tun_net_xmit+0x21c/0x460 [ 117.864782] netpoll_start_xmit+0x11d/0x1b0 [ 117.864788] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864789] __br_forward+0x1b9/0x1e0 [bridge] [ 117.864789] ? skb_clone+0x53/0xd0 [ 117.864790] ? __skb_clone+0x2e/0x120 [ 117.864790] deliver_clone+0x37/0x50 [bridge] [ 117.864790] maybe_deliver+0x89/0xc0 [bridge] [ 117.864791] br_flood+0x6c/0x130 [bridge] [ 117.864791] br_dev_xmit+0x315/0x3c0 [bridge] [ 117.864792] netpoll_start_xmit+0x11d/0x1b0 [ 117.864792] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864792] netpoll_send_udp+0x2c6/0x3e8 [ 117.864793] write_msg+0xd9/0xf0 [netconsole] [ 117.864793] console_unlock+0x386/0x4e0 [ 117.864793] vprintk_emit+0x17e/0x280 [ 117.864794] vprintk_default+0x29/0x50 [ 117.864794] vprintk_func+0x4c/0xbc [ 117.864794] printk+0x58/0x6f [ 117.864795] loop_fun+0x24/0x41 [printmsg_loop] [ 117.864795] kthread+0x104/0x140 [ 117.864795] ? 0xffffffffc05b1000 [ 117.864796] ? kthread_park+0x80/0x80 [ 117.864796] ret_from_fork+0x35/0x40
---
Changes in v2:
- change commit and title
- add netpoll_xmit_complete helper
- add one more return value check of netpoll_start_xmit
--- net/core/netpoll.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
net/core/netpoll.c +8 -3
@@ -95,6 +95,11 @@ static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,return status; } +static inline bool netpoll_xmit_complete (int rc) +{ +return dev_xmit_complete (rc); +} +static void queue_process (struct work_struct *work) { struct netpoll_info *npinfo =
@@ -122,7 +127,7 @@ static void queue_process(struct work_struct *work) txq =netdev_get_tx_queue (dev, q_index);HARD_TX_LOCK (dev, txq,smp_processor_id ());if (netif_xmit_frozen_or_stopped (txq) || -netpoll_start_xmit (skb, dev, txq) !=NETDEV_TX_OK ) { + !netpoll_xmit_complete (netpoll_start_xmit (skb, dev, txq))) {skb_queue_head (&npinfo->txq, skb);HARD_TX_UNLOCK (dev, txq);local_irq_restore (flags);
@@ -335,7 +340,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,HARD_TX_UNLOCK (dev, txq); -if (status ==NETDEV_TX_OK ) +if (netpoll_xmit_complete (status))break ; }
@@ -352,7 +357,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, } -if (status !=NETDEV_TX_OK ) { +if (!netpoll_xmit_complete (status)) {skb_queue_tail (&npinfo->txq, skb);schedule_delayed_work (&npinfo->tx_work,0 ); }
Signature · 1 line
2.7.4
David Miller
maintainerRe: [PATCH v2] net: fix skb use after free in netpoll
From: Feng Sun <[email protected]>
Date: Mon, 26 Aug 2019 14:13:40 +0800
net/core/netpoll.c static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev, · line 101
Quoted 4 lines
net/core/netpoll.c +4
-0+static inline bool netpoll_xmit_complete (int rc) +{ +return dev_xmit_complete (rc); +}
There is no need for this useless indirection, just call dev_xmit_complete() staright.
Also, even if it was suitable, never use the inline keyword in foo.c files.
PATCH v3 1/1 net: fix skb use after free in netpoll
After commit baeababb5b85d5c4e6c917efe2a1504179438d3b
("tun: return NET_XMIT_DROP for dropped packets"),
when tun_net_xmit drop packets, it will free skb and return NET_XMIT_DROP, netpoll_send_skb_on_dev will run into following use after free cases:
1. retry netpoll_start_xmit with freed skb;
2. queue freed skb in npinfo->txq.queue_process will also run into use after free case.
hit netpoll_send_skb_on_dev first case with following kernel log:
[ 117.864773] kernel BUG at mm/slub.c:306! [ 117.864773] invalid opcode: 0000 [#1] SMP PTI [ 117.864774] CPU: 3 PID: 2627 Comm: loop_printmsg Kdump: loaded Tainted: P OE 5.3.0-050300rc5-generic #201908182231 [ 117.864775] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Ubuntu-1.8.2-1ubuntu1 04/01/2014 [ 117.864775] RIP: 0010:kmem_cache_free+0x28d/0x2b0 [ 117.864781] Call Trace: [ 117.864781] ? tun_net_xmit+0x21c/0x460 [ 117.864781] kfree_skbmem+0x4e/0x60 [ 117.864782] kfree_skb+0x3a/0xa0 [ 117.864782] tun_net_xmit+0x21c/0x460 [ 117.864782] netpoll_start_xmit+0x11d/0x1b0 [ 117.864788] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864789] __br_forward+0x1b9/0x1e0 [bridge] [ 117.864789] ? skb_clone+0x53/0xd0 [ 117.864790] ? __skb_clone+0x2e/0x120 [ 117.864790] deliver_clone+0x37/0x50 [bridge] [ 117.864790] maybe_deliver+0x89/0xc0 [bridge] [ 117.864791] br_flood+0x6c/0x130 [bridge] [ 117.864791] br_dev_xmit+0x315/0x3c0 [bridge] [ 117.864792] netpoll_start_xmit+0x11d/0x1b0 [ 117.864792] netpoll_send_skb_on_dev+0x1b8/0x200 [ 117.864792] netpoll_send_udp+0x2c6/0x3e8 [ 117.864793] write_msg+0xd9/0xf0 [netconsole] [ 117.864793] console_unlock+0x386/0x4e0 [ 117.864793] vprintk_emit+0x17e/0x280 [ 117.864794] vprintk_default+0x29/0x50 [ 117.864794] vprintk_func+0x4c/0xbc [ 117.864794] printk+0x58/0x6f [ 117.864795] loop_fun+0x24/0x41 [printmsg_loop] [ 117.864795] kthread+0x104/0x140 [ 117.864795] ? 0xffffffffc05b1000 [ 117.864796] ? kthread_park+0x80/0x80 [ 117.864796] ret_from_fork+0x35/0x40
---
Changes in v3:
- use dev_xmit_complete directly
Changes in v2:
- change commit and title
- add netpoll_xmit_complete helper
- add one more return value check of netpoll_start_xmit
--- net/core/netpoll.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/netpoll.c +3 -3
@@ -122,7 +122,7 @@ static void queue_process(struct work_struct *work) txq =netdev_get_tx_queue (dev, q_index);HARD_TX_LOCK (dev, txq,smp_processor_id ());if (netif_xmit_frozen_or_stopped (txq) || -netpoll_start_xmit (skb, dev, txq) !=NETDEV_TX_OK ) { + !dev_xmit_complete (netpoll_start_xmit (skb, dev, txq))) {skb_queue_head (&npinfo->txq, skb);HARD_TX_UNLOCK (dev, txq);local_irq_restore (flags);
@@ -335,7 +335,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,HARD_TX_UNLOCK (dev, txq); -if (status ==NETDEV_TX_OK ) +if (dev_xmit_complete (status))break ; }
@@ -352,7 +352,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, } -if (status !=NETDEV_TX_OK ) { +if (!dev_xmit_complete (status)) {skb_queue_tail (&npinfo->txq, skb);schedule_delayed_work (&npinfo->tx_work,0 ); }
Signature · 1 line
2.7.4
David Miller
maintainerRe: [PATCH v3] net: fix skb use after free in netpoll
From: Feng Sun <[email protected]>
Date: Mon, 26 Aug 2019 14:46:04 +0800
Quoted 9 lines
After commit
baeababb5b85d5c4e6c917efe2a1504179438d3b
("tun: returnNET_XMIT_DROPfor dropped packets"),
whentun_net_xmitdrop packets, it will free skb and returnNET_XMIT_DROP,netpoll_send_skb_on_devwill run into following use after free cases:
1. retrynetpoll_start_xmitwith freed skb;
2. queue freed skb innpinfo->txq.queue_processwill also run into use after free case.hit
netpoll_send_skb_on_devfirst case with following kernel log:
...
Quoted 2 lines
Signed-off-by Feng Sun <[email protected]>Signed-off-by Xiaojun Zhao <[email protected]>
Applied and queued up for -stable.