]> git.proxmox.com Git - mirror_qemu.git/blob - hw/tpm/tpm_util.c
tpm: fix alignment issues
[mirror_qemu.git] / hw / tpm / tpm_util.c
1 /*
2 * TPM utility functions
3 *
4 * Copyright (c) 2010 - 2015 IBM Corporation
5 * Authors:
6 * Stefan Berger <stefanb@us.ibm.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>
20 */
21
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qapi/visitor.h"
26 #include "tpm_util.h"
27 #include "tpm_int.h"
28 #include "exec/memory.h"
29 #include "sysemu/tpm_backend.h"
30 #include "hw/qdev.h"
31
32 #define DEBUG_TPM 0
33
34 #define DPRINTF(fmt, ...) do { \
35 if (DEBUG_TPM) { \
36 fprintf(stderr, "tpm-util:"fmt"\n", ## __VA_ARGS__); \
37 } \
38 } while (0)
39
40 /* tpm backend property */
41
42 static void get_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
43 Error **errp)
44 {
45 DeviceState *dev = DEVICE(obj);
46 TPMBackend **be = qdev_get_prop_ptr(dev, opaque);
47 char *p;
48
49 p = g_strdup(*be ? (*be)->id : "");
50 visit_type_str(v, name, &p, errp);
51 g_free(p);
52 }
53
54 static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
55 Error **errp)
56 {
57 DeviceState *dev = DEVICE(obj);
58 Error *local_err = NULL;
59 Property *prop = opaque;
60 TPMBackend *s, **be = qdev_get_prop_ptr(dev, prop);
61 char *str;
62
63 if (dev->realized) {
64 qdev_prop_set_after_realize(dev, name, errp);
65 return;
66 }
67
68 visit_type_str(v, name, &str, &local_err);
69 if (local_err) {
70 error_propagate(errp, local_err);
71 return;
72 }
73
74 s = qemu_find_tpm_be(str);
75 if (s == NULL) {
76 error_setg(errp, "Property '%s.%s' can't find value '%s'",
77 object_get_typename(obj), prop->name, str);
78 } else if (tpm_backend_init(s, TPM_IF(obj), errp) == 0) {
79 *be = s; /* weak reference, avoid cyclic ref */
80 }
81 g_free(str);
82 }
83
84 static void release_tpm(Object *obj, const char *name, void *opaque)
85 {
86 DeviceState *dev = DEVICE(obj);
87 Property *prop = opaque;
88 TPMBackend **be = qdev_get_prop_ptr(dev, prop);
89
90 if (*be) {
91 tpm_backend_reset(*be);
92 }
93 }
94
95 const PropertyInfo qdev_prop_tpm = {
96 .name = "str",
97 .description = "ID of a tpm to use as a backend",
98 .get = get_tpm,
99 .set = set_tpm,
100 .release = release_tpm,
101 };
102
103 /*
104 * Write an error message in the given output buffer.
105 */
106 void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
107 {
108 if (out_len >= sizeof(struct tpm_resp_hdr)) {
109 stw_be_p(out, TPM_TAG_RSP_COMMAND);
110 stl_be_p(out + 2, sizeof(struct tpm_resp_hdr));
111 stl_be_p(out + 6, TPM_FAIL);
112 }
113 }
114
115 bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len)
116 {
117 if (in_len >= sizeof(struct tpm_req_hdr)) {
118 return tpm_cmd_get_ordinal(in) == TPM_ORD_ContinueSelfTest;
119 }
120
121 return false;
122 }
123
124 /*
125 * Send request to a TPM device. We expect a response within one second.
126 */
127 static int tpm_util_request(int fd,
128 const void *request,
129 size_t requestlen,
130 void *response,
131 size_t responselen)
132 {
133 fd_set readfds;
134 int n;
135 struct timeval tv = {
136 .tv_sec = 1,
137 .tv_usec = 0,
138 };
139
140 n = write(fd, request, requestlen);
141 if (n < 0) {
142 return -errno;
143 }
144 if (n != requestlen) {
145 return -EFAULT;
146 }
147
148 FD_ZERO(&readfds);
149 FD_SET(fd, &readfds);
150
151 /* wait for a second */
152 n = select(fd + 1, &readfds, NULL, NULL, &tv);
153 if (n != 1) {
154 return -errno;
155 }
156
157 n = read(fd, response, responselen);
158 if (n < sizeof(struct tpm_resp_hdr)) {
159 return -EFAULT;
160 }
161
162 /* check the header */
163 if (tpm_cmd_get_size(response) != n) {
164 return -EMSGSIZE;
165 }
166
167 return 0;
168 }
169
170 /*
171 * A basic test of a TPM device. We expect a well formatted response header
172 * (error response is fine).
173 */
174 static int tpm_util_test(int fd,
175 const void *request,
176 size_t requestlen,
177 uint16_t *return_tag)
178 {
179 char buf[1024];
180 ssize_t ret;
181
182 ret = tpm_util_request(fd, request, requestlen,
183 buf, sizeof(buf));
184 if (ret < 0) {
185 return ret;
186 }
187
188 *return_tag = tpm_cmd_get_tag(buf);
189
190 return 0;
191 }
192
193 /*
194 * Probe for the TPM device in the back
195 * Returns 0 on success with the version of the probed TPM set, 1 on failure.
196 */
197 int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
198 {
199 /*
200 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
201 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
202 *
203 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
204 * header.
205 * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
206 * in the header and an error code.
207 */
208 const struct tpm_req_hdr test_req = {
209 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
210 .len = cpu_to_be32(sizeof(test_req)),
211 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
212 };
213
214 const struct tpm_req_hdr test_req_tpm2 = {
215 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
216 .len = cpu_to_be32(sizeof(test_req_tpm2)),
217 .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
218 };
219 uint16_t return_tag;
220 int ret;
221
222 /* Send TPM 2 command */
223 ret = tpm_util_test(tpm_fd, &test_req_tpm2,
224 sizeof(test_req_tpm2), &return_tag);
225 /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
226 if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
227 *tpm_version = TPM_VERSION_2_0;
228 return 0;
229 }
230
231 /* Send TPM 1.2 command */
232 ret = tpm_util_test(tpm_fd, &test_req,
233 sizeof(test_req), &return_tag);
234 if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
235 *tpm_version = TPM_VERSION_1_2;
236 /* this is a TPM 1.2 */
237 return 0;
238 }
239
240 *tpm_version = TPM_VERSION_UNSPEC;
241
242 return 1;
243 }
244
245 int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
246 size_t *buffersize)
247 {
248 int ret;
249
250 switch (tpm_version) {
251 case TPM_VERSION_1_2: {
252 const struct tpm_req_get_buffer_size {
253 struct tpm_req_hdr hdr;
254 uint32_t capability;
255 uint32_t len;
256 uint32_t subcap;
257 } QEMU_PACKED tpm_get_buffer_size = {
258 .hdr = {
259 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
260 .len = cpu_to_be32(sizeof(tpm_get_buffer_size)),
261 .ordinal = cpu_to_be32(TPM_ORD_GetCapability),
262 },
263 .capability = cpu_to_be32(TPM_CAP_PROPERTY),
264 .len = cpu_to_be32(sizeof(uint32_t)),
265 .subcap = cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER),
266 };
267 struct tpm_resp_get_buffer_size {
268 struct tpm_resp_hdr hdr;
269 uint32_t len;
270 uint32_t buffersize;
271 } QEMU_PACKED tpm_resp;
272
273 ret = tpm_util_request(tpm_fd, &tpm_get_buffer_size,
274 sizeof(tpm_get_buffer_size),
275 &tpm_resp, sizeof(tpm_resp));
276 if (ret < 0) {
277 return ret;
278 }
279
280 if (be32_to_cpu(tpm_resp.hdr.len) != sizeof(tpm_resp) ||
281 be32_to_cpu(tpm_resp.len) != sizeof(uint32_t)) {
282 DPRINTF("tpm_resp->hdr.len = %u, expected = %zu\n",
283 be32_to_cpu(tpm_resp.hdr.len), sizeof(tpm_resp));
284 DPRINTF("tpm_resp->len = %u, expected = %zu\n",
285 be32_to_cpu(tpm_resp.len), sizeof(uint32_t));
286 error_report("tpm_util: Got unexpected response to "
287 "TPM_GetCapability; errcode: 0x%x",
288 be32_to_cpu(tpm_resp.hdr.errcode));
289 return -EFAULT;
290 }
291 *buffersize = be32_to_cpu(tpm_resp.buffersize);
292 break;
293 }
294 case TPM_VERSION_2_0: {
295 const struct tpm2_req_get_buffer_size {
296 struct tpm_req_hdr hdr;
297 uint32_t capability;
298 uint32_t property;
299 uint32_t count;
300 } QEMU_PACKED tpm2_get_buffer_size = {
301 .hdr = {
302 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
303 .len = cpu_to_be32(sizeof(tpm2_get_buffer_size)),
304 .ordinal = cpu_to_be32(TPM2_CC_GetCapability),
305 },
306 .capability = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES),
307 .property = cpu_to_be32(TPM2_PT_MAX_COMMAND_SIZE),
308 .count = cpu_to_be32(2), /* also get TPM2_PT_MAX_RESPONSE_SIZE */
309 };
310 struct tpm2_resp_get_buffer_size {
311 struct tpm_resp_hdr hdr;
312 uint8_t more;
313 uint32_t capability;
314 uint32_t count;
315 uint32_t property1;
316 uint32_t value1;
317 uint32_t property2;
318 uint32_t value2;
319 } QEMU_PACKED tpm2_resp;
320
321 ret = tpm_util_request(tpm_fd, &tpm2_get_buffer_size,
322 sizeof(tpm2_get_buffer_size),
323 &tpm2_resp, sizeof(tpm2_resp));
324 if (ret < 0) {
325 return ret;
326 }
327
328 if (be32_to_cpu(tpm2_resp.hdr.len) != sizeof(tpm2_resp) ||
329 be32_to_cpu(tpm2_resp.count) != 2) {
330 DPRINTF("tpm2_resp->hdr.len = %u, expected = %zu\n",
331 be32_to_cpu(tpm2_resp.hdr.len), sizeof(tpm2_resp));
332 DPRINTF("tpm2_resp->len = %u, expected = %u\n",
333 be32_to_cpu(tpm2_resp.count), 2);
334 error_report("tpm_util: Got unexpected response to "
335 "TPM2_GetCapability; errcode: 0x%x",
336 be32_to_cpu(tpm2_resp.hdr.errcode));
337 return -EFAULT;
338 }
339 *buffersize = MAX(be32_to_cpu(tpm2_resp.value1),
340 be32_to_cpu(tpm2_resp.value2));
341 break;
342 }
343 case TPM_VERSION_UNSPEC:
344 return -EFAULT;
345 }
346
347 DPRINTF("buffersize of device: %zu\n", *buffersize);
348
349 return 0;
350 }
351
352 void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
353 {
354 g_free(tsb->buffer);
355 tsb->buffer = NULL;
356 tsb->size = 0;
357 }