| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bootctl.h" |
| 7 |
#include "bootctl-random-seed.h" |
| 8 |
#include "bootctl-util.h" |
| 9 |
#include "efivars.h" |
| 10 |
#include "env-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "find-esp.h" |
| 13 |
#include "fs-util.h" |
| 14 |
#include "glyph-util.h" |
| 15 |
#include "io-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "random-util.h" |
| 18 |
#include "sha256.h" |
| 19 |
#include "tmpfile-util.h" |
| 20 |
#include "umask-util.h" |
| 21 |
|
| 22 |
static int random_seed_verify_permissions(int fd, mode_t expected_type) { |
| 23 |
_cleanup_free_ char *full_path = NULL; |
| 24 |
struct stat st; |
| 25 |
int r; |
| 26 |
|
| 27 |
assert(fd >= 0); |
| 28 |
|
| 29 |
r = fd_get_path(fd, &full_path); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Unable to determine full path of random seed fd: %m"); |
| 32 |
|
| 33 |
if (fstat(fd, &st) < 0) |
| 34 |
return log_error_errno(errno, "Unable to stat %s: %m", full_path); |
| 35 |
|
| 36 |
if (((st.st_mode ^ expected_type) & S_IFMT) != 0) |
| 37 |
return log_error_errno(SYNTHETIC_ERRNO(EBADF), |
| 38 |
"Unexpected inode type when validating random seed access mode on '%s'.", full_path); |
| 39 |
|
| 40 |
if ((st.st_mode & 0007) == 0) /* All world bits are off? Then all is good */ |
| 41 |
return 0; |
| 42 |
|
| 43 |
if (S_ISREG(expected_type)) |
| 44 |
log_error("%s%sRandom seed file '%s' is world accessible, which is a security hole!%s%s", |
| 45 |
optional_glyph(GLYPH_WARNING_SIGN), optional_glyph(GLYPH_SPACE), |
| 46 |
full_path, |
| 47 |
optional_glyph(GLYPH_SPACE), optional_glyph(GLYPH_WARNING_SIGN)); |
| 48 |
else |
| 49 |
log_error("%s%s Mount point '%s' which backs the random seed file is world accessible, which is a security hole! %s%s", |
| 50 |
optional_glyph(GLYPH_WARNING_SIGN), optional_glyph(GLYPH_SPACE), |
| 51 |
full_path, |
| 52 |
optional_glyph(GLYPH_SPACE), optional_glyph(GLYPH_WARNING_SIGN)); |
| 53 |
|
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
static int set_system_token(void) { |
| 58 |
uint8_t buffer[RANDOM_EFI_SEED_SIZE]; |
| 59 |
size_t token_size; |
| 60 |
int r; |
| 61 |
|
| 62 |
if (!touch_variables()) |
| 63 |
return 0; |
| 64 |
|
| 65 |
r = secure_getenv_bool("SYSTEMD_WRITE_SYSTEM_TOKEN"); |
| 66 |
if (r < 0) { |
| 67 |
if (r != -ENXIO) |
| 68 |
log_warning_errno(r, "Failed to parse $SYSTEMD_WRITE_SYSTEM_TOKEN, ignoring."); |
| 69 |
} else if (r == 0) { |
| 70 |
log_notice("Not writing system token, because $SYSTEMD_WRITE_SYSTEM_TOKEN is set to false."); |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
r = efi_get_variable(EFI_LOADER_VARIABLE_STR("LoaderSystemToken"), NULL, NULL, &token_size); |
| 75 |
if (r == -ENODATA) |
| 76 |
log_debug_errno(r, "LoaderSystemToken EFI variable is invalid (too short?), replacing."); |
| 77 |
else if (r < 0) { |
| 78 |
if (r != -ENOENT) |
| 79 |
return log_error_errno(r, "Failed to test system token validity: %m"); |
| 80 |
} else { |
| 81 |
if (token_size >= sizeof(buffer)) { |
| 82 |
/* Let's avoid writes if we can, and initialize this only once. */ |
| 83 |
log_debug("System token already written, not updating."); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
log_debug("Existing system token size (%zu) does not match our expectations (%zu), replacing.", token_size, sizeof(buffer)); |
| 88 |
} |
| 89 |
|
| 90 |
r = crypto_random_bytes(buffer, sizeof(buffer)); |
| 91 |
if (r < 0) |
| 92 |
return log_error_errno(r, "Failed to acquire random seed: %m"); |
| 93 |
|
| 94 |
/* Let's write this variable with an umask in effect, so that unprivileged users can't see the token |
| 95 |
* and possibly get identification information or too much insight into the kernel's entropy pool |
| 96 |
* state. */ |
| 97 |
WITH_UMASK(0077) { |
| 98 |
r = efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderSystemToken"), buffer, sizeof(buffer)); |
| 99 |
if (r < 0) { |
| 100 |
if (arg_graceful() == ARG_GRACEFUL_NO) |
| 101 |
return log_error_errno(r, "Failed to write 'LoaderSystemToken' EFI variable: %m"); |
| 102 |
|
| 103 |
if (r == -EINVAL) |
| 104 |
log_notice_errno(r, "Unable to write 'LoaderSystemToken' EFI variable (firmware problem?), ignoring: %m"); |
| 105 |
else |
| 106 |
log_notice_errno(r, "Unable to write 'LoaderSystemToken' EFI variable, ignoring: %m"); |
| 107 |
} else |
| 108 |
log_info("Successfully initialized system token in EFI variable with %zu bytes.", sizeof(buffer)); |
| 109 |
} |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int install_random_seed(const char *esp, int esp_fd) { |
| 115 |
_cleanup_close_ int loader_dir_fd = -EBADF, fd = -EBADF; |
| 116 |
_cleanup_free_ char *tmp = NULL; |
| 117 |
uint8_t buffer[RANDOM_EFI_SEED_SIZE]; |
| 118 |
struct sha256_ctx hash_state; |
| 119 |
bool refreshed, warned = false; |
| 120 |
int r; |
| 121 |
|
| 122 |
assert(esp); |
| 123 |
assert(esp_fd >= 0); |
| 124 |
|
| 125 |
assert_cc(RANDOM_EFI_SEED_SIZE == SHA256_DIGEST_SIZE); |
| 126 |
|
| 127 |
if (!arg_install_random_seed) |
| 128 |
return 0; |
| 129 |
|
| 130 |
(void) random_seed_verify_permissions(esp_fd, S_IFDIR); |
| 131 |
|
| 132 |
loader_dir_fd = open_mkdir_at(esp_fd, "loader", O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOFOLLOW, 0775); |
| 133 |
if (loader_dir_fd < 0) |
| 134 |
return log_error_errno(loader_dir_fd, "Failed to open loader directory '%s/loader': %m", esp); |
| 135 |
|
| 136 |
r = crypto_random_bytes(buffer, sizeof(buffer)); |
| 137 |
if (r < 0) |
| 138 |
return log_error_errno(r, "Failed to acquire random seed: %m"); |
| 139 |
|
| 140 |
sha256_init_ctx(&hash_state); |
| 141 |
sha256_process_bytes_and_size(buffer, sizeof(buffer), &hash_state); |
| 142 |
|
| 143 |
fd = openat(loader_dir_fd, "random-seed", O_NOFOLLOW|O_CLOEXEC|O_RDONLY|O_NOCTTY); |
| 144 |
if (fd < 0) { |
| 145 |
if (errno != ENOENT) |
| 146 |
return log_error_errno(errno, "Failed to open old random seed file: %m"); |
| 147 |
|
| 148 |
sha256_process_bytes(&(const ssize_t) { 0 }, sizeof(ssize_t), &hash_state); |
| 149 |
refreshed = false; |
| 150 |
} else { |
| 151 |
ssize_t n; |
| 152 |
|
| 153 |
warned = random_seed_verify_permissions(fd, S_IFREG) > 0; |
| 154 |
|
| 155 |
/* Hash the old seed in so that we never regress in entropy. */ |
| 156 |
|
| 157 |
n = read(fd, buffer, sizeof(buffer)); |
| 158 |
if (n < 0) |
| 159 |
return log_error_errno(errno, "Failed to read old random seed file: %m"); |
| 160 |
|
| 161 |
sha256_process_bytes_and_size(buffer, n, &hash_state); |
| 162 |
|
| 163 |
fd = safe_close(fd); |
| 164 |
refreshed = n > 0; |
| 165 |
} |
| 166 |
|
| 167 |
sha256_finish_ctx(&hash_state, buffer); |
| 168 |
|
| 169 |
if (tempfn_random("random-seed", "bootctl", &tmp) < 0) |
| 170 |
return log_oom(); |
| 171 |
|
| 172 |
fd = openat(loader_dir_fd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|O_WRONLY|O_CLOEXEC, 0600); |
| 173 |
if (fd < 0) |
| 174 |
return log_error_errno(fd, "Failed to open random seed file for writing: %m"); |
| 175 |
|
| 176 |
CLEANUP_TMPFILE_AT(loader_dir_fd, tmp); |
| 177 |
|
| 178 |
if (!warned) /* only warn once per seed file */ |
| 179 |
(void) random_seed_verify_permissions(fd, S_IFREG); |
| 180 |
|
| 181 |
r = loop_write(fd, buffer, sizeof(buffer)); |
| 182 |
if (r < 0) |
| 183 |
return log_error_errno(r, "Failed to write random seed file: %m"); |
| 184 |
|
| 185 |
if (fsync(fd) < 0 || fsync(loader_dir_fd) < 0) |
| 186 |
return log_error_errno(errno, "Failed to sync random seed file: %m"); |
| 187 |
|
| 188 |
if (renameat(loader_dir_fd, tmp, loader_dir_fd, "random-seed") < 0) |
| 189 |
return log_error_errno(errno, "Failed to move random seed file into place: %m"); |
| 190 |
|
| 191 |
tmp = mfree(tmp); /* disarm CLEANUP_TMPFILE_AT() */ |
| 192 |
|
| 193 |
if (syncfs(fd) < 0) |
| 194 |
return log_error_errno(errno, "Failed to sync ESP file system: %m"); |
| 195 |
|
| 196 |
log_info("Random seed file %s/loader/random-seed successfully %s (%zu bytes).", esp, refreshed ? "refreshed" : "written", sizeof(buffer)); |
| 197 |
|
| 198 |
return set_system_token(); |
| 199 |
} |
| 200 |
|
| 201 |
int verb_random_seed(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 202 |
int r; |
| 203 |
|
| 204 |
_cleanup_close_ int esp_fd = -EBADF; |
| 205 |
r = find_esp_and_warn(arg_root, arg_esp_path, /* unprivileged_mode= */ false, &arg_esp_path, &esp_fd); |
| 206 |
if (r == -ENOKEY) { |
| 207 |
/* find_esp_and_warn() doesn't warn about ENOKEY, so let's do that on our own */ |
| 208 |
if (arg_graceful() == ARG_GRACEFUL_NO) |
| 209 |
return log_error_errno(r, "Unable to find ESP."); |
| 210 |
|
| 211 |
log_notice("No ESP found, not initializing random seed."); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
if (r < 0) |
| 215 |
return r; |
| 216 |
|
| 217 |
r = install_random_seed(arg_esp_path, esp_fd); |
| 218 |
if (r < 0) |
| 219 |
return r; |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |