security_dac.c 34.7 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2013 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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.
 *
 * 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
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26
 *
 * POSIX DAC security driver
 */

#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "security_dac.h"
27
#include "virerror.h"
28
#include "virfile.h"
29
#include "viralloc.h"
30
#include "virlog.h"
31
#include "virpci.h"
32
#include "virusb.h"
33
#include "virscsi.h"
34
#include "virstoragefile.h"
35
#include "virstring.h"
36 37

#define VIR_FROM_THIS VIR_FROM_SECURITY
38
#define SECURITY_DAC_NAME "dac"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

typedef struct _virSecurityDACData virSecurityDACData;
typedef virSecurityDACData *virSecurityDACDataPtr;

struct _virSecurityDACData {
    uid_t user;
    gid_t group;
    bool dynamicOwnership;
};

void virSecurityDACSetUser(virSecurityManagerPtr mgr,
                           uid_t user)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->user = user;
}

void virSecurityDACSetGroup(virSecurityManagerPtr mgr,
                            gid_t group)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->group = group;
}

void virSecurityDACSetDynamicOwnership(virSecurityManagerPtr mgr,
                                       bool dynamicOwnership)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->dynamicOwnership = dynamicOwnership;
}

70 71 72
static
int parseIds(const char *label, uid_t *uidPtr, gid_t *gidPtr)
{
73
    int rc = -1;
74 75
    uid_t theuid;
    gid_t thegid;
76 77 78 79 80
    char *tmp_label = NULL;
    char *sep = NULL;
    char *owner = NULL;
    char *group = NULL;

81
    if (VIR_STRDUP(tmp_label, label) < 0)
82
        goto cleanup;
83

84 85 86 87 88 89 90 91 92 93 94 95
    /* Split label */
    sep = strchr(tmp_label, ':');
    if (sep == NULL) {
        virReportError(VIR_ERR_INVALID_ARG,
                       _("Missing separator ':' in DAC label \"%s\""),
                       label);
        goto cleanup;
    }
    *sep = '\0';
    owner = tmp_label;
    group = sep + 1;

96 97 98 99 100 101
    /* Parse owner and group, error message is defined by
     * virGetUserID or virGetGroupID.
     */
    if (virGetUserID(owner, &theuid) < 0 ||
        virGetGroupID(group, &thegid) < 0)
        goto cleanup;
102 103

    if (uidPtr)
104
        *uidPtr = theuid;
105
    if (gidPtr)
106
        *gidPtr = thegid;
107 108 109 110 111 112 113

    rc = 0;

cleanup:
    VIR_FREE(tmp_label);

    return rc;
114 115
}

116
/* returns 1 if label isn't found, 0 on success, -1 on error */
117 118 119 120 121 122 123 124
static
int virSecurityDACParseIds(virDomainDefPtr def, uid_t *uidPtr, gid_t *gidPtr)
{
    uid_t uid;
    gid_t gid;
    virSecurityLabelDefPtr seclabel;

    if (def == NULL)
125
        return 1;
126 127

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
128
    if (seclabel == NULL || seclabel->label == NULL) {
129 130
        VIR_DEBUG("DAC seclabel for domain '%s' wasn't found", def->name);
        return 1;
131 132
    }

133
    if (parseIds(seclabel->label, &uid, &gid) < 0)
134 135 136 137 138 139 140 141 142 143 144 145 146 147
        return -1;

    if (uidPtr)
        *uidPtr = uid;
    if (gidPtr)
        *gidPtr = gid;

    return 0;
}

static
int virSecurityDACGetIds(virDomainDefPtr def, virSecurityDACDataPtr priv,
                         uid_t *uidPtr, gid_t *gidPtr)
{
148 149 150 151 152 153 154
    int ret;

    if (!def && !priv) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to determine default DAC seclabel "
                         "for an unknown object"));
        return -1;
155
    }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    if ((ret = virSecurityDACParseIds(def, uidPtr, gidPtr)) <= 0)
        return ret;

    if (!priv) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("DAC seclabel couldn't be determined "
                         "for domain '%s'"), def->name);
        return -1;
    }

    if (uidPtr)
        *uidPtr = priv->user;
    if (gidPtr)
        *gidPtr = priv->group;

    return 0;
173 174
}

175 176

/* returns 1 if label isn't found, 0 on success, -1 on error */
177 178 179 180 181 182 183 184 185
static
int virSecurityDACParseImageIds(virDomainDefPtr def,
                                uid_t *uidPtr, gid_t *gidPtr)
{
    uid_t uid;
    gid_t gid;
    virSecurityLabelDefPtr seclabel;

    if (def == NULL)
186
        return 1;
187 188

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
189
    if (seclabel == NULL || seclabel->imagelabel == NULL) {
190 191
        VIR_DEBUG("DAC imagelabel for domain '%s' wasn't found", def->name);
        return 1;
192 193
    }

194
    if (parseIds(seclabel->imagelabel, &uid, &gid) < 0)
195 196 197 198 199 200 201 202 203 204 205 206 207 208
        return -1;

    if (uidPtr)
        *uidPtr = uid;
    if (gidPtr)
        *gidPtr = gid;

    return 0;
}

static
int virSecurityDACGetImageIds(virDomainDefPtr def, virSecurityDACDataPtr priv,
                         uid_t *uidPtr, gid_t *gidPtr)
{
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    int ret;

    if (!def && !priv) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to determine default DAC imagelabel "
                         "for an unknown object"));
        return -1;
    }

    if ((ret = virSecurityDACParseImageIds(def, uidPtr, gidPtr)) <= 0)
        return ret;

    if (!priv) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("DAC imagelabel couldn't be determined "
                         "for domain '%s'"), def->name);
        return -1;
226
    }
227 228 229 230 231 232 233

    if (uidPtr)
        *uidPtr = priv->user;
    if (gidPtr)
        *gidPtr = priv->group;

    return 0;
234 235 236
}


237
static virSecurityDriverStatus
238
virSecurityDACProbe(const char *virtDriver ATTRIBUTE_UNUSED)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
    return SECURITY_DRIVER_ENABLE;
}

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

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


static const char * virSecurityDACGetModel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
258
    return SECURITY_DAC_NAME;
259 260 261 262 263 264 265 266
}

static const char * virSecurityDACGetDOI(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
    return "0";
}

static int
267
virSecurityDACSetOwnership(const char *path, uid_t uid, gid_t gid)
268
{
269 270
    VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'",
             path, (long) uid, (long) gid);
271 272 273 274 275 276 277 278 279 280 281 282 283

    if (chown(path, uid, gid) < 0) {
        struct stat sb;
        int chown_errno = errno;

        if (stat(path, &sb) >= 0) {
            if (sb.st_uid == uid &&
                sb.st_gid == gid) {
                /* It's alright, there's nothing to change anyway. */
                return 0;
            }
        }

284
        if (chown_errno == EOPNOTSUPP || chown_errno == EINVAL) {
285 286 287
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "supported by filesystem",
                     (long) uid, (long) gid, path);
288
        } else if (chown_errno == EPERM) {
289 290 291
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "permitted",
                     (long) uid, (long) gid, path);
292
        } else if (chown_errno == EROFS) {
293 294 295
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "possible on readonly filesystem",
                     (long) uid, (long) gid, path);
296 297
        } else {
            virReportSystemError(chown_errno,
298 299 300
                                 _("unable to set user and group to '%ld:%ld' "
                                   "on '%s'"),
                                 (long) uid, (long) gid, path);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            return -1;
        }
    }
    return 0;
}

static int
virSecurityDACRestoreSecurityFileLabel(const char *path)
{
    struct stat buf;
    int rc = -1;
    char *newpath = NULL;

    VIR_INFO("Restoring DAC user and group on '%s'", path);

    if (virFileResolveLink(path, &newpath) < 0) {
        virReportSystemError(errno,
                             _("cannot resolve symlink %s"), path);
        goto err;
    }

    if (stat(newpath, &buf) != 0)
        goto err;

    /* XXX record previous ownership */
    rc = virSecurityDACSetOwnership(newpath, 0, 0);

err:
    VIR_FREE(newpath);
    return rc;
}


static int
virSecurityDACSetSecurityFileLabel(virDomainDiskDefPtr disk ATTRIBUTE_UNUSED,
                                   const char *path,
                                   size_t depth ATTRIBUTE_UNUSED,
                                   void *opaque)
{
340 341 342
    void **params = opaque;
    virSecurityManagerPtr mgr = params[0];
    virDomainDefPtr def = params[1];
343
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
344 345 346 347 348
    uid_t user;
    gid_t group;

    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;
349

350
    return virSecurityDACSetOwnership(path, user, group);
351 352 353 354 355
}


static int
virSecurityDACSetSecurityImageLabel(virSecurityManagerPtr mgr,
356
                                    virDomainDefPtr def ATTRIBUTE_UNUSED,
357 358 359
                                    virDomainDiskDefPtr disk)

{
360
    void *params[2];
361 362 363 364 365
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

366 367 368
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

369 370
    params[0] = mgr;
    params[1] = def;
371 372 373
    return virDomainDiskDefForeachPath(disk,
                                       false,
                                       virSecurityDACSetSecurityFileLabel,
374
                                       params);
375 376 377 378 379
}


static int
virSecurityDACRestoreSecurityImageLabelInt(virSecurityManagerPtr mgr,
380
                                           virDomainDefPtr def ATTRIBUTE_UNUSED,
381 382 383 384 385 386 387 388
                                           virDomainDiskDefPtr disk,
                                           int migrated)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

389 390 391
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    /* Don't restore labels on readoly/shared disks, because
     * other VMs may still be accessing these
     * Alternatively we could iterate over all running
     * domains and try to figure out if it is in use, but
     * this would not work for clustered filesystems, since
     * we can't see running VMs using the file on other nodes
     * Safest bet is thus to skip the restore step.
     */
    if (disk->readonly || disk->shared)
        return 0;

    if (!disk->src)
        return 0;

    /* 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;
        }
    }

    return virSecurityDACRestoreSecurityFileLabel(disk->src);
}


static int
virSecurityDACRestoreSecurityImageLabel(virSecurityManagerPtr mgr,
428
                                        virDomainDefPtr def,
429 430
                                        virDomainDiskDefPtr disk)
{
431
    return virSecurityDACRestoreSecurityImageLabelInt(mgr, def, disk, 0);
432 433 434 435
}


static int
436 437
virSecurityDACSetSecurityHostdevLabelHelper(const char *file,
                                            void *opaque)
438
{
439 440 441
    void **params = opaque;
    virSecurityManagerPtr mgr = params[0];
    virDomainDefPtr def = params[1];
442
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
443 444
    uid_t user;
    gid_t group;
445

446 447 448 449
    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

    return virSecurityDACSetOwnership(file, user, group);
450 451 452
}


453 454 455 456 457 458 459 460 461
static int
virSecurityDACSetSecurityPCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
                                  const char *file,
                                  void *opaque)
{
    return virSecurityDACSetSecurityHostdevLabelHelper(file, opaque);
}


462
static int
463
virSecurityDACSetSecurityUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
464 465 466
                                  const char *file,
                                  void *opaque)
{
467 468
    return virSecurityDACSetSecurityHostdevLabelHelper(file, opaque);
}
469

470

471 472 473 474 475 476
static int
virSecurityDACSetSecuritySCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                                   const char *file,
                                   void *opaque)
{
    return virSecurityDACSetSecurityHostdevLabelHelper(file, opaque);
477 478 479 480 481
}


static int
virSecurityDACSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
482
                                      virDomainDefPtr def,
483 484
                                      virDomainHostdevDefPtr dev,
                                      const char *vroot)
485
{
486
    void *params[] = {mgr, def};
487 488 489 490 491 492 493 494 495 496 497
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int ret = -1;

    if (!priv->dynamicOwnership)
        return 0;

    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
498
        virUSBDevicePtr usb;
499

500 501 502
        if (dev->missing)
            return 0;

503 504 505
        usb = virUSBDeviceNew(dev->source.subsys.u.usb.bus,
                              dev->source.subsys.u.usb.device,
                              vroot);
506 507 508
        if (!usb)
            goto done;

509 510 511
        ret = virUSBDeviceFileIterate(usb, virSecurityDACSetSecurityUSBLabel,
                                      params);
        virUSBDeviceFree(usb);
512 513 514 515
        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
516
        virPCIDevicePtr pci =
517 518 519 520
            virPCIDeviceNew(dev->source.subsys.u.pci.addr.domain,
                            dev->source.subsys.u.pci.addr.bus,
                            dev->source.subsys.u.pci.addr.slot,
                            dev->source.subsys.u.pci.addr.function);
521 522 523 524

        if (!pci)
            goto done;

525
        if (dev->source.subsys.u.pci.backend
526
            == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
527 528
            char *vfioGroupDev = virPCIDeviceGetVFIOGroupDev(pci);

529 530
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
531
                goto done;
532
            }
533 534 535 536 537 538 539
            ret = virSecurityDACSetSecurityPCILabel(pci, vfioGroupDev, params);
            VIR_FREE(vfioGroupDev);
        } else {
            ret = virPCIDeviceFileIterate(pci, virSecurityDACSetSecurityPCILabel,
                                          params);
        }

540
        virPCIDeviceFree(pci);
541 542 543
        break;
    }

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
        virSCSIDevicePtr scsi =
            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
                             dev->source.subsys.u.scsi.bus,
                             dev->source.subsys.u.scsi.target,
                             dev->source.subsys.u.scsi.unit,
                             dev->readonly);

        if (!scsi)
            goto done;

        ret = virSCSIDeviceFileIterate(scsi, virSecurityDACSetSecuritySCSILabel,
                                       params);
        virSCSIDeviceFree(scsi);

        break;
    }

562 563 564 565 566 567 568 569 570 571 572
    default:
        ret = 0;
        break;
    }

done:
    return ret;
}


static int
573
virSecurityDACRestoreSecurityPCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
574 575 576 577 578 579 580 581
                                      const char *file,
                                      void *opaque ATTRIBUTE_UNUSED)
{
    return virSecurityDACRestoreSecurityFileLabel(file);
}


static int
582 583 584
virSecurityDACRestoreSecurityUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                                      const char *file,
                                      void *opaque ATTRIBUTE_UNUSED)
585 586 587 588 589
{
    return virSecurityDACRestoreSecurityFileLabel(file);
}


590 591 592 593 594 595 596 597 598
static int
virSecurityDACRestoreSecuritySCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                                       const char *file,
                                       void *opaque ATTRIBUTE_UNUSED)
{
    return virSecurityDACRestoreSecurityFileLabel(file);
}


599 600
static int
virSecurityDACRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
601 602 603
                                          virDomainDefPtr def ATTRIBUTE_UNUSED,
                                          virDomainHostdevDefPtr dev,
                                          const char *vroot)
604 605 606 607 608 609 610 611 612 613 614 615 616

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int ret = -1;

    if (!priv->dynamicOwnership)
        return 0;

    if (dev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
        return 0;

    switch (dev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
617
        virUSBDevicePtr usb;
618 619 620

        if (dev->missing)
            return 0;
621

622 623 624
        usb = virUSBDeviceNew(dev->source.subsys.u.usb.bus,
                              dev->source.subsys.u.usb.device,
                              vroot);
625 626 627
        if (!usb)
            goto done;

628 629
        ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreSecurityUSBLabel, mgr);
        virUSBDeviceFree(usb);
630 631 632 633 634

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
635
        virPCIDevicePtr pci =
636 637 638 639
            virPCIDeviceNew(dev->source.subsys.u.pci.addr.domain,
                            dev->source.subsys.u.pci.addr.bus,
                            dev->source.subsys.u.pci.addr.slot,
                            dev->source.subsys.u.pci.addr.function);
640 641 642 643

        if (!pci)
            goto done;

644
        if (dev->source.subsys.u.pci.backend
645
            == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
646 647
            char *vfioGroupDev = virPCIDeviceGetVFIOGroupDev(pci);

648 649
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
650
                goto done;
651
            }
652 653 654 655 656
            ret = virSecurityDACRestoreSecurityPCILabel(pci, vfioGroupDev, mgr);
            VIR_FREE(vfioGroupDev);
        } else {
            ret = virPCIDeviceFileIterate(pci, virSecurityDACRestoreSecurityPCILabel, mgr);
        }
657
        virPCIDeviceFree(pci);
658 659 660
        break;
    }

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
        virSCSIDevicePtr scsi =
            virSCSIDeviceNew(dev->source.subsys.u.scsi.adapter,
                             dev->source.subsys.u.scsi.bus,
                             dev->source.subsys.u.scsi.target,
                             dev->source.subsys.u.scsi.unit,
                             dev->readonly);

        if (!scsi)
            goto done;

        ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSecuritySCSILabel, mgr);
        virSCSIDeviceFree(scsi);

        break;
    }

678 679 680 681 682 683 684 685 686 687 688 689
    default:
        ret = 0;
        break;
    }

done:
    return ret;
}


static int
virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
690
                              virDomainDefPtr def,
691
                              virDomainChrSourceDefPtr dev)
692 693 694 695 696

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    char *in = NULL, *out = NULL;
    int ret = -1;
697 698 699 700 701
    uid_t user;
    gid_t group;

    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;
702 703 704 705

    switch (dev->type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
706
        ret = virSecurityDACSetOwnership(dev->data.file.path, user, group);
707 708 709
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
710 711 712 713 714 715
        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)) {
716 717
            if ((virSecurityDACSetOwnership(in, user, group) < 0) ||
                (virSecurityDACSetOwnership(out, user, group) < 0)) {
718
                goto done;
719 720
            }
        } else if (virSecurityDACSetOwnership(dev->data.file.path,
721
                                              user, group) < 0) {
722
            goto done;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        }
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

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

static int
virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
740
                                  virDomainChrSourceDefPtr dev)
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
{
    char *in = NULL, *out = NULL;
    int ret = -1;

    switch (dev->type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
        ret = virSecurityDACRestoreSecurityFileLabel(dev->data.file.path);
        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;
        }
757 758 759
        if (virFileExists(in) && virFileExists(out)) {
            if ((virSecurityDACRestoreSecurityFileLabel(out) < 0) ||
                (virSecurityDACRestoreSecurityFileLabel(in) < 0)) {
760
            goto done;
761 762 763 764
            }
        } else if (virSecurityDACRestoreSecurityFileLabel(dev->data.file.path) < 0) {
            goto done;
        }
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

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


static int
virSecurityDACRestoreChardevCallback(virDomainDefPtr def ATTRIBUTE_UNUSED,
                                     virDomainChrDefPtr dev,
                                     void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

787
    return virSecurityDACRestoreChardevLabel(mgr, &dev->source);
788 789 790
}


791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
static int
virSecurityDACSetSecurityTPMFileLabel(virSecurityManagerPtr mgr,
                                      virDomainDefPtr def,
                                      virDomainTPMDefPtr tpm)
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
        ret = virSecurityDACSetChardevLabel(mgr, def,
                                            &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


static int
virSecurityDACRestoreSecurityTPMFileLabel(
                                   virSecurityManagerPtr mgr,
                                   virDomainTPMDefPtr tpm)
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
        ret = virSecurityDACRestoreChardevLabel(mgr,
                                          &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


831 832
static int
virSecurityDACRestoreSecurityAllLabel(virSecurityManagerPtr mgr,
833
                                      virDomainDefPtr def,
834 835 836 837 838 839 840 841 842 843 844
                                      int migrated)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int i;
    int rc = 0;

    if (!priv->dynamicOwnership)
        return 0;


    VIR_DEBUG("Restoring security label on %s migrated=%d",
845
              def->name, migrated);
846

847
    for (i = 0; i < def->nhostdevs; i++) {
848
        if (virSecurityDACRestoreSecurityHostdevLabel(mgr,
849
                                                      def,
850 851
                                                      def->hostdevs[i],
                                                      NULL) < 0)
852 853
            rc = -1;
    }
854
    for (i = 0; i < def->ndisks; i++) {
855
        if (virSecurityDACRestoreSecurityImageLabelInt(mgr,
856 857
                                                       def,
                                                       def->disks[i],
858 859 860 861
                                                       migrated) < 0)
            rc = -1;
    }

862
    if (virDomainChrDefForeach(def,
863 864
                               false,
                               virSecurityDACRestoreChardevCallback,
865
                               mgr) < 0)
866 867
        rc = -1;

868 869 870 871 872 873
    if (def->tpm) {
        if (virSecurityDACRestoreSecurityTPMFileLabel(mgr,
                                                      def->tpm) < 0)
            rc = -1;
    }

874 875
    if (def->os.kernel &&
        virSecurityDACRestoreSecurityFileLabel(def->os.kernel) < 0)
876 877
        rc = -1;

878 879
    if (def->os.initrd &&
        virSecurityDACRestoreSecurityFileLabel(def->os.initrd) < 0)
880 881
        rc = -1;

O
Olivia Yin 已提交
882 883 884 885
    if (def->os.dtb &&
        virSecurityDACRestoreSecurityFileLabel(def->os.dtb) < 0)
        rc = -1;

886 887 888 889 890 891 892 893 894 895 896
    return rc;
}


static int
virSecurityDACSetChardevCallback(virDomainDefPtr def ATTRIBUTE_UNUSED,
                                 virDomainChrDefPtr dev,
                                 void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

897
    return virSecurityDACSetChardevLabel(mgr, def, &dev->source);
898 899 900 901 902
}


static int
virSecurityDACSetSecurityAllLabel(virSecurityManagerPtr mgr,
903
                                  virDomainDefPtr def,
904 905 906 907
                                  const char *stdin_path ATTRIBUTE_UNUSED)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int i;
908 909
    uid_t user;
    gid_t group;
910 911 912 913

    if (!priv->dynamicOwnership)
        return 0;

914
    for (i = 0; i < def->ndisks; i++) {
915
        /* XXX fixme - we need to recursively label the entire tree :-( */
916
        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_DIR)
917 918
            continue;
        if (virSecurityDACSetSecurityImageLabel(mgr,
919 920
                                                def,
                                                def->disks[i]) < 0)
921 922
            return -1;
    }
923
    for (i = 0; i < def->nhostdevs; i++) {
924
        if (virSecurityDACSetSecurityHostdevLabel(mgr,
925
                                                  def,
926 927
                                                  def->hostdevs[i],
                                                  NULL) < 0)
928 929 930
            return -1;
    }

931
    if (virDomainChrDefForeach(def,
932 933
                               true,
                               virSecurityDACSetChardevCallback,
934
                               mgr) < 0)
935 936
        return -1;

937 938 939 940 941 942 943
    if (def->tpm) {
        if (virSecurityDACSetSecurityTPMFileLabel(mgr,
                                                  def,
                                                  def->tpm) < 0)
            return -1;
    }

944 945 946
    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;

947
    if (def->os.kernel &&
948
        virSecurityDACSetOwnership(def->os.kernel, user, group) < 0)
949 950
        return -1;

951
    if (def->os.initrd &&
952
        virSecurityDACSetOwnership(def->os.initrd, user, group) < 0)
953 954
        return -1;

O
Olivia Yin 已提交
955 956 957 958
    if (def->os.dtb &&
        virSecurityDACSetOwnership(def->os.dtb, user, group) < 0)
        return -1;

959 960 961 962 963 964
    return 0;
}


static int
virSecurityDACSetSavedStateLabel(virSecurityManagerPtr mgr,
965
                                 virDomainDefPtr def,
966 967
                                 const char *savefile)
{
968 969
    uid_t user;
    gid_t group;
970 971
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

972 973 974 975
    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;

    return virSecurityDACSetOwnership(savefile, user, group);
976 977 978 979 980
}


static int
virSecurityDACRestoreSavedStateLabel(virSecurityManagerPtr mgr,
981
                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
982 983 984 985 986 987 988 989 990 991 992 993 994
                                     const char *savefile)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

    return virSecurityDACRestoreSecurityFileLabel(savefile);
}


static int
virSecurityDACSetProcessLabel(virSecurityManagerPtr mgr,
995
                              virDomainDefPtr def ATTRIBUTE_UNUSED)
996
{
997 998
    uid_t user;
    gid_t group;
999 1000
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

1001 1002 1003
    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

1004 1005
    VIR_DEBUG("Dropping privileges of DEF to %u:%u",
              (unsigned int) user, (unsigned int) group);
1006

1007
    if (virSetUIDGID(user, group) < 0)
1008 1009 1010 1011 1012 1013
        return -1;

    return 0;
}


1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static int
virSecurityDACSetChildProcessLabel(virSecurityManagerPtr mgr,
                                   virDomainDefPtr def ATTRIBUTE_UNUSED,
                                   virCommandPtr cmd)
{
    uid_t user;
    gid_t group;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

    VIR_DEBUG("Setting child to drop privileges of DEF to %u:%u",
              (unsigned int) user, (unsigned int) group);

    virCommandSetUID(cmd, user);
    virCommandSetGID(cmd, group);
    return 0;
}


1035 1036 1037 1038 1039 1040 1041 1042
static int
virSecurityDACVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
1043 1044
virSecurityDACGenLabel(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
1045
{
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    int rc = -1;
    virSecurityLabelDefPtr seclabel;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
    if (seclabel == NULL) {
        return rc;
    }

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

    if (seclabel->model
        && STRNEQ(seclabel->model, SECURITY_DAC_NAME)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("security label model %s is not supported "
                         "with selinux"),
                       seclabel->model);
            return rc;
    }

1071
    switch (seclabel->type) {
1072 1073 1074 1075 1076 1077 1078 1079 1080
    case VIR_DOMAIN_SECLABEL_STATIC:
        if (seclabel->label == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("missing label for static security "
                             "driver in domain %s"), def->name);
            return rc;
        }
        break;
    case VIR_DOMAIN_SECLABEL_DYNAMIC:
E
Eric Blake 已提交
1081
        if (virAsprintf(&seclabel->label, "%u:%u",
1082 1083
                        (unsigned int) priv->user,
                        (unsigned int) priv->group) < 0) {
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
            virReportOOMError();
            return rc;
        }
        if (seclabel->label == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot generate dac user and group id "
                             "for domain %s"), def->name);
            return rc;
        }
        break;
    case VIR_DOMAIN_SECLABEL_NONE:
        /* no op */
1096
        return 0;
1097 1098 1099 1100 1101 1102 1103
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected security label type '%s'"),
                       virDomainSeclabelTypeToString(seclabel->type));
        return rc;
    }

1104 1105 1106 1107
    if (!seclabel->norelabel && !seclabel->imagelabel &&
        VIR_STRDUP(seclabel->imagelabel, seclabel->label) < 0) {
        VIR_FREE(seclabel->label);
        return rc;
1108 1109
    }

1110 1111 1112 1113 1114
    return 0;
}

static int
virSecurityDACReleaseLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1115
                           virDomainDefPtr def ATTRIBUTE_UNUSED)
1116 1117 1118 1119 1120 1121
{
    return 0;
}

static int
virSecurityDACReserveLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1122 1123
                           virDomainDefPtr def ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED)
1124 1125 1126 1127 1128 1129
{
    return 0;
}

static int
virSecurityDACGetProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1130 1131
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
                              pid_t pid ATTRIBUTE_UNUSED,
1132 1133
                              virSecurityLabelPtr seclabel ATTRIBUTE_UNUSED)
{
1134 1135 1136 1137 1138 1139 1140
    virSecurityLabelDefPtr secdef =
        virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

    if (!secdef || !seclabel)
        return -1;

    if (secdef->label)
1141 1142
        ignore_value(virStrcpy(seclabel->label, secdef->label,
                               VIR_SECURITY_LABEL_BUFLEN));
1143

1144 1145 1146 1147
    return 0;
}

static int
1148
virSecurityDACSetDaemonSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1149
                                   virDomainDefPtr vm ATTRIBUTE_UNUSED)
1150 1151 1152 1153 1154
{
    return 0;
}


1155 1156
static int
virSecurityDACSetSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1157
                             virDomainDefPtr def ATTRIBUTE_UNUSED)
1158 1159 1160 1161 1162
{
    return 0;
}


1163 1164
static int
virSecurityDACClearSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1165
                               virDomainDefPtr def ATTRIBUTE_UNUSED)
1166 1167 1168 1169
{
    return 0;
}

1170
static int
1171
virSecurityDACSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1172
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
1173
                              int fd ATTRIBUTE_UNUSED)
1174 1175 1176 1177
{
    return 0;
}

1178 1179 1180 1181 1182 1183 1184 1185
static int
virSecurityDACSetTapFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                            virDomainDefPtr def ATTRIBUTE_UNUSED,
                            int fd ATTRIBUTE_UNUSED)
{
    return 0;
}

1186 1187 1188 1189 1190
static char *virSecurityDACGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                           virDomainDefPtr vm ATTRIBUTE_UNUSED) {
    return NULL;
}

1191
virSecurityDriver virSecurityDriverDAC = {
1192
    .privateDataLen                     = sizeof(virSecurityDACData),
1193
    .name                               = SECURITY_DAC_NAME,
1194 1195 1196
    .probe                              = virSecurityDACProbe,
    .open                               = virSecurityDACOpen,
    .close                              = virSecurityDACClose,
1197

1198 1199
    .getModel                           = virSecurityDACGetModel,
    .getDOI                             = virSecurityDACGetDOI,
1200

1201
    .domainSecurityVerify               = virSecurityDACVerify,
1202

1203 1204
    .domainSetSecurityImageLabel        = virSecurityDACSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityDACRestoreSecurityImageLabel,
1205

1206 1207 1208
    .domainSetSecurityDaemonSocketLabel = virSecurityDACSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityDACSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityDACClearSocketLabel,
1209

1210 1211 1212
    .domainGenSecurityLabel             = virSecurityDACGenLabel,
    .domainReserveSecurityLabel         = virSecurityDACReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityDACReleaseLabel,
1213

1214 1215
    .domainGetSecurityProcessLabel      = virSecurityDACGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityDACSetProcessLabel,
1216
    .domainSetSecurityChildProcessLabel = virSecurityDACSetChildProcessLabel,
1217

1218 1219
    .domainSetSecurityAllLabel          = virSecurityDACSetSecurityAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityDACRestoreSecurityAllLabel,
1220

1221 1222
    .domainSetSecurityHostdevLabel      = virSecurityDACSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityDACRestoreSecurityHostdevLabel,
1223

1224 1225
    .domainSetSavedStateLabel           = virSecurityDACSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityDACRestoreSavedStateLabel,
1226

1227
    .domainSetSecurityImageFDLabel      = virSecurityDACSetImageFDLabel,
1228
    .domainSetSecurityTapFDLabel        = virSecurityDACSetTapFDLabel,
1229

1230
    .domainGetSecurityMountOptions      = virSecurityDACGetMountOptions,
1231
};