Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1241463rbb; Mon, 26 Feb 2024 03:28:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUKpvomQA+23lXPaGgTW2k256PD43Z076pvxq35bi8z6z5yFRmjp5MPfd8DIlCd+P8EROl1cEwWKGh94l6Wr/QBPTBzT/zmENxbusMQgQ== X-Google-Smtp-Source: AGHT+IHhW+jCzksu0R7oHSDsOcmLwoYlCDMYjBP/xxFL4xPuNilH3HPsM66tgN45C2J9gcDEQnTy X-Received: by 2002:a05:6a00:80c4:b0:6e4:71ad:e13c with SMTP id ei4-20020a056a0080c400b006e471ade13cmr5454795pfb.26.1708946932353; Mon, 26 Feb 2024 03:28:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708946932; cv=pass; d=google.com; s=arc-20160816; b=qpQ8yGC+HgHDxcDSV4PhVW/f2IetSTyF0tX2jTfBnxFMjKq3PLi8mqOBuMqlpMNkkG 3MqdZi4lR/iJD1i3Sikpvr3X4QCM+sy2LQ3bHJP83NVeSbZAs+ko/EMu5IuFjkx/JrDX OOGSGFjWExCLNCRNrf+rr0+8cdShWx8OBlc4/Ik1J7+3Uyv+bawCi9zf/T1TA0DUT0Ns B/f5GLcXUCBu2hKLbqOGtla+wzvto3G42LSm5OiwltRE4y0Htg6dU41wgYVCRfTXgr8Y s06QQRQ0w3P8pzyWIsxwSu1N+Uev3/eZiWqEv4A7ZSS+OdN0KFLw/rEvSQ0B9u9azlRF V4LA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=YUBb5zD+GwuxupxQ4TAzWAOJqy2HhzpNAOsPo2YrqhM=; fh=KaChwVjn0h6Ai3rAOqL6+jCCVWMsxSOv7w19GFniv3E=; b=jtkvPKdYa3c2cbYCC6uWfDwcJORn9vEBblxxrrmaoGZVWgUnTs7jGKOrdnJ9WZQtYt yUwdu9GVWAdJE2bQ/gxWvILQG+HroJUXsc55pnBx8Ig6vel0U+N5VWXP+rEN9dgLY9Qq 0a6CVeZiBZxWmOCyPa6SlM4K8+LBv7J5d2qMsQOD7uX01DfG1PVACTTqeQyrklDJEfRl 64hBxDBk2e1vPTpsih9YiQR5p1coNQWyxJsAC4DJ07zLxZO/WH2kWmPh358KEuZfEwvJ K52iWXX2bGJHhsrh0ITtBvDlkGhXaAIRfA2yfG6G7YU8G9dumzD3SGeLPFTftvdIBDow lpQw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Gi6LzCPg; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-81263-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id t2-20020a63eb02000000b005d692a181d9si3439489pgh.340.2024.02.26.03.28.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 03:28:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-81263-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Gi6LzCPg; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-81263-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0567528836D for ; Mon, 26 Feb 2024 11:28:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5BC121DA24; Mon, 26 Feb 2024 11:28:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="Gi6LzCPg" Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED7241D535; Mon, 26 Feb 2024 11:28:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708946922; cv=none; b=s8ovpqInt6cVLU7W+K+AxPdjPtHcsaj4+UtR09UrBNgdH0NeSSdECRmoeMxi4LnuQ2GKbY45ibzPt1fjS5JMJLlXq8BFNj8MaAuxVa/zSSbqcmIjKJlhoeCf6Okj8kc18zVMdFDQdzDUlb6qcpojtPybwOc7sPrnYKP1xAp+zcM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708946922; c=relaxed/simple; bh=9r4Qy7hs0vSKI9uUdjPW2Fhk40C0I8X2MjZQPOXqT44=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=nxv0su92EzsdIUjnd5VkC4gvcKitwaVN3stnKOG+IR6Olsj2TjHChpYNeU8EcKPe0cWfv4YFg7TkPvWDiMEwCS1EKRJvQSnwbVvy0X4SJddjUNEqFhiEYZxEkjK81Y/MLCuTbjY8y7DM1rKmb1+6K7nKmtOQsobGXttz/adpVbQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=Gi6LzCPg; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41QARVOg009380; Mon, 26 Feb 2024 11:28:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=qcppdkim1; bh=YUBb5zD+GwuxupxQ4TAz WAOJqy2HhzpNAOsPo2YrqhM=; b=Gi6LzCPgYIRN1DR5ACKTaX+2Xqp/cnRK3Iyi FzsH8p4f7l7c5+s8ciHQkcVWb1ygDnb2Vrby0vAmbt0XJf8kRiKi6oOSaEQIRtbX Ol729OvmZBjlpobtkbAj1x7co9adoVGGhe8Pa4aBMHdD+WfqrVm/bwrxDCsNgXJs Z8KZ2vFVHvXbS1+VdRaSoxevWAxqd63od/ZNhV9PIWKFfe6NaTbGTvltRF4ZMgod 2as16AqE/2oHeW4fRgqyqAR8WNb5GalIcWSH1iJ1vOrA5cYsn4foRhzU7scj5kt8 fBI2G7vrapTp0rtQ4GLI1uwdarnU9AnxxYNDKGBfDXnPwGT9Qw== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3wgkxm0r18-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 26 Feb 2024 11:28:38 +0000 (GMT) Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 41QBSbuA017977 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 26 Feb 2024 11:28:37 GMT Received: from hu-kriskura-hyd.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 26 Feb 2024 03:28:33 -0800 From: Krishna Kurapati To: Greg Kroah-Hartman , =?UTF-8?q?Maciej=20=C5=BBenczykowski?= CC: , , , , , Krishna Kurapati Subject: [RFC PATCH] usb: gadget: ncm: Fix handling of zero block length packets Date: Mon, 26 Feb 2024 16:58:16 +0530 Message-ID: <20240226112816.2616719-1-quic_kriskura@quicinc.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="y" Content-Transfer-Encoding: 8bit X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: GJ68AuHhhZanVx89CWMvuBMiFJIXtUiB X-Proofpoint-GUID: GJ68AuHhhZanVx89CWMvuBMiFJIXtUiB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-26_07,2024-02-26_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 bulkscore=0 mlxlogscore=869 clxscore=1011 impostorscore=0 suspectscore=0 priorityscore=1501 lowpriorityscore=0 mlxscore=0 spamscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2402120000 definitions=main-2402260085 While connecting to a Linux host with CDC_NCM_NTB_DEF_SIZE_TX set to 65536, it has been observed that we receive short packets, which come at interval of 5-10 seconds sometimes and have block length zero but still contain 1-2 valid datagrams present. According to the NCM spec: "If wBlockLength = 0x0000, the block is terminated by a short packet. In this case, the USB transfer must still be shorter than dwNtbInMaxSize or dwNtbOutMaxSize. If exactly dwNtbInMaxSize or dwNtbOutMaxSize bytes are sent, and the size is a multiple of wMaxPacketSize for the given pipe, then no ZLP shall be sent. wBlockLength= 0x0000 must be used with extreme care, because of the possibility that the host and device may get out of sync, and because of test issues. wBlockLength = 0x0000 allows the sender to reduce latency by starting to send a very large NTB, and then shortening it when the sender discovers that there’s not sufficient data to justify sending a large NTB" However, there is a potential issue with the current implementation, as it checks for the occurrence of multiple NTBs in a single giveback by verifying if the leftover bytes to be processed is zero or not. If the block length reads zero, we would process the same NTB infintely because the leftover bytes is never zero and it leads to a crash. Fix this by bailing out if block length reads zero. Fixes: 427694cfaafa ("usb: gadget: ncm: Handle decoding of multiple NTB's in unwrap call") Signed-off-by: Krishna Kurapati --- PS: Although this issue was seen after CDC_NCM_NTB_DEF_SIZE_TX was modified to 64K on host side, I still believe this can come up at any time as per the spec. Also I assumed that the giveback where block length is zero, has only one NTB and not multiple ones. drivers/usb/gadget/function/f_ncm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c index e2a059cfda2c..355e370e5140 100644 --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -1337,6 +1337,9 @@ static int ncm_unwrap_ntb(struct gether *port, VDBG(port->func.config->cdev, "Parsed NTB with %d frames\n", dgram_counter); + if (block_len == 0) + goto done; + to_process -= block_len; /* @@ -1351,6 +1354,7 @@ static int ncm_unwrap_ntb(struct gether *port, goto parse_ntb; } +done: dev_consume_skb_any(skb); return 0; -- 2.34.1