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 "tpm-emu.h" 20 21 void tpm_emu_test_wait_cond(TPMTestState *s) 22 { 23 gint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 24 25 g_mutex_lock(&s->data_mutex); 26 27 if (!s->data_cond_signal && 28 !g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { 29 g_assert_not_reached(); 30 } 31 32 s->data_cond_signal = false; 33 34 g_mutex_unlock(&s->data_mutex); 35 } 36 37 static void *tpm_emu_tpm_thread(void *data) 38 { 39 TPMTestState *s = data; 40 QIOChannel *ioc = s->tpm_ioc; 41 42 s->tpm_msg = g_new(struct tpm_hdr, 1); 43 while (true) { 44 int minhlen = sizeof(s->tpm_msg->tag) + sizeof(s->tpm_msg->len); 45 46 if (!qio_channel_read(ioc, (char *)s->tpm_msg, minhlen, &error_abort)) { 47 break; 48 } 49 s->tpm_msg->tag = be16_to_cpu(s->tpm_msg->tag); 50 s->tpm_msg->len = be32_to_cpu(s->tpm_msg->len); 51 g_assert_cmpint(s->tpm_msg->len, >=, minhlen); 52 53 s->tpm_msg = g_realloc(s->tpm_msg, s->tpm_msg->len); 54 qio_channel_read(ioc, (char *)&s->tpm_msg->code, 55 s->tpm_msg->len - minhlen, &error_abort); 56 s->tpm_msg->code = be32_to_cpu(s->tpm_msg->code); 57 58 /* reply error */ 59 switch (s->tpm_version) { 60 case TPM_VERSION_2_0: 61 s->tpm_msg->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); 62 s->tpm_msg->len = cpu_to_be32(sizeof(struct tpm_hdr)); 63 s->tpm_msg->code = cpu_to_be32(TPM_RC_FAILURE); 64 break; 65 case TPM_VERSION_1_2: 66 s->tpm_msg->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND); 67 s->tpm_msg->len = cpu_to_be32(sizeof(struct tpm_hdr)); 68 s->tpm_msg->code = cpu_to_be32(TPM_FAIL); 69 break; 70 default: 71 g_debug("unsupport TPM version %u", s->tpm_version); 72 g_assert_not_reached(); 73 } 74 qio_channel_write(ioc, (char *)s->tpm_msg, be32_to_cpu(s->tpm_msg->len), 75 &error_abort); 76 } 77 78 g_free(s->tpm_msg); 79 s->tpm_msg = NULL; 80 object_unref(OBJECT(s->tpm_ioc)); 81 return NULL; 82 } 83 84 void *tpm_emu_ctrl_thread(void *data) 85 { 86 TPMTestState *s = data; 87 QIOChannelSocket *lioc = qio_channel_socket_new(); 88 QIOChannel *ioc; 89 90 qio_channel_socket_listen_sync(lioc, s->addr, 1, &error_abort); 91 92 g_mutex_lock(&s->data_mutex); 93 s->data_cond_signal = true; 94 g_mutex_unlock(&s->data_mutex); 95 g_cond_signal(&s->data_cond); 96 97 qio_channel_wait(QIO_CHANNEL(lioc), G_IO_IN); 98 ioc = QIO_CHANNEL(qio_channel_socket_accept(lioc, &error_abort)); 99 g_assert(ioc); 100 101 { 102 uint32_t cmd = 0; 103 struct iovec iov = { .iov_base = &cmd, .iov_len = sizeof(cmd) }; 104 int *pfd = NULL; 105 size_t nfd = 0; 106 107 qio_channel_readv_full(ioc, &iov, 1, &pfd, &nfd, &error_abort); 108 cmd = be32_to_cpu(cmd); 109 g_assert_cmpint(cmd, ==, CMD_SET_DATAFD); 110 g_assert_cmpint(nfd, ==, 1); 111 s->tpm_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(*pfd, &error_abort)); 112 g_free(pfd); 113 114 cmd = 0; 115 qio_channel_write(ioc, (char *)&cmd, sizeof(cmd), &error_abort); 116 117 s->emu_tpm_thread = g_thread_new(NULL, tpm_emu_tpm_thread, s); 118 } 119 120 while (true) { 121 uint32_t cmd; 122 ssize_t ret; 123 124 ret = qio_channel_read(ioc, (char *)&cmd, sizeof(cmd), NULL); 125 if (ret <= 0) { 126 break; 127 } 128 129 cmd = be32_to_cpu(cmd); 130 switch (cmd) { 131 case CMD_GET_CAPABILITY: { 132 ptm_cap cap = cpu_to_be64(0x3fff); 133 qio_channel_write(ioc, (char *)&cap, sizeof(cap), &error_abort); 134 break; 135 } 136 case CMD_INIT: { 137 ptm_init init; 138 qio_channel_read(ioc, (char *)&init.u.req, sizeof(init.u.req), 139 &error_abort); 140 init.u.resp.tpm_result = 0; 141 qio_channel_write(ioc, (char *)&init.u.resp, sizeof(init.u.resp), 142 &error_abort); 143 break; 144 } 145 case CMD_SHUTDOWN: { 146 ptm_res res = 0; 147 qio_channel_write(ioc, (char *)&res, sizeof(res), &error_abort); 148 /* the tpm data thread is expected to finish now */ 149 g_thread_join(s->emu_tpm_thread); 150 break; 151 } 152 case CMD_STOP: { 153 ptm_res res = 0; 154 qio_channel_write(ioc, (char *)&res, sizeof(res), &error_abort); 155 break; 156 } 157 case CMD_SET_BUFFERSIZE: { 158 ptm_setbuffersize sbs; 159 qio_channel_read(ioc, (char *)&sbs.u.req, sizeof(sbs.u.req), 160 &error_abort); 161 sbs.u.resp.buffersize = sbs.u.req.buffersize ?: cpu_to_be32(4096); 162 sbs.u.resp.tpm_result = 0; 163 sbs.u.resp.minsize = cpu_to_be32(128); 164 sbs.u.resp.maxsize = cpu_to_be32(4096); 165 qio_channel_write(ioc, (char *)&sbs.u.resp, sizeof(sbs.u.resp), 166 &error_abort); 167 break; 168 } 169 case CMD_SET_LOCALITY: { 170 ptm_loc loc; 171 /* Note: this time it's not u.req / u.resp... */ 172 qio_channel_read(ioc, (char *)&loc, sizeof(loc), &error_abort); 173 g_assert_cmpint(loc.u.req.loc, ==, 0); 174 loc.u.resp.tpm_result = 0; 175 qio_channel_write(ioc, (char *)&loc, sizeof(loc), &error_abort); 176 break; 177 } 178 case CMD_GET_TPMESTABLISHED: { 179 ptm_est est = { 180 .u.resp.bit = 0, 181 }; 182 qio_channel_write(ioc, (char *)&est, sizeof(est), &error_abort); 183 break; 184 } 185 default: 186 g_debug("unimplemented %u", cmd); 187 g_assert_not_reached(); 188 } 189 } 190 191 object_unref(OBJECT(ioc)); 192 object_unref(OBJECT(lioc)); 193 return NULL; 194 } 195