]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-50.sh
Merge pull request #22274 from yuwata/resolve-comment
[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 set -eux
6 set -o pipefail
7
8 export SYSTEMD_LOG_LEVEL=debug
9
10 cleanup()
11 {
12 if [ -z "${image_dir}" ]; then
13 return
14 fi
15 rm -rf "${image_dir}"
16 }
17
18 cd /tmp
19
20 image_dir="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
21 if [ -z "${image_dir}" ] || [ ! -d "${image_dir}" ]; then
22 echo "mktemp under /tmp failed"
23 exit 1
24 fi
25
26 trap cleanup EXIT
27
28 cp /usr/share/minimal* "${image_dir}/"
29 image="${image_dir}/minimal_0"
30 roothash="$(cat "${image}.roothash")"
31
32 os_release="$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)"
33
34 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"'
35 systemd-dissect "${image}.raw" | grep -q -F "MARKER=1"
36 systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release")
37
38 mv "${image}.verity" "${image}.fooverity"
39 mv "${image}.roothash" "${image}.foohash"
40 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"'
41 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F "MARKER=1"
42 systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F -f <(sed 's/"//g' "$os_release")
43 mv "${image}.fooverity" "${image}.verity"
44 mv "${image}.foohash" "${image}.roothash"
45
46 mkdir -p "${image_dir}/mount" "${image_dir}/mount2"
47 systemd-dissect --mount "${image}.raw" "${image_dir}/mount"
48 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
49 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
50 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
51 # Verity volume should be shared (opened only once)
52 systemd-dissect --mount "${image}.raw" "${image_dir}/mount2"
53 verity_count=$(find /dev/mapper/ -name "*verity*" | wc -l)
54 # In theory we should check that count is exactly one. In practice, libdevmapper
55 # randomly and unpredictably fails with an unhelpful EINVAL when a device is open
56 # (and even mounted and in use), so best-effort is the most we can do for now
57 if [ "${verity_count}" -lt 1 ]; then
58 echo "Verity device ${image}.raw not found in /dev/mapper/"
59 exit 1
60 fi
61 umount "${image_dir}/mount"
62 umount "${image_dir}/mount2"
63
64 systemd-run -P -p RootImage="${image}.raw" cat /usr/lib/os-release | grep -q -F "MARKER=1"
65 mv "${image}.verity" "${image}.fooverity"
66 mv "${image}.roothash" "${image}.foohash"
67 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"
68 # Let's use the long option name just here as a test
69 systemd-run -P --property RootImage="${image}.raw" --property RootHash="${roothash}" --property RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
70 mv "${image}.fooverity" "${image}.verity"
71 mv "${image}.foohash" "${image}.roothash"
72
73 # Make a GPT disk on the fly, with the squashfs as partition 1 and the verity hash tree as partition 2
74 machine="$(uname -m)"
75 if [ "${machine}" = "x86_64" ]; then
76 root_guid=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
77 verity_guid=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5
78 signature_guid=41092b05-9fc8-4523-994f-2def0408b176
79 architecture="x86-64"
80 elif [ "${machine}" = "i386" ] || [ "${machine}" = "i686" ] || [ "${machine}" = "x86" ]; then
81 root_guid=44479540-f297-41b2-9af7-d131d5f0458a
82 verity_guid=d13c5d3b-b5d1-422a-b29f-9454fdc89d76
83 signature_guid=5996fc05-109c-48de-808b-23fa0830b676
84 architecture="x86"
85 elif [ "${machine}" = "aarch64" ] || [ "${machine}" = "aarch64_be" ] || [ "${machine}" = "armv8b" ] || [ "${machine}" = "armv8l" ]; then
86 root_guid=b921b045-1df0-41c3-af44-4c6f280d3fae
87 verity_guid=df3300ce-d69f-4c92-978c-9bfb0f38d820
88 signature_guid=6db69de6-29f4-4758-a7a5-962190f00ce3
89 architecture="arm64"
90 elif [ "${machine}" = "arm" ]; then
91 root_guid=69dad710-2ce4-4e3c-b16c-21a1d49abed3
92 verity_guid=7386cdf2-203c-47a9-a498-f2ecce45a2d6
93 signature_guid=42b0455f-eb11-491d-98d3-56145ba9d037
94 architecture="arm"
95 elif [ "${machine}" = "loongarch64" ]; then
96 root_guid=77055800-792c-4f94-b39a-98c91b762bb6
97 verity_guid=f3393b22-e9af-4613-a948-9d3bfbd0c535
98 signature_guid=5afb67eb-ecc8-4f85-ae8e-ac1e7c50e7d0
99 architecture="loongarch64"
100 elif [ "${machine}" = "ia64" ]; then
101 root_guid=993d8d3d-f80e-4225-855a-9daf8ed7ea97
102 verity_guid=86ed10d5-b607-45bb-8957-d350f23d0571
103 signature_guid=e98b36ee-32ba-4882-9b12-0ce14655f46a
104 architecture="ia64"
105 elif [ "${machine}" = "s390x" ]; then
106 root_guid=5eead9a9-fe09-4a1e-a1d7-520d00531306
107 verity_guid=b325bfbe-c7be-4ab8-8357-139e652d2f6b
108 signature_guid=c80187a5-73a3-491a-901a-017c3fa953e9
109 architecture="s390x"
110 elif [ "${machine}" = "ppc64le" ]; then
111 root_guid=c31c45e6-3f39-412e-80fb-4809c4980599
112 verity_guid=906bd944-4589-4aae-a4e4-dd983917446a
113 signature_guid=d4a236e7-e873-4c07-bf1d-bf6cf7f1c3c6
114 architecture="ppc64-le"
115 else
116 echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
117 exit 1
118 fi
119 # du rounds up to block size, which is more helpful for partitioning
120 root_size="$(du -k "${image}.raw" | cut -f1)"
121 verity_size="$(du -k "${image}.verity" | cut -f1)"
122 signature_size=4
123 # 4MB seems to be the minimum size blkid will accept, below that probing fails
124 dd if=/dev/zero of="${image}.gpt" bs=512 count=$((8192+root_size*2+verity_size*2+signature_size*2))
125 # sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
126 # so do some basic rounding up if the minimal image is more than 1 MB
127 if [ "${root_size}" -ge 1024 ]; then
128 root_size="$((root_size/1024 + 1))MiB"
129 else
130 root_size="${root_size}KiB"
131 fi
132 verity_size="$((verity_size * 2))KiB"
133 signature_size="$((signature_size * 2))KiB"
134
135 HAVE_OPENSSL=0
136 if systemctl --version | grep -q -- +OPENSSL ; then
137 # The openssl binary is installed conditionally.
138 # If we have OpenSSL support enabled and openssl is missing, fail early
139 # with a proper error message.
140 if ! command -v openssl >/dev/null 2>&1; then
141 echo "openssl missing" >/failed
142 exit 1
143 fi
144 HAVE_OPENSSL=1
145 # Unfortunately OpenSSL insists on reading some config file, hence provide one with mostly placeholder contents
146 cat >> "${image}.openssl.cnf" <<EOF
147 [ req ]
148 prompt = no
149 distinguished_name = req_distinguished_name
150
151 [ req_distinguished_name ]
152 C = DE
153 ST = Test State
154 L = Test Locality
155 O = Org Name
156 OU = Org Unit Name
157 CN = Common Name
158 emailAddress = test@email.com
159 EOF
160
161 # Create key pair
162 openssl req -config "${image}.openssl.cnf" -new -x509 -newkey rsa:1024 -keyout "${image}.key" -out "${image}.crt" -days 365 -nodes
163 # Sign Verity root hash with it
164 openssl smime -sign -nocerts -noattr -binary -in "${image}.roothash" -inkey "${image}.key" -signer "${image}.crt" -outform der -out "${image}.roothash.p7s"
165 # Generate signature partition JSON data
166 echo '{"rootHash":"'"${roothash}"'","signature":"'"$(base64 -w 0 < "${image}.roothash.p7s")"'"}' > "${image}.verity-sig"
167 # Pad it
168 truncate -s "${signature_size}" "${image}.verity-sig"
169 # Register certificate in the (userspace) verity key ring
170 mkdir -p /run/verity.d
171 ln -s "${image}.crt" /run/verity.d/ok.crt
172 fi
173
174 # Construct a UUID from hash
175 # input: 11111111222233334444555566667777
176 # output: 11111111-2222-3333-4444-555566667777
177 uuid="$(head -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
178 echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk "${image}.gpt"
179 uuid="$(tail -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
180 echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk "${image}.gpt" --append
181 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
182 echo -e "size=${signature_size}, type=${signature_guid}" | sfdisk "${image}.gpt" --append
183 fi
184 sfdisk --part-label "${image}.gpt" 1 "Root Partition"
185 sfdisk --part-label "${image}.gpt" 2 "Verity Partition"
186 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
187 sfdisk --part-label "${image}.gpt" 3 "Signature Partition"
188 fi
189 loop="$(losetup --show -P -f "${image}.gpt")"
190 dd if="${image}.raw" of="${loop}p1"
191 dd if="${image}.verity" of="${loop}p2"
192 if [ "${HAVE_OPENSSL}" -eq 1 ]; then
193 dd if="${image}.verity-sig" of="${loop}p3"
194 fi
195 losetup -d "${loop}"
196
197 # Derive partition UUIDs from root hash, in UUID syntax
198 ROOT_UUID="$(systemd-id128 -u show "$(head -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
199 VERITY_UUID="$(systemd-id128 -u show "$(tail -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
200
201 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":"yes",'
202 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,'
203 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F "MARKER=1"
204 systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F -f <(sed 's/"//g' "$os_release")
205
206 systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" "${image_dir}/mount"
207 grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
208 grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
209 grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
210 umount "${image_dir}/mount"
211
212 # add explicit -p MountAPIVFS=yes once to test the parser
213 systemd-run -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
214
215 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"
216 systemd-run -P -p RootImage="${image}.gpt" -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
217
218 mkdir -p "${image_dir}/result"
219 cat >/run/systemd/system/testservice-50a.service <<EOF
220 [Service]
221 Type=oneshot
222 ExecStart=bash -c "mount >/run/result/a"
223 BindPaths=${image_dir}/result:/run/result
224 TemporaryFileSystem=/run
225 RootImage=${image}.raw
226 RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
227 RootImageOptions=nosuid,dev
228 EOF
229 systemctl start testservice-50a.service
230 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F "noatime"
231 grep -F "squashfs" "${image_dir}/result/a" | grep -q -F -v "nosuid"
232
233 cat >/run/systemd/system/testservice-50b.service <<EOF
234 [Service]
235 Type=oneshot
236 ExecStart=bash -c "mount >/run/result/b"
237 BindPaths=${image_dir}/result:/run/result
238 TemporaryFileSystem=/run
239 RootImage=${image}.gpt
240 RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
241 RootImageOptions=home:ro,dev nosuid,dev,%%foo
242 # this is the default, but let's specify once to test the parser
243 MountAPIVFS=yes
244 EOF
245 systemctl start testservice-50b.service
246 grep -F "squashfs" "${image_dir}/result/b" | grep -q -F "noatime"
247
248 # Check that specifier escape is applied %%foo → %foo
249 busctl get-property org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/testservice_2d50b_2eservice org.freedesktop.systemd1.Service RootImageOptions | grep -F "nosuid,dev,%foo"
250
251 # Now do some checks with MountImages, both by itself, with options and in combination with RootImage, and as single FS or GPT image
252 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"
253 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"
254 systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount | grep -F "squashfs" | grep -q -F "nosuid"
255 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"
256 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3" cat /run/img2:3/usr/lib/os-release | grep -q -F "MARKER=1"
257 systemd-run -P -p MountImages="${image}.raw:/run/img2\:3:nosuid" mount | grep -F "squashfs" | grep -q -F "nosuid"
258 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"
259 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"
260 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"
261 cat >/run/systemd/system/testservice-50c.service <<EOF
262 [Service]
263 MountAPIVFS=yes
264 TemporaryFileSystem=/run
265 RootImage=${image}.raw
266 MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
267 MountImages=${image}.raw:/run/img2\:3:nosuid
268 ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
269 ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
270 ExecStart=bash -c "mount >>/run/result/c"
271 BindPaths=${image_dir}/result:/run/result
272 Type=oneshot
273 EOF
274 systemctl start testservice-50c.service
275 grep -q -F "MARKER=1" "${image_dir}/result/c"
276 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F "noatime"
277 grep -F "squashfs" "${image_dir}/result/c" | grep -q -F -v "nosuid"
278
279 # Adding a new mounts at runtime works if the unit is in the active state,
280 # so use Type=notify to make sure there's no race condition in the test
281 cat >/run/systemd/system/testservice-50d.service <<EOF
282 [Service]
283 RuntimeMaxSec=300
284 Type=notify
285 RemainAfterExit=yes
286 MountAPIVFS=yes
287 PrivateTmp=yes
288 ExecStart=/bin/sh -c 'systemd-notify --ready; while ! grep -q -F MARKER /tmp/img/usr/lib/os-release; do sleep 0.1; done; mount | grep -F "/tmp/img" | grep -q -F "nosuid"'
289 EOF
290 systemctl start testservice-50d.service
291
292 systemctl mount-image --mkdir testservice-50d.service "${image}.raw" /tmp/img root:nosuid
293
294 while systemctl show -P SubState testservice-50d.service | grep -q running
295 do
296 sleep 0.1
297 done
298
299 systemctl is-active testservice-50d.service
300
301 # ExtensionImages will set up an overlay
302 systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
303 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"
304 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"
305 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"
306 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"
307 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"
308 cat >/run/systemd/system/testservice-50e.service <<EOF
309 [Service]
310 MountAPIVFS=yes
311 TemporaryFileSystem=/run /var/lib
312 StateDirectory=app0
313 RootImage=${image}.raw
314 ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
315 # Relevant only for sanitizer runs
316 UnsetEnvironment=LD_PRELOAD
317 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
318 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
319 Type=oneshot
320 RemainAfterExit=yes
321 EOF
322 systemctl start testservice-50e.service
323 systemctl is-active testservice-50e.service
324
325 # ExtensionDirectories will set up an overlay
326 mkdir -p "${image_dir}/app0" "${image_dir}/app1"
327 systemd-run -P --property ExtensionDirectories="${image_dir}/nonexistent" --property RootImage="${image}.raw" cat /opt/script0.sh && { echo 'unexpected success'; exit 1; }
328 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh && { echo 'unexpected success'; exit 1; }
329 systemd-dissect --mount /usr/share/app0.raw "${image_dir}/app0"
330 systemd-dissect --mount /usr/share/app1.raw "${image_dir}/app1"
331 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
332 systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
333 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"
334 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"
335 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"
336 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"
337 cat >/run/systemd/system/testservice-50f.service <<EOF
338 [Service]
339 MountAPIVFS=yes
340 TemporaryFileSystem=/run /var/lib
341 StateDirectory=app0
342 RootImage=${image}.raw
343 ExtensionDirectories=${image_dir}/app0 ${image_dir}/app1
344 # Relevant only for sanitizer runs
345 UnsetEnvironment=LD_PRELOAD
346 ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
347 ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
348 Type=oneshot
349 RemainAfterExit=yes
350 EOF
351 systemctl start testservice-50f.service
352 systemctl is-active testservice-50f.service
353 umount "${image_dir}/app0"
354 umount "${image_dir}/app1"
355
356 echo OK >/testok
357
358 exit 0