security_dac.c 29.7 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2012 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
O
Osier Yang 已提交
15 16
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * POSIX DAC security driver
 */

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

#include "security_dac.h"
#include "virterror_internal.h"
#include "util.h"
#include "memory.h"
#include "logging.h"
#include "pci.h"
#include "hostusb.h"
#include "storage_file.h"

#define VIR_FROM_THIS VIR_FROM_SECURITY
36
#define SECURITY_DAC_NAME "dac"
37 38 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

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

68 69 70
static
int parseIds(const char *label, uid_t *uidPtr, gid_t *gidPtr)
{
71 72
    unsigned int theuid;
    unsigned int thegid;
73 74 75 76 77
    char *endptr = NULL;

    if (label == NULL)
        return -1;

78
    if (virStrToLong_ui(label, &endptr, 10, &theuid) ||
79 80 81 82
        endptr == NULL || *endptr != ':') {
        return -1;
    }

83
    if (virStrToLong_ui(endptr + 1, NULL, 10, &thegid))
84 85 86
        return -1;

    if (uidPtr)
87
        *uidPtr = theuid;
88
    if (gidPtr)
89
        *gidPtr = thegid;
90 91 92
    return 0;
}

93
/* returns 1 if label isn't found, 0 on success, -1 on error */
94 95 96 97 98 99 100 101
static
int virSecurityDACParseIds(virDomainDefPtr def, uid_t *uidPtr, gid_t *gidPtr)
{
    uid_t uid;
    gid_t gid;
    virSecurityLabelDefPtr seclabel;

    if (def == NULL)
102
        return 1;
103 104

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
105
    if (seclabel == NULL || seclabel->label == NULL) {
106 107
        VIR_DEBUG("DAC seclabel for domain '%s' wasn't found", def->name);
        return 1;
108 109 110
    }

    if (seclabel->label && parseIds(seclabel->label, &uid, &gid)) {
111 112 113
       virReportError(VIR_ERR_INVALID_ARG,
                       _("failed to parse DAC seclabel '%s' for domain '%s'"),
                       seclabel->label, def->name);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        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)
{
129 130 131 132 133 134 135
    int ret;

    if (!def && !priv) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to determine default DAC seclabel "
                         "for an unknown object"));
        return -1;
136
    }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    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;
154 155
}

156 157

/* returns 1 if label isn't found, 0 on success, -1 on error */
158 159 160 161 162 163 164 165 166
static
int virSecurityDACParseImageIds(virDomainDefPtr def,
                                uid_t *uidPtr, gid_t *gidPtr)
{
    uid_t uid;
    gid_t gid;
    virSecurityLabelDefPtr seclabel;

    if (def == NULL)
167
        return 1;
168 169

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
170
    if (seclabel == NULL || seclabel->imagelabel == NULL) {
171 172
        VIR_DEBUG("DAC imagelabel for domain '%s' wasn't found", def->name);
        return 1;
173 174 175 176
    }

    if (seclabel->imagelabel
        && parseIds(seclabel->imagelabel, &uid, &gid)) {
177 178 179
       virReportError(VIR_ERR_INVALID_ARG,
                       _("failed to parse DAC imagelabel '%s' for domain '%s'"),
                       seclabel->imagelabel, def->name);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        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)
{
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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;
212
    }
213 214 215 216 217 218 219

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

    return 0;
220 221 222
}


223
static virSecurityDriverStatus
224
virSecurityDACProbe(const char *virtDriver ATTRIBUTE_UNUSED)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
{
    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)
{
244
    return SECURITY_DAC_NAME;
245 246 247 248 249 250 251 252
}

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

static int
253
virSecurityDACSetOwnership(const char *path, uid_t uid, gid_t gid)
254
{
255 256
    VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'",
             path, (long) uid, (long) gid);
257 258 259 260 261 262 263 264 265 266 267 268 269

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

270
        if (chown_errno == EOPNOTSUPP || chown_errno == EINVAL) {
271 272 273
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "supported by filesystem",
                     (long) uid, (long) gid, path);
274
        } else if (chown_errno == EPERM) {
275 276 277
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "permitted",
                     (long) uid, (long) gid, path);
278
        } else if (chown_errno == EROFS) {
279 280 281
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "possible on readonly filesystem",
                     (long) uid, (long) gid, path);
282 283
        } else {
            virReportSystemError(chown_errno,
284 285 286
                                 _("unable to set user and group to '%ld:%ld' "
                                   "on '%s'"),
                                 (long) uid, (long) gid, path);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 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
            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)
{
326 327 328
    void **params = opaque;
    virSecurityManagerPtr mgr = params[0];
    virDomainDefPtr def = params[1];
329
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
330 331 332 333 334
    uid_t user;
    gid_t group;

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

336
    return virSecurityDACSetOwnership(path, user, group);
337 338 339 340 341
}


static int
virSecurityDACSetSecurityImageLabel(virSecurityManagerPtr mgr,
342
                                    virDomainDefPtr def ATTRIBUTE_UNUSED,
343 344 345
                                    virDomainDiskDefPtr disk)

{
346 347 348
    uid_t user;
    gid_t group;
    void *params[2];
349 350 351 352 353
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

354 355 356
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

357 358 359 360 361
    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;

    params[0] = mgr;
    params[1] = def;
362 363 364
    return virDomainDiskDefForeachPath(disk,
                                       virSecurityManagerGetAllowDiskFormatProbing(mgr),
                                       false,
365
                                       user, group,
366
                                       virSecurityDACSetSecurityFileLabel,
367
                                       params);
368 369 370 371 372
}


static int
virSecurityDACRestoreSecurityImageLabelInt(virSecurityManagerPtr mgr,
373
                                           virDomainDefPtr def ATTRIBUTE_UNUSED,
374 375 376 377 378 379 380 381
                                           virDomainDiskDefPtr disk,
                                           int migrated)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

382 383 384
    if (disk->type == VIR_DOMAIN_DISK_TYPE_NETWORK)
        return 0;

385 386 387 388 389 390 391 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
    /* 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,
421
                                        virDomainDefPtr def,
422 423
                                        virDomainDiskDefPtr disk)
{
424
    return virSecurityDACRestoreSecurityImageLabelInt(mgr, def, disk, 0);
425 426 427 428 429 430 431 432
}


static int
virSecurityDACSetSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
                                  const char *file,
                                  void *opaque)
{
433 434 435
    void **params = opaque;
    virSecurityManagerPtr mgr = params[0];
    virDomainDefPtr def = params[1];
436
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
437 438
    uid_t user;
    gid_t group;
439

440 441 442 443
    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

    return virSecurityDACSetOwnership(file, user, group);
444 445 446 447 448 449 450 451
}


static int
virSecurityDACSetSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
                                  const char *file,
                                  void *opaque)
{
452 453 454
    void **params = opaque;
    virSecurityManagerPtr mgr = params[0];
    virDomainDefPtr def = params[1];
455
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
456 457
    uid_t user;
    gid_t group;
458

459 460 461 462
    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

    return virSecurityDACSetOwnership(file, user, group);
463 464 465 466 467
}


static int
virSecurityDACSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
468
                                      virDomainDefPtr def,
469 470
                                      virDomainHostdevDefPtr dev)
{
471
    void *params[] = {mgr, def};
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    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: {
        usbDevice *usb = usbGetDevice(dev->source.subsys.u.usb.bus,
                                      dev->source.subsys.u.usb.device);

        if (!usb)
            goto done;

489 490
        ret = usbDeviceFileIterate(usb, virSecurityDACSetSecurityUSBLabel,
                                   params);
491 492 493 494 495 496 497 498 499 500 501 502 503
        usbFreeDevice(usb);
        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

504 505
        ret = pciDeviceFileIterate(pci, virSecurityDACSetSecurityPCILabel,
                                   params);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
        pciFreeDevice(pci);

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}


static int
virSecurityDACRestoreSecurityPCILabel(pciDevice *dev ATTRIBUTE_UNUSED,
                                      const char *file,
                                      void *opaque ATTRIBUTE_UNUSED)
{
    return virSecurityDACRestoreSecurityFileLabel(file);
}


static int
virSecurityDACRestoreSecurityUSBLabel(usbDevice *dev ATTRIBUTE_UNUSED,
                                       const char *file,
                                       void *opaque ATTRIBUTE_UNUSED)
{
    return virSecurityDACRestoreSecurityFileLabel(file);
}


static int
virSecurityDACRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
541
                                           virDomainDefPtr def ATTRIBUTE_UNUSED,
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                                           virDomainHostdevDefPtr dev)

{
    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: {
        usbDevice *usb = usbGetDevice(dev->source.subsys.u.usb.bus,
                                      dev->source.subsys.u.usb.device);

        if (!usb)
            goto done;

        ret = usbDeviceFileIterate(usb, virSecurityDACRestoreSecurityUSBLabel, mgr);
        usbFreeDevice(usb);

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
        pciDevice *pci = pciGetDevice(dev->source.subsys.u.pci.domain,
                                      dev->source.subsys.u.pci.bus,
                                      dev->source.subsys.u.pci.slot,
                                      dev->source.subsys.u.pci.function);

        if (!pci)
            goto done;

        ret = pciDeviceFileIterate(pci, virSecurityDACRestoreSecurityPCILabel, mgr);
        pciFreeDevice(pci);

        break;
    }

    default:
        ret = 0;
        break;
    }

done:
    return ret;
}


static int
virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
595
                              virDomainDefPtr def,
596
                              virDomainChrSourceDefPtr dev)
597 598 599 600 601

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    char *in = NULL, *out = NULL;
    int ret = -1;
602 603 604 605 606
    uid_t user;
    gid_t group;

    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;
607 608 609 610

    switch (dev->type) {
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
611
        ret = virSecurityDACSetOwnership(dev->data.file.path, user, group);
612 613 614
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
615 616 617 618 619 620
        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)) {
621 622
            if ((virSecurityDACSetOwnership(in, user, group) < 0) ||
                (virSecurityDACSetOwnership(out, user, group) < 0)) {
623
                goto done;
624 625
            }
        } else if (virSecurityDACSetOwnership(dev->data.file.path,
626
                                              user, group) < 0) {
627
            goto done;
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        }
        ret = 0;
        break;

    default:
        ret = 0;
        break;
    }

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

static int
virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
645
                                  virDomainChrSourceDefPtr dev)
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
{
    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;
        }
662 663 664
        if (virFileExists(in) && virFileExists(out)) {
            if ((virSecurityDACRestoreSecurityFileLabel(out) < 0) ||
                (virSecurityDACRestoreSecurityFileLabel(in) < 0)) {
665
            goto done;
666 667 668 669
            }
        } else if (virSecurityDACRestoreSecurityFileLabel(dev->data.file.path) < 0) {
            goto done;
        }
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
        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;

692
    return virSecurityDACRestoreChardevLabel(mgr, &dev->source);
693 694 695 696 697
}


static int
virSecurityDACRestoreSecurityAllLabel(virSecurityManagerPtr mgr,
698
                                      virDomainDefPtr def,
699 700 701 702 703 704 705 706 707 708 709
                                      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",
710
              def->name, migrated);
711

712
    for (i = 0 ; i < def->nhostdevs ; i++) {
713
        if (virSecurityDACRestoreSecurityHostdevLabel(mgr,
714 715
                                                      def,
                                                      def->hostdevs[i]) < 0)
716 717
            rc = -1;
    }
718
    for (i = 0 ; i < def->ndisks ; i++) {
719
        if (virSecurityDACRestoreSecurityImageLabelInt(mgr,
720 721
                                                       def,
                                                       def->disks[i],
722 723 724 725
                                                       migrated) < 0)
            rc = -1;
    }

726
    if (virDomainChrDefForeach(def,
727 728
                               false,
                               virSecurityDACRestoreChardevCallback,
729
                               mgr) < 0)
730 731
        rc = -1;

732 733
    if (def->os.kernel &&
        virSecurityDACRestoreSecurityFileLabel(def->os.kernel) < 0)
734 735
        rc = -1;

736 737
    if (def->os.initrd &&
        virSecurityDACRestoreSecurityFileLabel(def->os.initrd) < 0)
738 739 740 741 742 743 744 745 746 747 748 749 750
        rc = -1;

    return rc;
}


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

751
    return virSecurityDACSetChardevLabel(mgr, def, &dev->source);
752 753 754 755 756
}


static int
virSecurityDACSetSecurityAllLabel(virSecurityManagerPtr mgr,
757
                                  virDomainDefPtr def,
758 759 760 761
                                  const char *stdin_path ATTRIBUTE_UNUSED)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int i;
762 763
    uid_t user;
    gid_t group;
764 765 766 767

    if (!priv->dynamicOwnership)
        return 0;

768
    for (i = 0 ; i < def->ndisks ; i++) {
769
        /* XXX fixme - we need to recursively label the entire tree :-( */
770
        if (def->disks[i]->type == VIR_DOMAIN_DISK_TYPE_DIR)
771 772
            continue;
        if (virSecurityDACSetSecurityImageLabel(mgr,
773 774
                                                def,
                                                def->disks[i]) < 0)
775 776
            return -1;
    }
777
    for (i = 0 ; i < def->nhostdevs ; i++) {
778
        if (virSecurityDACSetSecurityHostdevLabel(mgr,
779 780
                                                  def,
                                                  def->hostdevs[i]) < 0)
781 782 783
            return -1;
    }

784
    if (virDomainChrDefForeach(def,
785 786
                               true,
                               virSecurityDACSetChardevCallback,
787
                               mgr) < 0)
788 789
        return -1;

790 791 792
    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;

793
    if (def->os.kernel &&
794
        virSecurityDACSetOwnership(def->os.kernel, user, group) < 0)
795 796
        return -1;

797
    if (def->os.initrd &&
798
        virSecurityDACSetOwnership(def->os.initrd, user, group) < 0)
799 800 801 802 803 804 805 806
        return -1;

    return 0;
}


static int
virSecurityDACSetSavedStateLabel(virSecurityManagerPtr mgr,
807
                                 virDomainDefPtr def,
808 809
                                 const char *savefile)
{
810 811
    uid_t user;
    gid_t group;
812 813
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

814 815 816 817
    if (virSecurityDACGetImageIds(def, priv, &user, &group))
        return -1;

    return virSecurityDACSetOwnership(savefile, user, group);
818 819 820 821 822
}


static int
virSecurityDACRestoreSavedStateLabel(virSecurityManagerPtr mgr,
823
                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
824 825 826 827 828 829 830 831 832 833 834 835 836
                                     const char *savefile)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

    return virSecurityDACRestoreSecurityFileLabel(savefile);
}


static int
virSecurityDACSetProcessLabel(virSecurityManagerPtr mgr,
837
                              virDomainDefPtr def ATTRIBUTE_UNUSED)
838
{
839 840
    uid_t user;
    gid_t group;
841 842
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

843 844 845 846
    if (virSecurityDACGetIds(def, priv, &user, &group))
        return -1;

    VIR_DEBUG("Dropping privileges of DEF to %u:%u", user, group);
847

848
    if (virSetUIDGID(user, group) < 0)
849 850 851 852 853 854 855 856 857 858 859 860 861 862
        return -1;

    return 0;
}


static int
virSecurityDACVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
863 864
virSecurityDACGenLabel(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
865
{
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    int rc = -1;
    virSecurityLabelDefPtr seclabel;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (mgr == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("invalid security driver"));
        return rc;
    }

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

    switch(seclabel->type) {
    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:
        if (virAsprintf(&seclabel->label, "%d:%d", priv->user, priv->group) < 0) {
            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 */
920
        return 0;
921 922 923 924 925 926 927 928
    default:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected security label type '%s'"),
                       virDomainSeclabelTypeToString(seclabel->type));
        return rc;
    }

    if (!seclabel->norelabel) {
929
        if (seclabel->imagelabel == NULL && seclabel->label != NULL) {
930 931 932 933 934 935 936 937 938 939 940 941
            seclabel->imagelabel = strdup(seclabel->label);
            if (seclabel->imagelabel == NULL) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("cannot generate dac user and group id "
                                 "for domain %s"), def->name);
                VIR_FREE(seclabel->label);
                seclabel->label = NULL;
                return rc;
            }
        }
    }

942 943 944 945 946
    return 0;
}

static int
virSecurityDACReleaseLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
947
                           virDomainDefPtr def ATTRIBUTE_UNUSED)
948 949 950 951 952 953
{
    return 0;
}

static int
virSecurityDACReserveLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
954 955
                           virDomainDefPtr def ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED)
956 957 958 959 960 961
{
    return 0;
}

static int
virSecurityDACGetProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
962 963
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
                              pid_t pid ATTRIBUTE_UNUSED,
964 965
                              virSecurityLabelPtr seclabel ATTRIBUTE_UNUSED)
{
966 967 968 969 970 971 972 973 974
    virSecurityLabelDefPtr secdef =
        virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

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

    if (secdef->label)
        strcpy(seclabel->label, secdef->label);

975 976 977 978
    return 0;
}

static int
979
virSecurityDACSetDaemonSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
980
                                   virDomainDefPtr vm ATTRIBUTE_UNUSED)
981 982 983 984 985
{
    return 0;
}


986 987
static int
virSecurityDACSetSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
988
                             virDomainDefPtr def ATTRIBUTE_UNUSED)
989 990 991 992 993
{
    return 0;
}


994 995
static int
virSecurityDACClearSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
996
                               virDomainDefPtr def ATTRIBUTE_UNUSED)
997 998 999 1000
{
    return 0;
}

1001
static int
1002
virSecurityDACSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1003
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
1004
                              int fd ATTRIBUTE_UNUSED)
1005 1006 1007 1008
{
    return 0;
}

1009 1010 1011 1012 1013
static char *virSecurityDACGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                           virDomainDefPtr vm ATTRIBUTE_UNUSED) {
    return NULL;
}

1014
virSecurityDriver virSecurityDriverDAC = {
1015
    .privateDataLen                     = sizeof(virSecurityDACData),
1016
    .name                               = SECURITY_DAC_NAME,
1017 1018 1019
    .probe                              = virSecurityDACProbe,
    .open                               = virSecurityDACOpen,
    .close                              = virSecurityDACClose,
1020

1021 1022
    .getModel                           = virSecurityDACGetModel,
    .getDOI                             = virSecurityDACGetDOI,
1023

1024
    .domainSecurityVerify               = virSecurityDACVerify,
1025

1026 1027
    .domainSetSecurityImageLabel        = virSecurityDACSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityDACRestoreSecurityImageLabel,
1028

1029 1030 1031
    .domainSetSecurityDaemonSocketLabel = virSecurityDACSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityDACSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityDACClearSocketLabel,
1032

1033 1034 1035
    .domainGenSecurityLabel             = virSecurityDACGenLabel,
    .domainReserveSecurityLabel         = virSecurityDACReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityDACReleaseLabel,
1036

1037 1038
    .domainGetSecurityProcessLabel      = virSecurityDACGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityDACSetProcessLabel,
1039

1040 1041
    .domainSetSecurityAllLabel          = virSecurityDACSetSecurityAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityDACRestoreSecurityAllLabel,
1042

1043 1044
    .domainSetSecurityHostdevLabel      = virSecurityDACSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityDACRestoreSecurityHostdevLabel,
1045

1046 1047
    .domainSetSavedStateLabel           = virSecurityDACSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityDACRestoreSavedStateLabel,
1048

1049
    .domainSetSecurityImageFDLabel      = virSecurityDACSetImageFDLabel,
1050

1051
    .domainGetSecurityMountOptions      = virSecurityDACGetMountOptions,
1052
};