]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-5.10/usb-gadget-ncm-fix-handling-of-zero-block-length-packets.patch
Fixes for 5.10
[thirdparty/kernel/stable-queue.git] / queue-5.10 / usb-gadget-ncm-fix-handling-of-zero-block-length-packets.patch
1 From f90ce1e04cbcc76639d6cba0fdbd820cd80b3c70 Mon Sep 17 00:00:00 2001
2 From: Krishna Kurapati <quic_kriskura@quicinc.com>
3 Date: Wed, 28 Feb 2024 17:24:41 +0530
4 Subject: usb: gadget: ncm: Fix handling of zero block length packets
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 From: Krishna Kurapati <quic_kriskura@quicinc.com>
10
11 commit f90ce1e04cbcc76639d6cba0fdbd820cd80b3c70 upstream.
12
13 While connecting to a Linux host with CDC_NCM_NTB_DEF_SIZE_TX
14 set to 65536, it has been observed that we receive short packets,
15 which come at interval of 5-10 seconds sometimes and have block
16 length zero but still contain 1-2 valid datagrams present.
17
18 According to the NCM spec:
19
20 "If wBlockLength = 0x0000, the block is terminated by a
21 short packet. In this case, the USB transfer must still
22 be shorter than dwNtbInMaxSize or dwNtbOutMaxSize. If
23 exactly dwNtbInMaxSize or dwNtbOutMaxSize bytes are sent,
24 and the size is a multiple of wMaxPacketSize for the
25 given pipe, then no ZLP shall be sent.
26
27 wBlockLength= 0x0000 must be used with extreme care, because
28 of the possibility that the host and device may get out of
29 sync, and because of test issues.
30
31 wBlockLength = 0x0000 allows the sender to reduce latency by
32 starting to send a very large NTB, and then shortening it when
33 the sender discovers that there’s not sufficient data to justify
34 sending a large NTB"
35
36 However, there is a potential issue with the current implementation,
37 as it checks for the occurrence of multiple NTBs in a single
38 giveback by verifying if the leftover bytes to be processed is zero
39 or not. If the block length reads zero, we would process the same
40 NTB infintely because the leftover bytes is never zero and it leads
41 to a crash. Fix this by bailing out if block length reads zero.
42
43 Cc: stable@vger.kernel.org
44 Fixes: 427694cfaafa ("usb: gadget: ncm: Handle decoding of multiple NTB's in unwrap call")
45 Signed-off-by: Krishna Kurapati <quic_kriskura@quicinc.com>
46 Reviewed-by: Maciej Żenczykowski <maze@google.com>
47 Link: https://lore.kernel.org/r/20240228115441.2105585-1-quic_kriskura@quicinc.com
48 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
49 ---
50 drivers/usb/gadget/function/f_ncm.c | 2 +-
51 1 file changed, 1 insertion(+), 1 deletion(-)
52
53 --- a/drivers/usb/gadget/function/f_ncm.c
54 +++ b/drivers/usb/gadget/function/f_ncm.c
55 @@ -1357,7 +1357,7 @@ parse_ntb:
56 if (to_process == 1 &&
57 (*(unsigned char *)(ntb_ptr + block_len) == 0x00)) {
58 to_process--;
59 - } else if (to_process > 0) {
60 + } else if ((to_process > 0) && (block_len != 0)) {
61 ntb_ptr = (unsigned char *)(ntb_ptr + block_len);
62 goto parse_ntb;
63 }