Blame SOURCES/0417-video-fb-video_fb-Fix-multiple-integer-overflows.patch

468bd4
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
468bd4
From: Darren Kenny <darren.kenny@oracle.com>
468bd4
Date: Wed, 4 Nov 2020 14:43:44 +0000
468bd4
Subject: [PATCH] video/fb/video_fb: Fix multiple integer overflows
468bd4
468bd4
The calculation of the unsigned 64-bit value is being generated by
468bd4
multiplying 2, signed or unsigned, 32-bit integers which may overflow
468bd4
before promotion to unsigned 64-bit. Fix all of them.
468bd4
468bd4
Fixes: CID 73703, CID 73767, CID 73833
468bd4
468bd4
Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
468bd4
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
468bd4
---
468bd4
 grub-core/video/fb/video_fb.c | 52 ++++++++++++++++++++++++++++++-------------
468bd4
 1 file changed, 36 insertions(+), 16 deletions(-)
468bd4
468bd4
diff --git a/grub-core/video/fb/video_fb.c b/grub-core/video/fb/video_fb.c
030dc3
index 1a602c8b251..1c9a138dcdc 100644
468bd4
--- a/grub-core/video/fb/video_fb.c
468bd4
+++ b/grub-core/video/fb/video_fb.c
468bd4
@@ -25,6 +25,7 @@
468bd4
 #include <grub/fbutil.h>
468bd4
 #include <grub/bitmap.h>
468bd4
 #include <grub/dl.h>
468bd4
+#include <grub/safemath.h>
468bd4
 
468bd4
 GRUB_MOD_LICENSE ("GPLv3+");
468bd4
 
468bd4
@@ -1417,15 +1418,23 @@ doublebuf_blit_update_screen (void)
468bd4
 {
468bd4
   if (framebuffer.current_dirty.first_line
468bd4
       <= framebuffer.current_dirty.last_line)
468bd4
-    grub_memcpy ((char *) framebuffer.pages[0]
468bd4
-		 + framebuffer.current_dirty.first_line
468bd4
-		 * framebuffer.back_target->mode_info.pitch,
468bd4
-		 (char *) framebuffer.back_target->data
468bd4
-		 + framebuffer.current_dirty.first_line
468bd4
-		 * framebuffer.back_target->mode_info.pitch,
468bd4
-		 framebuffer.back_target->mode_info.pitch
468bd4
-		 * (framebuffer.current_dirty.last_line
468bd4
-		    - framebuffer.current_dirty.first_line));
468bd4
+    {
468bd4
+      grub_size_t copy_size;
468bd4
+
468bd4
+      if (grub_sub (framebuffer.current_dirty.last_line,
468bd4
+		    framebuffer.current_dirty.first_line, &copy_size) ||
468bd4
+	  grub_mul (framebuffer.back_target->mode_info.pitch, copy_size, &copy_size))
468bd4
+	{
468bd4
+	  /* Shouldn't happen, but if it does we've a bug. */
468bd4
+	  return GRUB_ERR_BUG;
468bd4
+	}
468bd4
+
468bd4
+      grub_memcpy ((char *) framebuffer.pages[0] + framebuffer.current_dirty.first_line *
468bd4
+		   framebuffer.back_target->mode_info.pitch,
468bd4
+		   (char *) framebuffer.back_target->data + framebuffer.current_dirty.first_line *
468bd4
+		   framebuffer.back_target->mode_info.pitch,
468bd4
+		   copy_size);
468bd4
+    }
468bd4
   framebuffer.current_dirty.first_line
468bd4
     = framebuffer.back_target->mode_info.height;
468bd4
   framebuffer.current_dirty.last_line = 0;
468bd4
@@ -1439,7 +1448,7 @@ grub_video_fb_doublebuf_blit_init (struct grub_video_fbrender_target **back,
468bd4
 				   volatile void *framebuf)
468bd4
 {
468bd4
   grub_err_t err;
468bd4
-  grub_size_t page_size = mode_info.pitch * mode_info.height;
468bd4
+  grub_size_t page_size = (grub_size_t) mode_info.pitch * mode_info.height;
468bd4
 
468bd4
   framebuffer.offscreen_buffer = grub_zalloc (page_size);
468bd4
   if (! framebuffer.offscreen_buffer)
468bd4
@@ -1482,12 +1491,23 @@ doublebuf_pageflipping_update_screen (void)
468bd4
     last_line = framebuffer.previous_dirty.last_line;
468bd4
 
468bd4
   if (first_line <= last_line)
468bd4
-    grub_memcpy ((char *) framebuffer.pages[framebuffer.render_page]
468bd4
-		 + first_line * framebuffer.back_target->mode_info.pitch,
468bd4
-		 (char *) framebuffer.back_target->data
468bd4
-		 + first_line * framebuffer.back_target->mode_info.pitch,
468bd4
-		 framebuffer.back_target->mode_info.pitch
468bd4
-		 * (last_line - first_line));
468bd4
+    {
468bd4
+      grub_size_t copy_size;
468bd4
+
468bd4
+      if (grub_sub (last_line, first_line, &copy_size) ||
468bd4
+	  grub_mul (framebuffer.back_target->mode_info.pitch, copy_size, &copy_size))
468bd4
+	{
468bd4
+	  /* Shouldn't happen, but if it does we've a bug. */
468bd4
+	  return GRUB_ERR_BUG;
468bd4
+	}
468bd4
+
468bd4
+      grub_memcpy ((char *) framebuffer.pages[framebuffer.render_page] + first_line *
468bd4
+		   framebuffer.back_target->mode_info.pitch,
468bd4
+		   (char *) framebuffer.back_target->data + first_line *
468bd4
+		   framebuffer.back_target->mode_info.pitch,
468bd4
+		   copy_size);
468bd4
+    }
468bd4
+
468bd4
   framebuffer.previous_dirty = framebuffer.current_dirty;
468bd4
   framebuffer.current_dirty.first_line
468bd4
     = framebuffer.back_target->mode_info.height;