ceph-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()
@ 2021-08-05 15:14 Tuo Li
  2021-08-05 19:25 ` Jeff Layton
  0 siblings, 1 reply; 2+ messages in thread
From: Tuo Li @ 2021-08-05 15:14 UTC (permalink / raw)
  To: jlayton, idryomov
  Cc: ceph-devel, linux-kernel, baijiaju1990, Tuo Li, TOTE Robot

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, check m->m_info before the 
for loop to free m->m_info[i].export_targets.

Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
v2:
* Put an "if (m->m_info)" around the for loop in ceph_mdsmap_destroy()
instead of freeing m and returning -ENOMEM in ceph_mdsmap_decode().
  Thank Jeff Layton for helpful advice.
---
 fs/ceph/mdsmap.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/ceph/mdsmap.c b/fs/ceph/mdsmap.c
index abd9af7727ad..26d6fa049b44 100644
--- a/fs/ceph/mdsmap.c
+++ b/fs/ceph/mdsmap.c
@@ -393,9 +393,11 @@ struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2)
 void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
 {
 	int i;
-
-	for (i = 0; i < m->possible_max_rank; i++)
-		kfree(m->m_info[i].export_targets);
+	
+	if (m->m_info) {
+		for (i = 0; i < m->possible_max_rank; i++)
+			kfree(m->m_info[i].export_targets);
+	}
 	kfree(m->m_info);
 	kfree(m->m_data_pg_pools);
 	kfree(m);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()
  2021-08-05 15:14 [PATCH v2] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode() Tuo Li
@ 2021-08-05 19:25 ` Jeff Layton
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Layton @ 2021-08-05 19:25 UTC (permalink / raw)
  To: Tuo Li, idryomov; +Cc: ceph-devel, linux-kernel, baijiaju1990, TOTE Robot

On Thu, 2021-08-05 at 08:14 -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, check m->m_info before the 
> for loop to free m->m_info[i].export_targets.
> 
> Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
> v2:
> * Put an "if (m->m_info)" around the for loop in ceph_mdsmap_destroy()
> instead of freeing m and returning -ENOMEM in ceph_mdsmap_decode().
>   Thank Jeff Layton for helpful advice.
> ---
>  fs/ceph/mdsmap.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ceph/mdsmap.c b/fs/ceph/mdsmap.c
> index abd9af7727ad..26d6fa049b44 100644
> --- a/fs/ceph/mdsmap.c
> +++ b/fs/ceph/mdsmap.c
> @@ -393,9 +393,11 @@ struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2)
>  void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
>  {
>  	int i;
> -
> -	for (i = 0; i < m->possible_max_rank; i++)
> -		kfree(m->m_info[i].export_targets);
> +	

This patch added some whitespace damage above. I went ahead and fixed it
up before merging.

> +	if (m->m_info) {
> +		for (i = 0; i < m->possible_max_rank; i++)
> +			kfree(m->m_info[i].export_targets);
> +	}
>  	kfree(m->m_info);

I also moved the above kfree into the if statement since we have to
check it anyway (no need to do it twice).

>  	kfree(m->m_data_pg_pools);
>  	kfree(m);

Merged! Thanks for the patch.
-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-05 19:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-05 15:14 [PATCH v2] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode() Tuo Li
2021-08-05 19:25 ` Jeff Layton

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).