2 * utils - miscellaneous device utilities for cryptsetup
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2011, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
31 #include <sys/types.h>
33 #include <sys/ioctl.h>
36 #include <sys/resource.h>
38 #include "libcryptsetup.h"
41 static char *error=NULL;
43 __attribute__((format(printf, 1, 0)))
44 void set_error_va(const char *fmt, va_list va)
55 r = vasprintf(&error, fmt, va);
62 if (r && error[r - 1] == '\n')
66 __attribute__((format(printf, 1, 2)))
67 void set_error(const char *fmt, ...)
72 set_error_va(fmt, va);
76 const char *get_error(void)
81 static int get_alignment(int fd)
83 int alignment = DEFAULT_MEM_ALIGNMENT;
85 #ifdef _PC_REC_XFER_ALIGN
86 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
88 alignment = DEFAULT_MEM_ALIGNMENT;
93 static void *aligned_malloc(void **base, int size, int alignment)
95 #ifdef HAVE_POSIX_MEMALIGN
96 return posix_memalign(base, alignment, size) ? NULL : *base;
98 /* Credits go to Michal's padlock patches for this alignment code */
101 ptr = malloc(size + alignment);
102 if(ptr == NULL) return NULL;
105 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
106 ptr += alignment - ((long)(ptr) & (alignment - 1));
111 static int sector_size(int fd)
114 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
120 int sector_size_for_device(const char *device)
122 int fd = open(device, O_RDONLY);
131 ssize_t write_blockwise(int fd, void *orig_buf, size_t count)
133 void *hangover_buf, *hangover_buf_base = NULL;
134 void *buf, *buf_base = NULL;
135 int r, hangover, solid, bsize, alignment;
138 if ((bsize = sector_size(fd)) < 0)
141 hangover = count % bsize;
142 solid = count - hangover;
143 alignment = get_alignment(fd);
145 if ((long)orig_buf & (alignment - 1)) {
146 buf = aligned_malloc(&buf_base, count, alignment);
149 memcpy(buf, orig_buf, count);
153 r = write(fd, buf, solid);
154 if (r < 0 || r != solid)
158 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
162 r = read(fd, hangover_buf, bsize);
163 if (r < 0 || r != bsize)
166 r = lseek(fd, -bsize, SEEK_CUR);
169 memcpy(hangover_buf, (char*)buf + solid, hangover);
171 r = write(fd, hangover_buf, bsize);
172 if (r < 0 || r != bsize)
177 free(hangover_buf_base);
183 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
184 void *hangover_buf, *hangover_buf_base = NULL;
185 void *buf, *buf_base = NULL;
186 int r, hangover, solid, bsize, alignment;
189 if ((bsize = sector_size(fd)) < 0)
192 hangover = count % bsize;
193 solid = count - hangover;
194 alignment = get_alignment(fd);
196 if ((long)orig_buf & (alignment - 1)) {
197 buf = aligned_malloc(&buf_base, count, alignment);
203 r = read(fd, buf, solid);
204 if(r < 0 || r != solid)
208 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
211 r = read(fd, hangover_buf, bsize);
212 if (r < 0 || r != bsize)
215 memcpy((char *)buf + solid, hangover_buf, hangover);
219 free(hangover_buf_base);
220 if (buf != orig_buf) {
221 memcpy(orig_buf, buf, count);
228 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
229 * but we also need a function to deal with short writes at the start. But this information
230 * is implicitly included in the read/write offset, which can not be set to non-aligned
231 * boundaries. Hence, we combine llseek with write.
233 ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset) {
235 void *frontPadBuf_base = NULL;
236 int r, bsize, frontHang;
237 size_t innerCount = 0;
240 if ((bsize = sector_size(fd)) < 0)
243 frontHang = offset % bsize;
245 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
249 frontPadBuf = aligned_malloc(&frontPadBuf_base,
250 bsize, get_alignment(fd));
254 r = read(fd, frontPadBuf, bsize);
255 if (r < 0 || r != bsize)
258 innerCount = bsize - frontHang;
259 if (innerCount > count)
262 memcpy(frontPadBuf + frontHang, buf, innerCount);
264 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
267 r = write(fd, frontPadBuf, bsize);
268 if (r < 0 || r != bsize)
275 ret = count ? write_blockwise(fd, buf, count) : 0;
279 free(frontPadBuf_base);
284 int device_ready(struct crypt_device *cd, const char *device, int mode)
291 if(stat(device, &st) < 0) {
292 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
296 if (!S_ISBLK(st.st_mode))
299 log_dbg("Trying to open and read device %s.", device);
300 devfd = open(device, mode | O_DIRECT | O_SYNC);
302 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
303 (mode & O_EXCL) ? _("exclusive ") : "",
304 (mode & O_RDWR) ? _("writable") : _("read-only"));
308 /* Try to read first sector */
309 s = read_blockwise(devfd, buf, sizeof(buf));
310 if (s < 0 || s != sizeof(buf)) {
311 log_verbose(cd, _("Cannot read device %s.\n"), device);
315 memset(buf, 0, sizeof(buf));
321 int get_device_infos(const char *device,
327 unsigned long size_small;
334 if (stat(device, &st) < 0)
337 /* never wipe header on mounted device */
338 if (open_exclusive && S_ISBLK(st.st_mode))
341 /* Try to open read-write to check whether it is a read-only device */
342 fd = open(device, O_RDWR | flags);
343 if (fd == -1 && errno == EROFS) {
345 fd = open(device, O_RDONLY | flags);
348 if (fd == -1 && open_exclusive && errno == EBUSY)
354 /* If the device can be opened read-write, i.e. readonly is still 0, then
355 * check whether BKROGET says that it is read-only. E.g. read-only loop
356 * devices may be openend read-write but are read-only according to BLKROGET
358 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
361 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
362 *size >>= SECTOR_SHIFT;
367 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
368 *size = (uint64_t)size_small;
379 int device_check_and_adjust(struct crypt_device *cd,
386 int r, real_readonly;
392 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
395 log_err(cd, _("Cannot use device %s which is in use "
396 "(already mapped or mounted).\n"),
399 log_err(cd, _("Cannot get info about device %s.\n"),
407 log_err(cd, _("Device %s has zero size.\n"), device);
410 if (*size < *offset) {
411 log_err(cd, _("Device %s is too small.\n"), device);
420 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
421 *size, *read_only ? "RO" : "RW", *offset);
425 int wipe_device_header(const char *device, int sectors)
429 int size = sectors * SECTOR_SIZE;
432 int flags = O_RDWR | O_DIRECT | O_SYNC;
434 if (stat(device, &st) < 0)
437 /* never wipe header on mounted device */
438 if (S_ISBLK(st.st_mode))
441 devfd = open(device, flags);
443 return errno == EBUSY ? -EBUSY : -EINVAL;
445 buffer = malloc(size);
450 memset(buffer, 0, size);
452 r = write_blockwise(devfd, buffer, size) < size ? -EIO : 0;
461 #define DEFAULT_PROCESS_PRIORITY -18
463 static int _priority;
464 static int _memlock_count = 0;
466 // return 1 if memory is locked
467 int crypt_memlock_inc(struct crypt_device *ctx)
469 if (!_memlock_count++) {
470 log_dbg("Locking memory.");
471 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
472 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
477 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
478 log_err(ctx, _("Cannot get process priority.\n"));
480 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
481 log_err(ctx, _("setpriority %d failed: %s\n"),
482 DEFAULT_PROCESS_PRIORITY, strerror(errno));
484 return _memlock_count ? 1 : 0;
487 int crypt_memlock_dec(struct crypt_device *ctx)
489 if (_memlock_count && (!--_memlock_count)) {
490 log_dbg("Unlocking memory.");
491 if (munlockall() == -1)
492 log_err(ctx, _("Cannot unlock memory.\n"));
493 if (setpriority(PRIO_PROCESS, 0, _priority))
494 log_err(ctx, _("setpriority %d failed: %s\n"), _priority, strerror(errno));
496 return _memlock_count ? 1 : 0;
499 /* DEVICE TOPOLOGY */
501 /* block device topology ioctls, introduced in 2.6.32 */
503 #define BLKIOMIN _IO(0x12,120)
504 #define BLKIOOPT _IO(0x12,121)
505 #define BLKALIGNOFF _IO(0x12,122)
508 void get_topology_alignment(const char *device,
509 unsigned long *required_alignment, /* bytes */
510 unsigned long *alignment_offset, /* bytes */
511 unsigned long default_alignment)
513 int dev_alignment_offset = 0;
514 unsigned int min_io_size = 0, opt_io_size = 0;
515 unsigned long temp_alignment = 0;
518 *required_alignment = default_alignment;
519 *alignment_offset = 0;
521 fd = open(device, O_RDONLY);
525 /* minimum io size */
526 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
527 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
528 device, default_alignment);
532 /* optimal io size */
533 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
534 opt_io_size = min_io_size;
536 /* alignment offset, bogus -1 means misaligned/unknown */
537 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
538 dev_alignment_offset = 0;
539 *alignment_offset = (unsigned long)dev_alignment_offset;
541 temp_alignment = (unsigned long)min_io_size;
543 if (temp_alignment < (unsigned long)opt_io_size)
544 temp_alignment = (unsigned long)opt_io_size;
546 /* If calculated alignment is multiple of default, keep default */
547 if (temp_alignment && (default_alignment % temp_alignment))
548 *required_alignment = temp_alignment;
550 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
551 min_io_size, opt_io_size, *alignment_offset, *required_alignment);