mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nayna Jain <nayna@linux.vnet.ibm.com>
To: tpmdd-devel@lists.sourceforge.net
Cc: peterhuewe@gmx.de, tpmdd@selhorst.net,
	jarkko.sakkinen@linux.intel.com, jgunthorpe@obsidianresearch.com,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Nayna Jain <nayna@linux.vnet.ibm.com>
Subject: [PATCH] tpm: add buffer access validation in tpm2_get_pcr_allocation()
Date: Fri, 27 Jan 2017 10:25:49 -0500	[thread overview]
Message-ID: <1485530749-22948-1-git-send-email-nayna@linux.vnet.ibm.com> (raw)

This patch add validation in tpm2_get_pcr_allocation to avoid
access beyond response buffer length.

Suggested-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: Nayna Jain <nayna@linux.vnet.ibm.com>
---
 drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 4aad84c..02c1ea7 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	struct tpm2_pcr_selection pcr_selection;
 	struct tpm_buf buf;
 	void *marker;
-	unsigned int count = 0;
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;
 	int rc;
-	int i;
+	int i = 0;
 
 	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
 	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	}
 
 	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];
+
 	for (i = 0; i < count; i++) {
+		pcr_select_offset = marker +
+			offsetof(struct tpm2_pcr_selection, size_of_select);
+		if (pcr_select_offset >= end) {
+			rc = -EFAULT;
+			break;
+		}
+
 		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
 		chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
-		marker = marker + sizeof(struct tpm2_pcr_selection);
+		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
+			sizeof(pcr_selection.size_of_select) +
+			sizeof(u8) * pcr_selection.size_of_select;
+		marker = marker + sizeof_pcr_selection;
 	}
 
 out:
-	if (count < ARRAY_SIZE(chip->active_banks))
-		chip->active_banks[count] = TPM2_ALG_ERROR;
+	if (i < ARRAY_SIZE(chip->active_banks))
+		chip->active_banks[i] = TPM2_ALG_ERROR;
 
 	tpm_buf_destroy(&buf);
 
-- 
2.5.0

             reply	other threads:[~2017-01-27 15:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-27 15:25 Nayna Jain [this message]
2017-01-29 14:40 ` Jarkko Sakkinen
2017-01-29 17:18   ` Nayna
2017-01-29 21:20     ` Jarkko Sakkinen
2017-01-30  2:58       ` Nayna
2017-01-30 21:47         ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1485530749-22948-1-git-send-email-nayna@linux.vnet.ibm.com \
    --to=nayna@linux.vnet.ibm.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=jgunthorpe@obsidianresearch.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=peterhuewe@gmx.de \
    --cc=tpmdd-devel@lists.sourceforge.net \
    --cc=tpmdd@selhorst.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®