1From 7ce3259f67ac2cd93acb0ec0080c24b3b69e66c6 Mon Sep 17 00:00:00 2001
2From: Darren Kenny <darren.kenny@oracle.com>
3Date: Wed, 4 Nov 2020 15:10:51 +0000
4Subject: [PATCH] video/fb/fbfill: Fix potential integer overflow
5
6The multiplication of 2 unsigned 32-bit integers may overflow before
7promotion to unsigned 64-bit. We should ensure that the multiplication
8is done with overflow detection. Additionally, use grub_sub() for
9subtraction.
10
11Fixes: CID 73640, CID 73697, CID 73702, CID 73823
12
13Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
14Signed-off-by: Marco A Benatto <mbenatto@redhat.com>
15Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
16Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
17---
18 grub-core/video/fb/fbfill.c | 17 +++++++++++++----
19 1 file changed, 13 insertions(+), 4 deletions(-)
20
21diff --git a/grub-core/video/fb/fbfill.c b/grub-core/video/fb/fbfill.c
22index 11816d0..a37acd1 100644
23--- a/grub-core/video/fb/fbfill.c
24+++ b/grub-core/video/fb/fbfill.c
25@@ -31,6 +31,7 @@
26 #include <grub/fbfill.h>
27 #include <grub/fbutil.h>
28 #include <grub/types.h>
29+#include <grub/safemath.h>
30 #include <grub/video.h>
31
32 /* Generic filler that works for every supported mode.  */
33@@ -61,7 +62,9 @@ grub_video_fbfill_direct32 (struct grub_video_fbblit_info *dst,
34
35   /* Calculate the number of bytes to advance from the end of one line
36      to the beginning of the next line.  */
37-  rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
38+  if (grub_mul (dst->mode_info->bytes_per_pixel, width, &rowskip) ||
39+      grub_sub (dst->mode_info->pitch, rowskip, &rowskip))
40+    return;
41
42   /* Get the start address.  */
43   dstptr = grub_video_fb_get_video_ptr (dst, x, y);
44@@ -98,7 +101,9 @@ grub_video_fbfill_direct24 (struct grub_video_fbblit_info *dst,
45 #endif
46   /* Calculate the number of bytes to advance from the end of one line
47      to the beginning of the next line.  */
48-  rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
49+  if (grub_mul (dst->mode_info->bytes_per_pixel, width, &rowskip) ||
50+      grub_sub (dst->mode_info->pitch, rowskip, &rowskip))
51+    return;
52
53   /* Get the start address.  */
54   dstptr = grub_video_fb_get_video_ptr (dst, x, y);
55@@ -131,7 +136,9 @@ grub_video_fbfill_direct16 (struct grub_video_fbblit_info *dst,
56
57   /* Calculate the number of bytes to advance from the end of one line
58      to the beginning of the next line.  */
59-  rowskip = (dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width);
60+  if (grub_mul (dst->mode_info->bytes_per_pixel, width, &rowskip) ||
61+      grub_sub (dst->mode_info->pitch, rowskip, &rowskip))
62+    return;
63
64   /* Get the start address.  */
65   dstptr = grub_video_fb_get_video_ptr (dst, x, y);
66@@ -161,7 +168,9 @@ grub_video_fbfill_direct8 (struct grub_video_fbblit_info *dst,
67
68   /* Calculate the number of bytes to advance from the end of one line
69      to the beginning of the next line.  */
70-  rowskip = dst->mode_info->pitch - dst->mode_info->bytes_per_pixel * width;
71+  if (grub_mul (dst->mode_info->bytes_per_pixel, width, &rowskip) ||
72+      grub_sub (dst->mode_info->pitch, rowskip, &rowskip))
73+    return;
74
75   /* Get the start address.  */
76   dstptr = grub_video_fb_get_video_ptr (dst, x, y);
77--
782.14.2
79
80