ceph-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()
@ 2021-08-05 12:20 Tuo Li
  2021-08-05 12:29 ` Jeff Layton
  0 siblings, 1 reply; 3+ messages in thread
From: Tuo Li @ 2021-08-05 12:20 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, 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++) {
-- 
2.25.1


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

* Re: [PATCH] ceph: fix possible null-pointer dereference in ceph_mdsmap_decode()
  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
  2021-08-05 12:52   ` Tuo Li
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff Layton @ 2021-08-05 12:29 UTC (permalink / raw)
  To: Tuo Li, idryomov; +Cc: ceph-devel, linux-kernel, baijiaju1990, TOTE Robot

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>


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

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

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.


On 2021/8/5 20:29, Jeff Layton wrote:
> 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?
>


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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2021-08-05 12:52   ` Tuo Li

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