Skip to content

Commit ab520be

Browse files
Paul Durrantbostrovs
authored andcommitted
xen/privcmd: Add IOCTL_PRIVCMD_DM_OP
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>
1 parent dc9eab6 commit ab520be

9 files changed

Lines changed: 196 additions & 0 deletions

File tree

‎arch/arm/xen/enlighten.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,5 @@ EXPORT_SYMBOL_GPL(HYPERVISOR_tmem_op);
457457
EXPORT_SYMBOL_GPL(HYPERVISOR_platform_op);
458458
EXPORT_SYMBOL_GPL(HYPERVISOR_multicall);
459459
EXPORT_SYMBOL_GPL(HYPERVISOR_vm_assist);
460+
EXPORT_SYMBOL_GPL(HYPERVISOR_dm_op);
460461
EXPORT_SYMBOL_GPL(privcmd_call);

‎arch/arm/xen/hypercall.S‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ HYPERCALL1(tmem_op);
9292
HYPERCALL1(platform_op_raw);
9393
HYPERCALL2(multicall);
9494
HYPERCALL2(vm_assist);
95+
HYPERCALL3(dm_op);
9596

9697
ENTRY(privcmd_call)
9798
stmdb sp!, {r4}

‎arch/arm64/xen/hypercall.S‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ HYPERCALL1(tmem_op);
8484
HYPERCALL1(platform_op_raw);
8585
HYPERCALL2(multicall);
8686
HYPERCALL2(vm_assist);
87+
HYPERCALL3(dm_op);
8788

8889
ENTRY(privcmd_call)
8990
mov x16, x0

‎arch/x86/include/asm/xen/hypercall.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ HYPERVISOR_xenpmu_op(unsigned int op, void *arg)
472472
return _hypercall2(int, xenpmu_op, op, arg);
473473
}
474474

475+
static inline int
476+
HYPERVISOR_dm_op(
477+
domid_t dom, unsigned int nr_bufs, void *bufs)
478+
{
479+
return _hypercall3(int, dm_op, dom, nr_bufs, bufs);
480+
}
481+
475482
static inline void
476483
MULTI_fpu_taskswitch(struct multicall_entry *mcl, int set)
477484
{

‎drivers/xen/privcmd.c‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/pagemap.h>
2323
#include <linux/seq_file.h>
2424
#include <linux/miscdevice.h>
25+
#include <linux/moduleparam.h>
2526

2627
#include <asm/pgalloc.h>
2728
#include <asm/pgtable.h>
@@ -32,6 +33,7 @@
3233
#include <xen/xen.h>
3334
#include <xen/privcmd.h>
3435
#include <xen/interface/xen.h>
36+
#include <xen/interface/hvm/dm_op.h>
3537
#include <xen/features.h>
3638
#include <xen/page.h>
3739
#include <xen/xen-ops.h>
@@ -43,6 +45,17 @@ MODULE_LICENSE("GPL");
4345

4446
#define PRIV_VMA_LOCKED ((void *)1)
4547

48+
static unsigned int privcmd_dm_op_max_num = 16;
49+
module_param_named(dm_op_max_nr_bufs, privcmd_dm_op_max_num, uint, 0644);
50+
MODULE_PARM_DESC(dm_op_max_nr_bufs,
51+
"Maximum number of buffers per dm_op hypercall");
52+
53+
static unsigned int privcmd_dm_op_buf_max_size = 4096;
54+
module_param_named(dm_op_buf_max_size, privcmd_dm_op_buf_max_size, uint,
55+
0644);
56+
MODULE_PARM_DESC(dm_op_buf_max_size,
57+
"Maximum size of a dm_op hypercall buffer");
58+
4659
static int privcmd_vma_range_is_mapped(
4760
struct vm_area_struct *vma,
4861
unsigned long addr,
@@ -548,6 +561,128 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
548561
goto out;
549562
}
550563

564+
static int lock_pages(
565+
struct privcmd_dm_op_buf kbufs[], unsigned int num,
566+
struct page *pages[], unsigned int nr_pages)
567+
{
568+
unsigned int i;
569+
570+
for (i = 0; i < num; i++) {
571+
unsigned int requested;
572+
int pinned;
573+
574+
requested = DIV_ROUND_UP(
575+
offset_in_page(kbufs[i].uptr) + kbufs[i].size,
576+
PAGE_SIZE);
577+
if (requested > nr_pages)
578+
return -ENOSPC;
579+
580+
pinned = get_user_pages_fast(
581+
(unsigned long) kbufs[i].uptr,
582+
requested, FOLL_WRITE, pages);
583+
if (pinned < 0)
584+
return pinned;
585+
586+
nr_pages -= pinned;
587+
pages += pinned;
588+
}
589+
590+
return 0;
591+
}
592+
593+
static void unlock_pages(struct page *pages[], unsigned int nr_pages)
594+
{
595+
unsigned int i;
596+
597+
if (!pages)
598+
return;
599+
600+
for (i = 0; i < nr_pages; i++) {
601+
if (pages[i])
602+
put_page(pages[i]);
603+
}
604+
}
605+
606+
static long privcmd_ioctl_dm_op(void __user *udata)
607+
{
608+
struct privcmd_dm_op kdata;
609+
struct privcmd_dm_op_buf *kbufs;
610+
unsigned int nr_pages = 0;
611+
struct page **pages = NULL;
612+
struct xen_dm_op_buf *xbufs = NULL;
613+
unsigned int i;
614+
long rc;
615+
616+
if (copy_from_user(&kdata, udata, sizeof(kdata)))
617+
return -EFAULT;
618+
619+
if (kdata.num == 0)
620+
return 0;
621+
622+
if (kdata.num > privcmd_dm_op_max_num)
623+
return -E2BIG;
624+
625+
kbufs = kcalloc(kdata.num, sizeof(*kbufs), GFP_KERNEL);
626+
if (!kbufs)
627+
return -ENOMEM;
628+
629+
if (copy_from_user(kbufs, kdata.ubufs,
630+
sizeof(*kbufs) * kdata.num)) {
631+
rc = -EFAULT;
632+
goto out;
633+
}
634+
635+
for (i = 0; i < kdata.num; i++) {
636+
if (kbufs[i].size > privcmd_dm_op_buf_max_size) {
637+
rc = -E2BIG;
638+
goto out;
639+
}
640+
641+
if (!access_ok(VERIFY_WRITE, kbufs[i].uptr,
642+
kbufs[i].size)) {
643+
rc = -EFAULT;
644+
goto out;
645+
}
646+
647+
nr_pages += DIV_ROUND_UP(
648+
offset_in_page(kbufs[i].uptr) + kbufs[i].size,
649+
PAGE_SIZE);
650+
}
651+
652+
pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
653+
if (!pages) {
654+
rc = -ENOMEM;
655+
goto out;
656+
}
657+
658+
xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
659+
if (!xbufs) {
660+
rc = -ENOMEM;
661+
goto out;
662+
}
663+
664+
rc = lock_pages(kbufs, kdata.num, pages, nr_pages);
665+
if (rc)
666+
goto out;
667+
668+
for (i = 0; i < kdata.num; i++) {
669+
set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);
670+
xbufs[i].size = kbufs[i].size;
671+
}
672+
673+
xen_preemptible_hcall_begin();
674+
rc = HYPERVISOR_dm_op(kdata.dom, kdata.num, xbufs);
675+
xen_preemptible_hcall_end();
676+
677+
out:
678+
unlock_pages(pages, nr_pages);
679+
kfree(xbufs);
680+
kfree(pages);
681+
kfree(kbufs);
682+
683+
return rc;
684+
}
685+
551686
static long privcmd_ioctl(struct file *file,
552687
unsigned int cmd, unsigned long data)
553688
{
@@ -571,6 +706,10 @@ static long privcmd_ioctl(struct file *file,
571706
ret = privcmd_ioctl_mmap_batch(udata, 2);
572707
break;
573708

709+
case IOCTL_PRIVCMD_DM_OP:
710+
ret = privcmd_ioctl_dm_op(udata);
711+
break;
712+
574713
default:
575714
break;
576715
}

‎include/uapi/xen/privcmd.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ struct privcmd_mmapbatch_v2 {
7777
int __user *err; /* array of error codes */
7878
};
7979

80+
struct privcmd_dm_op_buf {
81+
void __user *uptr;
82+
size_t size;
83+
};
84+
85+
struct privcmd_dm_op {
86+
domid_t dom;
87+
__u16 num;
88+
const struct privcmd_dm_op_buf __user *ubufs;
89+
};
90+
8091
/*
8192
* @cmd: IOCTL_PRIVCMD_HYPERCALL
8293
* @arg: &privcmd_hypercall_t
@@ -98,5 +109,7 @@ struct privcmd_mmapbatch_v2 {
98109
_IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch))
99110
#define IOCTL_PRIVCMD_MMAPBATCH_V2 \
100111
_IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2))
112+
#define IOCTL_PRIVCMD_DM_OP \
113+
_IOC(_IOC_NONE, 'P', 5, sizeof(struct privcmd_dm_op))
101114

102115
#endif /* __LINUX_PUBLIC_PRIVCMD_H__ */

‎include/xen/arm/hypercall.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ int HYPERVISOR_physdev_op(int cmd, void *arg);
5353
int HYPERVISOR_vcpu_op(int cmd, int vcpuid, void *extra_args);
5454
int HYPERVISOR_tmem_op(void *arg);
5555
int HYPERVISOR_vm_assist(unsigned int cmd, unsigned int type);
56+
int HYPERVISOR_dm_op(domid_t domid, unsigned int nr_bufs, void *bufs);
5657
int HYPERVISOR_platform_op_raw(void *arg);
5758
static inline int HYPERVISOR_platform_op(struct xen_platform_op *op)
5859
{

‎include/xen/interface/hvm/dm_op.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2016, Citrix Systems Inc
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef __XEN_PUBLIC_HVM_DM_OP_H__
24+
#define __XEN_PUBLIC_HVM_DM_OP_H__
25+
26+
struct xen_dm_op_buf {
27+
GUEST_HANDLE(void) h;
28+
xen_ulong_t size;
29+
};
30+
DEFINE_GUEST_HANDLE_STRUCT(xen_dm_op_buf);
31+
32+
#endif /* __XEN_PUBLIC_HVM_DM_OP_H__ */

‎include/xen/interface/xen.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#define __HYPERVISOR_tmem_op 38
8282
#define __HYPERVISOR_xc_reserved_op 39 /* reserved for XenClient */
8383
#define __HYPERVISOR_xenpmu_op 40
84+
#define __HYPERVISOR_dm_op 41
8485

8586
/* Architecture-specific hypercall definitions. */
8687
#define __HYPERVISOR_arch_0 48

0 commit comments

Comments
 (0)