Skip to contentSkip to conversation

lib/relocator: Fix dereference after NULL check

PATCH In review Alec Brown via Grub-devel 3 participants 3 messages Nov 19, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/relocator: Fix dereference after NULL check

In the function free_subchunk(), after checking that subchu->post isn't NULL, grub_memset() is called on subchu->pre->freebytes but it should be called on subchu->post->freebytes. If subchu->pre is NULL but subchu->post isn't NULL, then this could lead to a NULL pointer dereference.

Fixes CID 473882
---
 grub-core/lib/relocator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/lib/relocator.c +3 -3
index 1e1e09704..37da0c6db 100644
@@ -398,9 +398,9 @@ free_subchunk (const struct grub_relocator_subchunk *subchu)
 	if (subchu->post)
 	  {
 	    int off = subchu->start + subchu->size - fend;
-	    grub_memset (subchu->pre->freebytes,
-			 0xff, sizeof (subchu->pre->freebytes) - off / 8);
-	    subchu->pre->freebytes[off / 8] |= ((1 << (8 - (off % 8))) - 1);
+	    grub_memset (subchu->post->freebytes,
+			 0xff, sizeof (subchu->post->freebytes) - off / 8 - 1);
+	    subchu->post->freebytes[sizeof (subchu->post->freebytes) - off / 8 - 1] |= ((1 << (8 - (off % 8))) - 1);
 	    check_leftover (subchu->post);
 	  }
 #endif
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Alec Brown via Grub-devel

In the function free_subchunk(), after checking that subchu->post isn't NULL, grub_memset() is called on subchu->pre->freebytes but it should be called on subchu->post->freebytes. If subchu->pre is NULL but subchu->post isn't NULL, then this could lead to a NULL pointer dereference.

Quoted 4 lines
Fixes CID 473882

Thanks,
Sudhakar

Quoted 29 lines

---
grub-core/lib/relocator.c | 6 +++---

1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/lib/relocator.c +3 -3
index 1e1e09704..37da0c6db 100644
@@ -398,9 +398,9 @@ free_subchunk (const struct grub_relocator_subchunk *subchu)
 if (subchu->post)
 {
   int off = subchu->start + subchu->size - fend;
-    grub_memset (subchu->pre->freebytes,
- 0xff, sizeof (subchu->pre->freebytes) - off / 8);
-    subchu->pre->freebytes[off / 8] |= ((1 << (8 - (off % 8))) - 1);
+    grub_memset (subchu->post->freebytes,
+ 0xff, sizeof (subchu->post->freebytes) - off / 8 - 1);
+    subchu->post->freebytes[sizeof (subchu->post->freebytes) - off / 8 - 1] |= ((1 << (8 - (off % 8))) - 1);
   check_leftover (subchu->post);
 }
 #endif
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by
#2 lore raw
Quoted 6 lines · Sudhakar Kuppusamy
Quoted 6 lines · Alec Brown via Grub-devel

In the function free_subchunk(), after checking that subchu->post isn't NULL, grub_memset() is called on subchu->pre->freebytes but it should be called on subchu->post->freebytes. If subchu->pre is NULL but subchu->post isn't NULL, then this could lead to a NULL pointer dereference.

Quoted 6 lines
Quoted 4 lines
Fixes CID 473882

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel