]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
grub-editenv: Warn a user against editing environment block
authorMichael Chang <MChang@suse.com>
Tue, 5 Nov 2019 09:20:22 +0000 (09:20 +0000)
committerDaniel Kiper <daniel.kiper@oracle.com>
Mon, 18 Nov 2019 13:19:25 +0000 (14:19 +0100)
The environment block is a preallocated 1024-byte file which serves as
persistent storage for environment variables. It has its own format
which is sensitive to corruption if an editor does not know how to
process it. Besides that the editor may inadvertently change grubenv
file size and/or make it sparse which can lead to unexpected results.

This patch adds a message to the grubenv file to warn a user against
editing it by tools other than grub-editenv.

Signed-off-by: Michael Chang <mchang@suse.com>
Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
util/editenv.c

index eb2d0c03a98f15bda3b3e127b6613261702c5ca8..81f68bd107b3178f679dace61cb03856542fdeb8 100644 (file)
 #include <string.h>
 
 #define DEFAULT_ENVBLK_SIZE    1024
+#define GRUB_ENVBLK_MESSAGE    "# WARNING: Do not edit this file by tools other than "PACKAGE"-editenv!!!\n"
 
 void
 grub_util_create_envblk_file (const char *name)
 {
   FILE *fp;
-  char *buf;
-  char *namenew;
+  char *buf, *pbuf, *namenew;
 
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
 
@@ -46,9 +46,13 @@ grub_util_create_envblk_file (const char *name)
     grub_util_error (_("cannot open `%s': %s"), namenew,
                     strerror (errno));
 
-  memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
-  memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
-          DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
+  pbuf = buf;
+  memcpy (pbuf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
+  pbuf += sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
+  memcpy (pbuf, GRUB_ENVBLK_MESSAGE, sizeof (GRUB_ENVBLK_MESSAGE) - 1);
+  pbuf += sizeof (GRUB_ENVBLK_MESSAGE) - 1;
+  memset (pbuf , '#',
+          DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) - sizeof (GRUB_ENVBLK_MESSAGE) + 2);
 
   if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
     grub_util_error (_("cannot write to `%s': %s"), namenew,