linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Shaobo He <shaobo@cs.utah.edu>, linux-rdma@vger.kernel.org
Cc: Steve Wise <swise@chelsio.com>,
	Doug Ledford <dledford@redhat.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	open list <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] cxgb4: fix undefined behavior in mem.c
Date: Thu, 28 Feb 2019 14:56:36 -0800	[thread overview]
Message-ID: <1551394596.31902.209.camel@acm.org> (raw)
In-Reply-To: <1551393519-96595-1-git-send-email-shaobo@cs.utah.edu>

On Thu, 2019-02-28 at 15:38 -0700, Shaobo He wrote:
> In function `c4iw_dealloc_mw`, variable mhp's value is printed after
> freed, which triggers undefined behavior according to this post:
> https://trust-in-soft.com/dangling-pointer-indeterminate/.
> 
> This commit fixes it by swapping the order of `kfree` and `pr_debug`.
> 
> Signed-off-by: Shaobo He <shaobo@cs.utah.edu>
> ---
>  drivers/infiniband/hw/cxgb4/mem.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/infiniband/hw/cxgb4/mem.c b/drivers/infiniband/hw/cxgb4/mem.c
> index 7b76e6f..bb8e0bc 100644
> --- a/drivers/infiniband/hw/cxgb4/mem.c
> +++ b/drivers/infiniband/hw/cxgb4/mem.c
> @@ -684,8 +684,8 @@ int c4iw_dealloc_mw(struct ib_mw *mw)
>  			  mhp->wr_waitp);
>  	kfree_skb(mhp->dereg_skb);
>  	c4iw_put_wr_wait(mhp->wr_waitp);
> -	kfree(mhp);
>  	pr_debug("ib_mw %p mmid 0x%x ptr %p\n", mw, mmid, mhp);
> +	kfree(mhp);
>  	return 0;
>  }

Please quote the relevant paragraphs from the C standard. All I have found
about free() in ISO/IEC 9899:2017 is the following:

Description
The free function causes the space pointed to by ptr to be deallocated, that
is, made available for further allocation. If ptr is a null pointer, no
action occurs. Otherwise, if the argument does not match a pointer earlier
returned by a memory management function, or if the space has been
deallocated by a call to free or realloc, the behavior is undefined.

That is not sufficient to claim that the above code triggers undefined
behavior.

Bart.

  reply	other threads:[~2019-02-28 22:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-28 22:38 [PATCH] cxgb4: fix undefined behavior in mem.c Shaobo He
2019-02-28 22:56 ` Bart Van Assche [this message]
2019-02-28 23:18   ` Shaobo He
2019-02-28 23:33     ` Bart Van Assche
2019-02-28 23:57       ` Shaobo He
2019-03-01 14:26         ` Doug Ledford
2019-03-01 21:21           ` Shaobo He
2019-03-01 18:15     ` Christopher Lameter
2019-03-04 19:54 ` Jason Gunthorpe

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=1551394596.31902.209.camel@acm.org \
    --to=bvanassche@acm.org \
    --cc=dledford@redhat.com \
    --cc=jgg@ziepe.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=shaobo@cs.utah.edu \
    --cc=swise@chelsio.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).