summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2000-04-04 13:38:24 +0000
committermillert <>2000-04-04 13:38:24 +0000
commitead26851c853278c861706f40bd67ea1e0e88bcb (patch)
tree4246def207e838bab4b2b798d8147c06b916ca92
parent807f25d100c3ec168a67f87fac561c57c185571e (diff)
downloadopenbsd-ead26851c853278c861706f40bd67ea1e0e88bcb.tar.gz
openbsd-ead26851c853278c861706f40bd67ea1e0e88bcb.tar.bz2
openbsd-ead26851c853278c861706f40bd67ea1e0e88bcb.zip
Fix an fd leak if the read from /dev/arandom fails. Pointed out by
Markus Friedl.
-rw-r--r--src/lib/libc/stdlib/random.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
index 7c6e5f7eb9..2b97c5a54a 100644
--- a/src/lib/libc/stdlib/random.c
+++ b/src/lib/libc/stdlib/random.c
@@ -32,7 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: random.c,v 1.7 2000/04/03 23:23:48 millert Exp $"; 35static char *rcsid = "$OpenBSD: random.c,v 1.8 2000/04/04 13:38:24 millert Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h> 38#include <sys/types.h>
@@ -245,10 +245,8 @@ srandomdev()
245 else 245 else
246 len = rand_deg * sizeof(state[0]); 246 len = rand_deg * sizeof(state[0]);
247 247
248 if ((fd = open("/dev/arandom", O_RDONLY, 0)) != -1 && 248 if ((fd = open("/dev/arandom", O_RDONLY, 0)) == -1 ||
249 read(fd, (void *) state, len) == (ssize_t) len) { 249 read(fd, (void *) state, len) != (ssize_t) len) {
250 close(fd);
251 } else {
252 struct timeval tv; 250 struct timeval tv;
253 u_int junk; 251 u_int junk;
254 252
@@ -257,6 +255,8 @@ srandomdev()
257 srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk); 255 srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk);
258 return; 256 return;
259 } 257 }
258 if (fd != -1)
259 close(fd);
260 260
261 if (rand_type != TYPE_0) { 261 if (rand_type != TYPE_0) {
262 fptr = &state[rand_sep]; 262 fptr = &state[rand_sep];