| 1 | /* |
| 2 | * Minimal TPM emulator for TPM test cases |
| 3 | * |
| 4 | * Copyright (c) 2018 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Marc-André Lureau <marcandre.lureau@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include <glib/gstdio.h> |
| 15 | |
| 16 | #include "backends/tpm/tpm_ioctl.h" |
| 17 | #include "io/channel-socket.h" |
| 18 | #include "qapi/error.h" |
| 19 | #include "qemu/bswap.h" |
| 20 | #include "qobject/qlist.h" |
| 21 | #include "qobject/qstring.h" |
| 22 | #include "tpm-emu.h" |
| 23 | |
| 24 | void tpm_emu_test_wait_cond(TPMTestState *s) |
| 25 | { |
| 26 | gint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; |
| 27 | |
| 28 | g_mutex_lock(&s->data_mutex); |
| 29 | |
| 30 | if (!s->data_cond_signal && |
| 31 | !g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { |
| 32 | g_assert_not_reached(); |
| 33 | } |
| 34 | |
| 35 | s->data_cond_signal = false; |
| 36 | |
| 37 | g_mutex_unlock(&s->data_mutex); |
| 38 | } |
| 39 | |
| 40 | static void tpm_emu_close_ioc(void *ioc) |
| 41 | { |
| 42 | qio_channel_close(ioc, NULL); |
| 43 | } |
| 44 | |
| 45 | static void *tpm_emu_tpm_thread(void *data) |
| 46 | { |
| 47 | TPMTestState *s = data; |
| 48 | QIOChannel *ioc = s->tpm_ioc; |
| 49 | |
| 50 | qtest_add_abrt_handler(tpm_emu_close_ioc, ioc); |
| 51 | |
| 52 | s->tpm_msg = g_new(struct tpm_hdr, 1); |
| 53 | while (true) { |
| 54 | int minhlen = sizeof(s->tpm_msg->tag) + sizeof(s->tpm_msg->len); |
| 55 | |
| 56 | if (!qio_channel_read(ioc, (char *)s->tpm_msg, minhlen, &error_abort)) { |
| 57 | break; |
| 58 | } |
| 59 | s->tpm_msg->tag = be16_to_cpu(s->tpm_msg->tag); |
| 60 | s->tpm_msg->len = be32_to_cpu(s->tpm_msg->len); |
| 61 | g_assert_cmpint(s->tpm_msg->len, >=, minhlen); |
| 62 | |
| 63 | s->tpm_msg = g_realloc(s->tpm_msg, s->tpm_msg->len); |
| 64 | qio_channel_read(ioc, (char *)&s->tpm_msg->code, |
| 65 | s->tpm_msg->len - minhlen, &error_abort); |
| 66 | s->tpm_msg->code = be32_to_cpu(s->tpm_msg->code); |
| 67 | |
| 68 | /* reply error */ |
| 69 | switch (s->tpm_version) { |
| 70 | case TPM_VERSION_2_0: |
| 71 | s->tpm_msg->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); |
| 72 | s->tpm_msg->len = cpu_to_be32(sizeof(struct tpm_hdr)); |
| 73 | s->tpm_msg->code = cpu_to_be32(TPM_RC_FAILURE); |
| 74 | break; |
| 75 | case TPM_VERSION_1_2: |
| 76 | s->tpm_msg->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND); |
| 77 | s->tpm_msg->len = cpu_to_be32(sizeof(struct tpm_hdr)); |
| 78 | s->tpm_msg->code = cpu_to_be32(TPM_FAIL); |
| 79 | break; |
| 80 | default: |
| 81 | g_debug("unsupported TPM version %u", s->tpm_version); |
| 82 | g_assert_not_reached(); |
| 83 | } |
| 84 | qio_channel_write(ioc, (char *)s->tpm_msg, be32_to_cpu(s->tpm_msg->len), |
| 85 | &error_abort); |
| 86 | } |
| 87 | |
| 88 | qtest_remove_abrt_handler(ioc); |
| 89 | g_free(s->tpm_msg); |
| 90 | s->tpm_msg = NULL; |
| 91 | object_unref(OBJECT(s->tpm_ioc)); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | void *tpm_emu_ctrl_thread(void *data) |
| 96 | { |
| 97 | TPMTestState *s = data; |
| 98 | QIOChannelSocket *lioc = qio_channel_socket_new(); |
| 99 | QIOChannel *ioc; |
| 100 | |
| 101 | qio_channel_socket_listen_sync(lioc, s->addr, 1, &error_abort); |
| 102 | |
| 103 | g_mutex_lock(&s->data_mutex); |
| 104 | s->data_cond_signal = true; |
| 105 | g_mutex_unlock(&s->data_mutex); |
| 106 | g_cond_signal(&s->data_cond); |
| 107 | |
| 108 | qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN); |
| 109 | ioc = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort)); |
| 110 | g_assert(ioc); |
| 111 | qtest_add_abrt_handler(tpm_emu_close_ioc, ioc); |
| 112 | |
| 113 | { |
| 114 | uint32_t cmd = 0; |
| 115 | struct iovec iov = { .iov_base = &cmd, .iov_len = sizeof(cmd) }; |
| 116 | int *pfd = NULL; |
| 117 | size_t nfd = 0; |
| 118 | |
| 119 | qio_channel_readv_full(ioc, &iov, 1, &pfd, &nfd, 0, &error_abort); |
| 120 | cmd = be32_to_cpu(cmd); |
| 121 | g_assert_cmpint(cmd, ==, CMD_SET_DATAFD); |
| 122 | g_assert_cmpint(nfd, ==, 1); |
| 123 | s->tpm_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(*pfd, &error_abort)); |
| 124 | g_free(pfd); |
| 125 | |
| 126 | cmd = 0; |
| 127 | qio_channel_write(ioc, (char *)&cmd, sizeof(cmd), &error_abort); |
| 128 | |
| 129 | s->emu_tpm_thread = g_thread_new(NULL, tpm_emu_tpm_thread, s); |
| 130 | } |
| 131 | |
| 132 | while (true) { |
| 133 | uint32_t cmd; |
| 134 | ssize_t ret; |
| 135 | |
| 136 | ret = qio_channel_read(ioc, (char *)&cmd, sizeof(cmd), NULL); |
| 137 | if (ret <= 0) { |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | cmd = be32_to_cpu(cmd); |
| 142 | switch (cmd) { |
| 143 | case CMD_GET_CAPABILITY: { |
| 144 | ptm_cap cap = cpu_to_be64(0x3fff); |
| 145 | qio_channel_write(ioc, (char *)&cap, sizeof(cap), &error_abort); |
| 146 | break; |
| 147 | } |
| 148 | case CMD_INIT: { |
| 149 | ptm_init init; |
| 150 | qio_channel_read(ioc, (char *)&init.u.req, sizeof(init.u.req), |
| 151 | &error_abort); |
| 152 | init.u.resp.tpm_result = 0; |
| 153 | qio_channel_write(ioc, (char *)&init.u.resp, sizeof(init.u.resp), |
| 154 | &error_abort); |
| 155 | break; |
| 156 | } |
| 157 | case CMD_SHUTDOWN: { |
| 158 | ptm_res res = 0; |
| 159 | qio_channel_write(ioc, (char *)&res, sizeof(res), &error_abort); |
| 160 | /* the tpm data thread is expected to finish now */ |
| 161 | g_thread_join(s->emu_tpm_thread); |
| 162 | break; |
| 163 | } |
| 164 | case CMD_STOP: { |
| 165 | ptm_res res = 0; |
| 166 | qio_channel_write(ioc, (char *)&res, sizeof(res), &error_abort); |
| 167 | break; |
| 168 | } |
| 169 | case CMD_SET_BUFFERSIZE: { |
| 170 | ptm_setbuffersize sbs; |
| 171 | qio_channel_read(ioc, (char *)&sbs.u.req, sizeof(sbs.u.req), |
| 172 | &error_abort); |
| 173 | sbs.u.resp.buffersize = sbs.u.req.buffersize ?: cpu_to_be32(4096); |
| 174 | sbs.u.resp.tpm_result = 0; |
| 175 | sbs.u.resp.minsize = cpu_to_be32(128); |
| 176 | sbs.u.resp.maxsize = cpu_to_be32(4096); |
| 177 | qio_channel_write(ioc, (char *)&sbs.u.resp, sizeof(sbs.u.resp), |
| 178 | &error_abort); |
| 179 | break; |
| 180 | } |
| 181 | case CMD_SET_LOCALITY: { |
| 182 | ptm_loc loc; |
| 183 | /* Note: this time it's not u.req / u.resp... */ |
| 184 | qio_channel_read(ioc, (char *)&loc, sizeof(loc), &error_abort); |
| 185 | g_assert_cmpint(loc.u.req.loc, ==, 0); |
| 186 | loc.u.resp.tpm_result = 0; |
| 187 | qio_channel_write(ioc, (char *)&loc, sizeof(loc), &error_abort); |
| 188 | break; |
| 189 | } |
| 190 | case CMD_GET_TPMESTABLISHED: { |
| 191 | ptm_est est = { |
| 192 | .u.resp.bit = 0, |
| 193 | }; |
| 194 | qio_channel_write(ioc, (char *)&est, sizeof(est), &error_abort); |
| 195 | break; |
| 196 | } |
| 197 | default: |
| 198 | g_debug("unimplemented %u", cmd); |
| 199 | g_assert_not_reached(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | qtest_remove_abrt_handler(ioc); |
| 204 | object_unref(OBJECT(ioc)); |
| 205 | object_unref(OBJECT(lioc)); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | bool tpm_model_is_available(const char *args, const char *tpm_if) |
| 210 | { |
| 211 | QTestState *qts; |
| 212 | QDict *rsp_tpm; |
| 213 | bool ret = false; |
| 214 | |
| 215 | qts = qtest_init(args); |
| 216 | if (!qts) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | rsp_tpm = qtest_qmp(qts, "{ 'execute': 'query-tpm'}"); |
| 221 | if (!qdict_haskey(rsp_tpm, "error")) { |
| 222 | QDict *rsp_models = qtest_qmp(qts, |
| 223 | "{ 'execute': 'query-tpm-models'}"); |
| 224 | if (qdict_haskey(rsp_models, "return")) { |
| 225 | QList *models = qdict_get_qlist(rsp_models, "return"); |
| 226 | QListEntry *e; |
| 227 | |
| 228 | QLIST_FOREACH_ENTRY(models, e) { |
| 229 | QString *s = qobject_to(QString, qlist_entry_obj(e)); |
| 230 | const char *ename = qstring_get_str(s); |
| 231 | if (!strcmp(ename, tpm_if)) { |
| 232 | ret = true; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | qobject_unref(rsp_models); |
| 238 | } |
| 239 | qobject_unref(rsp_tpm); |
| 240 | qtest_quit(qts); |
| 241 | |
| 242 | return ret; |
| 243 | } |