Skip to contentSkip to conversation

input (mainly cmdline) parsing corrections

PATCH Superseded Jan Beulich 3 participants 19 messages Mar 24 – Apr 7

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover input (mainly cmdline) parsing corrections

A number of issues were identified by Kamil, by way of fuzzing.

1: EFI: avoid OOB config file reads
2: llc-coloring: improve checking while parsing 3: ns16550: harden name/value pair parsing 4: ns16550: harden positional parsing
5: video/vesa: harden font height parsing

Jan

#1 lore raw

PATCH 1/5 EFI: avoid OOB config file reads

The message emitted by pre_parse() pretty clearly states the intention. Make sure we actually do so.

Fixes bf6501a62e80 ("x86-64: EFI boot code")
xen/common/efi/boot.c +5 -0
@@ -907,8 +907,13 @@ static void __init pre_parse(const struc
             start = 0;
     }
     if ( file->size && end[-1] )
+    {
          PrintStr(L"No newline at end of config file,"
                    " last line will be ignored.\r\n");
+
+         for ( UINTN pos = file->size; pos-- && *--end; )
+             *end = 0;
+    }
 }
 
 static void __init init_secure_boot_mode(void)
#2 lore raw

Re: [PATCH 1/5] EFI: avoid OOB config file reads

Quoted 10 lines · Jan Beulich

The message emitted by pre_parse() pretty clearly states the intention. Make sure we actually do so.

Fixes bf6501a62e80 ("x86-64: EFI boot code")

xen/common/efi/boot.c static void __init pre_parse(const struc · line 916

Quoted 10 lines
xen/common/efi/boot.c +5 -0
@@ -907,8 +907,13 @@ static void __init pre_parse(const struc
             start = 0;
     }
     if ( file->size && end[-1] )
+    {
          PrintStr(L"No newline at end of config file,"
                    " last line will be ignored.\r\n");
+
+         for ( UINTN pos = file->size; pos-- && *--end; )
+             *end = 0;
+    }

I agree this is what the the function intended.

But, ignoring the final line is rude and there's no viable editor in a UEFI shell to fix it.  Can't we just copy the file into a one-byte-bigger buffer and terminate it properly?

~Andrew

#3 lore raw

Re: [PATCH 1/5] EFI: avoid OOB config file reads

Quoted 24 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich

The message emitted by pre_parse() pretty clearly states the intention. Make sure we actually do so.

Fixes bf6501a62e80 ("x86-64: EFI boot code")

xen/common/efi/boot.c static void __init pre_parse(const struc · line 916

Quoted 10 lines
xen/common/efi/boot.c +5 -0
@@ -907,8 +907,13 @@ static void __init pre_parse(const struc
             start = 0;
     }
     if ( file->size && end[-1] )
+    {
          PrintStr(L"No newline at end of config file,"
                    " last line will be ignored.\r\n");
+
+         for ( UINTN pos = file->size; pos-- && *--end; )
+             *end = 0;
+    }

I agree this is what the the function intended.

Quoted 2 lines

But, ignoring the final line is rude and there's no viable editor in a UEFI shell to fix it.

On all the early EFI systems I had in use there was one. I'd need to check more recent systems, though. (I know some come without any EFI shell at all.)

Quoted 2 lines

  Can't we just copy the file into a
one-byte-bigger buffer and terminate it properly?

If that would have been possible with equally little churn, I would have done it that way. Of course it is possible in principle.

Jan

#4 lore raw

Re: [PATCH 1/5] EFI: avoid OOB config file reads

Quoted 28 lines · Jan Beulich
Quoted 24 lines · Andrew Cooper
Quoted 10 lines · Jan Beulich

The message emitted by pre_parse() pretty clearly states the intention. Make sure we actually do so.

Fixes bf6501a62e80 ("x86-64: EFI boot code")

xen/common/efi/boot.c static void __init pre_parse(const struc · line 916

Quoted 10 lines
xen/common/efi/boot.c +5 -0
@@ -907,8 +907,13 @@ static void __init pre_parse(const struc
             start = 0;
     }
     if ( file->size && end[-1] )
+    {
          PrintStr(L"No newline at end of config file,"
                    " last line will be ignored.\r\n");
+
+         for ( UINTN pos = file->size; pos-- && *--end; )
+             *end = 0;
+    }

I agree this is what the the function intended.

Quoted 2 lines

But, ignoring the final line is rude and there's no viable editor in a UEFI shell to fix it.

Quoted 8 lines

On all the early EFI systems I had in use there was one. I'd need to check more recent systems, though. (I know some come without any EFI shell at all.)

Quoted 2 lines

  Can't we just copy the file into a
one-byte-bigger buffer and terminate it properly?

If that would have been possible with equally little churn, I would have done it that way. Of course it is possible in principle.

Actually, it doesn't look all that bad - let me give that a try.

Jan

#5 lore raw

PATCH 2/5 llc-coloring: improve checking while parsing

In reply to Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")
xen/common/llc-coloring.c +3 -2
@@ -76,8 +76,9 @@ static int __init parse_color_config(con
         else                /* Single value */
             end = start;
 
-        if ( start > end || (end - start) > (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start)) >= max_num_colors )
+        if ( end >= NR_LLC_COLORS || start > end ||
+             (end - start) >= (UINT_MAX - *num_colors) ||
+             (*num_colors + (end - start + 1)) >= max_num_colors )
             return -EINVAL;
 
         /* Colors are range checked in check_colors() */
#6 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

Quoted 10 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")

For reference, I previously reported the UINT_MAX wraparound aspect here [1] and later also here [2]. The off-by-1 in the inclusive-range accounting looks like a separate issue.

Those threads also mention a few other related corner cases.

Best regards,
Mykola

[1] https://patchew.org/Xen/[email protected]/[email protected]/#[email protected]
[2] https://lists.xen.org/archives/html/xen-devel/2026-01/msg00369.html

Quoted 18 lines
xen/common/llc-coloring.c +3 -2
@@ -76,8 +76,9 @@ static int __init parse_color_config(con
         else                /* Single value */
             end = start;
 
-        if ( start > end || (end - start) > (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start)) >= max_num_colors )
+        if ( end >= NR_LLC_COLORS || start > end ||
+             (end - start) >= (UINT_MAX - *num_colors) ||
+             (*num_colors + (end - start + 1)) >= max_num_colors )
             return -EINVAL;
 
         /* Colors are range checked in check_colors() */
#7 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

Quoted 9 lines · Mykola Kvach
Quoted 8 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Quoted 5 lines
Quoted 2 lines
Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")

For reference, I previously reported the UINT_MAX wraparound aspect here [1] and later also here [2].

I've added another Reported-by, yet I wonder (in particular wrt [2]): Why did you not simply send patches? They likely would long have gone in.

Jan

Quoted 12 lines

The off-by-1 in the inclusive-range accounting looks like a separate issue.

Those threads also mention a few other related corner cases.

Best regards,
Mykola

[1] https://patchew.org/Xen/[email protected]/[email protected]/#[email protected]
[2] https://lists.xen.org/archives/html/xen-devel/2026-01/msg00369.html

#8 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

Quoted 11 lines · Jan Beulich
Quoted 9 lines · Mykola Kvach
Quoted 8 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")

For reference, I previously reported the UINT_MAX wraparound aspect here [1] and later also here [2].

I've added another Reported-by, yet I wonder (in particular wrt [2]): Why did you not simply send patches? They likely would long have gone in.

Thank you.

I did not have the time earlier to turn those reports into proper patches.

This month I have had some bandwidth again, so I started revisiting a number of previously reported issues.

At the moment I am also preparing some other fixes related to ITS, and the LLC issues were next on my list.

Best regards,
Mykola

Quoted 15 lines

Jan

Quoted 12 lines

The off-by-1 in the inclusive-range accounting looks like a separate issue.

Those threads also mention a few other related corner cases.

Best regards,
Mykola

[1] https://patchew.org/Xen/[email protected]/[email protected]/#[email protected]
[2] https://lists.xen.org/archives/html/xen-devel/2026-01/msg00369.html

#9 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")
xen/common/llc-coloring.c +0 -0
@@ -76,8 +76,9 @@ static int __init parse_color_config(con
         else                /* Single value */

xen/common/llc-coloring.c static int __init parse_color_config(con · line 84

Quoted 10 lines
xen/common/llc-coloring.c +3 -2
             end = start;
 
-        if ( start > end || (end - start) > (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start)) >= max_num_colors )
+        if ( end >= NR_LLC_COLORS || start > end ||
+             (end - start) >= (UINT_MAX - *num_colors) ||
+             (*num_colors + (end - start + 1)) >= max_num_colors )
             return -EINVAL;
 
         /* Colors are range checked in check_colors() */

I think this is correct, so Acked-by: Andrew Cooper <[email protected]>

However, the parsing logic is also ridiculous.  Most of the complexity comes because of parsing a bitmap but storing it longhand in an array of unsigned ints.

Instead, the global variables default_colors, dom0_colors and xen_colors should be bitmaps sized by NR_LLC_COLORS, and d->llc_colours should be a bitmap sized by xen_num_colors (which itself is bound by NR_LLC_COLORS).

With the default of 32 colours, this would involve no memory allocation at all, even on 32bit builds of Xen.

~Andrew

#10 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")
xen/common/llc-coloring.c +3 -2
@@ -76,8 +76,9 @@ static int __init parse_color_config(con
         else                /* Single value */
             end = start;
 
-        if ( start > end || (end - start) > (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start)) >= max_num_colors )
+        if ( end >= NR_LLC_COLORS || start > end ||
+             (end - start) >= (UINT_MAX - *num_colors) ||
+             (*num_colors + (end - start + 1)) >= max_num_colors )
             return -EINVAL;
 
Quoted 5 lines

xen/common/llc-coloring.c static int __init parse_color_config(con · line 84

Quoted 2 lines
xen/common/llc-coloring.c +0 -0
         /* Colors are range checked in check_colors() */

I think this is correct, so Acked-by: Andrew Cooper <[email protected]>

Thanks.

Quoted 10 lines

However, the parsing logic is also ridiculous.  Most of the complexity comes because of parsing a bitmap but storing it longhand in an array of unsigned ints.

Instead, the global variables default_colors, dom0_colors and xen_colors should be bitmaps sized by NR_LLC_COLORS, and d->llc_colours should be a bitmap sized by xen_num_colors (which itself is bound by NR_LLC_COLORS).

With the default of 32 colours, this would involve no memory allocation at all, even on 32bit builds of Xen.

It's pretty space inefficient, yes, but the parsing wouldn't become simpler when using bitmaps, would it?

Jan

#11 lore raw

Re: [PATCH 2/5] llc-coloring: improve checking while parsing

Quoted 36 lines · Jan Beulich
Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Jan Beulich

We can easily avoid the risk of wrapping UINT_MAX <-> 0 by applying a check against the compile-time-constant maximum number of colors.

Additionally the overflow checks suffered from an off-by-1, as the parsed ranges are inclusive (e.g. end == start being possible, requiring 1 array slot, while availability of 0 slots was checked in that case).

Fixes 6cdea3444eaf ("xen/arm: add Dom0 cache coloring support")
xen/common/llc-coloring.c +3 -2
@@ -76,8 +76,9 @@ static int __init parse_color_config(con
         else                /* Single value */
             end = start;
 
-        if ( start > end || (end - start) > (UINT_MAX - *num_colors) ||
-             (*num_colors + (end - start)) >= max_num_colors )
+        if ( end >= NR_LLC_COLORS || start > end ||
+             (end - start) >= (UINT_MAX - *num_colors) ||
+             (*num_colors + (end - start + 1)) >= max_num_colors )
             return -EINVAL;
 
Quoted 4 lines

xen/common/llc-coloring.c static int __init parse_color_config(con · line 84

Quoted 2 lines
xen/common/llc-coloring.c +0 -0
         /* Colors are range checked in check_colors() */

I think this is correct, so Acked-by: Andrew Cooper <[email protected]>

Thanks.

Quoted 4 lines

However, the parsing logic is also ridiculous.  Most of the complexity comes because of parsing a bitmap but storing it longhand in an array of unsigned ints.

Quoted 8 lines
Quoted 6 lines

Instead, the global variables default_colors, dom0_colors and xen_colors should be bitmaps sized by NR_LLC_COLORS, and d->llc_colours should be a bitmap sized by xen_num_colors (which itself is bound by NR_LLC_COLORS).

With the default of 32 colours, this would involve no memory allocation at all, even on 32bit builds of Xen.

It's pretty space inefficient, yes, but the parsing wouldn't become simpler when using bitmaps, would it?

Yes it would.

The current logic has opencoded bitmap helpers, and removing *num_colors simplifies all the boundary conditions (you simply don't need a cursor when collecting into a real bitmap.)

~Andrew

#12 lore raw

PATCH 3/5 ns16550: harden name/value pair parsing

In reply to Jan Beulich

strsep(), unless passed a 1st argument which itself points at a NULL pointer, won't return NULL. Instead if the separator(s) looked for isn't found, that pointed-to field would be updated to hold NULL. Hence the important check in get_token() is for token to be non-NULL after the call (which get_token()'s callers rely upon), while the checking of param_name is left there just in case.

Fixes 97fd49a7e074 ("ns16550: add support for UART parameters to be specifed with name-value pairs")
xen/drivers/char/ns16550.c +1 -1
@@ -1466,7 +1466,7 @@ static enum __init serial_param_type get
     unsigned int i;
 
     param_name = strsep(&token, "=");
-    if ( param_name == NULL )
+    if ( !param_name || !token )
         return num_serial_params;
 
     /* Linear search for the parameter. */
Acked-by on v1 3/5
#13 lore raw

Re: [PATCH 3/5] ns16550: harden name/value pair parsing

Quoted 10 lines · Jan Beulich

strsep(), unless passed a 1st argument which itself points at a NULL pointer, won't return NULL. Instead if the separator(s) looked for isn't found, that pointed-to field would be updated to hold NULL. Hence the important check in get_token() is for token to be non-NULL after the call (which get_token()'s callers rely upon), while the checking of param_name is left there just in case.

Fixes 97fd49a7e074 ("ns16550: add support for UART parameters to be specifed with name-value pairs")
#14 lore raw

PATCH 4/5 ns16550: harden positional parsing

In reply to Jan Beulich

Advancing past a character without knowing whether that's the NUL char is at risk of reading beyond the buffer.

Fixes 25de1f692059 ("Allow arch-specific defaults to be specified for ns16550")
xen/drivers/char/ns16550.c +3 -0
@@ -1541,6 +1541,9 @@ static bool __init parse_positional(stru
     {
         uart->data_bits = simple_strtoul(conf, &conf, 10);
 
+        if ( !*conf )
+            PARSE_ERR_RET("bad DPS setting");
+
         uart->parity = parse_parity_char(*conf);
 
         uart->stop_bits = simple_strtoul(conf + 1, &conf, 10);
#16 lore raw

PATCH 5/5 video/vesa: harden font height parsing

In reply to Jan Beulich

We should not prematurely increment the pointer, to avoid inadvertently skipping the NUL terminator.

Fixes 6d9199bd0f22 ("x86-64: enable hypervisor output on VESA frame buffer")
xen/drivers/video/vesa.c +2 -2
@@ -31,8 +31,8 @@ static unsigned int __initdata vram_rema
 static unsigned int __initdata font_height;
 static int __init cf_check parse_font_height(const char *s)
 {
-    if ( simple_strtoul(s, &s, 10) == 8 && (*s++ == 'x') )
-        font_height = simple_strtoul(s, &s, 10);
+    if ( simple_strtoul(s, &s, 10) == 8 && (*s == 'x') )
+        font_height = simple_strtoul(s + 1, &s, 10);
     if ( *s != '\0' )
         font_height = 0;
 
#18 lore raw

Ping: [PATCH 0/5] input (mainly cmdline) parsing corrections

In reply to Jan Beulich
Quoted 3 lines · Jan Beulich

A number of issues were identified by Kamil, by way of fuzzing.

1: EFI: avoid OOB config file reads

Leaving aside this one, which was redone quite differently (and re-submitted independently), ...

Quoted 4 lines

2: llc-coloring: improve checking while parsing 3: ns16550: harden name/value pair parsing 4: ns16550: harden positional parsing
5: video/vesa: harden font height parsing

... may I ask for acks (or otherwise) on these ones?

Thanks, Jan