Merge pull request #34481 from yuwata/has-tpm2
tpm2-util: several cleanups for tpm2_support()
This commit is contained in:
@@ -667,7 +667,7 @@ static int has_tpm2(void) {
|
||||
*
|
||||
* Note that we don't check if we ourselves are built with TPM2 support here! */
|
||||
|
||||
return FLAGS_SET(tpm2_support(), TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_FIRMWARE);
|
||||
return FLAGS_SET(tpm2_support_full(TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_FIRMWARE), TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_FIRMWARE);
|
||||
}
|
||||
|
||||
static int condition_test_security(Condition *c, char **env) {
|
||||
|
||||
@@ -886,7 +886,7 @@ int encrypt_credential_and_warn(
|
||||
* container tpm2_support will detect this, and will return a different flag combination of
|
||||
* TPM2_SUPPORT_FULL, effectively skipping the use of TPM2 when inside one. */
|
||||
|
||||
try_tpm2 = tpm2_support() == TPM2_SUPPORT_FULL;
|
||||
try_tpm2 = tpm2_is_fully_supported();
|
||||
if (!try_tpm2)
|
||||
log_debug("System lacks TPM2 support or running in a container, not attempting to use TPM2.");
|
||||
} else
|
||||
|
||||
+22
-20
@@ -3,6 +3,7 @@
|
||||
#include <sys/file.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "ansi-color.h"
|
||||
#include "constants.h"
|
||||
#include "creds-util.h"
|
||||
#include "cryptsetup-util.h"
|
||||
@@ -7872,11 +7873,11 @@ int tpm2_sym_mode_from_string(const char *mode) {
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown symmetric mode name '%s'", mode);
|
||||
}
|
||||
|
||||
Tpm2Support tpm2_support(void) {
|
||||
Tpm2Support tpm2_support_full(Tpm2Support mask) {
|
||||
Tpm2Support support = TPM2_SUPPORT_NONE;
|
||||
int r;
|
||||
|
||||
if (detect_container() <= 0) {
|
||||
if (((mask & (TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_DRIVER)) != 0) && detect_container() <= 0) {
|
||||
/* Check if there's a /dev/tpmrm* device via sysfs. If we run in a container we likely just
|
||||
* got the host sysfs mounted. Since devices are generally not virtualized for containers,
|
||||
* let's assume containers never have a TPM, at least for now. */
|
||||
@@ -7893,18 +7894,24 @@ Tpm2Support tpm2_support(void) {
|
||||
support |= TPM2_SUPPORT_SUBSYSTEM;
|
||||
}
|
||||
|
||||
if (efi_has_tpm2())
|
||||
if (FLAGS_SET(mask, TPM2_SUPPORT_FIRMWARE) && efi_has_tpm2())
|
||||
support |= TPM2_SUPPORT_FIRMWARE;
|
||||
|
||||
#if HAVE_TPM2
|
||||
support |= TPM2_SUPPORT_SYSTEM;
|
||||
|
||||
r = dlopen_tpm2();
|
||||
if (r >= 0)
|
||||
support |= TPM2_SUPPORT_LIBRARIES;
|
||||
if (FLAGS_SET(mask, TPM2_SUPPORT_LIBRARIES)) {
|
||||
r = dlopen_tpm2();
|
||||
if (r >= 0)
|
||||
support |= TPM2_SUPPORT_LIBRARIES;
|
||||
}
|
||||
#endif
|
||||
|
||||
return support;
|
||||
return support & mask;
|
||||
}
|
||||
|
||||
static void print_field(const char *s, bool supported) {
|
||||
printf("%s%s%s%s\n", supported ? ansi_green() : ansi_red(), plus_minus(supported), s, ansi_normal());
|
||||
}
|
||||
|
||||
int verb_has_tpm2_generic(bool quiet) {
|
||||
@@ -7914,22 +7921,17 @@ int verb_has_tpm2_generic(bool quiet) {
|
||||
|
||||
if (!quiet) {
|
||||
if (s == TPM2_SUPPORT_FULL)
|
||||
puts("yes");
|
||||
printf("%syes%s\n", ansi_green(), ansi_normal());
|
||||
else if (s == TPM2_SUPPORT_NONE)
|
||||
puts("no");
|
||||
printf("%sno%s\n", ansi_red(), ansi_normal());
|
||||
else
|
||||
puts("partial");
|
||||
printf("%spartial%s\n", ansi_yellow(), ansi_normal());
|
||||
|
||||
printf("%sfirmware\n"
|
||||
"%sdriver\n"
|
||||
"%ssystem\n"
|
||||
"%ssubsystem\n"
|
||||
"%slibraries\n",
|
||||
plus_minus(s & TPM2_SUPPORT_FIRMWARE),
|
||||
plus_minus(s & TPM2_SUPPORT_DRIVER),
|
||||
plus_minus(s & TPM2_SUPPORT_SYSTEM),
|
||||
plus_minus(s & TPM2_SUPPORT_SUBSYSTEM),
|
||||
plus_minus(s & TPM2_SUPPORT_LIBRARIES));
|
||||
print_field("firmware", FLAGS_SET(s, TPM2_SUPPORT_FIRMWARE));
|
||||
print_field("driver", FLAGS_SET(s, TPM2_SUPPORT_DRIVER));
|
||||
print_field("system", FLAGS_SET(s, TPM2_SUPPORT_SYSTEM));
|
||||
print_field("subsystem", FLAGS_SET(s, TPM2_SUPPORT_SUBSYSTEM));
|
||||
print_field("libraries", FLAGS_SET(s, TPM2_SUPPORT_LIBRARIES));
|
||||
}
|
||||
|
||||
/* Return inverted bit flags. So that TPM2_SUPPORT_FULL becomes EXIT_SUCCESS and the other values
|
||||
|
||||
@@ -450,8 +450,8 @@ typedef struct {
|
||||
} systemd_tpm2_plugin_params;
|
||||
|
||||
typedef enum Tpm2Support {
|
||||
/* NOTE! The systemd-creds tool returns these flags 1:1 as exit status. Hence these flags are pretty
|
||||
* much ABI! Hence, be extra careful when changing/extending these definitions. */
|
||||
/* NOTE! The systemd-analyze has-tpm2 command returns these flags 1:1 as exit status. Hence these
|
||||
* flags are pretty much ABI! Hence, be extra careful when changing/extending these definitions. */
|
||||
TPM2_SUPPORT_NONE = 0, /* no support */
|
||||
TPM2_SUPPORT_FIRMWARE = 1 << 0, /* firmware reports TPM2 was used */
|
||||
TPM2_SUPPORT_DRIVER = 1 << 1, /* the kernel has a driver loaded for it */
|
||||
@@ -461,7 +461,13 @@ typedef enum Tpm2Support {
|
||||
TPM2_SUPPORT_FULL = TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER|TPM2_SUPPORT_SYSTEM|TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_LIBRARIES,
|
||||
} Tpm2Support;
|
||||
|
||||
Tpm2Support tpm2_support(void);
|
||||
Tpm2Support tpm2_support_full(Tpm2Support mask);
|
||||
static inline Tpm2Support tpm2_support(void) {
|
||||
return tpm2_support_full(TPM2_SUPPORT_FULL);
|
||||
}
|
||||
static inline bool tpm2_is_fully_supported(void) {
|
||||
return tpm2_support() == TPM2_SUPPORT_FULL;
|
||||
}
|
||||
|
||||
int verb_has_tpm2_generic(bool quiet);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user