File grub2-blsbumpcounter-menu.patch of Package grub2

Call the command bls_bumpcounter when an entry is executed. Adding the
bls_bumpcounter command in the list of commands generated by blscfg breaks FDE,
as each command is logged in the PCR. Do not unset `enable_blscfg` grub env var,
otherwise bls_bumpcounter would not be called.

--- a/grub-core/normal/menu.c
+++ b/grub-core/normal/menu.c
@@ -332,6 +332,29 @@
     grub_env_set ("default", ptr + 1);
   else
     grub_env_unset ("default");
+
+#ifdef GRUB_MACHINE_EFI
+  const char* val = grub_env_get ("enable_blscfg");
+  if (val && (val[0] == '1' || val[0] == 'y') && entry->blsuki != NULL)
+    {
+      char* id = grub_strdup (entry->blsuki->filename);
+
+      if (id == NULL)
+	grub_print_error ();
+      else
+	{
+	  char* args[] = { id };
+	  grub_size_t id_len = grub_strlen (id);
+
+	  if (id_len >= 4 && grub_strcmp (id + id_len - 4, ".conf") == 0)
+	    id[id_len - 4] = '\0';
+	  grub_command_execute ("bls_bumpcounter", 1, args);
+	  grub_free (id);
+	}
+    }
+  grub_env_unset ("enable_blscfg");
+#endif
+
 #ifdef GRUB_MACHINE_IEEE1275
   char *cas_entry_id = NULL;
   char *cas_entry_source;
--- a/grub-core/normal/main.c
+++ b/grub-core/normal/main.c
@@ -356,7 +356,6 @@
       /* Ignore any error.  */
       grub_errno = GRUB_ERR_NONE;
       /* unset to let configfile and source commands continue to work */
-      grub_env_unset ("enable_blscfg");
       goto check_batch;
     }
 #endif
--- a/grub-core/commands/blsuki.c
+++ b/grub-core/commands/blsuki.c
@@ -1094,8 +1094,6 @@
   grub_size_t size;
   bool blsuki_save_default;
 
-  char *bumpcounter = NULL;
-
   linux_path = blsuki_get_val (entry, "linux", NULL);
   if (linux_path == NULL)
     {
@@ -1156,23 +1154,10 @@
   if (grub_errno != GRUB_ERR_NONE)
     goto finish;
 
-  /* "bls_bumpcounter " + id + "\n" */
-  int bumpcounter_size = sizeof("bls_bumpcounter ") + grub_strlen(id) + 1;
-  bumpcounter = grub_malloc(bumpcounter_size);
-  if (!bumpcounter)
-  {
-    grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-    goto finish;
-  }
-  char *tmp = bumpcounter;
-  tmp = grub_stpcpy(tmp, "bls_bumpcounter ");
-  tmp = grub_stpcpy(tmp, id);
-  tmp = grub_stpcpy(tmp, "\n");
-
   blsuki_save_default = grub_env_get_bool ("blsuki_save_default", false);
-  src = grub_xasprintf ("%s%s%s%s%s",
+  src = grub_xasprintf ("%s%s%s%s",
 			blsuki_save_default ? "savedefault\n" : "",
-			linux_cmd, bumpcounter ? bumpcounter : "", initrd_cmd ? initrd_cmd : "",
+			linux_cmd, initrd_cmd ? initrd_cmd : "",
 			dt_cmd ? dt_cmd : "");
 
   grub_normal_add_menu_entry (argc, argv, classes, id, users, hotkey, NULL, src, 0, entry, 0);
@@ -1186,7 +1171,6 @@
   grub_free (args);
   grub_free (argv);
   grub_free (src);
-  grub_free (bumpcounter);
 }
 
 #ifdef GRUB_MACHINE_EFI
openSUSE Build Service is sponsored by