xen/arm: Correctly handle non-page aligned pointer in raw_copy_from_guest
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
@@ -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
Quoted 20 lines · Julien Grall
The current implementation of
raw_copy_guesthelper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned toPAGE_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 corruptionThis 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.Signed-off-by Julien Grall <[email protected]>Cc George Dunlap <[email protected]>
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 toPAGE_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:
- Onlyraw_copy_from_guestis 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@@ -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 ; }
Quoted 13 lines · Julien Grall
The current implementation of
raw_copy_guesthelper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned toPAGE_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.Signed-off-by Julien Grall <[email protected]>
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 toPAGE_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:
- Onlyraw_copy_from_guestis 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
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 ; }
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Julien Grall
The current implementation of
raw_copy_guesthelper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned toPAGE_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.Signed-off-by Julien Grall <[email protected]>Acked-by Ian Campbell <[email protected]>
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.
Quoted 30 lines · Ian Campbell
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Julien Grall
The current implementation of
raw_copy_guesthelper may lead to data corruption and sometimes Xen crash when the guest virtual address is not aligned toPAGE_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 corruptionQuoted 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.Signed-off-by Julien Grall <[email protected]>Acked-by Ian Campbell <[email protected]>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