Skip to content

Commit 8ed545f

Browse files
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 1e2ec8a) Co-authored-by: Rolf Eike Beer <[email protected]>
1 parent 6e9456e commit 8ed545f

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix unaligned accesses in siphash24(). Patch by Rolf Eike Beer.

Python/pyhash.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * SIZEOF_PY_HASH_T,
366366
static uint64_t
367367
siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
368368
uint64_t b = (uint64_t)src_sz << 56;
369-
const uint64_t *in = (uint64_t*)src;
369+
const uint8_t *in = (uint8_t*)src;
370370

371371
uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
372372
uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
@@ -375,28 +375,28 @@ siphash24(uint64_t k0, uint64_t k1, const void *src, Py_ssize_t src_sz) {
375375

376376
uint64_t t;
377377
uint8_t *pt;
378-
uint8_t *m;
379378

380379
while (src_sz >= 8) {
381-
uint64_t mi = _le64toh(*in);
382-
in += 1;
383-
src_sz -= 8;
380+
uint64_t mi;
381+
memcpy(&mi, in, sizeof(mi));
382+
mi = _le64toh(mi);
383+
in += sizeof(mi);
384+
src_sz -= sizeof(mi);
384385
v3 ^= mi;
385386
DOUBLE_ROUND(v0,v1,v2,v3);
386387
v0 ^= mi;
387388
}
388389

389390
t = 0;
390391
pt = (uint8_t *)&t;
391-
m = (uint8_t *)in;
392392
switch (src_sz) {
393-
case 7: pt[6] = m[6]; /* fall through */
394-
case 6: pt[5] = m[5]; /* fall through */
395-
case 5: pt[4] = m[4]; /* fall through */
396-
case 4: memcpy(pt, m, sizeof(uint32_t)); break;
397-
case 3: pt[2] = m[2]; /* fall through */
398-
case 2: pt[1] = m[1]; /* fall through */
399-
case 1: pt[0] = m[0]; /* fall through */
393+
case 7: pt[6] = in[6]; /* fall through */
394+
case 6: pt[5] = in[5]; /* fall through */
395+
case 5: pt[4] = in[4]; /* fall through */
396+
case 4: memcpy(pt, in, sizeof(uint32_t)); break;
397+
case 3: pt[2] = in[2]; /* fall through */
398+
case 2: pt[1] = in[1]; /* fall through */
399+
case 1: pt[0] = in[0]; /* fall through */
400400
}
401401
b |= _le64toh(t);
402402

0 commit comments

Comments
 (0)