HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/cryptsetup/cryptsetup.c
Git blob: 425fc7be32b5b7f2d380a78cfecc8640727d6b68
Lines 1–1000 of 2,795
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sd-device.h"
#include "sd-event.h"
#include "sd-json.h"
#include "alloc-util.h"
#include "ask-password-api.h"
#include "build.h"
#include "cryptsetup-fido2.h"
#include "cryptsetup-keyfile.h"
#include "cryptsetup-pkcs11.h"
#include "cryptsetup-tpm2.h"
#include "cryptsetup-util.h"
#include "dlopen-note.h"
#include "efi-api.h"
#include "efi-loader.h"
#include "efivars.h"
#include "env-util.h"
#include "errno-util.h"
#include "escape.h"
#include "extract-word.h"
#include "fileio.h"
#include "fs-util.h"
#include "hexdecoct.h"
#include "json-util.h"
#include "libfido2-util.h"
#include "libmount-util.h"
#include "log.h"
#include "main-func.h"
#include "memory-util.h"
#include "nulstr-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "pcrextend-util.h"
#include "pkcs11-util.h"
#include "process-util.h"
#include "random-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "tpm2-pcr.h"
#include "tpm2-util.h"
#include "verbs.h"
/* internal helper */
#define ANY_LUKS "LUKS"
/* as in src/cryptsetup.h */
#define CRYPT_SECTOR_SIZE 512U
#define CRYPT_MAX_SECTOR_SIZE 4096U
typedef enum PassphraseType {
PASSPHRASE_NONE,
PASSPHRASE_REGULAR = 1 << 0,
PASSPHRASE_RECOVERY_KEY = 1 << 1,
PASSPHRASE_BOTH = PASSPHRASE_REGULAR|PASSPHRASE_RECOVERY_KEY,
_PASSPHRASE_TYPE_MAX,
_PASSPHRASE_TYPE_INVALID = -1,
} PassphraseType;
typedef enum TokenType {
TOKEN_TPM2,
TOKEN_FIDO2,
TOKEN_PKCS11,
_TOKEN_TYPE_MAX,
_TOKEN_TYPE_INVALID = -EINVAL,
} TokenType;
static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */
static char *arg_cipher = NULL;
static unsigned arg_key_size = 0;
static unsigned arg_sector_size = CRYPT_SECTOR_SIZE;
static int arg_key_slot = CRYPT_ANY_SLOT;
static unsigned arg_keyfile_size = 0;
static uint64_t arg_keyfile_offset = 0;
static bool arg_keyfile_erase = false;
static bool arg_try_empty_password = false;
static char *arg_hash = NULL;
static char *arg_header = NULL;
static unsigned arg_tries = 3;
static bool arg_readonly = false;
static bool arg_verify = false;
static bool arg_password_cache_set = false; /* Not the actual argument value, just an indicator that some value is set */
static AskPasswordFlags arg_ask_password_flags = ASK_PASSWORD_ACCEPT_CACHED | ASK_PASSWORD_PUSH_CACHE;
static bool arg_discards = false;
static bool arg_same_cpu_crypt = false;
static bool arg_submit_from_crypt_cpus = false;
static bool arg_no_read_workqueue = false;
static bool arg_no_write_workqueue = false;
static bool arg_tcrypt_hidden = false;
static bool arg_tcrypt_system = false;
static bool arg_tcrypt_veracrypt = false;
static uint32_t arg_tcrypt_veracrypt_pim = 0;
static char **arg_tcrypt_keyfiles = NULL;
static uint64_t arg_offset = 0;
static uint64_t arg_skip = 0;
static usec_t arg_timeout = USEC_INFINITY;
static char *arg_pkcs11_uri = NULL;
static bool arg_pkcs11_uri_auto = false;
static char *arg_fido2_device = NULL;
static bool arg_fido2_device_auto = false;
static void *arg_fido2_cid = NULL;
static size_t arg_fido2_cid_size = 0;
static char *arg_fido2_rp_id = NULL;
/* For now and for compatibility, if the user explicitly configured FIDO2 support and we do
* not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we
* use PIN + UP when needed, and do not configure UV at all. */
static Fido2EnrollFlags arg_fido2_manual_flags = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT;
static char *arg_tpm2_device = NULL; /* These and the following fields are about locking an encrypted volume to the local TPM */
static bool arg_tpm2_device_auto = false;
static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
static char *arg_tpm2_signature = NULL;
static bool arg_tpm2_pin = false;
static char *arg_tpm2_pcrlock = NULL;
static usec_t arg_token_timeout_usec = 30*USEC_PER_SEC;
static unsigned arg_tpm2_measure_pcr = UINT_MAX; /* This and the following field is about measuring the unlocked volume key to the local TPM */
static char *arg_tpm2_measure_keyslot_nvpcr = NULL;
static char *arg_link_keyring = NULL;
static char *arg_link_key_type = NULL;
static char *arg_link_key_description = NULL;
static char *arg_fixate_volume_key = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep);
STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep);
STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep);
STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_keyslot_nvpcr, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep);
STATIC_DESTRUCTOR_REGISTER(arg_link_keyring, freep);
STATIC_DESTRUCTOR_REGISTER(arg_link_key_type, freep);
STATIC_DESTRUCTOR_REGISTER(arg_link_key_description, freep);
STATIC_DESTRUCTOR_REGISTER(arg_fixate_volume_key, freep);
COMMAND(
"systemd-cryptsetup\0",
"Attach or detach an encrypted block device.",
.man_pages = "systemd-cryptsetup(8)\0",
);
static const char* const passphrase_type_table[_PASSPHRASE_TYPE_MAX] = {
[PASSPHRASE_REGULAR] = "passphrase",
[PASSPHRASE_RECOVERY_KEY] = "recovery key",
[PASSPHRASE_BOTH] = "passphrase or recovery key",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(passphrase_type, PassphraseType);
static const char* const token_type_table[_TOKEN_TYPE_MAX] = {
[TOKEN_TPM2] = "tpm2",
[TOKEN_FIDO2] = "fido2",
[TOKEN_PKCS11] = "pkcs11",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(token_type, TokenType);
/* Options Debian's crypttab knows we don't:
check=
checkargs=
noearly
loud
quiet
keyscript=
initramfs
*/
static int parse_one_option(const char *option) {
const char *val;
int r;
assert(option);
/* Handled outside of this tool */
if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
return 0;
if (startswith(option, "keyfile-timeout="))
return 0;
if ((val = startswith(option, "cipher="))) {
r = free_and_strdup(&arg_cipher, val);
if (r < 0)
return log_oom();
} else if ((val = startswith(option, "size="))) {
unsigned key_size;
r = safe_atou(val, &key_size);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
if (key_size % 8) {
log_warning("size= not a multiple of 8, ignoring.");
return 0;
}
arg_key_size = key_size / 8;
} else if ((val = startswith(option, "sector-size="))) {
unsigned sector_size;
r = safe_atou(val, §or_size);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
if (!ISPOWEROF2(sector_size)) {
log_warning("sector-size= not a power of 2, ignoring.");
return 0;
}
if (sector_size < CRYPT_SECTOR_SIZE || sector_size > CRYPT_MAX_SECTOR_SIZE) {
log_warning("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE);
return 0;
}
arg_sector_size = sector_size;
} else if ((val = startswith(option, "key-slot=")) ||
(val = startswith(option, "keyslot="))) {
arg_type = ANY_LUKS;
r = safe_atoi(val, &arg_key_slot);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if ((val = startswith(option, "tcrypt-keyfile="))) {
arg_type = CRYPT_TCRYPT;
if (path_is_absolute(val)) {
if (strv_extend(&arg_tcrypt_keyfiles, val) < 0)
return log_oom();
} else
log_warning("Key file path \"%s\" is not absolute, ignoring.", val);
} else if ((val = startswith(option, "keyfile-size="))) {
r = safe_atou(val, &arg_keyfile_size);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if ((val = startswith(option, "keyfile-offset="))) {
r = safe_atou64(val, &arg_keyfile_offset);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if ((val = startswith(option, "keyfile-erase="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_keyfile_erase = r;
} else if (streq(option, "keyfile-erase"))
arg_keyfile_erase = true;
else if ((val = startswith(option, "hash="))) {
r = free_and_strdup(&arg_hash, val);
if (r < 0)
return log_oom();
} else if ((val = startswith(option, "header="))) {
if (!arg_type || !STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT))
arg_type = ANY_LUKS;
if (!path_is_absolute(val))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Header path \"%s\" is not absolute, refusing.", val);
if (arg_header)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Duplicate header= option, refusing.");
arg_header = strdup(val);
if (!arg_header)
return log_oom();
} else if ((val = startswith(option, "tries="))) {
r = safe_atou(val, &arg_tries);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if (STR_IN_SET(option, "readonly", "read-only"))
arg_readonly = true;
else if (streq(option, "verify"))
arg_verify = true;
else if ((val = startswith(option, "password-echo="))) {
if (streq(val, "masked"))
arg_ask_password_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT);
else {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Invalid password-echo= option \"%s\", ignoring.", val);
return 0;
}
SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r);
SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r);
}
} else if ((val = startswith(option, "password-cache="))) {
if (streq(val, "read-only")) {
arg_ask_password_flags |= ASK_PASSWORD_ACCEPT_CACHED;
arg_ask_password_flags &= ~ASK_PASSWORD_PUSH_CACHE;
} else {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Invalid password-cache= option \"%s\", ignoring.", val);
return 0;
}
SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, r);
}
arg_password_cache_set = true;
} else if (STR_IN_SET(option, "allow-discards", "discard"))
arg_discards = true;
else if (streq(option, "same-cpu-crypt"))
arg_same_cpu_crypt = true;
else if (streq(option, "submit-from-crypt-cpus"))
arg_submit_from_crypt_cpus = true;
else if (streq(option, "no-read-workqueue"))
arg_no_read_workqueue = true;
else if (streq(option, "no-write-workqueue"))
arg_no_write_workqueue = true;
else if (streq(option, "luks"))
arg_type = ANY_LUKS;
else if (streq(option, "bitlk"))
arg_type = CRYPT_BITLK;
else if (streq(option, "tcrypt"))
arg_type = CRYPT_TCRYPT;
else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) {
arg_type = CRYPT_TCRYPT;
arg_tcrypt_hidden = true;
} else if (streq(option, "tcrypt-system")) {
arg_type = CRYPT_TCRYPT;
arg_tcrypt_system = true;
} else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) {
arg_type = CRYPT_TCRYPT;
arg_tcrypt_veracrypt = true;
} else if ((val = startswith(option, "veracrypt-pim="))) {
r = safe_atou32(val, &arg_tcrypt_veracrypt_pim);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
} else if (STR_IN_SET(option, "plain", "swap", "tmp") ||
startswith(option, "tmp="))
arg_type = CRYPT_PLAIN;
else if ((val = startswith(option, "timeout="))) {
r = parse_sec_fix_0(val, &arg_timeout);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if ((val = startswith(option, "offset="))) {
r = safe_atou64(val, &arg_offset);
if (r < 0)
return log_error_errno(r, "Failed to parse %s: %m", option);
} else if ((val = startswith(option, "skip="))) {
r = safe_atou64(val, &arg_skip);
if (r < 0)
return log_error_errno(r, "Failed to parse %s: %m", option);
} else if ((val = startswith(option, "pkcs11-uri="))) {
if (streq(val, "auto")) {
arg_pkcs11_uri = mfree(arg_pkcs11_uri);
arg_pkcs11_uri_auto = true;
} else {
if (!pkcs11_uri_valid(val))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing.");
r = free_and_strdup(&arg_pkcs11_uri, val);
if (r < 0)
return log_oom();
arg_pkcs11_uri_auto = false;
}
} else if ((val = startswith(option, "fido2-device="))) {
if (streq(val, "auto")) {
arg_fido2_device = mfree(arg_fido2_device);
arg_fido2_device_auto = true;
} else {
r = free_and_strdup(&arg_fido2_device, val);
if (r < 0)
return log_oom();
arg_fido2_device_auto = false;
}
} else if ((val = startswith(option, "fido2-cid="))) {
if (streq(val, "auto"))
arg_fido2_cid = mfree(arg_fido2_cid);
else {
_cleanup_free_ void *cid = NULL;
size_t cid_size;
r = unbase64mem(val, &cid, &cid_size);
if (r < 0)
return log_error_errno(r, "Failed to decode FIDO2 CID data: %m");
free(arg_fido2_cid);
arg_fido2_cid = TAKE_PTR(cid);
arg_fido2_cid_size = cid_size;
}
/* Turn on FIDO2 as side-effect, if not turned on yet. */
if (!arg_fido2_device && !arg_fido2_device_auto)
arg_fido2_device_auto = true;
} else if ((val = startswith(option, "fido2-rp="))) {
r = free_and_strdup(&arg_fido2_rp_id, val);
if (r < 0)
return log_oom();
} else if ((val = startswith(option, "fido2-pin="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_fido2_manual_flags &= ~FIDO2ENROLL_PIN_IF_NEEDED;
SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_PIN, r);
} else if ((val = startswith(option, "fido2-up="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_fido2_manual_flags &= ~FIDO2ENROLL_UP_IF_NEEDED;
SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UP, r);
} else if ((val = startswith(option, "fido2-uv="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_fido2_manual_flags &= ~FIDO2ENROLL_UV_OMIT;
SET_FLAG(arg_fido2_manual_flags, FIDO2ENROLL_UV, r);
} else if ((val = startswith(option, "tpm2-device="))) {
if (streq(val, "auto")) {
arg_tpm2_device = mfree(arg_tpm2_device);
arg_tpm2_device_auto = true;
} else {
r = free_and_strdup(&arg_tpm2_device, val);
if (r < 0)
return log_oom();
arg_tpm2_device_auto = false;
}
} else if ((val = startswith(option, "tpm2-pcrs="))) {
r = tpm2_parse_pcr_argument_to_mask(val, &arg_tpm2_pcr_mask);
if (r < 0)
return r;
} else if ((val = startswith(option, "tpm2-signature="))) {
if (!path_is_absolute(val))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"TPM2 signature path \"%s\" is not absolute, refusing.", val);
r = free_and_strdup(&arg_tpm2_signature, val);
if (r < 0)
return log_oom();
} else if ((val = startswith(option, "tpm2-pin="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_tpm2_pin = r;
} else if ((val = startswith(option, "tpm2-pcrlock="))) {
if (!path_is_absolute(val))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"TPM2 pcrlock policy path \"%s\" is not absolute, refusing.", val);
r = free_and_strdup(&arg_tpm2_pcrlock, val);
if (r < 0)
return log_oom();
} else if ((val = startswith(option, "tpm2-measure-pcr="))) {
unsigned pcr;
r = safe_atou(val, &pcr);
if (r < 0) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
pcr = r ? TPM2_PCR_SYSTEM_IDENTITY : UINT_MAX;
} else if (!TPM2_PCR_INDEX_VALID(pcr)) {
log_warning("Selected TPM index for measurement %u outside of allowed range 0…%u, ignoring.", pcr, TPM2_PCRS_MAX-1);
return 0;
}
arg_tpm2_measure_pcr = pcr;
} else if ((val = startswith(option, "tpm2-measure-bank="))) {
/* Deprecated: the PCR banks to measure into are now chosen by systemd-pcrextend
* (it extends all suitable banks). Kept for compatibility, but ignored. */
log_warning("The tpm2-measure-bank= option is deprecated and has no effect, ignoring.");
} else if ((val = startswith(option, "tpm2-measure-keyslot-nvpcr="))) {
r = isempty(val) ? false : parse_boolean(val);
if (r == 0) {
arg_tpm2_measure_keyslot_nvpcr = mfree(arg_tpm2_measure_keyslot_nvpcr);
return 0;
}
if (r > 0)
val = "cryptsetup";
else if (!tpm2_nvpcr_name_is_valid(val)) {
log_warning("Invalid NvPCR name, ignoring: %s", option);
return 0;
}
if (free_and_strdup(&arg_tpm2_measure_keyslot_nvpcr, val) < 0)
return log_oom();
} else if ((val = startswith(option, "try-empty-password="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
arg_try_empty_password = r;
} else if (streq(option, "try-empty-password"))
arg_try_empty_password = true;
else if ((val = startswith(option, "headless="))) {
r = parse_boolean(val);
if (r < 0) {
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
return 0;
}
SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_HEADLESS, r);
} else if (streq(option, "headless"))
arg_ask_password_flags |= ASK_PASSWORD_HEADLESS;
else if ((val = startswith(option, "token-timeout="))) {
r = parse_sec_fix_0(val, &arg_token_timeout_usec);
if (r < 0)
log_warning_errno(r, "Failed to parse %s, ignoring: %m", option);
} else if ((val = startswith(option, "link-volume-key="))) {
_cleanup_free_ char *keyring = NULL, *key_type = NULL, *key_description = NULL;
const char *sep;
/* Stick with cryptsetup --link-vk-to-keyring format
* <keyring_description>::%<key_type>:<key_description>,
* where %<key_type> is optional and defaults to 'user'.
*/
sep = strstr(val, "::");
if (!sep)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
/* cryptsetup (cli) supports <keyring_description> passed in various formats:
* - well-known keyrings prefixed with '@' (@u user, @s session, etc)
* - text descriptions prefixed with "%:" or "%keyring:".
* - text description with no prefix.
* - numeric keyring id (ignored in current patch set). */
keyring = strndup(val, sep - val);
if (!keyring)
return log_oom();
/* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
if (!IN_SET(*keyring, '@', '%'))
if (!strprepend(&keyring, "%:"))
return log_oom();
sep += 2;
/* %<key_type> is optional (and defaults to 'user') */
if (*sep == '%') {
/* must be separated by colon */
const char *c = strchr(sep, ':');
if (!c)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse link-volume-key= option value: %s", val);
key_type = strndup(sep + 1, c - sep - 1);
if (!key_type)
return log_oom();
sep = c + 1;
}
key_description = strdup(sep);
if (!key_description)
return log_oom();
free_and_replace(arg_link_keyring, keyring);
free_and_replace(arg_link_key_type, key_type);
free_and_replace(arg_link_key_description, key_description);
} else if ((val = startswith(option, "fixate-volume-key="))) {
r = free_and_strdup(&arg_fixate_volume_key, val);
if (r < 0)
return log_oom();
} else if (!streq(option, "x-initrd.attach"))
log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option);
return 0;
}
static int parse_crypt_config(const char *options) {
assert(options);
for (;;) {
_cleanup_free_ char *word = NULL;
int r;
r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS);
if (r < 0)
return log_error_errno(r, "Failed to parse options: %m");
if (r == 0)
break;
r = parse_one_option(word);
if (r < 0)
return r;
}
/* sanity-check options */
if (arg_type && !streq(arg_type, CRYPT_PLAIN)) {
if (arg_offset != 0)
log_warning("offset= ignored with type %s", arg_type);
if (arg_skip != 0)
log_warning("skip= ignored with type %s", arg_type);
}
if (arg_pkcs11_uri || arg_pkcs11_uri_auto) {
/* If password-cache was not configured explicitly, default to no cache for PKCS#11 */
if (!arg_password_cache_set)
arg_ask_password_flags &= ~(ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE);
/* This prevents future backward-compatibility issues if we decide to allow caching for PKCS#11 */
if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_ACCEPT_CACHED))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Password cache is not supported for PKCS#11 security tokens.");
}
return 0;
}
static char* disk_description(const char *path) {
static const char name_fields[] =
"DM_NAME\0"
"ID_MODEL_FROM_DATABASE\0"
"ID_MODEL\0";
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *name;
struct stat st;
assert(path);
if (stat(path, &st) < 0)
return NULL;
if (!S_ISBLK(st.st_mode))
return NULL;
if (sd_device_new_from_stat_rdev(&device, &st) < 0)
return NULL;
if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) {
_cleanup_free_ char *unescaped = NULL;
ssize_t l;
/* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
* reverse this here to make the string more human friendly in case people embed spaces or
* other weird stuff. */
l = cunescape(name, UNESCAPE_RELAX, &unescaped);
if (l < 0) {
log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
return NULL;
}
if (!isempty(unescaped) && !string_has_cc(unescaped, NULL))
return TAKE_PTR(unescaped);
}
/* These need no unescaping. */
NULSTR_FOREACH(i, name_fields)
if (sd_device_get_property_value(device, i, &name) >= 0 &&
!isempty(name))
return strdup(name);
return NULL;
}
static char* disk_mount_point(const char *label) {
_cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
_cleanup_free_ char *device = NULL;
int r;
/* Yeah, we don't support native systemd unit files here for now */
assert(label);
device = strjoin("/dev/mapper/", label);
if (!device)
return NULL;
r = libmount_parse_fstab(&table, &iter);
if (r < 0)
return NULL;
for (;;) {
struct libmnt_fs *fs;
r = sym_mnt_table_next_fs(table, iter, &fs);
if (r != 0)
return NULL;
if (path_equal(sym_mnt_fs_get_source(fs), device)) {
const char *target = sym_mnt_fs_get_target(fs);
if (target)
return strdup(target);
}
}
}
static char *friendly_disk_name(const char *src, const char *vol) {
_cleanup_free_ char *description = NULL, *mount_point = NULL;
char *name_buffer = NULL;
int r;
assert(src);
assert(vol);
description = disk_description(src);
mount_point = disk_mount_point(vol);
/* If the description string is simply the volume name, then let's not show this twice */
if (description && streq(vol, description))
description = mfree(description);
if (mount_point && description)
r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point);
else if (mount_point)
r = asprintf(&name_buffer, "%s on %s", vol, mount_point);
else if (description)
r = asprintf(&name_buffer, "%s (%s)", description, vol);
else
return strdup(vol);
if (r < 0)
return NULL;
return name_buffer;
}
static PassphraseType check_registered_passwords(struct crypt_device *cd) {
_cleanup_free_ bool *slots = NULL;
int slot_max;
PassphraseType passphrase_type = PASSPHRASE_NONE;
assert(cd);
if (!streq_ptr(sym_crypt_get_type(cd), CRYPT_LUKS2)) {
log_debug("%s: not a LUKS2 device, only passphrases are supported", sym_crypt_get_device_name(cd));
return PASSPHRASE_REGULAR;
}
/* Search all used slots */
assert_se((slot_max = sym_crypt_keyslot_max(CRYPT_LUKS2)) > 0);
slots = new(bool, slot_max);
if (!slots)
return log_oom();
for (int slot = 0; slot < slot_max; slot++)
slots[slot] = IN_SET(sym_crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST);
/* Iterate all LUKS2 tokens and keep track of all their slots */
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
const char *type;
sd_json_variant *w, *z;
int tk;
tk = cryptsetup_get_token_as_json(cd, token, NULL, &v);
if (IN_SET(tk, -ENOENT, -EINVAL))
continue;
if (tk < 0) {
log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m");
continue;
}
w = sd_json_variant_by_key(v, "type");
if (!w || !sd_json_variant_is_string(w)) {
log_warning("Token JSON data lacks type field, ignoring.");
continue;
}
type = sd_json_variant_string(w);
if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
/* At least exists one recovery key */
if (streq(type, "systemd-recovery"))
passphrase_type |= PASSPHRASE_RECOVERY_KEY;
w = sd_json_variant_by_key(v, "keyslots");
if (!w || !sd_json_variant_is_array(w)) {
log_warning("Token JSON data lacks keyslots field, ignoring.");
continue;
}
JSON_VARIANT_ARRAY_FOREACH(z, w) {
unsigned u;
int at;
if (!sd_json_variant_is_string(z)) {
log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
continue;
}
at = safe_atou(sd_json_variant_string(z), &u);
if (at < 0) {
log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
continue;
}
if (u >= (unsigned) slot_max) {
log_warning("Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
continue;
}
slots[u] = false;
}
}
}
/* Check if any of the slots is not referenced by systemd tokens */
for (int slot = 0; slot < slot_max; slot++)
if (slots[slot]) {
passphrase_type |= PASSPHRASE_REGULAR;
break;
}
/* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
* we will not be able to enter a passphrase. */
return passphrase_type;
}
static int get_password(
const char *vol,
const char *src,
usec_t until,
bool ignore_cached,
PassphraseType passphrase_type,
char ***ret) {
_cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL, *id = NULL;
_cleanup_strv_free_erase_ char **passwords = NULL;
AskPasswordFlags flags = arg_ask_password_flags;
int r;
assert(vol);
assert(src);
assert(ret);
if (FLAGS_SET(arg_ask_password_flags, ASK_PASSWORD_HEADLESS))
return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option.");
friendly = friendly_disk_name(src, vol);
if (!friendly)
return log_oom();
if (asprintf(&text, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type), friendly) < 0)
return log_oom();
disk_path = cescape(src);
if (!disk_path)
return log_oom();
id = strjoin("cryptsetup:", disk_path);
if (!id)
return log_oom();
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = text,
.icon = "drive-harddisk",
.id = id,
.keyring = "cryptsetup",
.credential = "cryptsetup.passphrase",
.until = until,
.hup_fd = -EBADF,
};
if (ignore_cached)
flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
r = ask_password_auto(&req, flags, &passwords);
if (r < 0)
return log_error_errno(r, "Failed to query password: %m");
if (arg_verify) {
_cleanup_strv_free_erase_ char **passwords2 = NULL;
assert(strv_length(passwords) == 1);
text = mfree(text);
if (asprintf(&text, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type), friendly) < 0)
return log_oom();
free(id);
id = strjoin("cryptsetup-verification:", disk_path);
if (!id)
return log_oom();
req.message = text;
req.id = id;
r = ask_password_auto(&req, flags, &passwords2);
if (r < 0)
return log_error_errno(r, "Failed to query verification password: %m");
assert(strv_length(passwords2) == 1);
if (!streq(passwords[0], passwords2[0]))
return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN),
"Passwords did not match, retrying.");
}
strv_uniq(passwords);
STRV_FOREACH(p, passwords) {
char *c;
if (strlen(*p)+1 >= arg_key_size)
continue;
/* Pad password if necessary */
c = new(char, arg_key_size);
if (!c)
return log_oom();
strncpy(c, *p, arg_key_size);
erase_and_free(*p);
*p = TAKE_PTR(c);
}
*ret = TAKE_PTR(passwords);
return 0;
}