Skip to contentSkip to conversation

unix/hostdisk: Fix signed integer overflow

PATCH In review Lidong Chen via Grub-devel 1 participant 2 messages Jun 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unix/hostdisk: Fix signed integer overflow

The potential overflow issue arises at "size += ret;" because 'size' is of type ssize_t (signed) while 'len' is size_t (unsigned). Repeatedly adding read sizes ('ret') to 'size' can potentially exceed the maximum value of ssize_t, causing it to overflow into a negative or incorrect value. The fix is to ensure 'len' is within the range of GRUB_SSIZE_MAX.

Fixes CID 473850
Fixes CID 473863
---
 grub-core/osdep/unix/hostdisk.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/osdep/unix/hostdisk.c +6 -0
index 3a00d7451..613406770 100644
@@ -101,6 +101,9 @@ grub_util_fd_read (grub_util_fd_t fd, char *buf, size_t len)
 {
   ssize_t size = 0;
 
+  if (len > GRUB_SSIZE_MAX)
+    return -1;
+
   while (len)
     {
       ssize_t ret = read (fd, buf, len);
@@ -131,6 +134,9 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
 {
   ssize_t size = 0;
 
+  if (len > GRUB_SSIZE_MAX)
+    return -1;
+
   while (len)
     {
       ssize_t ret = write (fd, buf, len);
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author Reviewed-by Daniel Kiper on v1 1/1
#1 lore raw
Quoted 10 lines · Lidong Chen

The potential overflow issue arises at "size += ret;" because 'size' is of type ssize_t (signed) while 'len' is size_t (unsigned). Repeatedly adding read sizes ('ret') to 'size' can potentially exceed the maximum value of ssize_t, causing it to overflow into a negative or incorrect value. The fix is to ensure 'len' is within the range of GRUB_SSIZE_MAX.

Fixes CID 473850
Fixes CID 473863

Daniel

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