-
Notifications
You must be signed in to change notification settings - Fork 1
/
0074-Add-grub-set-bootflag-utility.patch
290 lines (278 loc) · 8.35 KB
/
0074-Add-grub-set-bootflag-utility.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Hans de Goede <[email protected]>
Date: Tue, 12 Jun 2018 13:25:16 +0200
Subject: [PATCH] Add grub-set-bootflag utility
This commit adds a new grub-set-bootflag utility, which can be used
to set known bootflags in the grubenv: boot_success or menu_show_once.
grub-set-bootflag is different from grub-editenv in 2 ways:
1) It is intended to be executed by regular users so must be installed
as suid root. As such it is written to not use any existing grubenv
related code for easy auditing.
It can't be executed through pkexec because we want to call it under gdm
and pkexec does not work under gdm due the gdm user having /sbin/nologin
as shell.
2) Since it can be executed by regular users it only allows setting
(assigning a value of 1 to) bootflags which it knows about. Currently
those are just boot_success and menu_show_once.
This commit also adds a couple of example systemd and files which show
how this can be used to set boot_success from a user-session:
docs/grub-boot-success.service
docs/grub-boot-success.timer
The 2 grub-boot-success.systemd files should be placed in /lib/systemd/user
and a symlink to grub-boot-success.timer should be added to
/lib/systemd/user/timers.target.wants.
Signed-off-by: Hans de Goede <[email protected]>
[makhomed: grub-boot-success.timer: Only run if not in a container]
Signed-off-by: Gena Makhomed <[email protected]>
[rharwood: migrate to h2m]
Signed-off-by: Robbie Harwood <[email protected]>
---
Makefile.util.def | 7 ++
util/grub-set-bootflag.c | 172 +++++++++++++++++++++++++++++++++++++++++
conf/Makefile.extra-dist | 3 +
docs/grub-boot-success.service | 6 ++
docs/grub-boot-success.timer | 7 ++
docs/man/grub-set-bootflag.h2m | 2 +
6 files changed, 197 insertions(+)
create mode 100644 util/grub-set-bootflag.c
create mode 100644 docs/grub-boot-success.service
create mode 100644 docs/grub-boot-success.timer
create mode 100644 docs/man/grub-set-bootflag.h2m
diff --git a/Makefile.util.def b/Makefile.util.def
index 5b9f85d4869..01096ded815 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -1468,3 +1468,10 @@ program = {
ldadd = grub-core/lib/gnulib/libgnu.a;
ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
};
+
+program = {
+ name = grub-set-bootflag;
+ installdir = sbin;
+ mansection = 1;
+ common = util/grub-set-bootflag.c;
+};
diff --git a/util/grub-set-bootflag.c b/util/grub-set-bootflag.c
new file mode 100644
index 00000000000..d506f7e75bc
--- /dev/null
+++ b/util/grub-set-bootflag.c
@@ -0,0 +1,172 @@
+/* grub-set-bootflag.c - tool to set boot-flags in the grubenv. */
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2018 Free Software Foundation, Inc.
+ *
+ * GRUB is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GRUB is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * NOTE this gets run by users as root (through pkexec), so this does not
+ * use any grub library / util functions to allow for easy auditing.
+ * The grub headers are only included to get certain defines.
+ */
+
+#include <config-util.h> /* For *_DIR_NAME defines */
+#include <grub/types.h>
+#include <grub/lib/envblk.h> /* For GRUB_ENVBLK_DEFCFG define */
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "progname.h"
+
+#define GRUBENV "/" GRUB_BOOT_DIR_NAME "/" GRUB_DIR_NAME "/" GRUB_ENVBLK_DEFCFG
+#define GRUBENV_SIZE 1024
+
+const char *bootflags[] = {
+ "boot_success",
+ "menu_show_once",
+ NULL
+};
+
+static void usage(FILE *out)
+{
+ int i;
+
+ fprintf (out, "Usage: 'grub-set-bootflag <bootflag>', where <bootflag> is one of:\n");
+ for (i = 0; bootflags[i]; i++)
+ fprintf (out, " %s\n", bootflags[i]);
+}
+
+int main(int argc, char *argv[])
+{
+ /* NOTE buf must be at least the longest bootflag length + 4 bytes */
+ char env[GRUBENV_SIZE + 1], buf[64], *s;
+ const char *bootflag;
+ int i, len, ret;
+ FILE *f;
+
+ if (argc != 2)
+ {
+ usage (stderr);
+ return 1;
+ }
+ else if (!strcmp (argv[1], "--help"))
+ {
+ usage (stdout);
+ return 0;
+ }
+ else if (!strcmp (argv[1], "--version"))
+ {
+ printf ("grub-set-bootflag (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
+ return 0;
+ }
+
+ for (i = 0; bootflags[i]; i++)
+ if (!strcmp (argv[1], bootflags[i]))
+ break;
+ if (!bootflags[i])
+ {
+ fprintf (stderr, "Invalid bootflag: '%s'\n", argv[1]);
+ usage (stderr);
+ return 1;
+ }
+
+ bootflag = bootflags[i];
+ len = strlen (bootflag);
+
+ f = fopen (GRUBENV, "r");
+ if (!f)
+ {
+ perror ("Error opening " GRUBENV " for reading");
+ return 1;
+ }
+
+ ret = fread (env, 1, GRUBENV_SIZE, f);
+ fclose (f);
+ if (ret != GRUBENV_SIZE)
+ {
+ errno = EINVAL;
+ perror ("Error reading from " GRUBENV);
+ return 1;
+ }
+
+ /* 0 terminate env */
+ env[GRUBENV_SIZE] = 0;
+
+ if (strncmp (env, GRUB_ENVBLK_SIGNATURE, strlen (GRUB_ENVBLK_SIGNATURE)))
+ {
+ fprintf (stderr, "Error invalid environment block\n");
+ return 1;
+ }
+
+ /* Find a pre-existing definition of the bootflag */
+ s = strstr (env, bootflag);
+ while (s && s[len] != '=')
+ s = strstr (s + len, bootflag);
+
+ if (s && ((s[len + 1] != '0' && s[len + 1] != '1') || s[len + 2] != '\n'))
+ {
+ fprintf (stderr, "Pre-existing bootflag '%s' has unexpected value\n", bootflag);
+ return 1;
+ }
+
+ /* No pre-existing bootflag? -> find free space */
+ if (!s)
+ {
+ for (i = 0; i < (len + 3); i++)
+ buf[i] = '#';
+ buf[i] = 0;
+ s = strstr (env, buf);
+ }
+
+ if (!s)
+ {
+ fprintf (stderr, "No space in grubenv to store bootflag '%s'\n", bootflag);
+ return 1;
+ }
+
+ /* The grubenv is not 0 terminated, so memcpy the name + '=' , '1', '\n' */
+ snprintf(buf, sizeof(buf), "%s=1\n", bootflag);
+ memcpy(s, buf, len + 3);
+
+ /* "r+", don't truncate so that the diskspace stays reserved */
+ f = fopen (GRUBENV, "r+");
+ if (!f)
+ {
+ perror ("Error opening " GRUBENV " for writing");
+ return 1;
+ }
+
+ ret = fwrite (env, 1, GRUBENV_SIZE, f);
+ if (ret != GRUBENV_SIZE)
+ {
+ perror ("Error writing to " GRUBENV);
+ return 1;
+ }
+
+ ret = fflush (f);
+ if (ret)
+ {
+ perror ("Error flushing " GRUBENV);
+ return 1;
+ }
+
+ fsync (fileno (f));
+ fclose (f);
+
+ return 0;
+}
diff --git a/conf/Makefile.extra-dist b/conf/Makefile.extra-dist
index 5e7126f9878..26ac8765e30 100644
--- a/conf/Makefile.extra-dist
+++ b/conf/Makefile.extra-dist
@@ -15,6 +15,9 @@ EXTRA_DIST += docs/man
EXTRA_DIST += docs/autoiso.cfg
EXTRA_DIST += docs/grub.cfg
EXTRA_DIST += docs/osdetect.cfg
+EXTRA_DIST += docs/org.gnu.grub.policy
+EXTRA_DIST += docs/grub-boot-success.service
+EXTRA_DIST += docs/grub-boot-success.timer
EXTRA_DIST += conf/i386-cygwin-img-ld.sc
diff --git a/docs/grub-boot-success.service b/docs/grub-boot-success.service
new file mode 100644
index 00000000000..80e79584c91
--- /dev/null
+++ b/docs/grub-boot-success.service
@@ -0,0 +1,6 @@
+[Unit]
+Description=Mark boot as successful
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/grub2-set-bootflag boot_success
diff --git a/docs/grub-boot-success.timer b/docs/grub-boot-success.timer
new file mode 100644
index 00000000000..406f1720056
--- /dev/null
+++ b/docs/grub-boot-success.timer
@@ -0,0 +1,7 @@
+[Unit]
+Description=Mark boot as successful after the user session has run 2 minutes
+ConditionUser=!@system
+ConditionVirtualization=!container
+
+[Timer]
+OnActiveSec=2min
diff --git a/docs/man/grub-set-bootflag.h2m b/docs/man/grub-set-bootflag.h2m
new file mode 100644
index 00000000000..94ec0b92ede
--- /dev/null
+++ b/docs/man/grub-set-bootflag.h2m
@@ -0,0 +1,2 @@
+[NAME]
+grub-set-bootflag \- set a bootflag in the GRUB environment block