security_dac.c 64.9 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 "virmdev.h"
37
#include "virpci.h"
38
#include "virusb.h"
39
#include "virscsi.h"
40
#include "virscsivhost.h"
41
#include "virstoragefile.h"
42
#include "virstring.h"
M
Martin Kletzander 已提交
43
#include "virutil.h"
44 45

#define VIR_FROM_THIS VIR_FROM_SECURITY
46 47 48

VIR_LOG_INIT("security.security_dac");

49
#define SECURITY_DAC_NAME "dac"
50 51 52 53 54 55 56

typedef struct _virSecurityDACData virSecurityDACData;
typedef virSecurityDACData *virSecurityDACDataPtr;

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

65 66 67 68 69 70 71 72
typedef struct _virSecurityDACCallbackData virSecurityDACCallbackData;
typedef virSecurityDACCallbackData *virSecurityDACCallbackDataPtr;

struct _virSecurityDACCallbackData {
    virSecurityManagerPtr manager;
    virSecurityLabelDefPtr secdef;
};

73 74 75
typedef struct _virSecurityDACChownItem virSecurityDACChownItem;
typedef virSecurityDACChownItem *virSecurityDACChownItemPtr;
struct _virSecurityDACChownItem {
76
    char *path;
77 78 79
    const virStorageSource *src;
    uid_t uid;
    gid_t gid;
80
    bool restore;
81 82 83 84 85
};

typedef struct _virSecurityDACChownList virSecurityDACChownList;
typedef virSecurityDACChownList *virSecurityDACChownListPtr;
struct _virSecurityDACChownList {
86
    virSecurityManagerPtr manager;
87 88 89 90 91 92 93 94 95 96 97 98
    virSecurityDACChownItemPtr *items;
    size_t nItems;
};


virThreadLocal chownList;

static int
virSecurityDACChownListAppend(virSecurityDACChownListPtr list,
                              const char *path,
                              const virStorageSource *src,
                              uid_t uid,
99 100
                              gid_t gid,
                              bool restore)
101
{
102 103 104
    int ret = -1;
    char *tmp = NULL;
    virSecurityDACChownItemPtr item = NULL;
105 106 107 108

    if (VIR_ALLOC(item) < 0)
        return -1;

109 110 111 112
    if (VIR_STRDUP(tmp, path) < 0)
        goto cleanup;

    item->path = tmp;
113 114 115
    item->src = src;
    item->uid = uid;
    item->gid = gid;
116
    item->restore = restore;
117

118 119
    if (VIR_APPEND_ELEMENT(list->items, list->nItems, item) < 0)
        goto cleanup;
120

121 122 123 124 125 126 127
    tmp = NULL;

    ret = 0;
 cleanup:
    VIR_FREE(tmp);
    VIR_FREE(item);
    return ret;
128 129 130 131 132 133 134 135 136 137 138
}

static void
virSecurityDACChownListFree(void *opaque)
{
    virSecurityDACChownListPtr list = opaque;
    size_t i;

    if (!list)
        return;

139 140
    for (i = 0; i < list->nItems; i++) {
        VIR_FREE(list->items[i]->path);
141
        VIR_FREE(list->items[i]);
142
    }
143
    VIR_FREE(list->items);
144
    virObjectUnref(list->manager);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    VIR_FREE(list);
}


/**
 * virSecurityDACTransactionAppend:
 * @path: Path to chown
 * @src: disk source to chown
 * @uid: user ID
 * @gid: group ID
 *
 * Appends an entry onto transaction list.
 *
 * Returns: 1 in case of successful append
 *          0 if there is no transaction enabled
 *         -1 otherwise.
 */
static int
virSecurityDACTransactionAppend(const char *path,
                                const virStorageSource *src,
                                uid_t uid,
166 167
                                gid_t gid,
                                bool restore)
168 169 170 171 172
{
    virSecurityDACChownListPtr list = virThreadLocalGet(&chownList);
    if (!list)
        return 0;

173
    if (virSecurityDACChownListAppend(list, path, src, uid, gid, restore) < 0)
174 175 176 177 178 179
        return -1;

    return 1;
}


180 181 182 183 184
static int virSecurityDACSetOwnership(virSecurityManagerPtr mgr,
                                      const virStorageSource *src,
                                      const char *path,
                                      uid_t uid,
                                      gid_t gid);
185

186 187 188
static int virSecurityDACRestoreFileLabelInternal(virSecurityManagerPtr mgr,
                                                  const virStorageSource *src,
                                                  const char *path);
189 190 191 192 193 194 195
/**
 * virSecurityDACTransactionRun:
 * @pid: process pid
 * @opaque: opaque data
 *
 * This is the callback that runs in the same namespace as the domain we are
 * relabelling. For given transaction (@opaque) it relabels all the paths on
196 197
 * the list. Depending on security manager configuration it might lock paths
 * we will relabel.
198 199 200 201 202 203 204 205 206
 *
 * Returns: 0 on success
 *         -1 otherwise.
 */
static int
virSecurityDACTransactionRun(pid_t pid ATTRIBUTE_UNUSED,
                             void *opaque)
{
    virSecurityDACChownListPtr list = opaque;
207 208
    const char **paths = NULL;
    size_t npaths = 0;
209
    size_t i;
210
    int rv = 0;
211 212 213 214 215 216 217 218
    int ret = -1;

    if (VIR_ALLOC_N(paths, list->nItems) < 0)
        return -1;

    for (i = 0; i < list->nItems; i++) {
        const char *p = list->items[i]->path;

219 220
        if (!p ||
            virFileIsDir(p))
221 222 223 224 225 226 227
            continue;

        VIR_APPEND_ELEMENT_COPY_INPLACE(paths, npaths, p);
    }

    if (virSecurityManagerMetadataLock(list->manager, paths, npaths) < 0)
        goto cleanup;
228 229 230 231 232

    for (i = 0; i < list->nItems; i++) {
        virSecurityDACChownItemPtr item = list->items[i];

        /* TODO Implement rollback */
233 234 235 236 237 238 239 240 241 242 243 244 245
        if (!item->restore) {
            rv = virSecurityDACSetOwnership(list->manager,
                                            item->src,
                                            item->path,
                                            item->uid,
                                            item->gid);
        } else {
            rv = virSecurityDACRestoreFileLabelInternal(list->manager,
                                                        item->src,
                                                        item->path);
        }

        if (rv < 0)
246
            break;
247 248
    }

249 250 251 252 253 254 255 256 257 258
    if (virSecurityManagerMetadataUnlock(list->manager, paths, npaths) < 0)
        goto cleanup;

    if (rv < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    VIR_FREE(paths);
    return ret;
259 260 261
}


262 263 264 265 266
/* returns -1 on error, 0 on success */
int
virSecurityDACSetUserAndGroup(virSecurityManagerPtr mgr,
                              uid_t user,
                              gid_t group)
267 268 269 270
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->user = user;
    priv->group = group;
271

272
    if (virAsprintf(&priv->baselabel, "+%u:+%u",
273 274
                    (unsigned int)user,
                    (unsigned int)group) < 0)
275 276 277
        return -1;

    return 0;
278 279
}

O
Osier Yang 已提交
280 281 282
void
virSecurityDACSetDynamicOwnership(virSecurityManagerPtr mgr,
                                  bool dynamicOwnership)
283 284 285 286 287
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->dynamicOwnership = dynamicOwnership;
}

288 289 290 291 292 293 294 295 296
void
virSecurityDACSetMountNamespace(virSecurityManagerPtr mgr,
                                bool mountNamespace)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->mountNamespace = mountNamespace;
}


297 298 299 300 301 302 303 304
void
virSecurityDACSetChownCallback(virSecurityManagerPtr mgr,
                               virSecurityManagerDACChownCallback chownCallback)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    priv->chownCallback = chownCallback;
}

305
/* returns 1 if label isn't found, 0 on success, -1 on error */
O
Osier Yang 已提交
306
static int
307
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
308 309
virSecurityDACParseIds(virSecurityLabelDefPtr seclabel,
                       uid_t *uidPtr, gid_t *gidPtr)
310
{
311
    if (!seclabel || !seclabel->label)
312
        return 1;
313

314
    if (virParseOwnershipIds(seclabel->label, uidPtr, gidPtr) < 0)
315 316 317 318 319
        return -1;

    return 0;
}

O
Osier Yang 已提交
320
static int
321
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
322 323
virSecurityDACGetIds(virSecurityLabelDefPtr seclabel,
                     virSecurityDACDataPtr priv,
324 325
                     uid_t *uidPtr, gid_t *gidPtr,
                     gid_t **groups, int *ngroups)
326
{
327 328
    int ret;

329 330 331 332 333
    if (groups)
        *groups = priv ? priv->groups : NULL;
    if (ngroups)
        *ngroups = priv ? priv->ngroups : 0;

334
    if ((ret = virSecurityDACParseIds(seclabel, uidPtr, gidPtr)) <= 0)
335 336 337
        return ret;

    if (!priv) {
338 339
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("DAC seclabel couldn't be determined"));
340 341 342
        return -1;
    }

343 344
    *uidPtr = priv->user;
    *gidPtr = priv->group;
345 346

    return 0;
347 348
}

349 350

/* returns 1 if label isn't found, 0 on success, -1 on error */
O
Osier Yang 已提交
351
static int
352
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
353
virSecurityDACParseImageIds(virSecurityLabelDefPtr seclabel,
O
Osier Yang 已提交
354
                            uid_t *uidPtr, gid_t *gidPtr)
355
{
356
    if (!seclabel || !seclabel->imagelabel)
357
        return 1;
358

359
    if (virParseOwnershipIds(seclabel->imagelabel, uidPtr, gidPtr) < 0)
360 361 362 363 364
        return -1;

    return 0;
}

O
Osier Yang 已提交
365
static int
366
ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
367 368
virSecurityDACGetImageIds(virSecurityLabelDefPtr seclabel,
                          virSecurityDACDataPtr priv,
O
Osier Yang 已提交
369
                          uid_t *uidPtr, gid_t *gidPtr)
370
{
371 372
    int ret;

373
    if ((ret = virSecurityDACParseImageIds(seclabel, uidPtr, gidPtr)) <= 0)
374 375 376
        return ret;

    if (!priv) {
377 378
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("DAC imagelabel couldn't be determined"));
379
        return -1;
380
    }
381

382 383
    *uidPtr = priv->user;
    *gidPtr = priv->group;
384 385

    return 0;
386 387
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/**
 * 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;
}
431

432
static virSecurityDriverStatus
433
virSecurityDACProbe(const char *virtDriver ATTRIBUTE_UNUSED)
434 435 436 437 438 439 440
{
    return SECURITY_DRIVER_ENABLE;
}

static int
virSecurityDACOpen(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
441 442 443 444 445 446 447
    if (virThreadLocalInit(&chownList,
                           virSecurityDACChownListFree) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to initialize thread local variable"));
        return -1;
    }

448 449 450 451
    return 0;
}

static int
452
virSecurityDACClose(virSecurityManagerPtr mgr)
453
{
454 455
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    VIR_FREE(priv->groups);
456
    VIR_FREE(priv->baselabel);
457 458 459 460
    return 0;
}


O
Osier Yang 已提交
461 462
static const char *
virSecurityDACGetModel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
463
{
464
    return SECURITY_DAC_NAME;
465 466
}

O
Osier Yang 已提交
467 468
static const char *
virSecurityDACGetDOI(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
469 470 471 472
{
    return "0";
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
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;
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/**
 * virSecurityDACTransactionStart:
 * @mgr: security manager
 *
 * Starts a new transaction. In transaction nothing is chown()-ed until
 * TransactionCommit() is called. This is implemented as a list that is
 * appended to whenever chown() would be called. Since secdriver APIs
 * can be called from multiple threads (to work over different domains)
 * the pointer to the list is stored in thread local variable.
 *
 * Returns 0 on success,
 *        -1 otherwise.
 */
static int
virSecurityDACTransactionStart(virSecurityManagerPtr mgr)
{
    virSecurityDACChownListPtr list;

    list = virThreadLocalGet(&chownList);
    if (list) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Another relabel transaction is already started"));
        return -1;
    }

    if (VIR_ALLOC(list) < 0)
        return -1;

516
    list->manager = virObjectRef(mgr);
517 518 519 520

    if (virThreadLocalSet(&chownList, list) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to set thread local variable"));
521
        virSecurityDACChownListFree(list);
522 523 524 525 526 527 528 529 530 531 532
        return -1;
    }

    return 0;
}

/**
 * virSecurityDACTransactionCommit:
 * @mgr: security manager
 * @pid: domain's PID
 *
533 534 535 536 537 538 539 540
 * If @pid is not -1 then enter the @pid namespace (usually @pid refers
 * to a domain) and perform all the chown()-s on the list. If @pid is -1
 * then the transaction is performed in the namespace of the caller.
 *
 * Note that the transaction is also freed, therefore new one has to be
 * started after successful return from this function. Also it is
 * considered as error if there's no transaction set and this function
 * is called.
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
 *
 * Returns: 0 on success,
 *         -1 otherwise.
 */
static int
virSecurityDACTransactionCommit(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                pid_t pid)
{
    virSecurityDACChownListPtr list;
    int ret = -1;

    list = virThreadLocalGet(&chownList);
    if (!list) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("No transaction is set"));
        goto cleanup;
    }

    if (virThreadLocalSet(&chownList, NULL) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to clear thread local variable"));
        goto cleanup;
    }

565 566 567 568 569 570
    if ((pid == -1 &&
         virSecurityDACTransactionRun(pid, list) < 0) ||
        (pid != -1 &&
         virProcessRunInMountNamespace(pid,
                                       virSecurityDACTransactionRun,
                                       list) < 0))
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
        goto cleanup;

    ret = 0;
 cleanup:
    virSecurityDACChownListFree(list);
    return ret;
}

/**
 * virSecurityDACTransactionAbort:
 * @mgr: security manager
 *
 * Cancels and frees any out standing transaction.
 */
static void
virSecurityDACTransactionAbort(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED)
{
    virSecurityDACChownListPtr list;

    list = virThreadLocalGet(&chownList);
    if (!list)
        return;

    if (virThreadLocalSet(&chownList, NULL) < 0)
        VIR_DEBUG("Unable to clear thread local variable");
    virSecurityDACChownListFree(list);
}


600
static int
601 602
virSecurityDACSetOwnershipInternal(const virSecurityDACData *priv,
                                   const virStorageSource *src,
603 604 605
                                   const char *path,
                                   uid_t uid,
                                   gid_t gid)
606
{
607 608
    int rc;

609 610 611
    /* Be aware that this function might run in a separate process.
     * Therefore, any driver state changes would be thrown away. */

612 613 614 615 616 617 618 619 620
    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 {
621 622
        struct stat sb;

623 624 625 626 627 628 629 630 631 632
        if (!path) {
            if (!src || !src->path)
                return 0;

            if (!virStorageSourceIsLocalStorage(src))
                return 0;

            path = src->path;
        }

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

638 639 640
        if (sb.st_uid == uid && sb.st_gid == gid) {
            /* nothing to chown */
            return 0;
641
        }
642 643

        rc = chown(path, uid, gid);
644
    }
645

646
    if (rc < 0) {
647
        if (errno == EOPNOTSUPP || errno == EINVAL) {
648 649
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "supported by filesystem",
650
                     (long)uid, (long)gid, path);
651
        } else if (errno == EPERM) {
652 653
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "permitted",
654
                     (long)uid, (long)gid, path);
655
        } else if (errno == EROFS) {
656 657
            VIR_INFO("Setting user and group to '%ld:%ld' on '%s' not "
                     "possible on readonly filesystem",
658
                     (long)uid, (long)gid, path);
659
        } else {
660
            virReportSystemError(errno,
661 662
                                 _("unable to set user and group to '%ld:%ld' "
                                   "on '%s'"),
663
                                 (long)uid, (long)gid, path);
664 665 666 667 668 669
            return -1;
        }
    }
    return 0;
}

670

671
static int
672
virSecurityDACSetOwnership(virSecurityManagerPtr mgr,
673
                           const virStorageSource *src,
674 675 676
                           const char *path,
                           uid_t uid,
                           gid_t gid)
677
{
678
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
679
    struct stat sb;
680
    int rc;
681 682 683 684 685

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

686 687 688 689 690 691 692 693
    /* Be aware that this function might run in a separate process.
     * Therefore, any driver state changes would be thrown away. */

    if ((rc = virSecurityDACTransactionAppend(path, src, uid, gid, false)) < 0)
        return -1;
    else if (rc > 0)
        return 0;

694 695 696 697 698 699 700 701 702 703
    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;
    }

704 705 706
    VIR_INFO("Setting DAC user and group on '%s' to '%ld:%ld'",
             NULLSTR(src ? src->path : path), (long)uid, (long)gid);

707
    return virSecurityDACSetOwnershipInternal(priv, src, path, uid, gid);
708 709 710 711
}


static int
712
virSecurityDACRestoreFileLabelInternal(virSecurityManagerPtr mgr,
713
                                       const virStorageSource *src,
714
                                       const char *path)
715
{
716
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
717 718 719 720 721 722 723 724
    int rv;
    uid_t uid = 0;  /* By default return to root:root */
    gid_t gid = 0;

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

725 726 727 728 729 730 731 732
    /* Be aware that this function might run in a separate process.
     * Therefore, any driver state changes would be thrown away. */

    if ((rv = virSecurityDACTransactionAppend(path, src, uid, gid, true)) < 0)
        return -1;
    else if (rv > 0)
        return 0;

733 734 735 736 737 738 739 740
    if (path) {
        rv = virSecurityDACRecallLabel(priv, path, &uid, &gid);
        if (rv < 0)
            return -1;
        if (rv > 0)
            return 0;
    }

741 742 743
    VIR_INFO("Restoring DAC user and group on '%s' to %ld:%ld",
             NULLSTR(src ? src->path : path), (long)uid, (long)gid);

744
    return virSecurityDACSetOwnershipInternal(priv, src, path, uid, gid);
745 746 747 748
}


static int
749
virSecurityDACRestoreFileLabel(virSecurityManagerPtr mgr,
750
                               const char *path)
751
{
752
    return virSecurityDACRestoreFileLabelInternal(mgr, NULL, path);
753 754 755 756
}


static int
757 758 759 760
virSecurityDACSetImageLabelInternal(virSecurityManagerPtr mgr,
                                    virDomainDefPtr def,
                                    virStorageSourcePtr src,
                                    virStorageSourcePtr parent)
761
{
762
    virSecurityLabelDefPtr secdef;
763
    virSecurityDeviceLabelDefPtr disk_seclabel;
764
    virSecurityDeviceLabelDefPtr parent_seclabel = NULL;
765
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
766 767 768
    uid_t user;
    gid_t group;

769 770 771 772
    if (!priv->dynamicOwnership)
        return 0;

    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
773
    if (secdef && !secdef->relabel)
774
        return 0;
775

776 777 778 779 780 781 782 783
    disk_seclabel = virStorageSourceGetSecurityLabelDef(src, SECURITY_DAC_NAME);
    if (parent)
        parent_seclabel = virStorageSourceGetSecurityLabelDef(parent,
                                                              SECURITY_DAC_NAME);

    if (disk_seclabel && (!disk_seclabel->relabel || disk_seclabel->label)) {
        if (!disk_seclabel->relabel)
            return 0;
784 785 786

        if (virParseOwnershipIds(disk_seclabel->label, &user, &group) < 0)
            return -1;
787 788 789 790 791 792 793
    } else if (parent_seclabel &&
               (!parent_seclabel->relabel || parent_seclabel->label)) {
        if (!parent_seclabel->relabel)
            return 0;

        if (virParseOwnershipIds(parent_seclabel->label, &user, &group) < 0)
            return -1;
794 795 796 797
    } else {
        if (virSecurityDACGetImageIds(secdef, priv, &user, &group))
            return -1;
    }
798

799
    return virSecurityDACSetOwnership(mgr, src, NULL, user, group);
800 801 802
}


803 804 805 806 807 808 809 810
static int
virSecurityDACSetImageLabel(virSecurityManagerPtr mgr,
                            virDomainDefPtr def,
                            virStorageSourcePtr src)
{
    return virSecurityDACSetImageLabelInternal(mgr, def, src, NULL);
}

811
static int
812 813 814
virSecurityDACSetDiskLabel(virSecurityManagerPtr mgr,
                           virDomainDefPtr def,
                           virDomainDiskDefPtr disk)
815 816

{
817
    virStorageSourcePtr next;
818

819
    for (next = disk->src; virStorageSourceIsBacking(next); next = next->backingStore) {
820
        if (virSecurityDACSetImageLabelInternal(mgr, def, next, disk->src) < 0)
821 822
            return -1;
    }
823

824
    return 0;
825 826 827 828
}


static int
829 830 831 832
virSecurityDACRestoreImageLabelInt(virSecurityManagerPtr mgr,
                                   virDomainDefPtr def,
                                   virStorageSourcePtr src,
                                   bool migrated)
833 834
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
835 836
    virSecurityLabelDefPtr secdef;
    virSecurityDeviceLabelDefPtr disk_seclabel;
837 838 839 840

    if (!priv->dynamicOwnership)
        return 0;

841 842 843 844 845 846 847
    /* 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;
848

849
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
850
    if (secdef && !secdef->relabel)
851 852
        return 0;

853
    disk_seclabel = virStorageSourceGetSecurityLabelDef(src,
854
                                                        SECURITY_DAC_NAME);
855
    if (disk_seclabel && !disk_seclabel->relabel)
856 857
        return 0;

858 859 860
    /* 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 :-) */
861
    if (migrated) {
862 863 864 865 866 867 868 869 870 871
        int rc = 1;

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

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

872 873
        if (rc == 1) {
            VIR_DEBUG("Skipping image label restore on %s because FS is shared",
874
                      src->path);
875 876 877 878
            return 0;
        }
    }

879
    return virSecurityDACRestoreFileLabelInternal(mgr, src, NULL);
880 881 882 883
}


static int
884 885 886
virSecurityDACRestoreImageLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr def,
                                virStorageSourcePtr src)
887
{
888
    return virSecurityDACRestoreImageLabelInt(mgr, def, src, false);
889 890 891 892
}


static int
893 894 895
virSecurityDACRestoreDiskLabel(virSecurityManagerPtr mgr,
                               virDomainDefPtr def,
                               virDomainDiskDefPtr disk)
896
{
897
    return virSecurityDACRestoreImageLabelInt(mgr, def, disk->src, false);
898 899 900 901
}


static int
902 903
virSecurityDACSetHostdevLabelHelper(const char *file,
                                    void *opaque)
904
{
905 906 907
    virSecurityDACCallbackDataPtr cbdata = opaque;
    virSecurityManagerPtr mgr = cbdata->manager;
    virSecurityLabelDefPtr secdef = cbdata->secdef;
908
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
909 910
    uid_t user;
    gid_t group;
911

912
    if (virSecurityDACGetIds(secdef, priv, &user, &group, NULL, NULL) < 0)
913 914
        return -1;

915
    return virSecurityDACSetOwnership(mgr, NULL, file, user, group);
916 917 918
}


919
static int
920 921 922
virSecurityDACSetPCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
                          const char *file,
                          void *opaque)
923
{
924
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
925 926 927
}


928
static int
929 930 931
virSecurityDACSetUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                          const char *file,
                          void *opaque)
932
{
933
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
934
}
935

936

937
static int
938 939 940
virSecurityDACSetSCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                           const char *file,
                           void *opaque)
941
{
942
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
943 944 945
}


946 947 948 949 950 951 952 953 954
static int
virSecurityDACSetHostLabel(virSCSIVHostDevicePtr dev ATTRIBUTE_UNUSED,
                           const char *file,
                           void *opaque)
{
    return virSecurityDACSetHostdevLabelHelper(file, opaque);
}


955
static int
956 957 958 959
virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              virDomainHostdevDefPtr dev,
                              const char *vroot)
960 961
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
962
    virSecurityDACCallbackData cbdata;
963
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
964
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
965
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
966
    virDomainHostdevSubsysSCSIVHostPtr hostsrc = &dev->source.subsys.u.scsi_host;
967
    virDomainHostdevSubsysMediatedDevPtr mdevsrc = &dev->source.subsys.u.mdev;
968 969 970 971 972 973 974 975
    int ret = -1;

    if (!priv->dynamicOwnership)
        return 0;

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

976
    /* Like virSecurityDACSetImageLabel() for a networked disk,
977 978
     * do nothing for an iSCSI hostdev
     */
979 980
    if (dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
        scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
981 982
        return 0;

983 984 985
    cbdata.manager = mgr;
    cbdata.secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

986
    if (cbdata.secdef && !cbdata.secdef->relabel)
987 988
        return 0;

989
    switch ((virDomainHostdevSubsysType)dev->source.subsys.type) {
990
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
991
        virUSBDevicePtr usb;
992

993 994 995
        if (dev->missing)
            return 0;

996
        if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot)))
997 998
            goto done;

999
        ret = virUSBDeviceFileIterate(usb,
1000
                                      virSecurityDACSetUSBLabel,
1001
                                      &cbdata);
1002
        virUSBDeviceFree(usb);
1003 1004 1005 1006
        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
1007
        virPCIDevicePtr pci =
1008 1009
            virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
                            pcisrc->addr.slot, pcisrc->addr.function);
1010 1011 1012 1013

        if (!pci)
            goto done;

1014
        if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
1015
            char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
1016

1017 1018
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
1019
                goto done;
1020
            }
1021
            ret = virSecurityDACSetPCILabel(pci, vfioGroupDev, &cbdata);
1022 1023
            VIR_FREE(vfioGroupDev);
        } else {
1024
            ret = virPCIDeviceFileIterate(pci,
1025
                                          virSecurityDACSetPCILabel,
1026
                                          &cbdata);
1027 1028
        }

1029
        virPCIDeviceFree(pci);
1030 1031 1032
        break;
    }

1033
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
1034
        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
1035
        virSCSIDevicePtr scsi =
1036
            virSCSIDeviceNew(NULL,
1037 1038
                             scsihostsrc->adapter, scsihostsrc->bus,
                             scsihostsrc->target, scsihostsrc->unit,
1039
                             dev->readonly, dev->shareable);
1040 1041 1042 1043

        if (!scsi)
            goto done;

1044
        ret = virSCSIDeviceFileIterate(scsi,
1045
                                       virSecurityDACSetSCSILabel,
1046
                                       &cbdata);
1047 1048 1049 1050 1051
        virSCSIDeviceFree(scsi);

        break;
    }

1052
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
        virSCSIVHostDevicePtr host = virSCSIVHostDeviceNew(hostsrc->wwpn);

        if (!host)
            goto done;

        ret = virSCSIVHostDeviceFileIterate(host,
                                            virSecurityDACSetHostLabel,
                                            &cbdata);
        virSCSIVHostDeviceFree(host);
        break;
1063 1064
    }

1065 1066 1067
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
        char *vfiodev = NULL;

1068
        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
1069 1070 1071 1072 1073 1074 1075 1076
            goto done;

        ret = virSecurityDACSetHostdevLabelHelper(vfiodev, &cbdata);

        VIR_FREE(vfiodev);
        break;
    }

1077
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
1078 1079 1080 1081
        ret = 0;
        break;
    }

1082
 done:
1083 1084 1085 1086 1087
    return ret;
}


static int
1088 1089 1090
virSecurityDACRestorePCILabel(virPCIDevicePtr dev ATTRIBUTE_UNUSED,
                              const char *file,
                              void *opaque)
1091
{
1092
    virSecurityManagerPtr mgr = opaque;
1093
    return virSecurityDACRestoreFileLabel(mgr, file);
1094 1095 1096 1097
}


static int
1098 1099 1100
virSecurityDACRestoreUSBLabel(virUSBDevicePtr dev ATTRIBUTE_UNUSED,
                              const char *file,
                              void *opaque)
1101
{
1102
    virSecurityManagerPtr mgr = opaque;
1103
    return virSecurityDACRestoreFileLabel(mgr, file);
1104 1105 1106
}


1107
static int
1108 1109 1110
virSecurityDACRestoreSCSILabel(virSCSIDevicePtr dev ATTRIBUTE_UNUSED,
                               const char *file,
                               void *opaque)
1111
{
1112
    virSecurityManagerPtr mgr = opaque;
1113
    return virSecurityDACRestoreFileLabel(mgr, file);
1114 1115 1116
}


1117 1118 1119 1120 1121 1122
static int
virSecurityDACRestoreHostLabel(virSCSIVHostDevicePtr dev ATTRIBUTE_UNUSED,
                               const char *file,
                               void *opaque)
{
    virSecurityManagerPtr mgr = opaque;
1123
    return virSecurityDACRestoreFileLabel(mgr, file);
1124 1125 1126
}


1127
static int
1128 1129 1130 1131
virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainHostdevDefPtr dev,
                                  const char *vroot)
1132 1133 1134

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1135
    virSecurityLabelDefPtr secdef;
1136
    virDomainHostdevSubsysUSBPtr usbsrc = &dev->source.subsys.u.usb;
1137
    virDomainHostdevSubsysPCIPtr pcisrc = &dev->source.subsys.u.pci;
1138
    virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
1139
    virDomainHostdevSubsysSCSIVHostPtr hostsrc = &dev->source.subsys.u.scsi_host;
1140
    virDomainHostdevSubsysMediatedDevPtr mdevsrc = &dev->source.subsys.u.mdev;
1141 1142
    int ret = -1;

1143 1144
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1145
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1146 1147 1148 1149 1150
        return 0;

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

1151
    /* Like virSecurityDACRestoreImageLabelInt() for a networked disk,
1152 1153
     * do nothing for an iSCSI hostdev
     */
1154 1155
    if (dev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI &&
        scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
1156 1157
        return 0;

1158
    switch ((virDomainHostdevSubsysType)dev->source.subsys.type) {
1159
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
1160
        virUSBDevicePtr usb;
1161 1162 1163

        if (dev->missing)
            return 0;
1164

1165
        if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, vroot)))
1166 1167
            goto done;

1168
        ret = virUSBDeviceFileIterate(usb, virSecurityDACRestoreUSBLabel, mgr);
1169
        virUSBDeviceFree(usb);
1170 1171 1172 1173 1174

        break;
    }

    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
1175
        virPCIDevicePtr pci =
1176 1177
            virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
                            pcisrc->addr.slot, pcisrc->addr.function);
1178 1179 1180 1181

        if (!pci)
            goto done;

1182
        if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
1183
            char *vfioGroupDev = virPCIDeviceGetIOMMUGroupDev(pci);
1184

1185 1186
            if (!vfioGroupDev) {
                virPCIDeviceFree(pci);
1187
                goto done;
1188
            }
1189
            ret = virSecurityDACRestorePCILabel(pci, vfioGroupDev, mgr);
1190 1191
            VIR_FREE(vfioGroupDev);
        } else {
1192
            ret = virPCIDeviceFileIterate(pci, virSecurityDACRestorePCILabel, mgr);
1193
        }
1194
        virPCIDeviceFree(pci);
1195 1196 1197
        break;
    }

1198
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
1199
        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
1200
        virSCSIDevicePtr scsi =
1201
            virSCSIDeviceNew(NULL,
1202 1203
                             scsihostsrc->adapter, scsihostsrc->bus,
                             scsihostsrc->target, scsihostsrc->unit,
1204
                             dev->readonly, dev->shareable);
1205 1206 1207 1208

        if (!scsi)
            goto done;

1209
        ret = virSCSIDeviceFileIterate(scsi, virSecurityDACRestoreSCSILabel, mgr);
1210 1211 1212 1213 1214
        virSCSIDeviceFree(scsi);

        break;
    }

1215
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: {
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        virSCSIVHostDevicePtr host = virSCSIVHostDeviceNew(hostsrc->wwpn);

        if (!host)
            goto done;

        ret = virSCSIVHostDeviceFileIterate(host,
                                            virSecurityDACRestoreHostLabel,
                                            mgr);
        virSCSIVHostDeviceFree(host);

        break;
1227 1228
    }

1229 1230 1231
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
        char *vfiodev = NULL;

1232
        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
1233 1234
            goto done;

1235
        ret = virSecurityDACRestoreFileLabel(mgr, vfiodev);
1236 1237 1238 1239
        VIR_FREE(vfiodev);
        break;
    }

1240
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
1241 1242 1243 1244
        ret = 0;
        break;
    }

1245
 done:
1246 1247 1248 1249 1250 1251
    return ret;
}


static int
virSecurityDACSetChardevLabel(virSecurityManagerPtr mgr,
1252
                              virDomainDefPtr def,
1253 1254
                              virDomainChrSourceDefPtr dev_source,
                              bool chardevStdioLogd)
1255 1256 1257

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1258
    virSecurityLabelDefPtr seclabel;
1259
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
1260 1261
    char *in = NULL, *out = NULL;
    int ret = -1;
1262 1263 1264
    uid_t user;
    gid_t group;

1265 1266
    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1267 1268
    chr_seclabel = virDomainChrSourceDefGetSecurityLabelDef(dev_source,
                                                            SECURITY_DAC_NAME);
1269

1270
    if (chr_seclabel && !chr_seclabel->relabel)
1271 1272
        return 0;

1273 1274 1275
    if (!chr_seclabel &&
        dev_source->type == VIR_DOMAIN_CHR_TYPE_FILE &&
        chardevStdioLogd)
1276 1277
        return 0;

1278 1279 1280 1281 1282 1283 1284
    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;
    }
1285

1286
    switch ((virDomainChrType)dev_source->type) {
1287 1288
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
1289
        ret = virSecurityDACSetOwnership(mgr, NULL,
1290
                                         dev_source->data.file.path,
1291
                                         user, group);
1292 1293 1294
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
1295 1296
        if (virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0 ||
            virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0)
1297 1298
            goto done;
        if (virFileExists(in) && virFileExists(out)) {
1299 1300
            if (virSecurityDACSetOwnership(mgr, NULL, in, user, group) < 0 ||
                virSecurityDACSetOwnership(mgr, NULL, out, user, group) < 0)
1301
                goto done;
1302
        } else if (virSecurityDACSetOwnership(mgr, NULL,
1303
                                              dev_source->data.file.path,
1304
                                              user, group) < 0) {
1305
            goto done;
1306 1307 1308 1309
        }
        ret = 0;
        break;

1310 1311
    case VIR_DOMAIN_CHR_TYPE_UNIX:
        if (!dev_source->data.nix.listen) {
1312
            if (virSecurityDACSetOwnership(mgr, NULL,
1313
                                           dev_source->data.nix.path,
1314 1315 1316 1317 1318 1319
                                           user, group) < 0)
                goto done;
        }
        ret = 0;
        break;

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    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:
1330 1331 1332 1333
        ret = 0;
        break;
    }

1334
 done:
1335 1336 1337 1338 1339 1340
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}

static int
1341
virSecurityDACRestoreChardevLabel(virSecurityManagerPtr mgr,
1342
                                  virDomainDefPtr def ATTRIBUTE_UNUSED,
1343 1344
                                  virDomainChrSourceDefPtr dev_source,
                                  bool chardevStdioLogd)
1345
{
1346
    virSecurityDeviceLabelDefPtr chr_seclabel = NULL;
1347 1348 1349
    char *in = NULL, *out = NULL;
    int ret = -1;

1350 1351
    chr_seclabel = virDomainChrSourceDefGetSecurityLabelDef(dev_source,
                                                            SECURITY_DAC_NAME);
1352

1353
    if (chr_seclabel && !chr_seclabel->relabel)
1354 1355
        return 0;

1356 1357 1358
    if (!chr_seclabel &&
        dev_source->type == VIR_DOMAIN_CHR_TYPE_FILE &&
        chardevStdioLogd)
1359 1360
        return 0;

1361
    switch ((virDomainChrType)dev_source->type) {
1362 1363
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
1364
        ret = virSecurityDACRestoreFileLabel(mgr, dev_source->data.file.path);
1365 1366 1367
        break;

    case VIR_DOMAIN_CHR_TYPE_PIPE:
J
Jiri Denemark 已提交
1368 1369
        if (virAsprintf(&out, "%s.out", dev_source->data.file.path) < 0 ||
            virAsprintf(&in, "%s.in", dev_source->data.file.path) < 0)
1370
            goto done;
1371
        if (virFileExists(in) && virFileExists(out)) {
1372 1373
            if (virSecurityDACRestoreFileLabel(mgr, out) < 0 ||
                virSecurityDACRestoreFileLabel(mgr, in) < 0)
J
Jim Fehlig 已提交
1374
                goto done;
1375
        } else if (virSecurityDACRestoreFileLabel(mgr, dev_source->data.file.path) < 0) {
1376 1377
            goto done;
        }
1378 1379 1380
        ret = 0;
        break;

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    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:
1392 1393 1394 1395
        ret = 0;
        break;
    }

1396
 done:
1397 1398 1399 1400 1401 1402
    VIR_FREE(in);
    VIR_FREE(out);
    return ret;
}


1403 1404 1405 1406 1407 1408
struct _virSecuritySELinuxChardevCallbackData {
    virSecurityManagerPtr mgr;
    bool chardevStdioLogd;
};


1409
static int
1410
virSecurityDACRestoreChardevCallback(virDomainDefPtr def,
1411
                                     virDomainChrDefPtr dev ATTRIBUTE_UNUSED,
1412 1413
                                     void *opaque)
{
1414
    struct _virSecuritySELinuxChardevCallbackData *data = opaque;
1415

1416 1417
    return virSecurityDACRestoreChardevLabel(data->mgr, def, dev->source,
                                             data->chardevStdioLogd);
1418 1419 1420
}


1421
static int
1422 1423 1424
virSecurityDACSetTPMFileLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
                              virDomainTPMDefPtr tpm)
1425 1426 1427 1428 1429
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
1430
        ret = virSecurityDACSetChardevLabel(mgr, def,
1431 1432
                                            &tpm->data.passthrough.source,
                                            false);
1433
        break;
1434
    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
1435 1436 1437 1438
        ret = virSecurityDACSetChardevLabel(mgr, def,
                                            &tpm->data.emulator.source,
                                            false);
        break;
1439 1440 1441 1442 1443 1444 1445 1446 1447
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


static int
1448 1449 1450
virSecurityDACRestoreTPMFileLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr def,
                                  virDomainTPMDefPtr tpm)
1451 1452 1453 1454 1455
{
    int ret = 0;

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
1456
        ret = virSecurityDACRestoreChardevLabel(mgr, def,
1457 1458
                                                &tpm->data.passthrough.source,
                                                false);
1459
        break;
1460
    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
1461
        /* swtpm will have removed the Unix socket upon termination */
1462 1463 1464 1465 1466 1467 1468 1469
    case VIR_DOMAIN_TPM_TYPE_LAST:
        break;
    }

    return ret;
}


1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
static int
virSecurityDACSetGraphicsLabel(virSecurityManagerPtr mgr,
                               virDomainDefPtr def,
                               virDomainGraphicsDefPtr gfx)

{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr seclabel;
    uid_t user;
    gid_t group;

    /* Skip chowning the shared render file if namespaces are disabled */
    if (!priv->mountNamespace)
        return 0;

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

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

    if (gfx->type == VIR_DOMAIN_GRAPHICS_TYPE_SPICE &&
        gfx->data.spice.gl == VIR_TRISTATE_BOOL_YES &&
        gfx->data.spice.rendernode) {
1495
        if (virSecurityDACSetOwnership(mgr, NULL,
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
                                       gfx->data.spice.rendernode,
                                       user, group) < 0)
            return -1;
    }

    return 0;
}


static int
virSecurityDACRestoreGraphicsLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                               virDomainDefPtr def ATTRIBUTE_UNUSED,
                               virDomainGraphicsDefPtr gfx ATTRIBUTE_UNUSED)

{
    /* The only graphics labelling we do is dependent on mountNamespaces,
       in which case 'restoring' the label doesn't actually accomplish
       anything, so there's nothing to do here */
    return 0;
}


1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
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;

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

1539
        ret = virSecurityDACSetOwnership(mgr, NULL, input->source.evdev, user, group);
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
        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)
{
    int ret = -1;

1560
    switch ((virDomainInputType)input->type) {
1561
    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
1562
        ret = virSecurityDACRestoreFileLabel(mgr, input->source.evdev);
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
        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;
}


1577 1578 1579 1580 1581 1582 1583 1584 1585
static int
virSecurityDACRestoreMemoryLabel(virSecurityManagerPtr mgr,
                                 virDomainDefPtr def ATTRIBUTE_UNUSED,
                                 virDomainMemoryDefPtr mem)
{
    int ret = -1;

    switch ((virDomainMemoryModel) mem->model) {
    case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
1586
        ret = virSecurityDACRestoreFileLabel(mgr, mem->nvdimmPath);
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
        break;

    case VIR_DOMAIN_MEMORY_MODEL_DIMM:
    case VIR_DOMAIN_MEMORY_MODEL_LAST:
    case VIR_DOMAIN_MEMORY_MODEL_NONE:
        ret = 0;
        break;
    }

    return ret;
}


1600
static int
1601 1602
virSecurityDACRestoreAllLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr def,
1603 1604
                              bool migrated,
                              bool chardevStdioLogd)
1605 1606
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1607
    virSecurityLabelDefPtr secdef;
1608
    size_t i;
1609 1610
    int rc = 0;

1611
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1612

1613
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1614
        return 0;
1615 1616

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

1619
    for (i = 0; i < def->nhostdevs; i++) {
1620 1621 1622 1623
        if (virSecurityDACRestoreHostdevLabel(mgr,
                                              def,
                                              def->hostdevs[i],
                                              NULL) < 0)
1624 1625
            rc = -1;
    }
1626

1627 1628 1629 1630 1631
    for (i = 0; i < def->ngraphics; i++) {
        if (virSecurityDACRestoreGraphicsLabel(mgr, def, def->graphics[i]) < 0)
            return -1;
    }

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

1637
    for (i = 0; i < def->ndisks; i++) {
1638 1639 1640 1641
        if (virSecurityDACRestoreImageLabelInt(mgr,
                                               def,
                                               def->disks[i]->src,
                                               migrated) < 0)
1642 1643 1644
            rc = -1;
    }

1645 1646 1647 1648 1649 1650 1651
    for (i = 0; i < def->nmems; i++) {
        if (virSecurityDACRestoreMemoryLabel(mgr,
                                             def,
                                             def->mems[i]) < 0)
            rc = -1;
    }

1652 1653 1654 1655 1656
    struct _virSecuritySELinuxChardevCallbackData chardevData = {
        .mgr = mgr,
        .chardevStdioLogd = chardevStdioLogd,
    };

1657
    if (virDomainChrDefForeach(def,
1658 1659
                               false,
                               virSecurityDACRestoreChardevCallback,
1660
                               &chardevData) < 0)
1661 1662
        rc = -1;

1663
    if (def->tpm) {
1664 1665 1666
        if (virSecurityDACRestoreTPMFileLabel(mgr,
                                              def,
                                              def->tpm) < 0)
1667 1668 1669
            rc = -1;
    }

1670
    if (def->os.loader && def->os.loader->nvram &&
1671
        virSecurityDACRestoreFileLabel(mgr, def->os.loader->nvram) < 0)
1672 1673
        rc = -1;

1674 1675 1676 1677 1678
    return rc;
}


static int
1679
virSecurityDACSetChardevCallback(virDomainDefPtr def,
1680
                                 virDomainChrDefPtr dev ATTRIBUTE_UNUSED,
1681 1682
                                 void *opaque)
{
1683
    struct _virSecuritySELinuxChardevCallbackData *data = opaque;
1684

1685 1686
    return virSecurityDACSetChardevLabel(data->mgr, def, dev->source,
                                         data->chardevStdioLogd);
1687 1688 1689
}


1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
static int
virSecurityDACSetMemoryLabel(virSecurityManagerPtr mgr,
                             virDomainDefPtr def,
                             virDomainMemoryDefPtr mem)

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

    switch ((virDomainMemoryModel) mem->model) {
    case VIR_DOMAIN_MEMORY_MODEL_NVDIMM:
        seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
        if (seclabel && !seclabel->relabel)
            return 0;

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

1711
        ret = virSecurityDACSetOwnership(mgr, NULL, mem->nvdimmPath, user, group);
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
        break;

    case VIR_DOMAIN_MEMORY_MODEL_DIMM:
    case VIR_DOMAIN_MEMORY_MODEL_LAST:
    case VIR_DOMAIN_MEMORY_MODEL_NONE:
        ret = 0;
        break;
    }

    return ret;
}


1725
static int
1726 1727
virSecurityDACSetAllLabel(virSecurityManagerPtr mgr,
                          virDomainDefPtr def,
1728 1729
                          const char *stdin_path ATTRIBUTE_UNUSED,
                          bool chardevStdioLogd)
1730 1731
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
1732
    virSecurityLabelDefPtr secdef;
1733
    size_t i;
1734 1735
    uid_t user;
    gid_t group;
1736

1737 1738
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1739
    if (!priv->dynamicOwnership || (secdef && !secdef->relabel))
1740 1741
        return 0;

1742
    for (i = 0; i < def->ndisks; i++) {
1743
        /* XXX fixme - we need to recursively label the entire tree :-( */
E
Eric Blake 已提交
1744
        if (virDomainDiskGetType(def->disks[i]) == VIR_STORAGE_TYPE_DIR)
1745
            continue;
1746 1747 1748
        if (virSecurityDACSetDiskLabel(mgr,
                                       def,
                                       def->disks[i]) < 0)
1749 1750
            return -1;
    }
1751

1752 1753 1754 1755 1756
    for (i = 0; i < def->ngraphics; i++) {
        if (virSecurityDACSetGraphicsLabel(mgr, def, def->graphics[i]) < 0)
            return -1;
    }

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

1762
    for (i = 0; i < def->nhostdevs; i++) {
1763 1764 1765 1766
        if (virSecurityDACSetHostdevLabel(mgr,
                                          def,
                                          def->hostdevs[i],
                                          NULL) < 0)
1767 1768 1769
            return -1;
    }

1770 1771 1772 1773 1774 1775 1776
    for (i = 0; i < def->nmems; i++) {
        if (virSecurityDACSetMemoryLabel(mgr,
                                         def,
                                         def->mems[i]) < 0)
            return -1;
    }

1777 1778 1779 1780 1781
    struct _virSecuritySELinuxChardevCallbackData chardevData = {
        .mgr = mgr,
        .chardevStdioLogd = chardevStdioLogd,
    };

1782
    if (virDomainChrDefForeach(def,
1783 1784
                               true,
                               virSecurityDACSetChardevCallback,
1785
                               &chardevData) < 0)
1786 1787
        return -1;

1788
    if (def->tpm) {
1789 1790 1791
        if (virSecurityDACSetTPMFileLabel(mgr,
                                          def,
                                          def->tpm) < 0)
1792 1793 1794
            return -1;
    }

1795
    if (virSecurityDACGetImageIds(secdef, priv, &user, &group))
1796 1797
        return -1;

1798
    if (def->os.loader && def->os.loader->nvram &&
1799
        virSecurityDACSetOwnership(mgr, NULL,
1800
                                   def->os.loader->nvram, user, group) < 0)
1801 1802
        return -1;

1803
    if (def->os.kernel &&
1804
        virSecurityDACSetOwnership(mgr, NULL,
1805
                                   def->os.kernel, user, group) < 0)
1806 1807
        return -1;

1808
    if (def->os.initrd &&
1809
        virSecurityDACSetOwnership(mgr, NULL,
1810
                                   def->os.initrd, user, group) < 0)
1811 1812
        return -1;

O
Olivia Yin 已提交
1813
    if (def->os.dtb &&
1814
        virSecurityDACSetOwnership(mgr, NULL,
1815
                                   def->os.dtb, user, group) < 0)
O
Olivia Yin 已提交
1816 1817
        return -1;

J
Ján Tomko 已提交
1818
    if (def->os.slic_table &&
1819
        virSecurityDACSetOwnership(mgr, NULL,
J
Ján Tomko 已提交
1820 1821 1822
                                   def->os.slic_table, user, group) < 0)
        return -1;

1823 1824 1825 1826 1827 1828
    return 0;
}


static int
virSecurityDACSetSavedStateLabel(virSecurityManagerPtr mgr,
1829
                                 virDomainDefPtr def,
1830 1831
                                 const char *savefile)
{
1832 1833
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1834 1835
    uid_t user;
    gid_t group;
1836

1837 1838 1839
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

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

1842
    return virSecurityDACSetOwnership(mgr, NULL, savefile, user, group);
1843 1844 1845 1846 1847
}


static int
virSecurityDACRestoreSavedStateLabel(virSecurityManagerPtr mgr,
1848
                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
1849 1850 1851 1852 1853 1854 1855
                                     const char *savefile)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    if (!priv->dynamicOwnership)
        return 0;

1856
    return virSecurityDACRestoreFileLabel(mgr, savefile);
1857 1858 1859 1860 1861
}


static int
virSecurityDACSetProcessLabel(virSecurityManagerPtr mgr,
1862
                              virDomainDefPtr def)
1863
{
1864 1865
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1866 1867
    uid_t user;
    gid_t group;
1868 1869
    gid_t *groups;
    int ngroups;
1870

1871 1872 1873
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

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

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

1879
    if (virSetUIDGID(user, group, groups, ngroups) < 0)
1880 1881 1882
        return -1;

    return 0;
1883 1884 1885
}


1886 1887
static int
virSecurityDACSetChildProcessLabel(virSecurityManagerPtr mgr,
1888
                                   virDomainDefPtr def,
1889 1890
                                   virCommandPtr cmd)
{
1891 1892
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityLabelDefPtr secdef;
1893 1894 1895
    uid_t user;
    gid_t group;

1896 1897
    secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

1898
    if (virSecurityDACGetIds(secdef, priv, &user, &group, NULL, NULL) < 0)
1899 1900
        return -1;

J
Jiri Denemark 已提交
1901
    VIR_DEBUG("Setting child to drop privileges to %u:%u",
1902
              (unsigned int)user, (unsigned int)group);
1903 1904 1905 1906 1907 1908 1909

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


1910 1911 1912 1913 1914 1915 1916 1917
static int
virSecurityDACVerify(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                     virDomainDefPtr def ATTRIBUTE_UNUSED)
{
    return 0;
}

static int
1918 1919
virSecurityDACGenLabel(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
1920
{
1921 1922 1923 1924 1925
    int rc = -1;
    virSecurityLabelDefPtr seclabel;
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);

    seclabel = virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);
1926
    if (seclabel == NULL)
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
        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;
    }

1945
    switch ((virDomainSeclabelType)seclabel->type) {
1946 1947 1948 1949 1950 1951 1952 1953 1954
    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:
1955
        if (virAsprintf(&seclabel->label, "+%u:+%u",
1956 1957
                        (unsigned int)priv->user,
                        (unsigned int)priv->group) < 0)
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
            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 */
1968
        return 0;
1969 1970
    case VIR_DOMAIN_SECLABEL_DEFAULT:
    case VIR_DOMAIN_SECLABEL_LAST:
1971 1972 1973 1974 1975 1976
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected security label type '%s'"),
                       virDomainSeclabelTypeToString(seclabel->type));
        return rc;
    }

1977
    if (seclabel->relabel && !seclabel->imagelabel &&
1978 1979 1980
        VIR_STRDUP(seclabel->imagelabel, seclabel->label) < 0) {
        VIR_FREE(seclabel->label);
        return rc;
1981 1982
    }

1983 1984 1985 1986 1987
    return 0;
}

static int
virSecurityDACReleaseLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1988
                           virDomainDefPtr def ATTRIBUTE_UNUSED)
1989 1990 1991 1992 1993 1994
{
    return 0;
}

static int
virSecurityDACReserveLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
1995 1996
                           virDomainDefPtr def ATTRIBUTE_UNUSED,
                           pid_t pid ATTRIBUTE_UNUSED)
1997 1998 1999 2000
{
    return 0;
}

2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
#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);

2012
    if (virAsprintf(&path, "/proc/%d", (int)pid) < 0)
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
        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,
2023
             "+%u:+%u", (unsigned int)sb.st_uid, (unsigned int)sb.st_gid);
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
    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,
2052
             "+%u:+%u", (unsigned int)p.ki_uid, (unsigned int)p.ki_groups[0]);
2053 2054 2055 2056 2057

    return 0;
}
#else
static int
J
Ján Tomko 已提交
2058 2059
virSecurityDACGetProcessLabelInternal(pid_t pid ATTRIBUTE_UNUSED,
                                      virSecurityLabelPtr seclabel ATTRIBUTE_UNUSED)
2060 2061 2062 2063 2064 2065 2066
{
    virReportSystemError(ENOSYS, "%s",
                         _("Cannot get process uid and gid on this platform"));
    return -1;
}
#endif

2067 2068
static int
virSecurityDACGetProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
2069
                              virDomainDefPtr def,
2070
                              pid_t pid,
2071
                              virSecurityLabelPtr seclabel)
2072
{
2073 2074 2075
    virSecurityLabelDefPtr secdef =
        virDomainDefGetSecurityLabelDef(def, SECURITY_DAC_NAME);

2076 2077 2078 2079 2080 2081 2082 2083
    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;
    }
2084 2085

    if (secdef->label)
2086 2087
        ignore_value(virStrcpy(seclabel->label, secdef->label,
                               VIR_SECURITY_LABEL_BUFLEN));
2088

2089 2090 2091 2092
    return 0;
}

static int
2093
virSecurityDACSetDaemonSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
2094
                                   virDomainDefPtr vm ATTRIBUTE_UNUSED)
2095 2096 2097 2098 2099
{
    return 0;
}


2100 2101
static int
virSecurityDACSetSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
2102
                             virDomainDefPtr def ATTRIBUTE_UNUSED)
2103 2104 2105 2106 2107
{
    return 0;
}


2108 2109
static int
virSecurityDACClearSocketLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
2110
                               virDomainDefPtr def ATTRIBUTE_UNUSED)
2111 2112 2113 2114
{
    return 0;
}

2115
static int
2116
virSecurityDACSetImageFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
2117
                              virDomainDefPtr def ATTRIBUTE_UNUSED,
2118
                              int fd ATTRIBUTE_UNUSED)
2119 2120 2121 2122
{
    return 0;
}

2123 2124 2125 2126 2127 2128 2129 2130
static int
virSecurityDACSetTapFDLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                            virDomainDefPtr def ATTRIBUTE_UNUSED,
                            int fd ATTRIBUTE_UNUSED)
{
    return 0;
}

O
Osier Yang 已提交
2131 2132 2133 2134
static char *
virSecurityDACGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                              virDomainDefPtr vm ATTRIBUTE_UNUSED)
{
2135 2136 2137
    return NULL;
}

2138 2139 2140 2141 2142 2143 2144 2145
static const char *
virSecurityDACGetBaseLabel(virSecurityManagerPtr mgr,
                           int virt ATTRIBUTE_UNUSED)
{
    virSecurityDACDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    return priv->baselabel;
}

2146
static int
2147 2148
virSecurityDACDomainSetPathLabel(virSecurityManagerPtr mgr,
                                 virDomainDefPtr def,
2149 2150
                                 const char *path,
                                 bool allowSubtree ATTRIBUTE_UNUSED)
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
{
    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;

2162
    return virSecurityDACSetOwnership(mgr, NULL, path, user, group);
2163 2164
}

2165
virSecurityDriver virSecurityDriverDAC = {
2166
    .privateDataLen                     = sizeof(virSecurityDACData),
2167
    .name                               = SECURITY_DAC_NAME,
2168 2169 2170
    .probe                              = virSecurityDACProbe,
    .open                               = virSecurityDACOpen,
    .close                              = virSecurityDACClose,
2171

2172 2173
    .getModel                           = virSecurityDACGetModel,
    .getDOI                             = virSecurityDACGetDOI,
2174

2175 2176
    .preFork                            = virSecurityDACPreFork,

2177 2178 2179 2180
    .transactionStart                   = virSecurityDACTransactionStart,
    .transactionCommit                  = virSecurityDACTransactionCommit,
    .transactionAbort                   = virSecurityDACTransactionAbort,

2181
    .domainSecurityVerify               = virSecurityDACVerify,
2182

2183 2184
    .domainSetSecurityDiskLabel         = virSecurityDACSetDiskLabel,
    .domainRestoreSecurityDiskLabel     = virSecurityDACRestoreDiskLabel,
2185

2186 2187
    .domainSetSecurityImageLabel        = virSecurityDACSetImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityDACRestoreImageLabel,
2188

2189 2190 2191
    .domainSetSecurityMemoryLabel       = virSecurityDACSetMemoryLabel,
    .domainRestoreSecurityMemoryLabel   = virSecurityDACRestoreMemoryLabel,

2192 2193 2194
    .domainSetSecurityInputLabel        = virSecurityDACSetInputLabel,
    .domainRestoreSecurityInputLabel    = virSecurityDACRestoreInputLabel,

2195 2196 2197
    .domainSetSecurityDaemonSocketLabel = virSecurityDACSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityDACSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityDACClearSocketLabel,
2198

2199 2200 2201
    .domainGenSecurityLabel             = virSecurityDACGenLabel,
    .domainReserveSecurityLabel         = virSecurityDACReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityDACReleaseLabel,
2202

2203 2204
    .domainGetSecurityProcessLabel      = virSecurityDACGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityDACSetProcessLabel,
2205
    .domainSetSecurityChildProcessLabel = virSecurityDACSetChildProcessLabel,
2206

2207 2208
    .domainSetSecurityAllLabel          = virSecurityDACSetAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityDACRestoreAllLabel,
2209

2210 2211
    .domainSetSecurityHostdevLabel      = virSecurityDACSetHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityDACRestoreHostdevLabel,
2212

2213 2214
    .domainSetSavedStateLabel           = virSecurityDACSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityDACRestoreSavedStateLabel,
2215

2216
    .domainSetSecurityImageFDLabel      = virSecurityDACSetImageFDLabel,
2217
    .domainSetSecurityTapFDLabel        = virSecurityDACSetTapFDLabel,
2218

2219
    .domainGetSecurityMountOptions      = virSecurityDACGetMountOptions,
2220 2221

    .getBaseLabel                       = virSecurityDACGetBaseLabel,
2222

2223
    .domainSetPathLabel                 = virSecurityDACDomainSetPathLabel,
2224 2225 2226

    .domainSetSecurityChardevLabel      = virSecurityDACSetChardevLabel,
    .domainRestoreSecurityChardevLabel  = virSecurityDACRestoreChardevLabel,
2227
};