Skip to contentSkip to conversation

tests: Adjust path for swtpm state to use path under /var/tmp/

PATCH In review Stefan Berger 4 participants 5 messages Nov 6, 2024 – Nov 7, 2024 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests: Adjust path for swtpm state to use path under /var/tmp/

From: Stefan Berger <[email protected]>

To avoid AppArmor-related test failures when functional test are run from somewhere under /mnt, adjust the path to swtpm's state to use an AppArmor- supported path, such as /var/tmp, which is provided by the python function tempfile.TemporaryDirectory().

An update to swtpm's AppArmor profile is also being done to support /var/tmp.

---
 tests/functional/test_arm_aspeed.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/functional/test_arm_aspeed.py +3 -3
index 9761fc06a4..a574b1e521 100644
@@ -227,11 +227,11 @@ def test_arm_ast2600_evb_buildroot_tpm(self):
 
         image_path = self.ASSET_BR2_202302_AST2600_TPM_FLASH.fetch()
 
-        socket_dir = tempfile.TemporaryDirectory(prefix="qemu_")
-        socket = os.path.join(socket_dir.name, 'swtpm-socket')
+        tpmstate_dir = tempfile.TemporaryDirectory(prefix="qemu_")
+        socket = os.path.join(tpmstate_dir.name, 'swtpm-socket')
 
         subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
-                        '--tpmstate', f'dir={self.vm.temp_dir}',
+                        '--tpmstate', f'dir={tpmstate_dir.name}',
                         '--ctrl', f'type=unixio,path={socket}'])
 
         self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Stefan Berger

From: Stefan Berger <[email protected]>

To avoid AppArmor-related test failures when functional test are run from somewhere under /mnt, adjust the path to swtpm's state to use an AppArmor- supported path, such as /var/tmp, which is provided by the python function tempfile.TemporaryDirectory().

Quoted 5 lines

An update to swtpm's AppArmor profile is also being done to support /var/tmp.

Fixes f04cb2d00d5c ("tests/functional: Convert most Aspeed machine tests")

Thanks,

C.

Quoted 23 lines
---
  tests/functional/test_arm_aspeed.py | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
tests/functional/test_arm_aspeed.py +3 -3
index 9761fc06a4..a574b1e521 100644
@@ -227,11 +227,11 @@ def test_arm_ast2600_evb_buildroot_tpm(self):
 
          image_path = self.ASSET_BR2_202302_AST2600_TPM_FLASH.fetch()
 
-        socket_dir = tempfile.TemporaryDirectory(prefix="qemu_")
-        socket = os.path.join(socket_dir.name, 'swtpm-socket')
+        tpmstate_dir = tempfile.TemporaryDirectory(prefix="qemu_")
+        socket = os.path.join(tpmstate_dir.name, 'swtpm-socket')
 
          subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
-                        '--tpmstate', f'dir={self.vm.temp_dir}',
+                        '--tpmstate', f'dir={tpmstate_dir.name}',
                          '--ctrl', f'type=unixio,path={socket}'])
 
          self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
Tested-by on v1 1/1
#2 lore raw
In reply to Stefan Berger
Quoted 8 lines · Stefan Berger

From: Stefan Berger <[email protected]>

To avoid AppArmor-related test failures when functional test are run from somewhere under /mnt, adjust the path to swtpm's state to use an AppArmor- supported path, such as /var/tmp, which is provided by the python function tempfile.TemporaryDirectory().

Quoted 5 lines

An update to swtpm's AppArmor profile is also being done to support /var/tmp.

Thanks for writing this patch. I can confirm that the test now runs OK on my Ubuntu setup, so

Quoted 9 lines
---
 tests/functional/test_arm_aspeed.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/functional/test_arm_aspeed.py +0 -0
index 9761fc06a4..a574b1e521 100644

tests/functional/test_arm_aspeed.py def test_arm_ast2600_evb_buildroot_tpm(self): · line 231

Quoted 5 lines
tests/functional/test_arm_aspeed.py +0 -2
@@ -227,11 +227,11 @@ def test_arm_ast2600_evb_buildroot_tpm(self):
 
         image_path = self.ASSET_BR2_202302_AST2600_TPM_FLASH.fetch()
 
-        socket_dir = tempfile.TemporaryDirectory(prefix="qemu_")
-        socket = os.path.join(socket_dir.name, 'swtpm-socket')

I think it would be helpful to add a brief comment here:
# We must put the TPM state dir in /tmp/, not the build dir, # because some distros use AppArmor to lock down swtpm and # restrict the set of locations it can write to.

just as a guard against somebody in future coming along and trying to clean up/rationalize where tests are creating their temporary files.

Quoted 11 lines
tests/functional/test_arm_aspeed.py +3 -2
+        tpmstate_dir = tempfile.TemporaryDirectory(prefix="qemu_")
+        socket = os.path.join(tpmstate_dir.name, 'swtpm-socket')
 
         subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
-                        '--tpmstate', f'dir={self.vm.temp_dir}',
+                        '--tpmstate', f'dir={tpmstate_dir.name}',
                         '--ctrl', f'type=unixio,path={socket}'])
 
         self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
--

2.34.1

thanks
-- PMM

#3 lore raw
Quoted 9 lines · Peter Maydell
Quoted 8 lines · Stefan Berger

From: Stefan Berger <[email protected]>

To avoid AppArmor-related test failures when functional test are run from somewhere under /mnt, adjust the path to swtpm's state to use an AppArmor- supported path, such as /var/tmp, which is provided by the python function tempfile.TemporaryDirectory().

Quoted 8 lines
Quoted 5 lines

An update to swtpm's AppArmor profile is also being done to support /var/tmp.

Thanks for writing this patch. I can confirm that the test now runs OK on my Ubuntu setup, so

That's good to hear. However, it surprises me because the tests probably use /var/tmp/ as temp dir and that's no supported in the AppArmor profile yet.

Quoted 42 lines
Quoted 9 lines
---
  tests/functional/test_arm_aspeed.py | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
tests/functional/test_arm_aspeed.py +0 -0
index 9761fc06a4..a574b1e521 100644

tests/functional/test_arm_aspeed.py def test_arm_ast2600_evb_buildroot_tpm(self): · line 231

Quoted 5 lines
tests/functional/test_arm_aspeed.py +0 -2
@@ -227,11 +227,11 @@ def test_arm_ast2600_evb_buildroot_tpm(self):
 
          image_path = self.ASSET_BR2_202302_AST2600_TPM_FLASH.fetch()
 
-        socket_dir = tempfile.TemporaryDirectory(prefix="qemu_")
-        socket = os.path.join(socket_dir.name, 'swtpm-socket')

I think it would be helpful to add a brief comment here:
# We must put the TPM state dir in /tmp/, not the build dir, # because some distros use AppArmor to lock down swtpm and # restrict the set of locations it can write to.

just as a guard against somebody in future coming along and trying to clean up/rationalize where tests are creating their temporary files.

Quoted 11 lines
tests/functional/test_arm_aspeed.py +3 -2
+        tpmstate_dir = tempfile.TemporaryDirectory(prefix="qemu_")
+        socket = os.path.join(tpmstate_dir.name, 'swtpm-socket')
 
          subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
-                        '--tpmstate', f'dir={self.vm.temp_dir}',
+                        '--tpmstate', f'dir={tpmstate_dir.name}',
                          '--ctrl', f'type=unixio,path={socket}'])
 
          self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
--

2.34.1

thanks
-- PMM

#4 lore raw
Quoted 19 lines · Stefan Berger
Quoted 15 lines · Peter Maydell
Quoted 13 lines · Stefan Berger

From: Stefan Berger <[email protected]>

To avoid AppArmor-related test failures when functional test are run from somewhere under /mnt, adjust the path to swtpm's state to use an AppArmor- supported path, such as /var/tmp, which is provided by the python function tempfile.TemporaryDirectory().

An update to swtpm's AppArmor profile is also being done to support /var/tmp.

Quoted 6 lines
Quoted 2 lines

Thanks for writing this patch. I can confirm that the test now runs OK on my Ubuntu setup, so

That's good to hear. However, it surprises me because the tests probably use /var/tmp/ as temp dir and that's no supported in the AppArmor profile yet.

On my machine the temporary directory that tempfile.TemporaryDirectory() creates is under /tmp/. This matches what the Python docs say:
https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir
where if you haven't set TMPDIR, TEMP or TMP then you get /tmp/ (assuming it exists; you get /var/tmp if it doesn't).

thanks
-- PMM