qemu_audit.c 12.7 KB
Newer Older
1 2 3
/*
 * qemu_audit.c: QEMU audit management
 *
4
 * Copyright (C) 2006-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

26 27 28
#include <sys/stat.h>
#include <sys/types.h>

29 30 31 32 33 34
#include "qemu_audit.h"
#include "virtaudit.h"
#include "uuid.h"
#include "logging.h"
#include "memory.h"

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* 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 *
qemuAuditGetRdev(const char *path)
{
    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);
        virAsprintf(&ret, "%02X:%02X", maj, min);
    }
    return ret;
}
#else
static char *
qemuAuditGetRdev(const char *path ATTRIBUTE_UNUSED)
{
    return NULL;
}
#endif

60 61 62 63
void
qemuAuditDisk(virDomainObjPtr vm,
              virDomainDiskDefPtr oldDef, virDomainDiskDefPtr newDef,
              const char *reason, bool success)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
    char *oldsrc = NULL;
    char *newsrc = NULL;

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

    if (!(oldsrc = virAuditEncode("old-disk",
                                  oldDef && oldDef->src ?
                                  oldDef->src : "?"))) {
        VIR_WARN0("OOM while encoding audit message");
        goto cleanup;
    }
    if (!(newsrc = virAuditEncode("new-disk",
                                  newDef && newDef->src ?
                                  newDef->src : "?"))) {
        VIR_WARN0("OOM while encoding audit message");
        goto cleanup;
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
              "resrc=disk reason=%s %s uuid=%s %s %s",
              reason, vmname, uuidstr,
              oldsrc, newsrc);

cleanup:
    VIR_FREE(vmname);
    VIR_FREE(oldsrc);
    VIR_FREE(newsrc);
}


101 102 103 104
void
qemuAuditNet(virDomainObjPtr vm,
             virDomainNetDefPtr oldDef, virDomainNetDefPtr newDef,
             const char *reason, bool success)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char newMacstr[VIR_MAC_STRING_BUFLEN];
    char oldMacstr[VIR_MAC_STRING_BUFLEN];
    char *vmname;

    virUUIDFormat(vm->def->uuid, uuidstr);
    if (oldDef)
        virFormatMacAddr(oldDef->mac, oldMacstr);
    if (newDef)
        virFormatMacAddr(newDef->mac, newMacstr);
    if (!(vmname = virAuditEncode("vm", vm->def->name))) {
        VIR_WARN0("OOM while encoding audit message");
        return;
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
              "resrc=net reason=%s %s uuid=%s old-net='%s' new-net='%s'",
              reason, vmname, uuidstr,
              oldDef ? oldMacstr : "?",
              newDef ? newMacstr : "?");

    VIR_FREE(vmname);
}


131
/**
132
 * qemuAuditHostdev:
133 134
 * @vm: domain making a change in pass-through host device
 * @hostdev: device being attached or removed
135
 * @reason: one of "start", "attach", or "detach"
136 137 138 139 140
 * @success: true if the device passthrough operation succeeded
 *
 * Log an audit message about an attempted device passthrough change.
 */
void
141 142
qemuAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
                 const char *reason, bool success)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
    char *address;
    char *device;

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

    switch (hostdev->source.subsys.type) {
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
        if (virAsprintf(&address, "%.4x:%.2x:%.2x.%.1x",
                        hostdev->source.subsys.u.pci.domain,
                        hostdev->source.subsys.u.pci.bus,
                        hostdev->source.subsys.u.pci.slot,
                        hostdev->source.subsys.u.pci.function) < 0) {
            VIR_WARN0("OOM while encoding audit message");
            goto cleanup;
        }
        break;
    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
        if (virAsprintf(&address, "%.3d.%.3d",
                        hostdev->source.subsys.u.usb.bus,
                        hostdev->source.subsys.u.usb.device) < 0) {
            VIR_WARN0("OOM while encoding audit message");
            goto cleanup;
        }
        break;
    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)))) {
        VIR_WARN0("OOM while encoding audit message");
        goto cleanup;
    }

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
186
              "resrc=dev reason=%s %s uuid=%s bus=%s %s",
187 188 189 190 191 192 193 194 195 196 197
              reason, vmname, uuidstr,
              virDomainHostdevSubsysTypeToString(hostdev->source.subsys.type),
              device);

cleanup:
    VIR_FREE(vmname);
    VIR_FREE(device);
    VIR_FREE(address);
}


198
/**
199
 * qemuAuditCgroup:
200 201 202
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
203 204 205 206
 * @extra: additional details, in the form "all",
 * "major category=xyz maj=nn", or "path path=xyz dev=nn:mm" (the
 * latter two are generated by qemuAuditCgroupMajor and
 * qemuAuditCgroupPath).
207 208 209 210
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
211
void
212
qemuAuditCgroup(virDomainObjPtr vm, virCgroupPtr cgroup,
213
                const char *reason, const char *extra, bool success)
214 215 216
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;
217 218
    char *controller = NULL;
    char *detail;
219 220 221 222 223 224

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

226 227 228 229
    virCgroupPathOfController(cgroup, VIR_CGROUP_CONTROLLER_DEVICES,
                              NULL, &controller);
    detail = virAuditEncode("cgroup", VIR_AUDIT_STR(controller));

230
    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
231 232 233
              "resrc=cgroup reason=%s %s uuid=%s %s class=%s",
              reason, vmname, uuidstr,
              detail ? detail : "cgroup=?", extra);
234 235

    VIR_FREE(vmname);
236 237
    VIR_FREE(controller);
    VIR_FREE(detail);
238 239 240 241 242 243 244 245 246
}

/**
 * qemuAuditCgroupMajor:
 * @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
247
 * @perms: string containing "r", "w", and/or "m" as appropriate
248 249 250 251 252 253 254
 * @success: true if the cgroup operation succeeded
 *
 * Log an audit message about an attempted cgroup device ACL change.
 */
void
qemuAuditCgroupMajor(virDomainObjPtr vm, virCgroupPtr cgroup,
                     const char *reason, int maj, const char *name,
255
                     const char *perms, bool success)
256 257 258
{
    char *extra;

259 260
    if (virAsprintf(&extra, "major category=%s maj=%02X acl=%s",
                    name, maj, perms) < 0) {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        VIR_WARN0("OOM while encoding audit message");
        return;
    }

    qemuAuditCgroup(vm, cgroup, reason, extra, success);

    VIR_FREE(extra);
}

/**
 * qemuAuditCgroupPath:
 * @vm: domain making the cgroups ACL change
 * @cgroup: cgroup that manages the devices
 * @reason: either "allow" or "deny"
 * @path: the device being adjusted
276
 * @perms: string containing "r", "w", and/or "m" as appropriate
277 278 279 280 281 282 283
 * @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
qemuAuditCgroupPath(virDomainObjPtr vm, virCgroupPtr cgroup,
284 285
                    const char *reason, const char *path, const char *perms,
                    int rc)
286 287 288 289 290 291 292 293 294 295 296 297
{
    char *detail;
    char *rdev;
    char *extra;

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

    rdev = qemuAuditGetRdev(path);

    if (!(detail = virAuditEncode("path", path)) ||
298 299
        virAsprintf(&extra, "path path=%s rdev=%s acl=%s",
                    path, VIR_AUDIT_STR(rdev), perms) < 0) {
300 301 302 303
        VIR_WARN0("OOM while encoding audit message");
        goto cleanup;
    }

304
    qemuAuditCgroup(vm, cgroup, reason, extra, rc == 0);
305 306

cleanup:
307
    VIR_FREE(extra);
308
    VIR_FREE(detail);
309
    VIR_FREE(rdev);
310 311
}

312
/**
313
 * qemuAuditResource:
314 315 316 317 318 319 320 321 322 323
 * @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
324 325 326
qemuAuditResource(virDomainObjPtr vm, const char *resource,
                  unsigned long long oldval, unsigned long long newval,
                  const char *reason, bool success)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;

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

    VIR_AUDIT(VIR_AUDIT_RECORD_RESOURCE, success,
              "resrc=%s reason=%s %s uuid=%s old-%s=%lld new-%s=%lld",
              resource, reason, vmname, uuidstr,
              resource, oldval, resource, newval);

    VIR_FREE(vmname);
}

void
346 347 348
qemuAuditMemory(virDomainObjPtr vm,
                unsigned long long oldmem, unsigned long long newmem,
                const char *reason, bool success)
349
{
350
    return qemuAuditResource(vm, "mem", oldmem, newmem, reason, success);
351 352 353
}

void
354 355 356
qemuAuditVcpu(virDomainObjPtr vm,
              unsigned int oldvcpu, unsigned int newvcpu,
              const char *reason, bool success)
357
{
358
    return qemuAuditResource(vm, "vcpu", oldvcpu, newvcpu, reason, success);
359 360
}

361 362 363
static void
qemuAuditLifecycle(virDomainObjPtr vm, const char *op,
                   const char *reason, bool success)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;

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

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

    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_CONTROL, success,
              "op=%s reason=%s %s uuid=%s", op, reason, vmname, uuidstr);

    VIR_FREE(vmname);
}


382 383
void
qemuAuditDomainStart(virDomainObjPtr vm, const char *reason, bool success)
384 385 386 387 388 389
{
    int i;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        virDomainDiskDefPtr disk = vm->def->disks[i];
        if (disk->src) /* Skips CDROM without media initially inserted */
390
            qemuAuditDisk(vm, NULL, disk, "start", true);
391 392 393 394
    }

    for (i = 0 ; i < vm->def->nnets ; i++) {
        virDomainNetDefPtr net = vm->def->nets[i];
395
        qemuAuditNet(vm, NULL, net, "start", true);
396 397
    }

398 399
    for (i = 0 ; i < vm->def->nhostdevs ; i++) {
        virDomainHostdevDefPtr hostdev = vm->def->hostdevs[i];
400
        qemuAuditHostdev(vm, hostdev, "start", true);
401 402
    }

403 404
    qemuAuditMemory(vm, 0, vm->def->mem.cur_balloon, "start", true);
    qemuAuditVcpu(vm, 0, vm->def->vcpus, "start", true);
405

406
    qemuAuditLifecycle(vm, "start", reason, success);
407 408 409
}


410 411
void
qemuAuditDomainStop(virDomainObjPtr vm, const char *reason)
412
{
413
    qemuAuditLifecycle(vm, "stop", reason, true);
414 415
}

416 417
void
qemuAuditSecurityLabel(virDomainObjPtr vm, bool success)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    char *vmname;

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

    VIR_AUDIT(VIR_AUDIT_RECORD_MACHINE_ID, success,
              "%s uuid=%s vm-ctx=%s img-ctx=%s",
              vmname, uuidstr,
              VIR_AUDIT_STR(vm->def->seclabel.label),
              VIR_AUDIT_STR(vm->def->seclabel.imagelabel));

    VIR_FREE(vmname);
}