Skip to content

Commit 5c14a19

Browse files
neosys007kuba-moo
authored andcommitted
nfc: s3fwrn5: allocate rx skb before consuming bytes
s3fwrn82_uart_read() reports the number of accepted bytes to the serdev core. The current code consumes bytes into recv_skb and may already deliver a complete 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: 3f52c2c ("nfc: s3fwrn5: Support a UART interface") Signed-off-by: Pengpeng Hou <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 77facb3 commit 5c14a19

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

drivers/nfc/s3fwrn5/uart.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ static size_t s3fwrn82_uart_read(struct serdev_device *serdev,
5858
size_t i;
5959

6060
for (i = 0; i < count; i++) {
61+
if (!phy->recv_skb) {
62+
phy->recv_skb = alloc_skb(NCI_SKB_BUFF_LEN, GFP_KERNEL);
63+
if (!phy->recv_skb)
64+
return i;
65+
}
66+
6167
skb_put_u8(phy->recv_skb, *data++);
6268

6369
if (phy->recv_skb->len < S3FWRN82_NCI_HEADER)
@@ -69,9 +75,7 @@ static size_t s3fwrn82_uart_read(struct serdev_device *serdev,
6975

7076
s3fwrn5_recv_frame(phy->common.ndev, phy->recv_skb,
7177
phy->common.mode);
72-
phy->recv_skb = alloc_skb(NCI_SKB_BUFF_LEN, GFP_KERNEL);
73-
if (!phy->recv_skb)
74-
return 0;
78+
phy->recv_skb = NULL;
7579
}
7680

7781
return i;

0 commit comments

Comments
 (0)