net: fec: Coverity issue: Dereference null return value
Subsystems (3)
[email protected]
maintainerPATCH 1/1 net: fec: Coverity issue: Dereference null return value
From: Wei Fang <[email protected]>
The build_skb might return a null pointer but there is no check on the return value in the fec_enet_rx_queue(). So a null pointer dereference might occur. To avoid this, we check the return value of build_skb. If the return value is a null pointer, the driver will recycle the page and update the statistic of ndev. Then jump to rx_processing_done to clear the status flags of the BD so that the hardware can recycle the BD.
--- drivers/net/ethernet/freescale/fec_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/net/ethernet/freescale/fec_main.c +10 -0
@@ -1674,6 +1674,16 @@ fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)* bridging applications. */ skb =build_skb (page_address (page),PAGE_SIZE ); +if (unlikely (!skb)) { +page_pool_recycle_direct (rxq->page_pool, page); + ndev->stats.rx_packets--; + ndev->stats.rx_bytes -= pkt_len; + ndev->stats.rx_dropped++; + +netdev_err (ndev,"build_skb failed!\n" ); + goto rx_processing_done; + } +skb_reserve (skb, data_start);skb_put (skb, pkt_len - sub_len);skb_mark_for_recycle (skb);
Signature · 1 line
2.25.1
Quoted 20 lines · On Thu, 2022-12-15 at 17:11 +0800, [email protected] wrote:
From: Wei Fang <[email protected]>
The
build_skbmight return a null pointer but there is no check on the return value in thefec_enet_rx_queue(). So a null pointer dereference might occur. To avoid this, we check the return value ofbuild_skb. If the return value is a null pointer, the driver will recycle the page and update the statistic of ndev. Then jump torx_processing_doneto clear the status flags of the BD so that the hardware can recycle the BD.Signed-off-by Wei Fang <[email protected]>Reviewed-by Shenwei Wang <[email protected]>--- drivers/net/ethernet/freescale/fec_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/net/ethernet/freescale/fec_main.c +0
-0
drivers/net/ethernet/freescale/fec_main.c fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) · line 1681
Quoted 8 lines
drivers/net/ethernet/freescale/fec_main.c +5
-0@@ -1674,6 +1674,16 @@ fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)* bridging applications. */ skb =build_skb (page_address (page),PAGE_SIZE ); +if (unlikely (!skb)) { +page_pool_recycle_direct (rxq->page_pool, page); + ndev->stats.rx_packets--; + ndev->stats.rx_bytes -= pkt_len; + ndev->stats.rx_dropped++;
I'm not sure you really need to bother with rewinding the rx_packets and rx_bytes counters. I know that the rx_dropped statistic will get incremented in the network stack in the event of a packet failing to enqueue to the backlog, so it might be better to just leave the rx_packets counter as is and assume the actual packet count is rx_packets - rx_dropped.
drivers/net/ethernet/freescale/fec_main.c fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) · line 1683
Quoted 2 lines
drivers/net/ethernet/freescale/fec_main.c +2
-0+ +netdev_err (ndev,"build_skb failed!\n" );
Instead of netdev_err you may want to consider netdev_err_once for this. Generally speaking when we start seeing memory allocation error issues they can get very noisy very quickly as you are likely to fail the allocation for every packet in a given polling session, and sessions to follow.
Quoted 6 lines
drivers/net/ethernet/freescale/fec_main.c +3
-0+ goto rx_processing_done; + } +skb_reserve (skb, data_start);skb_put (skb, pkt_len - sub_len);skb_mark_for_recycle (skb);
Wei Fang
maintainerQuoted 34 lines
-----Original Message-----
From: Alexander H Duyck <[email protected]>
Sent: 2022年12月16日 23:34
To: Wei Fang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Clark Wang <[email protected]>; Shenwei Wang <[email protected]>; dl-linux-imx <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH net] net: fec: Coverity issue: Dereference null return valuedrivers/net/ethernet/freescale/fec_main.c fec_enet_rx_queue(struct net_device *ndev, int
Quoted 21 lines · On Thu, 2022-12-15 at 17:11 +0800, [email protected] wrote:
From: Wei Fang <[email protected]>
The
build_skbmight return a null pointer but there is no check on the return value in thefec_enet_rx_queue(). So a null pointer dereference might occur. To avoid this, we check the return value ofbuild_skb. If the return value is a null pointer, the driver will recycle the page and update the statistic of ndev. Then jump torx_processing_doneto clear the status flags of the BD so that the hardware can recycle the BD.Signed-off-by Wei Fang <[email protected]>Reviewed-by Shenwei Wang <[email protected]>--- drivers/net/ethernet/freescale/fec_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+)drivers/net/ethernet/freescale/fec_main.c +0
-0@@ -1674,6 +1674,16 @@ fec_enet_rx_queue(struct net_device *ndev, intbudget, u16
queue_id)
Quoted 15 lines
drivers/net/ethernet/freescale/fec_main.c fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) · line 1681
Quoted 8 lines
drivers/net/ethernet/freescale/fec_main.c +5
-0* bridging applications. */ skb =build_skb (page_address (page),PAGE_SIZE ); +if (unlikely (!skb)) { +page_pool_recycle_direct (rxq->page_pool, page); + ndev->stats.rx_packets--; + ndev->stats.rx_bytes -= pkt_len; + ndev->stats.rx_dropped++;I'm not sure you really need to bother with rewinding the
rx_packetsandrx_bytescounters. I know that therx_droppedstatistic will get incremented in the network stack in the event of a packet failing to enqueue to the backlog, so it might be better to just leave therx_packetscounter as is and assume the actual packet count isrx_packets-rx_dropped.
According to your advice, I looked up the Linux document, actually as you said, the rx_packets should include packets which host had to drop at various stages of processing (even in the driver). Thanks for your review, I‘ll amend this in the next version.
Quoted 8 lines
drivers/net/ethernet/freescale/fec_main.c fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) · line 1683
Quoted 2 lines
drivers/net/ethernet/freescale/fec_main.c +2
-0+ +netdev_err (ndev,"build_skb failed!\n" );Instead of
netdev_erryou may want to considernetdev_err_oncefor this. Generally speaking when we start seeing memory allocation error issues they can get very noisy very quickly as you are likely to fail the allocation for every packet in a given polling session, and sessions to follow.
Yes, it's better to use netdev_err_once than netdev_err in the situation you describe. Thanks again!
Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/freescale/fec_main.c +3
-0+ goto rx_processing_done; + } +skb_reserve (skb, data_start);skb_put (skb, pkt_len - sub_len);skb_mark_for_recycle (skb);
Paolo Abeni
maintainerHello,
Quoted 11 lines · On Thu, 2022-12-15 at 17:11 +0800, [email protected] wrote:
From: Wei Fang <[email protected]>
The
build_skbmight return a null pointer but there is no check on the return value in thefec_enet_rx_queue(). So a null pointer dereference might occur. To avoid this, we check the return value ofbuild_skb. If the return value is a null pointer, the driver will recycle the page and update the statistic of ndev. Then jump torx_processing_doneto clear the status flags of the BD so that the hardware can recycle the BD.Signed-off-by Wei Fang <[email protected]>Reviewed-by Shenwei Wang <[email protected]>
You need to include a suitable fixes tag here. Please repost adding it and retaining Alex's reviwed-by tag, thanks!
Paolo