[PATCH] mm: memory_hotplug: fix memory error handling

Muchun Song posted 1 patch 3 years, 11 months ago
drivers/base/memory.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
[PATCH] mm: memory_hotplug: fix memory error handling
Posted by Muchun Song 3 years, 11 months ago
The device_unregister() is supposed to be used to unregister devices if
device_register() has succeed.  And device_unregister() will put device.
The caller should not do it again, otherwise, the first call of
put_device() will drop the last reference count, then the next call
of device_unregister() will UAF on device.

Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Cc: <stable@vger.kernel.org>
---
 drivers/base/memory.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/base/memory.c b/drivers/base/memory.c
index 7222ff9b5e05..084d67fd55cc 100644
--- a/drivers/base/memory.c
+++ b/drivers/base/memory.c
@@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
 	}
 	ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
 			      GFP_KERNEL));
-	if (ret) {
-		put_device(&memory->dev);
+	if (ret)
 		device_unregister(&memory->dev);
-	}
+
 	return ret;
 }
 
-- 
2.11.0
Re: [PATCH] mm: memory_hotplug: fix memory error handling
Posted by David Hildenbrand 3 years, 11 months ago
On 30.05.22 07:33, Muchun Song wrote:
> The device_unregister() is supposed to be used to unregister devices if
> device_register() has succeed.  And device_unregister() will put device.
> The caller should not do it again, otherwise, the first call of
> put_device() will drop the last reference count, then the next call
> of device_unregister() will UAF on device.
> 
> Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> Cc: <stable@vger.kernel.org>
> ---
>  drivers/base/memory.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> index 7222ff9b5e05..084d67fd55cc 100644
> --- a/drivers/base/memory.c
> +++ b/drivers/base/memory.c
> @@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
>  	}
>  	ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
>  			      GFP_KERNEL));
> -	if (ret) {
> -		put_device(&memory->dev);
> +	if (ret)
>  		device_unregister(&memory->dev);
> -	}
> +
>  	return ret;
>  }
>  

See

https://lkml.kernel.org/r/d44c63d78affe844f020dc02ad6af29abc448fc4.1650611702.git.christophe.jaillet@wanadoo.fr

-- 
Thanks,

David / dhildenb
Re: [PATCH] mm: memory_hotplug: fix memory error handling
Posted by Muchun Song 3 years, 11 months ago
On Mon, May 30, 2022 at 2:56 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 30.05.22 07:33, Muchun Song wrote:
> > The device_unregister() is supposed to be used to unregister devices if
> > device_register() has succeed.  And device_unregister() will put device.
> > The caller should not do it again, otherwise, the first call of
> > put_device() will drop the last reference count, then the next call
> > of device_unregister() will UAF on device.
> >
> > Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
> > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > Cc: <stable@vger.kernel.org>
> > ---
> >  drivers/base/memory.c | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> > index 7222ff9b5e05..084d67fd55cc 100644
> > --- a/drivers/base/memory.c
> > +++ b/drivers/base/memory.c
> > @@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
> >       }
> >       ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
> >                             GFP_KERNEL));
> > -     if (ret) {
> > -             put_device(&memory->dev);
> > +     if (ret)
> >               device_unregister(&memory->dev);
> > -     }
> > +
> >       return ret;
> >  }
> >
>
> See
>
> https://lkml.kernel.org/r/d44c63d78affe844f020dc02ad6af29abc448fc4.1650611702.git.christophe.jaillet@wanadoo.fr
>

I see. Good job by Christophe. Thanks David.
Re: [PATCH] mm: memory_hotplug: fix memory error handling
Posted by David Hildenbrand 3 years, 11 months ago
On 30.05.22 09:08, Muchun Song wrote:
> On Mon, May 30, 2022 at 2:56 PM David Hildenbrand <david@redhat.com> wrote:
>>
>> On 30.05.22 07:33, Muchun Song wrote:
>>> The device_unregister() is supposed to be used to unregister devices if
>>> device_register() has succeed.  And device_unregister() will put device.
>>> The caller should not do it again, otherwise, the first call of
>>> put_device() will drop the last reference count, then the next call
>>> of device_unregister() will UAF on device.
>>>
>>> Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
>>> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
>>> Cc: <stable@vger.kernel.org>
>>> ---
>>>  drivers/base/memory.c | 5 ++---
>>>  1 file changed, 2 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
>>> index 7222ff9b5e05..084d67fd55cc 100644
>>> --- a/drivers/base/memory.c
>>> +++ b/drivers/base/memory.c
>>> @@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
>>>       }
>>>       ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
>>>                             GFP_KERNEL));
>>> -     if (ret) {
>>> -             put_device(&memory->dev);
>>> +     if (ret)
>>>               device_unregister(&memory->dev);
>>> -     }
>>> +
>>>       return ret;
>>>  }
>>>
>>
>> See
>>
>> https://lkml.kernel.org/r/d44c63d78affe844f020dc02ad6af29abc448fc4.1650611702.git.christophe.jaillet@wanadoo.fr
>>
> 
> I see. Good job by Christophe. Thanks David.
> 

I'm curious how both of you found that issue? Just by staring at that
code? :)

-- 
Thanks,

David / dhildenb
Re: [PATCH] mm: memory_hotplug: fix memory error handling
Posted by Muchun Song 3 years, 11 months ago
On Mon, May 30, 2022 at 3:10 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 30.05.22 09:08, Muchun Song wrote:
> > On Mon, May 30, 2022 at 2:56 PM David Hildenbrand <david@redhat.com> wrote:
> >>
> >> On 30.05.22 07:33, Muchun Song wrote:
> >>> The device_unregister() is supposed to be used to unregister devices if
> >>> device_register() has succeed.  And device_unregister() will put device.
> >>> The caller should not do it again, otherwise, the first call of
> >>> put_device() will drop the last reference count, then the next call
> >>> of device_unregister() will UAF on device.
> >>>
> >>> Fixes: 4fb6eabf1037 ("drivers/base/memory.c: cache memory blocks in xarray to accelerate lookup")
> >>> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> >>> Cc: <stable@vger.kernel.org>
> >>> ---
> >>>  drivers/base/memory.c | 5 ++---
> >>>  1 file changed, 2 insertions(+), 3 deletions(-)
> >>>
> >>> diff --git a/drivers/base/memory.c b/drivers/base/memory.c
> >>> index 7222ff9b5e05..084d67fd55cc 100644
> >>> --- a/drivers/base/memory.c
> >>> +++ b/drivers/base/memory.c
> >>> @@ -636,10 +636,9 @@ static int __add_memory_block(struct memory_block *memory)
> >>>       }
> >>>       ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
> >>>                             GFP_KERNEL));
> >>> -     if (ret) {
> >>> -             put_device(&memory->dev);
> >>> +     if (ret)
> >>>               device_unregister(&memory->dev);
> >>> -     }
> >>> +
> >>>       return ret;
> >>>  }
> >>>
> >>
> >> See
> >>
> >> https://lkml.kernel.org/r/d44c63d78affe844f020dc02ad6af29abc448fc4.1650611702.git.christophe.jaillet@wanadoo.fr
> >>
> >
> > I see. Good job by Christophe. Thanks David.
> >
>
> I'm curious how both of you found that issue? Just by staring at that
> code? :)
>

I am reading the code here today, then I saw it by chance.
No tricks :-).

THanks.