lkml.org 
[lkml]   [2017]   [Oct]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 1/1] mpi: check for shift exponent greater than 31.
Date
This patch check for shift exponent greater than 31,
detected by UBSAN.

1)UBSAN: Undefined behaviour in lib/mpi/generic_mpih-lshift.c:57:22
shift exponent 32 is too large for 32-bit type 'long unsigned int'

2)UBSAN: Undefined behaviour in lib/mpi/generic_mpih-lshift.c:60:20
shift exponent 32 is too large for 32-bit type 'long unsigned int'

3)UBSAN: Undefined behaviour in lib/mpi/generic_mpih-rshift.c:57:21
shift exponent 32 is too large for 32-bit type 'long unsigned int'

4) UBSAN: Undefined behaviour in lib/mpi/generic_mpih-rshift.c:60:19
shift exponent 32 is too large for 32-bit type 'long unsigned int'

So instead of undefined behaviour defining behaviour when shift exponent
is greater than 31 then value will be Zero .

Signed-off-by: Ayush Mittal <ayush.m@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
---
lib/mpi/generic_mpih-lshift.c | 6 +++---
lib/mpi/generic_mpih-rshift.c | 6 +++---
lib/mpi/mpi-internal.h | 3 +++
3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/lib/mpi/generic_mpih-lshift.c b/lib/mpi/generic_mpih-lshift.c
index 8631892..1c8277e 100644
--- a/lib/mpi/generic_mpih-lshift.c
+++ b/lib/mpi/generic_mpih-lshift.c
@@ -50,14 +50,14 @@
sh_2 = BITS_PER_MPI_LIMB - sh_1;
i = usize - 1;
low_limb = up[i];
- retval = low_limb >> sh_2;
+ retval = MPI_RSHIFT(low_limb, sh_2);
high_limb = low_limb;
while (--i >= 0) {
low_limb = up[i];
- wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
+ wp[i] = MPI_LSHIFT(high_limb, sh_1) | MPI_RSHIFT(low_limb, sh_2);
high_limb = low_limb;
}
- wp[i] = high_limb << sh_1;
+ wp[i] = MPI_LSHIFT(high_limb, sh_1);

return retval;
}
diff --git a/lib/mpi/generic_mpih-rshift.c b/lib/mpi/generic_mpih-rshift.c
index ffa3288..3a28bdd 100644
--- a/lib/mpi/generic_mpih-rshift.c
+++ b/lib/mpi/generic_mpih-rshift.c
@@ -50,14 +50,14 @@
wp -= 1;
sh_2 = BITS_PER_MPI_LIMB - sh_1;
high_limb = up[0];
- retval = high_limb << sh_2;
+ retval = MPI_LSHIFT(high_limb, sh_2);
low_limb = high_limb;
for (i = 1; i < usize; i++) {
high_limb = up[i];
- wp[i] = (low_limb >> sh_1) | (high_limb << sh_2);
+ wp[i] = MPI_RSHIFT(low_limb, sh_1) | MPI_LSHIFT(high_limb, sh_2);
low_limb = high_limb;
}
- wp[i] = low_limb >> sh_1;
+ wp[i] = MPI_RSHIFT(low_limb, sh_1);

return retval;
}
diff --git a/lib/mpi/mpi-internal.h b/lib/mpi/mpi-internal.h
index 7eceedd..9c94a98 100644
--- a/lib/mpi/mpi-internal.h
+++ b/lib/mpi/mpi-internal.h
@@ -37,6 +37,9 @@
#include <linux/mpi.h>
#include <linux/errno.h>

+#define MPI_LSHIFT(val, shift) (shift >= BITS_PER_MPI_LIMB ? 0 : (val << shift))
+#define MPI_RSHIFT(val, shift) (shift >= BITS_PER_MPI_LIMB ? 0 : (val >> shift))
+
#define log_debug printk
#define log_bug printk

--
1.9.1
\
 
 \ /
  Last update: 2017-10-12 13:08    [W:0.033 / U:0.064 seconds]
©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site