[Devel,rh7] ms/mm/list_lru.c: avoid error-path NULL pointer deref

Submitted by Andrey Ryabinin on June 19, 2017, 1:41 p.m.

Details

Message ID 20170619134119.14672-1-aryabinin@virtuozzo.com
State New
Series "ms/mm/list_lru.c: avoid error-path NULL pointer deref"
Headers show

Commit Message

Andrey Ryabinin June 19, 2017, 1:41 p.m.
From: Alexander Polakov <apolyakov@beget.ru>

commit 1bc11d70b5db7c6bb1414b283d7f09b1fe1ac0d0 upsteram.

As described in https://bugzilla.kernel.org/show_bug.cgi?id=177821:

After some analysis it seems to be that the problem is in alloc_super().
In case list_lru_init_memcg() fails it goes into destroy_super(), which
calls list_lru_destroy().

And in list_lru_init() we see that in case memcg_init_list_lru() fails,
lru->node is freed, but not set NULL, which then leads list_lru_destroy()
to believe it is initialized and call memcg_destroy_list_lru().
memcg_destroy_list_lru() in turn can access lru->node[i].memcg_lrus,
which is NULL.

[akpm@linux-foundation.org: add comment]
Signed-off-by: Alexander Polakov <apolyakov@beget.ru>
Acked-by: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

https://jira.sw.ru/browse/PSBM-67300
Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
---
 mm/list_lru.c | 2 ++
 1 file changed, 2 insertions(+)

Patch hide | download patch | download mbox

diff --git a/mm/list_lru.c b/mm/list_lru.c
index 909eca2c820e..84b4c21d78d7 100644
--- a/mm/list_lru.c
+++ b/mm/list_lru.c
@@ -532,6 +532,8 @@  int __list_lru_init(struct list_lru *lru, bool memcg_aware,
 	err = memcg_init_list_lru(lru, memcg_aware);
 	if (err) {
 		kfree(lru->node);
+		/* Do this so a list_lru_destroy() doesn't crash: */
+		lru->node = NULL;
 		goto out;
 	}
 

Comments

Konstantin Khorenko June 19, 2017, 2:14 p.m.
Andrey, thank you!

Zhenya, Vasya, please prepare a ReadyKernel patch for it.

https://readykernel.com/

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

On 06/19/2017 04:41 PM, Andrey Ryabinin wrote:
> From: Alexander Polakov <apolyakov@beget.ru>
>
> commit 1bc11d70b5db7c6bb1414b283d7f09b1fe1ac0d0 upsteram.
>
> As described in https://bugzilla.kernel.org/show_bug.cgi?id=177821:
>
> After some analysis it seems to be that the problem is in alloc_super().
> In case list_lru_init_memcg() fails it goes into destroy_super(), which
> calls list_lru_destroy().
>
> And in list_lru_init() we see that in case memcg_init_list_lru() fails,
> lru->node is freed, but not set NULL, which then leads list_lru_destroy()
> to believe it is initialized and call memcg_destroy_list_lru().
> memcg_destroy_list_lru() in turn can access lru->node[i].memcg_lrus,
> which is NULL.
>
> [akpm@linux-foundation.org: add comment]
> Signed-off-by: Alexander Polakov <apolyakov@beget.ru>
> Acked-by: Vladimir Davydov <vdavydov.dev@gmail.com>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
>
> https://jira.sw.ru/browse/PSBM-67300
> Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
> ---
>  mm/list_lru.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/mm/list_lru.c b/mm/list_lru.c
> index 909eca2c820e..84b4c21d78d7 100644
> --- a/mm/list_lru.c
> +++ b/mm/list_lru.c
> @@ -532,6 +532,8 @@ int __list_lru_init(struct list_lru *lru, bool memcg_aware,
>  	err = memcg_init_list_lru(lru, memcg_aware);
>  	if (err) {
>  		kfree(lru->node);
> +		/* Do this so a list_lru_destroy() doesn't crash: */
> +		lru->node = NULL;
>  		goto out;
>  	}
>
>
Evgenii Shatokhin June 19, 2017, 2:49 p.m.
On 19.06.2017 17:14, Konstantin Khorenko wrote:
> Andrey, thank you!
>
> Zhenya, Vasya, please prepare a ReadyKernel patch for it.

I'll try to add this fix to the version 23.x of RK patches, work in 
progress.

>
> https://readykernel.com/
>
> --
> Best regards,
>
> Konstantin Khorenko,
> Virtuozzo Linux Kernel Team
>
> On 06/19/2017 04:41 PM, Andrey Ryabinin wrote:
>> From: Alexander Polakov <apolyakov@beget.ru>
>>
>> commit 1bc11d70b5db7c6bb1414b283d7f09b1fe1ac0d0 upsteram.
>>
>> As described in https://bugzilla.kernel.org/show_bug.cgi?id=177821:
>>
>> After some analysis it seems to be that the problem is in alloc_super().
>> In case list_lru_init_memcg() fails it goes into destroy_super(), which
>> calls list_lru_destroy().
>>
>> And in list_lru_init() we see that in case memcg_init_list_lru() fails,
>> lru->node is freed, but not set NULL, which then leads list_lru_destroy()
>> to believe it is initialized and call memcg_destroy_list_lru().
>> memcg_destroy_list_lru() in turn can access lru->node[i].memcg_lrus,
>> which is NULL.
>>
>> [akpm@linux-foundation.org: add comment]
>> Signed-off-by: Alexander Polakov <apolyakov@beget.ru>
>> Acked-by: Vladimir Davydov <vdavydov.dev@gmail.com>
>> Cc: Al Viro <viro@zeniv.linux.org.uk>
>> Cc: <stable@vger.kernel.org>
>> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
>>
>> https://jira.sw.ru/browse/PSBM-67300
>> Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
>> ---
>>  mm/list_lru.c | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/mm/list_lru.c b/mm/list_lru.c
>> index 909eca2c820e..84b4c21d78d7 100644
>> --- a/mm/list_lru.c
>> +++ b/mm/list_lru.c
>> @@ -532,6 +532,8 @@ int __list_lru_init(struct list_lru *lru, bool
>> memcg_aware,
>>      err = memcg_init_list_lru(lru, memcg_aware);
>>      if (err) {
>>          kfree(lru->node);
>> +        /* Do this so a list_lru_destroy() doesn't crash: */
>> +        lru->node = NULL;
>>          goto out;
>>      }
>>
>>
> .
>