10 #include <sys/types.h>
12 #include <sys/ioctl.h>
15 #include <sys/resource.h>
17 #include "libcryptsetup.h"
20 static char *error=NULL;
22 void set_error_va(const char *fmt, va_list va)
33 r = vasprintf(&error, fmt, va);
40 if (r && error[r - 1] == '\n')
44 void set_error(const char *fmt, ...)
49 set_error_va(fmt, va);
53 const char *get_error(void)
58 static int get_alignment(int fd)
60 int alignment = DEFAULT_MEM_ALIGNMENT;
62 #ifdef _PC_REC_XFER_ALIGN
63 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
65 alignment = DEFAULT_MEM_ALIGNMENT;
70 static void *aligned_malloc(void **base, int size, int alignment)
72 #ifdef HAVE_POSIX_MEMALIGN
73 return posix_memalign(base, alignment, size) ? NULL : *base;
75 /* Credits go to Michal's padlock patches for this alignment code */
78 ptr = malloc(size + alignment);
79 if(ptr == NULL) return NULL;
82 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
83 ptr += alignment - ((long)(ptr) & (alignment - 1));
88 static int sector_size(int fd)
91 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
97 int sector_size_for_device(const char *device)
99 int fd = open(device, O_RDONLY);
108 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
110 void *hangover_buf, *hangover_buf_base = NULL;
111 void *buf, *buf_base = NULL;
112 int r, hangover, solid, bsize, alignment;
115 if ((bsize = sector_size(fd)) < 0)
118 hangover = count % bsize;
119 solid = count - hangover;
120 alignment = get_alignment(fd);
122 if ((long)orig_buf & (alignment - 1)) {
123 buf = aligned_malloc(&buf_base, count, alignment);
126 memcpy(buf, orig_buf, count);
128 buf = (void *)orig_buf;
130 r = write(fd, buf, solid);
131 if (r < 0 || r != solid)
135 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
139 r = read(fd, hangover_buf, bsize);
140 if(r < 0 || r != bsize) goto out;
142 r = lseek(fd, -bsize, SEEK_CUR);
145 memcpy(hangover_buf, buf + solid, hangover);
147 r = write(fd, hangover_buf, bsize);
148 if(r < 0 || r != bsize) goto out;
149 free(hangover_buf_base);
158 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
159 void *hangover_buf, *hangover_buf_base;
160 void *buf, *buf_base = NULL;
161 int r, hangover, solid, bsize, alignment;
164 if ((bsize = sector_size(fd)) < 0)
167 hangover = count % bsize;
168 solid = count - hangover;
169 alignment = get_alignment(fd);
171 if ((long)orig_buf & (alignment - 1)) {
172 buf = aligned_malloc(&buf_base, count, alignment);
178 r = read(fd, buf, solid);
179 if(r < 0 || r != solid)
183 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
186 r = read(fd, hangover_buf, bsize);
187 if (r < 0 || r != bsize)
190 memcpy(buf + solid, hangover_buf, hangover);
191 free(hangover_buf_base);
195 if (buf != orig_buf) {
196 memcpy(orig_buf, buf, count);
203 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
204 * but we also need a function to deal with short writes at the start. But this information
205 * is implicitly included in the read/write offset, which can not be set to non-aligned
206 * boundaries. Hence, we combine llseek with write.
209 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
210 int bsize = sector_size(fd);
211 const char *orig_buf = buf;
212 char frontPadBuf[bsize];
213 int frontHang = offset % bsize;
215 int innerCount = count < bsize ? count : bsize;
220 lseek(fd, offset - frontHang, SEEK_SET);
222 r = read(fd,frontPadBuf,bsize);
225 memcpy(frontPadBuf+frontHang, buf, innerCount);
227 lseek(fd, offset - frontHang, SEEK_SET);
228 r = write(fd,frontPadBuf,bsize);
234 if(count <= 0) return buf - orig_buf;
236 return write_blockwise(fd, buf, count) + innerCount;
239 int device_ready(struct crypt_device *cd, const char *device, int mode)
246 if(stat(device, &st) < 0) {
247 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
251 log_dbg("Trying to open and read device %s.", device);
252 devfd = open(device, mode | O_DIRECT | O_SYNC);
254 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
255 (mode & O_EXCL) ? _("exclusive ") : "",
256 (mode & O_RDWR) ? _("writable") : _("read-only"));
260 /* Try to read first sector */
261 s = read_blockwise(devfd, buf, sizeof(buf));
262 if (s < 0 || s != sizeof(buf)) {
263 log_err(cd, _("Cannot read device %s.\n"), device);
267 memset(buf, 0, sizeof(buf));
273 int get_device_infos(const char *device, struct device_infos *infos, struct crypt_device *cd)
276 unsigned long size_small;
281 /* Try to open read-write to check whether it is a read-only device */
282 fd = open(device, O_RDWR);
284 if (errno == EROFS) {
286 fd = open(device, O_RDONLY);
290 fd = open(device, O_RDONLY);
293 log_err(cd, _("Cannot open device: %s\n"), device);
298 /* If the device can be opened read-write, i.e. readonly is still 0, then
299 * check whether BKROGET says that it is read-only. E.g. read-only loop
300 * devices may be openend read-write but are read-only according to BLKROGET
302 if (readonly == 0 && ioctl(fd, BLKROGET, &readonly) < 0) {
303 log_err(cd, _("BLKROGET failed on device %s.\n"), device);
307 #error BLKROGET not available
311 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
312 size >>= SECTOR_SHIFT;
319 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
320 size = (uint64_t)size_small;
325 # error Need at least the BLKGETSIZE ioctl!
328 log_err(cd, _("BLKGETSIZE failed on device %s.\n"), device);
332 infos->readonly = readonly;
338 int wipe_device_header(const char *device, int sectors)
341 int size = sectors * SECTOR_SIZE;
345 devfd = open(device, O_RDWR | O_DIRECT | O_SYNC);
349 buffer = malloc(size);
354 memset(buffer, 0, size);
356 r = write_blockwise(devfd, buffer, size) < size ? -EIO : 0;
365 #define DEFAULT_PROCESS_PRIORITY -18
367 static int _priority;
368 static int _memlock_count = 0;
370 // return 1 if memory is locked
371 int crypt_memlock_inc(struct crypt_device *ctx)
373 if (!_memlock_count++) {
374 log_dbg("Locking memory.");
375 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
376 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
381 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
382 log_err(ctx, _("Cannot get process priority.\n"));
384 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
385 log_err(ctx, _("setpriority %u failed: %s"),
386 DEFAULT_PROCESS_PRIORITY, strerror(errno));
388 return _memlock_count ? 1 : 0;
391 int crypt_memlock_dec(struct crypt_device *ctx)
393 if (_memlock_count && (!--_memlock_count)) {
394 log_dbg("Unlocking memory.");
396 log_err(ctx, _("Cannot unlock memory."));
397 if (setpriority(PRIO_PROCESS, 0, _priority))
398 log_err(ctx, _("setpriority %u failed: %s"), _priority, strerror(errno));
400 return _memlock_count ? 1 : 0;
403 /* DEVICE TOPOLOGY */
405 /* block device topology ioctls, introduced in 2.6.32 */
407 #define BLKIOMIN _IO(0x12,120)
408 #define BLKIOOPT _IO(0x12,121)
409 #define BLKALIGNOFF _IO(0x12,122)
412 void get_topology_alignment(const char *device,
413 unsigned long *required_alignment, /* bytes */
414 unsigned long *alignment_offset, /* bytes */
415 unsigned long default_alignment)
417 int dev_alignment_offset = 0;
418 unsigned int min_io_size = 0, opt_io_size = 0;
419 unsigned long temp_alignment = 0;
422 *required_alignment = default_alignment;
423 *alignment_offset = 0;
425 fd = open(device, O_RDONLY);
429 /* minimum io size */
430 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
431 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
432 device, default_alignment);
436 /* optimal io size */
437 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
438 opt_io_size = min_io_size;
440 /* alignment offset, bogus -1 means misaligned/unknown */
441 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
442 dev_alignment_offset = 0;
443 *alignment_offset = (unsigned long)dev_alignment_offset;
445 temp_alignment = (unsigned long)min_io_size;
447 if (temp_alignment < (unsigned long)opt_io_size)
448 temp_alignment = (unsigned long)opt_io_size;
450 /* If calculated alignment is multiple of default, keep default */
451 if (temp_alignment && (default_alignment % temp_alignment))
452 *required_alignment = temp_alignment;
454 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
455 min_io_size, opt_io_size, *alignment_offset, *required_alignment);