grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Warn the user to edit environment block by grub-editenv


From: Michael Chang
Subject: [PATCH] Warn the user to edit environment block by grub-editenv
Date: Tue, 5 Nov 2019 09:20:22 +0000

The environment block is a preallocated 1024-byte file serves as persistent
storage for environment variables. It has its own format which is sensitive to
corruption if using editor doesn't know how to process it. Besides the editor
may inadvertantly change size allocation that could have it sparse the
filesystem which could lead to unexpected outcome.

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

Signed-off-by: Michael Chang <address@hidden>
---
 util/editenv.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/util/editenv.c b/util/editenv.c
index eb2d0c03a..ee4c9e53f 100644
--- a/util/editenv.c
+++ b/util/editenv.c
@@ -30,12 +30,14 @@
 #include <string.h>
 
 #define DEFAULT_ENVBLK_SIZE    1024
+#define GRUB_ENVBLK_MESSAGE    "# WARNING: Do not edit this file other than by 
"PACKAGE"-editenv\n"
 
 void
 grub_util_create_envblk_file (const char *name)
 {
   FILE *fp;
   char *buf;
+  char *pbuf;
   char *namenew;
 
   buf = xmalloc (DEFAULT_ENVBLK_SIZE);
@@ -46,9 +48,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,
-- 
2.16.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]