Add interceptors for the md4(3) from NetBSD

Summary:
MD4Init, MD4Update, MD4Final, MD4End, MD4File, MD4Data - calculates the
RSA Data Security, Inc., "MD4" message digest.

Add dedicated test.

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

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

Tags: #sanitizers

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

llvm-svn: 348677
This commit is contained in:
Kamil Rytarowski 2018-12-08 01:43:39 +00:00
parent 3f47a6fbd7
commit e9bf12f82f
5 changed files with 199 additions and 0 deletions

View File

@ -7981,6 +7981,83 @@ INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
#define INIT_SHA1
#endif
#if SANITIZER_INTERCEPT_MD4
INTERCEPTOR(void, MD4Init, void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
REAL(MD4Init)(context);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
}
INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
unsigned int len) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
REAL(MD4Update)(context, data, len);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
}
INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
REAL(MD4Final)(digest, context);
if (digest)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
}
INTERCEPTOR(char *, MD4End, void *context, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
char *ret = REAL(MD4End)(context, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
return ret;
}
INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
if (filename)
COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
char *ret = REAL(MD4File)(filename, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
return ret;
}
INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
char *ret = REAL(MD4Data)(data, len, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
return ret;
}
#define INIT_MD4 \
COMMON_INTERCEPT_FUNCTION(MD4Init); \
COMMON_INTERCEPT_FUNCTION(MD4Update); \
COMMON_INTERCEPT_FUNCTION(MD4Final); \
COMMON_INTERCEPT_FUNCTION(MD4End); \
COMMON_INTERCEPT_FUNCTION(MD4File); \
COMMON_INTERCEPT_FUNCTION(MD4Data)
#else
#define INIT_MD4
#endif
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map =
@ -8252,6 +8329,7 @@ static void InitializeCommonInterceptors() {
INIT_STRTOI;
INIT_CAPSICUM;
INIT_SHA1;
INIT_MD4;
INIT___PRINTF_CHK;
}

View File

@ -536,5 +536,6 @@
#define SANITIZER_INTERCEPT_STRTOI SI_NETBSD
#define SANITIZER_INTERCEPT_CAPSICUM SI_FREEBSD
#define SANITIZER_INTERCEPT_SHA1 SI_NETBSD
#define SANITIZER_INTERCEPT_MD4 SI_NETBSD
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

View File

@ -2104,6 +2104,9 @@ const int modctl_exists = MODCTL_EXISTS;
const unsigned SHA1_CTX_sz = sizeof(SHA1_CTX);
const unsigned SHA1_return_length = SHA1_DIGEST_STRING_LENGTH;
const unsigned MD4_CTX_sz = sizeof(MD4_CTX);
const unsigned MD4_return_length = MD4_DIGEST_STRING_LENGTH;
} // namespace __sanitizer
using namespace __sanitizer;

View File

@ -2228,6 +2228,9 @@ extern const int si_SEGV_ACCERR;
extern const unsigned SHA1_CTX_sz;
extern const unsigned SHA1_return_length;
extern const unsigned MD4_CTX_sz;
extern const unsigned MD4_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 <md4.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test1() {
MD4_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
uint8_t digest[MD4_DIGEST_LENGTH];
MD4Init(&ctx);
MD4Update(&ctx, entropy, __arraycount(entropy));
MD4Final(digest, &ctx);
printf("test1: '");
for (size_t i = 0; i < __arraycount(digest); i++)
printf("%02x", digest[i]);
printf("'\n");
}
void test2() {
MD4_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
char digest[MD4_DIGEST_STRING_LENGTH];
MD4Init(&ctx);
MD4Update(&ctx, entropy, __arraycount(entropy));
char *p = MD4End(&ctx, digest);
assert(p == digest);
printf("test2: '%s'\n", digest);
}
void test3() {
MD4_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
MD4Init(&ctx);
MD4Update(&ctx, entropy, __arraycount(entropy));
char *p = MD4End(&ctx, NULL);
assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1);
printf("test3: '%s'\n", p);
free(p);
}
void test4() {
char digest[MD4_DIGEST_STRING_LENGTH];
char *p = MD4File("/etc/fstab", digest);
assert(p == digest);
printf("test4: '%s'\n", p);
}
void test5() {
char *p = MD4File("/etc/fstab", NULL);
assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1);
printf("test5: '%s'\n", p);
free(p);
}
void test6() {
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
char digest[MD4_DIGEST_STRING_LENGTH];
char *p = MD4Data(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 = MD4Data(entropy, __arraycount(entropy), NULL);
assert(strlen(p) == MD4_DIGEST_STRING_LENGTH - 1);
printf("test7: '%s'\n", p);
free(p);
}
int main(void) {
printf("MD4\n");
test1();
test2();
test3();
test4();
test5();
test6();
test7();
// CHECK: MD4
// CHECK: test1: 'bf78fda2ca35eb7a026bfcdd3d17283d'
// CHECK: test2: 'bf78fda2ca35eb7a026bfcdd3d17283d'
// CHECK: test3: 'bf78fda2ca35eb7a026bfcdd3d17283d'
// CHECK: test4: '85b3d78ce68be51f710272728fe606af'
// CHECK: test5: '85b3d78ce68be51f710272728fe606af'
// CHECK: test6: '{{.*}}'
// CHECK: test7: '{{.*}}'
return 0;
}