2023-02-08 16:01:48 -05:00
|
|
|
#!/bin/bash
|
2017-03-31 11:18:46 -04:00
|
|
|
# extend a TPM PCR with a module and then load it
|
|
|
|
# any arguments will also be measured.
|
|
|
|
# The default PCR to be extended is 5, but can be
|
|
|
|
# overridden with the MODULE_PCR environment variable
|
|
|
|
|
2023-09-02 04:16:16 -04:00
|
|
|
. /etc/functions
|
|
|
|
|
2024-02-01 14:30:31 -05:00
|
|
|
TRACE_FUNC
|
2017-03-31 11:18:46 -04:00
|
|
|
|
|
|
|
MODULE="$1"; shift
|
|
|
|
|
|
|
|
if [ -z "$MODULE_PCR" ]; then
|
|
|
|
MODULE_PCR=5
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
if [ -z "$MODULE" ]; then
|
|
|
|
die "Usage: $0 module [args...]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -r "$MODULE" ]; then
|
|
|
|
die "$MODULE: not found?"
|
|
|
|
fi
|
|
|
|
|
2024-02-22 13:36:25 -05:00
|
|
|
# Check if module is already loaded while remove trailing .ko if present
|
|
|
|
module=$(basename "$MODULE")
|
|
|
|
module=${module%.ko}
|
|
|
|
if lsmod | grep -q "^$module\\b"; then
|
|
|
|
DEBUG "$module: already loaded"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2017-04-10 17:48:52 -04:00
|
|
|
if [ ! -r /sys/class/tpm/tpm0/pcrs -o ! -x /bin/tpm ]; then
|
2022-08-25 14:43:31 -04:00
|
|
|
if [ ! -c /dev/tpmrm0 -o ! -x /bin/tpm2 ]; then
|
|
|
|
tpm_missing=1
|
|
|
|
fi
|
2017-04-10 17:48:52 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$tpm_missing" ]; then
|
2023-11-06 15:53:17 -05:00
|
|
|
DEBUG "Extending TPM PCR $MODULE_PCR with $MODULE prior of usage"
|
2022-08-25 14:43:31 -04:00
|
|
|
tpmr extend -ix "$MODULE_PCR" -if "$MODULE" \
|
2017-04-10 17:48:52 -04:00
|
|
|
|| die "$MODULE: tpm extend failed"
|
|
|
|
fi
|
2017-03-31 11:18:46 -04:00
|
|
|
|
2017-04-11 06:31:25 -04:00
|
|
|
if [ ! -z "$*" -a -z "$tpm_missing" ]; then
|
2023-11-06 15:53:17 -05:00
|
|
|
DEBUG "Extending TPM PCR $MODULE_PCR with $*"
|
2017-03-31 11:18:46 -04:00
|
|
|
TMPFILE=/tmp/insmod.$$
|
|
|
|
echo "$@" > $TMPFILE
|
2023-11-06 15:53:17 -05:00
|
|
|
DEBUG "Extending TPM PCR $MODULE_PCR with $MODULE prior of usage"
|
2022-08-25 14:43:31 -04:00
|
|
|
tpmr extend -ix "$MODULE_PCR" -if $TMPFILE \
|
2017-04-10 17:48:52 -04:00
|
|
|
|| die "$MODULE: tpm extend on arguments failed"
|
2017-03-31 11:18:46 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Since we have replaced the real insmod, we must invoke
|
|
|
|
# the busybox insmod via the original executable
|
2023-09-02 04:16:16 -04:00
|
|
|
DEBUG "Loading $MODULE with busybox insmod"
|
2017-04-10 17:48:52 -04:00
|
|
|
busybox insmod "$MODULE" "$@" \
|
2024-02-22 13:36:25 -05:00
|
|
|
|| die "$MODULE: insmod failed"
|