From: Remi Gacogne Date: Wed, 29 May 2019 14:00:35 +0000 (+0200) Subject: Handle short reads from our random device X-Git-Tag: dnsdist-1.4.0-beta1~8^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=144c9852279ef0219c72fe549f72a40f3a255edc;p=pdns Handle short reads from our random device Reported by Coverity (CID 1401626). --- diff --git a/pdns/dns_random.cc b/pdns/dns_random.cc index bb6e8e615..e2b8e4575 100644 --- a/pdns/dns_random.cc +++ b/pdns/dns_random.cc @@ -281,11 +281,21 @@ uint32_t dns_random(uint32_t upper_bound) { #endif case RNG_URANDOM: { uint32_t num = 0; + size_t attempts = 5; do { - if (read(urandom_fd, &num, sizeof(num)) < 0) { + ssize_t got = read(urandom_fd, &num, sizeof(num)); + if (got < 0) { (void)close(urandom_fd); throw std::runtime_error("Cannot read random device"); } + else if (static_cast(got) != sizeof(num)) { + /* short read, let's retry */ + if (attempts == 0) { + throw std::runtime_error("Too many short reads on random device"); + } + attempts--; + continue; + } } while(num < min);