Debian Codegraph @

mkosi/mkosi.sanitizers/mkosi.postinst

raw
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -e
4 set -o nounset
5
6 LIBSYSTEMD="$(mkosi-chroot ldconfig -p | grep libsystemd.so.0 | sed 's/[^/]*\//\//')"
7
8 if [[ ! -f "$BUILDROOT/$LIBSYSTEMD" ]]; then
9 exit 0
10 fi
11
12 # ASAN and syscall filters aren't compatible with each other. Also, drop any memory limits
13 # as these are quite unpredictable when running under sanitizers.
14 find "$BUILDROOT"/{etc,usr/lib}/systemd/system/ -name '*.service' -type f | while read -r unit; do
15 if grep -q -e MemoryDeny -e MemoryMax -e MemoryHigh -e MemorySwapMax -e SystemCall "$unit" ; then
16 mkdir -p "$unit.d"
17 cat > "$unit.d/sanitizer-compat.conf" <<EOF
18 [Service]
19 MemoryDenyWriteExecute=no
20 MemoryMax=
21 MemoryHigh=
22 MemorySwapMax=
23 SystemCallFilter=
24 EOF
25 fi
26 done
27
28 # 'systemd-hwdb update' takes > 50s when built with sanitizers so let's not run it by default.
29 systemctl --root="$BUILDROOT" mask systemd-hwdb-update.service
30
31 ASAN_RT_PATH="$(grep libasan.so < <(mkosi-chroot ldd "$LIBSYSTEMD") | cut -d ' ' -f 3)"
32 if [[ -z "$ASAN_RT_PATH" ]]; then
33 ASAN_RT_PATH="$(grep libclang_rt.asan < <(mkosi-chroot ldd "$LIBSYSTEMD") | cut -d ' ' -f 3)"
34
35 # As clang's ASan DSO is usually in a non-standard path, let's check if the RUNPATH is set accordingly.
36 if mkosi-chroot ldd "$LIBSYSTEMD" | grep -q "libclang_rt.asan.*not found"; then
37 echo >&2 "clang's ASan DSO libclang_rt.asan is not present in the runtime library path"
38 exit 1
39 fi
40 fi
41 if [[ -z "$ASAN_RT_PATH" ]]; then
42 echo >&2 "systemd is not linked against the ASan DSO"
43 echo >&2 "gcc does this by default, for clang compile with -shared-libasan"
44 exit 1
45 fi
46
47 wrap=(
48 /usr/lib/polkit-1/polkitd
49 /usr/lib/systemd/tests/testdata/TEST-74-AUX-UTILS.units/proxy-echo.py
50 /usr/libexec/polkit-1/polkitd
51 /usr/lib/systemd/tests/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py
52 /usr/lib/systemd/tests/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-report-server.py
53 agetty
54 btrfs
55 capsh
56 chgrp
57 chown
58 cryptsetup
59 curl
60 dbus-broker-launch
61 dbus-daemon
62 delv
63 dfuzzer
64 dhcpd
65 dig
66 dnf
67 dnf5
68 dmsetup
69 dnsmasq
70 findmnt
71 getent
72 getfacl
73 groupadd
74 groupmod
75 groups
76 id
77 integritysetup
78 iscsiadm
79 iscsid
80 keymgr
81 knotc
82 knotd
83 kpartx
84 logger
85 login
86 ls
87 lsblk
88 lsns
89 lsof
90 lvm
91 mdadm
92 mkfs.btrfs
93 mkfs.ext4
94 mksquashfs
95 mount
96 mountpoint
97 multipath
98 multipathd
99 nvme
100 p11-kit
101 pkill
102 ps
103 setfacl
104 setpriv
105 socat
106 sshd
107 stat
108 stress-ng
109 su
110 swapoff
111 swapon
112 tar
113 tgtd
114 # The tpm2 tools (tpm2_readpublic, tpm2_pcrextend, ...) all are symlinks to tpm2
115 tpm2
116 umount
117 unix_chkpwd
118 useradd
119 userdel
120 veritysetup
121 )
122
123 for bin in "${wrap[@]}"; do
124 if ! mkosi-chroot bash -c "command -v $bin" >/dev/null; then
125 continue
126 fi
127
128 if [[ "$bin" == getent ]]; then
129 enable_lsan=1
130 else
131 enable_lsan=0
132 fi
133
134 target="$(mkosi-chroot bash -c "command -v $bin")"
135
136 mv "$BUILDROOT/$target" "$BUILDROOT/$target.orig"
137
138 cat >"$BUILDROOT/$target" <<EOF
139 #!/bin/bash
140 # Preload the ASan runtime DSO, otherwise ASan will complain
141 export LD_PRELOAD="$ASAN_RT_PATH"
142 # Disable LSan to speed things up, since we don't care about leak reports
143 # from 'external' binaries. In addition, disable quarantine (for use-after-free
144 # detection) and malloc stack frame collection as we don't care about these in
145 # 'external' binaries either, and they just unnecessarily hog up memory & cpu
146 # cycles.
147 export ASAN_OPTIONS=detect_leaks=$enable_lsan:quarantine_size_mb=0:malloc_context_size=0
148 # Set argv[0] to the original binary name without the ".orig" suffix
149 exec -a "\$0" -- "${target}.orig" "\$@"
150 EOF
151 chmod +x "$BUILDROOT/$target"
152 done
153
154 cat >"$BUILDROOT"/usr/lib/systemd/systemd-asan-env <<EOF
155 LD_PRELOAD=$ASAN_RT_PATH
156 LSAN_OPTIONS=detect_leaks=0
157 EOF