All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: smatch@vger.kernel.org
Subject: Re: [RFC PATCH] check_freeing_devm: Also track erroneous usage of kfree when the pointer has been re-assigned
Date: Thu, 26 May 2022 12:51:54 +0300	[thread overview]
Message-ID: <20220526095154.GD2146@kadam> (raw)
In-Reply-To: <6e88b795e1283cc306f9d009dcec70ff162943e8.1653548169.git.christophe.jaillet@wanadoo.fr>

On Thu, May 26, 2022 at 09:15:16AM +0200, Christophe JAILLET wrote:
> When some memory is allocated, a common pattern is to use a tmp variable
> to check if the allocation has succeeded or not. This tmp variable is then
> stored in another variable for future use.
> 
>    ptr = devm_kmalloc(...);
>    if (!ptr)
>       return -ENOMEM;
>    another_val = ptr;
> 
> So trying to see if this 'alias' is incorrectly freed with kfree makes
> sense.
> 
> To do that, add a new hook that check, when an assignment is detected if
> the right part of the expression is already recorded.
> If so, also record the right part, so that bogus kfree can check both
> reference.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Thanks! Applied.

> ---
> This is my first 'real' code proposal for smatch.
> It is likely to be wrong/incomplete/imperfect. Maybe this functionality
> is already implemented somewhere else and I just try to duplicate it.

No, it's fine.  There are a lot of different ways to write any check.
I wrote this check nine years ago.  If I were to write it now, I probably
would only hook into the free functions and not have any states.

	orig = get_assigned_expr_recurse();
	if (!orig || orig->type != EXPR_CALL)
		return;

	str = expr_to_str(orig);
	if (!str)
		return;
	if (strstr(str, "devm_"))
		sm_warning("devm_ data freed");
	free_string(str);

But, whatever, the check works fine as-is.  It's not like I have run out
of other work to do.

The other idea that I have, is to look at struct member types.  Make a
list of "(struct foo)->bar" was allocated with devm_ so that type cannot
be freed using kfree() within the same file.  It's not tied to the
variables at all, only to the struct member names.

That check would print many of the same warnings as this check but it's
fine to print duplicate warnings since the heuristic is different.

> 
> All I know is that it seams to work for me, even if it has not detected any
> issue yet :) (compiling takes SO MUCH time on my machine)

Yeah. :/  How big is your smatch_db.sqlite file?

I have been trying to speed things up on my system these past two weeks.

regards,
dan carpenter

  reply	other threads:[~2022-05-26  9:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-26  7:15 [RFC PATCH] check_freeing_devm: Also track erroneous usage of kfree when the pointer has been re-assigned Christophe JAILLET
2022-05-26  9:51 ` Dan Carpenter [this message]
2022-05-26 10:30   ` Christophe JAILLET
2022-05-26 11:07     ` Dan Carpenter
2022-05-26 11:40       ` Marion & Christophe JAILLET
2022-05-26 12:00         ` Dan Carpenter
2022-05-26 13:37           ` Christophe JAILLET
2022-05-26 13:49             ` Dan Carpenter
2022-05-26 14:22               ` Christophe JAILLET

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=20220526095154.GD2146@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=christophe.jaillet@wanadoo.fr \
    --cc=smatch@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.