Skip to contentSkip to conversation

xen: fix buffer over-read in bitmap_to_xenctl_bitmap()

PATCH Superseded Roger Pau Monne 3 participants 6 messages Apr 24, 2025 – Apr 25, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +7 -1
index bf1a7fd91e36..415d6bc074f6 100644
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }
+
     if ( !IS_ENABLED(LITTLE_ENDIAN) )
     {
         buf = xmalloc_array(uint8_t, xen_bytes);
@@ -396,7 +402,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
#1 lore raw
Quoted 3 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.

/sigh

Quoted 5 lines

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')

You ought to note that this is only not getting an XSA because 288c4641c80d isn't in a released Xen yet.

Quoted 9 lines
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +0 -0
index bf1a7fd91e36..415d6bc074f6 100644

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 376

Quoted 8 lines
xen/common/bitmap.c +5 -0
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }

I don't see any hypercalls performing a bits==0 check, so I expect this is reachable.

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 408

Quoted 13 lines
xen/common/bitmap.c +2 -1
+
     if ( !IS_ENABLED(LITTLE_ENDIAN) )
     {
         buf = xmalloc_array(uint8_t, xen_bytes);
@@ -396,7 +402,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;
 

This (preexisting) logic is mad.  The overwhelming majority of cases are going to be a multiple of 8, and as you notice, the 0 case can't be fixed like this.

It should all be inside a copy_bytes conditional as is done in xenctl_bitmap_to_bitmap().

~Andrew

#2 lore raw
Quoted 7 lines · Andrew Cooper
Quoted 3 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.

/sigh

Quoted 8 lines
Quoted 5 lines

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')

You ought to note that this is only not getting an XSA because 288c4641c80d isn't in a released Xen yet.

Yeah, I did explicitly check this wasn't backported to any stable branches.

Quoted 12 lines
Quoted 12 lines
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +0 -0
index bf1a7fd91e36..415d6bc074f6 100644
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
Quoted 8 lines

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 376

Quoted 5 lines
xen/common/bitmap.c +5 -0
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }

I don't see any hypercalls performing a bits==0 check, so I expect this is reachable.

bitmap_to_xenctl_bitmap() has just two callers, one passes nr_cpu_ids, the other MAX_NUMNODES. I think there are no callers that pass 0, much less from hypercall provided values.

Quoted 9 lines
Quoted 9 lines
xen/common/bitmap.c +1 -1
+
     if ( !IS_ENABLED(LITTLE_ENDIAN) )
     {
         buf = xmalloc_array(uint8_t, xen_bytes);
@@ -396,7 +402,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];
Quoted 8 lines

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 408

Quoted 4 lines
xen/common/bitmap.c +1 -0
+    last = bytemap[(nbits - 1) / 8];
     if ( nbits % 8 )
         last &= (1U << (nbits % 8)) - 1;
 

This (preexisting) logic is mad.  The overwhelming majority of cases are going to be a multiple of 8, and as you notice, the 0 case can't be fixed like this.

It's indeed a weird logic. The usage of last should be restricted to nbits % 8 != 0, and the rest of the cases handled directly by the copy_to_guest() call above this logic.

Quoted 2 lines

It should all be inside a copy_bytes conditional as is done in xenctl_bitmap_to_bitmap().

I could do it like that, but seeing the values passed by the only two callers it seemed less churn to add early check and assert.

Thanks, Roger.

#3 lore raw
Quoted 41 lines · Roger Pau Monné
Quoted 7 lines · Andrew Cooper
Quoted 3 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.

/sigh

Quoted 8 lines
Quoted 5 lines

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')

You ought to note that this is only not getting an XSA because 288c4641c80d isn't in a released Xen yet.

Yeah, I did explicitly check this wasn't backported to any stable branches.

Quoted 12 lines
Quoted 12 lines
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +0 -0
index bf1a7fd91e36..415d6bc074f6 100644
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
Quoted 8 lines

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 376

Quoted 5 lines
xen/common/bitmap.c +5 -0
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }

I don't see any hypercalls performing a bits==0 check, so I expect this is reachable.

Quoted 3 lines

bitmap_to_xenctl_bitmap() has just two callers, one passes nr_cpu_ids, the other MAX_NUMNODES. I think there are no callers that pass 0, much less from hypercall provided values.

Still I don't think there should be an assertion here, not even an error. As much as memcpy(x, y, 0) is okay, it ought to be okay to invoke this function for entirely void bitmaps.

Jan

#4 lore raw
Quoted 42 lines · Jan Beulich
Quoted 41 lines · Roger Pau Monné
Quoted 7 lines · Andrew Cooper
Quoted 3 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.

/sigh

Quoted 8 lines
Quoted 5 lines

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')

You ought to note that this is only not getting an XSA because 288c4641c80d isn't in a released Xen yet.

Yeah, I did explicitly check this wasn't backported to any stable branches.

Quoted 20 lines
Quoted 9 lines
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +0 -0
index bf1a7fd91e36..415d6bc074f6 100644

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 376

Quoted 8 lines
xen/common/bitmap.c +5 -0
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }

I don't see any hypercalls performing a bits==0 check, so I expect this is reachable.

Quoted 7 lines
Quoted 3 lines

bitmap_to_xenctl_bitmap() has just two callers, one passes nr_cpu_ids, the other MAX_NUMNODES. I think there are no callers that pass 0, much less from hypercall provided values.

Still I don't think there should be an assertion here, not even an error. As much as memcpy(x, y, 0) is okay, it ought to be okay to invoke this function for entirely void bitmaps.

OK, are we fine then with just returning early if bits == 0? No assert and no error.

Thanks, Roger.

#5 lore raw
Quoted 47 lines · Roger Pau Monné
Quoted 46 lines · Jan Beulich
Quoted 44 lines · Roger Pau Monné
Quoted 7 lines · Andrew Cooper
Quoted 3 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.

/sigh

Quoted 8 lines
Quoted 5 lines

While there also ensure that bitmap_to_xenctl_bitmap() is not called with a bitmap of 0 length.

Fixes 288c4641c80d ('xen: simplify bitmap_to_xenctl_bitmap for little endian')

You ought to note that this is only not getting an XSA because 288c4641c80d isn't in a released Xen yet.

Yeah, I did explicitly check this wasn't backported to any stable branches.

Quoted 12 lines
Quoted 12 lines
---
 xen/common/bitmap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
xen/common/bitmap.c +0 -0
index bf1a7fd91e36..415d6bc074f6 100644
@@ -369,6 +369,12 @@ int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap,
     const uint8_t *bytemap;
     uint8_t last, *buf = NULL;
 
Quoted 8 lines

xen/common/bitmap.c int bitmap_to_xenctl_bitmap(struct xenctl_bitmap *xenctl_bitmap, · line 376

Quoted 5 lines
xen/common/bitmap.c +5 -0
+    if ( !nbits )
+    {
+	ASSERT_UNREACHABLE();
+	return -EILSEQ;
+    }

I don't see any hypercalls performing a bits==0 check, so I expect this is reachable.

bitmap_to_xenctl_bitmap() has just two callers, one passes nr_cpu_ids, the other MAX_NUMNODES. I think there are no callers that pass 0, much less from hypercall provided values.

Quoted 6 lines
Quoted 3 lines

Still I don't think there should be an assertion here, not even an error. As much as memcpy(x, y, 0) is okay, it ought to be okay to invoke this function for entirely void bitmaps.

OK, are we fine then with just returning early if bits == 0? No assert and no error.

I'd be fine with that, yes.

Jan