security_selinux.c 40.5 KB
Newer Older
1
/*
2
 * Copyright (C) 2008-2011 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
 *
 * 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 已提交
21 22 23
#if HAVE_SELINUX_LABEL_H
# include <selinux/label.h>
#endif
24

25
#include "security_driver.h"
26 27 28 29
#include "security_selinux.h"
#include "virterror_internal.h"
#include "util.h"
#include "memory.h"
30
#include "logging.h"
31 32
#include "pci.h"
#include "hostusb.h"
33
#include "storage_file.h"
E
Eric Blake 已提交
34
#include "virfile.h"
D
Daniel P. Berrange 已提交
35 36 37

#define VIR_FROM_THIS VIR_FROM_SECURITY

38
static char default_domain_context[1024];
39
static char default_content_context[1024];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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 已提交
60
        if (STREQ(ptr->mcs, mcs))
61 62
            return -1;
    }
D
Daniel P. Berrange 已提交
63 64
    if (VIR_ALLOC(ptr) < 0)
        return -1;
65 66 67 68 69 70 71 72 73 74 75 76 77
    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 已提交
78
        if (STREQ(ptr->mcs, mcs)) {
79 80 81 82 83
            if (prevptr)
                prevptr->next = ptr->next;
            else {
                mcsList = ptr->next;
            }
84 85
            VIR_FREE(ptr->mcs);
            VIR_FREE(ptr);
86 87 88 89 90 91 92 93 94 95 96 97
            return 0;
        }
        prevptr = ptr;
    }
    return -1;
}

static char *
SELinuxGenNewContext(const char *oldcontext, const char *mcs)
{
    char *newcontext = NULL;
    char *scontext = strdup(oldcontext);
98
    context_t con;
99
    if (!scontext) goto err;
100
    con = context_new(scontext);
101 102 103 104 105 106 107 108 109 110
    if (!con) goto err;
    context_range_set(con, mcs);
    newcontext = strdup(context_str(con));
    context_free(con);
err:
    freecon(scontext);
    return (newcontext);
}

static int
111
SELinuxInitialize(void)
112 113 114 115 116 117
{
    char *ptr = NULL;
    int fd = 0;

    fd = open(selinux_virtual_domain_context_path(), O_RDONLY);
    if (fd < 0) {
118
        virReportSystemError(errno,
119 120
                             _("cannot open SELinux virtual domain context file '%s'"),
                             selinux_virtual_domain_context_path());
121 122 123 124
        return -1;
    }

    if (saferead(fd, default_domain_context, sizeof(default_domain_context)) < 0) {
125
        virReportSystemError(errno,
126 127
                             _("cannot read SELinux virtual domain context file %s"),
                             selinux_virtual_domain_context_path());
128
        VIR_FORCE_CLOSE(fd);
129 130
        return -1;
    }
131
    VIR_FORCE_CLOSE(fd);
132 133 134 135 136

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

    if ((fd = open(selinux_virtual_image_context_path(), O_RDONLY)) < 0) {
137
        virReportSystemError(errno,
138 139
                             _("cannot open SELinux virtual image context file %s"),
                             selinux_virtual_image_context_path());
140 141 142 143
        return -1;
    }

    if (saferead(fd, default_image_context, sizeof(default_image_context)) < 0) {
144
        virReportSystemError(errno,
145 146
                             _("cannot read SELinux virtual image context file %s"),
                             selinux_virtual_image_context_path());
147
        VIR_FORCE_CLOSE(fd);
148 149
        return -1;
    }
150
    VIR_FORCE_CLOSE(fd);
151 152

    ptr = strchrnul(default_image_context, '\n');
153 154 155 156 157 158 159
    if (*ptr == '\n') {
        *ptr = '\0';
        strcpy(default_content_context, ptr+1);
        ptr = strchrnul(default_content_context, '\n');
        if (*ptr == '\n')
            *ptr = '\0';
    }
160 161 162 163
    return 0;
}

static int
164
SELinuxGenSecurityLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
165
                        virDomainDefPtr def)
166 167
{
    int rc = -1;
168
    char *mcs = NULL;
169 170 171
    char *scontext = NULL;
    int c1 = 0;
    int c2 = 0;
172
    context_t ctx = NULL;
173

174 175 176
    if ((def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC) &&
        !def->seclabel.baselabel &&
        def->seclabel.model) {
177 178 179 180 181
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("security model already defined for VM"));
        return rc;
    }

182 183
    if (def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
        def->seclabel.label) {
184
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
185
                               "%s", _("security label already defined for VM"));
186
        return rc;
D
Daniel P. Berrange 已提交
187
    }
188

189
    if (def->seclabel.imagelabel) {
190 191 192 193 194
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("security image label already defined for VM"));
        return rc;
    }

195 196
    if (def->seclabel.model &&
        STRNEQ(def->seclabel.model, SECURITY_SELINUX_NAME)) {
197 198
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("security label model %s is not supported with selinux"),
199
                               def->seclabel.model);
200 201 202
        return rc;
    }

203 204
    if (def->seclabel.type == VIR_DOMAIN_SECLABEL_STATIC) {
        if (!(ctx = context_new(def->seclabel.label)) ) {
205 206
            virReportSystemError(errno,
                                 _("unable to allocate socket security context '%s'"),
207
                                 def->seclabel.label);
208
            return rc;
209 210
        }

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        const char *range = context_range_get(ctx);
        if (!range ||
            !(mcs = strdup(range))) {
            virReportOOMError();
            goto cleanup;
        }
    } else {
        do {
            c1 = virRandom(1024);
            c2 = virRandom(1024);

            if ( c1 == c2 ) {
                if (virAsprintf(&mcs, "s0:c%d", c1) < 0) {
                    virReportOOMError();
                    goto cleanup;
                }
            } else {
                if (c1 > c2) {
                    c1 ^= c2;
                    c2 ^= c1;
                    c1 ^= c2;
                }
                if (virAsprintf(&mcs, "s0:c%d,c%d", c1, c2) < 0) {
                    virReportOOMError();
                    goto cleanup;
                }
            }
        } while (mcsAdd(mcs) == -1);

240 241 242
        def->seclabel.label =
            SELinuxGenNewContext(def->seclabel.baselabel ?
                                 def->seclabel.baselabel :
243
                                 default_domain_context, mcs);
244
        if (! def->seclabel.label)  {
245 246 247 248
            virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("cannot generate selinux context for %s"), mcs);
            goto cleanup;
        }
D
Daniel P. Berrange 已提交
249
    }
250 251
    def->seclabel.imagelabel = SELinuxGenNewContext(default_image_context, mcs);
    if (!def->seclabel.imagelabel)  {
252
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
D
Daniel P. Berrange 已提交
253
                               _("cannot generate selinux context for %s"), mcs);
254
        goto cleanup;
D
Daniel P. Berrange 已提交
255
    }
256

257 258
    if (!def->seclabel.model &&
        !(def->seclabel.model = strdup(SECURITY_SELINUX_NAME))) {
259
        virReportOOMError();
260
        goto cleanup;
D
Daniel P. Berrange 已提交
261 262
    }

263
    rc = 0;
264 265 266

cleanup:
    if (rc != 0) {
267 268 269 270 271 272
        if (def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC)
            VIR_FREE(def->seclabel.label);
        VIR_FREE(def->seclabel.imagelabel);
        if (def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
            !def->seclabel.baselabel)
            VIR_FREE(def->seclabel.model);
273 274 275 276
    }

    if (ctx)
        context_free(ctx);
D
Daniel P. Berrange 已提交
277
    VIR_FREE(scontext);
278 279 280
    VIR_FREE(mcs);

    VIR_DEBUG("model=%s label=%s imagelabel=%s baselabel=%s",
281 282 283 284
              NULLSTR(def->seclabel.model),
              NULLSTR(def->seclabel.label),
              NULLSTR(def->seclabel.imagelabel),
              NULLSTR(def->seclabel.baselabel));
285

286 287 288
    return rc;
}

289
static int
290
SELinuxReserveSecurityLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
291 292
                            virDomainDefPtr def,
                            pid_t pid)
293 294 295 296 297
{
    security_context_t pctx;
    context_t ctx = NULL;
    const char *mcs;

298
    if (def->seclabel.type == VIR_DOMAIN_SECLABEL_STATIC)
299 300
        return 0;

301
    if (getpidcon(pid, &pctx) == -1) {
302
        virReportSystemError(errno,
303
                             _("unable to get PID %d security context"), pid);
304 305 306 307
        return -1;
    }

    ctx = context_new(pctx);
308
    freecon(pctx);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    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;
}



329 330 331 332 333 334 335
static int
SELinuxSecurityDriverProbe(void)
{
    return is_selinux_enabled() ? SECURITY_DRIVER_ENABLE : SECURITY_DRIVER_DISABLE;
}

static int
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
SELinuxSecurityDriverOpen(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
    return SELinuxInitialize();
}

static int
SELinuxSecurityDriverClose(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
    return 0;
}


static const char *SELinuxSecurityGetModel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
    return SECURITY_SELINUX_NAME;
}

static const char *SELinuxSecurityGetDOI(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
354 355 356 357 358
{
    /*
     * Where will the DOI come from?  SELinux configuration, or qemu
     * configuration? For the moment, we'll just set it to "0".
     */
359
    return SECURITY_SELINUX_VOID_DOI;
360 361 362
}

static int
363
SELinuxGetSecurityProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
364 365
                               virDomainDefPtr def ATTRIBUTE_UNUSED,
                               pid_t pid,
366
                               virSecurityLabelPtr sec)
367 368 369
{
    security_context_t ctx;

370
    if (getpidcon(pid, &ctx) == -1) {
371
        virReportSystemError(errno,
372
                             _("unable to get PID %d security context"),
373
                             pid);
374 375 376 377
        return -1;
    }

    if (strlen((char *) ctx) >= VIR_SECURITY_LABEL_BUFLEN) {
378
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
379
                               _("security label exceeds "
C
Cole Robinson 已提交
380
                                 "maximum length: %d"),
381
                               VIR_SECURITY_LABEL_BUFLEN - 1);
382
        freecon(ctx);
383 384 385 386
        return -1;
    }

    strcpy(sec->label, (char *) ctx);
387
    freecon(ctx);
388 389 390

    sec->enforcing = security_getenforce();
    if (sec->enforcing == -1) {
391
        virReportSystemError(errno, "%s",
392
                             _("error calling security_getenforce()"));
393 394 395 396 397 398
        return -1;
    }

    return 0;
}

399 400 401
/* 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.  */
402
static int
403
SELinuxSetFileconHelper(const char *path, char *tcon, bool optional)
404
{
405
    security_context_t econ;
406

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

409
    if (setfilecon(path, tcon) < 0) {
410 411
        int setfilecon_errno = errno;

412 413 414 415
        if (getfilecon(path, &econ) >= 0) {
            if (STREQ(tcon, econ)) {
                freecon(econ);
                /* It's alright, there's nothing to change anyway. */
416
                return optional ? 1 : 0;
417 418 419
            }
            freecon(econ);
        }
420 421

        /* if the error complaint is related to an image hosted on
422 423
         * an nfs mount, or a usbfs/sysfs filesystem not supporting
         * labelling, then just ignore it & hope for the best.
424
         * The user hopefully set one of the necessary SELinux
425
         * virt_use_{nfs,usb,pci}  boolean tunables to allow it...
426
         */
427
        if (setfilecon_errno != EOPNOTSUPP && setfilecon_errno != ENOTSUP) {
428
            virReportSystemError(setfilecon_errno,
429
                                 _("unable to set security context '%s' on '%s'"),
430
                                 tcon, path);
431 432
            if (security_getenforce() == 1)
                return -1;
433
        } else {
434 435 436 437 438 439 440 441 442 443 444 445 446 447
            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);
            }
448 449
            if (optional)
                return 1;
450
        }
451 452 453 454
    }
    return 0;
}

455 456 457 458 459 460 461 462 463 464 465 466
static int
SELinuxSetFileconOptional(const char *path, char *tcon)
{
    return SELinuxSetFileconHelper(path, tcon, true);
}

static int
SELinuxSetFilecon(const char *path, char *tcon)
{
    return SELinuxSetFileconHelper(path, tcon, false);
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
static int
SELinuxFSetFilecon(int fd, char *tcon)
{
    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 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
/* 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
}

525 526 527

/* This method shouldn't raise errors, since they'll overwrite
 * errors that the caller(s) are already dealing with */
528
static int
529
SELinuxRestoreSecurityFileLabel(const char *path)
530
{
531 532 533 534
    struct stat buf;
    security_context_t fcon = NULL;
    int rc = -1;
    char *newpath = NULL;
535
    char ebuf[1024];
536

537 538
    VIR_INFO("Restoring SELinux context on '%s'", path);

539
    if (virFileResolveLink(path, &newpath) < 0) {
540 541
        VIR_WARN("cannot resolve symlink %s: %s", path,
                 virStrerror(errno, ebuf, sizeof(ebuf)));
D
Daniel P. Berrange 已提交
542
        goto err;
543
    }
544

545
    if (stat(newpath, &buf) != 0) {
546 547
        VIR_WARN("cannot stat %s: %s", newpath,
                 virStrerror(errno, ebuf, sizeof(ebuf)));
D
Daniel P. Berrange 已提交
548
        goto err;
549
    }
D
Daniel P. Berrange 已提交
550

E
Eric Blake 已提交
551
    if (getContext(newpath, buf.st_mode, &fcon) < 0) {
552
        VIR_WARN("cannot lookup default selinux label for %s", newpath);
553
    } else {
554
        rc = SELinuxSetFilecon(newpath, fcon);
555
    }
556

557
err:
558
    freecon(fcon);
559 560
    VIR_FREE(newpath);
    return rc;
561 562
}

563
static int
564
SELinuxRestoreSecurityImageLabelInt(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
565
                                    virDomainDefPtr def,
566 567
                                    virDomainDiskDefPtr disk,
                                    int migrated)
568
{
569
    const virSecurityLabelDefPtr secdef = &def->seclabel;
570

571
    if (secdef->norelabel || (disk->seclabel && disk->seclabel->norelabel))
572 573
        return 0;

574 575 576 577 578 579 580 581 582 583 584
    /* 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;

585
    if (!disk->src || disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
586 587
        return 0;

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    /* 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;
        }
    }

604
    return SELinuxRestoreSecurityFileLabel(disk->src);
605 606
}

607 608

static int
609
SELinuxRestoreSecurityImageLabel(virSecurityManagerPtr mgr,
610
                                 virDomainDefPtr def,
611 612
                                 virDomainDiskDefPtr disk)
{
613
    return SELinuxRestoreSecurityImageLabelInt(mgr, def, disk, 0);
614 615 616
}


617 618 619 620 621 622 623
static int
SELinuxSetSecurityFileLabel(virDomainDiskDefPtr disk,
                            const char *path,
                            size_t depth,
                            void *opaque)
{
    const virSecurityLabelDefPtr secdef = opaque;
624
    int ret;
625

626 627 628 629 630 631 632
    if (disk->seclabel && disk->seclabel->norelabel)
        return 0;

    if (disk->seclabel && !disk->seclabel->norelabel &&
        disk->seclabel->label) {
        ret = SELinuxSetFilecon(path, disk->seclabel->label);
    } else if (depth == 0) {
633
        if (disk->shared) {
634
            ret = SELinuxSetFileconOptional(path, default_image_context);
635
        } else if (disk->readonly) {
636
            ret = SELinuxSetFileconOptional(path, default_content_context);
637
        } else if (secdef->imagelabel) {
638
            ret = SELinuxSetFileconOptional(path, secdef->imagelabel);
639
        } else {
640
            ret = 0;
641 642
        }
    } else {
643 644 645 646 647 648 649 650 651 652 653
        ret = SELinuxSetFileconOptional(path, default_content_context);
    }
    if (ret == 1 && !disk->seclabel) {
        /* If we failed to set a label, but virt_use_nfs let us
         * proceed anyway, then we don't need to relabel later.  */
        if (VIR_ALLOC(disk->seclabel) < 0) {
            virReportOOMError();
            return -1;
        }
        disk->seclabel->norelabel = true;
        ret = 0;
654
    }
655
    return ret;
656 657
}

658
static int
659
SELinuxSetSecurityImageLabel(virSecurityManagerPtr mgr,
660
                             virDomainDefPtr def,
661
                             virDomainDiskDefPtr disk)
662 663

{
664
    const virSecurityLabelDefPtr secdef = &def->seclabel;
665
    bool allowDiskFormatProbing = virSecurityManagerGetAllowDiskFormatProbing(mgr);
666

667
    if (secdef->norelabel)
668 669
        return 0;

670 671 672
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

673
    return virDomainDiskDefForeachPath(disk,
674
                                       allowDiskFormatProbing,
675
                                       true,
676 677
                                       SELinuxSetSecurityFileLabel,
                                       secdef);
678 679
}

680 681

static int
682
SELinuxSetSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
683 684
                           const char *file, void *opaque)
{
685 686
    virDomainDefPtr def = opaque;
    const virSecurityLabelDefPtr secdef = &def->seclabel;
687

688
    return SELinuxSetFilecon(file, secdef->imagelabel);
689 690 691
}

static int
692
SELinuxSetSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
693 694
                           const char *file, void *opaque)
{
695 696
    virDomainDefPtr def = opaque;
    const virSecurityLabelDefPtr secdef = &def->seclabel;
697

698
    return SELinuxSetFilecon(file, secdef->imagelabel);
699 700 701
}

static int
702
SELinuxSetSecurityHostdevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
703
                               virDomainDefPtr def,
704 705 706
                               virDomainHostdevDefPtr dev)

{
707
    const virSecurityLabelDefPtr secdef = &def->seclabel;
708 709
    int ret = -1;

710
    if (secdef->norelabel)
711 712
        return 0;

713 714 715 716 717
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

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

721 722
        if (!usb)
            goto done;
723

724
        ret = usbDeviceFileIterate(usb, SELinuxSetSecurityUSBLabel, def);
725
        usbFreeDevice(usb);
M
Mark McLoughlin 已提交
726
        break;
727 728 729
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
730
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
731 732 733 734 735 736 737
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

738
        ret = pciDeviceFileIterate(pci, SELinuxSetSecurityPCILabel, def);
739
        pciFreeDevice(pci);
740 741 742 743 744 745 746 747 748 749 750 751 752

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

753

754
static int
755
SELinuxRestoreSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
756 757 758
                               const char *file,
                               void *opaque ATTRIBUTE_UNUSED)
{
759
    return SELinuxRestoreSecurityFileLabel(file);
760 761 762
}

static int
763
SELinuxRestoreSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
764 765 766
                               const char *file,
                               void *opaque ATTRIBUTE_UNUSED)
{
767
    return SELinuxRestoreSecurityFileLabel(file);
768 769 770
}

static int
771
SELinuxRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
772
                                   virDomainDefPtr def,
773 774 775
                                   virDomainHostdevDefPtr dev)

{
776
    const virSecurityLabelDefPtr secdef = &def->seclabel;
777 778
    int ret = -1;

779
    if (secdef->norelabel)
780 781
        return 0;

782 783 784 785 786
    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
787
        usbDevice *usb = usbGetDevice(dev->source.subsys.u.usb.bus,
788
                                      dev->source.subsys.u.usb.device);
789 790 791 792

        if (!usb)
            goto done;

793
        ret = usbDeviceFileIterate(usb, SELinuxRestoreSecurityUSBLabel, NULL);
794
        usbFreeDevice(usb);
795 796 797 798 799

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
800
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
801 802 803 804 805 806 807
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

808
        ret = pciDeviceFileIterate(pci, SELinuxRestoreSecurityPCILabel, NULL);
809
        pciFreeDevice(pci);
810 811 812 813 814 815 816 817 818 819 820 821 822

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}

823 824

static int
825
SELinuxSetSecurityChardevLabel(virDomainDefPtr def,
826
                               virDomainChrSourceDefPtr dev)
827 828

{
829
    const virSecurityLabelDefPtr secdef = &def->seclabel;
830 831 832
    char *in = NULL, *out = NULL;
    int ret = -1;

833
    if (secdef->norelabel)
834 835 836 837 838 839 840 841 842
        return 0;

    switch (dev->type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
        ret = SELinuxSetFilecon(dev->data.file.path, secdef->imagelabel);
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
843 844 845 846 847 848
        if ((virAsprintf(&in, "%s.in", dev->data.file.path) < 0) ||
            (virAsprintf(&out, "%s.out", dev->data.file.path) < 0)) {
            virReportOOMError();
            goto done;
        }
        if (virFileExists(in) && virFileExists(out)) {
849
            if ((SELinuxSetFilecon(in, secdef->imagelabel) < 0) ||
850
                (SELinuxSetFilecon(out, secdef->imagelabel) < 0)) {
851
                goto done;
852 853 854
            }
        } else if (SELinuxSetFilecon(dev->data.file.path, secdef->imagelabel) < 0) {
            goto done;
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
        }
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

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

static int
871
SELinuxRestoreSecurityChardevLabel(virDomainDefPtr def,
872
                                   virDomainChrSourceDefPtr dev)
873 874

{
875
    const virSecurityLabelDefPtr secdef = &def->seclabel;
876 877 878
    char *in = NULL, *out = NULL;
    int ret = -1;

879
    if (secdef->norelabel)
880 881 882 883 884
        return 0;

    switch (dev->type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
885 886 887
        if (SELinuxRestoreSecurityFileLabel(dev->data.file.path) < 0)
            goto done;
        ret = 0;
888 889 890 891 892 893 894
        break;
    case VIR_DOMAIN_CHR_TYPE_PIPE:
        if ((virAsprintf(&out, "%s.out", dev->data.file.path) < 0) ||
            (virAsprintf(&in, "%s.in", dev->data.file.path) < 0)) {
            virReportOOMError();
            goto done;
        }
895 896 897 898 899 900
        if (virFileExists(in) && virFileExists(out)) {
            if ((SELinuxRestoreSecurityFileLabel(out) < 0) ||
                (SELinuxRestoreSecurityFileLabel(in) < 0)) {
                goto done;
            }
        } else if (SELinuxRestoreSecurityFileLabel(dev->data.file.path) < 0) {
901
            goto done;
902
        }
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

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


static int
919
SELinuxRestoreSecurityChardevCallback(virDomainDefPtr def,
920
                                      virDomainChrDefPtr dev,
921
                                      void *opaque ATTRIBUTE_UNUSED)
922
{
923 924 925 926 927
    /* 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;

928
    return SELinuxRestoreSecurityChardevLabel(def, &dev->source);
929 930 931
}


E
Eric Blake 已提交
932
static int
933
SELinuxRestoreSecuritySmartcardCallback(virDomainDefPtr def,
E
Eric Blake 已提交
934
                                        virDomainSmartcardDefPtr dev,
935
                                        void *opaque ATTRIBUTE_UNUSED)
E
Eric Blake 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949
{
    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;
        return SELinuxRestoreSecurityFileLabel(database);

    case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
950
        return SELinuxRestoreSecurityChardevLabel(def, &dev->data.passthru);
E
Eric Blake 已提交
951 952 953 954 955 956 957 958 959 960 961 962

    default:
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown smartcard type %d"),
                               dev->type);
        return -1;
    }

    return 0;
}


963
static int
964
SELinuxRestoreSecurityAllLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
965
                               virDomainDefPtr def,
966
                               int migrated ATTRIBUTE_UNUSED)
967
{
968
    const virSecurityLabelDefPtr secdef = &def->seclabel;
969 970
    int i;
    int rc = 0;
971

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

974
    if (secdef->norelabel)
975 976
        return 0;

977
    for (i = 0 ; i < def->nhostdevs ; i++) {
978
        if (SELinuxRestoreSecurityHostdevLabel(mgr,
979 980
                                               def,
                                               def->hostdevs[i]) < 0)
981
            rc = -1;
982
    }
983
    for (i = 0 ; i < def->ndisks ; i++) {
984
        if (SELinuxRestoreSecurityImageLabelInt(mgr,
985 986
                                                def,
                                                def->disks[i],
987
                                                migrated) < 0)
988 989
            rc = -1;
    }
990

991
    if (virDomainChrDefForeach(def,
992 993
                               false,
                               SELinuxRestoreSecurityChardevCallback,
994
                               NULL) < 0)
995 996
        rc = -1;

997
    if (virDomainSmartcardDefForeach(def,
E
Eric Blake 已提交
998 999
                                     false,
                                     SELinuxRestoreSecuritySmartcardCallback,
1000
                                     NULL) < 0)
E
Eric Blake 已提交
1001 1002
        rc = -1;

1003 1004
    if (def->os.kernel &&
        SELinuxRestoreSecurityFileLabel(def->os.kernel) < 0)
1005 1006
        rc = -1;

1007 1008
    if (def->os.initrd &&
        SELinuxRestoreSecurityFileLabel(def->os.initrd) < 0)
1009 1010
        rc = -1;

1011 1012 1013 1014
    return rc;
}

static int
1015
SELinuxReleaseSecurityLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1016
                            virDomainDefPtr def)
1017
{
1018
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    if (secdef->type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
        if (secdef->label != NULL) {
            context_t con = context_new(secdef->label);
            if (con) {
                mcsRemove(context_range_get(con));
                context_free(con);
            }
        }
        VIR_FREE(secdef->label);
        if (!secdef->baselabel)
            VIR_FREE(secdef->model);
1031 1032 1033
    }
    VIR_FREE(secdef->imagelabel);

1034
    return 0;
1035 1036
}

1037 1038

static int
1039
SELinuxSetSavedStateLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1040
                          virDomainDefPtr def,
1041 1042
                          const char *savefile)
{
1043
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1044

1045
    if (secdef->norelabel)
1046 1047
        return 0;

1048
    return SELinuxSetFilecon(savefile, secdef->imagelabel);
1049 1050 1051 1052
}


static int
1053
SELinuxRestoreSavedStateLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1054
                              virDomainDefPtr def,
1055 1056
                              const char *savefile)
{
1057
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1058

1059
    if (secdef->norelabel)
1060 1061
        return 0;

1062
    return SELinuxRestoreSecurityFileLabel(savefile);
1063 1064 1065
}


1066
static int
1067 1068
SELinuxSecurityVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                      virDomainDefPtr def)
1069 1070
{
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1071 1072 1073 1074 1075 1076 1077 1078 1079
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
                               secdef->model, virSecurityManagerGetModel(mgr));
        return -1;
    }

1080 1081
    if (secdef->type == VIR_DOMAIN_SECLABEL_STATIC) {
        if (security_check_context(secdef->label) != 0) {
1082
            virSecurityReportError(VIR_ERR_XML_ERROR,
1083 1084 1085 1086 1087 1088 1089
                                   _("Invalid security label %s"), secdef->label);
            return -1;
        }
    }
    return 0;
}

1090
static int
1091
SELinuxSetSecurityProcessLabel(virSecurityManagerPtr mgr,
1092
                               virDomainDefPtr def)
1093 1094
{
    /* TODO: verify DOI */
1095
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1096

1097
    if (def->seclabel.label == NULL)
1098 1099
        return 0;

1100
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1101
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
1102 1103 1104
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
1105
                               secdef->model, virSecurityManagerGetModel(mgr));
1106
        if (security_getenforce() == 1)
1107
            return -1;
1108 1109 1110
    }

    if (setexeccon(secdef->label) == -1) {
1111
        virReportSystemError(errno,
1112 1113
                             _("unable to set security context '%s'"),
                             secdef->label);
1114
        if (security_getenforce() == 1)
1115
            return -1;
1116 1117
    }

1118 1119 1120
    return 0;
}

1121
static int
1122
SELinuxSetSecurityDaemonSocketLabel(virSecurityManagerPtr mgr,
1123
                                    virDomainDefPtr def)
1124 1125
{
    /* TODO: verify DOI */
1126
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1127 1128 1129 1130 1131
    context_t execcon = NULL;
    context_t proccon = NULL;
    security_context_t scon = NULL;
    int rc = -1;

1132
    if (def->seclabel.label == NULL)
1133 1134
        return 0;

1135
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1136 1137 1138 1139
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
1140
                               secdef->model, virSecurityManagerGetModel(mgr));
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
        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",
1173
              def->name, context_str(proccon));
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    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;
}

1192 1193
static int
SELinuxSetSecuritySocketLabel(virSecurityManagerPtr mgr,
1194
                              virDomainDefPtr vm)
1195
{
1196
    const virSecurityLabelDefPtr secdef = &vm->seclabel;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    int rc = -1;

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

    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
                               secdef->model, virSecurityManagerGetModel(mgr));
        goto done;
    }

    VIR_DEBUG("Setting VM %s socket context %s",
1212
              vm->name, secdef->label);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    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;
}

1229
static int
1230
SELinuxClearSecuritySocketLabel(virSecurityManagerPtr mgr,
1231
                                virDomainDefPtr def)
1232 1233
{
    /* TODO: verify DOI */
1234
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1235

1236
    if (def->seclabel.label == NULL)
1237 1238
        return 0;

1239
    if (!STREQ(virSecurityManagerGetModel(mgr), secdef->model)) {
1240 1241 1242 1243
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("security label driver mismatch: "
                                 "'%s' model configured for domain, but "
                                 "hypervisor driver is '%s'."),
1244
                               secdef->model, virSecurityManagerGetModel(mgr));
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
        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;
}

1259 1260

static int
1261
SELinuxSetSecurityChardevCallback(virDomainDefPtr def,
1262
                                  virDomainChrDefPtr dev,
1263
                                  void *opaque ATTRIBUTE_UNUSED)
1264
{
1265 1266 1267 1268 1269
    /* 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;

1270
    return SELinuxSetSecurityChardevLabel(def, &dev->source);
1271 1272 1273
}


E
Eric Blake 已提交
1274
static int
1275
SELinuxSetSecuritySmartcardCallback(virDomainDefPtr def,
E
Eric Blake 已提交
1276
                                    virDomainSmartcardDefPtr dev,
1277
                                    void *opaque ATTRIBUTE_UNUSED)
E
Eric Blake 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
{
    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;
        return SELinuxSetFilecon(database, default_content_context);

    case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
1292
        return SELinuxSetSecurityChardevLabel(def, &dev->data.passthru);
E
Eric Blake 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

    default:
        virSecurityReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown smartcard type %d"),
                               dev->type);
        return -1;
    }

    return 0;
}


1305
static int
1306
SELinuxSetSecurityAllLabel(virSecurityManagerPtr mgr,
1307
                           virDomainDefPtr def,
1308
                           const char *stdin_path)
1309
{
1310
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1311 1312
    int i;

1313
    if (secdef->norelabel)
1314 1315
        return 0;

1316
    for (i = 0 ; i < def->ndisks ; i++) {
1317
        /* XXX fixme - we need to recursively label the entire tree :-( */
1318
        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_DIR) {
1319
            VIR_WARN("Unable to relabel directory tree %s for disk %s",
1320
                     def->disks[i]->src, def->disks[i]->dst);
1321
            continue;
1322
        }
1323
        if (SELinuxSetSecurityImageLabel(mgr,
1324
                                         def, def->disks[i]) < 0)
1325 1326
            return -1;
    }
1327
    /* XXX fixme process  def->fss if relabel == true */
1328

1329
    for (i = 0 ; i < def->nhostdevs ; i++) {
1330
        if (SELinuxSetSecurityHostdevLabel(mgr,
1331 1332
                                           def,
                                           def->hostdevs[i]) < 0)
1333
            return -1;
1334 1335
    }

1336
    if (virDomainChrDefForeach(def,
1337 1338
                               true,
                               SELinuxSetSecurityChardevCallback,
1339
                               NULL) < 0)
1340 1341
        return -1;

1342
    if (virDomainSmartcardDefForeach(def,
E
Eric Blake 已提交
1343 1344
                                     true,
                                     SELinuxSetSecuritySmartcardCallback,
1345
                                     NULL) < 0)
E
Eric Blake 已提交
1346 1347
        return -1;

1348 1349
    if (def->os.kernel &&
        SELinuxSetFilecon(def->os.kernel, default_content_context) < 0)
1350 1351
        return -1;

1352 1353
    if (def->os.initrd &&
        SELinuxSetFilecon(def->os.initrd, default_content_context) < 0)
1354 1355
        return -1;

1356 1357 1358 1359 1360 1361
    if (stdin_path) {
        if (SELinuxSetFilecon(stdin_path, default_content_context) < 0 &&
            virStorageFileIsSharedFSType(stdin_path,
                                         VIR_STORAGE_FILE_SHFS_NFS) != 1)
            return -1;
    }
1362

1363 1364 1365
    return 0;
}

1366
static int
1367
SELinuxSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1368
                       virDomainDefPtr def,
1369
                       int fd)
1370
{
1371
    const virSecurityLabelDefPtr secdef = &def->seclabel;
1372 1373 1374 1375 1376 1377 1378

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

    return SELinuxFSetFilecon(fd, secdef->imagelabel);
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
virSecurityDriver virSecurityDriverSELinux = {
    0,
    SECURITY_SELINUX_NAME,
    SELinuxSecurityDriverProbe,
    SELinuxSecurityDriverOpen,
    SELinuxSecurityDriverClose,

    SELinuxSecurityGetModel,
    SELinuxSecurityGetDOI,

    SELinuxSecurityVerify,

    SELinuxSetSecurityImageLabel,
    SELinuxRestoreSecurityImageLabel,

1394
    SELinuxSetSecurityDaemonSocketLabel,
1395
    SELinuxSetSecuritySocketLabel,
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
    SELinuxClearSecuritySocketLabel,

    SELinuxGenSecurityLabel,
    SELinuxReserveSecurityLabel,
    SELinuxReleaseSecurityLabel,

    SELinuxGetSecurityProcessLabel,
    SELinuxSetSecurityProcessLabel,

    SELinuxSetSecurityAllLabel,
    SELinuxRestoreSecurityAllLabel,

    SELinuxSetSecurityHostdevLabel,
    SELinuxRestoreSecurityHostdevLabel,

    SELinuxSetSavedStateLabel,
    SELinuxRestoreSavedStateLabel,
1413

1414
    SELinuxSetImageFDLabel,
1415
};