2 * Copyright (C) 2016 Intel Corporation
9 * This file contains TPM2 protocol implementations of the commands
10 * used by the kernel internally.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; version 2
18 #include <linux/gfp.h>
19 #include <asm/unaligned.h>
22 enum tpm2_handle_types {
23 TPM2_HT_HMAC_SESSION = 0x02000000,
24 TPM2_HT_POLICY_SESSION = 0x03000000,
25 TPM2_HT_TRANSIENT = 0x80000000,
35 static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
39 for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
40 if (space->session_tbl[i])
41 tpm2_flush_context_cmd(chip, space->session_tbl[i],
46 int tpm2_init_space(struct tpm_space *space)
48 space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
49 if (!space->context_buf)
52 space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
53 if (space->session_buf == NULL) {
54 kfree(space->context_buf);
61 void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)
63 mutex_lock(&chip->tpm_mutex);
64 tpm2_flush_sessions(chip, space);
65 mutex_unlock(&chip->tpm_mutex);
66 kfree(space->context_buf);
67 kfree(space->session_buf);
70 static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
71 unsigned int *offset, u32 *handle)
74 struct tpm2_context *ctx;
75 unsigned int body_size;
78 rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_LOAD);
82 ctx = (struct tpm2_context *)&buf[*offset];
83 body_size = sizeof(*ctx) + be16_to_cpu(ctx->blob_size);
84 tpm_buf_append(&tbuf, &buf[*offset], body_size);
86 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 4,
87 TPM_TRANSMIT_NESTED, NULL);
89 dev_warn(&chip->dev, "%s: failed with a system error %d\n",
91 tpm_buf_destroy(&tbuf);
93 } else if (tpm2_rc_value(rc) == TPM2_RC_HANDLE ||
94 rc == TPM2_RC_REFERENCE_H0) {
96 * TPM_RC_HANDLE means that the session context can't
97 * be loaded because of an internal counter mismatch
98 * that makes the TPM think there might have been a
99 * replay. This might happen if the context was saved
100 * and loaded outside the space.
102 * TPM_RC_REFERENCE_H0 means the session has been
103 * flushed outside the space
106 tpm_buf_destroy(&tbuf);
109 dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
111 tpm_buf_destroy(&tbuf);
115 *handle = be32_to_cpup((__be32 *)&tbuf.data[TPM_HEADER_SIZE]);
116 *offset += body_size;
118 tpm_buf_destroy(&tbuf);
122 static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
123 unsigned int buf_size, unsigned int *offset)
126 unsigned int body_size;
129 rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_SAVE);
133 tpm_buf_append_u32(&tbuf, handle);
135 rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0,
136 TPM_TRANSMIT_NESTED, NULL);
138 dev_warn(&chip->dev, "%s: failed with a system error %d\n",
140 tpm_buf_destroy(&tbuf);
142 } else if (tpm2_rc_value(rc) == TPM2_RC_REFERENCE_H0) {
143 tpm_buf_destroy(&tbuf);
146 dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
148 tpm_buf_destroy(&tbuf);
152 body_size = tpm_buf_length(&tbuf) - TPM_HEADER_SIZE;
153 if ((*offset + body_size) > buf_size) {
154 dev_warn(&chip->dev, "%s: out of backing storage\n", __func__);
155 tpm_buf_destroy(&tbuf);
159 memcpy(&buf[*offset], &tbuf.data[TPM_HEADER_SIZE], body_size);
160 *offset += body_size;
161 tpm_buf_destroy(&tbuf);
165 static void tpm2_flush_space(struct tpm_chip *chip)
167 struct tpm_space *space = &chip->work_space;
170 for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
171 if (space->context_tbl[i] && ~space->context_tbl[i])
172 tpm2_flush_context_cmd(chip, space->context_tbl[i],
173 TPM_TRANSMIT_NESTED);
175 tpm2_flush_sessions(chip, space);
178 static int tpm2_load_space(struct tpm_chip *chip)
180 struct tpm_space *space = &chip->work_space;
185 for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
186 if (!space->context_tbl[i])
189 /* sanity check, should never happen */
190 if (~space->context_tbl[i]) {
191 dev_err(&chip->dev, "context table is inconsistent");
195 rc = tpm2_load_context(chip, space->context_buf, &offset,
196 &space->context_tbl[i]);
201 for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
204 if (!space->session_tbl[i])
207 rc = tpm2_load_context(chip, space->session_buf,
210 /* load failed, just forget session */
211 space->session_tbl[i] = 0;
213 tpm2_flush_space(chip);
216 if (handle != space->session_tbl[i]) {
217 dev_warn(&chip->dev, "session restored to wrong handle\n");
218 tpm2_flush_space(chip);
226 static bool tpm2_map_to_phandle(struct tpm_space *space, void *handle)
228 u32 vhandle = be32_to_cpup((__be32 *)handle);
232 i = 0xFFFFFF - (vhandle & 0xFFFFFF);
233 if (i >= ARRAY_SIZE(space->context_tbl) || !space->context_tbl[i])
236 phandle = space->context_tbl[i];
237 *((__be32 *)handle) = cpu_to_be32(phandle);
241 static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd)
243 struct tpm_space *space = &chip->work_space;
244 unsigned int nr_handles;
249 i = tpm2_find_cc(chip, cc);
253 attrs = chip->cc_attrs_tbl[i];
254 nr_handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
256 handle = (__be32 *)&cmd[TPM_HEADER_SIZE];
257 for (i = 0; i < nr_handles; i++, handle++) {
258 if ((be32_to_cpu(*handle) & 0xFF000000) == TPM2_HT_TRANSIENT) {
259 if (!tpm2_map_to_phandle(space, handle))
267 int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
275 memcpy(&chip->work_space.context_tbl, &space->context_tbl,
276 sizeof(space->context_tbl));
277 memcpy(&chip->work_space.session_tbl, &space->session_tbl,
278 sizeof(space->session_tbl));
279 memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
280 memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
282 rc = tpm2_load_space(chip);
284 tpm2_flush_space(chip);
288 rc = tpm2_map_command(chip, cc, cmd);
290 tpm2_flush_space(chip);
297 static bool tpm2_add_session(struct tpm_chip *chip, u32 handle)
299 struct tpm_space *space = &chip->work_space;
302 for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++)
303 if (space->session_tbl[i] == 0)
306 if (i == ARRAY_SIZE(space->session_tbl))
309 space->session_tbl[i] = handle;
313 static u32 tpm2_map_to_vhandle(struct tpm_space *space, u32 phandle, bool alloc)
317 for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
319 if (!space->context_tbl[i]) {
320 space->context_tbl[i] = phandle;
323 } else if (space->context_tbl[i] == phandle)
327 if (i == ARRAY_SIZE(space->context_tbl))
330 return TPM2_HT_TRANSIENT | (0xFFFFFF - i);
333 static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
336 struct tpm_space *space = &chip->work_space;
337 struct tpm_output_header *header = (void *)rsp;
344 if (be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS)
347 i = tpm2_find_cc(chip, cc);
348 /* sanity check, should never happen */
352 attrs = chip->cc_attrs_tbl[i];
353 if (!((attrs >> TPM2_CC_ATTR_RHANDLE) & 1))
356 phandle = be32_to_cpup((__be32 *)&rsp[TPM_HEADER_SIZE]);
357 phandle_type = phandle & 0xFF000000;
359 switch (phandle_type) {
360 case TPM2_HT_TRANSIENT:
361 vhandle = tpm2_map_to_vhandle(space, phandle, true);
365 *(__be32 *)&rsp[TPM_HEADER_SIZE] = cpu_to_be32(vhandle);
367 case TPM2_HT_HMAC_SESSION:
368 case TPM2_HT_POLICY_SESSION:
369 if (!tpm2_add_session(chip, phandle))
373 dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
380 tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_NESTED);
381 dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__,
386 struct tpm2_cap_handles {
393 static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
396 struct tpm_space *space = &chip->work_space;
397 struct tpm_output_header *header = (void *)rsp;
398 struct tpm2_cap_handles *data;
405 if (cc != TPM2_CC_GET_CAPABILITY ||
406 be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS) {
410 if (len < TPM_HEADER_SIZE + 9)
413 data = (void *)&rsp[TPM_HEADER_SIZE];
414 if (be32_to_cpu(data->capability) != TPM2_CAP_HANDLES)
417 if (len != TPM_HEADER_SIZE + 9 + 4 * be32_to_cpu(data->count))
420 for (i = 0, j = 0; i < be32_to_cpu(data->count); i++) {
421 phandle = be32_to_cpup((__be32 *)&data->handles[i]);
422 phandle_type = phandle & 0xFF000000;
424 switch (phandle_type) {
425 case TPM2_HT_TRANSIENT:
426 vhandle = tpm2_map_to_vhandle(space, phandle, false);
430 data->handles[j] = cpu_to_be32(vhandle);
435 data->handles[j] = cpu_to_be32(phandle);
442 header->length = cpu_to_be32(TPM_HEADER_SIZE + 9 + 4 * j);
443 data->count = cpu_to_be32(j);
447 static int tpm2_save_space(struct tpm_chip *chip)
449 struct tpm_space *space = &chip->work_space;
454 for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
455 if (!(space->context_tbl[i] && ~space->context_tbl[i]))
458 rc = tpm2_save_context(chip, space->context_tbl[i],
459 space->context_buf, PAGE_SIZE,
462 space->context_tbl[i] = 0;
467 tpm2_flush_context_cmd(chip, space->context_tbl[i],
468 TPM_TRANSMIT_NESTED);
469 space->context_tbl[i] = ~0;
472 for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
473 if (!space->session_tbl[i])
476 rc = tpm2_save_context(chip, space->session_tbl[i],
477 space->session_buf, PAGE_SIZE,
481 /* handle error saving session, just forget it */
482 space->session_tbl[i] = 0;
484 tpm2_flush_space(chip);
492 int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
493 u32 cc, u8 *buf, size_t *bufsiz)
495 struct tpm_output_header *header = (void *)buf;
501 rc = tpm2_map_response_header(chip, cc, buf, *bufsiz);
503 tpm2_flush_space(chip);
507 rc = tpm2_map_response_body(chip, cc, buf, *bufsiz);
509 tpm2_flush_space(chip);
513 rc = tpm2_save_space(chip);
515 tpm2_flush_space(chip);
519 *bufsiz = be32_to_cpu(header->length);
521 memcpy(&space->context_tbl, &chip->work_space.context_tbl,
522 sizeof(space->context_tbl));
523 memcpy(&space->session_tbl, &chip->work_space.session_tbl,
524 sizeof(space->session_tbl));
525 memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
526 memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);