|
| 1 | +#!/usr/bin/sh |
| 2 | + |
| 3 | +type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh |
| 4 | + |
| 5 | +if ! fipsmode=$(getarg fips) || [ "$fipsmode" = "0" ] || [ -z "$fipsmode" ]; then |
| 6 | + # Do nothing if not in FIPS mode |
| 7 | + return 0 |
| 8 | +fi |
| 9 | + |
| 10 | +policyfile=/etc/crypto-policies/config |
| 11 | +fipspolicyfile=/usr/share/crypto-policies/default-fips-config |
| 12 | +backends=/etc/crypto-policies/back-ends |
| 13 | +fipsbackends=/usr/share/crypto-policies/back-ends/FIPS |
| 14 | + |
| 15 | +# When in FIPS mode, check the active crypto policy by reading the |
| 16 | +# $root/etc/crypto-policies/config file. If it is not "FIPS", or does not start |
| 17 | +# with "FIPS:", automatically switch to the FIPS policy by creating |
| 18 | +# bind-mounts. |
| 19 | + |
| 20 | +if ! [ -r "${NEWROOT}${policyfile}" ]; then |
| 21 | + # No crypto-policies configured, possibly not a system that uses |
| 22 | + # crypto-policies? |
| 23 | + return 0 |
| 24 | +fi |
| 25 | + |
| 26 | +if ! [ -f "${NEWROOT}${fipspolicyfile}" ]; then |
| 27 | + # crypto-policies is too old to deal with automatic bind-mounting of the |
| 28 | + # FIPS policy over the normal policy, do not attempt to do the bind-mount. |
| 29 | + return 0 |
| 30 | +fi |
| 31 | + |
| 32 | +policy=$(cat "${NEWROOT}${policyfile}") |
| 33 | + |
| 34 | +# Remove the largest suffix pattern matching ":*" from the string (i.e., the |
| 35 | +# complete list of active policy modules), then check for FIPS. This is part of |
| 36 | +# POSIX sh (https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02). |
| 37 | +if [ "${policy%%:*}" = "FIPS" ]; then |
| 38 | + return 0 |
| 39 | +fi |
| 40 | + |
| 41 | +# Current crypto policy is not FIPS or FIPS-based, but the system is in FIPS |
| 42 | +# mode; this is an inconsistent configuration. Automatically bind-mount a FIPS |
| 43 | +# configuration over this. |
| 44 | +if ! mount -o bind,ro "${NEWROOT}${fipsbackends}" "${NEWROOT}${backends}"; then |
| 45 | + warn "Failed to bind-mount FIPS policy over ${backends} (the system is in FIPS mode, but the crypto-policy is not)." |
| 46 | + # If this bind-mount failed, don't attempt to do the other one to avoid |
| 47 | + # a system that seems to be in FIPS crypto-policy but actually is not. |
| 48 | + return 0 |
| 49 | +fi |
| 50 | + |
| 51 | +mount -o bind,ro "${NEWROOT}${fipspolicyfile}" "${NEWROOT}${policyfile}" \ |
| 52 | + || warn "Failed to bind-mount FIPS crypto-policy state file over ${policyfile} (the system is in FIPS mode, but the crypto-policy is not)." |
0 commit comments