Skip to content

Commit 4d57856

Browse files
jejbJarkko Sakkinen
authored andcommitted
tpm2: add session handle context saving and restoring to the space code
Sessions are different from transient objects in that their handles may not be virtualized (because they're used for some hmac calculations). Additionally when a session is context saved, a vestigial memory remains in the TPM and if it is also flushed, that will be lost and the session context will refuse to load next time, so the code is updated to flush only transient objects after a context save. Add a separate array (chip->session_tbl) to save and restore sessions by handle. Use the failure of a context save or load to signal that the session has been flushed from the TPM and we can remove its memory from chip->session_tbl. Sessions are also isolated during each instance of a tpm space. This means that spaces shouldn't be able to see each other's sessions and is enforced by ensuring that a space user may only refer to sessions handles that are present in their own chip->session_tbl. Finally when a space is closed, all the sessions belonging to it should be flushed so the handles may be re-used by other spaces. Note that if we get a session save or load error, all sessions are effectively flushed. Even though we restore the session buffer, all the old sessions will refuse to load after the flush and they'll be purged from our session memory. This means that while transient context handling is still soft in the face of errors, session handling is hard (any failure of the model means all sessions are lost). Fixes-from: Colin Ian King <colin.king@canonical.com> Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
1 parent fdc915f commit 4d57856

4 files changed

Lines changed: 116 additions & 13 deletions

File tree

‎drivers/char/tpm/tpm-chip.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static void tpm_dev_release(struct device *dev)
130130

131131
kfree(chip->log.bios_event_log);
132132
kfree(chip->work_space.context_buf);
133+
kfree(chip->work_space.session_buf);
133134
kfree(chip);
134135
}
135136

@@ -224,6 +225,11 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
224225
rc = -ENOMEM;
225226
goto out;
226227
}
228+
chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
229+
if (!chip->work_space.session_buf) {
230+
rc = -ENOMEM;
231+
goto out;
232+
}
227233

228234
return chip;
229235

@@ -294,7 +300,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
294300
"unable to cdev_add() %s, major %d, minor %d, err=%d\n",
295301
dev_name(&chip->devs), MAJOR(chip->devs.devt),
296302
MINOR(chip->devs.devt), rc);
297-
tpm_del_char_device(chip, true);
298303
return rc;
299304
}
300305

@@ -306,7 +311,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
306311
dev_name(&chip->devs), MAJOR(chip->devs.devt),
307312
MINOR(chip->devs.devt), rc);
308313
cdev_del(&chip->cdevs);
309-
tpm_del_char_device(chip, true);
310314
return rc;
311315
}
312316

‎drivers/char/tpm/tpm.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ enum tpm2_cc_attrs {
161161
struct tpm_space {
162162
u32 context_tbl[3];
163163
u8 *context_buf;
164+
u32 session_tbl[3];
165+
u8 *session_buf;
164166
};
165167

166168
enum tpm_chip_flags {
@@ -588,7 +590,7 @@ unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
588590
int tpm2_probe(struct tpm_chip *chip);
589591
int tpm2_find_cc(struct tpm_chip *chip, u32 cc);
590592
int tpm2_init_space(struct tpm_space *space);
591-
void tpm2_del_space(struct tpm_space *space);
593+
void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space);
592594
int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
593595
u8 *cmd);
594596
int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,

‎drivers/char/tpm/tpm2-space.c‎

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,39 @@ struct tpm2_context {
3232
__be16 blob_size;
3333
} __packed;
3434

35+
static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
36+
{
37+
int i;
38+
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],
42+
TPM_TRANSMIT_UNLOCKED);
43+
}
44+
}
45+
3546
int tpm2_init_space(struct tpm_space *space)
3647
{
3748
space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
3849
if (!space->context_buf)
3950
return -ENOMEM;
4051

52+
space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
53+
if (space->session_buf == NULL) {
54+
kfree(space->context_buf);
55+
return -ENOMEM;
56+
}
57+
4158
return 0;
4259
}
4360

44-
void tpm2_del_space(struct tpm_space *space)
61+
void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)
4562
{
63+
mutex_lock(&chip->tpm_mutex);
64+
tpm2_flush_sessions(chip, space);
65+
mutex_unlock(&chip->tpm_mutex);
4666
kfree(space->context_buf);
67+
kfree(space->session_buf);
4768
}
4869

4970
static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
@@ -69,6 +90,20 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
6990
__func__, rc);
7091
tpm_buf_destroy(&tbuf);
7192
return -EFAULT;
93+
} else if (tpm2_rc_value(rc) == TPM2_RC_HANDLE ||
94+
rc == TPM2_RC_REFERENCE_H0) {
95+
/*
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.
101+
*
102+
* TPM_RC_REFERENCE_H0 means the session has been
103+
* flushed outside the space
104+
*/
105+
rc = -ENOENT;
106+
tpm_buf_destroy(&tbuf);
72107
} else if (rc > 0) {
73108
dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
74109
__func__, rc);
@@ -121,7 +156,6 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
121156
}
122157

123158
memcpy(&buf[*offset], &tbuf.data[TPM_HEADER_SIZE], body_size);
124-
tpm2_flush_context_cmd(chip, handle, TPM_TRANSMIT_UNLOCKED);
125159
*offset += body_size;
126160
tpm_buf_destroy(&tbuf);
127161
return 0;
@@ -136,6 +170,8 @@ static void tpm2_flush_space(struct tpm_chip *chip)
136170
if (space->context_tbl[i] && ~space->context_tbl[i])
137171
tpm2_flush_context_cmd(chip, space->context_tbl[i],
138172
TPM_TRANSMIT_UNLOCKED);
173+
174+
tpm2_flush_sessions(chip, space);
139175
}
140176

141177
static int tpm2_load_space(struct tpm_chip *chip)
@@ -161,6 +197,28 @@ static int tpm2_load_space(struct tpm_chip *chip)
161197
return rc;
162198
}
163199

200+
for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
201+
u32 handle;
202+
203+
if (!space->session_tbl[i])
204+
continue;
205+
206+
rc = tpm2_load_context(chip, space->session_buf,
207+
&offset, &handle);
208+
if (rc == -ENOENT) {
209+
/* load failed, just forget session */
210+
space->session_tbl[i] = 0;
211+
} else if (rc) {
212+
tpm2_flush_space(chip);
213+
return rc;
214+
}
215+
if (handle != space->session_tbl[i]) {
216+
dev_warn(&chip->dev, "session restored to wrong handle\n");
217+
tpm2_flush_space(chip);
218+
return -EFAULT;
219+
}
220+
}
221+
164222
return 0;
165223
}
166224

@@ -171,7 +229,7 @@ static bool tpm2_map_to_phandle(struct tpm_space *space, void *handle)
171229
int i;
172230

173231
i = 0xFFFFFF - (vhandle & 0xFFFFFF);
174-
if (i > ARRAY_SIZE(space->context_tbl) || !space->context_tbl[i])
232+
if (i >= ARRAY_SIZE(space->context_tbl) || !space->context_tbl[i])
175233
return false;
176234

177235
phandle = space->context_tbl[i];
@@ -215,7 +273,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
215273

216274
memcpy(&chip->work_space.context_tbl, &space->context_tbl,
217275
sizeof(space->context_tbl));
276+
memcpy(&chip->work_space.session_tbl, &space->session_tbl,
277+
sizeof(space->session_tbl));
218278
memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
279+
memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
219280

220281
rc = tpm2_load_space(chip);
221282
if (rc) {
@@ -232,6 +293,22 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
232293
return 0;
233294
}
234295

296+
static bool tpm2_add_session(struct tpm_chip *chip, u32 handle)
297+
{
298+
struct tpm_space *space = &chip->work_space;
299+
int i;
300+
301+
for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++)
302+
if (space->session_tbl[i] == 0)
303+
break;
304+
305+
if (i == ARRAY_SIZE(space->session_tbl))
306+
return false;
307+
308+
space->session_tbl[i] = handle;
309+
return true;
310+
}
311+
235312
static u32 tpm2_map_to_vhandle(struct tpm_space *space, u32 phandle, bool alloc)
236313
{
237314
int i;
@@ -288,6 +365,8 @@ static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
288365
break;
289366
case TPM2_HT_HMAC_SESSION:
290367
case TPM2_HT_POLICY_SESSION:
368+
if (!tpm2_add_session(chip, phandle))
369+
goto out_no_slots;
291370
break;
292371
default:
293372
dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
@@ -350,15 +429,11 @@ static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
350429
data->handles[j] = cpu_to_be32(vhandle);
351430
j++;
352431
break;
353-
case TPM2_HT_HMAC_SESSION:
354-
case TPM2_HT_POLICY_SESSION:
432+
433+
default:
355434
data->handles[j] = cpu_to_be32(phandle);
356435
j++;
357436
break;
358-
default:
359-
dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
360-
__func__, phandle);
361-
break;
362437
}
363438

364439
}
@@ -388,9 +463,28 @@ static int tpm2_save_space(struct tpm_chip *chip)
388463
} else if (rc)
389464
return rc;
390465

466+
tpm2_flush_context_cmd(chip, space->context_tbl[i],
467+
TPM_TRANSMIT_UNLOCKED);
391468
space->context_tbl[i] = ~0;
392469
}
393470

471+
for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
472+
if (!space->session_tbl[i])
473+
continue;
474+
475+
rc = tpm2_save_context(chip, space->session_tbl[i],
476+
space->session_buf, PAGE_SIZE,
477+
&offset);
478+
479+
if (rc == -ENOENT) {
480+
/* handle error saving session, just forget it */
481+
space->session_tbl[i] = 0;
482+
} else if (rc < 0) {
483+
tpm2_flush_space(chip);
484+
return rc;
485+
}
486+
}
487+
394488
return 0;
395489
}
396490

@@ -425,7 +519,10 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
425519

426520
memcpy(&space->context_tbl, &chip->work_space.context_tbl,
427521
sizeof(space->context_tbl));
522+
memcpy(&space->session_tbl, &chip->work_space.session_tbl,
523+
sizeof(space->session_tbl));
428524
memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
525+
memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
429526

430527
return 0;
431528
}

‎drivers/char/tpm/tpmrm-dev.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int tpmrm_release(struct inode *inode, struct file *file)
3939
struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
4040

4141
tpm_common_release(file, fpriv);
42-
tpm2_del_space(&priv->space);
42+
tpm2_del_space(fpriv->chip, &priv->space);
4343
kfree(priv);
4444

4545
return 0;

0 commit comments

Comments
 (0)