From: Tim Gardner <tim.gardner@canonical.com>
To: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
Cc: linux-kernel@vger.kernel.org,
Seth Forshee <seth.forshee@canonical.com>,
Debora Velarde <debora@linux.vnet.ibm.com>,
Marcel Selhorst <m.selhorst@sirrix.com>,
tpmdd-devel@lists.sourceforge.net
Subject: Re: [PATCH 2/3] TPM: Close data_pending and data_buffer races
Date: Thu, 22 Dec 2011 11:44:10 -0700 [thread overview]
Message-ID: <4EF37A7A.2060504@canonical.com> (raw)
In-Reply-To: <4EF36C1F.6040409@linux.vnet.ibm.com>
On 12/22/2011 10:42 AM, Rajiv Andrade wrote:
> On 20-12-2011 17:39, Tim Gardner wrote:
>> On 12/20/2011 09:38 AM, Rajiv Andrade wrote:
>>> 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
>>
>> I'm not sure I agree. Moving just that statement doesn't close the
>> race. Perhaps you could send me your version of this patch so that its
>> clear what you are suggesting.
>>
>> rtg
> diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
> index 6a8771f..6a37212b 100644
> --- a/drivers/char/tpm/tpm.c
> +++ b/drivers/char/tpm/tpm.c
> @@ -1210,7 +1210,6 @@ 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);
> if (ret_size> 0) { /* relay data */
> if (size< ret_size)
> ret_size = size;
> @@ -1223,6 +1222,7 @@ ssize_t tpm_read(struct file *file, char __user *buf,
> mutex_unlock(&chip->buffer_mutex);
> } + atomic_set(&chip->data_pending, 0);
> return ret_size;
> }
>
> If we reset chip->data_pending after the buffer was copied to userspace,
> it's guaranteed that tpm_write() won't touch such buffer before tpm_read()
> handles it, given it polls chip->data_pending first.
>
NAK - this patch makes it worse (if I'm reading your email client
garbled patch correctly). Now it races with tpm_write() as well as
timeout_work(). You cannot futz with chip->data_pending outside of the
exclusion zones. Consider what will happen if a user process just loops
doing reads. chip->data_pending gets cleared every time tpm_read() is
called, regardless of what tpm_write() or timeout_work() are doing at
the time.
tpm_read() / tpm_write() is a simple producer consumer model. Just use
mutexes in an uncomplicated way. There is no need for data_pending to be
atomic_t.
rtg
--
Tim Gardner tim.gardner@canonical.com
next prev parent reply other threads:[~2011-12-22 18:44 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-06 18:29 [PATCH 0/3] TPM: CVE patch, close a race, atomic cleanup Tim Gardner
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
2011-12-20 19:39 ` Tim Gardner
2011-12-22 17:42 ` Rajiv Andrade
2011-12-22 18:44 ` Tim Gardner [this message]
2011-12-22 20:02 ` Rajiv Andrade
2011-12-23 14:25 ` Tim Gardner
2011-12-27 20:02 ` [tpmdd-devel] " Mimi Zohar
2012-01-11 19:43 ` Rajiv Andrade
2012-07-25 17:36 ` Kent Yoder
2011-12-06 18:29 ` [PATCH 3/3] TPM: data_pending is no longer atomic Tim Gardner
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=4EF37A7A.2060504@canonical.com \
--to=tim.gardner@canonical.com \
--cc=debora@linux.vnet.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=m.selhorst@sirrix.com \
--cc=seth.forshee@canonical.com \
--cc=srajiv@linux.vnet.ibm.com \
--cc=tpmdd-devel@lists.sourceforge.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
Powered by JetHome