All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Chang <MChang@suse.com>
To: "grub-devel@gnu.org" <grub-devel@gnu.org>
Subject: [PATCH] Warn the user to edit environment block by grub-editenv
Date: Tue, 5 Nov 2019 09:20:22 +0000	[thread overview]
Message-ID: <20191105092010.29652-1-mchang@suse.com> (raw)

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 <mchang@suse.com>
---
 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	other threads:[~2019-11-05  9:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-05  9:20 Michael Chang [this message]
2019-11-05 11:49 ` [PATCH] Warn the user to edit environment block by grub-editenv Javier Martinez Canillas
2019-11-07  8:48   ` Michael Chang
2019-11-05 11:58 ` Daniel Kiper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191105092010.29652-1-mchang@suse.com \
    --to=mchang@suse.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.