security_stack.c 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2010-2011 Red Hat, Inc.
 *
 * 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
O
Osier Yang 已提交
15 16
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 *
 * Stacked security driver
 */

#include <config.h>

#include "security_stack.h"

#include "virterror_internal.h"
26
#include "memory.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 57 58 59 60

    if (VIR_ALLOC(item) < 0) {
        virReportOOMError();
        return -1;
    }
    item->securityManager = nested;
61 62 63 64 65
    if (tmp)
        tmp->next = item;
    else
        priv->itemsHead = item;

66 67 68 69 70 71 72
    return 0;
}

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

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

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

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

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

101 102 103 104 105 106
    return 0;
}

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

static const char *
virSecurityStackGetDOI(virSecurityManagerPtr mgr)
{
113
    return virSecurityManagerGetDOI(virSecurityStackGetPrimary(mgr));
114 115 116 117 118 119 120
}

static int
virSecurityStackVerify(virSecurityManagerPtr mgr,
                       virDomainDefPtr def)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
121
    virSecurityStackItemPtr item = priv->itemsHead;
122 123
    int rc = 0;

124 125 126 127 128 129
    for(; item; item = item->next) {
        if (virSecurityManagerVerify(item->securityManager, def) < 0) {
            rc = -1;
            break;
        }
    }
130 131 132 133 134 135 136

    return rc;
}


static int
virSecurityStackGenLabel(virSecurityManagerPtr mgr,
137
                         virDomainDefPtr vm)
138 139 140
{
    int rc = 0;

141
    if (virSecurityManagerGenLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
142 143
        rc = -1;

144
// TODO
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#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,
162
                             virDomainDefPtr vm)
163 164 165
{
    int rc = 0;

166
    if (virSecurityManagerReleaseLabel(virSecurityStackGetPrimary(mgr), vm) < 0)
167
        rc = -1;
168 169

// TODO
170 171 172 173 174 175 176 177 178 179 180 181
#if 0
    /* XXX See note in GenLabel */
    if (virSecurityManagerReleaseLabel(priv->secondary, vm) < 0)
        rc = -1;
#endif

    return rc;
}


static int
virSecurityStackReserveLabel(virSecurityManagerPtr mgr,
182 183
                             virDomainDefPtr vm,
                             pid_t pid)
184 185 186
{
    int rc = 0;

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

    return rc;
}


static int
virSecurityStackSetSecurityImageLabel(virSecurityManagerPtr mgr,
202
                                      virDomainDefPtr vm,
203 204 205
                                      virDomainDiskDefPtr disk)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
206
    virSecurityStackItemPtr item = priv->itemsHead;
207 208
    int rc = 0;

209 210 211 212
    for (; item; item = item->next) {
        if (virSecurityManagerSetImageLabel(item->securityManager, vm, disk) < 0)
            rc = -1;
    }
213 214 215 216 217 218 219

    return rc;
}


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

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

    return rc;
}


static int
virSecurityStackSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
238
                                        virDomainDefPtr vm,
239 240 241 242
                                        virDomainHostdevDefPtr dev)

{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
243
    virSecurityStackItemPtr item = priv->itemsHead;
244 245
    int rc = 0;

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

    return rc;
}


static int
virSecurityStackRestoreSecurityHostdevLabel(virSecurityManagerPtr mgr,
257
                                            virDomainDefPtr vm,
258 259 260
                                            virDomainHostdevDefPtr dev)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
261
    virSecurityStackItemPtr item = priv->itemsHead;
262 263
    int rc = 0;

264 265 266 267
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreHostdevLabel(item->securityManager, vm, dev) < 0)
            rc = -1;
    }
268 269 270 271 272 273 274

    return rc;
}


static int
virSecurityStackSetSecurityAllLabel(virSecurityManagerPtr mgr,
275
                                    virDomainDefPtr vm,
276 277 278
                                    const char *stdin_path)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
279
    virSecurityStackItemPtr item = priv->itemsHead;
280 281
    int rc = 0;

282 283 284 285
    for (; item; item = item->next) {
        if (virSecurityManagerSetAllLabel(item->securityManager, vm, stdin_path) < 0)
            rc = -1;
    }
286 287 288 289 290 291 292

    return rc;
}


static int
virSecurityStackRestoreSecurityAllLabel(virSecurityManagerPtr mgr,
293
                                        virDomainDefPtr vm,
294 295 296
                                        int migrated)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
297
    virSecurityStackItemPtr item = priv->itemsHead;
298 299
    int rc = 0;

300 301 302 303
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreAllLabel(item->securityManager, vm, migrated) < 0)
            rc = -1;
    }
304 305 306 307 308 309 310

    return rc;
}


static int
virSecurityStackSetSavedStateLabel(virSecurityManagerPtr mgr,
311
                                   virDomainDefPtr vm,
312 313 314
                                   const char *savefile)
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
315
    virSecurityStackItemPtr item = priv->itemsHead;
316 317
    int rc = 0;

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

    return rc;
}


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

336 337 338 339
    for (; item; item = item->next) {
        if (virSecurityManagerRestoreSavedStateLabel(item->securityManager, vm, savefile) < 0)
            rc = -1;
    }
340 341 342 343 344 345 346

    return rc;
}


static int
virSecurityStackSetProcessLabel(virSecurityManagerPtr mgr,
347
                                virDomainDefPtr vm)
348 349
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
350
    virSecurityStackItemPtr item = priv->itemsHead;
351 352
    int rc = 0;

353 354 355 356
    for (; item; item = item->next) {
        if (virSecurityManagerSetProcessLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
357 358 359 360 361 362

    return rc;
}

static int
virSecurityStackGetProcessLabel(virSecurityManagerPtr mgr,
363 364
                                virDomainDefPtr vm,
                                pid_t pid,
365 366 367 368
                                virSecurityLabelPtr seclabel)
{
    int rc = 0;

369
// TODO
370
#if 0
371
    if (virSecurityManagerGetProcessLabel(priv->secondary, vm, pid, seclabel) < 0)
372 373
        rc = -1;
#endif
374
    if (virSecurityManagerGetProcessLabel(virSecurityStackGetPrimary(mgr), vm, pid, seclabel) < 0)
375 376 377 378 379 380 381
        rc = -1;

    return rc;
}


static int
382
virSecurityStackSetDaemonSocketLabel(virSecurityManagerPtr mgr,
383
                                     virDomainDefPtr vm)
384 385
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
386
    virSecurityStackItemPtr item = priv->itemsHead;
387 388
    int rc = 0;

389 390 391 392
    for (; item; item = item->next) {
        if (virSecurityManagerSetDaemonSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
393 394 395 396 397

    return rc;
}


398 399
static int
virSecurityStackSetSocketLabel(virSecurityManagerPtr mgr,
400
                               virDomainDefPtr vm)
401 402
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
403
    virSecurityStackItemPtr item = priv->itemsHead;
404 405
    int rc = 0;

406 407 408 409
    for (; item; item = item->next) {
        if (virSecurityManagerSetSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
410 411 412 413 414

    return rc;
}


415 416
static int
virSecurityStackClearSocketLabel(virSecurityManagerPtr mgr,
417
                                 virDomainDefPtr vm)
418 419
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
420
    virSecurityStackItemPtr item = priv->itemsHead;
421 422
    int rc = 0;

423 424 425 426
    for (; item; item = item->next) {
        if (virSecurityManagerClearSocketLabel(item->securityManager, vm) < 0)
            rc = -1;
    }
427 428 429 430

    return rc;
}

431
static int
432
virSecurityStackSetImageFDLabel(virSecurityManagerPtr mgr,
433
                                virDomainDefPtr vm,
434
                                int fd)
435 436
{
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
437
    virSecurityStackItemPtr item = priv->itemsHead;
438 439
    int rc = 0;

440 441 442 443
    for (; item; item = item->next) {
        if (virSecurityManagerSetImageFDLabel(item->securityManager, vm, fd) < 0)
            rc = -1;
    }
444 445 446 447

    return rc;
}

448 449 450 451
static char *virSecurityStackGetMountOptions(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
                                             virDomainDefPtr vm ATTRIBUTE_UNUSED) {
    return NULL;
}
452

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
virSecurityManagerPtr*
virSecurityStackGetNested(virSecurityManagerPtr mgr)
{
    virSecurityManagerPtr *list = NULL;
    virSecurityStackDataPtr priv = virSecurityManagerGetPrivateData(mgr);
    virSecurityStackItemPtr item;
    int len = 0, i = 0;

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

    if (VIR_ALLOC_N(list, len + 1) < 0) {
        virReportOOMError();
        return NULL;
    }

    for (item = priv->itemsHead; item; item = item->next, i++)
        list[i] = item->securityManager;
    list[len] = NULL;

    return list;
}

476
virSecurityDriver virSecurityDriverStack = {
477 478 479 480 481
    .privateDataLen                     = sizeof(virSecurityStackData),
    .name                               = "stack",
    .probe                              = virSecurityStackProbe,
    .open                               = virSecurityStackOpen,
    .close                              = virSecurityStackClose,
482

483 484
    .getModel                           = virSecurityStackGetModel,
    .getDOI                             = virSecurityStackGetDOI,
485

486
    .domainSecurityVerify               = virSecurityStackVerify,
487

488 489
    .domainSetSecurityImageLabel        = virSecurityStackSetSecurityImageLabel,
    .domainRestoreSecurityImageLabel    = virSecurityStackRestoreSecurityImageLabel,
490

491 492 493
    .domainSetSecurityDaemonSocketLabel = virSecurityStackSetDaemonSocketLabel,
    .domainSetSecuritySocketLabel       = virSecurityStackSetSocketLabel,
    .domainClearSecuritySocketLabel     = virSecurityStackClearSocketLabel,
494

495 496 497
    .domainGenSecurityLabel             = virSecurityStackGenLabel,
    .domainReserveSecurityLabel         = virSecurityStackReserveLabel,
    .domainReleaseSecurityLabel         = virSecurityStackReleaseLabel,
498

499 500
    .domainGetSecurityProcessLabel      = virSecurityStackGetProcessLabel,
    .domainSetSecurityProcessLabel      = virSecurityStackSetProcessLabel,
501

502 503
    .domainSetSecurityAllLabel          = virSecurityStackSetSecurityAllLabel,
    .domainRestoreSecurityAllLabel      = virSecurityStackRestoreSecurityAllLabel,
504

505 506
    .domainSetSecurityHostdevLabel      = virSecurityStackSetSecurityHostdevLabel,
    .domainRestoreSecurityHostdevLabel  = virSecurityStackRestoreSecurityHostdevLabel,
507

508 509
    .domainSetSavedStateLabel           = virSecurityStackSetSavedStateLabel,
    .domainRestoreSavedStateLabel       = virSecurityStackRestoreSavedStateLabel,
510

511
    .domainSetSecurityImageFDLabel      = virSecurityStackSetImageFDLabel,
512

513
    .domainGetSecurityMountOptions      = virSecurityStackGetMountOptions,
514
};