Skip to content

Commit 77fb64f

Browse files
jgross1opsiff
authored andcommitted
xen/privcmd: restrict usage in unprivileged domU
commit 453b8fb68f3641fea970db88b7d9a153ed2a37e8 upstream. 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 <teddy.astie@vates.tech> Fixes: 1c5de19 ("xen: add privcmd driver") Signed-off-by: Juergen Gross <jgross@suse.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> (cherry picked from commit 1879319d790f7d57622cdc22807b60ea78b56b6d) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent e3936f7 commit 77fb64f

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,6 +31,8 @@
3031
#include <linux/seq_file.h>
3132
#include <linux/miscdevice.h>
3233
#include <linux/moduleparam.h>
34+
#include <linux/notifier.h>
35+
#include <linux/wait.h>
3336

3437
#include <asm/xen/hypervisor.h>
3538
#include <asm/xen/hypercall.h>
@@ -43,6 +46,7 @@
4346
#include <xen/page.h>
4447
#include <xen/xen-ops.h>
4548
#include <xen/balloon.h>
49+
#include <xen/xenbus.h>
4650

4751
#include "privcmd.h"
4852

@@ -65,6 +69,11 @@ struct privcmd_data {
6569
domid_t domid;
6670
};
6771

72+
/* DOMID_INVALID implies no restriction */
73+
static domid_t target_domain = DOMID_INVALID;
74+
static bool restrict_wait;
75+
static DECLARE_WAIT_QUEUE_HEAD(restrict_wait_wq);
76+
6877
static int privcmd_vma_range_is_mapped(
6978
struct vm_area_struct *vma,
7079
unsigned long addr,
@@ -1156,13 +1165,16 @@ static long privcmd_ioctl(struct file *file,
11561165

11571166
static int privcmd_open(struct inode *ino, struct file *file)
11581167
{
1159-
struct privcmd_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
1168+
struct privcmd_data *data;
1169+
1170+
if (wait_event_interruptible(restrict_wait_wq, !restrict_wait) < 0)
1171+
return -EINTR;
11601172

1173+
data = kzalloc(sizeof(*data), GFP_KERNEL);
11611174
if (!data)
11621175
return -ENOMEM;
11631176

1164-
/* DOMID_INVALID implies no restriction */
1165-
data->domid = DOMID_INVALID;
1177+
data->domid = target_domain;
11661178

11671179
file->private_data = data;
11681180
return 0;
@@ -1255,13 +1267,55 @@ static struct miscdevice privcmd_dev = {
12551267
.fops = &xen_privcmd_fops,
12561268
};
12571269

1270+
static int init_restrict(struct notifier_block *notifier,
1271+
unsigned long event,
1272+
void *data)
1273+
{
1274+
char *target;
1275+
unsigned int domid;
1276+
1277+
/* Default to an guaranteed unused domain-id. */
1278+
target_domain = DOMID_IDLE;
1279+
1280+
target = xenbus_read(XBT_NIL, "target", "", NULL);
1281+
if (IS_ERR(target) || kstrtouint(target, 10, &domid)) {
1282+
pr_err("No target domain found, blocking all hypercalls\n");
1283+
goto out;
1284+
}
1285+
1286+
target_domain = domid;
1287+
1288+
out:
1289+
if (!IS_ERR(target))
1290+
kfree(target);
1291+
1292+
restrict_wait = false;
1293+
wake_up_all(&restrict_wait_wq);
1294+
1295+
return NOTIFY_DONE;
1296+
}
1297+
1298+
static struct notifier_block xenstore_notifier = {
1299+
.notifier_call = init_restrict,
1300+
};
1301+
1302+
static void __init restrict_driver(void)
1303+
{
1304+
restrict_wait = true;
1305+
1306+
register_xenstore_notifier(&xenstore_notifier);
1307+
}
1308+
12581309
static int __init privcmd_init(void)
12591310
{
12601311
int err;
12611312

12621313
if (!xen_domain())
12631314
return -ENODEV;
12641315

1316+
if (!xen_initial_domain())
1317+
restrict_driver();
1318+
12651319
err = misc_register(&privcmd_dev);
12661320
if (err != 0) {
12671321
pr_err("Could not register Xen privcmd device\n");

0 commit comments

Comments
 (0)