linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH  v2 1/2] selftests/tpm2: Open tpm dev in unbuffered mode
@ 2019-02-12 23:42 Tadeusz Struk
  2019-02-12 23:42 ` [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads Tadeusz Struk
  0 siblings, 1 reply; 5+ messages in thread
From: Tadeusz Struk @ 2019-02-12 23:42 UTC (permalink / raw)
  To: jarkko.sakkinen
  Cc: linux-kselftest, shuah, linux-kernel, linux-integrity,
	linux-security-module, PeterHuewe, jgg

In order to have control over how many bytes are read or written
the device needs to be opened in unbuffered mode.

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---

v2:
- Changed subject tags to selftests/tpm2:
---
 tools/testing/selftests/tpm2/tpm2.py |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
index 40ea95ce2ead..c2b9f2b1a0ac 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -357,9 +357,9 @@ class Client:
         self.flags = flags
 
         if (self.flags & Client.FLAG_SPACE) == 0:
-            self.tpm = open('/dev/tpm0', 'r+b')
+            self.tpm = open('/dev/tpm0', 'r+b', buffering=0)
         else:
-            self.tpm = open('/dev/tpmrm0', 'r+b')
+            self.tpm = open('/dev/tpmrm0', 'r+b', buffering=0)
 
     def close(self):
         self.tpm.close()


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

* [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads
  2019-02-12 23:42 [PATCH v2 1/2] selftests/tpm2: Open tpm dev in unbuffered mode Tadeusz Struk
@ 2019-02-12 23:42 ` Tadeusz Struk
  2019-02-13 15:13   ` Jarkko Sakkinen
  0 siblings, 1 reply; 5+ messages in thread
From: Tadeusz Struk @ 2019-02-12 23:42 UTC (permalink / raw)
  To: jarkko.sakkinen
  Cc: linux-kselftest, shuah, linux-kernel, linux-integrity,
	linux-security-module, PeterHuewe, jgg

Three new tests added:
1. Send get random cmd, read header in 1st read, read the rest in second
   read - expect success
2. Send get random cmd, read only part of the response, send another
   get random command, read the response - expect success
3. Send get random cmd followed by another get random cmd, without
   reading the first response - expect the second cmd to fail with -EBUSY

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---

v2:
- Removed extra logging
- Changed subject tag to selftest/tpm2:
---
 tools/testing/selftests/tpm2/tpm2.py       |    1 
 tools/testing/selftests/tpm2/tpm2_tests.py |   66 ++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/tools/testing/selftests/tpm2/tpm2.py b/tools/testing/selftests/tpm2/tpm2.py
index c2b9f2b1a0ac..828c18584624 100644
--- a/tools/testing/selftests/tpm2/tpm2.py
+++ b/tools/testing/selftests/tpm2/tpm2.py
@@ -22,6 +22,7 @@ TPM2_CC_UNSEAL = 0x015E
 TPM2_CC_FLUSH_CONTEXT = 0x0165
 TPM2_CC_START_AUTH_SESSION = 0x0176
 TPM2_CC_GET_CAPABILITY	= 0x017A
+TPM2_CC_GET_RANDOM = 0x017B
 TPM2_CC_PCR_READ = 0x017E
 TPM2_CC_POLICY_PCR = 0x017F
 TPM2_CC_PCR_EXTEND = 0x0182
diff --git a/tools/testing/selftests/tpm2/tpm2_tests.py b/tools/testing/selftests/tpm2/tpm2_tests.py
index 3bb066fea4a0..e82d84043c3f 100644
--- a/tools/testing/selftests/tpm2/tpm2_tests.py
+++ b/tools/testing/selftests/tpm2/tpm2_tests.py
@@ -158,6 +158,72 @@ class SmokeTest(unittest.TestCase):
             pass
         self.assertEqual(rejected, True)
 
+    def test_read_partial_resp(self):
+        """Reads random in two subsequent reads"""
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+            hdr = self.client.tpm.read(10)
+            sz = struct.unpack('>I', hdr[2:6])[0]
+            rsp = self.client.tpm.read()
+        except:
+            pass
+        self.assertEqual(sz, 10 + 2 + 32)
+        self.assertEqual(len(rsp), 2 + 32)
+
+    def test_read_partial_overwrite(self):
+        """Reads only part of the response and issue a new cmd"""
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+            # Read part of the respone
+            rsp1 = self.client.tpm.read(15)
+
+            # Send a new cmd
+            self.client.tpm.write(cmd)
+
+            # Read the whole respone
+            rsp2 = self.client.tpm.read()
+        except:
+            pass
+        self.assertEqual(len(rsp1), 15)
+        self.assertEqual(len(rsp2), 10 + 2 + 32)
+
+    def test_send_two_cmds(self):
+        """Send two cmds without reading a response"""
+        rejected = False
+        try:
+            fmt = '>HIIH'
+            cmd = struct.pack(fmt,
+                              tpm2.TPM2_ST_NO_SESSIONS,
+                              struct.calcsize(fmt),
+                              tpm2.TPM2_CC_GET_RANDOM,
+                              0x20)
+            self.client.tpm.write(cmd)
+
+            # expect the second one to raise -EBUSY error
+            self.client.tpm.write(cmd)
+            rsp = self.client.tpm.read()
+
+        except IOError, e:
+            # read the response
+            rsp = self.client.tpm.read()
+            rejected = True
+            pass
+        except:
+            pass
+        self.assertEqual(rejected, True)
+
 class SpaceTest(unittest.TestCase):
     def setUp(self):
         logging.basicConfig(filename='SpaceTest.log', level=logging.DEBUG)


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

* Re: [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads
  2019-02-12 23:42 ` [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads Tadeusz Struk
@ 2019-02-13 15:13   ` Jarkko Sakkinen
  2019-02-13 16:27     ` Tadeusz Struk
  0 siblings, 1 reply; 5+ messages in thread
From: Jarkko Sakkinen @ 2019-02-13 15:13 UTC (permalink / raw)
  To: Tadeusz Struk
  Cc: linux-kselftest, shuah, linux-kernel, linux-integrity,
	linux-security-module, PeterHuewe, jgg

On Tue, 2019-02-12 at 15:42 -0800, Tadeusz Struk wrote:
> Three new tests added:
> 1. Send get random cmd, read header in 1st read, read the rest in second
>    read - expect success
> 2. Send get random cmd, read only part of the response, send another
>    get random command, read the response - expect success
> 3. Send get random cmd followed by another get random cmd, without
>    reading the first response - expect the second cmd to fail with -EBUSY
> 
> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>

Getting still some garbage in the output:

$ sudo ./test_smoke.sh 
[sudo] password for jsakkine: 
test_read_partial_overwrite (tpm2_tests.SmokeTest)
Reads only part of the response and issue a new cmd ... ok
test_read_partial_resp (tpm2_tests.SmokeTest)
Reads random in two subsequent reads ... ok
test_seal_with_auth (tpm2_tests.SmokeTest) ... ok
test_seal_with_policy (tpm2_tests.SmokeTest) ... ok
test_seal_with_too_long_auth (tpm2_tests.SmokeTest) ... ok
test_send_two_cmds (tpm2_tests.SmokeTest)
Send two cmds without reading a response ... ok
test_too_short_cmd (tpm2_tests.SmokeTest) ... ok
test_unseal_with_wrong_auth (tpm2_tests.SmokeTest) ... ok
test_unseal_with_wrong_policy (tpm2_tests.SmokeTest) ... ok

----------------------------------------------------------------------
Ran 9 tests in 47.374s

OK

Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

Can give reviewed-by after those are cleaned up.

/Jarkko


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

* Re: [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads
  2019-02-13 15:13   ` Jarkko Sakkinen
@ 2019-02-13 16:27     ` Tadeusz Struk
  2019-02-13 16:33       ` Jarkko Sakkinen
  0 siblings, 1 reply; 5+ messages in thread
From: Tadeusz Struk @ 2019-02-13 16:27 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: linux-kselftest, shuah, linux-kernel, linux-integrity,
	linux-security-module, PeterHuewe, jgg

On 2/13/19 7:13 AM, Jarkko Sakkinen wrote:
> On Tue, 2019-02-12 at 15:42 -0800, Tadeusz Struk wrote:
>> Three new tests added:
>> 1. Send get random cmd, read header in 1st read, read the rest in second
>>    read - expect success
>> 2. Send get random cmd, read only part of the response, send another
>>    get random command, read the response - expect success
>> 3. Send get random cmd followed by another get random cmd, without
>>    reading the first response - expect the second cmd to fail with -EBUSY
>>
>> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
> 
> Getting still some garbage in the output:
> 
> $ sudo ./test_smoke.sh 
> [sudo] password for jsakkine: 
> test_read_partial_overwrite (tpm2_tests.SmokeTest)
> Reads only part of the response and issue a new cmd ... ok
> test_read_partial_resp (tpm2_tests.SmokeTest)
> Reads random in two subsequent reads ... ok
> test_seal_with_auth (tpm2_tests.SmokeTest) ... ok
> test_seal_with_policy (tpm2_tests.SmokeTest) ... ok
> test_seal_with_too_long_auth (tpm2_tests.SmokeTest) ... ok
> test_send_two_cmds (tpm2_tests.SmokeTest)
> Send two cmds without reading a response ... ok
> test_too_short_cmd (tpm2_tests.SmokeTest) ... ok
> test_unseal_with_wrong_auth (tpm2_tests.SmokeTest) ... ok
> test_unseal_with_wrong_policy (tpm2_tests.SmokeTest) ... ok

Looks like python unittest prints out the docstrings:

+    def test_read_partial_resp(self):
+        """Reads random in two subsequent reads"""

I can remove them if it makes more difficult to process the output.
Thanks,
-- 
Tadeusz

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

* Re: [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads
  2019-02-13 16:27     ` Tadeusz Struk
@ 2019-02-13 16:33       ` Jarkko Sakkinen
  0 siblings, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2019-02-13 16:33 UTC (permalink / raw)
  To: Tadeusz Struk
  Cc: linux-kselftest, shuah, linux-kernel, linux-integrity,
	linux-security-module, PeterHuewe, jgg

On Wed, Feb 13, 2019 at 08:27:44AM -0800, Tadeusz Struk wrote:
> On 2/13/19 7:13 AM, Jarkko Sakkinen wrote:
> > On Tue, 2019-02-12 at 15:42 -0800, Tadeusz Struk wrote:
> >> Three new tests added:
> >> 1. Send get random cmd, read header in 1st read, read the rest in second
> >>    read - expect success
> >> 2. Send get random cmd, read only part of the response, send another
> >>    get random command, read the response - expect success
> >> 3. Send get random cmd followed by another get random cmd, without
> >>    reading the first response - expect the second cmd to fail with -EBUSY
> >>
> >> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
> > 
> > Getting still some garbage in the output:
> > 
> > $ sudo ./test_smoke.sh 
> > [sudo] password for jsakkine: 
> > test_read_partial_overwrite (tpm2_tests.SmokeTest)
> > Reads only part of the response and issue a new cmd ... ok
> > test_read_partial_resp (tpm2_tests.SmokeTest)
> > Reads random in two subsequent reads ... ok
> > test_seal_with_auth (tpm2_tests.SmokeTest) ... ok
> > test_seal_with_policy (tpm2_tests.SmokeTest) ... ok
> > test_seal_with_too_long_auth (tpm2_tests.SmokeTest) ... ok
> > test_send_two_cmds (tpm2_tests.SmokeTest)
> > Send two cmds without reading a response ... ok
> > test_too_short_cmd (tpm2_tests.SmokeTest) ... ok
> > test_unseal_with_wrong_auth (tpm2_tests.SmokeTest) ... ok
> > test_unseal_with_wrong_policy (tpm2_tests.SmokeTest) ... ok
> 
> Looks like python unittest prints out the docstrings:
> 
> +    def test_read_partial_resp(self):
> +        """Reads random in two subsequent reads"""
> 
> I can remove them if it makes more difficult to process the output.

Yeah, I mean it also outputs the function name, which is good enough
IMHO.

/Jarkko

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

end of thread, other threads:[~2019-02-13 16:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-12 23:42 [PATCH v2 1/2] selftests/tpm2: Open tpm dev in unbuffered mode Tadeusz Struk
2019-02-12 23:42 ` [PATCH v2 2/2] selftests/tpm2: Extend tests to cover partial reads Tadeusz Struk
2019-02-13 15:13   ` Jarkko Sakkinen
2019-02-13 16:27     ` Tadeusz Struk
2019-02-13 16:33       ` Jarkko Sakkinen

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