Skip to content

Commit 453b8fb

Browse files
committed
xen/privcmd: restrict usage in unprivileged domU
The Xen privcmd driver allows to issue arbitrary hypercalls from user space processes. This is normally no problem, as access is usually limited to root and the hypervisor will deny any hypercalls affecting other domains. In case the guest is booted using secure boot, however, the privcmd driver would be enabling a root user process to modify e.g. kernel memory contents, thus breaking the secure boot feature. The only known case where an unprivileged domU is really needing to use the privcmd driver is the case when it is acting as the device model for another guest. In this case all hypercalls issued via the privcmd driver will target that other guest. Fortunately the privcmd driver can already be locked down to allow only hypercalls targeting a specific domain, but this mode can be activated from user land only today. The target domain can be obtained from Xenstore, so when not running in dom0 restrict the privcmd driver to that target domain from the beginning, resolving the potential problem of breaking secure boot. This is XSA-482 Reported-by: Teddy Astie <[email protected]> Fixes: 1c5de19 ("xen: add privcmd driver") Signed-off-by: Juergen Gross <[email protected]> --- V2: - defer reading from Xenstore if Xenstore isn't ready yet (Jan Beulich) - wait in open() if target domain isn't known yet - issue message in case no target domain found (Jan Beulich)
1 parent 0e4f8f1 commit 453b8fb

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

drivers/xen/privcmd.c

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/eventfd.h>
1313
#include <linux/file.h>
1414
#include <linux/kernel.h>
15+
#include <linux/kstrtox.h>
1516
#include <linux/module.h>
1617
#include <linux/mutex.h>
1718
#include <linux/poll.h>
@@ -30,7 +31,9 @@
3031
#include <linux/seq_file.h>
3132
#include <linux/miscdevice.h>
3233
#include <linux/moduleparam.h>
34+
#include <linux/notifier.h>
3335
#include <linux/virtio_mmio.h>
36+
#include <linux/wait.h>
3437

3538
#include <asm/xen/hypervisor.h>
3639
#include <asm/xen/hypercall.h>
@@ -46,6 +49,7 @@
4649
#include <xen/page.h>
4750
#include <xen/xen-ops.h>
4851
#include <xen/balloon.h>
52+
#include <xen/xenbus.h>
4953
#ifdef CONFIG_XEN_ACPI
5054
#include <xen/acpi.h>
5155
#endif
@@ -72,6 +76,11 @@ struct privcmd_data {
7276
domid_t domid;
7377
};
7478

79+
/* DOMID_INVALID implies no restriction */
80+
static domid_t target_domain = DOMID_INVALID;
81+
static bool restrict_wait;
82+
static DECLARE_WAIT_QUEUE_HEAD(restrict_wait_wq);
83+
7584
static int privcmd_vma_range_is_mapped(
7685
struct vm_area_struct *vma,
7786
unsigned long addr,
@@ -1563,13 +1572,16 @@ static long privcmd_ioctl(struct file *file,
15631572

15641573
static int privcmd_open(struct inode *ino, struct file *file)
15651574
{
1566-
struct privcmd_data *data = kzalloc_obj(*data);
1575+
struct privcmd_data *data;
1576+
1577+
if (wait_event_interruptible(restrict_wait_wq, !restrict_wait) < 0)
1578+
return -EINTR;
15671579

1580+
data = kzalloc_obj(*data);
15681581
if (!data)
15691582
return -ENOMEM;
15701583

1571-
/* DOMID_INVALID implies no restriction */
1572-
data->domid = DOMID_INVALID;
1584+
data->domid = target_domain;
15731585

15741586
file->private_data = data;
15751587
return 0;
@@ -1662,13 +1674,55 @@ static struct miscdevice privcmd_dev = {
16621674
.fops = &xen_privcmd_fops,
16631675
};
16641676

1677+
static int init_restrict(struct notifier_block *notifier,
1678+
unsigned long event,
1679+
void *data)
1680+
{
1681+
char *target;
1682+
unsigned int domid;
1683+
1684+
/* Default to an guaranteed unused domain-id. */
1685+
target_domain = DOMID_IDLE;
1686+
1687+
target = xenbus_read(XBT_NIL, "target", "", NULL);
1688+
if (IS_ERR(target) || kstrtouint(target, 10, &domid)) {
1689+
pr_err("No target domain found, blocking all hypercalls\n");
1690+
goto out;
1691+
}
1692+
1693+
target_domain = domid;
1694+
1695+
out:
1696+
if (!IS_ERR(target))
1697+
kfree(target);
1698+
1699+
restrict_wait = false;
1700+
wake_up_all(&restrict_wait_wq);
1701+
1702+
return NOTIFY_DONE;
1703+
}
1704+
1705+
static struct notifier_block xenstore_notifier = {
1706+
.notifier_call = init_restrict,
1707+
};
1708+
1709+
static void __init restrict_driver(void)
1710+
{
1711+
restrict_wait = true;
1712+
1713+
register_xenstore_notifier(&xenstore_notifier);
1714+
}
1715+
16651716
static int __init privcmd_init(void)
16661717
{
16671718
int err;
16681719

16691720
if (!xen_domain())
16701721
return -ENODEV;
16711722

1723+
if (!xen_initial_domain())
1724+
restrict_driver();
1725+
16721726
err = misc_register(&privcmd_dev);
16731727
if (err != 0) {
16741728
pr_err("Could not register Xen privcmd device\n");

0 commit comments

Comments
 (0)