mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: trix@redhat.com
Cc: jack@suse.cz, william.kucharski@oracle.com, jeffm@suse.com,
	joseph.qi@linux.alibaba.com, liao.pingfang@zte.com.cn,
	reiserfs-devel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] reiserfs : fix improper free in reiserfs_get_block
Date: Tue, 14 Jul 2020 14:10:43 +0100	[thread overview]
Message-ID: <20200714131043.GB12769@casper.infradead.org> (raw)
In-Reply-To: <20200714130509.11791-1-trix@redhat.com>

On Tue, Jul 14, 2020 at 06:05:09AM -0700, trix@redhat.com wrote:
> From: Tom Rix <trix@redhat.com>
> 
> clang static analysis flags this error
> 
> inode.c:1083:5: warning: Argument to kfree() is the address of the
>   local variable 'unf_single', which is not memory allocated by
>   malloc() [unix.Malloc]
>                                 kfree(un);
>                                 ^~~~~~~~~
> Assignment of 'un'
> 
> 	/*
> 	 * We use this in case we need to allocate
> 	 * only one block which is a fastpath
> 	 */
> 	unp_t unf_single = 0;
> 
> 	...
> 
> 	if (blocks_needed == 1) {
> 		un = &unf_single;
> 	} else {
> 		un = kcalloc(min(blocks_needed, max_to_insert),
> 			     UNFM_P_SIZE, GFP_NOFS);
> 		if (!un) {
> 			un = &unf_single;
> 			blocks_needed = 1;
> 			max_to_insert = 0;
> 		}
> 	}
> 
> The logic to free 'un'
> 
> 	if (blocks_needed != 1)
> 		kfree(un);
> 
> Because the kcalloc failure falls back to using unf_single,
> the if-check for the free is wrong.

I think you mean "Because clang's static analysis is limited, it
warns incorrectly about this".  There's no path to get to the
kfree with blocks_needed != 1 and un being equal to &unf_single.

> So improve the check.
> 
> Signed-off-by: Tom Rix <trix@redhat.com>
> ---
>  fs/reiserfs/inode.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/reiserfs/inode.c b/fs/reiserfs/inode.c
> index 1509775da040..4d62148e43e6 100644
> --- a/fs/reiserfs/inode.c
> +++ b/fs/reiserfs/inode.c
> @@ -1079,7 +1079,7 @@ int reiserfs_get_block(struct inode *inode, sector_t block,
>  						     UNFM_P_SIZE *
>  						     blocks_needed);
>  
> -			if (blocks_needed != 1)
> +			if (un != &unf_single)
>  				kfree(un);

I don't actually object to this patch, but your analysis of clang's
analysis is wrong.

  reply	other threads:[~2020-07-14 13:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-14 13:05 trix
2020-07-14 13:10 ` Matthew Wilcox [this message]
2020-07-14 13:12   ` Tom Rix
2020-07-15  8:04     ` Jan Kara
2020-07-16 13:48       ` Tom Rix

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=20200714131043.GB12769@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=jack@suse.cz \
    --cc=jeffm@suse.com \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=liao.pingfang@zte.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=reiserfs-devel@vger.kernel.org \
    --cc=trix@redhat.com \
    --cc=william.kucharski@oracle.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

all inboxes | Powered by JetHome®