]> granicus.if.org Git - pdns/commitdiff
Handle short reads from our random device
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 29 May 2019 14:00:35 +0000 (16:00 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 29 May 2019 14:40:45 +0000 (16:40 +0200)
Reported by Coverity (CID 1401626).

pdns/dns_random.cc

index bb6e8e615dd900664ecb844cbaf457369ec00869..e2b8e45754973300f34c90b887a8dab6b2af0d1f 100644 (file)
@@ -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<size_t>(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);