xref: /openbmc/qemu/tests/qtest/tpm-util.c (revision 3878d0c7)
1 /*
2  * QTest TPM utilities
3  *
4  * Copyright (c) 2018 IBM Corporation
5  * Copyright (c) 2018 Red Hat, Inc.
6  *
7  * Authors:
8  *   Stefan Berger <stefanb@linux.vnet.ibm.com>
9  *   Marc-André Lureau <marcandre.lureau@redhat.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2 or later.
12  * See the COPYING file in the top-level directory.
13  */
14 
15 #include "qemu/osdep.h"
16 #include <glib/gstdio.h>
17 
18 #include "hw/acpi/tpm.h"
19 #include "libqtest.h"
20 #include "tpm-util.h"
21 #include "qapi/qmp/qdict.h"
22 
23 void tpm_util_crb_transfer(QTestState *s,
24                            const unsigned char *req, size_t req_size,
25                            unsigned char *rsp, size_t rsp_size)
26 {
27     uint64_t caddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_CMD_LADDR);
28     uint64_t raddr = qtest_readq(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_RSP_ADDR);
29 
30     qtest_writeb(s, TPM_CRB_ADDR_BASE + A_CRB_LOC_CTRL, 1);
31 
32     qtest_memwrite(s, caddr, req, req_size);
33 
34     uint32_t sts, start = 1;
35     uint64_t end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
36     qtest_writel(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START, start);
37     while (true) {
38         start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
39         if ((start & 1) == 0) {
40             break;
41         }
42         if (g_get_monotonic_time() >= end_time) {
43             break;
44         }
45     };
46     start = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_START);
47     g_assert_cmpint(start & 1, ==, 0);
48     sts = qtest_readl(s, TPM_CRB_ADDR_BASE + A_CRB_CTRL_STS);
49     g_assert_cmpint(sts & 1, ==, 0);
50 
51     qtest_memread(s, raddr, rsp, rsp_size);
52 }
53 
54 void tpm_util_tis_transfer(QTestState *s,
55                            const unsigned char *req, size_t req_size,
56                            unsigned char *rsp, size_t rsp_size)
57 {
58     uint32_t sts;
59     uint16_t bcount;
60     size_t i;
61 
62     /* request use of locality 0 */
63     qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS), TPM_TIS_ACCESS_REQUEST_USE);
64     qtest_writel(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_COMMAND_READY);
65 
66     sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
67     bcount = (sts >> 8) & 0xffff;
68     g_assert_cmpint(bcount, >=, req_size);
69 
70     /* transmit command */
71     for (i = 0; i < req_size; i++) {
72         qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO), req[i]);
73     }
74 
75     /* start processing */
76     qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_STS), TPM_TIS_STS_TPM_GO);
77 
78     uint64_t end_time = g_get_monotonic_time() + 50 * G_TIME_SPAN_SECOND;
79     do {
80         sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
81         if ((sts & TPM_TIS_STS_DATA_AVAILABLE) != 0) {
82             break;
83         }
84     } while (g_get_monotonic_time() < end_time);
85 
86     sts = qtest_readl(s, TIS_REG(0, TPM_TIS_REG_STS));
87     bcount = (sts >> 8) & 0xffff;
88 
89     memset(rsp, 0, rsp_size);
90     for (i = 0; i < bcount; i++) {
91         rsp[i] = qtest_readb(s, TIS_REG(0, TPM_TIS_REG_DATA_FIFO));
92     }
93 
94     /* relinquish use of locality 0 */
95     qtest_writeb(s, TIS_REG(0, TPM_TIS_REG_ACCESS),
96                  TPM_TIS_ACCESS_ACTIVE_LOCALITY);
97 }
98 
99 void tpm_util_startup(QTestState *s, tx_func *tx)
100 {
101     unsigned char buffer[1024];
102     static const unsigned char tpm_startup[] =
103         "\x80\x01\x00\x00\x00\x0c\x00\x00\x01\x44\x00\x00";
104     static const unsigned char tpm_startup_resp[] =
105         "\x80\x01\x00\x00\x00\x0a\x00\x00\x00\x00";
106 
107     tx(s, tpm_startup, sizeof(tpm_startup), buffer, sizeof(buffer));
108 
109     g_assert_cmpmem(buffer, sizeof(tpm_startup_resp),
110                     tpm_startup_resp, sizeof(tpm_startup_resp));
111 }
112 
113 void tpm_util_pcrextend(QTestState *s, tx_func *tx)
114 {
115     unsigned char buffer[1024];
116     static const unsigned char tpm_pcrextend[] =
117         "\x80\x02\x00\x00\x00\x41\x00\x00\x01\x82\x00\x00\x00\x0a\x00\x00"
118         "\x00\x09\x40\x00\x00\x09\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00"
119         "\x0b\x74\x65\x73\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
120         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
121         "\x00";
122 
123     static const unsigned char tpm_pcrextend_resp[] =
124         "\x80\x02\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
125         "\x01\x00\x00";
126 
127     tx(s, tpm_pcrextend, sizeof(tpm_pcrextend), buffer, sizeof(buffer));
128 
129     g_assert_cmpmem(buffer, sizeof(tpm_pcrextend_resp),
130                     tpm_pcrextend_resp, sizeof(tpm_pcrextend_resp));
131 }
132 
133 void tpm_util_pcrread(QTestState *s, tx_func *tx,
134                       const unsigned char *exp_resp, size_t exp_resp_size)
135 {
136     unsigned char buffer[1024];
137     static const unsigned char tpm_pcrread[] =
138         "\x80\x01\x00\x00\x00\x14\x00\x00\x01\x7e\x00\x00\x00\x01\x00\x0b"
139         "\x03\x00\x04\x00";
140 
141     tx(s, tpm_pcrread, sizeof(tpm_pcrread), buffer, sizeof(buffer));
142 
143     /* skip pcrUpdateCounter (14th byte) in comparison */
144     g_assert(exp_resp_size >= 15);
145     g_assert_cmpmem(buffer, 13, exp_resp, 13);
146     g_assert_cmpmem(&buffer[14], exp_resp_size - 14,
147                     &exp_resp[14], exp_resp_size - 14);
148 }
149 
150 bool tpm_util_swtpm_has_tpm2(void)
151 {
152     bool has_tpm2 = false;
153     char *out = NULL;
154     static const char *argv[] = {
155         "swtpm", "socket", "--help", NULL
156     };
157 
158     if (!g_spawn_sync(NULL /* working_dir */,
159                       (char **)argv,
160                       NULL /* envp */,
161                       G_SPAWN_SEARCH_PATH,
162                       NULL /* child_setup */,
163                       NULL /* user_data */,
164                       &out,
165                       NULL /* err */,
166                       NULL /* exit_status */,
167                       NULL)) {
168         return false;
169     }
170 
171     if (strstr(out, "--tpm2")) {
172         has_tpm2 = true;
173     }
174 
175     g_free(out);
176     return has_tpm2;
177 }
178 
179 gboolean tpm_util_swtpm_start(const char *path, GPid *pid,
180                               SocketAddress **addr, GError **error)
181 {
182     char *swtpm_argv_tpmstate = g_strdup_printf("dir=%s", path);
183     char *swtpm_argv_ctrl = g_strdup_printf("type=unixio,path=%s/sock",
184                                             path);
185     gchar *swtpm_argv[] = {
186         g_strdup("swtpm"), g_strdup("socket"),
187         g_strdup("--tpmstate"), swtpm_argv_tpmstate,
188         g_strdup("--ctrl"), swtpm_argv_ctrl,
189         g_strdup("--tpm2"),
190         NULL
191     };
192     gboolean succ;
193     unsigned i;
194 
195     *addr = g_new0(SocketAddress, 1);
196     (*addr)->type = SOCKET_ADDRESS_TYPE_UNIX;
197     (*addr)->u.q_unix.path = g_build_filename(path, "sock", NULL);
198 
199     succ = g_spawn_async(NULL, swtpm_argv, NULL, G_SPAWN_SEARCH_PATH,
200                          NULL, NULL, pid, error);
201 
202     for (i = 0; swtpm_argv[i]; i++) {
203         g_free(swtpm_argv[i]);
204     }
205 
206     return succ;
207 }
208 
209 void tpm_util_swtpm_kill(GPid pid)
210 {
211     int n;
212 
213     if (!pid) {
214         return;
215     }
216 
217     g_spawn_close_pid(pid);
218 
219     n = kill(pid, 0);
220     if (n < 0) {
221         return;
222     }
223 
224     kill(pid, SIGKILL);
225 }
226 
227 void tpm_util_migrate(QTestState *who, const char *uri)
228 {
229     QDict *rsp;
230 
231     rsp = qtest_qmp(who,
232                     "{ 'execute': 'migrate', 'arguments': { 'uri': %s } }",
233                     uri);
234     g_assert(qdict_haskey(rsp, "return"));
235     qobject_unref(rsp);
236 }
237 
238 void tpm_util_wait_for_migration_complete(QTestState *who)
239 {
240     while (true) {
241         QDict *rsp;
242         QDict *rsp_return;
243         bool completed;
244         const char *status;
245 
246         rsp = qtest_qmp(who, "{ 'execute': 'query-migrate' }");
247         g_assert(qdict_haskey(rsp, "return"));
248         rsp_return = qdict_get_qdict(rsp, "return");
249 
250         g_assert(!qdict_haskey(rsp_return, "error"));
251         status = qdict_get_str(rsp_return, "status");
252         completed = strcmp(status, "completed") == 0;
253         g_assert_cmpstr(status, !=,  "failed");
254         qobject_unref(rsp);
255         if (completed) {
256             return;
257         }
258         usleep(1000);
259     }
260 }
261 
262 void tpm_util_migration_start_qemu(QTestState **src_qemu,
263                                    QTestState **dst_qemu,
264                                    SocketAddress *src_tpm_addr,
265                                    SocketAddress *dst_tpm_addr,
266                                    const char *miguri,
267                                    const char *ifmodel,
268                                    const char *machine_options)
269 {
270     char *src_qemu_args, *dst_qemu_args;
271 
272     src_qemu_args = g_strdup_printf(
273         "%s "
274         "-chardev socket,id=chr,path=%s "
275         "-tpmdev emulator,id=dev,chardev=chr "
276         "-device %s,tpmdev=dev ",
277         machine_options ? : "", src_tpm_addr->u.q_unix.path, ifmodel);
278 
279     *src_qemu = qtest_init(src_qemu_args);
280 
281     dst_qemu_args = g_strdup_printf(
282         "%s "
283         "-chardev socket,id=chr,path=%s "
284         "-tpmdev emulator,id=dev,chardev=chr "
285         "-device %s,tpmdev=dev "
286         "-incoming %s",
287         machine_options ? : "",
288         dst_tpm_addr->u.q_unix.path,
289         ifmodel, miguri);
290 
291     *dst_qemu = qtest_init(dst_qemu_args);
292 
293     g_free(src_qemu_args);
294     g_free(dst_qemu_args);
295 }
296 
297 /* Remove directory with remainders of swtpm */
298 void tpm_util_rmdir(const char *path)
299 {
300     char *filename;
301     int ret;
302 
303     filename = g_strdup_printf("%s/tpm2-00.permall", path);
304     g_unlink(filename);
305     g_free(filename);
306 
307     filename = g_strdup_printf("%s/.lock", path);
308     g_unlink(filename);
309     g_free(filename);
310 
311     ret = g_rmdir(path);
312     g_assert(!ret);
313 }
314