From 1b2af9526a54b3ff0a13216366a9149f5a29b64e Mon Sep 17 00:00:00 2001 From: tedu Date: Fri, 5 Aug 2016 15:42:05 +0000 Subject: [PATCH] use a larger chunk for getentropy() and save some for next time. coalesces some syscalls instead of one per random number. ok deraadt --- libexec/ld.so/util.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/libexec/ld.so/util.c b/libexec/ld.so/util.c index ebf33e071b2..c81cb5350e2 100644 --- a/libexec/ld.so/util.c +++ b/libexec/ld.so/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.41 2016/03/21 22:41:28 bluhm Exp $ */ +/* $OpenBSD: util.c,v 1.42 2016/08/05 15:42:05 tedu Exp $ */ /* * Copyright (c) 1998 Per Fogelstrom, Opsycon AB @@ -74,16 +74,36 @@ _dl_strdup(const char *orig) void _dl_randombuf(void *v, size_t buflen) { + static char bytes[256]; + static u_int reserve; char *buf = v; size_t chunk; while (buflen != 0) { - if (buflen > 256) - chunk = 256; + if (reserve == 0) { + if (_dl_getentropy(bytes, sizeof(bytes)) != 0) + _dl_exit(8); + reserve = sizeof(bytes); + } + if (buflen > reserve) + chunk = reserve; else chunk = buflen; - if (_dl_getentropy(buf, chunk) != 0) - _dl_exit(8); +#if 0 + memcpy(buf, bytes + reserve - chunk, chunk); + memset(bytes + reserve - chunk, 0, chunk); +#else + { + char *d = buf; + char *s = bytes + reserve - chunk; + u_int l; + for (l = chunk; l > 0; l--, s++, d++) { + *d = *s; + *s = 0; + } + } +#endif + reserve -= chunk; buflen -= chunk; buf += chunk; } -- 2.20.1