linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Cengiz Can <cengiz@kernel.wtf>
Cc: linux-kernel@vger.kernel.org, Anton Vorontsov <anton@enomsg.org>,
	Colin Cross <ccross@android.com>, Tony Luck <tony.luck@intel.com>
Subject: Re: [PATCH] fs: pstore: fix double-free on ramoops_init_przs
Date: Tue, 7 Jan 2020 10:05:11 -0800	[thread overview]
Message-ID: <202001071002.A236EBCA0@keescook> (raw)
In-Reply-To: <20200107110445.162404-1-cengiz@kernel.wtf>

On Tue, Jan 07, 2020 at 02:04:46PM +0300, Cengiz Can wrote:
> According to Coverity scanner (CID 1457526) kfree on ram.c:591 frees
> label which has already been freed.
> 
> Here's the flow as I have understood (this is my first time reading
> pstore's files):
> 
> Whenever `persistent_ram_new` fails, it implicitly calls
> `persistent_ram_free(prz)` which already does `kfree(prz->label)` and a
> `kfree(prz)` consequently.
> 
> Removed `kfree(label)` to prevent double-free.

I think this is a false positive (have you actually hit the
double-free?). The logic in this area is:

label = kmalloc(...)
prz[i] = persistent_ram_new(..., label, ...)
if (IS_ERR(prz[i])) {
    kfree(label)
    while (i > 0) {
        i--;
        persistent_ram_free(prz[i]);
    }
}

nothing was freeing the label on the failed prz, but all the other prz
labels were free (i.e. there is a "i--" that skips the failed prz
alloc).

-Kees

> 
> Signed-off-by: Cengiz Can <cengiz@kernel.wtf>
> ---
>  fs/pstore/ram.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c
> index 487ee39b4..e196aa08f 100644
> --- a/fs/pstore/ram.c
> +++ b/fs/pstore/ram.c
> @@ -588,7 +588,6 @@ static int ramoops_init_przs(const char *name,
>  			dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
>  				name, record_size,
>  				(unsigned long long)*paddr, err);
> -			kfree(label);
>  
>  			while (i > 0) {
>  				i--;
> -- 
> 2.24.1
> 

-- 
Kees Cook

  reply	other threads:[~2020-01-07 18:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-07 11:04 [PATCH] fs: pstore: fix double-free on ramoops_init_przs Cengiz Can
2020-01-07 18:05 ` Kees Cook [this message]
2020-01-07 19:40   ` Cengiz Can
2020-01-08 18:01     ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202001071002.A236EBCA0@keescook \
    --to=keescook@chromium.org \
    --cc=anton@enomsg.org \
    --cc=ccross@android.com \
    --cc=cengiz@kernel.wtf \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).