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
5set -eux
6set -o pipefail
7
8export SYSTEMD_LOG_LEVEL=debug
9
10cleanup()
11{
12    if [ -z "${image_dir}" ]; then
13        return
14    fi
15    rm -rf "${image_dir}"
16}
17
18cd /tmp
19
20image_dir="$(mktemp -d -t -p /tmp tmp.XXXXXX)"
21if [ -z "${image_dir}" ] || [ ! -d "${image_dir}" ]; then
22    echo "mktemp under /tmp failed"
23    exit 1
24fi
25
26trap cleanup EXIT
27
28cp /usr/share/minimal* "${image_dir}/"
29image="${image_dir}/minimal_0"
30roothash="$(cat "${image}.roothash")"
31
32os_release="$(test -e /etc/os-release && echo /etc/os-release || echo /usr/lib/os-release)"
33
34systemd-dissect --json=short "${image}.raw" | grep -q -F '{"rw":"ro","designator":"root","partition_uuid":null,"partition_label":null,"fstype":"squashfs","architecture":null,"verity":"external"'
35systemd-dissect "${image}.raw" | grep -q -F "MARKER=1"
36systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release")
37
38mv "${image}.verity" "${image}.fooverity"
39mv "${image}.roothash" "${image}.foohash"
40systemd-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"'
41systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F "MARKER=1"
42systemd-dissect "${image}.raw" --root-hash="${roothash}" --verity-data="${image}.fooverity" | grep -q -F -f <(sed 's/"//g' "$os_release")
43mv "${image}.fooverity" "${image}.verity"
44mv "${image}.foohash" "${image}.roothash"
45
46mkdir -p "${image_dir}/mount" "${image_dir}/mount2"
47systemd-dissect --mount "${image}.raw" "${image_dir}/mount"
48grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
49grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
50grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
51# Verity volume should be shared (opened only once)
52systemd-dissect --mount "${image}.raw" "${image_dir}/mount2"
53verity_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
57if [ "${verity_count}" -lt 1 ]; then
58    echo "Verity device ${image}.raw not found in /dev/mapper/"
59    exit 1
60fi
61umount "${image_dir}/mount"
62umount "${image_dir}/mount2"
63
64systemd-run -P -p RootImage="${image}.raw" cat /usr/lib/os-release | grep -q -F "MARKER=1"
65mv "${image}.verity" "${image}.fooverity"
66mv "${image}.roothash" "${image}.foohash"
67systemd-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
69systemd-run -P --property RootImage="${image}.raw" --property RootHash="${roothash}" --property RootVerity="${image}.fooverity" cat /usr/lib/os-release | grep -q -F "MARKER=1"
70mv "${image}.fooverity" "${image}.verity"
71mv "${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
74machine="$(uname -m)"
75if [ "${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"
80elif [ "${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"
85elif [ "${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"
90elif [ "${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"
95elif [ "${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"
100elif [ "${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"
105elif [ "${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"
110elif [ "${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"
115else
116    echo "Unexpected uname -m: ${machine} in testsuite-50.sh, please fix me"
117    exit 1
118fi
119# du rounds up to block size, which is more helpful for partitioning
120root_size="$(du -k "${image}.raw" | cut -f1)"
121verity_size="$(du -k "${image}.verity" | cut -f1)"
122signature_size=4
123# 4MB seems to be the minimum size blkid will accept, below that probing fails
124dd 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
127if [ "${root_size}" -ge 1024 ]; then
128    root_size="$((root_size/1024 + 1))MiB"
129else
130    root_size="${root_size}KiB"
131fi
132verity_size="$((verity_size * 2))KiB"
133signature_size="$((signature_size * 2))KiB"
134
135HAVE_OPENSSL=0
136if 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 ]
148prompt = no
149distinguished_name = req_distinguished_name
150
151[ req_distinguished_name ]
152C = DE
153ST = Test State
154L = Test Locality
155O = Org Name
156OU = Org Unit Name
157CN = Common Name
158emailAddress = test@email.com
159EOF
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
172fi
173
174# Construct a UUID from hash
175# input:  11111111222233334444555566667777
176# output: 11111111-2222-3333-4444-555566667777
177uuid="$(head -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
178echo -e "label: gpt\nsize=${root_size}, type=${root_guid}, uuid=${uuid}" | sfdisk "${image}.gpt"
179uuid="$(tail -c 32 "${image}.roothash" | sed -r 's/(.{8})(.{4})(.{4})(.{4})(.+)/\1-\2-\3-\4-\5/')"
180echo -e "size=${verity_size}, type=${verity_guid}, uuid=${uuid}" | sfdisk "${image}.gpt" --append
181if [ "${HAVE_OPENSSL}" -eq 1 ]; then
182    echo -e "size=${signature_size}, type=${signature_guid}" | sfdisk "${image}.gpt" --append
183fi
184sfdisk --part-label "${image}.gpt" 1 "Root Partition"
185sfdisk --part-label "${image}.gpt" 2 "Verity Partition"
186if [ "${HAVE_OPENSSL}" -eq 1 ]; then
187    sfdisk --part-label "${image}.gpt" 3 "Signature Partition"
188fi
189loop="$(losetup --show -P -f "${image}.gpt")"
190dd if="${image}.raw" of="${loop}p1"
191dd if="${image}.verity" of="${loop}p2"
192if [ "${HAVE_OPENSSL}" -eq 1 ]; then
193    dd if="${image}.verity-sig" of="${loop}p3"
194fi
195losetup -d "${loop}"
196
197# Derive partition UUIDs from root hash, in UUID syntax
198ROOT_UUID="$(systemd-id128 -u show "$(head -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
199VERITY_UUID="$(systemd-id128 -u show "$(tail -c 32 "${image}.roothash")" -u | tail -n 1 | cut -b 6-)"
200
201systemd-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",'
202systemd-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,'
203systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F "MARKER=1"
204systemd-dissect --root-hash "${roothash}" "${image}.gpt" | grep -q -F -f <(sed 's/"//g' "$os_release")
205
206systemd-dissect --root-hash "${roothash}" --mount "${image}.gpt" "${image_dir}/mount"
207grep -q -F -f "$os_release" "${image_dir}/mount/usr/lib/os-release"
208grep -q -F -f "$os_release" "${image_dir}/mount/etc/os-release"
209grep -q -F "MARKER=1" "${image_dir}/mount/usr/lib/os-release"
210umount "${image_dir}/mount"
211
212# add explicit -p MountAPIVFS=yes once to test the parser
213systemd-run -P -p RootImage="${image}.gpt" -p RootHash="${roothash}" -p MountAPIVFS=yes cat /usr/lib/os-release | grep -q -F "MARKER=1"
214
215systemd-run -P -p RootImage="${image}.raw" -p RootImageOptions="root:nosuid,dev home:ro,dev ro,noatime" mount | grep -F "squashfs" | grep -q -F "nosuid"
216systemd-run -P -p RootImage="${image}.gpt" -p RootImageOptions="root:ro,noatime root:ro,dev" mount | grep -F "squashfs" | grep -q -F "noatime"
217
218mkdir -p "${image_dir}/result"
219cat >/run/systemd/system/testservice-50a.service <<EOF
220[Service]
221Type=oneshot
222ExecStart=bash -c "mount >/run/result/a"
223BindPaths=${image_dir}/result:/run/result
224TemporaryFileSystem=/run
225RootImage=${image}.raw
226RootImageOptions=root:ro,noatime home:ro,dev relatime,dev
227RootImageOptions=nosuid,dev
228EOF
229systemctl start testservice-50a.service
230grep -F "squashfs" "${image_dir}/result/a" | grep -q -F "noatime"
231grep -F "squashfs" "${image_dir}/result/a" | grep -q -F -v "nosuid"
232
233cat >/run/systemd/system/testservice-50b.service <<EOF
234[Service]
235Type=oneshot
236ExecStart=bash -c "mount >/run/result/b"
237BindPaths=${image_dir}/result:/run/result
238TemporaryFileSystem=/run
239RootImage=${image}.gpt
240RootImageOptions=root:ro,noatime,nosuid home:ro,dev nosuid,dev
241RootImageOptions=home:ro,dev nosuid,dev,%%foo
242# this is the default, but let's specify once to test the parser
243MountAPIVFS=yes
244EOF
245systemctl start testservice-50b.service
246grep -F "squashfs" "${image_dir}/result/b" | grep -q -F "noatime"
247
248# Check that specifier escape is applied %%foo → %foo
249busctl 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
252systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img1/usr/lib/os-release | grep -q -F "MARKER=1"
253systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2" cat /run/img2/usr/lib/os-release | grep -q -F "MARKER=1"
254systemd-run -P -p MountImages="${image}.gpt:/run/img1 ${image}.raw:/run/img2:nosuid,dev" mount | grep -F "squashfs" | grep -q -F "nosuid"
255systemd-run -P -p MountImages="${image}.gpt:/run/img1:root:nosuid ${image}.raw:/run/img2:home:suid" mount | grep -F "squashfs" | grep -q -F "nosuid"
256systemd-run -P -p MountImages="${image}.raw:/run/img2\:3" cat /run/img2:3/usr/lib/os-release | grep -q -F "MARKER=1"
257systemd-run -P -p MountImages="${image}.raw:/run/img2\:3:nosuid" mount | grep -F "squashfs" | grep -q -F "nosuid"
258systemd-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"
259systemd-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"
260systemd-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"
261cat >/run/systemd/system/testservice-50c.service <<EOF
262[Service]
263MountAPIVFS=yes
264TemporaryFileSystem=/run
265RootImage=${image}.raw
266MountImages=${image}.gpt:/run/img1:root:noatime:home:relatime
267MountImages=${image}.raw:/run/img2\:3:nosuid
268ExecStart=bash -c "cat /run/img1/usr/lib/os-release >/run/result/c"
269ExecStart=bash -c "cat /run/img2:3/usr/lib/os-release >>/run/result/c"
270ExecStart=bash -c "mount >>/run/result/c"
271BindPaths=${image_dir}/result:/run/result
272Type=oneshot
273EOF
274systemctl start testservice-50c.service
275grep -q -F "MARKER=1" "${image_dir}/result/c"
276grep -F "squashfs" "${image_dir}/result/c" | grep -q -F "noatime"
277grep -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
281cat >/run/systemd/system/testservice-50d.service <<EOF
282[Service]
283RuntimeMaxSec=300
284Type=notify
285RemainAfterExit=yes
286MountAPIVFS=yes
287PrivateTmp=yes
288ExecStart=/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 "/dev/mapper/${roothash}-verity" | grep -q -F "nosuid"'
289EOF
290systemctl start testservice-50d.service
291
292systemctl mount-image --mkdir testservice-50d.service "${image}.raw" /tmp/img root:nosuid
293
294while systemctl show -P SubState testservice-50d.service | grep -q running
295do
296    sleep 0.1
297done
298
299systemctl is-active testservice-50d.service
300
301# ExtensionImages will set up an overlay
302systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
303systemd-run -P --property ExtensionImages=/usr/share/app0.raw --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
304systemd-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"
305systemd-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"
306systemd-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"
307systemd-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"
308cat >/run/systemd/system/testservice-50e.service <<EOF
309[Service]
310MountAPIVFS=yes
311TemporaryFileSystem=/run /var/lib
312StateDirectory=app0
313RootImage=${image}.raw
314ExtensionImages=/usr/share/app0.raw /usr/share/app1.raw:nosuid
315# Relevant only for sanitizer runs
316UnsetEnvironment=LD_PRELOAD
317ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
318ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
319Type=oneshot
320RemainAfterExit=yes
321EOF
322systemctl start testservice-50e.service
323systemctl is-active testservice-50e.service
324
325# ExtensionDirectories will set up an overlay
326mkdir -p "${image_dir}/app0" "${image_dir}/app1"
327systemd-run -P --property ExtensionDirectories="${image_dir}/nonexistent" --property RootImage="${image}.raw" cat /opt/script0.sh && { echo 'unexpected success'; exit 1; }
328systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh && { echo 'unexpected success'; exit 1; }
329systemd-dissect --mount /usr/share/app0.raw "${image_dir}/app0"
330systemd-dissect --mount /usr/share/app1.raw "${image_dir}/app1"
331systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
332systemd-run -P --property ExtensionDirectories="${image_dir}/app0" --property RootImage="${image}.raw" cat /usr/lib/systemd/system/some_file | grep -q -F "MARKER=1"
333systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script0.sh | grep -q -F "extension-release.app0"
334systemd-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"
335systemd-run -P --property ExtensionDirectories="${image_dir}/app0 ${image_dir}/app1" --property RootImage="${image}.raw" cat /opt/script1.sh | grep -q -F "extension-release.app2"
336systemd-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"
337cat >/run/systemd/system/testservice-50f.service <<EOF
338[Service]
339MountAPIVFS=yes
340TemporaryFileSystem=/run /var/lib
341StateDirectory=app0
342RootImage=${image}.raw
343ExtensionDirectories=${image_dir}/app0 ${image_dir}/app1
344# Relevant only for sanitizer runs
345UnsetEnvironment=LD_PRELOAD
346ExecStart=/bin/bash -c '/opt/script0.sh | grep ID'
347ExecStart=/bin/bash -c '/opt/script1.sh | grep ID'
348Type=oneshot
349RemainAfterExit=yes
350EOF
351systemctl start testservice-50f.service
352systemctl is-active testservice-50f.service
353umount "${image_dir}/app0"
354umount "${image_dir}/app1"
355
356echo OK >/testok
357
358exit 0
359