domain_audit.c 31.4 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
 */

#include <config.h>

24 25
#include <sys/stat.h>

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

32 33
#include <sys/types.h>

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

41 42
VIR_LOG_INIT("conf.domain_audit");

43 44 45 46
/* 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 *
47
virDomainAuditGetRdev(const char *path)
48 49 50 51 52 53 54 55
{
    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);
56
        ignore_value(virAsprintfQuiet(&ret, "%02X:%02X", maj, min));
57 58 59 60 61
    }
    return ret;
}
#else
static char *
62
virDomainAuditGetRdev(const char *path ATTRIBUTE_UNUSED)
63 64 65 66 67
{
    return NULL;
}
#endif

68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static const char *
virDomainAuditGetVirtType(virDomainDefPtr def)
{
    const char *virt;

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

    return virt;
}


84 85 86 87 88 89 90
static void
virDomainAuditGenericDev(virDomainObjPtr vm,
                         const char *type,
                         const char *oldsrcpath,
                         const char *newsrcpath,
                         const char *reason,
                         bool success)
91
{
92 93
    char *newdev = NULL;
    char *olddev = NULL;
94
    char uuidstr[VIR_UUID_STRING_BUFLEN];
95
    char *vmname = NULL;
96 97
    char *oldsrc = NULL;
    char *newsrc = NULL;
98
    const char *virt = virDomainAuditGetVirtType(vm->def);
99

100 101
    /* if both new and old source aren't provided don't log anything */
    if (!newsrcpath && !oldsrcpath)
102
        return;
103 104 105 106 107 108 109 110 111 112 113

    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;
114

115 116 117 118 119
    if (!(newsrc = virAuditEncode(newdev, VIR_AUDIT_STR(newsrcpath))))
        goto no_memory;

    if (!(oldsrc = virAuditEncode(olddev, VIR_AUDIT_STR(oldsrcpath))))
        goto no_memory;
120 121

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
122 123
              "virt=%s resrc=%s reason=%s %s uuid=%s %s %s",
              virt, type, reason, vmname, uuidstr, oldsrc, newsrc);
124

125
 cleanup:
126 127
    VIR_FREE(newdev);
    VIR_FREE(olddev);
128 129 130
    VIR_FREE(vmname);
    VIR_FREE(oldsrc);
    VIR_FREE(newsrc);
131 132 133 134 135 136 137 138
    return;

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


139 140 141 142 143 144 145 146 147 148 149
void
virDomainAuditChardev(virDomainObjPtr vm,
                      virDomainChrDefPtr oldDef,
                      virDomainChrDefPtr newDef,
                      const char *reason,
                      bool success)
{
    virDomainChrSourceDefPtr oldsrc = NULL;
    virDomainChrSourceDefPtr newsrc = NULL;

    if (oldDef)
150
        oldsrc = oldDef->source;
151 152

    if (newDef)
153
        newsrc = newDef->source;
154 155

    virDomainAuditGenericDev(vm, "chardev",
156 157
                             virDomainChrSourceDefGetPath(oldsrc),
                             virDomainChrSourceDefGetPath(newsrc),
158 159 160 161
                             reason, success);
}


P
Peter Krempa 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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,
196
                                     virDomainChrSourceDefGetPath(def->data.passthru),
P
Peter Krempa 已提交
197 198 199 200 201 202 203 204 205 206
                                     reason, success);
            break;

        case VIR_DOMAIN_SMARTCARD_TYPE_LAST:
            break;
        }
    }
}


207 208
void
virDomainAuditDisk(virDomainObjPtr vm,
209 210 211 212
                   virStorageSourcePtr oldDef,
                   virStorageSourcePtr newDef,
                   const char *reason,
                   bool success)
213
{
214 215 216 217 218 219 220 221 222 223
    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);
224 225 226
}


L
Luyao Huang 已提交
227
void
228
virDomainAuditRNG(virDomainObjPtr vm,
229
                  virDomainRNGDefPtr oldDef, virDomainRNGDefPtr newDef,
230 231 232 233 234 235
                  const char *reason, bool success)
{
    const char *newsrcpath = NULL;
    const char *oldsrcpath = NULL;

    if (newDef) {
236
        switch ((virDomainRNGBackend) newDef->backend) {
237
        case VIR_DOMAIN_RNG_BACKEND_RANDOM:
238
            newsrcpath = newDef->source.file;
239 240 241
            break;

        case VIR_DOMAIN_RNG_BACKEND_EGD:
242
            newsrcpath = virDomainChrSourceDefGetPath(newDef->source.chardev);
243 244 245 246 247 248 249 250
            break;

        case VIR_DOMAIN_RNG_BACKEND_LAST:
            break;
        }
    }

    if (oldDef) {
251
        switch ((virDomainRNGBackend) oldDef->backend) {
252
        case VIR_DOMAIN_RNG_BACKEND_RANDOM:
253
            oldsrcpath = oldDef->source.file;
254 255 256
            break;

        case VIR_DOMAIN_RNG_BACKEND_EGD:
257
            oldsrcpath = virDomainChrSourceDefGetPath(oldDef->source.chardev);
258 259 260 261 262 263 264
            break;

        case VIR_DOMAIN_RNG_BACKEND_LAST:
            break;
        }
    }

265
    virDomainAuditGenericDev(vm, "rng", oldsrcpath, newsrcpath, reason, success);
266 267 268
}


D
Daniel P. Berrange 已提交
269 270 271 272 273
void
virDomainAuditFS(virDomainObjPtr vm,
                 virDomainFSDefPtr oldDef, virDomainFSDefPtr newDef,
                 const char *reason, bool success)
{
274
    virDomainAuditGenericDev(vm, "fs",
275 276
                             oldDef ? oldDef->src->path : NULL,
                             newDef ? newDef->src->path : NULL,
277
                             reason, success);
D
Daniel P. Berrange 已提交
278 279 280
}


281
void
282 283 284
virDomainAuditNet(virDomainObjPtr vm,
                  virDomainNetDefPtr oldDef, virDomainNetDefPtr newDef,
                  const char *reason, bool success)
285 286 287 288 289
{
    char newMacstr[VIR_MAC_STRING_BUFLEN];
    char oldMacstr[VIR_MAC_STRING_BUFLEN];

    if (oldDef)
290
        virMacAddrFormat(&oldDef->mac, oldMacstr);
291

292
    if (newDef)
293
        virMacAddrFormat(&newDef->mac, newMacstr);
294

295 296 297 298
    virDomainAuditGenericDev(vm, "net",
                             oldDef ? oldMacstr : NULL,
                             newDef ? newMacstr : NULL,
                             reason, success);
299 300
}

301
/**
302
 * virDomainAuditNetDevice:
W
Wang Rui 已提交
303 304
 * @vmDef: the definition of the VM
 * @netDef: details of network device that fd will be tied to
305 306
 * @device: device being opened (such as /dev/vhost-net,
 * /dev/net/tun, /dev/tanN). Note that merely opening a device
307
 * does not mean that virDomain owns it; a followup virDomainAuditNet
308 309 310 311 312 313
 * 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
314 315
virDomainAuditNetDevice(virDomainDefPtr vmDef, virDomainNetDefPtr netDef,
                        const char *device, bool success)
316 317 318 319
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char macstr[VIR_MAC_STRING_BUFLEN];
    char *vmname;
320
    char *dev_name = NULL;
321
    char *rdev;
322
    const char *virt = virDomainAuditGetVirtType(vmDef);
323 324

    virUUIDFormat(vmDef->uuid, uuidstr);
325
    virMacAddrFormat(&netDef->mac, macstr);
326
    rdev = virDomainAuditGetRdev(device);
327 328

    if (!(vmname = virAuditEncode("vm", vmDef->name)) ||
329
        !(dev_name = virAuditEncode("path", device))) {
330
        VIR_WARN("OOM while encoding audit message");
331 332 333 334
        goto cleanup;
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
335
              "virt=%s resrc=net reason=open %s uuid=%s net=%s %s rdev=%s",
336
              virt, vmname, uuidstr, macstr, dev_name, VIR_AUDIT_STR(rdev));
337

338
 cleanup:
339
    VIR_FREE(vmname);
340
    VIR_FREE(dev_name);
341 342
    VIR_FREE(rdev);
}
343

344
/**
345
 * virDomainAuditHostdev:
346 347
 * @vm: domain making a change in pass-through host device
 * @hostdev: device being attached or removed
348
 * @reason: one of "start", "attach", or "detach"
349 350 351 352 353
 * @success: true if the device passthrough operation succeeded
 *
 * Log an audit message about an attempted device passthrough change.
 */
void
354 355
virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
                      const char *reason, bool success)
356 357 358
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
359 360
    char *address = NULL;
    char *device = NULL;
361 362
    const char *virt = virDomainAuditGetVirtType(vm->def);

363
    virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
364
    virDomainHostdevSubsysPCIPtr pcisrc = &hostdev->source.subsys.u.pci;
365
    virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
366
    virDomainHostdevSubsysSCSIVHostPtr hostsrc = &hostdev->source.subsys.u.scsi_host;
367
    virDomainHostdevSubsysMediatedDevPtr mdevsrc = &hostdev->source.subsys.u.mdev;
368 369 370

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

375
    switch ((virDomainHostdevMode) hostdev->mode) {
376
    case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
377
        switch ((virDomainHostdevSubsysType) hostdev->source.subsys.type) {
378
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
379 380
            if (virAsprintfQuiet(&address,
                                 VIR_PCI_DEVICE_ADDRESS_FMT,
381 382 383 384
                                 pcisrc->addr.domain,
                                 pcisrc->addr.bus,
                                 pcisrc->addr.slot,
                                 pcisrc->addr.function) < 0) {
385 386 387 388 389
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
            }
            break;
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
390
            if (virAsprintfQuiet(&address, "%.3d.%.3d",
391
                                 usbsrc->bus, usbsrc->device) < 0) {
392 393
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
H
Han Cheng 已提交
394 395
            }
            break;
396
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: {
397 398 399 400 401
            if (scsisrc->protocol ==
                VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
                /* Follow virDomainAuditDisk && virDomainAuditGenericDev
                 * and don't audit the networked device.
                 */
H
Han Cheng 已提交
402
                goto cleanup;
403 404 405
            } else {
                virDomainHostdevSubsysSCSIHostPtr scsihostsrc =
                    &scsisrc->u.host;
406
                if (virAsprintfQuiet(&address, "%s:%u:%u:%llu",
407 408 409 410 411 412
                                     scsihostsrc->adapter, scsihostsrc->bus,
                                     scsihostsrc->target,
                                     scsihostsrc->unit) < 0) {
                    VIR_WARN("OOM while encoding audit message");
                    goto cleanup;
                }
413 414
            }
            break;
415
        }
416 417 418 419 420 421
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
            if (VIR_STRDUP_QUIET(address, hostsrc->wwpn) < 0) {
                VIR_WARN("OOM while encoding audit message");
                goto cleanup;
            }
            break;
422 423
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
            if (VIR_STRDUP_QUIET(address, mdevsrc->uuidstr) < 0) {
424
                VIR_WARN("OOM while encoding audit message");
425 426 427 428
                goto cleanup;
            }
            break;
        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
429 430 431 432 433 434 435
        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)))) {
436
            VIR_WARN("OOM while encoding audit message");
437 438
            goto cleanup;
        }
439 440 441 442 443 444

        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);
445
        break;
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    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);
476 477 478 479
            goto cleanup;
        }
        break;

480
    case VIR_DOMAIN_HOSTDEV_MODE_LAST:
481 482 483
    default:
        VIR_WARN("Unexpected hostdev mode while encoding audit message: %d",
                 hostdev->mode);
484 485 486
        goto cleanup;
    }

487
 cleanup:
488 489 490 491 492 493
    VIR_FREE(vmname);
    VIR_FREE(device);
    VIR_FREE(address);
}


494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
/**
 * 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;
509 510
    char *address = NULL;
    char *device = NULL;
511
    const char *virt = virDomainAuditGetVirtType(vm->def);
512 513 514 515 516 517 518 519 520

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

    switch (redirdev->bus) {
    case VIR_DOMAIN_REDIRDEV_BUS_USB:
521
        if (VIR_STRDUP_QUIET(address, "USB redirdev") < 0) {
522 523 524
            VIR_WARN("OOM while encoding audit message");
            goto cleanup;
        }
525
        break;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    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);

543
 cleanup:
544 545 546 547 548 549
    VIR_FREE(vmname);
    VIR_FREE(device);
    VIR_FREE(address);
}


550 551
/**
 * virDomainAuditTPM:
552
 * @vm: domain making a change in pass-through host device or emulator
553 554
 * @tpm: TPM device being attached or removed
 * @reason: one of "start", "attach", or "detach"
555
 * @success: true if the device operation succeeded
556
 *
557 558
 * Log an audit message about an attempted device passthrough or emulator
 * change.
559 560 561 562 563 564 565 566 567
 */
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;
568
    const char *virt = virDomainAuditGetVirtType(vm->def);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

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

    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,
585
                  "virt=%s resrc=tpm reason=%s %s uuid=%s %s",
586 587
                  virt, reason, vmname, uuidstr, device);
        break;
588
    case VIR_DOMAIN_TPM_TYPE_EMULATOR:
589 590 591 592 593 594 595 596 597
        path = tpm->data.emulator.source.data.nix.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=tpm-emulator reason=%s %s uuid=%s %s",
                  virt, reason, vmname, uuidstr, device);
598
        break;
599
    case VIR_DOMAIN_TPM_TYPE_LAST:
600 601 602 603
    default:
        break;
    }

604
 cleanup:
605 606 607 608 609
    VIR_FREE(vmname);
    VIR_FREE(device);
}


610
/**
611
 * virDomainAuditCgroup:
612 613 614
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
615 616
 * @extra: additional details, in the form "all",
 * "major category=xyz maj=nn", or "path path=xyz dev=nn:mm" (the
617 618
 * latter two are generated by virDomainAuditCgroupMajor and
 * virDomainAuditCgroupPath).
619 620 621 622
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
623
void
624 625
virDomainAuditCgroup(virDomainObjPtr vm, virCgroupPtr cgroup,
                     const char *reason, const char *extra, bool success)
626 627 628
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
629 630
    char *controller = NULL;
    char *detail;
631
    const char *virt = virDomainAuditGetVirtType(vm->def);
632 633 634

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

E
Eric Blake 已提交
639 640 641
    ignore_value(virCgroupPathOfController(cgroup,
                                           VIR_CGROUP_CONTROLLER_DEVICES,
                                           NULL, &controller));
642 643
    detail = virAuditEncode("cgroup", VIR_AUDIT_STR(controller));

644
    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
645 646
              "virt=%s resrc=cgroup reason=%s %s uuid=%s %s class=%s",
              virt, reason, vmname, uuidstr,
647
              detail ? detail : "cgroup=?", extra);
648 649

    VIR_FREE(vmname);
650 651
    VIR_FREE(controller);
    VIR_FREE(detail);
652 653 654
}

/**
655
 * virDomainAuditCgroupMajor:
656 657 658 659 660
 * @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
661
 * @perms: string containing "r", "w", and/or "m" as appropriate
662 663 664 665 666
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
void
667 668 669
virDomainAuditCgroupMajor(virDomainObjPtr vm, virCgroupPtr cgroup,
                          const char *reason, int maj, const char *name,
                          const char *perms, bool success)
670 671 672
{
    char *extra;

673 674
    if (virAsprintfQuiet(&extra, "major category=%s maj=%02X acl=%s",
                         name, maj, perms) < 0) {
675
        VIR_WARN("OOM while encoding audit message");
676 677 678
        return;
    }

679
    virDomainAuditCgroup(vm, cgroup, reason, extra, success);
680 681 682 683 684

    VIR_FREE(extra);
}

/**
685
 * virDomainAuditCgroupPath:
686 687 688 689
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
 * @path: the device being adjusted
690
 * @perms: string containing "r", "w", and/or "m" as appropriate
691 692 693 694 695 696
 * @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
697 698 699
virDomainAuditCgroupPath(virDomainObjPtr vm, virCgroupPtr cgroup,
                         const char *reason, const char *path, const char *perms,
                         int rc)
700 701 702
{
    char *detail;
    char *rdev;
703
    char *extra = NULL;
704 705 706 707 708

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

709
    rdev = virDomainAuditGetRdev(path);
710 711

    if (!(detail = virAuditEncode("path", path)) ||
712 713
        virAsprintfQuiet(&extra, "path %s rdev=%s acl=%s",
                         detail, VIR_AUDIT_STR(rdev), perms) < 0) {
714
        VIR_WARN("OOM while encoding audit message");
715 716 717
        goto cleanup;
    }

718
    virDomainAuditCgroup(vm, cgroup, reason, extra, rc == 0);
719

720
 cleanup:
721
    VIR_FREE(extra);
722
    VIR_FREE(detail);
723
    VIR_FREE(rdev);
724 725
}

726
/**
727
 * virDomainAuditResource:
728 729 730 731 732 733 734 735 736 737
 * @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
738 739 740
virDomainAuditResource(virDomainObjPtr vm, const char *resource,
                       unsigned long long oldval, unsigned long long newval,
                       const char *reason, bool success)
741 742 743
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
744
    const char *virt = virDomainAuditGetVirtType(vm->def);
745 746 747

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

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
753 754
              "virt=%s resrc=%s reason=%s %s uuid=%s old-%s=%lld new-%s=%lld",
              virt, resource, reason, vmname, uuidstr,
755 756 757 758 759 760
              resource, oldval, resource, newval);

    VIR_FREE(vmname);
}

void
761 762 763
virDomainAuditMemory(virDomainObjPtr vm,
                     unsigned long long oldmem, unsigned long long newmem,
                     const char *reason, bool success)
764
{
765
    return virDomainAuditResource(vm, "mem", oldmem, newmem, reason, success);
766 767 768
}

void
769 770 771
virDomainAuditVcpu(virDomainObjPtr vm,
                   unsigned int oldvcpu, unsigned int newvcpu,
                   const char *reason, bool success)
772
{
773
    return virDomainAuditResource(vm, "vcpu", oldvcpu, newvcpu, reason, success);
774 775
}

776 777 778 779 780 781 782 783 784
void
virDomainAuditIOThread(virDomainObjPtr vm,
                       unsigned int oldiothread, unsigned int newiothread,
                       const char *reason, bool success)
{
    return virDomainAuditResource(vm, "iothread", oldiothread, newiothread,
                                  reason, success);
}

785
static void
786 787
virDomainAuditLifecycle(virDomainObjPtr vm, const char *op,
                        const char *reason, bool success)
788 789 790
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
791
    const char *virt = virDomainAuditGetVirtType(vm->def);
792 793 794 795

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

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

    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_CONTROL, success,
801 802
              "virt=%s op=%s reason=%s %s uuid=%s vm-pid=%lld",
              virt, op, reason, vmname, uuidstr, (long long)vm->pid);
803 804 805 806 807

    VIR_FREE(vmname);
}


808
void
809
virDomainAuditStart(virDomainObjPtr vm, const char *reason, bool success)
810
{
811
    size_t i;
812

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

816
    for (i = 0; i < vm->def->nfss; i++) {
D
Daniel P. Berrange 已提交
817 818 819 820
        virDomainFSDefPtr fs = vm->def->fss[i];
        virDomainAuditFS(vm, NULL, fs, "start", true);
    }

821
    for (i = 0; i < vm->def->nnets; i++) {
822
        virDomainNetDefPtr net = vm->def->nets[i];
823
        virDomainAuditNet(vm, NULL, net, "start", true);
824 825
    }

826
    for (i = 0; i < vm->def->nhostdevs; i++) {
827
        virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
828
        virDomainAuditHostdev(vm, hostdev, "start", true);
829 830
    }

831
    for (i = 0; i < vm->def->nredirdevs; i++) {
832 833 834 835
        virDomainRedirdevDefPtr redirdev = vm->def->redirdevs[i];
        virDomainAuditRedirdev(vm, redirdev, "start", true);
    }

836 837 838 839 840 841 842 843 844 845 846 847 848
    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) &&
849
             vm->def->os.type == VIR_DOMAIN_OSTYPE_HVM)
850 851 852 853 854
            continue;

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

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

858 859
    for (i = 0; i < vm->def->nrngs; i++)
        virDomainAuditRNG(vm, NULL, vm->def->rngs[i], "start", true);
860

861 862 863
    if (vm->def->tpm)
        virDomainAuditTPM(vm, vm->def->tpm, "start", true);

864 865 866
    for (i = 0; i < vm->def->nshmems; i++)
        virDomainAuditShmem(vm, vm->def->shmems[i], "start", true);

867 868 869
    for (i = 0; i < vm->def->ninputs; i++)
        virDomainAuditInput(vm, vm->def->inputs[i], "start", true);

870
    virDomainAuditMemory(vm, 0, virDomainDefGetMemoryTotal(vm->def),
871
                         "start", true);
872
    virDomainAuditVcpu(vm, 0, virDomainDefGetVcpus(vm->def), "start", true);
873 874
    if (vm->def->niothreadids)
        virDomainAuditIOThread(vm, 0, vm->def->niothreadids, "start", true);
875

876
    virDomainAuditLifecycle(vm, "start", reason, success);
877 878
}

879 880
void
virDomainAuditInit(virDomainObjPtr vm,
881 882
                   pid_t initpid,
                   ino_t pidns)
883 884 885
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
886
    const char *virt = virDomainAuditGetVirtType(vm->def);
887 888 889 890 891 892 893 894 895

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

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

    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_CONTROL, true,
896 897 898
              "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);
899 900 901

    VIR_FREE(vmname);
}
902

903
void
904
virDomainAuditStop(virDomainObjPtr vm, const char *reason)
905
{
906
    virDomainAuditLifecycle(vm, "stop", reason, true);
907 908
}

909
void
910
virDomainAuditSecurityLabel(virDomainObjPtr vm, bool success)
911 912 913
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
914
    size_t i;
915
    const char *virt = virDomainAuditGetVirtType(vm->def);
916 917 918

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

923 924 925 926 927 928 929 930
    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));
    }
931 932 933

    VIR_FREE(vmname);
}
934 935 936 937 938 939 940 941

void
virDomainAuditShmem(virDomainObjPtr vm,
                    virDomainShmemDefPtr def,
                    const char *reason, bool success)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname = virAuditEncode("vm", vm->def->name);
942
    const char *srcpath = virDomainChrSourceDefGetPath(&def->server.chr);
943
    const char *virt = virDomainAuditGetVirtType(vm->def);
944
    char *shmpath = NULL;
945 946 947

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

948 949
    if (!vmname ||
        virAsprintfQuiet(&shmpath, "/dev/shm/%s", def->name) < 0) {
950 951 952 953 954 955 956 957 958 959
        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 = "?";
    }

960 961 962 963 964 965 966 967 968
    if (def->server.enabled) {
        VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                  "virt=%s resrc=ivshmem-socket reason=%s %s uuid=%s path=%s",
                  virt, reason, vmname, uuidstr, VIR_AUDIT_STR(srcpath));
    } else {
        VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                  "virt=%s resrc=shmem reason=%s %s uuid=%s size=%llu path=%s",
                  virt, reason, vmname, uuidstr, def->size, VIR_AUDIT_STR(shmpath));
   }
969 970 971

 cleanup:
    VIR_FREE(vmname);
972
    VIR_FREE(shmpath);
973 974
    return;
}
975 976 977 978 979 980 981 982 983 984


void
virDomainAuditInput(virDomainObjPtr vm,
                    virDomainInputDefPtr input,
                    const char *reason,
                    bool success)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
985
    const char *virt = virDomainAuditGetVirtType(vm->def);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

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

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

    switch ((virDomainInputType) input->type) {
    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
    case VIR_DOMAIN_INPUT_TYPE_TABLET:
    case VIR_DOMAIN_INPUT_TYPE_KBD:
        break;

    case VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH:
        VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
                  "virt=%s resrc=evdev reason=%s %s uuid=%s path=%s",
                  virt, reason, vmname, uuidstr, VIR_AUDIT_STR(input->source.evdev));
        break;

    case VIR_DOMAIN_INPUT_TYPE_LAST:
        break;
    }

 cleanup:
    VIR_FREE(vmname);
    return;

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