Skip to contentSkip to conversation

Warn the user to edit environment block by grub-editenv

PATCH In review Michael Chang 3 participants 4 messages Nov 5, 2019 – Nov 7, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Warn the user to edit environment block by grub-editenv

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.

---
 util/editenv.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
util/editenv.c +9 -3
index eb2d0c03a..ee4c9e53f 100644
@@ -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,
Signature · 1 line
2.16.4
Reviewed-by on v1 1/1
#1 lore raw

Hello Michael,

Quoted 6 lines · Michael Chang

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.

Yes, we also get reports about this issue: https://bugzilla.redhat.com/show_bug.cgi?id=1625124

Quoted 3 lines

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

Agreed that users should be warned that the file should only be edited with the GRUB tools.

I also wonder if grub-editenv can't be made more robust and correct a grubenv file (i.e: filling the missing # characters / removing any extra # character) if is found to be corrupted and not having the expected size.

Quoted 5 lines
---
 util/editenv.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

The patch looks good to me.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
#2 lore raw
Quoted 16 lines · Javier Martinez Canillas

Hello Michael,

Quoted 6 lines · Michael Chang

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.

Yes, we also get reports about this issue: https://bugzilla.redhat.com/show_bug.cgi?id=1625124

Quoted 3 lines

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

Quoted 6 lines

Agreed that users should be warned that the file should only be edited with the GRUB tools.

I also wonder if grub-editenv can't be made more robust and correct a grubenv file (i.e: filling the missing # characters / removing any extra # character) if is found to be corrupted and not having the expected size.

IMHO when people trying to read grubenv file they didn't expect the sanitization from happening, as that might be troublesome to describe problem if anyone wants to troubleshoot issue related to malformed grubenv which is just victim of filesystem corruption and like.

I personally wouldn't mind if the function is provided as separate command and backup original file somewhere ..

Quoted 10 lines
Quoted 6 lines
---
 util/editenv.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

The patch looks good to me.

Thanks a lot for your feedback and review. :)

Regards,
Michael

Quoted 6 lines

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat
Reviewed-by on v1 1/1
#3 lore raw
In reply to Michael Chang
Quoted 10 lines · Michael Chang

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.

Daniel