mm: fix a possible null pointer dereference in setup_zone_pageset()
Subsystems (3)
PATCH 1/1 mm: fix a possible null pointer dereference in setup_zone_pageset()
The function call alloc_percpu() returns a pointer to the memory address, but it hasn't been checked. Our static analysis tool indicates that null pointer dereference may exist in pointer zone->per_cpu_pageset. It is always safe to judge the null pointer before use.
--- mm/page_alloc.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/page_alloc.c +6 -0
@@ -5703,8 +5703,14 @@ void __meminit setup_zone_pageset(struct zone *zone)/* Size may be 0 on !SMP && !NUMA */ if (sizeof (struct per_cpu_zonestat ) >0 ) zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat); +if (!zone->per_cpu_pageset) +return ; zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages); +if (!zone->per_cpu_pageset) { +free_percpu (zone->per_cpu_pageset); +return ; + }for_each_possible_cpu (cpu) {struct per_cpu_pages *pcp;struct per_cpu_zonestat *pzstats;
Signature · 1 line
2.34.1
Quoted 17 lines · Qiu-ji Chen
The function call
alloc_percpu()returns a pointer to the memory address, but it hasn't been checked. Our static analysis tool indicates that null pointer dereference may exist in pointerzone->per_cpu_pageset. It is always safe to judge the null pointer before use.Signed-off-by Qiu-ji Chen <[email protected]>Fixes 9420f89db2dd ("mm: move most of core MM initialization to mm/mm_init.c")--- mm/page_alloc.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/page_alloc.c +0
-0
mm/page_alloc.c void __meminit setup_zone_pageset(struct zone *zone) · line 5707
Quoted 5 lines
mm/page_alloc.c +2
-0@@ -5703,8 +5703,14 @@ void __meminit setup_zone_pageset(struct zone *zone)/* Size may be 0 on !SMP && !NUMA */ if (sizeof (struct per_cpu_zonestat ) >0 ) zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat); +if (!zone->per_cpu_pageset) +return ;
Don't we initialize this for all with &boot_pageset? How could this ever happen?
mm/page_alloc.c void __meminit setup_zone_pageset(struct zone *zone) · line 5712
Quoted 5 lines
mm/page_alloc.c +3
-0zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages); +if (!zone->per_cpu_pageset) { +free_percpu (zone->per_cpu_pageset); +return ;
If it's NULL, we free it. Why?
mm/page_alloc.c void __meminit setup_zone_pageset(struct zone *zone) · line 5716
Quoted 4 lines
mm/page_alloc.c +1
-0+ }for_each_possible_cpu (cpu) {struct per_cpu_pages *pcp;struct per_cpu_zonestat *pzstats;
Also, how could core code ever recover if this function would return early, leaving something partially initialized?
The missing NULL check is concerning, but looking into alloc_percpu() we treat these as atomic allocations and would print a warning in case this would ever happen. So likely it never really happens in practice.
I wonder if we simply want to leave it unmodified (IOW set to &boot_pageset) in case the allocation fails. We'd already print a warning in this unexpected scenario.
Signature · 3 lines
Cheers, David / dhildenb