Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp806979ybx; Tue, 5 Nov 2019 05:56:41 -0800 (PST) X-Google-Smtp-Source: APXvYqyXnlW1jjPBD9GLKmQvP4ngN0Pfwb494f5HueoEpM7HqGHZTfw2H5lYUTZ7GvkQQgZPAdJv X-Received: by 2002:a05:6402:b02:: with SMTP id bm2mr13671623edb.130.1572962201474; Tue, 05 Nov 2019 05:56:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572962201; cv=none; d=google.com; s=arc-20160816; b=phuZMoLkPjXc0L0yYPLISJ3FSflPXKr1UG3FNSB2g96bRPZucA3sxp+qxYV5j3pjDC Y6EkrnLWvm8KQ0t3/KAh1aQWtdusyXcSgODowhJklb3TEJDKpUZQedp7S4mK+lrdOpDO SC5KjcK4k/wv/oOOXBnE45QIjWcQ66BIduee1Wny4RTfnzoYUM/g8m6v6yXxUYFS/TUz nhnhS69316a+Vp8e9HMt67gEIozvxjHocLmuB12LtS7m35urbjc6DBGLVEjOk8j8aFNS bmZhHzblSMy4hZtRFkeFCB9K5o+tJHnASQBKe24q4NSuPTFG8RwybWwfZ0sJnC4ooJiV 0p1A== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=PyXIR+PmEYH6qf7Ne4Mnmzv5YmXO3mLrP9LUHKfXPGw=; b=nDbiC3RHsG4o79Ksm6cO2X/lKD9yLRugxpeixAkW99/Dnj7WMpy0rWpWQUElE71jpV /Z4hgcUN5cGRHgcEdzCw5PQFwVPai4gu+1J99jlLEIqzF95sPLXtBBYpV45YnoFHra60 5N3EcfLc5WetaJT7PF9xglXA4A41ASdXFEBO865UcZ8TZviIDSQuYShKFK6oHMrEKQe0 vcwq2kI3eYTKzqR29zSoOrNeIjxoOf1xtQuIg7FcSljGJ13ZhlZey8qRc/GkCoMu/IMH UF/83lIUbR7y93KTcqYB5lmzVIDD/sBpZk/sGPPBHXt7XDiXydf1NuCpDxkdX/8RmOh4 sTFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nrLLcvG1; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z13si3032427ejp.16.2019.11.05.05.56.17; Tue, 05 Nov 2019 05:56:41 -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=@google.com header.s=20161025 header.b=nrLLcvG1; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389371AbfKENz0 (ORCPT + 99 others); Tue, 5 Nov 2019 08:55:26 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:33320 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389325AbfKENzZ (ORCPT ); Tue, 5 Nov 2019 08:55:25 -0500 Received: by mail-wr1-f65.google.com with SMTP id s1so21523652wro.0 for ; Tue, 05 Nov 2019 05:55:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=PyXIR+PmEYH6qf7Ne4Mnmzv5YmXO3mLrP9LUHKfXPGw=; b=nrLLcvG14uq+/X/VA+nQN9xUWpd5bUqLUw8+DW6y6mWEQ4kkljqlcZlkNd8Y+iazcv +Otpdi9PB8TpTEf+BFG7BRu6C4H0xn0dHbTE3b+kKAJd29FOhKhCjHTxH/azEtQ5h7yM JU8H+7stYX9tn4RyzLsIwaQkV/RGM9nY3vN8dU6dz5IS6JNCqCknbaBCAVYqyQVdnR+f FlSJALeQqX+R4CttbO/g8G95D3aRoVysHz6QIH+dvl/Vc7VapDBxyxp8BaFeF/NJQUUZ YHC1lBDlGm9cRdf4WRWjh21h9EazOfczbEmDkg2xuwtBQMxBLUXVrnprO1L3RlQFQ4V2 4w9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=PyXIR+PmEYH6qf7Ne4Mnmzv5YmXO3mLrP9LUHKfXPGw=; b=NPzo82fm6eSpxT2FOvE1Vd5ci4er6RPiI55VTjCr/nnGU/Ml5NvkiP0ZlycRIdrxdW A7yWjOy9lDfXnNYoeel1gWXx2l0Pv7Pt+2xhYzAfgQL8HjkNGX9CpU2xhY/EwWC2aJ9Y JQJ9+vGUX5q9sC2Ksb7DsaKZ8PYILfnSErAAYJIBCx0DL6fmUd7rIvFCbaoBWLxZITNJ wgwEh8uMHuRXjZqpfpJ2fsdTGmWwr7a8UrqaBGY2YXktIs262pWWfITXbizpDcyJ0MMM RdRytZPCuVQK7eihgbXO349mr3eQQhWxZuAD0tymUHxFcnSvbBtZGQQIhf2WrNldyKGW uP+Q== X-Gm-Message-State: APjAAAXzDydlrOWg19svcSHAfPP4MfjGFwWKkD2JXJbcvUfZAkwknKMd augrfXDeLfYOHsmUSH1SeBX3yiM/1BUHQ1sQL6uk0w== X-Received: by 2002:a05:6000:18c:: with SMTP id p12mr7058466wrx.154.1572962120795; Tue, 05 Nov 2019 05:55:20 -0800 (PST) MIME-Version: 1.0 References: <00000000000013c4c1059625a655@google.com> <87ftj32v6y.fsf@miraculix.mork.no> <1572952516.2921.6.camel@suse.com> <875zjy33z2.fsf@miraculix.mork.no> In-Reply-To: <875zjy33z2.fsf@miraculix.mork.no> From: Alexander Potapenko Date: Tue, 5 Nov 2019 14:55:09 +0100 Message-ID: Subject: Re: KMSAN: uninit-value in cdc_ncm_set_dgram_size To: =?UTF-8?Q?Bj=C3=B8rn_Mork?= , Greg Kroah-Hartman Cc: Oliver Neukum , syzbot , David Miller , LKML , USB list , Networking , syzkaller-bugs Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org + Greg K-H On Tue, Nov 5, 2019 at 1:25 PM Bj=C3=B8rn Mork wrote: > > Oliver Neukum writes: > > Am Montag, den 04.11.2019, 22:22 +0100 schrieb Bj=C3=B8rn Mork: > >> This looks like a false positive to me. max_datagram_size is two bytes > >> declared as > >> > >> __le16 max_datagram_size; > >> > >> and the code leading up to the access on drivers/net/usb/cdc_ncm.c:587 > >> is: > >> > >> /* read current mtu value from device */ > >> err =3D usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE, > >> USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_= INTERFACE, > >> 0, iface_no, &max_datagram_size, 2); > > > > At this point err can be 1. > > > >> if (err < 0) { > >> dev_dbg(&dev->intf->dev, "GET_MAX_DATAGRAM_SIZE failed= \n"); > >> goto out; > >> } > >> > >> if (le16_to_cpu(max_datagram_size) =3D=3D ctx->max_datagram_si= ze) > >> > >> > >> > >> AFAICS, there is no way max_datagram_size can be uninitialized here. > >> usbnet_read_cmd() either read 2 bytes into it or returned an error, > > > > No. usbnet_read_cmd() will return the number of bytes transfered up > > to the number requested or an error. > > Ah, OK. So that could be fixed with e.g. > > if (err < 2) > goto out; It'd better be (err < sizeof(max_datagram_size)), and probably in the call to usbnet_read_cmd() as well. > > Or would it be better to add a strict length checking variant of this > API? There are probably lots of similar cases where we expect a > multibyte value and a short read is (or should be) considered an error. > I can't imagine any situation where we want a 2, 4, 6 or 8 byte value > and expect a flexible length returned. This is really a widespread problem on syzbot: a lot of USB devices use similar code calling usb_control_msg() to read from the device and not checking that the buffer is fully initialized. Greg, do you know how often usb_control_msg() is expected to read less than |size| bytes? Is it viable to make it return an error if this happens? Almost nobody is using this function correctly (i.e. checking that it has read the whole buffer before accessing it). > >> causing the access to be skipped. Or am I missing something? > > > > Yes. You can get half the MTU. We have a similar class of bugs > > with MAC addresses. > > Right. And probably all 16 or 32 bit integer reads... > > Looking at the NCM spec, I see that the wording is annoyingly flexible > wrt length - both ways. E.g for GetNetAddress: > > To get the entire network address, the host should set wLength to at > least 6. The function shall never return more than 6 bytes in response > to this command. > > Maybe the correct fix is simply to let usbnet_read_cmd() initialize the > full buffer regardless of what the device returns? I.e. > > diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c > index dde05e2fdc3e..df3efafca450 100644 > --- a/drivers/net/usb/usbnet.c > +++ b/drivers/net/usb/usbnet.c > @@ -1982,7 +1982,7 @@ static int __usbnet_read_cmd(struct usbnet *dev, u8= cmd, u8 reqtype, > cmd, reqtype, value, index, size); > > if (size) { > - buf =3D kmalloc(size, GFP_KERNEL); > + buf =3D kzalloc(size, GFP_KERNEL); > if (!buf) > goto out; > } > @@ -1992,7 +1992,7 @@ static int __usbnet_read_cmd(struct usbnet *dev, u8= cmd, u8 reqtype, > USB_CTRL_GET_TIMEOUT); > if (err > 0 && err <=3D size) { > if (data) > - memcpy(data, buf, err); > + memcpy(data, buf, size); > else > netdev_dbg(dev->net, > "Huh? Data requested but thrown away.\n"); > > > > > What do you think? > > Personally, I don't think it makes sense for a device to return a 1-byte > mtu or 3-byte mac address. But the spec allows it and this would at > least make it safe. > > We have a couple of similar bugs elsewhere in the same driver, BTW.. > > > Bj=C3=B8rn --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg