Disable fork detection test on qemu.

Its madvise() is still broken today (just in a different way from the
past - it still returns success on invalid advice flags, which means
that we cannot distinguish pre- and post-MADV_WIPEONFORK implementation
versions of qemu.

I also thought of another way - namely, checking MADV_WIPEONFORK on a
MAP_SHARED mapping - which qemu correctly refuses, but FreeBSD's
linux.ko does not. So given it's inaccurate anyway, let's keep the
current detection and behaviour as is, namely, that we always draw
entropy when needing random data when on qemu.

Change-Id: Ib28994d8d22a7360dcfdcc8a6cc5e66f6a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/95107
Reviewed-by: Xiangfei Ding <xfding@google.com>
Commit-Queue: Rudolf Polzer <rpolzer@google.com>
Commit-Queue: Xiangfei Ding <xfding@google.com>
Auto-Submit: Rudolf Polzer <rpolzer@google.com>
diff --git a/crypto/rand/fork_detect_test.cc b/crypto/rand/fork_detect_test.cc
index 15f3fc0..b219f89 100644
--- a/crypto/rand/fork_detect_test.cc
+++ b/crypto/rand/fork_detect_test.cc
@@ -39,6 +39,7 @@
 
 #if defined(OPENSSL_LINUX)
 #include <stdlib.h>
+#include <sys/mman.h>
 #include <sys/utsname.h>
 #endif
 
@@ -165,6 +166,27 @@
         }
       }
     }
+
+    // Qemu claims support for any unsupported MADV_ flags, so the production
+    // code currently always disables reliance on MADV_WIPEONFORK when on qemu.
+    // Detect this situation explicitly here too so the test does not fail.
+    long page_size = sysconf(_SC_PAGESIZE);
+    ASSERT_GT(page_size, 0) << "System does not provide its page size. Expect "
+                               "reduced performance, but no security impact.";
+    void *addr =
+        mmap(nullptr, static_cast<size_t>(page_size), PROT_READ | PROT_WRITE,
+             MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    ASSERT_NE(addr, nullptr)
+        << "mmap() cannot produce a private anonymous mapping. Expect reduced "
+           "performance, but no security impact.";
+    int madvise_broken = madvise(addr, page_size, -1) == 0;
+    munmap(addr, page_size);
+    if (madvise_broken) {
+      GTEST_SKIP()
+          << "System claims support for madvise() with invalid flags (typical "
+             "of qemu), which means return values are unreliable. Expect even "
+             "more reduced performance at runtime.\n";
+    }
   }
 #endif
   EXPECT_NE(start, uint64_t{0})