Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1127551ybk; Thu, 14 May 2020 00:49:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4z+eV+ddMKtcXfgXoe8zwc6+DMOXIWhQOovorPBAC5OhoPNdtOWLykc0IAXrszdyFrk5z X-Received: by 2002:a17:906:cb98:: with SMTP id mf24mr2739212ejb.146.1589442554173; Thu, 14 May 2020 00:49:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589442554; cv=none; d=google.com; s=arc-20160816; b=SfhLs3LnhuYcDV2mYYQHrlN8MXuxDFeZ/neo3OtAuGL+1wOeoJ7/iXvFavRqLlZr3n veAeydFrCRsqUhS+K5/0v/LW/ficICLn1ASmJ7fSwe9dWUr6dMHjdXnwC5gFgch4mHxX 4cS94qv3EhTAdNWtzHsVW6F6o5wXF2JwvHLZuQcdM9zCv14D0zaazVDYgqKGVNH061Kq YE/zyaYIA2LjUSPaRgeOHFq1q5ZKe9hlZXnvwPaKKrAiUme/Xu2rJ1Xg9678ENSiSY4P JmSk7y0IlYgWo/IKKZ6Eh6vrfy+egiwZsbwsxlyxLimTNaywAVleVyLyqKmCoZDItMBc bI2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=4Psf2nDOfGHomw7ZPMES5ZS/LgPRbJipYsI9yWwXgYo=; b=JECNXJoFi4dfnWT4hCvNCseBUYTz+2FgFqeKToknXWY/98Z88BaKqslW0+Crl/+033 +qqgazAKMZJQ7byGMUS6S7LcGT8Yl4sjAjfC1kG4jEDWX/YndSC20f/RQnMy0bVzbFht l9KVgulu/oMDrMDxQIsaVUJZ/GJ4cmXoJkYo7BM6/KPxpFYseKO2eQmMxFZaPIBRWS68 y1mN5SH2boFpsExURQPGPZs9mYLxo3NiBE/0xTksjfnyWvGpcHJpCw0N7E4nljMiUiuM 7cufxQcfTM5rhYwjBvnu9kpLWIEgq/78hWxIlEgCj82V4+Gn9nDqglHX5bTtUmuA7QGO qXng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wyV8dKIj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v12si1299056edb.234.2020.05.14.00.48.50; Thu, 14 May 2020 00:49:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wyV8dKIj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726037AbgENHrC (ORCPT + 99 others); Thu, 14 May 2020 03:47:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:47734 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725974AbgENHrC (ORCPT ); Thu, 14 May 2020 03:47:02 -0400 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 35E34206BE; Thu, 14 May 2020 07:47:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589442421; bh=wN80gGI8V+ylaxhZ0uVEzr/XB82cdY/3/3iPL444rCA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=wyV8dKIjLO7rETVBqMxhDcXNwBCiYADAEr93vBBL4ZJl/y577W1fIntacfew8QfRa vt5DU2KMibyPDnZ18TCUQvd8fHYQRg6g/OvM9AAqpcaLisyd/3np0z7HHNyqbuhARI 2sNCauNuuF90gzLVvznhjQAUFqDJAkmpKjMrP3jk= Date: Thu, 14 May 2020 09:46:59 +0200 From: Greg KH To: Christoph Hellwig , jeremy.linton@arm.com Cc: Hillf Danton , syzbot , Thomas Gleixner , jeremy.linton@arm.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com, x86@kernel.org Subject: Re: Validating dma_mmap_coherent() parameters before calling (was Re: WARNING in memtype_reserve) Message-ID: <20200514074659.GA1569055@kroah.com> References: <000000000000f0d8d205a531f1a3@google.com> <20200509074507.GC1831917@kroah.com> <87wo5l4ecm.fsf@nanos.tec.linutronix.de> <20200513124445.GA1082735@kroah.com> <87zhab249p.fsf@nanos.tec.linutronix.de> <20200514035458.14760-1-hdanton@sina.com> <20200514061417.GA8367@lst.de> <20200514062750.GA1488715@kroah.com> <20200514063158.GA8780@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200514063158.GA8780@lst.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 14, 2020 at 08:31:58AM +0200, Christoph Hellwig wrote: > On Thu, May 14, 2020 at 08:27:50AM +0200, Greg KH wrote: > > On Thu, May 14, 2020 at 08:14:17AM +0200, Christoph Hellwig wrote: > > > Guys, can you please start formal thread on this? I have no > > > idea where this came from and what the rationale is. Btw, if the > > > pfn is crap in dma_direct_mmap then the dma_addr_t passed in is > > > crap, as it is derived from that. What is the caller, and how is > > > this triggered? > > > > > > Ok, to summarize, commit 2bef9aed6f0e ("usb: usbfs: correct kernel->user > > page attribute mismatch") changed a call from remap_pfn_range() to > > dma_mmap_coherent() for usb data buffers being sent from userspace. > > I only need to look at the commit for 3 seconds to tell you that it is > completely buggy. While using dma_mmap_coherent is fundamentally the > right thing and absolutely required for dma_alloc_* allocations, USB > also uses it's own local gen pool allocator or plain kmalloc for not > DMA capable controller. This need to use remap_pfn_range. I'm pretty > sure you hit one of those cases. > > The logic should be something like: > > if (hcd->localmem_pool || !hcd_uses_dma(hcd)) > remap_pfn_range() > else > dma_mmap_coherent() Ok, that's simple enough, patch is below. Jeremy, any objection to this change? thanks, greg k-h diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c index b9db9812d6c5..d93d94d7ff50 100644 --- a/drivers/usb/core/devio.c +++ b/drivers/usb/core/devio.c @@ -251,9 +251,19 @@ static int usbdev_mmap(struct file *file, struct vm_area_struct *vma) usbm->vma_use_count = 1; INIT_LIST_HEAD(&usbm->memlist); - if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, size)) { - dec_usb_memory_use_count(usbm, &usbm->vma_use_count); - return -EAGAIN; + if (hcd->localmem_pool || !hcd_uses_dma(hcd)) { + if (remap_pfn_range(vma, vma->vm_start, + virt_to_phys(usbm->mem) >> PAGE_SHIFT, + size, vma->vm_page_prot) < 0) { + dec_usb_memory_use_count(usbm, &usbm->vma_use_count); + return -EAGAIN; + } + } else { + if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, + size)) { + dec_usb_memory_use_count(usbm, &usbm->vma_use_count); + return -EAGAIN; + } } vma->vm_flags |= VM_IO;