6f7af4
From ec2e4e70a1037b2df535e48ef7389b7b76b5a29a Mon Sep 17 00:00:00 2001
6f7af4
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
6f7af4
Date: Thu, 13 Jan 2022 17:35:59 +0100
6f7af4
Subject: [PATCH] fix(dracut-shutdown): add cleanup handler on failure
6f7af4
6f7af4
It may happen that dracut-shutdown.service fails, for example on timeout
6f7af4
due to very low bandwidth.
6f7af4
In such case, for hardening purposes, a new dracut-shutdown-onfailure.service
6f7af4
unit doing dracut-shutdown.service cleanup needs to execute to make sure
6f7af4
switching root to an incomplete initramfs won't occur later.
6f7af4
6f7af4
Resolves: #1924587
6f7af4
---
6f7af4
 Makefile                                                    |  1 +
6f7af4
 dracut.spec                                                 |  1 +
6f7af4
 .../98dracut-systemd/dracut-shutdown-onfailure.service      | 13 +++++++++++++
6f7af4
 modules.d/98dracut-systemd/dracut-shutdown.service          |  1 +
6f7af4
 modules.d/98dracut-systemd/dracut-shutdown.service.8.asc    |  3 +++
6f7af4
 5 files changed, 19 insertions(+)
6f7af4
6f7af4
diff --git a/Makefile b/Makefile
6f7af4
index 503d069f..1c0f48ad 100644
6f7af4
--- a/Makefile
6f7af4
+++ b/Makefile
6f7af4
@@ -142,6 +142,7 @@ ifneq ($(enable_documentation),no)
6f7af4
 endif
6f7af4
 	if [ -n "$(systemdsystemunitdir)" ]; then \
6f7af4
 		mkdir -p $(DESTDIR)$(systemdsystemunitdir); \
6f7af4
+		ln -srf $(DESTDIR)$(pkglibdir)/modules.d/98dracut-systemd/dracut-shutdown-onfailure.service $(DESTDIR)$(systemdsystemunitdir)/dracut-shutdown-onfailure.service; \
6f7af4
 		ln -srf $(DESTDIR)$(pkglibdir)/modules.d/98dracut-systemd/dracut-shutdown.service $(DESTDIR)$(systemdsystemunitdir)/dracut-shutdown.service; \
6f7af4
 		mkdir -p $(DESTDIR)$(systemdsystemunitdir)/sysinit.target.wants; \
6f7af4
 		ln -s ../dracut-shutdown.service \
6f7af4
diff --git a/dracut.spec b/dracut.spec
6f7af4
index e1c22256..90fa903a 100644
6f7af4
--- a/dracut.spec
6f7af4
+++ b/dracut.spec
6f7af4
@@ -404,6 +404,7 @@ echo 'dracut_rescue_image="yes"' > $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/
6f7af4
 %dir %{_sharedstatedir}/initramfs
6f7af4
 %if %{defined _unitdir}
6f7af4
 %{_unitdir}/dracut-shutdown.service
6f7af4
+%{_unitdir}/dracut-shutdown-onfailure.service
6f7af4
 %{_unitdir}/sysinit.target.wants/dracut-shutdown.service
6f7af4
 %{_unitdir}/dracut-cmdline.service
6f7af4
 %{_unitdir}/dracut-initqueue.service
6f7af4
diff --git a/modules.d/98dracut-systemd/dracut-shutdown-onfailure.service b/modules.d/98dracut-systemd/dracut-shutdown-onfailure.service
6f7af4
new file mode 100644
6f7af4
index 00000000..96de58c5
6f7af4
--- /dev/null
6f7af4
+++ b/modules.d/98dracut-systemd/dracut-shutdown-onfailure.service
6f7af4
@@ -0,0 +1,13 @@
6f7af4
+#  This file is part of dracut.
6f7af4
+#
6f7af4
+# See dracut.bootup(7) for details
6f7af4
+
6f7af4
+[Unit]
6f7af4
+Description=Service executing upon dracut-shutdown failure to perform cleanup
6f7af4
+Documentation=man:dracut-shutdown.service(8)
6f7af4
+DefaultDependencies=no
6f7af4
+
6f7af4
+[Service]
6f7af4
+Type=oneshot
6f7af4
+ExecStart=-/bin/rm /run/initramfs/shutdown
6f7af4
+StandardError=null
6f7af4
diff --git a/modules.d/98dracut-systemd/dracut-shutdown.service b/modules.d/98dracut-systemd/dracut-shutdown.service
6f7af4
index b7324586..dd4cf81e 100644
6f7af4
--- a/modules.d/98dracut-systemd/dracut-shutdown.service
6f7af4
+++ b/modules.d/98dracut-systemd/dracut-shutdown.service
6f7af4
@@ -8,6 +8,7 @@ Documentation=man:dracut-shutdown.service(8)
6f7af4
 After=local-fs.target boot.mount boot.automount
6f7af4
 Wants=local-fs.target
6f7af4
 ConditionPathExists=!/run/initramfs/bin/sh
6f7af4
+OnFailure=dracut-shutdown-onfailure.service
6f7af4
 
6f7af4
 [Service]
6f7af4
 RemainAfterExit=yes
6f7af4
diff --git a/modules.d/98dracut-systemd/dracut-shutdown.service.8.asc b/modules.d/98dracut-systemd/dracut-shutdown.service.8.asc
6f7af4
index ba80b187..21ec88ca 100644
6f7af4
--- a/modules.d/98dracut-systemd/dracut-shutdown.service.8.asc
6f7af4
+++ b/modules.d/98dracut-systemd/dracut-shutdown.service.8.asc
6f7af4
@@ -40,6 +40,9 @@ by injecting "rd.break=pre-shutdown rd.shell" or "rd.break=shutdown rd.shell".
6f7af4
 # touch /run/initramfs/.need_shutdown
6f7af4
 ----
6f7af4
 
6f7af4
+In case the unpack of the initramfs fails, dracut-shutdown-onfailure.service
6f7af4
+executes to make sure switch root doesn't happen, since it would result in
6f7af4
+switching to an incomplete initramfs.
6f7af4
 
6f7af4
 AUTHORS
6f7af4
 -------
6f7af4