Skip to content

Commit 0d17e60

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 308dbe0 commit 0d17e60

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
@@ -369,7 +369,7 @@ siphash24(const void *src, Py_ssize_t src_sz) {
369369
uint64_t k0 = _le64toh(_Py_HashSecret.siphash.k0);
370370
uint64_t k1 = _le64toh(_Py_HashSecret.siphash.k1);
371371
uint64_t b = (uint64_t)src_sz << 56;
372-
const uint64_t *in = (uint64_t*)src;
372+
const uint8_t *in = (uint8_t*)src;
373373

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

379379
uint64_t t;
380380
uint8_t *pt;
381-
uint8_t *m;
382381

383382
while (src_sz >= 8) {
384-
uint64_t mi = _le64toh(*in);
385-
in += 1;
386-
src_sz -= 8;
383+
uint64_t mi;
384+
memcpy(&mi, in, sizeof(mi));
385+
mi = _le64toh(mi);
386+
in += sizeof(mi);
387+
src_sz -= sizeof(mi);
387388
v3 ^= mi;
388389
DOUBLE_ROUND(v0,v1,v2,v3);
389390
v0 ^= mi;
390391
}
391392

392393
t = 0;
393394
pt = (uint8_t *)&t;
394-
m = (uint8_t *)in;
395395
switch (src_sz) {
396-
case 7: pt[6] = m[6]; /* fall through */
397-
case 6: pt[5] = m[5]; /* fall through */
398-
case 5: pt[4] = m[4]; /* fall through */
399-
case 4: memcpy(pt, m, sizeof(uint32_t)); break;
400-
case 3: pt[2] = m[2]; /* fall through */
401-
case 2: pt[1] = m[1]; /* fall through */
402-
case 1: pt[0] = m[0]; /* fall through */
396+
case 7: pt[6] = in[6]; /* fall through */
397+
case 6: pt[5] = in[5]; /* fall through */
398+
case 5: pt[4] = in[4]; /* fall through */
399+
case 4: memcpy(pt, in, sizeof(uint32_t)); break;
400+
case 3: pt[2] = in[2]; /* fall through */
401+
case 2: pt[1] = in[1]; /* fall through */
402+
case 1: pt[0] = in[0]; /* fall through */
403403
}
404404
b |= _le64toh(t);
405405

0 commit comments

Comments
 (0)