Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6010628ybc; Wed, 27 Nov 2019 13:20:57 -0800 (PST) X-Google-Smtp-Source: APXvYqw62JgAGuWLoFTcxJt3BKGbh5u9FG7T34SdHlP56bxkVfy7yu2xnG+Eou+OjmGze1emHUdn X-Received: by 2002:a17:906:3798:: with SMTP id n24mr7732347ejc.15.1574889657516; Wed, 27 Nov 2019 13:20:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574889657; cv=none; d=google.com; s=arc-20160816; b=U4BQ3rIr8bLB96mtR9fqxm+S1js/8sN/HkK5OGFzpsLnsDoKugo9VPusB0AW3zVWz6 Y+rX6R6o++5M+NTYyQbsmdufMfWv/TQrH3h+b97t0GpNZGd7bt9qSm412d0EWq4pu+Gm 5Jv1PCOu70bW31YkVcJmQYw2+qQ06dsIocHh22AOElk5lGzHQZ4hbQCv185WUsILuZ2e 1APNM1al2Jq7gR1Uh7YF5Y+YA2YioXzDC1vSpMEwcKe6nFy+5Oq5jDYJlFqMivTxUZRx jha6LFfpXZtSMuZPpfMruABkq+5zjdN6DFn54+JKK3GrAWNLcy/4XI3buK1rZv5bLAsg v5vg== 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=iyNVuU0QPzH2JkjBo3GAMOeFOf+Ny4Pz4vyLqxG3x8227wH/+jtY/qitjj8gSr5QnX CnAH6El/RH+Gj7oS7dRZPhkIhOW1X5B1XauoBuL0xp58UHbk+TUUnG02bkDxgUsSxd+6 zdxNl7Gs602bEOeQAGiQk8fV/zHd8bktBoKgdwKNtmcbrbItsCpqOIXpmBB/J8gIgSVi KV2Ye56F3WxRSQUR8LHxrSmoCnocacbCVyLHUSZWMJgcDxZD17aCspEaxqM3ogm6weAS qgxbEZs5CEtm6yapI0gi0ohmn/lsdLlhotxr4n5K/7zEkkBgrTI3873+wJHvbZcYclYX LNug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hEqFAHVP; 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 g6si13115202edk.210.2019.11.27.13.20.34; Wed, 27 Nov 2019 13:20:57 -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=hEqFAHVP; 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 S1733216AbfK0VLq (ORCPT + 99 others); Wed, 27 Nov 2019 16:11:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:40818 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732783AbfK0VLp (ORCPT ); Wed, 27 Nov 2019 16:11:45 -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 17436215F1; Wed, 27 Nov 2019 21:11:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574889104; bh=WYLjzLJr4LJQ1JTM+hMm/fYTi2rQD3RG0X8a8GmwJMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hEqFAHVPsjFnM20IC8ejVEaI4Wf1VawJMD5enWNcdk6y7sk7GFImtH2qXr16PIMnX n8Ytf/etakWSyUYhLZ6Bs1LN1g1I7GYJmAemcUVeCXJNdh0S5HHy8kjyWPBx2ewFDt eBecHyNvZn0Qqrk2ShYhCVYBf1/mf8uChIESqb0s= 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 5.3 83/95] usbip: Fix uninitialized symbol nents in stub_recv_cmd_submit() Date: Wed, 27 Nov 2019 21:32:40 +0100 Message-Id: <20191127202951.812055210@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127202845.651587549@linuxfoundation.org> References: <20191127202845.651587549@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);