Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1226616imm; Fri, 28 Sep 2018 14:27:03 -0700 (PDT) X-Google-Smtp-Source: ACcGV62M/e3p91apLRhg/+IEi7Hd54XCdHpajF6WxyNlWA7KZ9RqhVSluOq+g82iQBgAc5pkGgQv X-Received: by 2002:a63:c608:: with SMTP id w8-v6mr415647pgg.16.1538170022938; Fri, 28 Sep 2018 14:27:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538170022; cv=none; d=google.com; s=arc-20160816; b=pfHXv5sjlATRQ0TPBjPYOlzyOQPiR9Q7RVoImtNkzxIJZyaaH9+Ej3XneunY9QpPMh T5xkqvlD0+v4UwqCSW83+2VMOWBS3uvDu7NJqn0Um/a7jjgX2K8b9QOSCRQJVFKfVPuU 1MEqPv2w8iKPv2Iyju5niorB8e/w6P25Yi5cGGhqoR37CMpJ/iQQbvJYqBUNt0M26eHR qRxfuG6+C67ihic1b6CO8tl8khtjxVb81oaxL2SStoyL52uf4y1bEz8XH2tMFfksIECV dD+VxpfhUWUtyZRQ0RY9YZ7ZOumrUToR0cm7JIlLG/eKUGHCMXB1Ve2e80FO3dH0wkiX elUw== 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:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=Biw2hGaph0E16znt7fXOs7NQ3HA82ApxCq/jIMM0URw=; b=VvUETdnNqdfidtLaFHXCj0pjG0sESzrJQq9mdHiUT//qPwQkTYHCiTgAwuoR4hpgYt QYqAr+AyFfanmqnzP4pL3AC8yGOa9Y77xiVVLxINtaHTH8KkQuZqZ9mavs6P0mEsyQyN kWY0piHrtU5cEHt90OBkhJt21QYpjOL0Ch096siFu75Z9YvHGMABJ2++csOpNXJ/15o8 iaTmnEf3xXJ6ngAXoLlBC7L1gnnFNVJ4K3s9aKTVPpczR7q+oDTfOx3stukwIX5c0YOR 7Cg/tbfz1xDpamxUVBHz5pZ8L8OLWFT5iU2tzz/ctaVEm2VhfWEmEOpySTVRi0XpDOAY 9GmA== ARC-Authentication-Results: i=1; mx.google.com; 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 d34-v6si6018896pla.195.2018.09.28.14.26.47; Fri, 28 Sep 2018 14:27:02 -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; 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 S1727357AbeI2DwK (ORCPT + 99 others); Fri, 28 Sep 2018 23:52:10 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:35346 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726451AbeI2DwK (ORCPT ); Fri, 28 Sep 2018 23:52:10 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.8.65]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D4D44CAB; Fri, 28 Sep 2018 21:26:31 +0000 (UTC) Date: Fri, 28 Sep 2018 14:26:30 -0700 From: Andrew Morton To: nixiaoming Cc: , , , , Kees Cook , Joel Fernandes , Geliang Tang Subject: Re: [PATCH] fix memory leak in ramoops_init Message-Id: <20180928142630.f4501105f413f2b5b4772a8b@linux-foundation.org> In-Reply-To: <20180917091531.21356-1-nixiaoming@huawei.com> References: <20180917091531.21356-1-nixiaoming@huawei.com> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 17 Sep 2018 17:15:31 +0800 nixiaoming wrote: > 1, memory leak in ramoops_register_dummy. > dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); > but no free when platform_device_register_data return fail > > 2, if kzalloc(sizeof(*dummy_data), GFP_KERNEL) return NULL, > but platform_driver_register(&ramoops_driver) return 0 > kfree(NULL) in ramoops_exit > so, add return val for ramoops_register_dummy, and check it in ramoops_init > > 3, memory leak in ramoops_init. > miss platform_device_unregister(dummy) and kfree(dummy_data) > when platform_driver_register(&ramoops_driver) return fail Looks right. It's unclear (to me) who maintains fs/pstore/ram.c. Let's add some Cc's and see if we can catch a reviewed-by. From: nixiaoming Subject: fs/pstore/ram.c: fix memory leak in ramoops_init() 1, memory leak in ramoops_register_dummy. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); but no free when platform_device_register_data return fail 2, if kzalloc(sizeof(*dummy_data), GFP_KERNEL) return NULL, but platform_driver_register(&ramoops_driver) return 0 kfree(NULL) in ramoops_exit so, add return val for ramoops_register_dummy, and check it in ramoops_init 3, memory leak in ramoops_init. miss platform_device_unregister(dummy) and kfree(dummy_data) when platform_driver_register(&ramoops_driver) return fail Link: http://lkml.kernel.org/r/20180917091531.21356-1-nixiaoming@huawei.com Signed-off-by: nixiaoming Cc: Jan Kara Cc: Amir Goldstein Cc: Kees Cook Cc: Joel Fernandes Cc: Geliang Tang Signed-off-by: Andrew Morton --- fs/pstore/ram.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) --- a/fs/pstore/ram.c~fix-memory-leak-in-ramoops_init +++ a/fs/pstore/ram.c @@ -898,17 +898,17 @@ static struct platform_driver ramoops_dr }, }; -static void ramoops_register_dummy(void) +static int ramoops_register_dummy(void) { if (!mem_size) - return; + return -EINVAL; pr_info("using module parameters\n"); dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL); if (!dummy_data) { pr_info("could not allocate pdata\n"); - return; + return -ENOMEM; } dummy_data->mem_size = mem_size; @@ -932,13 +932,25 @@ static void ramoops_register_dummy(void) if (IS_ERR(dummy)) { pr_info("could not create platform device: %ld\n", PTR_ERR(dummy)); + kfree(dummy_data); + return PTR_ERR(dummy); } + return 0; } static int __init ramoops_init(void) { - ramoops_register_dummy(); - return platform_driver_register(&ramoops_driver); + int ret = ramoops_register_dummy(); + + if (ret != 0) + return ret; + + ret = platform_driver_register(&ramoops_driver); + if (ret != 0) { + platform_device_unregister(dummy); + kfree(dummy_data); + } + return ret; } late_initcall(ramoops_init); _