From 5008c73e62e08284d2e49d0c696c61120385d967 Mon Sep 17 00:00:00 2001 From: William Brown Date: Mon, 30 Oct 2017 12:01:34 +1000 Subject: [PATCH] Ticket 49424 - Resolve csiphash alignment issues Bug Description: On some platforms, uint64_t is not the same size as a void * - as well, if the input is not aligned correctly, then a number of nasty crashes can result Fix Description: Instead of relying on alignment to be correct, we should memcpy the data to inputs instead. https://pagure.io/389-ds-base/issue/49424 Author: wibrown Review by: ??? --- src/libsds/external/csiphash/csiphash.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/libsds/external/csiphash/csiphash.c b/src/libsds/external/csiphash/csiphash.c index 0089c82..ee21be4 100644 --- a/src/libsds/external/csiphash/csiphash.c +++ b/src/libsds/external/csiphash/csiphash.c @@ -32,6 +32,9 @@ #include #include /* for size_t */ +#include /* calloc,free */ +#include /* memcpy */ + #include #if defined(HAVE_SYS_ENDIAN_H) @@ -75,11 +78,27 @@ uint64_t sds_siphash13(const void *src, size_t src_sz, const char key[16]) { - const uint64_t *_key = (uint64_t *)key; + uint64_t _key[2] = {0}; + memcpy(_key, key, 16); uint64_t k0 = _le64toh(_key[0]); uint64_t k1 = _le64toh(_key[1]); uint64_t b = (uint64_t)src_sz << 56; - const uint64_t *in = (uint64_t *)src; + + size_t input_sz = src_sz / sizeof(uint64_t); + /* Account for non-uint64_t alligned input */ + if (src_sz % sizeof(uint64_t) > 0) { + input_sz += 1; + } + + /* Could make this stack allocation */ + uint64_t *in = calloc(1, input_sz * sizeof(uint64_t)); + /* + * Because all crypto code sucks, they modify *in + * during operation, so we stash a copy of the ptr here. + * alternately, we could use stack allocated array, but gcc + * will complain about the vla being unbounded. + */ + uint64_t *in_ptr = memcpy(in, src, src_sz); uint64_t v0 = k0 ^ 0x736f6d6570736575ULL; uint64_t v1 = k1 ^ 0x646f72616e646f6dULL; @@ -126,5 +145,9 @@ sds_siphash13(const void *src, size_t src_sz, const char key[16]) v2 ^= 0xff; // dround dROUND(v0, v1, v2, v3); + + free(in_ptr); + return (v0 ^ v1) ^ (v2 ^ v3); } + -- 1.8.3.1