Commit Graph

976 Commits

Author SHA1 Message Date
Stefan Berger
bdf7e3d6df swtpm: Daemonize before starting TPM to hold locks
Since the CUSE TPM starts TPM in one process but then daemonizes and
effectively runs in a child process, the lock records on the storage
that the parent may have set up are lost due to them not being inherited
by the child. Fix the issue by daemonizing before the TPM is started so
that the child grabs the lock on the storage. Prevent CUSE from forking
so that not another child is created.

As a side-effect, this now moves any error reporting, that may previously
have occurred in the main process and where messages were show on stderr,
into the child process. A log is now required for these messages to become
visible.

Resolves: https://github.com/stefanberger/swtpm/issues/1050
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-08-21 12:13:05 -04:00
Stefan Berger
3d5d0f2fcb swtpm: Suppress the logging when displaying info flags' JSON result
When printing the output of the info flags, the resuling JSON printed to
stdout should be the only printout. Therefore, suppress all informative
output to stdout so that either the JSON is the only output or only error
messages are printed.

Fixes: 3f551e1dc ("swtpm: Implement --print-info to run TPMLIB_GetInfo with flags")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-08-18 14:27:15 -04:00
Stefan Berger
2f46832dab swtpm: Print some warnings to stdout rather than stderr
Have warnings print to stdout since they do not lead to swtpm exiting with
an error code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-08-18 14:27:15 -04:00
Stefan Berger
bf15af46d1 swtpm: Only disable informative message when using SUPPRESS_INFO_LOGGING
When the special logging file descriptor SUPPRESS_INFO_LOGGING is chosen,
then only suppress informative and warning messages while still allowing
error messages to be printed to stderr.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-08-18 14:27:15 -04:00
Stefan Berger
a9f680df00 cuse: Move duplicate code into tpm_end()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-08-15 16:16:30 -04:00
Stefan Berger
4a0e632f37 swtpm: Prevent an integer overflow (false positive; CID 440360)
Prevent an integer overflow with the recvd variable. However, the
buffer_len variable serves as an upper bound for how many bytes will ever
be received, so that this integer overflow will never occur. Therefore,
this is a false positive reported by Coverity. Fix it anyway.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:31:44 -04:00
Stefan Berger
2562694adc swtpm: Prevent an integer overflow (false positive; CID 440361)
Prevent an integer overflow that could result from adding the return value
of 'n' to an existing value. However, for this to occurr in this function,
one would have to write() more than 4G of data on a 32bit system for
example. So, this is a false positive reported by Covertity, but fix it
anyway.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
6a25e170a9 swtpm: Adjust code to avoid integer overflows (false positive; CID 440362)
Change the type of buffersize parameter of SWTPM_IO_Read from size_t to
uint32_t to match that of the caller and to avoid Coverity complaints
about possible integer overflows. Also change the offset to uint32_t.

An integer overflow would never have occurred since buffersize always
served as an upper bound of the number of bytes received.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
7622a6e51d swtpm: Check for sysconf returning bad size for _SC_PAGESIZE (CID 440365)
Catch the unlikely case that sysconf returns 0 for _SC_PAGESIZE and avoid
integer overflow with the pagesize calculation, that should never occur if
pagesize is within normal limits. Also ensure that no overflow happens
with the msync_count variable.

On 64bit machines, casting count to size_t could work but would not work on
32bit platforms where size_t is the same as uint32_t, so the overflow
complain would not go away there.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
0727e87445 swtpm: Report error on rename's in error path (CID 466753)
If the revert of the renaming of files fails report an error there as well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
eb79f144dd swtpm: Return ssize_t rather than int from logging functions (CID 440364)
Return ssize_t rather than int from logging function to resolve Coverity
complaints about possible integer overflows. Since no caller looks at the
return value from the logging functions, no other changes are necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
f0c91b3947 swtpm: Mark do_fsync in linear backend as unused parameter
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-29 10:03:32 -04:00
Stefan Berger
24fdb845b4 swtpm: Display error for fsync option on linear storage backend
Display an error if the user set the fsync option when using the
linear storage backend.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-28 07:29:44 -04:00
Stefan Berger
9578dc45f2 swtpm: Display error for backup option on linear storage backend
Display an error if the user set the backup option when using the
linear storage backend. Update the documentation about the rejection.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-28 07:29:44 -04:00
Stefan Berger
093248eb4d swtpm: Add option for storage backend to use fsync
Add an option to have the storage backend use fsync whenever state is
written to disk. Advertise this capability with
'tpmstate-dir-backend-opt-fsync' and adjust a test case.

Only support for the directory-backend is implemented.

Extend the swtpm man page with a description of this new option.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-27 16:28:39 -04:00
Stefan Berger
795c1973c9 swtpm: Implement support for fsync on a file and directory
Implement support for fsync on a file and directory when using the
directory-backend. Pass the user's choice for whether to call fsync and
pass the boolean into the storage backend. Only the directory-backend is
supporting this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-27 16:28:39 -04:00
Stefan Berger
ec7730c70d swtpm_setup: Also check respbuffer_len for possible NULL pointer (CID 466756)
Some checks failed
Coverity Scan / coverity (push) Has been cancelled
respbuffer_len is a pointer that may be NULL but will not be NULL when
respbuffer is not NULL. Nevertheless, also check it for NULL pointer before
accessing it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-22 17:21:53 -04:00
Leo
17fc25d48a cuse: add libfuse3 support
libfuse2 contains unaddressed security issue. (libfuse/libfuse#15)

libfuse3 is preferred over libfuse2, while libfuse2 support is kept as
fallback.

	- src/swtpm/cuse_tpm.c: fuse3 as default, add a macro `WITH_FUSE2` when fallback fuse library is linked against
	- configure.ac: check fuse3, if not found, check fuse2, if still not found, fail out; show LIBFUSE_CFLAGS and LIBFUSE_LIBS in `./configure` output

Signed-off-by: Leo <i@hardrain980.com>
2025-05-22 10:47:23 -04:00
Stefan Berger
1db1037525 swtpm: Return error if more bytes received than user wanted to send
Some checks are pending
Coverity Scan / coverity (push) Waiting to run
Return an error code if HASH_DATA received more bytes from the user than
what the user indicated in the length field that he wanted to send. This
avoids an integer underrun of the 'remain' variable in the loop that would
then cause the loop to wanting to receive around 4GB of data.

Also fix some indentation issues.
Use be32toh instead of (the equivalent) htobe32 when reading from the
packet.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-20 20:28:25 -04:00
Stefan Berger
95476e31ff swtpm_setup: Hint at profile default-v2 when CreatePrimary() fails with 0x2c4
If TPM2_CreatePrimary(RSA) fails with 0x2c4 error code, display an error
message hinting the user at using the 'default-v2' profile since most likely
the RSA key size is too large for the default or given profile.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-09 10:56:13 -04:00
Stefan Berger
9b7021bb01 swtpm: Implement storage functions to restore a state backup file
When the users specifies that a backup file is to be made and the permanent
state file is missing when the NVRAM is initialized (SWPTM_NVRAM_Init), but
the backup state file exists, then swtpm will permanently rename the backup
file to permanent state file and attempt to start with it. Otherwise, it
will try to start with the 'normal' permanent state file first and if this
fails, it will rename the backup file to the permanent state file and
attempt to start with it. If both cases fail, it will revert any renaming.

Only support for the directory-backend is implemented.

Extend the swtpm man page with a description of this behavior.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-09 08:44:42 -04:00
Stefan Berger
e3694b7b15 swtpm: Add option for storage backend to make a backup file
Add an option to have the storage backend make a backup file of
the permanent state file. Advertise this capability with
'tpmstate-dir-backend-opt-backup' and adjust a test case.

Extend the documentation.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-09 08:44:42 -04:00
Stefan Berger
186f72a3a9 swtpm: Make a backup of the permanent state file in dir-backend
Query for tpmstate_get_make_backup() to decide whether to make a backup of
the permanent state file. If a backup is requested, then rename the current
state file in the directory backend to the backup file (suffix .bak).

Only the directory backend supports backing up of the permanent state file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-09 08:44:42 -04:00
Stefan Berger
e3668c9f26 swtpm_setup: Add support for RSA-4096 keys for EKs
Add support for RSA-4096 kyes for EKs. This requires users to choose the
default-v2 profile because this is the only profile that currently enables
this type of a key.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-08 20:49:36 -04:00
Stefan Berger
6df68dcde5 swtpm: Make swtpm compilable on GNU/Hurd systems
Fix issues related to headers to make swtpm compilable on GNU/Hurd systems.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-28 20:38:14 -04:00
Stefan Berger
bfa18c8208 swtpm_setup: Define PATH_MAX on GNU/Hurd systems since undefined there
Per documentation GNU/Hurd does not define PATH_MAX. Therefore, define it
ourselves.

https://www.gnu.org/software/libc/manual/html_node/Limits-for-Files.html

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-28 20:38:14 -04:00
Stefan Berger
9bdd62d1e9 swtpm: Define ACCESSPERMS if not defined in sys/stat.h
Define ACCESSPERMS if it is not defined, such as for example on Alpine.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-26 14:50:30 -04:00
Stefan Berger
b602ca0b81 swtpm: Factor-out code to file_read
Move the file reading parts from SWTPM_NVRAM_LoadData_Dir into file_read.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-26 14:06:47 -04:00
Stefan Berger
9e2c3a1dec swtpm: Factor-out code to file_write
Move the file writing parts from SWTPM_NVRAM_StoreData_Dir into file_write.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-26 14:06:47 -04:00
Stefan Berger
7d2ee6ebc2 swtpm: Remove unused fsync code from directory backend
Remove the unused fsync code from the directory backend since it could not
be used due to potential reason for TPM command timeouts.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-26 14:06:47 -04:00
Stefan Berger
a79d2d0671 swtpm: Close connection_fd.fd before assigning new value (CID 459830)
Coverity complains that the assignment of connection_fd.fd = mlp->fd
leaks the value of connection_fd.fd. However, the logic is so that
this cannot happen because further down in the loop:

1) only when connection_fd.fd < 0, then pollfds[DATA_SERVER_FD] gets
   a value
2) connection_fd.fd = accept() only happens if 1) happened

However, if mlp->flags & MAIN_LOOP_FLAG_USE_FD is != 0 then
connection_fd was assigned a value and 1) never happens.

=> Fix the Coverity complaint even though it is a false positive.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-09 10:20:46 -04:00
Stefan Berger
8a42a2f7e8 swtpm: Use custom profile's Algorithms when removing FIPS-disabled ones
Use the custom profile's Algorithms when adjusting them for FIPS mode,
rather than the list of all implemented Algorithms. The list of implemented
Algorithms contains for example elliptic curve identifiers, such as
ecc-nist-p192, ecc-nist-p224, ecc-nist-p256, ecc-nist-p384, ecc-nist-p521,
ecc-bn-p256, ecc-bn-p638, that are not part of the custom profile but are
enabled with the ecc-min-size=192, ecc-nist, and ecc-bn shortcuts there.
Using the algorithms of the custom profile avoids confusion since otherwise
the additional ecc-nist-* and ecc-bn-* algorithm identifiers appear in the
modified custom profile even though the were not part of the original one.

Test:

  swtpm_setup --tpm2 --tpmstate . --overwrite \
     --profile-name custom --profile-remove-disabled fips-host

  before:
  ...,ecc,ecc-min-size=224,ecc-nist,ecc-bn,ecc-nist-p224,ecc-nist-p256,
      ecc-nist-p384,ecc-nist-p521,ecc-bn-p256,ecc-bn-p638,ecc-sm2-p256,...

  now:

  ...,ecc,ecc-min-size=224,ecc-nist,ecc-bn,ecc-sm2-p256,...

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-04 12:21:27 -04:00
Stefan Berger
64bbdcadad swtpm_setup: Do not pass a TPM 2 profile to swtpm when reconfiguring
Ensure that no profile is passed to the TPM 2 when it is to be reconfigured
by:

- Showing an error if user tries to pass a profile when also --reconfigure
  is passed
- Not taking the default profile from the swtpm_setup.conf configuration
  file if the user did not pass a profile

Extend an existing test case with a default profile in its swtpm_setup.conf
so that the above 2nd item is tested.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-03-28 11:35:22 -04:00
Stefan Berger
2c4cd12050 swtpm_setup: Use DISTRO_PROFILES_DIR when listing profiles (fix path issue)
When listing profiles, then the profiles in the distro directory did not
show up since the directory formed by 'DATAROOTDIR "swtpm/profiles"' was
missing a '/' at the end of DATAROOTDIR. Use DISTRO_PROFILES_DIR instead.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-03-26 08:41:59 -04:00
Stefan Berger
1cee599a4d selinux: Add rule to allow swtpm_t opening of virt_log_t files (BZ 2278123)
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2278123#c40
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-02-24 09:21:18 -05:00
Marc-André Lureau
cfe93d90b6 SELinux: add NFS permissions for swtpm_t
swtpm fails with a NFS mount. `setsebool virt_use_nfs on` should fix it.

Resolves: https://issues.redhat.com/browse/RHEL-73809

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2025-01-20 11:04:23 -05:00
Marc-André Lureau
01e804ff0a SELinux: allow to map state file
Specify vtpm state to a file instead of dir:

<tpm model="tpm-crb">
  <backend type="emulator" version="2.0">
    <source type="file" path="/var/lib/libvirt/swtpm/mytest/mytpm2-00.permall"/>
  </backend>
</tpm>

$ virsh start avocado-vt-vm1
error: Failed to start domain 'avocado-vt-vm1'
error: internal error: Could not run '/usr/bin/swtpm_setup'. exitstatus: 1; Check error log '/var/log/swtpm/libvirt/qemu/avocado-vt-vm1-swtpm.log' for details.

$ cat /var/log/swtpm/libvirt/qemu/avocado-vt-vm1-swtpm.log
SWTPM_NVRAM_LinearFile_Mmap: Could not mmap file: Permission denied
/usr/bin/swtpm exit with status 256:

$ ausearch -m avc
----
time->Thu Dec 12 08:43:07 2024
type=PROCTITLE msg=audit(1734010987.020:1455): proctitle=2F7573722F62696E2F737774706D00736F636B6574002D2D7072696E742D737461746573002D2D74706D7374617465006261636B656E642D7572693D66696C653A2F2F2F7661722F6C69622F6C6962766972742F737774706D2F6D79746573742F6D7974706D322D30302E7065726D616C6C002D2D74706D32002D2D6C6F67
type=SYSCALL msg=audit(1734010987.020:1455): arch=c000003e syscall=9 success=no exit=-13 a0=0 a1=c0 a2=3 a3=1 items=0 ppid=22547 pid=22549 auid=4294967295 uid=59 gid=59 euid=59 suid=59 fsuid=59 egid=59 sgid=59 fsgid=59 tty=(none) ses=4294967295 comm="swtpm" exe="/usr/bin/swtpm" subj=system_u:system_r:swtpm_t:s0 key=(null)
type=AVC msg=audit(1734010987.020:1455): avc:  denied  { map } for
pid=22549 comm="swtpm"
path="/var/lib/libvirt/swtpm/mytest/mytpm2-00.permall" dev="dm-0"
ino=202744025 scontext=system_u:system_r:swtpm_t:s0
tcontext=system_u:object_r:virt_var_lib_t:s0 tclass=file permissive=0

Resolves: https://issues.redhat.com/browse/RHEL-70835

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2025-01-20 11:04:23 -05:00
Stefan Berger
3d6a8b75b3 swtpm: Fix build error on 32bit systems due to inconsistent _FILE_OFFSET_BITS
Some checks failed
Docker / build (push) Has been cancelled
Fix the following build error due to missing include of config.h where
_FILE_OFFSET_BITS is defined and leads to different sizes of off_t depending
on whether it is defined and/or included:

tpmlib.h:76:7: error: type of 'tpmlib_handle_tcg_tpm2_cmd_header' does not match original declaration [-Werror=lto-type-mismatch]
   76 | off_t tpmlib_handle_tcg_tpm2_cmd_header(const unsigned char *command,
      |       ^
tpmlib.c:576:7: note: return value type mismatch
  576 | off_t tpmlib_handle_tcg_tpm2_cmd_header(const unsigned char *command,
      |       ^
tpmlib.c:576:7: note: 'tpmlib_handle_tcg_tpm2_cmd_header' was previously declared here
tpmlib.c:576:7: note: code may be misoptimized unless '-fno-strict-aliasing' is used
lto1: all warnings being treated as errors
lto-wrapper: fatal error: gcc returned 1 exit status

Link: https://bugzilla.redhat.com/show_bug.cgi?id=2334600
Fixes: 599e2436d4 ("configure.ac: enable 64-bit file API on 32-bit systems")
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-12-27 17:48:32 -05:00
Stefan Berger
3a8e779e11 SELinux: Add rule for swtpm to be able to read password from pipe
Some checks are pending
Docker / build (push) Waiting to run
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2334271
Resolves: https://github.com/stefanberger/swtpm/issues/964
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-12-26 18:28:03 -05:00
Marc-André Lureau
544b3981c4 swtpm-setup: fix invalid path
Make sure there is a '/' after DATAROOTDIR.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-12-16 07:46:45 -05:00
Stefan Berger
c5c4af9efd man: Improve swtpm_setup --tpm option documentation
Some checks failed
Docker / lint (push) Has been cancelled
Docker / build (push) Has been cancelled
Docker / test (push) Has been cancelled
Improve the swtpm_setup --tpm option documentation that did not mention
that the socket option must be passed along when swtpm is being used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-12-10 11:06:38 -05:00
Stefan Berger
d059f40c61 swtpm/swtpm_setup: Initialize variables with NULL for RPM build
When building an rpm with swtpm.spec on Fedora 40 this type of errors
appear on variables that normally do not need to be initialized.

In file included from /usr/include/glib-2.0/glib.h:117,
                 from profile.c:14:
In function ‘g_autoptr_cleanup_generic_gfree’,
    inlined from ‘profile_gather_local’ at profile.c:307:23,
    inlined from ‘profile_printall’ at profile.c:366:10:
/usr/include/glib-2.0/glib/glib-autocleanups.h:32:3: error: ‘dir’ may be used uninitialized [-Werror=maybe-uninitialized]
   32 |   g_free (*pp);
      |   ^~~~~~~~~~~~
profile.c: In function ‘profile_printall’:
profile.c:307:23: note: ‘dir’ was declared here
  307 |     g_autofree gchar *dir;
      |                       ^~~

Include string.h since in some older build environments strcmp and strlen
do not have prototypes otherwise.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-15 07:52:59 -05:00
Stefan Berger
89f7a0545d swtpm: Check for null pointer from parsing string value
Check for a null pointer from parsing the string value in
json_get_submap_value(). All callers assume that the returned value is
non-NULL and therefore ensure that there is always a valid string.
However, all callers also provide trusted input from TPMLIB_GetInfo that
should never cause a NULL pointer.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-13 16:47:17 -05:00
Stefan Berger
fcda38b463 swtpm_setup: Handle case when returned profile Name is null
The profile '{"Name": null}' will not lead to a parser error but return
NULL for the 'Name'. Therefore, check for variable name being a NULL
pointer. Since the user may provide this type of profile this could have
lead to crashes when name was accessed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-13 16:47:17 -05:00
Stefan Berger
e02bf61a22 swtpm_cert: Move error message about importing signing key into else branch
Move the error message about the failure to import a signing key into the
else branch where it should be (all other branches of the if-then-else
statement have a check already). Also mention the key's filename and hint
at possibly corrupted key.

Link: https://bugzilla.redhat.com/show_bug.cgi?id=2325901
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-13 13:52:07 -05:00
Stefan Berger
4eb51c38d9 swtpm_setup: Add missing --print-profiles to help screen
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-12 20:53:07 -05:00
Stefan Berger
bf4fc05517 swtpm: Fix name of variable passed to TPM_DEBUG
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-08 12:41:36 -05:00
Stefan Berger
28345d008b swtpm_setup: Comment flags for storage primary key and deprecate --create-spk
Comment the flags used for creating the storage primary key.
Deprecate the --create-spk option since it may create an RSA-3072 key
and it creates a NIST P384 instead of NIST P256, both of which users may
not expect and know how to use.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-07 08:33:04 -05:00
Stefan Berger
1982c51535 swtpm: Indent 'Tested' output of tested algorithm by one more space
Move the 'Tested: tdes' type of debugging output one more indentation
level up so that they can be filtered-out easier from control and data
channel communication.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-11-05 09:20:53 -05:00
Sergei Trofimovich
599e2436d4 configure.ac: enable 64-bit file API on 32-bit systems
My local filesystem is btrfs with a long life. It's inodes ecxeed 32-bit
space and that causes test failures in `swtpm` on `i686-linux`
containers:

    FAIL: test_parameters
    FAIL: test_swtpm_setup_file_backend
    FAIL: test_swtpm_setup_overwrite
    FAIL: test_tpm2_swtpm_setup_create_cert
    FAIL: test_tpm2_swtpm_setup_overwrite
    FAIL: test_swtpm_setup_create_cert
    FAIL: test_tpm2_parameters

The example test failure log looks this way:

    FAIL: test_migration_key
    ========================

    Need to be root to run test with CUSE interface.
    Need to be root to run test with CUSE interface.
    ==== Starting swtpm with interfaces socket+socket ====
    Test 1: Ok
    ==== Starting swtpm with interfaces socket+socket ====
    Test 2: Ok
    ==== Starting swtpm with interfaces socket+socket ====
    swtpm: Missing migration key to decrypt volatilestate
    Test 3: Ok
    ==== Starting swtpm with interfaces socket+socket ====
    Could not stat file '/build/tests/data/migkey1/volatilestate.bin': Value too large for defined data type
    Error: Could not load encrypted volatile state into TPM.
    FAIL test_migration_key (exit status: 1)

The `stat()` fails because inode value exceeds 32-bit value:

    $ stat /build/tests/data/migkey1/volatilestate.bin
      File: /build/tests/data/migkey1/volatilestate.bin
      Size: 1290            Blocks: 8          IO Block: 4096   regular file
    Device: 0,30    Inode: 9639547569  Links: 1
    ...

The change fixes all the test failures. To fix
`test_tpm2_swtpm_setup_create_cert` I also had to include `config.h`
into `swtpm_backend_dir.c` to get 64-bit file open there as well.

Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
2024-11-04 14:14:09 -05:00