security_dac.c 47.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2014 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
 *
 * POSIX DAC security driver
 */

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

26 27 28 29 30
#ifdef  __FreeBSD__
# include <sys/sysctl.h>
# include <sys/user.h>
#endif

31
#include "security_dac.h"
32
#include "virerror.h"
33
#include "virfile.h"
34
#include "viralloc.h"
35
#include "virlog.h"
36
#include "virpci.h"
37
#include "virusb.h"
38
#include "virscsi.h"
39
#include "virstoragefile.h"
40
#include "virstring.h"
M
Martin Kletzander 已提交
41
#include "virutil.h"
42 43

#define VIR_FROM_THIS VIR_FROM_SECURITY
44 45 46

VIR_LOG_INIT("security.security_dac");

47
#define SECURITY_DAC_NAME "dac"
48 49 50 51 52 53 54

typedef struct _virSecurityDACData virSecurityDACData;
typedef virSecurityDACData *virSecurityDACDataPtr;

struct _virSecurityDACData {
    uid_t user;
    gid_t group;
55 56
    gid_t *groups;
    int ngroups;
57
    bool dynamicOwnership;
58
    char *baselabel;
59
    virSecurityManagerDACChownCallback chownCallback;
60 61
};

62 63 64 65 66 67 68 69
typedef struct _virSecurityDACCallbackData virSecurityDACCallbackData;
typedef virSecurityDACCallbackData *virSecurityDACCallbackDataPtr;

struct _virSecurityDACCallbackData {
    virSecurityManagerPtr manager;
    virSecurityLabelDefPtr secdef;
};

70 71 72 73 74
/* returns -1 on error, 0 on success */
int
virSecurityDACSetUserAndGroup(virSecurityManagerPtr mgr,
                              uid_t user,
                              gid_t group)
75 76 77 78
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->user = user;
    priv->group = group;
79

80
    if (virAsprintf(&priv->baselabel, "+%u:+%u",
81 82 83 84 85
                    (unsigned int) user,
                    (unsigned int) group) < 0)
        return -1;

    return 0;
86 87
}

O
Osier Yang 已提交
88 89 90
void
virSecurityDACSetDynamicOwnership(virSecurityManagerPtr mgr,
                                  bool dynamicOwnership)
91 92 93 94 95
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->dynamicOwnership = dynamicOwnership;
}

96 97 98 99 100 101 102 103
void
virSecurityDACSetChownCallback(virSecurityManagerPtr mgr,
                               virSecurityManagerDACChownCallback chownCallback)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->chownCallback = chownCallback;
}

104
/* returns 1 if label isn't found, 0 on success, -1 on error */
O
Osier Yang 已提交
105
static int
106
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
107 108
virSecurityDACParseIds(virSecurityLabelDefPtr seclabel,
                       uid_t *uidPtr, gid_t *gidPtr)
109
{
110
    if (!seclabel || !seclabel->label)
111
        return 1;
112

113
    if (virParseOwnershipIds(seclabel->label, uidPtr, gidPtr) < 0)
114 115 116 117 118
        return -1;

    return 0;
}

O
Osier Yang 已提交
119
static int
120
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
121 122
virSecurityDACGetIds(virSecurityLabelDefPtr seclabel,
                     virSecurityDACDataPtr priv,
123 124
                     uid_t *uidPtr, gid_t *gidPtr,
                     gid_t **groups, int *ngroups)
125
{
126 127
    int ret;

128 129 130 131 132
    if (groups)
        *groups = priv ? priv->groups : NULL;
    if (ngroups)
        *ngroups = priv ? priv->ngroups : 0;

133
    if ((ret = virSecurityDACParseIds(seclabel, uidPtr, gidPtr)) <= 0)
134 135 136
        return ret;

    if (!priv) {
137 138
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("DAC seclabel couldn't be determined"));
139 140 141
        return -1;
    }

142 143
    *uidPtr = priv->user;
    *gidPtr = priv->group;
144 145

    return 0;
146 147
}

148 149

/* returns 1 if label isn't found, 0 on success, -1 on error */
O
Osier Yang 已提交
150
static int
151
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
152
virSecurityDACParseImageIds(virSecurityLabelDefPtr seclabel,
O
Osier Yang 已提交
153
                            uid_t *uidPtr, gid_t *gidPtr)
154
{
155
    if (!seclabel || !seclabel->imagelabel)
156
        return 1;
157

158
    if (virParseOwnershipIds(seclabel->imagelabel, uidPtr, gidPtr) < 0)
159 160 161 162 163
        return -1;

    return 0;
}

O
Osier Yang 已提交
164
static int
165
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
166 167
virSecurityDACGetImageIds(virSecurityLabelDefPtr seclabel,
                          virSecurityDACDataPtr priv,
O
Osier Yang 已提交
168
                          uid_t *uidPtr, gid_t *gidPtr)
169
{
170 171
    int ret;

172
    if ((ret = virSecurityDACParseImageIds(seclabel, uidPtr, gidPtr)) <= 0)
173 174 175
        return ret;

    if (!priv) {
176 177
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("DAC imagelabel couldn't be determined"));
178
        return -1;
179
    }
180

181 182
    *uidPtr = priv->user;
    *gidPtr = priv->group;
183 184

    return 0;
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/**
 * virSecurityDACRememberLabel:
 * @priv: driver's private data
 * @path: path to the file
 * @uid: user owning the @path
 * @gid: group owning the @path
 *
 * Remember the owner of @path (represented by @uid:@gid).
 *
 * Returns: 0 on success, -1 on failure
 */
static int
virSecurityDACRememberLabel(virSecurityDACDataPtr priv ATTRIBUTE_UNUSED,
                            const char *path ATTRIBUTE_UNUSED,
                            uid_t uid ATTRIBUTE_UNUSED,
                            gid_t gid ATTRIBUTE_UNUSED)
{
    return 0;
}

/**
 * virSecurityDACRecallLabel:
 * @priv: driver's private data
 * @path: path to the file
 * @uid: user owning the @path
 * @gid: group owning the @path
 *
 * Recall the previously recorded owner for the @path. However, it may happen
 * that @path is still in use (e.g. by another domain). In that case, 1 is
 * returned and caller should not relabel the @path.
 *
 * Returns: 1 if @path is still in use (@uid and @gid not touched)
 *          0 if @path should be restored (@uid and @gid set)
 *         -1 on failure (@uid and @gid not touched)
 */
static int
virSecurityDACRecallLabel(virSecurityDACDataPtr priv ATTRIBUTE_UNUSED,
                          const char *path ATTRIBUTE_UNUSED,
                          uid_t *uid ATTRIBUTE_UNUSED,
                          gid_t *gid ATTRIBUTE_UNUSED)
{
    return 0;
}
230

231
static virSecurityDriverStatus
232
virSecurityDACProbe(const char *virtDriver ATTRIBUTE_UNUSED)
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
244
virSecurityDACClose(virSecurityManagerPtr mgr)
245
{
246 247
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    VIR_FREE(priv->groups);
248
    VIR_FREE(priv->baselabel);
249 250 251 252
    return 0;
}


O
Osier Yang 已提交
253 254
static const char *
virSecurityDACGetModel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
255
{
256
    return SECURITY_DAC_NAME;
257 258
}

O
Osier Yang 已提交
259 260
static const char *
virSecurityDACGetDOI(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
261 262 263 264
{
    return "0";
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static int
virSecurityDACPreFork(virSecurityManagerPtr mgr)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int ngroups;

    VIR_FREE(priv->groups);
    priv->ngroups = 0;
    if ((ngroups = virGetGroupList(priv->user, priv->group,
                                   &priv->groups)) < 0)
        return -1;
    priv->ngroups = ngroups;
    return 0;
}

280
static int
281 282 283 284 285
virSecurityDACSetOwnershipInternal(virSecurityDACDataPtr priv,
                                   virStorageSourcePtr src,
                                   const char *path,
                                   uid_t uid,
                                   gid_t gid)
286
{
287 288
    int rc;

289
    VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'",
290 291 292 293 294 295 296 297 298 299 300
             NULLSTR(src ? src->path : path), (long) uid, (long) gid);

    if (priv && src && priv->chownCallback) {
        rc = priv->chownCallback(src, uid, gid);
        /* here path is used only for error messages */
        path = NULLSTR(src->path);

        /* on -2 returned an error was already reported */
        if (rc == -2)
            return -1;
    } else {
301 302
        struct stat sb;

303 304 305 306 307 308 309 310 311 312
        if (!path) {
            if (!src || !src->path)
                return 0;

            if (!virStorageSourceIsLocalStorage(src))
                return 0;

            path = src->path;
        }

313 314 315 316
        if (stat(path, &sb) < 0) {
            virReportSystemError(errno, _("unable to stat: %s"), path);
            return -1;
        }
317

318 319 320
        if (sb.st_uid == uid && sb.st_gid == gid) {
            /* nothing to chown */
            return 0;
321
        }
322 323

        rc = chown(path, uid, gid);
324
    }
325

326
    if (rc < 0) {
327
        if (errno == EOPNOTSUPP || errno == EINVAL) {
328 329 330
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "supported by filesystem",
                     (long) uid, (long) gid, path);
331
        } else if (errno == EPERM) {
332 333 334
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "permitted",
                     (long) uid, (long) gid, path);
335
        } else if (errno == EROFS) {
336 337 338
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "possible on readonly filesystem",
                     (long) uid, (long) gid, path);
339
        } else {
340
            virReportSystemError(errno,
341 342 343
                                 _("unable to set user and group to '%ld:%ld' "
                                   "on '%s'"),
                                 (long) uid, (long) gid, path);
344 345 346 347 348 349
            return -1;
        }
    }
    return 0;
}

350

351
static int
352
virSecurityDACSetOwnership(virSecurityDACDataPtr priv,
353
                           virStorageSourcePtr src,
354 355 356
                           const char *path,
                           uid_t uid,
                           gid_t gid)
357
{
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    struct stat sb;

    if (!path && src && src->path &&
        virStorageSourceIsLocalStorage(src))
        path = src->path;

    if (path) {
        if (stat(path, &sb) < 0) {
            virReportSystemError(errno, _("unable to stat: %s"), path);
            return -1;
        }

        if (virSecurityDACRememberLabel(priv, path, sb.st_uid, sb.st_gid) < 0)
            return -1;
    }

374
    return virSecurityDACSetOwnershipInternal(priv, src, path, uid, gid);
375 376 377 378
}


static int
379 380 381
virSecurityDACRestoreFileLabelInternal(virSecurityDACDataPtr priv,
                                       virStorageSourcePtr src,
                                       const char *path)
382
{
383 384 385 386
    int rv;
    uid_t uid = 0;  /* By default return to root:root */
    gid_t gid = 0;

387 388
    VIR_INFO("Restoring DAC user and group on '%s'",
             NULLSTR(src ? src->path : path));
389

390 391 392 393 394 395 396 397 398 399 400 401 402
    if (!path && src && src->path &&
        virStorageSourceIsLocalStorage(src))
        path = src->path;

    if (path) {
        rv = virSecurityDACRecallLabel(priv, path, &uid, &gid);
        if (rv < 0)
            return -1;
        if (rv > 0)
            return 0;
    }

    return virSecurityDACSetOwnershipInternal(priv, src, path, uid, gid);
403 404 405 406
}


static int
407 408
virSecurityDACRestoreFileLabel(virSecurityDACDataPtr priv,
                               const char *path)
409
{
410
    return virSecurityDACRestoreFileLabelInternal(priv, NULL, path);
411 412 413 414
}


static int
415 416 417
virSecurityDACSetImageLabel(virSecurityManagerPtr mgr,
                            virDomainDefPtr def,
                            virStorageSourcePtr src)
418
{
419
    virSecurityLabelDefPtr secdef;
420
    virSecurityDeviceLabelDefPtr disk_seclabel;
421
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
422 423 424
    uid_t user;
    gid_t group;

425 426 427 428
    if (!priv->dynamicOwnership)
        return 0;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
429
    if (secdef && !secdef->relabel)
430
        return 0;
431

432 433
    disk_seclabel = virStorageSourceGetSecurityLabelDef(src,
                                                        SECURITY_DAC_NAME);
434
    if (disk_seclabel && !disk_seclabel->relabel)
435 436 437 438 439 440 441 442 443
        return 0;

    if (disk_seclabel && disk_seclabel->label) {
        if (virParseOwnershipIds(disk_seclabel->label, &user, &group) < 0)
            return -1;
    } else {
        if (virSecurityDACGetImageIds(secdef, priv, &user, &group))
            return -1;
    }
444

445
    return virSecurityDACSetOwnership(priv, src, NULL, user, group);
446 447 448 449
}


static int
450 451 452
virSecurityDACSetDiskLabel(virSecurityManagerPtr mgr,
                           virDomainDefPtr def,
                           virDomainDiskDefPtr disk)
453 454

{
455
    virStorageSourcePtr next;
456

457
    for (next = disk->src; next; next = next->backingStore) {
458
        if (virSecurityDACSetImageLabel(mgr, def, next) < 0)
459 460
            return -1;
    }
461

462
    return 0;
463 464 465 466
}


static int
467 468 469 470
virSecurityDACRestoreImageLabelInt(virSecurityManagerPtr mgr,
                                   virDomainDefPtr def,
                                   virStorageSourcePtr src,
                                   bool migrated)
471 472
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
473 474
    virSecurityLabelDefPtr secdef;
    virSecurityDeviceLabelDefPtr disk_seclabel;
475 476 477 478

    if (!priv->dynamicOwnership)
        return 0;

479 480 481 482 483 484 485
    /* 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 (src->readonly || src->shared)
        return 0;
486

487
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
488
    if (secdef && !secdef->relabel)
489 490
        return 0;

491
    disk_seclabel = virStorageSourceGetSecurityLabelDef(src,
492
                                                        SECURITY_DAC_NAME);
493
    if (disk_seclabel && !disk_seclabel->relabel)
494 495
        return 0;

496 497 498
    /* If we have a shared FS and are doing migration, 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 :-) */
499
    if (migrated) {
500 501 502 503 504 505 506 507 508 509
        int rc = 1;

        if (virStorageSourceIsLocalStorage(src)) {
            if (!src->path)
                return 0;

            if ((rc = virFileIsSharedFS(src->path)) < 0)
                return -1;
        }

510 511
        if (rc == 1) {
            VIR_DEBUG("Skipping image label restore on %s because FS is shared",
512
                      src->path);
513 514 515 516
            return 0;
        }
    }

517
    return virSecurityDACRestoreFileLabelInternal(priv, src, NULL);
518 519 520 521
}


static int
522 523 524
virSecurityDACRestoreImageLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr def,
                                virStorageSourcePtr src)
525
{
526
    return virSecurityDACRestoreImageLabelInt(mgr, def, src, false);
527 528 529 530
}


static int
531 532 533
virSecurityDACRestoreDiskLabel(virSecurityManagerPtr mgr,
                               virDomainDefPtr def,
                               virDomainDiskDefPtr disk)
534
{
535
    return virSecurityDACRestoreImageLabelInt(mgr, def, disk->src, false);
536 537 538 539
}


static int
540 541
virSecurityDACSetHostdevLabelHelper(const char *file,
                                    void *opaque)
542
{
543 544 545
    virSecurityDACCallbackDataPtr cbdata = opaque;
    virSecurityManagerPtr mgr = cbdata->manager;
    virSecurityLabelDefPtr secdef = cbdata->secdef;
546
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
547 548
    uid_t user;
    gid_t group;
549

550
    if (virSecurityDACGetIds(secdef, priv, &user, &group, NULL, NULL) < 0)
551 552
        return -1;

553
    return virSecurityDACSetOwnership(priv, NULL, file, user, group);
554 555 556
}


557
static int
558 559 560
virSecurityDACSetPCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
                          const char *file,
                          void *opaque)
561
{
562
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
563 564 565
}


566
static int
567 568 569
virSecurityDACSetUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                          const char *file,
                          void *opaque)
570
{
571
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
572
}
573

574

575
static int
576 577 578
virSecurityDACSetSCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                           const char *file,
                           void *opaque)
579
{
580
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
581 582 583 584
}


static int
585 586 587 588
virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              virDomainHostdevDefPtr dev,
                              const char *vroot)
589 590
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
591
    virSecurityDACCallbackData cbdata;
592
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
593
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
594
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
595 596 597 598 599 600 601 602
    int ret = -1;

    if (!priv->dynamicOwnership)
        return 0;

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

603
    /* Like virSecurityDACSetImageLabel() for a networked disk,
604 605
     * do nothing for an iSCSI hostdev
     */
606 607
    if (dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
        scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
608 609
        return 0;

610 611 612
    cbdata.manager = mgr;
    cbdata.secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

613
    if (cbdata.secdef && !cbdata.secdef->relabel)
614 615
        return 0;

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

620 621 622
        if (dev->missing)
            return 0;

623
        if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot)))
624 625
            goto done;

626
        ret = virUSBDeviceFileIterate(usb,
627
                                      virSecurityDACSetUSBLabel,
628
                                      &cbdata);
629
        virUSBDeviceFree(usb);
630 631 632 633
        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
634
        virPCIDevicePtr pci =
635 636
            virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
                            pcisrc->addr.slot, pcisrc->addr.function);
637 638 639 640

        if (!pci)
            goto done;

641
        if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
642
            char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
643

644 645
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
646
                goto done;
647
            }
648
            ret = virSecurityDACSetPCILabel(pci, vfioGroupDev, &cbdata);
649 650
            VIR_FREE(vfioGroupDev);
        } else {
651
            ret = virPCIDeviceFileIterate(pci,
652
                                          virSecurityDACSetPCILabel,
653
                                          &cbdata);
654 655
        }

656
        virPCIDeviceFree(pci);
657 658 659
        break;
    }

660
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
661
        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
662
        virSCSIDevicePtr scsi =
663
            virSCSIDeviceNew(NULL,
664 665
                             scsihostsrc->adapter, scsihostsrc->bus,
                             scsihostsrc->target, scsihostsrc->unit,
666
                             dev->readonly, dev->shareable);
667 668 669 670

        if (!scsi)
            goto done;

671
        ret = virSCSIDeviceFileIterate(scsi,
672
                                       virSecurityDACSetSCSILabel,
673
                                       &cbdata);
674 675 676 677 678
        virSCSIDeviceFree(scsi);

        break;
    }

679
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
680 681 682 683
        ret = 0;
        break;
    }

684
 done:
685 686 687 688 689
    return ret;
}


static int
690 691 692
virSecurityDACRestorePCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
                              const char *file,
                              void *opaque)
693
{
694 695
    virSecurityManagerPtr mgr = opaque;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
696
    return virSecurityDACRestoreFileLabel(priv, file);
697 698 699 700
}


static int
701 702 703
virSecurityDACRestoreUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                              const char *file,
                              void *opaque)
704
{
705 706
    virSecurityManagerPtr mgr = opaque;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
707
    return virSecurityDACRestoreFileLabel(priv, file);
708 709 710
}


711
static int
712 713 714
virSecurityDACRestoreSCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                               const char *file,
                               void *opaque)
715
{
716 717
    virSecurityManagerPtr mgr = opaque;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
718
    return virSecurityDACRestoreFileLabel(priv, file);
719 720 721
}


722
static int
723 724 725 726
virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainHostdevDefPtr dev,
                                  const char *vroot)
727 728 729

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
730
    virSecurityLabelDefPtr secdef;
731
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
732
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
733
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
734 735
    int ret = -1;

736 737
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

738
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
739 740 741 742 743
        return 0;

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

744
    /* Like virSecurityDACRestoreImageLabelInt() for a networked disk,
745 746
     * do nothing for an iSCSI hostdev
     */
747 748
    if (dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
        scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
749 750
        return 0;

751
    switch ((virDomainHostdevSubsysType) dev->source.subsys.type) {
752
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
753
        virUSBDevicePtr usb;
754 755 756

        if (dev->missing)
            return 0;
757

758
        if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot)))
759 760
            goto done;

761
        ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr);
762
        virUSBDeviceFree(usb);
763 764 765 766 767

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
768
        virPCIDevicePtr pci =
769 770
            virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
                            pcisrc->addr.slot, pcisrc->addr.function);
771 772 773 774

        if (!pci)
            goto done;

775
        if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
776
            char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
777

778 779
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
780
                goto done;
781
            }
782
            ret = virSecurityDACRestorePCILabel(pci, vfioGroupDev, mgr);
783 784
            VIR_FREE(vfioGroupDev);
        } else {
785
            ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr);
786
        }
787
        virPCIDeviceFree(pci);
788 789 790
        break;
    }

791
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
792
        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
793
        virSCSIDevicePtr scsi =
794
            virSCSIDeviceNew(NULL,
795 796
                             scsihostsrc->adapter, scsihostsrc->bus,
                             scsihostsrc->target, scsihostsrc->unit,
797
                             dev->readonly, dev->shareable);
798 799 800 801

        if (!scsi)
            goto done;

802
        ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr);
803 804 805 806 807
        virSCSIDeviceFree(scsi);

        break;
    }

808
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
809 810 811 812
        ret = 0;
        break;
    }

813
 done:
814 815 816 817 818 819
    return ret;
}


static int
virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
820
                              virDomainDefPtr def,
821 822
                              virDomainChrDefPtr dev,
                              virDomainChrSourceDefPtr dev_source)
823 824 825

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
826
    virSecurityLabelDefPtr seclabel;
827
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
828 829
    char *in = NULL, *out = NULL;
    int ret = -1;
830 831 832
    uid_t user;
    gid_t group;

833 834
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

835 836 837 838
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_DAC_NAME);

839
    if (chr_seclabel && !chr_seclabel->relabel)
840 841 842 843 844 845 846 847 848
        return 0;

    if (chr_seclabel && chr_seclabel->label) {
        if (virParseOwnershipIds(chr_seclabel->label, &user, &group) < 0)
            return -1;
    } else {
        if (virSecurityDACGetIds(seclabel, priv, &user, &group, NULL, NULL) < 0)
            return -1;
    }
849

850
    switch ((virDomainChrType) dev_source->type) {
851 852
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
853 854
        ret = virSecurityDACSetOwnership(priv, NULL,
                                         dev_source->data.file.path,
855
                                         user, group);
856 857 858
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
859 860
        if (virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0 ||
            virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0)
861 862
            goto done;
        if (virFileExists(in) && virFileExists(out)) {
J
Jiri Denemark 已提交
863 864
            if (virSecurityDACSetOwnership(priv, NULL, in, user, group) < 0 ||
                virSecurityDACSetOwnership(priv, NULL, out, user, group) < 0)
865
                goto done;
866 867
        } else if (virSecurityDACSetOwnership(priv, NULL,
                                              dev_source->data.file.path,
868
                                              user, group) < 0) {
869
            goto done;
870 871 872 873
        }
        ret = 0;
        break;

874 875
    case VIR_DOMAIN_CHR_TYPE_UNIX:
        if (!dev_source->data.nix.listen) {
876 877
            if (virSecurityDACSetOwnership(priv, NULL,
                                           dev_source->data.nix.path,
878 879 880 881 882 883
                                           user, group) < 0)
                goto done;
        }
        ret = 0;
        break;

884 885 886 887 888 889 890 891 892 893
    case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
    case VIR_DOMAIN_CHR_TYPE_NULL:
    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_PTY:
    case VIR_DOMAIN_CHR_TYPE_STDIO:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_TCP:
    case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
    case VIR_DOMAIN_CHR_TYPE_NMDM:
    case VIR_DOMAIN_CHR_TYPE_LAST:
894 895 896 897
        ret = 0;
        break;
    }

898
 done:
899 900 901 902 903 904
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}

static int
905
virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr,
906
                                  virDomainDefPtr def ATTRIBUTE_UNUSED,
907 908
                                  virDomainChrDefPtr dev,
                                  virDomainChrSourceDefPtr dev_source)
909
{
910
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
911
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
912 913 914
    char *in = NULL, *out = NULL;
    int ret = -1;

915 916 917 918
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_DAC_NAME);

919
    if (chr_seclabel && !chr_seclabel->relabel)
920 921
        return 0;

922
    switch ((virDomainChrType) dev_source->type) {
923 924
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
925
        ret = virSecurityDACRestoreFileLabel(priv, dev_source->data.file.path);
926 927 928
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
929 930
        if (virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0 ||
            virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0)
931
            goto done;
932
        if (virFileExists(in) && virFileExists(out)) {
933 934
            if (virSecurityDACRestoreFileLabel(priv, out) < 0 ||
                virSecurityDACRestoreFileLabel(priv, in) < 0)
J
Jim Fehlig 已提交
935
                goto done;
936
        } else if (virSecurityDACRestoreFileLabel(priv, dev_source->data.file.path) < 0) {
937 938
            goto done;
        }
939 940 941
        ret = 0;
        break;

942 943 944 945 946 947 948 949 950 951 952
    case VIR_DOMAIN_CHR_TYPE_NULL:
    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_PTY:
    case VIR_DOMAIN_CHR_TYPE_STDIO:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_TCP:
    case VIR_DOMAIN_CHR_TYPE_UNIX:
    case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
    case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
    case VIR_DOMAIN_CHR_TYPE_NMDM:
    case VIR_DOMAIN_CHR_TYPE_LAST:
953 954 955 956
        ret = 0;
        break;
    }

957
 done:
958 959 960 961 962 963 964
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}


static int
965
virSecurityDACRestoreChardevCallback(virDomainDefPtr def,
966 967 968 969 970
                                     virDomainChrDefPtr dev,
                                     void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

971
    return virSecurityDACRestoreChardevLabel(mgr, def, dev, dev->source);
972 973 974
}


975
static int
976 977 978
virSecurityDACSetTPMFileLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              virDomainTPMDefPtr tpm)
979 980 981 982 983
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
984
        ret = virSecurityDACSetChardevLabel(mgr, def, NULL,
985 986 987 988 989 990 991 992 993 994 995
                                            &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


static int
996 997 998
virSecurityDACRestoreTPMFileLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainTPMDefPtr tpm)
999 1000 1001 1002 1003
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
1004
        ret = virSecurityDACRestoreChardevLabel(mgr, def, NULL,
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
                                          &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
static int
virSecurityDACSetInputLabel(virSecurityManagerPtr mgr,
                            virDomainDefPtr def,
                            virDomainInputDefPtr input)

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr seclabel;
    int ret = -1;
    uid_t user;
    gid_t group;

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
    if (seclabel && !seclabel->relabel)
        return 0;

    switch ((virDomainInputType) input->type) {
    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
        if (virSecurityDACGetIds(seclabel, priv, &user, &group, NULL, NULL) < 0)
            return -1;

        ret = virSecurityDACSetOwnership(priv, NULL, input->source.evdev, user, group);
        break;

    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
    case VIR_DOMAIN_INPUT_TYPE_TABLET:
    case VIR_DOMAIN_INPUT_TYPE_KBD:
    case VIR_DOMAIN_INPUT_TYPE_LAST:
        ret = 0;
        break;
    }

    return ret;
}

static int
virSecurityDACRestoreInputLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr def ATTRIBUTE_UNUSED,
                                virDomainInputDefPtr input)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    int ret = -1;

    switch ((virDomainInputType) input->type) {
    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
1060
        ret = virSecurityDACRestoreFileLabel(priv, input->source.evdev);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
        break;

    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
    case VIR_DOMAIN_INPUT_TYPE_TABLET:
    case VIR_DOMAIN_INPUT_TYPE_KBD:
    case VIR_DOMAIN_INPUT_TYPE_LAST:
        ret = 0;
        break;
    }

    return ret;
}


1075
static int
1076 1077 1078
virSecurityDACRestoreAllLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              bool migrated)
1079 1080
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1081
    virSecurityLabelDefPtr secdef;
1082
    size_t i;
1083 1084
    int rc = 0;

1085
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1086

1087
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1088
        return 0;
1089 1090

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

1093
    for (i = 0; i < def->nhostdevs; i++) {
1094 1095 1096 1097
        if (virSecurityDACRestoreHostdevLabel(mgr,
                                              def,
                                              def->hostdevs[i],
                                              NULL) < 0)
1098 1099
            rc = -1;
    }
1100 1101 1102 1103 1104 1105

    for (i = 0; i < def->ninputs; i++) {
        if (virSecurityDACRestoreInputLabel(mgr, def, def->inputs[i]) < 0)
            rc = -1;
    }

1106
    for (i = 0; i < def->ndisks; i++) {
1107 1108 1109 1110
        if (virSecurityDACRestoreImageLabelInt(mgr,
                                               def,
                                               def->disks[i]->src,
                                               migrated) < 0)
1111 1112 1113
            rc = -1;
    }

1114
    if (virDomainChrDefForeach(def,
1115 1116
                               false,
                               virSecurityDACRestoreChardevCallback,
1117
                               mgr) < 0)
1118 1119
        rc = -1;

1120
    if (def->tpm) {
1121 1122 1123
        if (virSecurityDACRestoreTPMFileLabel(mgr,
                                              def,
                                              def->tpm) < 0)
1124 1125 1126
            rc = -1;
    }

1127
    if (def->os.loader && def->os.loader->nvram &&
1128
        virSecurityDACRestoreFileLabel(priv, def->os.loader->nvram) < 0)
1129 1130
        rc = -1;

1131 1132 1133 1134 1135
    return rc;
}


static int
1136
virSecurityDACSetChardevCallback(virDomainDefPtr def,
1137 1138 1139 1140 1141
                                 virDomainChrDefPtr dev,
                                 void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

1142
    return virSecurityDACSetChardevLabel(mgr, def, dev, dev->source);
1143 1144 1145 1146
}


static int
1147 1148 1149
virSecurityDACSetAllLabel(virSecurityManagerPtr mgr,
                          virDomainDefPtr def,
                          const char *stdin_path ATTRIBUTE_UNUSED)
1150 1151
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1152
    virSecurityLabelDefPtr secdef;
1153
    size_t i;
1154 1155
    uid_t user;
    gid_t group;
1156

1157 1158
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1159
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1160 1161
        return 0;

1162
    for (i = 0; i < def->ndisks; i++) {
1163
        /* XXX fixme - we need to recursively label the entire tree :-( */
E
Eric Blake 已提交
1164
        if (virDomainDiskGetType(def->disks[i]) == VIR_STORAGE_TYPE_DIR)
1165
            continue;
1166 1167 1168
        if (virSecurityDACSetDiskLabel(mgr,
                                       def,
                                       def->disks[i]) < 0)
1169 1170
            return -1;
    }
1171 1172 1173 1174 1175 1176

    for (i = 0; i < def->ninputs; i++) {
        if (virSecurityDACSetInputLabel(mgr, def, def->inputs[i]) < 0)
            return -1;
    }

1177
    for (i = 0; i < def->nhostdevs; i++) {
1178 1179 1180 1181
        if (virSecurityDACSetHostdevLabel(mgr,
                                          def,
                                          def->hostdevs[i],
                                          NULL) < 0)
1182 1183 1184
            return -1;
    }

1185
    if (virDomainChrDefForeach(def,
1186 1187
                               true,
                               virSecurityDACSetChardevCallback,
1188
                               mgr) < 0)
1189 1190
        return -1;

1191
    if (def->tpm) {
1192 1193 1194
        if (virSecurityDACSetTPMFileLabel(mgr,
                                          def,
                                          def->tpm) < 0)
1195 1196 1197
            return -1;
    }

1198
    if (virSecurityDACGetImageIds(secdef, priv, &user, &group))
1199 1200
        return -1;

1201
    if (def->os.loader && def->os.loader->nvram &&
1202 1203
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.loader->nvram, user, group) < 0)
1204 1205
        return -1;

1206
    if (def->os.kernel &&
1207 1208
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.kernel, user, group) < 0)
1209 1210
        return -1;

1211
    if (def->os.initrd &&
1212 1213
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.initrd, user, group) < 0)
1214 1215
        return -1;

O
Olivia Yin 已提交
1216
    if (def->os.dtb &&
1217 1218
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.dtb, user, group) < 0)
O
Olivia Yin 已提交
1219 1220
        return -1;

J
Ján Tomko 已提交
1221 1222 1223 1224 1225
    if (def->os.slic_table &&
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.slic_table, user, group) < 0)
        return -1;

1226 1227 1228 1229 1230 1231
    return 0;
}


static int
virSecurityDACSetSavedStateLabel(virSecurityManagerPtr mgr,
1232
                                 virDomainDefPtr def,
1233 1234
                                 const char *savefile)
{
1235 1236
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1237 1238
    uid_t user;
    gid_t group;
1239

1240 1241 1242
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

    if (virSecurityDACGetImageIds(secdef, priv, &user, &group) < 0)
1243 1244
        return -1;

1245
    return virSecurityDACSetOwnership(priv, NULL, savefile, user, group);
1246 1247 1248 1249 1250
}


static int
virSecurityDACRestoreSavedStateLabel(virSecurityManagerPtr mgr,
1251
                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
1252 1253 1254 1255 1256 1257 1258
                                     const char *savefile)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

1259
    return virSecurityDACRestoreFileLabel(priv, savefile);
1260 1261 1262 1263 1264
}


static int
virSecurityDACSetProcessLabel(virSecurityManagerPtr mgr,
1265
                              virDomainDefPtr def)
1266
{
1267 1268
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1269 1270
    uid_t user;
    gid_t group;
1271 1272
    gid_t *groups;
    int ngroups;
1273

1274 1275 1276
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

    if (virSecurityDACGetIds(secdef, priv, &user, &group, &groups, &ngroups) < 0)
1277
        return -1;
1278

J
Jiri Denemark 已提交
1279
    VIR_DEBUG("Dropping privileges to %u:%u, %d supplemental groups",
1280
              (unsigned int) user, (unsigned int) group, ngroups);
1281

1282
    if (virSetUIDGID(user, group, groups, ngroups) < 0)
1283 1284 1285
        return -1;

    return 0;
1286 1287 1288
}


1289 1290
static int
virSecurityDACSetChildProcessLabel(virSecurityManagerPtr mgr,
1291
                                   virDomainDefPtr def,
1292 1293
                                   virCommandPtr cmd)
{
1294 1295
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1296 1297 1298
    uid_t user;
    gid_t group;

1299 1300
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1301
    if (virSecurityDACGetIds(secdef, priv, &user, &group, NULL, NULL) < 0)
1302 1303
        return -1;

J
Jiri Denemark 已提交
1304
    VIR_DEBUG("Setting child to drop privileges to %u:%u",
1305 1306 1307 1308 1309 1310 1311 1312
              (unsigned int) user, (unsigned int) group);

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


1313 1314 1315 1316 1317 1318 1319 1320
static int
virSecurityDACVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
1321 1322
virSecurityDACGenLabel(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
1323
{
1324 1325 1326 1327 1328
    int rc = -1;
    virSecurityLabelDefPtr seclabel;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1329
    if (seclabel == NULL)
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        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;
    }

1348
    switch ((virDomainSeclabelType) seclabel->type) {
1349 1350 1351 1352 1353 1354 1355 1356 1357
    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:
1358
        if (virAsprintf(&seclabel->label, "+%u:+%u",
1359
                        (unsigned int) priv->user,
1360
                        (unsigned int) priv->group) < 0)
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
            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 */
1371
        return 0;
1372 1373
    case VIR_DOMAIN_SECLABEL_DEFAULT:
    case VIR_DOMAIN_SECLABEL_LAST:
1374 1375 1376 1377 1378 1379
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected security label type '%s'"),
                       virDomainSeclabelTypeToString(seclabel->type));
        return rc;
    }

1380
    if (seclabel->relabel && !seclabel->imagelabel &&
1381 1382 1383
        VIR_STRDUP(seclabel->imagelabel, seclabel->label) < 0) {
        VIR_FREE(seclabel->label);
        return rc;
1384 1385
    }

1386 1387 1388 1389 1390
    return 0;
}

static int
virSecurityDACReleaseLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1391
                           virDomainDefPtr def ATTRIBUTE_UNUSED)
1392 1393 1394 1395 1396 1397
{
    return 0;
}

static int
virSecurityDACReserveLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1398 1399
                           virDomainDefPtr def ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED)
1400 1401 1402 1403
{
    return 0;
}

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
#ifdef __linux__
static int
virSecurityDACGetProcessLabelInternal(pid_t pid,
                                      virSecurityLabelPtr seclabel)
{
    struct stat sb;
    char *path = NULL;
    int ret = -1;

    VIR_DEBUG("Getting DAC user and group on process '%d'", pid);

    if (virAsprintf(&path, "/proc/%d", (int) pid) < 0)
        goto cleanup;

    if (lstat(path, &sb) < 0) {
        virReportSystemError(errno,
                             _("unable to get uid and gid for PID %d via procfs"),
                             pid);
        goto cleanup;
    }

    snprintf(seclabel->label, VIR_SECURITY_LABEL_BUFLEN,
             "+%u:+%u", (unsigned int) sb.st_uid, (unsigned int) sb.st_gid);
    ret = 0;

 cleanup:
    VIR_FREE(path);
    return ret;
}
#elif defined(__FreeBSD__)
static int
virSecurityDACGetProcessLabelInternal(pid_t pid,
                                      virSecurityLabelPtr seclabel)
{
    struct kinfo_proc p;
    int mib[4];
    size_t len = 4;

    sysctlnametomib("kern.proc.pid", mib, &len);

    len = sizeof(struct kinfo_proc);
    mib[3] = pid;

    if (sysctl(mib, 4, &p, &len, NULL, 0) < 0) {
        virReportSystemError(errno,
                             _("unable to get PID %d uid and gid via sysctl"),
                             pid);
        return -1;
    }

    snprintf(seclabel->label, VIR_SECURITY_LABEL_BUFLEN,
             "+%u:+%u", (unsigned int) p.ki_uid, (unsigned int) p.ki_groups[0]);

    return 0;
}
#else
static int
J
Ján Tomko 已提交
1461 1462
virSecurityDACGetProcessLabelInternal(pid_t pid ATTRIBUTE_UNUSED,
                                      virSecurityLabelPtr seclabel ATTRIBUTE_UNUSED)
1463 1464 1465 1466 1467 1468 1469
{
    virReportSystemError(ENOSYS, "%s",
                         _("Cannot get process uid and gid on this platform"));
    return -1;
}
#endif

1470 1471
static int
virSecurityDACGetProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1472
                              virDomainDefPtr def,
1473
                              pid_t pid,
1474
                              virSecurityLabelPtr seclabel)
1475
{
1476 1477 1478
    virSecurityLabelDefPtr secdef =
        virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1479 1480 1481 1482 1483 1484 1485 1486
    if (secdef == NULL) {
        VIR_DEBUG("missing label for DAC security "
                  "driver in domain %s", def->name);

        if (virSecurityDACGetProcessLabelInternal(pid, seclabel) < 0)
            return -1;
        return 0;
    }
1487 1488

    if (secdef->label)
1489 1490
        ignore_value(virStrcpy(seclabel->label, secdef->label,
                               VIR_SECURITY_LABEL_BUFLEN));
1491

1492 1493 1494 1495
    return 0;
}

static int
1496
virSecurityDACSetDaemonSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1497
                                   virDomainDefPtr vm ATTRIBUTE_UNUSED)
1498 1499 1500 1501 1502
{
    return 0;
}


1503 1504
static int
virSecurityDACSetSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1505
                             virDomainDefPtr def ATTRIBUTE_UNUSED)
1506 1507 1508 1509 1510
{
    return 0;
}


1511 1512
static int
virSecurityDACClearSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1513
                               virDomainDefPtr def ATTRIBUTE_UNUSED)
1514 1515 1516 1517
{
    return 0;
}

1518
static int
1519
virSecurityDACSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1520
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
1521
                              int fd ATTRIBUTE_UNUSED)
1522 1523 1524 1525
{
    return 0;
}

1526 1527 1528 1529 1530 1531 1532 1533
static int
virSecurityDACSetTapFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                            virDomainDefPtr def ATTRIBUTE_UNUSED,
                            int fd ATTRIBUTE_UNUSED)
{
    return 0;
}

O
Osier Yang 已提交
1534 1535 1536 1537
static char *
virSecurityDACGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                              virDomainDefPtr vm ATTRIBUTE_UNUSED)
{
1538 1539 1540
    return NULL;
}

1541 1542 1543 1544 1545 1546 1547 1548
static const char *
virSecurityDACGetBaseLabel(virSecurityManagerPtr mgr,
                           int virt ATTRIBUTE_UNUSED)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    return priv->baselabel;
}

1549
static int
1550 1551 1552
virSecurityDACDomainSetPathLabel(virSecurityManagerPtr mgr,
                                 virDomainDefPtr def,
                                 const char *path)
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr seclabel;
    uid_t user;
    gid_t group;

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

    if (virSecurityDACGetIds(seclabel, priv, &user, &group, NULL, NULL) < 0)
        return -1;

1564
    return virSecurityDACSetOwnership(priv, NULL, path, user, group);
1565 1566
}

1567
virSecurityDriver virSecurityDriverDAC = {
1568
    .privateDataLen                     = sizeof(virSecurityDACData),
1569
    .name                               = SECURITY_DAC_NAME,
1570 1571 1572
    .probe                              = virSecurityDACProbe,
    .open                               = virSecurityDACOpen,
    .close                              = virSecurityDACClose,
1573

1574 1575
    .getModel                           = virSecurityDACGetModel,
    .getDOI                             = virSecurityDACGetDOI,
1576

1577 1578
    .preFork                            = virSecurityDACPreFork,

1579
    .domainSecurityVerify               = virSecurityDACVerify,
1580

1581 1582
    .domainSetSecurityDiskLabel         = virSecurityDACSetDiskLabel,
    .domainRestoreSecurityDiskLabel     = virSecurityDACRestoreDiskLabel,
1583

1584 1585
    .domainSetSecurityImageLabel        = virSecurityDACSetImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityDACRestoreImageLabel,
1586

1587 1588 1589
    .domainSetSecurityDaemonSocketLabel = virSecurityDACSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityDACSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityDACClearSocketLabel,
1590

1591 1592 1593
    .domainGenSecurityLabel             = virSecurityDACGenLabel,
    .domainReserveSecurityLabel         = virSecurityDACReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityDACReleaseLabel,
1594

1595 1596
    .domainGetSecurityProcessLabel      = virSecurityDACGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityDACSetProcessLabel,
1597
    .domainSetSecurityChildProcessLabel = virSecurityDACSetChildProcessLabel,
1598

1599 1600
    .domainSetSecurityAllLabel          = virSecurityDACSetAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityDACRestoreAllLabel,
1601

1602 1603
    .domainSetSecurityHostdevLabel      = virSecurityDACSetHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityDACRestoreHostdevLabel,
1604

1605 1606
    .domainSetSavedStateLabel           = virSecurityDACSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityDACRestoreSavedStateLabel,
1607

1608
    .domainSetSecurityImageFDLabel      = virSecurityDACSetImageFDLabel,
1609
    .domainSetSecurityTapFDLabel        = virSecurityDACSetTapFDLabel,
1610

1611
    .domainGetSecurityMountOptions      = virSecurityDACGetMountOptions,
1612 1613

    .getBaseLabel                       = virSecurityDACGetBaseLabel,
1614

1615
    .domainSetPathLabel                 = virSecurityDACDomainSetPathLabel,
1616
};