[compiler-rt] r348679 - Add interceptors for md5(3) from NetBSD

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 7 17:50:18 PST 2018


Author: kamil
Date: Fri Dec  7 17:50:18 2018
New Revision: 348679

URL: http://llvm.org/viewvc/llvm-project?rev=348679&view=rev
Log:
Add interceptors for md5(3) from NetBSD

Summary:
MD5Init, MD5Update, MD5Final, MD5End, MD5File, MD5Data - calculates the
RSA Data Security, Inc., "MD5" message digest.

Add a dedicated test.

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

Subscribers: kubamracek, llvm-commits, mgorny, #sanitizers

Tags: #sanitizers

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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/md5.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.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=348679&r1=348678&r2=348679&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  7 17:50:18 2018
@@ -8151,6 +8151,83 @@ INTERCEPTOR(char *, RMD160Data, u8 *data
 #define INIT_RMD160
 #endif
 
+#if SANITIZER_INTERCEPT_MD5
+INTERCEPTOR(void, MD5Init, void *context) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
+  REAL(MD5Init)(context);
+  if (context)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
+}
+
+INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
+            unsigned int len) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
+  if (data && len > 0)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
+  if (context)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
+  REAL(MD5Update)(context, data, len);
+  if (context)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
+}
+
+INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
+  if (context)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
+  REAL(MD5Final)(digest, context);
+  if (digest)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
+}
+
+INTERCEPTOR(char *, MD5End, void *context, char *buf) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
+  if (context)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
+  char *ret = REAL(MD5End)(context, buf);
+  if (ret)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
+  return ret;
+}
+
+INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
+  if (filename)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
+  char *ret = REAL(MD5File)(filename, buf);
+  if (ret)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
+  return ret;
+}
+
+INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
+            char *buf) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
+  if (data && len > 0)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
+  char *ret = REAL(MD5Data)(data, len, buf);
+  if (ret)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
+  return ret;
+}
+
+#define INIT_MD5                                                               \
+  COMMON_INTERCEPT_FUNCTION(MD5Init);                                          \
+  COMMON_INTERCEPT_FUNCTION(MD5Update);                                        \
+  COMMON_INTERCEPT_FUNCTION(MD5Final);                                         \
+  COMMON_INTERCEPT_FUNCTION(MD5End);                                           \
+  COMMON_INTERCEPT_FUNCTION(MD5File);                                          \
+  COMMON_INTERCEPT_FUNCTION(MD5Data)
+#else
+#define INIT_MD5
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map =
@@ -8424,6 +8501,7 @@ static void InitializeCommonInterceptors
   INIT_SHA1;
   INIT_MD4;
   INIT_RMD160;
+  INIT_MD5;
 
   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=348679&r1=348678&r2=348679&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  7 17:50:18 2018
@@ -538,5 +538,6 @@
 #define SANITIZER_INTERCEPT_SHA1 SI_NETBSD
 #define SANITIZER_INTERCEPT_MD4 SI_NETBSD
 #define SANITIZER_INTERCEPT_RMD160 SI_NETBSD
+#define SANITIZER_INTERCEPT_MD5 SI_NETBSD
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc?rev=348679&r1=348678&r2=348679&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc Fri Dec  7 17:50:18 2018
@@ -2110,6 +2110,9 @@ const unsigned MD4_return_length = MD4_D
 
 const unsigned RMD160_CTX_sz = sizeof(RMD160_CTX);
 const unsigned RMD160_return_length = RMD160_DIGEST_STRING_LENGTH;
+
+const unsigned MD5_CTX_sz = sizeof(MD5_CTX);
+const unsigned MD5_return_length = MD5_DIGEST_STRING_LENGTH;
 }  // namespace __sanitizer
 
 using namespace __sanitizer;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h?rev=348679&r1=348678&r2=348679&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h Fri Dec  7 17:50:18 2018
@@ -2234,6 +2234,9 @@ extern const unsigned MD4_return_length;
 
 extern const unsigned RMD160_CTX_sz;
 extern const unsigned RMD160_return_length;
+
+extern const unsigned MD5_CTX_sz;
+extern const unsigned MD5_return_length;
 }  // namespace __sanitizer
 
 #define CHECK_TYPE_SIZE(TYPE) \

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/md5.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/md5.cc?rev=348679&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/md5.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/md5.cc Fri Dec  7 17:50:18 2018
@@ -0,0 +1,114 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <sys/param.h>
+
+#include <assert.h>
+#include <endian.h>
+#include <md5.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void test1() {
+  MD5_CTX ctx;
+  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+  uint8_t digest[MD5_DIGEST_LENGTH];
+
+  MD5Init(&ctx);
+  MD5Update(&ctx, entropy, __arraycount(entropy));
+  MD5Final(digest, &ctx);
+
+  printf("test1: '");
+  for (size_t i = 0; i < __arraycount(digest); i++)
+    printf("%02x", digest[i]);
+  printf("'\n");
+}
+
+void test2() {
+  MD5_CTX ctx;
+  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+  char digest[MD5_DIGEST_STRING_LENGTH];
+
+  MD5Init(&ctx);
+  MD5Update(&ctx, entropy, __arraycount(entropy));
+  char *p = MD5End(&ctx, digest);
+  assert(p);
+
+  printf("test2: '%s'\n", digest);
+}
+
+void test3() {
+  MD5_CTX ctx;
+  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+
+  MD5Init(&ctx);
+  MD5Update(&ctx, entropy, __arraycount(entropy));
+  char *p = MD5End(&ctx, NULL);
+  assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1);
+
+  printf("test3: '%s'\n", p);
+
+  free(p);
+}
+
+void test4() {
+  char digest[MD5_DIGEST_STRING_LENGTH];
+
+  char *p = MD5File("/etc/fstab", digest);
+  assert(p == digest);
+
+  printf("test4: '%s'\n", p);
+}
+
+void test5() {
+  char *p = MD5File("/etc/fstab", NULL);
+  assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1);
+
+  printf("test5: '%s'\n", p);
+
+  free(p);
+}
+
+void test6() {
+  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+  char digest[MD5_DIGEST_STRING_LENGTH];
+
+  char *p = MD5Data(entropy, __arraycount(entropy), digest);
+  assert(p == digest);
+
+  printf("test6: '%s'\n", p);
+}
+
+void test7() {
+  uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+
+  char *p = MD5Data(entropy, __arraycount(entropy), NULL);
+  assert(strlen(p) == MD5_DIGEST_STRING_LENGTH - 1);
+
+  printf("test7: '%s'\n", p);
+
+  free(p);
+}
+
+int main(void) {
+  printf("MD5\n");
+
+  test1();
+  test2();
+  test3();
+  test4();
+  test5();
+  test6();
+  test7();
+
+  // CHECK: MD5
+  // CHECK: test1: '86e65b1ef4a830af347ac05ab4f0e999'
+  // CHECK: test2: '86e65b1ef4a830af347ac05ab4f0e999'
+  // CHECK: test3: '86e65b1ef4a830af347ac05ab4f0e999'
+  // CHECK: test4: 'd6798ca88175b5feece4dda691a5b9b5'
+  // CHECK: test5: 'd6798ca88175b5feece4dda691a5b9b5'
+  // CHECK: test6: '86e65b1ef4a830af347ac05ab4f0e999'
+  // CHECK: test7: '86e65b1ef4a830af347ac05ab4f0e999'
+
+  return 0;
+}




More information about the llvm-commits mailing list