Commit Graph

1269 Commits

Author SHA1 Message Date
Stefan Berger
9d8e6e36f6 tests: Check for permanent state file change due to TPM2_PCR_Read
Extend a test case to check whether the permanent state files changes
when a TPM2_PCR_Read() is executed. The state file change would cause
timeouts inside VMs when an fsync() is used that fully synchronzies the
state of the TPM 2 with the data on disk. The issue has been resolved in
more recent versions of libtpms (master and branches). The updated
libtpms is required if fsync() for files and directories is to be enabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-17 18:25:11 -04:00
Stefan Berger
9a5e11789b swtpm: Reenable fsync on files and directory
This now requires a libtpms version with the following patch applied:

1fb639195b

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-17 16:34:21 -04:00
Stefan Berger
4b5654a19f swtpm: Disable fsync on file & dir due to TPM timeouts (issue #597)
We cannot currently fsync on the TPM's state file and the dir since this
takes too long and commands in a VM may time out. The reason for this is
that the TPM 2 code occasionally writes the permanent state out even on
commands like TPM2_PCR_Extend that must not take a long time.

See explanation for this in the libtpms PR https://github.com/stefanberger/libtpms/pull/274 .

We will re-enable this feature in 'a while' once the updated libtpms
version has been made more widely available.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-17 16:32:38 -04:00
Stefan Berger
8def57facd swtpm_setup: Initialize variables to avoid compiler warnings
Fix issue #591 by initializing the variables swtpm_has_tpm2 and
swtpm_has_tpm12.

swtpm_setup.c:1178:31: note: 'swtpm_has_tpm2' was declared here
     gboolean swtpm_has_tpm12, swtpm_has_tpm2;
                               ^~~~~~~~~~~~~~
swtpm_setup.c:1019:5: error: 'swtpm_has_tpm12' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     printf("{ \"type\": \"swtpm_setup\", "
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            "\"features\": [ %s%s\"cmdarg-keyfile-fd\", \"cmdarg-pwdfile-fd\", \"tpm12-not-need-root\""

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-15 08:44:14 -04:00
Stefan Berger
57b1f7ef88 swtpm: Fix case when no backend URI has been specified
It is possible to start swtpm with this command line even though
neither storage nor communication channels have been provided.

./src/swtpm/swtpm socket --flags not-need-init,startup-clear

Since sending a startup message to the TPM will cause it to
want to store permanent state, we have to handle the case when
no storage backend was given and therefore the backend_uri
is NULL.

Previously the above command line caused a NULL pointer exception
but now handles this case with the following output:

swtpm: SWTPM_NVRAM_Init: Missing backend URI.
swtpm: Error: Could not initialize libtpms.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-12 07:34:10 -04:00
Stefan Berger
0151ec3a52 swtpm: Fix a forgotten endianess-conversion
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-11 17:50:52 -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
9f5342fc68 swtpm_cert: Allow passing -1 for days to get a non-expiring certificate
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 14:07:02 -04:00
Stefan Berger
634e6705c3 swtpm_setup: Exit with error code 1 on unknown option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 11:20:59 -04:00
Stefan Berger
79e4c44c04 swtpm_localca: Exit with error code 1 on unknown option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 11:20:59 -04:00
Stefan Berger
f1d425b5c1 swtpm_localca: Use g_get_user_config_dir() rather than own code
Replace own code with g_get_user_config_dir() which makes things
a lot simpler.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 19:50:24 -04:00
Stefan Berger
874c333802 swtpm_setup: Use g_get_user_config_dir() rather than own code
Replace own code with g_get_user_config_dir() which makes things
a lot simpler.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 19:50:24 -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
5d4dd13e80 samples: Have swtpm-create-user-config-files run swtpm_setup
Have swtpm-create-user-config-files run swtpm_setup with the new
--create-config-files option and possible parameters.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
a7254fab5e swtpm_setup: Implement --create-user-config skip-if-exist
Implement support for skipping over creating the files
if any one of the config files already exist.

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
50670dca12 swtpm: Move ARRAY_LEN and min #define's to swtpm_utils.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Thomas Lamprecht
f2786a759f d/swtpm-tools postinst: create the TSS user if it does not exist
Adapted from tpm-udev [0] which handles that, but it is not really a
hard-requirement for swtpm and TSS_USER is configurable after all
(even if that is mostly used for the test system).

So, create that user and group if it does not exists to avoid errors
and failing installation.

[0]: https://salsa.debian.org/debian/tpm-udev/-/blob/master/debian/tpm-udev.postinst

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-10-07 12:47:08 -04:00
Thomas Lamprecht
7d0f8e1c81 d/swtpm-tools postinst: avoid trying to create/chown in non-configure steps
configure steps should be limited to get only executed on, well
configuration, so check for that and do nothing in the remaining
commands [0] the postinst can be called with.

[0]: https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html#summary-of-ways-maintainer-scripts-are-called

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-10-07 12:47:08 -04:00
Thomas Lamprecht
088a8ca44b debian: downgrade trousers package dependency to recommended
Currently `trousers` is listed as (hard) Dependency, but it does not
seems to be required for quite a few usecases, e.g., ours where we
mainly using swtpm for providing a tpm to VMs.

With trousers in Debian one gets an additional pain point: it comes
with rather dated and in some cirumstances failing by mistake init
script [0] that can throw errors when setting up during installation
and thus fail the whole installation of swtpm as Debian policy for
`Depends` hits:

> A package will not be configured unless all of the packages listed
> in its Depends field have been correctly configured
-- [1]

Declaring dependencies for things are not a hard requirement means
that a user will be required to install more dependencies than
actually needed.

Just documenting that as fact why I investigated in the
hard-requirement on trousers in the first place, not an actual
justification - it's a packaging bug after all.

So downgrade the dependency to "Suggests", as it seems a reasonable
level when checking its documented meaning:

> This is used to declare that one package may be more useful with
> one or more others. Using this field tells the packaging system and
> the user that the listed packages are related to this one and can
> perhaps enhance its usefulness, but that installing this one without
> them is perfectly reasonable.
-- [1]

[0]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=895201
[1]: https://www.debian.org/doc/debian-policy/ch-relationships.html

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2021-10-07 09:38:43 -04:00
Stefan Berger
225d7009ba man: Enumerate the nvram-backend-dir separately
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-06 19:25:41 -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
2e3745db08 swtpm_setup: Move gmalloc after block that may return NULL on failure
To avoid a memory leak, move the gmalloc after a block that may
return NULL on failure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 18:07:20 -04:00
Stefan Berger
b63a3e4b2e swtpm_setup: Introduce label to skip over closing of pidfile
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 18:07:20 -04:00
Stefan Berger
680ad059aa swtpm: Call msync with length = 0 on Cygwin
Cygwin internally uses the Windows API call FlushViewOfFile that
seems to not like to be called with an excessive number of bytes.
Instead, call it with length = 0 so that 'the file is flushed from
the base address to the end of the mapping' and then msync() succeeds.

Source:
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-flushviewoffile

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 15:22:14 -04:00
Stefan Berger
57a8a914c7 swtpm_setup: Use SWTPM_ATTR_UNUSED on unused parameters (BSDs)
To silence the BSD gcc compiler on unused function parameters,
use SWTPM_ATTR_UNUSED.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Berger
ca3379116b swtpm_setup: Use pidfile filename rather than fd (Cygwni, BSDs)
Use the pidfile filename rather than the fd because Cygwin for example
does not seem to support passed file descriptors and also OpenBSD
does not pass some test cases because of this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -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
4a5acef4d7 headers: Add defines for little endian data conversion (OS X)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Berger
0b5826e186 swtpm: Use compiler and system dependent header files
The header file endian.h does not exists everywhere, so we have to
use system_dependencies.h to get something similar on all systems.

Some compilers, such as gcc on FreeBSD, are picky about unused
function parameters, so we have to decorate the unused ones
with SWTPM_ATTR_UNUSED.

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
a4555cb887 swtpm_setup: mention dir:// and file:// in man page and usage
Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -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
6f8b8c6268 swtpm_setup: add file backend
Add a second backend for supporting the file:// linear nvram store in
swtpm.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
81371f665f swtpm_setup: add abstract swtpm_backend_ops with dir:// implementation
Abstract away implementation specific code for handling TPM state in
swtpm_setup. The current code for handling directories is moved to
'swtpm_backend_dir.c'.

Where possible, the input argument is simply passed verbatim as
'backend-uri' to swtpm.

No functional change intended, aside from supporting 'dir://' as
optional prefix. The checks for lock-file accessibility are moved to
check_access(), but that shouldn't affect anything AFAICT.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
5d9edaf401 swtpm_setup: use mkstemp for pidfile
This avoids issues with state backends where we don't have a convenient
directory for a pidfile available.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
3da95325ee swtpm_setup: remove redundant delete_state function
...and use delete_swtpm_statefiles instead. This function iterates the
folder instead of just deleting one file, but since it is already called
before the init call guarded here, it can only affect files created by
this run anyway.

Note that delete_state had slightly different return semantics, but it
doesn't matter, as the return value is ignored here anyway (best effort
cleanup).

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
b129b8dfd1 swtpm: Add "file://" backend to man page
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 Reiter
8394db7126 swtpm: Add "linear file" nvram store backend
Implements a second abstraction layer as an NVRAM storage backend: The
"linear" backend stores data in a simple format that can contain
multiple files (multiple TPM states and numbers) in one linear address
space. This can then be mapped to files or other "block-device-like"
interfaces using nvram_linear_file_ops implementations.

A simple one using mmap is provided with the URI type "file://".

Does not support any locking at the moment, users must ensure exclusive
access themselves.

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
d755072372 swtpm: Make fsync-related errors non-fatal (for libvirt using AppArmor)
Only recent libvirt versions have the patch for the AppArmor profile for
libvirt to allow fsync after opening a directory for reading. Rather
than failing hard on the open-directory-for-reading error, log it once
and continue and do not try it again after.

This patch addresses the problems seen on Ubuntu related to an older
version of libvirt without the AppArmor profile update.
- issue #484
- issue #549
- issue #559

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-02 17:12:30 -04:00
Stefan Berger
96b43e4d60 Revert "swtpm: Make fsync() errors non-fatal (for libvirt using AppArmor)"
This reverts commit 04cb07fa6c.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-02 17:12:30 -04:00
Stefan Berger
c01b1cfbac
Update bug_report.md
Ask users to include log files, such as libvirt log files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-02 11:18:02 -04:00
Stefan Berger
bc5728a974 swtpm: Make fsync() errors non-fatal (for libvirt using AppArmor)
Only recent libvirt versions have the patch for the AppArmor profile
for libvirt to allow fsync on dir and directory. Rather than failing
hard on this error, log it once and continue and do not try fsync
again after.

This patch addresses the problems seen on Ubuntu related to an older
version of libvirt without the AppArmor profile update.
- issue #484
- issue #549
- issue #559

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-01 14:16:32 -04:00
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