Debian Codegraph @

src/core/ipe-setup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "constants.h"
8 #include "copy.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "ipe-setup.h"
12 #include "log.h"
13 #include "path-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16
17 #define IPE_SECFS_DIR "/sys/kernel/security/ipe"
18 #define IPE_SECFS_NEW_POLICY IPE_SECFS_DIR "/new_policy"
19 #define IPE_SECFS_POLICIES IPE_SECFS_DIR "/policies/"
20
21 int ipe_setup(void) {
22 #if ENABLE_IPE
23 _cleanup_strv_free_ char **policies = NULL;
24 int r;
25
26 /* Very quick smoke tests first: this is in the critical, sequential boot path, and in most cases it
27 * is unlikely this will be configured, so do the fastest existence checks first and immediately
28 * return if there's nothing to do. */
29
30 if (access(IPE_SECFS_DIR, F_OK) < 0) {
31 log_debug_errno(errno, "IPE support is disabled in the kernel, ignoring: %m");
32 return 0;
33 }
34
35 r = conf_files_list_nulstr(
36 &policies,
37 ".p7b",
38 /* root= */ NULL,
39 CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN,
40 CONF_PATHS_NULSTR("ipe"));
41 if (r < 0)
42 return log_error_errno(r, "Failed to assemble list of IPE policies: %m");
43
44 STRV_FOREACH(policy, policies) {
45 _cleanup_free_ char *policy_name = NULL, *file_name = NULL, *output_path = NULL, *activate_path = NULL;
46 _cleanup_close_ int input = -EBADF, output = -EBADF;
47 const char *suffix;
48
49 r = path_extract_filename(*policy, &file_name);
50 if (r < 0)
51 return log_error_errno(r, "Failed to extract filename from IPE policy path %s: %m", *policy);
52
53 /* Filtered by conf_files_list_nulstr() */
54 suffix = ASSERT_PTR(endswith(file_name, ".p7b"));
55
56 policy_name = strndup(file_name, suffix - file_name);
57 if (!policy_name)
58 return log_oom();
59
60 if (!filename_is_valid(policy_name))
61 return log_error_errno(
62 SYNTHETIC_ERRNO(EINVAL),
63 "Invalid IPE policy name %s",
64 policy_name);
65
66 input = open(*policy, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
67 if (input < 0)
68 return log_error_errno(
69 errno,
70 "Failed to open the IPE policy file %s: %m",
71 *policy);
72
73 /* If policy is already installed, try to update it */
74 output_path = path_join(IPE_SECFS_POLICIES, policy_name, "update");
75 if (!output_path)
76 return log_oom();
77
78 output = open(output_path, O_WRONLY|O_CLOEXEC);
79 if (output < 0 && errno == ENOENT)
80 /* Policy is not installed, install it and activate it */
81 output = open(IPE_SECFS_NEW_POLICY, O_WRONLY|O_CLOEXEC);
82 if (output < 0)
83 return log_error_errno(
84 errno,
85 "Failed to open the IPE policy handle for writing: %m");
86
87 /* The policy is inline signed in binary format, so it has to be copied in one go, otherwise the
88 * kernel will reject partial inputs with -EBADMSG. */
89 r = copy_bytes(input, output, UINT64_MAX, /* copy_flags= */ 0);
90 if (r < 0)
91 return log_error_errno(
92 r,
93 "Failed to copy the IPE policy %s to %s: %m",
94 *policy,
95 output_path);
96
97 output = safe_close(output);
98
99 activate_path = path_join(IPE_SECFS_POLICIES, policy_name, "active");
100 if (!activate_path)
101 return log_oom();
102
103 r = write_string_file(activate_path, "1", WRITE_STRING_FILE_DISABLE_BUFFER);
104 if (r == -ESTALE) {
105 log_debug_errno(r,
106 "IPE policy %s is already loaded with a version that is equal or higher, skipping.",
107 policy_name);
108 continue;
109 }
110 if (r < 0)
111 return log_error_errno(r, "Failed to activate the IPE policy %s: %m", policy_name);
112
113 log_info("Successfully loaded and activated the IPE policy %s.", policy_name);
114 }
115
116 #endif /* ENABLE_IPE */
117 return 0;
118 }