]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-50.sh
Merge pull request #29601 from yuwata/mmap-check-overflow
[thirdparty/systemd.git] / test / units / testsuite-50.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
4 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 # shellcheck disable=SC2233,SC2235
6 set -eux
7 set -o pipefail
8
9 export SYSTEMD_LOG_LEVEL=debug
10
11 # shellcheck disable=SC2317
12 cleanup() {(
13 set +ex
14
15 if [ -z "${image_dir}" ]; then
16 return
17 fi
18 umount "${image_dir}/app0"
19 umount "${image_dir}/app1"
20 umount "${image_dir}/app-nodistro"
21 umount "${image_dir}/service-scoped-test"
22 rm -rf "${image_dir}"
23 )}
24
25 udevadm control --log-level=debug
26
27 cd /tmp
28
29 image_dir="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
30 if [ -z "${image_dir}" ] || [ ! -d "${image_dir}" ]; then
31 echo "mktemp under /tmp failed"
32 exit 1
33 fi
34
35 trap cleanup EXIT
36
37 cp /usr/share/minimal* "${image_dir}/"
38 image="${image_dir}/minimal_0"
39 roothash="$(cat "${image}.roothash")"
40
41 os_release="$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)"
42
43 systemd-dissect --json=short "${image}.raw" | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
44 systemd-dissect "${image}.raw" | grep -q -F "MARKER=1"
45 systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release")
46
47 systemd-dissect --list "${image}.raw" | grep -q '^etc/os-release$'
48 systemd-dissect --mtree "${image}.raw" --mtree-hash yes | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]* sha256sum=[a-z0-9]*$"
49 systemd-dissect --mtree "${image}.raw" --mtree-hash no | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]*$"
50
51 read -r SHA256SUM1 _ < <(systemd-dissect --copy-from "${image}.raw" etc/os-release | sha256sum)
52 test "$SHA256SUM1" != ""
53 read -r SHA256SUM2 _ < <(systemd-dissect --read-only --with "${image}.raw" sha256sum etc/os-release)
54 test "$SHA256SUM2" != ""
55 test "$SHA256SUM1" = "$SHA256SUM2"
56
57 mv "${image}.verity" "${image}.fooverity"
58 mv "${image}.roothash" "${image}.foohash"
59 systemd-dissect --json=short "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
60 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F "MARKER=1"
61 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F -f <(sed 's/"//g' "$os_release")
62 mv "${image}.fooverity" "${image}.verity"
63 mv "${image}.foohash" "${image}.roothash"
64
65 mkdir -p "${image_dir}/mount" "${image_dir}/mount2"
66 systemd-dissect --mount "${image}.raw" "${image_dir}/mount"
67 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
68 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
69 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
70 # Verity volume should be shared (opened only once)
71 systemd-dissect --mount "${image}.raw" "${image_dir}/mount2"
72 verity_count=$(find /dev/mapper/ -name "*verity*" | wc -l)
73 # In theory we should check that count is exactly one. In practice, libdevmapper
74 # randomly and unpredictably fails with an unhelpful EINVAL when a device is open
75 # (and even mounted and in use), so best-effort is the most we can do for now
76 if [ "${verity_count}" -lt 1 ]; then
77 echo "Verity device ${image}.raw not found in /dev/mapper/"
78 exit 1
79 fi
80 systemd-dissect --umount "${image_dir}/mount"
81 systemd-dissect --umount "${image_dir}/mount2"
82
83 systemd-run -P -p RootImage="${image}.raw" cat /usr/lib/os-release | grep -q -F "MARKER=1"
84 mv "${image}.verity" "${image}.fooverity"
85 mv "${image}.roothash" "${image}.foohash"
86 systemd-run -P -p RootImage="${image}.raw" -p RootHash="${image}.foohash" -p RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
87 # Let's use the long option name just here as a test
88 systemd-run -P --property RootImage="${image}.raw" --property RootHash="${roothash}" --property RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
89 mv "${image}.fooverity" "${image}.verity"
90 mv "${image}.foohash" "${image}.roothash"
91
92 # Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
93 machine="$(uname -m)"
94 if [ "${machine}" = "x86_64" ]; then
95 root_guid=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
96 verity_guid=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
97 signature_guid=41092b05-9fc8-4523-994f-2def0408b176
98 architecture="x86-64"
99 elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
100 root_guid=44479540-f297-41b2-9af7-d131d5f0458a
101 verity_guid=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
102 signature_guid=5996fc05-109c-48de-808b-23fa0830b676
103 architecture="x86"
104 elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
105 root_guid=b921b045-1df0-41c3-af44-4c6f280d3fae
106 verity_guid=df3300ce-d69f-4c92-978c-9bfb0f38d820
107 signature_guid=6db69de6-29f4-4758-a7a5-962190f00ce3
108 architecture="arm64"
109 elif [ "${machine}" = "arm" ]; then
110 root_guid=69dad710-2ce4-4e3c-b16c-21a1d49abed3
111 verity_guid=7386cdf2-203c-47a9-a498-f2ecce45a2d6
112 signature_guid=42b0455f-eb11-491d-98d3-56145ba9d037
113 architecture="arm"
114 elif [ "${machine}" = "loongarch64" ]; then
115 root_guid=77055800-792c-4f94-b39a-98c91b762bb6
116 verity_guid=f3393b22-e9af-4613-a948-9d3bfbd0c535
117 signature_guid=5afb67eb-ecc8-4f85-ae8e-ac1e7c50e7d0
118 architecture="loongarch64"
119 elif [ "${machine}" = "ia64" ]; then
120 root_guid=993d8d3d-f80e-4225-855a-9daf8ed7ea97
121 verity_guid=86ed10d5-b607-45bb-8957-d350f23d0571
122 signature_guid=e98b36ee-32ba-4882-9b12-0ce14655f46a
123 architecture="ia64"
124 elif [ "${machine}" = "s390x" ]; then
125 root_guid=5eead9a9-fe09-4a1e-a1d7-520d00531306
126 verity_guid=b325bfbe-c7be-4ab8-8357-139e652d2f6b
127 signature_guid=c80187a5-73a3-491a-901a-017c3fa953e9
128 architecture="s390x"
129 elif [ "${machine}" = "ppc64le" ]; then
130 root_guid=c31c45e6-3f39-412e-80fb-4809c4980599
131 verity_guid=906bd944-4589-4aae-a4e4-dd983917446a
132 signature_guid=d4a236e7-e873-4c07-bf1d-bf6cf7f1c3c6
133 architecture="ppc64-le"
134 else
135 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
136 exit 1
137 fi
138 # du rounds up to block size, which is more helpful for partitioning
139 root_size="$(du -k "${image}.raw" | cut -f1)"
140 verity_size="$(du -k "${image}.verity" | cut -f1)"
141 signature_size=4
142 # 4MB seems to be the minimum size blkid will accept, below that probing fails
143 dd if=/dev/zero of="${image}.gpt" bs=512 count=$((8192+root_size*2+verity_size*2+signature_size*2))
144 # sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
145 # so do some basic rounding up if the minimal image is more than 1 MB
146 if [ "${root_size}" -ge 1024 ]; then
147 root_size="$((root_size/1024 + 1))MiB"
148 else
149 root_size="${root_size}KiB"
150 fi
151 verity_size="$((verity_size * 2))KiB"
152 signature_size="$((signature_size * 2))KiB"
153
154 HAVE_OPENSSL=0
155 if systemctl --version | grep -q -- +OPENSSL ; then
156 # The openssl binary is installed conditionally.
157 # If we have OpenSSL support enabled and openssl is missing, fail early
158 # with a proper error message.
159 if ! command -v openssl >/dev/null 2>&1; then
160 echo "openssl missing" >/failed
161 exit 1
162 fi
163
164 HAVE_OPENSSL=1
165 OPENSSL_CONFIG="$(mktemp)"
166 # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
167 cat >"${OPENSSL_CONFIG:?}" <<EOF
168 [ req ]
169 prompt = no
170 distinguished_name = req_distinguished_name
171
172 [ req_distinguished_name ]
173 C = DE
174 ST = Test State
175 L = Test Locality
176 O = Org Name
177 OU = Org Unit Name
178 CN = Common Name
179 emailAddress = test@email.com
180 EOF
181
182 # Create key pair
183 openssl req -config "$OPENSSL_CONFIG" -new -x509 -newkey rsa:1024 -keyout "${image}.key" -out "${image}.crt" -days 365 -nodes
184 # Sign Verity root hash with it
185 openssl smime -sign -nocerts -noattr -binary -in "${image}.roothash" -inkey "${image}.key" -signer "${image}.crt" -outform der -out "${image}.roothash.p7s"
186 # Generate signature partition JSON data
187 echo '{"rootHash":"'"${roothash}"'","signature":"'"$(base64 -w 0 <"${image}.roothash.p7s")"'"}' >"${image}.verity-sig"
188 # Pad it
189 truncate -s "${signature_size}" "${image}.verity-sig"
190 # Register certificate in the (userspace) verity key ring
191 mkdir -p /run/verity.d
192 ln -s "${image}.crt" /run/verity.d/ok.crt
193 fi
194
195 # Construct a UUID from hash
196 # input: 11111111222233334444555566667777
197 # output: 11111111-2222-3333-4444-555566667777
198 uuid="$(head -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
199 echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk "${image}.gpt"
200 uuid="$(tail -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
201 echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk "${image}.gpt" --append
202 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
203 echo -e "size=${signature_size}, type=${signature_guid}" | sfdisk "${image}.gpt" --append
204 fi
205 sfdisk --part-label "${image}.gpt" 1 "Root Partition"
206 sfdisk --part-label "${image}.gpt" 2 "Verity Partition"
207 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
208 sfdisk --part-label "${image}.gpt" 3 "Signature Partition"
209 fi
210 loop="$(losetup --show -P -f "${image}.gpt")"
211 partitions=(
212 "${loop:?}p1"
213 "${loop:?}p2"
214 )
215 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
216 partitions+=( "${loop:?}p3" )
217 fi
218 # The kernel sometimes(?) does not emit "add" uevent for loop block partition devices.
219 # Let's not expect the devices to be initialized.
220 udevadm wait --timeout 60 --settle --initialized=no "${partitions[@]}"
221 udevadm lock --device="${loop}p1" dd if="${image}.raw" of="${loop}p1"
222 udevadm lock --device="${loop}p2" dd if="${image}.verity" of="${loop}p2"
223 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
224 udevadm lock --device="${loop}p3" dd if="${image}.verity-sig" of="${loop}p3"
225 fi
226 losetup -d "${loop}"
227
228 # Derive partition UUIDs from root hash, in UUID syntax
229 ROOT_UUID="$(systemd-id128 -u show "$(head -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
230 VERITY_UUID="$(systemd-id128 -u show "$(tail -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
231
232 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q '{"rw":"ro","designator":"root","partition_uuid":"'"$ROOT_UUID"'","partition_label":"Root Partition","fstype":"squashfs","architecture":"'"$architecture"'","verity":"signed",'
233 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q '{"rw":"ro","designator":"root-verity","partition_uuid":"'"$VERITY_UUID"'","partition_label":"Verity Partition","fstype":"DM_verity_hash","architecture":"'"$architecture"'","verity":null,'
234 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
235 systemd-dissect --json=short --root-hash "${roothash}" "${image}.gpt" | grep -q -E '{"rw":"ro","designator":"root-verity-sig","partition_uuid":"'".*"'","partition_label":"Signature Partition","fstype":"verity_hash_signature","architecture":"'"$architecture"'","verity":null,'
236 fi
237 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F "MARKER=1"
238 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F -f <(sed 's/"//g' "$os_release")
239
240 # Test image policies
241 systemd-dissect --validate "${image}.gpt"
242 systemd-dissect --validate "${image}.gpt" --image-policy='*'
243 (! systemd-dissect --validate "${image}.gpt" --image-policy='~')
244 (! systemd-dissect --validate "${image}.gpt" --image-policy='-')
245 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=absent)
246 (! systemd-dissect --validate "${image}.gpt" --image-policy=swap=unprotected+encrypted+verity)
247 systemd-dissect --validate "${image}.gpt" --image-policy=root=unprotected
248 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity
249 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity-sig=unused+absent
250 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent
251 systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:swap=absent+unprotected
252 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=verity:root-verity=unused+absent)
253 systemd-dissect --validate "${image}.gpt" --image-policy=root=signed
254 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity-sig=unused+absent)
255 (! systemd-dissect --validate "${image}.gpt" --image-policy=root=signed:root-verity=unused+absent)
256
257 # Test RootImagePolicy= unit file setting
258 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
259 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='*' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
260 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='~' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
261 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='-' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
262 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=absent' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
263 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=verity' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
264 systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=signed' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
265 (! systemd-run --wait -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p RootImagePolicy='root=encrypted' -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1")
266
267 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" "${image_dir}/mount"
268 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
269 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
270 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
271 systemd-dissect --umount "${image_dir}/mount"
272
273 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" --in-memory "${image_dir}/mount"
274 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
275 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
276 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
277 systemd-dissect --umount "${image_dir}/mount"
278
279 # add explicit -p MountAPIVFS=yes once to test the parser
280 systemd-run -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
281
282 systemd-run -P -p RootImage="${image}.raw" -p RootImageOptions="root:nosuid,dev home:ro,dev ro,noatime" mount | grep -F "squashfs" | grep -q -F "nosuid"
283 systemd-run -P -p RootImage="${image}.gpt" -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
284
285 mkdir -p "${image_dir}/result"
286 cat >/run/systemd/system/testservice-50a.service <<EOF
287 [Service]
288 Type=oneshot
289 ExecStart=bash -c "mount >/run/result/a"
290 BindPaths=${image_dir}/result:/run/result
291 TemporaryFileSystem=/run
292 RootImage=${image}.raw
293 RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
294 RootImageOptions=nosuid,dev
295 EOF
296 systemctl start testservice-50a.service
297 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F "noatime"
298 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F -v "nosuid"
299
300 cat >/run/systemd/system/testservice-50b.service <<EOF
301 [Service]
302 Type=oneshot
303 ExecStart=bash -c "mount >/run/result/b"
304 BindPaths=${image_dir}/result:/run/result
305 TemporaryFileSystem=/run
306 RootImage=${image}.gpt
307 RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
308 RootImageOptions=home:ro,dev nosuid,dev,%%foo
309 # this is the default, but let's specify once to test the parser
310 MountAPIVFS=yes
311 EOF
312 systemctl start testservice-50b.service
313 grep -F "squashfs" "${image_dir}/result/b" | grep -q -F "noatime"
314
315 # Check that specifier escape is applied %%foo → %foo
316 busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions | grep -F "nosuid,dev,%foo"
317
318 # Now do some checks with MountImages, both by itself, with options and in combination with RootImage, and as single FS or GPT image
319 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
320 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
321 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount | grep -F "squashfs" | grep -q -F "nosuid"
322 systemd-run -P -p MountImages="${image}.gpt:/run/img1:root:nosuid ${image}.raw:/run/img2:home:suid" mount | grep -F "squashfs" | grep -q -F "nosuid"
323 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3" cat /run/img2:3/usr/lib/os-release | grep -q -F "MARKER=1"
324 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3:nosuid" mount | grep -F "squashfs" | grep -q -F "nosuid"
325 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.raw" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /usr/lib/os-release | grep -q -F "MARKER=1"
326 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.raw" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
327 systemd-run -P -p TemporaryFileSystem=/run -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
328 cat >/run/systemd/system/testservice-50c.service <<EOF
329 [Service]
330 MountAPIVFS=yes
331 TemporaryFileSystem=/run
332 RootImage=${image}.raw
333 MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
334 MountImages=${image}.raw:/run/img2\:3:nosuid
335 ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
336 ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
337 ExecStart=bash -c "mount >>/run/result/c"
338 BindPaths=${image_dir}/result:/run/result
339 Type=oneshot
340 EOF
341 systemctl start testservice-50c.service
342 grep -q -F "MARKER=1" "${image_dir}/result/c"
343 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F "noatime"
344 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F -v "nosuid"
345
346 # Adding a new mounts at runtime works if the unit is in the active state,
347 # so use Type=notify to make sure there's no race condition in the test
348 cat >/run/systemd/system/testservice-50d.service <<EOF
349 [Service]
350 RuntimeMaxSec=300
351 Type=notify
352 RemainAfterExit=yes
353 MountAPIVFS=yes
354 PrivateTmp=yes
355 ExecStart=/bin/sh -c ' \\
356 systemd-notify --ready; \\
357 while [ ! -f /tmp/img/usr/lib/os-release ] || ! grep -q -F MARKER /tmp/img/usr/lib/os-release; do \\
358 sleep 0.1; \\
359 done; \\
360 mount; \\
361 mount | grep -F "on /tmp/img type squashfs" | grep -q -F "nosuid"; \\
362 '
363 EOF
364 systemctl start testservice-50d.service
365
366 # Mount twice to exercise mount-beneath (on kernel 6.5+, on older kernels it will just overmount)
367 mkdir -p /tmp/wrong/foo
368 mksquashfs /tmp/wrong/foo /tmp/wrong.raw
369 systemctl mount-image --mkdir testservice-50d.service /tmp/wrong.raw /tmp/img
370 test "$(systemctl show -P SubState testservice-50d.service)" = "running"
371 systemctl mount-image --mkdir testservice-50d.service "${image}.raw" /tmp/img root:nosuid
372
373 while systemctl show -P SubState testservice-50d.service | grep -q running
374 do
375 sleep 0.1
376 done
377
378 systemctl is-active testservice-50d.service
379
380 # ExtensionImages will set up an overlay
381 systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
382 systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
383 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
384 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
385 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /opt/script1.sh | grep -q -F "extension-release.app2"
386 systemd-run -P --property ExtensionImages="/usr/share/app0.raw /usr/share/app1.raw" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/other_file | grep -q -F "MARKER=1"
387 systemd-run -P --property ExtensionImages=/usr/share/app-nodistro.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
388 systemd-run -P --property ExtensionImages=/etc/service-scoped-test.raw --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
389 # Check that using a symlink to NAME-VERSION.raw works as long as the symlink has the correct name NAME.raw
390 mkdir -p /usr/share/symlink-test/
391 cp /usr/share/app-nodistro.raw /usr/share/symlink-test/app-nodistro-v1.raw
392 ln -fs /usr/share/symlink-test/app-nodistro-v1.raw /usr/share/symlink-test/app-nodistro.raw
393 systemd-run -P --property ExtensionImages=/usr/share/symlink-test/app-nodistro.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
394
395 # Symlink check again but for confext
396 mkdir -p /etc/symlink-test/
397 cp /etc/service-scoped-test.raw /etc/symlink-test/service-scoped-test-v1.raw
398 ln -fs /etc/symlink-test/service-scoped-test-v1.raw /etc/symlink-test/service-scoped-test.raw
399 systemd-run -P --property ExtensionImages=/etc/symlink-test/service-scoped-test.raw --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
400
401 cat >/run/systemd/system/testservice-50e.service <<EOF
402 [Service]
403 MountAPIVFS=yes
404 TemporaryFileSystem=/run /var/lib
405 StateDirectory=app0
406 RootImage=${image}.raw
407 ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
408 # Relevant only for sanitizer runs
409 UnsetEnvironment=LD_PRELOAD
410 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
411 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
412 Type=oneshot
413 RemainAfterExit=yes
414 EOF
415 systemctl start testservice-50e.service
416 systemctl is-active testservice-50e.service
417
418 # ExtensionDirectories will set up an overlay
419 mkdir -p "${image_dir}/app0" "${image_dir}/app1" "${image_dir}/app-nodistro" "${image_dir}/service-scoped-test"
420 (! systemd-run -P --property ExtensionDirectories="${image_dir}/nonexistent" --property RootImage="${image}.raw" cat /opt/script0.sh)
421 (! systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh)
422 systemd-dissect --mount /usr/share/app0.raw "${image_dir}/app0"
423 systemd-dissect --mount /usr/share/app1.raw "${image_dir}/app1"
424 systemd-dissect --mount /usr/share/app-nodistro.raw "${image_dir}/app-nodistro"
425 systemd-dissect --mount /etc/service-scoped-test.raw "${image_dir}/service-scoped-test"
426 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
427 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
428 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
429 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
430 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script1.sh | grep -q -F "extension-release.app2"
431 systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/other_file | grep -q -F "MARKER=1"
432 systemd-run -P --property ExtensionDirectories="${image_dir}/app-nodistro" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
433 systemd-run -P --property ExtensionDirectories="${image_dir}/service-scoped-test" --property RootImage="${image}.raw" cat /etc/systemd/system/some_file | grep -q -F "MARKER_CONFEXT_123"
434 cat >/run/systemd/system/testservice-50f.service <<EOF
435 [Service]
436 MountAPIVFS=yes
437 TemporaryFileSystem=/run /var/lib
438 StateDirectory=app0
439 RootImage=${image}.raw
440 ExtensionDirectories=${image_dir}/app0 ${image_dir}/app1
441 # Relevant only for sanitizer runs
442 UnsetEnvironment=LD_PRELOAD
443 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
444 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
445 Type=oneshot
446 RemainAfterExit=yes
447 EOF
448 systemctl start testservice-50f.service
449 systemctl is-active testservice-50f.service
450 systemd-dissect --umount "${image_dir}/app0"
451 systemd-dissect --umount "${image_dir}/app1"
452
453 # Test that an extension consisting of an empty directory under /etc/extensions/ takes precedence
454 mkdir -p /var/lib/extensions/
455 ln -s /usr/share/app-nodistro.raw /var/lib/extensions/app-nodistro.raw
456 systemd-sysext merge
457 grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file
458 systemd-sysext unmerge
459 mkdir -p /etc/extensions/app-nodistro
460 systemd-sysext merge
461 test ! -e /usr/lib/systemd/system/some_file
462 systemd-sysext unmerge
463 rmdir /etc/extensions/app-nodistro
464
465 # Similar, but go via varlink
466 varlinkctl call /run/systemd/io.systemd.sysext io.systemd.sysext.List '{}'
467 (! grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file )
468 varlinkctl call /run/systemd/io.systemd.sysext io.systemd.sysext.Merge '{}'
469 grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file
470 varlinkctl call /run/systemd/io.systemd.sysext io.systemd.sysext.Refresh '{}'
471 grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file
472 varlinkctl call /run/systemd/io.systemd.sysext io.systemd.sysext.Unmerge '{}'
473 (! grep -q -F "MARKER=1" /usr/lib/systemd/system/some_file )
474
475 # Check that extensions cannot contain os-release
476 mkdir -p /run/extensions/app-reject/usr/lib/{extension-release.d/,systemd/system}
477 echo "ID=_any" >/run/extensions/app-reject/usr/lib/extension-release.d/extension-release.app-reject
478 echo "ID=_any" >/run/extensions/app-reject/usr/lib/os-release
479 touch /run/extensions/app-reject/usr/lib/systemd/system/other_file
480 (! systemd-sysext merge)
481 test ! -e /usr/lib/systemd/system/some_file
482 test ! -e /usr/lib/systemd/system/other_file
483 systemd-sysext unmerge
484 rm -rf /run/extensions/app-reject
485 rm /var/lib/extensions/app-nodistro.raw
486
487 mkdir -p /run/machines /run/portables /run/extensions
488 touch /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
489
490 systemd-dissect --discover --json=short >/tmp/discover.json
491 grep -q -F '{"name":"a","type":"raw","class":"machine","ro":false,"path":"/run/machines/a.raw"' /tmp/discover.json
492 grep -q -F '{"name":"b","type":"raw","class":"portable","ro":false,"path":"/run/portables/b.raw"' /tmp/discover.json
493 grep -q -F '{"name":"c","type":"raw","class":"sysext","ro":false,"path":"/run/extensions/c.raw"' /tmp/discover.json
494 rm /tmp/discover.json /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
495
496 # Check that the /sbin/mount.ddi helper works
497 T="/tmp/mounthelper.$RANDOM"
498 mount -t ddi "${image}.gpt" "$T" -o ro,X-mount.mkdir,discard
499 umount -R "$T"
500 rmdir "$T"
501
502 LOOP="$(systemd-dissect --attach --loop-ref=waldo "${image}.raw")"
503
504 # Wait until the symlinks we want to test are established
505 udevadm trigger -w "$LOOP"
506
507 # Check if the /dev/loop/* symlinks really reference the right device
508 test /dev/disk/by-loop-ref/waldo -ef "$LOOP"
509
510 if [ "$(stat -c '%Hd:%Ld' "${image}.raw")" != '?d:?d' ] ; then
511 # Old stat didn't know the %Hd and %Ld specifiers and turned them into ?d
512 # instead. Let's simply skip the test on such old systems.
513 test "$(stat -c '/dev/disk/by-loop-inode/%Hd:%Ld-%i' "${image}.raw")" -ef "$LOOP"
514 fi
515
516 # Detach by loopback device
517 systemd-dissect --detach "$LOOP"
518
519 # Test long reference name.
520 # Note, sizeof_field(struct loop_info64, lo_file_name) == 64,
521 # and --loop-ref accepts upto 63 characters, and udev creates symlink
522 # based on the name when it has upto _62_ characters.
523 name="$(for _ in {1..62}; do echo -n 'x'; done)"
524 LOOP="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw")"
525 udevadm trigger -w "$LOOP"
526
527 # Check if the /dev/disk/by-loop-ref/$name symlink really references the right device
528 test "/dev/disk/by-loop-ref/$name" -ef "$LOOP"
529
530 # Detach by the /dev/disk/by-loop-ref symlink
531 systemd-dissect --detach "/dev/disk/by-loop-ref/$name"
532
533 name="$(for _ in {1..63}; do echo -n 'x'; done)"
534 LOOP="$(systemd-dissect --attach --loop-ref="$name" "${image}.raw")"
535 udevadm trigger -w "$LOOP"
536
537 # Check if the /dev/disk/by-loop-ref/$name symlink does not exist
538 test ! -e "/dev/disk/by-loop-ref/$name"
539
540 # Detach by backing inode
541 systemd-dissect --detach "${image}.raw"
542 (! systemd-dissect --detach "${image}.raw")
543
544 # check for confext functionality
545 mkdir -p /run/confexts/test/etc/extension-release.d
546 echo "ID=_any" >/run/confexts/test/etc/extension-release.d/extension-release.test
547 echo "ARCHITECTURE=_any" >>/run/confexts/test/etc/extension-release.d/extension-release.test
548 echo "MARKER_CONFEXT_123" >/run/confexts/test/etc/testfile
549 cat <<EOF >/run/confexts/test/etc/testscript
550 #!/bin/bash
551 echo "This should not happen"
552 EOF
553 chmod +x /run/confexts/test/etc/testscript
554 systemd-confext merge
555 grep -q -F "MARKER_CONFEXT_123" /etc/testfile
556 (! /etc/testscript)
557 systemd-confext status
558 systemd-confext unmerge
559 rm -rf /run/confexts/
560
561 unsquashfs -no-xattrs -d /tmp/img "${image}.raw"
562 systemd-run --unit=test-root-ephemeral \
563 -p RootDirectory=/tmp/img \
564 -p RootEphemeral=yes \
565 -p Type=exec \
566 bash -c "touch /abc && sleep infinity"
567 test -n "$(ls -A /var/lib/systemd/ephemeral-trees)"
568 systemctl stop test-root-ephemeral
569 # shellcheck disable=SC2016
570 timeout 10 bash -c 'until test -z "$(ls -A /var/lib/systemd/ephemeral-trees)"; do sleep .5; done'
571 test ! -f /tmp/img/abc
572
573 systemd-dissect --mtree /tmp/img
574 systemd-dissect --list /tmp/img
575
576 read -r SHA256SUM1 _ < <(systemd-dissect --copy-from /tmp/img etc/os-release | sha256sum)
577 test "$SHA256SUM1" != ""
578
579 echo abc > abc
580 systemd-dissect --copy-to /tmp/img abc /abc
581 test -f /tmp/img/abc
582
583 # Test for dissect tool support with systemd-sysext
584 mkdir -p /run/extensions/ testkit/usr/lib/extension-release.d/
585 echo "ID=_any" >testkit/usr/lib/extension-release.d/extension-release.testkit
586 echo "ARCHITECTURE=_any" >>testkit/usr/lib/extension-release.d/extension-release.testkit
587 echo "MARKER_SYSEXT_123" >testkit/usr/lib/testfile
588 mksquashfs testkit/ testkit.raw
589 cp testkit.raw /run/extensions/
590 unsquashfs -l /run/extensions/testkit.raw
591 systemd-dissect --no-pager /run/extensions/testkit.raw | grep -q '✓ sysext for portable service'
592 systemd-dissect --no-pager /run/extensions/testkit.raw | grep -q '✓ sysext for system'
593 systemd-sysext merge
594 systemd-sysext status
595 grep -q -F "MARKER_SYSEXT_123" /usr/lib/testfile
596 systemd-sysext unmerge
597 rm -rf /run/extensions/ testkit/
598
599 # Test for dissect tool support with systemd-confext
600 mkdir -p /run/confexts/ testjob/etc/extension-release.d/
601 echo "ID=_any" >testjob/etc/extension-release.d/extension-release.testjob
602 echo "ARCHITECTURE=_any" >>testjob/etc/extension-release.d/extension-release.testjob
603 echo "MARKER_CONFEXT_123" >testjob/etc/testfile
604 mksquashfs testjob/ testjob.raw
605 cp testjob.raw /run/confexts/
606 unsquashfs -l /run/confexts/testjob.raw
607 systemd-dissect --no-pager /run/confexts/testjob.raw | grep -q '✓ confext for system'
608 systemd-dissect --no-pager /run/confexts/testjob.raw | grep -q '✓ confext for portable service'
609 systemd-confext merge
610 systemd-confext status
611 grep -q -F "MARKER_CONFEXT_123" /etc/testfile
612 systemd-confext unmerge
613 rm -rf /run/confexts/ testjob/
614
615 systemd-run -P -p RootImage="${image}.raw" cat /run/host/os-release | cmp "${os_release}"
616
617 # Test that systemd-sysext reloads the daemon.
618 mkdir -p /var/lib/extensions/
619 ln -s /usr/share/app-reload.raw /var/lib/extensions/app-reload.raw
620 systemd-sysext merge --no-reload
621 # the service should not be running
622 if systemctl --quiet is-active foo.service; then
623 echo "foo.service should not be active"
624 exit 1
625 fi
626 systemd-sysext unmerge --no-reload
627 systemd-sysext merge
628 for RETRY in $(seq 60) LAST; do
629 if journalctl --boot --unit foo.service | grep -q -P 'echo\[[0-9]+\]: foo'; then
630 break
631 fi
632 if [ "${RETRY}" = LAST ]; then
633 echo "Output of foo.service not found"
634 exit 1
635 fi
636 sleep 0.5
637 done
638 systemd-sysext unmerge --no-reload
639 # Grep on the Warning to find the warning helper mentioning the daemon reload.
640 systemctl status foo.service 2>&1 | grep -q -F "Warning"
641 systemd-sysext merge
642 systemd-sysext unmerge
643 systemctl status foo.service 2>&1 | grep -v -q -F "Warning"
644
645 # Test systemd-repart --make-ddi=:
646 if command -v mksquashfs >/dev/null 2>&1; then
647
648 openssl req -config "$OPENSSL_CONFIG" -subj="/CN=waldo" -x509 -sha256 -nodes -days 365 -newkey rsa:4096 -keyout /tmp/test-50-privkey.key -out /tmp/test-50-cert.crt
649
650 mkdir -p /tmp/test-50-confext/etc/extension-release.d/
651
652 echo "foobar50" > /tmp/test-50-confext/etc/waldo
653
654 ( grep -e '^\(ID\|VERSION_ID\)=' /etc/os-release ; echo IMAGE_ID=waldo ; echo IMAGE_VERSION=7 ) > /tmp/test-50-confext/etc/extension-release.d/extension-release.waldo
655
656 mkdir -p /run/confexts
657
658 SYSTEMD_REPART_OVERRIDE_FSTYPE=squashfs systemd-repart -C -s /tmp/test-50-confext --certificate=/tmp/test-50-cert.crt --private-key=/tmp/test-50-privkey.key /run/confexts/waldo.confext.raw
659 rm -rf /tmp/test-50-confext
660
661 mkdir -p /run/verity.d
662 cp /tmp/test-50-cert.crt /run/verity.d/
663 systemd-dissect --mtree /run/confexts/waldo.confext.raw
664
665 systemd-confext refresh
666
667 read -r X < /etc/waldo
668 test "$X" = foobar50
669
670 rm /run/verity.d/test-50-cert.crt /run/confexts/waldo.confext.raw /tmp/test-50-cert.crt /tmp/test-50-privkey.key
671
672 systemd-confext refresh
673
674 (! test -f /tmp/test-50-confext/etc/waldo )
675 fi
676
677 # Sneak in a couple of expected-to-fail invocations to cover
678 # https://github.com/systemd/systemd/issues/29610
679 (! systemd-run -P -p MountImages="/this/should/definitely/not/exist.img:/run/img2\:3:nosuid" false)
680 (! systemd-run -P -p ExtensionImages="/this/should/definitely/not/exist.img" false)
681 (! systemd-run -P -p RootImage="/this/should/definitely/not/exist.img" false)
682 (! systemd-run -P -p ExtensionDirectories="/foo/bar /foo/baz" false)
683
684 touch /testok