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