naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 75037db319f9eb240d73186eee457f62d4b16c7d Mon Sep 17 00:00:00 2001
594167
From: Frantisek Sumsal <frantisek@sumsal.cz>
594167
Date: Tue, 14 Jun 2022 22:54:39 +0200
594167
Subject: [PATCH] test: wrap binaries using systemd DSOs when running w/ ASan
594167
594167
Let's detect & wrap binaries which are linked against systemd DSOs and
594167
we're running under ASan, since otherwise running such binaries ends
594167
with:
594167
594167
```
594167
==633==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
594167
```
594167
594167
(cherry picked from commit 3917534d620c2b358a196431b9e2593218ba1ac9)
594167
594167
Related: #2087652
594167
---
594167
 test/test-functions | 40 +++++++++++++++++++++++++++++++++++++++-
594167
 1 file changed, 39 insertions(+), 1 deletion(-)
594167
594167
diff --git a/test/test-functions b/test/test-functions
594167
index a0ad8b2fb1..34aeac339f 100644
594167
--- a/test/test-functions
594167
+++ b/test/test-functions
594167
@@ -2426,6 +2426,9 @@ inst_binary() {
594167
 
594167
     local file line
594167
     local so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
594167
+    # DSOs provided by systemd
594167
+    local systemd_so_regex='/(libudev|libsystemd.*|.+[\-_]systemd([\-_].+)?|libnss_(mymachines|myhostname|resolve)).so'
594167
+    local wrap_binary=0
594167
     # I love bash!
594167
     while read -r line; do
594167
         [[ "$line" = 'not a dynamic executable' ]] && break
594167
@@ -2434,6 +2437,12 @@ inst_binary() {
594167
         # by ldd attempting to use the unprefixed RPATH.
594167
         [[ "$line" =~ libsystemd.*\ not\ found ]] && continue
594167
 
594167
+        # We're built with ASan and the target binary loads one of the systemd's
594167
+        # DSOs, so we need to tweak the environment before executing the binary
594167
+        if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$line" =~ $systemd_so_regex ]]; then
594167
+            wrap_binary=1
594167
+        fi
594167
+
594167
         if [[ "$line" =~ $so_regex ]]; then
594167
             file="${BASH_REMATCH[1]}"
594167
             [[ -e "${initdir}/$file" ]] && continue
594167
@@ -2449,7 +2458,36 @@ inst_binary() {
594167
             exit 1
594167
         fi
594167
     done < <(LC_ALL=C ldd "$bin" 2>/dev/null)
594167
-    inst_simple "$bin" "$target"
594167
+
594167
+    # Same as above, but we need to wrap certain libraries unconditionally
594167
+    #
594167
+    # login - dlopen()s (not only) systemd's PAM modules
594167
+    # tar - called by machinectl in TEST-25
594167
+    if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$bin" =~ /(login|tar)$ ]]; then
594167
+        wrap_binary=1
594167
+    fi
594167
+
594167
+    if get_bool "$wrap_binary"; then
594167
+        dinfo "Creating ASan-compatible wrapper for binary '$target'"
594167
+        # Install the target binary with a ".orig" suffix
594167
+        inst_simple "$bin" "${target}.orig"
594167
+        # Create a simple shell wrapper in place of the target binary, which
594167
+        # sets necessary ASan-related env variables and then exec()s the
594167
+        # suffixed target binary
594167
+        cat >"$initdir/$target" <
594167
+#!/bin/bash
594167
+# Preload the ASan runtime DSO, otherwise ASAn will complain
594167
+export LD_PRELOAD="$ASAN_RT_PATH"
594167
+# Disable LSan to speed things up, since we don't care about leak reports
594167
+# from 'external' binaries
594167
+export ASAN_OPTIONS=detect_leaks=0
594167
+# Set argv[0] to the original binary name without the ".orig" suffix
594167
+exec -a "\$0" -- "${target}.orig" "\$@"
594167
+EOF
594167
+        chmod +x "$initdir/$target"
594167
+    else
594167
+        inst_simple "$bin" "$target"
594167
+    fi
594167
 }
594167
 
594167
 # same as above, except for shell scripts.