From 06c92591e9420bdc290abf49072991b96fa467ef Mon Sep 17 00:00:00 2001
From: Stuart Hayes <stuart.w.hayes@gmail.com>
Date: Fri, 8 Feb 2019 15:48:20 -0500
Subject: [PATCH] Hook exit when shim_lock protocol installed

A recent commit moved where the shim_lock protocol is loaded and
unloaded, but did not move where exit was hooked and unhooked.  Exit
needs to be hooked when the protocol is installed, so that the protocol
will be uninstalled on exit.  Otherwise, the system can crash if, for
example, shim loads grub, the user exits grub, shim is run again, which
installs a second instance of the protocol, and then grub tries to use
the shim_lock protocol that was installed by the first instance of shim.

Signed-off-by: Stuart Hayes <stuart.w.hayes@gmail.com>
---
 shim.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/shim.c b/shim.c
index 37afbbde..433f1901 100644
--- a/shim.c
+++ b/shim.c
@@ -2504,9 +2504,9 @@ shim_init(void)
 			loader_is_participating = 0;
 		}
 
-		hook_exit(systab);
 	}
 
+	hook_exit(systab);
 	return install_shim_protocols();
 }
 
@@ -2524,9 +2524,10 @@ shim_fini(void)
 		 * Remove our hooks from system services.
 		 */
 		unhook_system_services();
-		unhook_exit();
 	}
 
+	unhook_exit();
+
 	/*
 	 * Free the space allocated for the alternative 2nd stage loader
 	 */
--
2.19.1
