Skip to contentSkip to conversation

lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl

PATCH Superseded Sowmini Varadhan 2 participants 3 messages Apr 21, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl

Commit 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") added mpi_write_to_sgl() which generates traps due to unaligned access on some platforms like sparc. Fix this by using the get_unaligned* and put_unaligned* functions.

Fixes 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers")
---
 lib/mpi/mpicoder.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
lib/mpi/mpicoder.c +8 -4
index eb15e7d..6771378 100644
@@ -21,6 +21,7 @@
 #include <linux/bitops.h>
 #include <linux/count_zeros.h>
 #include "mpi-internal.h"
+#include <asm/unaligned.h>
 
 #define MAX_EXTERN_MPI_BITS 16384
 
@@ -405,10 +406,13 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
 				p -= sizeof(alimb);
 				continue;
 			} else {
-				mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
-				mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
-							+ lzeros;
-				*limb1 = *limb2;
+				mpi_limb_t tmp;
+
+				tmp = get_unaligned_be32((void *)p -
+							 sizeof(alimb) +
+							 lzeros);
+				put_unaligned_be32(tmp,
+						   (void *)p - sizeof(alimb));
 				p -= lzeros;
 				y = lzeros;
 			}
Signature · 1 line
1.7.1
#1 lore raw
Quoted 32 lines · Sowmini Varadhan

Commit 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") added mpi_write_to_sgl() which generates traps due to unaligned access on some platforms like sparc. Fix this by using the get_unaligned* and put_unaligned* functions.

Fixes 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers")
---
 lib/mpi/mpicoder.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
lib/mpi/mpicoder.c +1 -4
index eb15e7d..6771378 100644
@@ -21,6 +21,7 @@
 #include <linux/bitops.h>
 #include <linux/count_zeros.h>
 #include "mpi-internal.h"
+#include <asm/unaligned.h>
 
 #define MAX_EXTERN_MPI_BITS 16384
 
@@ -405,10 +406,13 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
 				p -= sizeof(alimb);
 				continue;
 			} else {
-				mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
-				mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
-							+ lzeros;
-				*limb1 = *limb2;

lib/mpi/mpicoder.c int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, · line 418

Quoted 10 lines
lib/mpi/mpicoder.c +7 -0
+				mpi_limb_t tmp;
+
+				tmp = get_unaligned_be32((void *)p -
+							 sizeof(alimb) +
+							 lzeros);
+				put_unaligned_be32(tmp,
+						   (void *)p - sizeof(alimb));
 				p -= lzeros;
 				y = lzeros;
 			}

What if the mpi_limb_t will happen to be 64 bit?
Thanks,

Signature · 1 line
TS
#2 lore raw
Quoted 3 lines · Tadeusz Struk

What if the mpi_limb_t will happen to be 64 bit?
Thanks,

When I checked this with cscope, I found

typedef unsigned long int mpi_limb_t;

thus I used the *32 functions.

But you obviously know better, since you wrote this code (and bug). If you anticipate that mpi_limb_t in some environment today, I can check for sizeof(mpi_limb_t), and predicate it to use the *32 or *64 functions based on the result. Do you think that is necessary?

Regards,
--Sowmini