Skip to contentSkip to conversation

tpm: Fix error handling in async work

PATCH Superseded Tadeusz Struk 3 participants 7 messages Jan 11, 2022 – Jan 16, 2022

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 tpm: Fix error handling in async work

When an invalid (non existing) handle is used in a TPM command, that uses the resource manager interface (/dev/tpmrm0) the resource manager tries to load it from its internal cache, but fails and the tpm_dev_transmit returns an -EINVAL error to the caller. The existing async handler doesn't handle these error cases currently and the condition in the poll handler never returns mask with EPOLLIN set.
The result is that the poll call blocks and the application gets stuck until the user_read_timer wakes it up after 120 sec.
Change the tpm_dev_async_work function to handle error conditions returned from tpm_dev_transmit they are also reflected in the poll mask and a correct error code could passed back to the caller.

Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Fixes 9e1b74a63f77 ("tpm: add support for nonblocking operation")

---
Changed in v2:
- Updated commit message with better problem description
- Fixed typeos.
Changed in v3:
- Added a comment to tpm_dev_async_work.
- Updated commit message.

---
 drivers/char/tpm/tpm-dev-common.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-dev-common.c +7 -1
index c08cbb306636..50df8f09ff79 100644
@@ -69,7 +69,13 @@ static void tpm_dev_async_work(struct work_struct *work)
 	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
 			       sizeof(priv->data_buffer));
 	tpm_put_ops(priv->chip);
-	if (ret > 0) {
+
+	/*
+	 * If ret is > 0 then tpm_dev_transmit returned the size of the
+	 * response. If ret is < 0 then tpm_dev_transmit failed and
+	 * returned a return code.
+	 */
+	if (ret != 0) {
 		priv->response_length = ret;
 		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
 	}
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 2/2 selftests: tpm: add async space test with noneexisting handle

Add a test for /dev/tpmrm0 in async mode that checks if the code handles invalid handles correctly.

---
Changes in v2:
- Updated commit message

---
 tools/testing/selftests/tpm2/tpm2_tests.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/testing/selftests/tpm2/tpm2_tests.py +16 -0
index 9d764306887b..b373b0936e40 100644
@@ -302,3 +302,19 @@ class AsyncTest(unittest.TestCase):
         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()
+
+    def test_flush_invlid_context(self):
+        log = logging.getLogger(__name__)
+        log.debug(sys._getframe().f_code.co_name)
+
+        async_client = tpm2.Client(tpm2.Client.FLAG_SPACE | tpm2.Client.FLAG_NONBLOCK)
+        log.debug("Calling flush_context passing in an invalid handle ")
+        handle = 0x80123456
+        rc = 0
+        try:
+            async_client.flush_context(handle)
+        except OSError as e:
+            rc = e.errno
+
+        self.assertEqual(rc, 22)
+        async_client.close()
Signature · 1 line
2.30.2
maintainer
#2 lore raw
In reply to Tadeusz Struk
Quoted 53 lines · Tadeusz Struk

When an invalid (non existing) handle is used in a TPM command, that uses the resource manager interface (/dev/tpmrm0) the resource manager tries to load it from its internal cache, but fails and the tpm_dev_transmit returns an -EINVAL error to the caller. The existing async handler doesn't handle these error cases currently and the condition in the poll handler never returns mask with EPOLLIN set.
The result is that the poll call blocks and the application gets stuck until the user_read_timer wakes it up after 120 sec.
Change the tpm_dev_async_work function to handle error conditions returned from tpm_dev_transmit they are also reflected in the poll mask and a correct error code could passed back to the caller.

Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Fixes 9e1b74a63f77 ("tpm: add support for nonblocking operation")

---
Changed in v2:
- Updated commit message with better problem description
- Fixed typeos.
Changed in v3:
- Added a comment to tpm_dev_async_work.
- Updated commit message.

---
 drivers/char/tpm/tpm-dev-common.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-dev-common.c +7 -1
index c08cbb306636..50df8f09ff79 100644
@@ -69,7 +69,13 @@ static void tpm_dev_async_work(struct work_struct *work)
 	ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
 			       sizeof(priv->data_buffer));
 	tpm_put_ops(priv->chip);
-	if (ret > 0) {
+
+	/*
+	 * If ret is > 0 then tpm_dev_transmit returned the size of the
+	 * response. If ret is < 0 then tpm_dev_transmit failed and
+	 * returned a return code.
+	 */
+	if (ret != 0) {
 		priv->response_length = ret;
 		mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
 	}
Signature · 1 line
2.30.2

These look good to me! Thank you. I'm in process of compiling a test kernel.

/Jarkko

#3 lore raw
Quoted 2 lines · Jarkko Sakkinen

These look good to me! Thank you. I'm in process of compiling a test kernel.

Thanks Jarkko,
You can run the new test before and after applying the change and see how it behaves. Also just noticed a mistake in the comment, sorry but it was quite late when I sent it.

+ /*
+ * If ret is > 0 then tpm_dev_transmit returned the size of the
+ * response. If ret is < 0 then tpm_dev_transmit failed and
+ * returned a return code.
+ */

In the above could you please replace:

s/returned a return code/returned an error code/

before applying the patch. I would appreciate that.

Signature · 2 lines
Thanks,
Tadeusz
maintainer Tested-by
#4 lore raw
Quoted 15 lines · Tadeusz Struk
Quoted 2 lines · Jarkko Sakkinen

These look good to me! Thank you. I'm in process of compiling a test kernel.

Thanks Jarkko,
You can run the new test before and after applying the change and see how it behaves. Also just noticed a mistake in the comment, sorry but it was quite late when I sent it.

drivers/char/tpm/tpm-dev-common.c +5 -0
+	/*
+	 * If ret is > 0 then tpm_dev_transmit returned the size of the
+	 * response. If ret is < 0 then tpm_dev_transmit failed and
+	 * returned a return code.
+	 */
Quoted 5 lines

In the above could you please replace:

s/returned a return code/returned an error code/

before applying the patch. I would appreciate that.

Please send new versions, there's also this:

def test_flush_invlid_context()

I'd figure "invlid" should be "invalid"

You can add, as these changes do not change the semantics of the patches:

It's always best if you author the final version, as then a clear reference on what was accepted exist at lore.kernel.org.

BR, Jarkko

maintainer
#5 lore raw
Quoted 28 lines · Jarkko Sakkinen
Quoted 15 lines · Tadeusz Struk
Quoted 2 lines · Jarkko Sakkinen

These look good to me! Thank you. I'm in process of compiling a test kernel.

Thanks Jarkko,
You can run the new test before and after applying the change and see how it behaves. Also just noticed a mistake in the comment, sorry but it was quite late when I sent it.

drivers/char/tpm/tpm-dev-common.c +5 -0
+	/*
+	 * If ret is > 0 then tpm_dev_transmit returned the size of the
+	 * response. If ret is < 0 then tpm_dev_transmit failed and
+	 * returned a return code.
+	 */
Quoted 5 lines

In the above could you please replace:

s/returned a return code/returned an error code/

before applying the patch. I would appreciate that.

Please send new versions, there's also this:

def test_flush_invlid_context()

I'd figure "invlid" should be "invalid"

Quoted 7 lines

You can add, as these changes do not change the semantics of the patches:

It's always best if you author the final version, as then a clear reference on what was accepted exist at lore.kernel.org.

Maybe it is good to mention that the test environment was libvirt hosted QEMU using swtpm, which I tried for the first time, instead of real hadware (libvirt has a nice property that it handles the startup/shutdown of swtpm). I managed to run all tests so I guess swtpm is working properly.

/Jarkko

#6 lore raw
Quoted 9 lines · Jarkko Sakkinen
Quoted 9 lines

Please send new versions, there's also this:

def test_flush_invlid_context()

I'd figure "invlid" should be "invalid"

You can add, as these changes do not change the semantics of the patches:

Quoted 8 lines
Quoted 4 lines

It's always best if you author the final version, as then a clear reference on what was accepted exist at lore.kernel.org.

Maybe it is good to mention that the test environment was libvirt hosted QEMU using swtpm, which I tried for the first time, instead of real hadware (libvirt has a nice property that it handles the startup/shutdown of swtpm). I managed to run all tests so I guess swtpm is working properly.

Yes, I have been using it all the time for testing since the support was added to qemu. New versions on their way.

Thanks,
Tadeusz