Coverage Report

Created: 2025-11-25 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptsetup/lib/internal.h
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * libcryptsetup - cryptsetup library internal
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
#ifndef INTERNAL_H
12
#define INTERNAL_H
13
14
#include <stdint.h>
15
#include <stdarg.h>
16
#include <stdbool.h>
17
#include <stdlib.h>
18
#include <unistd.h>
19
#include <inttypes.h>
20
#include <fcntl.h>
21
#include <assert.h>
22
23
#include "nls.h"
24
#include "bitops.h"
25
#include "utils_blkid.h"
26
#include "utils_crypt.h"
27
#include "utils_loop.h"
28
#include "utils_dm.h"
29
#include "utils_keyring.h"
30
#include "utils_io.h"
31
#include "crypto_backend/crypto_backend.h"
32
#include "utils_storage_wrappers.h"
33
34
#include "libcryptsetup.h"
35
36
#include "libcryptsetup_macros.h"
37
#include "libcryptsetup_symver.h"
38
39
956k
#define LOG_MAX_LEN   4096
40
#define MAX_DM_DEPS   32
41
42
0
#define CRYPT_SUBDEV           "SUBDEV" /* prefix for sublayered devices underneath public crypt types */
43
0
#define CRYPT_LUKS2_HW_OPAL    "LUKS2-OPAL" /* dm uuid prefix used for any HW OPAL enabled LUKS2 device */
44
45
#ifndef O_CLOEXEC
46
#define O_CLOEXEC 0
47
#endif
48
49
struct crypt_device;
50
struct luks2_reencrypt;
51
struct volume_key;
52
53
typedef enum {
54
  KEY_QUALITY_KEY = 0,
55
  KEY_QUALITY_NORMAL,
56
  KEY_QUALITY_EMPTY
57
} key_quality_info;
58
59
struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key);
60
struct volume_key *crypt_alloc_volume_key_by_safe_alloc(void **safe_alloc);
61
struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, size_t keylength,
62
               key_quality_info quality);
63
void crypt_free_volume_key(struct volume_key *vk);
64
const char *crypt_volume_key_get_key(const struct volume_key *vk);
65
size_t crypt_volume_key_length(const struct volume_key *vk);
66
int crypt_volume_key_set_description(struct volume_key *key,
67
             const char *key_description, key_type_t keyring_key_type);
68
int crypt_volume_key_set_description_by_name(struct volume_key *vk, const char *key_name);
69
key_type_t crypt_volume_key_kernel_key_type(const struct volume_key *vk);
70
const char *crypt_volume_key_description(const struct volume_key *vk);
71
void crypt_volume_key_set_id(struct volume_key *vk, int id);
72
int crypt_volume_key_get_id(const struct volume_key *vk);
73
void crypt_volume_key_add_next(struct volume_key **vks, struct volume_key *vk);
74
struct volume_key *crypt_volume_key_next(struct volume_key *vk);
75
struct volume_key *crypt_volume_key_by_id(struct volume_key *vk, int id);
76
void crypt_volume_key_pass_safe_alloc(struct volume_key *vk, void **safe_alloc);
77
bool crypt_volume_key_is_set(const struct volume_key *vk);
78
bool crypt_volume_key_upload_kernel_key(struct volume_key *vk);
79
void crypt_volume_key_drop_uploaded_kernel_key(struct crypt_device *cd, struct volume_key *vk);
80
void crypt_volume_key_drop_kernel_key(struct crypt_device *cd, struct volume_key *vk);
81
82
struct crypt_pbkdf_type *crypt_get_pbkdf(struct crypt_device *cd);
83
int init_pbkdf_type(struct crypt_device *cd,
84
        const struct crypt_pbkdf_type *pbkdf,
85
        const char *dev_type);
86
int verify_pbkdf_params(struct crypt_device *cd,
87
      const struct crypt_pbkdf_type *pbkdf);
88
int crypt_benchmark_pbkdf_internal(struct crypt_device *cd,
89
           struct crypt_pbkdf_type *pbkdf,
90
           size_t volume_key_size);
91
const char *crypt_get_cipher_spec(struct crypt_device *cd);
92
uint32_t pbkdf_adjusted_phys_memory_kb(void);
93
94
/* Device backend */
95
struct device;
96
int device_alloc(struct crypt_device *cd, struct device **device, const char *path);
97
int device_alloc_no_check(struct device **device, const char *path);
98
void device_close(struct crypt_device *cd, struct device *device);
99
void device_free(struct crypt_device *cd, struct device *device);
100
const char *device_path(const struct device *device);
101
const char *device_block_path(const struct device *device);
102
void device_topology_alignment(struct crypt_device *cd,
103
             struct device *device,
104
             unsigned long *required_alignment, /* bytes */
105
             unsigned long *alignment_offset,   /* bytes */
106
             unsigned long default_alignment);
107
size_t device_block_size(struct crypt_device *cd, struct device *device);
108
int device_read_ahead(struct device *device, uint32_t *read_ahead);
109
int device_size(struct device *device, uint64_t *size);
110
int device_open(struct crypt_device *cd, struct device *device, int flags);
111
int device_open_excl(struct crypt_device *cd, struct device *device, int flags);
112
void device_release_excl(struct crypt_device *cd, struct device *device);
113
void device_disable_direct_io(struct device *device);
114
int device_is_identical(struct device *device1, struct device *device2);
115
int device_is_rotational(struct device *device);
116
int device_is_dax(struct device *device);
117
int device_is_zoned(struct device *device);
118
int device_is_nop_dif(struct device *device, uint32_t *tag_size);
119
size_t device_alignment(struct device *device);
120
int device_direct_io(const struct device *device);
121
int device_fallocate(struct device *device, uint64_t size);
122
void device_sync(struct crypt_device *cd, struct device *device);
123
int device_check_size(struct crypt_device *cd,
124
          struct device *device,
125
          uint64_t req_offset, int falloc);
126
void device_set_block_size(struct device *device, size_t size);
127
size_t device_optimal_encryption_sector_size(struct crypt_device *cd, struct device *device);
128
129
int device_open_locked(struct crypt_device *cd, struct device *device, int flags);
130
int device_read_lock(struct crypt_device *cd, struct device *device);
131
int device_write_lock(struct crypt_device *cd, struct device *device);
132
void device_read_unlock(struct crypt_device *cd, struct device *device);
133
void device_write_unlock(struct crypt_device *cd, struct device *device);
134
bool device_is_locked(struct device *device);
135
136
enum devcheck { DEV_OK = 0, DEV_EXCL = 1 };
137
int device_check_access(struct crypt_device *cd,
138
      struct device *device,
139
      enum devcheck device_check);
140
int device_block_adjust(struct crypt_device *cd,
141
      struct device *device,
142
      enum devcheck device_check,
143
      uint64_t device_offset,
144
      uint64_t *size,
145
      uint32_t *flags);
146
size_t size_round_up(size_t size, size_t block);
147
148
int create_or_reload_device(struct crypt_device *cd, const char *name,
149
         const char *type, struct crypt_dm_active_device *dmd);
150
151
int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
152
         const char *type, struct crypt_dm_active_device *dmd,
153
         struct crypt_dm_active_device *dmdi);
154
155
/* Receive backend devices from context helpers */
156
struct device *crypt_metadata_device(struct crypt_device *cd);
157
struct device *crypt_data_device(struct crypt_device *cd);
158
159
uint64_t crypt_get_metadata_size_bytes(struct crypt_device *cd);
160
uint64_t crypt_get_keyslots_size_bytes(struct crypt_device *cd);
161
uint64_t crypt_get_data_offset_sectors(struct crypt_device *cd);
162
int crypt_opal_supported(struct crypt_device *cd, struct device *opal_device);
163
164
int crypt_confirm(struct crypt_device *cd, const char *msg);
165
166
char *crypt_lookup_dev(const char *dev_id);
167
int crypt_dev_is_rotational(int major, int minor);
168
int crypt_dev_is_dax(int major, int minor);
169
int crypt_dev_is_zoned(int major, int minor);
170
int crypt_dev_is_nop_dif(int major, int minor, uint32_t *tag_size);
171
int crypt_dev_is_partition(const char *dev_path);
172
char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t size);
173
int crypt_dev_get_partition_number(const char *dev_path);
174
char *crypt_get_base_device(const char *dev_path);
175
uint64_t crypt_dev_partition_offset(const char *dev_path);
176
int lookup_by_disk_id(const char *dm_uuid);
177
int lookup_by_sysfs_uuid_field(const char *dm_uuid);
178
179
size_t crypt_getpagesize(void);
180
unsigned crypt_cpusonline(void);
181
uint64_t crypt_getphysmemory_kb(void);
182
uint64_t crypt_getphysmemoryfree_kb(void);
183
bool crypt_swapavailable(void);
184
185
int init_crypto(struct crypt_device *ctx);
186
187
315k
#define log_dbg(c, x...) crypt_logf(c, CRYPT_LOG_DEBUG, x)
188
0
#define log_std(c, x...) crypt_logf(c, CRYPT_LOG_NORMAL, x)
189
0
#define log_verbose(c, x...) crypt_logf(c, CRYPT_LOG_VERBOSE, x)
190
3.16k
#define log_err(c, x...) crypt_logf(c, CRYPT_LOG_ERROR, x)
191
192
int crypt_get_debug_level(void);
193
194
void crypt_process_priority(struct crypt_device *cd, int *priority, bool raise);
195
196
int crypt_metadata_locking_enabled(void);
197
198
int crypt_random_init(struct crypt_device *ctx);
199
int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
200
void crypt_random_exit(void);
201
int crypt_random_default_key_rng(void);
202
203
int crypt_plain_hash(struct crypt_device *cd,
204
         const char *hash_name,
205
         char *key, size_t key_size,
206
         const char *passphrase, size_t passphrase_size);
207
int PLAIN_activate(struct crypt_device *cd,
208
         const char *name,
209
         struct volume_key *vk,
210
         uint64_t size,
211
         uint32_t flags);
212
213
void *crypt_get_hdr(struct crypt_device *cd, const char *type);
214
void crypt_set_luks2_reencrypt(struct crypt_device *cd, struct luks2_reencrypt *rh);
215
struct luks2_reencrypt *crypt_get_luks2_reencrypt(struct crypt_device *cd);
216
217
int onlyLUKS2(struct crypt_device *cd);
218
int onlyLUKS2reencrypt(struct crypt_device *cd);
219
220
int crypt_wipe_device(struct crypt_device *cd,
221
  struct device *device,
222
  crypt_wipe_pattern pattern,
223
  uint64_t offset,
224
  uint64_t length,
225
  size_t wipe_block_size,
226
  int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
227
  void *usrptr);
228
229
/* Internal integrity helpers */
230
const char *crypt_get_integrity(struct crypt_device *cd);
231
int crypt_get_integrity_key_size(struct crypt_device *cd, bool dm_compat);
232
int crypt_get_integrity_tag_size(struct crypt_device *cd);
233
234
int crypt_key_in_keyring(struct crypt_device *cd);
235
void crypt_set_key_in_keyring(struct crypt_device *cd, unsigned key_in_keyring);
236
int crypt_volume_key_load_in_keyring(struct crypt_device *cd, struct volume_key *vk);
237
int crypt_keyring_get_user_key(struct crypt_device *cd,
238
    const char *key_description,
239
    char **key,
240
    size_t *key_size);
241
int crypt_keyring_get_key_by_name(struct crypt_device *cd,
242
    const char *key_description,
243
    char **key,
244
    size_t *key_size);
245
246
int crypt_keyring_get_keysize_by_name(struct crypt_device *cd,
247
    const char *key_description,
248
    size_t *r_key_size);
249
250
int crypt_use_keyring_for_vk(struct crypt_device *cd);
251
void crypt_unlink_key_from_thread_keyring(struct crypt_device *cd,
252
    key_serial_t key_id);
253
void crypt_unlink_key_by_description_from_thread_keyring(struct crypt_device *cd,
254
    const char *key_description,
255
    key_type_t ktype);
256
void crypt_drop_uploaded_keyring_key(struct crypt_device *cd, struct volume_key *vks);
257
258
static inline uint64_t compact_version(uint16_t major, uint16_t minor, uint16_t patch, uint16_t release)
259
0
{
260
0
  return (uint64_t)release | ((uint64_t)patch << 16) | ((uint64_t)minor << 32) | ((uint64_t)major << 48);
261
0
}
Unexecuted instantiation: setup.c:compact_version
Unexecuted instantiation: utils.c:compact_version
Unexecuted instantiation: utils_devpath.c:compact_version
Unexecuted instantiation: utils_wipe.c:compact_version
Unexecuted instantiation: utils_device.c:compact_version
Unexecuted instantiation: utils_device_locking.c:compact_version
Unexecuted instantiation: utils_pbkdf.c:compact_version
Unexecuted instantiation: utils_safe_memory.c:compact_version
Unexecuted instantiation: libdevmapper.c:compact_version
Unexecuted instantiation: volumekey.c:compact_version
Unexecuted instantiation: random.c:compact_version
Unexecuted instantiation: crypt_plain.c:compact_version
Unexecuted instantiation: integrity.c:compact_version
Unexecuted instantiation: loopaes.c:compact_version
Unexecuted instantiation: tcrypt.c:compact_version
Unexecuted instantiation: keyslot_context.c:compact_version
Unexecuted instantiation: keymanage.c:compact_version
Unexecuted instantiation: verity_hash.c:compact_version
Unexecuted instantiation: verity_fec.c:compact_version
Unexecuted instantiation: verity.c:compact_version
Unexecuted instantiation: luks2_disk_metadata.c:compact_version
Unexecuted instantiation: luks2_json_format.c:compact_version
Unexecuted instantiation: luks2_json_metadata.c:compact_version
Unexecuted instantiation: luks2_luks1_convert.c:compact_version
Unexecuted instantiation: luks2_digest.c:compact_version
Unexecuted instantiation: luks2_digest_pbkdf2.c:compact_version
Unexecuted instantiation: luks2_keyslot.c:compact_version
Unexecuted instantiation: luks2_keyslot_luks2.c:compact_version
Unexecuted instantiation: luks2_keyslot_reenc.c:compact_version
Unexecuted instantiation: luks2_reencrypt.c:compact_version
Unexecuted instantiation: luks2_reencrypt_digest.c:compact_version
Unexecuted instantiation: luks2_segment.c:compact_version
Unexecuted instantiation: luks2_token_keyring.c:compact_version
Unexecuted instantiation: luks2_token.c:compact_version
Unexecuted instantiation: hw_opal.c:compact_version
Unexecuted instantiation: bitlk.c:compact_version
Unexecuted instantiation: fvault2.c:compact_version
Unexecuted instantiation: utils_benchmark.c:compact_version
Unexecuted instantiation: utils_storage_wrappers.c:compact_version
Unexecuted instantiation: af.c:compact_version
Unexecuted instantiation: keyencryption.c:compact_version
262
263
int kernel_version(uint64_t *kversion);
264
265
int crypt_serialize_lock(struct crypt_device *cd);
266
void crypt_serialize_unlock(struct crypt_device *cd);
267
268
bool crypt_string_in(const char *str, char **list, size_t list_size);
269
int crypt_strcmp(const char *a, const char *b);
270
int crypt_compare_dm_devices(struct crypt_device *cd,
271
             const struct crypt_dm_active_device *src,
272
             const struct crypt_dm_active_device *tgt);
273
141
static inline void *crypt_zalloc(size_t size) { return calloc(1, size); }
Unexecuted instantiation: setup.c:crypt_zalloc
Unexecuted instantiation: utils.c:crypt_zalloc
Unexecuted instantiation: utils_devpath.c:crypt_zalloc
Unexecuted instantiation: utils_wipe.c:crypt_zalloc
Unexecuted instantiation: utils_device.c:crypt_zalloc
Unexecuted instantiation: utils_device_locking.c:crypt_zalloc
Unexecuted instantiation: utils_pbkdf.c:crypt_zalloc
Unexecuted instantiation: utils_safe_memory.c:crypt_zalloc
Unexecuted instantiation: libdevmapper.c:crypt_zalloc
volumekey.c:crypt_zalloc
Line
Count
Source
273
141
static inline void *crypt_zalloc(size_t size) { return calloc(1, size); }
Unexecuted instantiation: random.c:crypt_zalloc
Unexecuted instantiation: crypt_plain.c:crypt_zalloc
Unexecuted instantiation: integrity.c:crypt_zalloc
Unexecuted instantiation: loopaes.c:crypt_zalloc
Unexecuted instantiation: tcrypt.c:crypt_zalloc
Unexecuted instantiation: keyslot_context.c:crypt_zalloc
Unexecuted instantiation: keymanage.c:crypt_zalloc
Unexecuted instantiation: verity_hash.c:crypt_zalloc
Unexecuted instantiation: verity_fec.c:crypt_zalloc
Unexecuted instantiation: verity.c:crypt_zalloc
Unexecuted instantiation: luks2_disk_metadata.c:crypt_zalloc
Unexecuted instantiation: luks2_json_format.c:crypt_zalloc
Unexecuted instantiation: luks2_json_metadata.c:crypt_zalloc
Unexecuted instantiation: luks2_luks1_convert.c:crypt_zalloc
Unexecuted instantiation: luks2_digest.c:crypt_zalloc
Unexecuted instantiation: luks2_digest_pbkdf2.c:crypt_zalloc
Unexecuted instantiation: luks2_keyslot.c:crypt_zalloc
Unexecuted instantiation: luks2_keyslot_luks2.c:crypt_zalloc
Unexecuted instantiation: luks2_keyslot_reenc.c:crypt_zalloc
Unexecuted instantiation: luks2_reencrypt.c:crypt_zalloc
Unexecuted instantiation: luks2_reencrypt_digest.c:crypt_zalloc
Unexecuted instantiation: luks2_segment.c:crypt_zalloc
Unexecuted instantiation: luks2_token_keyring.c:crypt_zalloc
Unexecuted instantiation: luks2_token.c:crypt_zalloc
Unexecuted instantiation: hw_opal.c:crypt_zalloc
Unexecuted instantiation: bitlk.c:crypt_zalloc
Unexecuted instantiation: fvault2.c:crypt_zalloc
Unexecuted instantiation: utils_benchmark.c:crypt_zalloc
Unexecuted instantiation: utils_storage_wrappers.c:crypt_zalloc
Unexecuted instantiation: af.c:crypt_zalloc
Unexecuted instantiation: keyencryption.c:crypt_zalloc
274
275
static inline bool uint64_mult_overflow(uint64_t *u, uint64_t b, size_t size)
276
175
{
277
175
  *u = (uint64_t)b * size;
278
175
  if (size == 0)
279
2
    return true;
280
173
  if ((uint64_t)(*u / size) != b)
281
3
    return true;
282
170
  return false;
283
173
}
Unexecuted instantiation: setup.c:uint64_mult_overflow
Unexecuted instantiation: utils.c:uint64_mult_overflow
Unexecuted instantiation: utils_devpath.c:uint64_mult_overflow
Unexecuted instantiation: utils_wipe.c:uint64_mult_overflow
Unexecuted instantiation: utils_device.c:uint64_mult_overflow
Unexecuted instantiation: utils_device_locking.c:uint64_mult_overflow
Unexecuted instantiation: utils_pbkdf.c:uint64_mult_overflow
Unexecuted instantiation: utils_safe_memory.c:uint64_mult_overflow
Unexecuted instantiation: libdevmapper.c:uint64_mult_overflow
Unexecuted instantiation: volumekey.c:uint64_mult_overflow
Unexecuted instantiation: random.c:uint64_mult_overflow
Unexecuted instantiation: crypt_plain.c:uint64_mult_overflow
Unexecuted instantiation: integrity.c:uint64_mult_overflow
Unexecuted instantiation: loopaes.c:uint64_mult_overflow
Unexecuted instantiation: tcrypt.c:uint64_mult_overflow
Unexecuted instantiation: keyslot_context.c:uint64_mult_overflow
Unexecuted instantiation: keymanage.c:uint64_mult_overflow
Unexecuted instantiation: verity_hash.c:uint64_mult_overflow
Unexecuted instantiation: verity_fec.c:uint64_mult_overflow
Unexecuted instantiation: verity.c:uint64_mult_overflow
Unexecuted instantiation: luks2_disk_metadata.c:uint64_mult_overflow
Unexecuted instantiation: luks2_json_format.c:uint64_mult_overflow
Unexecuted instantiation: luks2_json_metadata.c:uint64_mult_overflow
Unexecuted instantiation: luks2_luks1_convert.c:uint64_mult_overflow
Unexecuted instantiation: luks2_digest.c:uint64_mult_overflow
Unexecuted instantiation: luks2_digest_pbkdf2.c:uint64_mult_overflow
Unexecuted instantiation: luks2_keyslot.c:uint64_mult_overflow
Unexecuted instantiation: luks2_keyslot_luks2.c:uint64_mult_overflow
Unexecuted instantiation: luks2_keyslot_reenc.c:uint64_mult_overflow
Unexecuted instantiation: luks2_reencrypt.c:uint64_mult_overflow
Unexecuted instantiation: luks2_reencrypt_digest.c:uint64_mult_overflow
Unexecuted instantiation: luks2_segment.c:uint64_mult_overflow
Unexecuted instantiation: luks2_token_keyring.c:uint64_mult_overflow
Unexecuted instantiation: luks2_token.c:uint64_mult_overflow
Unexecuted instantiation: hw_opal.c:uint64_mult_overflow
Unexecuted instantiation: bitlk.c:uint64_mult_overflow
fvault2.c:uint64_mult_overflow
Line
Count
Source
276
175
{
277
175
  *u = (uint64_t)b * size;
278
175
  if (size == 0)
279
2
    return true;
280
173
  if ((uint64_t)(*u / size) != b)
281
3
    return true;
282
170
  return false;
283
173
}
Unexecuted instantiation: utils_benchmark.c:uint64_mult_overflow
Unexecuted instantiation: utils_storage_wrappers.c:uint64_mult_overflow
Unexecuted instantiation: af.c:uint64_mult_overflow
Unexecuted instantiation: keyencryption.c:uint64_mult_overflow
284
285
141
#define KEY_NOT_VERIFIED -2
286
#define KEY_EXTERNAL_VERIFICATION -1
287
0
#define KEY_VERIFIED 0
288
289
size_t crypt_safe_alloc_size(const void *data);
290
291
#endif /* INTERNAL_H */