security_selinux.c 21.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2008-2010 Red Hat, Inc.
3 4 5 6 7 8 9 10
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Authors:
 *     James Morris <jmorris@namei.org>
11
 *     Dan Walsh <dwalsh@redhat.com>
12 13 14 15 16 17 18 19 20 21
 *
 * SELinux security driver.
 */
#include <config.h>
#include <selinux/selinux.h>
#include <selinux/context.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

22
#include "security_driver.h"
23 24 25 26
#include "security_selinux.h"
#include "virterror_internal.h"
#include "util.h"
#include "memory.h"
27
#include "logging.h"
28 29
#include "pci.h"
#include "hostusb.h"
30
#include "storage_file.h"
D
Daniel P. Berrange 已提交
31 32 33

#define VIR_FROM_THIS VIR_FROM_SECURITY

34
static char default_domain_context[1024];
35
static char default_content_context[1024];
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static char default_image_context[1024];
#define SECURITY_SELINUX_VOID_DOI       "0"
#define SECURITY_SELINUX_NAME "selinux"

/* TODO
   The data struct of used mcs should be replaced with a better data structure in the future
*/

struct MCS {
    char *mcs;
    struct MCS *next;
};
static struct MCS *mcsList = NULL;

static int
mcsAdd(const char *mcs)
{
    struct MCS *ptr;

    for (ptr = mcsList; ptr; ptr = ptr->next) {
D
Daniel P. Berrange 已提交
56
        if (STREQ(ptr->mcs, mcs))
57 58
            return -1;
    }
D
Daniel P. Berrange 已提交
59 60
    if (VIR_ALLOC(ptr) < 0)
        return -1;
61 62 63 64 65 66 67 68 69 70 71 72 73
    ptr->mcs = strdup(mcs);
    ptr->next = mcsList;
    mcsList = ptr;
    return 0;
}

static int
mcsRemove(const char *mcs)
{
    struct MCS *prevptr = NULL;
    struct MCS *ptr = NULL;

    for (ptr = mcsList; ptr; ptr = ptr->next) {
D
Daniel P. Berrange 已提交
74
        if (STREQ(ptr->mcs, mcs)) {
75 76 77 78 79
            if (prevptr)
                prevptr->next = ptr->next;
            else {
                mcsList = ptr->next;
            }
80 81
            VIR_FREE(ptr->mcs);
            VIR_FREE(ptr);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            return 0;
        }
        prevptr = ptr;
    }
    return -1;
}

static char *
SELinuxGenNewContext(const char *oldcontext, const char *mcs)
{
    char *newcontext = NULL;
    char *scontext = strdup(oldcontext);
    if (!scontext) goto err;
    context_t con = context_new(scontext);
    if (!con) goto err;
    context_range_set(con, mcs);
    newcontext = strdup(context_str(con));
    context_free(con);
err:
    freecon(scontext);
    return (newcontext);
}

static int
106
SELinuxInitialize(void)
107 108 109 110 111 112
{
    char *ptr = NULL;
    int fd = 0;

    fd = open(selinux_virtual_domain_context_path(), O_RDONLY);
    if (fd < 0) {
113
        virReportSystemError(errno,
114 115
                             _("cannot open SELinux virtual domain context file '%s'"),
                             selinux_virtual_domain_context_path());
116 117 118 119
        return -1;
    }

    if (saferead(fd, default_domain_context, sizeof(default_domain_context)) < 0) {
120
        virReportSystemError(errno,
121 122
                             _("cannot read SELinux virtual domain context file %s"),
                             selinux_virtual_domain_context_path());
123 124 125 126 127 128 129 130 131
        close(fd);
        return -1;
    }
    close(fd);

    ptr = strchrnul(default_domain_context, '\n');
    *ptr = '\0';

    if ((fd = open(selinux_virtual_image_context_path(), O_RDONLY)) < 0) {
132
        virReportSystemError(errno,
133 134
                             _("cannot open SELinux virtual image context file %s"),
                             selinux_virtual_image_context_path());
135 136 137 138
        return -1;
    }

    if (saferead(fd, default_image_context, sizeof(default_image_context)) < 0) {
139
        virReportSystemError(errno,
140 141
                             _("cannot read SELinux virtual image context file %s"),
                             selinux_virtual_image_context_path());
142 143 144 145 146 147
        close(fd);
        return -1;
    }
    close(fd);

    ptr = strchrnul(default_image_context, '\n');
148 149 150 151 152 153 154
    if (*ptr == '\n') {
        *ptr = '\0';
        strcpy(default_content_context, ptr+1);
        ptr = strchrnul(default_content_context, '\n');
        if (*ptr == '\n')
            *ptr = '\0';
    }
155 156 157 158
    return 0;
}

static int
159
SELinuxGenSecurityLabel(virDomainObjPtr vm)
160 161 162 163 164 165
{
    int rc = -1;
    char mcs[1024];
    char *scontext = NULL;
    int c1 = 0;
    int c2 = 0;
166

167 168 169
    if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

170 171 172
    if (vm->def->seclabel.label ||
        vm->def->seclabel.model ||
        vm->def->seclabel.imagelabel) {
173
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
174
                               "%s", _("security label already defined for VM"));
175
        return rc;
D
Daniel P. Berrange 已提交
176
    }
177 178 179 180 181 182 183 184

    do {
        c1 = virRandom(1024);
        c2 = virRandom(1024);

        if ( c1 == c2 ) {
            sprintf(mcs, "s0:c%d", c1);
        } else {
D
Daniel P. Berrange 已提交
185
            if ( c1 < c2 )
186 187 188 189 190 191 192
                sprintf(mcs, "s0:c%d,c%d", c1, c2);
            else
                sprintf(mcs, "s0:c%d,c%d", c2, c1);
        }
    } while(mcsAdd(mcs) == -1);

    vm->def->seclabel.label = SELinuxGenNewContext(default_domain_context, mcs);
D
Daniel P. Berrange 已提交
193
    if (! vm->def->seclabel.label)  {
194
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
D
Daniel P. Berrange 已提交
195 196 197
                               _("cannot generate selinux context for %s"), mcs);
        goto err;
    }
198
    vm->def->seclabel.imagelabel = SELinuxGenNewContext(default_image_context, mcs);
D
Daniel P. Berrange 已提交
199
    if (! vm->def->seclabel.imagelabel)  {
200
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
D
Daniel P. Berrange 已提交
201 202 203
                               _("cannot generate selinux context for %s"), mcs);
        goto err;
    }
204
    vm->def->seclabel.model = strdup(SECURITY_SELINUX_NAME);
205
    if (!vm->def->seclabel.model) {
206
        virReportOOMError();
D
Daniel P. Berrange 已提交
207 208 209
        goto err;
    }

210 211 212 213

    rc = 0;
    goto done;
err:
D
Daniel P. Berrange 已提交
214 215 216
    VIR_FREE(vm->def->seclabel.label);
    VIR_FREE(vm->def->seclabel.imagelabel);
    VIR_FREE(vm->def->seclabel.model);
217
done:
D
Daniel P. Berrange 已提交
218
    VIR_FREE(scontext);
219 220 221
    return rc;
}

222
static int
223
SELinuxReserveSecurityLabel(virDomainObjPtr vm)
224 225 226 227 228
{
    security_context_t pctx;
    context_t ctx = NULL;
    const char *mcs;

229 230 231
    if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

232
    if (getpidcon(vm->pid, &pctx) == -1) {
233
        virReportSystemError(errno,
234
                             _("unable to get PID %d security context"), vm->pid);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        return -1;
    }

    ctx = context_new(pctx);
    VIR_FREE(pctx);
    if (!ctx)
        goto err;

    mcs = context_range_get(ctx);
    if (!mcs)
        goto err;

    mcsAdd(mcs);

    context_free(ctx);

    return 0;

err:
    context_free(ctx);
    return -1;
}



260 261 262 263 264 265 266
static int
SELinuxSecurityDriverProbe(void)
{
    return is_selinux_enabled() ? SECURITY_DRIVER_ENABLE : SECURITY_DRIVER_DISABLE;
}

static int
267
SELinuxSecurityDriverOpen(virSecurityDriverPtr drv)
268 269 270 271 272
{
    /*
     * Where will the DOI come from?  SELinux configuration, or qemu
     * configuration? For the moment, we'll just set it to "0".
     */
273
    virSecurityDriverSetDOI(drv, SECURITY_SELINUX_VOID_DOI);
274
    return SELinuxInitialize();
275 276 277
}

static int
278
SELinuxGetSecurityProcessLabel(virDomainObjPtr vm,
279
                               virSecurityLabelPtr sec)
280 281 282 283
{
    security_context_t ctx;

    if (getpidcon(vm->pid, &ctx) == -1) {
284
        virReportSystemError(errno,
285 286
                             _("unable to get PID %d security context"),
                             vm->pid);
287 288 289 290
        return -1;
    }

    if (strlen((char *) ctx) >= VIR_SECURITY_LABEL_BUFLEN) {
291
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
292
                               _("security label exceeds "
C
Cole Robinson 已提交
293
                                 "maximum length: %d"),
294 295 296 297 298
                               VIR_SECURITY_LABEL_BUFLEN - 1);
        return -1;
    }

    strcpy(sec->label, (char *) ctx);
299
    VIR_FREE(ctx);
300 301 302

    sec->enforcing = security_getenforce();
    if (sec->enforcing == -1) {
303
        virReportSystemError(errno, "%s",
304
                             _("error calling security_getenforce()"));
305 306 307 308 309 310 311
        return -1;
    }

    return 0;
}

static int
312
SELinuxSetFilecon(const char *path, char *tcon)
313
{
314
    security_context_t econ;
315

316 317
    VIR_INFO("Setting SELinux context on '%s' to '%s'", path, tcon);

318
    if (setfilecon(path, tcon) < 0) {
319 320
        int setfilecon_errno = errno;

321 322 323 324 325 326 327 328
        if (getfilecon(path, &econ) >= 0) {
            if (STREQ(tcon, econ)) {
                freecon(econ);
                /* It's alright, there's nothing to change anyway. */
                return 0;
            }
            freecon(econ);
        }
329 330

        /* if the error complaint is related to an image hosted on
331 332
         * an nfs mount, or a usbfs/sysfs filesystem not supporting
         * labelling, then just ignore it & hope for the best.
333
         * The user hopefully set one of the necessary SELinux
334
         * virt_use_{nfs,usb,pci}  boolean tunables to allow it...
335 336
         */
        if (setfilecon_errno != EOPNOTSUPP) {
337
            virReportSystemError(setfilecon_errno,
338 339
                                 _("unable to set security context '%s' on '%s'"),
                                 tcon, path);
340 341
            if (security_getenforce() == 1)
                return -1;
342 343 344
        } else {
            VIR_INFO("Setting security context '%s' on '%s' not supported",
                     tcon, path);
345
        }
346 347 348 349 350
    }
    return 0;
}

static int
351
SELinuxRestoreSecurityFileLabel(const char *path)
352
{
353 354 355 356
    struct stat buf;
    security_context_t fcon = NULL;
    int rc = -1;
    char *newpath = NULL;
357

358 359
    VIR_INFO("Restoring SELinux context on '%s'", path);

360 361
    if (virFileResolveLink(path, &newpath) < 0) {
        virReportSystemError(errno,
D
Daniel P. Berrange 已提交
362 363
                             _("cannot resolve symlink %s"), path);
        goto err;
364
    }
365

366 367 368
    if (stat(newpath, &buf) != 0) {
        virReportSystemError(errno,
                             _("cannot stat %s"), newpath);
D
Daniel P. Berrange 已提交
369
        goto err;
370
    }
D
Daniel P. Berrange 已提交
371 372

    if (matchpathcon(newpath, buf.st_mode, &fcon) == 0)  {
373
        rc = SELinuxSetFilecon(newpath, fcon);
374 375 376 377
    } else {
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("cannot restore selinux file label for %s"),
                               newpath);
378
    }
379

380 381 382 383
err:
    VIR_FREE(fcon);
    VIR_FREE(newpath);
    return rc;
384 385
}

386
static int
387 388 389
SELinuxRestoreSecurityImageLabelInt(virDomainObjPtr vm,
                                    virDomainDiskDefPtr disk,
                                    int migrated)
390
{
391 392 393 394 395
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

396 397 398 399 400 401 402 403 404 405 406 407 408 409
    /* Don't restore labels on readoly/shared disks, because
     * other VMs may still be accessing these
     * Alternatively we could iterate over all running
     * domains and try to figure out if it is in use, but
     * this would not work for clustered filesystems, since
     * we can't see running VMs using the file on other nodes
     * Safest bet is thus to skip the restore step.
     */
    if (disk->readonly || disk->shared)
        return 0;

    if (!disk->src)
        return 0;

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    /* If we have a shared FS & doing migrated, we must not
     * change ownership, because that kills access on the
     * destination host which is sub-optimal for the guest
     * VM's I/O attempts :-)
     */
    if (migrated) {
        int rc = virStorageFileIsSharedFS(disk->src);
        if (rc < 0)
            return -1;
        if (rc == 1) {
            VIR_DEBUG("Skipping image label restore on %s because FS is shared",
                      disk->src);
            return 0;
        }
    }

426
    return SELinuxRestoreSecurityFileLabel(disk->src);
427 428
}

429 430 431 432 433 434 435 436 437

static int
SELinuxRestoreSecurityImageLabel(virDomainObjPtr vm,
                                 virDomainDiskDefPtr disk)
{
    return SELinuxRestoreSecurityImageLabelInt(vm, disk, 0);
}


438
static int
439
SELinuxSetSecurityImageLabel(virDomainObjPtr vm,
440
                             virDomainDiskDefPtr disk)
441 442 443

{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
444
    const char *path;
445

446 447 448
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

449 450 451
    if (!disk->src)
        return 0;

452 453 454 455 456
    path = disk->src;
    do {
        virStorageFileMetadata meta;
        int ret;

457
        ret = virStorageFileGetMetadata(path, &meta);
458 459 460 461 462 463

        if (path != disk->src)
            VIR_FREE(path);
        path = NULL;

        if (ret < 0)
464
           break;
465 466

        if (meta.backingStore != NULL &&
467
            SELinuxSetFilecon(meta.backingStore,
468 469 470 471 472 473 474 475
                              default_content_context) < 0) {
            VIR_FREE(meta.backingStore);
            return -1;
        }

        path = meta.backingStore;
    } while (path != NULL);

476
    if (disk->shared) {
477
        return SELinuxSetFilecon(disk->src, default_image_context);
478
    } else if (disk->readonly) {
479
        return SELinuxSetFilecon(disk->src, default_content_context);
480
    } else if (secdef->imagelabel) {
481
        return SELinuxSetFilecon(disk->src, secdef->imagelabel);
482
    }
483

484 485 486
    return 0;
}

487 488

static int
489
SELinuxSetSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
490 491 492 493 494
                           const char *file, void *opaque)
{
    virDomainObjPtr vm = opaque;
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

495
    return SELinuxSetFilecon(file, secdef->imagelabel);
496 497 498
}

static int
499
SELinuxSetSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
500 501 502 503 504
                           const char *file, void *opaque)
{
    virDomainObjPtr vm = opaque;
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

505
    return SELinuxSetFilecon(file, secdef->imagelabel);
506 507 508
}

static int
509
SELinuxSetSecurityHostdevLabel(virDomainObjPtr vm,
510 511 512
                               virDomainHostdevDefPtr dev)

{
513
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
514 515
    int ret = -1;

516 517 518
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

519 520 521 522 523
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
524
        usbDevice *usb = usbGetDevice(dev->source.subsys.u.usb.bus,
525
                                      dev->source.subsys.u.usb.device);
526

527 528
        if (!usb)
            goto done;
529

530
        ret = usbDeviceFileIterate(usb, SELinuxSetSecurityUSBLabel, vm);
531
        usbFreeDevice(usb);
M
Mark McLoughlin 已提交
532
        break;
533 534 535
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
536
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
537 538 539 540 541 542 543
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

544
        ret = pciDeviceFileIterate(pci, SELinuxSetSecurityPCILabel, vm);
545
        pciFreeDevice(pci);
546 547 548 549 550 551 552 553 554 555 556 557 558

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

559

560
static int
561
SELinuxRestoreSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
562 563 564
                               const char *file,
                               void *opaque ATTRIBUTE_UNUSED)
{
565
    return SELinuxRestoreSecurityFileLabel(file);
566 567 568
}

static int
569
SELinuxRestoreSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
570 571 572
                               const char *file,
                               void *opaque ATTRIBUTE_UNUSED)
{
573
    return SELinuxRestoreSecurityFileLabel(file);
574 575 576
}

static int
577
SELinuxRestoreSecurityHostdevLabel(virDomainObjPtr vm,
578 579 580
                                   virDomainHostdevDefPtr dev)

{
581
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
582 583
    int ret = -1;

584 585 586
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

587 588 589 590 591
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
592
        usbDevice *usb = usbGetDevice(dev->source.subsys.u.usb.bus,
593
                                      dev->source.subsys.u.usb.device);
594 595 596 597

        if (!usb)
            goto done;

598
        ret = usbDeviceFileIterate(usb, SELinuxRestoreSecurityUSBLabel, NULL);
599
        usbFreeDevice(usb);
600 601 602 603 604

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
605
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
606 607 608 609 610 611 612
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

613
        ret = pciDeviceFileIterate(pci, SELinuxRestoreSecurityPCILabel, NULL);
614
        pciFreeDevice(pci);
615 616 617 618 619 620 621 622 623 624 625 626 627

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

628
static int
629 630
SELinuxRestoreSecurityAllLabel(virDomainObjPtr vm,
                               int migrated ATTRIBUTE_UNUSED)
631 632 633 634
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
    int i;
    int rc = 0;
635 636 637

    VIR_DEBUG("Restoring security label on %s", vm->def->name);

638 639 640 641
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

    for (i = 0 ; i < vm->def->nhostdevs ; i++) {
642
        if (SELinuxRestoreSecurityHostdevLabel(vm, vm->def->hostdevs[i]) < 0)
643
            rc = -1;
644
    }
645
    for (i = 0 ; i < vm->def->ndisks ; i++) {
646 647 648
        if (SELinuxRestoreSecurityImageLabelInt(vm,
                                                vm->def->disks[i],
                                                migrated) < 0)
649 650
            rc = -1;
    }
651

652 653 654 655 656 657 658 659
    if (vm->def->os.kernel &&
        SELinuxRestoreSecurityFileLabel(vm->def->os.kernel) < 0)
        rc = -1;

    if (vm->def->os.initrd &&
        SELinuxRestoreSecurityFileLabel(vm->def->os.initrd) < 0)
        rc = -1;

660 661 662 663
    return rc;
}

static int
664
SELinuxReleaseSecurityLabel(virDomainObjPtr vm)
665 666 667
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

668 669
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC ||
        secdef->label == NULL)
670 671
        return 0;

672 673 674 675 676 677 678 679 680 681
    context_t con = context_new(secdef->label);
    if (con) {
        mcsRemove(context_range_get(con));
        context_free(con);
    }

    VIR_FREE(secdef->model);
    VIR_FREE(secdef->label);
    VIR_FREE(secdef->imagelabel);

682
    return 0;
683 684
}

685 686

static int
687
SELinuxSetSavedStateLabel(virDomainObjPtr vm,
688 689 690 691
                          const char *savefile)
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

692 693 694
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

695
    return SELinuxSetFilecon(savefile, secdef->imagelabel);
696 697 698 699
}


static int
700
SELinuxRestoreSavedStateLabel(virDomainObjPtr vm,
701 702
                              const char *savefile)
{
703 704 705 706 707
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

708
    return SELinuxRestoreSecurityFileLabel(savefile);
709 710 711
}


712
static int
713
SELinuxSecurityVerify(virDomainDefPtr def)
714 715 716 717
{
    const virSecurityLabelDefPtr secdef = &def->seclabel;
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC) {
        if (security_check_context(secdef->label) != 0) {
718
            virSecurityReportError(VIR_ERR_XML_ERROR,
719 720 721 722 723 724 725
                                   _("Invalid security label %s"), secdef->label);
            return -1;
        }
    }
    return 0;
}

726
static int
727
SELinuxSetSecurityProcessLabel(virSecurityDriverPtr drv,
728
                               virDomainObjPtr vm)
729 730 731 732
{
    /* TODO: verify DOI */
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;

733 734 735
    if (vm->def->seclabel.label == NULL)
        return 0;

736
    if (!STREQ(drv->name, secdef->model)) {
737
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
738 739 740 741
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
                               secdef->model, drv->name);
742
        if (security_getenforce() == 1)
743
            return -1;
744 745 746
    }

    if (setexeccon(secdef->label) == -1) {
747
        virReportSystemError(errno,
748 749
                             _("unable to set security context '%s'"),
                             secdef->label);
750
        if (security_getenforce() == 1)
751
            return -1;
752 753
    }

754 755 756 757
    return 0;
}

static int
758
SELinuxSetSecurityAllLabel(virDomainObjPtr vm)
759 760 761 762 763 764 765 766 767 768 769 770 771
{
    const virSecurityLabelDefPtr secdef = &vm->def->seclabel;
    int i;

    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC)
        return 0;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        /* XXX fixme - we need to recursively label the entire tree :-( */
        if (vm->def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_DIR) {
            VIR_WARN("Unable to relabel directory tree %s for disk %s",
                     vm->def->disks[i]->src, vm->def->disks[i]->dst);
            continue;
772
        }
773
        if (SELinuxSetSecurityImageLabel(vm, vm->def->disks[i]) < 0)
774 775 776
            return -1;
    }
    for (i = 0 ; i < vm->def->nhostdevs ; i++) {
777
        if (SELinuxSetSecurityHostdevLabel(vm, vm->def->hostdevs[i]) < 0)
778
            return -1;
779 780
    }

781 782 783 784 785 786 787 788
    if (vm->def->os.kernel &&
        SELinuxSetFilecon(vm->def->os.kernel, default_content_context) < 0)
        return -1;

    if (vm->def->os.initrd &&
        SELinuxSetFilecon(vm->def->os.initrd, default_content_context) < 0)
        return -1;

789 790 791 792 793 794 795
    return 0;
}

virSecurityDriver virSELinuxSecurityDriver = {
    .name                       = SECURITY_SELINUX_NAME,
    .probe                      = SELinuxSecurityDriverProbe,
    .open                       = SELinuxSecurityDriverOpen,
796
    .domainSecurityVerify       = SELinuxSecurityVerify,
797 798 799
    .domainSetSecurityImageLabel = SELinuxSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel = SELinuxRestoreSecurityImageLabel,
    .domainGenSecurityLabel     = SELinuxGenSecurityLabel,
800
    .domainReserveSecurityLabel     = SELinuxReserveSecurityLabel,
801 802 803 804 805
    .domainReleaseSecurityLabel     = SELinuxReleaseSecurityLabel,
    .domainGetSecurityProcessLabel     = SELinuxGetSecurityProcessLabel,
    .domainSetSecurityProcessLabel     = SELinuxSetSecurityProcessLabel,
    .domainRestoreSecurityAllLabel = SELinuxRestoreSecurityAllLabel,
    .domainSetSecurityAllLabel     = SELinuxSetSecurityAllLabel,
806 807
    .domainSetSecurityHostdevLabel = SELinuxSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel = SELinuxRestoreSecurityHostdevLabel,
808 809
    .domainSetSavedStateLabel = SELinuxSetSavedStateLabel,
    .domainRestoreSavedStateLabel = SELinuxRestoreSavedStateLabel,
810
};