Skip to contentSkip to conversation

xen/arm: Correctly handle non-page aligned pointer in raw_copy_from_guest

PATCH In review Julien Grall 3 participants 5 messages Feb 18, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/arm: Correctly handle non-page aligned pointer in raw_copy_from_guest

The current implementation of raw_copy_guest helper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned to PAGE_SIZE.

When the total length is higher than a page, the length to read is badly compute with
min(len, (unsigned)(PAGE_SIZE - offset))

As the offset is only computed one time per function, if the start address was not aligned to PAGE_SIZE, we can end up in same iteration:
- to read accross page boundary => xen crash
- read the previous page => data corruption

This issue can be resolved by setting offset to 0 at the end of the first iteration. Indeed, after it, the virtual guest address is always aligned to PAGE_SIZE.

---
This patch is a bug fix for Xen 4.4. Without this patch the data may be corrupted when Xen is copied data from the guest if the guest virtual address is not aligned to PAGE_SIZE. Sometimes it can also crash Xen.

This function is used in numerous place in Xen. If it introduces another bug we can see quickly with small amount of data.

Changes in v2:
- Only raw_copy_from_guest is buggy, the other raw_copy_* helpers where safe because of the "offset = 0" at the end of the loop
- Update commit message and title

---
 xen/arch/arm/guestcopy.c |    5 +++++
 1 file changed, 5 insertions(+)
xen/arch/arm/guestcopy.c +5 -0
index af0af6b..715bb4e 100644
@@ -96,6 +96,11 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned le
         len -= size;
         from += size;
         to += size;
+        /*
+         * After the first iteration, guest virtual address is correctly
+         * aligned to PAGE_SIZE.
+         */
+        offset = 0;
     }
     return 0;
 }
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 20 lines · Julien Grall

The current implementation of raw_copy_guest helper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned to PAGE_SIZE.

When the total length is higher than a page, the length to read is badly compute with
min(len, (unsigned)(PAGE_SIZE - offset))

As the offset is only computed one time per function, if the start address was not aligned to PAGE_SIZE, we can end up in same iteration:
- to read accross page boundary => xen crash
- read the previous page => data corruption

This issue can be resolved by setting offset to 0 at the end of the first iteration. Indeed, after it, the virtual guest address is always aligned to PAGE_SIZE.

Quoted 7 lines

---
This patch is a bug fix for Xen 4.4. Without this patch the data may be corrupted when Xen is copied data from the guest if the guest virtual address is not aligned to PAGE_SIZE. Sometimes it can also crash Xen.

This function is used in numerous place in Xen. If it introduces another bug we can see quickly with small amount of data.

Quoted 25 lines

Changes in v2:
- Only raw_copy_from_guest is buggy, the other raw_copy_* helpers where safe because of the "offset = 0" at the end of the loop
- Update commit message and title

---
  xen/arch/arm/guestcopy.c |    5 +++++
  1 file changed, 5 insertions(+)
xen/arch/arm/guestcopy.c +5 -0
index af0af6b..715bb4e 100644
@@ -96,6 +96,11 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned le
          len -= size;
          from += size;
          to += size;
+        /*
+         * After the first iteration, guest virtual address is correctly
+         * aligned to PAGE_SIZE.
+         */
+        offset = 0;
      }
      return 0;
  }
Acked-by on v2 1/1
#2 lore raw
In reply to Julien Grall
Quoted 13 lines · Julien Grall

The current implementation of raw_copy_guest helper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned to PAGE_SIZE.

When the total length is higher than a page, the length to read is badly compute with
min(len, (unsigned)(PAGE_SIZE - offset))

As the offset is only computed one time per function, if the start address was not aligned to PAGE_SIZE, we can end up in same iteration:
- to read accross page boundary => xen crash
- read the previous page => data corruption

Quoted 5 lines

This issue can be resolved by setting offset to 0 at the end of the first iteration. Indeed, after it, the virtual guest address is always aligned to PAGE_SIZE.

Quoted 6 lines
Cc George Dunlap <[email protected]>

---
This patch is a bug fix for Xen 4.4. Without this patch the data may be corrupted when Xen is copied data from the guest if the guest virtual address is not aligned to PAGE_SIZE. Sometimes it can also crash Xen.

The crash is what makes this undoubtedly necessary for 4.4 IMHO.

Quoted 16 lines

This function is used in numerous place in Xen. If it introduces another bug we can see quickly with small amount of data.

Changes in v2:
- Only raw_copy_from_guest is buggy, the other raw_copy_* helpers where safe because of the "offset = 0" at the end of the loop
- Update commit message and title

---
 xen/arch/arm/guestcopy.c |    5 +++++
 1 file changed, 5 insertions(+)
xen/arch/arm/guestcopy.c +0 -0
index af0af6b..715bb4e 100644

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned le · line 102

Quoted 7 lines
xen/arch/arm/guestcopy.c +4 -0
@@ -96,6 +96,11 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned le
         len -= size;
         from += size;
         to += size;
+        /*
+         * After the first iteration, guest virtual address is correctly
+         * aligned to PAGE_SIZE.
+         */

I'd like to duplicate this comment in the other two places too -- if you are OK with it I will do that as part of committing.

Quoted 4 lines
xen/arch/arm/guestcopy.c +1 -0
+        offset = 0;
     }
     return 0;
 }
#3 lore raw
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Julien Grall

The current implementation of raw_copy_guest helper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned to PAGE_SIZE.

When the total length is higher than a page, the length to read is badly compute with
min(len, (unsigned)(PAGE_SIZE - offset))

As the offset is only computed one time per function, if the start address was not aligned to PAGE_SIZE, we can end up in same iteration:
- to read accross page boundary => xen crash
- read the previous page => data corruption

Quoted 7 lines
Quoted 5 lines

This issue can be resolved by setting offset to 0 at the end of the first iteration. Indeed, after it, the virtual guest address is always aligned to PAGE_SIZE.

and applied.

Quoted 7 lines

xen/arch/arm/guestcopy.c unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned le · line 102

Quoted 4 lines
xen/arch/arm/guestcopy.c +4 -0
+        /*
+         * After the first iteration, guest virtual address is correctly
+         * aligned to PAGE_SIZE.
+         */

I'd like to duplicate this comment in the other two places too -- if you are OK with it I will do that as part of committing.

I did this.

Ian.

#4 lore raw
Quoted 30 lines · Ian Campbell
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Julien Grall

The current implementation of raw_copy_guest helper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned to PAGE_SIZE.

When the total length is higher than a page, the length to read is badly compute with
min(len, (unsigned)(PAGE_SIZE - offset))

As the offset is only computed one time per function, if the start address was not aligned to PAGE_SIZE, we can end up in same iteration:
- to read accross page boundary => xen crash
- read the previous page => data corruption

Quoted 7 lines
Quoted 5 lines

This issue can be resolved by setting offset to 0 at the end of the first iteration. Indeed, after it, the virtual guest address is always aligned to PAGE_SIZE.

and applied.

Quoted 5 lines
Quoted 4 lines
xen/arch/arm/guestcopy.c +4 -0
+        /*
+         * After the first iteration, guest virtual address is correctly
+         * aligned to PAGE_SIZE.
+         */
Quoted 4 lines
Quoted 2 lines

I'd like to duplicate this comment in the other two places too -- if you are OK with it I will do that as part of committing.

I did this.

Thanks! I didn't see this part on the previous message.

Signature · 1 line
Julien Grall