[compiler-rt] 1940d78 - [compiler-rt][sanitizer] setproctitle interception for NetBSD/FreeBSD. (#131648)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 25 22:17:56 PDT 2025


Author: David CARLIER
Date: 2025-03-26T05:17:53Z
New Revision: 1940d7816a6025d606983af7493c762461287ee8

URL: https://github.com/llvm/llvm-project/commit/1940d7816a6025d606983af7493c762461287ee8
DIFF: https://github.com/llvm/llvm-project/commit/1940d7816a6025d606983af7493c762461287ee8.diff

LOG: [compiler-rt][sanitizer] setproctitle interception for NetBSD/FreeBSD. (#131648)

Added: 
    compiler-rt/test/asan/TestCases/Posix/setproctitle.c

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 24a8a2d4dc55b..761dbd3f5a679 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -1855,6 +1855,22 @@ FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
 #define INIT_ISOC99_PRINTF
 #endif
 
+#if SANITIZER_INTERCEPT_SETPROCTITLE
+INTERCEPTOR(void, setproctitle, const char *fmt, ...) {
+  void *ctx;
+  va_list ap;
+  va_start(ap, fmt);
+  COMMON_INTERCEPTOR_ENTER(ctx, setproctitle, fmt, ap);
+  if (common_flags()->check_printf)
+    printf_common(ctx, fmt, ap);
+  REAL(setproctitle)(fmt, ap);
+  va_end(ap);
+}
+#  define INIT_SETPROCTITLE COMMON_INTERCEPT_FUNCTION(setproctitle);
+#else
+#  define INIT_SETPROCTITLE
+#endif
+
 #if SANITIZER_INTERCEPT_IOCTL
 #include "sanitizer_common_interceptors_ioctl.inc"
 #include "sanitizer_interceptors_ioctl_netbsd.inc"
@@ -10328,6 +10344,7 @@ static void InitializeCommonInterceptors() {
   INIT_PRINTF;
   INIT_PRINTF_L;
   INIT_ISOC99_PRINTF;
+  INIT_SETPROCTITLE;
   INIT_FREXP;
   INIT_FREXPF_FREXPL;
   INIT_GETPWNAM_AND_FRIENDS;

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index e52f131e26b1a..468b5494d0092 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -238,6 +238,8 @@ SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
 #define SANITIZER_INTERCEPT_ISOC99_PRINTF SI_GLIBC
 #endif
 
+#define SANITIZER_INTERCEPT_SETPROCTITLE (SI_FREEBSD || SI_NETBSD)
+
 #define SANITIZER_INTERCEPT___PRINTF_CHK \
   (SANITIZER_INTERCEPT_PRINTF && SI_GLIBC)
 

diff  --git a/compiler-rt/test/asan/TestCases/Posix/setproctitle.c b/compiler-rt/test/asan/TestCases/Posix/setproctitle.c
new file mode 100644
index 0000000000000..adf9626cd3084
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Posix/setproctitle.c
@@ -0,0 +1,20 @@
+// RUN: %clang_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// RUN: %env_asan_opts=check_printf=1 %run %t 2>&1 | FileCheck %s
+
+// REQUIRES: freebsd || netbsd
+
+#include <unistd.h>
+
+int main() {
+  const char fmt[2] = "%s%d\n";
+  setproctitle("%s", "setproctitle");
+  setproctitle("%c%c%c", 'c', "a", 'e');
+  setproctitle(fmt, "abcdef", -5);
+  return 0;
+}
+
+// CHECK: ERROR: AddressSanitizer: stack-buffer-overflow
+// CHECK-NEXT: READ of size {{[0-9]+}} at {{.*}}
+// CHECK: #0 {{.*}} printf_common
+// CHECK: #1 {{.*}} setproctitle
+// CHECK: #2 {{.*}} main


        


More information about the llvm-commits mailing list