Skip to contentSkip to conversation

xen: fix buffer over-read in bitmap_to_xenctl_bitmap()

PATCH In review Roger Pau Monne 2 participants 2 messages Apr 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen: fix buffer over-read in bitmap_to_xenctl_bitmap()

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')

---
Changes since v1:
- Handle length 0.

---
 xen/common/bitmap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
xen/common/bitmap.c +4 -1
index bf1a7fd91e36..4f96fda3895c 100644
@@ -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;
 
Signature · 1 line
2.48.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Roger Pau Monne

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')