libs/gnttab: fix FreeBSD gntdev interface
PATCH 1/1 libs/gnttab: fix FreeBSD gntdev interface
Current interface to the gntdev in FreeBSD is wrong, and mostly worked out of luck before the PTI FreeBSD fixes, when kernel and user-space where sharing the same page tables.
On FreeBSD ioctls have the size of the passed struct encoded in the ioctl number, because the generic ioctl handler in the OS takes care of copying the data from user-space to kernel space, and then calls the device specific ioctl handler. Thus using ioctl structs with variable sizes is not possible.
The fix is to turn the array of structs at the end of ioctl_gntdev_alloc_gref and ioctl_gntdev_map_grant_ref into pointers, that can be properly accessed from the kernel gntdev driver using the copyin/copyout functions. Note that this is exactly how it's done for the privcmd driver.
---
Rationale for acceptance into 4.11:
- Without this fix the grant table device is not usable on FreeBSD.
- It affects FreeBSD code exclusively, there's no risk for Linux or other OSes.
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Juergen Gross <[email protected]>
--- tools/include/xen-sys/FreeBSD/gntdev.h | 4 +- tools/libs/gnttab/freebsd.c | 63 +++++++++++++------------- 2 files changed, 33 insertions(+), 34 deletions(-)
tools/include/xen-sys/FreeBSD/gntdev.h +2 -2
@@ -138,7 +138,7 @@ struct ioctl_gntdev_alloc_gref {/* OUT parameters */ uint64_t index;/* Variable OUT parameter */ -uint32_t gref_ids[1 ]; +uint32_t *gref_ids; };#define GNTDEV_ALLOC_FLAG_WRITABLE 1
@@ -167,7 +167,7 @@ struct ioctl_gntdev_map_grant_ref {/* OUT parameters */ uint64_t index;/* Variable IN parameter */ -struct ioctl_gntdev_grant_ref refs[1 ]; +struct ioctl_gntdev_grant_ref *refs; };#define IOCTL_GNTDEV_UNMAP_GRANT_REF \
tools/libs/gnttab/freebsd.c +31 -32
144 lines not on this page · raw message
Signature · 7 lines
2.17.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 10 lines · Roger Pau Monne
Current interface to the gntdev in FreeBSD is wrong, and mostly worked out of luck before the PTI FreeBSD fixes, when kernel and user-space where sharing the same page tables.
On FreeBSD ioctls have the size of the passed struct encoded in the ioctl number, because the generic ioctl handler in the OS takes care of copying the data from user-space to kernel space, and then calls the device specific ioctl handler. Thus using ioctl structs with variable sizes is not possible.
Quoted 7 lines
The fix is to turn the array of structs at the end of
ioctl_gntdev_alloc_grefandioctl_gntdev_map_grant_refinto pointers, that can be properly accessed from the kernel gntdev driver using the copyin/copyout functions. Note that this is exactly how it's done for the privcmd driver.Signed-off-by Roger Pau Monné <[email protected]>
Juergen
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 3 lines · Roger Pau Monne
Current interface to the gntdev in FreeBSD is wrong, and mostly worked out of luck before the PTI FreeBSD fixes, when kernel and user-space where sharing the same page tables.
where -> were?
Quoted 7 lines
On FreeBSD ioctls have the size of the passed struct encoded in the ioctl number, because the generic ioctl handler in the OS takes care of copying the data from user-space to kernel space, and then calls the device specific ioctl handler. Thus using ioctl structs with variable sizes is not possible.
Quoted 7 lines
The fix is to turn the array of structs at the end of
ioctl_gntdev_alloc_grefandioctl_gntdev_map_grant_refinto pointers, that can be properly accessed from the kernel gntdev driver using the copyin/copyout functions. Note that this is exactly how it's done for the privcmd driver.Signed-off-by Roger Pau Monné <[email protected]>
Not sure I follow. Isn't turning the array into pointer still results in a variable length array?
The code looks sane to me.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 20 lines · Wei Liu
Quoted 3 lines · Roger Pau Monne
Current interface to the gntdev in FreeBSD is wrong, and mostly worked out of luck before the PTI FreeBSD fixes, when kernel and user-space where sharing the same page tables.
where -> were?
Quoted 13 lines
On FreeBSD ioctls have the size of the passed struct encoded in the ioctl number, because the generic ioctl handler in the OS takes care of copying the data from user-space to kernel space, and then calls the device specific ioctl handler. Thus using ioctl structs with variable sizes is not possible.
The fix is to turn the array of structs at the end of
ioctl_gntdev_alloc_grefandioctl_gntdev_map_grant_refinto pointers, that can be properly accessed from the kernel gntdev driver using the copyin/copyout functions. Note that this is exactly how it's done for the privcmd driver.
Quoted 4 lines
Quoted 1 line
Signed-off-by Roger Pau Monné <[email protected]>Not sure I follow. Isn't turning the array into pointer still results in a variable length array?
But it won't be a flexible array member, which is what causes the issue, it will be an independent pointer. Doing something like:
copyin(kernel_space, user_space, sizeof(struct ioctl)); copyin(krefs_array, kernel_space->refs, sizeof(...));
Will work properly.
The problem with the current layout is that the first copyin is automatically performed by the ioctl generic system handler, and thus the sizeof will be wrong because it will use the layout of the struct as defined in the header, that has only one element in the array.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Wei Liu
Acked-byQuoted 32 lines · Roger Pau Monné
Quoted 20 lines · Wei Liu
Quoted 3 lines · Roger Pau Monne
Current interface to the gntdev in FreeBSD is wrong, and mostly worked out of luck before the PTI FreeBSD fixes, when kernel and user-space where sharing the same page tables.
where -> were?
Quoted 13 lines
On FreeBSD ioctls have the size of the passed struct encoded in the ioctl number, because the generic ioctl handler in the OS takes care of copying the data from user-space to kernel space, and then calls the device specific ioctl handler. Thus using ioctl structs with variable sizes is not possible.
The fix is to turn the array of structs at the end of
ioctl_gntdev_alloc_grefandioctl_gntdev_map_grant_refinto pointers, that can be properly accessed from the kernel gntdev driver using the copyin/copyout functions. Note that this is exactly how it's done for the privcmd driver.Quoted 4 lines
Quoted 1 line
Signed-off-by Roger Pau Monné <[email protected]>Not sure I follow. Isn't turning the array into pointer still results in a variable length array?
But it won't be a flexible array member, which is what causes the issue, it will be an independent pointer. Doing something like:
copyin(kernel_space, user_space, sizeof(struct ioctl));copyin(krefs_array, kernel_space->refs, sizeof(...));
Quoted 7 lines
Will work properly.
The problem with the current layout is that the first copyin is automatically performed by the ioctl generic system handler, and thus the sizeof will be wrong because it will use the layout of the struct as defined in the header, that has only one element in the array.
I see.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel