Skip to contentSkip to conversation

mm: fix a possible null pointer dereference in setup_zone_pageset()

PATCH In review Qiu-ji Chen 2 participants 4 messages Nov 7, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Fixes 9420f89db2dd ("mm: move most of core MM initialization to mm/mm_init.c")

---
V2:
Fixed the incorrect code logic.
Thanks David Hildenbrand for helpful suggestion.

---
 mm/page_alloc.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/page_alloc.c +6 -0
index 8afab64814dc..7c8a74fd02d6 100644
@@ -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_zonestats)
+		return;
 
 	zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages);
+	if (!zone->per_cpu_pageset) {
+		free_percpu(zone->per_cpu_zonestats);
+		return;
+	}
 	for_each_possible_cpu(cpu) {
 		struct per_cpu_pages *pcp;
 		struct per_cpu_zonestat *pzstats;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 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 pointer zone->per_cpu_pageset. It is always safe to judge the null pointer before use.

Fixes 9420f89db2dd ("mm: move most of core MM initialization to mm/mm_init.c")

---
V2:
Fixed the incorrect code logic.
Thanks David Hildenbrand for helpful suggestion.

---
  mm/page_alloc.c | 6 ++++++
  1 file changed, 6 insertions(+)
mm/page_alloc.c +1 -0
index 8afab64814dc..7c8a74fd02d6 100644
@@ -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_zonestats)

mm/page_alloc.c void __meminit setup_zone_pageset(struct zone *zone) · line 5716

Quoted 10 lines
mm/page_alloc.c +5 -0
+		return;
 
  	zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages);
+	if (!zone->per_cpu_pageset) {
+		free_percpu(zone->per_cpu_zonestats);
+		return;
+	}
  	for_each_possible_cpu(cpu) {
  		struct per_cpu_pages *pcp;
  		struct per_cpu_zonestat *pzstats;

Unmodified patch, likely not what you wanted to send?

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw

Hello, the previous patch did not correctly check and release the relevant pointers. Version 2 has fixed these issues. Thank you for your response.

Quoted 46 lines · David Hildenbrand
Quoted 25 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 pointer zone->per_cpu_pageset. It is always safe to judge the null pointer before use.

Fixes 9420f89db2dd ("mm: move most of core MM initialization to mm/mm_init.c")

---
V2:
Fixed the incorrect code logic.
Thanks David Hildenbrand for helpful suggestion.

---
  mm/page_alloc.c | 6 ++++++
  1 file changed, 6 insertions(+)
mm/page_alloc.c +1 -0
index 8afab64814dc..7c8a74fd02d6 100644
@@ -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_zonestats)

mm/page_alloc.c void __meminit setup_zone_pageset(struct zone *zone) · line 5716

Quoted 10 lines
mm/page_alloc.c +5 -0
+             return;
 
      zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages);
+     if (!zone->per_cpu_pageset) {
+             free_percpu(zone->per_cpu_zonestats);
+             return;
+     }
      for_each_possible_cpu(cpu) {
              struct per_cpu_pages *pcp;
              struct per_cpu_zonestat *pzstats;

Unmodified patch, likely not what you wanted to send?

--
Cheers,

David / dhildenb

#3 lore raw
Quoted 3 lines · Qiu-ji Chen

Hello, the previous patch did not correctly check and release the relevant pointers. Version 2 has fixed these issues. Thank you for your response.

I'm afraid I'm missing how my review feedback was addresses :/

Signature · 3 lines
Cheers,

David / dhildenb