andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone

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

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