Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp1841904ybm; Thu, 23 May 2019 07:32:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqx4Idboa0jhCYwy0uEDq0lBTdVopH0CJ2otoaK9Z7XCOs8GUTr7OpQAO4QOAPsWdrBfNPBL X-Received: by 2002:a17:902:54f:: with SMTP id 73mr98500641plf.140.1558621967869; Thu, 23 May 2019 07:32:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558621967; cv=none; d=google.com; s=arc-20160816; b=yx7lrwTa7exngWpv18PqyYqPJKSzwb5vb5QAOVQ2ihilNNrYp6VlHFiA+ejxlYKeLW LFnRACGsKK9UDLbWmncBK4Oywyy9a9MTHRgb/YiphdfeCwf2JEjhf1xVsfr46nLpVb1Y Q8AbdWKQP8T/hX0vkjyNFlOURZE6PkiF9bOaRUjWaZzxcFjlqehCx7UYg0xj39gcH+Lf DSxD5cHEA4RkquI4p3Y80m9xoBcTZvLLkuOwajT1K0kXAJD1jbPza3GYNbyCK+pSLfQb DkOylbIJEEbtjSPOWwtafOsbFD5eARJbJr/U98z2AD8zuQja4xjgTtW2mszyyCHQadXi BJlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=OzO5RxAs85LgaOleUXBoRLjXCQwekMSHZAP0cUDWHtg=; b=OP7DT01tip+3SyKThz08eCdRqM1J0TfZ7ewAjcaxPQBuqj6A6oo/1qKzikXO7ipEOP Fr59W6uk71n30eKLammwu1tQXuJLyjTzN007S5MxP3n6ZGwROT3U+xRUNQ/47LyaJljn 9IR6+Huw7ZqYkaCHafUKmTMk6DQ8tL3of5bVDq8faG48FdmWCD0v7TLfgh1IXo9rQ8Ab Lzn65bo01vbfLyxpGRy53rBDJRnb6My7xrtTLg3vom1sOSUW32zFrg+LKyM3QQTBIQrG 9ozioNel8HsxvjwivV7LigQwQpbkztTZ+R4z6ETiHk4Lx5qJ2RC0oCeNPINxdIUKa5t3 /SBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jVOwhhEJ; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z1si28822199plk.245.2019.05.23.07.32.29; Thu, 23 May 2019 07:32:47 -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=pass header.i=@gmail.com header.s=20161025 header.b=jVOwhhEJ; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730856AbfEWObT (ORCPT + 99 others); Thu, 23 May 2019 10:31:19 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:47009 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730708AbfEWObT (ORCPT ); Thu, 23 May 2019 10:31:19 -0400 Received: by mail-pl1-f196.google.com with SMTP id r18so2825971pls.13; Thu, 23 May 2019 07:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=OzO5RxAs85LgaOleUXBoRLjXCQwekMSHZAP0cUDWHtg=; b=jVOwhhEJb5le1IMdGM9kPsH/V44mOmocvvXjZUIcPBj8+ylGNBd8RN7RuTlVrd7HqV 8zuhWExktEV+QaezF41L5rt9x7FFCyTtdBf6DB7OWiGEXBQQ3hZMKd4fZwGLyVDfsj8h /qpN62uubjW7lQ/GJs2rXKJZYAXjlqJCU9ADo+ZeAKaodrbdpdoqHAAjHkhRrIL2UZVm MohVf0uPAqvR+flYQBiETtqq//jtOB3vSecQexEe967n7OIatP91SFq3OYWnw/OSMQ5T Y6QTnTaTDZpp18F/1AUYP5EJzU5aqkwMWDoHlMe8NnPI2xSjHPXH635j4FSygTN3asHH s94Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=OzO5RxAs85LgaOleUXBoRLjXCQwekMSHZAP0cUDWHtg=; b=XGyMQP/8F0M4RQNhHmOxmZVTOGOTyiy+EyEH+y4kzlon9PZdWyiwzQ6uDEKgz+UWwb 2QQeO2blf1BhESHaQKCSKPJcZ2WzMJLfGcAmFEvSEuEZvCPiZj/sWEsjnvT85QVwSzdw FoCDt95JaKOQJTojpnXt/wW8KGBPEqNW4uD2iABj5Q2iByHqIvb6//GXTqUouz5yvtO9 OPLBoVZlAk61CXibrpxuXUUsxLfgX7TZsaK9IhHVigZJeWlTrQeTE3ZW4wnsKP5ikd8U 52wA+XgjmCjuYkqSqgD9ytysr4eGknWLssEizTheUHxkiXsWAvhQtPZTT/wl3BkhZBGQ g5+A== X-Gm-Message-State: APjAAAWqkRJ/e/YJMNe7Xv7WV3cXCSLURdyEpUlI3ADLjNOmIgi4BnaT /moGhyeq7jSnu3aGpT6tdAUwkWrGZ8c= X-Received: by 2002:a17:902:e18d:: with SMTP id cd13mr31139018plb.301.1558621878491; Thu, 23 May 2019 07:31:18 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id 140sm45674391pfw.123.2019.05.23.07.30.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 May 2019 07:31:17 -0700 (PDT) Date: Thu, 23 May 2019 22:30:22 +0800 From: Gen Zhang To: kvalo@codeaurora.org, davem@davemloft.net Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] spi: Fix a memory leaking bug in wl1271_probe() Message-ID: <20190523143022.GA26485@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In wl1271_probe(), 'glue->core' is allocated by platform_device_alloc(), when this allocation fails, ENOMEM is returned. However, 'pdev_data' and 'glue' are allocated by devm_kzalloc() before 'glue->core'. When platform_device_alloc() returns NULL, we should also free 'pdev_data' and 'glue' before wl1271_probe() ends to prevent leaking memory. Similarly, we shoulf free 'pdev_data' when 'glue' is NULL. And we should free 'pdev_data' and 'glue' when 'glue->reg' is error and when 'ret' is error. Further, we should free 'glue->core', 'pdev_data' and 'glue' when this function normally ends to prevent leaking memory. Signed-off-by: Gen Zhang --- diff --git a/drivers/net/wireless/ti/wlcore/spi.c b/drivers/net/wireless/ti/wlcore/spi.c index 62ce54a..3a020bd 100644 --- a/drivers/net/wireless/ti/wlcore/spi.c +++ b/drivers/net/wireless/ti/wlcore/spi.c @@ -480,7 +480,7 @@ static int wl1271_probe(struct spi_device *spi) struct wl12xx_spi_glue *glue; struct wlcore_platdev_data *pdev_data; struct resource res[1]; - int ret; + int ret = -ENOMEM; pdev_data = devm_kzalloc(&spi->dev, sizeof(*pdev_data), GFP_KERNEL); if (!pdev_data) @@ -491,7 +491,8 @@ static int wl1271_probe(struct spi_device *spi) glue = devm_kzalloc(&spi->dev, sizeof(*glue), GFP_KERNEL); if (!glue) { dev_err(&spi->dev, "can't allocate glue\n"); - return -ENOMEM; + ret = -ENOMEM; + goto out_free1; } glue->dev = &spi->dev; @@ -503,31 +504,35 @@ static int wl1271_probe(struct spi_device *spi) spi->bits_per_word = 32; glue->reg = devm_regulator_get(&spi->dev, "vwlan"); - if (PTR_ERR(glue->reg) == -EPROBE_DEFER) - return -EPROBE_DEFER; + if (PTR_ERR(glue->reg) == -EPROBE_DEFER) { + ret = -EPROBE_DEFER; + goto out_free2; + } if (IS_ERR(glue->reg)) { dev_err(glue->dev, "can't get regulator\n"); - return PTR_ERR(glue->reg); + ret = PTR_ERR(glue->reg); + goto out_free2; } ret = wlcore_probe_of(spi, glue, pdev_data); if (ret) { dev_err(glue->dev, "can't get device tree parameters (%d)\n", ret); - return ret; + goto out_free2; } ret = spi_setup(spi); if (ret < 0) { dev_err(glue->dev, "spi_setup failed\n"); - return ret; + goto out_free2; } glue->core = platform_device_alloc(pdev_data->family->name, PLATFORM_DEVID_AUTO); if (!glue->core) { dev_err(glue->dev, "can't allocate platform_device\n"); - return -ENOMEM; + ret = -ENOMEM; + goto out_free2; } glue->core->dev.parent = &spi->dev; @@ -557,10 +562,18 @@ static int wl1271_probe(struct spi_device *spi) goto out_dev_put; } + platform_device_put(glue->core); + devm_kfree(&func->dev, glue); + devm_kfree(&func->dev, pdev_data); return 0; out_dev_put: platform_device_put(glue->core); +out_free2: + devm_kfree(&func->dev, glue); +out_free1: + devm_kfree(&func->dev, pdev_data); +out: return ret; } ---