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

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