domain_audit.c 30.8 KB
Newer Older
1
/*
2
 * domain_audit.c: Domain audit management
3
 *
4
 * Copyright (C) 2006-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

26 27
#include <sys/stat.h>

28 29 30 31 32 33
#ifdef MAJOR_IN_MKDEV
# include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
# include <sys/sysmacros.h>
#endif

34 35
#include <sys/types.h>

36
#include "domain_audit.h"
37
#include "viraudit.h"
38
#include "viruuid.h"
39
#include "virlog.h"
40
#include "viralloc.h"
41
#include "virstring.h"
42

43 44
VIR_LOG_INIT("conf.domain_audit");

45 46 47 48
/* Return nn:mm in hex for block and character devices, and NULL
 * for other file types, stat failure, or allocation failure.  */
#if defined major && defined minor
static char *
49
virDomainAuditGetRdev(const char *path)
50 51 52 53 54 55 56 57
{
    char *ret = NULL;
    struct stat sb;

    if (stat(path, &sb) == 0 &&
        (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) {
        int maj = major(sb.st_rdev);
        int min = minor(sb.st_rdev);
58
        ignore_value(virAsprintfQuiet(&ret, "%02X:%02X", maj, min));
59 60 61 62 63
    }
    return ret;
}
#else
static char *
64
virDomainAuditGetRdev(const char *path ATTRIBUTE_UNUSED)
65 66 67 68 69
{
    return NULL;
}
#endif

70 71 72 73 74 75 76

static const char *
virDomainAuditChardevPath(virDomainChrSourceDefPtr chr)
{
    if (!chr)
        return NULL;

77
    switch ((virDomainChrType) chr->type) {
78 79 80 81
    case VIR_DOMAIN_CHR_TYPE_PTY:
    case VIR_DOMAIN_CHR_TYPE_DEV:
    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
82
    case VIR_DOMAIN_CHR_TYPE_NMDM:
83 84 85 86 87 88 89 90 91 92 93
        return chr->data.file.path;

    case VIR_DOMAIN_CHR_TYPE_UNIX:
        return chr->data.nix.path;

    case VIR_DOMAIN_CHR_TYPE_TCP:
    case VIR_DOMAIN_CHR_TYPE_UDP:
    case VIR_DOMAIN_CHR_TYPE_NULL:
    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_STDIO:
    case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
94
    case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
95 96 97 98 99 100 101 102
    case VIR_DOMAIN_CHR_TYPE_LAST:
        return NULL;
    }

    return NULL;
}


103 104 105 106 107 108 109
static void
virDomainAuditGenericDev(virDomainObjPtr vm,
                         const char *type,
                         const char *oldsrcpath,
                         const char *newsrcpath,
                         const char *reason,
                         bool success)
110
{
111 112
    char *newdev = NULL;
    char *olddev = NULL;
113
    char uuidstr[VIR_UUID_STRING_BUFLEN];
114
    char *vmname = NULL;
115 116
    char *oldsrc = NULL;
    char *newsrc = NULL;
117
    const char *virt;
118

119 120
    /* if both new and old source aren't provided don't log anything */
    if (!newsrcpath && !oldsrcpath)
121
        return;
122 123 124 125 126 127 128 129 130 131 132

    if (virAsprintfQuiet(&newdev, "new-%s", type) < 0)
        goto no_memory;

    if (virAsprintfQuiet(&olddev, "old-%s", type) < 0)
        goto no_memory;

    virUUIDFormat(vm->def->uuid, uuidstr);

    if (!(vmname = virAuditEncode("vm", vm->def->name)))
        goto no_memory;
133

134
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
135 136
        VIR_WARN("Unexpected virt type %d while encoding audit message",
                 vm->def->virtType);
137 138 139
        virt = "?";
    }

140 141 142 143 144
    if (!(newsrc = virAuditEncode(newdev, VIR_AUDIT_STR(newsrcpath))))
        goto no_memory;

    if (!(oldsrc = virAuditEncode(olddev, VIR_AUDIT_STR(oldsrcpath))))
        goto no_memory;
145 146

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
147 148
              "virt=%s resrc=%s reason=%s %s uuid=%s %s %s",
              virt, type, reason, vmname, uuidstr, oldsrc, newsrc);
149

150
 cleanup:
151 152
    VIR_FREE(newdev);
    VIR_FREE(olddev);
153 154 155
    VIR_FREE(vmname);
    VIR_FREE(oldsrc);
    VIR_FREE(newsrc);
156 157 158 159 160 161 162 163
    return;

 no_memory:
    VIR_WARN("OOM while encoding audit message");
    goto cleanup;
}


164 165 166 167 168 169 170 171 172 173 174
void
virDomainAuditChardev(virDomainObjPtr vm,
                      virDomainChrDefPtr oldDef,
                      virDomainChrDefPtr newDef,
                      const char *reason,
                      bool success)
{
    virDomainChrSourceDefPtr oldsrc = NULL;
    virDomainChrSourceDefPtr newsrc = NULL;

    if (oldDef)
175
        oldsrc = oldDef->source;
176 177

    if (newDef)
178
        newsrc = newDef->source;
179 180 181 182 183 184 185 186

    virDomainAuditGenericDev(vm, "chardev",
                             virDomainAuditChardevPath(oldsrc),
                             virDomainAuditChardevPath(newsrc),
                             reason, success);
}


P
Peter Krempa 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static void
virDomainAuditSmartcard(virDomainObjPtr vm,
                        virDomainSmartcardDefPtr def,
                        const char *reason,
                        bool success)
{
    const char *database = VIR_DOMAIN_SMARTCARD_DEFAULT_DATABASE;
    size_t i;

    if (def) {
        switch ((virDomainSmartcardType) def->type) {
        case VIR_DOMAIN_SMARTCARD_TYPE_HOST:
            virDomainAuditGenericDev(vm, "smartcard",
                                     NULL, "nss-smartcard-device",
                                     reason, success);
            break;

        case VIR_DOMAIN_SMARTCARD_TYPE_HOST_CERTIFICATES:
            for (i = 0; i < VIR_DOMAIN_SMARTCARD_NUM_CERTIFICATES; i++) {
                virDomainAuditGenericDev(vm, "smartcard", NULL,
                                         def->data.cert.file[i],
                                         reason, success);
            }

            if (def->data.cert.database)
                database = def->data.cert.database;

            virDomainAuditGenericDev(vm, "smartcard",
                                     NULL, database,
                                     reason, success);
            break;

        case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH:
            virDomainAuditGenericDev(vm, "smartcard", NULL,
                                     virDomainAuditChardevPath(&def->data.passthru),
                                     reason, success);
            break;

        case VIR_DOMAIN_SMARTCARD_TYPE_LAST:
            break;
        }
    }
}


232 233
void
virDomainAuditDisk(virDomainObjPtr vm,
234 235 236 237
                   virStorageSourcePtr oldDef,
                   virStorageSourcePtr newDef,
                   const char *reason,
                   bool success)
238
{
239 240 241 242 243 244 245 246 247 248
    const char *oldsrc = NULL;
    const char *newsrc = NULL;

    if (oldDef && virStorageSourceIsLocalStorage(oldDef))
        oldsrc = oldDef->path;

    if (newDef && virStorageSourceIsLocalStorage(newDef))
        newsrc = newDef->path;

    virDomainAuditGenericDev(vm, "disk", oldsrc, newsrc, reason, success);
249 250 251
}


L
Luyao Huang 已提交
252
void
253
virDomainAuditRNG(virDomainObjPtr vm,
254
                  virDomainRNGDefPtr oldDef, virDomainRNGDefPtr newDef,
255 256 257 258 259 260
                  const char *reason, bool success)
{
    const char *newsrcpath = NULL;
    const char *oldsrcpath = NULL;

    if (newDef) {
261
        switch ((virDomainRNGBackend) newDef->backend) {
262
        case VIR_DOMAIN_RNG_BACKEND_RANDOM:
263
            newsrcpath = newDef->source.file;
264 265 266 267 268 269 270 271 272 273 274 275
            break;

        case VIR_DOMAIN_RNG_BACKEND_EGD:
            newsrcpath = virDomainAuditChardevPath(newDef->source.chardev);
            break;

        case VIR_DOMAIN_RNG_BACKEND_LAST:
            break;
        }
    }

    if (oldDef) {
276
        switch ((virDomainRNGBackend) oldDef->backend) {
277
        case VIR_DOMAIN_RNG_BACKEND_RANDOM:
278
            oldsrcpath = oldDef->source.file;
279 280 281 282 283 284 285 286 287 288 289
            break;

        case VIR_DOMAIN_RNG_BACKEND_EGD:
            oldsrcpath = virDomainAuditChardevPath(oldDef->source.chardev);
            break;

        case VIR_DOMAIN_RNG_BACKEND_LAST:
            break;
        }
    }

290
    virDomainAuditGenericDev(vm, "rng", oldsrcpath, newsrcpath, reason, success);
291 292 293
}


D
Daniel P. Berrange 已提交
294 295 296 297 298
void
virDomainAuditFS(virDomainObjPtr vm,
                 virDomainFSDefPtr oldDef, virDomainFSDefPtr newDef,
                 const char *reason, bool success)
{
299
    virDomainAuditGenericDev(vm, "fs",
300 301
                             oldDef ? oldDef->src->path : NULL,
                             newDef ? newDef->src->path : NULL,
302
                             reason, success);
D
Daniel P. Berrange 已提交
303 304 305
}


306
void
307 308 309
virDomainAuditNet(virDomainObjPtr vm,
                  virDomainNetDefPtr oldDef, virDomainNetDefPtr newDef,
                  const char *reason, bool success)
310 311 312 313 314
{
    char newMacstr[VIR_MAC_STRING_BUFLEN];
    char oldMacstr[VIR_MAC_STRING_BUFLEN];

    if (oldDef)
315
        virMacAddrFormat(&oldDef->mac, oldMacstr);
316

317
    if (newDef)
318
        virMacAddrFormat(&newDef->mac, newMacstr);
319

320 321 322 323
    virDomainAuditGenericDev(vm, "net",
                             oldDef ? oldMacstr : NULL,
                             newDef ? newMacstr : NULL,
                             reason, success);
324 325
}

326
/**
327
 * virDomainAuditNetDevice:
W
Wang Rui 已提交
328 329
 * @vmDef: the definition of the VM
 * @netDef: details of network device that fd will be tied to
330 331
 * @device: device being opened (such as /dev/vhost-net,
 * /dev/net/tun, /dev/tanN). Note that merely opening a device
332
 * does not mean that virDomain owns it; a followup virDomainAuditNet
333 334 335 336 337 338
 * shows whether the fd was passed on.
 * @success: true if the device was opened
 *
 * Log an audit message about an attempted network device open.
 */
void
339 340
virDomainAuditNetDevice(virDomainDefPtr vmDef, virDomainNetDefPtr netDef,
                        const char *device, bool success)
341 342 343 344
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char macstr[VIR_MAC_STRING_BUFLEN];
    char *vmname;
345
    char *dev_name = NULL;
346
    char *rdev;
347
    const char *virt;
348 349

    virUUIDFormat(vmDef->uuid, uuidstr);
350
    virMacAddrFormat(&netDef->mac, macstr);
351
    rdev = virDomainAuditGetRdev(device);
352 353

    if (!(vmname = virAuditEncode("vm", vmDef->name)) ||
354
        !(dev_name = virAuditEncode("path", device))) {
355
        VIR_WARN("OOM while encoding audit message");
356 357 358
        goto cleanup;
    }

359 360 361 362 363
    if (!(virt = virDomainVirtTypeToString(vmDef->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vmDef->virtType);
        virt = "?";
    }

364
    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
365
              "virt=%s resrc=net reason=open %s uuid=%s net=%s %s rdev=%s",
366
              virt, vmname, uuidstr, macstr, dev_name, VIR_AUDIT_STR(rdev));
367

368
 cleanup:
369
    VIR_FREE(vmname);
370
    VIR_FREE(dev_name);
371 372
    VIR_FREE(rdev);
}
373

374
/**
375
 * virDomainAuditHostdev:
376 377
 * @vm: domain making a change in pass-through host device
 * @hostdev: device being attached or removed
378
 * @reason: one of "start", "attach", or "detach"
379 380 381 382 383
 * @success: true if the device passthrough operation succeeded
 *
 * Log an audit message about an attempted device passthrough change.
 */
void
384 385
virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
                      const char *reason, bool success)
386 387 388
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
389 390
    char *address = NULL;
    char *device = NULL;
391
    const char *virt;
392
    virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
393
    virDomainHostdevSubsysPCIPtr pcisrc = &hostdev->source.subsys.u.pci;
394
    virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
395 396 397

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
398
        VIR_WARN("OOM while encoding audit message");
399 400 401
        return;
    }

402 403 404 405 406
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

407 408 409 410
    switch (hostdev->mode) {
    case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
        switch (hostdev->source.subsys.type) {
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
411
            if (virAsprintfQuiet(&address, "%.4x:%.2x:%.2x.%.1x",
412 413 414 415
                                 pcisrc->addr.domain,
                                 pcisrc->addr.bus,
                                 pcisrc->addr.slot,
                                 pcisrc->addr.function) < 0) {
416 417 418 419 420
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
            }
            break;
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
421
            if (virAsprintfQuiet(&address, "%.3d.%.3d",
422
                                 usbsrc->bus, usbsrc->device) < 0) {
423 424
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
H
Han Cheng 已提交
425 426
            }
            break;
427
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
428 429 430 431 432
            if (scsisrc->protocol ==
                VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
                /* Follow virDomainAuditDisk && virDomainAuditGenericDev
                 * and don't audit the networked device.
                 */
H
Han Cheng 已提交
433
                goto cleanup;
434 435 436
            } else {
                virDomainHostdevSubsysSCSIHostPtr scsihostsrc =
                    &scsisrc->u.host;
437
                if (virAsprintfQuiet(&address, "%s:%u:%u:%llu",
438 439 440 441 442 443
                                     scsihostsrc->adapter, scsihostsrc->bus,
                                     scsihostsrc->target,
                                     scsihostsrc->unit) < 0) {
                    VIR_WARN("OOM while encoding audit message");
                    goto cleanup;
                }
444 445
            }
            break;
446
        }
447 448 449 450 451 452 453
        default:
            VIR_WARN("Unexpected hostdev type while encoding audit message: %d",
                     hostdev->source.subsys.type);
            goto cleanup;
        }

        if (!(device = virAuditEncode("device", VIR_AUDIT_STR(address)))) {
454
            VIR_WARN("OOM while encoding audit message");
455 456
            goto cleanup;
        }
457 458 459 460 461 462

        VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                  "virt=%s resrc=dev reason=%s %s uuid=%s bus=%s %s",
                  virt, reason, vmname, uuidstr,
                  virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type),
                  device);
463
        break;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    case VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES:
        switch (hostdev->source.caps.type) {
        case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE:
            if (!(device = virAuditEncode("disk",
                                          VIR_AUDIT_STR(hostdev->source.caps.u.storage.block)))) {
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
            }

            VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                      "virt=%s resrc=hostdev reason=%s %s uuid=%s %s",
                      virt, reason, vmname, uuidstr, device);
            break;

        case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC:
            if (!(device = virAuditEncode("chardev",
                                          VIR_AUDIT_STR(hostdev->source.caps.u.misc.chardev)))) {
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
            }

            VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                      "virt=%s resrc=hostdev reason=%s %s uuid=%s %s",
                      virt, reason, vmname, uuidstr, device);
            break;

        default:
            VIR_WARN("Unexpected hostdev type while encoding audit message: %d",
                     hostdev->source.caps.type);
494 495 496 497
            goto cleanup;
        }
        break;

498 499 500
    default:
        VIR_WARN("Unexpected hostdev mode while encoding audit message: %d",
                 hostdev->mode);
501 502 503
        goto cleanup;
    }

504
 cleanup:
505 506 507 508 509 510
    VIR_FREE(vmname);
    VIR_FREE(device);
    VIR_FREE(address);
}


511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
/**
 * virDomainAuditRedirdev:
 * @vm: domain making a change in pass-through host device
 * @redirdev: device being attached or removed
 * @reason: one of "start", "attach", or "detach"
 * @success: true if the device passthrough operation succeeded
 *
 * Log an audit message about an attempted device passthrough change.
 */
void
virDomainAuditRedirdev(virDomainObjPtr vm, virDomainRedirdevDefPtr redirdev,
                      const char *reason, bool success)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
526 527
    char *address = NULL;
    char *device = NULL;
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    const char *virt;

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
        VIR_WARN("OOM while encoding audit message");
        return;
    }

    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

    switch (redirdev->bus) {
    case VIR_DOMAIN_REDIRDEV_BUS_USB:
543
        if (VIR_STRDUP_QUIET(address, "USB redirdev") < 0) {
544 545 546
            VIR_WARN("OOM while encoding audit message");
            goto cleanup;
        }
547
        break;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    default:
        VIR_WARN("Unexpected redirdev bus while encoding audit message: %d",
                 redirdev->bus);
        goto cleanup;
    }

    if (!(device = virAuditEncode("device", VIR_AUDIT_STR(address)))) {
        VIR_WARN("OOM while encoding audit message");
        goto cleanup;
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
              "virt=%s resrc=dev reason=%s %s uuid=%s bus=%s %s",
              virt, reason, vmname, uuidstr,
              virDomainRedirdevBusTypeToString(redirdev->bus),
              device);

565
 cleanup:
566 567 568 569 570 571
    VIR_FREE(vmname);
    VIR_FREE(device);
    VIR_FREE(address);
}


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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
/**
 * virDomainAuditTPM:
 * @vm: domain making a change in pass-through host device
 * @tpm: TPM device being attached or removed
 * @reason: one of "start", "attach", or "detach"
 * @success: true if the device passthrough operation succeeded
 *
 * Log an audit message about an attempted device passthrough change.
 */
static void
virDomainAuditTPM(virDomainObjPtr vm, virDomainTPMDefPtr tpm,
                  const char *reason, bool success)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
    char *path = NULL;
    char *device = NULL;
    const char *virt;

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
        VIR_WARN("OOM while encoding audit message");
        return;
    }

    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

    switch (tpm->type) {
    case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
        path = tpm->data.passthrough.source.data.file.path;
        if (!(device = virAuditEncode("device", VIR_AUDIT_STR(path)))) {
            VIR_WARN("OOM while encoding audit message");
            goto cleanup;
        }

        VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                  "virt=%s resrc=dev reason=%s %s uuid=%s %s",
                  virt, reason, vmname, uuidstr, device);
        break;
    default:
        break;
    }

618
 cleanup:
619 620 621 622 623
    VIR_FREE(vmname);
    VIR_FREE(device);
}


624
/**
625
 * virDomainAuditCgroup:
626 627 628
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
629 630
 * @extra: additional details, in the form "all",
 * "major category=xyz maj=nn", or "path path=xyz dev=nn:mm" (the
631 632
 * latter two are generated by virDomainAuditCgroupMajor and
 * virDomainAuditCgroupPath).
633 634 635 636
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
637
void
638 639
virDomainAuditCgroup(virDomainObjPtr vm, virCgroupPtr cgroup,
                     const char *reason, const char *extra, bool success)
640 641 642
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
643 644
    char *controller = NULL;
    char *detail;
645
    const char *virt;
646 647 648

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
649
        VIR_WARN("OOM while encoding audit message");
650 651
        return;
    }
652

653 654 655 656 657
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

E
Eric Blake 已提交
658 659 660
    ignore_value(virCgroupPathOfController(cgroup,
                                           VIR_CGROUP_CONTROLLER_DEVICES,
                                           NULL, &controller));
661 662
    detail = virAuditEncode("cgroup", VIR_AUDIT_STR(controller));

663
    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
664 665
              "virt=%s resrc=cgroup reason=%s %s uuid=%s %s class=%s",
              virt, reason, vmname, uuidstr,
666
              detail ? detail : "cgroup=?", extra);
667 668

    VIR_FREE(vmname);
669 670
    VIR_FREE(controller);
    VIR_FREE(detail);
671 672 673
}

/**
674
 * virDomainAuditCgroupMajor:
675 676 677 678 679
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
 * @maj: the major number of the device category
 * @name: a textual name for that device category, alphabetic only
680
 * @perms: string containing "r", "w", and/or "m" as appropriate
681 682 683 684 685
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
void
686 687 688
virDomainAuditCgroupMajor(virDomainObjPtr vm, virCgroupPtr cgroup,
                          const char *reason, int maj, const char *name,
                          const char *perms, bool success)
689 690 691
{
    char *extra;

692 693
    if (virAsprintfQuiet(&extra, "major category=%s maj=%02X acl=%s",
                         name, maj, perms) < 0) {
694
        VIR_WARN("OOM while encoding audit message");
695 696 697
        return;
    }

698
    virDomainAuditCgroup(vm, cgroup, reason, extra, success);
699 700 701 702 703

    VIR_FREE(extra);
}

/**
704
 * virDomainAuditCgroupPath:
705 706 707 708
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
 * @path: the device being adjusted
709
 * @perms: string containing "r", "w", and/or "m" as appropriate
710 711 712 713 714 715
 * @rc: > 0 if not a device, 0 if success, < 0 if failure
 *
 * Log an audit message about an attempted cgroup device ACL change to
 * a specific device.
 */
void
716 717 718
virDomainAuditCgroupPath(virDomainObjPtr vm, virCgroupPtr cgroup,
                         const char *reason, const char *path, const char *perms,
                         int rc)
719 720 721
{
    char *detail;
    char *rdev;
722
    char *extra = NULL;
723 724 725 726 727

    /* Nothing to audit for regular files.  */
    if (rc > 0)
        return;

728
    rdev = virDomainAuditGetRdev(path);
729 730

    if (!(detail = virAuditEncode("path", path)) ||
731 732
        virAsprintfQuiet(&extra, "path %s rdev=%s acl=%s",
                         detail, VIR_AUDIT_STR(rdev), perms) < 0) {
733
        VIR_WARN("OOM while encoding audit message");
734 735 736
        goto cleanup;
    }

737
    virDomainAuditCgroup(vm, cgroup, reason, extra, rc == 0);
738

739
 cleanup:
740
    VIR_FREE(extra);
741
    VIR_FREE(detail);
742
    VIR_FREE(rdev);
743 744
}

745
/**
746
 * virDomainAuditResource:
747 748 749 750 751 752 753 754 755 756
 * @vm: domain making an integer resource change
 * @resource: name of the resource: "mem" or "vcpu"
 * @oldval: the old value of the resource
 * @newval: the new value of the resource
 * @reason: either "start" or "update"
 * @success: true if the resource change succeeded
 *
 * Log an audit message about an attempted resource change.
 */
static void
757 758 759
virDomainAuditResource(virDomainObjPtr vm, const char *resource,
                       unsigned long long oldval, unsigned long long newval,
                       const char *reason, bool success)
760 761 762
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
763
    const char *virt;
764 765 766

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
767
        VIR_WARN("OOM while encoding audit message");
768 769 770
        return;
    }

771 772 773 774 775
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

776
    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
777 778
              "virt=%s resrc=%s reason=%s %s uuid=%s old-%s=%lld new-%s=%lld",
              virt, resource, reason, vmname, uuidstr,
779 780 781 782 783 784
              resource, oldval, resource, newval);

    VIR_FREE(vmname);
}

void
785 786 787
virDomainAuditMemory(virDomainObjPtr vm,
                     unsigned long long oldmem, unsigned long long newmem,
                     const char *reason, bool success)
788
{
789
    return virDomainAuditResource(vm, "mem", oldmem, newmem, reason, success);
790 791 792
}

void
793 794 795
virDomainAuditVcpu(virDomainObjPtr vm,
                   unsigned int oldvcpu, unsigned int newvcpu,
                   const char *reason, bool success)
796
{
797
    return virDomainAuditResource(vm, "vcpu", oldvcpu, newvcpu, reason, success);
798 799
}

800 801 802 803 804 805 806 807 808
void
virDomainAuditIOThread(virDomainObjPtr vm,
                       unsigned int oldiothread, unsigned int newiothread,
                       const char *reason, bool success)
{
    return virDomainAuditResource(vm, "iothread", oldiothread, newiothread,
                                  reason, success);
}

809
static void
810 811
virDomainAuditLifecycle(virDomainObjPtr vm, const char *op,
                        const char *reason, bool success)
812 813 814
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
815
    const char *virt;
816 817 818 819

    virUUIDFormat(vm->def->uuid, uuidstr);

    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
820
        VIR_WARN("OOM while encoding audit message");
821 822 823
        return;
    }

824 825 826 827 828
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

829
    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_CONTROL, success,
830 831
              "virt=%s op=%s reason=%s %s uuid=%s vm-pid=%lld",
              virt, op, reason, vmname, uuidstr, (long long)vm->pid);
832 833 834 835 836

    VIR_FREE(vmname);
}


837
void
838
virDomainAuditStart(virDomainObjPtr vm, const char *reason, bool success)
839
{
840
    size_t i;
841

842 843
    for (i = 0; i < vm->def->ndisks; i++)
        virDomainAuditDisk(vm, NULL, vm->def->disks[i]->src, "start", true);
844

845
    for (i = 0; i < vm->def->nfss; i++) {
D
Daniel P. Berrange 已提交
846 847 848 849
        virDomainFSDefPtr fs = vm->def->fss[i];
        virDomainAuditFS(vm, NULL, fs, "start", true);
    }

850
    for (i = 0; i < vm->def->nnets; i++) {
851
        virDomainNetDefPtr net = vm->def->nets[i];
852
        virDomainAuditNet(vm, NULL, net, "start", true);
853 854
    }

855
    for (i = 0; i < vm->def->nhostdevs; i++) {
856
        virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
857
        virDomainAuditHostdev(vm, hostdev, "start", true);
858 859
    }

860
    for (i = 0; i < vm->def->nredirdevs; i++) {
861 862 863 864
        virDomainRedirdevDefPtr redirdev = vm->def->redirdevs[i];
        virDomainAuditRedirdev(vm, redirdev, "start", true);
    }

865 866 867 868 869 870 871 872 873 874 875 876 877
    for (i = 0; i < vm->def->nserials; i++)
        virDomainAuditChardev(vm, NULL, vm->def->serials[i], "start", true);

    for (i = 0; i < vm->def->nparallels; i++)
        virDomainAuditChardev(vm, NULL, vm->def->parallels[i], "start", true);

    for (i = 0; i < vm->def->nchannels; i++)
        virDomainAuditChardev(vm, NULL, vm->def->channels[i], "start", true);

    for (i = 0; i < vm->def->nconsoles; i++) {
        if (i == 0 &&
            (vm->def->consoles[i]->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL ||
             vm->def->consoles[i]->targetType == VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_NONE) &&
878
             vm->def->os.type == VIR_DOMAIN_OSTYPE_HVM)
879 880 881 882 883
            continue;

        virDomainAuditChardev(vm, NULL, vm->def->consoles[i], "start", true);
    }

P
Peter Krempa 已提交
884 885 886
    for (i = 0; i < vm->def->nsmartcards; i++)
        virDomainAuditSmartcard(vm, vm->def->smartcards[i], "start", true);

887 888
    for (i = 0; i < vm->def->nrngs; i++)
        virDomainAuditRNG(vm, NULL, vm->def->rngs[i], "start", true);
889

890 891 892
    if (vm->def->tpm)
        virDomainAuditTPM(vm, vm->def->tpm, "start", true);

893 894 895
    for (i = 0; i < vm->def->nshmems; i++)
        virDomainAuditShmem(vm, vm->def->shmems[i], "start", true);

896
    virDomainAuditMemory(vm, 0, virDomainDefGetMemoryTotal(vm->def),
897
                         "start", true);
898
    virDomainAuditVcpu(vm, 0, virDomainDefGetVcpus(vm->def), "start", true);
899 900
    if (vm->def->niothreadids)
        virDomainAuditIOThread(vm, 0, vm->def->niothreadids, "start", true);
901

902
    virDomainAuditLifecycle(vm, "start", reason, success);
903 904
}

905 906
void
virDomainAuditInit(virDomainObjPtr vm,
907 908
                   pid_t initpid,
                   ino_t pidns)
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
    const char *virt;

    virUUIDFormat(vm->def->uuid, uuidstr);

    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
        VIR_WARN("OOM while encoding audit message");
        return;
    }

    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_CONTROL, true,
927 928 929
              "virt=%s op=init %s uuid=%s vm-pid=%lld init-pid=%lld pid-ns=%lld",
              virt, vmname, uuidstr, (long long)vm->pid, (long long)initpid,
              (long long)pidns);
930 931 932

    VIR_FREE(vmname);
}
933

934
void
935
virDomainAuditStop(virDomainObjPtr vm, const char *reason)
936
{
937
    virDomainAuditLifecycle(vm, "stop", reason, true);
938 939
}

940
void
941
virDomainAuditSecurityLabel(virDomainObjPtr vm, bool success)
942 943 944
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
945
    const char *virt;
946
    size_t i;
947 948 949

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
950
        VIR_WARN("OOM while encoding audit message");
951 952 953
        return;
    }

954 955 956 957 958
    if (!(virt = virDomainVirtTypeToString(vm->def->virtType))) {
        VIR_WARN("Unexpected virt type %d while encoding audit message", vm->def->virtType);
        virt = "?";
    }

959 960 961 962 963 964 965 966
    for (i = 0; i < vm->def->nseclabels; i++) {
        VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_ID, success,
                  "virt=%s %s uuid=%s vm-ctx=%s img-ctx=%s model=%s",
                  virt, vmname, uuidstr,
                  VIR_AUDIT_STR(vm->def->seclabels[i]->label),
                  VIR_AUDIT_STR(vm->def->seclabels[i]->imagelabel),
                  VIR_AUDIT_STR(vm->def->seclabels[i]->model));
    }
967 968 969

    VIR_FREE(vmname);
}
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985

void
virDomainAuditShmem(virDomainObjPtr vm,
                    virDomainShmemDefPtr def,
                    const char *reason, bool success)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname = virAuditEncode("vm", vm->def->name);
    const char *srcpath = virDomainAuditChardevPath(&def->server.chr);
    char *src = virAuditEncode("server", VIR_AUDIT_STR(srcpath));
    char *shmem = virAuditEncode("shmem", VIR_AUDIT_STR(def->name));
    const char *virt = virDomainVirtTypeToString(vm->def->virtType);
    char *size = NULL;

    virUUIDFormat(vm->def->uuid, uuidstr);

986
    if (!vmname || !src || !shmem ||
987 988 989 990 991 992 993 994 995 996 997 998 999
        virAsprintfQuiet(&size, "%llu", def->size) < 0) {
        VIR_WARN("OOM while encoding audit message");
        goto cleanup;
    }

    if (!virt) {
        VIR_WARN("Unexpected virt type %d while encoding audit message",
                 vm->def->virtType);
        virt = "?";
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
              "virt=%s resrc=shmem reason=%s %s uuid=%s size=%s %s %s",
1000
              virt, reason, vmname, uuidstr, size, shmem, src);
1001 1002 1003 1004 1005 1006 1007 1008

 cleanup:
    VIR_FREE(vmname);
    VIR_FREE(src);
    VIR_FREE(size);
    VIR_FREE(shmem);
    return;
}