Blame SOURCES/0268-grub-set-bootflag-Write-new-env-to-tmpfile-and-then-.patch

4d592c
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4d592c
From: Hans de Goede <hdegoede@redhat.com>
4d592c
Date: Fri, 22 Nov 2019 11:54:27 +0100
4d592c
Subject: [PATCH] grub-set-bootflag: Write new env to tmpfile and then rename
4d592c
4d592c
Make the grubenv writing code in grub-set-bootflag more robust by
4d592c
writing the modified grubenv to a tmpfile first and then renaming the
4d592c
tmpfile over the old grubenv (following symlinks).
4d592c
4d592c
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
4d592c
---
4d592c
 util/grub-set-bootflag.c | 87 +++++++++++++++++++++++++++++++++++++++++++-----
4d592c
 1 file changed, 78 insertions(+), 9 deletions(-)
4d592c
4d592c
diff --git a/util/grub-set-bootflag.c b/util/grub-set-bootflag.c
4d592c
index 32f1c104a2b..d3b80a0d204 100644
4d592c
--- a/util/grub-set-bootflag.c
4d592c
+++ b/util/grub-set-bootflag.c
4d592c
@@ -26,7 +26,9 @@
4d592c
 #include <config-util.h>     /* For *_DIR_NAME defines */
4d592c
 #include <grub/types.h>
4d592c
 #include <grub/lib/envblk.h> /* For GRUB_ENVBLK_DEFCFG define */
4d592c
+#include <limits.h>
4d592c
 #include <stdio.h>
4d592c
+#include <stdlib.h>
4d592c
 #include <string.h>
4d592c
 #include <unistd.h>
4d592c
 
4d592c
@@ -52,8 +54,10 @@ int main(int argc, char *argv[])
4d592c
 {
4d592c
   /* NOTE buf must be at least the longest bootflag length + 4 bytes */
4d592c
   char env[GRUBENV_SIZE + 1], buf[64], *s;
4d592c
+  /* +1 for 0 termination, +6 for "XXXXXX" in tmp filename */
4d592c
+  char env_filename[PATH_MAX + 1], tmp_filename[PATH_MAX + 6 + 1];
4d592c
   const char *bootflag;
4d592c
-  int i, len, ret;
4d592c
+  int i, fd, len, ret;
4d592c
   FILE *f;
4d592c
 
4d592c
   if (argc != 2)
4d592c
@@ -75,7 +79,32 @@ int main(int argc, char *argv[])
4d592c
   bootflag = bootflags[i];
4d592c
   len = strlen (bootflag);
4d592c
 
4d592c
-  f = fopen (GRUBENV, "r");
4d592c
+  /*
4d592c
+   * Really become root. setuid avoids an user killing us, possibly leaking
4d592c
+   * the tmpfile. setgid avoids the new grubenv's gid being that of the user.
4d592c
+   */
4d592c
+  ret = setuid(0);
4d592c
+  if (ret)
4d592c
+    {
4d592c
+      perror ("Error setuid(0) failed");
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  ret = setgid(0);
4d592c
+  if (ret)
4d592c
+    {
4d592c
+      perror ("Error setgid(0) failed");
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  /* Canonicalize GRUBENV filename, resolving symlinks, etc. */
4d592c
+  if (!realpath(GRUBENV, env_filename))
4d592c
+    {
4d592c
+      perror ("Error canonicalizing " GRUBENV " filename");
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  f = fopen (env_filename, "r");
4d592c
   if (!f)
4d592c
     {
4d592c
       perror ("Error opening " GRUBENV " for reading");
4d592c
@@ -129,30 +158,70 @@ int main(int argc, char *argv[])
4d592c
   snprintf(buf, sizeof(buf), "%s=1\n", bootflag);
4d592c
   memcpy(s, buf, len + 3);
4d592c
 
4d592c
-  /* "r+", don't truncate so that the diskspace stays reserved */
4d592c
-  f = fopen (GRUBENV, "r+");
4d592c
+
4d592c
+  /*
4d592c
+   * Create a tempfile for writing the new env.  Use the canonicalized filename
4d592c
+   * for the template so that the tmpfile is in the same dir / on same fs.
4d592c
+   */
4d592c
+  snprintf(tmp_filename, sizeof(tmp_filename), "%sXXXXXX", env_filename);
4d592c
+  fd = mkstemp(tmp_filename);
4d592c
+  if (fd == -1)
4d592c
+    {
4d592c
+      perror ("Creating tmpfile failed");
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  f = fdopen (fd, "w");
4d592c
   if (!f)
4d592c
     {
4d592c
-      perror ("Error opening " GRUBENV " for writing");
4d592c
+      perror ("Error fdopen of tmpfile failed");
4d592c
+      unlink(tmp_filename);
4d592c
       return 1;     
4d592c
     }
4d592c
 
4d592c
   ret = fwrite (env, 1, GRUBENV_SIZE, f);
4d592c
   if (ret != GRUBENV_SIZE)
4d592c
     {
4d592c
-      perror ("Error writing to " GRUBENV);
4d592c
+      perror ("Error writing tmpfile");
4d592c
+      unlink(tmp_filename);
4d592c
       return 1;     
4d592c
     }
4d592c
 
4d592c
   ret = fflush (f);
4d592c
   if (ret)
4d592c
     {
4d592c
-      perror ("Error flushing " GRUBENV);
4d592c
+      perror ("Error flushing tmpfile");
4d592c
+      unlink(tmp_filename);
4d592c
       return 1;     
4d592c
     }
4d592c
 
4d592c
-  fsync (fileno (f));
4d592c
-  fclose (f);
4d592c
+  ret = fsync (fileno (f));
4d592c
+  if (ret)
4d592c
+    {
4d592c
+      perror ("Error syncing tmpfile");
4d592c
+      unlink(tmp_filename);
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  ret = fclose (f);
4d592c
+  if (ret)
4d592c
+    {
4d592c
+      perror ("Error closing tmpfile");
4d592c
+      unlink(tmp_filename);
4d592c
+      return 1;
4d592c
+    }
4d592c
+
4d592c
+  /*
4d592c
+   * And finally rename the tmpfile with the new env over the old env, the
4d592c
+   * linux kernel guarantees that this is atomic (from a syscall pov).
4d592c
+   */
4d592c
+  ret = rename(tmp_filename, env_filename);
4d592c
+  if (ret)
4d592c
+    {
4d592c
+      perror ("Error renaming tmpfile to " GRUBENV " failed");
4d592c
+      unlink(tmp_filename);
4d592c
+      return 1;
4d592c
+    }
4d592c
 
4d592c
   return 0;
4d592c
 }