Skip to contentSkip to conversation

blsuki: Fix grub_errno leakage in blsuki_is_default_entry()

PATCH Superseded Michael Chang via Grub-devel 2 participants 8 messages Nov 10, 2025 – Nov 11, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blsuki: Fix grub_errno leakage in blsuki_is_default_entry()

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +6 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
     return true;
 
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 
Signature · 7 lines
2.51.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Michael Chang via Grub-devel

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

Quoted 8 lines

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

Thanks,
Sudhakar

Quoted 29 lines

---
grub-core/commands/blsuki.c | 6 ++++++

1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +6 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
    return true;
 
  def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
  if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
    return false;
 
Signature · 7 lines
2.51.1


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

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

#3 lore raw
In reply to Michael Chang via Grub-devel

Am 10.11.2025 um 08:38 schrieb Michael Chang via Grub-devel:

Quoted 23 lines

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +0 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
     return true;
 

grub-core/commands/blsuki.c blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int · line 1521

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 

This way you'll clear a possible previous GRUB_ERR_BAD_NUMBER Shouldn't it be this way:

     return true;

+ old_err = grub_errno;
+ grub_errno = GRUB_ERR_NONE;
def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+ if (grub_errno == GRUB_ERR_BAD_NUMBER)
+ {
+ grub_errno = old_err;
+ return false;
+ }
+ grub_errno = old_err;
+
if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)

     return false;

JMTC
KR Michael

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

#5 lore raw
In reply to Michael Chang via Grub-devel
Quoted 10 lines · Michael Chang

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

grub_strtol() can also set grub_errno to GRUB_ERR_OUT_OF_RANGE. Would it make sense to check for that as well? We do return false if def_idx is negative or larger than GRUB_INT_MAX, but I don't know if we would want to sustain GRUB_ERR_OUT_OF_RANGE in grub_errno past this function should the "def_entry" passed into this function contain a bad value that causes grub_strtol() to give an error.

Alec Brown

Quoted 28 lines
---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +0 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry,

int

     return true;
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
grub-core/commands/blsuki.c +6 -1
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 
--

2.51.1

Quoted 6 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://urldefense.com/v3/__https://lists.gnu.org/mailman/listinfo/grub-
devel__;!!ACWV5N9M2RV99hQ!PT07VdaZpBxwaIforN0iXRL1OG2vC6xOV2SQ6cgn5eVQvHxGFpEtx3c9eNW_ApT4iml6Fnoj FNjVxarvCu1C$

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

#4 lore raw
In reply to Michael Lawnick via Grub-devel
Quoted 26 lines · Michael Lawnick via Grub-devel

Am 10.11.2025 um 08:38 schrieb Michael Chang via Grub-devel:

Quoted 23 lines

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +0 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
     return true;
 
Quoted 12 lines

grub-core/commands/blsuki.c blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int · line 1521

Quoted 10 lines
grub-core/commands/blsuki.c +6 -0
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 

This way you'll clear a possible previous GRUB_ERR_BAD_NUMBER Shouldn't it be this way:

No, I think this patch does not introduce such behavior change. The previous error of any kind is not considered an issue, and there was no intent to preserve them unless grub_strtol() was explicitly surrounded by grub_error_push()/grub_error_pop(), but apparantly it is not there before the patch is applied. The error return is always overridden by the error return code of grub_strtol() if there's any.

grub-core/commands/blsuki.c blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int · line 1518

Quoted 12 lines
     return true;
grub-core/commands/blsuki.c +9 -0
+  old_err = grub_errno;
+  grub_errno = GRUB_ERR_NONE;
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = old_err;
+      return false;
+    }
+    grub_errno = old_err;
+

IMHO restoring unhandled grub_errno here would still be considered a bug. It could spread to other unrelated modules or functions which might interpret it as an unspecified/unknown error and thus disrupt the boot process in one way or another. This is exactly what the patch trying to avoid.

Thanks,
Michael

Quoted 10 lines
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;

JMTC
KR Michael

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

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

#7 lore raw
In reply to Michael Chang via Grub-devel

Hi,

Sorry. Forgot to mention the downstream bug report which was fixed by the patch.

https://bugzilla.suse.com/show_bug.cgi?id=1253097

Thanks,
Michael

Quoted 36 lines · Michael Chang

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +6 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
     return true;
 
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 
Signature · 1 line
2.51.1

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

#6 lore raw
In reply to Alec Brown via Grub-devel
Quoted 12 lines · Alec Brown
Quoted 10 lines · Michael Chang

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

Quoted 6 lines

grub_strtol() can also set grub_errno to GRUB_ERR_OUT_OF_RANGE. Would it make sense to check for that as well? We do return false if def_idx is negative or larger than GRUB_INT_MAX, but I don't know if we would want to sustain GRUB_ERR_OUT_OF_RANGE in grub_errno past this function should the "def_entry" passed into this function contain a bad value that causes grub_strtol() to give an error.

Yes, you are right that GRUB_ERR_OUT_OF_RANGE could pass through the function and cause the same issue as leaked grub_errno. I'll add a check for this case in the next version.

Thanks,
Michael

Quoted 31 lines

Alec Brown

Quoted 28 lines
---
 grub-core/commands/blsuki.c | 6 ++++++
 1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +0 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry,

int

     return true;
   def_idx = grub_strtol (def_entry, &def_entry_end, 0);
grub-core/commands/blsuki.c +6 -1
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
   if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
     return false;
 
--

2.51.1

Quoted 6 lines
Quoted 6 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://urldefense.com/v3/__https://lists.gnu.org/mailman/listinfo/grub-
devel__;!!ACWV5N9M2RV99hQ!PT07VdaZpBxwaIforN0iXRL1OG2vC6xOV2SQ6cgn5eVQvHxGFpEtx3c9eNW_ApT4iml6Fnoj FNjVxarvCu1C$

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

#2 lore raw
In reply to Sudhakar Kuppusamy
Quoted 12 lines · Sudhakar Kuppusamy
Quoted 12 lines · Michael Chang via Grub-devel

The grub_strtol() call in blsuki_is_default_entry() can set grub_errno to GRUB_ERR_BAD_NUMBER if the input string cannot be converted into any valid digits.

This errno value is currently left uncleared, which can lead to unexpected behavior in subsequent logic that tests the result of a function by checking grub_errno.

Quoted 6 lines
Quoted 4 lines

Clear grub_errno and return false when GRUB_ERR_BAD_NUMBER is set, as this specific error should be ignored in this context.

Thanks for the review. I'll add your Reviewed-by tag in next version.

Thanks,
Michael

Quoted 25 lines

Thanks,
Sudhakar

Quoted 21 lines

---
grub-core/commands/blsuki.c | 6 ++++++

1 file changed, 6 insertions(+)
grub-core/commands/blsuki.c +6 -0
index 21d767f05..df25b6fbc 100644
@@ -1510,6 +1510,12 @@ blsuki_is_default_entry (const char *def_entry, grub_blsuki_entry_t *entry, int
    return true;
 
  def_idx = grub_strtol (def_entry, &def_entry_end, 0);
+  if (grub_errno == GRUB_ERR_BAD_NUMBER)
+    {
+      grub_errno = GRUB_ERR_NONE;
+      return false;
+    }
+
  if (*def_entry_end != '\0' || def_idx < 0 || def_idx > GRUB_INT_MAX)
    return false;
 
Quoted 9 lines
Quoted 8 lines
Signature · 7 lines
2.51.1


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

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