summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2016-08-05 15:42:05 +0000
committertedu <tedu@openbsd.org>2016-08-05 15:42:05 +0000
commit1b2af9526a54b3ff0a13216366a9149f5a29b64e (patch)
tree20a011c173035f7542602fbde93138d5abd258f1
parentDon't leak the option data of non-DHCPINFORM messages received on (diff)
downloadwireguard-openbsd-1b2af9526a54b3ff0a13216366a9149f5a29b64e.tar.xz
wireguard-openbsd-1b2af9526a54b3ff0a13216366a9149f5a29b64e.zip
use a larger chunk for getentropy() and save some for next time.
coalesces some syscalls instead of one per random number. ok deraadt
-rw-r--r--libexec/ld.so/util.c30
1 files 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;
}