Enable ASLR feature
[platform/upstream/cryptsetup.git] / lib / random.c
1 /*
2  * cryptsetup kernel RNG access functions
3  *
4  * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <sys/select.h>
26
27 #include "libcryptsetup.h"
28 #include "internal.h"
29
30 static int random_initialised = 0;
31
32 #define URANDOM_DEVICE  "/dev/urandom"
33 static int urandom_fd = -1;
34
35 #define RANDOM_DEVICE   "/dev/random"
36 static int random_fd = -1;
37
38 /* Read random chunk - gathered data usually appears with this granularity */
39 #define RANDOM_DEVICE_CHUNK     8
40
41 /* Timeout to print warning if no random data (entropy) */
42 #define RANDOM_DEVICE_TIMEOUT   5
43
44 /* URANDOM_DEVICE access */
45 static int _get_urandom(struct crypt_device *ctx __attribute__((unused)),
46                         char *buf, size_t len)
47 {
48         int r;
49         size_t old_len = len;
50         char *old_buf = buf;
51
52         assert(urandom_fd != -1);
53
54         while(len) {
55                 r = read(urandom_fd, buf, len);
56                 if (r == -1 && errno != EINTR)
57                         return -EINVAL;
58                 if (r > 0) {
59                         len -= r;
60                         buf += r;
61                 }
62         }
63
64         assert(len == 0);
65         assert((size_t)(buf - old_buf) == old_len);
66
67         return 0;
68 }
69
70 static void _get_random_progress(struct crypt_device *ctx, int warn,
71                                  size_t expected_len, size_t read_len)
72 {
73         if (warn)
74                 log_std(ctx,
75                         _("System is out of entropy while generating volume key.\n"
76                           "Please move mouse or type some text in another window "
77                           "to gather some random events.\n"));
78
79         log_std(ctx, _("Generating key (%d%% done).\n"),
80                 (int)((expected_len - read_len) * 100 / expected_len));
81 }
82
83 /* RANDOM_DEVICE access */
84 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
85 {
86         int r, warn_once = 1;
87         size_t n, old_len = len;
88         char *old_buf = buf;
89         fd_set fds;
90         struct timeval tv;
91
92         assert(random_fd != -1);
93
94         while (len) {
95                 FD_ZERO(&fds);
96                 FD_SET(random_fd, &fds);
97
98                 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
99                 tv.tv_usec = 0;
100
101                 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
102                 if(r == -1)
103                         return -EINVAL;
104
105                 if(!r) {
106                         _get_random_progress(ctx, warn_once, old_len, len);
107                         warn_once = 0;
108                         continue;
109                 }
110
111                 do {
112                         n = RANDOM_DEVICE_CHUNK;
113                         if (len < RANDOM_DEVICE_CHUNK)
114                                 n = len;
115
116                         r = read(random_fd, buf, n);
117
118                         if (r == -1 && errno == EINTR) {
119                                 r = 0;
120                                 continue;
121                         }
122
123                         /* bogus read? */
124                         if(r > (int)n)
125                                 return -EINVAL;
126
127                         /* random device is opened with O_NONBLOCK, EAGAIN is expected */
128                         if (r == -1 && (errno != EAGAIN && errno != EWOULDBLOCK))
129                                 return -EINVAL;
130
131                         if (r > 0) {
132                                 len -= r;
133                                 buf += r;
134                         }
135                 } while (len && r > 0);
136         }
137
138         assert(len == 0);
139         assert((size_t)(buf - old_buf) == old_len);
140
141         if (!warn_once)
142                 _get_random_progress(ctx, 0, old_len, len);
143
144         return 0;
145 }
146 /* Initialisation of both RNG file descriptors is mandatory */
147 int crypt_random_init(struct crypt_device *ctx)
148 {
149         if (random_initialised)
150                 return 0;
151
152         /* Used for CRYPT_RND_NORMAL */
153         if(urandom_fd == -1)
154                 urandom_fd = open(URANDOM_DEVICE, O_RDONLY | O_CLOEXEC);
155         if(urandom_fd == -1)
156                 goto fail;
157
158         /* Used for CRYPT_RND_KEY */
159         if(random_fd == -1)
160                 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
161         if(random_fd == -1)
162                 goto fail;
163
164         if (crypt_fips_mode())
165                 log_verbose(ctx, _("Running in FIPS mode."));
166
167         random_initialised = 1;
168         return 0;
169 fail:
170         crypt_random_exit();
171         log_err(ctx, _("Fatal error during RNG initialisation."));
172         return -ENOSYS;
173 }
174
175 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
176 {
177         int status, rng_type;
178
179         switch(quality) {
180         case CRYPT_RND_NORMAL:
181                 status = _get_urandom(ctx, buf, len);
182                 break;
183         case CRYPT_RND_SALT:
184                 if (crypt_fips_mode())
185                         status = crypt_backend_rng(buf, len, quality, 1);
186                 else
187                         status = _get_urandom(ctx, buf, len);
188                 break;
189         case CRYPT_RND_KEY:
190                 if (crypt_fips_mode()) {
191                         status = crypt_backend_rng(buf, len, quality, 1);
192                         break;
193                 }
194                 rng_type = ctx ? crypt_get_rng_type(ctx) :
195                                  crypt_random_default_key_rng();
196                 switch (rng_type) {
197                 case CRYPT_RNG_URANDOM:
198                         status = _get_urandom(ctx, buf, len);
199                         break;
200                 case CRYPT_RNG_RANDOM:
201                         status = _get_random(ctx, buf, len);
202                         break;
203                 default:
204                         abort();
205                 }
206                 break;
207         default:
208                 log_err(ctx, _("Unknown RNG quality requested."));
209                 return -EINVAL;
210         }
211
212         if (status)
213                 log_err(ctx, _("Error reading from RNG."));
214
215         return status;
216 }
217
218 void crypt_random_exit(void)
219 {
220         random_initialised = 0;
221
222         if(random_fd != -1) {
223                 (void)close(random_fd);
224                 random_fd = -1;
225         }
226
227         if(urandom_fd != -1) {
228                 (void)close(urandom_fd);
229                 urandom_fd = -1;
230         }
231 }
232
233 int crypt_random_default_key_rng(void)
234 {
235         /* coverity[pointless_string_compare] */
236         if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
237                 return CRYPT_RNG_RANDOM;
238
239         /* coverity[pointless_string_compare] */
240         if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
241                 return CRYPT_RNG_URANDOM;
242
243         /* RNG misconfiguration is fatal */
244         abort();
245 }