nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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