Skip to content
Myr

Randomize hash seed to mitigate hash collision DoS attacks

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The hash function used a fixed seed, allowing attackers to craft colliding keys and cause O(n^2) object parsing performance. Initialize the seed from a random source at process startup to prevent the attack. This fixes CVE-2026-40164.

authored by itchyny · committed by itchyny · parent 6374ae0 · Browse files · 2 files+34 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Asaf Meizner <asafmeizner@gmail.com>

Diff stats

2 files+34 added−2 removed

  • other:+34 added−2 removed

2 files+34 added−2 removed

modified:src/jv.c+32 added-2 removed
38 unmodified lines
39#include <stdarg.h>40#include <limits.h>41#include <math.h>42#include <float.h>43#include <time.h>44#include <unistd.h>45#include <fcntl.h>46#include <pthread.h>4748#include "jv_alloc.h"49#include "jv.h"50#include "jv_unicode.h"
1158 unmodified lines
1209 return r;1210 }1211}12121209static const uint32_t HASH_SEED = 0x432A9843;1213static uint32_t hash_seed;1214static pthread_once_t hash_seed_once = PTHREAD_ONCE_INIT;12151216static void jvp_hash_seed_init(void) {1217 uint32_t seed;1218#if defined(HAVE_ARC4RANDOM)1219 seed = arc4random();1220#elif defined(HAVE_GETENTROPY)1221 if (getentropy(&seed, sizeof(seed)) != 0)1222 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1223#else1224 int fd = open("/dev/urandom", O_RDONLY);1225 if (fd >= 0) {1226 if (read(fd, &seed, sizeof(seed)) != 4)1227 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1228 close(fd);1229 } else {1230 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1231 }1232#endif1233 hash_seed = seed;1234}12351236static uint32_t jvp_hash_seed(void) {1237 pthread_once(&hash_seed_once, jvp_hash_seed_init);1238 return hash_seed;1239}12401241static uint32_t rotl32 (uint32_t x, int8_t r){1242 return (x << r) | (x >> (32 - r));1243}1244
9 unmodified lines
1254 const uint8_t* data = (const uint8_t*)str->data;1255 int len = (int)jvp_string_length(str);1256 const int nblocks = len / 4;12571228 uint32_t h1 = HASH_SEED;1258 uint32_t h1 = jvp_hash_seed();12591260 const uint32_t c1 = 0xcc9e2d51;1261 const uint32_t c2 = 0x1b873593;1262 const uint32_t* blocks = (const uint32_t *)(data + nblocks*4);
849 unmodified lines
modified:configure.ac+2 added
147 unmodified lines
148 [], [[#include <time.h>]])149AC_CHECK_MEMBER([struct tm.__tm_gmtoff], [AC_DEFINE([HAVE_TM___TM_GMT_OFF],1,[Define to 1 if the system has the __tm_gmt_off field in struct tm])],150 [], [[#include <time.h>]])151AC_FIND_FUNC([setlocale], [c], [#include <locale.h>], [0,0])152AC_FIND_FUNC([arc4random], [c], [#include <stdlib.h>], [])153AC_FIND_FUNC([getentropy], [c], [#include <unistd.h>], [0, 0])154155dnl Figure out if we have the pthread functions we actually need156AC_FIND_FUNC_NO_LIBS([pthread_key_create], [], [#include <pthread.h>], [NULL, NULL])157AC_FIND_FUNC_NO_LIBS([pthread_once], [], [#include <pthread.h>], [NULL, NULL])
148 unmodified lines