Received: by 10.192.165.156 with SMTP id m28csp96853imm; Tue, 17 Apr 2018 07:07:58 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/ddd7+Z0f8Dc4iyNg9DiSBGs7mv1kdNtonb7qU9A3udjMtS54CsdOzO61tynA7StU3MVfT X-Received: by 10.99.145.196 with SMTP id l187mr1905461pge.261.1523974078482; Tue, 17 Apr 2018 07:07:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523974078; cv=none; d=google.com; s=arc-20160816; b=kFdrnB3cBMgVWLxUdctbhez3LG9/V91fAYo0lEkSqTZsyhi/79Ox5JaIWinzXXk390 FkaWC6DVveFwUsVfDodBCgRtsfvksSHcAbKhHhC5i6FpDHG/82H3e0SaT6LgwxhWnrJw 50/23E6nY7Yp3idukfgcoT6HLYRRVNZre9ZlekLMF4u74Io6K31EK5ERiUd3buK1+TFK 7Scaaq4UBjWqXJmaJll8qoIwrQNtRDxU7uDQfYZMF9lh66sMABlEiY/NQahkRIc+can/ MXdg12G1AzzE0AfhY56Z22tjSduRq5tSNnRHKyzyUE9eDzAdzzgHDsTf1oXi6ZRbhIu1 8D7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=j36KbrEAjokTbn7T85ini4+M+lHGCIBkg+7AUNC9u9U=; b=jgJvhWqB1atUvqx7ZD8s0a8Rs7c37Mnp82S2RFPkDNxWpa1mQo7TwV2ntnQWYiKeYp yqDx8YeURCOYGgSniD+VexioV24FP37fs+QL9VyQj74PZ6Tj5/fMTHZnhPrUbrybBs8P qyrEm9TYTSK3C1RYjKhn5VAwevemIev67X6LQ8q1+kB2T0eYptss2ouAX5CDVBFSbU6A Qrr8nNca/ojntprKufcSd+G3lpRB0t16IHee0/5gP+xO9qDqykiUJcnua4ajhMT9Q5CN G2IcXnnRGJLWuYPVjJ2aD18BnrYa8vX+5l0IuM1U6k/3tBkpj3Fn42GhMq+ibkkkg9xa siSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=qmihPn0T; 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 y139si2238153pfc.191.2018.04.17.07.07.18; Tue, 17 Apr 2018 07:07:58 -0700 (PDT) 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=fail header.i=@gmail.com header.s=20161025 header.b=qmihPn0T; 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 S1752427AbeDQOEb (ORCPT + 99 others); Tue, 17 Apr 2018 10:04:31 -0400 Received: from mail-ua0-f194.google.com ([209.85.217.194]:44382 "EHLO mail-ua0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751270AbeDQOE3 (ORCPT ); Tue, 17 Apr 2018 10:04:29 -0400 Received: by mail-ua0-f194.google.com with SMTP id r16so12590108uak.11; Tue, 17 Apr 2018 07:04:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=j36KbrEAjokTbn7T85ini4+M+lHGCIBkg+7AUNC9u9U=; b=qmihPn0TxtA7mbSBtUEs0f3/Pit4lHEUVPCq4RP3HTIhzzDM3VkyIrM6hJhbWHK4Lm Hu4BBgzuk2gL7cbQ6aFjDLv/6lcGKtZherUhz+Bir0yyPbOoMb9NTozKz1WvDT9kYMnu q9MEz2PIkzxVAKsOAJOSKRUdvtYHLo3UeI+5FOZYcRMdglWSWOcGK5U9YUNgFdLnd5YX d0oCKzKRZ/3MOL/O9jNJo9nDQMN3PAKbPuICDbxCj86ZOxmLknZqNbbYsMXlEzvHDfpr Ekv4xzmrHgtssVvTn0WBS99iFkd41DKyGwxmClzZijsEFYsAoEostSzR0so00EwcW12Z QA1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=j36KbrEAjokTbn7T85ini4+M+lHGCIBkg+7AUNC9u9U=; b=CNuKumq6WvLjh0s+qwkPAkrMayJ5UCQaeFey+LE4JijPFhJwSBOTg+0VY7JVvIEEYn rjrKrWYIuMZxRDFiyp8wq3crrsTaVnsUR/R97wg/Gn/OkoWncSbKHqXPOaJdUvNVKjf/ rhXSxhi3/ivRny3wfltVbYnUmQHAJ4mTfZVoyHNQBBpAfHciXc2/ynABhEkhTXWI9XtM ScepVDHQ/E6hjwZq2OJyklBzQhKIXRSDeaB2akEoKAJjYLbLCIo6R5ey+V1QwUhGaKSe T6fCiw0bRyUirCZZ0N4/l3NBktHfJbQLyWa9VHW8gt/7gAezlD8VOeldxRYj4LGnePNM kYGA== X-Gm-Message-State: ALQs6tA7mzo14TlT4je5e+SvAdgWmmSgPMuqZ/Lk427TPFSfU+FnTgMW tg1YbC2zzFREamEhcPzY/S72HLp3TnjMLf3hGlNNuqhI X-Received: by 10.176.81.6 with SMTP id e6mr1627695uaa.33.1523973868425; Tue, 17 Apr 2018 07:04:28 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.122.68 with HTTP; Tue, 17 Apr 2018 07:04:27 -0700 (PDT) In-Reply-To: <1523972123-5700-1-git-send-email-jacopo+renesas@jmondi.org> References: <1523972123-5700-1-git-send-email-jacopo+renesas@jmondi.org> From: Geert Uytterhoeven Date: Tue, 17 Apr 2018 16:04:27 +0200 X-Google-Sender-Auth: 16_Wvygr1pCXa4LEQ3mqhWwwPmk Message-ID: Subject: Re: [PATCH] sh: mm: Fix unprotected access to struct device To: Jacopo Mondi Cc: Yoshinori Sato , Rich Felker , Thomas Petazzoni , Robin Murphy , Linux-Renesas , Linux-sh list , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jacopo, Thanks for your patch! On Tue, Apr 17, 2018 at 3:35 PM, Jacopo Mondi wrote: > With commit ce88313069c36eef80f21fd7 ("arch/sh: make the DMA mapping > operations observe dev->dma_pfn_offset") the generic DMA allocation > function on which the SH 'dma_alloc_coherent()' function relies on, > access the 'dma_pfn_offset' field of struct device. accesses > Unfortunately the 'dma_generic_alloc_coherent()' function is called from > several places with a NULL struct device argument, halting the CPU > during the boot process. > > This patch fixes the issue protecting access to dev->dma_pfn_offset, by protecting access to the > with a trivial check for validity. It also passes a valid 'struct device' > in the 'platform_resource_setup_memory' function which is the main user > of 'dma_alloc_coherent()', and inserting a WARN_ON() check to make future > (and existing) bogus users of this function they're should provide a valid drop "they're should"? > 'struct device' whenever possible. > --- a/arch/sh/mm/consistent.c > +++ b/arch/sh/mm/consistent.c > @@ -39,6 +39,8 @@ void *dma_generic_alloc_coherent(struct device *dev, size_t size, > void *ret, *ret_nocache; > int order = get_order(size); > > + WARN_ON(!dev); > + > gfp |= __GFP_ZERO; > > ret = (void *)__get_free_pages(gfp, order); > @@ -59,7 +61,9 @@ void *dma_generic_alloc_coherent(struct device *dev, size_t size, > > split_page(pfn_to_page(virt_to_phys(ret) >> PAGE_SHIFT), order); > > - *dma_handle = virt_to_phys(ret) - PFN_PHYS(dev->dma_pfn_offset); > + *dma_handle = virt_to_phys(ret); > + if (dev) > + *dma_handle -= PFN_PHYS(dev->dma_pfn_offset); I would keep the WARN_ON() and the (ideally unneeded) dev check as close to each other as possible: if (!WARN_ON(!dev)) *dma_handle -= PFN_PHYS(dev->dma_pfn_offset); > > return ret_nocache; > } > @@ -69,9 +73,14 @@ void dma_generic_free_coherent(struct device *dev, size_t size, > unsigned long attrs) > { > int order = get_order(size); > - unsigned long pfn = (dma_handle >> PAGE_SHIFT) + dev->dma_pfn_offset; > + unsigned long pfn = (dma_handle >> PAGE_SHIFT); > int k; > > + WARN_ON(!dev); > + > + if (dev) > + pfn += dev->dma_pfn_offset; if (!WARN_ON(!dev)) pfn += dev->dma_pfn_offset; > + > for (k = 0; k < (1 << order); k++) > __free_pages(pfn_to_page(pfn + k), 0); Reviewed-by: Geert Uytterhoeven Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds