Coverage Report

Created: 2025-12-14 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptsetup/lib/luks1/keyencryption.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * LUKS - Linux Unified Key Setup
4
 *
5
 * Copyright (C) 2004-2006 Clemens Fruhwirth <clemens@endorphin.org>
6
 * Copyright (C) 2009-2025 Red Hat, Inc. All rights reserved.
7
 * Copyright (C) 2012-2025 Milan Broz
8
 */
9
10
#include <stdio.h>
11
#include <string.h>
12
#include <errno.h>
13
#include <sys/stat.h>
14
#include "luks.h"
15
#include "af.h"
16
#include "internal.h"
17
18
static void _error_hint(struct crypt_device *ctx, const char *device,
19
      const char *cipher, const char *mode, size_t keyLength)
20
0
{
21
0
  const char *c;
22
0
  char cipher_spec[MAX_CIPHER_LEN * 3];
23
24
0
  if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, mode) < 0)
25
0
    return;
26
27
0
  log_err(ctx, _("Failed to setup dm-crypt key mapping for device %s.\n"
28
0
      "Check that kernel supports %s cipher (check syslog for more info)."),
29
0
      device, cipher_spec);
30
31
0
  if (!strncmp(mode, "xts", 3) && (keyLength != 256 && keyLength != 512))
32
0
    log_err(ctx, _("Key size in XTS mode must be 256 or 512 bits."));
33
0
  else if (!(c = strchr(mode, '-')) || strlen(c) < 4)
34
0
    log_err(ctx, _("Cipher specification should be in [cipher]-[mode]-[iv] format."));
35
0
}
36
37
static int LUKS_endec_template(char *src, size_t srcLength,
38
             const char *cipher, const char *cipher_mode,
39
             struct volume_key *vk,
40
             unsigned int sector,
41
             ssize_t (*func)(int, size_t, size_t, void *, size_t),
42
             int mode,
43
             struct crypt_device *ctx)
44
0
{
45
0
  char name[PATH_MAX], path[PATH_MAX];
46
0
  char cipher_spec[MAX_CIPHER_LEN * 3];
47
0
  struct crypt_dm_active_device dmd = {
48
0
    .flags = CRYPT_ACTIVATE_PRIVATE,
49
0
  };
50
0
  int r, devfd = -1, remove_dev = 0;
51
0
  size_t bsize, keyslot_alignment, alignment;
52
53
0
  log_dbg(ctx, "Using dmcrypt to access keyslot area.");
54
55
0
  bsize = device_block_size(ctx, crypt_metadata_device(ctx));
56
0
  alignment = device_alignment(crypt_metadata_device(ctx));
57
0
  if (!bsize || !alignment)
58
0
    return -EINVAL;
59
60
0
  if (bsize > LUKS_ALIGN_KEYSLOTS)
61
0
    keyslot_alignment = LUKS_ALIGN_KEYSLOTS;
62
0
  else
63
0
    keyslot_alignment = bsize;
64
0
  dmd.size = size_round_up(srcLength, keyslot_alignment) / SECTOR_SIZE;
65
66
0
  if (mode == O_RDONLY)
67
0
    dmd.flags |= CRYPT_ACTIVATE_READONLY;
68
69
0
  if (snprintf(name, sizeof(name), "temporary-cryptsetup-%d", getpid()) < 0)
70
0
    return -ENOMEM;
71
0
  if (snprintf(path, sizeof(path), "%s/%s", dm_get_dir(), name) < 0)
72
0
    return -ENOMEM;
73
0
  if (snprintf(cipher_spec, sizeof(cipher_spec), "%s-%s", cipher, cipher_mode) < 0)
74
0
    return -ENOMEM;
75
76
0
  r = device_block_adjust(ctx, crypt_metadata_device(ctx), DEV_OK,
77
0
        sector, &dmd.size, &dmd.flags);
78
0
  if (r < 0) {
79
0
    log_err(ctx, _("Device %s does not exist or access denied."),
80
0
      device_path(crypt_metadata_device(ctx)));
81
0
    return -EIO;
82
0
  }
83
84
0
  if (mode != O_RDONLY && dmd.flags & CRYPT_ACTIVATE_READONLY) {
85
0
    log_err(ctx, _("Cannot write to device %s, permission denied."),
86
0
      device_path(crypt_metadata_device(ctx)));
87
0
    return -EACCES;
88
0
  }
89
90
0
  r = dm_crypt_target_set(&dmd.segment, 0, dmd.size,
91
0
      crypt_metadata_device(ctx), vk, cipher_spec, 0, sector,
92
0
      NULL, 0, 0, SECTOR_SIZE);
93
0
  if (r)
94
0
    goto out;
95
96
0
  r = dm_create_device(ctx, name, "TEMP", &dmd);
97
0
  if (r < 0) {
98
0
    if (r != -EACCES && r != -ENOTSUP)
99
0
      _error_hint(ctx, device_path(crypt_metadata_device(ctx)),
100
0
            cipher, cipher_mode, crypt_volume_key_length(vk) * 8);
101
0
    r = -EIO;
102
0
    goto out;
103
0
  }
104
0
  remove_dev = 1;
105
106
0
  devfd = open(path, mode | O_DIRECT | O_SYNC);
107
0
  if (devfd == -1) {
108
0
    log_err(ctx, _("Failed to open temporary keystore device."));
109
0
    r = -EIO;
110
0
    goto out;
111
0
  }
112
113
0
  r = func(devfd, bsize, alignment, src, srcLength);
114
0
  if (r < 0) {
115
0
    log_err(ctx, _("Failed to access temporary keystore device."));
116
0
    r = -EIO;
117
0
  } else
118
0
    r = 0;
119
0
 out:
120
0
  dm_targets_free(ctx, &dmd);
121
0
  if (devfd != -1)
122
0
    close(devfd);
123
0
  if (remove_dev)
124
0
    dm_remove_device(ctx, name, CRYPT_DEACTIVATE_FORCE);
125
0
  return r;
126
0
}
127
128
int LUKS_encrypt_to_storage(char *src, size_t srcLength,
129
          const char *cipher,
130
          const char *cipher_mode,
131
          struct volume_key *vk,
132
          unsigned int sector,
133
          struct crypt_device *ctx)
134
0
{
135
0
  struct device *device = crypt_metadata_device(ctx);
136
0
  struct crypt_storage *s;
137
0
  int devfd, r = 0;
138
139
  /* Only whole sector writes supported */
140
0
  if (MISALIGNED_512(srcLength))
141
0
    return -EINVAL;
142
143
  /* Encrypt buffer */
144
0
  r = crypt_storage_init(&s, SECTOR_SIZE, cipher, cipher_mode, crypt_volume_key_get_key(vk),
145
0
      crypt_volume_key_length(vk), false);
146
147
0
  if (r)
148
0
    log_dbg(ctx, "Userspace crypto wrapper cannot use %s-%s (%d).",
149
0
      cipher, cipher_mode, r);
150
151
  /* Fallback to old temporary dmcrypt device */
152
0
  if (r == -ENOTSUP || r == -ENOENT)
153
0
    return LUKS_endec_template(src, srcLength, cipher, cipher_mode,
154
0
             vk, sector, write_blockwise, O_RDWR, ctx);
155
156
0
  if (r) {
157
0
    _error_hint(ctx, device_path(device), cipher, cipher_mode,
158
0
          crypt_volume_key_length(vk) * 8);
159
0
    return r;
160
0
  }
161
162
0
  log_dbg(ctx, "Using userspace crypto wrapper to access keyslot area.");
163
164
0
  r = crypt_storage_encrypt(s, 0, srcLength, src);
165
0
  crypt_storage_destroy(s);
166
167
0
  if (r)
168
0
    return r;
169
170
0
  r = -EIO;
171
172
  /* Write buffer to device */
173
0
  if (device_is_locked(device))
174
0
    devfd = device_open_locked(ctx, device, O_RDWR);
175
0
  else
176
0
    devfd = device_open(ctx, device, O_RDWR);
177
0
  if (devfd < 0)
178
0
    goto out;
179
180
0
  if (write_lseek_blockwise(devfd, device_block_size(ctx, device),
181
0
          device_alignment(device), src, srcLength,
182
0
          sector * SECTOR_SIZE) < 0)
183
0
    goto out;
184
185
0
  r = 0;
186
0
out:
187
0
  device_sync(ctx, device);
188
0
  if (r)
189
0
    log_err(ctx, _("IO error while encrypting keyslot."));
190
191
0
  return r;
192
0
}
193
194
int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
195
            const char *cipher,
196
            const char *cipher_mode,
197
            struct volume_key *vk,
198
            unsigned int sector,
199
            struct crypt_device *ctx)
200
0
{
201
0
  struct device *device = crypt_metadata_device(ctx);
202
0
  struct crypt_storage *s;
203
0
  struct stat st;
204
0
  int devfd, r = 0;
205
206
  /* Only whole sector reads supported */
207
0
  if (MISALIGNED_512(dstLength))
208
0
    return -EINVAL;
209
210
0
  r = crypt_storage_init(&s, SECTOR_SIZE, cipher, cipher_mode, crypt_volume_key_get_key(vk),
211
0
      crypt_volume_key_length(vk), false);
212
213
0
  if (r)
214
0
    log_dbg(ctx, "Userspace crypto wrapper cannot use %s-%s (%d).",
215
0
      cipher, cipher_mode, r);
216
217
  /* Fallback to old temporary dmcrypt device */
218
0
  if (r == -ENOTSUP || r == -ENOENT)
219
0
    return LUKS_endec_template(dst, dstLength, cipher, cipher_mode,
220
0
             vk, sector, read_blockwise, O_RDONLY, ctx);
221
222
0
  if (r) {
223
0
    _error_hint(ctx, device_path(device), cipher, cipher_mode,
224
0
         crypt_volume_key_length(vk) * 8);
225
0
    return r;
226
0
  }
227
228
0
  log_dbg(ctx, "Using userspace crypto wrapper to access keyslot area.");
229
230
  /* Read buffer from device */
231
0
  if (device_is_locked(device))
232
0
    devfd = device_open_locked(ctx, device, O_RDONLY);
233
0
  else
234
0
    devfd = device_open(ctx, device, O_RDONLY);
235
0
  if (devfd < 0) {
236
0
    log_err(ctx, _("Cannot open device %s."), device_path(device));
237
0
    crypt_storage_destroy(s);
238
0
    return -EIO;
239
0
  }
240
241
0
  if (read_lseek_blockwise(devfd, device_block_size(ctx, device),
242
0
         device_alignment(device), dst, dstLength,
243
0
         sector * SECTOR_SIZE) < 0) {
244
0
    if (!fstat(devfd, &st) && (st.st_size < (off_t)dstLength))
245
0
      log_err(ctx, _("Device %s is too small."), device_path(device));
246
0
    else
247
0
      log_err(ctx, _("IO error while decrypting keyslot."));
248
249
0
    crypt_storage_destroy(s);
250
0
    return -EIO;
251
0
  }
252
253
  /* Decrypt buffer */
254
0
  r = crypt_storage_decrypt(s, 0, dstLength, dst);
255
0
  crypt_storage_destroy(s);
256
257
0
  return r;
258
0
}