3e5b893
From b725e22731dc8d212ea3b168c077bcf71a0e45f0 Mon Sep 17 00:00:00 2001
3e5b893
From: Frantisek Sumsal <frantisek@sumsal.cz>
3e5b893
Date: Sun, 3 Sep 2023 20:42:32 +0200
3e5b893
Subject: [PATCH] Fix possible integer overflow
3e5b893
3e5b893
Based on the original upstream commit [0] and adapted to the EPEL 7
3e5b893
version (0.42.0).
3e5b893
3e5b893
[0] https://github.com/libtom/libtommath/commit/7bbc1f8e4fe6dce75055957645117180768efb15
3e5b893
---
3e5b893
 bn_mp_2expt.c           | 4 ++++
3e5b893
 bn_mp_grow.c            | 4 ++++
3e5b893
 bn_mp_init_size.c       | 4 ++++
3e5b893
 bn_mp_mul_2d.c          | 4 ++++
3e5b893
 bn_s_mp_mul_digs.c      | 4 ++++
3e5b893
 bn_s_mp_mul_high_digs.c | 4 ++++
3e5b893
 6 files changed, 24 insertions(+)
3e5b893
3e5b893
diff --git a/bn_mp_2expt.c b/bn_mp_2expt.c
3e5b893
index 4774aab..e9df9ba 100755
3e5b893
--- a/bn_mp_2expt.c
3e5b893
+++ b/bn_mp_2expt.c
3e5b893
@@ -25,6 +25,10 @@ mp_2expt (mp_int * a, int b)
3e5b893
 {
3e5b893
   int     res;
3e5b893
 
3e5b893
+  if (b < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* zero a as per default */
3e5b893
   mp_zero (a);
3e5b893
 
3e5b893
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
3e5b893
index f1c1cab..1a75340 100755
3e5b893
--- a/bn_mp_grow.c
3e5b893
+++ b/bn_mp_grow.c
3e5b893
@@ -21,6 +21,10 @@ int mp_grow (mp_int * a, int size)
3e5b893
   int     i;
3e5b893
   mp_digit *tmp;
3e5b893
 
3e5b893
+  if (size < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* if the alloc size is smaller alloc more ram */
3e5b893
   if (a->alloc < size) {
3e5b893
     /* ensure there are always at least MP_PREC digits extra on top */
3e5b893
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
3e5b893
index 69dd49c..519f51d 100755
3e5b893
--- a/bn_mp_init_size.c
3e5b893
+++ b/bn_mp_init_size.c
3e5b893
@@ -20,6 +20,10 @@ int mp_init_size (mp_int * a, int size)
3e5b893
 {
3e5b893
   int x;
3e5b893
 
3e5b893
+  if (size < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* pad size so there are always extra digits */
3e5b893
   size += (MP_PREC * 2) - (size % MP_PREC);	
3e5b893
   
3e5b893
diff --git a/bn_mp_mul_2d.c b/bn_mp_mul_2d.c
3e5b893
index 385ac59..99c1945 100755
3e5b893
--- a/bn_mp_mul_2d.c
3e5b893
+++ b/bn_mp_mul_2d.c
3e5b893
@@ -21,6 +21,10 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
3e5b893
   mp_digit d;
3e5b893
   int      res;
3e5b893
 
3e5b893
+  if (b < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* copy */
3e5b893
   if (a != c) {
3e5b893
      if ((res = mp_copy (a, c)) != MP_OKAY) {
3e5b893
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
3e5b893
index 86196bf..c328185 100755
3e5b893
--- a/bn_s_mp_mul_digs.c
3e5b893
+++ b/bn_s_mp_mul_digs.c
3e5b893
@@ -27,6 +27,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
3e5b893
   mp_word r;
3e5b893
   mp_digit tmpx, *tmpt, *tmpy;
3e5b893
 
3e5b893
+  if (digs < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* can we use the fast multiplier? */
3e5b893
   if (((digs) < MP_WARRAY) &&
3e5b893
       MIN (a->used, b->used) < 
3e5b893
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
3e5b893
index 019014e..8ffae4f 100755
3e5b893
--- a/bn_s_mp_mul_high_digs.c
3e5b893
+++ b/bn_s_mp_mul_high_digs.c
3e5b893
@@ -27,6 +27,10 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
3e5b893
   mp_word r;
3e5b893
   mp_digit tmpx, *tmpt, *tmpy;
3e5b893
 
3e5b893
+  if (digs < 0) {
3e5b893
+      return MP_VAL;
3e5b893
+  }
3e5b893
+
3e5b893
   /* can we use the fast multiplier? */
3e5b893
 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
3e5b893
   if (((a->used + b->used + 1) < MP_WARRAY)
3e5b893
-- 
3e5b893
2.41.0
3e5b893