[Python-checkins] bpo-28055: Fix unaligned accesses in siphash24(). (GH-6123)

Miss Islington (bot) webhook-mailer at python.org
Sun May 13 07:17:11 EDT 2018


https://github.com/python/cpython/commit/8ed545f6de37efdadbcf71c45bb8136b8cb9619d
commit: 8ed545f6de37efdadbcf71c45bb8136b8cb9619d
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-05-13T04:17:07-07:00
summary:

bpo-28055: Fix unaligned accesses in siphash24(). (GH-6123)


The hash implementation casts the input pointer to uint64_t* and directly reads
from this, which may cause unaligned accesses. Use memcpy() instead so this code
will not crash with SIGBUS on sparc.

https://bugs.gentoo.org/show_bug.cgi?id=636400
(cherry picked from commit 1e2ec8a996daec65d8d5a3d43b66a9909c6d0653)

Co-authored-by: Rolf Eike Beer <eike at sf-mail.de>

files:
A Misc/NEWS.d/next/Core and Builtins/2018-04-25-20-44-42.bpo-28055.f49kfC.rst
M Python/pyhash.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-04-25-20-44-42.bpo-28055.f49kfC.rst b/Misc/NEWS.d/next/Core and Builtins/2018-04-25-20-44-42.bpo-28055.f49kfC.rst
new file mode 100644
index 000000000000..c7d849906fc9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-04-25-20-44-42.bpo-28055.f49kfC.rst	
@@ -0,0 +1 @@
+Fix unaligned accesses in siphash24(). Patch by Rolf Eike Beer.
diff --git a/Python/pyhash.c b/Python/pyhash.c
index 9e6e594f441a..6fc12fde3fc7 100644
--- a/Python/pyhash.c
+++ b/Python/pyhash.c
@@ -366,7 +366,7 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
 static uint64_t
 siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
     uint64_t b = (uint64_t)src_sz << 56;
-    const uint64_t *in = (uint64_t*)src;
+    const uint8_t *in = (uint8_t*)src;
 
     uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
     uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
@@ -375,12 +375,13 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
 
     uint64_t t;
     uint8_t *pt;
-    uint8_t *m;
 
     while (src_sz >= 8) {
-        uint64_t mi = _le64toh(*in);
-        in += 1;
-        src_sz -= 8;
+        uint64_t mi;
+        memcpy(&mi, in, sizeof(mi));
+        mi = _le64toh(mi);
+        in += sizeof(mi);
+        src_sz -= sizeof(mi);
         v3 ^= mi;
         DOUBLE_ROUND(v0,v1,v2,v3);
         v0 ^= mi;
@@ -388,15 +389,14 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
 
     t = 0;
     pt = (uint8_t *)&t;
-    m = (uint8_t *)in;
     switch (src_sz) {
-        case 7: pt[6] = m[6]; /* fall through */
-        case 6: pt[5] = m[5]; /* fall through */
-        case 5: pt[4] = m[4]; /* fall through */
-        case 4: memcpy(pt, m, sizeof(uint32_t)); break;
-        case 3: pt[2] = m[2]; /* fall through */
-        case 2: pt[1] = m[1]; /* fall through */
-        case 1: pt[0] = m[0]; /* fall through */
+        case 7: pt[6] = in[6]; /* fall through */
+        case 6: pt[5] = in[5]; /* fall through */
+        case 5: pt[4] = in[4]; /* fall through */
+        case 4: memcpy(pt, in, sizeof(uint32_t)); break;
+        case 3: pt[2] = in[2]; /* fall through */
+        case 2: pt[1] = in[1]; /* fall through */
+        case 1: pt[0] = in[0]; /* fall through */
     }
     b |= _le64toh(t);
 



More information about the Python-checkins mailing list