Skip to contentSkip to conversation

Print module name on license check failure

PATCH In review Robbie Harwood 2 participants 4 messages Oct 25, 2021 – Oct 26, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Print module name on license check failure

Before performing the license check, resolve the module name so that it can be printed if the license check fails. Prior to this change, grub would only indicate that the check had been failed, but not by what module. This made it difficult to track down either the problem module, or debug the false positive further.

---
 grub-core/kern/dl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
grub-core/kern/dl.c +14 -7
index 48f8a7907..363bacc43 100644
@@ -457,14 +457,21 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name)
    Be sure to understand your license obligations.
 */
 static grub_err_t
-grub_dl_check_license (Elf_Ehdr *e)
+grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
 {
   Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
-  if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
+  if (!s)
+    return grub_error (GRUB_ERR_BAD_MODULE,
+		       "no license section in module %.64s", mod->name);
+
+  if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)
     return GRUB_ERR_NONE;
-  return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
+
+  return grub_error (GRUB_ERR_BAD_MODULE,
+		     "incompatible license in module %.64s: %.64s", mod->name,
+		     (char *) e + s->sh_offset);
 }
 
 static grub_err_t
@@ -641,8 +648,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
      constitutes linking) and GRUB core being licensed under GPLv3+.
      Be sure to understand your license obligations.
   */
-  if (grub_dl_check_license (e)
-      || grub_dl_resolve_name (mod, e)
+  if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_license (mod, e)
       || grub_dl_resolve_dependencies (mod, e)
       || grub_dl_load_segments (mod, e)
       || grub_dl_resolve_symbols (mod, e)
Signature · 1 line
2.33.0
#1 lore raw
Quoted 17 lines · Robbie Harwood

Before performing the license check, resolve the module name so that it can be printed if the license check fails. Prior to this change, grub would only indicate that the check had been failed, but not by what module. This made it difficult to track down either the problem module, or debug the false positive further.

---
 grub-core/kern/dl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
grub-core/kern/dl.c +0 -0
index 48f8a7907..363bacc43 100644
@@ -457,14 +457,21 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name)
    Be sure to understand your license obligations.

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 463

Quoted 10 lines
grub-core/kern/dl.c +2 -4
 */
 static grub_err_t
-grub_dl_check_license (Elf_Ehdr *e)
+grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
 {
   Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
-  if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
+  if (!s)

s == NULL please...

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 469

Quoted 6 lines
grub-core/kern/dl.c +6 -0
+    return grub_error (GRUB_ERR_BAD_MODULE,
+		       "no license section in module %.64s", mod->name);
+
+  if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)

Could use grub_strncmp() instead of grub_strcmp() here?

Quoted 10 lines
grub-core/kern/dl.c +4 -1
     return GRUB_ERR_NONE;
-  return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
+
+  return grub_error (GRUB_ERR_BAD_MODULE,
+		     "incompatible license in module %.64s: %.64s", mod->name,
+		     (char *) e + s->sh_offset);
 }
 
 static grub_err_t

grub-core/kern/dl.c grub_dl_load_core_noinit (void *addr, grub_size_t size) · line 652

Quoted 7 lines
grub-core/kern/dl.c +2 -2
@@ -641,8 +648,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
      constitutes linking) and GRUB core being licensed under GPLv3+.
      Be sure to understand your license obligations.
   */
-  if (grub_dl_check_license (e)
-      || grub_dl_resolve_name (mod, e)
+  if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_license (mod, e)

I think you should split this patch into two. One which improves error messages and another which imposes length restrictions on the grub_strcmp() and grub_error() in the grub_dl_check_license(). And I think you should use 63 instead of 64 (63 + NUL gives us 64 :-)).

Daniel

#2 lore raw
Quoted 31 lines · Daniel Kiper
Quoted 17 lines · Robbie Harwood

Before performing the license check, resolve the module name so that it can be printed if the license check fails. Prior to this change, grub would only indicate that the check had been failed, but not by what module. This made it difficult to track down either the problem module, or debug the false positive further.

---
 grub-core/kern/dl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
grub-core/kern/dl.c +0 -0
index 48f8a7907..363bacc43 100644
@@ -457,14 +457,21 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name)
    Be sure to understand your license obligations.

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 463

Quoted 10 lines
grub-core/kern/dl.c +2 -4
 */
 static grub_err_t
-grub_dl_check_license (Elf_Ehdr *e)
+grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
 {
   Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
-  if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
+  if (!s)

s == NULL please...

Quoted 8 lines

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 469

Quoted 6 lines
grub-core/kern/dl.c +6 -0
+    return grub_error (GRUB_ERR_BAD_MODULE,
+		       "no license section in module %.64s", mod->name);
+
+  if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)

Could use grub_strncmp() instead of grub_strcmp() here?

I don't object to doing so for clarity, but strictly speaking I don't think it's needed as the LICENSE= strings are terminated.

Quoted 8 lines
Quoted 8 lines
grub-core/kern/dl.c +4 -1
     return GRUB_ERR_NONE;
-  return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
+
+  return grub_error (GRUB_ERR_BAD_MODULE,
+		     "incompatible license in module %.64s: %.64s", mod->name,
+		     (char *) e + s->sh_offset);
 }
 
Quoted 13 lines

grub-core/kern/dl.c grub_dl_load_core_noinit (void *addr, grub_size_t size) · line 652

Quoted 9 lines
grub-core/kern/dl.c +2 -2
 static grub_err_t
@@ -641,8 +648,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
      constitutes linking) and GRUB core being licensed under GPLv3+.
      Be sure to understand your license obligations.
   */
-  if (grub_dl_check_license (e)
-      || grub_dl_resolve_name (mod, e)
+  if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_license (mod, e)

I think you should split this patch into two. One which improves error messages and another which imposes length restrictions on the grub_strcmp() and grub_error() in the grub_dl_check_license().

I can do that, but the NULL-check and strcmp are both pre-existing code from when the license check was added. Are you sure?

Quoted 2 lines

And I think you should use 63 instead of 64 (63 + NUL gives us 64 :-)).

Fine.

Be well,
--Robbie

#3 lore raw
Quoted 42 lines · Robbie Harwood
Quoted 31 lines · Daniel Kiper
Quoted 17 lines · Robbie Harwood

Before performing the license check, resolve the module name so that it can be printed if the license check fails. Prior to this change, grub would only indicate that the check had been failed, but not by what module. This made it difficult to track down either the problem module, or debug the false positive further.

---
 grub-core/kern/dl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
grub-core/kern/dl.c +0 -0
index 48f8a7907..363bacc43 100644
@@ -457,14 +457,21 @@ grub_dl_find_section (Elf_Ehdr *e, const char *name)
    Be sure to understand your license obligations.

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 463

Quoted 10 lines
grub-core/kern/dl.c +2 -4
 */
 static grub_err_t
-grub_dl_check_license (Elf_Ehdr *e)
+grub_dl_check_license (grub_dl_t mod, Elf_Ehdr *e)
 {
   Elf_Shdr *s = grub_dl_find_section (e, ".module_license");
-  if (s && (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
-	    || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0))
+  if (!s)

s == NULL please...

Quoted 8 lines

grub-core/kern/dl.c grub_dl_find_section (Elf_Ehdr *e, const char *name) · line 469

Quoted 6 lines
grub-core/kern/dl.c +6 -0
+    return grub_error (GRUB_ERR_BAD_MODULE,
+		       "no license section in module %.64s", mod->name);
+
+  if (grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv3+") == 0
+      || grub_strcmp ((char *) e + s->sh_offset, "LICENSE=GPLv2+") == 0)

Could use grub_strncmp() instead of grub_strcmp() here?

Quoted 2 lines

I don't object to doing so for clarity, but strictly speaking I don't think it's needed as the LICENSE= strings are terminated.

Huh! Yeah, you are right. So, please ignore my stupid comment.

Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
grub-core/kern/dl.c +4 -1
     return GRUB_ERR_NONE;
-  return grub_error (GRUB_ERR_BAD_MODULE, "incompatible license");
+
+  return grub_error (GRUB_ERR_BAD_MODULE,
+		     "incompatible license in module %.64s: %.64s", mod->name,
+		     (char *) e + s->sh_offset);
 }
 
Quoted 13 lines

grub-core/kern/dl.c grub_dl_load_core_noinit (void *addr, grub_size_t size) · line 652

Quoted 9 lines
grub-core/kern/dl.c +2 -2
 static grub_err_t
@@ -641,8 +648,8 @@ grub_dl_load_core_noinit (void *addr, grub_size_t size)
      constitutes linking) and GRUB core being licensed under GPLv3+.
      Be sure to understand your license obligations.
   */
-  if (grub_dl_check_license (e)
-      || grub_dl_resolve_name (mod, e)
+  if (grub_dl_resolve_name (mod, e)
+      || grub_dl_check_license (mod, e)

I think you should split this patch into two. One which improves error messages and another which imposes length restrictions on the grub_strcmp() and grub_error() in the grub_dl_check_license().

Quoted 2 lines

I can do that, but the NULL-check and strcmp are both pre-existing code from when the license check was added. Are you sure?

Ditto.

Quoted 2 lines
Quoted 2 lines

And I think you should use 63 instead of 64 (63 + NUL gives us 64 :-)).

I will fix it myself before pushing v3.

Daniel