]> xenbits.xensource.com Git - xen.git/commitdiff
xen: fix buffer over-read in bitmap_to_xenctl_bitmap()
authorRoger Pau Monne <roger.pau@citrix.com>
Thu, 24 Apr 2025 10:23:14 +0000 (12:23 +0200)
committerRoger Pau Monne <roger.pau@citrix.com>
Fri, 25 Apr 2025 12:50:19 +0000 (14:50 +0200)
There's an off-by-one when calculating the last byte in the input array to
bitmap_to_xenctl_bitmap(), which leads to bitmaps with sizes multiple of 8
to over-read and incorrectly use a byte past the end of the array.

Fixes: 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
xen/common/bitmap.c

index bf1a7fd91e36bc8983389bb89a990b9f432d12a5..4f96fda3895c47c45c307d20a60466e6bf1b0cbe 100644 (file)
@@ -369,6 +369,9 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
+    if ( !nbits )
+        return 0;
+
     if ( !IS_ENABLED(LITTLE_ENDIAN) )
     {
         buf = xmalloc_array(uint8_t, xen_bytes);
@@ -396,7 +399,7 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
      * their loops to 8 bits. Ensure we clear those left over bits so as to
      * prevent surprises.
      */
-    last = bytemap[nbits / 8];
+    last = bytemap[(nbits - 1) / 8];
     if ( nbits % 8 )
         last &= (1U << (nbits % 8)) - 1;