security_stack.c 19.6 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
 *
 * Stacked security driver
 */

#include <config.h>

#include "security_stack.h"

25
#include "virerror.h"
26
#include "viralloc.h"
27 28 29 30 31

#define VIR_FROM_THIS VIR_FROM_SECURITY

typedef struct _virSecurityStackData virSecurityStackData;
typedef virSecurityStackData *virSecurityStackDataPtr;
32
typedef struct _virSecurityStackItem virSecurityStackItem;
33
typedef virSecurityStackItem *virSecurityStackItemPtr;
34 35 36 37 38

struct _virSecurityStackItem {
    virSecurityManagerPtr securityManager;
    virSecurityStackItemPtr next;
};
39 40

struct _virSecurityStackData {
41
    virSecurityStackItemPtr itemsHead;
42 43
};

44 45 46 47 48 49
int
virSecurityStackAddNested(virSecurityManagerPtr mgr,
                          virSecurityManagerPtr nested)
{
    virSecurityStackItemPtr item = NULL;
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
50 51 52 53 54
    virSecurityStackItemPtr tmp;

    tmp = priv->itemsHead;
    while (tmp && tmp->next)
        tmp = tmp->next;
55

56
    if (VIR_ALLOC(item) < 0)
57 58
        return -1;
    item->securityManager = nested;
59 60 61 62 63
    if (tmp)
        tmp->next = item;
    else
        priv->itemsHead = item;

64 65 66 67 68 69 70
    return 0;
}

virSecurityManagerPtr
virSecurityStackGetPrimary(virSecurityManagerPtr mgr)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
71
    return priv->itemsHead->securityManager;
72 73 74
}

static virSecurityDriverStatus
75
virSecurityStackProbe(const char *virtDriver ATTRIBUTE_UNUSED)
76 77 78 79 80 81 82 83 84 85 86
{
    return SECURITY_DRIVER_ENABLE;
}

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

static int
E
Eric Blake 已提交
87
virSecurityStackClose(virSecurityManagerPtr mgr)
88
{
E
Eric Blake 已提交
89
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
90
    virSecurityStackItemPtr next, item = priv->itemsHead;
E
Eric Blake 已提交
91

92 93
    while (item) {
        next = item->next;
94
        virObjectUnref(item->securityManager);
95 96 97
        VIR_FREE(item);
        item = next;
    }
E
Eric Blake 已提交
98

99 100 101 102 103 104
    return 0;
}

static const char *
virSecurityStackGetModel(virSecurityManagerPtr mgr)
{
105
    return virSecurityManagerGetModel(virSecurityStackGetPrimary(mgr));
106 107 108 109 110
}

static const char *
virSecurityStackGetDOI(virSecurityManagerPtr mgr)
{
111
    return virSecurityManagerGetDOI(virSecurityStackGetPrimary(mgr));
112 113
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
static int
virSecurityStackPreFork(virSecurityManagerPtr mgr)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    /* XXX For now, we rely on no driver having any state that requires
     * rollback if a later driver in the stack fails; if this changes,
     * we'd need to split this into transaction semantics by dividing
     * the work into prepare/commit/abort.  */
    for (; item; item = item->next) {
        if (virSecurityManagerPreFork(item->securityManager) < 0) {
            rc = -1;
            break;
        }
E
Eric Blake 已提交
130 131 132 133 134
        /* Undo the unbalanced locking left behind after recursion; if
         * PostFork ever delegates to driver callbacks, we'd instead
         * need to recurse to an internal method that does not regrab
         * a lock. */
        virSecurityManagerPostFork(item->securityManager);
135 136 137 138 139
    }

    return rc;
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

static int
virSecurityStackTransactionStart(virSecurityManagerPtr mgr)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerTransactionStart(item->securityManager) < 0)
            rc = -1;
    }

    return rc;
}


static int
virSecurityStackTransactionCommit(virSecurityManagerPtr mgr,
                                  pid_t pid)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerTransactionCommit(item->securityManager, pid) < 0)
            rc = -1;
    }

    return rc;
}


static void
virSecurityStackTransactionAbort(virSecurityManagerPtr mgr)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;

    for (; item; item = item->next)
        virSecurityManagerTransactionAbort(item->securityManager);
}


185 186 187 188 189
static int
virSecurityStackVerify(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
190
    virSecurityStackItemPtr item = priv->itemsHead;
191 192
    int rc = 0;

193
    for (; item; item = item->next) {
194 195 196 197 198
        if (virSecurityManagerVerify(item->securityManager, def) < 0) {
            rc = -1;
            break;
        }
    }
199 200 201 202 203 204 205

    return rc;
}


static int
virSecurityStackGenLabel(virSecurityManagerPtr mgr,
206
                         virDomainDefPtr vm)
207 208 209
{
    int rc = 0;

210
    if (virSecurityManagerGenLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
211 212
        rc = -1;

213
// TODO
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#if 0
    /* We don't allow secondary drivers to generate labels.
     * This may have to change in the future, but requires
     * changes elsewhere in domain_conf.c and capabilities.c
     * XML formats first, to allow recording of multiple
     * labels
     */
    if (virSecurityManagerGenLabel(priv->secondary, vm) < 0)
        rc = -1;
#endif

    return rc;
}


static int
virSecurityStackReleaseLabel(virSecurityManagerPtr mgr,
231
                             virDomainDefPtr vm)
232 233 234
{
    int rc = 0;

235
    if (virSecurityManagerReleaseLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
236
        rc = -1;
237 238

// TODO
239 240 241 242 243 244 245 246 247 248 249 250
#if 0
    /* XXX See note in GenLabel */
    if (virSecurityManagerReleaseLabel(priv->secondary, vm) < 0)
        rc = -1;
#endif

    return rc;
}


static int
virSecurityStackReserveLabel(virSecurityManagerPtr mgr,
251 252
                             virDomainDefPtr vm,
                             pid_t pid)
253 254 255
{
    int rc = 0;

256
    if (virSecurityManagerReserveLabel(virSecurityStackGetPrimary(mgr), vm, pid) < 0)
257
        rc = -1;
258
// TODO
259 260
#if 0
    /* XXX See note in GenLabel */
261
    if (virSecurityManagerReserveLabel(priv->secondary, vm, pid) < 0)
262 263 264 265 266 267 268 269
        rc = -1;
#endif

    return rc;
}


static int
270 271 272
virSecurityStackSetDiskLabel(virSecurityManagerPtr mgr,
                             virDomainDefPtr vm,
                             virDomainDiskDefPtr disk)
273 274
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
275
    virSecurityStackItemPtr item = priv->itemsHead;
276 277
    int rc = 0;

278
    for (; item; item = item->next) {
279
        if (virSecurityManagerSetDiskLabel(item->securityManager, vm, disk) < 0)
280 281
            rc = -1;
    }
282 283 284 285 286 287

    return rc;
}


static int
288 289 290
virSecurityStackRestoreDiskLabel(virSecurityManagerPtr mgr,
                                 virDomainDefPtr vm,
                                 virDomainDiskDefPtr disk)
291 292
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
293
    virSecurityStackItemPtr item = priv->itemsHead;
294 295
    int rc = 0;

296
    for (; item; item = item->next) {
297
        if (virSecurityManagerRestoreDiskLabel(item->securityManager, vm, disk) < 0)
298 299
            rc = -1;
    }
300 301 302 303 304 305

    return rc;
}


static int
306 307 308 309
virSecurityStackSetHostdevLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr vm,
                                virDomainHostdevDefPtr dev,
                                const char *vroot)
310 311 312

{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
313
    virSecurityStackItemPtr item = priv->itemsHead;
314 315
    int rc = 0;

316
    for (; item; item = item->next) {
317 318 319 320
        if (virSecurityManagerSetHostdevLabel(item->securityManager,
                                              vm,
                                              dev,
                                              vroot) < 0)
321 322
            rc = -1;
    }
323 324 325 326 327 328

    return rc;
}


static int
329 330 331 332
virSecurityStackRestoreHostdevLabel(virSecurityManagerPtr mgr,
                                    virDomainDefPtr vm,
                                    virDomainHostdevDefPtr dev,
                                    const char *vroot)
333 334
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
335
    virSecurityStackItemPtr item = priv->itemsHead;
336 337
    int rc = 0;

338
    for (; item; item = item->next) {
339 340 341 342
        if (virSecurityManagerRestoreHostdevLabel(item->securityManager,
                                                  vm,
                                                  dev,
                                                  vroot) < 0)
343 344
            rc = -1;
    }
345 346 347 348 349 350

    return rc;
}


static int
351 352 353
virSecurityStackSetAllLabel(virSecurityManagerPtr mgr,
                            virDomainDefPtr vm,
                            const char *stdin_path)
354 355
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
356
    virSecurityStackItemPtr item = priv->itemsHead;
357 358
    int rc = 0;

359 360 361 362
    for (; item; item = item->next) {
        if (virSecurityManagerSetAllLabel(item->securityManager, vm, stdin_path) < 0)
            rc = -1;
    }
363 364 365 366 367 368

    return rc;
}


static int
369 370 371
virSecurityStackRestoreAllLabel(virSecurityManagerPtr mgr,
                                virDomainDefPtr vm,
                                bool migrated)
372 373
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
374
    virSecurityStackItemPtr item = priv->itemsHead;
375 376
    int rc = 0;

377 378 379 380
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreAllLabel(item->securityManager, vm, migrated) < 0)
            rc = -1;
    }
381 382 383 384 385 386 387

    return rc;
}


static int
virSecurityStackSetSavedStateLabel(virSecurityManagerPtr mgr,
388
                                   virDomainDefPtr vm,
389 390 391
                                   const char *savefile)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
392
    virSecurityStackItemPtr item = priv->itemsHead;
393 394
    int rc = 0;

395 396 397 398
    for (; item; item = item->next) {
        if (virSecurityManagerSetSavedStateLabel(item->securityManager, vm, savefile) < 0)
            rc = -1;
    }
399 400 401 402 403 404 405

    return rc;
}


static int
virSecurityStackRestoreSavedStateLabel(virSecurityManagerPtr mgr,
406
                                       virDomainDefPtr vm,
407 408 409
                                       const char *savefile)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
410
    virSecurityStackItemPtr item = priv->itemsHead;
411 412
    int rc = 0;

413 414 415 416
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreSavedStateLabel(item->securityManager, vm, savefile) < 0)
            rc = -1;
    }
417 418 419 420 421 422 423

    return rc;
}


static int
virSecurityStackSetProcessLabel(virSecurityManagerPtr mgr,
424
                                virDomainDefPtr vm)
425 426
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
427
    virSecurityStackItemPtr item = priv->itemsHead;
428 429
    int rc = 0;

430 431 432 433
    for (; item; item = item->next) {
        if (virSecurityManagerSetProcessLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
434 435 436 437

    return rc;
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
static int
virSecurityStackSetChildProcessLabel(virSecurityManagerPtr mgr,
                                     virDomainDefPtr vm,
                                     virCommandPtr cmd)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerSetChildProcessLabel(item->securityManager, vm, cmd) < 0)
            rc = -1;
    }

    return rc;
}

455 456
static int
virSecurityStackGetProcessLabel(virSecurityManagerPtr mgr,
457 458
                                virDomainDefPtr vm,
                                pid_t pid,
459 460 461 462
                                virSecurityLabelPtr seclabel)
{
    int rc = 0;

463
// TODO
464
#if 0
465
    if (virSecurityManagerGetProcessLabel(priv->secondary, vm, pid, seclabel) < 0)
466 467
        rc = -1;
#endif
468
    if (virSecurityManagerGetProcessLabel(virSecurityStackGetPrimary(mgr), vm, pid, seclabel) < 0)
469 470 471 472 473 474 475
        rc = -1;

    return rc;
}


static int
476
virSecurityStackSetDaemonSocketLabel(virSecurityManagerPtr mgr,
477
                                     virDomainDefPtr vm)
478 479
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
480
    virSecurityStackItemPtr item = priv->itemsHead;
481 482
    int rc = 0;

483 484 485 486
    for (; item; item = item->next) {
        if (virSecurityManagerSetDaemonSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
487 488 489 490 491

    return rc;
}


492 493
static int
virSecurityStackSetSocketLabel(virSecurityManagerPtr mgr,
494
                               virDomainDefPtr vm)
495 496
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
497
    virSecurityStackItemPtr item = priv->itemsHead;
498 499
    int rc = 0;

500 501 502 503
    for (; item; item = item->next) {
        if (virSecurityManagerSetSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
504 505 506 507 508

    return rc;
}


509 510
static int
virSecurityStackClearSocketLabel(virSecurityManagerPtr mgr,
511
                                 virDomainDefPtr vm)
512 513
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
514
    virSecurityStackItemPtr item = priv->itemsHead;
515 516
    int rc = 0;

517 518 519 520
    for (; item; item = item->next) {
        if (virSecurityManagerClearSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
521 522 523 524

    return rc;
}

525
static int
526
virSecurityStackSetImageFDLabel(virSecurityManagerPtr mgr,
527
                                virDomainDefPtr vm,
528
                                int fd)
529 530
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
531
    virSecurityStackItemPtr item = priv->itemsHead;
532 533
    int rc = 0;

534 535 536 537
    for (; item; item = item->next) {
        if (virSecurityManagerSetImageFDLabel(item->securityManager, vm, fd) < 0)
            rc = -1;
    }
538 539 540 541

    return rc;
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
static int
virSecurityStackSetTapFDLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr vm,
                              int fd)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerSetTapFDLabel(item->securityManager, vm, fd) < 0)
            rc = -1;
    }

    return rc;
}

559 560 561 562
static char *
virSecurityStackGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                virDomainDefPtr vm ATTRIBUTE_UNUSED)
{
563 564
    return NULL;
}
565

566 567 568 569 570 571
virSecurityManagerPtr*
virSecurityStackGetNested(virSecurityManagerPtr mgr)
{
    virSecurityManagerPtr *list = NULL;
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item;
572 573
    int len = 0;
    size_t i;
574 575 576 577

    for (item = priv->itemsHead; item; item = item->next)
        len++;

578
    if (VIR_ALLOC_N(list, len + 1) < 0)
579 580
        return NULL;

581
    for (i = 0, item = priv->itemsHead; item; item = item->next, i++)
582 583 584 585 586 587
        list[i] = item->securityManager;
    list[len] = NULL;

    return list;
}

588 589 590 591 592 593 594
static const char *
virSecurityStackGetBaseLabel(virSecurityManagerPtr mgr, int virtType)
{
    return virSecurityManagerGetBaseLabel(virSecurityStackGetPrimary(mgr),
                                          virtType);
}

595
static int
596 597 598
virSecurityStackSetImageLabel(virSecurityManagerPtr mgr,
                              virDomainDefPtr vm,
                              virStorageSourcePtr src)
599 600 601 602 603 604 605 606 607 608 609 610 611 612
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerSetImageLabel(item->securityManager, vm, src) < 0)
            rc = -1;
    }

    return rc;
}

static int
613 614 615
virSecurityStackRestoreImageLabel(virSecurityManagerPtr mgr,
                                  virDomainDefPtr vm,
                                  virStorageSourcePtr src)
616 617 618 619 620 621 622 623 624 625 626 627 628 629
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
        if (virSecurityManagerRestoreImageLabel(item->securityManager,
                                                vm, src) < 0)
            rc = -1;
    }

    return rc;
}

630
static int
631 632 633
virSecurityStackDomainSetPathLabel(virSecurityManagerPtr mgr,
                                   virDomainDefPtr vm,
                                   const char *path)
634 635 636 637 638 639
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

    for (; item; item = item->next) {
640 641
        if (virSecurityManagerDomainSetPathLabel(item->securityManager,
                                                 vm, path) < 0)
642 643 644 645 646 647
            rc = -1;
    }

    return rc;
}

648
virSecurityDriver virSecurityDriverStack = {
649 650 651 652 653
    .privateDataLen                     = sizeof(virSecurityStackData),
    .name                               = "stack",
    .probe                              = virSecurityStackProbe,
    .open                               = virSecurityStackOpen,
    .close                              = virSecurityStackClose,
654

655 656
    .getModel                           = virSecurityStackGetModel,
    .getDOI                             = virSecurityStackGetDOI,
657

658 659
    .preFork                            = virSecurityStackPreFork,

660 661 662 663
    .transactionStart                   = virSecurityStackTransactionStart,
    .transactionCommit                  = virSecurityStackTransactionCommit,
    .transactionAbort                   = virSecurityStackTransactionAbort,

664
    .domainSecurityVerify               = virSecurityStackVerify,
665

666 667
    .domainSetSecurityDiskLabel         = virSecurityStackSetDiskLabel,
    .domainRestoreSecurityDiskLabel     = virSecurityStackRestoreDiskLabel,
668

669 670
    .domainSetSecurityImageLabel        = virSecurityStackSetImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityStackRestoreImageLabel,
671

672 673 674
    .domainSetSecurityDaemonSocketLabel = virSecurityStackSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityStackSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityStackClearSocketLabel,
675

676 677 678
    .domainGenSecurityLabel             = virSecurityStackGenLabel,
    .domainReserveSecurityLabel         = virSecurityStackReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityStackReleaseLabel,
679

680 681
    .domainGetSecurityProcessLabel      = virSecurityStackGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityStackSetProcessLabel,
682
    .domainSetSecurityChildProcessLabel = virSecurityStackSetChildProcessLabel,
683

684 685
    .domainSetSecurityAllLabel          = virSecurityStackSetAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityStackRestoreAllLabel,
686

687 688
    .domainSetSecurityHostdevLabel      = virSecurityStackSetHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityStackRestoreHostdevLabel,
689

690 691
    .domainSetSavedStateLabel           = virSecurityStackSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityStackRestoreSavedStateLabel,
692

693
    .domainSetSecurityImageFDLabel      = virSecurityStackSetImageFDLabel,
694
    .domainSetSecurityTapFDLabel        = virSecurityStackSetTapFDLabel,
695

696
    .domainGetSecurityMountOptions      = virSecurityStackGetMountOptions,
697

698
    .getBaseLabel                       = virSecurityStackGetBaseLabel,
699

700
    .domainSetPathLabel                 = virSecurityStackDomainSetPathLabel,
701
};