]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
xen/privcmd: Add IOCTL_PRIVCMD_DM_OP
authorPaul Durrant <paul.durrant@citrix.com>
Mon, 13 Feb 2017 17:03:23 +0000 (17:03 +0000)
committerChuck Anderson <chuck.anderson@oracle.com>
Wed, 13 Sep 2017 04:37:53 +0000 (21:37 -0700)
Recently a new dm_op[1] hypercall was added to Xen to provide a mechanism
for restricting device emulators (such as QEMU) to a limited set of
hypervisor operations, and being able to audit those operations in the
kernel of the domain in which they run.

This patch adds IOCTL_PRIVCMD_DM_OP as gateway for __HYPERVISOR_dm_op.

NOTE: There is no requirement for user-space code to bounce data through
      locked memory buffers (as with IOCTL_PRIVCMD_HYPERCALL) since
      privcmd has enough information to lock the original buffers
      directly.

[1] http://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=524a98c2

Signed-off-by: Paul Durrant <paul.durrant@citrix.com>
Acked-by: Stefano Stabellini <sstabellini@kernel.org>
Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
OraBug: 26662731

(cherry picked from commit ab520be8cd5d56867fc95cfbc34b90880faf1f9d)
Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
Conflicts:
arch/arm/xen/enlighten.c
arch/arm/xen/hypercall.S
arch/arm64/xen/hypercall.S
        (uek doesn't define HYPERVISOR_vm_assist() for ARM)
        arch/arm/include/asm/xen/hypercall.h
        (Upstream defines HYPERVISOR_dm_op() in
         include/xen/arm/hypercall.h, which uek4 does not have)

arch/arm/include/asm/xen/hypercall.h
arch/arm/xen/enlighten.c
arch/arm/xen/hypercall.S
arch/arm64/xen/hypercall.S
arch/x86/include/asm/xen/hypercall.h
drivers/xen/privcmd.c
include/uapi/xen/privcmd.h
include/xen/interface/hvm/dm_op.h [new file with mode: 0644]
include/xen/interface/xen.h

index 712b50e0a6dc6901a5507ac89eeeeabdfefe4f19..04c2df450314ffdba9b55e2bae37d6fec90749c9 100644 (file)
@@ -49,6 +49,7 @@ int HYPERVISOR_memory_op(unsigned int cmd, void *arg);
 int HYPERVISOR_physdev_op(int cmd, void *arg);
 int HYPERVISOR_vcpu_op(int cmd, int vcpuid, void *extra_args);
 int HYPERVISOR_tmem_op(void *arg);
+int HYPERVISOR_dm_op(domid_t domid, unsigned int nr_bufs, void *bufs);
 int HYPERVISOR_multicall(struct multicall_entry *calls, uint32_t nr);
 
 static inline int
index ed24788a96eb42c14ef92b96a9ac941f1d02f62d..577377608e52f1a490c60d5cca9f7c7269a9742e 100644 (file)
@@ -391,4 +391,5 @@ EXPORT_SYMBOL_GPL(HYPERVISOR_physdev_op);
 EXPORT_SYMBOL_GPL(HYPERVISOR_vcpu_op);
 EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
 EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
+EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
 EXPORT_SYMBOL_GPL(privcmd_call);
index f00e080759384afd300398be488c5740f55f1091..d447ccb5a6fabb10d9703de3732d1fd4c66ce87b 100644 (file)
@@ -90,6 +90,7 @@ HYPERCALL2(physdev_op);
 HYPERCALL3(vcpu_op);
 HYPERCALL1(tmem_op);
 HYPERCALL2(multicall);
+HYPERCALL3(dm_op);
 
 ENTRY(privcmd_call)
        stmdb sp!, {r4}
index 8bbe9401f4f011d3239adcd2d6f5251d5fe37ff0..1e098f9ff848ee71c7ff8fef9bf354fff5efed79 100644 (file)
@@ -81,6 +81,7 @@ HYPERCALL2(physdev_op);
 HYPERCALL3(vcpu_op);
 HYPERCALL1(tmem_op);
 HYPERCALL2(multicall);
+HYPERCALL3(dm_op);
 
 ENTRY(privcmd_call)
        mov x16, x0
index 3bcdcc84259d98b919a7cc455b07082660bb6393..4fbd2f49305e44bb07dc176ab0b1f985811c2ffe 100644 (file)
@@ -471,6 +471,13 @@ HYPERVISOR_xenpmu_op(unsigned int op, void *arg)
        return _hypercall2(int, xenpmu_op, op, arg);
 }
 
+static inline int
+HYPERVISOR_dm_op(
+       domid_t dom, unsigned int nr_bufs, void *bufs)
+{
+       return _hypercall3(int, dm_op, dom, nr_bufs, bufs);
+}
+
 static inline void
 MULTI_fpu_taskswitch(struct multicall_entry *mcl, int set)
 {
index 5d9a102dbf6ca16f42b26840e12739f27997bd2e..f0f71acf27e757684da48f31e005ae0c6c8598cc 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/pagemap.h>
 #include <linux/seq_file.h>
 #include <linux/miscdevice.h>
+#include <linux/moduleparam.h>
 
 #include <asm/pgalloc.h>
 #include <asm/pgtable.h>
@@ -32,6 +33,7 @@
 #include <xen/xen.h>
 #include <xen/privcmd.h>
 #include <xen/interface/xen.h>
+#include <xen/interface/hvm/dm_op.h>
 #include <xen/features.h>
 #include <xen/page.h>
 #include <xen/xen-ops.h>
@@ -43,6 +45,17 @@ MODULE_LICENSE("GPL");
 
 #define PRIV_VMA_LOCKED ((void *)1)
 
+static unsigned int privcmd_dm_op_max_num = 16;
+module_param_named(dm_op_max_nr_bufs, privcmd_dm_op_max_num, uint, 0644);
+MODULE_PARM_DESC(dm_op_max_nr_bufs,
+                "Maximum number of buffers per dm_op hypercall");
+
+static unsigned int privcmd_dm_op_buf_max_size = 4096;
+module_param_named(dm_op_buf_max_size, privcmd_dm_op_buf_max_size, uint,
+                  0644);
+MODULE_PARM_DESC(dm_op_buf_max_size,
+                "Maximum size of a dm_op hypercall buffer");
+
 static int privcmd_vma_range_is_mapped(
                struct vm_area_struct *vma,
                unsigned long addr,
@@ -548,6 +561,128 @@ out_unlock:
        goto out;
 }
 
+static int lock_pages(
+       struct privcmd_dm_op_buf kbufs[], unsigned int num,
+       struct page *pages[], unsigned int nr_pages)
+{
+       unsigned int i;
+
+       for (i = 0; i < num; i++) {
+               unsigned int requested;
+               int pinned;
+
+               requested = DIV_ROUND_UP(
+                       offset_in_page(kbufs[i].uptr) + kbufs[i].size,
+                       PAGE_SIZE);
+               if (requested > nr_pages)
+                       return -ENOSPC;
+
+               pinned = get_user_pages_fast(
+                       (unsigned long) kbufs[i].uptr,
+                       requested, FOLL_WRITE, pages);
+               if (pinned < 0)
+                       return pinned;
+
+               nr_pages -= pinned;
+               pages += pinned;
+       }
+
+       return 0;
+}
+
+static void unlock_pages(struct page *pages[], unsigned int nr_pages)
+{
+       unsigned int i;
+
+       if (!pages)
+               return;
+
+       for (i = 0; i < nr_pages; i++) {
+               if (pages[i])
+                       put_page(pages[i]);
+       }
+}
+
+static long privcmd_ioctl_dm_op(void __user *udata)
+{
+       struct privcmd_dm_op kdata;
+       struct privcmd_dm_op_buf *kbufs;
+       unsigned int nr_pages = 0;
+       struct page **pages = NULL;
+       struct xen_dm_op_buf *xbufs = NULL;
+       unsigned int i;
+       long rc;
+
+       if (copy_from_user(&kdata, udata, sizeof(kdata)))
+               return -EFAULT;
+
+       if (kdata.num == 0)
+               return 0;
+
+       if (kdata.num > privcmd_dm_op_max_num)
+               return -E2BIG;
+
+       kbufs = kcalloc(kdata.num, sizeof(*kbufs), GFP_KERNEL);
+       if (!kbufs)
+               return -ENOMEM;
+
+       if (copy_from_user(kbufs, kdata.ubufs,
+                          sizeof(*kbufs) * kdata.num)) {
+               rc = -EFAULT;
+               goto out;
+       }
+
+       for (i = 0; i < kdata.num; i++) {
+               if (kbufs[i].size > privcmd_dm_op_buf_max_size) {
+                       rc = -E2BIG;
+                       goto out;
+               }
+
+               if (!access_ok(VERIFY_WRITE, kbufs[i].uptr,
+                              kbufs[i].size)) {
+                       rc = -EFAULT;
+                       goto out;
+               }
+
+               nr_pages += DIV_ROUND_UP(
+                       offset_in_page(kbufs[i].uptr) + kbufs[i].size,
+                       PAGE_SIZE);
+       }
+
+       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
+       if (!pages) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
+       if (!xbufs) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       rc = lock_pages(kbufs, kdata.num, pages, nr_pages);
+       if (rc)
+               goto out;
+
+       for (i = 0; i < kdata.num; i++) {
+               set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);
+               xbufs[i].size = kbufs[i].size;
+       }
+
+       xen_preemptible_hcall_begin();
+       rc = HYPERVISOR_dm_op(kdata.dom, kdata.num, xbufs);
+       xen_preemptible_hcall_end();
+
+out:
+       unlock_pages(pages, nr_pages);
+       kfree(xbufs);
+       kfree(pages);
+       kfree(kbufs);
+
+       return rc;
+}
+
 static long privcmd_ioctl(struct file *file,
                          unsigned int cmd, unsigned long data)
 {
@@ -571,6 +706,10 @@ static long privcmd_ioctl(struct file *file,
                ret = privcmd_ioctl_mmap_batch(udata, 2);
                break;
 
+       case IOCTL_PRIVCMD_DM_OP:
+               ret = privcmd_ioctl_dm_op(udata);
+               break;
+
        default:
                break;
        }
index 7ddeeda9380971b0327b09fc639eee9ebf82f1f7..f8c5d75b99e141c644ad6f0d9e9c2d62f2e95348 100644 (file)
@@ -77,6 +77,17 @@ struct privcmd_mmapbatch_v2 {
        int __user *err;  /* array of error codes */
 };
 
+struct privcmd_dm_op_buf {
+       void __user *uptr;
+       size_t size;
+};
+
+struct privcmd_dm_op {
+       domid_t dom;
+       __u16 num;
+       const struct privcmd_dm_op_buf __user *ubufs;
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -98,5 +109,7 @@ struct privcmd_mmapbatch_v2 {
        _IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch))
 #define IOCTL_PRIVCMD_MMAPBATCH_V2                             \
        _IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2))
+#define IOCTL_PRIVCMD_DM_OP                                    \
+       _IOC(_IOC_NONE, 'P', 5, sizeof(struct privcmd_dm_op))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
diff --git a/include/xen/interface/hvm/dm_op.h b/include/xen/interface/hvm/dm_op.h
new file mode 100644 (file)
index 0000000..ee9e480
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016, Citrix Systems Inc
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __XEN_PUBLIC_HVM_DM_OP_H__
+#define __XEN_PUBLIC_HVM_DM_OP_H__
+
+struct xen_dm_op_buf {
+       GUEST_HANDLE(void) h;
+       xen_ulong_t size;
+};
+DEFINE_GUEST_HANDLE_STRUCT(xen_dm_op_buf);
+
+#endif /* __XEN_PUBLIC_HVM_DM_OP_H__ */
index 1b0d189cd3d33f7ce19f739663afdcd7080703c9..4f4830ef8f93496df5d5c908ced15cc25ad1eb55 100644 (file)
@@ -81,6 +81,7 @@
 #define __HYPERVISOR_tmem_op              38
 #define __HYPERVISOR_xc_reserved_op       39 /* reserved for XenClient */
 #define __HYPERVISOR_xenpmu_op            40
+#define __HYPERVISOR_dm_op                41
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48