Blame SOURCES/0099-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>
8e15ce
---
8e15ce
 Makefile.util.def              |   7 ++
8e15ce
 util/grub-set-bootflag.c       | 160 +++++++++++++++++++++++++++++++++++++++++
8e15ce
 conf/Makefile.extra-dist       |   3 +
8e15ce
 docs/grub-boot-success.service |   6 ++
8e15ce
 docs/grub-boot-success.timer   |   6 ++
8e15ce
 util/grub-set-bootflag.1       |  20 ++++++
8e15ce
 6 files changed, 202 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
8e15ce
 create mode 100644 util/grub-set-bootflag.1
8e15ce
8e15ce
diff --git a/Makefile.util.def b/Makefile.util.def
8e15ce
index 04551e095bd..c6375933faa 100644
8e15ce
--- a/Makefile.util.def
8e15ce
+++ b/Makefile.util.def
8e15ce
@@ -1445,3 +1445,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
8e15ce
index 00000000000..bb198f02351
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-set-bootflag.c
8e15ce
@@ -0,0 +1,160 @@
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
+
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
+
8e15ce
+static void usage(void)
8e15ce
+{
8e15ce
+  int i;
8e15ce
+
8e15ce
+  fprintf (stderr, "Usage: 'grub-set-bootflag <bootflag>', where <bootflag> is one of:\n");
8e15ce
+  for (i = 0; bootflags[i]; i++)
8e15ce
+    fprintf (stderr, "  %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
+    {
8e15ce
+      usage();
8e15ce
+      return 1;
8e15ce
+    }
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]);
8e15ce
+      usage();
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
8e15ce
index b909f2c073a..ea58362b555 100644
8e15ce
--- a/conf/Makefile.extra-dist
8e15ce
+++ b/conf/Makefile.extra-dist
8e15ce
@@ -14,6 +14,9 @@ EXTRA_DIST += util/import_unicode.py
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
8e15ce
index 00000000000..80e79584c91
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
8e15ce
index 00000000000..5d8fcba21aa
8e15ce
--- /dev/null
8e15ce
+++ b/docs/grub-boot-success.timer
8e15ce
@@ -0,0 +1,6 @@
8e15ce
+[Unit]
8e15ce
+Description=Mark boot as successful after the user session has run 2 minutes
8e15ce
+ConditionUser=!@system
8e15ce
+
8e15ce
+[Timer]
8e15ce
+OnActiveSec=2min
8e15ce
diff --git a/util/grub-set-bootflag.1 b/util/grub-set-bootflag.1
8e15ce
new file mode 100644
8e15ce
index 00000000000..57801da22a0
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub-set-bootflag.1
8e15ce
@@ -0,0 +1,20 @@
8e15ce
+.TH GRUB-SET-BOOTFLAG 1 "Tue Jun 12 2018"
8e15ce
+.SH NAME
8e15ce
+\fBgrub-set-bootflag\fR \(em Set a bootflag in the GRUB environment block.
8e15ce
+
8e15ce
+.SH SYNOPSIS
8e15ce
+\fBgrub-set-bootflag\fR <\fIBOOTFLAG\fR>
8e15ce
+
8e15ce
+.SH DESCRIPTION
8e15ce
+\fBgrub-set-bootflag\fR is a command line to set bootflags in GRUB's
8e15ce
+stored environment.
8e15ce
+
8e15ce
+.SH COMMANDS
8e15ce
+.TP
8e15ce
+\fBBOOTFLAG\fR
8e15ce
+.RS 7
8e15ce
+Bootflag to set, one of \fIboot_success\fR or \fIshow_menu_once\fR.
8e15ce
+.RE
8e15ce
+
8e15ce
+.SH SEE ALSO
8e15ce
+.BR "info grub"