Skip to contentSkip to conversation

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

PATCH In review Tuo Li 2 participants 2 messages Aug 5, 2021 Ceph client

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

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

---
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(-)
fs/ceph/mdsmap.c +5 -3
index abd9af7727ad..26d6fa049b44 100644
@@ -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);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 26 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, check m->m_info before the for loop to free m->m_info[i].export_targets.

---
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(-)
fs/ceph/mdsmap.c +0 -0
index abd9af7727ad..26d6fa049b44 100644

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

Quoted 7 lines
fs/ceph/mdsmap.c +1 -3
@@ -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.

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

Quoted 5 lines
fs/ceph/mdsmap.c +4 -0
+	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).

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

Quoted 2 lines
fs/ceph/mdsmap.c +0 -0
 	kfree(m->m_data_pg_pools);
 	kfree(m);

Merged! Thanks for the patch.

Signature · 1 line
Jeff Layton <[email protected]>