| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "argv-util.h" |
| 8 |
#include "cryptsetup-util.h" |
| 9 |
#include "fileio.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "help-util.h" |
| 12 |
#include "integrity-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "path-util.h" |
| 16 |
#include "string-table.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "time-util.h" |
| 20 |
#include "verbs.h" |
| 21 |
|
| 22 |
static uint32_t arg_activate_flags; |
| 23 |
static int arg_percent; |
| 24 |
static usec_t arg_commit_time; |
| 25 |
static char *arg_existing_data_device; |
| 26 |
static IntegrityAlgorithm arg_integrity_algorithm = _INTEGRITY_ALGORITHM_INVALID; |
| 27 |
|
| 28 |
STATIC_DESTRUCTOR_REGISTER(arg_existing_data_device, freep); |
| 29 |
|
| 30 |
/* Integrity algorithm names used by dm-integrity */ |
| 31 |
static const char* const dm_integrity_algorithm_table[_INTEGRITY_ALGORITHM_MAX] = { |
| 32 |
[INTEGRITY_ALGORITHM_CRC32] = "crc32", |
| 33 |
[INTEGRITY_ALGORITHM_CRC32C] = "crc32c", |
| 34 |
[INTEGRITY_ALGORITHM_XXHASH64] = "xxhash64", |
| 35 |
[INTEGRITY_ALGORITHM_SHA1] = "sha1", |
| 36 |
[INTEGRITY_ALGORITHM_SHA256] = "sha256", |
| 37 |
[INTEGRITY_ALGORITHM_HMAC_SHA256] = "hmac(sha256)", |
| 38 |
[INTEGRITY_ALGORITHM_HMAC_SHA512] = "hmac(sha512)", |
| 39 |
[INTEGRITY_ALGORITHM_PHMAC_SHA256] = "phmac(sha256)", |
| 40 |
[INTEGRITY_ALGORITHM_PHMAC_SHA512] = "phmac(sha512)", |
| 41 |
}; |
| 42 |
|
| 43 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(dm_integrity_algorithm, IntegrityAlgorithm); |
| 44 |
|
| 45 |
static int help(void) { |
| 46 |
_cleanup_(table_unrefp) Table *verbs = NULL; |
| 47 |
int r; |
| 48 |
|
| 49 |
r = verbs_get_help_table(&verbs); |
| 50 |
if (r < 0) |
| 51 |
return r; |
| 52 |
|
| 53 |
help_cmdline("COMMAND ..."); |
| 54 |
help_abstract("Attach or detach an integrity protected block device."); |
| 55 |
|
| 56 |
help_section("Commands"); |
| 57 |
r = table_print_or_warn(verbs); |
| 58 |
if (r < 0) |
| 59 |
return r; |
| 60 |
|
| 61 |
help_man_page_reference("[email protected]", "8"); |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
static int load_key_file( |
| 66 |
const char *key_file, |
| 67 |
void **ret_key_file_contents, |
| 68 |
size_t *ret_key_file_size) { |
| 69 |
int r; |
| 70 |
_cleanup_(erase_and_freep) char *tmp_key_file_contents = NULL; |
| 71 |
size_t tmp_key_file_size; |
| 72 |
|
| 73 |
if (!path_is_absolute(key_file)) |
| 74 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "key file not absolute path: %s", key_file); |
| 75 |
|
| 76 |
r = read_full_file_full( |
| 77 |
AT_FDCWD, key_file, UINT64_MAX, DM_MAX_KEY_SIZE, |
| 78 |
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET|READ_FULL_FILE_FAIL_WHEN_LARGER, |
| 79 |
NULL, |
| 80 |
&tmp_key_file_contents, &tmp_key_file_size); |
| 81 |
if (r < 0) |
| 82 |
return log_error_errno(r, "Failed to process key file: %m"); |
| 83 |
|
| 84 |
if (ret_key_file_contents && ret_key_file_size) { |
| 85 |
*ret_key_file_contents = TAKE_PTR(tmp_key_file_contents); |
| 86 |
*ret_key_file_size = tmp_key_file_size; |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
static const char *integrity_algorithm_select(const void *key_file_buf) { |
| 93 |
IntegrityAlgorithm a = arg_integrity_algorithm >= 0 |
| 94 |
? arg_integrity_algorithm |
| 95 |
: (key_file_buf ? INTEGRITY_ALGORITHM_HMAC_SHA256 : INTEGRITY_ALGORITHM_CRC32C); |
| 96 |
|
| 97 |
return dm_integrity_algorithm_to_string(a); |
| 98 |
} |
| 99 |
|
| 100 |
VERB(verb_attach, "attach", "VOLUME DEVICE [HMAC_KEY_FILE|-] [OPTIONS]", 3, 5, 0, |
| 101 |
"Attach an integrity protected block device"); |
| 102 |
static int verb_attach(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 103 |
_cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
| 104 |
crypt_status_info status; |
| 105 |
_cleanup_(erase_and_freep) void *key_buf = NULL; |
| 106 |
size_t key_buf_size = 0; |
| 107 |
int r; |
| 108 |
|
| 109 |
/* attach name device optional_key_file optional_options */ |
| 110 |
|
| 111 |
assert(argc >= 3 && argc <= 5); |
| 112 |
|
| 113 |
const char *volume = argv[1], |
| 114 |
*device = argv[2], |
| 115 |
*key_file = mangle_none(argc > 3 ? argv[3] : NULL), |
| 116 |
*options = mangle_none(argc > 4 ? argv[4] : NULL); |
| 117 |
|
| 118 |
if (!filename_is_valid(volume)) |
| 119 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); |
| 120 |
|
| 121 |
if (key_file) { |
| 122 |
r = load_key_file(key_file, &key_buf, &key_buf_size); |
| 123 |
if (r < 0) |
| 124 |
return r; |
| 125 |
} |
| 126 |
|
| 127 |
if (options) { |
| 128 |
r = parse_integrity_options(options, &arg_activate_flags, &arg_percent, |
| 129 |
&arg_commit_time, &arg_existing_data_device, &arg_integrity_algorithm); |
| 130 |
if (r < 0) |
| 131 |
return r; |
| 132 |
} |
| 133 |
|
| 134 |
r = sym_crypt_init(&cd, device); |
| 135 |
if (r < 0) |
| 136 |
return log_error_errno(r, "Failed to open integrity device %s: %m", device); |
| 137 |
|
| 138 |
cryptsetup_enable_logging(cd); |
| 139 |
|
| 140 |
status = sym_crypt_status(cd, volume); |
| 141 |
if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { |
| 142 |
log_info("Volume %s already active.", volume); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
r = sym_crypt_load(cd, |
| 147 |
CRYPT_INTEGRITY, |
| 148 |
&(struct crypt_params_integrity) { |
| 149 |
.journal_watermark = arg_percent, |
| 150 |
.journal_commit_time = DIV_ROUND_UP(arg_commit_time, USEC_PER_SEC), |
| 151 |
.integrity = integrity_algorithm_select(key_buf), |
| 152 |
}); |
| 153 |
if (r < 0) |
| 154 |
return log_error_errno(r, "Failed to load integrity superblock: %m"); |
| 155 |
|
| 156 |
if (!isempty(arg_existing_data_device)) { |
| 157 |
r = sym_crypt_set_data_device(cd, arg_existing_data_device); |
| 158 |
if (r < 0) |
| 159 |
return log_error_errno(r, "Failed to add separate data device: %m"); |
| 160 |
} |
| 161 |
|
| 162 |
r = sym_crypt_activate_by_volume_key(cd, volume, key_buf, key_buf_size, arg_activate_flags); |
| 163 |
if (r < 0) |
| 164 |
return log_error_errno(r, "Failed to set up integrity device: %m"); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
VERB(verb_detach, "detach", "VOLUME", 2, 2, 0, |
| 170 |
"Detach an integrity protected block device"); |
| 171 |
static int verb_detach(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 172 |
_cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
| 173 |
int r; |
| 174 |
|
| 175 |
assert(argc == 2); |
| 176 |
|
| 177 |
const char *volume = argv[1]; |
| 178 |
|
| 179 |
if (!filename_is_valid(volume)) |
| 180 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); |
| 181 |
|
| 182 |
r = sym_crypt_init_by_name(&cd, volume); |
| 183 |
if (r == -ENODEV) { |
| 184 |
log_info("Volume %s already inactive.", volume); |
| 185 |
return 0; |
| 186 |
} |
| 187 |
if (r < 0) |
| 188 |
return log_error_errno(r, "crypt_init_by_name() failed: %m"); |
| 189 |
|
| 190 |
cryptsetup_enable_logging(cd); |
| 191 |
|
| 192 |
r = sym_crypt_deactivate(cd, volume); |
| 193 |
if (r < 0) |
| 194 |
return log_error_errno(r, "Failed to deactivate: %m"); |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
static int run(int argc, char *argv[]) { |
| 200 |
int r; |
| 201 |
|
| 202 |
if (argv_looks_like_help(argc, argv)) |
| 203 |
return help(); |
| 204 |
|
| 205 |
log_setup(); |
| 206 |
|
| 207 |
r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 208 |
if (r < 0) |
| 209 |
return r; |
| 210 |
|
| 211 |
umask(0022); |
| 212 |
|
| 213 |
return dispatch_verb(strv_skip(argv, 1), /* userdata= */ NULL); |
| 214 |
} |
| 215 |
|
| 216 |
DEFINE_MAIN_FUNCTION(run); |