tpmdd-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND 0/2] Avoid sending invalid data to the TPM
@ 2017-08-24  8:35 Alexander Steffen
       [not found] ` <20170824083545.13280-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Alexander Steffen @ 2017-08-24  8:35 UTC (permalink / raw)
  To: jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

When trying to send invalid commands (with mismatching commandSize values)
to the TPM we discovered some cases in which data is sent to the TPM that
should not be sent there. Similar problems were fixed years ago, but this
one slipped through it seems.

Alexander Steffen (2):
  tpm-dev-common: Reject too short writes
  tpm-interface: Fix checks of buffer size

 drivers/char/tpm/tpm-dev-common.c |  2 +-
 drivers/char/tpm/tpm-interface.c  | 16 ++++++++--------
 drivers/char/tpm/tpm.h            |  3 ++-
 3 files changed, 11 insertions(+), 10 deletions(-)

-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
       [not found] ` <20170824083545.13280-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
@ 2017-08-24  8:35   ` Alexander Steffen
  2017-08-25 16:54     ` Jarkko Sakkinen
  2017-08-24  8:35   ` [PATCH RESEND 2/2] tpm-interface: Fix checks of buffer size Alexander Steffen
  2017-08-25 16:44   ` [PATCH RESEND 0/2] Avoid sending invalid data to the TPM Jarkko Sakkinen
  2 siblings, 1 reply; 10+ messages in thread
From: Alexander Steffen @ 2017-08-24  8:35 UTC (permalink / raw)
  To: jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f
  Cc: stable-u79uwXL29TY76Z2rM5mHXA

tpm_common_write() in tpm-dev-common.c discards the information how much
data has actually been written to the buffer. Instead, all other code has
to rely on the commandSize field in the TPM command header to figure out
how many valid bytes are supposed to be in the buffer.

But there is nothing that enforces the value in the header to match the
actual buffer contents. So by claiming a larger size in the header than
has been written, stale buffer contents are sent to the TPM. With this
commit, this problem is detected and rejected accordingly.

This should have been fixed with CVE-2011-1161 long ago, but apparently
a correct version of that patch never made it into the kernel.

Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Signed-off-by: Alexander Steffen <Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
---
 drivers/char/tpm/tpm-dev-common.c | 2 +-
 drivers/char/tpm/tpm-interface.c  | 9 ++++++---
 drivers/char/tpm/tpm.h            | 3 ++-
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c
index 610638a..c39b581 100644
--- a/drivers/char/tpm/tpm-dev-common.c
+++ b/drivers/char/tpm/tpm-dev-common.c
@@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
 		return -EPIPE;
 	}
 	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
-				sizeof(priv->data_buffer), 0);
+				sizeof(priv->data_buffer), in_size, 0);
 
 	tpm_put_ops(priv->chip);
 	if (out_size < 0) {
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 1d6729b..78fb41d 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -375,6 +375,7 @@ static bool tpm_validate_command(struct tpm_chip *chip,
  * @chip: TPM chip to use
  * @buf: TPM command buffer
  * @bufsiz: length of the TPM command buffer
+ * @bufvalid: number of valid bytes in the TPM command buffer
  * @flags: tpm transmit flags - bitmap
  *
  * Return:
@@ -382,7 +383,8 @@ static bool tpm_validate_command(struct tpm_chip *chip,
  *     A negative number for system errors (errno).
  */
 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
-		     u8 *buf, size_t bufsiz, unsigned int flags)
+		     u8 *buf, size_t bufsiz, size_t bufvalid,
+		     unsigned int flags)
 {
 	struct tpm_output_header *header = (void *)buf;
 	int rc;
@@ -401,7 +403,7 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
 	if (count == 0)
 		return -ENODATA;
-	if (count > bufsiz) {
+	if (count > bufsiz || count > bufvalid) {
 		dev_err(&chip->dev,
 			"invalid count value %x %zx\n", count, bufsiz);
 		return -E2BIG;
@@ -522,7 +524,8 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 	int err;
 	ssize_t len;
 
-	len = tpm_transmit(chip, space, (u8 *)buf, bufsiz, flags);
+	len = tpm_transmit(chip, space, (u8 *)buf, bufsiz,
+			   be32_to_cpu(header->length), flags);
 	if (len <  0)
 		return len;
 
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 2d5466a..a020eca 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -511,7 +511,8 @@ enum tpm_transmit_flags {
 };
 
 ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
-		     u8 *buf, size_t bufsiz, unsigned int flags);
+		     u8 *buf, size_t bufsiz, size_t bufvalid,
+		     unsigned int flags);
 ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 			 const void *buf, size_t bufsiz,
 			 size_t min_rsp_body_length, unsigned int flags,
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RESEND 2/2] tpm-interface: Fix checks of buffer size
       [not found] ` <20170824083545.13280-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
  2017-08-24  8:35   ` [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes Alexander Steffen
@ 2017-08-24  8:35   ` Alexander Steffen
  2017-08-25 16:44   ` [PATCH RESEND 0/2] Avoid sending invalid data to the TPM Jarkko Sakkinen
  2 siblings, 0 replies; 10+ messages in thread
From: Alexander Steffen @ 2017-08-24  8:35 UTC (permalink / raw)
  To: jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

bufsiz contains the length of the buffer, whereas bufvalid contains the
number of valid bytes within the buffer. Therefore, bufvalid should be used
as a limit when reading from the buffer and bufsize when writing to it.

Signed-off-by: Alexander Steffen <Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
---
 drivers/char/tpm/tpm-interface.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 78fb41d..f481cd1 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -393,19 +393,16 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	unsigned long stop;
 	bool need_locality;
 
-	if (!tpm_validate_command(chip, space, buf, bufsiz))
+	if (!tpm_validate_command(chip, space, buf, bufvalid))
 		return -EINVAL;
 
-	if (bufsiz > TPM_BUFSIZE)
-		bufsiz = TPM_BUFSIZE;
-
 	count = be32_to_cpu(*((__be32 *) (buf + 2)));
 	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
 	if (count == 0)
 		return -ENODATA;
-	if (count > bufsiz || count > bufvalid) {
+	if (count > bufvalid) {
 		dev_err(&chip->dev,
-			"invalid count value %x %zx\n", count, bufsiz);
+			"invalid count value %x %zx\n", count, bufvalid);
 		return -E2BIG;
 	}
 
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 0/2] Avoid sending invalid data to the TPM
       [not found] ` <20170824083545.13280-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
  2017-08-24  8:35   ` [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes Alexander Steffen
  2017-08-24  8:35   ` [PATCH RESEND 2/2] tpm-interface: Fix checks of buffer size Alexander Steffen
@ 2017-08-25 16:44   ` Jarkko Sakkinen
       [not found]     ` <20170825164416.svo7khm4zsmosxbx-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
  2 siblings, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-08-25 16:44 UTC (permalink / raw)
  To: Alexander Steffen; +Cc: tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Thu, Aug 24, 2017 at 10:35:43AM +0200, Alexander Steffen wrote:
> When trying to send invalid commands (with mismatching commandSize values)
> to the TPM we discovered some cases in which data is sent to the TPM that
> should not be sent there. Similar problems were fixed years ago, but this
> one slipped through it seems.
> 
> Alexander Steffen (2):
>   tpm-dev-common: Reject too short writes
>   tpm-interface: Fix checks of buffer size
> 
>  drivers/char/tpm/tpm-dev-common.c |  2 +-
>  drivers/char/tpm/tpm-interface.c  | 16 ++++++++--------
>  drivers/char/tpm/tpm.h            |  3 ++-
>  3 files changed, 11 insertions(+), 10 deletions(-)
> 
> -- 
> 2.7.4
> 

Have you checked that these do no break /dev/tpmrm0?

I have some cheap unit tests here to smoke it:

https://github.com/jsakkine-intel/tpm2-scripts

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
  2017-08-24  8:35   ` [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes Alexander Steffen
@ 2017-08-25 16:54     ` Jarkko Sakkinen
       [not found]       ` <20170825165451.b7lv7t5w3nhbz7da-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-08-25 16:54 UTC (permalink / raw)
  To: Alexander Steffen; +Cc: tpmdd-devel, stable

On Thu, Aug 24, 2017 at 10:35:44AM +0200, Alexander Steffen wrote:
> tpm_common_write() in tpm-dev-common.c discards the information how much
> data has actually been written to the buffer. Instead, all other code has
> to rely on the commandSize field in the TPM command header to figure out
> how many valid bytes are supposed to be in the buffer.
> 
> But there is nothing that enforces the value in the header to match the
> actual buffer contents. So by claiming a larger size in the header than
> has been written, stale buffer contents are sent to the TPM. With this
> commit, this problem is detected and rejected accordingly.
> 
> This should have been fixed with CVE-2011-1161 long ago, but apparently
> a correct version of that patch never made it into the kernel.
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Alexander Steffen <Alexander.Steffen@infineon.com>
> ---
>  drivers/char/tpm/tpm-dev-common.c | 2 +-
>  drivers/char/tpm/tpm-interface.c  | 9 ++++++---
>  drivers/char/tpm/tpm.h            | 3 ++-
>  3 files changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c
> index 610638a..c39b581 100644
> --- a/drivers/char/tpm/tpm-dev-common.c
> +++ b/drivers/char/tpm/tpm-dev-common.c
> @@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
>  		return -EPIPE;
>  	}
>  	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> -				sizeof(priv->data_buffer), 0);
> +				sizeof(priv->data_buffer), in_size, 0);

Why you couldn't just

unsigned int bufsiz;

/* ... */

bufsiz = sizeof(priv->data_buffer);
if (in_size < bufsiz)
	bufsiz = in_size;

out_size = tpm_transmit(priv->chip, space, priv->data_buffer, bufsiz, 0);

/Jarkko

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
       [not found]       ` <20170825165451.b7lv7t5w3nhbz7da-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
@ 2017-08-28 17:11         ` Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w
  2017-08-30 10:40           ` Jarkko Sakkinen
  0 siblings, 1 reply; 10+ messages in thread
From: Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w @ 2017-08-28 17:11 UTC (permalink / raw)
  To: jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA
  Cc: tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f,
	stable-u79uwXL29TY76Z2rM5mHXA

> > diff --git a/drivers/char/tpm/tpm-dev-common.c
> > b/drivers/char/tpm/tpm-dev-common.c
> > index 610638a..c39b581 100644
> > --- a/drivers/char/tpm/tpm-dev-common.c
> > +++ b/drivers/char/tpm/tpm-dev-common.c
> > @@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file, const
> char __user *buf,
> >  		return -EPIPE;
> >  	}
> >  	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > -				sizeof(priv->data_buffer), 0);
> > +				sizeof(priv->data_buffer), in_size, 0);
> 
> Why you couldn't just
> 
> unsigned int bufsiz;
> 
> /* ... */
> 
> bufsiz = sizeof(priv->data_buffer);
> if (in_size < bufsiz)
> 	bufsiz = in_size;
> 
> out_size = tpm_transmit(priv->chip, space, priv->data_buffer, bufsiz, 0);

Because the code needs to know both how large the buffer is (in order to avoid buffer overflows when writing to it) and how much of the data in the buffer is valid (in order not to send random junk to the TPM). This is made more explicit in PATCH 2/2.

Your example fails as soon as the response is longer than the command.

Alexander
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
  2017-08-28 17:11         ` Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w
@ 2017-08-30 10:40           ` Jarkko Sakkinen
  2017-09-04 17:35             ` Alexander.Steffen
  0 siblings, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-08-30 10:40 UTC (permalink / raw)
  To: Alexander.Steffen; +Cc: tpmdd-devel, stable

On Mon, Aug 28, 2017 at 05:11:00PM +0000, Alexander.Steffen@infineon.com wrote:
> > > diff --git a/drivers/char/tpm/tpm-dev-common.c
> > > b/drivers/char/tpm/tpm-dev-common.c
> > > index 610638a..c39b581 100644
> > > --- a/drivers/char/tpm/tpm-dev-common.c
> > > +++ b/drivers/char/tpm/tpm-dev-common.c
> > > @@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file, const
> > char __user *buf,
> > >  		return -EPIPE;
> > >  	}
> > >  	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > > -				sizeof(priv->data_buffer), 0);
> > > +				sizeof(priv->data_buffer), in_size, 0);
> > 
> > Why you couldn't just
> > 
> > unsigned int bufsiz;
> > 
> > /* ... */
> > 
> > bufsiz = sizeof(priv->data_buffer);
> > if (in_size < bufsiz)
> > 	bufsiz = in_size;
> > 
> > out_size = tpm_transmit(priv->chip, space, priv->data_buffer, bufsiz, 0);
> 
> Because the code needs to know both how large the buffer is (in order to avoid buffer overflows when writing to it) and how much of the data in the buffer is valid (in order not to send random junk to the TPM). This is made more explicit in PATCH 2/2.
> 
> Your example fails as soon as the response is longer than the command.
> 
> Alexander

Got you.

Do the comparison for count tpm-dev-common.c as it is the only call site
where this is needed instead of scrabbling with the parameters. In other
call sites this is unnecessary at this point.

This will also make backporting a factor more sleek.

/Jarkko

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 0/2] Avoid sending invalid data to the TPM
       [not found]     ` <20170825164416.svo7khm4zsmosxbx-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
@ 2017-09-01 12:08       ` Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w
  0 siblings, 0 replies; 10+ messages in thread
From: Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w @ 2017-09-01 12:08 UTC (permalink / raw)
  To: jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA
  Cc: tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

> On Thu, Aug 24, 2017 at 10:35:43AM +0200, Alexander Steffen wrote:
> > When trying to send invalid commands (with mismatching commandSize
> > values) to the TPM we discovered some cases in which data is sent to
> > the TPM that should not be sent there. Similar problems were fixed
> > years ago, but this one slipped through it seems.
> >
> > Alexander Steffen (2):
> >   tpm-dev-common: Reject too short writes
> >   tpm-interface: Fix checks of buffer size
> >
> >  drivers/char/tpm/tpm-dev-common.c |  2 +-
> > drivers/char/tpm/tpm-interface.c  | 16 ++++++++--------
> >  drivers/char/tpm/tpm.h            |  3 ++-
> >  3 files changed, 11 insertions(+), 10 deletions(-)
> >
> > --
> > 2.7.4
> >
> 
> Have you checked that these do no break /dev/tpmrm0?
> 
> I have some cheap unit tests here to smoke it:
> 
> https://github.com/jsakkine-intel/tpm2-scripts
> 
> /Jarkko

I've now included your tests in my automation, so that they will run for all my future changes. Is it sufficient to use tpm2_smoke.py or should I also run keyctl-smoke.sh? Not having used keyctl before, I'm not sure whether it has any dependencies/side effects.

There was one thing I needed to fix to make the tests run with my TPMs and I've sent you a pull request via GitHub for it.

Alexander
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
  2017-08-30 10:40           ` Jarkko Sakkinen
@ 2017-09-04 17:35             ` Alexander.Steffen
       [not found]               ` <dd865ba5751e4d568709e0aaf209633b-nFblLGNE8XKJSz+rYg/bSJowlv4uC7bZ@public.gmane.org>
  0 siblings, 1 reply; 10+ messages in thread
From: Alexander.Steffen @ 2017-09-04 17:35 UTC (permalink / raw)
  To: jarkko.sakkinen; +Cc: tpmdd-devel, stable

> On Mon, Aug 28, 2017 at 05:11:00PM +0000,
> Alexander.Steffen@infineon.com wrote:
> > > > diff --git a/drivers/char/tpm/tpm-dev-common.c
> > > > b/drivers/char/tpm/tpm-dev-common.c
> > > > index 610638a..c39b581 100644
> > > > --- a/drivers/char/tpm/tpm-dev-common.c
> > > > +++ b/drivers/char/tpm/tpm-dev-common.c
> > > > @@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file,
> > > > const
> > > char __user *buf,
> > > >  		return -EPIPE;
> > > >  	}
> > > >  	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > > > -				sizeof(priv->data_buffer), 0);
> > > > +				sizeof(priv->data_buffer), in_size, 0);
> > >
> > > Why you couldn't just
> > >
> > > unsigned int bufsiz;
> > >
> > > /* ... */
> > >
> > > bufsiz = sizeof(priv->data_buffer);
> > > if (in_size < bufsiz)
> > > 	bufsiz = in_size;
> > >
> > > out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > > bufsiz, 0);
> >
> > Because the code needs to know both how large the buffer is (in order to
> avoid buffer overflows when writing to it) and how much of the data in the
> buffer is valid (in order not to send random junk to the TPM). This is made
> more explicit in PATCH 2/2.
> >
> > Your example fails as soon as the response is longer than the command.
> >
> > Alexander
> 
> Got you.
> 
> Do the comparison for count tpm-dev-common.c as it is the only call site
> where this is needed instead of scrabbling with the parameters. In other call
> sites this is unnecessary at this point.
> 
> This will also make backporting a factor more sleek.

I am not entirely happy with that approach, since it leads to worse code, splitting the buffer size validation logic over multiple places, but I can see how it makes backporting easier thanks to a smaller diff. A new patch is on its way.

Maybe at some point those parts of the code and the internal interfaces should be thoroughly cleaned up (i.e. rewritten).

Alexander

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes
       [not found]               ` <dd865ba5751e4d568709e0aaf209633b-nFblLGNE8XKJSz+rYg/bSJowlv4uC7bZ@public.gmane.org>
@ 2017-09-05 19:02                 ` Jarkko Sakkinen
  0 siblings, 0 replies; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-09-05 19:02 UTC (permalink / raw)
  To: Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w
  Cc: tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f,
	stable-u79uwXL29TY76Z2rM5mHXA

On Mon, Sep 04, 2017 at 05:35:00PM +0000, Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org wrote:
> > On Mon, Aug 28, 2017 at 05:11:00PM +0000,
> > Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org wrote:
> > > > > diff --git a/drivers/char/tpm/tpm-dev-common.c
> > > > > b/drivers/char/tpm/tpm-dev-common.c
> > > > > index 610638a..c39b581 100644
> > > > > --- a/drivers/char/tpm/tpm-dev-common.c
> > > > > +++ b/drivers/char/tpm/tpm-dev-common.c
> > > > > @@ -119,7 +119,7 @@ ssize_t tpm_common_write(struct file *file,
> > > > > const
> > > > char __user *buf,
> > > > >  		return -EPIPE;
> > > > >  	}
> > > > >  	out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > > > > -				sizeof(priv->data_buffer), 0);
> > > > > +				sizeof(priv->data_buffer), in_size, 0);
> > > >
> > > > Why you couldn't just
> > > >
> > > > unsigned int bufsiz;
> > > >
> > > > /* ... */
> > > >
> > > > bufsiz = sizeof(priv->data_buffer);
> > > > if (in_size < bufsiz)
> > > > 	bufsiz = in_size;
> > > >
> > > > out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
> > > > bufsiz, 0);
> > >
> > > Because the code needs to know both how large the buffer is (in order to
> > avoid buffer overflows when writing to it) and how much of the data in the
> > buffer is valid (in order not to send random junk to the TPM). This is made
> > more explicit in PATCH 2/2.
> > >
> > > Your example fails as soon as the response is longer than the command.
> > >
> > > Alexander
> > 
> > Got you.
> > 
> > Do the comparison for count tpm-dev-common.c as it is the only call site
> > where this is needed instead of scrabbling with the parameters. In other call
> > sites this is unnecessary at this point.
> > 
> > This will also make backporting a factor more sleek.
> 
> I am not entirely happy with that approach, since it leads to worse
> code, splitting the buffer size validation logic over multiple places,
> but I can see how it makes backporting easier thanks to a smaller
> diff. A new patch is on its way.

Well I do not see it as part of the common buffer validation logic if it
is required only when you access from user space.

It's not only about smaller diff but also unnecessary change of
semantics in the places where it is no absolutely needed.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-09-05 19:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-24  8:35 [PATCH RESEND 0/2] Avoid sending invalid data to the TPM Alexander Steffen
     [not found] ` <20170824083545.13280-1-Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w@public.gmane.org>
2017-08-24  8:35   ` [PATCH RESEND 1/2] tpm-dev-common: Reject too short writes Alexander Steffen
2017-08-25 16:54     ` Jarkko Sakkinen
     [not found]       ` <20170825165451.b7lv7t5w3nhbz7da-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2017-08-28 17:11         ` Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w
2017-08-30 10:40           ` Jarkko Sakkinen
2017-09-04 17:35             ` Alexander.Steffen
     [not found]               ` <dd865ba5751e4d568709e0aaf209633b-nFblLGNE8XKJSz+rYg/bSJowlv4uC7bZ@public.gmane.org>
2017-09-05 19:02                 ` Jarkko Sakkinen
2017-08-24  8:35   ` [PATCH RESEND 2/2] tpm-interface: Fix checks of buffer size Alexander Steffen
2017-08-25 16:44   ` [PATCH RESEND 0/2] Avoid sending invalid data to the TPM Jarkko Sakkinen
     [not found]     ` <20170825164416.svo7khm4zsmosxbx-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2017-09-01 12:08       ` Alexander.Steffen-d0qZbvYSIPpWk0Htik3J/w

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).