Skip to content

Commit bda1cbf

Browse files
committed
tpm2-sessions: Fix tpm2_read_public range checks
tpm2_read_public() has some rudimentary range checks but the function does not ensure that the response buffer has enough bytes for the full TPMT_HA payload. Re-implement the function with necessary checks and validation, and return name and name size for all handle types back to the caller. Cc: stable@vger.kernel.org # v6.10+ Fixes: d0a25bb ("tpm: Add HMAC session name/handle append") Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org> Reviewed-by: Jonathan McDowell <noodles@meta.com>
1 parent 6e9722e commit bda1cbf

2 files changed

Lines changed: 53 additions & 44 deletions

File tree

drivers/char/tpm/tpm2-cmd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
* used by the kernel internally.
1212
*/
1313

14+
#include "linux/dev_printk.h"
15+
#include "linux/tpm.h"
1416
#include "tpm.h"
1517
#include <crypto/hash_info.h>
18+
#include <linux/unaligned.h>
1619

1720
static bool disable_pcr_integrity;
1821
module_param(disable_pcr_integrity, bool, 0444);

drivers/char/tpm/tpm2-sessions.c

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -163,53 +163,61 @@ static int name_size(const u8 *name)
163163
}
164164
}
165165

166-
static int tpm2_parse_read_public(char *name, struct tpm_buf *buf)
166+
static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
167167
{
168-
struct tpm_header *head = (struct tpm_header *)buf->data;
168+
u32 mso = tpm2_handle_mso(handle);
169169
off_t offset = TPM_HEADER_SIZE;
170-
u32 tot_len = be32_to_cpu(head->length);
171-
int ret;
172-
u32 val;
173-
174-
/* we're starting after the header so adjust the length */
175-
tot_len -= TPM_HEADER_SIZE;
176-
177-
/* skip public */
178-
val = tpm_buf_read_u16(buf, &offset);
179-
if (val > tot_len)
180-
return -EINVAL;
181-
offset += val;
182-
/* name */
183-
val = tpm_buf_read_u16(buf, &offset);
184-
ret = name_size(&buf->data[offset]);
185-
if (ret < 0)
186-
return ret;
187-
188-
if (val != ret)
189-
return -EINVAL;
190-
191-
memcpy(name, &buf->data[offset], val);
192-
/* forget the rest */
193-
return 0;
194-
}
195-
196-
static int tpm2_read_public(struct tpm_chip *chip, u32 handle, char *name)
197-
{
170+
int rc, name_size_alg;
198171
struct tpm_buf buf;
199-
int rc;
172+
173+
if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
174+
mso != TPM2_MSO_NVRAM) {
175+
memcpy(name, &handle, sizeof(u32));
176+
return sizeof(u32);
177+
}
200178

201179
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_READ_PUBLIC);
202180
if (rc)
203181
return rc;
204182

205183
tpm_buf_append_u32(&buf, handle);
206-
rc = tpm_transmit_cmd(chip, &buf, 0, "read public");
207-
if (rc == TPM2_RC_SUCCESS)
208-
rc = tpm2_parse_read_public(name, &buf);
209184

210-
tpm_buf_destroy(&buf);
185+
rc = tpm_transmit_cmd(chip, &buf, 0, "TPM2_ReadPublic");
186+
if (rc) {
187+
tpm_buf_destroy(&buf);
188+
return tpm_ret_to_err(rc);
189+
}
211190

212-
return rc;
191+
/* Skip TPMT_PUBLIC: */
192+
offset += tpm_buf_read_u16(&buf, &offset);
193+
194+
/*
195+
* Ensure space for the length field of TPM2B_NAME and hashAlg field of
196+
* TPMT_HA (the extra four bytes).
197+
*/
198+
if (offset + 4 > tpm_buf_length(&buf)) {
199+
tpm_buf_destroy(&buf);
200+
return -EIO;
201+
}
202+
203+
rc = tpm_buf_read_u16(&buf, &offset);
204+
name_size_alg = name_size(&buf.data[offset]);
205+
206+
if (name_size_alg < 0)
207+
return name_size_alg;
208+
209+
if (rc != name_size_alg) {
210+
tpm_buf_destroy(&buf);
211+
return -EIO;
212+
}
213+
214+
if (offset + rc > tpm_buf_length(&buf)) {
215+
tpm_buf_destroy(&buf);
216+
return -EIO;
217+
}
218+
219+
memcpy(name, &buf.data[offset], rc);
220+
return name_size_alg;
213221
}
214222
#endif /* CONFIG_TCG_TPM2_HMAC */
215223

@@ -243,6 +251,7 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
243251
#ifdef CONFIG_TCG_TPM2_HMAC
244252
enum tpm2_mso_type mso = tpm2_handle_mso(handle);
245253
struct tpm2_auth *auth;
254+
u16 name_size_alg;
246255
int slot;
247256
int ret;
248257
#endif
@@ -273,8 +282,10 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
273282
mso == TPM2_MSO_NVRAM) {
274283
if (!name) {
275284
ret = tpm2_read_public(chip, handle, auth->name[slot]);
276-
if (ret)
285+
if (ret < 0)
277286
goto err;
287+
288+
name_size_alg = ret;
278289
}
279290
} else {
280291
if (name) {
@@ -286,13 +297,8 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
286297
}
287298

288299
auth->name_h[slot] = handle;
289-
if (name) {
290-
ret = name_size(name);
291-
if (ret < 0)
292-
goto err;
293-
294-
memcpy(auth->name[slot], name, ret);
295-
}
300+
if (name)
301+
memcpy(auth->name[slot], name, name_size_alg);
296302
#endif
297303
return 0;
298304

0 commit comments

Comments
 (0)