Skip to contentSkip to conversation

misc: fix invalid character recongition in strto*l

PATCH In review Aaron Miller 2 participants 5 messages Apr 27, 2016 – Apr 29, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 misc: fix invalid character recongition in strto*l

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4
---

Missed the >= vs < in the previously sent patch (I caught this, but then still mailed the broken patch)

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/kern/misc.c +3 -1
index 906d2c2..1c0c913 100644
@@ -391,10 +391,12 @@ grub_strtoull (const char *str, char **end, int base)
       unsigned long digit;
 
       digit = grub_tolower (*str) - '0';
+      if (digit >= (unsigned long) base)
+	break;
       if (digit > 9)
 	{
 	  digit += '0' - 'a' + 10;
-	  if (digit >= (unsigned long) base)
+	  if (digit >= (unsigned long) base || digit <= 9)
 	    break;
 	}
 
Signature · 1 line
2.8.0.rc2
#1 lore raw

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4
---

Need to move the out-of-base check to *after* the outside [0-9] handling or this breaks.

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/kern/misc.c +3 -1
index 906d2c2..3653d4d 100644
@@ -394,9 +394,11 @@ grub_strtoull (const char *str, char **end, int base)
       if (digit > 9)
 	{
 	  digit += '0' - 'a' + 10;
-	  if (digit >= (unsigned long) base)
+	  if (digit >= (unsigned long) base || digit <= 9)
 	    break;
 	}
+      if (digit >= (unsigned long) base)
+	break;
 
       found = 1;
 
Signature · 1 line
2.8.0.rc2
Quoted 37 lines · Aaron Miller

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4
---

Missed the >= vs < in the previously sent patch (I caught this, but then still mailed the broken patch)

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/kern/misc.c +3 -1
index 906d2c2..1c0c913 100644
@@ -391,10 +391,12 @@ grub_strtoull (const char *str, char **end, int 
 base)
       unsigned long digit;
 
       digit = grub_tolower (*str) - '0';
+      if (digit >= (unsigned long) base)
+	break;
       if (digit > 9)
 	{
 	  digit += '0' - 'a' + 10;
-	  if (digit >= (unsigned long) base)
+	  if (digit >= (unsigned long) base || digit <= 9)
 	    break;
 	}
Signature · 7 lines
-- 
2.8.0.rc2

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

28.04.2016 03:53, Aaron Miller пишет:

Quoted 4 lines

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4

Does it cause any real problem (i.e. is it 2.02 material)?

Quoted 13 lines

---

Need to move the out-of-base check to *after* the outside [0-9] handling or this breaks.

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/kern/misc.c +0 -0
index 906d2c2..3653d4d 100644

grub-core/kern/misc.c grub_strtoull (const char *str, char **end, int base) · line 397

Quoted 5 lines
grub-core/kern/misc.c +1 -1
@@ -394,9 +394,11 @@ grub_strtoull (const char *str, char **end, int base)
       if (digit > 9)
     {
       digit += '0' - 'a' + 10;
-      if (digit >= (unsigned long) base)
+      if (digit >= (unsigned long) base || digit <= 9)

base comparison becomes redundant here. And this needs comment explaining digit <= 9 comparison for future reference.

Quoted 7 lines
grub-core/kern/misc.c +2 -0
         break;
     }
+      if (digit >= (unsigned long) base)
+    break;
 
       found = 1;
 
#3 lore raw
Quoted 8 lines · Andrei Borzenkov

28.04.2016 03:53, Aaron Miller пишет:

Quoted 4 lines

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4

Does it cause any real problem (i.e. is it 2.02 material)?

With the later patch I sent (allowing port numbers including "[ipv6:addr]:port"), it is needed to keep something like "0000]" from being parsed as 6. I don't know of any case without that where it can cause a problem -- would be any case where a number being read is followed by any of `_^]\[

Quoted 8 lines
Quoted 8 lines

---

Need to move the out-of-base check to *after* the outside [0-9] handling or this breaks.

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

grub-core/kern/misc.c grub_strtoull (const char *str, char **end, int · line 398

Quoted 11 lines
grub-core/kern/misc.c +1 -1
index 906d2c2..3653d4d 100644
@@ -394,9 +394,11 @@ grub_strtoull (const char *str, char **end, int 
 base)
       if (digit > 9)
     {
       digit += '0' - 'a' + 10;
-      if (digit >= (unsigned long) base)
+      if (digit >= (unsigned long) base || digit <= 9)

base comparison becomes redundant here. And this needs comment explaining digit <= 9 comparison for future reference.

Will resend with a comment -- this and the below are both needed when for example base=8 and digit is '9' or '8' and should still be rejected. digit <= 9 is needed to prevent the '` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, [ = 4' problem.

Quoted 13 lines

grub-core/kern/misc.c grub_strtoull (const char *str, char **end, int base) · line 403

Quoted 7 lines
grub-core/kern/misc.c +2 -0
         break;
     }
+      if (digit >= (unsigned long) base)
+    break;
 
       found = 1;

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

#4 lore raw

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4
---

Added comment.

 grub-core/kern/misc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/kern/misc.c +5 -1
index 906d2c2..d181036 100644
@@ -394,9 +394,13 @@ grub_strtoull (const char *str, char **end, int base)
       if (digit > 9)
 	{
 	  digit += '0' - 'a' + 10;
-	  if (digit >= (unsigned long) base)
+	  // digit <= 9 check is needed to keep chars larger than
+	  // '9' but less than 'a' from being read as numbers
+	  if (digit >= (unsigned long) base || digit <= 9)
 	    break;
 	}
+      if (digit >= (unsigned long) base)
+	break;
 
       found = 1;
 
Signature · 1 line
2.8.0.rc2
Quoted 52 lines · Aaron Miller
Quoted 8 lines · Andrei Borzenkov

28.04.2016 03:53, Aaron Miller пишет:

Quoted 4 lines

Would previously allow digits larger than the base and didn't check that subtracting the difference from 0-9 to lowercase letters for characters larger than 9 didn't result in a value lower than 9, which allowed the
parses: ` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, and [ = 4

Does it cause any real problem (i.e. is it 2.02 material)?

With the later patch I sent (allowing port numbers including "[ipv6:addr]:port"), it is needed to keep something like "0000]" from being parsed as 6. I don't know of any case without that where it can cause a problem -- would be any case where a number being read is followed by any of `_^]\[

Quoted 8 lines
Quoted 8 lines

---

Need to move the out-of-base check to *after* the outside [0-9] handling or this breaks.

 grub-core/kern/misc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

grub-core/kern/misc.c grub_strtoull (const char *str, char **end, int · line 398

Quoted 11 lines
grub-core/kern/misc.c +1 -1
index 906d2c2..3653d4d 100644
@@ -394,9 +394,11 @@ grub_strtoull (const char *str, char **end, int 
 base)
       if (digit > 9)
     {
       digit += '0' - 'a' + 10;
-      if (digit >= (unsigned long) base)
+      if (digit >= (unsigned long) base || digit <= 9)

base comparison becomes redundant here. And this needs comment explaining digit <= 9 comparison for future reference.

Will resend with a comment -- this and the below are both needed when for example base=8 and digit is '9' or '8' and should still be rejected. digit <= 9 is needed to prevent the '` = 9, _ = 8, ^ = 7, ] = 6, \ = 5, [ = 4' problem.

Quoted 13 lines

grub-core/kern/misc.c grub_strtoull (const char *str, char **end, int · line 404

Quoted 7 lines
grub-core/kern/misc.c +2 -0
         break;
     }
+      if (digit >= (unsigned long) base)
+    break;
 
       found = 1;

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