security_dac.c 47.3 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 680 681 682
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
        /* Fall through for now */
    }

683
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
684 685 686 687
        ret = 0;
        break;
    }

688
 done:
689 690 691 692 693
    return ret;
}


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


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


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


726
static int
727 728 729 730
virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainHostdevDefPtr dev,
                                  const char *vroot)
731 732 733

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

740 741
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

742
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
743 744 745 746 747
        return 0;

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

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

755
    switch ((virDomainHostdevSubsysType) dev->source.subsys.type) {
756
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
757
        virUSBDevicePtr usb;
758 759 760

        if (dev->missing)
            return 0;
761

762
        if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot)))
763 764
            goto done;

765
        ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr);
766
        virUSBDeviceFree(usb);
767 768 769 770 771

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
772
        virPCIDevicePtr pci =
773 774
            virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
                            pcisrc->addr.slot, pcisrc->addr.function);
775 776 777 778

        if (!pci)
            goto done;

779
        if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
780
            char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
781

782 783
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
784
                goto done;
785
            }
786
            ret = virSecurityDACRestorePCILabel(pci, vfioGroupDev, mgr);
787 788
            VIR_FREE(vfioGroupDev);
        } else {
789
            ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr);
790
        }
791
        virPCIDeviceFree(pci);
792 793 794
        break;
    }

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

        if (!scsi)
            goto done;

806
        ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr);
807 808 809 810 811
        virSCSIDeviceFree(scsi);

        break;
    }

812 813 814 815
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
        /* Fall through for now */
    }

816
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
817 818 819 820
        ret = 0;
        break;
    }

821
 done:
822 823 824 825 826 827
    return ret;
}


static int
virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
828
                              virDomainDefPtr def,
829 830
                              virDomainChrDefPtr dev,
                              virDomainChrSourceDefPtr dev_source)
831 832 833

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
834
    virSecurityLabelDefPtr seclabel;
835
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
836 837
    char *in = NULL, *out = NULL;
    int ret = -1;
838 839 840
    uid_t user;
    gid_t group;

841 842
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

843 844 845 846
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_DAC_NAME);

847
    if (chr_seclabel && !chr_seclabel->relabel)
848 849 850 851 852 853 854 855 856
        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;
    }
857

858
    switch ((virDomainChrType) dev_source->type) {
859 860
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
861 862
        ret = virSecurityDACSetOwnership(priv, NULL,
                                         dev_source->data.file.path,
863
                                         user, group);
864 865 866
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
867 868
        if (virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0 ||
            virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0)
869 870
            goto done;
        if (virFileExists(in) && virFileExists(out)) {
J
Jiri Denemark 已提交
871 872
            if (virSecurityDACSetOwnership(priv, NULL, in, user, group) < 0 ||
                virSecurityDACSetOwnership(priv, NULL, out, user, group) < 0)
873
                goto done;
874 875
        } else if (virSecurityDACSetOwnership(priv, NULL,
                                              dev_source->data.file.path,
876
                                              user, group) < 0) {
877
            goto done;
878 879 880 881
        }
        ret = 0;
        break;

882 883
    case VIR_DOMAIN_CHR_TYPE_UNIX:
        if (!dev_source->data.nix.listen) {
884 885
            if (virSecurityDACSetOwnership(priv, NULL,
                                           dev_source->data.nix.path,
886 887 888 889 890 891
                                           user, group) < 0)
                goto done;
        }
        ret = 0;
        break;

892 893 894 895 896 897 898 899 900 901
    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:
902 903 904 905
        ret = 0;
        break;
    }

906
 done:
907 908 909 910 911 912
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}

static int
913
virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr,
914
                                  virDomainDefPtr def ATTRIBUTE_UNUSED,
915 916
                                  virDomainChrDefPtr dev,
                                  virDomainChrSourceDefPtr dev_source)
917
{
918
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
919
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
920 921 922
    char *in = NULL, *out = NULL;
    int ret = -1;

923 924 925 926
    if (dev)
        chr_seclabel = virDomainChrDefGetSecurityLabelDef(dev,
                                                          SECURITY_DAC_NAME);

927
    if (chr_seclabel && !chr_seclabel->relabel)
928 929
        return 0;

930
    switch ((virDomainChrType) dev_source->type) {
931 932
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
933
        ret = virSecurityDACRestoreFileLabel(priv, dev_source->data.file.path);
934 935 936
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
937 938
        if (virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0 ||
            virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0)
939
            goto done;
940
        if (virFileExists(in) && virFileExists(out)) {
941 942
            if (virSecurityDACRestoreFileLabel(priv, out) < 0 ||
                virSecurityDACRestoreFileLabel(priv, in) < 0)
J
Jim Fehlig 已提交
943
                goto done;
944
        } else if (virSecurityDACRestoreFileLabel(priv, dev_source->data.file.path) < 0) {
945 946
            goto done;
        }
947 948 949
        ret = 0;
        break;

950 951 952 953 954 955 956 957 958 959 960
    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:
961 962 963 964
        ret = 0;
        break;
    }

965
 done:
966 967 968 969 970 971 972
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}


static int
973
virSecurityDACRestoreChardevCallback(virDomainDefPtr def,
974 975 976 977 978
                                     virDomainChrDefPtr dev,
                                     void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

979
    return virSecurityDACRestoreChardevLabel(mgr, def, dev, dev->source);
980 981 982
}


983
static int
984 985 986
virSecurityDACSetTPMFileLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              virDomainTPMDefPtr tpm)
987 988 989 990 991
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
992
        ret = virSecurityDACSetChardevLabel(mgr, def, NULL,
993 994 995 996 997 998 999 1000 1001 1002 1003
                                            &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


static int
1004 1005 1006
virSecurityDACRestoreTPMFileLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainTPMDefPtr tpm)
1007 1008 1009 1010 1011
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
1012
        ret = virSecurityDACRestoreChardevLabel(mgr, def, NULL,
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
                                          &tpm->data.passthrough.source);
        break;
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


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 1060 1061 1062 1063 1064 1065 1066 1067
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:
1068
        ret = virSecurityDACRestoreFileLabel(priv, input->source.evdev);
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        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;
}


1083
static int
1084 1085 1086
virSecurityDACRestoreAllLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              bool migrated)
1087 1088
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1089
    virSecurityLabelDefPtr secdef;
1090
    size_t i;
1091 1092
    int rc = 0;

1093
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1094

1095
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1096
        return 0;
1097 1098

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

1101
    for (i = 0; i < def->nhostdevs; i++) {
1102 1103 1104 1105
        if (virSecurityDACRestoreHostdevLabel(mgr,
                                              def,
                                              def->hostdevs[i],
                                              NULL) < 0)
1106 1107
            rc = -1;
    }
1108 1109 1110 1111 1112 1113

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

1114
    for (i = 0; i < def->ndisks; i++) {
1115 1116 1117 1118
        if (virSecurityDACRestoreImageLabelInt(mgr,
                                               def,
                                               def->disks[i]->src,
                                               migrated) < 0)
1119 1120 1121
            rc = -1;
    }

1122
    if (virDomainChrDefForeach(def,
1123 1124
                               false,
                               virSecurityDACRestoreChardevCallback,
1125
                               mgr) < 0)
1126 1127
        rc = -1;

1128
    if (def->tpm) {
1129 1130 1131
        if (virSecurityDACRestoreTPMFileLabel(mgr,
                                              def,
                                              def->tpm) < 0)
1132 1133 1134
            rc = -1;
    }

1135
    if (def->os.loader && def->os.loader->nvram &&
1136
        virSecurityDACRestoreFileLabel(priv, def->os.loader->nvram) < 0)
1137 1138
        rc = -1;

1139 1140 1141 1142 1143
    return rc;
}


static int
1144
virSecurityDACSetChardevCallback(virDomainDefPtr def,
1145 1146 1147 1148 1149
                                 virDomainChrDefPtr dev,
                                 void *opaque)
{
    virSecurityManagerPtr mgr = opaque;

1150
    return virSecurityDACSetChardevLabel(mgr, def, dev, dev->source);
1151 1152 1153 1154
}


static int
1155 1156 1157
virSecurityDACSetAllLabel(virSecurityManagerPtr mgr,
                          virDomainDefPtr def,
                          const char *stdin_path ATTRIBUTE_UNUSED)
1158 1159
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1160
    virSecurityLabelDefPtr secdef;
1161
    size_t i;
1162 1163
    uid_t user;
    gid_t group;
1164

1165 1166
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1167
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1168 1169
        return 0;

1170
    for (i = 0; i < def->ndisks; i++) {
1171
        /* XXX fixme - we need to recursively label the entire tree :-( */
E
Eric Blake 已提交
1172
        if (virDomainDiskGetType(def->disks[i]) == VIR_STORAGE_TYPE_DIR)
1173
            continue;
1174 1175 1176
        if (virSecurityDACSetDiskLabel(mgr,
                                       def,
                                       def->disks[i]) < 0)
1177 1178
            return -1;
    }
1179 1180 1181 1182 1183 1184

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

1185
    for (i = 0; i < def->nhostdevs; i++) {
1186 1187 1188 1189
        if (virSecurityDACSetHostdevLabel(mgr,
                                          def,
                                          def->hostdevs[i],
                                          NULL) < 0)
1190 1191 1192
            return -1;
    }

1193
    if (virDomainChrDefForeach(def,
1194 1195
                               true,
                               virSecurityDACSetChardevCallback,
1196
                               mgr) < 0)
1197 1198
        return -1;

1199
    if (def->tpm) {
1200 1201 1202
        if (virSecurityDACSetTPMFileLabel(mgr,
                                          def,
                                          def->tpm) < 0)
1203 1204 1205
            return -1;
    }

1206
    if (virSecurityDACGetImageIds(secdef, priv, &user, &group))
1207 1208
        return -1;

1209
    if (def->os.loader && def->os.loader->nvram &&
1210 1211
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.loader->nvram, user, group) < 0)
1212 1213
        return -1;

1214
    if (def->os.kernel &&
1215 1216
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.kernel, user, group) < 0)
1217 1218
        return -1;

1219
    if (def->os.initrd &&
1220 1221
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.initrd, user, group) < 0)
1222 1223
        return -1;

O
Olivia Yin 已提交
1224
    if (def->os.dtb &&
1225 1226
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.dtb, user, group) < 0)
O
Olivia Yin 已提交
1227 1228
        return -1;

J
Ján Tomko 已提交
1229 1230 1231 1232 1233
    if (def->os.slic_table &&
        virSecurityDACSetOwnership(priv, NULL,
                                   def->os.slic_table, user, group) < 0)
        return -1;

1234 1235 1236 1237 1238 1239
    return 0;
}


static int
virSecurityDACSetSavedStateLabel(virSecurityManagerPtr mgr,
1240
                                 virDomainDefPtr def,
1241 1242
                                 const char *savefile)
{
1243 1244
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1245 1246
    uid_t user;
    gid_t group;
1247

1248 1249 1250
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

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

1253
    return virSecurityDACSetOwnership(priv, NULL, savefile, user, group);
1254 1255 1256 1257 1258
}


static int
virSecurityDACRestoreSavedStateLabel(virSecurityManagerPtr mgr,
1259
                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
1260 1261 1262 1263 1264 1265 1266
                                     const char *savefile)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

1267
    return virSecurityDACRestoreFileLabel(priv, savefile);
1268 1269 1270 1271 1272
}


static int
virSecurityDACSetProcessLabel(virSecurityManagerPtr mgr,
1273
                              virDomainDefPtr def)
1274
{
1275 1276
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1277 1278
    uid_t user;
    gid_t group;
1279 1280
    gid_t *groups;
    int ngroups;
1281

1282 1283 1284
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

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

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

1290
    if (virSetUIDGID(user, group, groups, ngroups) < 0)
1291 1292 1293
        return -1;

    return 0;
1294 1295 1296
}


1297 1298
static int
virSecurityDACSetChildProcessLabel(virSecurityManagerPtr mgr,
1299
                                   virDomainDefPtr def,
1300 1301
                                   virCommandPtr cmd)
{
1302 1303
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1304 1305 1306
    uid_t user;
    gid_t group;

1307 1308
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1309
    if (virSecurityDACGetIds(secdef, priv, &user, &group, NULL, NULL) < 0)
1310 1311
        return -1;

J
Jiri Denemark 已提交
1312
    VIR_DEBUG("Setting child to drop privileges to %u:%u",
1313 1314 1315 1316 1317 1318 1319 1320
              (unsigned int) user, (unsigned int) group);

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


1321 1322 1323 1324 1325 1326 1327 1328
static int
virSecurityDACVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
1329 1330
virSecurityDACGenLabel(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
1331
{
1332 1333 1334 1335 1336
    int rc = -1;
    virSecurityLabelDefPtr seclabel;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1337
    if (seclabel == NULL)
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
        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;
    }

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

1388
    if (seclabel->relabel && !seclabel->imagelabel &&
1389 1390 1391
        VIR_STRDUP(seclabel->imagelabel, seclabel->label) < 0) {
        VIR_FREE(seclabel->label);
        return rc;
1392 1393
    }

1394 1395 1396 1397 1398
    return 0;
}

static int
virSecurityDACReleaseLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1399
                           virDomainDefPtr def ATTRIBUTE_UNUSED)
1400 1401 1402 1403 1404 1405
{
    return 0;
}

static int
virSecurityDACReserveLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1406 1407
                           virDomainDefPtr def ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED)
1408 1409 1410 1411
{
    return 0;
}

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 1461 1462 1463 1464 1465 1466 1467 1468
#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 已提交
1469 1470
virSecurityDACGetProcessLabelInternal(pid_t pid ATTRIBUTE_UNUSED,
                                      virSecurityLabelPtr seclabel ATTRIBUTE_UNUSED)
1471 1472 1473 1474 1475 1476 1477
{
    virReportSystemError(ENOSYS, "%s",
                         _("Cannot get process uid and gid on this platform"));
    return -1;
}
#endif

1478 1479
static int
virSecurityDACGetProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1480
                              virDomainDefPtr def,
1481
                              pid_t pid,
1482
                              virSecurityLabelPtr seclabel)
1483
{
1484 1485 1486
    virSecurityLabelDefPtr secdef =
        virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1487 1488 1489 1490 1491 1492 1493 1494
    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;
    }
1495 1496

    if (secdef->label)
1497 1498
        ignore_value(virStrcpy(seclabel->label, secdef->label,
                               VIR_SECURITY_LABEL_BUFLEN));
1499

1500 1501 1502 1503
    return 0;
}

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


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


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

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

1534 1535 1536 1537 1538 1539 1540 1541
static int
virSecurityDACSetTapFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                            virDomainDefPtr def ATTRIBUTE_UNUSED,
                            int fd ATTRIBUTE_UNUSED)
{
    return 0;
}

O
Osier Yang 已提交
1542 1543 1544 1545
static char *
virSecurityDACGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                              virDomainDefPtr vm ATTRIBUTE_UNUSED)
{
1546 1547 1548
    return NULL;
}

1549 1550 1551 1552 1553 1554 1555 1556
static const char *
virSecurityDACGetBaseLabel(virSecurityManagerPtr mgr,
                           int virt ATTRIBUTE_UNUSED)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    return priv->baselabel;
}

1557
static int
1558 1559 1560
virSecurityDACDomainSetPathLabel(virSecurityManagerPtr mgr,
                                 virDomainDefPtr def,
                                 const char *path)
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
{
    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;

1572
    return virSecurityDACSetOwnership(priv, NULL, path, user, group);
1573 1574
}

1575
virSecurityDriver virSecurityDriverDAC = {
1576
    .privateDataLen                     = sizeof(virSecurityDACData),
1577
    .name                               = SECURITY_DAC_NAME,
1578 1579 1580
    .probe                              = virSecurityDACProbe,
    .open                               = virSecurityDACOpen,
    .close                              = virSecurityDACClose,
1581

1582 1583
    .getModel                           = virSecurityDACGetModel,
    .getDOI                             = virSecurityDACGetDOI,
1584

1585 1586
    .preFork                            = virSecurityDACPreFork,

1587
    .domainSecurityVerify               = virSecurityDACVerify,
1588

1589 1590
    .domainSetSecurityDiskLabel         = virSecurityDACSetDiskLabel,
    .domainRestoreSecurityDiskLabel     = virSecurityDACRestoreDiskLabel,
1591

1592 1593
    .domainSetSecurityImageLabel        = virSecurityDACSetImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityDACRestoreImageLabel,
1594

1595 1596 1597
    .domainSetSecurityDaemonSocketLabel = virSecurityDACSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityDACSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityDACClearSocketLabel,
1598

1599 1600 1601
    .domainGenSecurityLabel             = virSecurityDACGenLabel,
    .domainReserveSecurityLabel         = virSecurityDACReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityDACReleaseLabel,
1602

1603 1604
    .domainGetSecurityProcessLabel      = virSecurityDACGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityDACSetProcessLabel,
1605
    .domainSetSecurityChildProcessLabel = virSecurityDACSetChildProcessLabel,
1606

1607 1608
    .domainSetSecurityAllLabel          = virSecurityDACSetAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityDACRestoreAllLabel,
1609

1610 1611
    .domainSetSecurityHostdevLabel      = virSecurityDACSetHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityDACRestoreHostdevLabel,
1612

1613 1614
    .domainSetSavedStateLabel           = virSecurityDACSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityDACRestoreSavedStateLabel,
1615

1616
    .domainSetSecurityImageFDLabel      = virSecurityDACSetImageFDLabel,
1617
    .domainSetSecurityTapFDLabel        = virSecurityDACSetTapFDLabel,
1618

1619
    .domainGetSecurityMountOptions      = virSecurityDACGetMountOptions,
1620 1621

    .getBaseLabel                       = virSecurityDACGetBaseLabel,
1622

1623
    .domainSetPathLabel                 = virSecurityDACDomainSetPathLabel,
1624
};