10 #include <sys/types.h>
12 #include <sys/ioctl.h>
16 #include <sys/resource.h>
18 #include "libcryptsetup.h"
21 static char *error=NULL;
23 void set_error_va(const char *fmt, va_list va)
34 r = vasprintf(&error, fmt, va);
41 if (r && error[r - 1] == '\n')
45 void set_error(const char *fmt, ...)
50 set_error_va(fmt, va);
54 const char *get_error(void)
59 static int get_alignment(int fd)
61 int alignment = DEFAULT_MEM_ALIGNMENT;
63 #ifdef _PC_REC_XFER_ALIGN
64 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
66 alignment = DEFAULT_MEM_ALIGNMENT;
71 static void *aligned_malloc(void **base, int size, int alignment)
73 #ifdef HAVE_POSIX_MEMALIGN
74 return posix_memalign(base, alignment, size) ? NULL : *base;
76 /* Credits go to Michal's padlock patches for this alignment code */
79 ptr = malloc(size + alignment);
80 if(ptr == NULL) return NULL;
83 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
84 ptr += alignment - ((long)(ptr) & (alignment - 1));
89 static int sector_size(int fd)
92 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
98 int sector_size_for_device(const char *device)
100 int fd = open(device, O_RDONLY);
109 ssize_t write_blockwise(int fd, const void *orig_buf, size_t count)
111 void *hangover_buf, *hangover_buf_base = NULL;
112 void *buf, *buf_base = NULL;
113 int r, hangover, solid, bsize, alignment;
116 if ((bsize = sector_size(fd)) < 0)
119 hangover = count % bsize;
120 solid = count - hangover;
121 alignment = get_alignment(fd);
123 if ((long)orig_buf & (alignment - 1)) {
124 buf = aligned_malloc(&buf_base, count, alignment);
127 memcpy(buf, orig_buf, count);
129 buf = (void *)orig_buf;
131 r = write(fd, buf, solid);
132 if (r < 0 || r != solid)
136 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
140 r = read(fd, hangover_buf, bsize);
141 if(r < 0 || r != bsize) goto out;
143 r = lseek(fd, -bsize, SEEK_CUR);
146 memcpy(hangover_buf, buf + solid, hangover);
148 r = write(fd, hangover_buf, bsize);
149 if(r < 0 || r != bsize) goto out;
150 free(hangover_buf_base);
159 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
160 void *hangover_buf, *hangover_buf_base;
161 void *buf, *buf_base = NULL;
162 int r, hangover, solid, bsize, alignment;
165 if ((bsize = sector_size(fd)) < 0)
168 hangover = count % bsize;
169 solid = count - hangover;
170 alignment = get_alignment(fd);
172 if ((long)orig_buf & (alignment - 1)) {
173 buf = aligned_malloc(&buf_base, count, alignment);
179 r = read(fd, buf, solid);
180 if(r < 0 || r != solid)
184 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
187 r = read(fd, hangover_buf, bsize);
188 if (r < 0 || r != bsize)
191 memcpy(buf + solid, hangover_buf, hangover);
192 free(hangover_buf_base);
196 if (buf != orig_buf) {
197 memcpy(orig_buf, buf, count);
204 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
205 * but we also need a function to deal with short writes at the start. But this information
206 * is implicitly included in the read/write offset, which can not be set to non-aligned
207 * boundaries. Hence, we combine llseek with write.
210 ssize_t write_lseek_blockwise(int fd, const char *buf, size_t count, off_t offset) {
211 int bsize = sector_size(fd);
212 const char *orig_buf = buf;
213 char frontPadBuf[bsize];
214 int frontHang = offset % bsize;
216 int innerCount = count < bsize ? count : bsize;
221 lseek(fd, offset - frontHang, SEEK_SET);
223 r = read(fd,frontPadBuf,bsize);
226 memcpy(frontPadBuf+frontHang, buf, innerCount);
228 lseek(fd, offset - frontHang, SEEK_SET);
229 r = write(fd,frontPadBuf,bsize);
235 if(count <= 0) return buf - orig_buf;
237 return write_blockwise(fd, buf, count) + innerCount;
240 int device_ready(struct crypt_device *cd, const char *device, int mode)
247 if(stat(device, &st) < 0) {
248 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
252 log_dbg("Trying to open and read device %s.", device);
253 devfd = open(device, mode | O_DIRECT | O_SYNC);
255 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
256 (mode & O_EXCL) ? _("exclusive ") : "",
257 (mode & O_RDWR) ? _("writable") : _("read-only"));
261 /* Try to read first sector */
262 s = read_blockwise(devfd, buf, sizeof(buf));
263 if (s < 0 || s != sizeof(buf)) {
264 log_err(cd, _("Cannot read device %s.\n"), device);
268 memset(buf, 0, sizeof(buf));
274 int get_device_infos(const char *device, struct device_infos *infos, struct crypt_device *cd)
277 unsigned long size_small;
282 /* Try to open read-write to check whether it is a read-only device */
283 fd = open(device, O_RDWR);
285 if (errno == EROFS) {
287 fd = open(device, O_RDONLY);
291 fd = open(device, O_RDONLY);
294 log_err(cd, _("Cannot open device: %s\n"), device);
299 /* If the device can be opened read-write, i.e. readonly is still 0, then
300 * check whether BKROGET says that it is read-only. E.g. read-only loop
301 * devices may be openend read-write but are read-only according to BLKROGET
303 if (readonly == 0 && ioctl(fd, BLKROGET, &readonly) < 0) {
304 log_err(cd, _("BLKROGET failed on device %s.\n"), device);
308 #error BLKROGET not available
312 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
313 size >>= SECTOR_SHIFT;
320 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
321 size = (uint64_t)size_small;
326 # error Need at least the BLKGETSIZE ioctl!
329 log_err(cd, _("BLKGETSIZE failed on device %s.\n"), device);
333 infos->readonly = readonly;
339 int wipe_device_header(const char *device, int sectors)
342 int size = sectors * SECTOR_SIZE;
346 devfd = open(device, O_RDWR | O_DIRECT | O_SYNC);
350 buffer = malloc(size);
355 memset(buffer, 0, size);
357 r = write_blockwise(devfd, buffer, size) < size ? -EIO : 0;
366 #define DEFAULT_PROCESS_PRIORITY -18
368 static int _priority;
369 static int _memlock_count = 0;
371 // return 1 if memory is locked
372 int crypt_memlock_inc(struct crypt_device *ctx)
374 if (!_memlock_count++) {
375 log_dbg("Locking memory.");
376 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
377 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
382 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
383 log_err(ctx, _("Cannot get process priority.\n"));
385 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
386 log_err(ctx, _("setpriority %u failed: %s"),
387 DEFAULT_PROCESS_PRIORITY, strerror(errno));
389 return _memlock_count ? 1 : 0;
392 int crypt_memlock_dec(struct crypt_device *ctx)
394 if (_memlock_count && (!--_memlock_count)) {
395 log_dbg("Unlocking memory.");
397 log_err(ctx, _("Cannot unlock memory."));
398 if (setpriority(PRIO_PROCESS, 0, _priority))
399 log_err(ctx, _("setpriority %u failed: %s"), _priority, strerror(errno));
401 return _memlock_count ? 1 : 0;
404 /* DEVICE TOPOLOGY */
406 /* block device topology ioctls, introduced in 2.6.32 */
408 #define BLKIOMIN _IO(0x12,120)
409 #define BLKIOOPT _IO(0x12,121)
410 #define BLKALIGNOFF _IO(0x12,122)
413 void get_topology_alignment(const char *device,
414 unsigned long *required_alignment, /* bytes */
415 unsigned long *alignment_offset, /* bytes */
416 unsigned long default_alignment)
418 int dev_alignment_offset = 0;
419 unsigned int min_io_size = 0, opt_io_size = 0;
420 unsigned long temp_alignment = 0;
423 *required_alignment = default_alignment;
424 *alignment_offset = 0;
426 fd = open(device, O_RDONLY);
430 /* minimum io size */
431 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
432 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
433 device, default_alignment);
437 /* optimal io size */
438 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
439 opt_io_size = min_io_size;
441 /* alignment offset, bogus -1 means misaligned/unknown */
442 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
443 dev_alignment_offset = 0;
444 *alignment_offset = (unsigned long)dev_alignment_offset;
446 temp_alignment = (unsigned long)min_io_size;
448 if (temp_alignment < (unsigned long)opt_io_size)
449 temp_alignment = (unsigned long)opt_io_size;
451 /* If calculated alignment is multiple of default, keep default */
452 if (temp_alignment && (default_alignment % temp_alignment))
453 *required_alignment = temp_alignment;
455 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
456 min_io_size, opt_io_size, *alignment_offset, *required_alignment);