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