Commit Graph

1700 Commits

Author SHA1 Message Date
Stefan Berger
eb5875793e swtpm: nvstore_linear: Add comment to SWTPM_NVSTORE_LINEAR_MAX_STATES
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
fc57f557d8 swtpm: nvstore_linear_file: Initialize file descriptor to -1
Since 0 is a valid file descriptor and checks for valid file descriptors
is typically '>= 0', initialize the file descriptor to -1 to indicate
that it is unused.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
8ba4ec3c43 swtpm: Refactor parts into SWTPM_NVRAM_LinearFile_DoOpenURI
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
e159d26671 tests: Use ${CERTTOOL} rather than certtool (OS X)
On OS X we have to use ${CERTTOOL} rather than certtool to get
gnutls-certtool.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
86e8f8b861 swtpm_setup: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
47f37b0551 swtpm_ioctl: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
799290d229 swtpm_bios: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
489483f100 swtpm: Fix typo in header guard #define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
06168658cb Revert "swtpm: Print message in case error response is too long"
This reverts commit 8d4b247e3d since
CMD_GET_STATEBLOB can have more than 4 bytes in response when an
error happened (long-standing protocol error).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-12 13:19:05 -04:00
Stefan Berger
8d4b247e3d swtpm: Print message in case error response is too long
All error messages must only be 4 bytes long. Print an error message if
this is not the case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
3a0ff17329 swtpm_ioctl: Use ptm_cap_n for non-CUSE PTM_GET_CAPABILITY response
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
47c96a0c64 swtpm: Use ptm_cap_n to build PTM_GET_CAPABILITY response
Use the new ptm_caps_n to create the response for PTM_GET_CAPABILITY in the
socket and chardev implementations. Due to how the result was created
before, the tpm_result part of the response will always have to be
TPM_SUCCESS, since it was always 0 before.

Leave the CUSE implementation untouched since there the result was returned
in native endianess resulting in the first 4 bytes carrying the capability
flags.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
7499734f98 swtpm: Define a structure to return PTM_GET_CAPABILITY result
Define a structure to use for returning the PTM_GET_CAPABILITY result that
resembles the layout of all the commands' structures. Since only 17bits for
capabilities are currenlty defined and they are returned in big endian
format as part of a 64bit number, this change can be done without any side
effects. The upper 32bit of this number now become the tpm_result, which
will always be 0. The lower 32bits are the possible capability flags, of
which the 17 are used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
f29b870b5a cuse: Move display of help screen into function
Move the display of the help screen into its own function to be able
to usage fprintf with the help screen as format string to avoid static
analyzer warnings.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-08 12:11:37 -04:00
Stefan Berger
7b2ee0ed2e tests: Test activation of PCR banks when not all are available
Restrict available PCR banks to sha256 & sha384 and try to enable sha256
and sha512 and check the expected results.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-08 10:42:47 -04:00
Stefan Berger
3f551e1dc1 swtpm: Implement --print-info to run TPMLIB_GetInfo with flags
Implement --print-info that takes a number as argument and uses this number
as flags to call TPMLIB_GetInfo with. Display the JSON string and exit.

Extend the man page and update other parts where swtpm_ioctl is not necessary
anymore to use.

Extend a test case to also check that swtpm now returns the same result as
swtpm_ioctl does.

Append cmdarg-print-info to printed out capabilties. Adjust test cases.
(Expect 'profiles' to always be part of capabilties JSON.)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-07 11:09:04 -04:00
Stefan Berger
770abf3ff0 tests: Fix a typo in the name of a profile
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
8f670605b5 tests: Enable SWTPM_TEST_PROFILE for running test_tpm2_ibmtss2 with profile
Enable SWTPM_TEST_PROFILE for running the test_tpm2_ibmtss2 test cases
with a user provided profile. Document it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
2fee0e7bb5 man: Improvements and fixes to swtpm_setup manpage
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
50e7429b5b man: Improvements and fixes to swtpm manpage
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
c6db0e3185 swtpm: Support 'remove-disabled' for 'custom:'-prefixed profile names
Extend the support for 'remove-disabled' option parameter of the --profile
option to also work for profiles whose name starts with 'custom:'.

Modify an existing test case to use custom:test as profile name with this
option.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
4faf8eca44 man: Better describe --get-info option parameters for swtpm_ioctl
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
47d37ccba2 debian: Add rules for reading profiles from distro and local dirs
Allow a user to pass profiles from the distro or local dirs directly
to swtpm. A rule to allow reading profiles from somewhere under the
HOME directory already exists.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
34e5103045 swtpm_setup: Implement --print-profiles to display all profiles
Implement support for the --print-profiles option to search the local
and distro profiles directories for profiles (files with .json extension)
and then get the built-in ones from swtpm. Print them all to stdout.

Extend a test case and add description to the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
3d7b2445c3 swtpm_setup: Add profile entries to swtpm_setup.conf written by swtpm_setup
Have swtpm_setup --create-config-files write profile entries into
swtpm_setup.conf.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
5486f0437f swtpm_setup: Add support for --profile-name option
Add support for --profile-name option that searches for a profile in a
configurable local directory or a distro directory
(typically /usr/share/swtpm/profiles).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
7198e0d716 swtpm_setup: Accept profiles with name starting with 'custom:'
Accept profiles that start their name with 'custom:' and do not exceed
32 characters. The content of these profiles will be derived from the
built-in 'custom' profile unless Algorithms, Commands, or Attributes
are provided on the command line.

Adjust a test case to test with profile name starting with 'custom:'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 19:12:05 -04:00
Stefan Berger
82fb09c6a2 selinux: Change write to append for appending to log
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 09:57:34 -04:00
Stefan Berger
2d4a4c57cc selinux: Add rule for logging to svirt_image_t labeled files from swtpm_t
I was able to recreate the issue on one of my machines where swtpm_t
was trying to append to the log labeled with svirt_image_t. On another
machine this combination of labels does not seem to cause a problem.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2306817
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-30 21:32:22 -04:00
Stefan Berger
aa78a302ec swtpm: Display stderr in case swtpm --print-capabilities failed
When swtpm --print-capabilities for example fails to be able to access
the log file that swtpm_setup logs into, then it may fail to run. In
this case log the stderr output of swtpm.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-30 17:01:04 -04:00
Stefan Berger
2e2124928f swtpm_setup: Return error if reading of config file failed
Return an error if the reading of the config file failed so that
config_file_lines can never be NULL. Remove all checks for
config_file_lines == NULL.

It's very unlikely reading of the config file failed since there's a file
access check right before it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-27 15:12:58 -04:00
Stefan Berger
2e42685e6d swtpm_setup: Move checking access to and reading of config file into function
Move the checking of access to and reading of the configuration file into
its own function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-27 15:12:58 -04:00
Stefan Berger
e29457defc Dockerfile: Fix casing of 'as'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-25 07:20:37 -04:00
Stefan Berger
3a1be75d04 swtpm_setup: Fix counting of profile related options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 16:58:06 -04:00
Stefan Berger
e91b27911f swtpm: Initialize empty array
Initialize the empty array authpolicy[0] to quiet a static analyzer.
This array only serves the purpose of providing a non-NULL pointer
when passed to memconcat.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
c0589349c0 swtpm: Return TPM_FAIL from invalid header version case
Return TPM_FAIL in case of an invalid header version number to avoid a
potential segmentation fault when accessing an unavailable buffer due to
the header parser not returning an error code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
8ea14c6ea5 swtpm: Check gerror before calling g_error_free
To avoid glib warning messages check the gerror before calling
g_error_free with it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
04ebf36775 build-sys: Add -Wshadow to the CFLAGS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
82e3f38a82 swtpm_setup: Support default profile from file in swtpm_setup.conf
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
72f2022827 swtpm_setup: Support --profile-file-fd to read profile from file descriptor
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
d5bfdafdc4 swtpm_setup: Support --profile-file <file> to read profile from file
Enable reading a profile from a file. Pass it as open file descriptor to
swtpm.

Adjust one test case to read a profile from a file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
d488dbe7fd swtpm: Support --profile fd=<fd> to read profile from file descriptor
Enable reading a profile from a given file descriptor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
3171cbae02 swtpm: Support --profile file=<filename> to read profile from file
Enable reading a profile from a file.

If setting a profile fails, display the profile in the error message.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
9ef3db088a swtpm: Return error if json_parser_get_root returns NULL
An empty input string will for example cause json_parser_get_root to return
NULL. Return early in this case to avoid glib error messages.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
12f7f004e7 swtpm: check for len > 0 before calling strncmp
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
3d0fb38187 swtpm: Convert json_get_submap to use g_autoptr on variables
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
748e6c0f66 swtpm: Initialize seccomp_action with default value
Initialize seccomp_action with a default value that it will also
get assigned when handle_seccomp_options is called. This is done
to silence a static analyzer even though it's not necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
65c7c72959 swtpm: Implement concat_varrays and fix casts in concat_array calls
Implement concat_varrays that takes gchar ** as input and returns gchar **.
The concat_arrays has the identical implementation but takes const gchar **
as parameters and returns const gchar **. Use concat_arrays from
concat_varrays. Adjust all callers to cast arrays with constant strings to
const gchar *[] when calling concat_arrays. Modify some callers to call
concat_varrays now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
dba5fa616e utils: Implement wrappers for g_spawn_sync and g_spawn_async
Implement warpper for g_spawn_sync and g_spawn_async that that take argv
and envp arguments as 'const gchar **' since they will be called like
this from swtpm_localca and swtpm_setup. Internally glib also uses the
parameters like this when calling fork_exec() for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
402c31df25 swtpm_setup: Initialize key_description with a default string
Initialize key_description with a default string rather than using it
possibly uninitialized. In practice it would never be used uninitialized
since if a platform certificate is to be created, then the ek certificate
would have also been created and therefore swtpm2->ops->create_ek()
would have been called and set key_description.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00