stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] TPM: Zero buffer whole after copying to userspace
       [not found] <1323196162-2717-1-git-send-email-tim.gardner@canonical.com>
@ 2011-12-06 18:29 ` Tim Gardner
  2012-02-03 17:39   ` Rajiv Andrade
  2011-12-06 18:29 ` [PATCH 2/3] TPM: Close data_pending and data_buffer races Tim Gardner
  1 sibling, 1 reply; 4+ messages in thread
From: Tim Gardner @ 2011-12-06 18:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: Seth Forshee, Tim Gardner, Debora Velarde, Rajiv Andrade,
	Marcel Selhorst, tpmdd-devel, stable

Commit 3321c07ae5068568cd61ac9f4ba749006a7185c9 correctly clears the TPM
buffer if the user specified read length is >= the TPM buffer length. However,
if the user specified read length is < the TPM buffer length, then part of the
TPM buffer is left uncleared.

Reported-by: Seth Forshee <seth.forshee@canonical.com>
Cc: Debora Velarde <debora@linux.vnet.ibm.com>
Cc: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
Cc: Marcel Selhorst <m.selhorst@sirrix.com>
Cc: tpmdd-devel@lists.sourceforge.net
Cc: stable@vger.kernel.org
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 drivers/char/tpm/tpm.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
index 361a1df..b366b34 100644
--- a/drivers/char/tpm/tpm.c
+++ b/drivers/char/tpm/tpm.c
@@ -1115,12 +1115,13 @@ ssize_t tpm_read(struct file *file, char __user *buf,
 	ret_size = atomic_read(&chip->data_pending);
 	atomic_set(&chip->data_pending, 0);
 	if (ret_size > 0) {	/* relay data */
+		ssize_t orig_ret_size = ret_size;
 		if (size < ret_size)
 			ret_size = size;
 
 		mutex_lock(&chip->buffer_mutex);
 		rc = copy_to_user(buf, chip->data_buffer, ret_size);
-		memset(chip->data_buffer, 0, ret_size);
+		memset(chip->data_buffer, 0, orig_ret_size);
 		if (rc)
 			ret_size = -EFAULT;
 
-- 
1.7.0.4


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

* [PATCH 2/3] TPM: Close data_pending and data_buffer races
       [not found] <1323196162-2717-1-git-send-email-tim.gardner@canonical.com>
  2011-12-06 18:29 ` [PATCH 1/3] TPM: Zero buffer whole after copying to userspace Tim Gardner
@ 2011-12-06 18:29 ` Tim Gardner
  2011-12-20 16:38   ` Rajiv Andrade
  1 sibling, 1 reply; 4+ messages in thread
From: Tim Gardner @ 2011-12-06 18:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: Seth Forshee, Tim Gardner, Debora Velarde, Rajiv Andrade,
	Marcel Selhorst, tpmdd-devel, stable

There is a race betwen tpm_read() and tpm_write where both chip->data_pending
and chip->data_buffer can be changed by tpm_write() when tpm_read()
clears chip->data_pending, but before tpm_read() grabs the mutex.

Protect changes to chip->data_pending and chip->data_buffer by expanding
the scope of chip->buffer_mutex.

Reported-by: Seth Forshee <seth.forshee@canonical.com>
Cc: Debora Velarde <debora@linux.vnet.ibm.com>
Cc: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
Cc: Marcel Selhorst <m.selhorst@sirrix.com>
Cc: tpmdd-devel@lists.sourceforge.net
Cc: stable@vger.kernel.org
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 drivers/char/tpm/tpm.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
index b366b34..70bf9e5 100644
--- a/drivers/char/tpm/tpm.c
+++ b/drivers/char/tpm/tpm.c
@@ -1074,12 +1074,15 @@ ssize_t tpm_write(struct file *file, const char __user *buf,
 	struct tpm_chip *chip = file->private_data;
 	size_t in_size = size, out_size;
 
+	mutex_lock(&chip->buffer_mutex);
+
 	/* cannot perform a write until the read has cleared
 	   either via tpm_read or a user_read_timer timeout */
-	while (atomic_read(&chip->data_pending) != 0)
+	while (atomic_read(&chip->data_pending) != 0) {
+		mutex_unlock(&chip->buffer_mutex);
 		msleep(TPM_TIMEOUT);
-
-	mutex_lock(&chip->buffer_mutex);
+		mutex_lock(&chip->buffer_mutex);
+	}
 
 	if (in_size > TPM_BUFSIZE)
 		in_size = TPM_BUFSIZE;
@@ -1112,22 +1115,20 @@ ssize_t tpm_read(struct file *file, char __user *buf,
 
 	del_singleshot_timer_sync(&chip->user_read_timer);
 	flush_work_sync(&chip->work);
-	ret_size = atomic_read(&chip->data_pending);
-	atomic_set(&chip->data_pending, 0);
+	mutex_lock(&chip->buffer_mutex);
+	ret_size = atomic_xchg(&chip->data_pending, 0);
 	if (ret_size > 0) {	/* relay data */
 		ssize_t orig_ret_size = ret_size;
 		if (size < ret_size)
 			ret_size = size;
 
-		mutex_lock(&chip->buffer_mutex);
 		rc = copy_to_user(buf, chip->data_buffer, ret_size);
 		memset(chip->data_buffer, 0, orig_ret_size);
 		if (rc)
 			ret_size = -EFAULT;
-
-		mutex_unlock(&chip->buffer_mutex);
 	}
 
+	mutex_unlock(&chip->buffer_mutex);
 	return ret_size;
 }
 EXPORT_SYMBOL_GPL(tpm_read);
-- 
1.7.0.4


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

* Re: [PATCH 2/3] TPM: Close data_pending and data_buffer races
  2011-12-06 18:29 ` [PATCH 2/3] TPM: Close data_pending and data_buffer races Tim Gardner
@ 2011-12-20 16:38   ` Rajiv Andrade
  0 siblings, 0 replies; 4+ messages in thread
From: Rajiv Andrade @ 2011-12-20 16:38 UTC (permalink / raw)
  To: Tim Gardner
  Cc: linux-kernel, Seth Forshee, Debora Velarde, Marcel Selhorst,
	tpmdd-devel, stable

On 06/12/11 16:29, Tim Gardner wrote:
> There is a race betwen tpm_read() and tpm_write where both chip->data_pending
> and chip->data_buffer can be changed by tpm_write() when tpm_read()
> clears chip->data_pending, but before tpm_read() grabs the mutex.
>
> Protect changes to chip->data_pending and chip->data_buffer by expanding
> the scope of chip->buffer_mutex.
>
> Reported-by: Seth Forshee<seth.forshee@canonical.com>
> Cc: Debora Velarde<debora@linux.vnet.ibm.com>
> Cc: Rajiv Andrade<srajiv@linux.vnet.ibm.com>
> Cc: Marcel Selhorst<m.selhorst@sirrix.com>
> Cc: tpmdd-devel@lists.sourceforge.net
> Cc: stable@vger.kernel.org
> Signed-off-by: Tim Gardner<tim.gardner@canonical.com>
> ---
>   drivers/char/tpm/tpm.c |   17 +++++++++--------
>   1 files changed, 9 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
> index b366b34..70bf9e5 100644
> --- a/drivers/char/tpm/tpm.c
> +++ b/drivers/char/tpm/tpm.c
> @@ -1074,12 +1074,15 @@ ssize_t tpm_write(struct file *file, const char __user *buf,
>   	struct tpm_chip *chip = file->private_data;
>   	size_t in_size = size, out_size;
>
> +	mutex_lock(&chip->buffer_mutex);
> +
>   	/* cannot perform a write until the read has cleared
>   	   either via tpm_read or a user_read_timer timeout */
> -	while (atomic_read(&chip->data_pending) != 0)
> +	while (atomic_read(&chip->data_pending) != 0) {
> +		mutex_unlock(&chip->buffer_mutex);
>   		msleep(TPM_TIMEOUT);
> -
> -	mutex_lock(&chip->buffer_mutex);
> +		mutex_lock(&chip->buffer_mutex);
> +	}
>
>   	if (in_size>  TPM_BUFSIZE)
>   		in_size = TPM_BUFSIZE;
> @@ -1112,22 +1115,20 @@ ssize_t tpm_read(struct file *file, char __user *buf,
>
>   	del_singleshot_timer_sync(&chip->user_read_timer);
>   	flush_work_sync(&chip->work);
> -	ret_size = atomic_read(&chip->data_pending);
> -	atomic_set(&chip->data_pending, 0);
> +	mutex_lock(&chip->buffer_mutex);
> +	ret_size = atomic_xchg(&chip->data_pending, 0);
>   	if (ret_size>  0) {	/* relay data */
>   		ssize_t orig_ret_size = ret_size;
>   		if (size<  ret_size)
>   			ret_size = size;
>
> -		mutex_lock(&chip->buffer_mutex);
>   		rc = copy_to_user(buf, chip->data_buffer, ret_size);
>   		memset(chip->data_buffer, 0, orig_ret_size);
>   		if (rc)
>   			ret_size = -EFAULT;

What about just moving atomic_set(&chip->data_pending, 0); to here?
If I'm not missing anything, this would be cleaner.

Rajiv
> -
> -		mutex_unlock(&chip->buffer_mutex);
>   	}
>
> +	mutex_unlock(&chip->buffer_mutex);
>   	return ret_size;
>   }
>   EXPORT_SYMBOL_GPL(tpm_read);


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

* Re: [PATCH 1/3] TPM: Zero buffer whole after copying to userspace
  2011-12-06 18:29 ` [PATCH 1/3] TPM: Zero buffer whole after copying to userspace Tim Gardner
@ 2012-02-03 17:39   ` Rajiv Andrade
  0 siblings, 0 replies; 4+ messages in thread
From: Rajiv Andrade @ 2012-02-03 17:39 UTC (permalink / raw)
  To: Tim Gardner
  Cc: linux-kernel, Seth Forshee, Debora Velarde, Marcel Selhorst,
	tpmdd-devel, stable

On Tue, 06 Dec 2011, Tim Gardner wrote:

> Commit 3321c07ae5068568cd61ac9f4ba749006a7185c9 correctly clears the TPM
> buffer if the user specified read length is >= the TPM buffer length. However,
> if the user specified read length is < the TPM buffer length, then part of the
> TPM buffer is left uncleared.
> 
> Reported-by: Seth Forshee <seth.forshee@canonical.com>
> Cc: Debora Velarde <debora@linux.vnet.ibm.com>
> Cc: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
> Cc: Marcel Selhorst <m.selhorst@sirrix.com>
> Cc: tpmdd-devel@lists.sourceforge.net
> Cc: stable@vger.kernel.org
> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>

Applied to github.com:srajiv/tpm.git next

Thanks,
Rajiv


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

end of thread, other threads:[~2012-02-03 17:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1323196162-2717-1-git-send-email-tim.gardner@canonical.com>
2011-12-06 18:29 ` [PATCH 1/3] TPM: Zero buffer whole after copying to userspace Tim Gardner
2012-02-03 17:39   ` Rajiv Andrade
2011-12-06 18:29 ` [PATCH 2/3] TPM: Close data_pending and data_buffer races Tim Gardner
2011-12-20 16:38   ` Rajiv Andrade

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).