2 * utils - miscellaneous device utilities for cryptsetup
4 * Copyright (C) 2004 Jana Saout <jana@saout.de>
5 * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2021 Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/resource.h>
29 #include <sys/utsname.h>
33 size_t crypt_getpagesize(void)
35 long r = sysconf(_SC_PAGESIZE);
36 return r <= 0 ? DEFAULT_MEM_ALIGNMENT : (size_t)r;
39 unsigned crypt_cpusonline(void)
41 long r = sysconf(_SC_NPROCESSORS_ONLN);
45 uint64_t crypt_getphysmemory_kb(void)
47 long pagesize, phys_pages;
48 uint64_t phys_memory_kb;
50 pagesize = sysconf(_SC_PAGESIZE);
51 phys_pages = sysconf(_SC_PHYS_PAGES);
53 if (pagesize < 0 || phys_pages < 0)
56 phys_memory_kb = pagesize / 1024;
57 phys_memory_kb *= phys_pages;
59 return phys_memory_kb;
63 #define DEFAULT_PROCESS_PRIORITY -18
66 static int _memlock_count = 0;
68 // return 1 if memory is locked
69 int crypt_memlock_inc(struct crypt_device *ctx)
71 if (!_memlock_count++) {
72 log_dbg(ctx, "Locking memory.");
73 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
74 log_dbg(ctx, "Cannot lock memory with mlockall.");
79 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
80 log_err(ctx, _("Cannot get process priority."));
82 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
83 log_dbg(ctx, "setpriority %d failed: %s",
84 DEFAULT_PROCESS_PRIORITY, strerror(errno));
86 return _memlock_count ? 1 : 0;
89 int crypt_memlock_dec(struct crypt_device *ctx)
91 if (_memlock_count && (!--_memlock_count)) {
92 log_dbg(ctx, "Unlocking memory.");
93 if (munlockall() == -1)
94 log_err(ctx, _("Cannot unlock memory."));
95 if (setpriority(PRIO_PROCESS, 0, _priority))
96 log_dbg(ctx, "setpriority %d failed: %s", _priority, strerror(errno));
98 return _memlock_count ? 1 : 0;
101 /* Keyfile processing */
104 * A simple call to lseek(3) might not be possible for some inputs (e.g.
105 * reading from a pipe), so this function instead reads of up to BUFSIZ bytes
106 * at a time until the specified number of bytes. It returns -1 on read error
107 * or when it reaches EOF before the requested number of bytes have been
110 static int keyfile_seek(int fd, uint64_t bytes)
117 r = lseek64(fd, bytes, SEEK_CUR);
120 if (r < 0 && errno != ESPIPE)
124 /* figure out how much to read */
125 next_read = bytes > sizeof(tmp) ? sizeof(tmp) : (size_t)bytes;
127 bytes_r = read(fd, tmp, next_read);
132 crypt_safe_memzero(tmp, sizeof(tmp));
144 crypt_safe_memzero(tmp, sizeof(tmp));
145 return bytes == 0 ? 0 : -1;
148 int crypt_keyfile_device_read(struct crypt_device *cd, const char *keyfile,
149 char **key, size_t *key_size_read,
150 uint64_t keyfile_offset, size_t key_size,
153 int fd, regular_file, char_to_read = 0, char_read = 0, unlimited_read = 0;
154 int r = -EINVAL, newline;
157 uint64_t file_read_size;
160 if (!key || !key_size_read)
166 fd = keyfile ? open(keyfile, O_RDONLY) : STDIN_FILENO;
168 log_err(cd, _("Failed to open key file."));
173 log_err(cd, _("Cannot read keyfile from a terminal."));
178 /* If not requested otherwise, we limit input to prevent memory exhaustion */
180 key_size = DEFAULT_KEYFILE_SIZE_MAXKB * 1024 + 1;
182 /* use 4k for buffer (page divisor but avoid huge pages) */
183 buflen = 4096 - sizeof(size_t); // sizeof(struct safe_allocation);
189 if (stat(keyfile, &st) < 0) {
190 log_err(cd, _("Failed to stat key file."));
193 if (S_ISREG(st.st_mode)) {
195 file_read_size = (uint64_t)st.st_size;
197 if (keyfile_offset > file_read_size) {
198 log_err(cd, _("Cannot seek to requested keyfile offset."));
201 file_read_size -= keyfile_offset;
203 /* known keyfile size, alloc it in one step */
204 if (file_read_size >= (uint64_t)key_size)
206 else if (file_read_size)
207 buflen = file_read_size;
211 pass = crypt_safe_alloc(buflen);
213 log_err(cd, _("Out of memory while reading passphrase."));
217 /* Discard keyfile_offset bytes on input */
218 if (keyfile_offset && keyfile_seek(fd, keyfile_offset) < 0) {
219 log_err(cd, _("Cannot seek to requested keyfile offset."));
223 for (i = 0, newline = 0; i < key_size; i += char_read) {
226 pass = crypt_safe_realloc(pass, buflen);
228 log_err(cd, _("Out of memory while reading passphrase."));
234 if (flags & CRYPT_KEYFILE_STOP_EOL) {
235 /* If we should stop on newline, we must read the input
236 * one character at the time. Otherwise we might end up
237 * having read some bytes after the newline, which we
238 * promised not to do.
242 /* char_to_read = min(key_size - i, buflen - i) */
243 char_to_read = key_size < buflen ?
244 key_size - i : buflen - i;
246 char_read = read_buffer(fd, &pass[i], char_to_read);
248 log_err(cd, _("Error reading passphrase."));
255 /* Stop on newline only if not requested read from keyfile */
256 if ((flags & CRYPT_KEYFILE_STOP_EOL) && pass[i] == '\n') {
263 /* Fail if piped input dies reading nothing */
264 if (!i && !regular_file && !newline) {
265 log_err(cd, _("Nothing to read on input."));
270 /* Fail if we exceeded internal default (no specified size) */
271 if (unlimited_read && i == key_size) {
272 log_err(cd, _("Maximum keyfile size exceeded."));
276 if (!unlimited_read && i != key_size) {
277 log_err(cd, _("Cannot read requested amount of data."));
285 if (fd != STDIN_FILENO)
289 crypt_safe_free(pass);
293 int crypt_keyfile_read(struct crypt_device *cd, const char *keyfile,
294 char **key, size_t *key_size_read,
295 size_t keyfile_offset, size_t keyfile_size_max,
298 return crypt_keyfile_device_read(cd, keyfile, key, key_size_read,
299 keyfile_offset, keyfile_size_max, flags);
302 int kernel_version(uint64_t *kversion)
305 uint16_t maj, min, patch, rel;
311 if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16 "-%" SCNu16,
312 &maj, &min, &patch, &rel) == 4)
314 else if (sscanf(uts.release, "%" SCNu16 ".%" SCNu16 ".%" SCNu16,
315 &maj, &min, &patch) == 3) {
321 *kversion = version(maj, min, patch, rel);
326 bool crypt_string_in(const char *str, char **list, size_t list_size)
330 for (i = 0; *list && i < list_size; i++, list++)
331 if (!strcmp(str, *list))
337 /* compare two strings (allows NULL values) */
338 int crypt_strcmp(const char *a, const char *b)