Skip to contentSkip to conversation

Add grub_util_readlink()

PATCH Superseded Javier Martinez Canillas 4 participants 7 messages Oct 30, 2019 – Dec 13, 2019 Kbuild

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Add grub_util_readlink()

From: Peter Jones <[email protected]>

Currently grub-editenv and related tools are not able to follow symbolic links when finding their config file. For example the grub-editenv create command will wrongly overwrite a symlink in /boot/grub2/grubenv with a new regular file, instead of creating a file in the path the symlink points to.

A following patch will change that and add support in grub-editenv to follow symbolic links when finding the grub environment variables file.

Add a grub_util_readlink() helper function that is just a wrapper around the platform specific function to read the value of a symbolic link. This helper function will be used by the following patch for grub-editenv.

---

Changes in v3: None
Changes in v2:
- Add a better commit message explaining why the change is needed.
- Add Reviewed-by tag from Daniel Kiper.

 grub-core/osdep/windows/hostdisk.c    | 6 ++++++
 include/grub/osdep/hostfile_aros.h    | 6 ++++++
 include/grub/osdep/hostfile_unix.h    | 6 ++++++
 include/grub/osdep/hostfile_windows.h | 2 ++
 4 files changed, 20 insertions(+)
osdep/windows/hostdisk.c +6 -0
index 355100789a7..87a106c9b82 100644
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {
grub/osdep/hostfile_aros.h +6 -0
index a059c0fa40a..161fbb7bdfd 100644
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
grub/osdep/hostfile_unix.h +6 -0
index 9ffe46fa3ca..17cd3aa8b30 100644
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
grub/osdep/hostfile_windows.h +2 -0
index bf6451b6db4..8c92d0591bb 100644
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void
Signature · 1 line
2.21.0
#1 lore raw

PATCH v3 2/2 Make editenv chase symlinks including those across devices

From: Peter Jones <[email protected]>

The grub-editenv create command will wrongly overwrite /boot/grub2/grubenv with a regular file if grubenv is a symbolic link. But instead, it should create a new file in the path the symlink points to.

This lets /boot/grub2/grubenv be a symlink to /boot/efi/EFI/fedora/grubenv even when they're different mount points, which allows grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

For example, in Fedora the GRUB EFI builds have prefix set to /EFI/fedora (on the EFI System Partition), but for BIOS machine it'll be /boot/grub2 (which may or may not be its own mountpoint).

With this patch, on EFI machines we can make /boot/grub2/grubenv a symlink to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default will work on both kinds of systems.

---
Daniel,

I didn't add a check for x* allocation functions since as mentioned in my previous email these are not needed. The functions already check for NULL and call exit(3) if that's the case.

Changes in v3:
- A couple of style cleanups.

Changes in v2:
- Address issues pointed out by Daniel Kiper in the previous version.

 Makefile.util.def | 11 +++++++++
 util/editenv.c    | 59 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 2 deletions(-)
Makefile.util.def +11 -0
index 969d32f0097..733a397cb2b 100644
@@ -240,8 +240,19 @@ program = {
 
   common = util/grub-editenv.c;
   common = util/editenv.c;
+  common = util/grub-install-common.c;
   common = grub-core/osdep/init.c;
+  common = grub-core/osdep/compress.c;
+  extra_dist = grub-core/osdep/unix/compress.c;
+  extra_dist = grub-core/osdep/basic/compress.c;
+  common = util/mkimage.c;
+  common = util/grub-mkimage32.c;
+  common = util/grub-mkimage64.c;
+  common = grub-core/osdep/config.c;
+  common = util/config.c;
+  common = util/resolve.c;
 
+  ldadd = '$(LIBLZMA)';
   ldadd = libgrubmods.a;
   ldadd = libgrubgcry.a;
   ldadd = libgrubkern.a;
editenv.c +57 -2

79 lines not on this page · raw message

Signature · 1 line
2.21.0
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] Make editenv chase symlinks including those across devices

Quoted 14 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

The grub-editenv create command will wrongly overwrite /boot/grub2/grubenv with a regular file if grubenv is a symbolic link. But instead, it should create a new file in the path the symlink points to.

This lets /boot/grub2/grubenv be a symlink to /boot/efi/EFI/fedora/grubenv even when they're different mount points, which allows grub2-editenv to be the same across platforms (i.e. UEFI vs BIOS).

For example, in Fedora the GRUB EFI builds have prefix set to /EFI/fedora (on the EFI System Partition), but for BIOS machine it'll be /boot/grub2 (which may or may not be its own mountpoint).

Quoted 8 lines

With this patch, on EFI machines we can make /boot/grub2/grubenv a symlink to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default will work on both kinds of systems.

Quoted 6 lines

---
Daniel,

I didn't add a check for x* allocation functions since as mentioned in my previous email these are not needed. The functions already check for NULL and call exit(3) if that's the case.

Argh... This reminds me that I was asking somebody else about that and got the same reply. So, I have to get used to the x* allocation functions behavior. Sorry for the confusion.

Daniel

#3 lore raw
In reply to Javier Martinez Canillas

This patch LGTM

Quoted 109 lines · On Wed, 30 Oct 2019, 11:49 Javier Martinez Canillas, <[email protected]> wrote:

From: Peter Jones <[email protected]>

Currently grub-editenv and related tools are not able to follow symbolic links when finding their config file. For example the grub-editenv create command will wrongly overwrite a symlink in /boot/grub2/grubenv with a new regular file, instead of creating a file in the path the symlink points to.

A following patch will change that and add support in grub-editenv to follow symbolic links when finding the grub environment variables file.

Add a grub_util_readlink() helper function that is just a wrapper around the platform specific function to read the value of a symbolic link. This helper function will be used by the following patch for grub-editenv.

---

Changes in v3: None
Changes in v2:
- Add a better commit message explaining why the change is needed.
- Add Reviewed-by tag from Daniel Kiper.

 grub-core/osdep/windows/hostdisk.c    | 6 ++++++
 include/grub/osdep/hostfile_aros.h    | 6 ++++++
 include/grub/osdep/hostfile_unix.h    | 6 ++++++
 include/grub/osdep/hostfile_windows.h | 2 ++
 4 files changed, 20 insertions(+)
(quoted diff) +0 -0

grub-core/osdep/windows/hostdisk.c
index 355100789a7..87a106c9b82 100644

osdep/windows/hostdisk.c +6 -0
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {
(quoted diff) +0 -0

include/grub/osdep/hostfile_aros.h
index a059c0fa40a..161fbb7bdfd 100644

grub/osdep/hostfile_aros.h +6 -0
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
(quoted diff) +0 -0

include/grub/osdep/hostfile_unix.h
index 9ffe46fa3ca..17cd3aa8b30 100644

grub/osdep/hostfile_unix.h +6 -0
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
(quoted diff) +0 -0

include/grub/osdep/hostfile_windows.h
index bf6451b6db4..8c92d0591bb 100644

grub/osdep/hostfile_windows.h +2 -1
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void
--

2.21.0

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
In reply to Javier Martinez Canillas
Quoted 39 lines · Javier Martinez Canillas

From: Peter Jones <[email protected]>

Currently grub-editenv and related tools are not able to follow symbolic links when finding their config file. For example the grub-editenv create command will wrongly overwrite a symlink in /boot/grub2/grubenv with a new regular file, instead of creating a file in the path the symlink points to.

A following patch will change that and add support in grub-editenv to follow symbolic links when finding the grub environment variables file.

Add a grub_util_readlink() helper function that is just a wrapper around the platform specific function to read the value of a symbolic link. This helper function will be used by the following patch for grub-editenv.

---

Changes in v3: None
Changes in v2:
- Add a better commit message explaining why the change is needed.
- Add Reviewed-by tag from Daniel Kiper.

 grub-core/osdep/windows/hostdisk.c    | 6 ++++++
 include/grub/osdep/hostfile_aros.h    | 6 ++++++
 include/grub/osdep/hostfile_unix.h    | 6 ++++++
 include/grub/osdep/hostfile_windows.h | 2 ++
 4 files changed, 20 insertions(+)
osdep/windows/hostdisk.c +0 -0
index 355100789a7..87a106c9b82 100644
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }

osdep/windows/hostdisk.c grub_util_mkdir (const char *dir) · line 376

Quoted 10 lines
osdep/windows/hostdisk.c +6 -0
 
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {

This change breaks Windows build. I think that you should drop it and do some #ifdefery in patch #2. I mean I would leave existing code for Windows only and add new code for POSIX compliant systems.

Quoted 39 lines
grub/osdep/hostfile_aros.h +6 -0
index a059c0fa40a..161fbb7bdfd 100644
@@ -68,6 +68,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 struct grub_util_fd
grub/osdep/hostfile_unix.h +6 -0
index 9ffe46fa3ca..17cd3aa8b30 100644
@@ -71,6 +71,12 @@ grub_util_rename (const char *from, const char *to)
   return rename (from, to);
 }
 
+static inline ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 #define grub_util_mkdir(a) mkdir ((a), 0755)
 
 #if defined (__NetBSD__)
grub/osdep/hostfile_windows.h +0 -0
index bf6451b6db4..8c92d0591bb 100644

grub/osdep/hostfile_windows.h typedef struct grub_util_fd_dir *grub_util_fd_dir_t; · line 48

Quoted 8 lines
grub/osdep/hostfile_windows.h +2 -0
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void

This change is also probably not needed then...

Daniel

#5 lore raw

Hello Daniel,

On 11/5/19 12:42 PM, Daniel Kiper wrote:

[snip]

Quoted 6 lines
Quoted 6 lines
osdep/windows/hostdisk.c +0 -0
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
Quoted 14 lines

osdep/windows/hostdisk.c grub_util_mkdir (const char *dir) · line 376

Quoted 9 lines
osdep/windows/hostdisk.c +6 -0
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {

This change breaks Windows build. I think that you should drop it and do some #ifdefery in patch #2. I mean I would leave existing code for Windows only and add new code for POSIX compliant systems.

Ok, will do.

[snip]

Quoted 7 lines
Quoted 7 lines

#if defined (__NetBSD__)

grub/osdep/hostfile_windows.h +0 -0
index bf6451b6db4..8c92d0591bb 100644
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
Quoted 10 lines

grub/osdep/hostfile_windows.h typedef struct grub_util_fd_dir *grub_util_fd_dir_t; · line 48

Quoted 7 lines
grub/osdep/hostfile_windows.h +2 -0
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void

This change is also probably not needed then...

Do we really need this grub_util_readlink() indirection level then? Can the #ifdef code for POSIX just use readlink(2) directly?

Best regards,

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

Hello Daniel,

On 11/5/19 12:42 PM, Daniel Kiper wrote:

[snip]

Quoted 6 lines
Quoted 6 lines
osdep/windows/hostdisk.c +0 -0
@@ -365,6 +365,12 @@ grub_util_mkdir (const char *dir)
   free (windows_name);
 }
 
Quoted 14 lines

osdep/windows/hostdisk.c grub_util_mkdir (const char *dir) · line 376

Quoted 9 lines
osdep/windows/hostdisk.c +6 -0
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize)
+{
+  return readlink(name, buf, bufsize);
+}
+
 int
 grub_util_rename (const char *from, const char *to)
 {

This change breaks Windows build. I think that you should drop it and do some #ifdefery in patch #2. I mean I would leave existing code for Windows only and add new code for POSIX compliant systems.

Ok, will do.

[snip]

Quoted 7 lines
Quoted 7 lines

#if defined (__NetBSD__)

grub/osdep/hostfile_windows.h +0 -0
index bf6451b6db4..8c92d0591bb 100644
@@ -41,6 +41,8 @@ typedef struct grub_util_fd_dir *grub_util_fd_dir_t;
 
Quoted 10 lines

grub/osdep/hostfile_windows.h typedef struct grub_util_fd_dir *grub_util_fd_dir_t; · line 48

Quoted 7 lines
grub/osdep/hostfile_windows.h +2 -0
 int
 grub_util_rename (const char *from, const char *to);
+ssize_t
+grub_util_readlink (const char *name, char *buf, size_t bufsize);
 int
 grub_util_unlink (const char *name);
 void

This change is also probably not needed then...

Quoted 2 lines

Do we really need this grub_util_readlink() indirection level then? Can the #ifdef code for POSIX just use readlink(2) directly?

I think that yes/no because after dropping Windows code you still have two variants. So, I would leave this grub_util_readlink() indirection as is.

Daniel