Hi Everyone,

Analysis under UBsan revealed undefined behavior, even when 
CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS was in effect. There were two 
problems. First, assumeAligned was not always correct. Then, IsAligned<T> 
returns a doctored value of 1 when CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS  
was defined.

The patch below clears the UB. The commented code shown was the original 
code path. It was modified for testing. It showed up as a change due to a 
local commit.

Any comments or objections?

**********

$ cat misc.h.diff 
diff --git a/misc.h b/misc.h
index 20bc6d4..3c49516 100644
--- a/misc.h
+++ b/misc.h
@@ -1248,16 +1248,14 @@ inline void UnalignedPutWordNonTemplate(ByteOrder 
order, byte *block, word64 val
 template <class T>
 inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
 {
-// #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
-//    if (!assumeAligned)
-//        return UnalignedGetWordNonTemplate(order, block, (T*)NULL);
-//    assert(IsAligned<T>(block));
-// #endif
-//    return ConditionalByteReverse(order, *reinterpret_cast<const T 
*>(block));
-
+    CRYPTOPP_UNUSED(assumeAligned);
+#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
+    return ConditionalByteReverse(order, *reinterpret_cast<const T 
*>(block));
+#else
     T temp;
-    memmove(&temp, block, sizeof(temp));
+    memcpy(&temp, block, sizeof(temp));
     return ConditionalByteReverse(order, temp);
+#endif
 }
 
 template <class T>
@@ -1269,18 +1267,15 @@ inline void GetWord(bool assumeAligned, ByteOrder 
order, T &result, const byte *
 template <class T>
 inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T 
value, const byte *xorBlock = NULL)
 {
-// #ifndef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
-//    if (!assumeAligned)
-//        return UnalignedPutWordNonTemplate(order, block, value, 
xorBlock);
-//    assert(IsAligned<T>(block));
-//    assert(IsAligned<T>(xorBlock));
-//#endif
-//    *reinterpret_cast<T *>(block) = ConditionalByteReverse(order, value) 
^ (xorBlock ? *reinterpret_cast<const T *>(xorBlock) : 0);
-
+    CRYPTOPP_UNUSED(assumeAligned);
+#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
+    *reinterpret_cast<T *>(block) = ConditionalByteReverse(order, value) ^ 
(xorBlock ? *reinterpret_cast<const T *>(xorBlock) : 0);
+#else
     T t1, t2 = 0;
     t1 = ConditionalByteReverse(order, value);
-    if(xorBlock) memmove(&t2, xorBlock, sizeof(T));
-    memmove(block, &(t1 ^= t2), sizeof(T));
+    if(xorBlock) memcpy(&t2, xorBlock, sizeof(T));
+    memcpy(block, &(t1 ^= t2), sizeof(T));
+#endif
 }
 
 template <class T, class B, bool A=false>

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to