Skip to contentSkip to conversation

xen: Fix xenctl_cpumap_to_cpumask buffer size check

PATCH In review Matthew Daley 3 participants 5 messages Nov 13, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: Fix xenctl_cpumap_to_cpumask buffer size check

xenctl_cpumap_to_cpumask incorrectly uses sizeof when checking whether bits should be masked off from the input cpumap bitmap or not.

Fix by using the correct cpumask buffer size in place of sizeof.

xen/common/domctl.c +1 -1
index e153cb4..204e951 100644
@@ -78,7 +78,7 @@ int xenctl_cpumap_to_cpumask(
     {
         if ( copy_from_guest(bytemap, xenctl_cpumap->bitmap, copy_bytes) )
             err = -EFAULT;
-        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= sizeof(bytemap)) )
+        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= (nr_cpu_ids + 7) / 8) )
             bytemap[guest_bytes-1] &= ~(0xff << (xenctl_cpumap->nr_cpus & 7));
     }
 
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Matthew Daley

xenctl_cpumap_to_cpumask incorrectly uses sizeof when checking whether bits should be masked off from the input cpumap bitmap or not.

Fix by using the correct cpumask buffer size in place of sizeof.

However, I would have preferred the adjustment to be ...

xen/common/domctl.c int xenctl_cpumap_to_cpumask( · line 81

Quoted 10 lines
xen/common/domctl.c +1 -1
index e153cb4..204e951 100644
@@ -78,7 +78,7 @@ int xenctl_cpumap_to_cpumask(
     {
         if ( copy_from_guest(bytemap, xenctl_cpumap->bitmap, copy_bytes) )
             err = -EFAULT;
-        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= sizeof(bytemap)) )
+        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= (nr_cpu_ids + 7) / 8) )
        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= copy_bytes) )

or even (considering that guest_bytes >= copy_bytes due to the way copy_bytes gets initialized)

        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes == copy_bytes) )

to make explicit when exactly the masking is necessary.

Further, the fact this is not security relevant (i.e. the bug could not cause memory corruption) isn't obvious either: It is implied from _xmalloc() never returning chunks of data smaller than the size of a pointer, i.e. even if sizeof(void*) > guest_bytes > copy_bytes, the piece of memory erroneously written to would still be inside the allocation done at the top of the function. I'd suppose that would have been worth mentioning in the change description.

And, for the record (and in order to determine backporting needs), I caused this with c/s 23991:a7ccbc79fc17.

Jan

Quoted 11 lines
xen/common/domctl.c +0 -0
             bytemap[guest_bytes-1] &= ~(0xff << (xenctl_cpumap->nr_cpus & 7));
     }
 
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected] 
http://lists.xen.org/xen-devel 
Acked-by
#2 lore raw
Quoted 23 lines · Jan Beulich
Quoted 3 lines

Fix by using the correct cpumask buffer size in place of sizeof.

However, I would have preferred the adjustment to be ...

xen/common/domctl.c int xenctl_cpumap_to_cpumask( · line 83

Quoted 12 lines
xen/common/domctl.c +1 -1
index e153cb4..204e951 100644
@@ -78,7 +78,7 @@ int xenctl_cpumap_to_cpumask(
     {
         if ( copy_from_guest(bytemap, xenctl_cpumap->bitmap, copy_bytes) )
             err = -EFAULT;
-        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <=
 sizeof(bytemap)) )
+        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= (nr_cpu_ids +
 7) / 8) )
        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= copy_bytes) )
Quoted 6 lines

or even (considering that guest_bytes >= copy_bytes due to the way copy_bytes gets initialized)

        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes == copy_bytes) )

to make explicit when exactly the masking is necessary.

Any of the three alternatives is fine by me.

#3 lore raw

PATCH 1/1 xen: Fix xenctl_cpumap_to_cpumask buffer size check

In reply to Jan Beulich

xenctl_cpumap_to_cpumask incorrectly uses sizeof when checking whether bits should be masked off from the input cpumap bitmap or not.

Fix and make clearer by simply comparing the amount of bytes given in the input cpumap to the amount actually copied; if equal, bits may need to be masked off.

This does not have security impact: _xmalloc never returns allocations smaller than the size of a pointer, hence the uncorrected buffer size check would still not allow writes to unallocated memory.

---
Jan: Agreed with both of your points. Here's a v2.

xen/common/domctl.c +1 -1
index e153cb4..a7a6b9f 100644
@@ -78,7 +78,7 @@ int xenctl_cpumap_to_cpumask(
     {
         if ( copy_from_guest(bytemap, xenctl_cpumap->bitmap, copy_bytes) )
             err = -EFAULT;
-        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= sizeof(bytemap)) )
+        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes == copy_bytes) )
             bytemap[guest_bytes-1] &= ~(0xff << (xenctl_cpumap->nr_cpus & 7));
     }
 
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#4 lore raw
Quoted 7 lines · Matthew Daley

xenctl_cpumap_to_cpumask incorrectly uses sizeof when checking whether bits should be masked off from the input cpumap bitmap or not.

Fix and make clearer by simply comparing the amount of bytes given in the input cpumap to the amount actually copied; if equal, bits may need to be masked off.

Quoted 5 lines

This does not have security impact: _xmalloc never returns allocations smaller than the size of a pointer, hence the uncorrected buffer size check would still not allow writes to unallocated memory.

Quoted 18 lines

---
Jan: Agreed with both of your points. Here's a v2.

xen/common/domctl.c +1 -1
index e153cb4..a7a6b9f 100644
@@ -78,7 +78,7 @@ int xenctl_cpumap_to_cpumask(
     {
         if ( copy_from_guest(bytemap, xenctl_cpumap->bitmap, copy_bytes) )
             err = -EFAULT;
-        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes <= sizeof(bytemap))
 )
+        if ( (xenctl_cpumap->nr_cpus & 7) && (guest_bytes == copy_bytes) )
             bytemap[guest_bytes-1] &= ~(0xff << (xenctl_cpumap->nr_cpus &
 7));
     }