Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753000AbbDTT2v (ORCPT ); Mon, 20 Apr 2015 15:28:51 -0400 Received: from mail-ie0-f171.google.com ([209.85.223.171]:34264 "EHLO mail-ie0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751180AbbDTT2s (ORCPT ); Mon, 20 Apr 2015 15:28:48 -0400 Date: Mon, 20 Apr 2015 14:28:44 -0500 From: Bjorn Helgaas To: Ricardo Ribalda Delgado Cc: Andrew Morton , Vivek Goyal , Vinod Koul , Cliff Wickman , Jiang Liu , Jakub Sitnicki , Thierry Reding , Mike Travis , linux-kernel@vger.kernel.org, Grant Likely Subject: Re: [PATCH] kernel/resource: Invalid memory access in __release_resource Message-ID: <20150420192844.GD20701@google.com> References: <1429546972-28400-1-git-send-email-ricardo.ribalda@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1429546972-28400-1-git-send-email-ricardo.ribalda@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3639 Lines: 105 [+cc Grant (author of ac80a51e2ce5)] Hi Ricardo, On Mon, Apr 20, 2015 at 06:22:52PM +0200, Ricardo Ribalda Delgado wrote: > When a resource is initialized via of_platform_populate. > resource->parent is initialized to NULL via kzalloc. > (of_platform_populate->of_device_alloc->of_address_to_resource) > > If of_platform_depopulate is called later, resource->parent is > accessed (Offset 0x30 of address 0), causing a kernel error. Interesting; how'd you find this? It looks like the of_platform_depopulate() code has been this way for a long time, so we must be doing something new that makes us trip over this now. More analysis below... > This patch evaluates resouce->parent before accessing it. If it > is not initialized, -EACCESS is returned. > > Fixes: > BUG: unable to handle kernel NULL pointer deference at 0000000000000030 > IP: release_resource+0x26/0x90 > Signed-off-by: Ricardo Ribalda Delgado > --- > kernel/resource.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/kernel/resource.c b/kernel/resource.c > index 90552aa..35dc716 100644 > --- a/kernel/resource.c > +++ b/kernel/resource.c > @@ -237,6 +237,9 @@ static int __release_resource(struct resource *old) > { > struct resource *tmp, **p; > > + if (!old->parent) > + return -EINVAL; This path has been fine for a long time without testing for a NULL pointer, so I suspect this change papers over an issue that would be better fixed elsewhere. >From reading drivers/base/platform.c, it looks like the intent is that platform device users would use these interfaces: - platform_device_alloc() - platform_device_add_resources(platform_device *pdev) pdev->num_resources = num - platform_device_add(platform_device *pdev) for (i = 0; i < pdev->num_resources; i++) insert_resource() device_add(&pdev->dev) - platform_device_unregister(platform_device *pdev) platform_device_del(platform_device *pdev) for (i = 0; i < pdev->num_resources; i++) release_resource() Resources are added by platform_device_add_resources() and inserted into the resource tree by platform_device_add(). In this usage, resources are removed from the resource tree by platform_device_unregister(), and there's no issue with resource->parent being NULL. OF uses platform_device_alloc() and platform_device_unregister(), but not platform_device_add(). It doesn't call insert_resource(), and that breaks the platform_device_unregister() assumption that the resources are in the resource tree: - of_platform_populate() ... of_device_alloc() pdev = platform_device_alloc() # set pdev->resource, similar to platform_device_add_resources() of_device_add(platform_device *pdev) # similar to platform_device_add(), but note there's no # insert_resource() in this path device_add(&pdev->dev) - of_platform_depopulate() of_platform_device_destroy() platform_device_unregister(platform_device *pdev) platform_device_del(platform_device *pdev) for (i = 0; i < pdev->num_resources; i++) release_resource() I cc'd Grant because ac80a51e2ce5 ("of/device: populate platform_device (of_device) resource table on allocation") added the pdev->resource management in of_device_alloc(), so maybe he has more insight into this. > p = &old->parent->child; > for (;;) { > tmp = *p; > -- > 2.1.4 > -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/