]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.14/tpm-tpm_crb-avoid-unaligned-reads-in-crb_recv.patch
d36edf4f0054d5ccd270c790771388e183f322d4
[thirdparty/kernel/stable-queue.git] / queue-4.14 / tpm-tpm_crb-avoid-unaligned-reads-in-crb_recv.patch
1 From 3d7a850fdc1a2e4d2adbc95cc0fc962974725e88 Mon Sep 17 00:00:00 2001
2 From: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
3 Date: Mon, 4 Feb 2019 15:59:43 +0200
4 Subject: tpm/tpm_crb: Avoid unaligned reads in crb_recv()
5
6 From: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7
8 commit 3d7a850fdc1a2e4d2adbc95cc0fc962974725e88 upstream.
9
10 The current approach to read first 6 bytes from the response and then tail
11 of the response, can cause the 2nd memcpy_fromio() to do an unaligned read
12 (e.g. read 32-bit word from address aligned to a 16-bits), depending on how
13 memcpy_fromio() is implemented. If this happens, the read will fail and the
14 memory controller will fill the read with 1's.
15
16 This was triggered by 170d13ca3a2f, which should be probably refined to
17 check and react to the address alignment. Before that commit, on x86
18 memcpy_fromio() turned out to be memcpy(). By a luck GCC has done the right
19 thing (from tpm_crb's perspective) for us so far, but we should not rely on
20 that. Thus, it makes sense to fix this also in tpm_crb, not least because
21 the fix can be then backported to stable kernels and make them more robust
22 when compiled in differing environments.
23
24 Cc: stable@vger.kernel.org
25 Cc: James Morris <jmorris@namei.org>
26 Cc: Tomas Winkler <tomas.winkler@intel.com>
27 Cc: Jerry Snitselaar <jsnitsel@redhat.com>
28 Fixes: 30fc8d138e91 ("tpm: TPM 2.0 CRB Interface")
29 Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
30 Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com>
31 Acked-by: Tomas Winkler <tomas.winkler@intel.com>
32 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
33
34 ---
35 drivers/char/tpm/tpm_crb.c | 22 ++++++++++++++++------
36 1 file changed, 16 insertions(+), 6 deletions(-)
37
38 --- a/drivers/char/tpm/tpm_crb.c
39 +++ b/drivers/char/tpm/tpm_crb.c
40 @@ -288,19 +288,29 @@ static int crb_recv(struct tpm_chip *chi
41 struct crb_priv *priv = dev_get_drvdata(&chip->dev);
42 unsigned int expected;
43
44 - /* sanity check */
45 - if (count < 6)
46 + /* A sanity check that the upper layer wants to get at least the header
47 + * as that is the minimum size for any TPM response.
48 + */
49 + if (count < TPM_HEADER_SIZE)
50 return -EIO;
51
52 + /* If this bit is set, according to the spec, the TPM is in
53 + * unrecoverable condition.
54 + */
55 if (ioread32(&priv->regs_t->ctrl_sts) & CRB_CTRL_STS_ERROR)
56 return -EIO;
57
58 - memcpy_fromio(buf, priv->rsp, 6);
59 - expected = be32_to_cpup((__be32 *) &buf[2]);
60 - if (expected > count || expected < 6)
61 + /* Read the first 8 bytes in order to get the length of the response.
62 + * We read exactly a quad word in order to make sure that the remaining
63 + * reads will be aligned.
64 + */
65 + memcpy_fromio(buf, priv->rsp, 8);
66 +
67 + expected = be32_to_cpup((__be32 *)&buf[2]);
68 + if (expected > count || expected < TPM_HEADER_SIZE)
69 return -EIO;
70
71 - memcpy_fromio(&buf[6], &priv->rsp[6], expected - 6);
72 + memcpy_fromio(&buf[8], &priv->rsp[8], expected - 8);
73
74 return expected;
75 }