[PATCH 1/5] tools/python: handle libxl__physmap_info.name properly in convert-legacy-stream

Andrew Cooper posted 5 patches 4 years, 7 months ago
[PATCH 1/5] tools/python: handle libxl__physmap_info.name properly in convert-legacy-stream
Posted by Andrew Cooper 4 years, 7 months ago
From: Olaf Hering <olaf@aepfle.de>

The trailing member name[] in libxl__physmap_info is written as a
cstring into the stream. The current code does a sanity check if the
last byte is zero. This attempt fails with python3 because name[-1]
returns a type int. As a result the comparison with byte(\00) fails:

  File "/usr/lib/xen/bin/convert-legacy-stream", line 347, in read_libxl_toolstack
    raise StreamError("physmap name not NUL terminated")
  StreamError: physmap name not NUL terminated

To handle both python variants, cast to bytearray().

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
 tools/python/scripts/convert-legacy-stream | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
index 66ee3d2f5d29..227e1b5c3fd3 100755
--- a/tools/python/scripts/convert-legacy-stream
+++ b/tools/python/scripts/convert-legacy-stream
@@ -343,7 +343,7 @@ def read_libxl_toolstack(vm, data):
         if twidth == 64:
             name = name[:-4]
 
-        if name[-1] != b'\x00':
+        if bytearray(name)[-1] != 0:
             raise StreamError("physmap name not NUL terminated")
 
         root = b"physmap/%x" % (phys, )
-- 
2.11.0


Re: [PATCH 1/5] tools/python: handle libxl__physmap_info.name properly in convert-legacy-stream
Posted by Marek Marczykowski-Górecki 4 years, 7 months ago
On Mon, Jul 05, 2021 at 08:10:23PM +0100, Andrew Cooper wrote:
> From: Olaf Hering <olaf@aepfle.de>
> 
> The trailing member name[] in libxl__physmap_info is written as a
> cstring into the stream. The current code does a sanity check if the
> last byte is zero. This attempt fails with python3 because name[-1]
> returns a type int. As a result the comparison with byte(\00) fails:
> 
>   File "/usr/lib/xen/bin/convert-legacy-stream", line 347, in read_libxl_toolstack
>     raise StreamError("physmap name not NUL terminated")
>   StreamError: physmap name not NUL terminated
> 
> To handle both python variants, cast to bytearray().
> 
> Signed-off-by: Olaf Hering <olaf@aepfle.de>
> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

Acked-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>

> ---
>  tools/python/scripts/convert-legacy-stream | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/python/scripts/convert-legacy-stream b/tools/python/scripts/convert-legacy-stream
> index 66ee3d2f5d29..227e1b5c3fd3 100755
> --- a/tools/python/scripts/convert-legacy-stream
> +++ b/tools/python/scripts/convert-legacy-stream
> @@ -343,7 +343,7 @@ def read_libxl_toolstack(vm, data):
>          if twidth == 64:
>              name = name[:-4]
>  
> -        if name[-1] != b'\x00':
> +        if bytearray(name)[-1] != 0:
>              raise StreamError("physmap name not NUL terminated")
>  
>          root = b"physmap/%x" % (phys, )
> -- 
> 2.11.0
> 

-- 
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab