1From e8814c811132a70f9b55418f7567378a34ad3883 Mon Sep 17 00:00:00 2001
2From: Darren Kenny <darren.kenny@oracle.com>
3Date: Tue, 3 Nov 2020 16:43:37 +0000
4Subject: [PATCH] libgcrypt/mpi: Fix possible unintended sign extension
5
6The array of unsigned char gets promoted to a signed 32-bit int before
7it is finally promoted to a size_t. There is the possibility that this
8may result in the signed-bit being set for the intermediate signed
932-bit int. We should ensure that the promotion is to the correct type
10before we bitwise-OR the values.
11
12Fixes: CID 96697
13
14Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
15Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
16Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
17---
18 grub-core/lib/libgcrypt-grub/mpi/mpicoder.c | 2 +-
19 grub-core/lib/libgcrypt/mpi/mpicoder.c      | 2 +-
20 2 files changed, 2 insertions(+), 2 deletions(-)
21
22diff --git a/grub-core/lib/libgcrypt-grub/mpi/mpicoder.c b/grub-core/lib/libgcrypt-grub/mpi/mpicoder.c
23index 3d55dfc..faf1cd6 100644
24--- a/grub-core/lib/libgcrypt-grub/mpi/mpicoder.c
25+++ b/grub-core/lib/libgcrypt-grub/mpi/mpicoder.c
26@@ -460,7 +460,7 @@ gcry_mpi_scan (struct gcry_mpi **ret_mpi, enum gcry_mpi_format format,
27       if (len && len < 4)
28         return gcry_error (GPG_ERR_TOO_SHORT);
29
30-      n = (s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
31+      n = ((size_t)s[0] << 24 | (size_t)s[1] << 16 | (size_t)s[2] << 8 | (size_t)s[3]);
32       s += 4;
33       if (len)
34         len -= 4;
35diff --git a/grub-core/lib/libgcrypt/mpi/mpicoder.c b/grub-core/lib/libgcrypt/mpi/mpicoder.c
36index a3435ed..7ecad27 100644
37--- a/grub-core/lib/libgcrypt/mpi/mpicoder.c
38+++ b/grub-core/lib/libgcrypt/mpi/mpicoder.c
39@@ -458,7 +458,7 @@ gcry_mpi_scan (struct gcry_mpi **ret_mpi, enum gcry_mpi_format format,
40       if (len && len < 4)
41         return gcry_error (GPG_ERR_TOO_SHORT);
42
43-      n = (s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
44+      n = ((size_t)s[0] << 24 | (size_t)s[1] << 16 | (size_t)s[2] << 8 | (size_t)s[3]);
45       s += 4;
46       if (len)
47         len -= 4;
48--
492.14.2
50
51