Skip to contentSkip to conversation

ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()

PATCH Superseded Tuo Li 2 participants 3 messages Aug 5, 2021 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

PATCH 1/1 ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()

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.

---
 fs/ceph/mdsmap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/ceph/mdsmap.c +4 -2
index abd9af7727ad..7d73e4b64b12 100644
@@ -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++) {
Signature · 1 line
2.25.1
#1 lore raw
Quoted 23 lines · Tuo Li

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.

---
 fs/ceph/mdsmap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/ceph/mdsmap.c +0 -0
index abd9af7727ad..7d73e4b64b12 100644
@@ -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);
 

fs/ceph/mdsmap.c struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2) · line 175

Quoted 10 lines
fs/ceph/mdsmap.c +4 -2
 	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?

Signature · 1 line
Jeff Layton <[email protected]>
author
#2 lore raw

Thanks for your feedback. It sounds good to put an "if (m->m_info)" around the for loop to fix this bug, as well as avoid other potential bugs caused by calling ceph_mdsmap_destroy(). I think we can prepare a V2 patch according to your advice.

Quoted 41 lines · Jeff Layton
Quoted 23 lines · Tuo Li

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.

---
  fs/ceph/mdsmap.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
fs/ceph/mdsmap.c +0 -0
index abd9af7727ad..7d73e4b64b12 100644
@@ -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);
 

fs/ceph/mdsmap.c struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2) · line 175

Quoted 10 lines
fs/ceph/mdsmap.c +4 -2
  	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?