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

llvm-svn: 348679
This commit is contained in:
Kamil Rytarowski 2018-12-08 01:50:18 +00:00
parent f9b8569d72
commit 2e11b93f35
5 changed files with 199 additions and 0 deletions

View File

@ -8151,6 +8151,83 @@ INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
#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;
}

View File

@ -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

View File

@ -2110,6 +2110,9 @@ const unsigned MD4_return_length = MD4_DIGEST_STRING_LENGTH;
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;

View File

@ -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) \

View File

@ -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;
}