Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED745C63699 for ; Thu, 12 Nov 2020 05:40:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A58CA20825 for ; Thu, 12 Nov 2020 05:40:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728899AbgKLFii (ORCPT ); Thu, 12 Nov 2020 00:38:38 -0500 Received: from netrider.rowland.org ([192.131.102.5]:45643 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1727782AbgKLC3w (ORCPT ); Wed, 11 Nov 2020 21:29:52 -0500 Received: (qmail 256982 invoked by uid 1000); 11 Nov 2020 21:29:50 -0500 Date: Wed, 11 Nov 2020 21:29:50 -0500 From: Alan Stern To: Lukas Bulwahn Cc: Greg Kroah-Hartman , linux-usb@vger.kernel.org, usb-storage@lists.one-eyed-alien.net, clang-built-linux@googlegroups.com, Tom Rix , Nathan Chancellor Subject: Re: Use of uninitialized data in special error case of usb storage transport Message-ID: <20201112022950.GA256754@rowland.harvard.edu> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org On Wed, Nov 11, 2020 at 10:08:26PM +0100, Lukas Bulwahn wrote: > Dear Alan, dear Greg, > > > here is a quick report from the static analysis tool clang-analyzer on > ./drivers/usb/storage/transport.c: > > When usb_stor_bulk_transfer_sglist() returns with USB_STOR_XFER_ERROR, it > returns without writing to its parameter *act_len. > > Further, the two callers of usb_stor_bulk_transfer_sglist(): > > usb_stor_bulk_srb() and > usb_stor_bulk_transfer_sg(), > > use the passed variable partial without checking the return value. Hence, > the uninitialized value of partial is then used in the further execution > of those two functions. > > Clang-analyzer detects this potential control and data flow and warns: > > drivers/usb/storage/transport.c:469:40: warning: The right operand of '-' > is a garbage value [clang-analyzer-core.UndefinedBinaryOperatorResult] > scsi_set_resid(srb, scsi_bufflen(srb) - partial); > ^ > > drivers/usb/storage/transport.c:495:15: warning: Assigned value is garbage > or undefined [clang-analyzer-core.uninitialized.Assign] > length_left -= partial; > ^ > > The tool is right; unfortunately, I do not know anything about the > intended function here. What is the further operation of those two > functions supposed to be when USB_STOR_XFER_ERROR is returned from > usb_stor_bulk_transfer_sglist()? Should the passed arguments remain > untouched, so setting *act_len to zero for the error paths would be > a suitable fix to achieve that. > > A quick hint on that point and I can prepare a patch for you to pick up... > > Given that this code is pretty stable for years and probably in wider > use, the overall functionality is probably resilient to having this local > data being filled with arbitrary undefined data in the error case... but > who knows... Yeah. When a transfer error occurs, I believe the *act_len value is ignored by the higher layers. But it won't hurt to set it to a valid number, just in case. For the two early-return paths in usb_stor_bulk_transfer_sglist() the amount of data transferred is 0. So if act_len isn't NULL, setting *act_len to 0 would be fine. Alan Stern