Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -7252,6 +7252,125 @@ #define INIT_NETENT #endif +#if SANITIZER_INTERCEPT_SYSCTL +INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp, + SIZE_T *oldlenp, void *newp, SIZE_T newlen) { + void *ctx; + int res; + COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp, + newlen); + if (name) + COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name)); + if (newp) + COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen); + res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen); + if (!res) { + if (oldlenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp)); + if (oldp && oldlenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp); + } + return res; +} + +INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp, + void *newp, SIZE_T newlen) { + void *ctx; + int res; + COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp, + newlen); + if (sname) + COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1); + res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen); + if (!res) { + if (oldlenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp)); + if (oldp && oldlenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp); + } + return res; +} + +INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name, + unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode, + int v) { + void *ctx; + int res; + COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname, + csz, rnode, v); + if (sname) + COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1); + // Skip rnode, it's rarely used and not trivial to sanitize + // It's also used mostly internally + res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v); + if (!res) { + if (namelenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp)); + if (name && namelenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp); + } + if (cname && csz) { + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz)); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz); + } + return res; +} + +INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name, + SIZE_T *namelenp) { + void *ctx; + int res; + COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp); + if (sname) + COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1); + res = REAL(sysctlnametomib)(sname, name, namelenp); + if (!res) { + if (namelenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp)); + if (name && namelenp) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp); + } + return res; +} + +INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) { + void *ctx; + void *res; + COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len); + if (name) + COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen); + res = REAL(asysctl)(name, namelen, len); + if (len) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len)); + if (res && len) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len); + return res; +} + +INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) { + void *ctx; + void *res; + COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len); + if (sname) + COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, REAL(strlen)(sname) + 1); + res = REAL(asysctlbyname)(sname, len); + if (len) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len)); + if (res && len) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len); + return res; +} +#define INIT_SYSCTL \ + COMMON_INTERCEPT_FUNCTION(sysctl); \ + COMMON_INTERCEPT_FUNCTION(sysctlbyname); \ + COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo); \ + COMMON_INTERCEPT_FUNCTION(sysctlnametomib); \ + COMMON_INTERCEPT_FUNCTION(asysctl); \ + COMMON_INTERCEPT_FUNCTION(asysctlbyname); +#else +#define INIT_SYSCTL +#endif + static void InitializeCommonInterceptors() { static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1]; interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap(); @@ -7503,6 +7622,7 @@ INIT_TTYENT; INIT_PROTOENT; INIT_NETENT; + INIT_SYSCTL; INIT___PRINTF_CHK; } Index: lib/sanitizer_common/sanitizer_platform_interceptors.h =================================================================== --- lib/sanitizer_common/sanitizer_platform_interceptors.h +++ lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -514,5 +514,6 @@ #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD #define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD #define SANITIZER_INTERCEPT_NETENT SI_NETBSD +#define SANITIZER_INTERCEPT_SYSCTL SI_NETBSD #endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H Index: test/sanitizer_common/TestCases/NetBSD/sysctl.cc =================================================================== --- /dev/null +++ test/sanitizer_common/TestCases/NetBSD/sysctl.cc @@ -0,0 +1,109 @@ +// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s + +#include +#include + +#include + +#include +#include + +void test_sysctl() { + char buf[100]; + size_t len = sizeof(buf); + int mib[] = {CTL_KERN, KERN_OSTYPE}; + + if (sysctl(mib, __arraycount(mib), buf, &len, NULL, 0) == -1) + abort(); + + printf("OSTYPE: '%s'\n", buf); +} + +void test_sysctlbyname() { + char buf[100]; + size_t len = sizeof(buf); + int mib[] = {CTL_KERN, KERN_OSTYPE}; + + if (sysctlbyname("kern.ostype", buf, &len, NULL, 0) == -1) + abort(); + + printf("OSTYPE: '%s'\n", buf); +} + +void test_sysctlgetmibinfo() { + char buf[100]; + size_t len = sizeof(buf); + unsigned int mib_len; + int mib[CTL_MAXNAME]; + + if (sysctlgetmibinfo("kern.ostype", &mib[0], &mib_len, NULL, NULL, NULL, + SYSCTL_VERSION)) + abort(); + + if (sysctl(mib, mib_len, buf, &len, NULL, 0) == -1) + abort(); + + printf("OSTYPE: '%s'\n", buf); +} + +void test_sysctlnametomib() { + char buf[100]; + size_t len = sizeof(buf); + size_t mib_len; + int mib[CTL_MAXNAME]; + + mib_len = __arraycount(mib); + if (sysctlnametomib("kern.ostype", &mib[0], &mib_len) == -1) + abort(); + + if (sysctl(mib, mib_len, buf, &len, NULL, 0) == -1) + abort(); + + printf("OSTYPE: '%s'\n", buf); +} + +void test_asysctl() { + char *buf; + size_t len; + int mib[] = {CTL_KERN, KERN_OSTYPE}; + + if ((buf = (char *)asysctl(mib, __arraycount(mib), &len)) == NULL) + abort(); + + printf("OSTYPE: '%s'\n", buf); + + free(buf); +} + +void test_asysctlbyname() { + char *buf; + size_t len; + + if ((buf = (char *)asysctlbyname("kern.ostype", &len)) == NULL) + abort(); + + printf("OSTYPE: '%s'\n", buf); + + free(buf); +} + +int main(void) { + printf("sysctl\n"); + + test_sysctl(); + test_sysctlbyname(); + test_sysctlgetmibinfo(); + test_sysctlnametomib(); + test_asysctl(); + test_asysctlbyname(); + + // CHECK: sysctl + // CHECK: OSTYPE: '{{.*}}' + // CHECK: OSTYPE: '{{.*}}' + // CHECK: OSTYPE: '{{.*}}' + // CHECK: OSTYPE: '{{.*}}' + // CHECK: OSTYPE: '{{.*}}' + // CHECK: OSTYPE: '{{.*}}' + + return 0; +}