Skip to contentSkip to conversation

i386-pc: build verifiers API as module

PATCH Superseded Michael Chang 2 participants 3 messages Mar 18, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386-pc: build verifiers API as module

Given no core functions on i386-pc would require verifiers to work and the only consumer of the verifier API is the pgp module, it looks good to me that we can move the verifiers out of the kernel image and let moddep.lst to auto-load it when pgp is loaded on i386-pc platform.

This helps to reduce the size of core image and thus can relax the tension of exploading on some i386-pc system with very short MBR gap size. See also a very comprehensive summary from Colin [1] about the details.

[1] https://lists.gnu.org/archive/html/grub-devel/2021-03/msg00240.html

---
 configure.ac                |  1 +
 grub-core/Makefile.am       |  2 ++
 grub-core/Makefile.core.def |  8 +++++++-
 grub-core/kern/main.c       |  4 ++++
 grub-core/kern/verifiers.c  | 11 +++++++++++
 include/grub/verify.h       |  9 +++++++++
 6 files changed, 34 insertions(+), 1 deletion(-)
configure.ac +1 -0
index 74719416c..bbb91e80b 100644
@@ -1970,6 +1970,7 @@ AC_SUBST(BUILD_LIBM)
 AM_CONDITIONAL([COND_real_platform], [test x$platform != xnone])
 AM_CONDITIONAL([COND_emu], [test x$platform = xemu])
 AM_CONDITIONAL([COND_i386_pc], [test x$target_cpu = xi386 -a x$platform = xpc])
+AM_CONDITIONAL([COND_NOT_i386_pc], [test x$target_cpu != xi386 -o x$platform != xpc])
 AM_CONDITIONAL([COND_i386_efi], [test x$target_cpu = xi386 -a x$platform = xefi])
 AM_CONDITIONAL([COND_ia64_efi], [test x$target_cpu = xia64 -a x$platform = xefi])
 AM_CONDITIONAL([COND_i386_qemu], [test x$target_cpu = xi386 -a x$platform = xqemu])
grub-core/Makefile.am +2 -0
index ee88e44e9..fca0df9de 100644
@@ -93,7 +93,9 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
+if COND_NOT_i386_pc
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
+endif
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
grub-core/Makefile.core.def +7 -1
index 8022e1c0a..77fdccdb1 100644
@@ -141,7 +141,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
-  common = kern/verifiers.c;
+  nopc = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
@@ -946,6 +946,12 @@ module = {
   cppflags = '-I$(srcdir)/lib/posix_wrap';
 };
 
+module = {
+  name = verifiers;
+  common = kern/verifiers.c;
+  enable = i386_pc;
+};
+
 module = {
   name = hdparm;
   common = commands/hdparm.c;
grub-core/kern/main.c +4 -0
index 73967e2f5..c7c6d2d0b 100644
@@ -29,7 +29,9 @@
 #include <grub/command.h>
 #include <grub/reader.h>
 #include <grub/parser.h>
+#ifndef GRUB_MACHINE_PCBIOS
 #include <grub/verify.h>
+#endif
 
 #ifdef GRUB_MACHINE_PCBIOS
 #include <grub/machine/memory.h>
@@ -275,8 +277,10 @@ grub_main (void)
   grub_printf ("Welcome to GRUB!\n\n");
   grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
+#ifndef GRUB_MACHINE_PCBIOS
   /* Init verifiers API. */
   grub_verifiers_init ();
+#endif
 
   grub_load_config ();
 
grub-core/kern/verifiers.c +11 -0
index 75d7994cf..85887917d 100644
@@ -221,8 +221,19 @@ grub_verify_string (char *str, enum grub_verify_string_type type)
   return GRUB_ERR_NONE;
 }
 
+#ifdef GRUB_MACHINE_PCBIOS
+GRUB_MOD_INIT(verifiers)
+#else
 void
 grub_verifiers_init (void)
+#endif
 {
   grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verifiers_open);
 }
+
+#ifdef GRUB_MACHINE_PCBIOS
+GRUB_MOD_FINI(verifiers)
+{
+  grub_file_filter_unregister (GRUB_FILE_FILTER_VERIFY);
+}
+#endif
include/grub/verify.h +9 -0
index cd129c398..6fde244fc 100644
@@ -64,10 +64,14 @@ struct grub_file_verifier
   grub_err_t (*verify_string) (char *str, enum grub_verify_string_type type);
 };
 
+#ifdef GRUB_MACHINE_PCBIOS
+extern struct grub_file_verifier *grub_file_verifiers;
+#else
 extern struct grub_file_verifier *EXPORT_VAR (grub_file_verifiers);
 
 extern void
 grub_verifiers_init (void);
+#endif
 
 static inline void
 grub_verifier_register (struct grub_file_verifier *ver)
@@ -81,7 +85,12 @@ grub_verifier_unregister (struct grub_file_verifier *ver)
   grub_list_remove (GRUB_AS_LIST (ver));
 }
 
+#ifdef GRUB_MACHINE_PCBIOS
+grub_err_t
+grub_verify_string (char *str, enum grub_verify_string_type type);
+#else
 extern grub_err_t
 EXPORT_FUNC (grub_verify_string) (char *str, enum grub_verify_string_type type);
+#endif
 
 #endif /* ! GRUB_VERIFY_HEADER */
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Michael Chang via Grub-devel

Given no core functions on i386-pc would require verifiers to work and the only consumer of the verifier API is the pgp module, it looks good to me that we can move the verifiers out of the kernel image and let moddep.lst to auto-load it when pgp is loaded on i386-pc platform.

This helps to reduce the size of core image and thus can relax the tension of exploading on some i386-pc system with very short MBR gap size. See also a very comprehensive summary from Colin [1] about the details.

Thanks for working on this! It's certainly awkward to have to deal with this sort of thing, but apparently not as awkward as I'd feared, and it's better than the alternative.

Quoted 1 line

+AM_CONDITIONAL([COND_NOT_i386_pc], [test x$target_cpu != xi386 -o x$platform != xpc])

You could drop this and instead just do "if !COND_i386_pc" in grub-core/Makefile.am.

Quoted 5 lines
grub-core/kern/verifiers.c +0 -0
index 75d7994cf..85887917d 100644

grub-core/kern/verifiers.c grub_verify_string (char *str, enum grub_verify_string_type type) · line 229

Quoted 9 lines
grub-core/kern/verifiers.c +4 -0
@@ -221,8 +221,19 @@ grub_verify_string (char *str, enum grub_verify_string_type type)
   return GRUB_ERR_NONE;
 }
 
+#ifdef GRUB_MACHINE_PCBIOS
+GRUB_MOD_INIT(verifiers)
+#else
 void
 grub_verifiers_init (void)
+#endif

I think a comment here (or somewhere in the actual code, anyway, I don't mind where) would be useful so that people trying to work out what's going on don't have to hunt through commit logs to find out.

If these minor comments are fixed:

Signature · 1 line
Colin Watson (he/him)                              [[email protected]]
#2 lore raw
Quoted 6 lines · Colin Watson
Quoted 5 lines · Michael Chang via Grub-devel

Given no core functions on i386-pc would require verifiers to work and the only consumer of the verifier API is the pgp module, it looks good to me that we can move the verifiers out of the kernel image and let moddep.lst to auto-load it when pgp is loaded on i386-pc platform.

Quoted 8 lines
Quoted 4 lines

This helps to reduce the size of core image and thus can relax the tension of exploading on some i386-pc system with very short MBR gap size. See also a very comprehensive summary from Colin [1] about the details.

Thanks for working on this! It's certainly awkward to have to deal with this sort of thing, but apparently not as awkward as I'd feared, and it's better than the alternative.

If I remember correctly in the past every core image size increase on i386-pc build is counted. IMHO we should keep up with that practice at least in reviewing new release ...

Quoted 5 lines
Quoted 2 lines

+AM_CONDITIONAL([COND_NOT_i386_pc], [test x$target_cpu != xi386 -o x$platform != xpc])

You could drop this and instead just do "if !COND_i386_pc" in grub-core/Makefile.am.

Indeed it looks superfluous to invent COND_NOT_i386_pc here. I will fix in next patch.

Quoted 9 lines
Quoted 9 lines
grub-core/kern/verifiers.c +0 -0
index 75d7994cf..85887917d 100644
@@ -221,8 +221,19 @@ grub_verify_string (char *str, enum grub_verify_string_type type)
   return GRUB_ERR_NONE;
 }
 
Quoted 10 lines

grub-core/kern/verifiers.c grub_verify_string (char *str, enum grub_verify_string_type type) · line 229

Quoted 6 lines
grub-core/kern/verifiers.c +4 -0
+#ifdef GRUB_MACHINE_PCBIOS
+GRUB_MOD_INIT(verifiers)
+#else
 void
 grub_verifiers_init (void)
+#endif

I think a comment here (or somewhere in the actual code, anyway, I don't mind where) would be useful so that people trying to work out what's going on don't have to hunt through commit logs to find out.

OK. I will add the comment to explain what's going on in next patch.

Quoted 4 lines

If these minor comments are fixed:

Thanks. I will also add your Reviewed-by ...

Regards,
Michael

Quoted 4 lines
Signature · 2 lines
-- 
Colin Watson (he/him)                              [[email protected]]