Coverage Report

Created: 2026-06-08 06:07

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