xen/privcmd: add IOCTL_PRIVCMD_RESTRICT_DOMID
RFC PATCH 1/1 xen/privcmd: add IOCTL_PRIVCMD_RESTRICT_DOMID
This restricts the file descriptor to only being able map foreign memory belonging to a specific domain. Once a file descriptor has been restricted its restriction cannot be removed or changed.
A device model (e.g., QEMU) or similar can make use of this before dropping privileges to prevent the file descriptor being used to escalate privleges if the process is compromised.
FIXME: This is not good enough (yet) as it does not restrict what hypercalls may be performed. Fixing this requires a hypervisor ABI change.
--- drivers/xen/privcmd.c | 75 ++++++++++++++++++++++++++++++++++++++++++---- include/uapi/xen/privcmd.h | 26 ++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-)
drivers/xen/privcmd.c +70 -5
@@ -43,6 +43,18 @@ MODULE_LICENSE("GPL");#define PRIV_VMA_LOCKED ((void *)1) +#define UNRESTRICTED_DOMID ((domid_t)-1) + +struct privcmd_data { +domid_t restrict_domid; +}; + +static bool privcmd_is_allowed (struct privcmd_data *priv,domid_t domid) +{ +return priv->restrict_domid ==UNRESTRICTED_DOMID + || priv->restrict_domid == domid; +} + static int privcmd_vma_range_is_mapped( struct vm_area_struct *vma, unsigned long addr,
@@ -229,7 +241,7 @@ static int mmap_gfn_range(void *data, void *state)return 0 ; } -static long privcmd_ioctl_mmap (void __user *udata) +static long privcmd_ioctl_mmap (struct privcmd_data *priv,void __user *udata) {struct privcmd_mmap mmapcmd;struct mm_struct *mm = current->mm;
@@ -245,6 +257,9 @@ static long privcmd_ioctl_mmap(void __user *udata)if (copy_from_user (&mmapcmd, udata,sizeof (mmapcmd)))return -EFAULT ; +if (!privcmd_is_allowed (priv, mmapcmd.dom)) +return -EACCES ; + rc =gather_array (&pagelist, mmapcmd.num,sizeof (struct privcmd_mmap_entry ), mmapcmd.entry);
@@ -416,7 +431,8 @@ static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)static const struct vm_operations_struct privcmd_vm_ops; -static long privcmd_ioctl_mmap_batch (void __user *udata,int version) +static long privcmd_ioctl_mmap_batch (struct privcmd_data *priv,void __user *udata, +int version) {int ret;struct privcmd_mmapbatch_v2 m;
@@ -446,6 +462,9 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)return -EINVAL ; } +if (!privcmd_is_allowed (priv, m.dom)) +return -EACCES ; + nr_pages =DIV_ROUND_UP (m.num,XEN_PFN_PER_PAGE );if ((m.num <=0 ) || (nr_pages > (LONG_MAX >>PAGE_SHIFT )))return -EINVAL ;
@@ -548,9 +567,28 @@ out_unlock: goto out; } +static int privcmd_ioctl_restrict_domid (struct privcmd_data *priv, +void __user *udata) +{ +struct privcmd_restrict_domid prd; + +if (copy_from_user (&prd, udata,sizeof (prd))) +return -EFAULT ; + +if (prd.domid >=DOMID_FIRST_RESERVED ) +return -EINVAL ; +if (priv->restrict_domid !=UNRESTRICTED_DOMID ) +return -EACCES ; + + priv->restrict_domid = prd.domid; + +return 0 ; +} +static long privcmd_ioctl (struct file *file,unsigned int cmd,unsigned long data) { +struct privcmd_data *priv = file->private_data;int ret = -ENOSYS ;void __user *udata = (void __user *) data;
@@ -560,15 +598,19 @@ static long privcmd_ioctl(struct file *file,break ;case IOCTL_PRIVCMD_MMAP : - ret =privcmd_ioctl_mmap (udata); + ret =privcmd_ioctl_mmap (priv, udata);break ;case IOCTL_PRIVCMD_MMAPBATCH : - ret =privcmd_ioctl_mmap_batch (udata,1 ); + ret =privcmd_ioctl_mmap_batch (priv, udata,1 );break ;case IOCTL_PRIVCMD_MMAPBATCH_V2 : - ret =privcmd_ioctl_mmap_batch (udata,2 ); + ret =privcmd_ioctl_mmap_batch (priv, udata,2 ); +break ; + +case IOCTL_PRIVCMD_RESTRICT_DOMID : + ret =privcmd_ioctl_restrict_domid (priv, udata);break ;default :
@@ -644,10 +686,33 @@ static int privcmd_vma_range_is_mapped( is_mapped_fn, NULL) != 0; } +static int privcmd_open (struct inode *ino,struct file *filp) +{ +struct privcmd_data *priv; + + priv =kzalloc (sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->restrict_domid =UNRESTRICTED_DOMID ; + +return 0 ; +} + +static int privcmd_release (struct inode *inode,struct file *file) +{ +struct privcmd_data *priv = file->private_data; + +kfree (priv); +return 0 ; +} +const struct file_operations xen_privcmd_fops = { .owner =THIS_MODULE , .unlocked_ioctl = privcmd_ioctl, .mmap = privcmd_mmap, + .open = privcmd_open, + .release = privcmd_release, };EXPORT_SYMBOL_GPL (xen_privcmd_fops);
include/uapi/xen/privcmd.h +26 -0
@@ -77,6 +77,10 @@ struct privcmd_mmapbatch_v2 {int __user *err;/* array of error codes */ }; +struct privcmd_restrict_domid { +domid_t domid; +}; +/* * @cmd: IOCTL_PRIVCMD_HYPERCALL * @arg: &privcmd_hypercall_t
@@ -99,4 +103,26 @@ struct privcmd_mmapbatch_v2 {#define IOCTL_PRIVCMD_MMAPBATCH_V2 \ _IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2)) +/* +* @cmd: IOCTL_PRIVCMD_RESTRICT_DOMID +* @arg: struct privcmd_restrict_domid * +* Return: 0 on success, or -1 (with errno set). +* +* This restricts the file descriptor to only being able map foreign +* memory belonging to a specific domain. Once a file descriptor has +* been restricted its restriction cannot be removed or changed. +* +* A device model (e.g., QEMU) or similar can make use of this before +* dropping privileges to prevent the file descriptor being used to +* escalate privleges if the process is compromised. +* +* FIXME: This is not good enough (yet) as it does not restrict what +* hypercalls may be performed. +* +* EINVAL - the specified domid isn't valid. +* EACCES - the file descriptor has already been restricted. +*/ +#define IOCTL_PRIVCMD_RESTRICT_DOMID \ + _IOC(_IOC_NONE, 'P', 5, sizeof(struct privcmd_restrict_domid)) +#endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
drivers/xen/privcmd.c MODULE_LICENSE("GPL"); · line 46
Quoted 10 lines · David Vrabel
drivers/xen/privcmd.c +1
-0@@ -43,6 +43,18 @@ MODULE_LICENSE("GPL");#define PRIV_VMA_LOCKED ((void *)1) +#define UNRESTRICTED_DOMID ((domid_t)-1)
This can probably go into a header file since you've used the same macro for event channel restricted domains.
drivers/xen/privcmd.c MODULE_LICENSE("GPL"); · line 56
Quoted 10 lines
drivers/xen/privcmd.c +10
-0+ +struct privcmd_data { +domid_t restrict_domid; +}; + +static bool privcmd_is_allowed (struct privcmd_data *priv,domid_t domid) +{ +return priv->restrict_domid ==UNRESTRICTED_DOMID + || priv->restrict_domid == domid; +}
I also wonder whether this can be made useful to event channels (and possibly other operations we might want to try restricting in the future).
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel