Skip to contentSkip to conversation

xen/privcmd: allow fetching resource sizes

PATCH Superseded Roger Pau Monne 4 participants 8 messages Jan 11, 2021 – Jan 12, 2021 Xen hypervisor interface

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/privcmd: allow fetching resource sizes

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

Fixes 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')

---
NB: fetching the size of a resource shouldn't trigger an hypercall preemption, and hence I've dropped the preempt indications.
---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: [email protected]

---
 drivers/xen/privcmd.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/xen/privcmd.c +15 -6
index b0c73c58f987..a6e7e6e4286f 100644
@@ -717,14 +717,15 @@ static long privcmd_ioctl_restrict(struct file *file, void __user *udata)
 	return 0;
 }
 
-static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
+static long privcmd_ioctl_mmap_resource(struct file *file,
+				struct privcmd_mmap_resource __user *udata)
 {
 	struct privcmd_data *data = file->private_data;
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	struct privcmd_mmap_resource kdata;
 	xen_pfn_t *pfns = NULL;
-	struct xen_mem_acquire_resource xdata;
+	struct xen_mem_acquire_resource xdata = { };
 	int rc;
 
 	if (copy_from_user(&kdata, udata, sizeof(kdata)))
@@ -734,6 +735,18 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 	if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
 		return -EPERM;
 
+	xdata.domid = kdata.dom;
+	xdata.type = kdata.type;
+	xdata.id = kdata.id;
+
+	if (!kdata.addr && !kdata.num) {
+		/* Query the size of the resource. */
+		rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
+		if (rc)
+			return rc;
+		return __put_user(xdata.nr_frames, &udata->num);
+	}
+
 	mmap_write_lock(mm);
 
 	vma = find_vma(mm, kdata.addr);
@@ -768,10 +781,6 @@ static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata)
 	} else
 		vma->vm_private_data = PRIV_VMA_LOCKED;
 
-	memset(&xdata, 0, sizeof(xdata));
-	xdata.domid = kdata.dom;
-	xdata.type = kdata.type;
-	xdata.id = kdata.id;
 	xdata.frame = kdata.idx;
 	xdata.nr_frames = kdata.num;
 	set_xen_guest_handle(xdata.frame_list, pfns);
Signature · 1 line
2.29.2
#1 lore raw

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata) · line 742

Quoted 6 lines · Roger Pau Monne
drivers/xen/privcmd.c +5 -0
+	xdata.domid = kdata.dom;
+	xdata.type = kdata.type;
+	xdata.id = kdata.id;
+
+	if (!kdata.addr && !kdata.num) {

I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.

(And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)

-boris

#2 lore raw
Quoted 11 lines · On 11/01/2021 22:09, [email protected] wrote:

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata) · line 742

Quoted 6 lines · Roger Pau Monne
drivers/xen/privcmd.c +5 -0
+	xdata.domid = kdata.dom;
+	xdata.type = kdata.type;
+	xdata.id = kdata.id;
+
+	if (!kdata.addr && !kdata.num) {

I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.

Quoted 1 line

(And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)

Passing addr == 0 without num being 0 is already an error in Xen, and passing num == 0 without addr being 0 is bogus and will be an error by the time I'm finished fixing this.

FWIW, the common usecase for non-trivial examples will be:

xenforeignmem_resource_size(domid, type, id, &size); xenforeignmem_map_resource(domid, type, id, NULL, size, ...);

which translates into:

ioctl(MAP_RESOURCE, NULL, 0) => size
mmap(NULL, size, ...) => ptr
ioctl(MAP_RESOURCE, ptr, size)

from the kernels point of view, and two hypercalls from Xen's point of view.  The NULL's above are expected to be the common case for letting the kernel chose the vma, but ought to be filled in by the time the second ioctl() occurs.

See
https://lore.kernel.org/xen-devel/[email protected]/T/#u
for all the gory details.

~Andrew

maintainer
#3 lore raw
Quoted 29 lines · Andrew Cooper
Quoted 11 lines · On 11/01/2021 22:09, [email protected] wrote:

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata) · line 742

Quoted 6 lines · Roger Pau Monne
drivers/xen/privcmd.c +5 -0
+	xdata.domid = kdata.dom;
+	xdata.type = kdata.type;
+	xdata.id = kdata.id;
+
+	if (!kdata.addr && !kdata.num) {

I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.

Quoted 1 line

(And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)

Passing addr == 0 without num being 0 is already an error in Xen, and passing num == 0 without addr being 0 is bogus and will be an error by the time I'm finished fixing this.

FWIW, the common usecase for non-trivial examples will be:

xenforeignmem_resource_size(domid, type, id, &size); xenforeignmem_map_resource(domid, type, id, NULL, size, ...);

which translates into:

ioctl(MAP_RESOURCE, NULL, 0) => size
mmap(NULL, size, ...) => ptr
ioctl(MAP_RESOURCE, ptr, size)

Quoted 8 lines

from the kernels point of view, and two hypercalls from Xen's point of view.  The NULL's above are expected to be the common case for letting the kernel chose the vma, but ought to be filled in by the time the second ioctl() occurs.

See
https://lore.kernel.org/xen-devel/[email protected]/T/#u
for all the gory details.

I don't think the kernel should rely on the hypervisor to return an error in case addr != 0 and num == 0.

The driver should return -EINVAL in that case IMO.

Juergen

maintainer
#4 lore raw
Quoted 37 lines · Jürgen Groß
Quoted 36 lines · Andrew Cooper
Quoted 11 lines · On 11/01/2021 22:09, [email protected] wrote:
Quoted 5 lines · Roger Pau Monne

+    xdata.domid = kdata.dom;
+    xdata.type = kdata.type;
+    xdata.id = kdata.id;
+
+    if (!kdata.addr && !kdata.num) {

I think we should not allow only one of them to be zero. If it's only kdata.num then we will end up with pfns array set to ZERO_SIZE_PTR (which is 0x10). We seem to be OK in that we are not derefencing pfns (either in kernel or in hypervisor) if number of frames is zero but IMO we shouldn't be tempting the fate.

(And if it's only kdata.addr then we will get a vma but I am not sure it will do what we want.)

Passing addr == 0 without num being 0 is already an error in Xen, and passing num == 0 without addr being 0 is bogus and will be an error by the time I'm finished fixing this.

FWIW, the common usecase for non-trivial examples will be:

xenforeignmem_resource_size(domid, type, id, &size); xenforeignmem_map_resource(domid, type, id, NULL, size, ...);

which translates into:

ioctl(MAP_RESOURCE, NULL, 0) => size
mmap(NULL, size, ...) => ptr
ioctl(MAP_RESOURCE, ptr, size)

from the kernels point of view, and two hypercalls from Xen's point of view.  The NULL's above are expected to be the common case for letting the kernel chose the vma, but ought to be filled in by the time the second ioctl() occurs.

See
https://lore.kernel.org/xen-devel/[email protected]/T/#u

Quoted 6 lines
Quoted 1 line

for all the gory details.

I don't think the kernel should rely on the hypervisor to return an error in case addr != 0 and num == 0.

The driver should return -EINVAL in that case IMO.

And additionally I think the kernel should check num to be not too large (in the interface it is u64, while intermediate values are stored in unsigned int), limiting it to something below INT_MAX seems to be sensible.

Juergen

maintainer
#5 lore raw
In reply to Roger Pau Monne
Quoted 7 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

Fixes 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')

I don't think this addition is a reason to add a "Fixes:" tag. This is clearly new functionality.

Juergen

#6 lore raw
Quoted 11 lines · Jürgen Groß
Quoted 7 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

Fixes 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')

I don't think this addition is a reason to add a "Fixes:" tag. This is clearly new functionality.

It could be argued that not allowing to query the resource size was a shortcoming of the original implementation, but a backport request to stable would be more appropriate than a fixes tag I think. Will drop on next version and add a backport request if you agree.

Thanks, Roger.

maintainer
#7 lore raw
Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Jürgen Groß
Quoted 7 lines · Roger Pau Monne

Allow issuing an IOCTL_PRIVCMD_MMAP_RESOURCE ioctl with num = 0 and addr = 0 in order to fetch the size of a specific resource.

Add a shortcut to the default map resource path, since fetching the size requires no address to be passed in, and thus no VMA to setup.

Fixes 3ad0876554caf ('xen/privcmd: add IOCTL_PRIVCMD_MMAP_RESOURCE')
Quoted 7 lines
Quoted 2 lines

I don't think this addition is a reason to add a "Fixes:" tag. This is clearly new functionality.

It could be argued that not allowing to query the resource size was a shortcoming of the original implementation, but a backport request to stable would be more appropriate than a fixes tag I think. Will drop on next version and add a backport request if you agree.

Yes, please.

Juergen