* [PATCH] tpm: selftest: add test covering async mode
2019-12-09 21:34 ` Tadeusz Struk
@ 2019-12-10 19:04 ` Tadeusz Struk
2019-12-11 17:42 ` Jarkko Sakkinen
2019-12-10 21:17 ` [PATCH] tpm: fix WARNING: lock held when returning to user space Tadeusz Struk
` (2 subsequent siblings)
3 siblings, 1 reply; 16+ messages in thread
From: Tadeusz Struk @ 2019-12-10 19:04 UTC (permalink / raw)
To: jarkko.sakkinen
Cc: tadeusz.struk, peterz, linux-kernel, jgg, mingo, jeffrin,
linux-integrity, will, peterhuewe
Add a test that sends a tpm cmd in an asyn mode.
Currently there is a gap in test coverage with regards
to this functionality.
Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---
tools/testing/selftests/tpm2/test_smoke.sh | 1 +
tools/testing/selftests/tpm2/tpm2.py | 19 +++++++++++++++++--
tools/testing/selftests/tpm2/tpm2_tests.py | 13 +++++++++++++
3 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/tpm2/test_smoke.sh b/tools/testing/selftests/tpm2/test_smoke.sh
index 80521d46220c..cb54ab637ea6 100755
--- a/tools/testing/selftests/tpm2/test_smoke.sh
+++ b/tools/testing/selftests/tpm2/test_smoke.sh
@@ -2,3 +2,4 @@
# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
python -m unittest -v tpm2_tests.SmokeTest
+python -m unittest -v tpm2_tests.AsyncTest
diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
index 828c18584624..d0fcb66a88a6 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -6,8 +6,8 @@ import socket
import struct
import sys
import unittest
-from fcntl import ioctl
-
+import fcntl
+import select
TPM2_ST_NO_SESSIONS = 0x8001
TPM2_ST_SESSIONS = 0x8002
@@ -352,6 +352,7 @@ def hex_dump(d):
class Client:
FLAG_DEBUG = 0x01
FLAG_SPACE = 0x02
+ FLAG_NONBLOCK = 0x04
TPM_IOC_NEW_SPACE = 0xa200
def __init__(self, flags = 0):
@@ -362,13 +363,27 @@ class Client:
else:
self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
+ if (self.flags & Client.FLAG_NONBLOCK):
+ flags = fcntl.fcntl(self.tpm, fcntl.F_GETFL)
+ flags |= os.O_NONBLOCK
+ fcntl.fcntl(self.tpm, fcntl.F_SETFL, flags)
+ self.tpm_poll = select.poll()
+
def close(self):
self.tpm.close()
def send_cmd(self, cmd):
self.tpm.write(cmd)
+
+ if (self.flags & Client.FLAG_NONBLOCK):
+ self.tpm_poll.register(self.tpm, select.POLLIN)
+ self.tpm_poll.poll(10000)
+
rsp = self.tpm.read()
+ if (self.flags & Client.FLAG_NONBLOCK):
+ self.tpm_poll.unregister(self.tpm)
+
if (self.flags & Client.FLAG_DEBUG) != 0:
sys.stderr.write('cmd' + os.linesep)
sys.stderr.write(hex_dump(cmd) + os.linesep)
diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
index d4973be53493..728be7c69b76 100644
--- a/tools/testing/selftests/tpm2/tpm2_tests.py
+++ b/tools/testing/selftests/tpm2/tpm2_tests.py
@@ -288,3 +288,16 @@ class SpaceTest(unittest.TestCase):
self.assertEqual(rc, tpm2.TPM2_RC_COMMAND_CODE |
tpm2.TSS2_RESMGR_TPM_RC_LAYER)
+
+class AsyncTest(unittest.TestCase):
+ def setUp(self):
+ logging.basicConfig(filename='AsyncTest.log', level=logging.DEBUG)
+
+ def test_async(self):
+ log = logging.getLogger(__name__)
+ log.debug(sys._getframe().f_code.co_name)
+
+ async_client = tpm2.Client(tpm2.Client.FLAG_NONBLOCK)
+ log.debug("Calling get_cap in a NON_BLOCKING mode")
+ async_client.get_cap(tpm2.TPM2_CAP_HANDLES, tpm2.HR_LOADED_SESSION)
+ async_client.close()
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH] tpm: selftest: add test covering async mode
2019-12-10 19:04 ` [PATCH] tpm: selftest: add test covering async mode Tadeusz Struk
@ 2019-12-11 17:42 ` Jarkko Sakkinen
0 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2019-12-11 17:42 UTC (permalink / raw)
To: Tadeusz Struk
Cc: peterz, linux-kernel, jgg, mingo, jeffrin, linux-integrity, will,
peterhuewe
On Tue, Dec 10, 2019 at 11:04:59AM -0800, Tadeusz Struk wrote:
> Add a test that sends a tpm cmd in an asyn mode.
> Currently there is a gap in test coverage with regards
> to this functionality.
>
> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
Please include this with a bug fix to same patch set.
/Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH] tpm: fix WARNING: lock held when returning to user space
2019-12-09 21:34 ` Tadeusz Struk
2019-12-10 19:04 ` [PATCH] tpm: selftest: add test covering async mode Tadeusz Struk
@ 2019-12-10 21:17 ` Tadeusz Struk
2019-12-11 17:47 ` Jarkko Sakkinen
2019-12-11 17:52 ` Jarkko Sakkinen
2019-12-11 15:17 ` [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G ) Jeffrin Jose
2019-12-11 17:45 ` Jarkko Sakkinen
3 siblings, 2 replies; 16+ messages in thread
From: Tadeusz Struk @ 2019-12-10 21:17 UTC (permalink / raw)
To: jarkko.sakkinen
Cc: tadeusz.struk, peterz, linux-kernel, stable, jgg, mingo, jeffrin,
linux-integrity, will, peterhuewe
When an application sends TPM commands in NONBLOCKING mode
the driver holds chip->tpm_mutex returning from write(),
which triggers WARNING: lock held when returning to user space!
To silence this warning the driver needs to release the mutex
and acquire it again in tpm_dev_async_work() before sending
the command.
Cc: stable@vger.kernel.org
Fixes: 9e1b74a63f776 (tpm: add support for nonblocking operation)
Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---
drivers/char/tpm/tpm-dev-common.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c
index 2ec47a69a2a6..b23b0b999232 100644
--- a/drivers/char/tpm/tpm-dev-common.c
+++ b/drivers/char/tpm/tpm-dev-common.c
@@ -61,6 +61,12 @@ static void tpm_dev_async_work(struct work_struct *work)
mutex_lock(&priv->buffer_mutex);
priv->command_enqueued = false;
+ ret = tpm_try_get_ops(priv->chip);
+ if (ret) {
+ priv->response_length = ret;
+ goto out;
+ }
+
ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
sizeof(priv->data_buffer));
tpm_put_ops(priv->chip);
@@ -68,6 +74,7 @@ static void tpm_dev_async_work(struct work_struct *work)
priv->response_length = ret;
mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
}
+out:
mutex_unlock(&priv->buffer_mutex);
wake_up_interruptible(&priv->async_wait);
}
@@ -204,6 +211,7 @@ ssize_t tpm_common_write(struct file *file, const char __user *buf,
if (file->f_flags & O_NONBLOCK) {
priv->command_enqueued = true;
queue_work(tpm_dev_wq, &priv->async_work);
+ tpm_put_ops(priv->chip);
mutex_unlock(&priv->buffer_mutex);
return size;
}
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH] tpm: fix WARNING: lock held when returning to user space
2019-12-10 21:17 ` [PATCH] tpm: fix WARNING: lock held when returning to user space Tadeusz Struk
@ 2019-12-11 17:47 ` Jarkko Sakkinen
2019-12-11 17:52 ` Jarkko Sakkinen
1 sibling, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2019-12-11 17:47 UTC (permalink / raw)
To: Tadeusz Struk
Cc: peterz, linux-kernel, stable, jgg, mingo, jeffrin,
linux-integrity, will, peterhuewe
On Tue, Dec 10, 2019 at 01:17:51PM -0800, Tadeusz Struk wrote:
> When an application sends TPM commands in NONBLOCKING mode
> the driver holds chip->tpm_mutex returning from write(),
> which triggers WARNING: lock held when returning to user space!
> To silence this warning the driver needs to release the mutex
> and acquire it again in tpm_dev_async_work() before sending
> the command.
>
> Cc: stable@vger.kernel.org
> Fixes: 9e1b74a63f776 (tpm: add support for nonblocking operation)
> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
Bundle the patch and test into same patch set (no need for
cover letter) in the case we need to iterate (as they must
be in sync anyway).
/Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] tpm: fix WARNING: lock held when returning to user space
2019-12-10 21:17 ` [PATCH] tpm: fix WARNING: lock held when returning to user space Tadeusz Struk
2019-12-11 17:47 ` Jarkko Sakkinen
@ 2019-12-11 17:52 ` Jarkko Sakkinen
2019-12-11 18:00 ` Jarkko Sakkinen
1 sibling, 1 reply; 16+ messages in thread
From: Jarkko Sakkinen @ 2019-12-11 17:52 UTC (permalink / raw)
To: Tadeusz Struk
Cc: peterz, linux-kernel, stable, jgg, mingo, jeffrin,
linux-integrity, will, peterhuewe
On Tue, Dec 10, 2019 at 01:17:51PM -0800, Tadeusz Struk wrote:
> When an application sends TPM commands in NONBLOCKING mode
> the driver holds chip->tpm_mutex returning from write(),
> which triggers WARNING: lock held when returning to user space!
> To silence this warning the driver needs to release the mutex
> and acquire it again in tpm_dev_async_work() before sending
> the command.
This commit message is nonsense ATM. The code leaves locks held,
which is unacceptable.
Silencing warnings is worst possible rationale for a code change
that I can think of starting from the fact that is straight out
wrong and malicious.
/Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] tpm: fix WARNING: lock held when returning to user space
2019-12-11 17:52 ` Jarkko Sakkinen
@ 2019-12-11 18:00 ` Jarkko Sakkinen
0 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2019-12-11 18:00 UTC (permalink / raw)
To: Tadeusz Struk
Cc: peterz, linux-kernel, stable, jgg, mingo, jeffrin,
linux-integrity, will, peterhuewe
On Wed, Dec 11, 2019 at 07:52:02PM +0200, Jarkko Sakkinen wrote:
> On Tue, Dec 10, 2019 at 01:17:51PM -0800, Tadeusz Struk wrote:
> > When an application sends TPM commands in NONBLOCKING mode
> > the driver holds chip->tpm_mutex returning from write(),
> > which triggers WARNING: lock held when returning to user space!
> > To silence this warning the driver needs to release the mutex
> > and acquire it again in tpm_dev_async_work() before sending
> > the command.
>
> This commit message is nonsense ATM. The code leaves locks held,
> which is unacceptable.
>
> Silencing warnings is worst possible rationale for a code change
> that I can think of starting from the fact that is straight out
> wrong and malicious.
The code change, however, seems to be doing the right thing.
Please bundle it with the test and fix the commit message
appropriately.
/Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G )
2019-12-09 21:34 ` Tadeusz Struk
2019-12-10 19:04 ` [PATCH] tpm: selftest: add test covering async mode Tadeusz Struk
2019-12-10 21:17 ` [PATCH] tpm: fix WARNING: lock held when returning to user space Tadeusz Struk
@ 2019-12-11 15:17 ` Jeffrin Jose
2019-12-11 18:36 ` Tadeusz Struk
2019-12-11 17:45 ` Jarkko Sakkinen
3 siblings, 1 reply; 16+ messages in thread
From: Jeffrin Jose @ 2019-12-11 15:17 UTC (permalink / raw)
To: Tadeusz Struk
Cc: Jarkko Sakkinen, Will Deacon, peterz, mingo, linux-kernel,
linux-integrity, peterhuewe, jgg, jeffrin
> diff --git a/drivers/char/tpm/tpm-dev-common.c
> b/drivers/char/tpm/tpm-dev-common.c
> index 2ec47a69a2a6..47f1c0c5c8de 100644
> --- a/drivers/char/tpm/tpm-dev-common.c
> +++ b/drivers/char/tpm/tpm-dev-common.c
> @@ -61,6 +61,12 @@ static void tpm_dev_async_work(struct work_struct *work)
>
> mutex_lock(&priv->buffer_mutex);
> priv->command_enqueued = false;
> + ret = tpm_try_get_ops(priv->chip);
> + if (ret) {
> + priv->response_length = ret;
> + goto out;
> + }
> +
> ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
> sizeof(priv->data_buffer));
> tpm_put_ops(priv->chip);
> @@ -68,6 +74,7 @@ static void tpm_dev_async_work(struct work_struct *work)
> priv->response_length = ret;
> mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
> }
> +out:
> mutex_unlock(&priv->buffer_mutex);
> wake_up_interruptible(&priv->async_wait);
> }
> @@ -205,6 +212,7 @@ ssize_t tpm_common_write(struct file *file, const
> char __user *buf,
> priv->command_enqueued = true;
> queue_work(tpm_dev_wq, &priv->async_work);
> mutex_unlock(&priv->buffer_mutex);
> + tpm_put_ops(priv->chip);
> return size;
> }
>
>
>
> --
> Tadeusz
above patch shows errors when i try to apply it.
--------------------x------------------------x------------------
error: git diff header lacks filename information when removing 1 leading pathname component (line 2)
when i did related to this "diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c"
i get another error
error: corrupt patch at line 27
----------------------x------------------------x-----------------
i use "git apply"
--
software engineer
rajagiri school of engineering and technology
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G )
2019-12-11 15:17 ` [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G ) Jeffrin Jose
@ 2019-12-11 18:36 ` Tadeusz Struk
2019-12-12 8:12 ` Jeffrin Jose
0 siblings, 1 reply; 16+ messages in thread
From: Tadeusz Struk @ 2019-12-11 18:36 UTC (permalink / raw)
To: Jeffrin Jose
Cc: Jarkko Sakkinen, Will Deacon, peterz, mingo, linux-kernel,
linux-integrity, peterhuewe, jgg
On 12/11/19 7:17 AM, Jeffrin Jose wrote:
> above patch shows errors when i try to apply it.
> --------------------x------------------------x------------------
> error: git diff header lacks filename information when removing 1 leading pathname component (line 2)
> when i did related to this "diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c"
> i get another error
> error: corrupt patch at line 27
> ----------------------x------------------------x-----------------
>
> i use "git apply"
Hi,
This was just a copy and paste that wasn't meant to be applied.
If you want to try it please use the patch that I sent later:
https://patchwork.kernel.org/patch/11283317/
Thanks,
--
Tadeusz
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G )
2019-12-11 18:36 ` Tadeusz Struk
@ 2019-12-12 8:12 ` Jeffrin Jose
0 siblings, 0 replies; 16+ messages in thread
From: Jeffrin Jose @ 2019-12-12 8:12 UTC (permalink / raw)
To: Tadeusz Struk
Cc: Jarkko Sakkinen, Will Deacon, peterz, mingo, linux-kernel,
linux-integrity, peterhuewe, jgg, jeffrin
On Wed, Dec 11, 2019 at 10:36:15AM -0800, Tadeusz Struk wrote:
> On 12/11/19 7:17 AM, Jeffrin Jose wrote:
> > above patch shows errors when i try to apply it.
> > --------------------x------------------------x------------------
> > error: git diff header lacks filename information when removing 1 leading pathname component (line 2)
> > when i did related to this "diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c"
> > i get another error
> > error: corrupt patch at line 27
> > ----------------------x------------------------x-----------------
> >
> > i use "git apply"
>
> Hi,
> This was just a copy and paste that wasn't meant to be applied.
> If you want to try it please use the patch that I sent later:
> https://patchwork.kernel.org/patch/11283317/
>
i applied the patch and problem seem to be fixed.
Reported-by: Jeffrin Jose T <jeffrin@rajagiritech.edu.in>
Tested-by: Jeffrin Jose T <jeffrin@rajagiritech.edu.in>
--
software engineer
rajagiri school of engineering and technology
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G )
2019-12-09 21:34 ` Tadeusz Struk
` (2 preceding siblings ...)
2019-12-11 15:17 ` [PROBLEM]: WARNING: lock held when returning to user space! (5.4.1 #16 Tainted: G ) Jeffrin Jose
@ 2019-12-11 17:45 ` Jarkko Sakkinen
3 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2019-12-11 17:45 UTC (permalink / raw)
To: Tadeusz Struk
Cc: Will Deacon, Jeffrin Jose, peterz, mingo, linux-kernel,
linux-integrity, peterhuewe, jgg
On Mon, Dec 09, 2019 at 01:34:29PM -0800, Tadeusz Struk wrote:
> I think that's expected for a non-blocking operation.
What do you mean by "expected"?
It is a locking bug. When you implemented the feature you failed
to free locks before going back to the user space and I failed to
notice this when I reviewed the code.
> To get rid of the warning it should be changed to something like this:
>
> diff --git a/drivers/char/tpm/tpm-dev-common.c
> b/drivers/char/tpm/tpm-dev-common.c
> index 2ec47a69a2a6..47f1c0c5c8de 100644
> --- a/drivers/char/tpm/tpm-dev-common.c
> +++ b/drivers/char/tpm/tpm-dev-common.c
> @@ -61,6 +61,12 @@ static void tpm_dev_async_work(struct work_struct *work)
>
> mutex_lock(&priv->buffer_mutex);
> priv->command_enqueued = false;
> + ret = tpm_try_get_ops(priv->chip);
> + if (ret) {
> + priv->response_length = ret;
> + goto out;
> + }
> +
> ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
> sizeof(priv->data_buffer));
> tpm_put_ops(priv->chip);
> @@ -68,6 +74,7 @@ static void tpm_dev_async_work(struct work_struct *work)
> priv->response_length = ret;
> mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
> }
> +out:
> mutex_unlock(&priv->buffer_mutex);
> wake_up_interruptible(&priv->async_wait);
> }
> @@ -205,6 +212,7 @@ ssize_t tpm_common_write(struct file *file, const
> char __user *buf,
> priv->command_enqueued = true;
> queue_work(tpm_dev_wq, &priv->async_work);
> mutex_unlock(&priv->buffer_mutex);
> + tpm_put_ops(priv->chip);
> return size;
> }
>
>
>
> --
> Tadeusz
The fix looks appropriate but needs to be formalized as a patch.
/Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread