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, const 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);
151 buf = (void *)orig_buf;
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) goto out;
165 r = lseek(fd, -bsize, SEEK_CUR);
168 memcpy(hangover_buf, buf + solid, hangover);
170 r = write(fd, hangover_buf, bsize);
171 if(r < 0 || r != bsize) goto out;
172 free(hangover_buf_base);
181 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
182 void *hangover_buf, *hangover_buf_base;
183 void *buf, *buf_base = NULL;
184 int r, hangover, solid, bsize, alignment;
187 if ((bsize = sector_size(fd)) < 0)
190 hangover = count % bsize;
191 solid = count - hangover;
192 alignment = get_alignment(fd);
194 if ((long)orig_buf & (alignment - 1)) {
195 buf = aligned_malloc(&buf_base, count, alignment);
201 r = read(fd, buf, solid);
202 if(r < 0 || r != solid)
206 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
209 r = read(fd, hangover_buf, bsize);
210 if (r < 0 || r != bsize)
213 memcpy(buf + solid, hangover_buf, hangover);
214 free(hangover_buf_base);
218 if (buf != orig_buf) {
219 memcpy(orig_buf, buf, count);
226 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
227 * but we also need a function to deal with short writes at the start. But this information
228 * is implicitly included in the read/write offset, which can not be set to non-aligned
229 * boundaries. Hence, we combine llseek with write.
232 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
233 int bsize = sector_size(fd);
234 const char *orig_buf = buf;
235 char frontPadBuf[bsize];
236 int frontHang = offset % bsize;
238 int innerCount = count < bsize ? count : bsize;
243 lseek(fd, offset - frontHang, SEEK_SET);
245 r = read(fd,frontPadBuf,bsize);
248 memcpy(frontPadBuf+frontHang, buf, innerCount);
250 lseek(fd, offset - frontHang, SEEK_SET);
251 r = write(fd,frontPadBuf,bsize);
257 if(count <= 0) return buf - orig_buf;
259 return write_blockwise(fd, buf, count) + innerCount;
262 int device_ready(struct crypt_device *cd, const char *device, int mode)
269 if(stat(device, &st) < 0) {
270 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
274 if (!S_ISBLK(st.st_mode))
277 log_dbg("Trying to open and read device %s.", device);
278 devfd = open(device, mode | O_DIRECT | O_SYNC);
280 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
281 (mode & O_EXCL) ? _("exclusive ") : "",
282 (mode & O_RDWR) ? _("writable") : _("read-only"));
286 /* Try to read first sector */
287 s = read_blockwise(devfd, buf, sizeof(buf));
288 if (s < 0 || s != sizeof(buf)) {
289 log_verbose(cd, _("Cannot read device %s.\n"), device);
293 memset(buf, 0, sizeof(buf));
299 int get_device_infos(const char *device,
305 unsigned long size_small;
312 if (stat(device, &st) < 0)
315 /* never wipe header on mounted device */
316 if (open_exclusive && S_ISBLK(st.st_mode))
319 /* Try to open read-write to check whether it is a read-only device */
320 fd = open(device, O_RDWR | flags);
321 if (fd == -1 && errno == EROFS) {
323 fd = open(device, O_RDONLY | flags);
326 if (fd == -1 && open_exclusive && errno == EBUSY)
333 /* If the device can be opened read-write, i.e. readonly is still 0, then
334 * check whether BKROGET says that it is read-only. E.g. read-only loop
335 * devices may be openend read-write but are read-only according to BLKROGET
337 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
340 #error BLKROGET not available
344 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
345 *size >>= SECTOR_SHIFT;
352 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
353 *size = (uint64_t)size_small;
359 # error Need at least the BLKGETSIZE ioctl!
367 int device_check_and_adjust(struct crypt_device *cd,
374 int r, real_readonly;
380 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
383 log_err(cd, _("Cannot use device %s which is in use "
384 "(already mapped or mounted).\n"),
387 log_err(cd, _("Cannot get info about device %s.\n"),
395 log_err(cd, _("Device %s has zero size.\n"), device);
398 if (*size < *offset) {
399 log_err(cd, _("Device %s is too small.\n"), device);
408 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
409 *size, *read_only ? "RO" : "RW", *offset);
413 int wipe_device_header(const char *device, int sectors)
417 int size = sectors * SECTOR_SIZE;
420 int flags = O_RDWR | O_DIRECT | O_SYNC;
422 if (stat(device, &st) < 0)
425 /* never wipe header on mounted device */
426 if (S_ISBLK(st.st_mode))
429 devfd = open(device, flags);
431 return errno == EBUSY ? -EBUSY : -EINVAL;
433 buffer = malloc(size);
438 memset(buffer, 0, size);
440 r = write_blockwise(devfd, buffer, size) < size ? -EIO : 0;
449 #define DEFAULT_PROCESS_PRIORITY -18
451 static int _priority;
452 static int _memlock_count = 0;
454 // return 1 if memory is locked
455 int crypt_memlock_inc(struct crypt_device *ctx)
457 if (!_memlock_count++) {
458 log_dbg("Locking memory.");
459 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
460 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
465 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
466 log_err(ctx, _("Cannot get process priority.\n"));
468 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
469 log_err(ctx, _("setpriority %d failed: %s\n"),
470 DEFAULT_PROCESS_PRIORITY, strerror(errno));
472 return _memlock_count ? 1 : 0;
475 int crypt_memlock_dec(struct crypt_device *ctx)
477 if (_memlock_count && (!--_memlock_count)) {
478 log_dbg("Unlocking memory.");
479 if (munlockall() == -1)
480 log_err(ctx, _("Cannot unlock memory.\n"));
481 if (setpriority(PRIO_PROCESS, 0, _priority))
482 log_err(ctx, _("setpriority %d failed: %s\n"), _priority, strerror(errno));
484 return _memlock_count ? 1 : 0;
487 /* DEVICE TOPOLOGY */
489 /* block device topology ioctls, introduced in 2.6.32 */
491 #define BLKIOMIN _IO(0x12,120)
492 #define BLKIOOPT _IO(0x12,121)
493 #define BLKALIGNOFF _IO(0x12,122)
496 void get_topology_alignment(const char *device,
497 unsigned long *required_alignment, /* bytes */
498 unsigned long *alignment_offset, /* bytes */
499 unsigned long default_alignment)
501 int dev_alignment_offset = 0;
502 unsigned int min_io_size = 0, opt_io_size = 0;
503 unsigned long temp_alignment = 0;
506 *required_alignment = default_alignment;
507 *alignment_offset = 0;
509 fd = open(device, O_RDONLY);
513 /* minimum io size */
514 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
515 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
516 device, default_alignment);
520 /* optimal io size */
521 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
522 opt_io_size = min_io_size;
524 /* alignment offset, bogus -1 means misaligned/unknown */
525 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
526 dev_alignment_offset = 0;
527 *alignment_offset = (unsigned long)dev_alignment_offset;
529 temp_alignment = (unsigned long)min_io_size;
531 if (temp_alignment < (unsigned long)opt_io_size)
532 temp_alignment = (unsigned long)opt_io_size;
534 /* If calculated alignment is multiple of default, keep default */
535 if (temp_alignment && (default_alignment % temp_alignment))
536 *required_alignment = temp_alignment;
538 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
539 min_io_size, opt_io_size, *alignment_offset, *required_alignment);