Import QEMU upstream snapshot d2e570c

Upstream: https://gitlab.com/qemu-project/qemu.git

Upstream-Commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
This commit is contained in:
2026-08-31 02:15:30 +02:00
commit cf256aa081
11315 changed files with 3598369 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
config TPM_BACKEND
bool
depends on TPM
config TPM_PASSTHROUGH
bool
default y
# FIXME: should check for x86 host as well
depends on TPM_BACKEND && LINUX
config TPM_EMULATOR
bool
default y
depends on TPM_BACKEND
+6
View File
@@ -0,0 +1,6 @@
if have_tpm
system_ss.add(files('tpm_backend.c'))
system_ss.add(files('tpm_util.c'))
system_ss.add(when: 'CONFIG_TPM_PASSTHROUGH', if_true: files('tpm_passthrough.c'))
system_ss.add(when: 'CONFIG_TPM_EMULATOR', if_true: files('tpm_emulator.c'))
endif
+206
View File
@@ -0,0 +1,206 @@
/*
* QEMU TPM Backend
*
* Copyright IBM, Corp. 2013
*
* Authors:
* Stefan Berger <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
* Based on backends/rng.c by Anthony Liguori
*/
#include "qemu/osdep.h"
#include "system/tpm_backend.h"
#include "qapi/error.h"
#include "system/tpm.h"
#include "qemu/thread.h"
#include "qemu/main-loop.h"
#include "qemu/module.h"
#include "block/thread-pool.h"
#include "qemu/error-report.h"
static void tpm_backend_request_completed(void *opaque, int ret)
{
TPMBackend *s = TPM_BACKEND(opaque);
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
tic->request_completed(s->tpmif, ret);
/* no need for atomic, as long the BQL is taken */
s->cmd = NULL;
object_unref(OBJECT(s));
}
static int tpm_backend_worker_thread(gpointer data)
{
TPMBackend *s = TPM_BACKEND(data);
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
Error *err = NULL;
k->handle_request(s, s->cmd, &err);
if (err) {
error_report_err(err);
return -1;
}
return 0;
}
void tpm_backend_finish_sync(TPMBackend *s)
{
while (s->cmd) {
aio_poll(qemu_get_aio_context(), true);
}
}
enum TpmType tpm_backend_get_type(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
return k->type;
}
int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp)
{
if (s->tpmif) {
error_setg(errp, "TPM backend '%s' is already initialized", s->id);
return -1;
}
s->tpmif = tpmif;
object_ref(OBJECT(tpmif));
s->had_startup_error = false;
return 0;
}
int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize)
{
int res = 0;
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
/* terminate a running TPM */
tpm_backend_finish_sync(s);
res = k->startup_tpm ? k->startup_tpm(s, buffersize) : 0;
s->had_startup_error = (res != 0);
return res;
}
bool tpm_backend_had_startup_error(TPMBackend *s)
{
return s->had_startup_error;
}
void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd)
{
if (s->cmd != NULL) {
error_report("There is a TPM request pending");
return;
}
s->cmd = cmd;
object_ref(OBJECT(s));
thread_pool_submit_aio(tpm_backend_worker_thread, s,
tpm_backend_request_completed, s);
}
void tpm_backend_reset(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
if (k->reset) {
k->reset(s);
}
tpm_backend_finish_sync(s);
s->had_startup_error = false;
}
void tpm_backend_cancel_cmd(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
k->cancel_cmd(s);
}
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
return k->get_tpm_established_flag ?
k->get_tpm_established_flag(s) : false;
}
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
return k->reset_tpm_established_flag ?
k->reset_tpm_established_flag(s, locty) : 0;
}
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
return k->get_tpm_version(s);
}
size_t tpm_backend_get_buffer_size(TPMBackend *s)
{
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
return k->get_buffer_size(s);
}
TPMInfo *tpm_backend_query_tpm(TPMBackend *s)
{
TPMInfo *info = g_new0(TPMInfo, 1);
TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
TPMIfClass *tic = TPM_IF_GET_CLASS(s->tpmif);
info->id = g_strdup(s->id);
info->model = tic->model;
info->options = k->get_tpm_options(s);
return info;
}
static void tpm_backend_instance_finalize(Object *obj)
{
TPMBackend *s = TPM_BACKEND(obj);
object_unref(OBJECT(s->tpmif));
g_free(s->id);
}
static const TypeInfo tpm_backend_info = {
.name = TYPE_TPM_BACKEND,
.parent = TYPE_OBJECT,
.instance_size = sizeof(TPMBackend),
.instance_finalize = tpm_backend_instance_finalize,
.class_size = sizeof(TPMBackendClass),
.abstract = true,
};
static const TypeInfo tpm_if_info = {
.name = TYPE_TPM_IF,
.parent = TYPE_INTERFACE,
.class_size = sizeof(TPMIfClass),
};
static void register_types(void)
{
type_register_static(&tpm_backend_info);
type_register_static(&tpm_if_info);
}
type_init(register_types);
File diff suppressed because it is too large Load Diff
+88
View File
@@ -0,0 +1,88 @@
/*
* TPM configuration
*
* Copyright (C) 2011-2013 IBM Corporation
*
* Authors:
* Stefan Berger <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef BACKENDS_TPM_INT_H
#define BACKENDS_TPM_INT_H
#include "qemu/option.h"
#include "system/tpm.h"
#define TPM_STANDARD_CMDLINE_OPTS \
{ \
.name = "type", \
.type = QEMU_OPT_STRING, \
.help = "Type of TPM backend", \
}
struct tpm_req_hdr {
uint16_t tag;
uint32_t len;
uint32_t ordinal;
} QEMU_PACKED;
struct tpm_resp_hdr {
uint16_t tag;
uint32_t len;
uint32_t errcode;
} QEMU_PACKED;
#define TPM_TAG_RQU_COMMAND 0xc1
#define TPM_TAG_RQU_AUTH1_COMMAND 0xc2
#define TPM_TAG_RQU_AUTH2_COMMAND 0xc3
#define TPM_TAG_RSP_COMMAND 0xc4
#define TPM_TAG_RSP_AUTH1_COMMAND 0xc5
#define TPM_TAG_RSP_AUTH2_COMMAND 0xc6
#define TPM_BAD_PARAMETER 3
#define TPM_FAIL 9
#define TPM_KEYNOTFOUND 13
#define TPM_BAD_PARAM_SIZE 25
#define TPM_ENCRYPT_ERROR 32
#define TPM_DECRYPT_ERROR 33
#define TPM_BAD_KEY_PROPERTY 40
#define TPM_BAD_MODE 44
#define TPM_BAD_VERSION 46
#define TPM_BAD_LOCALITY 61
#define TPM_ORD_ContinueSelfTest 0x53
#define TPM_ORD_GetTicks 0xf1
#define TPM_ORD_GetCapability 0x65
#define TPM_CAP_PROPERTY 0x05
#define TPM_CAP_PROP_INPUT_BUFFER 0x124
/* TPM2 defines */
#define TPM2_ST_NO_SESSIONS 0x8001
#define TPM2_CC_ReadClock 0x00000181
#define TPM2_CC_GetCapability 0x0000017a
#define TPM2_CAP_TPM_PROPERTIES 0x6
#define TPM2_PT_MAX_COMMAND_SIZE 0x11e
#define TPM_RC_INSUFFICIENT 0x9a
#define TPM_RC_FAILURE 0x101
#define TPM_RC_LOCALITY 0x907
int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
size_t *buffersize);
typedef struct TPMSizedBuffer {
uint32_t size;
uint8_t *buffer;
} TPMSizedBuffer;
void tpm_sized_buffer_reset(TPMSizedBuffer *tsb);
#endif /* BACKENDS_TPM_INT_H */
+344
View File
@@ -0,0 +1,344 @@
/*
* tpm_ioctl.h
*
* (c) Copyright IBM Corporation 2014, 2015.
*
* This file is licensed under the terms of the 3-clause BSD license
*/
#ifndef _TPM_IOCTL_H_
#define _TPM_IOCTL_H_
#if defined(__CYGWIN__)
# define __USE_LINUX_IOCTL_DEFS
#endif
#ifndef _WIN32
#include <sys/uio.h>
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_IOCCOM_H
#include <sys/ioccom.h>
#endif
/*
* Every response from a command involving a TPM command execution must hold
* the ptm_res as the first element.
* ptm_res corresponds to the error code of a command executed by the TPM.
*/
typedef uint32_t ptm_res;
/* PTM_GET_CAPABILITY: Get supported capabilities (ioctl's) */
struct ptm_cap_n {
union {
struct {
ptm_res tpm_result; /* will always be TPM_SUCCESS (0) */
uint32_t caps;
} resp; /* response */
} u;
};
/* PTM_GET_TPMESTABLISHED: get the establishment bit */
struct ptm_est {
union {
struct {
ptm_res tpm_result;
unsigned char bit; /* TPM established bit */
} resp; /* response */
} u;
};
/* PTM_RESET_TPMESTABLISHED: reset establishment bit */
struct ptm_reset_est {
union {
struct {
uint8_t loc; /* locality to use */
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
/* PTM_INIT */
struct ptm_init {
union {
struct {
uint32_t init_flags; /* see definitions below */
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
/* above init_flags */
#define PTM_INIT_FLAG_DELETE_VOLATILE (1 << 0)
/* delete volatile state file after reading it */
/* PTM_SET_LOCALITY */
struct ptm_loc {
union {
struct {
uint8_t loc; /* locality to set */
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
/* PTM_HASH_DATA: hash given data */
struct ptm_hdata {
union {
struct {
uint32_t length;
uint8_t data[4096];
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
/*
* size of the TPM state blob to transfer; x86_64 can handle 8k,
* ppc64le only ~7k; keep the response below a 4k page size
*/
#define PTM_STATE_BLOB_SIZE (3 * 1024)
/*
* The following is the data structure to get state blobs from the TPM.
* If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple reads
* with this ioctl and with adjusted offset are necessary. All bytes
* must be transferred and the transfer is done once the last byte has been
* returned.
* It is possible to use the read() interface for reading the data; however, the
* first bytes of the state blob will be part of the response to the ioctl(); a
* subsequent read() is only necessary if the total length (totlength) exceeds
* the number of received bytes. seek() is not supported.
*/
struct ptm_getstate {
union {
struct {
uint32_t state_flags; /* may be: PTM_STATE_FLAG_DECRYPTED */
uint32_t type; /* which blob to pull */
uint32_t offset; /* offset from where to read */
} req; /* request */
struct {
ptm_res tpm_result;
uint32_t state_flags; /* may be: PTM_STATE_FLAG_ENCRYPTED */
uint32_t totlength; /* total length that will be transferred */
uint32_t length; /* number of bytes in following buffer */
uint8_t data[PTM_STATE_BLOB_SIZE];
} resp; /* response */
} u;
};
/* TPM state blob types */
#define PTM_BLOB_TYPE_PERMANENT 1
#define PTM_BLOB_TYPE_VOLATILE 2
#define PTM_BLOB_TYPE_SAVESTATE 3
/* state_flags above : */
#define PTM_STATE_FLAG_DECRYPTED 1 /* on input: get decrypted state */
#define PTM_STATE_FLAG_ENCRYPTED 2 /* on output: state is encrypted */
/*
* The following is the data structure to set state blobs in the TPM.
* If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple
* 'writes' using this ioctl are necessary. The last packet is indicated
* by the length being smaller than the PTM_STATE_BLOB_SIZE.
* The very first packet may have a length indicator of '0' enabling
* a write() with all the bytes from a buffer. If the write() interface
* is used, a final ioctl with a non-full buffer must be made to indicate
* that all data were transferred (a write with 0 bytes would not work).
*/
struct ptm_setstate {
union {
struct {
uint32_t state_flags; /* may be PTM_STATE_FLAG_ENCRYPTED */
uint32_t type; /* which blob to set */
uint32_t length; /* length of the data;
use 0 on the first packet to
transfer using write() */
uint8_t data[PTM_STATE_BLOB_SIZE];
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
/*
* PTM_GET_CONFIG: Data structure to get runtime configuration information
* such as which keys are applied.
*/
struct ptm_getconfig {
union {
struct {
ptm_res tpm_result;
uint32_t flags;
} resp; /* response */
} u;
};
#define PTM_CONFIG_FLAG_FILE_KEY 0x1
#define PTM_CONFIG_FLAG_MIGRATION_KEY 0x2
/*
* PTM_SET_BUFFERSIZE: Set the buffer size to be used by the TPM.
* A 0 on input queries for the current buffer size. Any other
* number will try to set the buffer size. The returned number is
* the buffer size that will be used, which can be larger than the
* requested one, if it was below the minimum, or smaller than the
* requested one, if it was above the maximum.
*/
struct ptm_setbuffersize {
union {
struct {
uint32_t buffersize; /* 0 to query for current buffer size */
} req; /* request */
struct {
ptm_res tpm_result;
uint32_t buffersize; /* buffer size in use */
uint32_t minsize; /* min. supported buffer size */
uint32_t maxsize; /* max. supported buffer size */
} resp; /* response */
} u;
};
#define PTM_GETINFO_SIZE (3 * 1024)
/*
* PTM_GET_INFO: Get info about the TPM implementation (from libtpms)
*
* This request allows to indirectly call TPMLIB_GetInfo(flags) and
* retrieve information from libtpms.
* Only one transaction is currently necessary for returning results
* to a client. Therefore, totlength and length will be the same if
* offset is 0.
*/
struct ptm_getinfo {
union {
struct {
uint64_t flags;
uint32_t offset; /* offset from where to read */
uint32_t pad; /* 32 bit arch */
} req; /* request */
struct {
ptm_res tpm_result;
uint32_t totlength;
uint32_t length;
char buffer[PTM_GETINFO_SIZE];
} resp; /* response */
} u;
};
#define SWTPM_INFO_TPMSPECIFICATION ((uint64_t)1 << 0)
#define SWTPM_INFO_TPMATTRIBUTES ((uint64_t)1 << 1)
/*
* PTM_LOCK_STORAGE: Lock the storage and retry n times
*/
struct ptm_lockstorage {
union {
struct {
uint32_t retries; /* number of retries */
} req; /* request */
struct {
ptm_res tpm_result;
} resp; /* response */
} u;
};
typedef uint64_t ptm_cap; /* CUSE-only; use ptm_cap_n otherwise */
typedef struct ptm_cap_n ptm_cap_n;
typedef struct ptm_est ptm_est;
typedef struct ptm_reset_est ptm_reset_est;
typedef struct ptm_loc ptm_loc;
typedef struct ptm_hdata ptm_hdata;
typedef struct ptm_init ptm_init;
typedef struct ptm_getstate ptm_getstate;
typedef struct ptm_setstate ptm_setstate;
typedef struct ptm_getconfig ptm_getconfig;
typedef struct ptm_setbuffersize ptm_setbuffersize;
typedef struct ptm_getinfo ptm_getinfo;
typedef struct ptm_lockstorage ptm_lockstorage;
/* capability flags returned by PTM_GET_CAPABILITY */
#define PTM_CAP_INIT (1)
#define PTM_CAP_SHUTDOWN (1 << 1)
#define PTM_CAP_GET_TPMESTABLISHED (1 << 2)
#define PTM_CAP_SET_LOCALITY (1 << 3)
#define PTM_CAP_HASHING (1 << 4)
#define PTM_CAP_CANCEL_TPM_CMD (1 << 5)
#define PTM_CAP_STORE_VOLATILE (1 << 6)
#define PTM_CAP_RESET_TPMESTABLISHED (1 << 7)
#define PTM_CAP_GET_STATEBLOB (1 << 8)
#define PTM_CAP_SET_STATEBLOB (1 << 9)
#define PTM_CAP_STOP (1 << 10)
#define PTM_CAP_GET_CONFIG (1 << 11)
#define PTM_CAP_SET_DATAFD (1 << 12)
#define PTM_CAP_SET_BUFFERSIZE (1 << 13)
#define PTM_CAP_GET_INFO (1 << 14)
#define PTM_CAP_SEND_COMMAND_HEADER (1 << 15)
#define PTM_CAP_LOCK_STORAGE (1 << 16)
#if !defined(_WIN32) && !defined(__GNU__)
enum {
PTM_GET_CAPABILITY = _IOR('P', 0, ptm_cap),
PTM_INIT = _IOWR('P', 1, ptm_init),
PTM_SHUTDOWN = _IOR('P', 2, ptm_res),
PTM_GET_TPMESTABLISHED = _IOR('P', 3, ptm_est),
PTM_SET_LOCALITY = _IOWR('P', 4, ptm_loc),
PTM_HASH_START = _IOR('P', 5, ptm_res),
PTM_HASH_DATA = _IOWR('P', 6, ptm_hdata),
PTM_HASH_END = _IOR('P', 7, ptm_res),
PTM_CANCEL_TPM_CMD = _IOR('P', 8, ptm_res),
PTM_STORE_VOLATILE = _IOR('P', 9, ptm_res),
PTM_RESET_TPMESTABLISHED = _IOWR('P', 10, ptm_reset_est),
PTM_GET_STATEBLOB = _IOWR('P', 11, ptm_getstate),
PTM_SET_STATEBLOB = _IOWR('P', 12, ptm_setstate),
PTM_STOP = _IOR('P', 13, ptm_res),
PTM_GET_CONFIG = _IOR('P', 14, ptm_getconfig),
PTM_SET_DATAFD = _IOR('P', 15, ptm_res),
PTM_SET_BUFFERSIZE = _IOWR('P', 16, ptm_setbuffersize),
PTM_GET_INFO = _IOWR('P', 17, ptm_getinfo),
PTM_LOCK_STORAGE = _IOWR('P', 18, ptm_lockstorage),
};
#endif
/*
* Commands used by the non-CUSE TPMs
*
* All messages container big-endian data.
*
* The return messages only contain the 'resp' part of the unions
* in the data structures above. Besides that the limits in the
* buffers above (ptm_hdata:u.req.data and ptm_get_state:u.resp.data
* and ptm_set_state:u.req.data) are 0xffffffff.
*/
enum {
CMD_GET_CAPABILITY = 1, /* 0x01 */
CMD_INIT, /* 0x02 */
CMD_SHUTDOWN, /* 0x03 */
CMD_GET_TPMESTABLISHED, /* 0x04 */
CMD_SET_LOCALITY, /* 0x05 */
CMD_HASH_START, /* 0x06 */
CMD_HASH_DATA, /* 0x07 */
CMD_HASH_END, /* 0x08 */
CMD_CANCEL_TPM_CMD, /* 0x09 */
CMD_STORE_VOLATILE, /* 0x0a */
CMD_RESET_TPMESTABLISHED, /* 0x0b */
CMD_GET_STATEBLOB, /* 0x0c */
CMD_SET_STATEBLOB, /* 0x0d */
CMD_STOP, /* 0x0e */
CMD_GET_CONFIG, /* 0x0f */
CMD_SET_DATAFD, /* 0x10 */
CMD_SET_BUFFERSIZE, /* 0x11 */
CMD_GET_INFO, /* 0x12 */
CMD_LOCK_STORAGE, /* 0x13 */
};
#endif /* _TPM_IOCTL_H_ */
+401
View File
@@ -0,0 +1,401 @@
/*
* passthrough TPM driver
*
* Copyright (c) 2010 - 2013 IBM Corporation
* Authors:
* Stefan Berger <[email protected]>
*
* Copyright (C) 2011 IAIK, Graz University of Technology
* Author: Andreas Niederl
*
* 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, see <http://www.gnu.org/licenses/>
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "qemu/module.h"
#include "qemu/sockets.h"
#include "system/tpm_backend.h"
#include "system/tpm_util.h"
#include "tpm_int.h"
#include "qapi/clone-visitor.h"
#include "qapi/qapi-visit-tpm.h"
#include "trace.h"
#include "qom/object.h"
#define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
OBJECT_DECLARE_SIMPLE_TYPE(TPMPassthruState, TPM_PASSTHROUGH)
/* data structures */
struct TPMPassthruState {
TPMBackend parent;
TPMPassthroughOptions *options;
const char *tpm_dev;
int tpm_fd;
bool tpm_executing;
bool tpm_op_canceled;
int cancel_fd;
TPMVersion tpm_version;
size_t tpm_buffersize;
};
#define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
/* functions */
static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
{
int ret;
reread:
ret = read(fd, buf, len);
if (ret < 0) {
if (errno != EINTR && errno != EAGAIN) {
return -1;
}
goto reread;
}
return ret;
}
static void tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
const uint8_t *in, uint32_t in_len,
uint8_t *out, uint32_t out_len,
bool *selftest_done, Error **errp)
{
ssize_t ret;
bool is_selftest;
/* FIXME: protect shared variables or use other sync mechanism */
tpm_pt->tpm_op_canceled = false;
tpm_pt->tpm_executing = true;
*selftest_done = false;
is_selftest = tpm_util_is_selftest(in, in_len);
ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
if (ret != in_len) {
if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
error_setg_errno(errp, errno, "tpm_passthrough: error while "
"transmitting data to TPM");
}
goto err_exit;
}
tpm_pt->tpm_executing = false;
ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
if (ret < 0) {
if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
error_setg_errno(errp, errno, "tpm_passthrough: error while "
"reading data from TPM");
}
} else if (ret < sizeof(struct tpm_resp_hdr) ||
tpm_cmd_get_size(out) != ret) {
ret = -1;
error_setg_errno(errp, errno, "tpm_passthrough: received invalid "
"response packet from TPM");
}
if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
*selftest_done = tpm_cmd_get_errcode(out) == 0;
}
err_exit:
if (ret < 0) {
tpm_util_write_fatal_error_response(out, out_len);
}
tpm_pt->tpm_executing = false;
}
static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
Error **errp)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
trace_tpm_passthrough_handle_request(cmd);
tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
cmd->out, cmd->out_len, &cmd->selftest_done,
errp);
}
static void tpm_passthrough_reset(TPMBackend *tb)
{
trace_tpm_passthrough_reset();
tpm_passthrough_cancel_cmd(tb);
}
static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
{
return false;
}
static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
uint8_t locty)
{
/* only a TPM 2.0 will support this */
return 0;
}
static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
int n;
/*
* As of Linux 3.7 the tpm_tis driver does not properly cancel
* commands on all TPM manufacturers' TPMs.
* Only cancel if we're busy so we don't cancel someone else's
* command, e.g., a command executed on the host.
*/
if (tpm_pt->tpm_executing) {
if (tpm_pt->cancel_fd >= 0) {
tpm_pt->tpm_op_canceled = true;
n = write(tpm_pt->cancel_fd, "-", 1);
if (n != 1) {
error_report("Canceling TPM command failed: %s",
strerror(errno));
}
} else {
error_report("Cannot cancel TPM command due to missing "
"TPM sysfs cancel entry");
}
}
}
static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
return tpm_pt->tpm_version;
}
static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
int ret;
ret = tpm_util_get_buffer_size(tpm_pt->tpm_fd, tpm_pt->tpm_version,
&tpm_pt->tpm_buffersize);
if (ret < 0) {
tpm_pt->tpm_buffersize = 4096;
}
return tpm_pt->tpm_buffersize;
}
/*
* Unless path or file descriptor set has been provided by user,
* determine the sysfs cancel file following kernel documentation
* in Documentation/ABI/stable/sysfs-class-tpm.
* From /dev/tpm0 create /sys/class/tpm/tpm0/device/cancel
* before 4.0: /sys/class/misc/tpm0/device/cancel
*/
static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
{
int fd = -1;
const char *dev;
char path[PATH_MAX];
if (tpm_pt->options->cancel_path) {
fd = qemu_open_old(tpm_pt->options->cancel_path, O_WRONLY);
if (fd < 0) {
error_report("tpm_passthrough: Could not open TPM cancel path: %s",
strerror(errno));
}
return fd;
}
dev = strrchr(tpm_pt->tpm_dev, '/');
if (!dev) {
error_report("tpm_passthrough: Bad TPM device path %s",
tpm_pt->tpm_dev);
return -1;
}
dev++;
if (snprintf(path, sizeof(path), "/sys/class/tpm/%s/device/cancel",
dev) < sizeof(path)) {
fd = qemu_open_old(path, O_WRONLY);
if (fd < 0) {
if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
dev) < sizeof(path)) {
fd = qemu_open_old(path, O_WRONLY);
}
}
}
if (fd < 0) {
error_report("tpm_passthrough: Could not guess TPM cancel path");
} else {
tpm_pt->options->cancel_path = g_strdup(path);
}
return fd;
}
static int
tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
{
const char *value;
value = qemu_opt_get(opts, "cancel-path");
if (value) {
tpm_pt->options->cancel_path = g_strdup(value);
}
value = qemu_opt_get(opts, "path");
if (value) {
tpm_pt->options->path = g_strdup(value);
}
tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
tpm_pt->tpm_fd = qemu_open_old(tpm_pt->tpm_dev, O_RDWR);
if (tpm_pt->tpm_fd < 0) {
error_report("Cannot access TPM device using '%s': %s",
tpm_pt->tpm_dev, strerror(errno));
return -1;
}
if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
error_report("'%s' is not a TPM device.",
tpm_pt->tpm_dev);
return -1;
}
tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
if (tpm_pt->cancel_fd < 0) {
return -1;
}
return 0;
}
static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
{
Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
object_unref(obj);
return NULL;
}
return TPM_BACKEND(obj);
}
static int tpm_passthrough_startup_tpm(TPMBackend *tb, size_t buffersize)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
if (buffersize && buffersize < tpm_pt->tpm_buffersize) {
error_report("Requested buffer size of %zu is smaller than host TPM's "
"fixed buffer size of %zu",
buffersize, tpm_pt->tpm_buffersize);
return -1;
}
return 0;
}
static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
{
TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
options->type = TPM_TYPE_PASSTHROUGH;
options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
TPM_PASSTHROUGH(tb)->options);
return options;
}
static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
TPM_STANDARD_CMDLINE_OPTS,
{
.name = "cancel-path",
.type = QEMU_OPT_STRING,
.help = "Sysfs file entry for canceling TPM commands",
},
{
.name = "path",
.type = QEMU_OPT_STRING,
.help = "Path to TPM device on the host",
},
{ /* end of list */ },
};
static void tpm_passthrough_inst_init(Object *obj)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
tpm_pt->tpm_fd = -1;
tpm_pt->cancel_fd = -1;
}
static void tpm_passthrough_inst_finalize(Object *obj)
{
TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
if (tpm_pt->tpm_fd >= 0) {
qemu_close(tpm_pt->tpm_fd);
}
if (tpm_pt->cancel_fd >= 0) {
qemu_close(tpm_pt->cancel_fd);
}
qapi_free_TPMPassthroughOptions(tpm_pt->options);
}
static void tpm_passthrough_class_init(ObjectClass *klass, const void *data)
{
TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
tbc->type = TPM_TYPE_PASSTHROUGH;
tbc->opts = tpm_passthrough_cmdline_opts;
tbc->desc = "Passthrough TPM backend driver";
tbc->create = tpm_passthrough_create;
tbc->startup_tpm = tpm_passthrough_startup_tpm;
tbc->reset = tpm_passthrough_reset;
tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
tbc->reset_tpm_established_flag =
tpm_passthrough_reset_tpm_established_flag;
tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
tbc->get_buffer_size = tpm_passthrough_get_buffer_size;
tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
tbc->handle_request = tpm_passthrough_handle_request;
}
static const TypeInfo tpm_passthrough_info = {
.name = TYPE_TPM_PASSTHROUGH,
.parent = TYPE_TPM_BACKEND,
.instance_size = sizeof(TPMPassthruState),
.class_init = tpm_passthrough_class_init,
.instance_init = tpm_passthrough_inst_init,
.instance_finalize = tpm_passthrough_inst_finalize,
};
static void tpm_passthrough_register(void)
{
type_register_static(&tpm_passthrough_info);
}
type_init(tpm_passthrough_register)
+360
View File
@@ -0,0 +1,360 @@
/*
* TPM utility functions
*
* Copyright (c) 2010 - 2015 IBM Corporation
* Authors:
* Stefan Berger <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "qemu/cutils.h"
#include "qapi/error.h"
#include "qapi/visitor.h"
#include "tpm_int.h"
#include "system/memory.h"
#include "hw/core/qdev-properties.h"
#include "system/tpm_backend.h"
#include "system/tpm_util.h"
#include "trace.h"
/* tpm backend property */
static void get_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp)
{
TPMBackend **be = object_field_prop_ptr(obj, opaque);
char *p;
p = g_strdup(*be ? (*be)->id : "");
visit_type_str(v, name, &p, errp);
g_free(p);
}
static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp)
{
const Property *prop = opaque;
TPMBackend *s, **be = object_field_prop_ptr(obj, prop);
char *str;
if (!visit_type_str(v, name, &str, errp)) {
return;
}
s = qemu_find_tpm_be(str);
if (s == NULL) {
error_setg(errp, "Property '%s.%s' can't find value '%s'",
object_get_typename(obj), name, str);
} else if (tpm_backend_init(s, TPM_IF(obj), errp) == 0) {
*be = s; /* weak reference, avoid cyclic ref */
}
g_free(str);
}
static void release_tpm(Object *obj, const char *name, void *opaque)
{
const Property *prop = opaque;
TPMBackend **be = object_field_prop_ptr(obj, prop);
if (*be) {
tpm_backend_reset(*be);
*be = NULL;
}
}
const PropertyInfo qdev_prop_tpm = {
.type = "str",
.description = "ID of a tpm to use as a backend",
.get = get_tpm,
.set = set_tpm,
.release = release_tpm,
};
/*
* Write an error message in the given output buffer.
*/
void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
{
if (out_len >= sizeof(struct tpm_resp_hdr)) {
tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
tpm_cmd_set_error(out, TPM_FAIL);
}
}
bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len)
{
if (in_len >= sizeof(struct tpm_req_hdr)) {
return tpm_cmd_get_ordinal(in) == TPM_ORD_ContinueSelfTest;
}
return false;
}
/*
* Send request to a TPM device. We expect a response within one second.
*/
static int tpm_util_request(int fd,
const void *request,
size_t requestlen,
void *response,
size_t responselen)
{
GPollFD fds[1] = { {.fd = fd, .events = G_IO_IN } };
int n;
n = write(fd, request, requestlen);
if (n < 0) {
return -errno;
}
if (n != requestlen) {
return -EFAULT;
}
/* wait for a second */
n = RETRY_ON_EINTR(g_poll(fds, 1, 1000));
if (n != 1) {
return -errno;
}
n = read(fd, response, responselen);
if (n < sizeof(struct tpm_resp_hdr)) {
return -EFAULT;
}
/* check the header */
if (tpm_cmd_get_size(response) != n) {
return -EMSGSIZE;
}
return 0;
}
/*
* A basic test of a TPM device. We expect a well formatted response header
* (error response is fine).
*/
static int tpm_util_test(int fd,
const void *request,
size_t requestlen,
uint16_t *return_tag)
{
char buf[1024];
ssize_t ret;
ret = tpm_util_request(fd, request, requestlen,
buf, sizeof(buf));
if (ret < 0) {
return ret;
}
*return_tag = tpm_cmd_get_tag(buf);
return 0;
}
/*
* Probe for the TPM device in the back
* Returns 0 on success with the version of the probed TPM set, 1 on failure.
*/
int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
{
/*
* Sending a TPM1.2 command to a TPM2 should return a TPM1.2
* header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
*
* Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
* header.
* Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
* in the header and an error code.
*/
const struct tpm_req_hdr test_req = {
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
.len = cpu_to_be32(sizeof(test_req)),
.ordinal = cpu_to_be32(TPM_ORD_GetTicks),
};
const struct tpm_req_hdr test_req_tpm2 = {
.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
.len = cpu_to_be32(sizeof(test_req_tpm2)),
.ordinal = cpu_to_be32(TPM2_CC_ReadClock),
};
uint16_t return_tag;
int ret;
/* Send TPM 2 command */
ret = tpm_util_test(tpm_fd, &test_req_tpm2,
sizeof(test_req_tpm2), &return_tag);
/* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
*tpm_version = TPM_VERSION_2_0;
return 0;
}
/* Send TPM 1.2 command */
ret = tpm_util_test(tpm_fd, &test_req,
sizeof(test_req), &return_tag);
if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
*tpm_version = TPM_VERSION_1_2;
/* this is a TPM 1.2 */
return 0;
}
*tpm_version = TPM_VERSION_UNSPEC;
return 1;
}
int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
size_t *buffersize)
{
int ret;
switch (tpm_version) {
case TPM_VERSION_1_2: {
const struct tpm_req_get_buffer_size {
struct tpm_req_hdr hdr;
uint32_t capability;
uint32_t len;
uint32_t subcap;
} QEMU_PACKED tpm_get_buffer_size = {
.hdr = {
.tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
.len = cpu_to_be32(sizeof(tpm_get_buffer_size)),
.ordinal = cpu_to_be32(TPM_ORD_GetCapability),
},
.capability = cpu_to_be32(TPM_CAP_PROPERTY),
.len = cpu_to_be32(sizeof(uint32_t)),
.subcap = cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER),
};
struct tpm_resp_get_buffer_size {
struct tpm_resp_hdr hdr;
uint32_t len;
uint32_t buffersize;
} QEMU_PACKED tpm_resp;
ret = tpm_util_request(tpm_fd, &tpm_get_buffer_size,
sizeof(tpm_get_buffer_size),
&tpm_resp, sizeof(tpm_resp));
if (ret < 0) {
return ret;
}
if (be32_to_cpu(tpm_resp.hdr.len) != sizeof(tpm_resp) ||
be32_to_cpu(tpm_resp.len) != sizeof(uint32_t)) {
trace_tpm_util_get_buffer_size_hdr_len(
be32_to_cpu(tpm_resp.hdr.len),
sizeof(tpm_resp));
trace_tpm_util_get_buffer_size_len(be32_to_cpu(tpm_resp.len),
sizeof(uint32_t));
error_report("tpm_util: Got unexpected response to "
"TPM_GetCapability; errcode: 0x%x",
be32_to_cpu(tpm_resp.hdr.errcode));
return -EFAULT;
}
*buffersize = be32_to_cpu(tpm_resp.buffersize);
break;
}
case TPM_VERSION_2_0: {
const struct tpm2_req_get_buffer_size {
struct tpm_req_hdr hdr;
uint32_t capability;
uint32_t property;
uint32_t count;
} QEMU_PACKED tpm2_get_buffer_size = {
.hdr = {
.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
.len = cpu_to_be32(sizeof(tpm2_get_buffer_size)),
.ordinal = cpu_to_be32(TPM2_CC_GetCapability),
},
.capability = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES),
.property = cpu_to_be32(TPM2_PT_MAX_COMMAND_SIZE),
.count = cpu_to_be32(2), /* also get TPM2_PT_MAX_RESPONSE_SIZE */
};
struct tpm2_resp_get_buffer_size {
struct tpm_resp_hdr hdr;
uint8_t more;
uint32_t capability;
uint32_t count;
uint32_t property1;
uint32_t value1;
uint32_t property2;
uint32_t value2;
} QEMU_PACKED tpm2_resp;
ret = tpm_util_request(tpm_fd, &tpm2_get_buffer_size,
sizeof(tpm2_get_buffer_size),
&tpm2_resp, sizeof(tpm2_resp));
if (ret < 0) {
return ret;
}
if (be32_to_cpu(tpm2_resp.hdr.len) != sizeof(tpm2_resp) ||
be32_to_cpu(tpm2_resp.count) != 2) {
trace_tpm_util_get_buffer_size_hdr_len2(
be32_to_cpu(tpm2_resp.hdr.len),
sizeof(tpm2_resp));
trace_tpm_util_get_buffer_size_len2(
be32_to_cpu(tpm2_resp.count), 2);
error_report("tpm_util: Got unexpected response to "
"TPM2_GetCapability; errcode: 0x%x",
be32_to_cpu(tpm2_resp.hdr.errcode));
return -EFAULT;
}
*buffersize = MAX(be32_to_cpu(tpm2_resp.value1),
be32_to_cpu(tpm2_resp.value2));
break;
}
case TPM_VERSION_UNSPEC:
return -EFAULT;
}
trace_tpm_util_get_buffer_size(*buffersize);
return 0;
}
void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
{
g_free(tsb->buffer);
tsb->buffer = NULL;
tsb->size = 0;
}
void tpm_util_show_buffer(const unsigned char *buffer,
size_t buffer_size, const char *string)
{
g_autoptr(GString) str = NULL;
size_t len, i, l;
if (!trace_event_get_state_backends(TRACE_TPM_UTIL_SHOW_BUFFER_CONTENT)) {
return;
}
len = MIN(tpm_cmd_get_size(buffer), buffer_size);
trace_tpm_util_show_buffer_header(string, len);
for (i = 0; i < len; i += l) {
if (str) {
g_string_append_c(str, '\n');
}
l = MIN(len, 16);
str = qemu_hexdump_line(str, buffer, l, 1, 0);
}
g_string_ascii_up(str);
trace_tpm_util_show_buffer_content(str->str);
}
+36
View File
@@ -0,0 +1,36 @@
# See docs/devel/tracing.rst for syntax documentation.
# tpm_passthrough.c
tpm_passthrough_handle_request(void *cmd) "processing command %p"
tpm_passthrough_reset(void) "reset"
# tpm_util.c
tpm_util_get_buffer_size_hdr_len(uint32_t len, size_t expected) "tpm_resp->hdr.len = %u, expected = %zu"
tpm_util_get_buffer_size_len(uint32_t len, size_t expected) "tpm_resp->len = %u, expected = %zu"
tpm_util_get_buffer_size_hdr_len2(uint32_t len, size_t expected) "tpm2_resp->hdr.len = %u, expected = %zu"
tpm_util_get_buffer_size_len2(uint32_t len, size_t expected) "tpm2_resp->len = %u, expected = %zu"
tpm_util_get_buffer_size(size_t len) "buffersize of device: %zu"
tpm_util_show_buffer_header(const char *direction, size_t len) "direction: %s len: %zu"
tpm_util_show_buffer_content(const char *buf) "%s"
# tpm_emulator.c
tpm_emulator_set_locality(uint8_t locty) "setting locality to %d"
tpm_emulator_handle_request(void) "processing TPM command"
tpm_emulator_probe_caps(uint32_t caps) "capabilities: 0x%x"
tpm_emulator_set_buffer_size(uint32_t buffersize, uint32_t minsize, uint32_t maxsize) "buffer size: %u, min: %u, max: %u"
tpm_emulator_startup_tpm_resume(bool is_resume, size_t buffersize) "is_resume: %d, buffer size: %zu"
tpm_emulator_get_tpm_established_flag(uint8_t flag) "got established flag: %d"
tpm_emulator_cancel_cmd_not_supt(void) "Backend does not support CANCEL_TPM_CMD"
tpm_emulator_lock_storage_cmd_not_supt(void) "Backend does not support LOCK_STORAGE"
tpm_emulator_vm_state_change(int running, int state) "state change to running %d state %d"
tpm_emulator_handle_device_opts_tpm12(void) "TPM Version 1.2"
tpm_emulator_handle_device_opts_tpm2(void) "TPM Version 2"
tpm_emulator_handle_device_opts_unspec(void) "TPM Version Unspecified"
tpm_emulator_handle_device_opts_startup_error(void) "Startup error"
tpm_emulator_get_state_blob(uint8_t type, uint32_t size, uint32_t flags) "got state blob type %d, %u bytes, flags 0x%08x"
tpm_emulator_set_state_blob(uint8_t type, uint32_t size, uint32_t flags) "set state blob type %d, %u bytes, flags 0x%08x"
tpm_emulator_set_state_blobs(void) "setting state blobs"
tpm_emulator_set_state_blobs_error(const char *msg) "error while setting state blobs: %s"
tpm_emulator_set_state_blobs_done(void) "Done setting state blobs"
tpm_emulator_pre_save(void) ""
tpm_emulator_inst_init(void) ""
+1
View File
@@ -0,0 +1 @@
#include "trace/trace-backends_tpm.h"