Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1269059imm; Fri, 28 Sep 2018 15:18:48 -0700 (PDT) X-Google-Smtp-Source: ACcGV62tTWSiiJruiNaBEK3HJFB+bQGAKT/J2Z87b+9Fswbyt8YpYtEkW0nhkgczEH2uapemw4OD X-Received: by 2002:a63:65c7:: with SMTP id z190-v6mr547685pgb.146.1538173128635; Fri, 28 Sep 2018 15:18:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538173128; cv=none; d=google.com; s=arc-20160816; b=dlQ0o1r4GCr56k6Fbf69yjTLlPcoAKwi1aZv1OQnTPX4IZEJcpLNEzwEWNASITwhvo S+T6G+E4oiZlGQLWbHdU5VYCFkCCrT8zXtLI5MDVNUlHFh4Q0VM6z3gGdg1QTZWYV84l ERIc68zp5rIcsSh3pEp8KFWF33q6go1NXkKMX6sGK50/yE/fRVVJP0frA8HJmCowF9/u ORybdViJpXy9chSICnBnqUK70l/ih5T05MibP1tNSnfQ0LFXnrP/TWlH6V82Ibr7HvWp l1+HsUM7saB1IKBYJe8/zG6RvEDuJHWp4TP5fzKA3VtX36F8LAHLQ6nHmBhmyYhuZ3m0 74qA== 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; bh=gtLPTQsJ1Y0ywvlsMAJhLgYov1Y0jAnFUB1dJhoTGmg=; b=yNo4A4vKTGkwQobJcvdN4VL8+EolGyea6/6dgnWor1xnAFU0zYdvGOYp7mwbA2Yj9j 4rY2ROLQbrz+mlhVk/4uil2sOr/6q18eFLzUsiPDcTpjd+w5QfjiMdsyzso6WniC0oZL 5+6SNf5EU7YxKio1lIo11lJiElOAs5+M3DnAvNZRbrdpxti4QssPv0KweTQKNnTiRSDX yru4j3UICt0F1nxSpgOknOm7Y3LLa8KW9T/7SwLn9UBzzDjHPZJL9vH317Z4/5TfpHeR qAC4p7zQRLM7yRnD7qAzHXGWDl+JnXn6qVkN08WL4xWxL/QcrzsWAESFCUDg8gXzrpLg Fo7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lp3g6lCc; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e83-v6si5764915pfk.198.2018.09.28.15.18.33; Fri, 28 Sep 2018 15:18:48 -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=@chromium.org header.s=google header.b=lp3g6lCc; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726739AbeI2EoM (ORCPT + 99 others); Sat, 29 Sep 2018 00:44:12 -0400 Received: from mail-yw1-f65.google.com ([209.85.161.65]:44739 "EHLO mail-yw1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726429AbeI2EoL (ORCPT ); Sat, 29 Sep 2018 00:44:11 -0400 Received: by mail-yw1-f65.google.com with SMTP id s73-v6so3270909ywg.11 for ; Fri, 28 Sep 2018 15:18:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=gtLPTQsJ1Y0ywvlsMAJhLgYov1Y0jAnFUB1dJhoTGmg=; b=lp3g6lCcwcpTQC5Hasszi/ii/w+Qu/1Asq5Q0+BE5i0dxWqBnDiuZZqiixG36vP6pT ckdHyM5MNtLKSFFJXyad+GlEtNunqB0zl6eXm2iWuET7ozk9kjM3gIPKD7qWo7a8JpMW zKt/v1GH19CosKMOZjRrv8QGnERg2a+ZbyaOE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=gtLPTQsJ1Y0ywvlsMAJhLgYov1Y0jAnFUB1dJhoTGmg=; b=rotcxShBWtJZfdDM80iwLlJBCPPEUa3iFIdafYhjUxDFMA3rClFT5mq/jeHnAHhf0J l0sL/OXvS37mUimXBvZ5Ua7Zle6saqtRzPW0ocmSh0WGgJMFUbwjyVMO1CaMDAiFRliL yGBi6D2EVG6xZthfFlFa6UQIy5bMGl9wx+x4n5yMLsvz14IqyVmZgesNszl0MZXkONXa v+989BXVUiI+BoUmSyiBMP2Hm4Zoj6pKstMUtKm5Z3BRDT9TgsvkIpY99tCxprED35t2 +3QCgg6mL5sek3k/rlgeVX5CO4727I4zPD163RAX874NNRJnLEFU75eyV6HAjdpbYPz6 FOkg== X-Gm-Message-State: ABuFfog8J4YtwoLtbTfi2C9l8ygPj0KMAzU6GwmfvSuZ9QT5ScwFFYQb B0Z98Q1bxZVvPpphyYWnLUi8Z0RzZ9Y= X-Received: by 2002:a81:5908:: with SMTP id n8-v6mr368003ywb.354.1538173102279; Fri, 28 Sep 2018 15:18:22 -0700 (PDT) Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com. [209.85.219.176]) by smtp.gmail.com with ESMTPSA id 124-v6sm2419727yws.25.2018.09.28.15.18.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Sep 2018 15:18:21 -0700 (PDT) Received: by mail-yb1-f176.google.com with SMTP id 5-v6so3327359ybf.3 for ; Fri, 28 Sep 2018 15:18:21 -0700 (PDT) X-Received: by 2002:a25:dd82:: with SMTP id u124-v6mr365705ybg.171.1538173100665; Fri, 28 Sep 2018 15:18:20 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d116:0:0:0:0:0 with HTTP; Fri, 28 Sep 2018 15:18:19 -0700 (PDT) In-Reply-To: <20180917091531.21356-1-nixiaoming@huawei.com> References: <20180917091531.21356-1-nixiaoming@huawei.com> From: Kees Cook Date: Fri, 28 Sep 2018 15:18:19 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] fix memory leak in ramoops_init To: nixiaoming Cc: Jan Kara , Amir Goldstein , "linux-fsdevel@vger.kernel.org" , LKML , Andrew Morton 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 In the future, please use scripts/get_maintainer.pl to find your To/Cc list. :) On Mon, Sep 17, 2018 at 2:15 AM, 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 Yup, that's a problem. > 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 The kfree(NULL) isn't a problem, but a NULL platform_data implies device tree data is expected, so it'll just confuse things. However, since the dummy platform data is optional, there's no need to plumb the return value back up. Either it gets set up correctly, or there is a pr_info() about why it has been ignored. > 3, memory leak in ramoops_init. > miss platform_device_unregister(dummy) and kfree(dummy_data) > when platform_driver_register(&ramoops_driver) return fail Agreed. > 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) Another bug is that this should actually be __init (it's only called by an __init). > { > 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 > I'll send an updated patch with a similar fix... -Kees -- Kees Cook Pixel Security