Coverage Report

Created: 2026-06-09 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptsetup/lib/setup.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * libcryptsetup - cryptsetup library
4
 *
5
 * Copyright (C) 2004 Jana Saout <jana@saout.de>
6
 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
7
 * Copyright (C) 2009-2025 Red Hat, Inc. All rights reserved.
8
 * Copyright (C) 2009-2025 Milan Broz
9
 */
10
11
#include <string.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <stdarg.h>
15
#if HAVE_SYS_UTSNAME_H
16
#include <sys/utsname.h>
17
#endif
18
#include <errno.h>
19
20
#include "libcryptsetup.h"
21
#include "luks1/luks.h"
22
#include "luks2/luks2.h"
23
#include "loopaes/loopaes.h"
24
#include "verity/verity.h"
25
#include "tcrypt/tcrypt.h"
26
#include "integrity/integrity.h"
27
#include "bitlk/bitlk.h"
28
#include "fvault2/fvault2.h"
29
#include "utils_device_locking.h"
30
#include "internal.h"
31
#include "keyslot_context.h"
32
#include "luks2/hw_opal/hw_opal.h"
33
34
0
#define CRYPT_CD_UNRESTRICTED (1 << 0)
35
0
#define CRYPT_CD_QUIET    (1 << 1)
36
37
struct crypt_device {
38
  char *type;
39
40
  struct device *device;
41
  struct device *metadata_device;
42
43
  struct volume_key *volume_key;
44
  int rng_type;
45
  uint32_t compatibility;
46
  struct crypt_pbkdf_type pbkdf;
47
48
  /* global context scope settings */
49
  unsigned key_in_keyring:1;
50
51
  bool link_vk_to_keyring;
52
  int32_t keyring_to_link_vk;
53
  const char *user_key_name1;
54
  const char *user_key_name2;
55
  key_type_t keyring_key_type;
56
57
  uint64_t data_offset;
58
  uint64_t metadata_size; /* Used in LUKS2 format */
59
  uint64_t keyslots_size; /* Used in LUKS2 format */
60
61
  /* Workaround for OOM during parallel activation (like in systemd) */
62
  bool memory_hard_pbkdf_lock_enabled;
63
  struct crypt_lock_handle *pbkdf_memory_hard_lock;
64
65
  union {
66
  struct { /* used in CRYPT_LUKS1 */
67
    struct luks_phdr hdr;
68
    char *cipher_spec;
69
  } luks1;
70
  struct { /* used in CRYPT_LUKS2 */
71
    struct luks2_hdr hdr;
72
    char cipher[MAX_CIPHER_LEN];    /* only for compatibility */
73
    char cipher_mode[MAX_CIPHER_LEN]; /* only for compatibility */
74
    char *keyslot_cipher;
75
    unsigned int keyslot_key_size;
76
    struct luks2_reencrypt *rh;
77
  } luks2;
78
  struct { /* used in CRYPT_PLAIN */
79
    struct crypt_params_plain hdr;
80
    char *cipher_spec;
81
    char *cipher;
82
    const char *cipher_mode;
83
    unsigned int key_size;
84
  } plain;
85
  struct { /* used in CRYPT_LOOPAES */
86
    struct crypt_params_loopaes hdr;
87
    char *cipher_spec;
88
    char *cipher;
89
    const char *cipher_mode;
90
    unsigned int key_size;
91
  } loopaes;
92
  struct { /* used in CRYPT_VERITY */
93
    struct crypt_params_verity hdr;
94
    const char *root_hash;
95
    unsigned int root_hash_size;
96
    char *uuid;
97
    struct device *fec_device;
98
  } verity;
99
  struct { /* used in CRYPT_TCRYPT */
100
    struct crypt_params_tcrypt params;
101
    struct tcrypt_phdr hdr;
102
  } tcrypt;
103
  struct { /* used in CRYPT_INTEGRITY */
104
    struct crypt_params_integrity params;
105
    struct volume_key *journal_mac_key;
106
    struct volume_key *journal_crypt_key;
107
    uint32_t sb_flags;
108
  } integrity;
109
  struct { /* used in CRYPT_BITLK */
110
    struct bitlk_metadata params;
111
    char *cipher_spec;
112
  } bitlk;
113
  struct { /* used in CRYPT_FVAULT2 */
114
    struct fvault2_params params;
115
  } fvault2;
116
  struct { /* used if initialized without header by name */
117
    char *active_name;
118
    /* buffers, must refresh from kernel on every query */
119
    char cipher_spec[MAX_CIPHER_LEN*2+1];
120
    char cipher[MAX_CIPHER_LEN];
121
    char integrity_spec[MAX_INTEGRITY_LEN];
122
    const char *cipher_mode;
123
    unsigned int key_size;
124
    uint32_t sector_size;
125
  } none;
126
  } u;
127
128
  /* callbacks definitions */
129
  void (*log)(int level, const char *msg, void *usrptr);
130
  void *log_usrptr;
131
  int (*confirm)(const char *msg, void *usrptr);
132
  void *confirm_usrptr;
133
};
134
135
/* Just to suppress redundant messages about crypto backend */
136
static int _crypto_logged = 0;
137
138
/* Log helper */
139
static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
140
static void *_default_log_usrptr = NULL;
141
static int _debug_level = 0;
142
143
/* Library can do metadata locking  */
144
static int _metadata_locking = 1;
145
146
/* Library scope detection for kernel keyring support */
147
static int _kernel_keyring_supported;
148
149
/* Library allowed to use kernel keyring for loading VK in kernel crypto layer */
150
static int _vk_via_keyring = 1;
151
152
void crypt_set_debug_level(int level)
153
0
{
154
0
  _debug_level = level;
155
0
}
156
157
int crypt_get_debug_level(void)
158
0
{
159
0
  return _debug_level;
160
0
}
161
162
void crypt_log(struct crypt_device *cd, int level, const char *msg)
163
232k
{
164
232k
  if (!msg)
165
0
    return;
166
167
232k
  if (level < _debug_level)
168
229k
    return;
169
170
3.03k
  if (cd && cd->log)
171
0
    cd->log(level, msg, cd->log_usrptr);
172
3.03k
  else if (_default_log)
173
1.56k
    _default_log(level, msg, _default_log_usrptr);
174
  /* Default to stdout/stderr if there is no callback. */
175
1.46k
  else
176
1.46k
    fprintf(level == CRYPT_LOG_ERROR ? stderr : stdout, "%s", msg);
177
3.03k
}
178
179
__attribute__((format(printf, 3, 4)))
180
void crypt_logf(struct crypt_device *cd, int level, const char *format, ...)
181
232k
{
182
232k
  va_list argp;
183
232k
  char target[LOG_MAX_LEN + 2];
184
232k
  int len;
185
186
232k
  va_start(argp, format);
187
188
232k
  len = vsnprintf(&target[0], LOG_MAX_LEN, format, argp);
189
232k
  if (len > 0 && len < LOG_MAX_LEN) {
190
    /* All verbose and error messages in tools end with EOL. */
191
232k
    if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR ||
192
229k
        level == CRYPT_LOG_DEBUG || level == CRYPT_LOG_DEBUG_JSON)
193
232k
      strncat(target, "\n", LOG_MAX_LEN);
194
195
232k
    crypt_log(cd, level, target);
196
232k
  }
197
198
232k
  va_end(argp);
199
232k
}
200
201
static const char *mdata_device_path(struct crypt_device *cd)
202
16.7k
{
203
16.7k
  return device_path(cd->metadata_device ?: cd->device);
204
16.7k
}
205
206
static const char *data_device_path(struct crypt_device *cd)
207
0
{
208
0
  return device_path(cd->device);
209
0
}
210
211
/* internal only */
212
struct device *crypt_metadata_device(struct crypt_device *cd)
213
44.0k
{
214
44.0k
  return cd->metadata_device ?: cd->device;
215
44.0k
}
216
217
struct device *crypt_data_device(struct crypt_device *cd)
218
3.34k
{
219
3.34k
  return cd->device;
220
3.34k
}
221
222
uint64_t crypt_get_metadata_size_bytes(struct crypt_device *cd)
223
0
{
224
0
  assert(cd);
225
0
  return cd->metadata_size;
226
0
}
227
228
uint64_t crypt_get_keyslots_size_bytes(struct crypt_device *cd)
229
0
{
230
0
  assert(cd);
231
0
  return cd->keyslots_size;
232
0
}
233
234
uint64_t crypt_get_data_offset_sectors(struct crypt_device *cd)
235
0
{
236
0
  assert(cd);
237
0
  return cd->data_offset;
238
0
}
239
240
int crypt_opal_supported(struct crypt_device *cd, struct device *opal_device)
241
0
{
242
0
  int r;
243
244
0
  assert(cd);
245
0
  assert(opal_device);
246
247
0
  r = opal_supported(cd, opal_device);
248
0
  if (r <= 0) {
249
0
    if (r == -ENOTSUP)
250
0
      log_err(cd, _("OPAL support is disabled in libcryptsetup."));
251
0
    else
252
0
      log_err(cd, _("Device %s or kernel does not support OPAL encryption."),
253
0
            device_path(opal_device));
254
0
    r = -EINVAL;
255
0
  } else
256
0
    r = 0;
257
258
0
  return r;
259
0
}
260
261
int init_crypto(struct crypt_device *ctx)
262
16.8k
{
263
16.8k
#if HAVE_SYS_UTSNAME_H
264
16.8k
  struct utsname uts;
265
16.8k
#endif
266
16.8k
  int r;
267
268
16.8k
  r = crypt_random_init(ctx);
269
16.8k
  if (r < 0) {
270
0
    log_err(ctx, _("Cannot initialize crypto RNG backend."));
271
0
    return r;
272
0
  }
273
274
16.8k
  r = crypt_backend_init();
275
16.8k
  if (r < 0)
276
0
    log_err(ctx, _("Cannot initialize crypto backend."));
277
278
16.8k
  if (!r && !_crypto_logged) {
279
2
    log_dbg(ctx, "Crypto backend (%s%s) initialized in cryptsetup library version %s.",
280
2
      crypt_backend_version(), crypt_argon2_version(), PACKAGE_VERSION);
281
282
2
#if HAVE_SYS_UTSNAME_H
283
2
    if (!uname(&uts))
284
2
      log_dbg(ctx, "Detected kernel %s %s %s.",
285
2
        uts.sysname, uts.release, uts.machine);
286
2
#endif
287
2
    _crypto_logged = 1;
288
2
  }
289
290
16.8k
  return r;
291
16.8k
}
292
293
static int process_key(struct crypt_device *cd, const char *hash_name,
294
           size_t key_size, const char *pass, size_t passLen,
295
           struct volume_key **vk)
296
0
{
297
0
  int r;
298
0
  void *key = NULL;
299
300
0
  if (!key_size)
301
0
    return -EINVAL;
302
303
0
  if (hash_name) {
304
0
    key = crypt_safe_alloc(key_size);
305
0
    if (!key)
306
0
      return -ENOMEM;
307
308
0
    r = crypt_plain_hash(cd, hash_name, key, key_size, pass, passLen);
309
0
    if (r < 0) {
310
0
      if (r == -ENOENT)
311
0
        log_err(cd, _("Hash algorithm %s not supported."),
312
0
          hash_name);
313
0
      else
314
0
        log_err(cd, _("Key processing error (using hash %s)."),
315
0
          hash_name);
316
0
      crypt_safe_free(key);
317
0
      return -EINVAL;
318
0
    }
319
0
    *vk = crypt_alloc_volume_key_by_safe_alloc(&key);
320
0
  } else if (passLen >= key_size) {
321
0
    *vk = crypt_alloc_volume_key(key_size, pass);
322
0
  } else {
323
0
    key = crypt_safe_alloc(key_size);
324
0
    if (!key)
325
0
      return -ENOMEM;
326
327
0
    crypt_safe_memcpy(key, pass, passLen);
328
329
0
    *vk = crypt_alloc_volume_key_by_safe_alloc(&key);
330
0
  }
331
332
0
  r = *vk ? 0 : -ENOMEM;
333
334
0
  crypt_safe_free(key);
335
336
0
  return r;
337
0
}
338
339
static int isPLAIN(const char *type)
340
10.1k
{
341
10.1k
  return (type && !strcmp(CRYPT_PLAIN, type));
342
10.1k
}
343
344
static int isLUKS1(const char *type)
345
36.8k
{
346
36.8k
  return (type && !strcmp(CRYPT_LUKS1, type));
347
36.8k
}
348
349
static int isLUKS2(const char *type)
350
31.8k
{
351
31.8k
  return (type && !strcmp(CRYPT_LUKS2, type));
352
31.8k
}
353
354
static int isLUKS(const char *type)
355
0
{
356
0
  return (isLUKS2(type) || isLUKS1(type));
357
0
}
358
359
static int isLOOPAES(const char *type)
360
8.46k
{
361
8.46k
  return (type && !strcmp(CRYPT_LOOPAES, type));
362
8.46k
}
363
364
static int isVERITY(const char *type)
365
11.9k
{
366
11.9k
  return (type && !strcmp(CRYPT_VERITY, type));
367
11.9k
}
368
369
static int isTCRYPT(const char *type)
370
3.44k
{
371
3.44k
  return (type && !strcmp(CRYPT_TCRYPT, type));
372
3.44k
}
373
374
static int isINTEGRITY(const char *type)
375
11.9k
{
376
11.9k
  return (type && !strcmp(CRYPT_INTEGRITY, type));
377
11.9k
}
378
379
static int isBITLK(const char *type)
380
11.9k
{
381
11.9k
  return (type && !strcmp(CRYPT_BITLK, type));
382
11.9k
}
383
384
static int isFVAULT2(const char *type)
385
1.72k
{
386
1.72k
  return (type && !strcmp(CRYPT_FVAULT2, type));
387
1.72k
}
388
389
static int _onlyLUKS(struct crypt_device *cd, uint32_t cdflags, uint32_t mask)
390
0
{
391
0
  int r = 0;
392
393
0
  if (cd && !cd->type) {
394
0
    if (!(cdflags & CRYPT_CD_QUIET))
395
0
      log_err(cd, _("Cannot determine device type. Incompatible activation of device?"));
396
0
    r = -EINVAL;
397
0
  }
398
399
0
  if (!cd || !isLUKS(cd->type)) {
400
0
    if (!(cdflags & CRYPT_CD_QUIET))
401
0
      log_err(cd, _("This operation is supported only for LUKS device."));
402
0
    r = -EINVAL;
403
0
  }
404
405
0
  if (r || (cdflags & CRYPT_CD_UNRESTRICTED) || isLUKS1(cd->type))
406
0
    return r;
407
408
0
  return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, mask, cdflags & CRYPT_CD_QUIET);
409
0
}
410
411
static int onlyLUKSunrestricted(struct crypt_device *cd)
412
0
{
413
0
  return _onlyLUKS(cd, CRYPT_CD_UNRESTRICTED, 0);
414
0
}
415
416
static int onlyLUKSnoRequirements(struct crypt_device *cd)
417
0
{
418
0
  return _onlyLUKS(cd, 0, 0);
419
0
}
420
421
static int onlyLUKS(struct crypt_device *cd)
422
0
{
423
0
  return _onlyLUKS(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_INLINE_HW_TAGS);
424
0
}
425
426
static int _onlyLUKS2(struct crypt_device *cd, uint32_t cdflags, uint32_t mask)
427
0
{
428
0
  int r = 0;
429
430
0
  if (cd && !cd->type) {
431
0
    if (!(cdflags & CRYPT_CD_QUIET))
432
0
      log_err(cd, _("Cannot determine device type. Incompatible activation of device?"));
433
0
    r = -EINVAL;
434
0
  }
435
436
0
  if (!cd || !isLUKS2(cd->type)) {
437
0
    if (!(cdflags & CRYPT_CD_QUIET))
438
0
      log_err(cd, _("This operation is supported only for LUKS2 device."));
439
0
    r = -EINVAL;
440
0
  }
441
442
0
  if (r || (cdflags & CRYPT_CD_UNRESTRICTED))
443
0
    return r;
444
445
0
  return LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, mask, cdflags & CRYPT_CD_QUIET);
446
0
}
447
448
static int onlyLUKS2unrestricted(struct crypt_device *cd)
449
0
{
450
0
  return _onlyLUKS2(cd, CRYPT_CD_UNRESTRICTED, 0);
451
0
}
452
453
/* Internal only */
454
int onlyLUKS2(struct crypt_device *cd)
455
0
{
456
0
  return _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_INLINE_HW_TAGS);
457
0
}
458
459
/* Internal only */
460
int onlyLUKS2reencrypt(struct crypt_device *cd)
461
0
{
462
0
  return _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_ONLINE_REENCRYPT);
463
0
}
464
465
static void crypt_set_null_type(struct crypt_device *cd)
466
10.1k
{
467
10.1k
  free(cd->type);
468
10.1k
  cd->type = NULL;
469
10.1k
  cd->data_offset = 0;
470
10.1k
  cd->metadata_size = 0;
471
10.1k
  cd->keyslots_size = 0;
472
10.1k
  crypt_safe_memzero(&cd->u, sizeof(cd->u));
473
10.1k
}
474
475
static void crypt_reset_null_type(struct crypt_device *cd)
476
10.1k
{
477
10.1k
  if (cd->type)
478
0
    return;
479
480
10.1k
  free(cd->u.none.active_name);
481
10.1k
  cd->u.none.active_name = NULL;
482
10.1k
}
483
484
/* keyslot helpers */
485
static int keyslot_verify_or_find_empty(struct crypt_device *cd, int *keyslot)
486
0
{
487
0
  crypt_keyslot_info ki;
488
489
0
  if (*keyslot == CRYPT_ANY_SLOT) {
490
0
    if (isLUKS1(cd->type))
491
0
      *keyslot = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
492
0
    else
493
0
      *keyslot = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, 0);
494
0
    if (*keyslot < 0) {
495
0
      log_err(cd, _("All key slots full."));
496
0
      return -EINVAL;
497
0
    }
498
0
  }
499
500
0
  if (isLUKS1(cd->type))
501
0
    ki = LUKS_keyslot_info(&cd->u.luks1.hdr, *keyslot);
502
0
  else
503
0
    ki = LUKS2_keyslot_info(&cd->u.luks2.hdr, *keyslot);
504
0
  switch (ki) {
505
0
    case CRYPT_SLOT_INVALID:
506
0
      log_err(cd, _("Key slot %d is invalid, please select between 0 and %d."),
507
0
        *keyslot, crypt_keyslot_max(cd->type) - 1);
508
0
      return -EINVAL;
509
0
    case CRYPT_SLOT_INACTIVE:
510
0
      break;
511
0
    default:
512
0
      log_err(cd, _("Key slot %d is full, please select another one."),
513
0
        *keyslot);
514
0
      return -EINVAL;
515
0
  }
516
517
0
  log_dbg(cd, "Selected keyslot %d.", *keyslot);
518
0
  return 0;
519
0
}
520
521
int PLAIN_activate(struct crypt_device *cd,
522
         const char *name,
523
         struct volume_key *vk,
524
         uint64_t size,
525
         uint32_t flags)
526
0
{
527
0
  int r;
528
0
  struct crypt_dm_active_device dmd = {
529
0
    .flags = flags,
530
0
    .size = size,
531
0
  };
532
533
0
  log_dbg(cd, "Trying to activate PLAIN device %s using cipher %s.",
534
0
    name, crypt_get_cipher_spec(cd));
535
536
0
  if (MISALIGNED(size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) {
537
0
    log_err(cd, _("Device size is not aligned to device logical block size."));
538
0
    return -EINVAL;
539
0
  }
540
541
0
  r = dm_crypt_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
542
0
      vk, crypt_get_cipher_spec(cd), crypt_get_iv_offset(cd),
543
0
      crypt_get_data_offset(cd), NULL, 0, 0, crypt_get_sector_size(cd));
544
0
  if (r < 0)
545
0
    return r;
546
547
0
  r = create_or_reload_device(cd, name, CRYPT_PLAIN, &dmd);
548
549
0
  dm_targets_free(cd, &dmd);
550
0
  return r;
551
0
}
552
553
int crypt_confirm(struct crypt_device *cd, const char *msg)
554
0
{
555
0
  if (!cd || !cd->confirm)
556
0
    return 1;
557
0
  else
558
0
    return cd->confirm(msg, cd->confirm_usrptr);
559
0
}
560
561
void crypt_set_log_callback(struct crypt_device *cd,
562
  void (*log)(int level, const char *msg, void *usrptr),
563
  void *usrptr)
564
1.74k
{
565
1.74k
  if (!cd) {
566
1.74k
    _default_log = log;
567
1.74k
    _default_log_usrptr = usrptr;
568
1.74k
  } else {
569
0
    cd->log = log;
570
0
    cd->log_usrptr = usrptr;
571
0
  }
572
1.74k
}
573
574
void crypt_set_confirm_callback(struct crypt_device *cd,
575
  int (*confirm)(const char *msg, void *usrptr),
576
  void *usrptr)
577
0
{
578
0
  if (cd) {
579
0
    cd->confirm = confirm;
580
0
    cd->confirm_usrptr = usrptr;
581
0
  }
582
0
}
583
584
const char *crypt_get_dir(void)
585
0
{
586
0
  return dm_get_dir();
587
0
}
588
589
int crypt_init(struct crypt_device **cd, const char *device)
590
6.66k
{
591
6.66k
  struct crypt_device *h = NULL;
592
6.66k
  int r;
593
594
6.66k
  if (!cd)
595
0
    return -EINVAL;
596
597
6.66k
  log_dbg(NULL, "Allocating context for crypt device %s.", device ?: "(none)");
598
#if !HAVE_DECL_O_CLOEXEC
599
  log_dbg(NULL, "Running without O_CLOEXEC.");
600
#endif
601
602
6.66k
  if (!(h = crypt_zalloc(sizeof(struct crypt_device))))
603
0
    return -ENOMEM;
604
605
6.66k
  r = device_alloc(NULL, &h->device, device);
606
6.66k
  if (r < 0) {
607
0
    free(h);
608
0
    return r;
609
0
  }
610
611
6.66k
  dm_backend_init(NULL);
612
613
6.66k
  h->rng_type = crypt_random_default_key_rng();
614
615
6.66k
  *cd = h;
616
6.66k
  return 0;
617
6.66k
}
618
619
static int crypt_check_data_device_size(struct crypt_device *cd)
620
0
{
621
0
  int r;
622
0
  uint64_t size, size_min;
623
624
  /* Check data device size, require at least header or one sector */
625
0
  size_min = crypt_get_data_offset(cd) << SECTOR_SHIFT ?: SECTOR_SIZE;
626
627
0
  r = device_size(cd->device, &size);
628
0
  if (r < 0)
629
0
    return r;
630
631
0
  if (size < size_min) {
632
0
    log_err(cd, _("Header detected but device %s is too small."),
633
0
      device_path(cd->device));
634
0
    return -EINVAL;
635
0
  }
636
637
0
  return r;
638
0
}
639
640
static int _crypt_set_data_device(struct crypt_device *cd, const char *device)
641
0
{
642
0
  struct device *dev = NULL;
643
0
  int r;
644
645
0
  r = device_alloc(cd, &dev, device);
646
0
  if (r < 0)
647
0
    return r;
648
649
0
  if (!cd->metadata_device) {
650
0
    cd->metadata_device = cd->device;
651
0
  } else
652
0
    device_free(cd, cd->device);
653
654
0
  cd->device = dev;
655
656
0
  r = crypt_check_data_device_size(cd);
657
0
  if (!r && isLUKS2(cd->type))
658
0
    device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr));
659
660
0
  return r;
661
0
}
662
663
int crypt_set_data_device(struct crypt_device *cd, const char *device)
664
0
{
665
  /* metadata device must be set */
666
0
  if (!cd || !cd->device || !device)
667
0
    return -EINVAL;
668
669
0
  log_dbg(cd, "Setting ciphertext data device to %s.", device ?: "(none)");
670
671
0
  if (!isLUKS1(cd->type) && !isLUKS2(cd->type) && !isVERITY(cd->type) &&
672
0
      !isINTEGRITY(cd->type) && !isTCRYPT(cd->type)) {
673
0
    log_err(cd, _("This operation is not supported for this device type."));
674
0
    return -EINVAL;
675
0
  }
676
677
0
  if (isLUKS2(cd->type) && crypt_get_luks2_reencrypt(cd)) {
678
0
    log_err(cd, _("Illegal operation with reencryption in-progress."));
679
0
    return -EINVAL;
680
0
  }
681
682
0
  return _crypt_set_data_device(cd, device);
683
0
}
684
685
int crypt_init_data_device(struct crypt_device **cd, const char *device, const char *data_device)
686
0
{
687
0
  int r;
688
689
0
  if (!cd)
690
0
    return -EINVAL;
691
692
0
  r = crypt_init(cd, device);
693
0
  if (r || !data_device || !strcmp(device, data_device))
694
0
    return r;
695
696
0
  log_dbg(NULL, "Setting ciphertext data device to %s.", data_device);
697
0
  r = _crypt_set_data_device(*cd, data_device);
698
0
  if (r) {
699
0
    crypt_free(*cd);
700
0
    *cd = NULL;
701
0
  }
702
703
0
  return r;
704
0
}
705
706
static void crypt_free_type(struct crypt_device *cd, const char *force_type)
707
10.1k
{
708
10.1k
  const char *type = force_type ?: cd->type;
709
710
10.1k
  if (isPLAIN(type)) {
711
0
    free(CONST_CAST(void*)cd->u.plain.hdr.hash);
712
0
    free(cd->u.plain.cipher);
713
0
    free(cd->u.plain.cipher_spec);
714
10.1k
  } else if (isLUKS2(type)) {
715
1.62k
    LUKS2_reencrypt_free(cd, cd->u.luks2.rh);
716
1.62k
    LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
717
1.62k
    free(cd->u.luks2.keyslot_cipher);
718
8.48k
  } else if (isLUKS1(type)) {
719
24
    free(cd->u.luks1.cipher_spec);
720
8.46k
  } else if (isLOOPAES(type)) {
721
0
    free(CONST_CAST(void*)cd->u.loopaes.hdr.hash);
722
0
    free(cd->u.loopaes.cipher);
723
0
    free(cd->u.loopaes.cipher_spec);
724
8.46k
  } else if (isVERITY(type)) {
725
0
    free(CONST_CAST(void*)cd->u.verity.hdr.hash_name);
726
0
    free(CONST_CAST(void*)cd->u.verity.hdr.data_device);
727
0
    free(CONST_CAST(void*)cd->u.verity.hdr.hash_device);
728
0
    free(CONST_CAST(void*)cd->u.verity.hdr.fec_device);
729
0
    free(CONST_CAST(void*)cd->u.verity.hdr.salt);
730
0
    free(CONST_CAST(void*)cd->u.verity.root_hash);
731
0
    free(cd->u.verity.uuid);
732
0
    device_free(cd, cd->u.verity.fec_device);
733
8.46k
  } else if (isINTEGRITY(type)) {
734
0
    free(CONST_CAST(void*)cd->u.integrity.params.integrity);
735
0
    free(CONST_CAST(void*)cd->u.integrity.params.journal_integrity);
736
0
    free(CONST_CAST(void*)cd->u.integrity.params.journal_crypt);
737
0
    crypt_free_volume_key(cd->u.integrity.journal_crypt_key);
738
0
    crypt_free_volume_key(cd->u.integrity.journal_mac_key);
739
8.46k
  } else if (isBITLK(type)) {
740
1.72k
    free(cd->u.bitlk.cipher_spec);
741
1.72k
    BITLK_bitlk_metadata_free(&cd->u.bitlk.params);
742
6.74k
  } else if (!type) {
743
5.02k
    free(cd->u.none.active_name);
744
5.02k
    cd->u.none.active_name = NULL;
745
5.02k
  }
746
747
10.1k
  crypt_set_null_type(cd);
748
10.1k
}
749
750
/* internal only */
751
struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd)
752
3.36k
{
753
3.36k
  return &cd->pbkdf;
754
3.36k
}
755
756
/*
757
 * crypt_load() helpers
758
 */
759
static int _crypt_load_luks2(struct crypt_device *cd, int reload, int repair)
760
4.92k
{
761
4.92k
  int r;
762
4.92k
  char *type = NULL;
763
4.92k
  struct luks2_hdr hdr2 = {};
764
765
4.92k
  log_dbg(cd, "%soading LUKS2 header (repair %sabled).", reload ? "Rel" : "L", repair ? "en" : "dis");
766
767
4.92k
  r = LUKS2_hdr_read(cd, &hdr2, repair);
768
4.92k
  if (r)
769
3.29k
    return r;
770
771
1.62k
  if (!reload) {
772
1.62k
    type = strdup(CRYPT_LUKS2);
773
1.62k
    if (!type) {
774
0
      r = -ENOMEM;
775
0
      goto out;
776
0
    }
777
1.62k
  }
778
779
1.62k
  if (verify_pbkdf_params(cd, &cd->pbkdf)) {
780
1.62k
    r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
781
1.62k
    if (r)
782
0
      goto out;
783
1.62k
  }
784
785
1.62k
  if (reload) {
786
0
    LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
787
0
    free(cd->u.luks2.keyslot_cipher);
788
0
  } else
789
1.62k
    cd->type = type;
790
791
1.62k
  r = 0;
792
1.62k
  memcpy(&cd->u.luks2.hdr, &hdr2, sizeof(hdr2));
793
1.62k
  cd->u.luks2.keyslot_cipher = NULL;
794
1.62k
  cd->u.luks2.rh = NULL;
795
796
1.62k
out:
797
1.62k
  if (r) {
798
0
    free(type);
799
0
    LUKS2_hdr_free(cd, &hdr2);
800
0
  }
801
1.62k
  return r;
802
1.62k
}
803
804
static void _luks2_rollback(struct crypt_device *cd)
805
0
{
806
0
  if (!cd || !isLUKS2(cd->type))
807
0
    return;
808
809
0
  if (LUKS2_hdr_rollback(cd, &cd->u.luks2.hdr)) {
810
0
    log_err(cd, _("Failed to rollback LUKS2 metadata in memory."));
811
0
    return;
812
0
  }
813
814
0
  free(cd->u.luks2.keyslot_cipher);
815
0
  cd->u.luks2.keyslot_cipher = NULL;
816
0
}
817
818
static int _crypt_load_luks(struct crypt_device *cd, const char *requested_type,
819
          bool quiet, bool repair)
820
6.66k
{
821
6.66k
  char *cipher_spec;
822
6.66k
  struct luks_phdr hdr = {};
823
6.66k
  int r, version;
824
825
6.66k
  r = init_crypto(cd);
826
6.66k
  if (r < 0)
827
0
    return r;
828
829
  /* This will return 0 if primary LUKS2 header is damaged */
830
6.66k
  version = LUKS2_hdr_version_unlocked(cd, NULL);
831
832
6.66k
  if ((isLUKS1(requested_type) && version == 2) ||
833
6.66k
      (isLUKS2(requested_type) && version == 1))
834
5
    return -EINVAL;
835
836
6.66k
  if (requested_type)
837
6.66k
    version = 0;
838
839
6.66k
  if (isLUKS1(requested_type) || version == 1) {
840
1.74k
    if (isLUKS2(cd->type)) {
841
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
842
0
      return -EINVAL;
843
0
    }
844
845
1.74k
    if (verify_pbkdf_params(cd, &cd->pbkdf)) {
846
1.74k
      r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1);
847
1.74k
      if (r)
848
0
        return r;
849
1.74k
    }
850
851
1.74k
    r = LUKS_read_phdr(&hdr, !quiet, repair, cd);
852
1.74k
    if (r)
853
1.71k
      goto out;
854
855
24
    if (!cd->type && !(cd->type = strdup(CRYPT_LUKS1))) {
856
0
      r = -ENOMEM;
857
0
      goto out;
858
0
    }
859
860
    /* Set hash to the same as in the loaded header */
861
24
    if (!cd->pbkdf.hash || strcmp(cd->pbkdf.hash, hdr.hashSpec)) {
862
18
      free(CONST_CAST(void*)cd->pbkdf.hash);
863
18
      cd->pbkdf.hash = strdup(hdr.hashSpec);
864
18
      if (!cd->pbkdf.hash) {
865
0
        r = -ENOMEM;
866
0
        goto out;
867
0
      }
868
18
    }
869
870
24
    if (asprintf(&cipher_spec, "%s-%s", hdr.cipherName, hdr.cipherMode) < 0) {
871
0
      r = -ENOMEM;
872
0
      goto out;
873
0
    }
874
875
24
    free(cd->u.luks1.cipher_spec);
876
24
    cd->u.luks1.cipher_spec = cipher_spec;
877
878
24
    memcpy(&cd->u.luks1.hdr, &hdr, sizeof(hdr));
879
4.92k
  } else if (isLUKS2(requested_type) || version == 2 || version == 0) {
880
4.92k
    if (isLUKS1(cd->type)) {
881
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
882
0
      return -EINVAL;
883
0
    }
884
885
    /*
886
     * Current LUKS2 repair just overrides blkid probes
887
     * and perform auto-recovery if possible. This is safe
888
     * unless future LUKS2 repair code do something more
889
     * sophisticated. In such case we would need to check
890
     * for LUKS2 requirements and decide if it's safe to
891
     * perform repair.
892
     */
893
4.92k
    r =  _crypt_load_luks2(cd, cd->type != NULL, repair);
894
4.92k
    if (!r)
895
1.62k
      device_set_block_size(crypt_data_device(cd), LUKS2_get_sector_size(&cd->u.luks2.hdr));
896
3.29k
    else if (!quiet)
897
0
      log_err(cd, _("Device %s is not a valid LUKS device."), mdata_device_path(cd));
898
4.92k
  } else {
899
0
    if (version > 2)
900
0
      log_err(cd, _("Unsupported LUKS version %d."), version);
901
0
    r = -EINVAL;
902
0
  }
903
6.66k
out:
904
6.66k
  crypt_safe_memzero(&hdr, sizeof(hdr));
905
906
6.66k
  return r;
907
6.66k
}
908
909
static int _crypt_load_tcrypt(struct crypt_device *cd, struct crypt_params_tcrypt *params)
910
0
{
911
0
  int r;
912
913
0
  if (!params)
914
0
    return -EINVAL;
915
916
0
  r = init_crypto(cd);
917
0
  if (r < 0)
918
0
    return r;
919
920
0
  memcpy(&cd->u.tcrypt.params, params, sizeof(*params));
921
922
0
  r = TCRYPT_read_phdr(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
923
924
0
  cd->u.tcrypt.params.passphrase = NULL;
925
0
  cd->u.tcrypt.params.passphrase_size = 0;
926
0
  cd->u.tcrypt.params.keyfiles = NULL;
927
0
  cd->u.tcrypt.params.keyfiles_count = 0;
928
0
  cd->u.tcrypt.params.veracrypt_pim = 0;
929
930
0
  if (r < 0)
931
0
    goto out;
932
933
0
  if (!cd->type && !(cd->type = strdup(CRYPT_TCRYPT)))
934
0
    r = -ENOMEM;
935
0
out:
936
0
  if (r < 0)
937
0
    crypt_free_type(cd, CRYPT_TCRYPT);
938
0
  return r;
939
0
}
940
941
static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verity *params)
942
0
{
943
0
  int r;
944
0
  uint64_t sb_offset = 0;
945
946
0
  r = init_crypto(cd);
947
0
  if (r < 0)
948
0
    return r;
949
950
0
  if (params && params->flags & CRYPT_VERITY_NO_HEADER)
951
0
    return -EINVAL;
952
953
0
  if (params)
954
0
    sb_offset = params->hash_area_offset;
955
956
0
  r = VERITY_read_sb(cd, sb_offset, &cd->u.verity.uuid, &cd->u.verity.hdr);
957
0
  if (r < 0)
958
0
    goto out;
959
960
0
  if (!cd->type && !(cd->type = strdup(CRYPT_VERITY))) {
961
0
    r = -ENOMEM;
962
0
    goto out;
963
0
  }
964
965
0
  if (params)
966
0
    cd->u.verity.hdr.flags = params->flags;
967
968
  /* Hash availability checked in sb load */
969
0
  cd->u.verity.root_hash_size = crypt_hash_size(cd->u.verity.hdr.hash_name);
970
0
  if (cd->u.verity.root_hash_size > 4096) {
971
0
    r = -EINVAL;
972
0
    goto out;
973
0
  }
974
975
0
  if (params && params->data_device &&
976
0
      (r = crypt_set_data_device(cd, params->data_device)) < 0)
977
0
    goto out;
978
979
0
  if (params && params->fec_device) {
980
0
    r = device_alloc(cd, &cd->u.verity.fec_device, params->fec_device);
981
0
    if (r < 0)
982
0
      goto out;
983
0
    cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
984
0
    cd->u.verity.hdr.fec_roots = params->fec_roots;
985
0
  }
986
0
out:
987
0
  if (r < 0)
988
0
    crypt_free_type(cd, CRYPT_VERITY);
989
0
  return r;
990
0
}
991
992
static int _crypt_load_integrity(struct crypt_device *cd,
993
         struct crypt_params_integrity *params)
994
0
{
995
0
  int r;
996
997
0
  r = init_crypto(cd);
998
0
  if (r < 0)
999
0
    return r;
1000
1001
0
  r = INTEGRITY_read_sb(cd, &cd->u.integrity.params, &cd->u.integrity.sb_flags);
1002
0
  if (r < 0)
1003
0
    goto out;
1004
1005
  // FIXME: add checks for fields in integrity sb vs params
1006
1007
0
  r = -ENOMEM;
1008
0
  if (params) {
1009
0
    cd->u.integrity.params.journal_watermark = params->journal_watermark;
1010
0
    cd->u.integrity.params.journal_commit_time = params->journal_commit_time;
1011
0
    cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
1012
0
    if (params->integrity &&
1013
0
        !(cd->u.integrity.params.integrity = strdup(params->integrity)))
1014
0
      goto out;
1015
0
    cd->u.integrity.params.integrity_key_size = params->integrity_key_size;
1016
0
    if (params->journal_integrity &&
1017
0
        !(cd->u.integrity.params.journal_integrity = strdup(params->journal_integrity)))
1018
0
      goto out;
1019
0
    if (params->journal_crypt &&
1020
0
        !(cd->u.integrity.params.journal_crypt = strdup(params->journal_crypt)))
1021
0
      goto out;
1022
1023
0
    if (params->journal_crypt_key) {
1024
0
      cd->u.integrity.journal_crypt_key =
1025
0
        crypt_alloc_volume_key(params->journal_crypt_key_size,
1026
0
                   params->journal_crypt_key);
1027
0
      if (!cd->u.integrity.journal_crypt_key)
1028
0
        goto out;
1029
0
    }
1030
0
    if (params->journal_integrity_key) {
1031
0
      cd->u.integrity.journal_mac_key =
1032
0
        crypt_alloc_volume_key(params->journal_integrity_key_size,
1033
0
                   params->journal_integrity_key);
1034
0
      if (!cd->u.integrity.journal_mac_key)
1035
0
        goto out;
1036
0
    }
1037
0
  }
1038
1039
0
  if (!cd->type && !(cd->type = strdup(CRYPT_INTEGRITY)))
1040
0
    goto out;
1041
0
  r = 0;
1042
0
out:
1043
0
  if (r < 0)
1044
0
    crypt_free_type(cd, CRYPT_INTEGRITY);
1045
0
  return r;
1046
0
}
1047
1048
static int _crypt_load_bitlk(struct crypt_device *cd)
1049
1.72k
{
1050
1.72k
  int r;
1051
1052
1.72k
  r = init_crypto(cd);
1053
1.72k
  if (r < 0)
1054
0
    return r;
1055
1056
1.72k
  r = BITLK_read_sb(cd, &cd->u.bitlk.params);
1057
1.72k
  if (r < 0)
1058
1.72k
    goto out;
1059
1060
0
  if (asprintf(&cd->u.bitlk.cipher_spec, "%s-%s",
1061
0
         cd->u.bitlk.params.cipher, cd->u.bitlk.params.cipher_mode) < 0) {
1062
0
    cd->u.bitlk.cipher_spec = NULL;
1063
0
    r = -ENOMEM;
1064
0
    goto out;
1065
0
  }
1066
1067
0
  if (!cd->type && !(cd->type = strdup(CRYPT_BITLK))) {
1068
0
    r = -ENOMEM;
1069
0
    goto out;
1070
0
  }
1071
1072
0
  device_set_block_size(crypt_data_device(cd), cd->u.bitlk.params.sector_size);
1073
1.72k
out:
1074
1.72k
  if (r < 0)
1075
1.72k
    crypt_free_type(cd, CRYPT_BITLK);
1076
1.72k
  return r;
1077
0
}
1078
1079
static int _crypt_load_fvault2(struct crypt_device *cd)
1080
1.72k
{
1081
1.72k
  int r;
1082
1083
1.72k
  r = init_crypto(cd);
1084
1.72k
  if (r < 0)
1085
0
    return r;
1086
1087
1.72k
  r = FVAULT2_read_metadata(cd, &cd->u.fvault2.params);
1088
1.72k
  if (r < 0)
1089
1.72k
    goto out;
1090
1091
0
  if (!cd->type && !(cd->type = strdup(CRYPT_FVAULT2)))
1092
0
    r = -ENOMEM;
1093
1.72k
out:
1094
1.72k
  if (r < 0)
1095
1.72k
    crypt_free_type(cd, CRYPT_FVAULT2);
1096
1.72k
  return r;
1097
0
}
1098
1099
int crypt_load(struct crypt_device *cd,
1100
         const char *requested_type,
1101
         void *params)
1102
10.1k
{
1103
10.1k
  int r;
1104
1105
10.1k
  if (!cd)
1106
0
    return -EINVAL;
1107
1108
10.1k
  log_dbg(cd, "Trying to load %s crypt type from device %s.",
1109
10.1k
    requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
1110
1111
10.1k
  if (!crypt_metadata_device(cd))
1112
0
    return -EINVAL;
1113
1114
10.1k
  crypt_reset_null_type(cd);
1115
10.1k
  cd->data_offset = 0;
1116
10.1k
  cd->metadata_size = 0;
1117
10.1k
  cd->keyslots_size = 0;
1118
1119
10.1k
  if (!requested_type || isLUKS1(requested_type) || isLUKS2(requested_type)) {
1120
6.66k
    if (cd->type && !isLUKS1(cd->type) && !isLUKS2(cd->type)) {
1121
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1122
0
      return -EINVAL;
1123
0
    }
1124
1125
6.66k
    r = _crypt_load_luks(cd, requested_type, true, false);
1126
6.66k
  } else if (isVERITY(requested_type)) {
1127
0
    if (cd->type && !isVERITY(cd->type)) {
1128
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1129
0
      return -EINVAL;
1130
0
    }
1131
0
    r = _crypt_load_verity(cd, params);
1132
3.44k
  } else if (isTCRYPT(requested_type)) {
1133
0
    if (cd->type && !isTCRYPT(cd->type)) {
1134
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1135
0
      return -EINVAL;
1136
0
    }
1137
0
    r = _crypt_load_tcrypt(cd, params);
1138
3.44k
  } else if (isINTEGRITY(requested_type)) {
1139
0
    if (cd->type && !isINTEGRITY(cd->type)) {
1140
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1141
0
      return -EINVAL;
1142
0
    }
1143
0
    r = _crypt_load_integrity(cd, params);
1144
3.44k
  } else if (isBITLK(requested_type)) {
1145
1.72k
    if (cd->type && !isBITLK(cd->type)) {
1146
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1147
0
      return -EINVAL;
1148
0
    }
1149
1.72k
    r = _crypt_load_bitlk(cd);
1150
1.72k
  } else if (isFVAULT2(requested_type)) {
1151
1.72k
    if (cd->type && !isFVAULT2(cd->type)) {
1152
0
      log_dbg(cd, "Context is already initialized to type %s", cd->type);
1153
0
      return -EINVAL;
1154
0
    }
1155
1.72k
    r = _crypt_load_fvault2(cd);
1156
1.72k
  } else
1157
0
    return -EINVAL;
1158
1159
10.1k
  return r;
1160
10.1k
}
1161
1162
/*
1163
 * crypt_init() helpers
1164
 */
1165
static int _init_by_name_crypt_none(struct crypt_device *cd)
1166
0
{
1167
0
  int r;
1168
0
  char _mode[MAX_CIPHER_LEN];
1169
0
  struct crypt_dm_active_device dmd;
1170
0
  struct dm_target *tgt = &dmd.segment;
1171
1172
0
  if (cd->type || !cd->u.none.active_name)
1173
0
    return -EINVAL;
1174
1175
0
  r = dm_query_device(cd, cd->u.none.active_name,
1176
0
      DM_ACTIVE_CRYPT_CIPHER |
1177
0
      DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
1178
0
  if (r < 0)
1179
0
    return r;
1180
0
  if (!single_segment(&dmd) || tgt->type != DM_CRYPT)
1181
0
    r = -EINVAL;
1182
0
  if (r >= 0)
1183
0
    r = crypt_parse_name_and_mode(tgt->u.crypt.cipher,
1184
0
                cd->u.none.cipher, NULL,
1185
0
                _mode);
1186
1187
0
  if (!r) {
1188
0
    r = snprintf(cd->u.none.cipher_spec, sizeof(cd->u.none.cipher_spec),
1189
0
       "%s-%s", cd->u.none.cipher, _mode);
1190
0
    if (r < 0 || (size_t)r >= sizeof(cd->u.none.cipher_spec))
1191
0
      r = -EINVAL;
1192
0
    else {
1193
0
      cd->u.none.cipher_mode = cd->u.none.cipher_spec + strlen(cd->u.none.cipher) + 1;
1194
0
      cd->u.none.key_size = crypt_volume_key_length(tgt->u.crypt.vk);
1195
0
      r = 0;
1196
0
    }
1197
0
  }
1198
1199
0
  if (!r && tgt->u.crypt.integrity) {
1200
0
    r = snprintf(cd->u.none.integrity_spec, sizeof(cd->u.none.integrity_spec),
1201
0
       "%s", tgt->u.crypt.integrity);
1202
0
    if (r < 0 || (size_t)r >= sizeof(cd->u.none.integrity_spec))
1203
0
      r = -EINVAL;
1204
0
    else
1205
0
      r = 0;
1206
0
  }
1207
1208
0
  cd->u.none.sector_size = tgt->u.crypt.sector_size;
1209
1210
0
  dm_targets_free(cd, &dmd);
1211
0
  return r;
1212
0
}
1213
1214
static const char *LUKS_UUID(struct crypt_device *cd)
1215
0
{
1216
0
  if (!cd)
1217
0
    return NULL;
1218
0
  else if (isLUKS1(cd->type))
1219
0
    return cd->u.luks1.hdr.uuid;
1220
0
  else if (isLUKS2(cd->type))
1221
0
    return cd->u.luks2.hdr.uuid;
1222
1223
0
  return NULL;
1224
0
}
1225
1226
static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
1227
0
{
1228
0
  bool found = false;
1229
0
  char **dep, *cipher_spec = NULL, cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
1230
0
  char deps_uuid_prefix[40], *deps[MAX_DM_DEPS+1] = {};
1231
0
  const char *dev;
1232
0
  char *iname = NULL;
1233
0
  int key_nums, r;
1234
0
  struct crypt_dm_active_device dmd, dmdi = {}, dmdep = {};
1235
0
  struct dm_target *tgt = &dmd.segment, *tgti = &dmdi.segment;
1236
1237
0
  r = dm_query_device(cd, name,
1238
0
      DM_ACTIVE_DEVICE |
1239
0
      DM_ACTIVE_UUID |
1240
0
      DM_ACTIVE_CRYPT_CIPHER |
1241
0
      DM_ACTIVE_CRYPT_KEYSIZE, &dmd);
1242
0
  if (r < 0)
1243
0
    return r;
1244
1245
0
  if (tgt->type != DM_CRYPT && tgt->type != DM_LINEAR) {
1246
0
    log_dbg(cd, "Unsupported device table detected in %s.", name);
1247
0
    r = -EINVAL;
1248
0
    goto out;
1249
0
  }
1250
1251
0
  r = -EINVAL;
1252
1253
0
  if (dmd.uuid) {
1254
0
    r = snprintf(deps_uuid_prefix, sizeof(deps_uuid_prefix), CRYPT_SUBDEV "-%.32s", dmd.uuid + 6);
1255
0
    if (r < 0 || (size_t)r != (sizeof(deps_uuid_prefix) - 1))
1256
0
      r = -EINVAL;
1257
0
  }
1258
1259
0
  if (r >= 0) {
1260
0
    r = dm_device_deps(cd, name, deps_uuid_prefix, deps, ARRAY_SIZE(deps));
1261
0
    if (r)
1262
0
      goto out;
1263
0
  }
1264
1265
0
  r = crypt_parse_name_and_mode(tgt->type == DM_LINEAR ? "null" : tgt->u.crypt.cipher, cipher,
1266
0
              &key_nums, cipher_mode);
1267
0
  if (r < 0) {
1268
    /* Allow crypt null context with unknown cipher string */
1269
0
    if (tgt->type == DM_CRYPT && !tgt->u.crypt.integrity) {
1270
0
      crypt_set_null_type(cd);
1271
0
      r = 0;
1272
0
      goto out;
1273
0
    }
1274
0
    log_err(cd, _("No known cipher specification pattern detected for active device %s."), name);
1275
0
    goto out;
1276
0
  }
1277
1278
0
  dep = deps;
1279
1280
0
  if (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size &&
1281
0
       (iname = dm_get_active_iname(cd, name))) {
1282
1283
0
    r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE, &dmdi);
1284
0
    free(iname);
1285
0
    if (r < 0)
1286
0
      goto out;
1287
    /*
1288
     * Data device for crypt with integrity is not dm-integrity device,
1289
     * but always the device underlying dm-integrity.
1290
     */
1291
0
    device_free(cd, cd->device);
1292
0
    MOVE_REF(cd->device, tgti->data_device);
1293
0
  }
1294
1295
  /* do not try to lookup LUKS2 header in detached header mode */
1296
0
  if (dmd.uuid && !cd->metadata_device && !found) {
1297
0
    while (*dep && !found) {
1298
0
      r = dm_query_device(cd, *dep, DM_ACTIVE_DEVICE, &dmdep);
1299
0
      if (r < 0)
1300
0
        goto out;
1301
1302
0
      tgt = &dmdep.segment;
1303
1304
0
      while (tgt && !found) {
1305
0
        dev = device_path(tgt->data_device);
1306
0
        if (!dev) {
1307
0
          tgt = tgt->next;
1308
0
          continue;
1309
0
        }
1310
0
        if (!strstr(dev, dm_get_dir()) ||
1311
0
            !crypt_string_in(dev + strlen(dm_get_dir()) + 1, deps, ARRAY_SIZE(deps))) {
1312
0
          device_free(cd, cd->device);
1313
0
          MOVE_REF(cd->device, tgt->data_device);
1314
0
          found = true;
1315
0
        }
1316
0
        tgt = tgt->next;
1317
0
      }
1318
0
      dep++;
1319
0
      dm_targets_free(cd, &dmdep);
1320
0
    }
1321
0
  }
1322
1323
0
  if (asprintf(&cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1324
0
    cipher_spec = NULL;
1325
0
    r = -ENOMEM;
1326
0
    goto out;
1327
0
  }
1328
1329
0
  tgt = &dmd.segment;
1330
0
  r = 0;
1331
1332
0
  if (isPLAIN(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1333
0
    cd->u.plain.hdr.hash = NULL; /* no way to get this */
1334
0
    cd->u.plain.hdr.offset = tgt->u.crypt.offset;
1335
0
    cd->u.plain.hdr.skip = tgt->u.crypt.iv_offset;
1336
0
    cd->u.plain.hdr.sector_size = tgt->u.crypt.sector_size;
1337
0
    cd->u.plain.key_size = crypt_volume_key_length(tgt->u.crypt.vk);
1338
0
    cd->u.plain.cipher = strdup(cipher);
1339
0
    if (!cd->u.plain.cipher) {
1340
0
      r = -ENOMEM;
1341
0
      goto out;
1342
0
    }
1343
0
    MOVE_REF(cd->u.plain.cipher_spec, cipher_spec);
1344
0
    cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1;
1345
0
    if (dmd.flags & CRYPT_ACTIVATE_KEYRING_KEY)
1346
0
      crypt_set_key_in_keyring(cd, 1);
1347
0
  } else if (isLOOPAES(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1348
0
    cd->u.loopaes.hdr.offset = tgt->u.crypt.offset;
1349
0
    cd->u.loopaes.cipher = strdup(cipher);
1350
0
    if (!cd->u.loopaes.cipher) {
1351
0
      r = -ENOMEM;
1352
0
      goto out;
1353
0
    }
1354
0
    MOVE_REF(cd->u.loopaes.cipher_spec, cipher_spec);
1355
0
    cd->u.loopaes.cipher_mode = cd->u.loopaes.cipher_spec + strlen(cipher) + 1;
1356
    /* version 3 uses last key for IV */
1357
0
    if (crypt_volume_key_length(tgt->u.crypt.vk) % key_nums)
1358
0
      key_nums++;
1359
0
    cd->u.loopaes.key_size = crypt_volume_key_length(tgt->u.crypt.vk) / key_nums;
1360
0
  } else if (isLUKS1(cd->type) || isLUKS2(cd->type)) {
1361
0
    if (crypt_metadata_device(cd)) {
1362
0
      r = _crypt_load_luks(cd, cd->type, true, false);
1363
0
      if (r < 0) {
1364
0
        log_dbg(cd, "LUKS device header does not match active device.");
1365
0
        crypt_set_null_type(cd);
1366
0
        device_close(cd, cd->metadata_device);
1367
0
        device_close(cd, cd->device);
1368
0
        r = 0;
1369
0
        goto out;
1370
0
      }
1371
      /* check whether UUIDs match each other */
1372
0
      r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd));
1373
0
      if (r < 0) {
1374
0
        log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s",
1375
0
          LUKS_UUID(cd), dmd.uuid);
1376
0
        crypt_free_type(cd, NULL);
1377
0
        r = 0;
1378
0
        goto out;
1379
0
      }
1380
0
    } else {
1381
0
      log_dbg(cd, "LUKS device header not available.");
1382
0
      crypt_set_null_type(cd);
1383
0
      r = 0;
1384
0
    }
1385
0
  } else if (isTCRYPT(cd->type) && single_segment(&dmd) && tgt->type == DM_CRYPT) {
1386
0
    r = TCRYPT_init_by_name(cd, name, dmd.uuid, tgt, &cd->device,
1387
0
          &cd->u.tcrypt.params, &cd->u.tcrypt.hdr);
1388
0
  } else if (isBITLK(cd->type)) {
1389
0
    r = _crypt_load_bitlk(cd);
1390
0
    if (r < 0) {
1391
0
      log_dbg(cd, "BITLK device header not available.");
1392
0
      crypt_set_null_type(cd);
1393
0
      r = 0;
1394
0
    }
1395
0
  } else if (isFVAULT2(cd->type)) {
1396
0
    r = _crypt_load_fvault2(cd);
1397
0
    if (r < 0) {
1398
0
      log_dbg(cd, "FVAULT2 device header not available.");
1399
0
      crypt_set_null_type(cd);
1400
0
      r = 0;
1401
0
    }
1402
0
  }
1403
0
out:
1404
0
  dm_targets_free(cd, &dmd);
1405
0
  dm_targets_free(cd, &dmdi);
1406
0
  dm_targets_free(cd, &dmdep);
1407
0
  free(CONST_CAST(void*)dmd.uuid);
1408
0
  free(cipher_spec);
1409
0
  dep = deps;
1410
0
  while (*dep)
1411
0
    free(*dep++);
1412
0
  return r;
1413
0
}
1414
1415
static int _init_by_name_verity(struct crypt_device *cd, const char *name)
1416
0
{
1417
0
  struct crypt_dm_active_device dmd;
1418
0
  struct dm_target *tgt = &dmd.segment;
1419
0
  int r;
1420
1421
0
  r = dm_query_device(cd, name,
1422
0
        DM_ACTIVE_DEVICE |
1423
0
        DM_ACTIVE_VERITY_HASH_DEVICE |
1424
0
        DM_ACTIVE_VERITY_ROOT_HASH |
1425
0
        DM_ACTIVE_VERITY_PARAMS, &dmd);
1426
0
  if (r < 0)
1427
0
    return r;
1428
0
  if (!single_segment(&dmd) || tgt->type != DM_VERITY) {
1429
0
    log_dbg(cd, "Unsupported device table detected in %s.", name);
1430
0
    r = -EINVAL;
1431
0
    goto out;
1432
0
  }
1433
0
  if (r > 0)
1434
0
    r = 0;
1435
1436
0
  if (isVERITY(cd->type)) {
1437
0
    cd->u.verity.uuid = NULL; // FIXME
1438
0
    cd->u.verity.hdr.flags = CRYPT_VERITY_NO_HEADER; //FIXME
1439
0
    cd->u.verity.hdr.data_size = tgt->u.verity.vp->data_size;
1440
0
    cd->u.verity.root_hash_size = tgt->u.verity.root_hash_size;
1441
0
    MOVE_REF(cd->u.verity.hdr.hash_name, tgt->u.verity.vp->hash_name);
1442
0
    cd->u.verity.hdr.data_device = NULL;
1443
0
    cd->u.verity.hdr.hash_device = NULL;
1444
0
    cd->u.verity.hdr.data_block_size = tgt->u.verity.vp->data_block_size;
1445
0
    cd->u.verity.hdr.hash_block_size = tgt->u.verity.vp->hash_block_size;
1446
0
    cd->u.verity.hdr.hash_area_offset = tgt->u.verity.hash_offset;
1447
0
    cd->u.verity.hdr.fec_area_offset = tgt->u.verity.fec_offset;
1448
0
    cd->u.verity.hdr.hash_type = tgt->u.verity.vp->hash_type;
1449
0
    cd->u.verity.hdr.flags = tgt->u.verity.vp->flags;
1450
0
    cd->u.verity.hdr.salt_size = tgt->u.verity.vp->salt_size;
1451
0
    MOVE_REF(cd->u.verity.hdr.salt, tgt->u.verity.vp->salt);
1452
0
    MOVE_REF(cd->u.verity.hdr.fec_device, tgt->u.verity.vp->fec_device);
1453
0
    cd->u.verity.hdr.fec_roots = tgt->u.verity.vp->fec_roots;
1454
0
    MOVE_REF(cd->u.verity.fec_device, tgt->u.verity.fec_device);
1455
0
    MOVE_REF(cd->metadata_device, tgt->u.verity.hash_device);
1456
0
    MOVE_REF(cd->u.verity.root_hash, tgt->u.verity.root_hash);
1457
0
  }
1458
0
out:
1459
0
  dm_targets_free(cd, &dmd);
1460
0
  return r;
1461
0
}
1462
1463
static int _init_by_name_integrity(struct crypt_device *cd, const char *name)
1464
0
{
1465
0
  struct crypt_dm_active_device dmd;
1466
0
  struct dm_target *tgt = &dmd.segment;
1467
0
  int r;
1468
1469
0
  r = dm_query_device(cd, name, DM_ACTIVE_DEVICE |
1470
0
              DM_ACTIVE_CRYPT_KEY |
1471
0
              DM_ACTIVE_CRYPT_KEYSIZE |
1472
0
              DM_ACTIVE_INTEGRITY_PARAMS, &dmd);
1473
0
  if (r < 0)
1474
0
    return r;
1475
0
  if (!single_segment(&dmd) || tgt->type != DM_INTEGRITY) {
1476
0
    log_dbg(cd, "Unsupported device table detected in %s.", name);
1477
0
    r = -EINVAL;
1478
0
    goto out;
1479
0
  }
1480
0
  if (r > 0)
1481
0
    r = 0;
1482
1483
0
  if (isINTEGRITY(cd->type)) {
1484
0
    cd->u.integrity.params.tag_size = tgt->u.integrity.tag_size;
1485
0
    cd->u.integrity.params.sector_size = tgt->u.integrity.sector_size;
1486
0
    cd->u.integrity.params.journal_size = tgt->u.integrity.journal_size;
1487
0
    cd->u.integrity.params.journal_watermark = tgt->u.integrity.journal_watermark;
1488
0
    cd->u.integrity.params.journal_commit_time = tgt->u.integrity.journal_commit_time;
1489
0
    cd->u.integrity.params.interleave_sectors = tgt->u.integrity.interleave_sectors;
1490
0
    cd->u.integrity.params.buffer_sectors = tgt->u.integrity.buffer_sectors;
1491
0
    MOVE_REF(cd->u.integrity.params.integrity, tgt->u.integrity.integrity);
1492
0
    MOVE_REF(cd->u.integrity.params.journal_integrity, tgt->u.integrity.journal_integrity);
1493
0
    MOVE_REF(cd->u.integrity.params.journal_crypt, tgt->u.integrity.journal_crypt);
1494
1495
0
    if (tgt->u.integrity.vk)
1496
0
      cd->u.integrity.params.integrity_key_size = crypt_volume_key_length(tgt->u.integrity.vk);
1497
0
    if (tgt->u.integrity.journal_integrity_key)
1498
0
      cd->u.integrity.params.journal_integrity_key_size = crypt_volume_key_length(tgt->u.integrity.journal_integrity_key);
1499
0
    if (tgt->u.integrity.journal_crypt_key)
1500
0
      cd->u.integrity.params.journal_crypt_key_size = crypt_volume_key_length(tgt->u.integrity.journal_crypt_key);
1501
0
    MOVE_REF(cd->metadata_device, tgt->u.integrity.meta_device);
1502
0
  }
1503
0
out:
1504
0
  dm_targets_free(cd, &dmd);
1505
0
  return r;
1506
0
}
1507
1508
int crypt_init_by_name_and_header(struct crypt_device **cd,
1509
          const char *name,
1510
          const char *header_device)
1511
0
{
1512
0
  crypt_status_info ci;
1513
0
  struct crypt_dm_active_device dmd;
1514
0
  struct dm_target *tgt = &dmd.segment;
1515
0
  const char *type = NULL;
1516
0
  int r;
1517
1518
0
  if (!cd || !name)
1519
0
    return -EINVAL;
1520
1521
0
  log_dbg(NULL, "Allocating crypt device context by device %s.", name);
1522
1523
0
  ci = crypt_status(NULL, name);
1524
0
  if (ci == CRYPT_INVALID)
1525
0
    return -ENODEV;
1526
1527
0
  if (ci < CRYPT_ACTIVE) {
1528
0
    log_err(NULL, _("Device %s is not active."), name);
1529
0
    return -ENODEV;
1530
0
  }
1531
1532
0
  r = dm_query_device(NULL, name, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &dmd);
1533
0
  if (r < 0)
1534
0
    return r;
1535
1536
0
  *cd = NULL;
1537
1538
0
  if (header_device) {
1539
0
    r = crypt_init(cd, header_device);
1540
0
  } else {
1541
0
    r = crypt_init(cd, device_path(tgt->data_device));
1542
1543
    /* Underlying device disappeared but mapping still active */
1544
0
    if (!tgt->data_device || r == -ENOTBLK)
1545
0
      log_verbose(NULL, _("Underlying device for crypt device %s disappeared."),
1546
0
            name);
1547
1548
    /* Underlying device is not readable but crypt mapping exists */
1549
0
    if (r == -ENOTBLK)
1550
0
      r = crypt_init(cd, NULL);
1551
0
  }
1552
1553
0
  if (r < 0)
1554
0
    goto out;
1555
1556
0
  if (dmd.uuid) {
1557
0
    if (!strncmp(CRYPT_PLAIN, dmd.uuid, sizeof(CRYPT_PLAIN)-1))
1558
0
      type = CRYPT_PLAIN;
1559
0
    else if (!strncmp(CRYPT_LOOPAES, dmd.uuid, sizeof(CRYPT_LOOPAES)-1))
1560
0
      type = CRYPT_LOOPAES;
1561
0
    else if (!strncmp(CRYPT_LUKS1, dmd.uuid, sizeof(CRYPT_LUKS1)-1))
1562
0
      type = CRYPT_LUKS1;
1563
0
    else if (!strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1))
1564
0
      type = CRYPT_LUKS2;
1565
0
    else if (!strncmp(CRYPT_VERITY, dmd.uuid, sizeof(CRYPT_VERITY)-1))
1566
0
      type = CRYPT_VERITY;
1567
0
    else if (!strncmp(CRYPT_TCRYPT, dmd.uuid, sizeof(CRYPT_TCRYPT)-1))
1568
0
      type = CRYPT_TCRYPT;
1569
0
    else if (!strncmp(CRYPT_INTEGRITY, dmd.uuid, sizeof(CRYPT_INTEGRITY)-1))
1570
0
      type = CRYPT_INTEGRITY;
1571
0
    else if (!strncmp(CRYPT_BITLK, dmd.uuid, sizeof(CRYPT_BITLK)-1))
1572
0
      type = CRYPT_BITLK;
1573
0
    else if (!strncmp(CRYPT_FVAULT2, dmd.uuid, sizeof(CRYPT_FVAULT2)-1))
1574
0
      type = CRYPT_FVAULT2;
1575
0
    else
1576
0
      log_dbg(NULL, "Unknown UUID set, some parameters are not set.");
1577
0
  } else
1578
0
    log_dbg(NULL, "Active device has no UUID set, some parameters are not set.");
1579
1580
0
  if (type) {
1581
0
    (*cd)->type = strdup(type);
1582
0
    if (!(*cd)->type) {
1583
0
      r = -ENOMEM;
1584
0
      goto out;
1585
0
    }
1586
0
  }
1587
1588
0
  if (header_device) {
1589
0
    r = crypt_set_data_device(*cd, device_path(tgt->data_device));
1590
0
    if (r < 0)
1591
0
      goto out;
1592
0
  }
1593
1594
  /* Try to initialize basic parameters from active device */
1595
1596
0
  if (tgt->type == DM_CRYPT || tgt->type == DM_LINEAR)
1597
0
    r = _init_by_name_crypt(*cd, name);
1598
0
  else if (tgt->type == DM_VERITY)
1599
0
    r = _init_by_name_verity(*cd, name);
1600
0
  else if (tgt->type == DM_INTEGRITY)
1601
0
    r = _init_by_name_integrity(*cd, name);
1602
0
out:
1603
0
  if (r == 0 && !(*cd)->type) {
1604
    /* For anonymous device (no header found) remember initialized name */
1605
0
    (*cd)->u.none.active_name = strdup(name);
1606
0
    if (!(*cd)->u.none.active_name)
1607
0
      r = -ENOMEM;
1608
0
  }
1609
1610
0
  if (r < 0) {
1611
0
    crypt_free(*cd);
1612
0
    *cd = NULL;
1613
0
  }
1614
1615
0
  free(CONST_CAST(void*)dmd.uuid);
1616
0
  dm_targets_free(NULL, &dmd);
1617
0
  return r;
1618
0
}
1619
1620
int crypt_init_by_name(struct crypt_device **cd, const char *name)
1621
0
{
1622
0
  return crypt_init_by_name_and_header(cd, name, NULL);
1623
0
}
1624
1625
/*
1626
 * crypt_format() helpers
1627
 */
1628
static int _crypt_format_plain(struct crypt_device *cd,
1629
             const char *cipher,
1630
             const char *cipher_mode,
1631
             const char *uuid,
1632
             size_t volume_key_size,
1633
             struct crypt_params_plain *params)
1634
0
{
1635
0
  unsigned int sector_size = params ? params->sector_size : SECTOR_SIZE;
1636
0
  uint64_t dev_size;
1637
1638
0
  if (!cipher || !cipher_mode) {
1639
0
    log_err(cd, _("Invalid plain crypt parameters."));
1640
0
    return -EINVAL;
1641
0
  }
1642
1643
0
  if (volume_key_size > 1024) {
1644
0
    log_err(cd, _("Invalid key size."));
1645
0
    return -EINVAL;
1646
0
  }
1647
1648
0
  if (uuid) {
1649
0
    log_err(cd, _("UUID is not supported for this crypt type."));
1650
0
    return -EINVAL;
1651
0
  }
1652
1653
0
  if (cd->metadata_device) {
1654
0
    log_err(cd, _("Detached metadata device is not supported for this crypt type."));
1655
0
    return -EINVAL;
1656
0
  }
1657
1658
  /* For compatibility with old params structure */
1659
0
  if (!sector_size)
1660
0
    sector_size = SECTOR_SIZE;
1661
1662
0
  if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
1663
0
      NOTPOW2(sector_size)) {
1664
0
    log_err(cd, _("Unsupported encryption sector size."));
1665
0
    return -EINVAL;
1666
0
  }
1667
1668
0
  if (sector_size > SECTOR_SIZE && !device_size(cd->device, &dev_size)) {
1669
0
    if (params && params->offset) {
1670
0
      if (params->offset > (UINT64_MAX / SECTOR_SIZE))
1671
0
        return -EINVAL;
1672
0
      if (dev_size < (params->offset * SECTOR_SIZE))
1673
0
        return -EINVAL;
1674
0
      dev_size -= (params->offset * SECTOR_SIZE);
1675
0
    }
1676
0
    if (dev_size % sector_size) {
1677
0
      log_err(cd, _("Device size is not aligned to requested sector size."));
1678
0
      return -EINVAL;
1679
0
    }
1680
0
    device_set_block_size(crypt_data_device(cd), sector_size);
1681
0
  }
1682
1683
0
  if (!(cd->type = strdup(CRYPT_PLAIN)))
1684
0
    return -ENOMEM;
1685
1686
0
  cd->u.plain.key_size = volume_key_size;
1687
0
  cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
1688
0
  if (!cd->volume_key)
1689
0
    return -ENOMEM;
1690
1691
0
  if (asprintf(&cd->u.plain.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1692
0
    cd->u.plain.cipher_spec = NULL;
1693
0
    return -ENOMEM;
1694
0
  }
1695
0
  cd->u.plain.cipher = strdup(cipher);
1696
0
  if (!cd->u.plain.cipher)
1697
0
    return -ENOMEM;
1698
1699
0
  cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1;
1700
1701
0
  if (params && params->hash) {
1702
0
    cd->u.plain.hdr.hash = strdup(params->hash);
1703
0
    if (!cd->u.plain.hdr.hash) {
1704
0
      free(cd->u.plain.cipher);
1705
0
      cd->u.plain.cipher = NULL;
1706
0
      return -ENOMEM;
1707
0
    }
1708
0
  }
1709
1710
0
  cd->u.plain.hdr.offset = params ? params->offset : 0;
1711
0
  cd->u.plain.hdr.skip = params ? params->skip : 0;
1712
0
  cd->u.plain.hdr.size = params ? params->size : 0;
1713
0
  cd->u.plain.hdr.sector_size = sector_size;
1714
1715
1716
0
  return 0;
1717
0
}
1718
1719
static int _crypt_format_luks1(struct crypt_device *cd,
1720
             const char *cipher,
1721
             const char *cipher_mode,
1722
             const char *uuid,
1723
             const char *volume_key,
1724
             size_t volume_key_size,
1725
             struct crypt_params_luks1 *params)
1726
0
{
1727
0
  int r;
1728
0
  unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1729
0
  unsigned long alignment_offset = 0;
1730
0
  uint64_t dev_size;
1731
1732
0
  if (!cipher || !cipher_mode)
1733
0
    return -EINVAL;
1734
1735
0
  if (!crypt_metadata_device(cd)) {
1736
0
    log_err(cd, _("Can't format LUKS without device."));
1737
0
    return -EINVAL;
1738
0
  }
1739
1740
0
  if (device_is_zoned(crypt_metadata_device(cd)) > 0) {
1741
0
    log_err(cd, _("Zoned device %s cannot be used for LUKS header."),
1742
0
      device_path(crypt_metadata_device(cd)));
1743
0
    return -EINVAL;
1744
0
  }
1745
1746
0
  if (params && cd->data_offset && params->data_alignment &&
1747
0
     (cd->data_offset % params->data_alignment)) {
1748
0
    log_err(cd, _("Requested data alignment is not compatible with data offset."));
1749
0
    return -EINVAL;
1750
0
  }
1751
1752
0
  if (!(cd->type = strdup(CRYPT_LUKS1)))
1753
0
    return -ENOMEM;
1754
1755
0
  if (volume_key)
1756
0
    cd->volume_key = crypt_alloc_volume_key(volume_key_size,
1757
0
                  volume_key);
1758
0
  else
1759
0
    cd->volume_key = crypt_generate_volume_key(cd, volume_key_size, KEY_QUALITY_KEY);
1760
1761
0
  if (!cd->volume_key)
1762
0
    return -ENOMEM;
1763
1764
0
  if (verify_pbkdf_params(cd, &cd->pbkdf)) {
1765
0
    r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1);
1766
0
    if (r)
1767
0
      return r;
1768
0
  }
1769
1770
0
  if (params && params->hash && strcmp(params->hash, cd->pbkdf.hash)) {
1771
0
    free(CONST_CAST(void*)cd->pbkdf.hash);
1772
0
    cd->pbkdf.hash = strdup(params->hash);
1773
0
    if (!cd->pbkdf.hash)
1774
0
      return -ENOMEM;
1775
0
  }
1776
1777
0
  if (params && params->data_device) {
1778
0
    if (!cd->metadata_device)
1779
0
      cd->metadata_device = cd->device;
1780
0
    else
1781
0
      device_free(cd, cd->device);
1782
0
    cd->device = NULL;
1783
0
    if (device_alloc(cd, &cd->device, params->data_device) < 0)
1784
0
      return -ENOMEM;
1785
0
  }
1786
1787
0
  if (device_is_dax(crypt_data_device(cd)) > 0)
1788
0
    log_std(cd, _("WARNING: DAX device can corrupt data as it does not guarantee atomic sector updates.\n"));
1789
1790
0
  if (params && cd->metadata_device) {
1791
    /* For detached header the alignment is used directly as data offset */
1792
0
    if (!cd->data_offset)
1793
0
      cd->data_offset = params->data_alignment;
1794
0
    required_alignment = params->data_alignment * SECTOR_SIZE;
1795
0
  } else if (params && params->data_alignment) {
1796
0
    required_alignment = params->data_alignment * SECTOR_SIZE;
1797
0
  } else
1798
0
    device_topology_alignment(cd, cd->device,
1799
0
               &required_alignment,
1800
0
               &alignment_offset, DEFAULT_DISK_ALIGNMENT);
1801
1802
0
  r = LUKS_check_cipher(cd, volume_key_size, cipher, cipher_mode);
1803
0
  if (r < 0)
1804
0
    return r;
1805
1806
0
  r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
1807
0
             cd->pbkdf.hash, uuid,
1808
0
             cd->data_offset * SECTOR_SIZE,
1809
0
             alignment_offset, required_alignment, cd);
1810
0
  if (r < 0)
1811
0
    return r;
1812
1813
0
  r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
1814
0
  if (r < 0)
1815
0
    return r;
1816
1817
1818
0
  if (asprintf(&cd->u.luks1.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
1819
0
    cd->u.luks1.cipher_spec = NULL;
1820
0
    return -ENOMEM;
1821
0
  }
1822
1823
0
  r = LUKS_wipe_header_areas(&cd->u.luks1.hdr, cd);
1824
0
  if (r < 0) {
1825
0
    free(cd->u.luks1.cipher_spec);
1826
0
    log_err(cd, _("Cannot wipe header on device %s."),
1827
0
      mdata_device_path(cd));
1828
0
    return r;
1829
0
  }
1830
1831
0
  r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
1832
0
  if (r) {
1833
0
    free(cd->u.luks1.cipher_spec);
1834
0
    return r;
1835
0
  }
1836
1837
0
  if (!device_size(crypt_data_device(cd), &dev_size) &&
1838
0
      dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
1839
0
    log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
1840
0
            device_path(crypt_data_device(cd)));
1841
1842
0
  return 0;
1843
0
}
1844
1845
static int LUKS2_check_encryption_params(struct crypt_device *cd,
1846
  const char *cipher,
1847
  const char *cipher_mode,
1848
  const char *integrity,
1849
  size_t required_integrity_key_size,
1850
  size_t volume_key_size,
1851
  const struct crypt_params_luks2 *params,
1852
  const char **ret_integrity,
1853
  size_t *ret_integrity_key_size)
1854
0
{
1855
0
  int r, integrity_key_size = 0;
1856
1857
0
  assert(cipher);
1858
0
  assert(cipher_mode);
1859
0
  assert(ret_integrity);
1860
1861
0
  if (integrity) {
1862
0
    if (params->integrity_params) {
1863
      /* Standalone dm-integrity must not be used */
1864
0
      if (params->integrity_params->integrity)
1865
0
        return -EINVAL;
1866
      /* FIXME: journal encryption and MAC is here not yet supported */
1867
0
      if (params->integrity_params->journal_crypt ||
1868
0
      params->integrity_params->journal_integrity)
1869
0
        return -ENOTSUP;
1870
0
    }
1871
0
    if (!INTEGRITY_tag_size(integrity, cipher, cipher_mode)) {
1872
      /* merge "none" string into NULL to make branching logic is easier */
1873
0
      if (!strcmp(integrity, "none"))
1874
0
        integrity = NULL;
1875
0
      else
1876
0
        return -EINVAL;
1877
0
    }
1878
0
    integrity_key_size = INTEGRITY_key_size(integrity, required_integrity_key_size);
1879
0
    if ((integrity_key_size < 0) || (integrity_key_size >= (int)volume_key_size)) {
1880
0
      log_err(cd, _("Volume key is too small for encryption with integrity extensions."));
1881
0
      return -EINVAL;
1882
0
    }
1883
0
    if (integrity_key_size && integrity_key_size < LUKS2_MIN_INTEGRITY_KEY_BYTES) {
1884
0
      log_err(cd, _("Integrity key size is too small."));
1885
0
      return -EINVAL;
1886
0
    }
1887
0
  }
1888
1889
  /* FIXME: allow this later also for normal ciphers (check AF_ALG availability. */
1890
0
  if (integrity && integrity_key_size == 0) {
1891
0
    r = crypt_cipher_check_kernel(cipher, cipher_mode, integrity, volume_key_size);
1892
0
    if (r < 0 && r != -ENOTSUP) {
1893
0
      log_err(cd, _("Cipher %s-%s (key size %zd bits) is not available."),
1894
0
        cipher, cipher_mode, volume_key_size * 8);
1895
0
      return r;
1896
0
    }
1897
0
  }
1898
1899
0
  if ((!integrity || integrity_key_size) && !crypt_cipher_wrapped_key(cipher, cipher_mode) &&
1900
0
      !INTEGRITY_tag_size(NULL, cipher, cipher_mode)) {
1901
0
    r = LUKS_check_cipher(cd, volume_key_size - integrity_key_size,
1902
0
              cipher, cipher_mode);
1903
0
    if (r < 0)
1904
0
      return r;
1905
0
  }
1906
1907
0
  *ret_integrity = integrity;
1908
0
  if (ret_integrity_key_size)
1909
0
    *ret_integrity_key_size = required_integrity_key_size ? integrity_key_size : 0;
1910
1911
0
  return 0;
1912
0
}
1913
1914
static int LUKS2_check_encryption_sector(struct crypt_device *cd, uint64_t device_size_bytes,
1915
    uint64_t data_offset_bytes, uint32_t sector_size, bool modify_sector_size,
1916
    bool verify_data_area_alignment, uint32_t *ret_sector_size)
1917
0
{
1918
0
  uint64_t dmc_flags;
1919
1920
0
  assert(ret_sector_size);
1921
1922
0
  if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
1923
0
      NOTPOW2(sector_size)) {
1924
0
    log_err(cd, _("Unsupported encryption sector size."));
1925
0
    return -EINVAL;
1926
0
  }
1927
1928
0
  if (sector_size != SECTOR_SIZE && !dm_flags(cd, DM_CRYPT, &dmc_flags) &&
1929
0
      !(dmc_flags & DM_SECTOR_SIZE_SUPPORTED)) {
1930
0
    if (modify_sector_size) {
1931
0
      log_dbg(cd, "dm-crypt does not support encryption sector size option. Reverting to 512 bytes.");
1932
0
      sector_size = SECTOR_SIZE;
1933
0
    } else
1934
0
      log_std(cd, _("WARNING: The device activation will fail, dm-crypt is missing "
1935
0
              "support for requested encryption sector size.\n"));
1936
0
  }
1937
1938
0
  if (modify_sector_size) {
1939
0
    if (data_offset_bytes && MISALIGNED(data_offset_bytes, sector_size)) {
1940
0
      log_dbg(cd, "Data offset not aligned to sector size. Reverting to 512 bytes.");
1941
0
      sector_size = SECTOR_SIZE;
1942
0
    } else if (MISALIGNED(device_size_bytes - data_offset_bytes, sector_size)) {
1943
      /* underflow does not affect misalignment checks */
1944
0
      log_dbg(cd, "Device size is not aligned to sector size. Reverting to 512 bytes.");
1945
0
      sector_size = SECTOR_SIZE;
1946
0
    }
1947
0
  }
1948
1949
  /* underflow does not affect misalignment checks */
1950
0
  if (verify_data_area_alignment &&
1951
0
      sector_size > SECTOR_SIZE &&
1952
0
      MISALIGNED(device_size_bytes - data_offset_bytes, sector_size)) {
1953
0
         log_err(cd, _("Device size is not aligned to requested sector size."));
1954
0
         return -EINVAL;
1955
0
  }
1956
1957
0
  *ret_sector_size = sector_size;
1958
1959
0
  return 0;
1960
0
}
1961
1962
static int _crypt_format_luks2(struct crypt_device *cd,
1963
             const char *cipher,
1964
             const char *cipher_mode,
1965
             const char *uuid,
1966
             const char *volume_key,
1967
             size_t volume_key_size,
1968
             struct crypt_params_luks2 *params,
1969
             bool sector_size_autodetect, bool integrity_inline)
1970
0
{
1971
0
  int r;
1972
0
  unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
1973
0
  unsigned long alignment_offset = 0;
1974
0
  unsigned int sector_size;
1975
0
  char cipher_spec[2*MAX_CAPI_ONE_LEN];
1976
0
  const char *integrity = params ? params->integrity : NULL;
1977
0
  size_t integrity_key_size = 0; /* only for independent, separate key in HMAC */
1978
0
  struct volume_key *integrity_key = NULL;
1979
0
  uint64_t data_offset_bytes, dev_size, metadata_size_bytes, keyslots_size_bytes;
1980
1981
0
  cd->u.luks2.hdr.jobj = NULL;
1982
0
  cd->u.luks2.keyslot_cipher = NULL;
1983
1984
0
  if (!cipher || !cipher_mode)
1985
0
    return -EINVAL;
1986
1987
0
  if (!crypt_metadata_device(cd)) {
1988
0
    log_err(cd, _("Can't format LUKS without device."));
1989
0
    return -EINVAL;
1990
0
  }
1991
1992
0
  if (device_is_zoned(crypt_metadata_device(cd)) > 0) {
1993
0
    log_err(cd, _("Zoned device %s cannot be used for LUKS header."),
1994
0
      device_path(crypt_metadata_device(cd)));
1995
0
    return -EINVAL;
1996
0
  }
1997
1998
0
  if (params && cd->data_offset && params->data_alignment &&
1999
0
     (cd->data_offset % params->data_alignment)) {
2000
0
    log_err(cd, _("Requested data alignment is not compatible with data offset."));
2001
0
    return -EINVAL;
2002
0
  }
2003
2004
0
  if (params && params->sector_size)
2005
0
    sector_size_autodetect = false;
2006
2007
0
  if (params && params->data_device) {
2008
0
    if (!cd->metadata_device)
2009
0
      cd->metadata_device = cd->device;
2010
0
    else
2011
0
      device_free(cd, cd->device);
2012
0
    cd->device = NULL;
2013
0
    if (device_alloc(cd, &cd->device, params->data_device) < 0)
2014
0
      return -ENOMEM;
2015
0
  }
2016
2017
0
  if (device_is_dax(crypt_data_device(cd)) > 0)
2018
0
    log_std(cd, _("WARNING: DAX device can corrupt data as it does not guarantee atomic sector updates.\n"));
2019
2020
0
  if (sector_size_autodetect) {
2021
0
    sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd));
2022
0
    log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.",
2023
0
      device_path(crypt_data_device(cd)), sector_size);
2024
0
  } else
2025
0
    sector_size = params ? params->sector_size : SECTOR_SIZE;
2026
2027
0
  r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
2028
0
  if (r < 0)
2029
0
    return r;
2030
2031
0
  if (!(cd->type = strdup(CRYPT_LUKS2)))
2032
0
    return -ENOMEM;
2033
2034
0
  if (volume_key)
2035
0
    cd->volume_key = crypt_alloc_volume_key(volume_key_size,
2036
0
                  volume_key);
2037
0
  else
2038
0
    cd->volume_key = crypt_generate_volume_key(cd, volume_key_size, KEY_QUALITY_KEY);
2039
2040
0
  if (!cd->volume_key)
2041
0
    return -ENOMEM;
2042
2043
0
  if (params && params->pbkdf)
2044
0
    r = crypt_set_pbkdf_type(cd, params->pbkdf);
2045
0
  else if (verify_pbkdf_params(cd, &cd->pbkdf))
2046
0
    r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
2047
2048
0
  if (r < 0)
2049
0
    return r;
2050
2051
0
  if (params && cd->metadata_device) {
2052
    /* For detached header the alignment is used directly as data offset */
2053
0
    if (!cd->data_offset)
2054
0
      cd->data_offset = params->data_alignment;
2055
0
    required_alignment = params->data_alignment * SECTOR_SIZE;
2056
0
  } else if (params && params->data_alignment) {
2057
0
    required_alignment = params->data_alignment * SECTOR_SIZE;
2058
0
  } else
2059
0
    device_topology_alignment(cd, cd->device,
2060
0
               &required_alignment,
2061
0
               &alignment_offset, DEFAULT_DISK_ALIGNMENT);
2062
2063
0
  if (params && params->integrity_params && params->integrity_params->integrity_key_size)
2064
0
    integrity_key_size = params->integrity_params->integrity_key_size;
2065
2066
0
  r = LUKS2_check_encryption_params(cd, cipher, cipher_mode, integrity, integrity_key_size,
2067
0
            volume_key_size, params, &integrity, &integrity_key_size);
2068
0
  if (r < 0)
2069
0
    goto out;
2070
2071
0
  r = device_size(crypt_data_device(cd), &dev_size);
2072
0
  if (r < 0)
2073
0
    goto out;
2074
2075
0
  r = LUKS2_hdr_get_storage_params(cd, alignment_offset, required_alignment,
2076
0
           &metadata_size_bytes, &keyslots_size_bytes, &data_offset_bytes);
2077
0
  if (r < 0)
2078
0
    goto out;
2079
2080
0
  r = LUKS2_check_encryption_sector(cd, dev_size, data_offset_bytes, sector_size,
2081
0
            sector_size_autodetect, integrity == NULL,
2082
0
            &sector_size);
2083
0
  if (r < 0)
2084
0
    goto out;
2085
2086
0
  if (*cipher_mode != '\0')
2087
0
    r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode);
2088
0
  else
2089
0
    r = snprintf(cipher_spec, sizeof(cipher_spec), "%s", cipher);
2090
0
  if (r < 0 || (size_t)r >= sizeof(cipher_spec)) {
2091
0
    r = -EINVAL;
2092
0
    goto out;
2093
0
  }
2094
2095
0
  r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key,
2096
0
             cipher_spec,
2097
0
             integrity, integrity_key_size,
2098
0
             uuid,
2099
0
             sector_size,
2100
0
             data_offset_bytes,
2101
0
             metadata_size_bytes, keyslots_size_bytes,
2102
0
             0, 0, 0);
2103
0
  if (r < 0)
2104
0
    goto out;
2105
2106
0
  if (integrity_inline) {
2107
0
    log_dbg(cd, "Adding LUKS2 inline HW tags requirement flag.");
2108
0
    r = LUKS2_config_set_requirement_version(cd, &cd->u.luks2.hdr,
2109
0
      CRYPT_REQUIREMENT_INLINE_HW_TAGS, 1, false);
2110
0
    if (r < 0)
2111
0
      goto out;
2112
0
  }
2113
2114
0
  if (params && (params->label || params->subsystem)) {
2115
0
    r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr,
2116
0
             params->label, params->subsystem, 0);
2117
0
    if (r < 0)
2118
0
      goto out;
2119
0
  }
2120
2121
0
  device_set_block_size(crypt_data_device(cd), sector_size);
2122
2123
0
  r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr);
2124
0
  if (r < 0) {
2125
0
    log_err(cd, _("Cannot wipe header on device %s."),
2126
0
      mdata_device_path(cd));
2127
0
    if (dev_size < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr))
2128
0
      log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd)));
2129
0
    goto out;
2130
0
  }
2131
2132
  /* Wipe integrity superblock and create integrity superblock */
2133
0
  if (crypt_get_integrity_tag_size(cd)) {
2134
0
    r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO,
2135
0
              crypt_get_data_offset(cd) * SECTOR_SIZE,
2136
0
              8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
2137
0
    if (r < 0) {
2138
0
      if (r == -EBUSY)
2139
0
        log_err(cd, _("Cannot format device %s in use."),
2140
0
          data_device_path(cd));
2141
0
      else if (r == -EACCES) {
2142
0
        log_err(cd, _("Cannot format device %s, permission denied."),
2143
0
          data_device_path(cd));
2144
0
        r = -EINVAL;
2145
0
      } else
2146
0
        log_err(cd, _("Cannot wipe header on device %s."),
2147
0
          data_device_path(cd));
2148
2149
0
      goto out;
2150
0
    }
2151
0
  }
2152
2153
  /* Format underlying virtual dm-integrity device */
2154
0
  if (!integrity_inline && crypt_get_integrity_tag_size(cd)) {
2155
0
    if (integrity_key_size) {
2156
0
      integrity_key = crypt_alloc_volume_key(integrity_key_size,
2157
0
          crypt_volume_key_get_key(cd->volume_key) + volume_key_size - integrity_key_size);
2158
0
      if (!integrity_key) {
2159
0
        r = -ENOMEM;
2160
0
        goto out;
2161
0
      }
2162
0
    }
2163
0
    r = INTEGRITY_format(cd, params ? params->integrity_params : NULL,
2164
0
             integrity_key, NULL, NULL, 0, NULL, false);
2165
0
    if (r)
2166
0
      log_err(cd, _("Cannot format integrity for device %s."),
2167
0
        data_device_path(cd));
2168
0
    crypt_free_volume_key(integrity_key);
2169
0
  }
2170
2171
0
  if (r < 0)
2172
0
    goto out;
2173
2174
  /* override sequence id check with format */
2175
0
  r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr);
2176
0
  if (r < 0) {
2177
0
    if (r == -EBUSY)
2178
0
      log_err(cd, _("Cannot format device %s in use."),
2179
0
        mdata_device_path(cd));
2180
0
    else if (r == -EACCES) {
2181
0
      log_err(cd, _("Cannot format device %s, permission denied."),
2182
0
        mdata_device_path(cd));
2183
0
      r = -EINVAL;
2184
0
    } else
2185
0
      log_err(cd, _("Cannot format device %s."),
2186
0
        mdata_device_path(cd));
2187
0
  }
2188
2189
0
out:
2190
0
  if (r) {
2191
0
    LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
2192
0
    return r;
2193
0
  }
2194
2195
  /* Device size can be larger now if it is a file container */
2196
0
  if (!device_size(crypt_data_device(cd), &dev_size) &&
2197
0
      dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
2198
0
    log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
2199
0
            device_path(crypt_data_device(cd)));
2200
2201
0
  return 0;
2202
0
}
2203
2204
static int opal_topology_alignment(struct crypt_device *cd,
2205
           uint64_t partition_offset_sectors,
2206
           uint64_t data_offset_sectors,
2207
           uint64_t required_alignment_sectors,
2208
           uint64_t default_alignment_bytes,
2209
           uint64_t *ret_alignment_offset_bytes,
2210
           uint64_t *ret_alignment_bytes,
2211
           uint32_t *ret_opal_block_bytes,
2212
           uint64_t *ret_opal_alignment_granularity_blocks)
2213
0
{
2214
0
  bool opal_align;
2215
0
  int r;
2216
0
  uint32_t opal_block_bytes, device_block_bytes;
2217
0
  uint64_t opal_alignment_granularity_blocks, opal_lowest_lba_blocks;
2218
2219
0
  assert(cd);
2220
0
  assert(ret_alignment_offset_bytes);
2221
0
  assert(ret_alignment_bytes);
2222
0
  assert(ret_opal_block_bytes);
2223
0
  assert(ret_opal_alignment_granularity_blocks);
2224
2225
0
  r = opal_geometry(cd, crypt_data_device(cd), &opal_align, &opal_block_bytes,
2226
0
        &opal_alignment_granularity_blocks, &opal_lowest_lba_blocks);
2227
0
  if (r) {
2228
0
    log_err(cd, _("Cannot get OPAL alignment parameters."));
2229
0
    return -EINVAL;
2230
0
  }
2231
2232
0
  device_block_bytes = device_block_size(cd, crypt_data_device(cd));
2233
2234
0
  log_dbg(cd, "OPAL geometry: alignment: '%c', logical block size: %" PRIu32 "/%" PRIu32
2235
0
        ", alignment granularity: %" PRIu64 ", lowest aligned LBA: %" PRIu64,
2236
0
        opal_align ? 'y' : 'n', opal_block_bytes, device_block_bytes,
2237
0
        opal_alignment_granularity_blocks, opal_lowest_lba_blocks);
2238
2239
0
  if (opal_block_bytes < SECTOR_SIZE || NOTPOW2(opal_block_bytes)) {
2240
0
    log_err(cd, _("Bogus OPAL logical block size."));
2241
0
    return -EINVAL;
2242
0
  }
2243
2244
0
  if (device_block_bytes != opal_block_bytes) {
2245
0
    log_err(cd, _("Bogus OPAL logical block size differs from device block size."));
2246
0
    return -EINVAL;
2247
0
  }
2248
2249
0
  if (data_offset_sectors &&
2250
0
      MISALIGNED(data_offset_sectors + partition_offset_sectors, opal_block_bytes / SECTOR_SIZE)) {
2251
0
    log_err(cd, _("Requested data offset is not compatible with OPAL block size."));
2252
0
    return -EINVAL;
2253
0
  }
2254
2255
  /* Data offset has priority over data alignment parameter */
2256
0
  if (!data_offset_sectors &&
2257
0
      MISALIGNED(required_alignment_sectors, opal_block_bytes / SECTOR_SIZE)) {
2258
0
    log_err(cd, _("Requested data alignment is not compatible with OPAL alignment."));
2259
0
    return -EINVAL;
2260
0
  }
2261
2262
0
  if (!opal_align) {
2263
    /* For detached header the alignment is used directly as data offset */
2264
0
    if (required_alignment_sectors || cd->metadata_device)
2265
0
      *ret_alignment_bytes = required_alignment_sectors * SECTOR_SIZE;
2266
0
    else
2267
0
      *ret_alignment_bytes = default_alignment_bytes;
2268
0
    *ret_alignment_offset_bytes = 0;
2269
0
    *ret_opal_block_bytes = opal_block_bytes;
2270
0
    *ret_opal_alignment_granularity_blocks = 1;
2271
0
    return 0;
2272
0
  }
2273
2274
0
  if (data_offset_sectors) {
2275
0
    if (MISALIGNED((((data_offset_sectors + partition_offset_sectors) * SECTOR_SIZE) / opal_block_bytes) - opal_lowest_lba_blocks,
2276
0
             opal_alignment_granularity_blocks)) {
2277
      // FIXME: Add hint to user on how to fix it
2278
0
      log_err(cd, _("Data offset does not satisfy OPAL alignment requirements."));
2279
0
      return -EINVAL;
2280
0
    }
2281
2282
0
    *ret_alignment_offset_bytes = 0;
2283
0
    *ret_alignment_bytes = 0;
2284
0
    *ret_opal_block_bytes = opal_block_bytes;
2285
0
    *ret_opal_alignment_granularity_blocks = opal_alignment_granularity_blocks;
2286
2287
0
    return 0;
2288
0
  }
2289
2290
0
  if (MISALIGNED(required_alignment_sectors * SECTOR_SIZE, opal_block_bytes * opal_alignment_granularity_blocks)) {
2291
0
    log_err(cd, _("Requested data alignment does not satisfy locking range alignment requirements."));
2292
0
    return -EINVAL;
2293
0
  }
2294
2295
  /* For detached header the alignment is used directly as data offset */
2296
0
  if (required_alignment_sectors || cd->metadata_device)
2297
0
    *ret_alignment_bytes = required_alignment_sectors * SECTOR_SIZE;
2298
0
  else
2299
0
    *ret_alignment_bytes = size_round_up(default_alignment_bytes, opal_block_bytes * opal_alignment_granularity_blocks);
2300
2301
  /* data offset is not set, calculate proper alignment */
2302
0
  *ret_alignment_offset_bytes = (partition_offset_sectors * SECTOR_SIZE) % (opal_block_bytes * opal_alignment_granularity_blocks);
2303
0
  if (*ret_alignment_offset_bytes)
2304
0
    *ret_alignment_offset_bytes = opal_block_bytes * opal_alignment_granularity_blocks - *ret_alignment_offset_bytes;
2305
2306
0
  if (*ret_alignment_offset_bytes)
2307
0
    log_dbg(cd, "Compensating misaligned partition offset by %" PRIu64 "bytes.",
2308
0
      *ret_alignment_offset_bytes);
2309
2310
0
  *ret_alignment_offset_bytes += (opal_lowest_lba_blocks * opal_block_bytes);
2311
0
  *ret_opal_block_bytes = opal_block_bytes;
2312
0
  *ret_opal_alignment_granularity_blocks = opal_alignment_granularity_blocks;
2313
2314
0
  log_dbg(cd, "OPAL alignment (%" PRIu32 "/%" PRIu64 "), offset = %" PRIu64 ". Required alignment is %" PRIu64 ".",
2315
0
    opal_block_bytes, opal_alignment_granularity_blocks, *ret_alignment_offset_bytes, *ret_alignment_bytes);
2316
2317
0
  return 0;
2318
0
}
2319
2320
int crypt_format_luks2_opal(struct crypt_device *cd,
2321
            const char *cipher,
2322
            const char *cipher_mode,
2323
            const char *uuid,
2324
            const char *volume_keys,
2325
            size_t volume_keys_size,
2326
            struct crypt_params_luks2 *params,
2327
            struct crypt_params_hw_opal *opal_params)
2328
0
{
2329
0
  bool opal_range_reset = false, subsystem_overridden = false, sector_size_autodetect = cipher != NULL;
2330
0
  int r;
2331
0
  char cipher_spec[128];
2332
0
  const char *integrity = params ? params->integrity : NULL;
2333
0
  size_t integrity_key_size = 0; /* only for independent, separate key in HMAC */
2334
0
  struct volume_key *integrity_key = NULL;
2335
0
  uint8_t opal_requirement_version;
2336
0
  uint32_t sector_size, opal_block_bytes, opal_segment_number = 1; /* We'll use the partition number if available later */
2337
0
  uint64_t alignment_offset_bytes, data_offset_bytes, device_size_bytes, opal_alignment_granularity_blocks,
2338
0
     partition_offset_sectors, range_offset_blocks, range_size_bytes,
2339
0
     required_alignment_bytes, metadata_size_bytes, keyslots_size_bytes,
2340
0
     provided_data_sectors;
2341
0
  struct volume_key *user_key = NULL;
2342
0
  struct crypt_lock_handle *opal_lh = NULL;
2343
2344
0
  if (!cd || !params || !opal_params ||
2345
0
      !opal_params->admin_key || !opal_params->admin_key_size || !opal_params->user_key_size)
2346
0
    return -EINVAL;
2347
2348
0
  if (cd->type) {
2349
0
    log_dbg(cd, "Context already formatted as %s.", cd->type);
2350
0
    return -EINVAL;
2351
0
  }
2352
2353
0
  log_dbg(cd, "Formatting device %s as type LUKS2 with OPAL HW encryption.", mdata_device_path(cd) ?: "(none)");
2354
2355
0
  r = init_crypto(cd);
2356
0
  if (r < 0)
2357
0
    return r;
2358
2359
0
  if (volume_keys_size < opal_params->user_key_size)
2360
0
    return -EINVAL;
2361
2362
0
  if (cipher && (volume_keys_size == opal_params->user_key_size))
2363
0
    return -EINVAL;
2364
2365
0
  if (!crypt_metadata_device(cd)) {
2366
0
    log_err(cd, _("Can't format LUKS without device."));
2367
0
    return -EINVAL;
2368
0
  }
2369
2370
0
  if (params->data_alignment &&
2371
0
      MISALIGNED(cd->data_offset, params->data_alignment)) {
2372
0
    log_err(cd, _("Requested data alignment is not compatible with data offset."));
2373
0
    return -EINVAL;
2374
0
  }
2375
2376
0
  if (params->data_device) {
2377
0
    if (!cd->metadata_device)
2378
0
      cd->metadata_device = cd->device;
2379
0
    else
2380
0
      device_free(cd, cd->device);
2381
0
    cd->device = NULL;
2382
0
    if (device_alloc(cd, &cd->device, params->data_device) < 0)
2383
0
      return -ENOMEM;
2384
0
  }
2385
2386
0
  r = crypt_opal_supported(cd, crypt_data_device(cd));
2387
0
  if (r < 0)
2388
0
    return r;
2389
2390
0
  if (params->sector_size)
2391
0
    sector_size_autodetect = false;
2392
2393
0
  partition_offset_sectors = crypt_dev_partition_offset(device_path(crypt_data_device(cd)));
2394
2395
0
  r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
2396
0
  if (r < 0)
2397
0
    return r;
2398
2399
  /*
2400
   * Check both data and metadata devices for exclusive access since
2401
   * we don't want to setup locking range on already used partition.
2402
   */
2403
0
  if (crypt_metadata_device(cd) != crypt_data_device(cd)) {
2404
0
    r = device_check_access(cd, crypt_data_device(cd), DEV_EXCL);
2405
0
    if (r < 0)
2406
0
      return r;
2407
0
  }
2408
2409
0
  if (!(cd->type = strdup(CRYPT_LUKS2)))
2410
0
    return -ENOMEM;
2411
2412
0
  if (volume_keys)
2413
0
    cd->volume_key = crypt_alloc_volume_key(volume_keys_size, volume_keys);
2414
0
  else
2415
0
    cd->volume_key = crypt_generate_volume_key(cd, volume_keys_size, KEY_QUALITY_KEY);
2416
2417
0
  if (!cd->volume_key) {
2418
0
    r = -ENOMEM;
2419
0
    goto out;
2420
0
  }
2421
2422
0
  if (cipher) {
2423
0
    user_key = crypt_alloc_volume_key(opal_params->user_key_size, crypt_volume_key_get_key(cd->volume_key));
2424
0
    if (!user_key) {
2425
0
      r = -ENOMEM;
2426
0
      goto out;
2427
0
    }
2428
0
  }
2429
2430
0
  r = 0;
2431
0
  if (params->pbkdf)
2432
0
    r = crypt_set_pbkdf_type(cd, params->pbkdf);
2433
0
  else if (verify_pbkdf_params(cd, &cd->pbkdf))
2434
0
    r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
2435
2436
0
  if (r < 0)
2437
0
    goto out;
2438
2439
0
  if (cd->metadata_device && !cd->data_offset)
2440
    /* For detached header the alignment is used directly as data offset */
2441
0
    cd->data_offset = params->data_alignment;
2442
2443
0
  r = opal_topology_alignment(cd, partition_offset_sectors,
2444
0
            cd->data_offset, params->data_alignment,
2445
0
            DEFAULT_DISK_ALIGNMENT, &alignment_offset_bytes, &required_alignment_bytes,
2446
0
            &opal_block_bytes, &opal_alignment_granularity_blocks);
2447
0
  if (r < 0)
2448
0
    goto out;
2449
2450
0
  if (sector_size_autodetect) {
2451
0
    sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd));
2452
0
    if ((opal_block_bytes * opal_alignment_granularity_blocks) > sector_size)
2453
0
      sector_size = opal_block_bytes * opal_alignment_granularity_blocks;
2454
0
    if (sector_size > MAX_SECTOR_SIZE)
2455
0
      sector_size = MAX_SECTOR_SIZE;
2456
0
    log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.",
2457
0
      device_path(crypt_data_device(cd)), sector_size);
2458
0
  } else
2459
0
    sector_size = params->sector_size;
2460
2461
  /* To ensure it is obvious and explicit that OPAL is being used, set the
2462
   * subsystem tag if the user hasn't passed one. */
2463
0
  if (!params->subsystem) {
2464
0
    params->subsystem = "HW-OPAL";
2465
0
    subsystem_overridden = true;
2466
0
  }
2467
2468
  /* We need to give the drive a segment number - use the partition number if there is
2469
   * one, otherwise the first valid (1) number if it's a single-volume setup */
2470
0
  r = crypt_dev_get_partition_number(device_path(crypt_data_device(cd)));
2471
0
  if (r > 0)
2472
0
    opal_segment_number = r;
2473
2474
0
  if (cipher) {
2475
0
    if (params->integrity_params && params->integrity_params->integrity_key_size)
2476
0
      integrity_key_size = params->integrity_params->integrity_key_size;
2477
2478
0
    r = LUKS2_check_encryption_params(cd, cipher, cipher_mode, integrity, 0,
2479
0
              volume_keys_size - opal_params->user_key_size,
2480
0
              params, &integrity, &integrity_key_size);
2481
0
    if (r < 0)
2482
0
      goto out;
2483
0
  }
2484
2485
0
  r = device_size(crypt_data_device(cd), &device_size_bytes);
2486
0
  if (r < 0)
2487
0
    goto out;
2488
2489
0
  r = LUKS2_hdr_get_storage_params(cd, alignment_offset_bytes, required_alignment_bytes,
2490
0
           &metadata_size_bytes, &keyslots_size_bytes, &data_offset_bytes);
2491
0
  if (r < 0)
2492
0
    goto out;
2493
2494
0
  r = -EINVAL;
2495
0
  if (device_size_bytes < data_offset_bytes && !cd->metadata_device) {
2496
0
    log_err(cd, _("Device %s is too small."), device_path(crypt_data_device(cd)));
2497
0
    goto out;
2498
0
  }
2499
2500
0
  device_size_bytes -= data_offset_bytes;
2501
0
  range_size_bytes = device_size_bytes - (device_size_bytes % (opal_block_bytes * opal_alignment_granularity_blocks));
2502
0
  if (!range_size_bytes)
2503
0
    goto out;
2504
2505
0
  if (device_size_bytes != range_size_bytes)
2506
0
    log_err(cd, _("Compensating device size by %" PRIu64 " sectors to align it with OPAL alignment granularity."),
2507
0
      (device_size_bytes - range_size_bytes) / SECTOR_SIZE);
2508
2509
0
  if (cipher) {
2510
0
    r = LUKS2_check_encryption_sector(cd, range_size_bytes, data_offset_bytes, sector_size,
2511
0
              sector_size_autodetect, integrity == NULL,
2512
0
              &sector_size);
2513
0
    if (r < 0)
2514
0
      goto out;
2515
2516
0
    if (*cipher_mode != '\0')
2517
0
      r = snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode);
2518
0
    else
2519
0
      r = snprintf(cipher_spec, sizeof(cipher_spec), "%s", cipher);
2520
0
    if (r < 0 || (size_t)r >= sizeof(cipher_spec)) {
2521
0
      r = -EINVAL;
2522
0
      goto out;
2523
0
    }
2524
0
  }
2525
2526
0
  r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key,
2527
0
             cipher ? cipher_spec : NULL,
2528
0
             integrity, integrity_key_size,
2529
0
             uuid,
2530
0
             sector_size,
2531
0
             data_offset_bytes,
2532
0
             metadata_size_bytes, keyslots_size_bytes,
2533
0
             range_size_bytes,
2534
0
             opal_segment_number,
2535
0
             opal_params->user_key_size);
2536
0
  if (r < 0)
2537
0
    goto out;
2538
2539
0
  if (params->label || params->subsystem) {
2540
0
    r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr,
2541
0
             params->label, params->subsystem, 0);
2542
0
    if (r < 0)
2543
0
      goto out;
2544
0
  }
2545
2546
0
  device_set_block_size(crypt_data_device(cd), sector_size);
2547
2548
0
  r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr);
2549
0
  if (r < 0) {
2550
0
    log_err(cd, _("Cannot wipe header on device %s."),
2551
0
      mdata_device_path(cd));
2552
0
    if (device_size_bytes < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr))
2553
0
      log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd)));
2554
0
    goto out;
2555
0
  }
2556
2557
0
  range_offset_blocks = (data_offset_bytes + partition_offset_sectors * SECTOR_SIZE) / opal_block_bytes;
2558
2559
0
  r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh);
2560
0
  if (r < 0) {
2561
0
    log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd)));
2562
0
    goto out;
2563
0
  }
2564
2565
0
  r = opal_setup_ranges(cd, crypt_data_device(cd), user_key ?: cd->volume_key,
2566
0
          range_offset_blocks, range_size_bytes / opal_block_bytes,
2567
0
          opal_block_bytes, opal_segment_number,
2568
0
          opal_params->admin_key, opal_params->admin_key_size,
2569
0
          !!(cd->compatibility & CRYPT_COMPAT_DISABLE_HW_OPAL_SUM),
2570
0
          &opal_requirement_version);
2571
0
  if (r < 0) {
2572
0
    if (r == -EPERM)
2573
0
      log_err(cd, _("Incorrect OPAL Admin key."));
2574
0
    else
2575
0
      log_err(cd, _("Cannot setup OPAL segment."));
2576
0
    goto out;
2577
0
  }
2578
2579
0
  opal_range_reset = true;
2580
2581
0
  log_dbg(cd, "Adding LUKS2 OPAL requirement flag (version: %u).", opal_requirement_version);
2582
0
  r = LUKS2_config_set_requirement_version(cd, &cd->u.luks2.hdr, CRYPT_REQUIREMENT_OPAL,
2583
0
             opal_requirement_version, false);
2584
0
  if (r < 0)
2585
0
    goto out;
2586
2587
  /* integrity metadata goes in unlocked OPAL locking range */
2588
0
  if (crypt_get_integrity_tag_size(cd)) {
2589
0
    r = opal_unlock(cd, crypt_data_device(cd), opal_segment_number, user_key ?: cd->volume_key);
2590
0
    if (r < 0)
2591
0
      goto out;
2592
2593
0
    r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO,
2594
0
              crypt_get_data_offset(cd) * SECTOR_SIZE,
2595
0
              8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
2596
0
    if (r < 0) {
2597
0
      if (r == -EBUSY)
2598
0
        log_err(cd, _("Cannot format device %s in use."),
2599
0
          data_device_path(cd));
2600
0
      else if (r == -EACCES) {
2601
0
        log_err(cd, _("Cannot format device %s, permission denied."),
2602
0
          data_device_path(cd));
2603
0
        r = -EINVAL;
2604
0
      } else
2605
0
        log_err(cd, _("Cannot wipe header on device %s."),
2606
0
          data_device_path(cd));
2607
2608
0
      goto out;
2609
0
    }
2610
2611
0
    if (integrity_key_size) {
2612
0
      integrity_key = crypt_alloc_volume_key(integrity_key_size,
2613
0
        crypt_volume_key_get_key(cd->volume_key) + volume_keys_size - integrity_key_size);
2614
2615
0
      if (!integrity_key) {
2616
0
        r = -ENOMEM;
2617
0
        goto out;
2618
0
      }
2619
0
    }
2620
2621
0
    r = INTEGRITY_format(cd, params->integrity_params, integrity_key, NULL, NULL,
2622
             /*
2623
              * Create reduced dm-integrity device only if locking range size does
2624
              * not match device size.
2625
              */
2626
0
             device_size_bytes != range_size_bytes ? range_size_bytes / SECTOR_SIZE : 0, NULL, false);
2627
0
    if (r)
2628
0
      log_err(cd, _("Cannot format integrity for device %s."),
2629
0
        data_device_path(cd));
2630
2631
0
    crypt_free_volume_key(integrity_key);
2632
0
    if (r < 0)
2633
0
      goto out;
2634
2635
0
    r = INTEGRITY_data_sectors(cd, crypt_data_device(cd),
2636
0
             crypt_get_data_offset(cd) * SECTOR_SIZE,
2637
0
             &provided_data_sectors);
2638
0
    if (r < 0)
2639
0
      goto out;
2640
2641
0
    if (!LUKS2_segment_set_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT,
2642
0
              &(uint64_t) {provided_data_sectors * SECTOR_SIZE})) {
2643
0
      r = -EINVAL;
2644
0
      goto out;
2645
0
    }
2646
2647
0
    r = opal_lock(cd, crypt_data_device(cd), opal_segment_number);
2648
0
    if (r < 0)
2649
0
      goto out;
2650
0
  }
2651
2652
  /* override sequence id check with format */
2653
0
  r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr);
2654
0
  if (r < 0) {
2655
0
    if (r == -EBUSY)
2656
0
      log_err(cd, _("Cannot format device %s in use."),
2657
0
        mdata_device_path(cd));
2658
0
    else if (r == -EACCES) {
2659
0
      log_err(cd, _("Cannot format device %s, permission denied."),
2660
0
        mdata_device_path(cd));
2661
0
      r = -EINVAL;
2662
0
    } else if (r == -EIO) {
2663
0
      log_err(cd, _("Cannot format device %s, OPAL device seems to be fully write-protected now."),
2664
0
        mdata_device_path(cd));
2665
0
      log_err(cd, _("This is perhaps a bug in firmware. Run OPAL PSID reset and reconnect for recovery."));
2666
0
    } else
2667
0
      log_err(cd, _("Cannot format device %s."),
2668
0
        mdata_device_path(cd));
2669
0
  }
2670
2671
0
out:
2672
0
  crypt_free_volume_key(user_key);
2673
2674
0
  if (subsystem_overridden)
2675
0
    params->subsystem = NULL;
2676
2677
0
  if (r >= 0) {
2678
0
    opal_exclusive_unlock(cd, opal_lh);
2679
0
    return 0;
2680
0
  }
2681
2682
0
  if (opal_range_reset &&
2683
0
      (opal_reset_segment(cd, crypt_data_device(cd), opal_segment_number,
2684
0
        opal_params->admin_key, opal_params->admin_key_size) < 0))
2685
0
    log_err(cd, _("Locking range %d reset on device %s failed."),
2686
0
      opal_segment_number, device_path(crypt_data_device(cd)));
2687
2688
0
  opal_exclusive_unlock(cd, opal_lh);
2689
0
  LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
2690
2691
0
  crypt_set_null_type(cd);
2692
0
  crypt_free_volume_key(cd->volume_key);
2693
0
  cd->volume_key = NULL;
2694
2695
0
  return r;
2696
0
}
2697
2698
static int _crypt_format_loopaes(struct crypt_device *cd,
2699
         const char *cipher,
2700
         const char *uuid,
2701
         size_t volume_key_size,
2702
         struct crypt_params_loopaes *params)
2703
0
{
2704
0
  if (!crypt_metadata_device(cd)) {
2705
0
    log_err(cd, _("Can't format LOOPAES without device."));
2706
0
    return -EINVAL;
2707
0
  }
2708
2709
0
  if (volume_key_size > 1024) {
2710
0
    log_err(cd, _("Invalid key size."));
2711
0
    return -EINVAL;
2712
0
  }
2713
2714
0
  if (uuid) {
2715
0
    log_err(cd, _("UUID is not supported for this crypt type."));
2716
0
    return -EINVAL;
2717
0
  }
2718
2719
0
  if (cd->metadata_device) {
2720
0
    log_err(cd, _("Detached metadata device is not supported for this crypt type."));
2721
0
    return -EINVAL;
2722
0
  }
2723
2724
0
  if (!(cd->type = strdup(CRYPT_LOOPAES)))
2725
0
    return -ENOMEM;
2726
2727
0
  cd->u.loopaes.key_size = volume_key_size;
2728
2729
0
  cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
2730
0
  if (!cd->u.loopaes.cipher)
2731
0
    return -ENOMEM;
2732
2733
0
  if (params && params->hash) {
2734
0
    cd->u.loopaes.hdr.hash = strdup(params->hash);
2735
0
    if (!cd->u.loopaes.hdr.hash) {
2736
0
      free(cd->u.loopaes.cipher);
2737
0
      cd->u.loopaes.cipher = NULL;
2738
0
      return -ENOMEM;
2739
0
    }
2740
0
  }
2741
2742
0
  cd->u.loopaes.hdr.offset = params ? params->offset : 0;
2743
0
  cd->u.loopaes.hdr.skip = params ? params->skip : 0;
2744
2745
0
  return 0;
2746
0
}
2747
2748
static int _crypt_format_verity(struct crypt_device *cd,
2749
         const char *uuid,
2750
         struct crypt_params_verity *params)
2751
0
{
2752
0
  int r = 0, hash_size;
2753
0
  uint64_t data_device_size, hash_blocks_size;
2754
0
  struct device *fec_device = NULL;
2755
0
  char *fec_device_path = NULL, *hash_name = NULL, *root_hash = NULL, *salt = NULL;
2756
2757
0
  if (!crypt_metadata_device(cd)) {
2758
0
    log_err(cd, _("Can't format VERITY without device."));
2759
0
    return -EINVAL;
2760
0
  }
2761
2762
0
  if (!params)
2763
0
    return -EINVAL;
2764
2765
0
  if (!params->data_device && !cd->metadata_device)
2766
0
    return -EINVAL;
2767
2768
0
  if (params->hash_type > VERITY_MAX_HASH_TYPE) {
2769
0
    log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
2770
0
    return -EINVAL;
2771
0
  }
2772
2773
0
  if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
2774
0
      VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
2775
0
    log_err(cd, _("Unsupported VERITY block size."));
2776
0
    return -EINVAL;
2777
0
  }
2778
2779
0
  if (MISALIGNED_512(params->hash_area_offset)) {
2780
0
    log_err(cd, _("Unsupported VERITY hash offset."));
2781
0
    return -EINVAL;
2782
0
  }
2783
2784
0
  if (MISALIGNED_512(params->fec_area_offset)) {
2785
0
    log_err(cd, _("Unsupported VERITY FEC offset."));
2786
0
    return -EINVAL;
2787
0
  }
2788
2789
0
  if (!(cd->type = strdup(CRYPT_VERITY)))
2790
0
    return -ENOMEM;
2791
2792
0
  if (params->data_device) {
2793
0
    r = crypt_set_data_device(cd, params->data_device);
2794
0
    if (r)
2795
0
      return r;
2796
0
  }
2797
2798
0
  if (!params->data_size) {
2799
0
    r = device_size(cd->device, &data_device_size);
2800
0
    if (r < 0)
2801
0
      return r;
2802
2803
0
    cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
2804
0
  } else
2805
0
    cd->u.verity.hdr.data_size = params->data_size;
2806
2807
0
  if (device_is_identical(crypt_metadata_device(cd), crypt_data_device(cd)) > 0 &&
2808
0
     (cd->u.verity.hdr.data_size * params->data_block_size) > params->hash_area_offset) {
2809
0
    log_err(cd, _("Data area overlaps with hash area."));
2810
0
    return -EINVAL;
2811
0
  }
2812
2813
0
  hash_size = crypt_hash_size(params->hash_name);
2814
0
  if (hash_size <= 0) {
2815
0
    log_err(cd, _("Hash algorithm %s not supported."),
2816
0
      params->hash_name);
2817
0
    return -EINVAL;
2818
0
  }
2819
0
  cd->u.verity.root_hash_size = hash_size;
2820
2821
0
  if (params->fec_device) {
2822
0
    fec_device_path = strdup(params->fec_device);
2823
0
    if (!fec_device_path)
2824
0
      return -ENOMEM;
2825
0
    r = device_alloc(cd, &fec_device, params->fec_device);
2826
0
    if (r < 0) {
2827
0
      r = -ENOMEM;
2828
0
      goto out;
2829
0
    }
2830
2831
0
    hash_blocks_size = VERITY_hash_blocks(cd, params) * params->hash_block_size;
2832
0
    if (device_is_identical(crypt_metadata_device(cd), fec_device) > 0 &&
2833
0
        (params->hash_area_offset + hash_blocks_size) > params->fec_area_offset) {
2834
0
      log_err(cd, _("Hash area overlaps with FEC area."));
2835
0
      r = -EINVAL;
2836
0
      goto out;
2837
0
    }
2838
2839
0
    if (device_is_identical(crypt_data_device(cd), fec_device) > 0 &&
2840
0
        (cd->u.verity.hdr.data_size * params->data_block_size) > params->fec_area_offset) {
2841
0
      log_err(cd, _("Data area overlaps with FEC area."));
2842
0
      r = -EINVAL;
2843
0
      goto out;
2844
0
    }
2845
0
  }
2846
2847
0
  root_hash = malloc(cd->u.verity.root_hash_size);
2848
0
  hash_name = strdup(params->hash_name);
2849
0
  salt = malloc(params->salt_size);
2850
2851
0
  if (!root_hash || !hash_name || !salt) {
2852
0
    r = -ENOMEM;
2853
0
    goto out;
2854
0
  }
2855
2856
0
  cd->u.verity.hdr.flags = params->flags;
2857
0
  cd->u.verity.root_hash = root_hash;
2858
0
  cd->u.verity.hdr.hash_name = hash_name;
2859
0
  cd->u.verity.hdr.data_device = NULL;
2860
0
  cd->u.verity.fec_device = fec_device;
2861
0
  cd->u.verity.hdr.fec_device = fec_device_path;
2862
0
  cd->u.verity.hdr.fec_roots = params->fec_roots;
2863
0
  cd->u.verity.hdr.data_block_size = params->data_block_size;
2864
0
  cd->u.verity.hdr.hash_block_size = params->hash_block_size;
2865
0
  cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
2866
0
  cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
2867
0
  cd->u.verity.hdr.hash_type = params->hash_type;
2868
0
  cd->u.verity.hdr.flags = params->flags;
2869
0
  cd->u.verity.hdr.salt_size = params->salt_size;
2870
0
  cd->u.verity.hdr.salt = salt;
2871
2872
0
  if (params->salt)
2873
0
    memcpy(salt, params->salt, params->salt_size);
2874
0
  else
2875
0
    r = crypt_random_get(cd, salt, params->salt_size, CRYPT_RND_SALT);
2876
0
  if (r)
2877
0
    goto out;
2878
2879
0
  if (params->flags & CRYPT_VERITY_CREATE_HASH) {
2880
0
    r = VERITY_create(cd, &cd->u.verity.hdr,
2881
0
          cd->u.verity.root_hash, cd->u.verity.root_hash_size);
2882
0
    if (!r && params->fec_device)
2883
0
      r = VERITY_FEC_process(cd, &cd->u.verity.hdr, cd->u.verity.fec_device, 0, NULL);
2884
0
    if (r)
2885
0
      goto out;
2886
0
  }
2887
2888
0
  if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
2889
0
    if (uuid) {
2890
0
      if (!(cd->u.verity.uuid = strdup(uuid)))
2891
0
        r = -ENOMEM;
2892
0
    } else
2893
0
      r = VERITY_UUID_generate(&cd->u.verity.uuid);
2894
2895
0
    if (!r)
2896
0
      r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
2897
0
              cd->u.verity.uuid,
2898
0
              &cd->u.verity.hdr);
2899
0
  }
2900
2901
0
out:
2902
0
  if (r) {
2903
0
    device_free(cd, fec_device);
2904
0
    free(root_hash);
2905
0
    free(hash_name);
2906
0
    free(fec_device_path);
2907
0
    free(salt);
2908
0
  }
2909
2910
0
  return r;
2911
0
}
2912
2913
static int _crypt_format_integrity(struct crypt_device *cd,
2914
           const char *uuid,
2915
           struct crypt_params_integrity *params,
2916
           const char *integrity_key, size_t integrity_key_size,
2917
           bool integrity_inline)
2918
0
{
2919
0
  int r;
2920
0
  uint32_t integrity_tag_size;
2921
0
  char *integrity = NULL, *journal_integrity = NULL, *journal_crypt = NULL;
2922
0
  struct volume_key *journal_crypt_key = NULL, *journal_mac_key = NULL, *ik = NULL;
2923
2924
0
  if (!params)
2925
0
    return -EINVAL;
2926
2927
0
  if (uuid) {
2928
0
    log_err(cd, _("UUID is not supported for this crypt type."));
2929
0
    return -EINVAL;
2930
0
  }
2931
2932
0
  if (integrity_key_size && integrity_key_size != params->integrity_key_size) {
2933
0
    log_err(cd, _("Integrity key size mismatch."));
2934
0
    return -EINVAL;
2935
0
  }
2936
2937
0
  r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
2938
0
  if (r < 0)
2939
0
    return r;
2940
2941
  /* Wipe first 8 sectors - fs magic numbers etc. */
2942
0
  r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
2943
0
            8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
2944
0
  if (r < 0) {
2945
0
    log_err(cd, _("Cannot wipe header on device %s."),
2946
0
      mdata_device_path(cd));
2947
0
    return r;
2948
0
  }
2949
2950
0
  if (!(cd->type = strdup(CRYPT_INTEGRITY)))
2951
0
    return -ENOMEM;
2952
2953
0
  if (params->journal_crypt_key) {
2954
0
    journal_crypt_key = crypt_alloc_volume_key(params->journal_crypt_key_size,
2955
0
                 params->journal_crypt_key);
2956
0
    if (!journal_crypt_key)
2957
0
      return -ENOMEM;
2958
0
  }
2959
2960
0
  if (params->journal_integrity_key) {
2961
0
    journal_mac_key = crypt_alloc_volume_key(params->journal_integrity_key_size,
2962
0
               params->journal_integrity_key);
2963
0
    if (!journal_mac_key) {
2964
0
      r = -ENOMEM;
2965
0
      goto out;
2966
0
    }
2967
0
  }
2968
2969
0
  if (params->integrity && !(integrity = strdup(params->integrity))) {
2970
0
    r = -ENOMEM;
2971
0
    goto out;
2972
0
  }
2973
0
  if (params->journal_integrity && !(journal_integrity = strdup(params->journal_integrity))) {
2974
0
    r = -ENOMEM;
2975
0
    goto out;
2976
0
  }
2977
0
  if (params->journal_crypt && !(journal_crypt = strdup(params->journal_crypt))) {
2978
0
    r = -ENOMEM;
2979
0
    goto out;
2980
0
  }
2981
2982
0
  integrity_tag_size = INTEGRITY_hash_tag_size(integrity);
2983
0
  if (integrity_tag_size > 0 && params->tag_size && integrity_tag_size != params->tag_size)
2984
0
    log_std(cd, _("WARNING: Requested tag size %d bytes differs from %s size output (%d bytes).\n"),
2985
0
      params->tag_size, integrity, integrity_tag_size);
2986
2987
0
  if (params->tag_size)
2988
0
    integrity_tag_size = params->tag_size;
2989
2990
0
  cd->u.integrity.journal_crypt_key = journal_crypt_key;
2991
0
  cd->u.integrity.journal_mac_key = journal_mac_key;
2992
0
  cd->u.integrity.params.journal_size = params->journal_size;
2993
0
  cd->u.integrity.params.journal_watermark = params->journal_watermark;
2994
0
  cd->u.integrity.params.journal_commit_time = params->journal_commit_time;
2995
0
  cd->u.integrity.params.interleave_sectors = params->interleave_sectors;
2996
0
  cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
2997
0
  cd->u.integrity.params.sector_size = params->sector_size;
2998
0
  cd->u.integrity.params.tag_size = integrity_tag_size;
2999
0
  cd->u.integrity.params.integrity = integrity;
3000
0
  cd->u.integrity.params.journal_integrity = journal_integrity;
3001
0
  cd->u.integrity.params.journal_crypt = journal_crypt;
3002
3003
0
  if (params->integrity_key_size) {
3004
0
    if (!integrity_key)
3005
0
      ik = crypt_generate_volume_key(cd, params->integrity_key_size, KEY_QUALITY_EMPTY);
3006
0
    else
3007
0
      ik = crypt_alloc_volume_key(params->integrity_key_size, integrity_key);
3008
0
    if (!ik) {
3009
0
      r = -ENOMEM;
3010
0
      goto out;
3011
0
    }
3012
0
  }
3013
3014
0
  r = INTEGRITY_format(cd, params, ik, cd->u.integrity.journal_crypt_key,
3015
0
           cd->u.integrity.journal_mac_key, 0, &cd->u.integrity.sb_flags,
3016
0
           integrity_inline);
3017
0
  if (r)
3018
0
    log_err(cd, _("Cannot format integrity for device %s."), mdata_device_path(cd));
3019
3020
0
  crypt_free_volume_key(ik);
3021
0
out:
3022
0
  if (r) {
3023
0
    crypt_free_volume_key(journal_crypt_key);
3024
0
    crypt_free_volume_key(journal_mac_key);
3025
0
    free(integrity);
3026
0
    free(journal_integrity);
3027
0
    free(journal_crypt);
3028
0
  }
3029
3030
0
  return r;
3031
0
}
3032
3033
int crypt_format_inline(struct crypt_device *cd,
3034
  const char *type,
3035
  const char *cipher,
3036
  const char *cipher_mode,
3037
  const char *uuid,
3038
  const char *volume_key,
3039
  size_t volume_key_size,
3040
  void *params)
3041
0
{
3042
0
  struct crypt_params_luks2 *lparams;
3043
0
  const struct crypt_params_integrity *iparams;
3044
0
  uint32_t device_tag_size, required_tag_size;
3045
0
  struct device *idevice;
3046
0
  size_t sector_size, required_sector_size;
3047
0
  int r;
3048
3049
0
  if (!cd || !params)
3050
0
    return -EINVAL;
3051
3052
0
  if (cd->type) {
3053
0
    log_dbg(cd, "Context already formatted as %s.", cd->type);
3054
0
    return -EINVAL;
3055
0
  }
3056
3057
0
  log_dbg(cd, "Formatting device %s as type %s with inline tags.", mdata_device_path(cd) ?: "(none)", type);
3058
3059
0
  crypt_reset_null_type(cd);
3060
3061
0
  r = init_crypto(cd);
3062
0
  if (r < 0)
3063
0
    return r;
3064
3065
0
  if (isINTEGRITY(type)) {
3066
0
    lparams = NULL;
3067
0
    iparams = params;
3068
0
    idevice = crypt_metadata_device(cd);
3069
0
    required_sector_size = iparams->sector_size;
3070
0
    required_tag_size = iparams->tag_size;
3071
3072
    /* Unused in standalone integrity */
3073
0
    if (cipher || cipher_mode)
3074
0
      return -EINVAL;
3075
0
  } else if (isLUKS2(type)) {
3076
0
    lparams = params;
3077
0
    iparams = lparams->integrity_params;
3078
3079
0
    if (lparams->data_device) {
3080
0
      if (!cd->metadata_device)
3081
0
        cd->metadata_device = cd->device;
3082
0
      else
3083
0
        device_free(cd, cd->device);
3084
0
      cd->device = NULL;
3085
0
      if (device_alloc(cd, &cd->device, lparams->data_device) < 0)
3086
0
        return -ENOMEM;
3087
0
    }
3088
3089
0
    idevice = crypt_data_device(cd);
3090
0
    required_sector_size = lparams->sector_size;
3091
3092
0
    if (!lparams->integrity || !idevice)
3093
0
      return -EINVAL;
3094
3095
0
    required_tag_size = INTEGRITY_tag_size(lparams->integrity, cipher, cipher_mode);
3096
0
  } else {
3097
0
    log_err(cd, _("Unknown or unsupported device type %s requested."), type);
3098
0
    return -EINVAL;
3099
0
  }
3100
3101
  /* In inline mode journal will be never used, check that params are not set */
3102
0
  if (iparams && (iparams->journal_size || iparams->journal_watermark || iparams->journal_commit_time ||
3103
0
      iparams->interleave_sectors || iparams->journal_integrity || iparams->journal_integrity_key ||
3104
0
      iparams->journal_integrity_key_size || iparams->journal_crypt || iparams->journal_crypt_key ||
3105
0
      iparams->journal_integrity_key_size))
3106
0
    return -EINVAL;
3107
3108
0
  r = device_is_nop_dif(idevice, &device_tag_size);
3109
0
  if (r < 0)
3110
0
    return r;
3111
3112
0
  if (!r) {
3113
0
    log_err(cd, _("Device %s does not provide inline integrity data fields."), mdata_device_path(cd));
3114
0
    return -EINVAL;
3115
0
  }
3116
3117
  /* We can get device_tag_size = 0 as kernel provides this info only for some block devices */
3118
0
  if (device_tag_size > 0 && device_tag_size < required_tag_size) {
3119
0
    log_err(cd, _("Inline tag size %" PRIu32 " [bytes] is larger than %" PRIu32 " provided by device %s."),
3120
0
      required_tag_size, device_tag_size, mdata_device_path(cd));
3121
0
    return -EINVAL;
3122
0
  }
3123
0
  log_dbg(cd, "Inline integrity is supported (%" PRIu32 ").", device_tag_size);
3124
3125
  /* Inline must use sectors size as hardware device */
3126
0
  sector_size = device_block_size(cd, idevice);
3127
0
  if (!sector_size)
3128
0
    return -EINVAL;
3129
3130
  /* No autodetection, use device sector size */
3131
0
  if (isLUKS2(type) && lparams && !required_sector_size)
3132
0
    lparams->sector_size = sector_size;
3133
0
  else if (sector_size != required_sector_size) {
3134
0
    log_err(cd, _("Sector must be the same as device hardware sector (%zu bytes)."), sector_size);
3135
0
    return -EINVAL;
3136
0
  }
3137
3138
0
  if (isINTEGRITY(type))
3139
0
    r = _crypt_format_integrity(cd, uuid, params, volume_key, volume_key_size, true);
3140
0
  else if (isLUKS2(type))
3141
0
    r = _crypt_format_luks2(cd, cipher, cipher_mode,
3142
0
          uuid, volume_key, volume_key_size, params, false, true);
3143
0
  else
3144
0
    r = -EINVAL;
3145
3146
0
  if (r < 0) {
3147
0
    crypt_set_null_type(cd);
3148
0
    crypt_free_volume_key(cd->volume_key);
3149
0
    cd->volume_key = NULL;
3150
0
  }
3151
3152
0
  return r;
3153
0
}
3154
3155
static int _crypt_format(struct crypt_device *cd,
3156
  const char *type,
3157
  const char *cipher,
3158
  const char *cipher_mode,
3159
  const char *uuid,
3160
  const char *volume_key,
3161
  size_t volume_key_size,
3162
  void *params,
3163
  bool sector_size_autodetect)
3164
0
{
3165
0
  int r;
3166
3167
0
  if (!cd || !type)
3168
0
    return -EINVAL;
3169
3170
0
  if (cd->type) {
3171
0
    log_dbg(cd, "Context already formatted as %s.", cd->type);
3172
0
    return -EINVAL;
3173
0
  }
3174
3175
0
  log_dbg(cd, "Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
3176
3177
0
  crypt_reset_null_type(cd);
3178
3179
0
  r = init_crypto(cd);
3180
0
  if (r < 0)
3181
0
    return r;
3182
3183
0
  if (isPLAIN(type))
3184
0
    r = _crypt_format_plain(cd, cipher, cipher_mode,
3185
0
          uuid, volume_key_size, params);
3186
0
  else if (isLUKS1(type))
3187
0
    r = _crypt_format_luks1(cd, cipher, cipher_mode,
3188
0
          uuid, volume_key, volume_key_size, params);
3189
0
  else if (isLUKS2(type))
3190
0
    r = _crypt_format_luks2(cd, cipher, cipher_mode,
3191
0
          uuid, volume_key, volume_key_size, params, sector_size_autodetect, false);
3192
0
  else if (isLOOPAES(type))
3193
0
    r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
3194
0
  else if (isVERITY(type))
3195
0
    r = _crypt_format_verity(cd, uuid, params);
3196
0
  else if (isINTEGRITY(type))
3197
0
    r = _crypt_format_integrity(cd, uuid, params, volume_key, volume_key_size, false);
3198
0
  else {
3199
0
    log_err(cd, _("Unknown or unsupported device type %s requested."), type);
3200
0
    r = -EINVAL;
3201
0
  }
3202
3203
0
  if (r < 0) {
3204
0
    crypt_set_null_type(cd);
3205
0
    crypt_free_volume_key(cd->volume_key);
3206
0
    cd->volume_key = NULL;
3207
0
  }
3208
3209
0
  return r;
3210
0
}
3211
3212
CRYPT_SYMBOL_EXPORT_NEW(int, crypt_format, 2, 4,
3213
  /* crypt_format parameters follows */
3214
  struct crypt_device *cd,
3215
  const char *type,
3216
  const char *cipher,
3217
  const char *cipher_mode,
3218
  const char *uuid,
3219
  const char *volume_key,
3220
  size_t volume_key_size,
3221
  void *params)
3222
0
{
3223
0
  return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, true);
3224
0
}
3225
3226
3227
CRYPT_SYMBOL_EXPORT_OLD(int, crypt_format, 2, 0,
3228
  /* crypt_format parameters follows */
3229
  struct crypt_device *cd,
3230
  const char *type,
3231
  const char *cipher,
3232
  const char *cipher_mode,
3233
  const char *uuid,
3234
  const char *volume_key,
3235
  size_t volume_key_size,
3236
  void *params)
3237
0
{
3238
0
  return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, false);
3239
0
}
3240
3241
int crypt_repair(struct crypt_device *cd,
3242
     const char *requested_type,
3243
     void *params __attribute__((unused)))
3244
0
{
3245
0
  int r;
3246
3247
0
  if (!cd)
3248
0
    return -EINVAL;
3249
3250
0
  log_dbg(cd, "Trying to repair %s crypt type from device %s.",
3251
0
    requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
3252
3253
0
  if (!crypt_metadata_device(cd))
3254
0
    return -EINVAL;
3255
3256
0
  if (requested_type && !isLUKS(requested_type))
3257
0
    return -EINVAL;
3258
3259
  /* Load with repair */
3260
0
  r = _crypt_load_luks(cd, requested_type, false, true);
3261
0
  if (r < 0)
3262
0
    return r;
3263
3264
  /* cd->type and header must be set in context */
3265
0
  r = crypt_check_data_device_size(cd);
3266
0
  if (r < 0)
3267
0
    crypt_set_null_type(cd);
3268
3269
0
  return r;
3270
0
}
3271
3272
/* compare volume keys */
3273
static int _compare_volume_keys(struct volume_key *svk, struct volume_key *tvk)
3274
0
{
3275
0
  if (svk == tvk)
3276
0
    return 0;
3277
3278
0
  if (!svk || !tvk)
3279
0
    return 1;
3280
3281
0
  if (crypt_volume_key_length(svk) != crypt_volume_key_length(tvk))
3282
0
    return 1;
3283
3284
  /* No switch between keyring and direct key specification */
3285
0
  if ((!crypt_volume_key_description(svk) && crypt_volume_key_description(tvk)) ||
3286
0
      (crypt_volume_key_description(svk) && !crypt_volume_key_description(tvk)) ||
3287
0
      (!crypt_volume_key_is_set(svk) && crypt_volume_key_is_set(tvk)) ||
3288
0
      (crypt_volume_key_is_set(svk) && !crypt_volume_key_is_set(tvk)))
3289
0
    return 1;
3290
3291
0
  if (crypt_volume_key_description(svk) &&
3292
0
      (crypt_volume_key_kernel_key_type(svk) != crypt_volume_key_kernel_key_type(tvk) ||
3293
0
      strcmp(crypt_volume_key_description(svk), crypt_volume_key_description(tvk))))
3294
0
    return 1;
3295
3296
0
  if (crypt_volume_key_is_set(svk) &&
3297
0
      crypt_backend_memeq(crypt_volume_key_get_key(svk),
3298
0
        crypt_volume_key_get_key(tvk),
3299
0
        crypt_volume_key_length(svk)))
3300
0
    return 1;
3301
3302
0
  return 0;
3303
0
}
3304
3305
static int _compare_volume_keys_luks2(struct volume_key *svk, struct volume_key *tvk)
3306
0
{
3307
0
  if (svk == tvk)
3308
0
    return 0;
3309
3310
0
  if (!svk || !tvk)
3311
0
    return 1;
3312
3313
0
  if (crypt_volume_key_length(svk) != crypt_volume_key_length(tvk))
3314
0
    return 1;
3315
3316
0
  if ((!crypt_volume_key_is_set(svk) && !crypt_volume_key_description(svk)) ||
3317
0
      (!crypt_volume_key_is_set(tvk) && !crypt_volume_key_description(tvk)))
3318
0
    return 1;
3319
3320
0
  if (crypt_volume_key_is_set(svk) && crypt_volume_key_is_set(tvk) &&
3321
0
      crypt_backend_memeq(crypt_volume_key_get_key(svk),
3322
0
        crypt_volume_key_get_key(tvk),
3323
0
        crypt_volume_key_length(svk)))
3324
0
    return 1;
3325
3326
0
  if (crypt_volume_key_description(svk) && crypt_volume_key_description(tvk))
3327
0
    return (crypt_volume_key_kernel_key_type(svk) != crypt_volume_key_kernel_key_type(tvk) ||
3328
0
      strcmp(crypt_volume_key_description(svk), crypt_volume_key_description(tvk)));
3329
3330
0
  return 0;
3331
0
}
3332
3333
static int _compare_device_types(struct crypt_device *cd,
3334
             const struct crypt_dm_active_device *src,
3335
             const struct crypt_dm_active_device *tgt)
3336
0
{
3337
0
  if (!tgt->uuid) {
3338
0
    log_dbg(cd, "Missing device uuid in target device.");
3339
0
    return -EINVAL;
3340
0
  }
3341
3342
  /*
3343
   * FIXME: The CRYPT_SUBDEV prefix should be enough but we need
3344
   * to keep INTEGRITY- for dm-integrity subdevices opened with
3345
   * cryptsetup version < 2.8.0. Drop the INTEGRITY condition
3346
   * in next Y release.
3347
   */
3348
0
  if (isLUKS2(cd->type) &&
3349
0
      (!strncmp("INTEGRITY-", tgt->uuid, strlen("INTEGRITY-")) ||
3350
0
       !strncmp(CRYPT_SUBDEV, tgt->uuid, strlen(CRYPT_SUBDEV)))) {
3351
0
    if (dm_uuid_cmp(tgt->uuid, src->uuid)) {
3352
0
      log_dbg(cd, "LUKS UUID mismatch.");
3353
0
      return -EINVAL;
3354
0
    }
3355
0
  } else if (isLUKS(cd->type)) {
3356
0
    if (!src->uuid || strncmp(cd->type, tgt->uuid, strlen(cd->type)) ||
3357
0
        dm_uuid_cmp(tgt->uuid, src->uuid)) {
3358
0
      log_dbg(cd, "LUKS UUID mismatch.");
3359
0
      return -EINVAL;
3360
0
    }
3361
0
  } else if (isPLAIN(cd->type) || isLOOPAES(cd->type)) {
3362
0
    if (strncmp(cd->type, tgt->uuid, strlen(cd->type))) {
3363
0
      log_dbg(cd, "Unexpected uuid prefix %s in target device.", tgt->uuid);
3364
0
      return -EINVAL;
3365
0
    }
3366
0
  } else if (!isINTEGRITY(cd->type)) {
3367
0
    log_dbg(cd, "Unsupported device type %s for reload.", cd->type ?: "<empty>");
3368
0
    return -ENOTSUP;
3369
0
  }
3370
3371
0
  return 0;
3372
0
}
3373
3374
static int _compare_crypt_devices(struct crypt_device *cd,
3375
             const struct dm_target *src,
3376
             const struct dm_target *tgt)
3377
0
{
3378
0
  char *src_cipher = NULL, *src_integrity = NULL;
3379
0
  int r = -EINVAL;
3380
3381
  /* for crypt devices keys are mandatory */
3382
0
  if (!src->u.crypt.vk || !tgt->u.crypt.vk)
3383
0
    return -EINVAL;
3384
3385
  /* CIPHER checks */
3386
0
  if (!src->u.crypt.cipher || !tgt->u.crypt.cipher)
3387
0
    return -EINVAL;
3388
3389
  /*
3390
   * dm_query_target converts capi cipher specification to dm-crypt format.
3391
   * We need to do same for cipher specification requested in source
3392
   * device.
3393
   */
3394
0
  if (crypt_capi_to_cipher(&src_cipher, &src_integrity, src->u.crypt.cipher, src->u.crypt.integrity))
3395
0
    return -EINVAL;
3396
3397
0
  if (strcmp(src_cipher, tgt->u.crypt.cipher)) {
3398
0
    log_dbg(cd, "Cipher specs do not match.");
3399
0
    goto out;
3400
0
  }
3401
3402
0
  if (crypt_volume_key_length(tgt->u.crypt.vk) == 0 && crypt_is_cipher_null(tgt->u.crypt.cipher))
3403
0
    log_dbg(cd, "Existing device uses cipher null. Skipping key comparison.");
3404
0
  else if (cd && isLUKS2(cd->type)) {
3405
0
    if (_compare_volume_keys_luks2(src->u.crypt.vk, tgt->u.crypt.vk)) {
3406
0
      log_dbg(cd, "Keys in LUKS2 context and target device do not match.");
3407
0
      goto out;
3408
0
    }
3409
0
  } else if (_compare_volume_keys(src->u.crypt.vk, tgt->u.crypt.vk)) {
3410
0
    log_dbg(cd, "Keys in context and target device do not match.");
3411
0
    goto out;
3412
0
  }
3413
3414
0
  if (crypt_strcmp(src_integrity, tgt->u.crypt.integrity)) {
3415
0
    log_dbg(cd, "Integrity parameters do not match.");
3416
0
    goto out;
3417
0
  }
3418
3419
0
  if (src->u.crypt.offset      != tgt->u.crypt.offset ||
3420
0
      src->u.crypt.sector_size != tgt->u.crypt.sector_size ||
3421
0
      src->u.crypt.iv_offset   != tgt->u.crypt.iv_offset ||
3422
0
      src->u.crypt.tag_size    != tgt->u.crypt.tag_size) {
3423
0
    log_dbg(cd, "Integer parameters do not match.");
3424
0
    goto out;
3425
0
  }
3426
3427
0
  if (device_is_identical(src->data_device, tgt->data_device) <= 0)
3428
0
    log_dbg(cd, "Data devices do not match.");
3429
0
  else
3430
0
    r = 0;
3431
3432
0
out:
3433
0
  free(src_cipher);
3434
0
  free(src_integrity);
3435
3436
0
  return r;
3437
0
}
3438
3439
static int _compare_integrity_devices(struct crypt_device *cd,
3440
             const struct dm_target *src,
3441
             const struct dm_target *tgt)
3442
0
{
3443
  /*
3444
   * some parameters may be implicit (and set in dm-integrity ctor)
3445
   *
3446
   *  journal_size
3447
   *  journal_watermark
3448
   *  journal_commit_time
3449
   *  buffer_sectors
3450
   *  interleave_sectors
3451
   */
3452
3453
  /* check remaining integer values that makes sense */
3454
0
  if (src->u.integrity.tag_size   != tgt->u.integrity.tag_size ||
3455
0
      src->u.integrity.offset   != tgt->u.integrity.offset   ||
3456
0
      src->u.integrity.sector_size  != tgt->u.integrity.sector_size) {
3457
0
    log_dbg(cd, "Integer parameters do not match.");
3458
0
    return -EINVAL;
3459
0
  }
3460
3461
0
  if (crypt_strcmp(src->u.integrity.integrity,       tgt->u.integrity.integrity) ||
3462
0
      crypt_strcmp(src->u.integrity.journal_integrity, tgt->u.integrity.journal_integrity) ||
3463
0
      crypt_strcmp(src->u.integrity.journal_crypt,     tgt->u.integrity.journal_crypt)) {
3464
0
    log_dbg(cd, "Journal parameters do not match.");
3465
0
    return -EINVAL;
3466
0
  }
3467
3468
  /* unfortunately dm-integrity doesn't support keyring */
3469
0
  if (_compare_volume_keys(src->u.integrity.vk, tgt->u.integrity.vk) ||
3470
0
      _compare_volume_keys(src->u.integrity.journal_integrity_key, tgt->u.integrity.journal_integrity_key) ||
3471
0
      _compare_volume_keys(src->u.integrity.journal_crypt_key, tgt->u.integrity.journal_crypt_key)) {
3472
0
    log_dbg(cd, "Journal keys do not match.");
3473
0
    return -EINVAL;
3474
0
  }
3475
3476
0
  if (device_is_identical(src->data_device, tgt->data_device) <= 0) {
3477
0
    log_dbg(cd, "Data devices do not match.");
3478
0
    return -EINVAL;
3479
0
  }
3480
3481
0
  return 0;
3482
0
}
3483
3484
int crypt_compare_dm_devices(struct crypt_device *cd,
3485
             const struct crypt_dm_active_device *src,
3486
             const struct crypt_dm_active_device *tgt)
3487
0
{
3488
0
  int r;
3489
0
  const struct dm_target *s, *t;
3490
3491
0
  if (!src || !tgt)
3492
0
    return -EINVAL;
3493
3494
0
  r = _compare_device_types(cd, src, tgt);
3495
0
  if (r)
3496
0
    return r;
3497
3498
0
  s = &src->segment;
3499
0
  t = &tgt->segment;
3500
3501
0
  while (s || t) {
3502
0
    if (!s || !t) {
3503
0
      log_dbg(cd, "segments count mismatch.");
3504
0
      return -EINVAL;
3505
0
    }
3506
0
    if (s->type != t->type) {
3507
0
      log_dbg(cd, "segment type mismatch.");
3508
0
      r = -EINVAL;
3509
0
      break;
3510
0
    }
3511
3512
0
    switch (s->type) {
3513
0
    case DM_CRYPT:
3514
0
      r = _compare_crypt_devices(cd, s, t);
3515
0
      break;
3516
0
    case DM_INTEGRITY:
3517
0
      r = _compare_integrity_devices(cd, s, t);
3518
0
      break;
3519
0
    case DM_LINEAR:
3520
0
      r = (s->u.linear.offset == t->u.linear.offset) ? 0 : -EINVAL;
3521
0
      break;
3522
0
    default:
3523
0
      r = -ENOTSUP;
3524
0
    }
3525
3526
0
    if (r)
3527
0
      break;
3528
3529
0
    s = s->next;
3530
0
    t = t->next;
3531
0
  }
3532
3533
0
  return r;
3534
0
}
3535
3536
static int _reload_device(struct crypt_device *cd, const char *name,
3537
        struct crypt_dm_active_device *sdmd, uint64_t dmflags)
3538
0
{
3539
0
  int r;
3540
0
  struct crypt_dm_active_device tdmd;
3541
0
  struct dm_target *src, *tgt = &tdmd.segment;
3542
3543
0
  assert(cd);
3544
0
  assert(sdmd);
3545
3546
0
  if (!cd->type || !name || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
3547
0
    return -EINVAL;
3548
3549
0
  src = &sdmd->segment;
3550
3551
0
  r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
3552
0
          DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
3553
0
          DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_INTEGRITY_PARAMS |
3554
0
          DM_ACTIVE_JOURNAL_CRYPT_KEY | DM_ACTIVE_JOURNAL_MAC_KEY, &tdmd);
3555
0
  if (r < 0) {
3556
0
    log_err(cd, _("Device %s is not active."), name);
3557
0
    return -EINVAL;
3558
0
  }
3559
3560
0
  if (!single_segment(&tdmd) ||
3561
0
      (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY) ||
3562
0
      (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size)) {
3563
0
    r = -ENOTSUP;
3564
0
    log_err(cd, _("Unsupported parameters on device %s."), name);
3565
0
    goto out;
3566
0
  }
3567
3568
0
  r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
3569
0
  if (r) {
3570
0
    log_err(cd, _("Mismatching parameters on device %s."), name);
3571
0
    goto out;
3572
0
  }
3573
3574
  /* Changing read only flag for active device makes no sense */
3575
0
  if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
3576
0
    sdmd->flags |= CRYPT_ACTIVATE_READONLY;
3577
0
  else
3578
0
    sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
3579
3580
  /*
3581
   * Only LUKS2 allows altering between volume key
3582
   * passed by hexbyte representation and reference
3583
   * to kernel keyring service.
3584
   *
3585
   * To make it easier pass src key directly after
3586
   * it was properly verified in crypt_compare_dm_devices
3587
   * call above.
3588
   */
3589
0
  if (isLUKS2(cd->type) && tgt->type == DM_CRYPT && src->u.crypt.vk) {
3590
0
    crypt_free_volume_key(tgt->u.crypt.vk);
3591
0
    tgt->u.crypt.vk = src->u.crypt.vk;
3592
0
  }
3593
3594
0
  if (tgt->type == DM_CRYPT)
3595
0
    r = device_block_adjust(cd, src->data_device, DEV_OK,
3596
0
          src->u.crypt.offset, &sdmd->size, NULL);
3597
0
  else if (tgt->type == DM_INTEGRITY)
3598
0
    r = device_block_adjust(cd, src->data_device, DEV_OK,
3599
0
          src->u.integrity.offset, &sdmd->size, NULL);
3600
0
  else
3601
0
    r = -EINVAL;
3602
3603
0
  if (r)
3604
0
    goto out;
3605
3606
0
  tdmd.flags = sdmd->flags;
3607
0
  tgt->size = tdmd.size = sdmd->size;
3608
3609
0
  r = dm_reload_device(cd, name, &tdmd, dmflags, 1);
3610
0
out:
3611
  /* otherwise dm_targets_free would free src key */
3612
0
  if (tgt->type == DM_CRYPT && src->u.crypt.vk == tgt->u.crypt.vk)
3613
0
    tgt->u.crypt.vk = NULL;
3614
3615
0
  dm_targets_free(cd, &tdmd);
3616
0
  free(CONST_CAST(void*)tdmd.uuid);
3617
3618
0
  return r;
3619
0
}
3620
3621
static int _reload_device_with_integrity(struct crypt_device *cd,
3622
  const char *name,
3623
  const char *iname,
3624
  const char *ipath,
3625
  struct crypt_dm_active_device *sdmd,
3626
  struct crypt_dm_active_device *sdmdi)
3627
0
{
3628
0
  int r;
3629
0
  struct crypt_dm_active_device tdmd, tdmdi = {};
3630
0
  struct dm_target *src, *srci, *tgt = &tdmd.segment, *tgti = &tdmdi.segment;
3631
0
  struct device *data_device = NULL;
3632
0
  bool clear = false;
3633
3634
0
  assert(cd);
3635
0
  assert(sdmd);
3636
0
  assert(sdmdi);
3637
3638
0
  if (!cd->type || !name || !iname || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
3639
0
    return -EINVAL;
3640
3641
0
  src = &sdmd->segment;
3642
0
  srci = &sdmdi->segment;
3643
3644
0
  r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
3645
0
          DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
3646
0
          DM_ACTIVE_CRYPT_KEY, &tdmd);
3647
0
  if (r < 0) {
3648
0
    log_err(cd, _("Device %s is not active."), name);
3649
0
    return -EINVAL;
3650
0
  }
3651
3652
0
  if (!single_segment(&tdmd) || tgt->type != DM_CRYPT || !tgt->u.crypt.tag_size) {
3653
0
    log_err(cd, _("Unsupported parameters on device %s."), name);
3654
0
    r = -ENOTSUP;
3655
0
    goto out;
3656
0
  }
3657
3658
0
  r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &tdmdi);
3659
0
  if (r < 0) {
3660
0
    log_err(cd, _("Device %s is not active."), iname);
3661
0
    r = -EINVAL;
3662
0
    goto out;
3663
0
  }
3664
3665
0
  if (!single_segment(&tdmdi) || tgti->type != DM_INTEGRITY) {
3666
0
    log_err(cd, _("Unsupported parameters on device %s."), iname);
3667
0
    r = -ENOTSUP;
3668
0
    goto out;
3669
0
  }
3670
3671
0
  r = crypt_compare_dm_devices(cd, sdmdi, &tdmdi);
3672
0
  if (r) {
3673
0
    log_err(cd, _("Mismatching parameters on device %s."), iname);
3674
0
    goto out;
3675
0
  }
3676
3677
  /* unsupported underneath dm-crypt with auth. encryption */
3678
0
  if (sdmdi->segment.u.integrity.meta_device || tdmdi.segment.u.integrity.meta_device) {
3679
0
    r = -ENOTSUP;
3680
0
    goto out;
3681
0
  }
3682
3683
0
  r = device_alloc(cd, &data_device, ipath);
3684
0
  if (r < 0)
3685
0
    goto out;
3686
3687
0
  r = device_block_adjust(cd, srci->data_device, DEV_OK,
3688
0
        srci->u.integrity.offset, &sdmdi->size, NULL);
3689
0
  if (r)
3690
0
    goto out;
3691
3692
0
  src->data_device = data_device;
3693
3694
0
  r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
3695
0
  if (r) {
3696
0
    log_err(cd, _("Crypt devices mismatch."));
3697
0
    goto out;
3698
0
  }
3699
3700
  /* Changing read only flag for active device makes no sense */
3701
0
  if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
3702
0
    sdmd->flags |= CRYPT_ACTIVATE_READONLY;
3703
0
  else
3704
0
    sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
3705
3706
0
  if (tdmdi.flags & CRYPT_ACTIVATE_READONLY)
3707
0
    sdmdi->flags |= CRYPT_ACTIVATE_READONLY;
3708
0
  else
3709
0
    sdmdi->flags &= ~CRYPT_ACTIVATE_READONLY;
3710
3711
  /*
3712
   * To make it easier pass src key directly after
3713
   * it was properly verified in crypt_compare_dm_devices
3714
   * call above.
3715
   */
3716
0
  crypt_free_volume_key(tgt->u.crypt.vk);
3717
0
  tgt->u.crypt.vk = src->u.crypt.vk;
3718
3719
0
  r = device_block_adjust(cd, src->data_device, DEV_OK,
3720
0
        src->u.crypt.offset, &sdmd->size, NULL);
3721
0
  if (r)
3722
0
    goto out;
3723
3724
0
  tdmd.flags = sdmd->flags;
3725
0
  tdmd.size = sdmd->size;
3726
3727
0
  if ((r = dm_reload_device(cd, iname, sdmdi, 0, 0))) {
3728
0
    log_err(cd, _("Failed to reload device %s."), iname);
3729
0
    goto out;
3730
0
  }
3731
3732
0
  if ((r = dm_reload_device(cd, name, &tdmd, 0, 0))) {
3733
0
    log_err(cd, _("Failed to reload device %s."), name);
3734
0
    clear = true;
3735
0
    goto out;
3736
0
  }
3737
3738
0
  if ((r = dm_suspend_device(cd, name, 0))) {
3739
0
    log_err(cd, _("Failed to suspend device %s."), name);
3740
0
    clear = true;
3741
0
    goto out;
3742
0
  }
3743
3744
0
  if ((r = dm_suspend_device(cd, iname, 0))) {
3745
0
    log_err(cd, _("Failed to suspend device %s."), iname);
3746
0
    clear = true;
3747
0
    goto out;
3748
0
  }
3749
3750
0
  if ((r = dm_resume_device(cd, iname, act2dmflags(sdmdi->flags)))) {
3751
0
    log_err(cd, _("Failed to resume device %s."), iname);
3752
0
    clear = true;
3753
0
    goto out;
3754
0
  }
3755
3756
0
  r = dm_resume_device(cd, name, act2dmflags(tdmd.flags));
3757
0
  if (!r)
3758
0
    goto out;
3759
3760
  /*
3761
   * This is worst case scenario. We have active underlying dm-integrity device with
3762
   * new table but dm-crypt resume failed for some reason. Tear everything down and
3763
   * burn it for good.
3764
   */
3765
3766
0
  log_err(cd, _("Fatal error while reloading device %s (on top of device %s)."), name, iname);
3767
3768
0
  if (dm_error_device(cd, name))
3769
0
    log_err(cd, _("Failed to switch device %s to dm-error."), name);
3770
0
  if (dm_error_device(cd, iname))
3771
0
    log_err(cd, _("Failed to switch device %s to dm-error."), iname);
3772
0
out:
3773
0
  if (clear) {
3774
0
    dm_clear_device(cd, name);
3775
0
    dm_clear_device(cd, iname);
3776
3777
0
    if (dm_status_suspended(cd, name) > 0)
3778
0
      dm_resume_device(cd, name, 0);
3779
0
    if (dm_status_suspended(cd, iname) > 0)
3780
0
      dm_resume_device(cd, iname, 0);
3781
0
  }
3782
3783
  /* otherwise dm_targets_free would free src key */
3784
0
  if (tgt->u.crypt.vk == src->u.crypt.vk)
3785
0
    tgt->u.crypt.vk = NULL;
3786
0
  dm_targets_free(cd, &tdmd);
3787
0
  dm_targets_free(cd, &tdmdi);
3788
0
  free(CONST_CAST(void*)tdmdi.uuid);
3789
0
  free(CONST_CAST(void*)tdmd.uuid);
3790
0
  device_free(cd, data_device);
3791
3792
0
  return r;
3793
0
}
3794
3795
int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
3796
0
{
3797
0
  struct crypt_dm_active_device dmdq, dmd = {};
3798
0
  struct dm_target *tgt = &dmdq.segment;
3799
0
  struct crypt_params_integrity params = {};
3800
0
  uint64_t supported_flags = 0, dmflags = 0;
3801
0
  uint64_t old_size;
3802
0
  int r;
3803
3804
  /* Device context type must be initialized */
3805
0
  if (!cd || !cd->type || !name)
3806
0
    return -EINVAL;
3807
3808
0
  if (isTCRYPT(cd->type) || isBITLK(cd->type)) {
3809
0
    log_err(cd, _("This operation is not supported for this device type."));
3810
0
    return -ENOTSUP;
3811
0
  }
3812
3813
0
  if (isLUKS2(cd->type) && !LUKS2_segments_dynamic_size(&cd->u.luks2.hdr)) {
3814
0
    log_err(cd, _("Can not resize LUKS2 device with static size."));
3815
0
    return -EINVAL;
3816
0
  }
3817
3818
0
  if (isLUKS2(cd->type) && crypt_get_integrity_tag_size(cd)) {
3819
0
    log_err(cd, _("Resize of LUKS2 device with integrity protection is not supported."));
3820
0
    return -ENOTSUP;
3821
0
  }
3822
3823
0
  if (new_size)
3824
0
    log_dbg(cd, "Resizing device %s to %" PRIu64 " sectors.", name, new_size);
3825
0
  else
3826
0
    log_dbg(cd, "Resizing device %s to underlying device size.", name);
3827
3828
0
  r = dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY |
3829
0
          DM_ACTIVE_INTEGRITY_PARAMS | DM_ACTIVE_JOURNAL_CRYPT_KEY |
3830
0
          DM_ACTIVE_JOURNAL_MAC_KEY, &dmdq);
3831
0
  if (r < 0) {
3832
0
    log_err(cd, _("Device %s is not active."), name);
3833
0
    return -EINVAL;
3834
0
  }
3835
0
  if (!single_segment(&dmdq) || (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)) {
3836
0
    log_dbg(cd, "Unsupported device table detected in %s.", name);
3837
0
    r = -EINVAL;
3838
0
    goto out;
3839
0
  }
3840
3841
0
  if ((dmdq.flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_key_in_keyring(cd)) {
3842
0
    r = -EPERM;
3843
0
    goto out;
3844
0
  }
3845
3846
0
  if (crypt_key_in_keyring(cd)) {
3847
0
    if (isLUKS2(cd->type))
3848
0
      r = LUKS2_key_description_by_segment(cd, &cd->u.luks2.hdr,
3849
0
            tgt->u.crypt.vk, CRYPT_DEFAULT_SEGMENT);
3850
0
    else if (isPLAIN(cd->type))
3851
0
      r = 0; /* key description was set on table load */
3852
0
    else
3853
0
      r = -EINVAL;
3854
0
    if (r < 0)
3855
0
      goto out;
3856
3857
0
    dmdq.flags |= CRYPT_ACTIVATE_KEYRING_KEY;
3858
0
  }
3859
3860
0
  if (crypt_loop_device(crypt_get_device_name(cd))) {
3861
0
    log_dbg(cd, "Trying to resize underlying loop device %s.",
3862
0
      crypt_get_device_name(cd));
3863
    /* Here we always use default size not new_size */
3864
0
    if (crypt_loop_resize(crypt_get_device_name(cd)))
3865
0
      log_err(cd, _("Cannot resize loop device."));
3866
0
  }
3867
3868
3869
  /*
3870
   * Integrity device metadata are maintained by the kernel. We need to
3871
   * reload the device (with the same parameters) and let the kernel
3872
   * calculate the maximum size of integrity device and store it in the
3873
   * superblock.
3874
   */
3875
0
  if (!new_size && tgt->type == DM_INTEGRITY) {
3876
0
    r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
3877
0
             crypt_get_data_offset(cd) * SECTOR_SIZE, &old_size);
3878
0
    if (r < 0)
3879
0
      goto out;
3880
3881
0
    dmd.size = dmdq.size;
3882
0
    dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH | CRYPT_ACTIVATE_PRIVATE;
3883
3884
0
    r = crypt_get_integrity_info(cd, &params);
3885
0
    if (r)
3886
0
      goto out;
3887
3888
0
    r = dm_integrity_target_set(cd, &dmd.segment, 0, dmdq.segment.size,
3889
0
        crypt_metadata_device(cd), crypt_data_device(cd),
3890
0
        crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
3891
0
        crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
3892
0
        tgt->u.integrity.journal_integrity_key, &params);
3893
0
    if (r)
3894
0
      goto out;
3895
    /* Backend device cannot be smaller here, device_block_adjust() will fail if so. */
3896
0
    r = _reload_device(cd, name, &dmd, DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH);
3897
0
    if (r)
3898
0
      goto out;
3899
3900
0
    r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
3901
0
        crypt_get_data_offset(cd) * SECTOR_SIZE, &new_size);
3902
0
    if (r < 0)
3903
0
      goto out;
3904
0
    log_dbg(cd, "Maximum integrity device size from kernel %" PRIu64, new_size);
3905
3906
0
    if (old_size == new_size && new_size == dmdq.size &&
3907
0
        !dm_flags(cd, tgt->type, &supported_flags) &&
3908
0
        !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
3909
0
      log_std(cd, _("WARNING: Maximum size already set or kernel doesn't support resize.\n"));
3910
0
  }
3911
3912
0
  r = device_block_adjust(cd, crypt_data_device(cd), DEV_OK,
3913
0
      crypt_get_data_offset(cd), &new_size, &dmdq.flags);
3914
0
  if (r)
3915
0
    goto out;
3916
3917
0
  if (MISALIGNED(new_size, (tgt->type == DM_CRYPT ? tgt->u.crypt.sector_size : tgt->u.integrity.sector_size) >> SECTOR_SHIFT)) {
3918
0
    log_err(cd, _("Device size is not aligned to requested sector size."));
3919
0
    r = -EINVAL;
3920
0
    goto out;
3921
0
  }
3922
3923
0
  if (MISALIGNED(new_size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) {
3924
0
    log_err(cd, _("Device size is not aligned to device logical block size."));
3925
0
    r = -EINVAL;
3926
0
    goto out;
3927
0
  }
3928
3929
0
  dmd.uuid = crypt_get_uuid(cd);
3930
0
  dmd.size = new_size;
3931
0
  dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH;
3932
3933
0
  if (tgt->type == DM_CRYPT) {
3934
0
    r = dm_crypt_target_set(&dmd.segment, 0, new_size, crypt_data_device(cd),
3935
0
        tgt->u.crypt.vk, crypt_get_cipher_spec(cd),
3936
0
        crypt_get_iv_offset(cd), crypt_get_data_offset(cd),
3937
0
        crypt_get_integrity(cd), crypt_get_integrity_key_size(cd, true), crypt_get_integrity_tag_size(cd),
3938
0
        crypt_get_sector_size(cd));
3939
0
    if (r < 0)
3940
0
      goto out;
3941
0
  } else if (tgt->type == DM_INTEGRITY) {
3942
0
    r = crypt_get_integrity_info(cd, &params);
3943
0
    if (r)
3944
0
      goto out;
3945
3946
0
    r = dm_integrity_target_set(cd, &dmd.segment, 0, new_size,
3947
0
        crypt_metadata_device(cd), crypt_data_device(cd),
3948
0
        crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
3949
0
        crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
3950
0
        tgt->u.integrity.journal_integrity_key, &params);
3951
0
    if (r)
3952
0
      goto out;
3953
0
  }
3954
3955
0
  if (new_size == dmdq.size) {
3956
0
    log_dbg(cd, "Device has already requested size %" PRIu64
3957
0
      " sectors.", dmdq.size);
3958
0
    r = 0;
3959
0
  } else {
3960
0
    if (isTCRYPT(cd->type))
3961
0
      r = -ENOTSUP;
3962
0
    else if (isLUKS2(cd->type))
3963
0
      r = LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, 0, 0);
3964
3965
0
    if (!r) {
3966
      /* Skip flush and lockfs if extending device */
3967
0
      if (new_size > dmdq.size)
3968
0
        dmflags = DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH;
3969
0
      r = _reload_device(cd, name, &dmd, dmflags);
3970
0
    }
3971
3972
0
    if (r && tgt->type == DM_INTEGRITY &&
3973
0
        !dm_flags(cd, tgt->type, &supported_flags) &&
3974
0
        !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
3975
0
      log_err(cd, _("Resize failed, the kernel doesn't support it."));
3976
0
  }
3977
0
out:
3978
0
  dm_targets_free(cd, &dmd);
3979
0
  dm_targets_free(cd, &dmdq);
3980
3981
0
  return r;
3982
0
}
3983
3984
int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
3985
0
{
3986
0
  const char *active_uuid;
3987
0
  int r;
3988
3989
0
  log_dbg(cd, "%s device uuid.", uuid ? "Setting new" : "Refreshing");
3990
3991
0
  if ((r = onlyLUKS(cd)))
3992
0
    return r;
3993
3994
0
  active_uuid = crypt_get_uuid(cd);
3995
3996
0
  if (uuid && active_uuid && !strncmp(uuid, active_uuid, UUID_STRING_L)) {
3997
0
    log_dbg(cd, "UUID is the same as requested (%s) for device %s.",
3998
0
      uuid, mdata_device_path(cd));
3999
0
    return 0;
4000
0
  }
4001
4002
0
  if (uuid)
4003
0
    log_dbg(cd, "Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
4004
0
  else
4005
0
    log_dbg(cd, "Requested new UUID refresh for %s.", mdata_device_path(cd));
4006
4007
0
  if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
4008
0
    return -EPERM;
4009
4010
0
  if (isLUKS1(cd->type))
4011
0
    return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
4012
0
  else
4013
0
    return LUKS2_hdr_uuid(cd, &cd->u.luks2.hdr, uuid);
4014
0
}
4015
4016
int crypt_set_label(struct crypt_device *cd, const char *label, const char *subsystem)
4017
0
{
4018
0
  int r;
4019
4020
0
  log_dbg(cd, "Setting new labels.");
4021
4022
0
  if ((r = onlyLUKS2(cd)))
4023
0
    return r;
4024
4025
0
  return LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, label, subsystem, 1);
4026
0
}
4027
4028
const char *crypt_get_label(struct crypt_device *cd)
4029
0
{
4030
0
  if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
4031
0
    return NULL;
4032
4033
0
  return cd->u.luks2.hdr.label;
4034
0
}
4035
4036
const char *crypt_get_subsystem(struct crypt_device *cd)
4037
0
{
4038
0
  if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
4039
0
    return NULL;
4040
4041
0
  return cd->u.luks2.hdr.subsystem;
4042
0
}
4043
4044
int crypt_header_backup(struct crypt_device *cd,
4045
      const char *requested_type,
4046
      const char *backup_file)
4047
0
{
4048
0
  int r;
4049
4050
0
  if (requested_type && !isLUKS(requested_type))
4051
0
    return -EINVAL;
4052
4053
0
  if (!backup_file)
4054
0
    return -EINVAL;
4055
4056
  /* Load with repair */
4057
0
  r = _crypt_load_luks(cd, requested_type, false, false);
4058
0
  if (r < 0)
4059
0
    return r;
4060
4061
0
  log_dbg(cd, "Requested header backup of device %s (%s) to "
4062
0
    "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
4063
4064
0
  if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
4065
0
    r = LUKS_hdr_backup(backup_file, cd);
4066
0
  else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type)))
4067
0
    r = LUKS2_hdr_backup(cd, &cd->u.luks2.hdr, backup_file);
4068
0
  else
4069
0
    r = -EINVAL;
4070
4071
0
  return r;
4072
0
}
4073
4074
int crypt_header_restore(struct crypt_device *cd,
4075
       const char *requested_type,
4076
       const char *backup_file)
4077
0
{
4078
0
  struct luks_phdr hdr1;
4079
0
  struct luks2_hdr hdr2;
4080
0
  int r, version;
4081
4082
0
  if (requested_type && !isLUKS(requested_type))
4083
0
    return -EINVAL;
4084
4085
0
  if (!cd || (cd->type && !isLUKS(cd->type)) || !backup_file)
4086
0
    return -EINVAL;
4087
4088
0
  r = init_crypto(cd);
4089
0
  if (r < 0)
4090
0
    return r;
4091
4092
0
  log_dbg(cd, "Requested header restore to device %s (%s) from "
4093
0
    "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
4094
4095
0
  version = LUKS2_hdr_version_unlocked(cd, backup_file);
4096
0
  if (!version ||
4097
0
     (requested_type && version == 1 && !isLUKS1(requested_type)) ||
4098
0
     (requested_type && version == 2 && !isLUKS2(requested_type))) {
4099
0
    log_err(cd, _("Header backup file does not contain compatible LUKS header."));
4100
0
    return -EINVAL;
4101
0
  }
4102
4103
0
  memset(&hdr2, 0, sizeof(hdr2));
4104
4105
0
  if (!cd->type) {
4106
0
    if (version == 1)
4107
0
      r = LUKS_hdr_restore(backup_file, &hdr1, cd);
4108
0
    else
4109
0
      r = LUKS2_hdr_restore(cd, &hdr2, backup_file);
4110
4111
0
    crypt_safe_memzero(&hdr1, sizeof(hdr1));
4112
0
    crypt_safe_memzero(&hdr2, sizeof(hdr2));
4113
0
  } else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type))) {
4114
0
    r = LUKS2_hdr_restore(cd, &cd->u.luks2.hdr, backup_file);
4115
0
    if (r)
4116
0
      (void) _crypt_load_luks2(cd, 1, 0);
4117
0
  } else if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
4118
0
    r = LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
4119
0
  else
4120
0
    r = -EINVAL;
4121
4122
0
  if (!r)
4123
0
    r = _crypt_load_luks(cd, version == 1 ? CRYPT_LUKS1 : CRYPT_LUKS2, false, true);
4124
4125
0
  return r;
4126
0
}
4127
4128
int crypt_header_is_detached(struct crypt_device *cd)
4129
0
{
4130
0
  int r;
4131
4132
0
  if (!cd || (cd->type && !isLUKS(cd->type)))
4133
0
    return -EINVAL;
4134
4135
0
  r = device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd));
4136
0
  if (r < 0) {
4137
0
    log_dbg(cd, "Failed to compare data and metadata devices path.");
4138
0
    return r;
4139
0
  }
4140
4141
0
  return r ? 0 : 1;
4142
0
}
4143
4144
void crypt_free(struct crypt_device *cd)
4145
6.66k
{
4146
6.66k
  if (!cd)
4147
0
    return;
4148
4149
6.66k
  log_dbg(cd, "Releasing crypt device %s context.", mdata_device_path(cd) ?: "empty");
4150
4151
6.66k
  dm_backend_exit(cd);
4152
6.66k
  crypt_free_volume_key(cd->volume_key);
4153
4154
6.66k
  crypt_free_type(cd, NULL);
4155
4156
6.66k
  device_free(cd, cd->device);
4157
6.66k
  device_free(cd, cd->metadata_device);
4158
4159
6.66k
  free(CONST_CAST(void*)cd->pbkdf.type);
4160
6.66k
  free(CONST_CAST(void*)cd->pbkdf.hash);
4161
6.66k
  free(CONST_CAST(void*)cd->user_key_name1);
4162
6.66k
  free(CONST_CAST(void*)cd->user_key_name2);
4163
4164
  /* Some structures can contain keys (TCRYPT), wipe it */
4165
6.66k
  crypt_safe_memzero(cd, sizeof(*cd));
4166
6.66k
  free(cd);
4167
6.66k
}
4168
4169
int crypt_suspend(struct crypt_device *cd,
4170
      const char *name)
4171
0
{
4172
0
  bool dm_opal_uuid;
4173
0
  crypt_status_info ci;
4174
0
  int r;
4175
0
  struct crypt_dm_active_device dmd, dmdi = {};
4176
0
  uint32_t opal_segment_number = 1;
4177
0
  uint64_t dmflags = DM_SUSPEND_WIPE_KEY;
4178
0
  struct dm_target *tgt = &dmd.segment;
4179
0
  char *iname = NULL;
4180
0
  struct crypt_lock_handle *opal_lh = NULL;
4181
4182
0
  if (!cd || !name)
4183
0
    return -EINVAL;
4184
4185
0
  log_dbg(cd, "Suspending volume %s.", name);
4186
4187
0
  if (cd->type && ((r = onlyLUKS(cd)) < 0))
4188
0
    return r;
4189
4190
0
  ci = crypt_status(cd, name);
4191
0
  if (ci < CRYPT_ACTIVE) {
4192
0
    log_err(cd, _("Volume %s is not active."), name);
4193
0
    return -EINVAL;
4194
0
  }
4195
4196
0
  r = dm_query_device(cd, name,
4197
0
          DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_CRYPT_KEYSIZE,
4198
0
          &dmd);
4199
0
  if (r < 0)
4200
0
    return r;
4201
4202
0
  log_dbg(cd, "Checking if active device %s has UUID type LUKS.", name);
4203
4204
0
  r = dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2);
4205
0
  if (r < 0)
4206
0
    r = dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS1);
4207
4208
0
  if (r < 0) {
4209
0
    log_err(cd, _("This operation is supported only for LUKS device."));
4210
0
    goto out;
4211
0
  }
4212
4213
0
  r = -EINVAL;
4214
4215
0
  if (isLUKS2(cd->type) && dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2)) {
4216
0
    log_dbg(cd, "LUKS device header type: %s mismatches DM device type.", cd->type);
4217
0
    goto out;
4218
0
  }
4219
4220
0
  if (isLUKS1(cd->type) && dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS1)) {
4221
0
    log_dbg(cd, "LUKS device header type: %s mismatches DM device type.", cd->type);
4222
0
    goto out;
4223
0
  }
4224
4225
  /* check if active device has LUKS2-OPAL dm uuid prefix */
4226
0
  dm_opal_uuid = !dm_uuid_type_cmp(dmd.uuid, CRYPT_LUKS2_HW_OPAL);
4227
4228
0
  if (!dm_opal_uuid && isLUKS2(cd->type) &&
4229
0
      LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT))
4230
0
    goto out;
4231
4232
0
  if (cd->type && (r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd))) < 0) {
4233
0
    log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s",
4234
0
      LUKS_UUID(cd), dmd.uuid);
4235
0
    goto out;
4236
0
  }
4237
4238
  /* check UUID of integrity device underneath crypt device */
4239
0
  if (crypt_get_integrity_tag_size(cd))
4240
0
      iname = dm_get_active_iname(cd, name);
4241
4242
0
  r = dm_status_suspended(cd, name);
4243
0
  if (r < 0)
4244
0
    goto out;
4245
4246
0
  if (r) {
4247
0
    log_err(cd, _("Volume %s is already suspended."), name);
4248
0
    r = -EINVAL;
4249
0
    goto out;
4250
0
  }
4251
4252
0
  if (dm_opal_uuid && crypt_data_device(cd)) {
4253
0
    if (isLUKS2(cd->type)) {
4254
0
      r = LUKS2_get_opal_segment_number(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, &opal_segment_number);
4255
0
      if (r < 0)
4256
0
        goto out;
4257
0
    } else {
4258
       /* Guess OPAL range number for LUKS2-OPAL device with missing header */
4259
0
      r = crypt_dev_get_partition_number(device_path(crypt_data_device(cd)));
4260
0
      if (r > 0)
4261
0
        opal_segment_number = r;
4262
0
    }
4263
0
  }
4264
4265
  /* we can't simply wipe wrapped keys. HW OPAL only encryption does not use dm-crypt target */
4266
0
  if (crypt_cipher_wrapped_key(crypt_get_cipher(cd), crypt_get_cipher_mode(cd)) ||
4267
0
      (dm_opal_uuid && tgt->type == DM_LINEAR))
4268
0
    dmflags &= ~DM_SUSPEND_WIPE_KEY;
4269
4270
0
  r = dm_suspend_device(cd, name, dmflags);
4271
0
  if (r) {
4272
0
    if (r == -ENOTSUP)
4273
0
      log_err(cd, _("Suspend is not supported for device %s."), name);
4274
0
    else
4275
0
      log_err(cd, _("Error during suspending device %s."), name);
4276
0
    goto out;
4277
0
  }
4278
4279
  /* Suspend integrity device underneath; keep crypt suspended if it fails */
4280
0
  if (iname) {
4281
0
    r = dm_suspend_device(cd, iname, 0);
4282
0
    if (r)
4283
0
      log_err(cd, _("Error during suspending device %s."), iname);
4284
0
  }
4285
4286
0
  if (single_segment(&dmd) && tgt->type == DM_CRYPT)
4287
0
    crypt_volume_key_drop_kernel_key(cd, tgt->u.crypt.vk);
4288
4289
0
  if (dm_opal_uuid && crypt_data_device(cd)) {
4290
0
    r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh);
4291
0
    if (r < 0) {
4292
0
      log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd)));
4293
0
      goto out;
4294
0
    }
4295
0
  }
4296
4297
0
  if (dm_opal_uuid && (!crypt_data_device(cd) || opal_lock(cd, crypt_data_device(cd), opal_segment_number)))
4298
0
    log_err(cd, _("Device %s was suspended but hardware OPAL device cannot be locked."), name);
4299
0
out:
4300
0
  opal_exclusive_unlock(cd, opal_lh);
4301
0
  free(iname);
4302
0
  dm_targets_free(cd, &dmd);
4303
0
  dm_targets_free(cd, &dmdi);
4304
0
  free(CONST_CAST(void*)dmd.uuid);
4305
0
  free(CONST_CAST(void*)dmdi.uuid);
4306
0
  return r;
4307
0
}
4308
4309
static int resume_luks1_by_volume_key(struct crypt_device *cd,
4310
    struct volume_key *vk,
4311
    const char *name)
4312
0
{
4313
0
  int r;
4314
0
  struct volume_key *zerokey = NULL;
4315
4316
0
  assert(vk && crypt_volume_key_get_id(vk) == 0);
4317
0
  assert(name);
4318
4319
0
  if (crypt_is_cipher_null(crypt_get_cipher_spec(cd))) {
4320
0
    zerokey = crypt_alloc_volume_key(0, NULL);
4321
0
    if (!zerokey)
4322
0
      return -ENOMEM;
4323
0
    vk = zerokey;
4324
0
  }
4325
4326
0
  r = dm_resume_and_reinstate_key(cd, name, vk);
4327
4328
0
  if (r == -ENOTSUP)
4329
0
    log_err(cd, _("Resume is not supported for device %s."), name);
4330
0
  else if (r)
4331
0
    log_err(cd, _("Error during resuming device %s."), name);
4332
4333
0
  crypt_free_volume_key(zerokey);
4334
4335
0
  return r;
4336
0
}
4337
4338
static void crypt_unlink_key_from_custom_keyring(struct crypt_device *cd, key_serial_t kid)
4339
0
{
4340
0
  assert(cd);
4341
0
  assert(cd->keyring_to_link_vk);
4342
4343
0
  log_dbg(cd, "Unlinking volume key (id: %" PRIi32 ") from kernel keyring (id: %" PRIi32 ").",
4344
0
    kid, cd->keyring_to_link_vk);
4345
4346
0
  if (!keyring_unlink_key_from_keyring(kid, cd->keyring_to_link_vk))
4347
0
    return;
4348
4349
0
  log_dbg(cd, "keyring_unlink_key_from_keyring failed with errno %d.", errno);
4350
0
  log_err(cd, _("Failed to unlink volume key from user specified keyring."));
4351
0
}
4352
4353
static key_serial_t crypt_single_volume_key_load_in_custom_keyring(struct crypt_device *cd,
4354
                   struct volume_key *vk,
4355
                   const char *user_key_name)
4356
0
{
4357
0
  key_serial_t kid;
4358
0
  const char *type_name;
4359
4360
0
  assert(cd);
4361
0
  assert(cd->link_vk_to_keyring);
4362
4363
0
  if (!vk || !(type_name = key_type_name(cd->keyring_key_type)))
4364
0
    return -EINVAL;
4365
4366
0
  log_dbg(cd, "Linking volume key (type %s, name %s) to the specified keyring",
4367
0
        type_name, user_key_name);
4368
4369
0
  kid = keyring_add_key_to_keyring(cd->keyring_key_type, user_key_name,
4370
0
           crypt_volume_key_get_key(vk),
4371
0
           crypt_volume_key_length(vk),
4372
0
           cd->keyring_to_link_vk);
4373
0
  if (kid <= 0)
4374
0
    log_dbg(cd, "The keyring_add_key_to_keyring function failed (error %d).", errno);
4375
4376
0
  return kid;
4377
0
}
4378
4379
static int crypt_volume_key_load_in_custom_keyring(struct crypt_device *cd,
4380
               struct volume_key *vk,
4381
               key_serial_t *kid1_out,
4382
               key_serial_t *kid2_out)
4383
0
{
4384
0
  key_serial_t kid1, kid2 = 0;
4385
4386
0
  assert(cd);
4387
0
  assert(cd->link_vk_to_keyring);
4388
0
  assert(cd->user_key_name1);
4389
4390
0
  if (!vk || !key_type_name(cd->keyring_key_type))
4391
0
    return -EINVAL;
4392
4393
0
  kid1 = crypt_single_volume_key_load_in_custom_keyring(cd, vk, cd->user_key_name1);
4394
0
  if (kid1 <= 0)
4395
0
    return -EINVAL;
4396
4397
0
  vk = crypt_volume_key_next(vk);
4398
0
  if (vk) {
4399
0
    assert(cd->user_key_name2);
4400
0
    kid2 = crypt_single_volume_key_load_in_custom_keyring(cd, vk, cd->user_key_name2);
4401
0
    if (kid2 <= 0) {
4402
0
      crypt_unlink_key_from_custom_keyring(cd, kid1);
4403
0
      return -EINVAL;
4404
0
    }
4405
0
  }
4406
4407
0
  *kid2_out = kid2;
4408
0
  *kid1_out = kid1;
4409
0
  return 0;
4410
0
}
4411
4412
static int resume_luks2_by_volume_key(struct crypt_device *cd,
4413
    int digest,
4414
    struct volume_key *vk,
4415
    const char *name)
4416
0
{
4417
0
  bool use_keyring;
4418
0
  int r, enc_type;
4419
0
  uint32_t opal_segment_number;
4420
0
  struct volume_key *p_crypt = vk, *p_opal = NULL, *zerokey = NULL, *crypt_key = NULL, *opal_key = NULL;
4421
0
  char *iname = NULL;
4422
0
  struct crypt_lock_handle *opal_lh = NULL;
4423
0
  key_serial_t kid1 = 0, kid2 = 0;
4424
4425
0
  assert(digest >= 0);
4426
0
  assert(vk && crypt_volume_key_get_id(vk) == digest);
4427
0
  assert(name);
4428
4429
0
  enc_type = crypt_get_hw_encryption_type(cd);
4430
0
  if (enc_type < 0)
4431
0
    return enc_type;
4432
4433
0
  use_keyring = crypt_use_keyring_for_vk(cd);
4434
4435
0
  if (enc_type == CRYPT_OPAL_HW_ONLY || enc_type == CRYPT_SW_AND_OPAL_HW) {
4436
0
    r = LUKS2_get_opal_segment_number(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT,
4437
0
              &opal_segment_number);
4438
0
    if (r < 0)
4439
0
      return r;
4440
4441
0
    r = LUKS2_split_crypt_and_opal_keys(cd, &cd->u.luks2.hdr,
4442
0
                vk, &crypt_key,
4443
0
                &opal_key);
4444
0
    if (r < 0)
4445
0
      return r;
4446
4447
0
    p_crypt = crypt_key;
4448
0
    p_opal = opal_key ?: vk;
4449
0
  }
4450
4451
0
  if (enc_type != CRYPT_OPAL_HW_ONLY && crypt_is_cipher_null(crypt_get_cipher_spec(cd))) {
4452
0
    zerokey = crypt_alloc_volume_key(0, NULL);
4453
0
    if (!zerokey) {
4454
0
      r = -ENOMEM;
4455
0
      goto out;
4456
0
    }
4457
0
    p_crypt = zerokey;
4458
0
    use_keyring = false;
4459
0
  }
4460
4461
0
  if (use_keyring) {
4462
0
    if (p_crypt) {
4463
0
      r = LUKS2_volume_key_load_in_keyring_by_digest(cd, p_crypt, digest);
4464
0
      if (r < 0)
4465
0
        goto out;
4466
0
    }
4467
4468
    /* upload volume key in custom keyring if requested */
4469
0
    if (cd->link_vk_to_keyring) {
4470
0
      r = crypt_volume_key_load_in_custom_keyring(cd, vk, &kid1, &kid2);
4471
0
      if (r < 0) {
4472
0
        log_err(cd, _("Failed to link volume key in user defined keyring."));
4473
0
        goto out;
4474
0
      }
4475
0
    }
4476
0
  }
4477
4478
0
  if (p_opal) {
4479
0
    r = opal_exclusive_lock(cd, crypt_data_device(cd), &opal_lh);
4480
0
    if (r < 0) {
4481
0
      log_err(cd, _("Failed to acquire OPAL lock on device %s."), device_path(crypt_data_device(cd)));
4482
0
      goto out;
4483
0
    }
4484
4485
0
    r = opal_unlock(cd, crypt_data_device(cd), opal_segment_number, p_opal);
4486
0
    if (r < 0) {
4487
0
      p_opal = NULL; /* do not lock on error path */
4488
0
      goto out;
4489
0
    }
4490
0
  }
4491
4492
0
  if (crypt_get_integrity_tag_size(cd) &&
4493
0
      (iname = dm_get_active_iname(cd, name))) {
4494
0
    r = dm_resume_device(cd, iname, 0);
4495
0
    if (r)
4496
0
      log_err(cd, _("Error during resuming device %s."), iname);
4497
0
    free(iname);
4498
0
  }
4499
4500
0
  if (enc_type == CRYPT_OPAL_HW_ONLY)
4501
0
    r = dm_resume_device(cd, name, 0);
4502
0
  else
4503
0
    r = dm_resume_and_reinstate_key(cd, name, p_crypt);
4504
4505
0
  if (r == -ENOTSUP)
4506
0
    log_err(cd, _("Resume is not supported for device %s."), name);
4507
0
  else if (r)
4508
0
    log_err(cd, _("Error during resuming device %s."), name);
4509
4510
0
out:
4511
0
  if (r < 0) {
4512
0
    crypt_drop_uploaded_keyring_key(cd, p_crypt);
4513
0
    if (cd->link_vk_to_keyring && kid1)
4514
0
      crypt_unlink_key_from_custom_keyring(cd, kid1);
4515
0
    if (cd->link_vk_to_keyring && kid2)
4516
0
      crypt_unlink_key_from_custom_keyring(cd, kid2);
4517
0
  }
4518
4519
0
  if (r < 0 && p_opal)
4520
0
    opal_lock(cd, crypt_data_device(cd), opal_segment_number);
4521
4522
0
  opal_exclusive_unlock(cd, opal_lh);
4523
0
  crypt_free_volume_key(zerokey);
4524
0
  crypt_free_volume_key(opal_key);
4525
0
  crypt_free_volume_key(crypt_key);
4526
4527
0
  return r;
4528
0
}
4529
4530
/* key must be properly verified */
4531
static int resume_by_volume_key(struct crypt_device *cd,
4532
    struct volume_key *vk,
4533
    const char *name)
4534
0
{
4535
0
  assert(cd);
4536
4537
0
  if (isLUKS2(cd->type))
4538
0
    return resume_luks2_by_volume_key(cd,
4539
0
        LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT),
4540
0
        vk, name);
4541
4542
0
  if (isLUKS1(cd->type))
4543
0
    return resume_luks1_by_volume_key(cd, vk, name);
4544
4545
0
  return -EINVAL;
4546
0
}
4547
4548
int crypt_resume_by_keyslot_context(struct crypt_device *cd,
4549
             const char *name,
4550
             int keyslot,
4551
             struct crypt_keyslot_context *kc)
4552
0
{
4553
0
  int r;
4554
0
  struct volume_key *vk = NULL;
4555
0
  int unlocked_keyslot = -EINVAL;
4556
0
  struct crypt_dm_active_device dmd = {};
4557
4558
0
  if (!name)
4559
0
    return -EINVAL;
4560
4561
0
  log_dbg(cd, "Resuming volume %s [keyslot %d] using %s.", name, keyslot, keyslot_context_type_string(kc));
4562
4563
0
  if ((r = onlyLUKS(cd)))
4564
0
    return r;
4565
4566
0
  r = dm_status_suspended(cd, name);
4567
0
  if (r < 0)
4568
0
    return r;
4569
4570
0
  if (!r) {
4571
0
    log_err(cd, _("Volume %s is not suspended."), name);
4572
0
    return -EINVAL;
4573
0
  }
4574
4575
0
  r = dm_query_device(cd, name, DM_ACTIVE_UUID, &dmd);
4576
0
  if (r < 0)
4577
0
    return r;
4578
4579
0
  r = dm_uuid_cmp(dmd.uuid, LUKS_UUID(cd));
4580
0
  if (r < 0) {
4581
0
    log_dbg(cd, "LUKS device header uuid: %s mismatches DM returned uuid %s",
4582
0
      LUKS_UUID(cd), dmd.uuid);
4583
0
    goto out;
4584
0
  }
4585
4586
0
  if (isLUKS1(cd->type) && kc->get_luks1_volume_key)
4587
0
    r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk);
4588
0
  else if (isLUKS2(cd->type) && kc->get_luks2_volume_key)
4589
0
    r = kc->get_luks2_volume_key(cd, kc, keyslot, &vk);
4590
0
  else
4591
0
    r = -EINVAL;
4592
0
  if (r < 0)
4593
0
    goto out;
4594
0
  unlocked_keyslot = r;
4595
4596
0
  if (isLUKS1(cd->type)) {
4597
0
    r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
4598
0
    crypt_volume_key_set_id(vk, 0);
4599
0
  } else if (isLUKS2(cd->type)) {
4600
0
    r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
4601
0
    crypt_volume_key_set_id(vk, r);
4602
0
  } else
4603
0
    r = -EINVAL;
4604
0
  if (r < 0)
4605
0
    goto out;
4606
4607
0
  r = resume_by_volume_key(cd, vk, name);
4608
0
out:
4609
0
  crypt_free_volume_key(vk);
4610
0
  free(CONST_CAST(void*)dmd.uuid);
4611
4612
0
  return r < 0 ? r : unlocked_keyslot;
4613
0
}
4614
4615
int crypt_resume_by_passphrase(struct crypt_device *cd,
4616
             const char *name,
4617
             int keyslot,
4618
             const char *passphrase,
4619
             size_t passphrase_size)
4620
0
{
4621
0
  int r;
4622
0
  struct crypt_keyslot_context kc = {};
4623
4624
0
  crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size);
4625
0
  r = crypt_resume_by_keyslot_context(cd, name, keyslot, &kc);
4626
0
  crypt_keyslot_context_destroy_internal(&kc);
4627
4628
0
  return r;
4629
0
}
4630
4631
int crypt_resume_by_keyfile_device_offset(struct crypt_device *cd,
4632
            const char *name,
4633
            int keyslot,
4634
            const char *keyfile,
4635
            size_t keyfile_size,
4636
            uint64_t keyfile_offset)
4637
0
{
4638
0
  int r;
4639
0
  struct crypt_keyslot_context kc = {};
4640
4641
0
  crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset);
4642
0
  r = crypt_resume_by_keyslot_context(cd, name, keyslot, &kc);
4643
0
  crypt_keyslot_context_destroy_internal(&kc);
4644
4645
0
  return r;
4646
0
}
4647
4648
int crypt_resume_by_keyfile(struct crypt_device *cd,
4649
          const char *name,
4650
          int keyslot,
4651
          const char *keyfile,
4652
          size_t keyfile_size)
4653
0
{
4654
0
  return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
4655
0
                keyfile, keyfile_size, 0);
4656
0
}
4657
4658
int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
4659
           const char *name,
4660
           int keyslot,
4661
           const char *keyfile,
4662
           size_t keyfile_size,
4663
           size_t keyfile_offset)
4664
0
{
4665
0
  return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
4666
0
              keyfile, keyfile_size, keyfile_offset);
4667
0
}
4668
4669
int crypt_resume_by_volume_key(struct crypt_device *cd,
4670
  const char *name,
4671
  const char *volume_key,
4672
  size_t volume_key_size)
4673
0
{
4674
0
  int r;
4675
0
  struct crypt_keyslot_context kc = {};
4676
4677
0
  crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
4678
0
  r = crypt_resume_by_keyslot_context(cd, name, CRYPT_ANY_SLOT /* unused */, &kc);
4679
0
  crypt_keyslot_context_destroy_internal(&kc);
4680
4681
0
  if (r == -EPERM || r == -ENOENT)
4682
0
    log_err(cd, _("Volume key does not match the volume."));
4683
4684
0
  return r;
4685
0
}
4686
4687
int crypt_resume_by_token_pin(struct crypt_device *cd, const char *name,
4688
  const char *type, int token, const char *pin, size_t pin_size,
4689
  void *usrptr)
4690
0
{
4691
0
  int r;
4692
0
  struct crypt_keyslot_context kc = {};
4693
4694
0
  crypt_keyslot_context_init_by_token_internal(&kc, token, type, pin, pin_size, usrptr);
4695
0
  r = crypt_resume_by_keyslot_context(cd, name, CRYPT_ANY_SLOT, &kc);
4696
0
  crypt_keyslot_context_destroy_internal(&kc);
4697
4698
0
  return r;
4699
0
}
4700
4701
/*
4702
 * Keyslot manipulation
4703
 */
4704
int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
4705
  int keyslot, // -1 any
4706
  const char *passphrase,
4707
  size_t passphrase_size,
4708
  const char *new_passphrase,
4709
  size_t new_passphrase_size)
4710
0
{
4711
0
  int r;
4712
0
  struct crypt_keyslot_context kc = {}, new_kc = {};
4713
4714
0
  if (!passphrase || !new_passphrase)
4715
0
    return -EINVAL;
4716
4717
0
  crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size);
4718
0
  crypt_keyslot_context_init_by_passphrase_internal(&new_kc, new_passphrase, new_passphrase_size);
4719
4720
0
  r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
4721
4722
0
  crypt_keyslot_context_destroy_internal(&kc);
4723
0
  crypt_keyslot_context_destroy_internal(&new_kc);
4724
4725
0
  return r;
4726
0
}
4727
4728
int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
4729
  int keyslot_old,
4730
  int keyslot_new,
4731
  const char *passphrase,
4732
  size_t passphrase_size,
4733
  const char *new_passphrase,
4734
  size_t new_passphrase_size)
4735
0
{
4736
0
  bool keyslot_swap = false;
4737
0
  int digest = -1, r;
4738
0
  struct luks2_keyslot_params params;
4739
0
  struct volume_key *vk = NULL;
4740
4741
0
  if (!passphrase || !new_passphrase)
4742
0
    return -EINVAL;
4743
4744
0
  log_dbg(cd, "Changing passphrase from old keyslot %d to new %d.",
4745
0
    keyslot_old, keyslot_new);
4746
4747
0
  if ((r = onlyLUKS(cd)))
4748
0
    return r;
4749
4750
0
  if (isLUKS1(cd->type))
4751
0
    r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
4752
0
             &cd->u.luks1.hdr, &vk, cd);
4753
0
  else if (isLUKS2(cd->type)) {
4754
0
    r = LUKS2_keyslot_open(cd, keyslot_old, CRYPT_ANY_SEGMENT, passphrase, passphrase_size, &vk);
4755
    /* will fail for keyslots w/o digest. fix if supported in a future */
4756
0
    if (r >= 0) {
4757
0
      digest = LUKS2_digest_by_keyslot(&cd->u.luks2.hdr, r);
4758
0
      if (digest < 0)
4759
0
        r = -EINVAL;
4760
0
    }
4761
0
  } else
4762
0
    r = -EINVAL;
4763
0
  if (r < 0)
4764
0
    goto out;
4765
4766
0
  if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
4767
0
    log_dbg(cd, "Keyslot mismatch.");
4768
0
    goto out;
4769
0
  }
4770
0
  keyslot_old = r;
4771
4772
0
  if (isLUKS1(cd->type)) {
4773
0
    if (keyslot_new == CRYPT_ANY_SLOT) {
4774
0
      keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
4775
0
      if (keyslot_new < 0)
4776
0
        keyslot_new = keyslot_old;
4777
0
    }
4778
0
  } else if (isLUKS2(cd->type)) {
4779
    /* If there is a free keyslot (both id and binary area) avoid in-place keyslot area overwrite  */
4780
0
    if (keyslot_new == CRYPT_ANY_SLOT || keyslot_new == keyslot_old) {
4781
0
      keyslot_new = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, crypt_volume_key_length(vk));
4782
0
      if (keyslot_new < 0)
4783
0
        keyslot_new = keyslot_old;
4784
0
      else
4785
0
        keyslot_swap = true;
4786
0
    }
4787
0
  }
4788
0
  log_dbg(cd, "Key change, old slot %d, new slot %d.", keyslot_old, keyslot_new);
4789
4790
0
  if (isLUKS1(cd->type)) {
4791
0
    if (keyslot_old == keyslot_new) {
4792
0
      log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
4793
0
      (void)crypt_keyslot_destroy(cd, keyslot_old);
4794
0
    }
4795
0
    r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
4796
0
         &cd->u.luks1.hdr, vk, cd);
4797
0
  } else if (isLUKS2(cd->type)) {
4798
0
    r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, &params);
4799
0
    if (r)
4800
0
      goto out;
4801
4802
0
    if (keyslot_old != keyslot_new) {
4803
0
      r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, digest, 1, 0);
4804
0
      if (r < 0)
4805
0
        goto out;
4806
0
      r = LUKS2_token_assignment_copy(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new, 0);
4807
0
      if (r < 0)
4808
0
        goto out;
4809
0
    } else
4810
0
      log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
4811
4812
0
    r = LUKS2_keyslot_store(cd,  &cd->u.luks2.hdr,
4813
0
          keyslot_new, new_passphrase,
4814
0
          new_passphrase_size, vk, &params);
4815
0
    if (r < 0)
4816
0
      goto out;
4817
4818
    /* Swap old & new so the final keyslot number remains */
4819
0
    if (keyslot_swap && keyslot_old != keyslot_new) {
4820
0
      r = LUKS2_keyslot_swap(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new);
4821
0
      if (r < 0)
4822
0
        goto out;
4823
4824
      /* Swap slot id */
4825
0
      r = keyslot_old;
4826
0
      keyslot_old = keyslot_new;
4827
0
      keyslot_new = r;
4828
0
    }
4829
0
  } else
4830
0
    r = -EINVAL;
4831
4832
0
  if (r >= 0 && keyslot_old != keyslot_new)
4833
0
    r = crypt_keyslot_destroy(cd, keyslot_old);
4834
4835
0
  if (r < 0)
4836
0
    log_err(cd, _("Failed to swap new key slot."));
4837
0
out:
4838
0
  crypt_free_volume_key(vk);
4839
0
  if (r < 0) {
4840
0
    _luks2_rollback(cd);
4841
0
    return r;
4842
0
  }
4843
0
  return keyslot_new;
4844
0
}
4845
4846
int crypt_keyslot_add_by_keyfile_device_offset(struct crypt_device *cd,
4847
  int keyslot,
4848
  const char *keyfile,
4849
  size_t keyfile_size,
4850
  uint64_t keyfile_offset,
4851
  const char *new_keyfile,
4852
  size_t new_keyfile_size,
4853
  uint64_t new_keyfile_offset)
4854
0
{
4855
0
  int r;
4856
0
  struct crypt_keyslot_context kc = {}, new_kc = {};
4857
4858
0
  if (!keyfile || !new_keyfile)
4859
0
    return -EINVAL;
4860
4861
0
  crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset);
4862
0
  crypt_keyslot_context_init_by_keyfile_internal(&new_kc, new_keyfile, new_keyfile_size, new_keyfile_offset);
4863
4864
0
  r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
4865
4866
0
  crypt_keyslot_context_destroy_internal(&kc);
4867
0
  crypt_keyslot_context_destroy_internal(&new_kc);
4868
4869
0
  return r;
4870
0
}
4871
4872
int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
4873
  int keyslot,
4874
  const char *keyfile,
4875
  size_t keyfile_size,
4876
  const char *new_keyfile,
4877
  size_t new_keyfile_size)
4878
0
{
4879
0
  return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
4880
0
        keyfile, keyfile_size, 0,
4881
0
        new_keyfile, new_keyfile_size, 0);
4882
0
}
4883
4884
int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
4885
  int keyslot,
4886
  const char *keyfile,
4887
  size_t keyfile_size,
4888
  size_t keyfile_offset,
4889
  const char *new_keyfile,
4890
  size_t new_keyfile_size,
4891
  size_t new_keyfile_offset)
4892
0
{
4893
0
  return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
4894
0
        keyfile, keyfile_size, keyfile_offset,
4895
0
        new_keyfile, new_keyfile_size, new_keyfile_offset);
4896
0
}
4897
4898
int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
4899
  int keyslot,
4900
  const char *volume_key,
4901
  size_t volume_key_size,
4902
  const char *passphrase,
4903
  size_t passphrase_size)
4904
0
{
4905
0
  int r;
4906
0
  struct crypt_keyslot_context kc = {}, new_kc = {};
4907
4908
0
  if (!passphrase)
4909
0
    return -EINVAL;
4910
4911
0
  crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
4912
0
  crypt_keyslot_context_init_by_passphrase_internal(&new_kc, passphrase, passphrase_size);
4913
4914
0
  r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
4915
4916
0
  crypt_keyslot_context_destroy_internal(&kc);
4917
0
  crypt_keyslot_context_destroy_internal(&new_kc);
4918
4919
0
  return r;
4920
0
}
4921
4922
int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
4923
0
{
4924
0
  crypt_keyslot_info ki;
4925
0
  int r;
4926
4927
0
  log_dbg(cd, "Destroying keyslot %d.", keyslot);
4928
4929
0
  if ((r = onlyLUKSunrestricted(cd)))
4930
0
    return r;
4931
4932
0
  ki = crypt_keyslot_status(cd, keyslot);
4933
0
  if (ki == CRYPT_SLOT_INVALID) {
4934
0
    log_err(cd, _("Key slot %d is invalid."), keyslot);
4935
0
    return -EINVAL;
4936
0
  }
4937
4938
0
  if (isLUKS1(cd->type)) {
4939
0
    if (ki == CRYPT_SLOT_INACTIVE) {
4940
0
      log_err(cd, _("Keyslot %d is not active."), keyslot);
4941
0
      return -EINVAL;
4942
0
    }
4943
0
    return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
4944
0
  }
4945
4946
0
  return LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot);
4947
0
}
4948
4949
static int _check_header_data_overlap(struct crypt_device *cd, const char *name)
4950
0
{
4951
0
  if (!name || !isLUKS(cd->type))
4952
0
    return 0;
4953
4954
0
  if (device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd)) <= 0)
4955
0
    return 0;
4956
4957
  /* FIXME: check real header size */
4958
0
  if (crypt_get_data_offset(cd) == 0) {
4959
0
    log_err(cd, _("Device header overlaps with data area."));
4960
0
    return -EINVAL;
4961
0
  }
4962
4963
0
  return 0;
4964
0
}
4965
4966
static int check_devices(struct crypt_device *cd, const char *name, const char *iname, uint32_t *flags)
4967
0
{
4968
0
  int r;
4969
4970
0
  if (!flags || !name)
4971
0
    return -EINVAL;
4972
4973
0
  if (iname) {
4974
0
    r = dm_status_device(cd, iname);
4975
0
    if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
4976
0
      return -EBUSY;
4977
0
    if (r < 0 && r != -ENODEV)
4978
0
      return r;
4979
0
    if (r == -ENODEV)
4980
0
      *flags &= ~CRYPT_ACTIVATE_REFRESH;
4981
0
  }
4982
4983
0
  r = dm_status_device(cd, name);
4984
0
  if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
4985
0
    return -EBUSY;
4986
0
  if (r < 0 && r != -ENODEV)
4987
0
    return r;
4988
0
  if (r == -ENODEV)
4989
0
    *flags &= ~CRYPT_ACTIVATE_REFRESH;
4990
4991
0
  return 0;
4992
0
}
4993
4994
static int _create_device_with_integrity(struct crypt_device *cd,
4995
  const char *type, const char *name, const char *iname,
4996
  const char *ipath, struct crypt_dm_active_device *dmd,
4997
  struct crypt_dm_active_device *dmdi)
4998
0
{
4999
0
  int r;
5000
0
  enum devcheck device_check;
5001
0
  struct dm_target *tgt;
5002
0
  struct device *device = NULL;
5003
5004
0
  if (!single_segment(dmd))
5005
0
    return -EINVAL;
5006
5007
0
  tgt = &dmd->segment;
5008
0
  if (tgt->type != DM_CRYPT)
5009
0
    return -EINVAL;
5010
5011
0
  device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
5012
5013
0
  r = INTEGRITY_activate_dmd_device(cd, iname, CRYPT_SUBDEV, dmdi, 0);
5014
0
  if (r)
5015
0
    return r;
5016
5017
0
  r = device_alloc(cd, &device, ipath);
5018
0
  if (r < 0)
5019
0
    goto out;
5020
0
  tgt->data_device = device;
5021
5022
0
  r = device_block_adjust(cd, tgt->data_device, device_check,
5023
0
        tgt->u.crypt.offset, &dmd->size, &dmd->flags);
5024
5025
0
  if (!r)
5026
0
    r = dm_create_device(cd, name, type, dmd);
5027
0
out:
5028
0
  if (r < 0)
5029
0
    dm_remove_device(cd, iname, 0);
5030
5031
0
  device_free(cd, device);
5032
0
  return r;
5033
0
}
5034
5035
static int kernel_keyring_support(void)
5036
0
{
5037
0
  static unsigned _checked = 0;
5038
5039
0
  if (!_checked) {
5040
0
    _kernel_keyring_supported = keyring_check();
5041
0
    _checked = 1;
5042
0
  }
5043
5044
0
  return _kernel_keyring_supported;
5045
0
}
5046
5047
static int dmcrypt_keyring_bug(void)
5048
0
{
5049
0
  uint64_t kversion;
5050
5051
0
  if (kernel_version(&kversion))
5052
0
    return 1;
5053
0
  return kversion < compact_version(4,15,0,0);
5054
0
}
5055
5056
int create_or_reload_device(struct crypt_device *cd, const char *name,
5057
         const char *type, struct crypt_dm_active_device *dmd)
5058
0
{
5059
0
  int r;
5060
0
  enum devcheck device_check;
5061
0
  struct dm_target *tgt;
5062
0
  uint64_t offset, dmflags = 0;
5063
5064
0
  if (!type || !name || !single_segment(dmd))
5065
0
    return -EINVAL;
5066
5067
0
  tgt = &dmd->segment;
5068
0
  if (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY && tgt->type != DM_LINEAR)
5069
0
    return -EINVAL;
5070
5071
  /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
5072
0
  r = check_devices(cd, name, NULL, &dmd->flags);
5073
0
  if (r)
5074
0
    return r;
5075
5076
0
  if (dmd->flags & CRYPT_ACTIVATE_REFRESH) {
5077
    /* Refresh and recalculate means increasing dm-integrity device */
5078
0
    if (tgt->type == DM_INTEGRITY && dmd->flags & CRYPT_ACTIVATE_RECALCULATE)
5079
0
      dmflags = DM_SUSPEND_SKIP_LOCKFS | DM_SUSPEND_NOFLUSH;
5080
0
    r = _reload_device(cd, name, dmd, dmflags);
5081
0
  } else {
5082
0
    if (tgt->type == DM_CRYPT || tgt->type == DM_LINEAR) {
5083
0
      device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
5084
0
      offset = tgt->type == DM_CRYPT ? tgt->u.crypt.offset : tgt->u.linear.offset;
5085
5086
0
      r = device_block_adjust(cd, tgt->data_device, device_check,
5087
0
          offset, &dmd->size, &dmd->flags);
5088
0
      if (!r) {
5089
0
        tgt->size = dmd->size;
5090
0
        r = dm_create_device(cd, name, type, dmd);
5091
0
      }
5092
0
    } else if (tgt->type == DM_INTEGRITY) {
5093
0
      r = device_block_adjust(cd, tgt->data_device, DEV_EXCL,
5094
0
          tgt->u.integrity.offset, NULL, &dmd->flags);
5095
0
      if (r)
5096
0
        return r;
5097
5098
0
      if (tgt->u.integrity.meta_device) {
5099
0
        r = device_block_adjust(cd, tgt->u.integrity.meta_device, DEV_EXCL, 0, NULL, NULL);
5100
0
        if (r)
5101
0
          return r;
5102
0
      }
5103
5104
0
      r = dm_create_device(cd, name, type, dmd);
5105
0
    }
5106
0
  }
5107
5108
0
  return r;
5109
0
}
5110
5111
int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
5112
         const char *type, struct crypt_dm_active_device *dmd,
5113
         struct crypt_dm_active_device *dmdi)
5114
0
{
5115
0
  int r;
5116
0
  char *iname = NULL, *ipath = NULL;
5117
5118
0
  if (!type || !name || !dmd || !dmdi)
5119
0
    return -EINVAL;
5120
5121
0
  r = dm_get_iname(name, &iname, false);
5122
0
  if (r)
5123
0
    goto out;
5124
5125
0
  r = dm_get_iname(name, &ipath, true);
5126
0
  if (r)
5127
0
    goto out;
5128
5129
  /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
5130
0
  r = check_devices(cd, name, iname, &dmd->flags);
5131
0
  if (r)
5132
0
    goto out;
5133
5134
0
  if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
5135
0
    r = _reload_device_with_integrity(cd, name, iname, ipath, dmd, dmdi);
5136
0
  else
5137
0
    r = _create_device_with_integrity(cd, type, name, iname, ipath, dmd, dmdi);
5138
0
out:
5139
0
  free(ipath);
5140
0
  free(iname);
5141
5142
0
  return r;
5143
0
}
5144
5145
static int load_all_keys(struct crypt_device *cd, struct volume_key *vks)
5146
0
{
5147
0
  int r;
5148
0
  struct volume_key *vk = vks;
5149
5150
0
  while (vk) {
5151
0
    r = LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, crypt_volume_key_get_id(vk));
5152
0
    if (r < 0)
5153
0
      return r;
5154
0
    vk = crypt_volume_key_next(vk);
5155
0
  }
5156
5157
0
  return 0;
5158
0
}
5159
5160
#if USE_LUKS2_REENCRYPTION
5161
static int _activate_reencrypt_device_by_vk(struct crypt_device *cd,
5162
  struct luks2_hdr *hdr,
5163
  const char *name,
5164
  struct volume_key *vks,
5165
  uint32_t flags)
5166
0
{
5167
0
  bool dynamic_size;
5168
0
  crypt_reencrypt_info ri;
5169
0
  uint64_t minimal_size, device_size;
5170
0
  int r = 0;
5171
0
  struct crypt_lock_handle *reencrypt_lock = NULL;
5172
0
  struct volume_key *vk;
5173
5174
0
  assert(hdr);
5175
0
  assert(vks);
5176
5177
0
  r = LUKS2_reencrypt_lock(cd, &reencrypt_lock);
5178
0
  if (r) {
5179
0
    if (r == -EBUSY)
5180
0
      log_err(cd, _("Reencryption in-progress. Cannot activate device."));
5181
0
    else
5182
0
      log_err(cd, _("Failed to get reencryption lock."));
5183
0
    return r;
5184
0
  }
5185
5186
0
  if ((r = crypt_load(cd, CRYPT_LUKS2, NULL)))
5187
0
    goto out;
5188
5189
0
  ri = LUKS2_reencrypt_status(hdr);
5190
0
  if (ri == CRYPT_REENCRYPT_INVALID) {
5191
0
    r = -EINVAL;
5192
0
    goto out;
5193
0
  }
5194
5195
0
  if (ri > CRYPT_REENCRYPT_NONE) {
5196
    /* it's sufficient to force re-verify the reencrypt digest only */
5197
0
    r = LUKS2_reencrypt_digest_verify(cd, &cd->u.luks2.hdr, vks);
5198
0
    if (r < 0)
5199
0
      goto out;
5200
5201
0
    if (ri == CRYPT_REENCRYPT_CRASH) {
5202
0
      r = LUKS2_reencrypt_locked_recovery_by_vks(cd, vks);
5203
0
      if (r < 0) {
5204
0
        log_err(cd, _("LUKS2 reencryption recovery using volume key(s) failed."));
5205
0
        goto out;
5206
0
      }
5207
5208
0
      ri = LUKS2_reencrypt_status(hdr);
5209
0
    }
5210
0
  }
5211
5212
  /* recovery finished reencryption or it was already finished after metadata reload */
5213
0
  if (ri == CRYPT_REENCRYPT_NONE) {
5214
0
    vk = crypt_volume_key_by_id(vks, LUKS2_digest_by_segment(hdr, CRYPT_DEFAULT_SEGMENT));
5215
0
    if (!vk) {
5216
0
      r = -EPERM;
5217
0
      goto out;
5218
0
    }
5219
5220
0
    r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
5221
0
    if (r >= 0)
5222
0
      r = LUKS2_activate(cd, name, vk, NULL, flags);
5223
0
    goto out;
5224
0
  }
5225
0
  if (ri > CRYPT_REENCRYPT_CLEAN) {
5226
0
    r = -EINVAL;
5227
0
    goto out;
5228
0
  }
5229
5230
0
  if ((r = LUKS2_get_data_size(hdr, &minimal_size, &dynamic_size)))
5231
0
    goto out;
5232
5233
0
  log_dbg(cd, "Entering clean reencryption state mode.");
5234
5235
0
  r = LUKS2_reencrypt_check_device_size(cd, hdr, minimal_size, &device_size,
5236
0
                !(flags & CRYPT_ACTIVATE_SHARED),
5237
0
                dynamic_size);
5238
0
  if (r < 0)
5239
0
    goto out;
5240
0
  r = LUKS2_activate_multi(cd, name, vks, device_size >> SECTOR_SHIFT, flags);
5241
0
out:
5242
0
  LUKS2_reencrypt_unlock(cd, reencrypt_lock);
5243
5244
0
  return r;
5245
0
}
5246
5247
/*
5248
 * Activation/deactivation of a device
5249
 */
5250
static int _activate_luks2_by_volume_key(struct crypt_device *cd,
5251
  const char *name,
5252
  struct volume_key *vk,
5253
  struct volume_key *external_key,
5254
  uint32_t flags)
5255
0
{
5256
0
  int r;
5257
0
  crypt_reencrypt_info ri;
5258
0
  ri = LUKS2_reencrypt_status(&cd->u.luks2.hdr);
5259
0
  if (ri == CRYPT_REENCRYPT_INVALID)
5260
0
    return -EINVAL;
5261
5262
0
  if (ri > CRYPT_REENCRYPT_NONE) {
5263
    /* reencryption must reverify keys after taking the reencryption lock and reloading metadata */
5264
0
    r = _activate_reencrypt_device_by_vk(cd, &cd->u.luks2.hdr, name, vk, flags);
5265
0
  } else {
5266
    /* hw-opal data segment type does not require volume key for activation */
5267
0
    assert(!vk || crypt_volume_key_get_id(vk) == LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT));
5268
0
    r = LUKS2_activate(cd, name, vk, external_key, flags);
5269
0
  }
5270
5271
0
  return r;
5272
0
}
5273
#else
5274
static int _activate_luks2_by_volume_key(struct crypt_device *cd,
5275
  const char *name,
5276
  struct volume_key *vk,
5277
  struct volume_key *external_key,
5278
  uint32_t flags)
5279
{
5280
  int r;
5281
  crypt_reencrypt_info ri;
5282
  ri = LUKS2_reencrypt_status(&cd->u.luks2.hdr);
5283
  if (ri == CRYPT_REENCRYPT_INVALID)
5284
    return -EINVAL;
5285
5286
  if (ri > CRYPT_REENCRYPT_NONE) {
5287
    log_err(cd, _("This operation is not supported for this device type."));
5288
    r = -ENOTSUP;
5289
  } else {
5290
    assert(crypt_volume_key_get_id(vk) == LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT));
5291
    r = LUKS2_activate(cd, name, vk, external_key, flags);
5292
  }
5293
5294
  return r;
5295
}
5296
#endif
5297
5298
static int _activate_loopaes(struct crypt_device *cd,
5299
  const char *name,
5300
  const char *buffer,
5301
  size_t buffer_size,
5302
  uint32_t flags)
5303
0
{
5304
0
  int r;
5305
0
  unsigned int key_count = 0;
5306
0
  struct volume_key *vk = NULL;
5307
0
  char *buffer_copy;
5308
5309
0
  buffer_copy = crypt_safe_alloc(buffer_size);
5310
0
  if (!buffer_copy)
5311
0
    return -ENOMEM;
5312
0
  crypt_safe_memcpy(buffer_copy, buffer, buffer_size);
5313
5314
0
  r = LOOPAES_parse_keyfile(cd, &vk, cd->u.loopaes.hdr.hash, &key_count,
5315
0
          buffer_copy, buffer_size);
5316
0
  crypt_safe_free(buffer_copy);
5317
5318
0
  if (!r && name)
5319
0
    r = LOOPAES_activate(cd, name, cd->u.loopaes.cipher, key_count,
5320
0
             vk, flags);
5321
5322
0
  crypt_free_volume_key(vk);
5323
5324
0
  return r;
5325
0
}
5326
5327
static int _activate_check_status(struct crypt_device *cd, const char *name, unsigned reload)
5328
0
{
5329
0
  int r;
5330
5331
0
  if (!name)
5332
0
    return 0;
5333
5334
0
  r = dm_status_device(cd, name);
5335
5336
0
  if (r >= 0 && reload)
5337
0
    return 0;
5338
5339
0
  if (r >= 0 || r == -EEXIST) {
5340
0
    log_err(cd, _("Device %s already exists."), name);
5341
0
    return -EEXIST;
5342
0
  }
5343
5344
0
  if (r == -ENODEV)
5345
0
    return 0;
5346
5347
0
  log_err(cd, _("Cannot use device %s, name is invalid or still in use."), name);
5348
0
  return r;
5349
0
}
5350
5351
static int _verify_reencrypt_keys(struct crypt_device *cd, struct volume_key *vks)
5352
0
{
5353
0
  int r;
5354
5355
0
  assert(cd && (isLUKS2(cd->type)));
5356
5357
0
  r = LUKS2_reencrypt_digest_verify(cd, &cd->u.luks2.hdr, vks);
5358
0
  if (r == -EPERM || r == -ENOENT || r == -EINVAL)
5359
0
    log_err(cd, _("Reencryption volume keys do not match the volume."));
5360
5361
0
  return r;
5362
0
}
5363
5364
static int _verify_key(struct crypt_device *cd,
5365
  bool unbound_key,
5366
  struct volume_key *vk)
5367
0
{
5368
0
  int r = -EINVAL;
5369
5370
0
  assert(cd);
5371
5372
0
  if (isPLAIN(cd->type)) {
5373
0
    if (vk && crypt_volume_key_length(vk) == cd->u.plain.key_size) {
5374
0
      r = KEY_VERIFIED;
5375
0
    } else
5376
0
      log_err(cd, _("Incorrect volume key specified for plain device."));
5377
0
  } else if (isLUKS1(cd->type)) {
5378
0
    if (!vk)
5379
0
      return -EINVAL;
5380
5381
0
    r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
5382
0
  } else if (isLUKS2(cd->type)) {
5383
0
    if (!vk)
5384
0
      return -EINVAL;
5385
5386
0
    if (unbound_key)
5387
0
      r = LUKS2_digest_verify_by_any_matching(cd, vk, /* exclude_default_segment= */ false);
5388
0
    else
5389
0
      r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
5390
0
  } else if (isVERITY(cd->type))
5391
0
    r = KEY_VERIFIED;
5392
0
  else if (isTCRYPT(cd->type))
5393
0
    r = KEY_VERIFIED;
5394
0
  else if (isINTEGRITY(cd->type))
5395
0
    r = KEY_VERIFIED;
5396
0
  else if (isBITLK(cd->type))
5397
0
    r = KEY_VERIFIED;
5398
0
  else if (isFVAULT2(cd->type)) {
5399
0
    if (vk && crypt_volume_key_length(vk) == FVAULT2_volume_key_size())
5400
0
      r = KEY_VERIFIED;
5401
0
  } else
5402
0
    log_err(cd, _("Device type is not properly initialized."));
5403
5404
0
  if (r >= KEY_VERIFIED)
5405
0
    crypt_volume_key_set_id(vk, r);
5406
5407
0
  return r > 0 ? 0 : r;
5408
0
}
5409
5410
/* activation/deactivation of device mapping */
5411
static int _activate_by_volume_key(struct crypt_device *cd,
5412
  const char *name,
5413
  struct volume_key *vk,
5414
  struct volume_key *external_key,
5415
  uint32_t flags)
5416
0
{
5417
0
  int r;
5418
5419
0
  assert(cd);
5420
0
  assert(name);
5421
5422
0
  r = _check_header_data_overlap(cd, name);
5423
0
  if (r < 0)
5424
0
    return r;
5425
5426
  /* use key directly, no hash */
5427
0
  if (isPLAIN(cd->type)) {
5428
0
    assert(!external_key);
5429
0
    assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5430
5431
0
    r = PLAIN_activate(cd, name, vk, cd->u.plain.hdr.size, flags);
5432
0
  } else if (isLUKS1(cd->type)) {
5433
0
    assert(!external_key);
5434
0
    assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5435
5436
0
    r = LUKS1_activate(cd, name, vk, flags);
5437
0
  } else if (isLUKS2(cd->type)) {
5438
0
    r = _activate_luks2_by_volume_key(cd, name, vk, external_key, flags);
5439
0
  } else if (isVERITY(cd->type)) {
5440
0
    assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5441
0
    r = VERITY_activate(cd, name, vk, external_key, cd->u.verity.fec_device,
5442
0
            &cd->u.verity.hdr, flags);
5443
0
  } else if (isTCRYPT(cd->type)) {
5444
0
    assert(!external_key);
5445
0
    r = TCRYPT_activate(cd, name, &cd->u.tcrypt.hdr,
5446
0
            &cd->u.tcrypt.params, flags);
5447
0
  } else if (isINTEGRITY(cd->type)) {
5448
0
    assert(!external_key);
5449
0
    assert(!vk || crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5450
0
    r = INTEGRITY_activate(cd, name, &cd->u.integrity.params, vk,
5451
0
               cd->u.integrity.journal_crypt_key,
5452
0
               cd->u.integrity.journal_mac_key, flags,
5453
0
               cd->u.integrity.sb_flags);
5454
0
  } else if (isBITLK(cd->type)) {
5455
0
    assert(!external_key);
5456
0
    assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5457
0
    r = BITLK_activate_by_volume_key(cd, name, vk, &cd->u.bitlk.params, flags);
5458
0
  } else if (isFVAULT2(cd->type)) {
5459
0
    assert(!external_key);
5460
0
    assert(crypt_volume_key_get_id(vk) == KEY_VERIFIED);
5461
0
    r = FVAULT2_activate_by_volume_key(cd, name, vk, &cd->u.fvault2.params, flags);
5462
0
  } else {
5463
0
    log_err(cd, _("Device type is not properly initialized."));
5464
0
    r = -EINVAL;
5465
0
  }
5466
5467
0
  return r;
5468
0
}
5469
5470
int crypt_activate_by_keyslot_context(struct crypt_device *cd,
5471
  const char *name,
5472
  int keyslot,
5473
  struct crypt_keyslot_context *kc,
5474
  int additional_keyslot,
5475
  struct crypt_keyslot_context *additional_kc,
5476
  uint32_t flags)
5477
0
{
5478
0
  bool use_keyring, luks2_reencryption = false;
5479
0
  struct volume_key *p_ext_key, *crypt_key = NULL, *opal_key = NULL, *vk = NULL,
5480
0
    *vk_sign = NULL, *p_crypt = NULL;
5481
0
  size_t passphrase_size;
5482
0
  const char *passphrase = NULL;
5483
0
  int unlocked_keyslot, r = -EINVAL;
5484
0
  key_serial_t kid1 = 0, kid2 = 0;
5485
0
  struct luks2_hdr *hdr = &cd->u.luks2.hdr;
5486
5487
0
  if (!cd || !kc)
5488
0
    return -EINVAL;
5489
5490
0
  log_dbg(cd, "%s volume %s [keyslot %d] using %s.",
5491
0
    name ? "Activating" : "Checking", name ?: "passphrase", keyslot, keyslot_context_type_string(kc));
5492
0
  if (!name && (flags & CRYPT_ACTIVATE_REFRESH))
5493
0
    return -EINVAL;
5494
0
  if ((flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_use_keyring_for_vk(cd))
5495
0
    return -EINVAL;
5496
0
  if ((flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) && name)
5497
0
    return -EINVAL;
5498
0
  if (!additional_kc && (additional_keyslot != CRYPT_ANY_SLOT))
5499
0
    return -EINVAL;
5500
0
  if ((kc->type == CRYPT_KC_TYPE_KEYRING) && !kernel_keyring_support()) {
5501
0
    log_err(cd, _("Kernel keyring is not supported by the kernel."));
5502
0
    return -EINVAL;
5503
0
  }
5504
0
  if ((kc->type == CRYPT_KC_TYPE_SIGNED_KEY) && !kernel_keyring_support()) {
5505
0
    log_err(cd, _("Kernel keyring missing: required for passing signature to kernel."));
5506
0
    return -EINVAL;
5507
0
  }
5508
0
  r = _check_header_data_overlap(cd, name);
5509
0
  if (r < 0)
5510
0
    return r;
5511
0
  r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
5512
0
  if (r < 0)
5513
0
    return r;
5514
5515
0
  if (kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN &&
5516
0
      isLOOPAES(cd->type)) {
5517
0
    r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size);
5518
0
    if (r < 0)
5519
0
      return r;
5520
5521
0
    return _activate_loopaes(cd, name, passphrase, passphrase_size, flags);
5522
0
  }
5523
5524
0
  if (flags & CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF)
5525
0
    cd->memory_hard_pbkdf_lock_enabled = true;
5526
5527
  /* acquire the volume key(s) */
5528
0
  r = -EINVAL;
5529
0
  if (isLUKS1(cd->type)) {
5530
0
    if (kc->get_luks1_volume_key)
5531
0
      r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk);
5532
0
  } else if (isLUKS2(cd->type)) {
5533
0
    if (flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY) {
5534
0
      if (kc->get_luks2_key)
5535
0
        r = kc->get_luks2_key(cd, kc, keyslot, CRYPT_ANY_SEGMENT, &vk);
5536
0
    } else {
5537
0
      switch (LUKS2_reencrypt_status(hdr)) {
5538
0
      case CRYPT_REENCRYPT_NONE:
5539
0
        if (kc->get_luks2_volume_key)
5540
0
          r = kc->get_luks2_volume_key(cd, kc, keyslot, &vk);
5541
0
        break;
5542
0
      case CRYPT_REENCRYPT_CLEAN: /* fall-through */
5543
0
      case CRYPT_REENCRYPT_CRASH:
5544
0
        luks2_reencryption = true;
5545
0
        r = LUKS2_keyslot_context_open_all_segments(cd, keyslot, additional_keyslot, kc, additional_kc, &vk);
5546
        /* fall-through */
5547
0
      default:
5548
0
        break;
5549
0
      }
5550
0
    }
5551
0
  } else if (isTCRYPT(cd->type)) {
5552
0
    r = 0;
5553
0
  } else if (name && isPLAIN(cd->type)) {
5554
0
    if (kc->type == CRYPT_KC_TYPE_VK_KEYRING) {
5555
0
      vk = crypt_alloc_volume_key(cd->u.plain.key_size, NULL);
5556
0
      if (!vk)
5557
0
        return -ENOMEM;
5558
0
      r = crypt_volume_key_set_description_by_name(vk, kc->u.vk_kr.key_description);
5559
0
      if (r < 0)
5560
0
        log_err(cd, _("Cannot use keyring key %s."), kc->u.vk_kr.key_description);
5561
0
    } else if (kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN) {
5562
0
      r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size);
5563
0
      if (r < 0)
5564
0
        return r;
5565
0
      r = process_key(cd, cd->u.plain.hdr.hash,
5566
0
          cd->u.plain.key_size,
5567
0
          passphrase, passphrase_size, &vk);
5568
0
    } else if (kc->get_plain_volume_key)
5569
0
      r = kc->get_plain_volume_key(cd, kc, &vk);
5570
0
  } else if (isBITLK(cd->type)) {
5571
0
    if (kc->get_bitlk_volume_key && (name || kc->type != CRYPT_KC_TYPE_KEY))
5572
0
      r = kc->get_bitlk_volume_key(cd, kc, &cd->u.bitlk.params, &vk);
5573
0
  } else if (isFVAULT2(cd->type)) {
5574
0
    if (kc->get_fvault2_volume_key)
5575
0
      r = kc->get_fvault2_volume_key(cd, kc, &cd->u.fvault2.params, &vk);
5576
0
  } else if (isVERITY(cd->type) && (name || kc->type != CRYPT_KC_TYPE_SIGNED_KEY)) {
5577
0
    if (kc->get_verity_volume_key)
5578
0
      r = kc->get_verity_volume_key(cd, kc, &vk, &vk_sign);
5579
0
    if (r >= 0)
5580
0
      r = VERITY_verify_params(cd, &cd->u.verity.hdr, vk_sign != NULL,
5581
0
             cd->u.verity.fec_device, vk);
5582
5583
0
    free(CONST_CAST(void*)cd->u.verity.root_hash);
5584
0
    cd->u.verity.root_hash = NULL;
5585
0
    flags |= CRYPT_ACTIVATE_READONLY;
5586
0
  } else if (isINTEGRITY(cd->type)) {
5587
0
    if (kc->get_integrity_volume_key)
5588
0
      r = kc->get_integrity_volume_key(cd, kc, &vk);
5589
0
  }
5590
0
  if (r < 0 && (r != -ENOENT || kc->type != CRYPT_KC_TYPE_KEY))
5591
0
    goto out;
5592
0
  unlocked_keyslot = r;
5593
5594
0
  if (r == -ENOENT && isLUKS(cd->type) && cd->volume_key) {
5595
0
    vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key),
5596
0
              crypt_volume_key_get_key(cd->volume_key));
5597
0
    r = vk ? 0 : -ENOMEM;
5598
0
  }
5599
0
  if (r == -ENOENT && isINTEGRITY(cd->type))
5600
0
    r = 0;
5601
5602
0
  if (r < 0)
5603
0
    goto out;
5604
5605
0
  if (luks2_reencryption)
5606
0
    r = _verify_reencrypt_keys(cd, vk);
5607
0
  else
5608
0
    r = _verify_key(cd, flags & CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY, vk);
5609
5610
0
  if (r < 0)
5611
0
    goto out;
5612
5613
0
  if (isLUKS2(cd->type)) {
5614
    /* split the key only if we do activation */
5615
0
    if (name && LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) {
5616
0
      r = LUKS2_split_crypt_and_opal_keys(cd, &cd->u.luks2.hdr,
5617
0
                  vk, &crypt_key,
5618
0
                  &opal_key);
5619
0
      if (r < 0)
5620
0
        goto out;
5621
5622
      /* copy volume key digest id in crypt subkey */
5623
0
      crypt_volume_key_set_id(crypt_key, crypt_volume_key_get_id(vk));
5624
5625
0
      p_crypt = crypt_key;
5626
0
      p_ext_key = opal_key ?: vk;
5627
0
    } else {
5628
0
      p_crypt = vk;
5629
0
      p_ext_key = NULL;
5630
0
    }
5631
5632
0
    if (!crypt_use_keyring_for_vk(cd))
5633
0
      use_keyring = false;
5634
0
    else
5635
      /* Force keyring use for activation of LUKS2 device in reencryption */
5636
0
      use_keyring = (name && (luks2_reencryption || !crypt_is_cipher_null(crypt_get_cipher(cd)))) ||
5637
0
              (flags & CRYPT_ACTIVATE_KEYRING_KEY);
5638
5639
0
    if (use_keyring) {
5640
      /* upload dm-crypt part of volume key in thread keyring if requested */
5641
0
      if (p_crypt) {
5642
0
        r = load_all_keys(cd, p_crypt);
5643
0
        if (r < 0)
5644
0
          goto out;
5645
0
        flags |= CRYPT_ACTIVATE_KEYRING_KEY;
5646
0
      }
5647
5648
      /* upload the volume key in custom user keyring if requested */
5649
0
      if (cd->link_vk_to_keyring) {
5650
0
        r = crypt_volume_key_load_in_custom_keyring(cd, vk, &kid1, &kid2);
5651
0
        if (r < 0) {
5652
0
          log_err(cd, _("Failed to link volume key in user defined keyring."));
5653
0
          goto out;
5654
0
        }
5655
0
      }
5656
0
    }
5657
0
  } else {
5658
0
    p_crypt = vk;
5659
0
    p_ext_key = vk_sign;
5660
0
  }
5661
5662
0
  if (name)
5663
0
    r = _activate_by_volume_key(cd, name, p_crypt, p_ext_key, flags);
5664
5665
0
  if (r >= 0 && unlocked_keyslot >= 0)
5666
0
    r = unlocked_keyslot;
5667
0
out:
5668
0
  if (r < 0) {
5669
0
    crypt_drop_uploaded_keyring_key(cd, vk);
5670
0
    crypt_drop_uploaded_keyring_key(cd, crypt_key);
5671
0
    if (cd->link_vk_to_keyring && kid1)
5672
0
      crypt_unlink_key_from_custom_keyring(cd, kid1);
5673
0
    if (cd->link_vk_to_keyring && kid2)
5674
0
      crypt_unlink_key_from_custom_keyring(cd, kid2);
5675
0
  }
5676
5677
0
  crypt_free_volume_key(vk);
5678
0
  crypt_free_volume_key(crypt_key);
5679
0
  crypt_free_volume_key(opal_key);
5680
0
  crypt_free_volume_key(vk_sign);
5681
0
  return r;
5682
0
}
5683
5684
int crypt_activate_by_passphrase(struct crypt_device *cd,
5685
  const char *name,
5686
  int keyslot,
5687
  const char *passphrase,
5688
  size_t passphrase_size,
5689
  uint32_t flags)
5690
0
{
5691
0
  int r;
5692
0
  struct crypt_keyslot_context kc = {};
5693
5694
0
  crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size);
5695
0
  r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags);
5696
0
  crypt_keyslot_context_destroy_internal(&kc);
5697
5698
0
  return r;
5699
0
}
5700
5701
int crypt_activate_by_keyfile_device_offset(struct crypt_device *cd,
5702
  const char *name,
5703
  int keyslot,
5704
  const char *keyfile,
5705
  size_t keyfile_size,
5706
  uint64_t keyfile_offset,
5707
  uint32_t flags)
5708
0
{
5709
0
  int r;
5710
0
  struct crypt_keyslot_context kc = {};
5711
5712
0
  crypt_keyslot_context_init_by_keyfile_internal(&kc, keyfile, keyfile_size, keyfile_offset);
5713
0
  r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags);
5714
0
  crypt_keyslot_context_destroy_internal(&kc);
5715
5716
0
  return r;
5717
0
}
5718
5719
int crypt_activate_by_keyfile(struct crypt_device *cd,
5720
  const char *name,
5721
  int keyslot,
5722
  const char *keyfile,
5723
  size_t keyfile_size,
5724
  uint32_t flags)
5725
0
{
5726
0
  return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile,
5727
0
          keyfile_size, 0, flags);
5728
0
}
5729
5730
int crypt_activate_by_keyfile_offset(struct crypt_device *cd,
5731
  const char *name,
5732
  int keyslot,
5733
  const char *keyfile,
5734
  size_t keyfile_size,
5735
  size_t keyfile_offset,
5736
  uint32_t flags)
5737
0
{
5738
0
  return crypt_activate_by_keyfile_device_offset(cd, name, keyslot, keyfile,
5739
0
          keyfile_size, keyfile_offset, flags);
5740
0
}
5741
5742
int crypt_activate_by_volume_key(struct crypt_device *cd,
5743
  const char *name,
5744
  const char *volume_key,
5745
  size_t volume_key_size,
5746
  uint32_t flags)
5747
0
{
5748
0
  int r;
5749
0
  struct crypt_keyslot_context kc = {};
5750
5751
0
  crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
5752
0
  r = crypt_activate_by_keyslot_context(cd, name, CRYPT_ANY_SLOT /* unused */, &kc, CRYPT_ANY_SLOT, &kc, flags);
5753
0
  crypt_keyslot_context_destroy_internal(&kc);
5754
5755
0
  return r;
5756
0
}
5757
5758
int crypt_activate_by_signed_key(struct crypt_device *cd,
5759
  const char *name,
5760
  const char *volume_key,
5761
  size_t volume_key_size,
5762
  const char *signature,
5763
  size_t signature_size,
5764
  uint32_t flags)
5765
0
{
5766
0
  int r;
5767
0
  struct crypt_keyslot_context kc = {};
5768
5769
0
  if (!cd || !isVERITY(cd->type))
5770
0
    return -EINVAL;
5771
5772
0
  if (!volume_key || !volume_key_size || (!name && signature)) {
5773
0
    log_err(cd, _("Incorrect root hash specified for verity device."));
5774
0
    return -EINVAL;
5775
0
  }
5776
5777
0
  if (signature)
5778
0
    crypt_keyslot_context_init_by_signed_key_internal(&kc, volume_key, volume_key_size,
5779
0
      signature, signature_size);
5780
0
  else
5781
0
    crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
5782
0
  r = crypt_activate_by_keyslot_context(cd, name, -2 /* unused */, &kc, CRYPT_ANY_SLOT, NULL, flags);
5783
0
  crypt_keyslot_context_destroy_internal(&kc);
5784
5785
0
  return r;
5786
0
}
5787
5788
int crypt_deactivate_by_name(struct crypt_device *cd, const char *name, uint32_t flags)
5789
0
{
5790
0
  struct crypt_device *fake_cd = NULL;
5791
0
  struct luks2_hdr *hdr2 = NULL;
5792
0
  struct crypt_dm_active_device dmd = {};
5793
0
  int r;
5794
0
  uint64_t get_flags = DM_ACTIVE_DEVICE | DM_ACTIVE_UUID | DM_ACTIVE_HOLDERS;
5795
5796
0
  if (!name)
5797
0
    return -EINVAL;
5798
5799
0
  if ((flags & CRYPT_DEACTIVATE_DEFERRED) && (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL))
5800
0
    return -EINVAL;
5801
5802
0
  log_dbg(cd, "Deactivating volume %s.", name);
5803
5804
0
  if (!cd) {
5805
0
    r = crypt_init_by_name(&fake_cd, name);
5806
0
    if (r < 0)
5807
0
      return r;
5808
0
    cd = fake_cd;
5809
0
  }
5810
5811
0
  if (flags & (CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL)) {
5812
0
    r = crypt_get_hw_encryption_type(cd);
5813
0
    if (r == CRYPT_SW_AND_OPAL_HW || r == CRYPT_OPAL_HW_ONLY) {
5814
0
      log_err(cd, _("OPAL does not support deferred deactivation."));
5815
0
      return -EINVAL;
5816
0
    }
5817
0
  }
5818
5819
  /* skip holders detection and early abort when some flags raised */
5820
0
  if (flags & (CRYPT_DEACTIVATE_FORCE | CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL))
5821
0
    get_flags &= ~DM_ACTIVE_HOLDERS;
5822
5823
0
  switch (crypt_status(cd, name)) {
5824
0
    case CRYPT_ACTIVE:
5825
0
    case CRYPT_BUSY:
5826
0
      r = dm_query_device(cd, name, get_flags, &dmd);
5827
0
      if (r >= 0) {
5828
0
        if (dmd.holders) {
5829
0
          log_err(cd, _("Device %s is still in use."), name);
5830
0
          r = -EBUSY;
5831
0
          break;
5832
0
        }
5833
0
      }
5834
5835
      /* For detached header case or missing metadata we need to check for OPAL2 devices
5836
       * from DM UUID */
5837
0
      if (dmd.uuid && (flags & (CRYPT_DEACTIVATE_DEFERRED | CRYPT_DEACTIVATE_DEFERRED_CANCEL)) &&
5838
0
          !strncmp(CRYPT_LUKS2_HW_OPAL, dmd.uuid, sizeof(CRYPT_LUKS2_HW_OPAL)-1)) {
5839
0
        log_err(cd, _("OPAL does not support deferred deactivation."));
5840
0
        r = -EINVAL;
5841
0
        break;
5842
0
      }
5843
5844
0
      if (flags & CRYPT_DEACTIVATE_DEFERRED_CANCEL) {
5845
0
        r = dm_cancel_deferred_removal(name);
5846
0
        if (r < 0)
5847
0
          log_err(cd, _("Could not cancel deferred remove from device %s."), name);
5848
0
        break;
5849
0
      }
5850
5851
0
      hdr2 = crypt_get_hdr(cd, CRYPT_LUKS2);
5852
5853
0
      if ((dmd.uuid && !strncmp(CRYPT_LUKS2, dmd.uuid, sizeof(CRYPT_LUKS2)-1)) || hdr2)
5854
0
        r = LUKS2_deactivate(cd, name, hdr2, &dmd, flags);
5855
0
      else if (isTCRYPT(cd->type))
5856
0
        r = TCRYPT_deactivate(cd, name, flags);
5857
0
      else
5858
0
        r = dm_remove_device(cd, name, flags);
5859
0
      if (r < 0 && crypt_status(cd, name) == CRYPT_BUSY) {
5860
0
        log_err(cd, _("Device %s is still in use."), name);
5861
0
        r = -EBUSY;
5862
0
      }
5863
0
      break;
5864
0
    case CRYPT_INACTIVE:
5865
0
      log_err(cd, _("Device %s is not active."), name);
5866
0
      r = -ENODEV;
5867
0
      break;
5868
0
    default:
5869
0
      log_err(cd, _("Invalid device %s."), name);
5870
0
      r = -EINVAL;
5871
0
  }
5872
5873
0
  dm_targets_free(cd, &dmd);
5874
0
  free(CONST_CAST(void*)dmd.uuid);
5875
0
  crypt_free(fake_cd);
5876
5877
0
  return r;
5878
0
}
5879
5880
int crypt_deactivate(struct crypt_device *cd, const char *name)
5881
0
{
5882
0
  return crypt_deactivate_by_name(cd, name, 0);
5883
0
}
5884
5885
int crypt_get_active_device(struct crypt_device *cd, const char *name,
5886
          struct crypt_active_device *cad)
5887
0
{
5888
0
  int r;
5889
0
  struct crypt_dm_active_device dmd, dmdi = {};
5890
0
  char *iname = NULL;
5891
0
  struct dm_target *tgt = &dmd.segment;
5892
0
  uint64_t min_offset = UINT64_MAX;
5893
5894
0
  if (!cd || !name || !cad)
5895
0
    return -EINVAL;
5896
5897
0
  r = dm_query_device(cd, name, DM_ACTIVE_DEVICE, &dmd);
5898
0
  if (r < 0)
5899
0
    return r;
5900
5901
  /*
5902
   * For integrity and LUKS2 (and detached header where context is NULL)
5903
   * we need flags from underlying dm-integrity device.
5904
   * This check must be skipped for non-LUKS2 integrity device.
5905
   */
5906
0
  if ((isLUKS2(cd->type) || !cd->type) && crypt_get_integrity_tag_size(cd)) {
5907
0
      if ((iname = dm_get_active_iname(cd, name))) {
5908
0
          if (dm_query_device(cd, iname, 0, &dmdi) >= 0)
5909
0
              dmd.flags |= dmdi.flags;
5910
0
          free(iname);
5911
0
      } else
5912
0
          dmd.flags |= (CRYPT_ACTIVATE_NO_JOURNAL | CRYPT_ACTIVATE_INLINE_MODE);
5913
0
  }
5914
5915
0
  if (cd && isTCRYPT(cd->type)) {
5916
0
    cad->offset = TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
5917
0
    cad->iv_offset  = TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
5918
0
  } else {
5919
0
    while (tgt) {
5920
0
      if (tgt->type == DM_CRYPT && (min_offset > tgt->u.crypt.offset)) {
5921
0
        min_offset = tgt->u.crypt.offset;
5922
0
        cad->iv_offset = tgt->u.crypt.iv_offset;
5923
0
      } else if (tgt->type == DM_INTEGRITY && (min_offset > tgt->u.integrity.offset)) {
5924
0
        min_offset = tgt->u.integrity.offset;
5925
0
        cad->iv_offset = 0;
5926
0
      } else if (tgt->type == DM_LINEAR && (min_offset > tgt->u.linear.offset)) {
5927
0
        min_offset = tgt->u.linear.offset;
5928
0
        cad->iv_offset = 0;
5929
0
      }
5930
0
      tgt = tgt->next;
5931
0
    }
5932
0
  }
5933
5934
0
  if (min_offset != UINT64_MAX)
5935
0
    cad->offset = min_offset;
5936
5937
0
  cad->size = dmd.size;
5938
0
  cad->flags  = dmd.flags;
5939
5940
0
  r = 0;
5941
0
  dm_targets_free(cd, &dmd);
5942
0
  dm_targets_free(cd, &dmdi);
5943
5944
0
  return r;
5945
0
}
5946
5947
uint64_t crypt_get_active_integrity_failures(struct crypt_device *cd, const char *name)
5948
0
{
5949
0
  struct crypt_dm_active_device dmd;
5950
0
  uint64_t failures = 0;
5951
5952
0
  if (!name)
5953
0
    return 0;
5954
5955
  /* LUKS2 / dm-crypt does not provide this count. */
5956
0
  if (dm_query_device(cd, name, 0, &dmd) < 0)
5957
0
    return 0;
5958
5959
0
  if (single_segment(&dmd) && dmd.segment.type == DM_INTEGRITY)
5960
0
    (void)dm_status_integrity_failures(cd, name, &failures);
5961
5962
0
  dm_targets_free(cd, &dmd);
5963
5964
0
  return failures;
5965
0
}
5966
5967
/*
5968
 * Volume key handling
5969
 */
5970
int crypt_volume_key_get(struct crypt_device *cd,
5971
  int keyslot,
5972
  char *volume_key,
5973
  size_t *volume_key_size,
5974
  const char *passphrase,
5975
  size_t passphrase_size)
5976
0
{
5977
0
  int r;
5978
0
  struct crypt_keyslot_context kc = {};
5979
5980
0
  if (!passphrase)
5981
0
    return crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, NULL);
5982
5983
0
  crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size);
5984
5985
0
  r = crypt_volume_key_get_by_keyslot_context(cd, keyslot, volume_key, volume_key_size, &kc);
5986
5987
0
  crypt_keyslot_context_destroy_internal(&kc);
5988
5989
0
  return r;
5990
0
}
5991
5992
int crypt_volume_key_get_by_keyslot_context(struct crypt_device *cd,
5993
  int keyslot,
5994
  char *volume_key,
5995
  size_t *volume_key_size,
5996
  struct crypt_keyslot_context *kc)
5997
0
{
5998
0
  size_t passphrase_size;
5999
0
  int key_len, r;
6000
0
  const char *passphrase = NULL;
6001
0
  struct volume_key *vk = NULL;
6002
6003
0
  if (!cd || !volume_key || !volume_key_size ||
6004
0
      (!kc && !isLUKS(cd->type) && !isTCRYPT(cd->type) && !isVERITY(cd->type) && !isBITLK(cd->type)))
6005
0
    return -EINVAL;
6006
6007
0
  if (isLUKS2(cd->type) && keyslot != CRYPT_ANY_SLOT)
6008
0
    key_len = LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot);
6009
0
  else
6010
0
    key_len = crypt_get_volume_key_size(cd);
6011
6012
0
  if (key_len < 0)
6013
0
    return -EINVAL;
6014
6015
0
  if (key_len > (int)*volume_key_size) {
6016
0
    log_err(cd, _("Volume key buffer too small."));
6017
0
    return -ENOMEM;
6018
0
  }
6019
6020
0
  if (kc && (!kc->get_passphrase || kc->type == CRYPT_KC_TYPE_KEY))
6021
0
    return -EINVAL;
6022
6023
0
  r = -EINVAL;
6024
6025
0
  if (isLUKS2(cd->type)) {
6026
0
    if (kc && !kc->get_luks2_key)
6027
0
      log_err(cd, _("Cannot retrieve volume key for LUKS2 device."));
6028
0
    else if (!kc)
6029
0
      r = -ENOENT;
6030
0
    else
6031
0
      r = kc->get_luks2_key(cd, kc, keyslot,
6032
0
          keyslot == CRYPT_ANY_SLOT ? CRYPT_DEFAULT_SEGMENT : CRYPT_ANY_SEGMENT,
6033
0
          &vk);
6034
0
  } else if (isLUKS1(cd->type)) {
6035
0
    if (kc && !kc->get_luks1_volume_key)
6036
0
      log_err(cd, _("Cannot retrieve volume key for LUKS1 device."));
6037
0
    else if (!kc)
6038
0
      r = -ENOENT;
6039
0
    else
6040
0
      r = kc->get_luks1_volume_key(cd, kc, keyslot, &vk);
6041
0
  } else if (isPLAIN(cd->type) && cd->u.plain.hdr.hash) {
6042
0
    if (kc && kc->get_passphrase && kc->type != CRYPT_KC_TYPE_TOKEN) {
6043
0
      r = kc->get_passphrase(cd, kc, &passphrase, &passphrase_size);
6044
0
      if (r < 0)
6045
0
        return r;
6046
0
      r = process_key(cd, cd->u.plain.hdr.hash, key_len,
6047
0
          passphrase, passphrase_size, &vk);
6048
0
    }
6049
0
    if (r < 0)
6050
0
      log_err(cd, _("Cannot retrieve volume key for plain device."));
6051
0
  } else if (isVERITY(cd->type)) {
6052
    /* volume_key == root hash */
6053
0
    if (cd->u.verity.root_hash) {
6054
0
      crypt_safe_memcpy(volume_key, cd->u.verity.root_hash, cd->u.verity.root_hash_size);
6055
0
      *volume_key_size = cd->u.verity.root_hash_size;
6056
0
      r = 0;
6057
0
    } else
6058
0
      log_err(cd, _("Cannot retrieve root hash for verity device."));
6059
0
  } else if (isTCRYPT(cd->type)) {
6060
0
    r = TCRYPT_get_volume_key(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params, &vk);
6061
0
  } else if (isBITLK(cd->type)) {
6062
0
    if (kc && kc->get_bitlk_volume_key)
6063
0
      r = kc->get_bitlk_volume_key(cd, kc, &cd->u.bitlk.params, &vk);
6064
0
    else if (!kc)
6065
0
      r = BITLK_get_volume_key(cd, NULL, 0, &cd->u.bitlk.params, &vk);
6066
0
    if (r < 0)
6067
0
      log_err(cd, _("Cannot retrieve volume key for BITLK device."));
6068
0
  } else if (isFVAULT2(cd->type)) {
6069
0
    if (kc && kc->get_fvault2_volume_key)
6070
0
      r = kc->get_fvault2_volume_key(cd, kc, &cd->u.fvault2.params, &vk);
6071
0
    if (r < 0)
6072
0
      log_err(cd, _("Cannot retrieve volume key for FVAULT2 device."));
6073
0
  } else
6074
0
    log_err(cd, _("This operation is not supported for %s crypt device."), cd->type ?: "(none)");
6075
6076
0
  if (r == -ENOENT && isLUKS(cd->type) && cd->volume_key) {
6077
0
    vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key),
6078
0
              crypt_volume_key_get_key(cd->volume_key));
6079
0
    r = vk ? 0 : -ENOMEM;
6080
0
  }
6081
6082
0
  if (r >= 0 && vk) {
6083
0
    crypt_safe_memcpy(volume_key, crypt_volume_key_get_key(vk), crypt_volume_key_length(vk));
6084
0
    *volume_key_size = crypt_volume_key_length(vk);
6085
0
  }
6086
6087
0
  crypt_free_volume_key(vk);
6088
0
  return r;
6089
0
}
6090
6091
int crypt_volume_key_verify(struct crypt_device *cd,
6092
  const char *volume_key,
6093
  size_t volume_key_size)
6094
0
{
6095
0
  struct volume_key *vk;
6096
0
  int r;
6097
6098
0
  if ((r = onlyLUKSunrestricted(cd)))
6099
0
    return r;
6100
6101
0
  vk = crypt_alloc_volume_key(volume_key_size, volume_key);
6102
0
  if (!vk)
6103
0
    return -ENOMEM;
6104
6105
0
  if (isLUKS1(cd->type))
6106
0
    r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
6107
0
  else if (isLUKS2(cd->type))
6108
0
    r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
6109
0
  else
6110
0
    r = -EINVAL;
6111
6112
0
  crypt_free_volume_key(vk);
6113
6114
0
  return r >= 0 ? 0 : r;
6115
0
}
6116
6117
/*
6118
 * RNG and memory locking
6119
 */
6120
void crypt_set_rng_type(struct crypt_device *cd, int rng_type)
6121
0
{
6122
0
  if (!cd)
6123
0
    return;
6124
6125
0
  switch (rng_type) {
6126
0
  case CRYPT_RNG_URANDOM:
6127
0
  case CRYPT_RNG_RANDOM:
6128
0
    log_dbg(cd, "RNG set to %d (%s).", rng_type, rng_type ? "random" : "urandom");
6129
0
    cd->rng_type = rng_type;
6130
0
  }
6131
0
}
6132
6133
int crypt_get_rng_type(struct crypt_device *cd)
6134
0
{
6135
0
  if (!cd)
6136
0
    return -EINVAL;
6137
6138
0
  return cd->rng_type;
6139
0
}
6140
6141
int crypt_memory_lock(struct crypt_device *cd, int lock)
6142
0
{
6143
0
  UNUSED(cd);
6144
0
  UNUSED(lock);
6145
6146
0
  return 0;
6147
0
}
6148
6149
void crypt_set_compatibility(struct crypt_device *cd, uint32_t flags)
6150
0
{
6151
0
  if (cd)
6152
0
    cd->compatibility = flags;
6153
0
}
6154
6155
uint32_t crypt_get_compatibility(struct crypt_device *cd)
6156
0
{
6157
0
  if (cd)
6158
0
    return cd->compatibility;
6159
6160
0
  return 0;
6161
0
}
6162
6163
/*
6164
 * Reporting
6165
 */
6166
crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
6167
0
{
6168
0
  int r;
6169
6170
0
  if (!name)
6171
0
    return CRYPT_INVALID;
6172
6173
0
  if (!cd)
6174
0
    dm_backend_init(cd);
6175
6176
0
  r = dm_status_device(cd, name);
6177
6178
0
  if (!cd)
6179
0
    dm_backend_exit(cd);
6180
6181
0
  if (r < 0 && r != -ENODEV)
6182
0
    return CRYPT_INVALID;
6183
6184
0
  if (r == 0)
6185
0
    return CRYPT_ACTIVE;
6186
6187
0
  if (r > 0)
6188
0
    return CRYPT_BUSY;
6189
6190
0
  return CRYPT_INACTIVE;
6191
0
}
6192
6193
static int _luks_dump(struct crypt_device *cd)
6194
0
{
6195
0
  int i;
6196
6197
0
  log_std(cd, "LUKS header information for %s\n\n", mdata_device_path(cd));
6198
0
  log_std(cd, "Version:       \t%" PRIu16 "\n", cd->u.luks1.hdr.version);
6199
0
  log_std(cd, "Cipher name:   \t%s\n", cd->u.luks1.hdr.cipherName);
6200
0
  log_std(cd, "Cipher mode:   \t%s\n", cd->u.luks1.hdr.cipherMode);
6201
0
  log_std(cd, "Hash spec:     \t%s\n", cd->u.luks1.hdr.hashSpec);
6202
0
  log_std(cd, "Payload offset:\t%" PRIu32 "\n", cd->u.luks1.hdr.payloadOffset);
6203
0
  log_std(cd, "MK bits:       \t%" PRIu32 "\n", cd->u.luks1.hdr.keyBytes * 8);
6204
0
  log_std(cd, "MK digest:     \t");
6205
0
  crypt_log_hex(cd, cd->u.luks1.hdr.mkDigest, LUKS_DIGESTSIZE, " ", 0, NULL);
6206
0
  log_std(cd, "\n");
6207
0
  log_std(cd, "MK salt:       \t");
6208
0
  crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt, LUKS_SALTSIZE/2, " ", 0, NULL);
6209
0
  log_std(cd, "\n               \t");
6210
0
  crypt_log_hex(cd, cd->u.luks1.hdr.mkDigestSalt+LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL);
6211
0
  log_std(cd, "\n");
6212
0
  log_std(cd, "MK iterations: \t%" PRIu32 "\n", cd->u.luks1.hdr.mkDigestIterations);
6213
0
  log_std(cd, "UUID:          \t%s\n\n", cd->u.luks1.hdr.uuid);
6214
0
  for(i = 0; i < LUKS_NUMKEYS; i++) {
6215
0
    if(cd->u.luks1.hdr.keyblock[i].active == LUKS_KEY_ENABLED) {
6216
0
      log_std(cd, "Key Slot %d: ENABLED\n",i);
6217
0
      log_std(cd, "\tIterations:         \t%" PRIu32 "\n",
6218
0
        cd->u.luks1.hdr.keyblock[i].passwordIterations);
6219
0
      log_std(cd, "\tSalt:               \t");
6220
0
      crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt,
6221
0
         LUKS_SALTSIZE/2, " ", 0, NULL);
6222
0
      log_std(cd, "\n\t                      \t");
6223
0
      crypt_log_hex(cd, cd->u.luks1.hdr.keyblock[i].passwordSalt +
6224
0
         LUKS_SALTSIZE/2, LUKS_SALTSIZE/2, " ", 0, NULL);
6225
0
      log_std(cd, "\n");
6226
6227
0
      log_std(cd, "\tKey material offset:\t%" PRIu32 "\n",
6228
0
        cd->u.luks1.hdr.keyblock[i].keyMaterialOffset);
6229
0
      log_std(cd, "\tAF stripes:            \t%" PRIu32 "\n",
6230
0
        cd->u.luks1.hdr.keyblock[i].stripes);
6231
0
    }
6232
0
    else
6233
0
      log_std(cd, "Key Slot %d: DISABLED\n", i);
6234
0
  }
6235
0
  return 0;
6236
0
}
6237
6238
int crypt_dump(struct crypt_device *cd)
6239
0
{
6240
0
  if (!cd)
6241
0
    return -EINVAL;
6242
0
  if (isLUKS1(cd->type))
6243
0
    return _luks_dump(cd);
6244
0
  else if (isLUKS2(cd->type))
6245
0
    return LUKS2_hdr_dump(cd, &cd->u.luks2.hdr);
6246
0
  else if (isVERITY(cd->type))
6247
0
    return VERITY_dump(cd, &cd->u.verity.hdr,
6248
0
           cd->u.verity.root_hash, cd->u.verity.root_hash_size,
6249
0
           cd->u.verity.fec_device);
6250
0
  else if (isTCRYPT(cd->type))
6251
0
    return TCRYPT_dump(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
6252
0
  else if (isINTEGRITY(cd->type))
6253
0
    return INTEGRITY_dump(cd, crypt_data_device(cd), 0);
6254
0
  else if (isBITLK(cd->type))
6255
0
    return BITLK_dump(cd, crypt_data_device(cd), &cd->u.bitlk.params);
6256
0
  else if (isFVAULT2(cd->type))
6257
0
    return FVAULT2_dump(cd, crypt_data_device(cd), &cd->u.fvault2.params);
6258
6259
0
  log_err(cd, _("Dump operation is not supported for this device type."));
6260
0
  return -EINVAL;
6261
0
}
6262
6263
int crypt_dump_json(struct crypt_device *cd, const char **json, uint32_t flags)
6264
0
{
6265
0
  if (!cd || flags)
6266
0
    return -EINVAL;
6267
0
  if (isLUKS2(cd->type))
6268
0
    return LUKS2_hdr_dump_json(cd, &cd->u.luks2.hdr, json);
6269
6270
0
  log_err(cd, _("Dump operation is not supported for this device type."));
6271
0
  return -EINVAL;
6272
0
}
6273
6274
/* internal only */
6275
const char *crypt_get_cipher_spec(struct crypt_device *cd)
6276
0
{
6277
0
  if (!cd)
6278
0
    return NULL;
6279
0
  else if (isLUKS2(cd->type))
6280
0
    return LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6281
0
  else if (isLUKS1(cd->type))
6282
0
    return cd->u.luks1.cipher_spec;
6283
0
  else if (isPLAIN(cd->type))
6284
0
    return cd->u.plain.cipher_spec;
6285
0
  else if (isLOOPAES(cd->type))
6286
0
    return cd->u.loopaes.cipher_spec;
6287
0
  else if (isBITLK(cd->type))
6288
0
    return cd->u.bitlk.cipher_spec;
6289
0
  else if (!cd->type && !_init_by_name_crypt_none(cd))
6290
0
    return cd->u.none.cipher_spec;
6291
6292
0
  return NULL;
6293
0
}
6294
6295
const char *crypt_get_cipher(struct crypt_device *cd)
6296
0
{
6297
0
  if (!cd)
6298
0
    return NULL;
6299
6300
0
  if (isPLAIN(cd->type))
6301
0
    return cd->u.plain.cipher;
6302
6303
0
  if (isLUKS1(cd->type))
6304
0
    return cd->u.luks1.hdr.cipherName;
6305
6306
0
  if (isLUKS2(cd->type)) {
6307
0
    if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT),
6308
0
                cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode))
6309
0
      return NULL;
6310
0
    return cd->u.luks2.cipher;
6311
0
  }
6312
6313
0
  if (isLOOPAES(cd->type))
6314
0
    return cd->u.loopaes.cipher;
6315
6316
0
  if (isTCRYPT(cd->type))
6317
0
    return cd->u.tcrypt.params.cipher;
6318
6319
0
  if (isBITLK(cd->type))
6320
0
    return cd->u.bitlk.params.cipher;
6321
6322
0
  if (isFVAULT2(cd->type))
6323
0
    return cd->u.fvault2.params.cipher;
6324
6325
0
  if (!cd->type && !_init_by_name_crypt_none(cd))
6326
0
    return cd->u.none.cipher;
6327
6328
0
  return NULL;
6329
0
}
6330
6331
const char *crypt_get_cipher_mode(struct crypt_device *cd)
6332
0
{
6333
0
  if (!cd)
6334
0
    return NULL;
6335
6336
0
  if (isPLAIN(cd->type))
6337
0
    return cd->u.plain.cipher_mode;
6338
6339
0
  if (isLUKS1(cd->type))
6340
0
    return cd->u.luks1.hdr.cipherMode;
6341
6342
0
  if (isLUKS2(cd->type)) {
6343
0
    if (crypt_parse_name_and_mode(LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT),
6344
0
                cd->u.luks2.cipher, NULL, cd->u.luks2.cipher_mode))
6345
0
      return NULL;
6346
0
    return cd->u.luks2.cipher_mode;
6347
0
  }
6348
6349
0
  if (isLOOPAES(cd->type))
6350
0
    return cd->u.loopaes.cipher_mode;
6351
6352
0
  if (isTCRYPT(cd->type))
6353
0
    return cd->u.tcrypt.params.mode;
6354
6355
0
  if (isBITLK(cd->type))
6356
0
    return cd->u.bitlk.params.cipher_mode;
6357
6358
0
  if (isFVAULT2(cd->type))
6359
0
    return cd->u.fvault2.params.cipher_mode;
6360
6361
0
  if (!cd->type && !_init_by_name_crypt_none(cd))
6362
0
    return cd->u.none.cipher_mode;
6363
6364
0
  return NULL;
6365
0
}
6366
6367
/* INTERNAL only */
6368
const char *crypt_get_integrity(struct crypt_device *cd)
6369
0
{
6370
0
  if (!cd)
6371
0
    return NULL;
6372
6373
0
  if (isINTEGRITY(cd->type))
6374
0
    return cd->u.integrity.params.integrity;
6375
6376
0
  if (isLUKS2(cd->type))
6377
0
    return LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6378
6379
0
  if (!cd->type && *cd->u.none.integrity_spec)
6380
0
    return cd->u.none.integrity_spec;
6381
6382
0
  return NULL;
6383
0
}
6384
6385
/* INTERNAL only */
6386
int crypt_get_integrity_key_size(struct crypt_device *cd, bool dm_compat)
6387
0
{
6388
0
  int key_size = 0;
6389
6390
0
  if (isLUKS2(cd->type)) {
6391
0
    key_size = INTEGRITY_key_size(crypt_get_integrity(cd),
6392
0
                LUKS2_get_integrity_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT));
6393
0
    if (dm_compat && key_size > 0 &&
6394
0
        key_size == INTEGRITY_key_size(crypt_get_integrity(cd), 0))
6395
0
      return 0;
6396
0
  }
6397
6398
0
  if (isINTEGRITY(cd->type) || !cd->type)
6399
0
    key_size = INTEGRITY_key_size(crypt_get_integrity(cd),  0);
6400
6401
0
  return key_size > 0 ? key_size : 0;
6402
0
}
6403
6404
/* INTERNAL only */
6405
int crypt_get_integrity_tag_size(struct crypt_device *cd)
6406
0
{
6407
0
  if (isINTEGRITY(cd->type))
6408
0
    return cd->u.integrity.params.tag_size;
6409
6410
0
  if (isLUKS2(cd->type) || !cd->type)
6411
0
    return INTEGRITY_tag_size(crypt_get_integrity(cd),
6412
0
            crypt_get_cipher(cd),
6413
0
            crypt_get_cipher_mode(cd));
6414
0
  return 0;
6415
0
}
6416
6417
int crypt_get_sector_size(struct crypt_device *cd)
6418
0
{
6419
0
  if (!cd)
6420
0
    return SECTOR_SIZE;
6421
6422
0
  if (isPLAIN(cd->type))
6423
0
    return cd->u.plain.hdr.sector_size;
6424
6425
0
  if (isINTEGRITY(cd->type))
6426
0
    return cd->u.integrity.params.sector_size;
6427
6428
0
  if (isLUKS2(cd->type))
6429
0
    return LUKS2_get_sector_size(&cd->u.luks2.hdr);
6430
6431
0
  if (!cd->type && cd->u.none.sector_size)
6432
0
    return cd->u.none.sector_size;
6433
6434
0
  return SECTOR_SIZE;
6435
0
}
6436
6437
const char *crypt_get_uuid(struct crypt_device *cd)
6438
0
{
6439
0
  if (!cd)
6440
0
    return NULL;
6441
6442
0
  if (isLUKS1(cd->type))
6443
0
    return cd->u.luks1.hdr.uuid;
6444
6445
0
  if (isLUKS2(cd->type))
6446
0
    return cd->u.luks2.hdr.uuid;
6447
6448
0
  if (isVERITY(cd->type))
6449
0
    return cd->u.verity.uuid;
6450
6451
0
  if (isBITLK(cd->type))
6452
0
    return cd->u.bitlk.params.guid;
6453
6454
0
  if (isFVAULT2(cd->type))
6455
0
    return cd->u.fvault2.params.family_uuid;
6456
6457
0
  return NULL;
6458
0
}
6459
6460
const char *crypt_get_device_name(struct crypt_device *cd)
6461
0
{
6462
0
  const char *path;
6463
6464
0
  if (!cd)
6465
0
    return NULL;
6466
6467
0
  path = device_block_path(cd->device);
6468
0
  if (!path)
6469
0
    path = device_path(cd->device);
6470
6471
0
  return path;
6472
0
}
6473
6474
const char *crypt_get_metadata_device_name(struct crypt_device *cd)
6475
0
{
6476
0
  const char *path;
6477
6478
0
  if (!cd || !cd->metadata_device)
6479
0
    return NULL;
6480
6481
0
  path = device_block_path(cd->metadata_device);
6482
0
  if (!path)
6483
0
    path = device_path(cd->metadata_device);
6484
6485
0
  return path;
6486
0
}
6487
6488
int crypt_get_volume_key_size(struct crypt_device *cd)
6489
0
{
6490
0
  int r;
6491
6492
0
  if (!cd)
6493
0
    return 0;
6494
6495
0
  if (isPLAIN(cd->type))
6496
0
    return cd->u.plain.key_size;
6497
6498
0
  if (isLUKS1(cd->type))
6499
0
    return cd->u.luks1.hdr.keyBytes;
6500
6501
0
  if (isLUKS2(cd->type)) {
6502
0
    r = LUKS2_get_volume_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6503
0
    if (r < 0 && cd->volume_key)
6504
0
      r = crypt_volume_key_length(cd->volume_key);
6505
0
    return r < 0 ? 0 : r;
6506
0
  }
6507
6508
0
  if (isLOOPAES(cd->type))
6509
0
    return cd->u.loopaes.key_size;
6510
6511
0
  if (isVERITY(cd->type))
6512
0
    return cd->u.verity.root_hash_size;
6513
6514
0
  if (isTCRYPT(cd->type))
6515
0
    return cd->u.tcrypt.params.key_size;
6516
6517
0
  if (isBITLK(cd->type))
6518
0
    return cd->u.bitlk.params.key_size / 8;
6519
6520
0
  if (isFVAULT2(cd->type))
6521
0
    return cd->u.fvault2.params.key_size;
6522
6523
0
  if (!cd->type && !_init_by_name_crypt_none(cd))
6524
0
    return cd->u.none.key_size;
6525
6526
0
  return 0;
6527
0
}
6528
6529
int crypt_get_old_volume_key_size(struct crypt_device *cd)
6530
0
{
6531
0
  int r = _onlyLUKS2(cd, CRYPT_CD_QUIET,
6532
0
         CRYPT_REQUIREMENT_ONLINE_REENCRYPT | CRYPT_REQUIREMENT_OPAL);
6533
6534
0
  if (r < 0)
6535
0
    return 0;
6536
6537
0
  r = LUKS2_get_old_volume_key_size(&cd->u.luks2.hdr);
6538
6539
0
  return r < 0 ? 0 : r;
6540
0
}
6541
6542
int crypt_get_hw_encryption_key_size(struct crypt_device *cd)
6543
0
{
6544
0
  if (!cd || !isLUKS2(cd->type))
6545
0
    return 0;
6546
6547
0
  return LUKS2_get_opal_key_size(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6548
0
}
6549
6550
int crypt_keyslot_get_key_size(struct crypt_device *cd, int keyslot)
6551
0
{
6552
0
  if (!cd || !isLUKS(cd->type))
6553
0
    return -EINVAL;
6554
6555
0
  if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
6556
0
    return -EINVAL;
6557
6558
0
  if (isLUKS1(cd->type))
6559
0
    return cd->u.luks1.hdr.keyBytes;
6560
6561
0
  if (isLUKS2(cd->type))
6562
0
    return LUKS2_get_keyslot_stored_key_size(&cd->u.luks2.hdr, keyslot);
6563
6564
0
  return -EINVAL;
6565
0
}
6566
6567
int crypt_keyslot_set_encryption(struct crypt_device *cd,
6568
  const char *cipher,
6569
  size_t key_size)
6570
0
{
6571
0
  char *tmp;
6572
6573
0
  if (!cd || !cipher || !key_size || !isLUKS2(cd->type))
6574
0
    return -EINVAL;
6575
6576
0
  if (LUKS2_keyslot_cipher_incompatible(cd, cipher))
6577
0
    return -EINVAL;
6578
6579
0
  if (!(tmp = strdup(cipher)))
6580
0
    return -ENOMEM;
6581
6582
0
  free(cd->u.luks2.keyslot_cipher);
6583
0
  cd->u.luks2.keyslot_cipher = tmp;
6584
0
  cd->u.luks2.keyslot_key_size = key_size;
6585
6586
0
  return 0;
6587
0
}
6588
6589
const char *crypt_keyslot_get_encryption(struct crypt_device *cd, int keyslot, size_t *key_size)
6590
0
{
6591
0
  const char *cipher;
6592
6593
0
  if (!cd || !isLUKS(cd->type) || !key_size)
6594
0
    return NULL;
6595
6596
0
  if (isLUKS1(cd->type)) {
6597
0
    if (keyslot != CRYPT_ANY_SLOT &&
6598
0
        LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot) < CRYPT_SLOT_ACTIVE)
6599
0
      return NULL;
6600
0
    *key_size = crypt_get_volume_key_size(cd);
6601
0
    return cd->u.luks1.cipher_spec;
6602
0
  }
6603
6604
0
  if (keyslot != CRYPT_ANY_SLOT)
6605
0
    return LUKS2_get_keyslot_cipher(&cd->u.luks2.hdr, keyslot, key_size);
6606
6607
  /* Keyslot encryption was set through crypt_keyslot_set_encryption() */
6608
0
  if (cd->u.luks2.keyslot_cipher) {
6609
0
    *key_size = cd->u.luks2.keyslot_key_size;
6610
0
    return cd->u.luks2.keyslot_cipher;
6611
0
  }
6612
6613
0
  if (LUKS2_segment_is_hw_opal(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT)) {
6614
    /* Fallback to default LUKS2 keyslot encryption */
6615
0
    *key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8;
6616
0
    return DEFAULT_LUKS2_KEYSLOT_CIPHER;
6617
0
  }
6618
6619
  /* Try to reuse volume encryption parameters */
6620
0
  cipher =  LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6621
0
  if (!LUKS2_keyslot_cipher_incompatible(cd, cipher)) {
6622
0
    *key_size = crypt_get_volume_key_size(cd);
6623
0
    if (*key_size)
6624
0
      return cipher;
6625
0
  }
6626
6627
  /* Fallback to default LUKS2 keyslot encryption */
6628
0
  *key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8;
6629
0
  return DEFAULT_LUKS2_KEYSLOT_CIPHER;
6630
0
}
6631
6632
int crypt_keyslot_get_pbkdf(struct crypt_device *cd, int keyslot, struct crypt_pbkdf_type *pbkdf)
6633
0
{
6634
0
  if (!cd || !pbkdf || keyslot == CRYPT_ANY_SLOT)
6635
0
    return -EINVAL;
6636
6637
0
  if (isLUKS1(cd->type))
6638
0
    return LUKS_keyslot_pbkdf(&cd->u.luks1.hdr, keyslot, pbkdf);
6639
0
  else if (isLUKS2(cd->type))
6640
0
    return LUKS2_keyslot_pbkdf(&cd->u.luks2.hdr, keyslot, pbkdf);
6641
6642
0
  return -EINVAL;
6643
0
}
6644
6645
int crypt_set_data_offset(struct crypt_device *cd, uint64_t data_offset)
6646
0
{
6647
0
  if (!cd)
6648
0
    return -EINVAL;
6649
0
  if (data_offset % (MAX_SECTOR_SIZE >> SECTOR_SHIFT)) {
6650
0
    log_err(cd, _("Data offset is not multiple of %u bytes."), MAX_SECTOR_SIZE);
6651
0
    return -EINVAL;
6652
0
  }
6653
6654
0
  cd->data_offset = data_offset;
6655
0
  log_dbg(cd, "Data offset set to %" PRIu64 " (512-byte) sectors.", data_offset);
6656
6657
0
  return 0;
6658
0
}
6659
6660
int crypt_set_metadata_size(struct crypt_device *cd,
6661
  uint64_t metadata_size,
6662
  uint64_t keyslots_size)
6663
0
{
6664
0
  if (!cd)
6665
0
    return -EINVAL;
6666
6667
0
  if (cd->type && !isLUKS2(cd->type))
6668
0
    return -EINVAL;
6669
6670
0
  if (metadata_size && LUKS2_check_metadata_area_size(metadata_size))
6671
0
    return -EINVAL;
6672
6673
0
  if (keyslots_size && LUKS2_check_keyslots_area_size(keyslots_size))
6674
0
    return -EINVAL;
6675
6676
0
  cd->metadata_size = metadata_size;
6677
0
  cd->keyslots_size = keyslots_size;
6678
6679
0
  return 0;
6680
0
}
6681
6682
int crypt_get_metadata_size(struct crypt_device *cd,
6683
  uint64_t *metadata_size,
6684
  uint64_t *keyslots_size)
6685
0
{
6686
0
  uint64_t msize, ksize;
6687
6688
0
  if (!cd)
6689
0
    return -EINVAL;
6690
6691
0
  if (!cd->type) {
6692
0
    msize = cd->metadata_size;
6693
0
    ksize = cd->keyslots_size;
6694
0
  } else if (isLUKS1(cd->type)) {
6695
0
    msize = LUKS_ALIGN_KEYSLOTS;
6696
0
    ksize = LUKS_device_sectors(&cd->u.luks1.hdr) * SECTOR_SIZE - msize;
6697
0
  } else if (isLUKS2(cd->type)) {
6698
0
    msize = LUKS2_metadata_size(&cd->u.luks2.hdr);
6699
0
    ksize = LUKS2_keyslots_size(&cd->u.luks2.hdr);
6700
0
  } else
6701
0
    return -EINVAL;
6702
6703
0
  if (metadata_size)
6704
0
    *metadata_size = msize;
6705
0
  if (keyslots_size)
6706
0
    *keyslots_size = ksize;
6707
6708
0
  return 0;
6709
0
}
6710
6711
uint64_t crypt_get_data_offset(struct crypt_device *cd)
6712
0
{
6713
0
  if (!cd)
6714
0
    return 0;
6715
6716
0
  if (isPLAIN(cd->type))
6717
0
    return cd->u.plain.hdr.offset;
6718
6719
0
  if (isLUKS1(cd->type))
6720
0
    return cd->u.luks1.hdr.payloadOffset;
6721
6722
0
  if (isLUKS2(cd->type))
6723
0
    return LUKS2_get_data_offset(&cd->u.luks2.hdr);
6724
6725
0
  if (isLOOPAES(cd->type))
6726
0
    return cd->u.loopaes.hdr.offset;
6727
6728
0
  if (isTCRYPT(cd->type))
6729
0
    return TCRYPT_get_data_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
6730
6731
0
  if (isBITLK(cd->type))
6732
0
    return cd->u.bitlk.params.volume_header_size / SECTOR_SIZE;
6733
6734
0
  if (isFVAULT2(cd->type))
6735
0
    return cd->u.fvault2.params.log_vol_off / SECTOR_SIZE;
6736
6737
0
  return cd->data_offset;
6738
0
}
6739
6740
uint64_t crypt_get_iv_offset(struct crypt_device *cd)
6741
0
{
6742
0
  if (!cd)
6743
0
    return 0;
6744
6745
0
  if (isPLAIN(cd->type))
6746
0
    return cd->u.plain.hdr.skip;
6747
6748
0
  if (isLOOPAES(cd->type))
6749
0
    return cd->u.loopaes.hdr.skip;
6750
6751
0
  if (isTCRYPT(cd->type))
6752
0
    return TCRYPT_get_iv_offset(cd, &cd->u.tcrypt.hdr, &cd->u.tcrypt.params);
6753
6754
0
  return 0;
6755
0
}
6756
6757
crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot)
6758
0
{
6759
0
  if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0) < 0)
6760
0
    return CRYPT_SLOT_INVALID;
6761
6762
0
  if (isLUKS1(cd->type))
6763
0
    return LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot);
6764
0
  else if(isLUKS2(cd->type))
6765
0
    return LUKS2_keyslot_info(&cd->u.luks2.hdr, keyslot);
6766
6767
0
  return CRYPT_SLOT_INVALID;
6768
0
}
6769
6770
int crypt_keyslot_max(const char *type)
6771
0
{
6772
0
  if (isLUKS1(type))
6773
0
    return LUKS_NUMKEYS;
6774
6775
0
  if (isLUKS2(type))
6776
0
    return LUKS2_KEYSLOTS_MAX;
6777
6778
0
  return -EINVAL;
6779
0
}
6780
6781
int crypt_keyslot_area(struct crypt_device *cd,
6782
  int keyslot,
6783
  uint64_t *offset,
6784
  uint64_t *length)
6785
0
{
6786
0
  if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0) || !offset || !length)
6787
0
    return -EINVAL;
6788
6789
0
  if (isLUKS2(cd->type))
6790
0
    return LUKS2_keyslot_area(&cd->u.luks2.hdr, keyslot, offset, length);
6791
6792
0
  return LUKS_keyslot_area(&cd->u.luks1.hdr, keyslot, offset, length);
6793
0
}
6794
6795
crypt_keyslot_priority crypt_keyslot_get_priority(struct crypt_device *cd, int keyslot)
6796
0
{
6797
0
  if (_onlyLUKS(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
6798
0
    return CRYPT_SLOT_PRIORITY_INVALID;
6799
6800
0
  if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
6801
0
    return CRYPT_SLOT_PRIORITY_INVALID;
6802
6803
0
  if (isLUKS2(cd->type))
6804
0
    return LUKS2_keyslot_priority_get(&cd->u.luks2.hdr, keyslot);
6805
6806
0
  return CRYPT_SLOT_PRIORITY_NORMAL;
6807
0
}
6808
6809
int crypt_keyslot_set_priority(struct crypt_device *cd, int keyslot, crypt_keyslot_priority priority)
6810
0
{
6811
0
  int r;
6812
6813
0
  log_dbg(cd, "Setting keyslot %d to priority %d.", keyslot, priority);
6814
6815
0
  if (priority == CRYPT_SLOT_PRIORITY_INVALID)
6816
0
    return -EINVAL;
6817
6818
0
  if (keyslot < 0 || keyslot >= crypt_keyslot_max(cd->type))
6819
0
    return -EINVAL;
6820
6821
0
  if ((r = onlyLUKS2(cd)))
6822
0
    return r;
6823
6824
0
  return LUKS2_keyslot_priority_set(cd, &cd->u.luks2.hdr, keyslot, priority, 1);
6825
0
}
6826
6827
const char *crypt_get_type(struct crypt_device *cd)
6828
3.36k
{
6829
3.36k
  return cd ? cd->type : NULL;
6830
3.36k
}
6831
6832
const char *crypt_get_default_type(void)
6833
0
{
6834
0
  return DEFAULT_LUKS_FORMAT;
6835
0
}
6836
6837
int crypt_get_type_defaults(const char *type, struct crypt_type_defaults *defaults)
6838
0
{
6839
0
  if (!type || !isLUKS(type) || !defaults)
6840
0
    return -EINVAL;
6841
6842
0
  memset(defaults, 0, sizeof(*defaults));
6843
6844
0
  defaults->cipher = DEFAULT_LUKS1_CIPHER;
6845
0
  defaults->cipher_mode = DEFAULT_LUKS1_MODE;
6846
0
  defaults->hash = DEFAULT_LUKS1_HASH;
6847
0
  defaults->key_size = DEFAULT_LUKS1_KEYBITS;
6848
6849
0
  if (isLUKS2(type)) {
6850
0
    defaults->integrity = "hmac-sha256";
6851
0
    defaults->tag_size = 32;
6852
0
  }
6853
0
  return 0;
6854
0
}
6855
6856
int crypt_get_hw_encryption_type(struct crypt_device *cd)
6857
0
{
6858
0
  if (!cd)
6859
0
    return -EINVAL;
6860
6861
0
  if (isLUKS2(cd->type)) {
6862
0
    if (LUKS2_segment_is_hw_opal_crypt(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT))
6863
0
      return CRYPT_SW_AND_OPAL_HW;
6864
0
    else if (LUKS2_segment_is_hw_opal_only(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT))
6865
0
      return CRYPT_OPAL_HW_ONLY;
6866
0
  }
6867
6868
0
  return CRYPT_SW_ONLY;
6869
0
}
6870
6871
int crypt_get_hw_opal_sum_enabled(struct crypt_device* cd)
6872
0
{
6873
0
  uint8_t version;
6874
6875
0
  if (!cd)
6876
0
    return -EINVAL;
6877
6878
0
  if (!isLUKS2(cd->type))
6879
0
    return -ENOTSUP;
6880
6881
  /* No Opal flag present */
6882
0
  if (LUKS2_config_get_opal_version(&cd->u.luks2.hdr, &version) < 0)
6883
0
    return -ENOTSUP;
6884
6885
0
  return version > 1 ? 1 : 0;
6886
0
}
6887
6888
int crypt_get_verity_info(struct crypt_device *cd,
6889
  struct crypt_params_verity *vp)
6890
0
{
6891
0
  if (!cd || !isVERITY(cd->type) || !vp)
6892
0
    return -EINVAL;
6893
6894
0
  vp->data_device = device_path(cd->device);
6895
0
  vp->hash_device = mdata_device_path(cd);
6896
0
  vp->fec_device  = device_path(cd->u.verity.fec_device);
6897
0
  vp->fec_area_offset = cd->u.verity.hdr.fec_area_offset;
6898
0
  vp->fec_roots = cd->u.verity.hdr.fec_roots;
6899
0
  vp->hash_name = cd->u.verity.hdr.hash_name;
6900
0
  vp->salt = cd->u.verity.hdr.salt;
6901
0
  vp->salt_size = cd->u.verity.hdr.salt_size;
6902
0
  vp->data_block_size = cd->u.verity.hdr.data_block_size;
6903
0
  vp->hash_block_size = cd->u.verity.hdr.hash_block_size;
6904
0
  vp->data_size = cd->u.verity.hdr.data_size;
6905
0
  vp->hash_area_offset = cd->u.verity.hdr.hash_area_offset;
6906
0
  vp->hash_type = cd->u.verity.hdr.hash_type;
6907
0
  vp->flags = cd->u.verity.hdr.flags & (CRYPT_VERITY_NO_HEADER | CRYPT_VERITY_ROOT_HASH_SIGNATURE);
6908
0
  return 0;
6909
0
}
6910
6911
int crypt_get_verity_repaired(struct crypt_device *cd, const char *name,
6912
            uint64_t *repaired)
6913
6914
0
{
6915
0
  if (!cd || !isVERITY(cd->type) || !name || !repaired)
6916
0
    return -EINVAL;
6917
6918
0
  return dm_status_verity_repaired(cd, name, repaired);
6919
0
}
6920
6921
int crypt_get_integrity_info(struct crypt_device *cd,
6922
  struct crypt_params_integrity *ip)
6923
0
{
6924
0
  if (!cd || !ip)
6925
0
    return -EINVAL;
6926
6927
0
  if (isINTEGRITY(cd->type)) {
6928
0
    ip->journal_size = cd->u.integrity.params.journal_size;
6929
0
    ip->journal_watermark = cd->u.integrity.params.journal_watermark;
6930
0
    ip->journal_commit_time = cd->u.integrity.params.journal_commit_time;
6931
0
    ip->interleave_sectors = cd->u.integrity.params.interleave_sectors;
6932
0
    ip->tag_size = cd->u.integrity.params.tag_size;
6933
0
    ip->sector_size = cd->u.integrity.params.sector_size;
6934
0
    ip->buffer_sectors = cd->u.integrity.params.buffer_sectors;
6935
6936
0
    ip->integrity = cd->u.integrity.params.integrity;
6937
0
    ip->integrity_key_size = crypt_get_integrity_key_size(cd, false);
6938
6939
0
    ip->journal_integrity = cd->u.integrity.params.journal_integrity;
6940
0
    ip->journal_integrity_key_size = cd->u.integrity.params.journal_integrity_key_size;
6941
0
    ip->journal_integrity_key = NULL;
6942
6943
0
    ip->journal_crypt = cd->u.integrity.params.journal_crypt;
6944
0
    ip->journal_crypt_key_size = cd->u.integrity.params.journal_crypt_key_size;
6945
0
    ip->journal_crypt_key = NULL;
6946
0
    return 0;
6947
0
  } else if (isLUKS2(cd->type)) {
6948
0
    ip->journal_size = 0; // FIXME
6949
0
    ip->journal_watermark = 0; // FIXME
6950
0
    ip->journal_commit_time = 0; // FIXME
6951
0
    ip->interleave_sectors = 0; // FIXME
6952
0
    ip->sector_size = crypt_get_sector_size(cd);
6953
0
    ip->buffer_sectors = 0; // FIXME
6954
6955
0
    ip->integrity = LUKS2_get_integrity(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
6956
0
    ip->integrity_key_size = crypt_get_integrity_key_size(cd, false);
6957
0
    ip->tag_size = INTEGRITY_tag_size(ip->integrity, crypt_get_cipher(cd), crypt_get_cipher_mode(cd));
6958
6959
0
    ip->journal_integrity = NULL;
6960
0
    ip->journal_integrity_key_size = 0;
6961
0
    ip->journal_integrity_key = NULL;
6962
6963
0
    ip->journal_crypt = NULL;
6964
0
    ip->journal_crypt_key_size = 0;
6965
0
    ip->journal_crypt_key = NULL;
6966
0
    return 0;
6967
0
  } else if (!cd->type) {
6968
0
    memset(ip, 0, sizeof(*ip));
6969
0
    ip->integrity = crypt_get_integrity(cd);
6970
0
    ip->integrity_key_size = crypt_get_integrity_key_size(cd, false);
6971
0
    ip->tag_size = crypt_get_integrity_tag_size(cd);
6972
0
  }
6973
6974
0
  return -ENOTSUP;
6975
0
}
6976
6977
int crypt_convert(struct crypt_device *cd,
6978
      const char *type,
6979
      void *params)
6980
0
{
6981
0
  struct luks_phdr hdr1;
6982
0
  struct luks2_hdr hdr2;
6983
0
  int r;
6984
6985
0
  if (!type)
6986
0
    return -EINVAL;
6987
6988
0
  log_dbg(cd, "Converting LUKS device to type %s", type);
6989
6990
0
  if ((r = onlyLUKSnoRequirements(cd)))
6991
0
    return r;
6992
6993
0
  if (isLUKS1(cd->type) && isLUKS2(type))
6994
0
    r = LUKS2_luks1_to_luks2(cd, &cd->u.luks1.hdr, &hdr2);
6995
0
  else if (isLUKS2(cd->type) && isLUKS1(type))
6996
0
    r = LUKS2_luks2_to_luks1(cd, &cd->u.luks2.hdr, &hdr1);
6997
0
  else
6998
0
    return -EINVAL;
6999
7000
0
  if (r < 0) {
7001
    /* in-memory header may be invalid after failed conversion */
7002
0
    _luks2_rollback(cd);
7003
0
    if (r == -EBUSY)
7004
0
      log_err(cd, _("Cannot convert device %s which is still in use."), mdata_device_path(cd));
7005
0
    return r;
7006
0
  }
7007
7008
0
  crypt_free_type(cd, NULL);
7009
7010
0
  return crypt_load(cd, type, params);
7011
0
}
7012
7013
/* Internal access function to header pointer */
7014
void *crypt_get_hdr(struct crypt_device *cd, const char *type)
7015
0
{
7016
0
  assert(cd);
7017
0
  assert(type);
7018
7019
  /* If requested type differs, ignore it */
7020
0
  if (!cd->type || strcmp(cd->type, type))
7021
0
    return NULL;
7022
7023
0
  if (isPLAIN(cd->type))
7024
0
    return &cd->u.plain;
7025
7026
0
  if (isLUKS1(cd->type))
7027
0
    return &cd->u.luks1.hdr;
7028
7029
0
  if (isLUKS2(type))
7030
0
    return &cd->u.luks2.hdr;
7031
7032
0
  if (isLOOPAES(cd->type))
7033
0
    return &cd->u.loopaes;
7034
7035
0
  if (isVERITY(cd->type))
7036
0
    return &cd->u.verity;
7037
7038
0
  if (isTCRYPT(cd->type))
7039
0
    return &cd->u.tcrypt;
7040
7041
0
  return NULL;
7042
0
}
7043
7044
/* internal only */
7045
struct luks2_reencrypt *crypt_get_luks2_reencrypt(struct crypt_device *cd)
7046
0
{
7047
0
  return cd->u.luks2.rh;
7048
0
}
7049
7050
/* internal only */
7051
void crypt_set_luks2_reencrypt(struct crypt_device *cd, struct luks2_reencrypt *rh)
7052
0
{
7053
0
  cd->u.luks2.rh = rh;
7054
0
}
7055
7056
/*
7057
 * Token handling
7058
 */
7059
int crypt_activate_by_token_pin(struct crypt_device *cd, const char *name,
7060
  const char *type, int token, const char *pin, size_t pin_size,
7061
  void *usrptr, uint32_t flags)
7062
0
{
7063
0
  int r;
7064
0
  struct crypt_keyslot_context kc = {};
7065
7066
0
  crypt_keyslot_context_init_by_token_internal(&kc, token, type, pin, pin_size, usrptr);
7067
0
  r = crypt_activate_by_keyslot_context(cd, name, CRYPT_ANY_SLOT, &kc, CRYPT_ANY_SLOT, &kc, flags);
7068
0
  crypt_keyslot_context_destroy_internal(&kc);
7069
7070
0
  return r;
7071
0
}
7072
7073
int crypt_activate_by_token(struct crypt_device *cd,
7074
  const char *name, int token, void *usrptr, uint32_t flags)
7075
0
{
7076
0
  return crypt_activate_by_token_pin(cd, name, NULL, token, NULL, 0, usrptr, flags);
7077
0
}
7078
7079
int crypt_token_json_get(struct crypt_device *cd, int token, const char **json)
7080
0
{
7081
0
  int r;
7082
7083
0
  if (!json)
7084
0
    return -EINVAL;
7085
7086
0
  log_dbg(cd, "Requesting JSON for token %d.", token);
7087
7088
0
  if ((r = onlyLUKS2unrestricted(cd)))
7089
0
    return r;
7090
7091
0
  return LUKS2_token_json_get(&cd->u.luks2.hdr, token, json) ?: token;
7092
0
}
7093
7094
int crypt_token_json_set(struct crypt_device *cd, int token, const char *json)
7095
0
{
7096
0
  int r;
7097
7098
0
  log_dbg(cd, "Updating JSON for token %d.", token);
7099
7100
0
  if ((r = onlyLUKS2(cd)))
7101
0
    return r;
7102
7103
0
  return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1);
7104
0
}
7105
7106
crypt_token_info crypt_token_status(struct crypt_device *cd, int token, const char **type)
7107
0
{
7108
0
  if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
7109
0
    return CRYPT_TOKEN_INVALID;
7110
7111
0
  return LUKS2_token_status(cd, &cd->u.luks2.hdr, token, type);
7112
0
}
7113
7114
int crypt_token_max(const char *type)
7115
0
{
7116
0
  if (isLUKS2(type))
7117
0
    return LUKS2_TOKENS_MAX;
7118
7119
0
  return -EINVAL;
7120
0
}
7121
7122
int crypt_token_luks2_keyring_get(struct crypt_device *cd,
7123
  int token,
7124
  struct crypt_token_params_luks2_keyring *params)
7125
0
{
7126
0
  crypt_token_info token_info;
7127
0
  const char *type;
7128
0
  int r;
7129
7130
0
  if (!params)
7131
0
    return -EINVAL;
7132
7133
0
  log_dbg(cd, "Requesting LUKS2 keyring token %d.", token);
7134
7135
0
  if ((r = onlyLUKS2unrestricted(cd)))
7136
0
    return r;
7137
7138
0
  token_info = LUKS2_token_status(cd, &cd->u.luks2.hdr, token, &type);
7139
0
  switch (token_info) {
7140
0
  case CRYPT_TOKEN_INVALID:
7141
0
    log_dbg(cd, "Token %d is invalid.", token);
7142
0
    return -EINVAL;
7143
0
  case CRYPT_TOKEN_INACTIVE:
7144
0
    log_dbg(cd, "Token %d is inactive.", token);
7145
0
    return -EINVAL;
7146
0
  case CRYPT_TOKEN_INTERNAL:
7147
0
    if (!strcmp(type, LUKS2_TOKEN_KEYRING))
7148
0
      break;
7149
    /* Fall through */
7150
0
  case CRYPT_TOKEN_INTERNAL_UNKNOWN:
7151
0
  case CRYPT_TOKEN_EXTERNAL:
7152
0
  case CRYPT_TOKEN_EXTERNAL_UNKNOWN:
7153
0
    log_dbg(cd, "Token %d has unexpected type %s.", token, type);
7154
0
    return -EINVAL;
7155
0
  }
7156
7157
0
  return LUKS2_token_keyring_get(&cd->u.luks2.hdr, token, params);
7158
0
}
7159
7160
int crypt_token_luks2_keyring_set(struct crypt_device *cd,
7161
  int token,
7162
  const struct crypt_token_params_luks2_keyring *params)
7163
0
{
7164
0
  int r;
7165
0
  char json[4096];
7166
7167
0
  if (!params || !params->key_description)
7168
0
    return -EINVAL;
7169
7170
0
  log_dbg(cd, "Creating new LUKS2 keyring token (%d).", token);
7171
7172
0
  if ((r = onlyLUKS2(cd)))
7173
0
    return r;
7174
7175
0
  r = LUKS2_token_keyring_json(json, sizeof(json), params);
7176
0
  if (r < 0)
7177
0
    return r;
7178
7179
0
  return LUKS2_token_create(cd, &cd->u.luks2.hdr, token, json, 1);
7180
0
}
7181
7182
int crypt_token_assign_keyslot(struct crypt_device *cd, int token, int keyslot)
7183
0
{
7184
0
  int r;
7185
7186
0
  if ((r = onlyLUKS2(cd)))
7187
0
    return r;
7188
7189
0
  if (token == CRYPT_ANY_TOKEN)
7190
0
    return -EINVAL;
7191
7192
0
  return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 1, 1);
7193
0
}
7194
7195
int crypt_token_unassign_keyslot(struct crypt_device *cd, int token, int keyslot)
7196
0
{
7197
0
  int r;
7198
7199
0
  if ((r = onlyLUKS2(cd)))
7200
0
    return r;
7201
7202
0
  if (token == CRYPT_ANY_TOKEN)
7203
0
    return -EINVAL;
7204
7205
0
  return LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot, token, 0, 1);
7206
0
}
7207
7208
int crypt_token_is_assigned(struct crypt_device *cd, int token, int keyslot)
7209
0
{
7210
0
  int r;
7211
7212
0
  if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0)))
7213
0
    return r;
7214
7215
0
  return LUKS2_token_is_assigned(&cd->u.luks2.hdr, keyslot, token);
7216
0
}
7217
7218
/* Internal only */
7219
int crypt_metadata_locking_enabled(void)
7220
53.1k
{
7221
53.1k
  return _metadata_locking;
7222
53.1k
}
7223
7224
int crypt_metadata_locking(struct crypt_device *cd __attribute__((unused)), int enable)
7225
0
{
7226
0
  if (enable && !_metadata_locking)
7227
0
    return -EPERM;
7228
7229
0
  _metadata_locking = enable ? 1 : 0;
7230
0
  return 0;
7231
0
}
7232
7233
int crypt_persistent_flags_set(struct crypt_device *cd, crypt_flags_type type, uint32_t flags)
7234
0
{
7235
0
  int r;
7236
7237
0
  if ((r = onlyLUKS2(cd)))
7238
0
    return r;
7239
7240
0
  if (type == CRYPT_FLAGS_ACTIVATION)
7241
0
    return LUKS2_config_set_flags(cd, &cd->u.luks2.hdr, flags);
7242
7243
0
  if (type == CRYPT_FLAGS_REQUIREMENTS)
7244
0
    return LUKS2_config_set_requirements(cd, &cd->u.luks2.hdr, flags, true);
7245
7246
0
  return -EINVAL;
7247
0
}
7248
7249
int crypt_persistent_flags_get(struct crypt_device *cd, crypt_flags_type type, uint32_t *flags)
7250
0
{
7251
0
  int r;
7252
7253
0
  if (!flags)
7254
0
    return -EINVAL;
7255
7256
0
  if ((r = onlyLUKS2unrestricted(cd)))
7257
0
    return r;
7258
7259
0
  if (type == CRYPT_FLAGS_ACTIVATION)
7260
0
    return LUKS2_config_get_flags(cd, &cd->u.luks2.hdr, flags);
7261
7262
0
  if (type == CRYPT_FLAGS_REQUIREMENTS) {
7263
0
    LUKS2_config_get_requirements(cd, &cd->u.luks2.hdr, flags);
7264
0
    return 0;
7265
0
  }
7266
7267
0
  return -EINVAL;
7268
0
}
7269
7270
static int update_volume_key_segment_digest(struct crypt_device *cd, struct luks2_hdr *hdr, int digest, int commit)
7271
0
{
7272
0
  int r;
7273
7274
  /* Remove any assignments in memory */
7275
0
  r = LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, CRYPT_ANY_DIGEST, 0, 0);
7276
0
  if (r)
7277
0
    return r;
7278
7279
  /* Assign it to the specific digest */
7280
0
  return LUKS2_digest_segment_assign(cd, hdr, CRYPT_DEFAULT_SEGMENT, digest, 1, commit);
7281
0
}
7282
7283
static int verify_and_update_segment_digest(struct crypt_device *cd,
7284
    struct luks2_hdr *hdr, int keyslot, struct crypt_keyslot_context *kc)
7285
0
{
7286
0
  int digest, r;
7287
0
  struct volume_key *vk = NULL;
7288
7289
0
  assert(kc);
7290
0
  assert(kc->get_luks2_key);
7291
0
  assert(keyslot >= 0);
7292
7293
0
  r = kc->get_luks2_key(cd, kc, keyslot, CRYPT_ANY_SEGMENT, &vk);
7294
0
  if (r < 0)
7295
0
    return r;
7296
7297
  /* check volume_key (param) digest matches keyslot digest */
7298
0
  r = LUKS2_digest_verify(cd, hdr, vk, keyslot);
7299
0
  if (r < 0)
7300
0
    goto out;
7301
0
  digest = r;
7302
7303
  /* nothing to do, volume key in keyslot is already assigned to default segment */
7304
0
  r = LUKS2_digest_verify_by_segment(cd, hdr, CRYPT_DEFAULT_SEGMENT, vk);
7305
0
  if (r >= 0)
7306
0
    goto out;
7307
7308
  /* FIXME: check new volume key is usable with current default segment */
7309
7310
0
  r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 1);
7311
0
  if (r)
7312
0
    log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot);
7313
0
out:
7314
0
  crypt_free_volume_key(vk);
7315
7316
0
  return r < 0 ? r : keyslot;
7317
0
}
7318
7319
static int luks2_keyslot_add_by_verified_volume_key(struct crypt_device *cd,
7320
  int keyslot_new,
7321
  const char *new_passphrase,
7322
  size_t new_passphrase_size,
7323
  struct volume_key *vk)
7324
0
{
7325
0
  int r;
7326
0
  struct luks2_keyslot_params params;
7327
7328
0
  assert(cd);
7329
0
  assert(keyslot_new >= 0);
7330
0
  assert(new_passphrase);
7331
0
  assert(vk);
7332
0
  assert(crypt_volume_key_get_id(vk) >= 0);
7333
7334
0
  r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, &params);
7335
0
  if (r < 0) {
7336
0
    log_err(cd, _("Failed to initialize default LUKS2 keyslot parameters."));
7337
0
    return r;
7338
0
  }
7339
7340
0
  r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, crypt_volume_key_get_id(vk), 1, 0);
7341
0
  if (r < 0) {
7342
0
    log_err(cd, _("Failed to assign keyslot %d to digest."), keyslot_new);
7343
0
    return r;
7344
0
  }
7345
7346
0
  r = LUKS2_keyslot_store(cd,  &cd->u.luks2.hdr, keyslot_new,
7347
0
        CONST_CAST(char*)new_passphrase,
7348
0
        new_passphrase_size, vk, &params);
7349
7350
0
  return r < 0 ? r : keyslot_new;
7351
0
}
7352
7353
static int luks2_keyslot_add_by_volume_key(struct crypt_device *cd,
7354
  int keyslot_new,
7355
  const char *new_passphrase,
7356
  size_t new_passphrase_size,
7357
  struct volume_key *vk)
7358
0
{
7359
0
  int r;
7360
7361
0
  assert(cd);
7362
0
  assert(keyslot_new >= 0);
7363
0
  assert(new_passphrase);
7364
0
  assert(vk);
7365
7366
0
  r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
7367
0
  if (r >= 0)
7368
0
    crypt_volume_key_set_id(vk, r);
7369
7370
0
  if (r < 0) {
7371
0
    log_err(cd, _("Volume key does not match the volume."));
7372
0
    return r;
7373
0
  }
7374
7375
0
  return luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
7376
0
}
7377
7378
static int luks1_keyslot_add_by_volume_key(struct crypt_device *cd,
7379
  int keyslot_new,
7380
  const char *new_passphrase,
7381
  size_t new_passphrase_size,
7382
  struct volume_key *vk)
7383
0
{
7384
0
  int r;
7385
7386
0
  assert(cd);
7387
0
  assert(keyslot_new >= 0);
7388
0
  assert(new_passphrase);
7389
0
  assert(vk);
7390
7391
0
  r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
7392
0
  if (r < 0) {
7393
0
    log_err(cd, _("Volume key does not match the volume."));
7394
0
    return r;
7395
0
  }
7396
7397
0
  r = LUKS_set_key(keyslot_new, CONST_CAST(char*)new_passphrase,
7398
0
       new_passphrase_size, &cd->u.luks1.hdr, vk, cd);
7399
7400
0
  return r < 0 ? r : keyslot_new;
7401
0
}
7402
7403
static int keyslot_add_by_key(struct crypt_device *cd,
7404
  bool is_luks1,
7405
  int keyslot_new,
7406
  const char *new_passphrase,
7407
  size_t new_passphrase_size,
7408
  struct volume_key *vk,
7409
  uint32_t flags)
7410
0
{
7411
0
  int r, digest;
7412
7413
0
  assert(cd);
7414
0
  assert(keyslot_new >= 0);
7415
0
  assert(new_passphrase);
7416
0
  assert(vk);
7417
7418
0
  if (is_luks1) {
7419
0
    if (flags)
7420
0
      return -EINVAL;
7421
0
    return luks1_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
7422
0
  }
7423
7424
  /* if passed key matches volume key digest tear down new vk flag */
7425
0
  if (flags & CRYPT_VOLUME_KEY_SET) {
7426
0
    digest = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
7427
0
    if (digest >= 0)
7428
0
      flags &= ~CRYPT_VOLUME_KEY_SET;
7429
0
    else if (digest != -EPERM) /* Anything other than -EPERM suggests broken metadata. Abort */
7430
0
      return digest;
7431
0
  }
7432
7433
  /*
7434
   * Drop CRYPT_VOLUME_KEY_DIGEST_REUSE flag if used without CRYPT_VOLUME_KEY_SET
7435
   * or CRYPT_VOLUME_KEY_NO_SEGMENT flags. The standalone CRYPT_VOLUME_KEY_DIGEST_REUSE flag
7436
   * is otherwise equivalent to adding new keyslot with current volume key.
7437
   */
7438
0
  if ((flags & CRYPT_VOLUME_KEY_DIGEST_REUSE) &&
7439
0
      !(flags & (CRYPT_VOLUME_KEY_SET | CRYPT_VOLUME_KEY_NO_SEGMENT)))
7440
0
    flags &= ~CRYPT_VOLUME_KEY_DIGEST_REUSE;
7441
7442
0
  if (!flags)
7443
0
    return luks2_keyslot_add_by_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
7444
7445
0
  digest = -ENOENT;
7446
  /* check if passed key matches any existing unbound digest */
7447
0
  if (flags & CRYPT_VOLUME_KEY_DIGEST_REUSE)
7448
0
    digest = LUKS2_digest_verify_by_any_matching(cd, vk, /* exclude_default_segment= */ true);
7449
7450
  /* Anything other than -EPERM or -ENOENT suggests broken metadata. Abort */
7451
0
  if (digest < 0 && digest != -ENOENT && digest != -EPERM)
7452
0
    return digest;
7453
7454
  /* no segment flag or new vk flag requires new key digest */
7455
0
  if (digest < 0 && (flags & (CRYPT_VOLUME_KEY_NO_SEGMENT | CRYPT_VOLUME_KEY_SET)))
7456
0
    digest = LUKS2_digest_create(cd, "pbkdf2", &cd->u.luks2.hdr, vk);
7457
7458
0
  r = digest;
7459
0
  if (r < 0)
7460
0
    return r;
7461
7462
0
  crypt_volume_key_set_id(vk, digest);
7463
7464
0
  if (flags & CRYPT_VOLUME_KEY_SET) {
7465
0
    r = update_volume_key_segment_digest(cd, &cd->u.luks2.hdr, digest, 0);
7466
0
    if (r < 0)
7467
0
      log_err(cd, _("Failed to assign keyslot %u as the new volume key."), keyslot_new);
7468
0
  }
7469
7470
0
  if (r >= 0)
7471
0
    r = luks2_keyslot_add_by_verified_volume_key(cd, keyslot_new, new_passphrase, new_passphrase_size, vk);
7472
7473
0
  return r < 0 ? r : keyslot_new;
7474
0
}
7475
7476
int crypt_keyslot_add_by_key(struct crypt_device *cd,
7477
  int keyslot,
7478
  const char *volume_key,
7479
  size_t volume_key_size,
7480
  const char *passphrase,
7481
  size_t passphrase_size,
7482
  uint32_t flags)
7483
0
{
7484
0
  int r;
7485
0
  struct crypt_keyslot_context kc = {}, new_kc = {};
7486
7487
0
  if (!passphrase || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) &&
7488
0
          (flags & CRYPT_VOLUME_KEY_SET)))
7489
0
    return -EINVAL;
7490
7491
0
  if ((r = onlyLUKS(cd)) < 0)
7492
0
    return r;
7493
7494
0
  if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot) > CRYPT_SLOT_INACTIVE &&
7495
0
      isLUKS2(cd->type)) {
7496
0
    if (volume_key)
7497
0
      crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
7498
0
    else
7499
0
      crypt_keyslot_context_init_by_passphrase_internal(&kc, passphrase, passphrase_size);
7500
7501
0
    r = verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot, &kc);
7502
7503
0
    crypt_keyslot_context_destroy_internal(&kc);
7504
7505
0
    return r;
7506
0
  }
7507
7508
0
  crypt_keyslot_context_init_by_key_internal(&kc, volume_key, volume_key_size);
7509
0
  crypt_keyslot_context_init_by_passphrase_internal(&new_kc, passphrase, passphrase_size);
7510
7511
0
  r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, flags);
7512
7513
0
  crypt_keyslot_context_destroy_internal(&kc);
7514
0
  crypt_keyslot_context_destroy_internal(&new_kc);
7515
7516
0
  return r;
7517
0
}
7518
7519
int crypt_keyslot_add_by_keyslot_context(struct crypt_device *cd,
7520
  int keyslot_existing,
7521
  struct crypt_keyslot_context *kc,
7522
  int keyslot_new,
7523
  struct crypt_keyslot_context *new_kc,
7524
  uint32_t flags)
7525
0
{
7526
0
  bool is_luks1;
7527
0
  int active_slots, r;
7528
0
  const char *new_passphrase;
7529
0
  size_t new_passphrase_size;
7530
0
  struct volume_key *vk = NULL;
7531
7532
0
  if (!kc || ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) &&
7533
0
        (flags & CRYPT_VOLUME_KEY_SET)))
7534
0
    return -EINVAL;
7535
7536
0
  r = flags ? onlyLUKS2(cd) : onlyLUKS(cd);
7537
0
  if (r)
7538
0
    return r;
7539
7540
0
  if ((flags & CRYPT_VOLUME_KEY_SET) && crypt_keyslot_status(cd, keyslot_existing) > CRYPT_SLOT_INACTIVE)
7541
0
    return verify_and_update_segment_digest(cd, &cd->u.luks2.hdr, keyslot_existing, kc);
7542
7543
0
  if (!new_kc || !new_kc->get_passphrase)
7544
0
    return -EINVAL;
7545
7546
0
  log_dbg(cd, "Adding new keyslot %d by %s%s, volume key provided by %s (%d).",
7547
0
    keyslot_new, keyslot_context_type_string(new_kc),
7548
0
    (flags & CRYPT_VOLUME_KEY_NO_SEGMENT) ? " unassigned to a crypt segment" : "",
7549
0
    keyslot_context_type_string(kc), keyslot_existing);
7550
7551
0
  r = keyslot_verify_or_find_empty(cd, &keyslot_new);
7552
0
  if (r < 0)
7553
0
    return r;
7554
7555
0
  is_luks1 = isLUKS1(cd->type);
7556
0
  if (is_luks1)
7557
0
    active_slots = LUKS_keyslot_active_count(&cd->u.luks1.hdr);
7558
0
  else
7559
0
    active_slots = LUKS2_keyslot_active_count(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
7560
7561
0
  if (active_slots < 0)
7562
0
    return -EINVAL;
7563
7564
0
  if (active_slots == 0 && kc->type != CRYPT_KC_TYPE_KEY)
7565
0
    r = -ENOENT;
7566
0
  else if (is_luks1 && kc->get_luks1_volume_key)
7567
0
    r = kc->get_luks1_volume_key(cd, kc, keyslot_existing, &vk);
7568
0
  else if (!is_luks1 && kc->get_luks2_volume_key)
7569
0
    r = kc->get_luks2_volume_key(cd, kc, keyslot_existing, &vk);
7570
0
  else
7571
0
    return -EINVAL;
7572
7573
0
  if (r == -ENOENT) {
7574
0
    if ((flags & CRYPT_VOLUME_KEY_NO_SEGMENT) && kc->type == CRYPT_KC_TYPE_KEY) {
7575
0
      if (!(vk = crypt_generate_volume_key(cd, kc->u.k.volume_key_size, KEY_QUALITY_KEY)))
7576
0
        return -ENOMEM;
7577
0
      r = 0;
7578
0
    } else if (cd->volume_key) {
7579
0
      if (!(vk = crypt_alloc_volume_key(crypt_volume_key_length(cd->volume_key),
7580
0
                crypt_volume_key_get_key(cd->volume_key))))
7581
0
        return -ENOMEM;
7582
0
      r = 0;
7583
0
    } else if (active_slots == 0) {
7584
0
      log_err(cd, _("Cannot add key slot, all slots disabled and no volume key provided."));
7585
0
      r = -EINVAL;
7586
0
    }
7587
0
  }
7588
7589
0
  if (r < 0)
7590
0
    return r;
7591
7592
0
  r = new_kc->get_passphrase(cd, new_kc, &new_passphrase, &new_passphrase_size);
7593
  /* If new keyslot context is token just assign it to new keyslot */
7594
0
  if (r >= 0 && new_kc->type == CRYPT_KC_TYPE_TOKEN && !is_luks1)
7595
0
    r = LUKS2_token_assign(cd, &cd->u.luks2.hdr, keyslot_new, new_kc->u.t.id, 1, 0);
7596
0
  if (r >= 0)
7597
0
    r = keyslot_add_by_key(cd, is_luks1, keyslot_new, new_passphrase, new_passphrase_size, vk, flags);
7598
7599
0
  crypt_free_volume_key(vk);
7600
7601
0
  if (r < 0) {
7602
0
    _luks2_rollback(cd);
7603
0
    return r;
7604
0
  }
7605
7606
0
  return keyslot_new;
7607
0
}
7608
7609
/*
7610
 * Keyring handling
7611
 */
7612
int crypt_use_keyring_for_vk(struct crypt_device *cd)
7613
0
{
7614
0
  uint64_t dmc_flags;
7615
7616
  /* dm backend must be initialized */
7617
0
  if (!cd)
7618
0
    return 0;
7619
7620
0
  if (!isPLAIN(cd->type) && !isLUKS2(cd->type))
7621
0
    return 0;
7622
7623
0
  if (!_vk_via_keyring || !kernel_keyring_support())
7624
0
    return 0;
7625
7626
0
  if (dm_flags(cd, DM_CRYPT, &dmc_flags))
7627
0
    return dmcrypt_keyring_bug() ? 0 : 1;
7628
7629
0
  return (dmc_flags & DM_KERNEL_KEYRING_SUPPORTED);
7630
0
}
7631
7632
int crypt_volume_key_keyring(struct crypt_device *cd __attribute__((unused)), int enable)
7633
0
{
7634
0
  _vk_via_keyring = enable ? 1 : 0;
7635
0
  return 0;
7636
0
}
7637
7638
/* internal only */
7639
int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk)
7640
0
{
7641
0
  if (!vk || !cd)
7642
0
    return -EINVAL;
7643
7644
0
  if (!crypt_volume_key_description(vk)) {
7645
0
    log_dbg(cd, "Invalid key description");
7646
0
    return -EINVAL;
7647
0
  }
7648
7649
0
  log_dbg(cd, "Loading key (type logon, name %s) in thread keyring.",
7650
0
    crypt_volume_key_description(vk));
7651
7652
0
  if (crypt_volume_key_upload_kernel_key(vk)) {
7653
0
    crypt_set_key_in_keyring(cd, 1);
7654
0
    return 0;
7655
0
  } else {
7656
0
    log_dbg(cd, "keyring_add_key_in_thread_keyring failed (error %d)", errno);
7657
0
    log_err(cd, _("Failed to load key in kernel keyring."));
7658
0
    return -EINVAL;
7659
0
  }
7660
0
}
7661
7662
/* internal only */
7663
int crypt_keyring_get_user_key(struct crypt_device *cd,
7664
    const char *key_description,
7665
    char **key,
7666
    size_t *key_size)
7667
0
{
7668
0
  int r;
7669
0
  key_serial_t kid;
7670
7671
0
  if (!key_description || !key || !key_size)
7672
0
    return -EINVAL;
7673
7674
0
  log_dbg(cd, "Requesting key %s (user type)", key_description);
7675
7676
0
  kid = keyring_request_key_id(USER_KEY, key_description);
7677
0
  if (kid == -ENOTSUP) {
7678
0
    log_dbg(cd, "Kernel keyring features disabled.");
7679
0
    return -ENOTSUP;
7680
0
  } else if (kid < 0) {
7681
0
    log_dbg(cd, "keyring_request_key_id failed with errno %d.", errno);
7682
0
    return -EINVAL;
7683
0
  }
7684
7685
0
  log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid);
7686
7687
0
  r = keyring_read_key(kid, key, key_size);
7688
0
  if (r < 0)
7689
0
    log_dbg(cd, "keyring_read_key failed with errno %d.", errno);
7690
7691
0
  return r;
7692
0
}
7693
7694
/* internal only */
7695
int crypt_keyring_get_key_by_name(struct crypt_device *cd,
7696
    const char *key_description,
7697
    char **key,
7698
    size_t *key_size)
7699
0
{
7700
0
  int r;
7701
0
  key_serial_t kid;
7702
7703
0
  if (!key_description || !key || !key_size)
7704
0
    return -EINVAL;
7705
7706
0
  log_dbg(cd, "Searching for kernel key by name %s.", key_description);
7707
7708
0
  kid = keyring_find_key_id_by_name(key_description);
7709
0
  if (kid == 0) {
7710
0
    log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", errno);
7711
0
    return -ENOENT;
7712
0
  }
7713
7714
0
  log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid);
7715
7716
0
  r = keyring_read_key(kid, key, key_size);
7717
0
  if (r < 0)
7718
0
    log_dbg(cd, "keyring_read_key failed with errno %d.", errno);
7719
7720
0
  return r;
7721
0
}
7722
7723
int crypt_keyring_get_keysize_by_name(struct crypt_device *cd,
7724
    const char *key_description,
7725
    size_t *r_key_size)
7726
0
{
7727
0
  int r;
7728
0
  key_serial_t kid;
7729
7730
0
  if (!key_description || !r_key_size)
7731
0
    return -EINVAL;
7732
7733
0
  log_dbg(cd, "Searching for kernel key by name %s.", key_description);
7734
7735
0
  kid = keyring_find_key_id_by_name(key_description);
7736
0
  if (kid == -ENOTSUP) {
7737
0
    log_dbg(cd, "Kernel keyring features disabled.");
7738
0
    return -ENOTSUP;
7739
0
  } else if (kid < 0) {
7740
0
    log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", errno);
7741
0
    return -EINVAL;
7742
0
  }
7743
0
  else if (kid == 0) {
7744
0
    log_dbg(cd, "keyring_find_key_id_by_name failed with errno %d.", ENOENT);
7745
0
    return -ENOENT;
7746
0
  }
7747
7748
0
  log_dbg(cd, "Reading content of kernel key (id %" PRIi32 ").", kid);
7749
7750
0
  r = keyring_read_keysize(kid, r_key_size);
7751
0
  if (r < 0)
7752
0
    log_dbg(cd, "keyring_read_keysize failed with errno %d.", errno);
7753
7754
0
  return r;
7755
0
}
7756
7757
/* internal only */
7758
int crypt_key_in_keyring(struct crypt_device *cd)
7759
0
{
7760
0
  return cd ? cd->key_in_keyring : 0;
7761
0
}
7762
7763
/* internal only */
7764
void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring)
7765
0
{
7766
0
  if (!cd)
7767
0
    return;
7768
7769
0
  cd->key_in_keyring = key_in_keyring;
7770
0
}
7771
7772
/* internal only */
7773
void crypt_unlink_key_from_thread_keyring(struct crypt_device *cd,
7774
    key_serial_t key_id)
7775
0
{
7776
0
  log_dbg(cd, "Unlinking volume key (id: %" PRIi32 ") from thread keyring.", key_id);
7777
7778
0
  if (keyring_unlink_key_from_thread_keyring(key_id))
7779
0
    log_dbg(cd, "keyring_unlink_key_from_thread_keyring failed with errno %d.", errno);
7780
0
}
7781
7782
void crypt_unlink_key_by_description_from_thread_keyring(struct crypt_device *cd,
7783
    const char *key_description,
7784
    key_type_t ktype)
7785
0
{
7786
0
  key_serial_t kid;
7787
0
  const char *type_name = key_type_name(ktype);
7788
7789
0
  if (!key_description || !type_name)
7790
0
    return;
7791
7792
0
  log_dbg(cd, "Requesting kernel key %s (type %s).", key_description, type_name);
7793
7794
0
  crypt_set_key_in_keyring(cd, 0);
7795
7796
0
  kid = keyring_request_key_id(ktype, key_description);
7797
0
  if (kid == -ENOTSUP) {
7798
0
    log_dbg(cd, "Kernel keyring features disabled.");
7799
0
    return;
7800
0
  } else if (kid < 0) {
7801
0
    log_dbg(cd, "keyring_request_key_id failed with errno %d.", errno);
7802
0
    return;
7803
0
  }
7804
7805
0
  crypt_unlink_key_from_thread_keyring(cd, kid);
7806
0
}
7807
7808
int crypt_set_keyring_to_link(struct crypt_device *cd, const char *key_description,
7809
            const char *old_key_description,
7810
            const char *key_type_desc, const char *keyring_to_link_vk)
7811
0
{
7812
0
  key_type_t key_type = USER_KEY;
7813
0
  const char *name1 = NULL, *name2 = NULL;
7814
0
  int32_t id = 0;
7815
0
  int r, ri;
7816
0
  struct luks2_hdr *hdr;
7817
0
  unsigned user_descriptions_count, vks_count = 1;
7818
7819
0
  if (!cd || ((!key_description && !old_key_description) && (keyring_to_link_vk || key_type_desc)) ||
7820
0
      ((key_description || old_key_description) && !keyring_to_link_vk))
7821
0
    return -EINVAL;
7822
7823
0
  hdr = crypt_get_hdr(cd, CRYPT_LUKS2);
7824
7825
  /* if only one key description is supplied, force it to be the first one */
7826
0
  if (!key_description && old_key_description)
7827
0
    return -EINVAL;
7828
7829
0
  if ((r = _onlyLUKS2(cd, 0, CRYPT_REQUIREMENT_OPAL | CRYPT_REQUIREMENT_ONLINE_REENCRYPT)))
7830
0
    return r;
7831
7832
0
  if (key_type_desc)
7833
0
    key_type = key_type_by_name(key_type_desc);
7834
0
  if (key_type != LOGON_KEY && key_type != USER_KEY)
7835
0
    return -EINVAL;
7836
7837
0
  ri = crypt_reencrypt_status(cd, NULL);
7838
0
  if (ri > CRYPT_REENCRYPT_NONE && ri < CRYPT_REENCRYPT_INVALID)
7839
0
    vks_count = LUKS2_reencrypt_vks_count(hdr);
7840
7841
0
  user_descriptions_count = (key_description ? 1 : 0) + (old_key_description ? 1 : 0);
7842
0
  if (user_descriptions_count != 0 && vks_count > user_descriptions_count)
7843
0
    return -ESRCH;
7844
7845
0
  if (keyring_to_link_vk) {
7846
0
    id = keyring_find_keyring_id_by_name(keyring_to_link_vk);
7847
0
    if (id == 0) {
7848
0
      log_err(cd, _("Could not find keyring described by \"%s\"."), keyring_to_link_vk);
7849
0
      return -EINVAL;
7850
0
    }
7851
0
    if (key_description && !(name1 = strdup(key_description)))
7852
0
      return -ENOMEM;
7853
0
    if (old_key_description && !(name2 = strdup(old_key_description))) {
7854
0
      free(CONST_CAST(void*)name1);
7855
0
      return -ENOMEM;
7856
0
    }
7857
0
  }
7858
7859
0
  cd->keyring_key_type = key_type;
7860
7861
0
  free(CONST_CAST(void*)cd->user_key_name1);
7862
0
  free(CONST_CAST(void*)cd->user_key_name2);
7863
0
  cd->user_key_name1 = name1;
7864
0
  cd->user_key_name2 = name2;
7865
0
  cd->keyring_to_link_vk = id;
7866
0
  cd->link_vk_to_keyring = id != 0;
7867
7868
0
  return 0;
7869
0
}
7870
7871
/* internal only */
7872
void crypt_drop_uploaded_keyring_key(struct crypt_device *cd, struct volume_key *vks)
7873
0
{
7874
0
  struct volume_key *vk = vks;
7875
7876
0
  while (vk) {
7877
0
    crypt_volume_key_drop_uploaded_kernel_key(cd, vk);
7878
0
    vk = crypt_volume_key_next(vk);
7879
0
  }
7880
0
}
7881
7882
int crypt_activate_by_keyring(struct crypt_device *cd,
7883
            const char *name,
7884
            const char *key_description,
7885
            int keyslot,
7886
            uint32_t flags)
7887
0
{
7888
0
  int r;
7889
0
  struct crypt_keyslot_context kc = {};
7890
7891
0
  if (!cd || !key_description)
7892
0
    return -EINVAL;
7893
7894
0
  crypt_keyslot_context_init_by_keyring_internal(&kc, key_description);
7895
0
  r = crypt_activate_by_keyslot_context(cd, name, keyslot, &kc, CRYPT_ANY_SLOT, &kc, flags);
7896
0
  crypt_keyslot_context_destroy_internal(&kc);
7897
7898
0
  return r;
7899
0
}
7900
7901
/*
7902
 * Workaround for serialization of parallel activation and memory-hard PBKDF
7903
 * In specific situation (systemd activation) this causes OOM killer activation.
7904
 * For now, let's provide this ugly way to serialize unlocking of devices.
7905
 */
7906
int crypt_serialize_lock(struct crypt_device *cd)
7907
0
{
7908
0
  if (!cd->memory_hard_pbkdf_lock_enabled)
7909
0
    return 0;
7910
7911
0
  log_dbg(cd, "Taking global memory-hard access serialization lock.");
7912
0
  if (crypt_write_lock(cd, "memory-hard-access", true, &cd->pbkdf_memory_hard_lock)) {
7913
0
    log_err(cd, _("Failed to acquire global memory-hard access serialization lock."));
7914
0
    cd->pbkdf_memory_hard_lock = NULL;
7915
0
    return -EINVAL;
7916
0
  }
7917
7918
0
  return 0;
7919
0
}
7920
7921
void crypt_serialize_unlock(struct crypt_device *cd)
7922
0
{
7923
0
  if (!cd->memory_hard_pbkdf_lock_enabled)
7924
0
    return;
7925
7926
0
  crypt_unlock_internal(cd, cd->pbkdf_memory_hard_lock);
7927
0
  cd->pbkdf_memory_hard_lock = NULL;
7928
0
}
7929
7930
crypt_reencrypt_info crypt_reencrypt_status(struct crypt_device *cd,
7931
    struct crypt_params_reencrypt *params)
7932
0
{
7933
0
  if (params)
7934
0
    memset(params, 0, sizeof(*params));
7935
7936
0
  if (!cd || !isLUKS(cd->type))
7937
0
    return CRYPT_REENCRYPT_INVALID;
7938
7939
0
  if (isLUKS1(cd->type))
7940
0
    return CRYPT_REENCRYPT_NONE;
7941
7942
0
  if (_onlyLUKS2(cd, CRYPT_CD_QUIET, CRYPT_REQUIREMENT_ONLINE_REENCRYPT))
7943
0
    return CRYPT_REENCRYPT_INVALID;
7944
7945
0
  return LUKS2_reencrypt_get_params(&cd->u.luks2.hdr, params);
7946
0
}
7947
7948
static void __attribute__((destructor)) libcryptsetup_exit(void)
7949
0
{
7950
0
  crypt_token_unload_external_all(NULL);
7951
7952
0
  crypt_backend_destroy();
7953
0
  crypt_random_exit();
7954
0
}