Skip to content
Myr

Prefer getentropy() over arc4random() to seed the string hash (#3629)

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

glibc's arc4random() aborts with "Fatal glibc error: cannot get entropy for arc4random" when the kernel lacks getrandom(2), so jq died on startup on older kernels once 0c7d133 began seeding the hash on every run. getentropy() merely returns -1/ENOSYS there, so prefer it, and route its failure through the existing `/dev/urandom` path - which works on precisely those kernels -- instead of a guessable `getpid() ^ time()`.

authored by Brandon · committed by GitHub · parent df24981 · Browse files · 1 file+12 added−7 removed
This commit

State

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

Diff stats

1 file+12 added−7 removed

  • other:+12 added−7 removed

1 file+12 added−7 removed

modified:src/jv.c+12 added-7 removed
1208 unmodified lines
1209static pthread_once_t hash_seed_once = PTHREAD_ONCE_INIT;12101211static void jvp_hash_seed_init(void) {1212 uint32_t seed;1213#if defined(HAVE_ARC4RANDOM)1214 seed = arc4random();1215#elif defined(HAVE_GETENTROPY)1216 if (getentropy(&seed, sizeof(seed)) != 0)1217 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1218#else1213 /* Prefer getentropy() over arc4random(): glibc's arc4random() aborts the1214 * process when the kernel lacks getrandom(2) (#3584), while getentropy()1215 * merely fails, letting us fall back to /dev/urandom. */1216#if defined(HAVE_GETENTROPY)1217 if (getentropy(&seed, sizeof(seed)) == 0) {1218 hash_seed = seed;1219 return;1220 }1221#elif defined(HAVE_ARC4RANDOM)1222 hash_seed = arc4random();1223 return;1224#endif1225 int fd = open("/dev/urandom", O_RDONLY);1226 if (fd >= 0) {1227 if (read(fd, &seed, sizeof(seed)) != 4)1228 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1229 close(fd);1230 } else {1231 seed = (uint32_t)getpid() ^ (uint32_t)time(NULL);1232 }1227#endif1233 hash_seed = seed;1234}12351236static uint32_t jvp_hash_seed(void) {
954 unmodified lines