Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1246306ybk; Thu, 14 May 2020 04:21:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeTqYArtVEIZZYhRxJGIGtUldCC2JNMFQsspOmI1UDv0zXnk1Xy7US27k44fFbvBs+6bBG X-Received: by 2002:a05:6402:3128:: with SMTP id dd8mr3529172edb.156.1589455301218; Thu, 14 May 2020 04:21:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589455301; cv=none; d=google.com; s=arc-20160816; b=oDFgUQvf6LiBfTSoVmX3e5Jayzq3o1rTkPAya171oRMRLGygn3FhQymKGKgk3duXar EqqIrj691ecNIAi5Wwd7YIymdlVH//CTBwLEa4SlyWS+E6deaXlURNSO9gcEjaFbWZ72 v01ep+PZueBckplkvejmNlaQzOPogOqiL3MgpXq1wQTCsObtsFql3/O43YMbOHh2Dz0H aJsb4Qt7xWq+0GR1IKbqh5vroDFs3TIS6Hu2OH6Ye1OskOYgQ7C3HxuLBTh3icjATQ2C 2xpatDdPprMVFN6HulaZ2z1kAFReEjqEwH9N4tLVZDOyxHFY8as3QwLbrCP1sYxqmIfY ra7w== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=3GmAnYqQusOvTMFsQhnhshy4dkVUknc4jxszQwBuBBE=; b=cTFXFsQA5FXU08hPLLRjaBPf0ljWjt21zo8lod9kzPLFOLBnuMo+wDPIvcBs7unnQf m0Z0EdCR/NxN753kKug8dnOnuPgKf1bhFhUNi/Me6+kd95Z5gjVA9y6CgooGBBGOdPaj B6rQ/DxgcFS1El5Smifmuo3tv5zFOnRo7jXe/KpxGbfjyWxSOXnC3mrPyaDXa8G6jFFQ 5NZZu/vUORQ0JZMh2aDaE404BWBxIKNn6ABxmVvHsqZqj7nbsdb1F3JbzP0QGyRN/N+W WyDjDE1rZrX+8f9IuVRXyFuT/lKvulUjrkfqzxfXqvMJf5QfKACyJPv2sBco5szdQMGo GoIQ== ARC-Authentication-Results: i=1; mx.google.com; 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 g12si1557264ejw.314.2020.05.14.04.21.17; Thu, 14 May 2020 04:21:41 -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; 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 S1726094AbgENLRo (ORCPT + 99 others); Thu, 14 May 2020 07:17:44 -0400 Received: from foss.arm.com ([217.140.110.172]:34472 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726010AbgENLRn (ORCPT ); Thu, 14 May 2020 07:17:43 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E827F30E; Thu, 14 May 2020 04:17:42 -0700 (PDT) Received: from [192.168.122.166] (unknown [10.119.48.101]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9B6E53F305; Thu, 14 May 2020 04:17:42 -0700 (PDT) Subject: Re: Validating dma_mmap_coherent() parameters before calling (was Re: WARNING in memtype_reserve) To: Greg KH , Christoph Hellwig Cc: Hillf Danton , syzbot , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, syzkaller-bugs@googlegroups.com, x86@kernel.org 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> <20200514074659.GA1569055@kroah.com> From: Jeremy Linton Message-ID: Date: Thu, 14 May 2020 06:17:41 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200514074659.GA1569055@kroah.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 5/14/20 2:46 AM, Greg KH wrote: > 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? No, thats fine but since I just translated usb_alloc_coherent() to dma_map_coherent in my not fully away head. Putting this as "usb_map_cohernet()" sort of makes more sense. > > 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; >