public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Tuo Li <islituo@gmail.com>, idryomov@gmail.com
Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju1990@gmail.com, TOTE Robot <oslab@tsinghua.edu.cn>
Subject: Re: [PATCH] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()
Date: Thu, 05 Aug 2021 08:29:30 -0400	[thread overview]
Message-ID: <ea6c827bcef4a0e424641f5eae2e17b2d0d8ebbe.camel@kernel.org> (raw)
In-Reply-To: <20210805122015.129824-1-islituo@gmail.com>

On Thu, 2021-08-05 at 05:20 -0700, Tuo Li wrote:
> kcalloc() is called to allocate memory for m->m_info, and if it fails, 
> ceph_mdsmap_destroy() behind the label out_err will be called:
>   ceph_mdsmap_destroy(m);
> 
> In ceph_mdsmap_destroy(), m->m_info is dereferenced through:
>   kfree(m->m_info[i].export_targets);
> 
> To fix this possible null-pointer dereference, if memory allocation
> for m->m_info fails, free m and return -ENOMEM.
> 
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
>  fs/ceph/mdsmap.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ceph/mdsmap.c b/fs/ceph/mdsmap.c
> index abd9af7727ad..7d73e4b64b12 100644
> --- a/fs/ceph/mdsmap.c
> +++ b/fs/ceph/mdsmap.c
> @@ -166,8 +166,10 @@ struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2)
>  	m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds);
>  
>  	m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS);
> -	if (!m->m_info)
> -		goto nomem;
> +	if (!m->m_info) {
> +		kfree(m);
> +		return ERR_PTR(-ENOMEM);
> +	}
>  
>  	/* pick out active nodes from mds_info (state > 0) */
>  	for (i = 0; i < n; i++) {

Good catch. This function is already pretty complex. How about we
instead fix this in ceph_mdsmap_destroy and make it safe to call that
with the mdsmap in this state?

Basically, just put an "if (m->m_info)" around the for loop in that
function. Sound ok?

-- 
Jeff Layton <jlayton@kernel.org>


  reply	other threads:[~2021-08-05 12:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05 12:20 [PATCH] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode() Tuo Li
2021-08-05 12:29 ` Jeff Layton [this message]
2021-08-05 12:52   ` Tuo Li

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=ea6c827bcef4a0e424641f5eae2e17b2d0d8ebbe.camel@kernel.org \
    --to=jlayton@kernel.org \
    --cc=baijiaju1990@gmail.com \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=islituo@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oslab@tsinghua.edu.cn \
    /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