[compiler-rt] r350123 - [Sanitizer] Intercept arc4random_buf / arc4random_addrandom on FreeBSD/NetBSD

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 28 08:04:28 PST 2018


Author: devnexen
Date: Fri Dec 28 08:04:28 2018
New Revision: 350123

URL: http://llvm.org/viewvc/llvm-project?rev=350123&view=rev
Log:
[Sanitizer] Intercept arc4random_buf / arc4random_addrandom on FreeBSD/NetBSD


- Disabled on purpose on Android and Darwin platform (for now).
- Darwin supports it, would need interception in its specific code before enabling it.
- Linux does not support it but only via third party library.
- Android supports it via bionic however it is known to have issue with older versions of the implementations. Can be enabled by an Android committer later on if necessary once there is more 'certainity'/been more tested.

Reviewers: krytarowski, vitalybuka

Reviewed By: krytarowski

Differential Revision: https://reviews.llvm.org/D56125

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/arc4random.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=350123&r1=350122&r2=350123&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Fri Dec 28 08:04:28 2018
@@ -9084,6 +9084,31 @@ INTERCEPTOR(void *, getfsfile, const cha
 #else
 #define INIT_GETFSENT
 #endif
+
+#if SANITIZER_INTERCEPT_ARC4RANDOM
+INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
+  REAL(arc4random_buf)(buf, len);
+  if (buf && len)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
+}
+
+INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
+  if (dat && datlen)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
+  REAL(arc4random_addrandom)(dat, datlen);
+}
+
+#define INIT_ARC4RANDOM \
+  COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
+  COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
+#else
+#define INIT_ARC4RANDOM
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map =
@@ -9365,6 +9390,7 @@ static void InitializeCommonInterceptors
   INIT_VIS;
   INIT_CDB;
   INIT_GETFSENT;
+  INIT_ARC4RANDOM;
 
   INIT___PRINTF_CHK;
 }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=350123&r1=350122&r2=350123&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Fri Dec 28 08:04:28 2018
@@ -546,5 +546,6 @@
 #define SANITIZER_INTERCEPT_CDB SI_NETBSD
 #define SANITIZER_INTERCEPT_VIS (SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_GETFSENT (SI_FREEBSD || SI_NETBSD)
+#define SANITIZER_INTERCEPT_ARC4RANDOM (SI_FREEBSD || SI_NETBSD)
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/arc4random.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/arc4random.cc?rev=350123&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/arc4random.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/arc4random.cc Fri Dec 28 08:04:28 2018
@@ -0,0 +1,62 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+//
+// UNSUPPORTED: linux, darwin, solaris
+
+#include <cstdlib>
+#include <ctime>
+#include <cstdio>
+#include <inttypes.h>
+
+void print_buf(unsigned char *buf, size_t buflen) {
+  printf("buf '");
+  for (auto i = 0; i < buflen; i ++)
+    printf("%" PRIx8, buf[i]);
+  printf("'\n");
+}
+
+void test_seed() {
+  time_t now = ::time(nullptr);
+  arc4random_addrandom((unsigned char *)&now, sizeof(now));
+}
+
+void test_arc4random() {
+  printf("test_arc4random\n");
+  auto i = arc4random();
+  print_buf((unsigned char *)&i, sizeof(i));
+}
+
+void test_arc4random_uniform() {
+  printf("test_arc4random_uniform\n");
+  auto i = arc4random_uniform(1024);
+  print_buf((unsigned char *)&i, sizeof(i));
+}
+
+void test_arc4random_buf10() {
+  printf("test_arc4random_buf10\n");
+  char buf[10];
+  arc4random_stir();
+  arc4random_buf(buf, sizeof(buf));
+  print_buf((unsigned char *)buf, sizeof(buf));
+}
+
+void test_arc4random_buf256() {
+  printf("test_arc4random_buf256\n");
+  char buf[256];
+  arc4random_stir();
+  arc4random_buf(buf, sizeof(buf));
+  print_buf((unsigned char *)buf, sizeof(buf));
+}
+
+int main(void) {
+  test_seed();
+  test_arc4random();
+  test_arc4random_buf10();
+  test_arc4random_buf256();
+  return 0;
+  // CHECK: test_arc4random
+  // CHECK: buf '{{.*}}'
+  // CHECK: test_arc4random_buf10
+  // CHECK: buf '{{.*}}'
+  // CHECK: test_arc4random_buf256
+  // CHECK: buf '{{.*}}'
+}




More information about the llvm-commits mailing list