Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752104AbeACLoC convert rfc822-to-8bit (ORCPT + 1 other); Wed, 3 Jan 2018 06:44:02 -0500 Received: from szxga08-in.huawei.com ([45.249.212.255]:43869 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751784AbeACLoB (ORCPT ); Wed, 3 Jan 2018 06:44:01 -0500 From: Nixiaoming To: "anton@enomsg.org" , "ccross@android.com" , "keescook@chromium.org" , "tony.luck@intel.com" , "anton.vorontsov@linaro.org" , "gregkh@linuxfoundation.org" CC: "linux-kernel@vger.kernel.org" Subject: [RESEND PATCH] fix memory leak in ramoops_init Thread-Topic: [RESEND PATCH] fix memory leak in ramoops_init Thread-Index: AQHThIgjZF5xOogr7UOElBGq9fldaw== Date: Wed, 3 Jan 2018 11:43:56 +0000 Message-ID: References: <1513049585-93784-1-git-send-email-nixiaoming@huawei.com> In-Reply-To: <1513049585-93784-1-git-send-email-nixiaoming@huawei.com> Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.57.88.168] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: 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 Signed-off-by: nixiaoming --- fs/pstore/ram.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index bd9812e..331b600 100644 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -604,17 +604,17 @@ static struct platform_driver ramoops_driver = { }, }; -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; @@ -636,13 +636,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; } postcore_initcall(ramoops_init); -- 1.9.0