]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
Mixed with srandomdev() BSD code
authorFrancis Dupont <fdupont@isc.org>
Wed, 5 Jan 2022 19:21:43 +0000 (20:21 +0100)
committerThomas Markwalder <tmark@isc.org>
Wed, 12 Jan 2022 13:53:55 +0000 (08:53 -0500)
client/dhclient.c

index e445a52d5d70d23e06b0c43b8cbeeab70bf7a2a9..a0ff656bb3c84d7cc08475837d73e694039e4ffb 100644 (file)
@@ -3,7 +3,7 @@
    DHCP Client. */
 
 /*
- * Copyright (c) 2004-2020 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 2004-2022 by Internet Systems Consortium, Inc. ("ISC")
  * Copyright (c) 1995-2003 by Internet Software Consortium
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
@@ -829,21 +829,36 @@ main(int argc, char **argv) {
                            ? DISCOVER_REQUESTED
                            : DISCOVER_RUNNING);
 
-       /* Make up a seed for the random number generator from current
-          time plus the sum of the last four bytes of each
-          interface's hardware address interpreted as an integer.
-          Not much entropy, but we're booting, so we're not likely to
-          find anything better. */
+       /* PLEASE PREFER the random device: not all systems use random
+        * process identifiers so the alternative can be predictable. */
        seed = 0;
-       for (ip = interfaces; ip; ip = ip->next) {
-               int junk;
-               memcpy(&junk,
-                      &ip->hw_address.hbuf[ip->hw_address.hlen -
-                                           sizeof seed], sizeof seed);
-               seed += junk;
+       size_t nrnd = 0;
+#ifdef ISC_PATH_RANDOMDEV
+       FILE *frnd = fopen(ISC_PATH_RANDOMDEV, "r");
+       if (frnd) {
+               nrnd = fread(&seed, sizeof(seed), 1, frnd);
+               fclose(frnd);
        }
-       srandom(seed + cur_time + (unsigned)getpid());
+#endif
+       /* Please leave the compiler to emit a warning about a constant
+        * condition in the if test. */
+       if (!nrnd) {
+               /* Make up a seed for the random number generator from current
+                  time plus the sum of the last four bytes of each
+                  interface's hardware address interpreted as an integer.
+                  Not much entropy, but we're booting, so we're not likely to
+                  find anything better. */
 
+               for (ip = interfaces; ip; ip = ip->next) {
+                       int junk;
+                       memcpy(&junk,
+                              &ip->hw_address.hbuf[ip->hw_address.hlen -
+                                                   sizeof seed], sizeof seed);
+                       seed += junk;
+               }
+               seed += cur_time + (unsigned)getpid();
+       }
+       srandom(seed);
 
        /*
         * Establish a default DUID.  We always do so for v6 and