Skip to content

Commit 1940d78

Browse files
authored
[compiler-rt][sanitizer] setproctitle interception for NetBSD/FreeBSD. (#131648)
1 parent a8588d8 commit 1940d78

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,22 @@ FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
18551855
#define INIT_ISOC99_PRINTF
18561856
#endif
18571857

1858+
#if SANITIZER_INTERCEPT_SETPROCTITLE
1859+
INTERCEPTOR(void, setproctitle, const char *fmt, ...) {
1860+
void *ctx;
1861+
va_list ap;
1862+
va_start(ap, fmt);
1863+
COMMON_INTERCEPTOR_ENTER(ctx, setproctitle, fmt, ap);
1864+
if (common_flags()->check_printf)
1865+
printf_common(ctx, fmt, ap);
1866+
REAL(setproctitle)(fmt, ap);
1867+
va_end(ap);
1868+
}
1869+
# define INIT_SETPROCTITLE COMMON_INTERCEPT_FUNCTION(setproctitle);
1870+
#else
1871+
# define INIT_SETPROCTITLE
1872+
#endif
1873+
18581874
#if SANITIZER_INTERCEPT_IOCTL
18591875
#include "sanitizer_common_interceptors_ioctl.inc"
18601876
#include "sanitizer_interceptors_ioctl_netbsd.inc"
@@ -10328,6 +10344,7 @@ static void InitializeCommonInterceptors() {
1032810344
INIT_PRINTF;
1032910345
INIT_PRINTF_L;
1033010346
INIT_ISOC99_PRINTF;
10347+
INIT_SETPROCTITLE;
1033110348
INIT_FREXP;
1033210349
INIT_FREXPF_FREXPL;
1033310350
INIT_GETPWNAM_AND_FRIENDS;

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
238238
#define SANITIZER_INTERCEPT_ISOC99_PRINTF SI_GLIBC
239239
#endif
240240

241+
#define SANITIZER_INTERCEPT_SETPROCTITLE (SI_FREEBSD || SI_NETBSD)
242+
241243
#define SANITIZER_INTERCEPT___PRINTF_CHK \
242244
(SANITIZER_INTERCEPT_PRINTF && SI_GLIBC)
243245

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
2+
// RUN: %env_asan_opts=check_printf=1 %run %t 2>&1 | FileCheck %s
3+
4+
// REQUIRES: freebsd || netbsd
5+
6+
#include <unistd.h>
7+
8+
int main() {
9+
const char fmt[2] = "%s%d\n";
10+
setproctitle("%s", "setproctitle");
11+
setproctitle("%c%c%c", 'c', "a", 'e');
12+
setproctitle(fmt, "abcdef", -5);
13+
return 0;
14+
}
15+
16+
// CHECK: ERROR: AddressSanitizer: stack-buffer-overflow
17+
// CHECK-NEXT: READ of size {{[0-9]+}} at {{.*}}
18+
// CHECK: #0 {{.*}} printf_common
19+
// CHECK: #1 {{.*}} setproctitle
20+
// CHECK: #2 {{.*}} main

0 commit comments

Comments
 (0)