public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nfc: pn533: allocate rx skb before consuming bytes
@ 2026-04-02  4:21 Pengpeng Hou
  2026-04-03 22:56 ` Jakub Kicinski
  2026-04-05  0:40 ` [PATCH net v2] " Pengpeng Hou
  0 siblings, 2 replies; 3+ messages in thread
From: Pengpeng Hou @ 2026-04-02  4:21 UTC (permalink / raw)
  To: netdev
  Cc: Lars Poeschel, Duoming Zhou, Rikard Falkeborn, linux-kernel,
	pengpeng, stable

pn532_receive_buf() reports the number of accepted bytes to the serdev
core. The current code consumes bytes into recv_skb and may already
hand a complete frame to pn533_recv_frame() before allocating a fresh
receive buffer.

If that alloc_skb() fails, the callback returns 0 even though it has
already consumed bytes, and it leaves recv_skb as NULL for the next
receive callback. That breaks the receive_buf() accounting contract and
can also lead to a NULL dereference on the next skb_put_u8().

Allocate the receive skb lazily before consuming the next byte instead.
If allocation fails, return the number of bytes already accepted.

Fixes: c656aa4c27b1 ("nfc: pn533: add UART phy driver")
Cc: stable@vger.kernel.org

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
 drivers/nfc/pn533/uart.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/nfc/pn533/uart.c b/drivers/nfc/pn533/uart.c
index 6d2f520a5bc8..412d341602aa 100644
--- a/drivers/nfc/pn533/uart.c
+++ b/drivers/nfc/pn533/uart.c
@@ -211,14 +211,19 @@ static size_t pn532_receive_buf(struct serdev_device *serdev,
 
 	timer_delete(&dev->cmd_timeout);
 	for (i = 0; i < count; i++) {
+		if (!dev->recv_skb) {
+			dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN,
+						  GFP_KERNEL);
+			if (!dev->recv_skb)
+				return i;
+		}
+
 		skb_put_u8(dev->recv_skb, *data++);
 		if (!pn532_uart_rx_is_frame(dev->recv_skb))
 			continue;
 
 		pn533_recv_frame(dev->priv, dev->recv_skb, 0);
-		dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, GFP_KERNEL);
-		if (!dev->recv_skb)
-			return 0;
+		dev->recv_skb = NULL;
 	}
 
 	return i;
-- 
2.50.1 (Apple Git-155)


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

* Re: [PATCH] nfc: pn533: allocate rx skb before consuming bytes
  2026-04-02  4:21 [PATCH] nfc: pn533: allocate rx skb before consuming bytes Pengpeng Hou
@ 2026-04-03 22:56 ` Jakub Kicinski
  2026-04-05  0:40 ` [PATCH net v2] " Pengpeng Hou
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2026-04-03 22:56 UTC (permalink / raw)
  To: Pengpeng Hou
  Cc: netdev, Lars Poeschel, Duoming Zhou, Rikard Falkeborn,
	linux-kernel, stable

On Thu,  2 Apr 2026 12:21:48 +0800 Pengpeng Hou wrote:
> pn532_receive_buf() reports the number of accepted bytes to the serdev
> core. The current code consumes bytes into recv_skb and may already
> hand a complete frame to pn533_recv_frame() before allocating a fresh
> receive buffer.

no longer applies, please rebase on net/main
-- 
pw-bot: cr

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

* [PATCH net v2] nfc: pn533: allocate rx skb before consuming bytes
  2026-04-02  4:21 [PATCH] nfc: pn533: allocate rx skb before consuming bytes Pengpeng Hou
  2026-04-03 22:56 ` Jakub Kicinski
@ 2026-04-05  0:40 ` Pengpeng Hou
  1 sibling, 0 replies; 3+ messages in thread
From: Pengpeng Hou @ 2026-04-05  0:40 UTC (permalink / raw)
  To: netdev
  Cc: Lars Poeschel, Duoming Zhou, Rikard Falkeborn, linux-kernel,
	pengpeng, stable

pn532_receive_buf() reports the number of accepted bytes to the serdev
core. The current code consumes bytes into recv_skb and may already hand
a complete frame to pn533_recv_frame() before allocating a fresh receive
buffer.

If that alloc_skb() fails, the callback returns 0 even though it has
already consumed bytes, and it leaves recv_skb as NULL for the next
receive callback. That breaks the receive_buf() accounting contract and
can also lead to a NULL dereference on the next skb_put_u8().

Allocate the receive skb lazily before consuming the next byte instead.
If allocation fails, return the number of bytes already accepted.

Fixes: c656aa4c27b1 ("nfc: pn533: add UART phy driver")
Cc: stable@vger.kernel.org
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
Changes since v1:
- rebase on `net/main`
- keep the same fix shape on top of the current tailroom handling

 drivers/nfc/pn533/uart.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/nfc/pn533/uart.c b/drivers/nfc/pn533/uart.c
index 1b82b7b2..e0d67cd2 100644
--- a/drivers/nfc/pn533/uart.c
+++ b/drivers/nfc/pn533/uart.c
@@ -211,6 +211,13 @@ static size_t pn532_receive_buf(struct serdev_device *serdev,
 
 	timer_delete(&dev->cmd_timeout);
 	for (i = 0; i < count; i++) {
+		if (!dev->recv_skb) {
+			dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN,
+						  GFP_KERNEL);
+			if (!dev->recv_skb)
+				return i;
+		}
+
 		if (unlikely(!skb_tailroom(dev->recv_skb)))
 			skb_trim(dev->recv_skb, 0);
 
@@ -219,9 +226,7 @@ static size_t pn532_receive_buf(struct serdev_device *serdev,
 			continue;
 
 		pn533_recv_frame(dev->priv, dev->recv_skb, 0);
-		dev->recv_skb = alloc_skb(PN532_UART_SKB_BUFF_LEN, GFP_KERNEL);
-		if (!dev->recv_skb)
-			return 0;
+		dev->recv_skb = NULL;
 	}
 
 	return i;
-- 
2.50.1


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

end of thread, other threads:[~2026-04-05  2:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02  4:21 [PATCH] nfc: pn533: allocate rx skb before consuming bytes Pengpeng Hou
2026-04-03 22:56 ` Jakub Kicinski
2026-04-05  0:40 ` [PATCH net v2] " Pengpeng Hou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox