Skip to contentSkip to conversation

pstore/zone: avoid dereferencing zero sized ptr after init zones

PATCH Superseded Eugen Hristev 2 participants 2 messages Dec 27, 2024 – Jan 9, 2025 Pstore filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pstore/zone: avoid dereferencing zero sized ptr after init zones

In psz_init_zones, if the requested area has a total_size less than record_size, kcalloc will be called with c == 0 and will return ZERO_SIZE_PTR.
Further, this will lead to an oops.

With this patch, in this scenario, it will look like this :

[    6.865545] pstore_zone:     total size : 28672 Bytes
[    6.865547] pstore_zone:     kmsg size : 65536 Bytes
[    6.865549] pstore_zone:     pmsg size : 0 Bytes
[    6.865551] pstore_zone:     console size : 0 Bytes
[    6.865553] pstore_zone:     ftrace size : 0 Bytes
[    6.872095] pstore_zone: zone dmesg total_size too small
[    6.878234] pstore_zone: alloc zones failed
---
 fs/pstore/zone.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/pstore/zone.c +4 -0
index 694db616663f..50a765999b54 100644
@@ -1217,6 +1217,10 @@ static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
 		pr_err("allocate for zones %s failed\n", name);
 		return ERR_PTR(-ENOMEM);
 	}
+	if (unlikely(zones == ZERO_SIZE_PTR)) {
+		pr_err("zone %s total_size too small\n", name);
+		return ERR_PTR(-EINVAL);
+	}
 
 	for (i = 0; i < c; i++) {
 		zone = psz_init_zone(type, off, record_size);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 24 lines · Eugen Hristev

In psz_init_zones, if the requested area has a total_size less than record_size, kcalloc will be called with c == 0 and will return ZERO_SIZE_PTR.
Further, this will lead to an oops.

With this patch, in this scenario, it will look like this :

[    6.865545] pstore_zone:     total size : 28672 Bytes
[    6.865547] pstore_zone:     kmsg size : 65536 Bytes
[    6.865549] pstore_zone:     pmsg size : 0 Bytes
[    6.865551] pstore_zone:     console size : 0 Bytes
[    6.865553] pstore_zone:     ftrace size : 0 Bytes
[    6.872095] pstore_zone: zone dmesg total_size too small
[    6.878234] pstore_zone: alloc zones failed
---
 fs/pstore/zone.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/pstore/zone.c +0 -0
index 694db616663f..50a765999b54 100644

fs/pstore/zone.c static struct pstore_zone **psz_init_zones(enum pstore_type_id type, · line 1223

Quoted 7 lines
fs/pstore/zone.c +4 -0
@@ -1217,6 +1217,10 @@ static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
 		pr_err("allocate for zones %s failed\n", name);
 		return ERR_PTR(-ENOMEM);
 	}
+	if (unlikely(zones == ZERO_SIZE_PTR)) {
+		pr_err("zone %s total_size too small\n", name);
+		return ERR_PTR(-EINVAL);
+	}

I'd rather catch this after the calculation of "c" rather than after a zero-sized allocation. Can you rework the patch to test it then?

-Kees

Quoted 6 lines
fs/pstore/zone.c +0 -0
 	for (i = 0; i < c; i++) {
 		zone = psz_init_zone(type, off, record_size);
Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook