Add interceptors for md2(3) from NetBSD

Summary:
MD2Init, MD2Update, MD2Final, MD2End, MD2File, MD2Data - calculates the
RSA Data Security, Inc., "MD2" 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/D55469

llvm-svn: 348744
This commit is contained in:
Kamil Rytarowski 2018-12-10 09:01:00 +00:00
parent 14d36e6e75
commit e7971f1bce
3 changed files with 193 additions and 0 deletions

View File

@ -8283,6 +8283,83 @@ INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
#define INIT_FSEEK
#endif
#if SANITIZER_INTERCEPT_MD2
INTERCEPTOR(void, MD2Init, void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
REAL(MD2Init)(context);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
}
INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
unsigned int len) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
REAL(MD2Update)(context, data, len);
if (context)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
}
INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
REAL(MD2Final)(digest, context);
if (digest)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
}
INTERCEPTOR(char *, MD2End, void *context, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
if (context)
COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
char *ret = REAL(MD2End)(context, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
return ret;
}
INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
if (filename)
COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, REAL(strlen)(filename) + 1);
char *ret = REAL(MD2File)(filename, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
return ret;
}
INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
if (data && len > 0)
COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
char *ret = REAL(MD2Data)(data, len, buf);
if (ret)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
return ret;
}
#define INIT_MD2 \
COMMON_INTERCEPT_FUNCTION(MD2Init); \
COMMON_INTERCEPT_FUNCTION(MD2Update); \
COMMON_INTERCEPT_FUNCTION(MD2Final); \
COMMON_INTERCEPT_FUNCTION(MD2End); \
COMMON_INTERCEPT_FUNCTION(MD2File); \
COMMON_INTERCEPT_FUNCTION(MD2Data)
#else
#define INIT_MD2
#endif
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map =
@ -8558,6 +8635,7 @@ static void InitializeCommonInterceptors() {
INIT_RMD160;
INIT_MD5;
INIT_FSEEK;
INIT_MD2;
INIT___PRINTF_CHK;
}

View File

@ -540,5 +540,6 @@
#define SANITIZER_INTERCEPT_RMD160 SI_NETBSD
#define SANITIZER_INTERCEPT_MD5 SI_NETBSD
#define SANITIZER_INTERCEPT_FSEEK SI_NETBSD
#define SANITIZER_INTERCEPT_MD2 SI_NETBSD
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

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 <md2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test1() {
MD2_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
uint8_t digest[MD2_DIGEST_LENGTH];
MD2Init(&ctx);
MD2Update(&ctx, entropy, __arraycount(entropy));
MD2Final(digest, &ctx);
printf("test1: '");
for (size_t i = 0; i < __arraycount(digest); i++)
printf("%02x", digest[i]);
printf("'\n");
}
void test2() {
MD2_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
char digest[MD2_DIGEST_STRING_LENGTH];
MD2Init(&ctx);
MD2Update(&ctx, entropy, __arraycount(entropy));
char *p = MD2End(&ctx, digest);
assert(p == digest);
printf("test2: '%s'\n", digest);
}
void test3() {
MD2_CTX ctx;
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
MD2Init(&ctx);
MD2Update(&ctx, entropy, __arraycount(entropy));
char *p = MD2End(&ctx, NULL);
assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);
printf("test3: '%s'\n", p);
free(p);
}
void test4() {
char digest[MD2_DIGEST_STRING_LENGTH];
char *p = MD2File("/etc/fstab", digest);
assert(p == digest);
printf("test4: '%s'\n", p);
}
void test5() {
char *p = MD2File("/etc/fstab", NULL);
assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);
printf("test5: '%s'\n", p);
free(p);
}
void test6() {
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
char digest[MD2_DIGEST_STRING_LENGTH];
char *p = MD2Data(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 = MD2Data(entropy, __arraycount(entropy), NULL);
assert(strlen(p) == MD2_DIGEST_STRING_LENGTH - 1);
printf("test7: '%s'\n", p);
free(p);
}
int main(void) {
printf("MD2\n");
test1();
test2();
test3();
test4();
test5();
test6();
test7();
// CHECK: MD2
// CHECK: test1: 'e303e49b34f981c2740cdf809200d51b'
// CHECK: test2: 'e303e49b34f981c2740cdf809200d51b'
// CHECK: test3: 'e303e49b34f981c2740cdf809200d51b'
// CHECK: test4: 'a409cff8627afa00f3b563cf5f09af05'
// CHECK: test5: 'a409cff8627afa00f3b563cf5f09af05'
// CHECK: test6: '{{.*}}'
// CHECK: test7: '{{.*}}'
return 0;
}