Debian Codegraph @

src/tpm2-setup/tpm2-clear.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-messages.h"
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "env-util.h"
8 #include "fileio.h"
9 #include "format-table.h"
10 #include "log.h"
11 #include "main-func.h"
12 #include "options.h"
13 #include "pretty-print.h"
14 #include "proc-cmdline.h"
15 #include "tpm2-util.h"
16
17 static bool arg_graceful = false;
18
19 static int help(void) {
20 _cleanup_free_ char *link = NULL;
21 _cleanup_(table_unrefp) Table *options = NULL;
22 int r;
23
24 r = terminal_urlify_man("systemd-tpm2-clear", "8", &link);
25 if (r < 0)
26 return log_oom();
27
28 r = option_parser_get_help_table(&options);
29 if (r < 0)
30 return r;
31
32 printf("%s [OPTIONS...]\n"
33 "\n%sRequest clearing of the TPM2 from PC firmware.%s\n"
34 "\n%sOptions:%s\n",
35 program_invocation_short_name,
36 ansi_highlight(),
37 ansi_normal(),
38 ansi_underline(),
39 ansi_normal());
40
41 r = table_print_or_warn(options);
42 if (r < 0)
43 return r;
44
45 printf("\nSee the %s for details.\n", link);
46 return 0;
47 }
48
49 static int parse_argv(int argc, char *argv[]) {
50 assert(argc >= 0);
51 assert(argv);
52
53 OptionParser opts = { argc, argv };
54
55 FOREACH_OPTION_OR_RETURN(c, &opts)
56 switch (c) {
57
58 OPTION_COMMON_HELP:
59 return help();
60
61 OPTION_COMMON_VERSION:
62 return version();
63
64 OPTION_LONG("graceful", NULL, "Exit gracefully if no TPM2 device is found"):
65 arg_graceful = true;
66 break;
67 }
68
69 if (option_parser_get_n_args(&opts) != 0)
70 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program expects no arguments.");
71
72 return 1;
73 }
74
75 static int request_tpm2_clear(void) {
76 int r, clear = -1;
77
78 r = secure_getenv_bool("SYSTEMD_TPM2_ALLOW_CLEAR");
79 if (r < 0 && r != -ENXIO)
80 return log_error_errno(r, "Failed to parse $SYSTEMD_TPM2_ALLOW_CLEAR: %m");
81 if (r >= 0)
82 clear = r;
83
84 if (clear < 0) {
85 bool b;
86 r = proc_cmdline_get_bool("systemd.tpm2_allow_clear", PROC_CMDLINE_TRUE_WHEN_MISSING, &b);
87 if (r < 0)
88 return log_error_errno(r, "Failed to parse systemd.tpm2_allow_clear kernel command line argument: %m");
89 clear = b;
90 }
91
92 assert(clear >= 0);
93
94 if (!clear) {
95 log_info("Clearing TPM2 disabled, exiting early.");
96 return 0;
97 }
98
99 /* Now issue PPI request */
100 r = write_string_file("/sys/class/tpm/tpm0/ppi/request", "5", /* flags= */ 0);
101 if (r < 0)
102 return log_error_errno(r, "Failed to request TPM2 clearing via PPI, unable to write to /sys/class/tpm/tpm0/ppi/request: %m");
103
104 log_struct(LOG_NOTICE,
105 LOG_MESSAGE_ID(SD_MESSAGE_TPM2_CLEAR_REQUESTED_STR),
106 LOG_MESSAGE("Requested TPM2 clearing via PPI. Firmware will verify with user and clear TPM on reboot."));
107 return 0;
108 }
109
110 static int run(int argc, char *argv[]) {
111 int r;
112
113 log_setup();
114
115 r = parse_argv(argc, argv);
116 if (r <= 0)
117 return r;
118
119 /* If we don't fully support the TPM we are unlikely able to reinitialize it after boot, hence don't
120 * be tempted to reset it in graceful mode. Otherwise we might destroy something without being able
121 * to rebuild it. */
122 if (arg_graceful && !tpm2_is_fully_supported()) {
123 log_notice("No complete TPM2 support detected, exiting gracefully.");
124 return 0;
125 }
126
127 return request_tpm2_clear();
128 }
129
130 DEFINE_MAIN_FUNCTION(run);