From 34144f5ec013eab51262847e3b3f3a528e71cf24 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 17 Dec 2018 20:57:06 +0000 Subject: [PATCH] [Sanitizer] capsicum variadic api subset Reviewers: markj, vitalybuka Reviewed By: markj Differential Revision: https://reviews.llvm.org/D55714 llvm-svn: 349392 --- .../sanitizer_common_interceptors.inc | 72 +++++++++++++++++++ .../TestCases/FreeBSD/capsicum.cc | 6 ++ 2 files changed, 78 insertions(+) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 36d5f9c9f72f..09fd17ef613e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7844,6 +7844,74 @@ INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base, #endif #if SANITIZER_INTERCEPT_CAPSICUM +#define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \ + { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \ + if (rights) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \ + __sanitizer_cap_rights_t *ret = \ + REAL(cap_rights_init)(rights, ##__VA_ARGS__); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \ + return ret; \ + } + +#define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \ + { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \ + if (rights) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \ + __sanitizer_cap_rights_t *ret = \ + REAL(cap_rights_set)(rights, ##__VA_ARGS__); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \ + return ret; \ + } + +#define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \ + { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \ + if (rights) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \ + __sanitizer_cap_rights_t *ret = \ + REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \ + if (ret) \ + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \ + return ret; \ + } + +#define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \ + { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \ + if (rights) \ + COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \ + return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \ + } + +INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init, + __sanitizer_cap_rights_t *rights) { + CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights); +} + +INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set, + __sanitizer_cap_rights_t *rights) { + CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights); +} + +INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear, + __sanitizer_cap_rights_t *rights) { + CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights); +} + +INTERCEPTOR(bool, cap_rights_is_set, + __sanitizer_cap_rights_t *rights) { + CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights); +} + INTERCEPTOR(int, cap_rights_limit, int fd, const __sanitizer_cap_rights_t *rights) { void *ctx; @@ -7932,6 +8000,10 @@ INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) { return ret; } #define INIT_CAPSICUM \ + COMMON_INTERCEPT_FUNCTION(cap_rights_init); \ + COMMON_INTERCEPT_FUNCTION(cap_rights_set); \ + COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \ + COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \ COMMON_INTERCEPT_FUNCTION(cap_rights_get); \ COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \ COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \ diff --git a/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/capsicum.cc b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/capsicum.cc index d17f27b38b97..1bfb6f8d945c 100644 --- a/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/capsicum.cc +++ b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/capsicum.cc @@ -49,6 +49,12 @@ void test_cap_rights() { rv = cap_rights_get(STDIN_FILENO, &grights); assert(rv == 0); assert(memcmp(&grights, &rights, sizeof(grights)) == 0); + cap_rights_t *iptr = cap_rights_set(&rights, CAP_IOCTL); + assert(iptr); + cap_rights_t *eptr = cap_rights_clear(&rights, CAP_READ); + assert(eptr); + hasit = cap_rights_is_set(&rights, CAP_IOCTL); + assert(hasit == true); printf("rights test: %d\n", rv); }