8c1fb5cb9d76bfde93e7445be5476a0dc0f3a0e6
[platform/upstream/cryptsetup.git] / lib / utils_safe_memory.c
1 /*
2  * utils_safe_memory - safe memory helpers
3  *
4  * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2009-2020 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
70         if (!data)
71                 return;
72
73         alloc = (struct safe_allocation *)
74                 ((char *)data - offsetof(struct safe_allocation, data));
75
76         crypt_safe_memzero(data, alloc->size);
77
78         alloc->size = 0x55aa55aa;
79         free(alloc);
80 }
81
82 void *crypt_safe_realloc(void *data, size_t size)
83 {
84         struct safe_allocation *alloc;
85         void *new_data;
86
87         new_data = crypt_safe_alloc(size);
88
89         if (new_data && data) {
90
91                 alloc = (struct safe_allocation *)
92                         ((char *)data - offsetof(struct safe_allocation, data));
93
94                 if (size > alloc->size)
95                         size = alloc->size;
96
97                 memcpy(new_data, data, size);
98         }
99
100         crypt_safe_free(data);
101         return new_data;
102 }