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 void set_error_va(const char *fmt, va_list va)
54 r = vasprintf(&error, fmt, va);
61 if (r && error[r - 1] == '\n')
65 void set_error(const char *fmt, ...)
70 set_error_va(fmt, va);
74 const char *get_error(void)
79 static int get_alignment(int fd)
81 int alignment = DEFAULT_MEM_ALIGNMENT;
83 #ifdef _PC_REC_XFER_ALIGN
84 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
86 alignment = DEFAULT_MEM_ALIGNMENT;
91 static void *aligned_malloc(void **base, int size, int alignment)
93 #ifdef HAVE_POSIX_MEMALIGN
94 return posix_memalign(base, alignment, size) ? NULL : *base;
96 /* Credits go to Michal's padlock patches for this alignment code */
99 ptr = malloc(size + alignment);
100 if(ptr == NULL) return NULL;
103 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
104 ptr += alignment - ((long)(ptr) & (alignment - 1));
109 static int sector_size(int fd)
112 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
118 int sector_size_for_device(const char *device)
120 int fd = open(device, O_RDONLY);
129 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
131 void *hangover_buf, *hangover_buf_base = NULL;
132 void *buf, *buf_base = NULL;
133 int r, hangover, solid, bsize, alignment;
136 if ((bsize = sector_size(fd)) < 0)
139 hangover = count % bsize;
140 solid = count - hangover;
141 alignment = get_alignment(fd);
143 if ((long)orig_buf & (alignment - 1)) {
144 buf = aligned_malloc(&buf_base, count, alignment);
147 memcpy(buf, orig_buf, count);
149 buf = (void *)orig_buf;
151 r = write(fd, buf, solid);
152 if (r < 0 || r != solid)
156 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
160 r = read(fd, hangover_buf, bsize);
161 if(r < 0 || r != bsize) goto out;
163 r = lseek(fd, -bsize, SEEK_CUR);
166 memcpy(hangover_buf, buf + solid, hangover);
168 r = write(fd, hangover_buf, bsize);
169 if(r < 0 || r != bsize) goto out;
170 free(hangover_buf_base);
179 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
180 void *hangover_buf, *hangover_buf_base;
181 void *buf, *buf_base = NULL;
182 int r, hangover, solid, bsize, alignment;
185 if ((bsize = sector_size(fd)) < 0)
188 hangover = count % bsize;
189 solid = count - hangover;
190 alignment = get_alignment(fd);
192 if ((long)orig_buf & (alignment - 1)) {
193 buf = aligned_malloc(&buf_base, count, alignment);
199 r = read(fd, buf, solid);
200 if(r < 0 || r != solid)
204 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
207 r = read(fd, hangover_buf, bsize);
208 if (r < 0 || r != bsize)
211 memcpy(buf + solid, hangover_buf, hangover);
212 free(hangover_buf_base);
216 if (buf != orig_buf) {
217 memcpy(orig_buf, buf, count);
224 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
225 * but we also need a function to deal with short writes at the start. But this information
226 * is implicitly included in the read/write offset, which can not be set to non-aligned
227 * boundaries. Hence, we combine llseek with write.
230 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
231 int bsize = sector_size(fd);
232 const char *orig_buf = buf;
233 char frontPadBuf[bsize];
234 int frontHang = offset % bsize;
236 int innerCount = count < bsize ? count : bsize;
241 lseek(fd, offset - frontHang, SEEK_SET);
243 r = read(fd,frontPadBuf,bsize);
246 memcpy(frontPadBuf+frontHang, buf, innerCount);
248 lseek(fd, offset - frontHang, SEEK_SET);
249 r = write(fd,frontPadBuf,bsize);
255 if(count <= 0) return buf - orig_buf;
257 return write_blockwise(fd, buf, count) + innerCount;
260 int device_ready(struct crypt_device *cd, const char *device, int mode)
267 if(stat(device, &st) < 0) {
268 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
272 if (!S_ISBLK(st.st_mode))
275 log_dbg("Trying to open and read device %s.", device);
276 devfd = open(device, mode | O_DIRECT | O_SYNC);
278 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
279 (mode & O_EXCL) ? _("exclusive ") : "",
280 (mode & O_RDWR) ? _("writable") : _("read-only"));
284 /* Try to read first sector */
285 s = read_blockwise(devfd, buf, sizeof(buf));
286 if (s < 0 || s != sizeof(buf)) {
287 log_verbose(cd, _("Cannot read device %s.\n"), device);
291 memset(buf, 0, sizeof(buf));
297 int get_device_infos(const char *device,
303 unsigned long size_small;
310 if (stat(device, &st) < 0)
313 /* never wipe header on mounted device */
314 if (open_exclusive && S_ISBLK(st.st_mode))
317 /* Try to open read-write to check whether it is a read-only device */
318 fd = open(device, O_RDWR | flags);
319 if (fd == -1 && errno == EROFS) {
321 fd = open(device, O_RDONLY | flags);
324 if (fd == -1 && open_exclusive && errno == EBUSY)
331 /* If the device can be opened read-write, i.e. readonly is still 0, then
332 * check whether BKROGET says that it is read-only. E.g. read-only loop
333 * devices may be openend read-write but are read-only according to BLKROGET
335 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
338 #error BLKROGET not available
342 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
343 *size >>= SECTOR_SHIFT;
350 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
351 *size = (uint64_t)size_small;
357 # error Need at least the BLKGETSIZE ioctl!
365 int device_check_and_adjust(struct crypt_device *cd,
372 int r, real_readonly;
378 r = get_device_infos(device, open_exclusive, &real_readonly, &real_size);
381 log_err(cd, _("Cannot use device %s which is in use "
382 "(already mapped or mounted).\n"),
385 log_err(cd, _("Cannot get info about device %s.\n"),
393 log_err(cd, _("Device %s has zero size.\n"), device);
396 if (*size < *offset) {
397 log_err(cd, _("Device %s is too small.\n"), device);
406 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
407 *size, *read_only ? "RO" : "RW", *offset);
411 int wipe_device_header(const char *device, int sectors)
415 int size = sectors * SECTOR_SIZE;
418 int flags = O_RDWR | O_DIRECT | O_SYNC;
420 if (stat(device, &st) < 0)
423 /* never wipe header on mounted device */
424 if (S_ISBLK(st.st_mode))
427 devfd = open(device, flags);
429 return errno == EBUSY ? -EBUSY : -EINVAL;
431 buffer = malloc(size);
436 memset(buffer, 0, size);
438 r = write_blockwise(devfd, buffer, size) < size ? -EIO : 0;
447 #define DEFAULT_PROCESS_PRIORITY -18
449 static int _priority;
450 static int _memlock_count = 0;
452 // return 1 if memory is locked
453 int crypt_memlock_inc(struct crypt_device *ctx)
455 if (!_memlock_count++) {
456 log_dbg("Locking memory.");
457 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
458 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
463 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
464 log_err(ctx, _("Cannot get process priority.\n"));
466 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
467 log_err(ctx, _("setpriority %d failed: %s\n"),
468 DEFAULT_PROCESS_PRIORITY, strerror(errno));
470 return _memlock_count ? 1 : 0;
473 int crypt_memlock_dec(struct crypt_device *ctx)
475 if (_memlock_count && (!--_memlock_count)) {
476 log_dbg("Unlocking memory.");
477 if (munlockall() == -1)
478 log_err(ctx, _("Cannot unlock memory.\n"));
479 if (setpriority(PRIO_PROCESS, 0, _priority))
480 log_err(ctx, _("setpriority %d failed: %s\n"), _priority, strerror(errno));
482 return _memlock_count ? 1 : 0;
485 /* DEVICE TOPOLOGY */
487 /* block device topology ioctls, introduced in 2.6.32 */
489 #define BLKIOMIN _IO(0x12,120)
490 #define BLKIOOPT _IO(0x12,121)
491 #define BLKALIGNOFF _IO(0x12,122)
494 void get_topology_alignment(const char *device,
495 unsigned long *required_alignment, /* bytes */
496 unsigned long *alignment_offset, /* bytes */
497 unsigned long default_alignment)
499 int dev_alignment_offset = 0;
500 unsigned int min_io_size = 0, opt_io_size = 0;
501 unsigned long temp_alignment = 0;
504 *required_alignment = default_alignment;
505 *alignment_offset = 0;
507 fd = open(device, O_RDONLY);
511 /* minimum io size */
512 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
513 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
514 device, default_alignment);
518 /* optimal io size */
519 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
520 opt_io_size = min_io_size;
522 /* alignment offset, bogus -1 means misaligned/unknown */
523 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
524 dev_alignment_offset = 0;
525 *alignment_offset = (unsigned long)dev_alignment_offset;
527 temp_alignment = (unsigned long)min_io_size;
529 if (temp_alignment < (unsigned long)opt_io_size)
530 temp_alignment = (unsigned long)opt_io_size;
532 /* If calculated alignment is multiple of default, keep default */
533 if (temp_alignment && (default_alignment % temp_alignment))
534 *required_alignment = temp_alignment;
536 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
537 min_io_size, opt_io_size, *alignment_offset, *required_alignment);