security_stack.c 16.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2010-2013 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 33 34 35 36 37 38
typedef struct _virSecurityStackItem virSecurityStackItem;
typedef virSecurityStackItem* virSecurityStackItemPtr;

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 130 131 132 133 134
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;
        }
    }

    return rc;
}

135 136 137 138 139
static int
virSecurityStackVerify(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
140
    virSecurityStackItemPtr item = priv->itemsHead;
141 142
    int rc = 0;

143
    for (; item; item = item->next) {
144 145 146 147 148
        if (virSecurityManagerVerify(item->securityManager, def) < 0) {
            rc = -1;
            break;
        }
    }
149 150 151 152 153 154 155

    return rc;
}


static int
virSecurityStackGenLabel(virSecurityManagerPtr mgr,
156
                         virDomainDefPtr vm)
157 158 159
{
    int rc = 0;

160
    if (virSecurityManagerGenLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
161 162
        rc = -1;

163
// TODO
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#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,
181
                             virDomainDefPtr vm)
182 183 184
{
    int rc = 0;

185
    if (virSecurityManagerReleaseLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
186
        rc = -1;
187 188

// TODO
189 190 191 192 193 194 195 196 197 198 199 200
#if 0
    /* XXX See note in GenLabel */
    if (virSecurityManagerReleaseLabel(priv->secondary, vm) < 0)
        rc = -1;
#endif

    return rc;
}


static int
virSecurityStackReserveLabel(virSecurityManagerPtr mgr,
201 202
                             virDomainDefPtr vm,
                             pid_t pid)
203 204 205
{
    int rc = 0;

206
    if (virSecurityManagerReserveLabel(virSecurityStackGetPrimary(mgr), vm, pid) < 0)
207
        rc = -1;
208
// TODO
209 210
#if 0
    /* XXX See note in GenLabel */
211
    if (virSecurityManagerReserveLabel(priv->secondary, vm, pid) < 0)
212 213 214 215 216 217 218 219 220
        rc = -1;
#endif

    return rc;
}


static int
virSecurityStackSetSecurityImageLabel(virSecurityManagerPtr mgr,
221
                                      virDomainDefPtr vm,
222 223 224
                                      virDomainDiskDefPtr disk)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
225
    virSecurityStackItemPtr item = priv->itemsHead;
226 227
    int rc = 0;

228 229 230 231
    for (; item; item = item->next) {
        if (virSecurityManagerSetImageLabel(item->securityManager, vm, disk) < 0)
            rc = -1;
    }
232 233 234 235 236 237 238

    return rc;
}


static int
virSecurityStackRestoreSecurityImageLabel(virSecurityManagerPtr mgr,
239
                                          virDomainDefPtr vm,
240 241 242
                                          virDomainDiskDefPtr disk)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
243
    virSecurityStackItemPtr item = priv->itemsHead;
244 245
    int rc = 0;

246 247 248 249
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreImageLabel(item->securityManager, vm, disk) < 0)
            rc = -1;
    }
250 251 252 253 254 255 256

    return rc;
}


static int
virSecurityStackSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
257
                                        virDomainDefPtr vm,
258 259
                                        virDomainHostdevDefPtr dev,
                                        const char *vroot)
260 261 262

{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
263
    virSecurityStackItemPtr item = priv->itemsHead;
264 265
    int rc = 0;

266
    for (; item; item = item->next) {
267 268 269 270
        if (virSecurityManagerSetHostdevLabel(item->securityManager,
                                              vm,
                                              dev,
                                              vroot) < 0)
271 272
            rc = -1;
    }
273 274 275 276 277 278 279

    return rc;
}


static int
virSecurityStackRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
280
                                            virDomainDefPtr vm,
281 282
                                            virDomainHostdevDefPtr dev,
                                            const char *vroot)
283 284
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
285
    virSecurityStackItemPtr item = priv->itemsHead;
286 287
    int rc = 0;

288
    for (; item; item = item->next) {
289 290 291 292
        if (virSecurityManagerRestoreHostdevLabel(item->securityManager,
                                                  vm,
                                                  dev,
                                                  vroot) < 0)
293 294
            rc = -1;
    }
295 296 297 298 299 300 301

    return rc;
}


static int
virSecurityStackSetSecurityAllLabel(virSecurityManagerPtr mgr,
302
                                    virDomainDefPtr vm,
303 304 305
                                    const char *stdin_path)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
306
    virSecurityStackItemPtr item = priv->itemsHead;
307 308
    int rc = 0;

309 310 311 312
    for (; item; item = item->next) {
        if (virSecurityManagerSetAllLabel(item->securityManager, vm, stdin_path) < 0)
            rc = -1;
    }
313 314 315 316 317 318 319

    return rc;
}


static int
virSecurityStackRestoreSecurityAllLabel(virSecurityManagerPtr mgr,
320
                                        virDomainDefPtr vm,
321 322 323
                                        int migrated)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
324
    virSecurityStackItemPtr item = priv->itemsHead;
325 326
    int rc = 0;

327 328 329 330
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreAllLabel(item->securityManager, vm, migrated) < 0)
            rc = -1;
    }
331 332 333 334 335 336 337

    return rc;
}


static int
virSecurityStackSetSavedStateLabel(virSecurityManagerPtr mgr,
338
                                   virDomainDefPtr vm,
339 340 341
                                   const char *savefile)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
342
    virSecurityStackItemPtr item = priv->itemsHead;
343 344
    int rc = 0;

345 346 347 348
    for (; item; item = item->next) {
        if (virSecurityManagerSetSavedStateLabel(item->securityManager, vm, savefile) < 0)
            rc = -1;
    }
349 350 351 352 353 354 355

    return rc;
}


static int
virSecurityStackRestoreSavedStateLabel(virSecurityManagerPtr mgr,
356
                                       virDomainDefPtr vm,
357 358 359
                                       const char *savefile)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
360
    virSecurityStackItemPtr item = priv->itemsHead;
361 362
    int rc = 0;

363 364 365 366
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreSavedStateLabel(item->securityManager, vm, savefile) < 0)
            rc = -1;
    }
367 368 369 370 371 372 373

    return rc;
}


static int
virSecurityStackSetProcessLabel(virSecurityManagerPtr mgr,
374
                                virDomainDefPtr vm)
375 376
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
377
    virSecurityStackItemPtr item = priv->itemsHead;
378 379
    int rc = 0;

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

    return rc;
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
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;
}

405 406
static int
virSecurityStackGetProcessLabel(virSecurityManagerPtr mgr,
407 408
                                virDomainDefPtr vm,
                                pid_t pid,
409 410 411 412
                                virSecurityLabelPtr seclabel)
{
    int rc = 0;

413
// TODO
414
#if 0
415
    if (virSecurityManagerGetProcessLabel(priv->secondary, vm, pid, seclabel) < 0)
416 417
        rc = -1;
#endif
418
    if (virSecurityManagerGetProcessLabel(virSecurityStackGetPrimary(mgr), vm, pid, seclabel) < 0)
419 420 421 422 423 424 425
        rc = -1;

    return rc;
}


static int
426
virSecurityStackSetDaemonSocketLabel(virSecurityManagerPtr mgr,
427
                                     virDomainDefPtr vm)
428 429
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
430
    virSecurityStackItemPtr item = priv->itemsHead;
431 432
    int rc = 0;

433 434 435 436
    for (; item; item = item->next) {
        if (virSecurityManagerSetDaemonSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
437 438 439 440 441

    return rc;
}


442 443
static int
virSecurityStackSetSocketLabel(virSecurityManagerPtr mgr,
444
                               virDomainDefPtr vm)
445 446
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
447
    virSecurityStackItemPtr item = priv->itemsHead;
448 449
    int rc = 0;

450 451 452 453
    for (; item; item = item->next) {
        if (virSecurityManagerSetSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
454 455 456 457 458

    return rc;
}


459 460
static int
virSecurityStackClearSocketLabel(virSecurityManagerPtr mgr,
461
                                 virDomainDefPtr vm)
462 463
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
464
    virSecurityStackItemPtr item = priv->itemsHead;
465 466
    int rc = 0;

467 468 469 470
    for (; item; item = item->next) {
        if (virSecurityManagerClearSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
471 472 473 474

    return rc;
}

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

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

    return rc;
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
}

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static int
virSecurityStackSetHugepages(virSecurityManagerPtr mgr,
                              virDomainDefPtr vm,
                              const char *path)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item = priv->itemsHead;
    int rc = 0;

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

    return rc;
}

526 527 528 529
static char *virSecurityStackGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                             virDomainDefPtr vm ATTRIBUTE_UNUSED) {
    return NULL;
}
530

531 532 533 534 535 536
virSecurityManagerPtr*
virSecurityStackGetNested(virSecurityManagerPtr mgr)
{
    virSecurityManagerPtr *list = NULL;
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item;
537 538
    int len = 0;
    size_t i;
539 540 541 542

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

543
    if (VIR_ALLOC_N(list, len + 1) < 0)
544 545
        return NULL;

546
    for (i = 0, item = priv->itemsHead; item; item = item->next, i++)
547 548 549 550 551 552
        list[i] = item->securityManager;
    list[len] = NULL;

    return list;
}

553
virSecurityDriver virSecurityDriverStack = {
554 555 556 557 558
    .privateDataLen                     = sizeof(virSecurityStackData),
    .name                               = "stack",
    .probe                              = virSecurityStackProbe,
    .open                               = virSecurityStackOpen,
    .close                              = virSecurityStackClose,
559

560 561
    .getModel                           = virSecurityStackGetModel,
    .getDOI                             = virSecurityStackGetDOI,
562

563 564
    .preFork                            = virSecurityStackPreFork,

565
    .domainSecurityVerify               = virSecurityStackVerify,
566

567 568
    .domainSetSecurityImageLabel        = virSecurityStackSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityStackRestoreSecurityImageLabel,
569

570 571 572
    .domainSetSecurityDaemonSocketLabel = virSecurityStackSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityStackSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityStackClearSocketLabel,
573

574 575 576
    .domainGenSecurityLabel             = virSecurityStackGenLabel,
    .domainReserveSecurityLabel         = virSecurityStackReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityStackReleaseLabel,
577

578 579
    .domainGetSecurityProcessLabel      = virSecurityStackGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityStackSetProcessLabel,
580
    .domainSetSecurityChildProcessLabel = virSecurityStackSetChildProcessLabel,
581

582 583
    .domainSetSecurityAllLabel          = virSecurityStackSetSecurityAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityStackRestoreSecurityAllLabel,
584

585 586
    .domainSetSecurityHostdevLabel      = virSecurityStackSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityStackRestoreSecurityHostdevLabel,
587

588 589
    .domainSetSavedStateLabel           = virSecurityStackSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityStackRestoreSavedStateLabel,
590

591
    .domainSetSecurityImageFDLabel      = virSecurityStackSetImageFDLabel,
592
    .domainSetSecurityTapFDLabel        = virSecurityStackSetTapFDLabel,
593

594
    .domainGetSecurityMountOptions      = virSecurityStackGetMountOptions,
595 596

    .domainSetSecurityHugepages         = virSecurityStackSetHugepages,
597
};