Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / utils_safe_memory.c
1 /*
2  * utils_safe_memory - safe memory helpers
3  *
4  * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2009-2023 Milan Broz
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include "libcryptsetup.h"
27
28 struct safe_allocation {
29         size_t size;
30         bool locked;
31         char data[0] __attribute__((aligned(8)));
32 };
33 #define OVERHEAD offsetof(struct safe_allocation, data)
34
35 /*
36  * Replacement for memset(s, 0, n) on stack that can be optimized out
37  * Also used in safe allocations for explicit memory wipe.
38  */
39 void crypt_safe_memzero(void *data, size_t size)
40 {
41         if (!data)
42                 return;
43
44 #ifdef HAVE_EXPLICIT_BZERO
45         explicit_bzero(data, size);
46 #else
47         volatile uint8_t *p = (volatile uint8_t *)data;
48
49         while(size--)
50                 *p++ = 0;
51 #endif
52 }
53
54 /* safe allocations */
55 void *crypt_safe_alloc(size_t size)
56 {
57         struct safe_allocation *alloc;
58
59         if (!size || size > (SIZE_MAX - OVERHEAD))
60                 return NULL;
61
62         alloc = malloc(size + OVERHEAD);
63         if (!alloc)
64                 return NULL;
65
66         crypt_safe_memzero(alloc, size + OVERHEAD);
67         alloc->size = size;
68
69         /* Ignore failure if it is over limit. */
70         if (!mlock(alloc, size + OVERHEAD))
71                 alloc->locked = true;
72
73         /* coverity[leaked_storage] */
74         return &alloc->data;
75 }
76
77 void crypt_safe_free(void *data)
78 {
79         struct safe_allocation *alloc;
80         volatile size_t *s;
81         void *p;
82
83         if (!data)
84                 return;
85
86         p = (char *)data - OVERHEAD;
87         alloc = (struct safe_allocation *)p;
88
89         crypt_safe_memzero(data, alloc->size);
90
91         if (alloc->locked) {
92                 munlock(alloc, alloc->size + OVERHEAD);
93                 alloc->locked = false;
94         }
95
96         s = (volatile size_t *)&alloc->size;
97         *s = 0x55aa55aa;
98         free(alloc);
99 }
100
101 void *crypt_safe_realloc(void *data, size_t size)
102 {
103         struct safe_allocation *alloc;
104         void *new_data;
105         void *p;
106
107         new_data = crypt_safe_alloc(size);
108
109         if (new_data && data) {
110
111                 p = (char *)data - OVERHEAD;
112                 alloc = (struct safe_allocation *)p;
113
114                 if (size > alloc->size)
115                         size = alloc->size;
116
117                 memcpy(new_data, data, size);
118         }
119
120         crypt_safe_free(data);
121         return new_data;
122 }