zrhoffman / rpms / 389-ds-base

Forked from rpms/389-ds-base 3 years ago
Clone

Blame SOURCES/0023-Ticket-49424-Resolve-csiphash-alignment-issues.patch

058656
From 5909e20899334816f36cac0e47105e56df52ad3c Mon Sep 17 00:00:00 2001
058656
From: William Brown <firstyear@redhat.com>
058656
Date: Mon, 30 Oct 2017 12:01:34 +1000
058656
Subject: [PATCH] Ticket 49424 - Resolve csiphash alignment issues
058656
058656
Bug Description:  On some platforms, uint64_t is not the same size
058656
as a void * - as well, if the input is not aligned correctly, then
058656
a number of nasty crashes can result
058656
058656
Fix Description:  Instead of relying on alignment to be correct,
058656
we should memcpy the data to inputs instead.
058656
058656
https://pagure.io/389-ds-base/issue/49424
058656
058656
Author: wibrown
058656
058656
Review by: lslebodn, cgrzemba, vashirov, mreynolds (Thanks!)
058656
058656
(cherry picked from commit 751446440f5269a246e6e652a64e63aa5933734a)
058656
---
058656
 src/libsds/external/csiphash/csiphash.c | 52 +++++++++++++++++++--------------
058656
 src/libsds/test/test_sds_csiphash.c     | 43 +++++++++++++++++++++------
058656
 2 files changed, 64 insertions(+), 31 deletions(-)
058656
058656
diff --git a/src/libsds/external/csiphash/csiphash.c b/src/libsds/external/csiphash/csiphash.c
058656
index 0089c82f7..2351db6cf 100644
058656
--- a/src/libsds/external/csiphash/csiphash.c
058656
+++ b/src/libsds/external/csiphash/csiphash.c
058656
@@ -32,6 +32,9 @@
058656
 #include <inttypes.h>
058656
 #include <stddef.h> /* for size_t */
058656
 
058656
+#include <stdlib.h> /* calloc,free */
058656
+#include <string.h> /* memcpy */
058656
+
058656
 #include <config.h>
058656
 
058656
 #if defined(HAVE_SYS_ENDIAN_H)
058656
@@ -75,11 +78,24 @@
058656
 uint64_t
058656
 sds_siphash13(const void *src, size_t src_sz, const char key[16])
058656
 {
058656
-    const uint64_t *_key = (uint64_t *)key;
058656
+    uint64_t _key[2] = {0};
058656
+    memcpy(_key, key, 16);
058656
     uint64_t k0 = _le64toh(_key[0]);
058656
     uint64_t k1 = _le64toh(_key[1]);
058656
     uint64_t b = (uint64_t)src_sz << 56;
058656
-    const uint64_t *in = (uint64_t *)src;
058656
+
058656
+    size_t input_sz = (src_sz / sizeof(uint64_t)) + 1;
058656
+
058656
+    /* Account for non-uint64_t alligned input */
058656
+    /* Could make this stack allocation */
058656
+    uint64_t *in = calloc(1, input_sz * sizeof(uint64_t));
058656
+    /*
058656
+     * Because all crypto code sucks, they modify *in
058656
+     * during operation, so we stash a copy of the ptr here.
058656
+     * alternately, we could use stack allocated array, but gcc
058656
+     * will complain about the vla being unbounded.
058656
+     */
058656
+    uint64_t *in_ptr = memcpy(in, src, src_sz);
058656
 
058656
     uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
058656
     uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
058656
@@ -96,27 +112,15 @@ sds_siphash13(const void *src, size_t src_sz, const char key[16])
058656
         v0 ^= mi;
058656
     }
058656
 
058656
+    /*
058656
+     * Because we allocate in as size + 1, we can over-read 0
058656
+     * for this buffer to be padded correctly. in here is a pointer to the
058656
+     * excess data because the while loop above increments the in pointer
058656
+     * to point to the excess once src_sz drops < 8.
058656
+     */
058656
     uint64_t t = 0;
058656
-    uint8_t *pt = (uint8_t *)&t;
058656
-    uint8_t *m = (uint8_t *)in;
058656
-
058656
-    switch (src_sz) {
058656
-    case 7:
058656
-        pt[6] = m[6]; /* FALLTHRU */
058656
-    case 6:
058656
-        pt[5] = m[5]; /* FALLTHRU */
058656
-    case 5:
058656
-        pt[4] = m[4]; /* FALLTHRU */
058656
-    case 4:
058656
-        *((uint32_t *)&pt[0]) = *((uint32_t *)&m[0]);
058656
-        break;
058656
-    case 3:
058656
-        pt[2] = m[2]; /* FALLTHRU */
058656
-    case 2:
058656
-        pt[1] = m[1]; /* FALLTHRU */
058656
-    case 1:
058656
-        pt[0] = m[0]; /* FALLTHRU */
058656
-    }
058656
+    memcpy(&t, in, sizeof(uint64_t));
058656
+
058656
     b |= _le64toh(t);
058656
 
058656
     v3 ^= b;
058656
@@ -126,5 +130,9 @@ sds_siphash13(const void *src, size_t src_sz, const char key[16])
058656
     v2 ^= 0xff;
058656
     // dround
058656
     dROUND(v0, v1, v2, v3);
058656
+
058656
+    free(in_ptr);
058656
+
058656
     return (v0 ^ v1) ^ (v2 ^ v3);
058656
 }
058656
+
058656
diff --git a/src/libsds/test/test_sds_csiphash.c b/src/libsds/test/test_sds_csiphash.c
058656
index cdb6b7f46..cc9a6b2b5 100644
058656
--- a/src/libsds/test/test_sds_csiphash.c
058656
+++ b/src/libsds/test/test_sds_csiphash.c
058656
@@ -25,23 +25,48 @@
058656
 static void
058656
 test_siphash(void **state __attribute__((unused)))
058656
 {
058656
-
058656
-    //
058656
     uint64_t value = 0;
058656
     uint64_t hashout = 0;
058656
     char key[16] = {0};
058656
 
058656
-    uint64_t test_a = 15794382300316794652U;
058656
-    uint64_t test_b = 13042610424265326907U;
058656
+    uint64_t test_simple = 15794382300316794652U;
058656
 
058656
-    // Initial simple test
058656
+    /* Initial simple test */
058656
     value = htole64(5);
058656
     hashout = sds_siphash13(&value, sizeof(uint64_t), key);
058656
-    assert_true(hashout == test_a);
058656
+    assert_int_equal(hashout, test_simple);
058656
+
058656
+    /* Test a range of input sizes to check endianness behaviour */
058656
+
058656
+    hashout = sds_siphash13("a", 1, key);
058656
+    assert_int_equal(hashout, 0x407448d2b89b1813U);
058656
+
058656
+    hashout = sds_siphash13("aa", 2, key);
058656
+    assert_int_equal(hashout, 0x7910e0436ed8d1deU);
058656
+
058656
+    hashout = sds_siphash13("aaa", 3, key);
058656
+    assert_int_equal(hashout, 0xf752893a6c769652U);
058656
+
058656
+    hashout = sds_siphash13("aaaa", 4, key);
058656
+    assert_int_equal(hashout, 0x8b02350718d87164U);
058656
+
058656
+    hashout = sds_siphash13("aaaaa", 5, key);
058656
+    assert_int_equal(hashout, 0x92a991474c7eef2U);
058656
+
058656
+    hashout = sds_siphash13("aaaaaa", 6, key);
058656
+    assert_int_equal(hashout, 0xf0ab815a640277ccU);
058656
+
058656
+    hashout = sds_siphash13("aaaaaaa", 7, key);
058656
+    assert_int_equal(hashout, 0x33f3c6d7dbc82c0dU);
058656
+
058656
+    hashout = sds_siphash13("aaaaaaaa", 8, key);
058656
+    assert_int_equal(hashout, 0xc501b12e18428c92U);
058656
+
058656
+    hashout = sds_siphash13("aaaaaaaabbbb", 12, key);
058656
+    assert_int_equal(hashout, 0xcddca673069ade64U);
058656
 
058656
-    char *test = "abc";
058656
-    hashout = sds_siphash13(test, 4, key);
058656
-    assert_true(hashout == test_b);
058656
+    hashout = sds_siphash13("aaaaaaaabbbbbbbb", 16, key);
058656
+    assert_int_equal(hashout, 0xdc54f0bfc0e1deb0U);
058656
 }
058656
 
058656
 int
058656
-- 
058656
2.13.6
058656