Skip to content

Commit

Permalink
[Sanitizers] intercept md5 and sha* apis on FreeBSD.
Browse files Browse the repository at this point in the history
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D110989
  • Loading branch information
devnexen committed Oct 6, 2021
1 parent 3129aa5 commit 18a7ebd
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,10 @@
#define SANITIZER_INTERCEPT_SHA1 SI_NETBSD
#define SANITIZER_INTERCEPT_MD4 SI_NETBSD
#define SANITIZER_INTERCEPT_RMD160 SI_NETBSD
#define SANITIZER_INTERCEPT_MD5 SI_NETBSD
#define SANITIZER_INTERCEPT_MD5 (SI_NETBSD || SI_FREEBSD)
#define SANITIZER_INTERCEPT_FSEEK (SI_NETBSD || SI_FREEBSD)
#define SANITIZER_INTERCEPT_MD2 SI_NETBSD
#define SANITIZER_INTERCEPT_SHA2 SI_NETBSD
#define SANITIZER_INTERCEPT_SHA2 (SI_NETBSD || SI_FREEBSD)
#define SANITIZER_INTERCEPT_CDB SI_NETBSD
#define SANITIZER_INTERCEPT_VIS (SI_NETBSD || SI_FREEBSD)
#define SANITIZER_INTERCEPT_POPEN SI_POSIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
#include <semaphore.h>
#include <signal.h>
#include <stddef.h>
#include <md5.h>
#include <sha224.h>

This comment has been minimized.

Copy link
@pinskia

pinskia Jan 6, 2024

This breaks building on pre-FreeBSD 12.0 due to unconditional include of sha224.h. Reported to GCC https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105347 .

This comment has been minimized.

Copy link
@devnexen

devnexen Jan 6, 2024

Author Member

but freebsd 12.x is eol no ?

This comment has been minimized.

Copy link
@devnexen

devnexen Jan 6, 2024

Author Member

more commits had been pushed since to drop support for old FreeBSD like here.

#include <sha256.h>
#include <sha384.h>
#include <sha512.h>
#include <stdio.h>
#include <stringlist.h>
#include <term.h>
Expand Down Expand Up @@ -361,6 +366,22 @@ const int si_SEGV_MAPERR = SEGV_MAPERR;
const int si_SEGV_ACCERR = SEGV_ACCERR;
const int unvis_valid = UNVIS_VALID;
const int unvis_validpush = UNVIS_VALIDPUSH;

const unsigned MD5_CTX_sz = sizeof(MD5_CTX);
const unsigned MD5_return_length = MD5_DIGEST_STRING_LENGTH;

#define SHA2_CONST(LEN) \
const unsigned SHA##LEN##_CTX_sz = sizeof(SHA##LEN##_CTX); \
const unsigned SHA##LEN##_return_length = SHA##LEN##_DIGEST_STRING_LENGTH; \
const unsigned SHA##LEN##_block_length = SHA##LEN##_BLOCK_LENGTH; \
const unsigned SHA##LEN##_digest_length = SHA##LEN##_DIGEST_LENGTH

SHA2_CONST(224);
SHA2_CONST(256);
SHA2_CONST(384);
SHA2_CONST(512);

#undef SHA2_CONST
} // namespace __sanitizer

using namespace __sanitizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,22 @@ extern unsigned IOCTL_KDSKBMODE;
extern const int si_SEGV_MAPERR;
extern const int si_SEGV_ACCERR;

extern const unsigned MD5_CTX_sz;
extern const unsigned MD5_return_length;

#define SHA2_EXTERN(LEN) \
extern const unsigned SHA##LEN##_CTX_sz; \
extern const unsigned SHA##LEN##_return_length; \
extern const unsigned SHA##LEN##_block_length; \
extern const unsigned SHA##LEN##_digest_length

SHA2_EXTERN(224);
SHA2_EXTERN(256);
SHA2_EXTERN(384);
SHA2_EXTERN(512);

#undef SHA2_EXTERN

struct __sanitizer_cap_rights {
u64 cr_rights[2];
};
Expand Down
119 changes: 119 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/FreeBSD/md5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// RUN: %clangxx -O0 -g %s -o %t -lmd && %run %t 2>&1 | FileCheck %s

#include <sys/param.h>

#include <assert.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];
size_t entropysz = sizeof(entropy);
size_t digestsz = sizeof(digest);

MD5Init(&ctx);
MD5Update(&ctx, entropy, entropysz);
MD5Final(digest, &ctx);

printf("test1: '");
for (size_t i = 0; i < digestsz; 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];
size_t entropysz = sizeof(entropy);

MD5Init(&ctx);
MD5Update(&ctx, entropy, entropysz);
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};
size_t entropysz = sizeof(entropy);

MD5Init(&ctx);
MD5Update(&ctx, entropy, entropysz);
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];
size_t entropysz = sizeof(entropy);

char *p = MD5Data(entropy, entropysz, digest);
assert(p == digest);

printf("test6: '%s'\n", p);
}

void test7() {
uint8_t entropy[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
size_t entropysz = sizeof(entropy);

char *p = MD5Data(entropy, entropysz, 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: '{{.*}}'
// CHECK: test5: '{{.*}}'
// CHECK: test6: '86e65b1ef4a830af347ac05ab4f0e999'
// CHECK: test7: '86e65b1ef4a830af347ac05ab4f0e999'

return 0;
}
Loading

0 comments on commit 18a7ebd

Please sign in to comment.