Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptsetup/lib/crypto_backend/memutils.c
Line
Count
Source
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/*
3
 * Safe memory utilities
4
 *
5
 * Copyright (C) 2024-2025 Milan Broz
6
 */
7
8
#include "crypto_backend_internal.h"
9
10
#define ATTR_NOINLINE __attribute__ ((noinline))
11
#define ATTR_ZERO_REGS
12
#if HAVE_ATTRIBUTE_ZEROCALLUSEDREGS
13
#    undef ATTR_ZERO_REGS
14
#    define ATTR_ZERO_REGS __attribute__ ((zero_call_used_regs("used")))
15
#endif
16
17
/* Workaround for https://github.com/google/sanitizers/issues/1507 */
18
#if defined __has_feature
19
# if __has_feature (memory_sanitizer)
20
#   undef HAVE_EXPLICIT_BZERO
21
# endif
22
#endif
23
24
/* Memzero helper (memset on stack can be optimized out) */
25
ATTR_NOINLINE ATTR_ZERO_REGS
26
void crypt_backend_memzero(void *s, size_t n)
27
61.2k
{
28
61.2k
#if HAVE_EXPLICIT_BZERO
29
61.2k
  explicit_bzero(s, n);
30
#else
31
  volatile uint8_t *p = (volatile uint8_t *)s;
32
  while(n--) *p++ = 0;
33
#endif
34
61.2k
}
35
36
/* Memcpy helper to avoid spilling sensitive data through additional registers */
37
ATTR_NOINLINE ATTR_ZERO_REGS
38
void *crypt_backend_memcpy(void *dst, const void *src, size_t n)
39
18.2k
{
40
18.2k
  volatile uint8_t *d = (volatile uint8_t *)dst;
41
18.2k
  const volatile uint8_t *s = (const volatile uint8_t *)src;
42
43
709k
  while(n--) *d++ = *s++;
44
45
18.2k
  return dst;
46
18.2k
}
47
48
/* Internal implementation for constant time memory comparison */
49
ATTR_NOINLINE ATTR_ZERO_REGS
50
int crypt_internal_memeq(const void *m1, const void *m2, size_t n)
51
0
{
52
0
  const unsigned char *_m1 = (const unsigned char *) m1;
53
0
  const unsigned char *_m2 = (const unsigned char *) m2;
54
0
  unsigned char result = 0;
55
0
  size_t i;
56
57
0
  for (i = 0; i < n; i++)
58
0
    result |= _m1[i] ^ _m2[i];
59
60
0
  return result;
61
0
}