security_selinux.c 59.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2008-2012 Red Hat, Inc.
3 4 5 6 7 8
 *
 * 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.
 *
O
Osier Yang 已提交
9 10 11 12 13 14
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library.  If not, see
O
Osier Yang 已提交
16 17
 * <http://www.gnu.org/licenses/>.
 *
18 19
 * Authors:
 *     James Morris <jmorris@namei.org>
20
 *     Dan Walsh <dwalsh@redhat.com>
21 22 23 24 25 26 27 28 29
 *
 * 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>
E
Eric Blake 已提交
30 31 32
#if HAVE_SELINUX_LABEL_H
# include <selinux/label.h>
#endif
33

34
#include "security_driver.h"
35 36 37 38
#include "security_selinux.h"
#include "virterror_internal.h"
#include "util.h"
#include "memory.h"
39
#include "logging.h"
40 41
#include "pci.h"
#include "hostusb.h"
42
#include "storage_file.h"
E
Eric Blake 已提交
43
#include "virfile.h"
44
#include "virhash.h"
45
#include "virrandom.h"
46 47
#include "util.h"
#include "conf.h"
D
Daniel P. Berrange 已提交
48 49 50

#define VIR_FROM_THIS VIR_FROM_SECURITY

51 52 53 54 55 56 57 58 59 60 61 62
#define MAX_CONTEXT 1024

typedef struct _virSecuritySELinuxData virSecuritySELinuxData;
typedef virSecuritySELinuxData *virSecuritySELinuxDataPtr;

typedef struct _virSecuritySELinuxCallbackData virSecuritySELinuxCallbackData;
typedef virSecuritySELinuxCallbackData *virSecuritySELinuxCallbackDataPtr;

struct _virSecuritySELinuxData {
    char *domain_context;
    char *file_context;
    char *content_context;
63
    virHashTablePtr mcs;
64 65 66 67 68 69 70
};

struct _virSecuritySELinuxCallbackData {
    virSecurityManagerPtr manager;
    virSecurityLabelDefPtr secdef;
};

71 72 73
#define SECURITY_SELINUX_VOID_DOI       "0"
#define SECURITY_SELINUX_NAME "selinux"

74 75 76
/*
 * Returns 0 on success, 1 if already reserved, or -1 on fatal error
 */
77
static int
78 79
virSecuritySELinuxMCSAdd(virSecurityManagerPtr mgr,
                         const char *mcs)
80
{
81
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
82

83 84 85 86
    if (virHashLookup(data->mcs, mcs))
        return 1;

    if (virHashAddEntry(data->mcs, mcs, (void*)0x1) < 0)
87
        return -1;
88

89 90 91
    return 0;
}

92 93 94
static void
virSecuritySELinuxMCSRemove(virSecurityManagerPtr mgr,
                            const char *mcs)
95
{
96 97 98
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);

    virHashRemoveEntry(data->mcs, mcs);
99 100
}

101 102 103 104 105 106 107 108

static char *
virSecuritySELinuxMCSFind(virSecurityManagerPtr mgr)
{
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
    int c1 = 0;
    int c2 = 0;
    char *mcs = NULL;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    security_context_t ourSecContext = NULL;
    context_t ourContext = NULL;
    char *sens, *cat, *tmp;
    int catMin, catMax, catRange;

    if (getcon(&ourSecContext) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to get current process SELinux context"));
        goto cleanup;
    }
    if (!(ourContext = context_new(ourSecContext))) {
        virReportSystemError(errno,
                             _("Unable to parse current SELinux context '%s'"),
                             ourSecContext);
        goto cleanup;
    }

    if (!(sens = strdup(context_range_get(ourContext)))) {
        virReportOOMError();
        goto cleanup;
    }

    /* Find and blank out the category part */
    if (!(tmp = strchr(sens, ':'))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse sensitivity level in %s"),
                       sens);
        goto cleanup;
    }
    *tmp = '\0';
    cat = tmp + 1;
    /* Find and blank out the sensitivity upper bound */
    if ((tmp = strchr(sens, '-')))
        *tmp = '\0';
    /* sens now just contains the sensitivity lower bound */

    /* Find & extract category min */
    tmp = cat;
    if (tmp[0] != 'c') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse category in %s"),
                       cat);
        goto cleanup;
    }
    tmp++;
    if (virStrToLong_i(tmp, &tmp, 10, &catMin) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse category in %s"),
                       cat);
        goto cleanup;
    }

    /* We *must* have a pair of categories otherwise
     * there's no range to allocate VM categories from */
    if (!tmp[0]) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("No category range available"));
        goto cleanup;
    }

    /* Find & extract category max (if any) */
    if (tmp[0] != '.') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse category in %s"),
                       cat);
        goto cleanup;
    }
    tmp++;
    if (tmp[0] != 'c') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse category in %s"),
                       cat);
        goto cleanup;
    }
    tmp++;
    if (virStrToLong_i(tmp, &tmp, 10, &catMax) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Cannot parse category in %s"),
                       cat);
        goto cleanup;
    }

    /* +1 since virRandomInt range is exclusive of the upper bound */
    catRange = (catMax - catMin) + 1;

    if (catRange < 8) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Category range c%d-c%d too small"),
                       catMin, catMax);
        goto cleanup;
    }

    VIR_DEBUG("Using sensitivity level '%s' cat min %d max %d range %d",
              sens, catMin, catMax, catRange);
203 204

    for (;;) {
205 206 207
        c1 = virRandomInt(catRange);
        c2 = virRandomInt(catRange);
        VIR_DEBUG("Try cat %s:c%d,c%d", sens, c1+catMin, c2+catMin);
208 209

        if (c1 == c2) {
210
            if (virAsprintf(&mcs, "%s:c%d", sens, catMin + c1) < 0) {
211 212 213 214 215 216 217 218 219
                virReportOOMError();
                return NULL;
            }
        } else {
            if (c1 > c2) {
                int t = c1;
                c1 = c2;
                c2 = t;
            }
220
            if (virAsprintf(&mcs, "%s:c%d,c%d", sens, catMin + c1, catMin + c2) < 0) {
221 222 223 224 225 226 227 228 229 230 231 232 233
                virReportOOMError();
                return NULL;
            }
        }

        if (virHashLookup(data->mcs, mcs) == NULL)
            goto cleanup;

        VIR_FREE(mcs);
    }

cleanup:
    VIR_DEBUG("Found context '%s'", NULLSTR(mcs));
234 235 236
    VIR_FREE(sens);
    freecon(ourSecContext);
    context_free(ourContext);
237 238 239 240
    return mcs;
}


241
static char *
242 243 244
virSecuritySELinuxGenNewContext(const char *basecontext,
                                const char *mcs,
                                bool isObjectContext)
245
{
246
    context_t context = NULL;
247 248
    char *ret = NULL;
    char *str;
249 250 251
    security_context_t ourSecContext = NULL;
    context_t ourContext = NULL;

252 253 254
    VIR_DEBUG("basecontext=%s mcs=%s isObjectContext=%d",
              basecontext, mcs, isObjectContext);

255 256 257 258 259 260 261 262 263 264 265
    if (getcon(&ourSecContext) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to get current process SELinux context"));
        goto cleanup;
    }
    if (!(ourContext = context_new(ourSecContext))) {
        virReportSystemError(errno,
                             _("Unable to parse current SELinux context '%s'"),
                             ourSecContext);
        goto cleanup;
    }
266
    VIR_DEBUG("process=%s", ourSecContext);
267 268 269 270 271 272 273 274

    if (!(context = context_new(basecontext))) {
        virReportSystemError(errno,
                             _("Unable to parse base SELinux context '%s'"),
                             basecontext);
        goto cleanup;
    }

275 276 277 278 279 280 281 282
    if (context_user_set(context,
                         context_user_get(ourContext)) != 0) {
        virReportSystemError(errno,
                             _("Unable to set SELinux context user '%s'"),
                             context_user_get(ourContext));
        goto cleanup;
    }

283 284
    if (!isObjectContext &&
        context_role_set(context,
285 286
                         context_role_get(ourContext)) != 0) {
        virReportSystemError(errno,
287
                             _("Unable to set SELinux context role '%s'"),
288 289 290 291
                             context_role_get(ourContext));
        goto cleanup;
    }

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    if (context_range_set(context, mcs) != 0) {
        virReportSystemError(errno,
                             _("Unable to set SELinux context MCS '%s'"),
                             mcs);
        goto cleanup;
    }
    if (!(str = context_str(context))) {
        virReportSystemError(errno, "%s",
                             _("Unable to format SELinux context"));
        goto cleanup;
    }
    if (!(ret = strdup(str))) {
        virReportOOMError();
        goto cleanup;
    }
307
    VIR_DEBUG("Generated context '%s'",  ret);
308
cleanup:
309 310
    freecon(ourSecContext);
    context_free(ourContext);
311 312
    context_free(context);
    return ret;
313 314
}

315

316
#ifdef HAVE_SELINUX_LXC_CONTEXTS_PATH
317
static int
318
virSecuritySELinuxLXCInitialize(virSecurityManagerPtr mgr)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
{
    virConfValuePtr scon = NULL;
    virConfValuePtr tcon = NULL;
    virConfValuePtr dcon = NULL;
    virConfPtr selinux_conf;
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);

    selinux_conf = virConfReadFile(selinux_lxc_contexts_path(), 0);
    if (!selinux_conf) {
        virReportSystemError(errno,
                             _("cannot open SELinux lxc contexts file '%s'"),
                             selinux_lxc_contexts_path());
        return -1;
    }

    scon = virConfGetValue(selinux_conf, "process");
    if (! scon || scon->type != VIR_CONF_STRING || (! scon->str)) {
        virReportSystemError(errno,
                             _("cannot read 'process' value from selinux lxc contexts file '%s'"),
                             selinux_lxc_contexts_path());
        goto error;
    }

    tcon = virConfGetValue(selinux_conf, "file");
    if (! tcon || tcon->type != VIR_CONF_STRING || (! tcon->str)) {
        virReportSystemError(errno,
                             _("cannot read 'file' value from selinux lxc contexts file '%s'"),
                             selinux_lxc_contexts_path());
        goto error;
    }

    dcon = virConfGetValue(selinux_conf, "content");
    if (! dcon || dcon->type != VIR_CONF_STRING || (! dcon->str)) {
        virReportSystemError(errno,
                             _("cannot read 'file' value from selinux lxc contexts file '%s'"),
                             selinux_lxc_contexts_path());
        goto error;
    }

    data->domain_context = strdup(scon->str);
    data->file_context = strdup(tcon->str);
    data->content_context = strdup(dcon->str);
    if (!data->domain_context ||
        !data->file_context ||
        !data->content_context) {
        virReportSystemError(errno,
                             _("cannot allocate memory for LXC SELinux contexts '%s'"),
                             selinux_lxc_contexts_path());
        goto error;
    }
369 370 371 372

    if (!(data->mcs = virHashCreate(10, NULL)))
        goto error;

373 374 375 376 377 378 379 380
    virConfFree(selinux_conf);
    return 0;

error:
    virConfFree(selinux_conf);
    VIR_FREE(data->domain_context);
    VIR_FREE(data->file_context);
    VIR_FREE(data->content_context);
381
    virHashFree(data->mcs);
382 383
    return -1;
}
384 385
#else
static int
386
virSecuritySELinuxLXCInitialize(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
387 388 389 390 391 392
{
    virReportSystemError(ENOSYS, "%s",
                         _("libselinux does not support LXC contexts path"));
    return -1;
}
#endif
393 394 395


static int
396
virSecuritySELinuxQEMUInitialize(virSecurityManagerPtr mgr)
397
{
398 399
    char *ptr;
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
400

401
    if (virFileReadAll(selinux_virtual_domain_context_path(), MAX_CONTEXT, &(data->domain_context)) < 0) {
402
        virReportSystemError(errno,
403
                             _("cannot read SELinux virtual domain context file '%s'"),
404
                             selinux_virtual_domain_context_path());
405
        goto error;
406 407
    }

408 409 410
    ptr = strchrnul(data->domain_context, '\n');
    if (ptr)
        *ptr = '\0';
411

412
    if (virFileReadAll(selinux_virtual_image_context_path(), 2*MAX_CONTEXT, &(data->file_context)) < 0) {
413
        virReportSystemError(errno,
414 415
                             _("cannot read SELinux virtual image context file %s"),
                             selinux_virtual_image_context_path());
416
        goto error;
417 418
    }

419 420
    ptr = strchrnul(data->file_context, '\n');
    if (ptr && *ptr == '\n') {
421
        *ptr = '\0';
422 423 424 425 426 427 428
        data->content_context = strdup(ptr+1);
        if (!data->content_context) {
            virReportOOMError();
            goto error;
        }
        ptr = strchrnul(data->content_context, '\n');
        if (ptr && *ptr == '\n')
429 430
            *ptr = '\0';
    }
431

432 433 434
    if (!(data->mcs = virHashCreate(10, NULL)))
        goto error;

435
    return 0;
436 437 438 439 440

error:
    VIR_FREE(data->domain_context);
    VIR_FREE(data->file_context);
    VIR_FREE(data->content_context);
441
    virHashFree(data->mcs);
442
    return -1;
443 444
}

445 446

static int
447
virSecuritySELinuxInitialize(virSecurityManagerPtr mgr)
448 449 450
{
    VIR_DEBUG("SELinuxInitialize %s", virSecurityManagerGetDriver(mgr));
    if (STREQ(virSecurityManagerGetDriver(mgr),  "LXC")) {
451
        return virSecuritySELinuxLXCInitialize(mgr);
452
    } else {
453
        return virSecuritySELinuxQEMUInitialize(mgr);
454 455 456 457
    }
}


458
static int
459 460
virSecuritySELinuxGenSecurityLabel(virSecurityManagerPtr mgr,
                                   virDomainDefPtr def)
461 462
{
    int rc = -1;
463
    char *mcs = NULL;
464
    char *scontext = NULL;
465
    context_t ctx = NULL;
466
    const char *range;
467 468
    virSecurityLabelDefPtr seclabel;
    virSecuritySELinuxDataPtr data;
469

470
    if (mgr == NULL) {
471
        virReportError(VIR_ERR_INTERNAL_ERROR,
472
                       "%s", _("invalid security driver"));
473 474 475
        return rc;
    }

476 477 478 479 480 481 482 483 484 485
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (seclabel == NULL) {
        return rc;
    }

    data = virSecurityManagerGetPrivateData(mgr);

    VIR_DEBUG("label=%s", virSecurityManagerGetDriver(mgr));
    if (seclabel->type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
        seclabel->label) {
486 487
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("security label already defined for VM"));
488
        return rc;
D
Daniel P. Berrange 已提交
489
    }
490

491
    if (seclabel->imagelabel) {
492 493
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("security image label already defined for VM"));
494 495 496
        return rc;
    }

497 498
    if (seclabel->model &&
        STRNEQ(seclabel->model, SECURITY_SELINUX_NAME)) {
499 500
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label model %s is not supported with selinux"),
501
                       seclabel->model);
502 503 504
        return rc;
    }

505
    VIR_DEBUG("type=%d", seclabel->type);
506

507
    switch (seclabel->type) {
508
    case VIR_DOMAIN_SECLABEL_STATIC:
509
        if (!(ctx = context_new(seclabel->label)) ) {
510 511
            virReportSystemError(errno,
                                 _("unable to allocate socket security context '%s'"),
512
                                 seclabel->label);
513
            return rc;
514 515
        }

516
        range = context_range_get(ctx);
517 518 519 520 521
        if (!range ||
            !(mcs = strdup(range))) {
            virReportOOMError();
            goto cleanup;
        }
522 523 524
        break;

    case VIR_DOMAIN_SECLABEL_DYNAMIC:
525 526 527 528 529
        if (!(mcs = virSecuritySELinuxMCSFind(mgr)))
            goto cleanup;

        if (virSecuritySELinuxMCSAdd(mgr, mcs) < 0)
            goto cleanup;
530

531 532 533 534 535
        seclabel->label =
            virSecuritySELinuxGenNewContext(seclabel->baselabel ?
                                 seclabel->baselabel :
                                 data->domain_context, mcs, false);
        if (!seclabel->label)  {
536 537
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot generate selinux context for %s"), mcs);
538
            goto cleanup;
539
        }
540 541 542 543 544 545 546
        break;

    case VIR_DOMAIN_SECLABEL_NONE:
        /* no op */
        break;

    default:
547 548
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected security label type '%s'"),
549
                       virDomainSeclabelTypeToString(seclabel->type));
550
        goto cleanup;
D
Daniel P. Berrange 已提交
551
    }
552

553
    if (!seclabel->norelabel) {
554
        seclabel->imagelabel = virSecuritySELinuxGenNewContext(data->file_context,
555 556 557
                                                               mcs,
                                                               true);
        if (!seclabel->imagelabel)  {
558 559
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot generate selinux context for %s"), mcs);
560
            goto cleanup;
561
        }
562 563
    }

564 565
    if (!seclabel->model &&
        !(seclabel->model = strdup(SECURITY_SELINUX_NAME))) {
566
        virReportOOMError();
567
        goto cleanup;
D
Daniel P. Berrange 已提交
568 569
    }

570
    rc = 0;
571 572 573

cleanup:
    if (rc != 0) {
574 575 576 577 578 579
        if (seclabel->type == VIR_DOMAIN_SECLABEL_DYNAMIC)
            VIR_FREE(seclabel->label);
        VIR_FREE(seclabel->imagelabel);
        if (seclabel->type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
            !seclabel->baselabel)
            VIR_FREE(seclabel->model);
580 581 582 583
    }

    if (ctx)
        context_free(ctx);
D
Daniel P. Berrange 已提交
584
    VIR_FREE(scontext);
585 586 587
    VIR_FREE(mcs);

    VIR_DEBUG("model=%s label=%s imagelabel=%s baselabel=%s",
588 589 590 591
              NULLSTR(seclabel->model),
              NULLSTR(seclabel->label),
              NULLSTR(seclabel->imagelabel),
              NULLSTR(seclabel->baselabel));
592

593 594 595
    return rc;
}

596
static int
597
virSecuritySELinuxReserveSecurityLabel(virSecurityManagerPtr mgr,
598 599
                                       virDomainDefPtr def,
                                       pid_t pid)
600 601 602 603
{
    security_context_t pctx;
    context_t ctx = NULL;
    const char *mcs;
604
    int rv;
605
    virSecurityLabelDefPtr seclabel;
606

607 608 609 610 611 612
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (seclabel == NULL) {
        return -1;
    }

    if (seclabel->type == VIR_DOMAIN_SECLABEL_STATIC)
613 614
        return 0;

615
    if (getpidcon(pid, &pctx) == -1) {
616
        virReportSystemError(errno,
617
                             _("unable to get PID %d security context"), pid);
618 619 620 621
        return -1;
    }

    ctx = context_new(pctx);
622
    freecon(pctx);
623
    if (!ctx)
624
        goto error;
625 626 627

    mcs = context_range_get(ctx);
    if (!mcs)
628 629
        goto error;

630
    if ((rv = virSecuritySELinuxMCSAdd(mgr, mcs)) < 0)
631
        goto error;
632

633 634 635 636 637 638
    if (rv == 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("MCS level for existing domain label %s already reserved"),
                       (char*)pctx);
        goto error;
    }
639 640 641 642 643

    context_free(ctx);

    return 0;

644
error:
645 646 647 648 649
    context_free(ctx);
    return -1;
}


650
static int
651
virSecuritySELinuxSecurityDriverProbe(const char *virtDriver)
652
{
653 654 655
    if (!is_selinux_enabled())
        return SECURITY_DRIVER_DISABLE;

656 657 658 659 660 661
    if (virtDriver && STREQ(virtDriver, "LXC")) {
#if HAVE_SELINUX_LXC_CONTEXTS_PATH
        if (!virFileExists(selinux_lxc_contexts_path()))
#endif
            return SECURITY_DRIVER_DISABLE;
    }
662 663

    return SECURITY_DRIVER_ENABLE;
664 665
}

666

667
static int
668
virSecuritySELinuxSecurityDriverOpen(virSecurityManagerPtr mgr)
669
{
670
    return virSecuritySELinuxInitialize(mgr);
671 672
}

673

674
static int
675
virSecuritySELinuxSecurityDriverClose(virSecurityManagerPtr mgr)
676
{
677 678 679 680 681
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);

    if (!data)
        return 0;

682 683
    virHashFree(data->mcs);

684 685 686 687
    VIR_FREE(data->domain_context);
    VIR_FREE(data->file_context);
    VIR_FREE(data->content_context);

688 689 690 691
    return 0;
}


692 693
static const char *
virSecuritySELinuxSecurityGetModel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
694 695 696 697
{
    return SECURITY_SELINUX_NAME;
}

698 699
static const char *
virSecuritySELinuxSecurityGetDOI(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
700 701 702 703 704
{
    /*
     * Where will the DOI come from?  SELinux configuration, or qemu
     * configuration? For the moment, we'll just set it to "0".
     */
705
    return SECURITY_SELINUX_VOID_DOI;
706 707 708
}

static int
709 710 711 712
virSecuritySELinuxGetSecurityProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                          virDomainDefPtr def ATTRIBUTE_UNUSED,
                                          pid_t pid,
                                          virSecurityLabelPtr sec)
713 714 715
{
    security_context_t ctx;

716
    if (getpidcon(pid, &ctx) == -1) {
717
        virReportSystemError(errno,
718
                             _("unable to get PID %d security context"),
719
                             pid);
720 721 722 723
        return -1;
    }

    if (strlen((char *) ctx) >= VIR_SECURITY_LABEL_BUFLEN) {
724 725 726 727
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label exceeds "
                         "maximum length: %d"),
                       VIR_SECURITY_LABEL_BUFLEN - 1);
728
        freecon(ctx);
729 730 731 732
        return -1;
    }

    strcpy(sec->label, (char *) ctx);
733
    freecon(ctx);
734

735
    VIR_DEBUG("label=%s", sec->label);
736 737
    sec->enforcing = security_getenforce();
    if (sec->enforcing == -1) {
738
        virReportSystemError(errno, "%s",
739
                             _("error calling security_getenforce()"));
740 741 742 743 744 745
        return -1;
    }

    return 0;
}

746 747 748
/* Attempt to change the label of PATH to TCON.  If OPTIONAL is true,
 * return 1 if labelling was not possible.  Otherwise, require a label
 * change, and return 0 for success, -1 for failure.  */
749
static int
750
virSecuritySELinuxSetFileconHelper(const char *path, char *tcon, bool optional)
751
{
752
    security_context_t econ;
753

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

756
    if (setfilecon(path, tcon) < 0) {
757 758
        int setfilecon_errno = errno;

759 760 761 762
        if (getfilecon(path, &econ) >= 0) {
            if (STREQ(tcon, econ)) {
                freecon(econ);
                /* It's alright, there's nothing to change anyway. */
763
                return optional ? 1 : 0;
764 765 766
            }
            freecon(econ);
        }
767 768

        /* if the error complaint is related to an image hosted on
769 770
         * an nfs mount, or a usbfs/sysfs filesystem not supporting
         * labelling, then just ignore it & hope for the best.
771
         * The user hopefully set one of the necessary SELinux
772
         * virt_use_{nfs,usb,pci}  boolean tunables to allow it...
773
         */
774
        if (setfilecon_errno != EOPNOTSUPP && setfilecon_errno != ENOTSUP) {
775
            virReportSystemError(setfilecon_errno,
776
                                 _("unable to set security context '%s' on '%s'"),
777
                                 tcon, path);
778 779
            if (security_getenforce() == 1)
                return -1;
780
        } else {
781 782 783 784 785 786 787 788 789 790 791 792 793 794
            const char *msg;
            if ((virStorageFileIsSharedFSType(path,
                                              VIR_STORAGE_FILE_SHFS_NFS) == 1) &&
                security_get_boolean_active("virt_use_nfs") != 1) {
                msg = _("Setting security context '%s' on '%s' not supported. "
                        "Consider setting virt_use_nfs");
               if (security_getenforce() == 1)
                   VIR_WARN(msg, tcon, path);
               else
                   VIR_INFO(msg, tcon, path);
            } else {
                VIR_INFO("Setting security context '%s' on '%s' not supported",
                         tcon, path);
            }
795 796
            if (optional)
                return 1;
797
        }
798 799 800 801
    }
    return 0;
}

802
static int
803
virSecuritySELinuxSetFileconOptional(const char *path, char *tcon)
804
{
805
    return virSecuritySELinuxSetFileconHelper(path, tcon, true);
806 807 808
}

static int
809
virSecuritySELinuxSetFilecon(const char *path, char *tcon)
810
{
811
    return virSecuritySELinuxSetFileconHelper(path, tcon, false);
812 813
}

814
static int
815
virSecuritySELinuxFSetFilecon(int fd, char *tcon)
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
{
    security_context_t econ;

    VIR_INFO("Setting SELinux context on fd %d to '%s'", fd, tcon);

    if (fsetfilecon(fd, tcon) < 0) {
        int fsetfilecon_errno = errno;

        if (fgetfilecon(fd, &econ) >= 0) {
            if (STREQ(tcon, econ)) {
                freecon(econ);
                /* It's alright, there's nothing to change anyway. */
                return 0;
            }
            freecon(econ);
        }

        /* if the error complaint is related to an image hosted on
         * an nfs mount, or a usbfs/sysfs filesystem not supporting
         * labelling, then just ignore it & hope for the best.
         * The user hopefully set one of the necessary SELinux
         * virt_use_{nfs,usb,pci}  boolean tunables to allow it...
         */
        if (fsetfilecon_errno != EOPNOTSUPP) {
            virReportSystemError(fsetfilecon_errno,
                                 _("unable to set security context '%s' on fd %d"),
                                 tcon, fd);
            if (security_getenforce() == 1)
                return -1;
        } else {
            VIR_INFO("Setting security context '%s' on fd %d not supported",
                     tcon, fd);
        }
    }
    return 0;
}

E
Eric Blake 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/* Set fcon to the appropriate label for path and mode, or return -1.  */
static int
getContext(const char *newpath, mode_t mode, security_context_t *fcon)
{
#if HAVE_SELINUX_LABEL_H
    struct selabel_handle *handle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
    int ret;

    if (handle == NULL)
        return -1;

    ret = selabel_lookup(handle, fcon, newpath, mode);
    selabel_close(handle);
    return ret;
#else
    return matchpathcon(newpath, mode, fcon);
#endif
}

872 873 874

/* This method shouldn't raise errors, since they'll overwrite
 * errors that the caller(s) are already dealing with */
875
static int
876
virSecuritySELinuxRestoreSecurityFileLabel(const char *path)
877
{
878 879 880 881
    struct stat buf;
    security_context_t fcon = NULL;
    int rc = -1;
    char *newpath = NULL;
882
    char ebuf[1024];
883

884 885
    VIR_INFO("Restoring SELinux context on '%s'", path);

886
    if (virFileResolveLink(path, &newpath) < 0) {
887 888
        VIR_WARN("cannot resolve symlink %s: %s", path,
                 virStrerror(errno, ebuf, sizeof(ebuf)));
D
Daniel P. Berrange 已提交
889
        goto err;
890
    }
891

892
    if (stat(newpath, &buf) != 0) {
893 894
        VIR_WARN("cannot stat %s: %s", newpath,
                 virStrerror(errno, ebuf, sizeof(ebuf)));
D
Daniel P. Berrange 已提交
895
        goto err;
896
    }
D
Daniel P. Berrange 已提交
897

E
Eric Blake 已提交
898
    if (getContext(newpath, buf.st_mode, &fcon) < 0) {
899
        VIR_WARN("cannot lookup default selinux label for %s", newpath);
900
    } else {
901
        rc = virSecuritySELinuxSetFilecon(newpath, fcon);
902
    }
903

904
err:
905
    freecon(fcon);
906 907
    VIR_FREE(newpath);
    return rc;
908 909
}

910
static int
911 912 913 914
virSecuritySELinuxRestoreSecurityImageLabelInt(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                               virDomainDefPtr def,
                                               virDomainDiskDefPtr disk,
                                               int migrated)
915
{
916 917 918 919 920 921
    virSecurityLabelDefPtr seclabel;
    virSecurityDeviceLabelDefPtr disk_seclabel;

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (seclabel == NULL)
        return -1;
922

923 924 925
    disk_seclabel = virDomainDiskDefGetSecurityLabelDef(disk,
                                                        SECURITY_SELINUX_NAME);
    if (seclabel->norelabel || (disk_seclabel && disk_seclabel->norelabel))
926 927
        return 0;

928 929 930 931 932 933 934 935 936 937 938
    /* 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;

939
    if (!disk->src || disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
940 941
        return 0;

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    /* 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;
        }
    }

958
    return virSecuritySELinuxRestoreSecurityFileLabel(disk->src);
959 960
}

961 962

static int
963 964 965
virSecuritySELinuxRestoreSecurityImageLabel(virSecurityManagerPtr mgr,
                                            virDomainDefPtr def,
                                            virDomainDiskDefPtr disk)
966
{
967
    return virSecuritySELinuxRestoreSecurityImageLabelInt(mgr, def, disk, 0);
968 969 970
}


971
static int
972 973 974 975
virSecuritySELinuxSetSecurityFileLabel(virDomainDiskDefPtr disk,
                                       const char *path,
                                       size_t depth,
                                       void *opaque)
976
{
977 978
    int ret;
    virSecurityDeviceLabelDefPtr disk_seclabel;
979 980 981
    virSecuritySELinuxCallbackDataPtr cbdata = opaque;
    const virSecurityLabelDefPtr secdef = cbdata->secdef;
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(cbdata->manager);
982

983 984 985 986
    disk_seclabel = virDomainDiskDefGetSecurityLabelDef(disk,
                                                        SECURITY_SELINUX_NAME);

    if (disk_seclabel && disk_seclabel->norelabel)
987 988
        return 0;

989 990 991
    if (disk_seclabel && !disk_seclabel->norelabel &&
        disk_seclabel->label) {
        ret = virSecuritySELinuxSetFilecon(path, disk_seclabel->label);
992
    } else if (depth == 0) {
993

994
        if (disk->shared) {
995
            ret = virSecuritySELinuxSetFileconOptional(path, data->file_context);
996
        } else if (disk->readonly) {
997
            ret = virSecuritySELinuxSetFileconOptional(path, data->content_context);
998
        } else if (secdef->imagelabel) {
999
            ret = virSecuritySELinuxSetFileconOptional(path, secdef->imagelabel);
1000
        } else {
1001
            ret = 0;
1002 1003
        }
    } else {
1004
        ret = virSecuritySELinuxSetFileconOptional(path, data->content_context);
1005
    }
1006
    if (ret == 1 && !disk_seclabel) {
1007 1008
        /* If we failed to set a label, but virt_use_nfs let us
         * proceed anyway, then we don't need to relabel later.  */
1009
        if (VIR_ALLOC(disk_seclabel) < 0) {
1010 1011 1012
            virReportOOMError();
            return -1;
        }
1013
        disk_seclabel->norelabel = true;
1014
        ret = 0;
1015
    }
1016
    return ret;
1017 1018
}

1019
static int
1020 1021 1022
virSecuritySELinuxSetSecurityImageLabel(virSecurityManagerPtr mgr,
                                        virDomainDefPtr def,
                                        virDomainDiskDefPtr disk)
1023 1024

{
1025
    bool allowDiskFormatProbing;
1026 1027
    virSecuritySELinuxCallbackData cbdata;
    cbdata.manager = mgr;
1028
    cbdata.secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
1029

1030 1031 1032 1033
    allowDiskFormatProbing = virSecurityManagerGetAllowDiskFormatProbing(mgr);

    if (cbdata.secdef == NULL)
        return -1;
1034

1035
    if (cbdata.secdef->norelabel)
1036 1037
        return 0;

1038 1039 1040
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

1041 1042 1043 1044 1045 1046
    /* XXX On one hand, it would be nice to have the driver's uid:gid
     * here so we could retry opens with it. On the other hand, it
     * probably doesn't matter because in practice that's only useful
     * for files on root-squashed NFS shares, and NFS doesn't properly
     * support selinux anyway.
     */
1047
    return virDomainDiskDefForeachPath(disk,
1048
                                       allowDiskFormatProbing,
1049
                                       true,
1050
                                       -1, -1, /* current process uid:gid */
1051
                                       virSecuritySELinuxSetSecurityFileLabel,
1052
                                       &cbdata);
1053 1054
}

1055 1056

static int
1057 1058
virSecuritySELinuxSetSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
                                      const char *file, void *opaque)
1059
{
1060
    virSecurityLabelDefPtr secdef;
1061
    virDomainDefPtr def = opaque;
1062

1063 1064 1065
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1066
    return virSecuritySELinuxSetFilecon(file, secdef->imagelabel);
1067 1068 1069
}

static int
1070 1071
virSecuritySELinuxSetSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
                                      const char *file, void *opaque)
1072
{
1073
    virSecurityLabelDefPtr secdef;
1074
    virDomainDefPtr def = opaque;
1075 1076 1077 1078

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1079

1080
    return virSecuritySELinuxSetFilecon(file, secdef->imagelabel);
1081 1082 1083
}

static int
1084 1085 1086
virSecuritySELinuxSetSecurityHostdevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                          virDomainDefPtr def,
                                          virDomainHostdevDefPtr dev)
1087 1088

{
1089
    virSecurityLabelDefPtr secdef;
1090 1091
    int ret = -1;

1092 1093 1094 1095
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

1096
    if (secdef->norelabel)
1097 1098
        return 0;

1099 1100 1101 1102 1103
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
1104
        usbDevice *usb;
1105

1106 1107 1108 1109 1110
        if (dev->missing)
            return 0;

        usb = usbGetDevice(dev->source.subsys.u.usb.bus,
                           dev->source.subsys.u.usb.device);
1111 1112
        if (!usb)
            goto done;
1113

1114
        ret = usbDeviceFileIterate(usb, virSecuritySELinuxSetSecurityUSBLabel, def);
1115
        usbFreeDevice(usb);
M
Mark McLoughlin 已提交
1116
        break;
1117 1118 1119
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
1120
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
1121 1122 1123 1124 1125 1126 1127
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

1128
        ret = pciDeviceFileIterate(pci, virSecuritySELinuxSetSecurityPCILabel, def);
1129
        pciFreeDevice(pci);
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

1143

1144
static int
1145 1146 1147
virSecuritySELinuxRestoreSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
                                          const char *file,
                                          void *opaque ATTRIBUTE_UNUSED)
1148
{
1149
    return virSecuritySELinuxRestoreSecurityFileLabel(file);
1150 1151 1152
}

static int
1153 1154 1155
virSecuritySELinuxRestoreSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
                                          const char *file,
                                          void *opaque ATTRIBUTE_UNUSED)
1156
{
1157
    return virSecuritySELinuxRestoreSecurityFileLabel(file);
1158 1159 1160
}

static int
1161 1162 1163
virSecuritySELinuxRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                              virDomainDefPtr def,
                                              virDomainHostdevDefPtr dev)
1164 1165

{
1166
    virSecurityLabelDefPtr secdef;
1167 1168
    int ret = -1;

1169 1170 1171 1172
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

1173
    if (secdef->norelabel)
1174 1175
        return 0;

1176 1177 1178 1179 1180
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
1181 1182 1183 1184
        usbDevice *usb;

        if (dev->missing)
            return 0;
1185

1186 1187
        usb = usbGetDevice(dev->source.subsys.u.usb.bus,
                           dev->source.subsys.u.usb.device);
1188 1189 1190
        if (!usb)
            goto done;

1191
        ret = usbDeviceFileIterate(usb, virSecuritySELinuxRestoreSecurityUSBLabel, NULL);
1192
        usbFreeDevice(usb);
1193 1194 1195 1196 1197

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
1198
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
1199 1200 1201 1202 1203 1204 1205
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

1206
        ret = pciDeviceFileIterate(pci, virSecuritySELinuxRestoreSecurityPCILabel, NULL);
1207
        pciFreeDevice(pci);
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

1221 1222

static int
1223
virSecuritySELinuxSetSecurityChardevLabel(virDomainDefPtr def,
1224 1225
                                          virDomainChrDefPtr dev,
                                          virDomainChrSourceDefPtr dev_source)
1226 1227

{
1228 1229 1230
    virSecurityLabelDefPtr seclabel;
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
    char *imagelabel = NULL;
1231 1232 1233
    char *in = NULL, *out = NULL;
    int ret = -1;

1234 1235
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (seclabel == NULL)
1236 1237
        return -1;

1238 1239 1240 1241 1242
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_SELINUX_NAME);

    if (seclabel->norelabel || (chr_seclabel && chr_seclabel->norelabel))
1243 1244
        return 0;

1245 1246 1247 1248 1249 1250
    if (chr_seclabel)
        imagelabel = chr_seclabel->label;
    if (!imagelabel)
        imagelabel = seclabel->imagelabel;

    switch (dev_source->type) {
1251 1252
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        ret = virSecuritySELinuxSetFilecon(dev_source->data.file.path,
                                           imagelabel);
        break;

    case VIR_DOMAIN_CHR_TYPE_UNIX:
        if (!dev_source->data.nix.listen) {
            if (virSecuritySELinuxSetFilecon(dev_source->data.file.path,
                                             imagelabel) < 0)
                goto done;
        }
        ret = 0;
1264 1265 1266
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
1267 1268
        if ((virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0) ||
            (virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0)) {
1269 1270 1271 1272
            virReportOOMError();
            goto done;
        }
        if (virFileExists(in) && virFileExists(out)) {
1273 1274
            if ((virSecuritySELinuxSetFilecon(in, imagelabel) < 0) ||
                (virSecuritySELinuxSetFilecon(out, imagelabel) < 0)) {
1275
                goto done;
1276
            }
1277 1278
        } else if (virSecuritySELinuxSetFilecon(dev_source->data.file.path,
                                                imagelabel) < 0) {
1279
            goto done;
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
        }
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

done:
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}

static int
1296
virSecuritySELinuxRestoreSecurityChardevLabel(virDomainDefPtr def,
1297 1298
                                              virDomainChrDefPtr dev,
                                              virDomainChrSourceDefPtr dev_source)
1299 1300

{
1301 1302
    virSecurityLabelDefPtr seclabel;
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
1303 1304 1305
    char *in = NULL, *out = NULL;
    int ret = -1;

1306 1307
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (seclabel == NULL)
1308 1309
        return -1;

1310 1311 1312 1313
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_SELINUX_NAME);
    if (seclabel->norelabel || (chr_seclabel && chr_seclabel->norelabel))
1314 1315
        return 0;

1316
    switch (dev_source->type) {
1317 1318
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
1319
        if (virSecuritySELinuxRestoreSecurityFileLabel(dev_source->data.file.path) < 0)
1320 1321
            goto done;
        ret = 0;
1322
        break;
1323 1324 1325 1326 1327 1328 1329 1330 1331

    case VIR_DOMAIN_CHR_TYPE_UNIX:
        if (!dev_source->data.nix.listen) {
            if (virSecuritySELinuxRestoreSecurityFileLabel(dev_source->data.file.path) < 0)
                goto done;
        }
        ret = 0;
        break;

1332
    case VIR_DOMAIN_CHR_TYPE_PIPE:
1333 1334
        if ((virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0) ||
            (virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0)) {
1335 1336 1337
            virReportOOMError();
            goto done;
        }
1338
        if (virFileExists(in) && virFileExists(out)) {
1339 1340
            if ((virSecuritySELinuxRestoreSecurityFileLabel(out) < 0) ||
                (virSecuritySELinuxRestoreSecurityFileLabel(in) < 0)) {
1341 1342
                goto done;
            }
1343
        } else if (virSecuritySELinuxRestoreSecurityFileLabel(dev_source->data.file.path) < 0) {
1344
            goto done;
1345
        }
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

done:
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}


static int
1362 1363 1364
virSecuritySELinuxRestoreSecurityChardevCallback(virDomainDefPtr def,
                                                 virDomainChrDefPtr dev,
                                                 void *opaque ATTRIBUTE_UNUSED)
1365
{
1366 1367 1368 1369 1370
    /* This is taken care of by processing of def->serials */
    if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL)
        return 0;

1371 1372
    return virSecuritySELinuxRestoreSecurityChardevLabel(def, dev,
                                                         &dev->source);
1373 1374 1375
}


E
Eric Blake 已提交
1376
static int
1377 1378 1379
virSecuritySELinuxRestoreSecuritySmartcardCallback(virDomainDefPtr def,
                                                   virDomainSmartcardDefPtr dev,
                                                   void *opaque ATTRIBUTE_UNUSED)
E
Eric Blake 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
{
    const char *database;

    switch (dev->type) {
    case VIR_DOMAIN_SMARTCARD_TYPE_HOST:
        break;

    case VIR_DOMAIN_SMARTCARD_TYPE_HOST_CERTIFICATES:
        database = dev->data.cert.database;
        if (!database)
            database = VIR_DOMAIN_SMARTCARD_DEFAULT_DATABASE;
1391
        return virSecuritySELinuxRestoreSecurityFileLabel(database);
E
Eric Blake 已提交
1392 1393

    case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
1394
        return virSecuritySELinuxRestoreSecurityChardevLabel(def, NULL, &dev->data.passthru);
E
Eric Blake 已提交
1395 1396

    default:
1397 1398 1399
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown smartcard type %d"),
                       dev->type);
E
Eric Blake 已提交
1400 1401 1402 1403 1404 1405 1406
        return -1;
    }

    return 0;
}


1407
static int
1408 1409 1410
virSecuritySELinuxRestoreSecurityAllLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                          virDomainDefPtr def,
                                          int migrated ATTRIBUTE_UNUSED)
1411
{
1412
    virSecurityLabelDefPtr secdef;
1413 1414
    int i;
    int rc = 0;
1415

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

1418 1419 1420 1421
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

1422
    if (secdef->norelabel)
1423 1424
        return 0;

1425
    for (i = 0 ; i < def->nhostdevs ; i++) {
1426 1427 1428
        if (virSecuritySELinuxRestoreSecurityHostdevLabel(mgr,
                                                          def,
                                                          def->hostdevs[i]) < 0)
1429
            rc = -1;
1430
    }
1431
    for (i = 0 ; i < def->ndisks ; i++) {
1432 1433 1434 1435
        if (virSecuritySELinuxRestoreSecurityImageLabelInt(mgr,
                                                           def,
                                                           def->disks[i],
                                                           migrated) < 0)
1436 1437
            rc = -1;
    }
1438

1439
    if (virDomainChrDefForeach(def,
1440
                               false,
1441
                               virSecuritySELinuxRestoreSecurityChardevCallback,
1442
                               NULL) < 0)
1443 1444
        rc = -1;

1445
    if (virDomainSmartcardDefForeach(def,
E
Eric Blake 已提交
1446
                                     false,
1447
                                     virSecuritySELinuxRestoreSecuritySmartcardCallback,
1448
                                     NULL) < 0)
E
Eric Blake 已提交
1449 1450
        rc = -1;

1451
    if (def->os.kernel &&
1452
        virSecuritySELinuxRestoreSecurityFileLabel(def->os.kernel) < 0)
1453 1454
        rc = -1;

1455
    if (def->os.initrd &&
1456
        virSecuritySELinuxRestoreSecurityFileLabel(def->os.initrd) < 0)
1457 1458
        rc = -1;

1459 1460 1461 1462
    return rc;
}

static int
1463
virSecuritySELinuxReleaseSecurityLabel(virSecurityManagerPtr mgr,
1464
                                       virDomainDefPtr def)
1465
{
1466 1467 1468 1469 1470
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1471

1472 1473 1474 1475
    if (secdef->type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
        if (secdef->label != NULL) {
            context_t con = context_new(secdef->label);
            if (con) {
1476
                virSecuritySELinuxMCSRemove(mgr, context_range_get(con));
1477 1478 1479 1480 1481 1482
                context_free(con);
            }
        }
        VIR_FREE(secdef->label);
        if (!secdef->baselabel)
            VIR_FREE(secdef->model);
1483 1484 1485
    }
    VIR_FREE(secdef->imagelabel);

1486
    return 0;
1487 1488
}

1489 1490

static int
1491 1492 1493
virSecuritySELinuxSetSavedStateLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                     virDomainDefPtr def,
                                     const char *savefile)
1494
{
1495 1496 1497 1498 1499
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1500

1501
    if (secdef->norelabel)
1502 1503
        return 0;

1504
    return virSecuritySELinuxSetFilecon(savefile, secdef->imagelabel);
1505 1506 1507 1508
}


static int
1509 1510 1511
virSecuritySELinuxRestoreSavedStateLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                         virDomainDefPtr def,
                                         const char *savefile)
1512
{
1513 1514 1515 1516 1517
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1518

1519
    if (secdef->norelabel)
1520 1521
        return 0;

1522
    return virSecuritySELinuxRestoreSecurityFileLabel(savefile);
1523 1524 1525
}


1526
static int
1527 1528
virSecuritySELinuxSecurityVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                 virDomainDefPtr def)
1529
{
1530 1531 1532 1533 1534 1535
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

1536
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1537 1538 1539 1540 1541
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label driver mismatch: "
                         "'%s' model configured for domain, but "
                         "hypervisor driver is '%s'."),
                       secdef->model, virSecurityManagerGetModel(mgr));
1542 1543 1544
        return -1;
    }

1545 1546
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC) {
        if (security_check_context(secdef->label) != 0) {
1547 1548
            virReportError(VIR_ERR_XML_ERROR,
                           _("Invalid security label %s"), secdef->label);
1549 1550 1551 1552 1553 1554
            return -1;
        }
    }
    return 0;
}

1555
static int
1556 1557
virSecuritySELinuxSetSecurityProcessLabel(virSecurityManagerPtr mgr,
                                          virDomainDefPtr def)
1558 1559
{
    /* TODO: verify DOI */
1560 1561 1562 1563 1564
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1565

1566
    if (secdef->label == NULL)
1567 1568
        return 0;

1569
    VIR_DEBUG("label=%s", secdef->label);
1570
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1571 1572 1573 1574 1575
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label driver mismatch: "
                         "'%s' model configured for domain, but "
                         "hypervisor driver is '%s'."),
                       secdef->model, virSecurityManagerGetModel(mgr));
1576
        if (security_getenforce() == 1)
1577
            return -1;
1578 1579 1580
    }

    if (setexeccon(secdef->label) == -1) {
1581
        virReportSystemError(errno,
1582 1583
                             _("unable to set security context '%s'"),
                             secdef->label);
1584
        if (security_getenforce() == 1)
1585
            return -1;
1586 1587
    }

1588 1589 1590
    return 0;
}

1591
static int
1592 1593
virSecuritySELinuxSetSecurityDaemonSocketLabel(virSecurityManagerPtr mgr,
                                               virDomainDefPtr def)
1594 1595
{
    /* TODO: verify DOI */
1596
    virSecurityLabelDefPtr secdef;
1597 1598 1599 1600 1601
    context_t execcon = NULL;
    context_t proccon = NULL;
    security_context_t scon = NULL;
    int rc = -1;

1602 1603 1604 1605 1606
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

    if (secdef->label == NULL)
1607 1608
        return 0;

1609
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1610 1611 1612 1613 1614
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label driver mismatch: "
                         "'%s' model configured for domain, but "
                         "hypervisor driver is '%s'."),
                       secdef->model, virSecurityManagerGetModel(mgr));
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
        goto done;
    }

    if ( !(execcon = context_new(secdef->label)) ) {
        virReportSystemError(errno,
                             _("unable to allocate socket security context '%s'"),
                             secdef->label);
        goto done;
    }

    if (getcon(&scon) == -1) {
        virReportSystemError(errno,
                             _("unable to get current process context '%s'"),
                             secdef->label);
        goto done;
    }

    if ( !(proccon = context_new(scon)) ) {
        virReportSystemError(errno,
                             _("unable to set socket security context '%s'"),
                             secdef->label);
        goto done;
    }

    if (context_range_set(proccon, context_range_get(execcon)) == -1) {
        virReportSystemError(errno,
                             _("unable to set socket security context range '%s'"),
                             secdef->label);
        goto done;
    }

    VIR_DEBUG("Setting VM %s socket context %s",
1647
              def->name, context_str(proccon));
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
    if (setsockcreatecon(context_str(proccon)) == -1) {
        virReportSystemError(errno,
                             _("unable to set socket security context '%s'"),
                             context_str(proccon));
        goto done;
    }

    rc = 0;
done:

    if (security_getenforce() != 1)
        rc = 0;
    if (execcon) context_free(execcon);
    if (proccon) context_free(proccon);
    freecon(scon);
    return rc;
}

1666
static int
1667 1668
virSecuritySELinuxSetSecuritySocketLabel(virSecurityManagerPtr mgr,
                                         virDomainDefPtr vm)
1669
{
1670
    virSecurityLabelDefPtr secdef;
1671 1672
    int rc = -1;

1673 1674 1675 1676
    secdef = virDomainDefGetSecurityLabelDef(vm, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;

1677 1678 1679 1680
    if (secdef->label == NULL)
        return 0;

    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1681 1682 1683 1684 1685
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label driver mismatch: "
                         "'%s' model configured for domain, but "
                         "hypervisor driver is '%s'."),
                       secdef->model, virSecurityManagerGetModel(mgr));
1686 1687 1688 1689
        goto done;
    }

    VIR_DEBUG("Setting VM %s socket context %s",
1690
              vm->name, secdef->label);
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    if (setsockcreatecon(secdef->label) == -1) {
        virReportSystemError(errno,
                             _("unable to set socket security context '%s'"),
                             secdef->label);
        goto done;
    }

    rc = 0;

done:
    if (security_getenforce() != 1)
        rc = 0;

    return rc;
}

1707
static int
1708 1709
virSecuritySELinuxClearSecuritySocketLabel(virSecurityManagerPtr mgr,
                                           virDomainDefPtr def)
1710 1711
{
    /* TODO: verify DOI */
1712 1713 1714 1715 1716
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1717

1718
    if (secdef->label == NULL)
1719 1720
        return 0;

1721
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1722 1723 1724 1725 1726
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label driver mismatch: "
                         "'%s' model configured for domain, but "
                         "hypervisor driver is '%s'."),
                       secdef->model, virSecurityManagerGetModel(mgr));
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
        if (security_getenforce() == 1)
            return -1;
    }

    if (setsockcreatecon(NULL) == -1) {
        virReportSystemError(errno,
                             _("unable to clear socket security context '%s'"),
                             secdef->label);
        if (security_getenforce() == 1)
            return -1;
    }
    return 0;
}

1741 1742

static int
1743 1744 1745
virSecuritySELinuxSetSecurityChardevCallback(virDomainDefPtr def,
                                             virDomainChrDefPtr dev,
                                             void *opaque ATTRIBUTE_UNUSED)
1746
{
1747 1748 1749 1750 1751
    /* This is taken care of by processing of def->serials */
    if (dev->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE &&
        dev->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL)
        return 0;

1752
    return virSecuritySELinuxSetSecurityChardevLabel(def, dev, &dev->source);
1753 1754 1755
}


E
Eric Blake 已提交
1756
static int
1757 1758 1759
virSecuritySELinuxSetSecuritySmartcardCallback(virDomainDefPtr def,
                                               virDomainSmartcardDefPtr dev,
                                               void *opaque)
E
Eric Blake 已提交
1760 1761
{
    const char *database;
1762 1763
    virSecurityManagerPtr mgr = opaque;
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
E
Eric Blake 已提交
1764 1765 1766 1767 1768 1769 1770 1771 1772

    switch (dev->type) {
    case VIR_DOMAIN_SMARTCARD_TYPE_HOST:
        break;

    case VIR_DOMAIN_SMARTCARD_TYPE_HOST_CERTIFICATES:
        database = dev->data.cert.database;
        if (!database)
            database = VIR_DOMAIN_SMARTCARD_DEFAULT_DATABASE;
1773
        return virSecuritySELinuxSetFilecon(database, data->content_context);
E
Eric Blake 已提交
1774 1775

    case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
1776
        return virSecuritySELinuxSetSecurityChardevLabel(def, NULL, &dev->data.passthru);
E
Eric Blake 已提交
1777 1778

    default:
1779 1780 1781
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unknown smartcard type %d"),
                       dev->type);
E
Eric Blake 已提交
1782 1783 1784 1785 1786 1787 1788
        return -1;
    }

    return 0;
}


1789
static int
1790 1791 1792
virSecuritySELinuxSetSecurityAllLabel(virSecurityManagerPtr mgr,
                                      virDomainDefPtr def,
                                      const char *stdin_path)
1793 1794
{
    int i;
1795 1796 1797 1798 1799 1800
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1801

1802
    if (secdef->norelabel)
1803 1804
        return 0;

1805
    for (i = 0 ; i < def->ndisks ; i++) {
1806
        /* XXX fixme - we need to recursively label the entire tree :-( */
1807
        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_DIR) {
1808
            VIR_WARN("Unable to relabel directory tree %s for disk %s",
1809
                     def->disks[i]->src, def->disks[i]->dst);
1810
            continue;
1811
        }
1812
        if (virSecuritySELinuxSetSecurityImageLabel(mgr,
1813
                                         def, def->disks[i]) < 0)
1814 1815
            return -1;
    }
1816
    /* XXX fixme process  def->fss if relabel == true */
1817

1818
    for (i = 0 ; i < def->nhostdevs ; i++) {
1819
        if (virSecuritySELinuxSetSecurityHostdevLabel(mgr,
1820 1821
                                           def,
                                           def->hostdevs[i]) < 0)
1822
            return -1;
1823 1824
    }

1825
    if (virDomainChrDefForeach(def,
1826
                               true,
1827
                               virSecuritySELinuxSetSecurityChardevCallback,
1828
                               NULL) < 0)
1829 1830
        return -1;

1831
    if (virDomainSmartcardDefForeach(def,
E
Eric Blake 已提交
1832
                                     true,
1833
                                     virSecuritySELinuxSetSecuritySmartcardCallback,
1834
                                     mgr) < 0)
E
Eric Blake 已提交
1835 1836
        return -1;

1837
    if (def->os.kernel &&
1838
        virSecuritySELinuxSetFilecon(def->os.kernel, data->content_context) < 0)
1839 1840
        return -1;

1841
    if (def->os.initrd &&
1842
        virSecuritySELinuxSetFilecon(def->os.initrd, data->content_context) < 0)
1843 1844
        return -1;

1845
    if (stdin_path) {
1846
        if (virSecuritySELinuxSetFilecon(stdin_path, data->content_context) < 0 &&
1847 1848 1849 1850
            virStorageFileIsSharedFSType(stdin_path,
                                         VIR_STORAGE_FILE_SHFS_NFS) != 1)
            return -1;
    }
1851

1852 1853 1854
    return 0;
}

1855
static int
1856 1857 1858
virSecuritySELinuxSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                  virDomainDefPtr def,
                                  int fd)
1859
{
1860 1861 1862 1863 1864
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return -1;
1865 1866 1867 1868

    if (secdef->imagelabel == NULL)
        return 0;

1869
    return virSecuritySELinuxFSetFilecon(fd, secdef->imagelabel);
1870 1871
}

1872 1873 1874 1875
static char *
virSecuritySELinuxGenImageLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr def)
{
1876
    virSecurityLabelDefPtr secdef;
1877 1878 1879 1880 1881 1882
    virSecuritySELinuxDataPtr data = virSecurityManagerGetPrivateData(mgr);
    const char *range;
    context_t ctx = NULL;
    char *label = NULL;
    const char *mcs = NULL;

1883 1884 1885 1886
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        goto cleanup;

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
    if (secdef->label) {
        ctx = context_new(secdef->label);
        if (!ctx) {
            virReportOOMError();
            goto cleanup;
        }
        range = context_range_get(ctx);
        if (range) {
            mcs = strdup(range);
            if (!mcs) {
                virReportOOMError();
                goto cleanup;
            }
1900 1901
            if (!(label = virSecuritySELinuxGenNewContext(data->file_context,
                                                          mcs, true)))
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
                goto cleanup;
        }
    }

cleanup:
        context_free(ctx);
        VIR_FREE(mcs);
        return label;
}

1912 1913 1914 1915
static char *
virSecuritySELinuxGetSecurityMountOptions(virSecurityManagerPtr mgr,
                                          virDomainDefPtr def)
{
1916
    char *opts = NULL;
1917 1918 1919 1920 1921
    virSecurityLabelDefPtr secdef;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
    if (secdef == NULL)
        return NULL;
1922 1923

    if (! secdef->imagelabel)
1924
        secdef->imagelabel = virSecuritySELinuxGenImageLabel(mgr,def);
1925 1926 1927 1928 1929 1930 1931

    if (secdef->imagelabel) {
        virAsprintf(&opts,
                    ",context=\"%s\"",
                    (const char*) secdef->imagelabel);
    }

1932
    VIR_DEBUG("imageLabel=%s", secdef->imagelabel);
1933 1934 1935
    return opts;
}

1936
virSecurityDriver virSecurityDriverSELinux = {
1937 1938
    .privateDataLen                     = sizeof(virSecuritySELinuxData),
    .name                               = SECURITY_SELINUX_NAME,
1939 1940 1941
    .probe                              = virSecuritySELinuxSecurityDriverProbe,
    .open                               = virSecuritySELinuxSecurityDriverOpen,
    .close                              = virSecuritySELinuxSecurityDriverClose,
1942

1943 1944
    .getModel                           = virSecuritySELinuxSecurityGetModel,
    .getDOI                             = virSecuritySELinuxSecurityGetDOI,
1945

1946
    .domainSecurityVerify               = virSecuritySELinuxSecurityVerify,
1947

1948 1949
    .domainSetSecurityImageLabel        = virSecuritySELinuxSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel    = virSecuritySELinuxRestoreSecurityImageLabel,
1950

1951 1952 1953
    .domainSetSecurityDaemonSocketLabel = virSecuritySELinuxSetSecurityDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecuritySELinuxSetSecuritySocketLabel,
    .domainClearSecuritySocketLabel     = virSecuritySELinuxClearSecuritySocketLabel,
1954

1955 1956 1957
    .domainGenSecurityLabel             = virSecuritySELinuxGenSecurityLabel,
    .domainReserveSecurityLabel         = virSecuritySELinuxReserveSecurityLabel,
    .domainReleaseSecurityLabel         = virSecuritySELinuxReleaseSecurityLabel,
1958

1959 1960
    .domainGetSecurityProcessLabel      = virSecuritySELinuxGetSecurityProcessLabel,
    .domainSetSecurityProcessLabel      = virSecuritySELinuxSetSecurityProcessLabel,
1961

1962 1963
    .domainSetSecurityAllLabel          = virSecuritySELinuxSetSecurityAllLabel,
    .domainRestoreSecurityAllLabel      = virSecuritySELinuxRestoreSecurityAllLabel,
1964

1965 1966
    .domainSetSecurityHostdevLabel      = virSecuritySELinuxSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecuritySELinuxRestoreSecurityHostdevLabel,
1967

1968 1969
    .domainSetSavedStateLabel           = virSecuritySELinuxSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecuritySELinuxRestoreSavedStateLabel,
1970

1971
    .domainSetSecurityImageFDLabel      = virSecuritySELinuxSetImageFDLabel,
1972

1973
    .domainGetSecurityMountOptions      = virSecuritySELinuxGetSecurityMountOptions,
1974
};