Skip to contentSkip to conversation

console: avoid buffer overflow in guest_console_write()

PATCH In review Jan Beulich 5 participants 23 messages Nov 29, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 console: avoid buffer overflow in guest_console_write()

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")
xen/drivers/char/console.c +1 -1
@@ -538,7 +538,7 @@ static long guest_console_write(XEN_GUES
                 __HYPERVISOR_console_io, "iih",
                 CONSOLEIO_write, count, buffer);
 
-        kcount = min_t(int, count, sizeof(kbuf)-1);
+        kcount = min(count + sizeof(char[0]), sizeof(kbuf) - 1);
         if ( copy_from_guest(kbuf, buffer, kcount) )
             return -EFAULT;
 

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 14 lines · Jan Beulich

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

xen/drivers/char/console.c static long guest_console_write(XEN_GUES · line 541

Quoted 5 lines
xen/drivers/char/console.c +1 -1
@@ -538,7 +538,7 @@ static long guest_console_write(XEN_GUES
                 __HYPERVISOR_console_io, "iih",
                 CONSOLEIO_write, count, buffer);
 
-        kcount = min_t(int, count, sizeof(kbuf)-1);
+        kcount = min(count + sizeof(char[0]), sizeof(kbuf) - 1);

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

That said, given the severity and urgency of this extremely-lucky-its-not-an-XSA, Reviewed-by: Andrew Cooper <[email protected]>, but ideally using the +0ul form.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")
xen/drivers/char/console.c +0 -0
@@ -538,7 +538,7 @@ static long guest_console_write(XEN_GUES
                 __HYPERVISOR_console_io, "iih",
                 CONSOLEIO_write, count, buffer);
 
Quoted 8 lines

xen/drivers/char/console.c static long guest_console_write(XEN_GUES · line 541

Quoted 2 lines
xen/drivers/char/console.c +1 -1
-        kcount = min_t(int, count, sizeof(kbuf)-1);
+        kcount = min(count + sizeof(char[0]), sizeof(kbuf) - 1);

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

That said, given the severity and urgency of this extremely-lucky-its-not-an-XSA, Reviewed-by: Andrew Cooper <[email protected]>, but ideally using the +0ul form.

Thanks.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 7 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

Quoted 6 lines

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

I don't know what i wrong with
(size_t)0
which is shorter, even !

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with
(size_t)0
which is shorter, even !

Or shorter yet, (size_t)count if you're wanting to go that route.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Ian Jackson
Quoted 15 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 7 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

Quoted 6 lines

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with
(size_t)0
which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 6 lines
Quoted 3 lines

I don't know what i wrong with
(size_t)0
which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

I don't know what i wrong with
(size_t)0
which is shorter, even !

Quoted 7 lines
Quoted 3 lines

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

I don't know what i wrong with
(size_t)0
which is shorter, even !

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with
(size_t)0
which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

Okay, I've switched to this, but while doing so I started wondering why we'd then not use

        kcount = min(count, (unsigned int)sizeof(kbuf) - 1);

which is an (often slightly cheaper) 32-bit operation (and which is what I had actually started from).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw
Quoted 32 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with

    (size_t)0

which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

Quoted 7 lines

Okay, I've switched to this, but while doing so I started wondering why we'd then not use

         kcount = min(count, (unsigned int)sizeof(kbuf) - 1);

which is an (often slightly cheaper) 32-bit operation (and which is what I had actually started from).

While modifying guest_console_write(), would you mind writing a '\0' to kbuf[kcount]? There is a "conring_puts(kbuf);" later in this function which would like a 0 terminated string as input.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw
Quoted 36 lines · Jürgen Groß
Quoted 35 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with

    (size_t)0

which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

Okay, I've switched to this, but while doing so I started wondering why we'd then not use

Quoted 8 lines
Quoted 4 lines
         kcount = min(count, (unsigned int)sizeof(kbuf) - 1);

which is an (often slightly cheaper) 32-bit operation (and which is what I had actually started from).

While modifying guest_console_write(), would you mind writing a '\0' to kbuf[kcount]? There is a "conring_puts(kbuf);" later in this function which would like a 0 terminated string as input.

That's not the right change for this problem, I think. Now that we support embedded nul characters, a count should be passed instead. Julien?

I also wouldn't want to merge this into this patch; I'm happy to send a separate one.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw
Quoted 42 lines · Jan Beulich
Quoted 34 lines · Jürgen Groß
Quoted 33 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with

    (size_t)0

which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

Okay, I've switched to this, but while doing so I started wondering why we'd then not use

Quoted 7 lines
Quoted 4 lines
         kcount = min(count, (unsigned int)sizeof(kbuf) - 1);

which is an (often slightly cheaper) 32-bit operation (and which is what I had actually started from).

While modifying guest_console_write(), would you mind writing a '\0' to kbuf[kcount]? There is a "conring_puts(kbuf);" later in this function which would like a 0 terminated string as input.

Quoted 6 lines

That's not the right change for this problem, I think. Now that we support embedded nul characters, a count should be passed instead. Julien?

I also wouldn't want to merge this into this patch; I'm happy to send a separate one.

I agree.  Lets fix the concrete stack corruption issue separately from the concern over NUL-correctness (which was the purpose of the patch which introduced this vulnerability to start with).

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw
In reply to Jan Beulich
Quoted 46 lines · Jan Beulich
Quoted 36 lines · Jürgen Groß
Quoted 35 lines · Jan Beulich
Quoted 23 lines · Andrew Cooper
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 13 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 12 lines · Andrew Cooper
Quoted 5 lines

Is sizeof(array[0]) always 0, or is this just a GCC-ism ?  Godbolt suggests is 0 on all compiler we support.

Either way, isn't the more common idiom + 0ul ?  Personally, I feel that is clearer to follow.

I decided against + 0ul or alike because in principle size_t and unsigned long are different types. In particular 32-bit x86 gcc uses unsigned int for size_t, and hence min()'s type safety check would cause the build to fail there. The same risk obviously exists for any 32-bit arch (e.g. Arm32, but I haven't checked what type it actually uses).

Quoted 3 lines

I don't know what i wrong with

     (size_t)0

which is shorter, even !

True. Yet it contains a cast, no matter how risk-free it may be in this case. With a cast, I could as well have written (yet shorter) (size_t)count.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Given that min() has a very strict typecheck, I think we should permit any use of an explicit cast in a single operand, because it *is* safer than switching to the min_t() route to make things compile.

Well, I can switch to (size_t)count if this is liked better overall.

Personally, I'd prefer this option most of all.

Okay, I've switched to this, but while doing so I started wondering why we'd then not use

Quoted 8 lines
Quoted 4 lines
          kcount = min(count, (unsigned int)sizeof(kbuf) - 1);

which is an (often slightly cheaper) 32-bit operation (and which is what I had actually started from).

While modifying guest_console_write(), would you mind writing a '\0' to kbuf[kcount]? There is a "conring_puts(kbuf);" later in this function which would like a 0 terminated string as input.

Quoted 6 lines

That's not the right change for this problem, I think. Now that we support embedded nul characters, a count should be passed instead. Julien?

I also wouldn't want to merge this into this patch; I'm happy to send a separate one.

Yeah, I now realized that it is easy to just add a count parameter to conring_puts() as it is called only twice and count is already known at the callsites.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v1 1/1
#14 lore raw
In reply to Jan Beulich

Hi,

Quoted 10 lines · Jan Beulich

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

Sorry for the breakage.

Cheers,

Quoted 13 lines
xen/drivers/char/console.c +1 -1
@@ -538,7 +538,7 @@ static long guest_console_write(XEN_GUES
                  __HYPERVISOR_console_io, "iih",
                  CONSOLEIO_write, count, buffer);
 
-        kcount = min_t(int, count, sizeof(kbuf)-1);
+        kcount = min(count + sizeof(char[0]), sizeof(kbuf) - 1);
          if ( copy_from_guest(kbuf, buffer, kcount) )
              return -EFAULT;
 
Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw
In reply to Jan Beulich

Jan Beulich writes ("[PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 10 lines

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

ea601ec9995b included this hunk:

case CONSOLEIO_read:
+ /*

  +         * The return value is either the number of characters read or
  +         * a negative value in case of error. So we need to prevent
  +         * overlap between the two sets.
  +         */

+ rc = -E2BIG;
+ if ( count > INT_MAX )
+ break;

Maybe it would be good to move that outside the switch so that it affects CONSOLEIO_write too ?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw
Quoted 20 lines · Ian Jackson

Jan Beulich writes ("[PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 10 lines

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

ea601ec9995b included this hunk:

case CONSOLEIO_read:
+ /*

  +         * The return value is either the number of characters read or
  +         * a negative value in case of error. So we need to prevent
  +         * overlap between the two sets.
  +         */
Quoted 6 lines

+ rc = -E2BIG;
+ if ( count > INT_MAX )
+ break;

Maybe it would be good to move that outside the switch so that it affects CONSOLEIO_write too ?

And any future subops? And limit output more than necessary (not that I think anyone will want to push more than 2G at a time through this interface, but anyway)?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Ian Jackson

Jan Beulich writes ("[PATCH] console: avoid buffer overflow in guest_console_write()"):

Quoted 10 lines

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

ea601ec9995b included this hunk:

case CONSOLEIO_read:
+ /*

  +         * The return value is either the number of characters read or
  +         * a negative value in case of error. So we need to prevent
  +         * overlap between the two sets.
  +         */

+ rc = -E2BIG;
+ if ( count > INT_MAX )
+ break;

Quoted 5 lines
Quoted 2 lines

Maybe it would be good to move that outside the switch so that it affects CONSOLEIO_write too ?

And any future subops? And limit output more than necessary (not that I think anyone will want to push more than 2G at a time through this interface, but anyway)?

Linux is seriously considering initrds > 4G now for various usecases.

2G really isn't enough for everyone, and we shouldn't hardcode blind presumptions like this.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich

The switch of guest_console_write()'s second parameter from plain to unsigned int has caused the function's main loop header to no longer guard the min_t() use within the function against effectively negative values, due to the casts hidden inside the macro. Replace by a plain min(), converting one of the arguments suitably without involving any cast.

Fixes ea601ec9995b ("xen/console: Rework HYPERCALL_console_io interface")

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

PATCH 1/1 CONSOLEIO_write stack overflow PoC

In reply to Jan Beulich

Classify it as an XSA test (which arguably ought to be named 'security'), despite no XSA being issues.

---
 docs/all-tests.dox                 |  2 ++
 tests/xsa-consoleio-write/Makefile |  9 +++++
 tests/xsa-consoleio-write/main.c   | 69 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+)
 create mode 100644 tests/xsa-consoleio-write/Makefile
 create mode 100644 tests/xsa-consoleio-write/main.c
docs/all-tests.dox +2 -0
index 50429127..bcf9b7ed 100644
@@ -143,6 +143,8 @@ XSA-293 - See @ref test-pv-fsgsbase.
 @subpage test-xsa-298 - missing descriptor table limit checking in x86 PV
 emulation.
 
+@subpage test-xsa-consoleio-write - CONSOLEIO_write stack overflow
+
 
 @section index-utility Utilities
 
tests/xsa-consoleio-write/Makefile new file +9 -0
new file mode 100644
index 00000000..d189b4de
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := xsa-consoleio-write
+CATEGORY  := xsa
+TEST-ENVS := hvm32pae
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/xsa-consoleio-write/main.c new file +69 -0

69 lines not on this page · raw message

Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author Reviewed-by on v1 1/1
#20 lore raw

Re: [Xen-devel] [PATCH XTF] CONSOLEIO_write stack overflow PoC

Quoted 2 lines · Andrew Cooper

Classify it as an XSA test (which arguably ought to be named 'security'), despite no XSA being issues.

Nit: issued

Quoted 1 line

FWIW

with a remark and a question:

docs/all-tests.dox XSA-293 - See @ref test-pv-fsgsbase. · line 149

Quoted 10 lines
docs/all-tests.dox +2 -0
@@ -143,6 +143,8 @@ XSA-293 - See @ref test-pv-fsgsbase.
 @subpage test-xsa-298 - missing descriptor table limit checking in x86 PV
 emulation.
 
+@subpage test-xsa-consoleio-write - CONSOLEIO_write stack overflow
+
 
 @section index-utility Utilities

Do you really want two successive blank lines there?

Quoted 22 lines
tests/xsa-consoleio-write/main.c new file +19 -0
@@ -0,0 +1,69 @@
+/**
+ * @file tests/xsa-consoleio-write/main.c
+ * @ref test-xsa-consoleio-write
+ *
+ * This issue was discovered before it made it into any released version of
+ * Xen.  Therefore, no XSA or CVE was issued.
+ *
+ * A bugfix in Xen 4.13 altered CONSOLEIO_write to tolerate passing NUL
+ * characters intact, as this is a requirement for various TTY setups.
+ *
+ * A signed-ness issue with the length calculation lead to a case where Xen
+ * will copy between 2 and 4G of guest provided data into a 128 byte object on
+ * the stack.
+ *
+ * @see tests/xsa-consoleio-write/main.c
+ */
+#include <xtf.h>
+
+const char test_title[] = "CONSOLEIO_write stack overflow PoC";

tests/xsa-consoleio-write/main.c line 29

Quoted 10 lines
tests/xsa-consoleio-write/main.c new file +10 -0
+
+uint8_t zero_page[PAGE_SIZE] __page_aligned_bss;
+
+/* Have the assembler build an L1/L2 pair mapping zero_page[] many times. */
+asm (".section \".data.page_aligned\", \"aw\";"
+     ".align 4096;"
+
+     "l1t:"
+     ".rept 512;"
+     ".long zero_page + "STR(PF_SYM(AD, P))", 0;"

There being no further (runtime) adjustment to this and ...

tests/xsa-consoleio-write/main.c line 36

Quoted 7 lines
tests/xsa-consoleio-write/main.c new file +7 -0
+     ".endr;"
+     ".size l1t, . - l1t;"
+     ".type l1t, @object;"
+
+     "l2t:"
+     ".rept 512;"
+     ".long l1t + "STR(PF_SYM(AD, P))", 0;"

... this, is it set in stone that phys == lin in XTF tests? Or did you mean this to be hvm32, not hvm32pae?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [Xen-devel] [PATCH XTF] CONSOLEIO_write stack overflow PoC

Quoted 68 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

Classify it as an XSA test (which arguably ought to be named 'security'), despite no XSA being issues.

Nit: issued

Quoted 1 line

FWIW

with a remark and a question:

docs/all-tests.dox XSA-293 - See @ref test-pv-fsgsbase. · line 149

Quoted 10 lines
docs/all-tests.dox +2 -0
@@ -143,6 +143,8 @@ XSA-293 - See @ref test-pv-fsgsbase.
 @subpage test-xsa-298 - missing descriptor table limit checking in x86 PV
 emulation.
 
+@subpage test-xsa-consoleio-write - CONSOLEIO_write stack overflow
+
 
 @section index-utility Utilities

Do you really want two successive blank lines there?

Quoted 22 lines
tests/xsa-consoleio-write/main.c new file +19 -0
@@ -0,0 +1,69 @@
+/**
+ * @file tests/xsa-consoleio-write/main.c
+ * @ref test-xsa-consoleio-write
+ *
+ * This issue was discovered before it made it into any released version of
+ * Xen.  Therefore, no XSA or CVE was issued.
+ *
+ * A bugfix in Xen 4.13 altered CONSOLEIO_write to tolerate passing NUL
+ * characters intact, as this is a requirement for various TTY setups.
+ *
+ * A signed-ness issue with the length calculation lead to a case where Xen
+ * will copy between 2 and 4G of guest provided data into a 128 byte object on
+ * the stack.
+ *
+ * @see tests/xsa-consoleio-write/main.c
+ */
+#include <xtf.h>
+
+const char test_title[] = "CONSOLEIO_write stack overflow PoC";

tests/xsa-consoleio-write/main.c line 29

Quoted 10 lines
tests/xsa-consoleio-write/main.c new file +10 -0
+
+uint8_t zero_page[PAGE_SIZE] __page_aligned_bss;
+
+/* Have the assembler build an L1/L2 pair mapping zero_page[] many times. */
+asm (".section \".data.page_aligned\", \"aw\";"
+     ".align 4096;"
+
+     "l1t:"
+     ".rept 512;"
+     ".long zero_page + "STR(PF_SYM(AD, P))", 0;"

There being no further (runtime) adjustment to this and ...

tests/xsa-consoleio-write/main.c line 36

Quoted 7 lines
tests/xsa-consoleio-write/main.c new file +7 -0
+     ".endr;"
+     ".size l1t, . - l1t;"
+     ".type l1t, @object;"
+
+     "l2t:"
+     ".rept 512;"
+     ".long l1t + "STR(PF_SYM(AD, P))", 0;"
Quoted 2 lines

... this, is it set in stone that phys == lin in XTF tests? Or did you mean this to be hvm32, not hvm32pae?

Well, this last part was nonsense - there wouldn't be any page tables if it was hvm32. But the question remains.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

Re: [Xen-devel] [PATCH XTF] CONSOLEIO_write stack overflow PoC

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

Classify it as an XSA test (which arguably ought to be named 'security'), despite no XSA being issues.

Nit: issued

Will fix.

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

FWIW

with a remark and a question:

Quoted 6 lines
docs/all-tests.dox +0 -0
@@ -143,6 +143,8 @@ XSA-293 - See @ref test-pv-fsgsbase.
 @subpage test-xsa-298 - missing descriptor table limit checking in x86 PV
 emulation.
 
Quoted 5 lines
Quoted 5 lines

docs/all-tests.dox XSA-293 - See @ref test-pv-fsgsbase. · line 149

Quoted 4 lines
docs/all-tests.dox +2 -0
+@subpage test-xsa-consoleio-write - CONSOLEIO_write stack overflow
+
 
 @section index-utility Utilities

Do you really want two successive blank lines there?

Yes.  It is an awkward consequence of the doxygen markup for subpage and section looking very similar at a glance.

Having a double space is the only way to easily spot paragraph boundaries when skimming through the file.

Quoted 42 lines
Quoted 42 lines
Quoted 23 lines
tests/xsa-consoleio-write/main.c new file +19 -0
@@ -0,0 +1,69 @@
+/**
+ * @file tests/xsa-consoleio-write/main.c
+ * @ref test-xsa-consoleio-write
+ *
+ * This issue was discovered before it made it into any released version of
+ * Xen.  Therefore, no XSA or CVE was issued.
+ *
+ * A bugfix in Xen 4.13 altered CONSOLEIO_write to tolerate passing NUL
+ * characters intact, as this is a requirement for various TTY setups.
+ *
+ * A signed-ness issue with the length calculation lead to a case where Xen
+ * will copy between 2 and 4G of guest provided data into a 128 byte object on
+ * the stack.
+ *
+ * @see tests/xsa-consoleio-write/main.c
+ */
+#include <xtf.h>
+
+const char test_title[] = "CONSOLEIO_write stack overflow PoC";

tests/xsa-consoleio-write/main.c line 29

Quoted 10 lines
tests/xsa-consoleio-write/main.c new file +10 -0
+
+uint8_t zero_page[PAGE_SIZE] __page_aligned_bss;
+
+/* Have the assembler build an L1/L2 pair mapping zero_page[] many times. */
+asm (".section \".data.page_aligned\", \"aw\";"
+     ".align 4096;"
+
+     "l1t:"
+     ".rept 512;"
+     ".long zero_page + "STR(PF_SYM(AD, P))", 0;"

There being no further (runtime) adjustment to this and ...

Quoted 7 lines
tests/xsa-consoleio-write/main.c new file +7 -0
+     ".endr;"
+     ".size l1t, . - l1t;"
+     ".type l1t, @object;"
+
+     "l2t:"
+     ".rept 512;"
+     ".long l1t + "STR(PF_SYM(AD, P))", 0;"
Quoted 4 lines
Quoted 2 lines

... this, is it set in stone that phys == lin in XTF tests? Or did you mean this to be hvm32, not hvm32pae?

Well, this last part was nonsense - there wouldn't be any page tables if it was hvm32. But the question remains.

Yes.  XTF has an identity layout (and this is stated in mm.h), specifically for compatibility between unpaged and paged tests.

Any test wanting to do something more exciting is free to do so.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel