Skip to content

Commit

Permalink
Add new interceptors: strlcpy(3) and strlcat(3)
Browse files Browse the repository at this point in the history
Summary:
NetBSD ships with strlcpy(3) and strlcat(3), a safe
replacement of strcpy(3) and strcat(3).

Hide both functions under SANITIZER_INTERCEPT_STRLCPY.

Sponsored by <The NetBSD Foundation>

Reviewers: joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: llvm-commits, kubamracek, #sanitizers

Tags: #sanitizers

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

llvm-svn: 324034
  • Loading branch information
krytarowski committed Feb 1, 2018
1 parent c7ef565 commit e2f8718
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6724,6 +6724,41 @@ INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
#define INIT_OPEN_BY_HANDLE_AT
#endif

#if SANITIZER_INTERCEPT_STRLCPY
INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
void *ctx;
SIZE_T res;
COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
if (src) {
// Keep strnlen as macro argument, as macro may ignore it.
COMMON_INTERCEPTOR_READ_STRING(ctx, src,
Min(REAL(strnlen)(src, size), size - 1) + 1);
}
res = REAL(strlcpy)(dst, src, size);
COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
return res;
}

INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
// src is checked in the strlcpy() interceptor
if (dst) {
SIZE_T len = REAL(strnlen)(dst, size);
COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
// Reuse the rest of the code in the strlcpy() interceptor
dst += len;
size -= len;
}
return WRAP(strlcpy)(dst, src, size);
}
#define INIT_STRLCPY \
COMMON_INTERCEPT_FUNCTION(strlcpy); \
COMMON_INTERCEPT_FUNCTION(strlcat);
#else
#define INIT_STRLCPY
#endif

static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
Expand Down Expand Up @@ -6946,9 +6981,9 @@ static void InitializeCommonInterceptors() {
INIT_GETGROUPLIST;
INIT_READLINK;
INIT_READLINKAT;

INIT_NAME_TO_HANDLE_AT;
INIT_OPEN_BY_HANDLE_AT;
INIT_STRLCPY;

#if SANITIZER_NETBSD
COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
#define SANITIZER_INTERCEPT_ACCESS SI_NETBSD
#define SANITIZER_INTERCEPT_FACCESSAT SI_NETBSD
#define SANITIZER_INTERCEPT_GETGROUPLIST SI_NETBSD
#define SANITIZER_INTERCEPT_STRLCPY SI_NETBSD

#define SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT SI_LINUX_NOT_ANDROID
Expand Down
52 changes: 52 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/NetBSD/strlcat.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void test1() {
const char src[] = "abc";
char dst[7] = {'x', 'y', 'z', 0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test2() {
const char src[] = "abc";
char dst[7] = {0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test3() {
const char src[] = "abc";
char dst[4] = {'x', 'y', 'z', 0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test4() {
const char src[] = "";
char dst[4] = {'x', 'y', 'z', 0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu\n", dst, len);
}

int main(void) {
test1();
test2();
test3();
test4();

// CHECK: xyzabc 6 abc 3 xyz 3 xyz 3

return 0;
}
52 changes: 52 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void test1() {
const char src[] = "abc";
char dst[7] = {'x', 'y', 'z', 0};
size_t len;

len = strlcpy(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test2() {
const char src[] = "abc";
char dst[7] = {0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test3() {
const char src[] = "abc";
char dst[4] = {'x', 'y', 'z', 0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu ", dst, len);
}

void test4() {
const char src[] = "";
char dst[4] = {'x', 'y', 'z', 0};
size_t len;

len = strlcat(dst, src, sizeof(dst));
printf("%s %zu\n", dst, len);
}

int main(void) {
test1();
test2();
test3();
test4();

// CHECK: abc 3 abc 3 xyz 3 0

return 0;
}

0 comments on commit e2f8718

Please sign in to comment.