Commit Graph

447 Commits

Author SHA1 Message Date
Stefan Berger
02ca22e7f6 swtpm: Support parsing of JSON maps as option values
Allow passing a JSON map as part of an option value in the format of
--foo name={...},... Prior to this patch this would not have worked since
the option values were broken apart around commas, which a map may also
contain. Now, if a '{' is following the '=', the value is attempted to be
parsed as a JSON map and the end of the map is searched considering
possibly embedded maps.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Marc-André Lureau
d6ca69ad46 tests: drop the need to set INSTALLED=1 when running tests
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Marc-André Lureau
24b8e202b6 tests: teach them to run installed
Rely on "common" variables.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Marc-André Lureau
90bd13750d tests: add installed-runner.sh
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Marc-André Lureau
bb59dc7f81 tests: use swtpm_setup.conf from tests
Replace usage of samples/swtpm_setup.conf, use the one from tests/
instead. This allows installed-tests to refer to it.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Marc-André Lureau
6ee1dac22d tests: install tests on the system
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Marc-André Lureau
457b28cc49 tests: lookup system binaries if INSTALLED is set
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 10:38:38 -04:00
Stefan Berger
81da4d3289 tests: Update to ibmtpm20tss v2.2.0
With patches applied to the test suite this now works with
libtpms v0.7 .. v0.10-dev.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-06-27 09:37:21 -04:00
Stefan Berger
f756ee8a28 tests: Use regex to compare --print-states sizes output
Older versions of libtpms produced smaller initial state files.
Therefore, use a regular expression to compare the sizes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-17 12:49:14 -04:00
Stefan Berger
b1b9a6a4ab tests: Add test cases for using swtpm --print-states while swtpm is running
swtpm <0.8 tried to lock the .lock file when executing --print-states,
which then failed when another swtpm was holding the lock. This adds
a test case for this scenario.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 08:43:01 -04:00
Stefan Berger
f7bfb5d8dc tests: Fix 3 occurrences of stray '\' before '-'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-10 19:40:31 -04:00
Stefan Berger
d258668d51 build-sys: Add support for --disable-tests to disable tests
Add support for --disable-tests to disable the tests and the checking
for tools that only need to be installed for the tests. Keep the tests
enabled as default as it was before.

Resolves: https://github.com/stefanberger/swtpm/issues/843
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-22 11:18:05 -04:00
Stefan Berger
2534112322 tests: Rearrange order of test cases to run some also as 'root'
Some test cases can be run as root, so rearrange the order of the
test cases so that not all of them are skipped if the test case
runs as 'root'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-19 14:28:01 -04:00
Stefan Berger
529988f1e4 tests: Add tests for command line options and combinations of options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-14 12:09:20 -04:00
Stefan Berger
a4347aff21 tests: Remove unnecessary '\' before '/'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-08 15:04:38 -05:00
Stefan Berger
06b1b1f9ff tests: Add softhsm_setup to shellcheck'ed files and fix issues
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-11-22 07:16:44 -05:00
Stefan Berger
3068b48d18 tests: Add missing 'exit 1' on unexpected file size on --reconfigure
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 17:19:29 -04:00
Stefan Berger
116953fb1d tests: Add test cases for swtpm_cert with max serial number
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 17:19:29 -04:00
Stefan Berger
efa030b9da tests: reformat regexs for easier readability and extension
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
67152d3e18 tests: Fix spelling mistakes
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-17 20:46:43 -04:00
Stefan Berger
bb2c7ed14e tests: Remove stray \ in front of - after already escaped '\-'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-26 20:50:01 -04:00
Stefan Berger
1fa820468f tests: Fix newly detected shellcheck issues SC2086 by quoting
Shellcheck v0.9 detected more SC2086 issues.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-26 20:50:01 -04:00
Stefan Berger
a464c649e2 tests: Disable shellcheck SC2317
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-26 20:50:01 -04:00
Stefan Berger
9a154fa45f tests: ibmtss2: Add patch to disable x509 test with older libtpms
Older versions of libtpms need to have another patch applied that disables
x509 certificate creation (0013-Disable-x509-test-cases-part2.patch).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-05-15 14:21:37 -04:00
Stefan Berger
3095011412 tests: Upgrade to ibmtss2 v2.0.1
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-05-11 09:24:54 -04:00
Stefan Berger
346b3d6265 tests: Fix shellcheck issue SC2148 by adding missing shell directive
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-31 10:37:31 -05:00
Stefan Berger
c2b54e6ab5 tests: Fix shellcheck issue SC2148 by adding a shell directive
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-30 18:25:45 -05:00
Stefan Berger
b40fc82700 tests: Fix 2 cases of shellcheck SC2001 using bash search-replace
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-30 18:25:45 -05:00
Stefan Berger
c8699ccd1b tests: Disable shellcheck SC2010 in two cases
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-30 18:25:45 -05:00
Stefan Berger
e5ccc7bb37 tests: Fix shellcheck issue SC2115 by using "${var:?}"
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-29 15:04:00 -05:00
Stefan Berger
5704342aa8 tests: Fix shellcheck issue SC2181 by if cmd; ...
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-27 11:58:54 -05:00
Stefan Berger
4cad2bb32e tests: Fix shellcheck issue SC2143 by using grep -q
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-26 08:12:59 -05:00
Stefan Berger
20655646c6 tests: Fix shellcheck issue SC2126 by using 'grep -c'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-14 13:31:53 -05:00
Stefan Berger
5b51dc6a58 tests: Fix issues related to SC2164
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-13 21:02:51 -05:00
Stefan Berger
fd7a812b24 tests: Fix code to pass shellcheck with some errors disabled
Fix the test cases to pass shellcheck with some of the errors
disable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-01-13 13:59:02 -05:00
Stefan Berger
22e975dc05 tests: Pass --verify-profile=medium to certtool if supported
certtool emits the following message if --verify-profile is not
passed:

Note that no verification profile was selected. In the future the medium profile will be enabled by default.
Use --verify-profile low to apply the default verification of NORMAL priority string.

Pass the --verify-profile option if certtool supports it (since ~3.6.12).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-11-02 11:14:19 -04:00
Stefan Berger
aa92bbf615 tests: Replicate test_ctrlchannel3 for TPM 2 to test_tpm2_ctrlchannel3
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
9c5bb4ac73 tests: Test automatic termination upon loss of ctrl channel connection
Extend the test_ctrlchannel3 to test for automatic termination of swtpm
upon loss of control channel connection.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
1f36b7fec4 tests: Use SOCK_STREAM for CMD_SET_DATAFD socketpair
Switch to SOCK_STREAM for the CMD_SET_DATAFD socketpair where the one
end is passed to swtpm to test that this type of socket will cause
automatic termination of swtpm when the connection is lost. This is also
the socket type that QEMU uses.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
d5296e19e7 swtpm: Extend capabilities JSON and show ctrl-opt-terminate
Extend the capabilities JSON and show the support for the terminate
parameter of the --ctrl option.

Adjust test cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
e5fdd1c181 tests: Add test case for state migration and storage locking
Add a test case that monitors the locking of the storage by swtpm using the
directory storage backend to ensure that the lock is taken at the right
time and released when required.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
6fbb219db9 swtpm: Implement CMD_LOCK_STORAGE to lock storage
Implement CMD_LOCK_STORAGE / PTM_LOCK_STORAGE for a user to be able to
lock the storage of the storage backend (if supported) after its lock
has been released for example when the 'savestate' blob was received
while the TPM state was migrated.

Also adjust test case and extend man pages.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
e821754bb6 swtpm: Advertise --migration option in capabilities JSON
Advertise the capability of supporting the --migration option
in the capabilies JSON that now has the cmdarg-migration verb:

$ swtpm socket --print-capabilities | jq
{
  "type": "swtpm",
  "features": [
    "tpm-1.2",
    "tpm-2.0",
    "tpm-send-command-header",
    "flags-opt-startup",
    "flags-opt-disable-auto-shutdown",
    "cmdarg-seccomp",
    "cmdarg-key-fd",
    "cmdarg-pwd-fd",
    "cmdarg-print-states",
    "cmdarg-migration",
    "nvram-backend-dir",
    "nvram-backend-file"
  ],
  "version": "0.8.0"
}

Adjust test cases and extend man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
959bbb5a19 tests: Check for fallocate tool and its support for --posix
Older versions of fallocate do not support the --posix option that the test
needs. If --posix is not supported, skip the test.

Also check for availability of the losetup tool.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-26 14:41:06 -04:00
Stefan Berger
cdc39fd977 swtpm: Advertise the --chroot option with cmdarg-chroot
Advertise the availability of the chroot option with the cmdarg-chroot
verb. Document it in the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-25 13:13:20 -04:00
Stefan Berger
a44b642d70 tests: If filesystem is mounted with nodev opt skip CUSE chroot test
The CUSE TPM test will not work if the filesystem the test case runs
on is mounted with the 'nodev' option since the CUSE TPM can then
not use /tmp/.../dev/cuse.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-25 13:13:20 -04:00
Jennifer Herbert
68e5428492 swtpm: Add a chroot option
Add an option to enter a chroot after starting swtpm. This is useful for
sandboxing purposes. When this option is used, it is expected that swtpm
is started as root and the --runas option is used to subsequently drop
privileges (otherwise the chroot could be escaped).

Signed-off-by: Jennifer Herbert <jennifer.herbert@citrix.com>
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-08-25 10:04:35 -04:00
Stefan Berger
487892f2fc tests: Add test case to check that swtpm sends a TPM2_Shutdown
Add a test case that checks that swtpm sends a TPM2_Shutdown() to the
TPM 2 upon abrupt re-initialization (CMD_INIT) or graceful shutdown
(control channel, CMD_SHUTDOWN) of the TPM 2 and avoids a potential
dictionary attack (DA) lock-out. A previously sent command failing
authorization with DA implications would otherwise trigger the
TPM_PT_LOCKOUT_COUNTER to increase by '1' if the TPM 2 was not properly
shut down by the client (guest OS) with a TPM2_Shutdown() command.

The test case tests whether a TPM2_Shutdown() is now sent before a reset.
The defined password-protected NVRAM area has the DA flag set and the test
case tries to read from it without providing a password. If we didn't send
the TPM2_Shutdown() before the test cases sends the reset (CMD_INIT), then
the dictionary attack lockout counter would be increased by one. With the
instrumentation in the previous patch the automatically sent
TPM2_Shutdown() keeps the counter at 0.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
97e910af58 swtpm: Introduce disable-auto-shutdown flag for --flags option
Introduce disable-auto-shutdown flag for the --flags option to disable
the sending of TPM2_Shutdown() if swtpm determines that it needs to send
this command to a TPM 2 before device reset or swtpm program termination.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
54de243efd tests: Remove dump of logfile at end of test
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-11 15:02:25 -04:00
Stefan Berger
bcf1fa951c swtpm: cuse: Restrict opening CUSE device to one openable file descriptor
Restrict the opening of the CUSE device to one single file descriptor. We
can modify the CUSE TPM in this way since the kernel's /dev/tpm0 cannot be
opened multiple times, either, and the CUSE TPM should behave in the same
way.

Adjust test the partial reads case to only open CUSE device file once by
using a python program. Close the open file descriptor 100 before using
swtpm_ioctl to avoid failures.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-09 18:27:07 -04:00
Stefan Berger
b255d07010 tests: Move swtpm_open_cmddev into swtpm_cmd_tx
Move swtpm_open_cmddev call into swtpm_cmd_tx since the latter function is
always called in a subshell that previously inherited the file descriptor
opened by the test cases. Remove swtpm_cmd_tx from nearly all test cases
and also remove closing of file descriptor 100 via 'exec 100>&-' from test
cases since this is not necessary anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-09 17:10:37 -04:00
Stefan Berger
b3c32ed5a0 swtpm: Also advertise the flags-opt-startup option for the CUSE interface
Commit 6559a902 implemented support for the startup-xyz flags for the CUSE
interface but the capability has not been advertised.

Adjust test cases to reflect the new verb being shown for
--print-capabilities.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-14 08:07:27 -04:00
Stefan Berger
1b21d052f7 tests: Do not activate SHA-1 PCR bank in test case
For being able to run tests with runtime-deactivated SHA-1 (in libtpms),
do not test with SHA-1 bank anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-13 21:46:56 -04:00
Stefan Berger
54864ccc0e test: Recreate TPM 2 state files with header
Use libtpms v0.6.6 and recreate the TPM 2 state file with header.
Start swtpm with the existing state files and have it rewrite the
volatiles state (swtpm_ioctl -v) and permanent state (tssnvdefine
+ tssnvundefine) files so that the header is on the files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-09 10:15:08 -04:00
Stefan Berger
1add354ee4 test: Recreate TPM 2 state files with header
Use libtpms v0.6.2 and recreate the TPM 2 state file with header.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-09 10:15:08 -04:00
Stefan Berger
5614c9e89d test: Recreate TPM 2 state files with header
Recreate TPM 2 state files that didn't have a header. Use latest
version of libtpms from the stable-0.6.0 branch to create the
state that more recent version have to be able to read.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-09 10:15:08 -04:00
Stefan Berger
3845a08e0a test: Recreate TPM 1.2 state files with header
Recreate a TPM 1.2 state file with header.

The state of the TPM 1.2 must be initialized with Startup(ST_CLEAR)
and then saved so that the proper error code appears as a result
when running this test.

The PCR values was originally created by extending PCR 10 with
sha1("test"). This was recreated using this sequence:

s=$(echo -en test | sha1sum | cut -d " " -f1 | sed -n 's/\([a-f0-9]\{2\}\)/\\x\1/pg')
echo -en $s > input
tss1extend  -ha 10 -if input

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-09 10:15:08 -04:00
Stefan Berger
cafdcfe71a test: Recreate TPM 1.2 state files with header
Recreate TPM 1.2 state files with similar content but with the state
file header. The older versions of the state files were created before
the header was introduced in v0.1. The goal is to be able to get rid
of code supporting pre-v0.1 files that had no header.

The PCR values was originally created by extending PCR 10 with
sha1("test"). This was recreated using this sequence:

s=$(echo -en test | sha1sum | cut -d " " -f1 | sed -n 's/\([a-f0-9]\{2\}\)/\\x\1/pg')
echo -en $s > input
tss1extend  -ha 10 -if input

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-09 10:15:08 -04:00
Stefan Berger
85ec64573b tests: Patch IBM TSS2 test suite for OpenSSL 3.x
Apply a patch to the IBM TSS2 v1.6 test suite when OpenSSL 3.x is de-
tected.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-03 13:27:13 -04:00
Stefan Berger
15d446885b tests: Repeat TSS command if it fails
Repeat tss command since it may fail if the test case is run alone (-29).
The reason for this is that the command may fail because of this here:

https://github.com/stefanberger/libtpms/blob/stable-0.9/src/tpm2/SessionProcess.c#L1204

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-03 13:27:13 -04:00
Stefan Berger
b34da36cde tests: Use ${WORKDIR} in config files to test env. var replacement
To test the replacement of environment variables with their values
use ${WORKDIR} in the test case config files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-23 09:17:11 -05:00
Stefan Berger
13aaff6b8f tests: Fix expiration date check for 32 bit machines
certtool on 32 bit machines seems to expire a never expiring certificate
in 2037 rather than 9999.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-05 16:58:54 -04:00
Stefan Berger
f894b45f99 tests: OSSL 3: Make TPM 1.2 test compile; skip IBM TSS 2 test
Add CFLAGS="-DOPENSSL_SUPPRESS_DEPRECATED=1" to the configure line
to avoid compile-time errors when building the TPM 1.2 test with
OpenSSL 3.0.

IBM TSS2 v1.6 test does not currently work with OpenSSL 3.0, so
skip it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-05 12:59:42 -04:00
Stefan Berger
25d4ac2d3a swtpm_setup: Add support for --reconfigure flag to change active PCR banks
Add support for --reconfigure option for the swtpm_setup to be able to
change the active PCR banks. This option only works with --tpm2 and does
not allow to pass several other options such --create-ek or
--create-ek-cert or --create-platform-cert that would alter the state of
the TPM 2 in other ways.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-05 06:39:49 -04:00
Stefan Berger
15b9ca6bc3 tests: Support filenames with spaces in some functions
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-05 06:39:49 -04:00
Stefan Berger
cce7503cbf tests: exit with error code if mktemp fails
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-02 08:35:49 -04:00
Stefan Berger
743b4d1b61 tests: Convert 2 test cases to use --daemon option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-27 18:58:25 -04:00
Stefan Berger
122106b8f2 tests: Add test case for chardev for swtpm --print-states option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-27 18:58:25 -04:00
Stefan Berger
6b2cb5ff5a test: Add missing _test_tpm2_file_permissions file to EXTRA_DIST
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-18 10:39:27 -04:00
Stefan Berger
e68cebaf5f swtpm_localca: Created certificates for CAs and TPM that do not expire
Rather than having the CA certificates, that are created on the fly,
expire in 10 years, have them not expire at all.

Also create TPM certificates that don't expire and extend a test
case for this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 14:07:02 -04:00
Stefan Berger
bcd5523b08 tests: Add a test case for the new swtpm_setup option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
2b60723766 swtpm_setup: Implement option --create-config-files to create config files
Implement the option --create-config-files to create config files
for swtpm_setup and swtpm-localca for a user account. The files will
be created under the $XDG_CONFIG_HOME or $HOME/.config directories.

This option supports optional arguments 'overwrite' to allow overwriting
existing config files as well as the optional argument 'root' to create
config files under root's home directory. Both options can be passed
by separating them with a ','.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
e6cbc55df6 swtpm: Display the --print-states capability and document it
Display the --print-states capability in the --print-capabilites
output as cmdarg-print-states.

Document availability in the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-06 19:25:41 -04:00
Stefan Berger
0bee3d2f5e tests: Use /usr/bin/env bash rather than /bin/bash (BSDs)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Berger
0841299317 swtpm: Use sys/mount.h and support block devs only if BLKGETSIZE64 exists
The header file fs/linux.h only exists on Linux but we can also
use sys/mount.h, which also exists on Cygwin and the BSDs.

Only support  block devices if BLKGETSIZE64 is defined.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Reiter
e229110079 swtpm_setup: add test case to exercise file backend
heavily copy-pas^W inspired by 'test_swtpm_setup_overwrite'

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
0716f994be swtpm: Add "nvram-backend-linear" capability
Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 09:01:30 -04:00
Stefan Reiter
7bc7b33b06 swtpm: Add tests for "linear file" backend
Adapt save_load_state tests to include coverage of the "linear file"
backend mode. "tpm2" is save/load is tested with both a regular file and
a loop device to excercise the blockdev mmap code.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 09:01:30 -04:00
Stefan Berger
35c6c90128 tests: Skip TPM 2 pkcs11-related test when ASAN is used
The key is freed using 'gnutls_privkey_deinit(pkcs11key)', yet the
following memory leaks show up that are most likely in the pkcs11 module.
Skip the test if ASAN is being used to avoid the test failure.

Direct leak of 55080 byte(s) in 1 object(s) allocated from:
    #0 0x7fdabb152af7 in calloc (/lib64/libasan.so.6+0xaeaf7)
    #1 0x7fdab6b737c6 in C_Initialize (/usr/lib64/pkcs11/libtpm2_pkcs11.so+0x147c6)
    #2 0x7fdab9a5f8a9 in initialize_module_inlock_reentrant (/lib64/libp11-kit.so.0+0x2b8a9)
    #3 0x7fdab9a5fc88 in managed_C_Initialize (/lib64/libp11-kit.so.0+0x2bc88)
    #4 0x7fdab9a66018 in p11_kit_modules_initialize (/lib64/libp11-kit.so.0+0x32018)
    #5 0x7fdab9a66778 in p11_kit_modules_load_and_initialize (/lib64/libp11-kit.so.0+0x32778)
    #6 0x7fdabab10dc5 in auto_load (/lib64/libgnutls.so.30+0x9cdc5)
    #7 0x7fdabab12656 in gnutls_pkcs11_init (/lib64/libgnutls.so.30+0x9e656)
    #8 0x7fdabab12779 in _gnutls_pkcs11_check_init (/lib64/libgnutls.so.30+0x9e779)
    #9 0x7fdabab1af1f in gnutls_pkcs11_privkey_import_url (/lib64/libgnutls.so.30+0xa6f1f)
    #10 0x7fdabaaee0e3 in gnutls_privkey_import_url (/lib64/libgnutls.so.30+0x7a0e3)
    #11 0x40abee in main /home/stefanb/dev/swtpm/src/swtpm_cert/ek-cert.c:1399
    #12 0x7fdab9f5ab74 in __libc_start_main (/lib64/libc.so.6+0x27b74)
    #13 0x40366d in _start (/home/stefanb/dev/swtpm/src/swtpm_cert/swtpm_cert+0x40366d)

Indirect leak of 8208 byte(s) in 1 object(s) allocated from:
    #0 0x7fdabb152af7 in calloc (/lib64/libasan.so.6+0xaeaf7)
    #1 0x7fdab6b736f9 in C_Initialize (/usr/lib64/pkcs11/libtpm2_pkcs11.so+0x146f9)
    #2 0x7fdab9a5f8a9 in initialize_module_inlock_reentrant (/lib64/libp11-kit.so.0+0x2b8a9)
    #3 0x7fdab9a5fc88 in managed_C_Initialize (/lib64/libp11-kit.so.0+0x2bc88)
    #4 0x7fdab9a66018 in p11_kit_modules_initialize (/lib64/libp11-kit.so.0+0x32018)
    #5 0x7fdab9a66778 in p11_kit_modules_load_and_initialize (/lib64/libp11-kit.so.0+0x32778)
    #6 0x7fdabab10dc5 in auto_load (/lib64/libgnutls.so.30+0x9cdc5)
    #7 0x7fdabab12656 in gnutls_pkcs11_init (/lib64/libgnutls.so.30+0x9e656)
    #8 0x7fdabab12779 in _gnutls_pkcs11_check_init (/lib64/libgnutls.so.30+0x9e779)
    #9 0x7fdabab1af1f in gnutls_pkcs11_privkey_import_url (/lib64/libgnutls.so.30+0xa6f1f)
    #10 0x7fdabaaee0e3 in gnutls_privkey_import_url (/lib64/libgnutls.so.30+0x7a0e3)
    #11 0x40abee in main /home/stefanb/dev/swtpm/src/swtpm_cert/ek-cert.c:1399
    #12 0x7fdab9f5ab74 in __libc_start_main (/lib64/libc.so.6+0x27b74)
    #13 0x40366d in _start (/home/stefanb/dev/swtpm/src/swtpm_cert/swtpm_cert+0x40366d)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-04 20:35:25 -04:00
Stefan Berger
3928bd2c1c tests: Use nm and grep to check for ASAN
clang doesn't link executables built with ASAN support to libasan, like
gcc does, so we have to use nm rather than ldd for checking for whether
the executable was built with ASAN. nm is part of the binutils package
and should be available on all systems where gcc was installed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 14:57:38 -04:00
Stefan Berger
736258228b tests: Replace bad '~' with '\!'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 14:57:38 -04:00
Stefan Berger
94aae1bbe5 test: Fix test_tpm2_file_permissions for BSD
BSD cp does not understand the -d option, so remove it.
It's better to use "su -u nobody -c '...'" than sudo, which makes
this test also work on the BSDs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 12:31:09 -04:00
Stefan Berger
30510820fe tests: Add a test case for testing --runas and ability to access files
Add a test case for testing the --runas parameter and the ability to access
existing files that have restrictive access permission so that only allow
the --runas user is able to access these files. This is to ensure that
swtpm can access these files properly when the handling of options and the
changing to the --runas user is moved around within swtpm.

Skip the test case if swtpm is linked with ASAN since then we get these
types of error messages that fail tests:

==== Starting swtpm with interfaces socket+socket ====
==3303263==LeakSanitizer has encountered a fatal error.
==3303263==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==3303263==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 09:23:28 -04:00
Stefan Berger
2fdb7c307b swtpm: Add size to each type of state and use JSON object
Add the size of the type state to the --print-states output and switch
back to a JSON object when enumerating the blobs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 07:46:38 -04:00
Stefan Berger
3c10960c25 tests: Fix tests using tpm2-abrmd by preventing concurrency
The two tests test_tpm2_samples_create_tpmca and
test_tpm2_swtpm_localca_pkcs11 cannot complete successfully if run in
parallel. To solve this issue, introduce a dependency via the log files
to prevent parallel execution of the two test. We have to append the
.test suffix to their names to be able to do this.

docs:
https://www.gnu.org/software/automake/manual/html_node/Parallel-Test-Harness.html

Resolves issue #501, though the problem with stability of the tpm2 pkcs11
module is not resolved.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-27 09:17:54 -04:00
Stefan Berger
f2aa3274b2 swtpm: Search for all state files and use abstracted names in JSON
Search for all the state files not just the permanent state and
when printing the JSON use the abstracted names rather than concrete
filenames that are only valid for the dir backend but will likely
not exist in other backends.

Adjust swtpm_setup to search for the abstracted name and also
adjust the error message to print out the abstracted name.

Adjust the test cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 10:26:07 -04:00
Stefan Berger
f6c42fe7d0 test: Remove import of twisted package function for python2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 07:25:53 -04:00
Eiichi Tsukata
c0ccbb042d tests: Fix test_tpm2_save_load_state_da_timeout rare timer race
When there is a delay between "Sending TPM2_NV_Write with wrong
password" and "timenow=$(date +%s)", the test can fail with "Error: Did
not get expected failure from TPM2_NV_Write() with good password.
Lockout should be enabled.". This can sometimes happen on slow or busy
systems.

To stabilize the test, this patch introduces precise recovery time
measurement ($timerecovery) and check good password works after that
time. As for lockout timeout, moves starting timer just before
TPM2_NV_Write and check good password fails before that time.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-16 07:57:33 -04:00
Eiichi Tsukata
9bdb0048ab tests: Fix test_tpm2_print_states
Currently it's wrongly running _test_print_states.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-09 21:47:21 -04:00
Eiichi Tsukata
5bc59a74f3 swtpm: Add --print-states for reporting TPM states status
--print-states reports TPM states stored in --tpmstate backend.
This feature is supposed to be used by swtpm_setup for checking TPM state
existence.

Sample output is as follows:

  $ swtpm socket --print-states --tpmstate dir=/tmp --tpm2 | jq .
  {
    "type": "swtpm",
    "states": [
      {
        "name": "tpm2-00.permall"
      }
    ]
  }

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -04:00
Eiichi Tsukata
aae539b27f swtpm_setup: Add tests for --overwrite options
The tests for the following options:
- with "--overwrite"
- with "--not-overwrite"
- without "--overwrite" nor "--not-overwrite"

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -04:00
Stefan Berger
346dc3d512 tests: Skip chardev related tests after checking for chardev support
Skip tests that require the chardev after checking for the chardev
interface support in swtpm. Remove the conditional addition of chardev
related tests from the Makefile.am.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-14 16:01:37 -04:00
Eiichi Tsukata
86931b4f30 swtpm: Add "nvram-backend-dir" capability
Append "nvram-backend-dir" to --print-capabilities output.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00
Eiichi Tsukata
b82eb7e788 swtpm: Add options for pluggable backend (--tpmstate backend-uri)
Add an options to specify pluggable backend URI.

Ex:
  --tpmstate backend-uri=dir://<path_to_dir>

Backend URI is specific to each backend plugin which points to the
location of the NVRAM files.
Currently, "dir" is the only one available backend. In this case
backend-uri should be a path to the directory where files are stored.

This option is designed to compatible with existing "dir" option.
If "dir" is specified, swtpm prioritize "dir" ignoring "backend-uri".

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00
Stefan Berger
3eac247752 swtpm_setup: Add support for checking for TPM 1.2 and TPM 2 support
Implement get_supported_tpm_versions to get swtpm's support for TPM 1.2
and TPM 2 and use it error out in case user choose a TPM version that
is not supported. Also display the supported TPM versions in the
capabilites JSON.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-10 09:38:48 -04:00
Stefan Berger
a93586190a tests: Fix test case number in case of test being skipped
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-10 09:38:48 -04:00
Marc-André Lureau
155ccdf561 swtpm*: report 'version' in --print-capabilities
The "version" field can be used to decide when capabilities are
supported/reported and give some library build details.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00
Marc-André Lureau
c48dd1e279 tests: test for availability of TPM 2.0 support in swtpm
Skip test cases for TPM 2.0 if TPM 2.0 support is not provided by swtpm.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00
Stefan Berger
f1adde9f29 tests: test for availability of TPM 1.2 support in swtpm
Skip test cases for TPM 1.2 if TPM 1.2 support is not provided by swtpm.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
[ skip more tests ]
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-08-10 07:13:24 -04:00