2021-11-04 22:19:56 +03:00
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -euxo pipefail
2024-11-26 18:06:39 +03:00
DM_NAME = "integrity_test"
DM_NODE = " /dev/mapper/ ${ DM_NAME } "
DM_SERVICE = " systemd-integritysetup@ ${ DM_NAME } .service "
FS_UUID = "01234567-ffff-eeee-eeee-0123456789ab"
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
TMP_DIR =
LOOP =
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
cleanup( ) (
set +e
if [ [ -n " ${ LOOP } " ] ] ; then
losetup -d " ${ LOOP } "
2021-11-04 22:19:56 +03:00
fi
2024-11-26 18:06:39 +03:00
if [ [ -n " ${ TMP_DIR } " ] ] ; then
rm -rf " ${ TMP_DIR } "
2021-11-04 22:19:56 +03:00
fi
2024-11-26 18:06:39 +03:00
rm -rf /run/udev/rules.d/
udevadm control --reload
)
2021-11-04 22:19:56 +03:00
trap cleanup EXIT
2024-11-26 18:06:39 +03:00
udevadm settle
# Enable debugging logs for loop and dm block devices.
mkdir -p /run/udev/rules.d/
cat >/run/udev/rules.d/00-integrity-test.rules <<EOF
SUBSYSTEM = = "block" , KERNEL = = "loop*|dm-*" , OPTIONS = "log_level=debug"
2023-02-05 23:41:24 +03:00
EOF
2021-11-04 22:19:56 +03:00
2024-11-27 18:10:36 +03:00
# FIXME:
# There is no ordering restriction between underlying loopback block devices and DM devices.
# Hence, we may get wrong device node symlinks. To workaround that issue, let's decrease the
# priority for loopback block devices.
cat >/run/udev/rules.d/99-priority.rules <<EOF
SUBSYSTEM = = "block" , KERNEL = = "loop*" , OPTIONS = "link_priority=-200"
EOF
2024-11-26 18:06:39 +03:00
udevadm control --reload
TMP_DIR = " $( mktemp -d -t -p / integrity.tmp.XXXXXX) "
dd if = /dev/zero of = " ${ TMP_DIR } /image " bs = 1048576 count = 64
dd if = /dev/zero of = " ${ TMP_DIR } /data " bs = 1048576 count = 64
LOOP = " $( losetup --show -f " ${ TMP_DIR } /image " ) "
udevadm wait --timeout= 30 --settle " ${ LOOP } "
test_cleanup( ) (
set +e
if [ [ -e " /run/systemd/generator/ ${ DM_SERVICE } " ] ] ; then
systemctl stop " ${ DM_SERVICE } "
elif [ [ -e " ${ DM_NODE } " ] ] ; then
integritysetup close " ${ DM_NAME } "
fi
udevadm wait --timeout= 30 --settle --removed " ${ DM_NODE } "
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Clear integritytab.
rm -f /etc/integritytab
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Make the generator to re-run.
systemctl daemon-reload
)
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
test_one( ) {
local algorithm = " ${ 1 ? } "
local separate_data = " ${ 2 ? } "
2024-11-27 18:10:36 +03:00
local data_option
2022-08-03 20:41:13 +03:00
2024-11-26 18:06:39 +03:00
trap test_cleanup RETURN
if [ [ " ${ separate_data } " = = 1 ] ] ; then
data_option = " --data-device= ${ TMP_DIR } /data "
2022-08-03 20:41:13 +03:00
else
data_option = ""
fi
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
integritysetup format " ${ LOOP } " --batch-mode -I " ${ algorithm } " " ${ data_option } "
integritysetup open -I " ${ algorithm } " " ${ LOOP } " " ${ DM_NAME } " " ${ data_option } "
udevadm wait --timeout= 30 --settle " ${ DM_NODE } "
mkfs.ext4 -U " ${ FS_UUID } " " ${ DM_NODE } "
# Wait for synthetic events being processed.
2021-11-04 22:19:56 +03:00
udevadm settle
2024-11-26 18:06:39 +03:00
integritysetup close " ${ DM_NAME } "
udevadm wait --timeout= 30 --settle --removed " ${ DM_NODE } "
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Create integritytab.
if [ [ " ${ separate_data } " = = 1 ] ] ; then
data_option = " ,data-device= ${ TMP_DIR } /data "
2022-08-03 20:41:13 +03:00
else
data_option = ""
fi
2024-11-26 18:06:39 +03:00
cat >"/etc/integritytab" <<EOF
${ DM_NAME } ${ LOOP } - integrity-algorithm= ${ algorithm } ${ data_option }
EOF
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Make the generator to re-run.
systemctl daemon-reload
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Check for existence of the unit file.
[ [ -e " /run/systemd/generator/ ${ DM_SERVICE } " ] ]
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Make sure we are in a consistent state, e.g. not already active before we start.
[ [ " $( systemctl is-active " ${ DM_SERVICE } " ) " = = inactive ] ]
systemctl start " ${ DM_SERVICE } "
udevadm wait --timeout= 30 --settle " ${ DM_NODE } "
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
# Check the signature on the FS to ensure we can retrieve it and that is matches.
2024-11-27 18:10:36 +03:00
[ [ " $( blkid -U " ${ FS_UUID } " ) " = = " ${ DM_NODE } " ] ]
2024-11-26 18:06:39 +03:00
}
2021-11-04 22:19:56 +03:00
2024-11-26 18:06:39 +03:00
for a in crc32c crc32 xxhash64 sha1 sha256; do
test_one " $a " 0
test_one " $a " 1
2021-11-04 22:19:56 +03:00
done
2023-07-12 16:49:55 +03:00
touch /testok