61370068189125fadb0e4b4a3926e0058f6695e6
[platform/upstream/cryptsetup.git] / lib / utils_safe_memory.c
1 /*
2  * utils_safe_memory - safe memory helpers
3  *
4  * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2009-2021 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 <string.h>
24 #include "libcryptsetup.h"
25
26 struct safe_allocation {
27         size_t  size;
28         char    data[0];
29 };
30
31 /*
32  * Replacement for memset(s, 0, n) on stack that can be optimized out
33  * Also used in safe allocations for explicit memory wipe.
34  */
35 void crypt_safe_memzero(void *data, size_t size)
36 {
37 #ifdef HAVE_EXPLICIT_BZERO
38         explicit_bzero(data, size);
39 #else
40         volatile uint8_t *p = (volatile uint8_t *)data;
41
42         while(size--)
43                 *p++ = 0;
44 #endif
45 }
46
47 /* safe allocations */
48 void *crypt_safe_alloc(size_t size)
49 {
50         struct safe_allocation *alloc;
51
52         if (!size || size > (SIZE_MAX - offsetof(struct safe_allocation, data)))
53                 return NULL;
54
55         alloc = malloc(size + offsetof(struct safe_allocation, data));
56         if (!alloc)
57                 return NULL;
58
59         alloc->size = size;
60         crypt_safe_memzero(&alloc->data, size);
61
62         /* coverity[leaked_storage] */
63         return &alloc->data;
64 }
65
66 void crypt_safe_free(void *data)
67 {
68         struct safe_allocation *alloc;
69         volatile size_t *s;
70
71         if (!data)
72                 return;
73
74         alloc = (struct safe_allocation *)
75                 ((char *)data - offsetof(struct safe_allocation, data));
76
77         crypt_safe_memzero(data, alloc->size);
78
79         s = (volatile size_t *)&alloc->size;
80         *s = 0x55aa55aa;
81         free(alloc);
82 }
83
84 void *crypt_safe_realloc(void *data, size_t size)
85 {
86         struct safe_allocation *alloc;
87         void *new_data;
88
89         new_data = crypt_safe_alloc(size);
90
91         if (new_data && data) {
92
93                 alloc = (struct safe_allocation *)
94                         ((char *)data - offsetof(struct safe_allocation, data));
95
96                 if (size > alloc->size)
97                         size = alloc->size;
98
99                 memcpy(new_data, data, size);
100         }
101
102         crypt_safe_free(data);
103         return new_data;
104 }