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

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