Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

This is missing from the initial implementation, and causes issues
when mapping resources that don't have fixed or known sizes.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Tested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: stable@vger.kernel.org # >= 4.18
Link: https://lore.kernel.org/r/20210112115358.23346-1-roger.pau@citrix.com
Signed-off-by: Juergen Gross <jgross@suse.com>

authored by

Roger Pau Monne and committed by
Juergen Gross
ef3a575b 6190c0cc

+19 -6
+19 -6
drivers/xen/privcmd.c
··· 717 717 return 0; 718 718 } 719 719 720 - static long privcmd_ioctl_mmap_resource(struct file *file, void __user *udata) 720 + static long privcmd_ioctl_mmap_resource(struct file *file, 721 + struct privcmd_mmap_resource __user *udata) 721 722 { 722 723 struct privcmd_data *data = file->private_data; 723 724 struct mm_struct *mm = current->mm; 724 725 struct vm_area_struct *vma; 725 726 struct privcmd_mmap_resource kdata; 726 727 xen_pfn_t *pfns = NULL; 727 - struct xen_mem_acquire_resource xdata; 728 + struct xen_mem_acquire_resource xdata = { }; 728 729 int rc; 729 730 730 731 if (copy_from_user(&kdata, udata, sizeof(kdata))) ··· 734 733 /* If restriction is in place, check the domid matches */ 735 734 if (data->domid != DOMID_INVALID && data->domid != kdata.dom) 736 735 return -EPERM; 736 + 737 + /* Both fields must be set or unset */ 738 + if (!!kdata.addr != !!kdata.num) 739 + return -EINVAL; 740 + 741 + xdata.domid = kdata.dom; 742 + xdata.type = kdata.type; 743 + xdata.id = kdata.id; 744 + 745 + if (!kdata.addr && !kdata.num) { 746 + /* Query the size of the resource. */ 747 + rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata); 748 + if (rc) 749 + return rc; 750 + return __put_user(xdata.nr_frames, &udata->num); 751 + } 737 752 738 753 mmap_write_lock(mm); 739 754 ··· 785 768 } else 786 769 vma->vm_private_data = PRIV_VMA_LOCKED; 787 770 788 - memset(&xdata, 0, sizeof(xdata)); 789 - xdata.domid = kdata.dom; 790 - xdata.type = kdata.type; 791 - xdata.id = kdata.id; 792 771 xdata.frame = kdata.idx; 793 772 xdata.nr_frames = kdata.num; 794 773 set_xen_guest_handle(xdata.frame_list, pfns);