Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5998963ybc; Wed, 27 Nov 2019 13:09:28 -0800 (PST) X-Google-Smtp-Source: APXvYqweFgr7hQePSEJqQ6GXla9a+3HerG7pPVlpNMsXLhcEoi1ZjsuOgcPJ9Ql7c3CFmdZSy5Ch X-Received: by 2002:a17:906:1805:: with SMTP id v5mr52689853eje.45.1574888968420; Wed, 27 Nov 2019 13:09:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574888968; cv=none; d=google.com; s=arc-20160816; b=RKfItU4cSJb1u6Y32HcHGXBoCZaZhijwTDyG5B2uyvAgj/XGWCgE2I6dvURKyu/2UH hqJXuCEYuHwYCF1ackRHkSZX4UGDPoPDbH0sLWS+7LNBiAGtb8/prAIjtm0nhjeA6Mt9 A2vvFhabjLkTnRgc3faIo0cTxA/aDF0sVSHYpL3emlfCsJlUB9DgppF3Hw2Uoztvd+Wq Nq6atddQnnJwx2MChYjbHscx3S7z2ioZjuyVhChpxpTJ7gYN5ZpgANyaCEXyk0+bfU4n mGeMuavTlR8rsjwSNNMrkaWKjyGd4dekj3frNYPjDYPqj4obRG4jOfJYun/4I2bIpZOT 4XEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nRcas5ZDOJZlFy8nLw4FIEZnoaKACTdrjc33omfi8Fs=; b=TUkH2WDZ4nBoExcYvz9GSDVeIXviXnIpvMCOs7pn1xfvEPvSs93ewtCsITzE3kx0fp 5YwhxX/cWQiPdBzwMiGl/A7Eye95R6BYmeE93RqpWHCMsuJbb8GXIMcUVGnPgEINczGM qaDfBm5nUCxZHRaKtuqoJ4RcAZOLNOuwAF+AlOuhzrBWR2Jo8A27tOh21C1irUJr/4Op 8NSyhsx9s6GORE50gAFrbJvS7etN5Mfr2HkZaScgZZQkDCwh4s6OuGvhsFGgizMG7QEm kN0uEhVnVKADfSiRxvcIgo9OdgkNPXGFsnEzvA1JtP8tHgTwSupj7471GV2D9wqDI20a VT5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nyou0jx3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f17si10066154eja.153.2019.11.27.13.09.04; Wed, 27 Nov 2019 13:09:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nyou0jx3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732619AbfK0VHb (ORCPT + 99 others); Wed, 27 Nov 2019 16:07:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:33616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732246AbfK0VH3 (ORCPT ); Wed, 27 Nov 2019 16:07:29 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9EB8F2176D; Wed, 27 Nov 2019 21:07:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888848; bh=WYLjzLJr4LJQ1JTM+hMm/fYTi2rQD3RG0X8a8GmwJMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nyou0jx3yVtD3Ugii90Cw3nDqChiAdy+JtkqF6fYK8DnnQDb4YH6zDaRJZCTQGrUQ 1DGJ074dp+m0R58TjLtdkqGXxcBMnvaya7LUoGpnuyuBl8Yy5QqAwXIw9c/8prU+dO sA1vBm5U4ROzQVwhxkMr0lw6hoGmhkqj5t8Tjizs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kbuild test robot , Dan Carpenter , Suwan Kim , Shuah Khan Subject: [PATCH 4.19 294/306] usbip: Fix uninitialized symbol nents in stub_recv_cmd_submit() Date: Wed, 27 Nov 2019 21:32:24 +0100 Message-Id: <20191127203136.223608358@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203114.766709977@linuxfoundation.org> References: <20191127203114.766709977@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Suwan Kim commit 2a9125317b247f2cf35c196f968906dcf062ae2d upstream. Smatch reported that nents is not initialized and used in stub_recv_cmd_submit(). nents is currently initialized by sgl_alloc() and used to allocate multiple URBs when host controller doesn't support scatter-gather DMA. The use of uninitialized nents means that buf_len is zero and use_sg is true. But buffer length should not be zero when an URB uses scatter-gather DMA. To prevent this situation, add the conditional that checks buf_len and use_sg. And move the use of nents right after the sgl_alloc() to avoid the use of uninitialized nents. If the error occurs, it adds SDEV_EVENT_ERROR_MALLOC and stub_priv will be released by stub event handler and connection will be shut down. Fixes: ea44d190764b ("usbip: Implement SG support to vhci-hcd and stub driver") Reported-by: kbuild test robot Reported-by: Dan Carpenter Signed-off-by: Suwan Kim Acked-by: Shuah Khan Cc: stable Link: https://lore.kernel.org/r/20191111141035.27788-1-suwan.kim027@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/usbip/stub_rx.c | 50 ++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 18 deletions(-) --- a/drivers/usb/usbip/stub_rx.c +++ b/drivers/usb/usbip/stub_rx.c @@ -470,18 +470,50 @@ static void stub_recv_cmd_submit(struct if (pipe == -1) return; + /* + * Smatch reported the error case where use_sg is true and buf_len is 0. + * In this case, It adds SDEV_EVENT_ERROR_MALLOC and stub_priv will be + * released by stub event handler and connection will be shut down. + */ priv = stub_priv_alloc(sdev, pdu); if (!priv) return; buf_len = (unsigned long long)pdu->u.cmd_submit.transfer_buffer_length; + if (use_sg && !buf_len) { + dev_err(&udev->dev, "sg buffer with zero length\n"); + goto err_malloc; + } + /* allocate urb transfer buffer, if needed */ if (buf_len) { if (use_sg) { sgl = sgl_alloc(buf_len, GFP_KERNEL, &nents); if (!sgl) goto err_malloc; + + /* Check if the server's HCD supports SG */ + if (!udev->bus->sg_tablesize) { + /* + * If the server's HCD doesn't support SG, break + * a single SG request into several URBs and map + * each SG list entry to corresponding URB + * buffer. The previously allocated SG list is + * stored in priv->sgl (If the server's HCD + * support SG, SG list is stored only in + * urb->sg) and it is used as an indicator that + * the server split single SG request into + * several URBs. Later, priv->sgl is used by + * stub_complete() and stub_send_ret_submit() to + * reassemble the divied URBs. + */ + support_sg = 0; + num_urbs = nents; + priv->completed_urbs = 0; + pdu->u.cmd_submit.transfer_flags &= + ~URB_DMA_MAP_SG; + } } else { buffer = kzalloc(buf_len, GFP_KERNEL); if (!buffer) @@ -489,24 +521,6 @@ static void stub_recv_cmd_submit(struct } } - /* Check if the server's HCD supports SG */ - if (use_sg && !udev->bus->sg_tablesize) { - /* - * If the server's HCD doesn't support SG, break a single SG - * request into several URBs and map each SG list entry to - * corresponding URB buffer. The previously allocated SG - * list is stored in priv->sgl (If the server's HCD support SG, - * SG list is stored only in urb->sg) and it is used as an - * indicator that the server split single SG request into - * several URBs. Later, priv->sgl is used by stub_complete() and - * stub_send_ret_submit() to reassemble the divied URBs. - */ - support_sg = 0; - num_urbs = nents; - priv->completed_urbs = 0; - pdu->u.cmd_submit.transfer_flags &= ~URB_DMA_MAP_SG; - } - /* allocate urb array */ priv->num_urbs = num_urbs; priv->urbs = kmalloc_array(num_urbs, sizeof(*priv->urbs), GFP_KERNEL);