nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0082-Add-grub-set-bootflag-utility.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Hans de Goede <hdegoede@redhat.com>
8e15ce
Date: Tue, 12 Jun 2018 13:25:16 +0200
8e15ce
Subject: [PATCH] Add grub-set-bootflag utility
8e15ce
8e15ce
This commit adds a new grub-set-bootflag utility, which can be used
8e15ce
to set known bootflags in the grubenv: boot_success or menu_show_once.
8e15ce
8e15ce
grub-set-bootflag is different from grub-editenv in 2 ways:
8e15ce
8e15ce
1) It is intended to be executed by regular users so must be installed
8e15ce
as suid root. As such it is written to not use any existing grubenv
8e15ce
related code for easy auditing.
8e15ce
8e15ce
It can't be executed through pkexec because we want to call it under gdm
8e15ce
and pkexec does not work under gdm due the gdm user having /sbin/nologin
8e15ce
as shell.
8e15ce
8e15ce
2) Since it can be executed by regular users it only allows setting
8e15ce
(assigning a value of 1 to) bootflags which it knows about. Currently
8e15ce
those are just boot_success and menu_show_once.
8e15ce
8e15ce
This commit also adds a couple of example systemd and files which show
8e15ce
how this can be used to set boot_success from a user-session:
8e15ce
8e15ce
docs/grub-boot-success.service
8e15ce
docs/grub-boot-success.timer
8e15ce
8e15ce
The 2 grub-boot-success.systemd files should be placed in /lib/systemd/user
8e15ce
and a symlink to grub-boot-success.timer should be added to
8e15ce
/lib/systemd/user/timers.target.wants.
8e15ce
8e15ce
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
b35c50
[makhomed: grub-boot-success.timer: Only run if not in a container]
b35c50
Signed-off-by: Gena Makhomed <makhomed@gmail.com>
b35c50
[rharwood: migrate to h2m]
b35c50
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
8e15ce
---
8e15ce
 Makefile.util.def              |   7 ++
b35c50
 util/grub-set-bootflag.c       | 172 +++++++++++++++++++++++++++++++++++++++++
8e15ce
 conf/Makefile.extra-dist       |   3 +
8e15ce
 docs/grub-boot-success.service |   6 ++
b35c50
 docs/grub-boot-success.timer   |   7 ++
b35c50
 docs/man/grub-set-bootflag.h2m |   2 +
b35c50
 6 files changed, 197 insertions(+)
8e15ce
 create mode 100644 util/grub-set-bootflag.c
8e15ce
 create mode 100644 docs/grub-boot-success.service
8e15ce
 create mode 100644 docs/grub-boot-success.timer
b35c50
 create mode 100644 docs/man/grub-set-bootflag.h2m
8e15ce
8e15ce
diff --git a/Makefile.util.def b/Makefile.util.def
b35c50
index cb8e3c3270..d066652e9b 100644
8e15ce
--- a/Makefile.util.def
8e15ce
+++ b/Makefile.util.def
b35c50
@@ -1429,3 +1429,10 @@ program = {
8e15ce
   ldadd = grub-core/lib/gnulib/libgnu.a;
8e15ce
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
8e15ce
 };
8e15ce
+
8e15ce
+program = {
8e15ce
+  name = grub-set-bootflag;
8e15ce
+  installdir = sbin;
8e15ce
+  mansection = 1;
8e15ce
+  common = util/grub-set-bootflag.c;
8e15ce
+};
8e15ce
diff --git a/util/grub-set-bootflag.c b/util/grub-set-bootflag.c
8e15ce
new file mode 100644
b35c50
index 0000000000..d506f7e75b
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-set-bootflag.c
b35c50
@@ -0,0 +1,172 @@
8e15ce
+/* grub-set-bootflag.c - tool to set boot-flags in the grubenv. */
8e15ce
+/*
8e15ce
+ *  GRUB  --  GRand Unified Bootloader
8e15ce
+ *  Copyright (C) 2018 Free Software Foundation, Inc.
8e15ce
+ *
8e15ce
+ *  GRUB is free software: you can redistribute it and/or modify
8e15ce
+ *  it under the terms of the GNU General Public License as published by
8e15ce
+ *  the Free Software Foundation, either version 3 of the License, or
8e15ce
+ *  (at your option) any later version.
8e15ce
+ *
8e15ce
+ *  GRUB is distributed in the hope that it will be useful,
8e15ce
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8e15ce
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8e15ce
+ *  GNU General Public License for more details.
8e15ce
+ *
8e15ce
+ *  You should have received a copy of the GNU General Public License
8e15ce
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8e15ce
+ */
8e15ce
+
8e15ce
+/*
8e15ce
+ * NOTE this gets run by users as root (through pkexec), so this does not
8e15ce
+ * use any grub library / util functions to allow for easy auditing.
8e15ce
+ * The grub headers are only included to get certain defines.
8e15ce
+ */
8e15ce
+
8e15ce
+#include <config-util.h>     /* For *_DIR_NAME defines */
8e15ce
+#include <grub/types.h>
8e15ce
+#include <grub/lib/envblk.h> /* For GRUB_ENVBLK_DEFCFG define */
8e15ce
+#include <errno.h>
8e15ce
+#include <stdio.h>
8e15ce
+#include <string.h>
8e15ce
+#include <unistd.h>
8e15ce
+
b35c50
+#include "progname.h"
b35c50
+
8e15ce
+#define GRUBENV "/" GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME "/" GRUB_ENVBLK_DEFCFG
8e15ce
+#define GRUBENV_SIZE 1024
8e15ce
+
8e15ce
+const char *bootflags[] = {
8e15ce
+  "boot_success",
8e15ce
+  "menu_show_once",
8e15ce
+  NULL
8e15ce
+};
8e15ce
+
b35c50
+static void usage(FILE *out)
8e15ce
+{
8e15ce
+  int i;
8e15ce
+
b35c50
+  fprintf (out, "Usage: 'grub-set-bootflag <bootflag>', where <bootflag> is one of:\n");
8e15ce
+  for (i = 0; bootflags[i]; i++)
b35c50
+    fprintf (out, "  %s\n", bootflags[i]);
8e15ce
+}
8e15ce
+
8e15ce
+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
+  const char *bootflag;
8e15ce
+  int i, len, ret;
8e15ce
+  FILE *f;
8e15ce
+
8e15ce
+  if (argc != 2)
8e15ce
+    {
b35c50
+      usage (stderr);
8e15ce
+      return 1;
8e15ce
+    }
b35c50
+  else if (!strcmp (argv[1], "--help"))
b35c50
+    {
b35c50
+      usage (stdout);
b35c50
+      return 0;
b35c50
+    }
b35c50
+  else if (!strcmp (argv[1], "--version"))
b35c50
+    {
b35c50
+      printf ("grub-set-bootflag (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
b35c50
+      return 0;
b35c50
+    }
8e15ce
+
8e15ce
+  for (i = 0; bootflags[i]; i++)
8e15ce
+    if (!strcmp (argv[1], bootflags[i]))
8e15ce
+      break;
8e15ce
+  if (!bootflags[i])
8e15ce
+    {
8e15ce
+      fprintf (stderr, "Invalid bootflag: '%s'\n", argv[1]);
b35c50
+      usage (stderr);
8e15ce
+      return 1;
8e15ce
+    }
8e15ce
+
8e15ce
+  bootflag = bootflags[i];
8e15ce
+  len = strlen (bootflag);
8e15ce
+
8e15ce
+  f = fopen (GRUBENV, "r");
8e15ce
+  if (!f)
8e15ce
+    {
8e15ce
+      perror ("Error opening " GRUBENV " for reading");
8e15ce
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  ret = fread (env, 1, GRUBENV_SIZE, f);
8e15ce
+  fclose (f);
8e15ce
+  if (ret != GRUBENV_SIZE)
8e15ce
+    {
8e15ce
+      errno = EINVAL;
8e15ce
+      perror ("Error reading from " GRUBENV);
8e15ce
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  /* 0 terminate env */
8e15ce
+  env[GRUBENV_SIZE] = 0;
8e15ce
+
8e15ce
+  if (strncmp (env, GRUB_ENVBLK_SIGNATURE, strlen (GRUB_ENVBLK_SIGNATURE)))
8e15ce
+    {
8e15ce
+      fprintf (stderr, "Error invalid environment block\n");
8e15ce
+      return 1;
8e15ce
+    }
8e15ce
+
8e15ce
+  /* Find a pre-existing definition of the bootflag */
8e15ce
+  s = strstr (env, bootflag);
8e15ce
+  while (s && s[len] != '=')
8e15ce
+    s = strstr (s + len, bootflag);
8e15ce
+
8e15ce
+  if (s && ((s[len + 1] != '0' && s[len + 1] != '1') || s[len + 2] != '\n'))
8e15ce
+    {
8e15ce
+      fprintf (stderr, "Pre-existing bootflag '%s' has unexpected value\n", bootflag);
8e15ce
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  /* No pre-existing bootflag? -> find free space */
8e15ce
+  if (!s)
8e15ce
+    {
8e15ce
+      for (i = 0; i < (len + 3); i++)
8e15ce
+        buf[i] = '#';
8e15ce
+      buf[i] = 0;
8e15ce
+      s = strstr (env, buf);
8e15ce
+    }
8e15ce
+
8e15ce
+  if (!s)
8e15ce
+    {
8e15ce
+      fprintf (stderr, "No space in grubenv to store bootflag '%s'\n", bootflag);
8e15ce
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  /* The grubenv is not 0 terminated, so memcpy the name + '=' , '1', '\n' */
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
+  if (!f)
8e15ce
+    {
8e15ce
+      perror ("Error opening " GRUBENV " for writing");
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
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  ret = fflush (f);
8e15ce
+  if (ret)
8e15ce
+    {
8e15ce
+      perror ("Error flushing " GRUBENV);
8e15ce
+      return 1;     
8e15ce
+    }
8e15ce
+
8e15ce
+  fsync (fileno (f));
8e15ce
+  fclose (f);
8e15ce
+
8e15ce
+  return 0;
8e15ce
+}
8e15ce
diff --git a/conf/Makefile.extra-dist b/conf/Makefile.extra-dist
b35c50
index 8f1485d52a..ad235de7fc 100644
8e15ce
--- a/conf/Makefile.extra-dist
8e15ce
+++ b/conf/Makefile.extra-dist
b35c50
@@ -15,6 +15,9 @@ EXTRA_DIST += docs/man
8e15ce
 EXTRA_DIST += docs/autoiso.cfg
8e15ce
 EXTRA_DIST += docs/grub.cfg
8e15ce
 EXTRA_DIST += docs/osdetect.cfg
8e15ce
+EXTRA_DIST += docs/org.gnu.grub.policy
8e15ce
+EXTRA_DIST += docs/grub-boot-success.service
8e15ce
+EXTRA_DIST += docs/grub-boot-success.timer
8e15ce
 
8e15ce
 EXTRA_DIST += conf/i386-cygwin-img-ld.sc
8e15ce
 
8e15ce
diff --git a/docs/grub-boot-success.service b/docs/grub-boot-success.service
8e15ce
new file mode 100644
b35c50
index 0000000000..80e79584c9
8e15ce
--- /dev/null
8e15ce
+++ b/docs/grub-boot-success.service
8e15ce
@@ -0,0 +1,6 @@
8e15ce
+[Unit]
8e15ce
+Description=Mark boot as successful
8e15ce
+
8e15ce
+[Service]
8e15ce
+Type=oneshot
8e15ce
+ExecStart=/usr/sbin/grub2-set-bootflag boot_success
8e15ce
diff --git a/docs/grub-boot-success.timer b/docs/grub-boot-success.timer
8e15ce
new file mode 100644
b35c50
index 0000000000..406f172005
8e15ce
--- /dev/null
8e15ce
+++ b/docs/grub-boot-success.timer
b35c50
@@ -0,0 +1,7 @@
8e15ce
+[Unit]
8e15ce
+Description=Mark boot as successful after the user session has run 2 minutes
8e15ce
+ConditionUser=!@system
b35c50
+ConditionVirtualization=!container
8e15ce
+
8e15ce
+[Timer]
8e15ce
+OnActiveSec=2min
b35c50
diff --git a/docs/man/grub-set-bootflag.h2m b/docs/man/grub-set-bootflag.h2m
8e15ce
new file mode 100644
b35c50
index 0000000000..94ec0b92ed
8e15ce
--- /dev/null
b35c50
+++ b/docs/man/grub-set-bootflag.h2m
b35c50
@@ -0,0 +1,2 @@
b35c50
+[NAME]
b35c50
+grub-set-bootflag \- set a bootflag in the GRUB environment block