diff --git a/modules.d/01fips-crypto-policies/fips-crypto-policies.sh b/modules.d/01fips-crypto-policies/fips-crypto-policies.sh new file mode 100755 index 0000000000..06aaf91dc1 --- /dev/null +++ b/modules.d/01fips-crypto-policies/fips-crypto-policies.sh @@ -0,0 +1,51 @@ +#!/usr/bin/sh + +type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh + +if ! fipsmode=$(getarg fips) || [ "$fipsmode" = "0" ] || [ -z "$fipsmode" ]; then + # Do nothing if not in FIPS mode + exit 0 +fi + +policyfile=/etc/crypto-policies/config +fipspolicyfile=/usr/share/crypto-policies/default-fips-config +backends=/etc/crypto-policies/back-ends +fipsbackends=/usr/share/crypto-policies/back-ends/FIPS + +# When in FIPS mode, check the active crypto policy by reading the +# $root/etc/crypto-policies/config file. If it is not "FIPS", or does not start +# with "FIPS:", automatically switch to the FIPS policy by creating +# bind-mounts. + +if ! [ -f "${NEWROOT}${policyfile}" ]; then + # No crypto-policies configured, possibly not a system that uses + # crypto-policies? + exit 0 +fi + +if ! [ -f "${NEWROOT}${fipspolicyfile}" ]; then + # crypto-policies is too old to deal with automatic bind-mounting of the + # FIPS policy over the normal policy, do not attempt to do the bind-mount. + exit 0 +fi + +policy=$(cat "${NEWROOT}${policyfile}") + +if [ "$policy" = "FIPS" ]; then + exit 0 +fi +# Remove the largest suffix pattern matching ":*" from the string (i.e., the +# complete list of active policy modules), then check for FIPS. This is part of +# POSIX sh (https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02). +if [ "${policy%%:*}" = "FIPS" ]; then + exit 0 +fi + +# Current crypto policy is not FIPS or FIPS-based, but the system is in FIPS +# mode; this is an inconsistent configuration. Automatically bind-mount a FIPS +# configuration over this. +mount -o bind,ro "${NEWROOT}${fipsbackends}" "${NEWROOT}${backends}" \ + || die "Failed to bind-mount FIPS policy over ${backends} (the system is in FIPS mode, but the crypto-policy is not)." + +mount -o bind,ro "${NEWROOT}${fipspolicyfile}" "${NEWROOT}${policyfile}" \ + || die "Failed to bind-mount FIPS crypto-policy state file over ${policyfile} (the system is in FIPS mode, but the crypto-policy is not)." diff --git a/modules.d/01fips-crypto-policies/module-setup.sh b/modules.d/01fips-crypto-policies/module-setup.sh new file mode 100755 index 0000000000..ee00452e80 --- /dev/null +++ b/modules.d/01fips-crypto-policies/module-setup.sh @@ -0,0 +1,27 @@ +#!/usr/bin/bash + +# called by dracut +check() { + # only enable on systems that use crypto-policies + [ -d "$dracutsysrootdir/etc/crypto-policies" ] && return 0 + + # include when something else depends on it or it is explicitly requested + return 255 +} + +# called by dracut +depends() { + return 0 +} + +# called by dracut +installkernel() { + return 0 +} + +# called by dracut +install() { + inst_hook pre-pivot 01 "$moddir/fips-crypto-policies.sh" + + inst_multiple mount +}