Commit Graph

1523 Commits

Author SHA1 Message Date
Stefan Berger
500f81ec0f swtpm_setup: Fix errno comparison on end-of-directory (FreeBSD)
FreeBSD may return errno EINVAL beside ENOENT once there are no more
entries in a directory to walk over. It claims that readdir() follows
the getdirentries() return codes, which do include EINVAL but not
ENOENT. But ENOENT is also being used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-30 15:37:14 -04:00
Stefan Berger
68dea0aba1 swtpm_localca: Fix memory leak on arrays of strings
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -04:00
Stefan Berger
1c3417f70b swtpm_localca: Use return from main rather than exit to resolve ASAN issue
Use return rather than exit() when returning from main so that g_autofree'd
variables can be freed.

This resolves issue #568 that occurs with clang only.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -04:00
Stefan Berger
b30a16ed38 swtpm_setup: Use return from main rather than exit to resolve ASAN issue
Use return rather than exit() when returning from main so that g_autofree'd
variables can be freed.

This resolves issue #568 that occurs with clang only.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -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
2bf7bd1827 swtpm_localca: Fix a TPM 1.2/TCSD related memory leak
Auto-free the variables used for holding values read from the config
file.

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
Eiichi Tsukata
7b7dcbb8d8 swtpm_setup: Forward --logfile option to swtpm
Whenever swtpm_setup is executed with --logfile option, forward the
option to swtpm (--log file=...). This helps debugging swtpm
initialization issues.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 12:48:30 -04:00
Eiichi Tsukata
15ff0d967a swtpm: Initialize logging as early as possible
Parse "--log" option earlier to help debugging initialization issues.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 12:48:30 -04:00
Eiichi Tsukata
4e1ce73596 build-sys: Minor error message typo fix for glib
Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 11:53:31 -04:00
Eiichi Tsukata
0b9c2a05da build-sys: Introduce --enable-sanitizers to configure
This enables address and undefined sanitizers.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 11:53:31 -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
68ac6478b4 swtpm_setup: Fix exit code on error to be '1'.
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
1b2b721e06 swtpm: Implement CheckState interface function for checking for stateblob
Implement a CheckState interface function for checking for the
TPM_PERMANENT_ALL_NAME blog. The dirctory backend does a simple stat on the
file without actually reading it, which otherwise may require the (correct)
key if it was encrypted.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 08:52:32 -04:00
Stefan Berger
3d8b87611c swtpm: Remove all interface functions from swtpm_nvstore_dir.h
Remove all interface functions from swtpm_nvstore_dir.h and make them
static in swtpm_nvstore_dir.c. This way we avoid direct calls to these
functions from elsewhere.

Move the declaration of the interface structure into swtpm_nvstore.h
to get rid of swtpm_nvstore_dir.h entirely.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 08:52:32 -04:00
Stefan Berger
78137e819f swtpm: Get rid of printf's in swtpm_aes.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-22 11:28:37 -04:00
Stefan Berger
6bc55a6752 swtpm: Cleanup storage backend on shutdown to unlock dir
Cleanup on the storage backend side on shutdown to unlock the
locked directory.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 21:21:41 -04:00
Stefan Berger
784d9e2194 swptm_ioctl: Eliminate an unreachable statement
If is_chardev is false, then we have a buffersize of 4096 bytes per
the following statement:

322    if (!is_chardev)
323        buffersize = 4096;

With this we end up in the following if branch:

384        if (buffersize) {
385            /* continue with the read interface */
[...]
418
419            break;
420        } else {

Per the break in line 419 we leave the loop, thus cannot reach the
statement we remove:

423        if (!is_chardev)
424            break;

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 17:53:14 -04:00
Nick Chevsky
1e2a6d99cd swtpm: Don't set END_ON_HUP flag in CMD_SET_DATAFD handler
Don't set flag MAIN_LOOP_FLAG_END_ON_HUP in the handler for control
channel command CMD_SET_DATAFD, since this caused the swtpm daemon to
unconditionally terminate when the data channel connection dropped.
Removing the flag allows this behavior to be controlled by the user
via the --terminate command-line switch, as it was meant to be.

Signed-off-by: Nick Chevsky <nchevsky@users.noreply.github.com>
2021-09-21 12:11:34 -04:00
Stefan Berger
ca4ee25ce7 swtpm: Use EVP_CIPHER in SWTPM_SymmetricKeyData_Encrypt/Decrypt
Use the EVP_CIPHER implementation for the AES CBC computations. This
API has been supported already in OpenSSL 1.1.x for sure and is also
not deprecated in OpenSSL 3.0.

This now resolves issue #538.
We can build src/swtpm without having to suppress deprecated API warnings.
Since this is the last issue related to OpenSSL deprecated APIs, we don't
need to suppress any OpenSSL 3.0 deprecated API warnings anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 11:52:07 -04:00
Stefan Berger
eecb73ebbd swtpm_setup: Use non-deprecated APIs to contruct RSA key (OSSL 3)
Use only non-deprecated APIs to construct RSA key when compiling with
OpenSSL 3.0.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 10:32:16 -04:00
Stefan Berger
516acd1d6b swtpm_setup: Move check for pkey = NULL right after creation
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 10:32:16 -04:00
Stefan Berger
76a42942a4 build-sys: selinux: Compare absolute path against absolute path
This patch fixes issue #533.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 21:20:26 -04:00
Stefan Berger
4c5a410291 swtpm: Add implementation of SWTPM_HMAC using OpenSSL 3.0 APIs
Add an implementation of SWTPM_HMAC using non-deprecated APIs when
compiling with OpenSSL 3.0.

This partially addresses issue #538.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 12:08:18 -04:00
Stefan Berger
2f7b271e33 Travis: Remove python3-twisted package from being installed
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 07:25:53 -04:00
Stefan Berger
c50bc51f99 build-sys: Remove check for pip3 that was used by python swtpm_setup
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 07:25:53 -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
Stefan Berger
3b5d849eaf build-sys: Use -DOPENSSL_SUPPRESS_DEPRECATED (OSSL 3)
Use the (undocumented) OPENSSL_SUPPRESS_DEPRECATED to suppress the
deprecated API warnings when compiling swtpm and swtpm_setup with
OpenSSL 3.0.0 replacing the global -Wno-deprecated-declarations.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-18 18:57:31 -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
Stefan Berger
1860183c42 build-sys: Add -Wno-deprecated-declarations to default CFLAGS (OSSL 3)
To be able to build with OpenSSL 3.0 we need to added
-Wno-deprecated-declarations to the default CFLAGS.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-15 13:36:07 -04:00
Eiichi Tsukata
784e05321f swtpm: Fix SWTPM_NVRAM_PrintJson() error handling logic
Return -1 when SWTPM_NVRAM_GetFilenameForName() or
SWTPM_NVRAM_Init() fails.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-09 21:47:21 -04:00
Eiichi Tsukata
89f25f91ce swtpm: Rename SWTPM_NVRAM_Print_Json()
Rename that to SWTPM_NVRAM_PrintJson() to conform the existing function
naming rule.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-09 21:47:21 -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
e0d2c0ed57 swtpm_setup: Use --print-states in check_state_overwrite()
Instead of directly accessing TPM state store file, use --print-states
to check if there is existing state file or not. This can reduce TPM
state directory dependencies of swtpm_setup.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -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
7703d4461a swtpm: Add GetFilepathForName()
Introduce GetFilepathForName() and use it to get a rooted state file
path instead of existing GetFilenameForName().
Move GetFilenameForName() to swtpm_nvstore.c so that other TPM state
store backend can use it.

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
Eiichi Tsukata
20ca1eb311 swtpm_setup: Use temp dir for certs instead of TPM state dir
Currently swtpm_setup saves ek.cert and platform.cert to TPM state
directory and removes them later. As the usage of these files are
temporary, temp directory can be used for this purpose. This reduces
dependencies on TPM state dir, which is convenient for pluggable state
store (https://github.com/stefanberger/swtpm/issues/461).

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-18 16:06:20 -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
Stefan Berger
7edc44d576 swtpm: Add missing braces around TPM_DEBUG after if statement
Fix the following compilation issue:

swtpm_nvstore_dir.c: In function 'SWTPM_NVRAM_Validate_Dir':
swtpm_nvstore_dir.c:149:86: error: suggest braces around empty body in an 'if' statement [-Werror=empty-body]
         TPM_DEBUG("SWTPM_NVRAM_Validate_Dir: Rooted state path %s\n", tpm_state_path);
                                                                                      ^
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 17:13:22 -04:00
Stefan Berger
e43946a597 swtpm: Allow sched_setattr in seccomp profile for CUSE TPM
glib's (v2.68) g_thread_pool_new() calls the syscall sched_setattr(),
which we must allow to avoid termination of the CUSE TPM.

This patch resolves issue #520.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 13:36:39 -04:00
Stefan Berger
8f57999d82 swtpm: Clear keys on heap at termination
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 10:34:40 -04:00
Eiichi Tsukata
59ad2983fd swtpm: Wipe out key to ensure it is not left on the stack
This change is conforming to the following SEI CERT recommendation.

MEM03-C. Clear sensitive information stored in reusable resources:
https://wiki.sei.cmu.edu/confluence/display/c/MEM03-C.+Clear+sensitive+information+stored+in+reusable+resources

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 07:15:05 -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
Eiichi Tsukata
2d3deaef29 swtpm: Abstract NVRAM interface for pluggable state store
Related GH issue: https://github.com/stefanberger/swtpm/issues/461

This patch adds an abstraction layer to NVRAM state store implementation
for pluggable storage backends. No functional changes are intended in
this change. The default state store backend ("dir backend") keeps
current behavior.

To make swtpm ready for pluggable store, this patch moves file related
operations to the seperate file (swtpm_nvstore_dir.c) and defined the
interface for plugins (nvram_backend_ops in swtpm_nvstore.h). The
interface can be used by each plugin which will be added later.

With the interface, each plugin can access its "backend_uri" which
points to the location of the backend storage, for example S3 bucket
URL or iSCSI URL, and decide how it stores TPM state data.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00