2 * crypt_reencrypt - crypt utility for offline reencryption
4 * Copyright (C) 2012 Milan Broz All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* The code works as follows:
21 * - create backup (detached) headers fo old and new device
22 * - mark original device unusable
23 * - maps two devices, one with old header one with new onto
24 * the _same_ underlying device
25 * - with direct-io reads old device and copy to new device in defined steps
26 * - keps simple off in file (allows restart)
27 * - there is several windows when corruption can happen
30 * dmsetup create x --table "0 $(blockdev --getsz DEV) crypt cipher_null-ecb-null - 0 DEV 0"
41 #include <sys/ioctl.h>
46 #include <libcryptsetup.h>
49 #include "cryptsetup.h"
51 static int opt_verbose = 0;
52 static int opt_debug = 0;
53 static const char *opt_cipher = NULL;
54 static const char *opt_hash = NULL;
55 static const char *opt_key_file = NULL;
56 static int opt_iteration_time = 1000;
57 static int opt_batch_mode = 0;
58 static int opt_version_mode = 0;
59 static int opt_random = 0;
60 static int opt_urandom = 0;
61 static int opt_bsize = 4;
62 static int opt_new = 0;
63 static const char *opt_new_file = NULL;
65 static const char **action_argv;
66 sigset_t signals_open;
72 uint64_t device_offset;
73 uint64_t device_shift;
76 enum { FORWARD = 0, BACKWARD = 1 } reencrypt_direction;
78 char header_file_org[PATH_MAX];
79 char header_file_new[PATH_MAX];
80 char log_file[PATH_MAX];
82 char crypt_path_org[PATH_MAX];
83 char crypt_path_new[PATH_MAX];
91 char MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
92 char NOMAGIC[] = {'L','U','K','S', 0xde, 0xad};
101 __attribute__((format(printf, 5, 6)))
102 static void clogger(struct crypt_device *cd, int level, const char *file,
103 int line, const char *format, ...)
108 va_start(argp, format);
110 if (vasprintf(&target, format, argp) > 0) {
112 crypt_log(cd, level, target);
113 } else if (opt_debug)
114 printf("# %s\n", target);
121 static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
125 case CRYPT_LOG_NORMAL:
128 case CRYPT_LOG_VERBOSE:
132 case CRYPT_LOG_ERROR:
135 case CRYPT_LOG_DEBUG:
137 printf("# %s\n", msg);
140 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
145 static void _quiet_log(int level, const char *msg, void *usrptr)
147 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
148 level = CRYPT_LOG_VERBOSE;
149 _log(level, msg, usrptr);
152 static int alignment(int fd)
156 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
162 static int device_magic(header_magic set_magic)
165 size_t block_size = 512;
169 devfd = open(rnc.device, O_RDWR | O_DIRECT);
171 return errno == EBUSY ? -EBUSY : -EINVAL;
173 if (posix_memalign((void *)&buf, alignment(devfd), block_size)) {
178 s = read(devfd, buf, block_size);
179 if (s < 0 || s != block_size) {
180 log_verbose(_("Cannot read device %s.\n"), rnc.device);
185 if (set_magic == MAKE_UNUSABLE && !memcmp(buf, MAGIC, MAGIC_L)) {
186 log_dbg("Marking LUKS device %s unusable.", rnc.device);
187 memcpy(buf, NOMAGIC, MAGIC_L);
190 } else if (set_magic == MAKE_USABLE && !memcmp(buf, NOMAGIC, MAGIC_L)) {
191 log_dbg("Marking LUKS device %s usable.", rnc.device);
192 memcpy(buf, MAGIC, MAGIC_L);
194 } else if (set_magic == CHECK_UNUSABLE) {
195 r = memcmp(buf, NOMAGIC, MAGIC_L) ? -EINVAL : 0;
197 rnc.device_uuid = strndup(&buf[0xa8], 40);
203 if (lseek(devfd, 0, SEEK_SET) == -1)
205 s = write(devfd, buf, block_size);
206 if (s < 0 || s != block_size) {
207 log_verbose(_("Cannot write device %s.\n"), rnc.device);
211 log_dbg("LUKS signature check failed for %s.", rnc.device);
214 memset(buf, 0, block_size);
220 static int create_empty_header(const char *new_file, uint64_t size)
225 log_dbg("Creating empty file %s of size %lu.", new_file, (unsigned long)size);
227 if (!(buf = malloc(size)))
229 memset(buf, 0, size);
231 fd = creat(new_file, S_IRUSR|S_IWUSR);
237 if (write(fd, buf, size) < size)
245 static int write_log(void)
247 static char buf[512];
250 //log_dbg("Updating LUKS reencryption log offset %" PRIu64 ".", offset);
251 memset(buf, 0, sizeof(buf));
252 snprintf(buf, sizeof(buf), "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
253 "version = %d\nUUID = %s\ndirection = %d\n"
254 "offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
255 1, rnc.device_uuid, rnc.reencrypt_direction,
256 rnc.device_offset, rnc.device_shift);
258 lseek(rnc.log_fd, 0, SEEK_SET);
259 r = write(rnc.log_fd, buf, sizeof(buf));
260 if (r < 0 || r != sizeof(buf))
266 static int parse_line_log(const char *line)
276 if (sscanf(line, "version = %d", &i) == 1) {
278 log_dbg("Log: Unexpected version = %i", i);
281 } else if (sscanf(line, "UUID = %40s", s) == 1) {
282 if (!rnc.device_uuid || strcmp(rnc.device_uuid, s)) {
283 log_dbg("Log: Unexpected UUID %s", s);
286 } else if (sscanf(line, "direction = %d", &i) == 1) {
287 log_dbg("Log: direction = %i", i);
288 rnc.reencrypt_direction = i;
289 } else if (sscanf(line, "offset = %" PRIu64, &u64) == 1) {
290 log_dbg("Log: offset = %" PRIu64, u64);
291 rnc.device_offset = u64;
292 } else if (sscanf(line, "shift = %" PRIu64, &u64) == 1) {
293 log_dbg("Log: shift = %" PRIu64, u64);
294 rnc.device_shift = u64;
301 static int parse_log(void)
303 static char buf[512];
307 s = read(rnc.log_fd, buf, sizeof(buf));
314 end = strchr(start, '\n');
317 if (parse_line_log(start)) {
318 log_err("Wrong log format.\n");
329 static int open_log(void)
333 if(stat(rnc.log_file, &st) < 0) {
334 log_dbg("Creating LUKS reencryption log file %s.", rnc.log_file);
336 // FIXME: move that somewhere else
337 rnc.reencrypt_direction = BACKWARD;
339 rnc.log_fd = open(rnc.log_file, O_RDWR|O_CREAT|O_DIRECT, S_IRUSR|S_IWUSR);
340 if (rnc.log_fd == -1)
345 log_dbg("Log file %s exists, restarting.", rnc.log_file);
346 rnc.log_fd = open(rnc.log_file, O_RDWR|O_DIRECT);
347 if (rnc.log_fd == -1)
352 /* Be sure it is correct format */
356 static void close_log(void)
358 log_dbg("Closing LUKS reencryption log file %s.", rnc.log_file);
359 if (rnc.log_fd != -1)
363 static int activate_luks_headers(void)
365 struct crypt_device *cd = NULL, *cd_new = NULL;
368 log_dbg("Activating LUKS devices from headers.");
370 if ((r = crypt_init(&cd, rnc.header_file_org)) ||
371 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
372 (r = crypt_set_data_device(cd, rnc.device)))
375 if ((r = crypt_activate_by_passphrase(cd, rnc.header_file_org,
376 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
377 CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_PRIVATE)) < 0)
380 if ((r = crypt_init(&cd_new, rnc.header_file_new)) ||
381 (r = crypt_load(cd_new, CRYPT_LUKS1, NULL)) ||
382 (r = crypt_set_data_device(cd_new, rnc.device)))
385 if ((r = crypt_activate_by_passphrase(cd_new, rnc.header_file_new,
386 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen,
387 CRYPT_ACTIVATE_SHARED|CRYPT_ACTIVATE_PRIVATE)) < 0)
395 static int backup_luks_headers(void)
397 struct crypt_device *cd = NULL, *cd_new = NULL;
398 struct crypt_params_luks1 params = {0};
399 char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
402 log_dbg("Creating LUKS header backup for device %s.", rnc.device);
403 if ((r = crypt_init(&cd, rnc.device)) ||
404 (r = crypt_load(cd, CRYPT_LUKS1, NULL)))
407 crypt_set_confirm_callback(cd, NULL, NULL);
408 if ((r = crypt_header_backup(cd, CRYPT_LUKS1, rnc.header_file_org)))
411 if ((r = create_empty_header(rnc.header_file_new,
412 crypt_get_data_offset(cd) * 512)))
415 params.hash = opt_hash ?: DEFAULT_LUKS1_HASH;
416 params.data_alignment = crypt_get_data_offset(cd);
417 params.data_device = rnc.device;
419 if ((r = crypt_init(&cd_new, rnc.header_file_new)))
423 crypt_set_rng_type(cd_new, CRYPT_RNG_RANDOM);
424 else if (opt_urandom)
425 crypt_set_rng_type(cd_new, CRYPT_RNG_URANDOM);
427 if (opt_iteration_time)
428 crypt_set_iteration_time(cd_new, opt_iteration_time);
431 r = crypt_parse_name_and_mode(opt_cipher, cipher, NULL, cipher_mode);
433 log_err(_("No known cipher specification pattern detected.\n"));
438 if ((r = crypt_format(cd_new, CRYPT_LUKS1,
439 opt_cipher ? cipher : crypt_get_cipher(cd),
440 opt_cipher ? cipher_mode : crypt_get_cipher_mode(cd),
442 NULL, crypt_get_volume_key_size(cd), ¶ms)))
445 if ((r = crypt_keyslot_add_by_volume_key(cd_new, rnc.keyslot,
446 NULL, 0, rnc.password, rnc.passwordLen)) < 0)
455 static void remove_headers(void)
457 struct crypt_device *cd = NULL;
459 if (crypt_init(&cd, NULL))
461 crypt_set_log_callback(cd, _quiet_log, NULL);
462 (void)crypt_deactivate(cd, rnc.header_file_org);
463 (void)crypt_deactivate(cd, rnc.header_file_new);
467 static int restore_luks_header(const char *backup)
469 struct crypt_device *cd = NULL;
472 r = crypt_init(&cd, rnc.device);
475 crypt_set_confirm_callback(cd, NULL, NULL);
476 r = crypt_header_restore(cd, CRYPT_LUKS1, backup);
483 static int copy_data_forward(int fd_old, int fd_new, size_t block_size, void *buf)
488 log_err("Reencrypting [");
490 while (rnc.device_offset < rnc.device_size) {
491 s1 = read(fd_old, buf, block_size);
492 if (s1 < 0 || (s1 != block_size && (rnc.device_offset + s1) != rnc.device_size)) {
493 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
496 s2 = write(fd_new, buf, s1);
498 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
501 rnc.device_offset += s1;
502 if (write_log() < 0) {
503 log_err("Log write error, some data are perhaps lost.\n");
507 if (rnc.device_offset > (j * (rnc.device_size / 10))) {
512 log_err("] Done.\n");
517 static int copy_data_backward(int fd_old, int fd_new, size_t block_size, void *buf)
519 ssize_t s1, s2, working_offset, working_block;
522 log_err("Reencrypting [");
525 while (rnc.device_offset) {
526 if (rnc.device_offset < block_size) {
528 working_block = rnc.device_offset;
530 working_offset = rnc.device_offset - block_size;
531 working_block = block_size;
534 if (lseek(fd_old, working_offset, SEEK_SET) < 0 ||
535 lseek(fd_new, working_offset, SEEK_SET) < 0)
537 //log_err("off: %06d, size %06d\n", working_offset, block_size);
539 s1 = read(fd_old, buf, working_block);
540 if (s1 < 0 || (s1 != working_block)) {
541 log_err("Read error, expecting %d, got %d.\n", (int)block_size, (int)s1);
544 s2 = write(fd_new, buf, working_block);
546 log_err("Write error, expecting %d, got %d.\n", (int)block_size, (int)s2);
549 rnc.device_offset -= s1;
550 if (write_log() < 0) {
551 log_err("Log write error, some data are perhaps lost.\n");
555 if (rnc.device_offset < (j * (rnc.device_size / 10))) {
560 log_err("] Done.\n");
565 static int copy_data(void)
567 size_t block_size = opt_bsize * 1024 * 1024;
568 int fd_old = -1, fd_new = -1;
572 fd_old = open(rnc.crypt_path_org, O_RDONLY | O_DIRECT);
576 fd_new = open(rnc.crypt_path_new, O_WRONLY | O_DIRECT);
580 if (lseek(fd_old, rnc.device_offset, SEEK_SET) == -1)
583 if (lseek(fd_new, rnc.device_offset, SEEK_SET) == -1)
587 if (ioctl(fd_old, BLKGETSIZE64, &rnc.device_size) < 0)
590 if (posix_memalign((void *)&buf, alignment(fd_new), block_size)) {
595 // FIXME: all this should be in init
596 if (!rnc.in_progress && rnc.reencrypt_direction == BACKWARD)
597 rnc.device_offset = rnc.device_size;
599 if (rnc.reencrypt_direction == FORWARD)
600 r = copy_data_forward(fd_old, fd_new, block_size, buf);
602 r = copy_data_backward(fd_old, fd_new, block_size, buf);
605 log_err("ERROR during reencryption.\n");
615 static int initialize_uuid(void)
617 struct crypt_device *cd = NULL;
620 /* Try to load LUKS from device */
621 if ((r = crypt_init(&cd, rnc.device)))
623 crypt_set_log_callback(cd, _quiet_log, NULL);
624 r = crypt_load(cd, CRYPT_LUKS1, NULL);
626 rnc.device_uuid = strdup(crypt_get_uuid(cd));
628 /* Reencryption already in progress - magic header? */
629 r = device_magic(CHECK_UNUSABLE);
635 static int initialize_passphrase(const char *device)
637 struct crypt_device *cd = NULL;
640 if ((r = crypt_init(&cd, device)) ||
641 (r = crypt_load(cd, CRYPT_LUKS1, NULL)) ||
642 (r = crypt_set_data_device(cd, rnc.device)))
645 if ((r = crypt_get_key(_("Enter LUKS passphrase: "),
646 &rnc.password, &rnc.passwordLen,
651 if ((r = crypt_activate_by_passphrase(cd, NULL,
652 CRYPT_ANY_SLOT, rnc.password, rnc.passwordLen, 0) < 0))
664 static int initialize_context(const char *device)
666 log_dbg("Initialising reencryption context.");
670 if (!(rnc.device = strndup(device, PATH_MAX)))
673 if (opt_new_file && !create_uuid()) {
674 log_err("Cannot create fake header.\n");
678 if (initialize_uuid()) {
679 log_err("No header found on device.\n");
683 /* Prepare device names */
684 if (snprintf(rnc.log_file, PATH_MAX,
685 "LUKS-%s.log", rnc.device_uuid) < 0)
687 if (snprintf(rnc.header_file_org, PATH_MAX,
688 "LUKS-%s.org", rnc.device_uuid) < 0)
690 if (snprintf(rnc.header_file_new, PATH_MAX,
691 "LUKS-%s.new", rnc.device_uuid) < 0)
694 /* Paths to encrypted devices */
695 if (snprintf(rnc.crypt_path_org, PATH_MAX,
696 "%s/%s", crypt_get_dir(), rnc.header_file_org) < 0)
698 if (snprintf(rnc.crypt_path_new, PATH_MAX,
699 "%s/%s", crypt_get_dir(), rnc.header_file_new) < 0)
705 // FIXME: add some routine to handle it
706 sigemptyset(&signals_open);
707 sigaddset(&signals_open, SIGINT);
708 sigprocmask(SIG_SETMASK, &signals_open, NULL);
713 static void destroy_context(void)
715 log_dbg("Destroying reencryption context.");
720 if ((rnc.reencrypt_direction == FORWARD &&
721 rnc.device_offset == rnc.device_size) ||
722 rnc.device_offset == 0) {
723 unlink(rnc.log_file);
724 unlink(rnc.header_file_org);
725 unlink(rnc.header_file_new);
728 crypt_safe_free(rnc.password);
731 free(rnc.device_uuid);
733 sigprocmask(SIG_UNBLOCK, &signals_open, NULL);
736 int run_reencrypt(const char *device)
740 if (initialize_context(device))
743 log_dbg("Running reencryption.");
745 if (!rnc.in_progress) {
746 if ((r = initialize_passphrase(rnc.device)) ||
747 (r = backup_luks_headers()) ||
748 (r = device_magic(MAKE_UNUSABLE)))
751 if ((r = initialize_passphrase(rnc.header_file_org)))
755 if ((r = activate_luks_headers()))
758 if ((r = copy_data()))
761 r = restore_luks_header(rnc.header_file_new);
767 static __attribute__ ((noreturn)) void usage(poptContext popt_context,
768 int exitcode, const char *error,
771 poptPrintUsage(popt_context, stderr, 0);
773 log_err("%s: %s\n", more, error);
774 poptFreeContext(popt_context);
778 static void help(poptContext popt_context,
779 enum poptCallbackReason reason __attribute__((unused)),
780 struct poptOption *key,
781 const char *arg __attribute__((unused)),
782 void *data __attribute__((unused)))
784 usage(popt_context, EXIT_SUCCESS, NULL, NULL);
787 static void _dbg_version_and_cmd(int argc, const char **argv)
791 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
792 for (i = 0; i < argc; i++) {
795 log_std("%s", argv[i]);
800 int main(int argc, const char **argv)
802 static struct poptOption popt_help_options[] = {
803 { NULL, '\0', POPT_ARG_CALLBACK, help, 0, NULL, NULL },
804 { "help", '?', POPT_ARG_NONE, NULL, 0, N_("Show this help message"), NULL },
805 { "usage", '\0', POPT_ARG_NONE, NULL, 0, N_("Display brief usage"), NULL },
808 static struct poptOption popt_options[] = {
809 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, popt_help_options, 0, N_("Help options:"), NULL },
810 { "version", '\0', POPT_ARG_NONE, &opt_version_mode, 0, N_("Print package version"), NULL },
811 { "verbose", 'v', POPT_ARG_NONE, &opt_verbose, 0, N_("Shows more detailed error messages"), NULL },
812 { "debug", '\0', POPT_ARG_NONE, &opt_debug, 0, N_("Show debug messages"), NULL },
813 { "block-size", 'B', POPT_ARG_INT, &opt_bsize, 0, N_("Reencryption block size"), N_("MB") },
814 { "new-header", 'N', POPT_ARG_INT, &opt_new, 0, N_("Create new header, need size on the end of device"), N_("MB") },
815 { "new-crypt", 'f', POPT_ARG_STRING, &opt_new_file, 0, N_("Log suffix for new reencryption file."), NULL },
816 { "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
817 { "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
818 { "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file."), NULL },
819 { "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
820 { "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
821 { "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
822 { "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
825 poptContext popt_context;
828 crypt_set_log_callback(NULL, _log, NULL);
829 log_err("WARNING: this is experimental code, it can completely break your data.\n");
831 setlocale(LC_ALL, "");
832 bindtextdomain(PACKAGE, LOCALEDIR);
835 popt_context = poptGetContext(PACKAGE, argc, argv, popt_options, 0);
836 poptSetOtherOptionHelp(popt_context,
837 N_("[OPTION...] <device>]"));
839 while((r = poptGetNextOpt(popt_context)) > 0) {
845 usage(popt_context, EXIT_FAILURE, poptStrerror(r),
846 poptBadOption(popt_context, POPT_BADOPTION_NOALIAS));
847 if (opt_version_mode) {
848 log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
849 poptFreeContext(popt_context);
853 action_argv = poptGetArgs(popt_context);
855 usage(popt_context, EXIT_FAILURE, _("Argument required."),
856 poptGetInvocationName(popt_context));
858 if (opt_random && opt_urandom)
859 usage(popt_context, EXIT_FAILURE, _("Only one of --use-[u]random options is allowed."),
860 poptGetInvocationName(popt_context));
862 if (opt_new && !opt_new_file)
863 usage(popt_context, EXIT_FAILURE, _("You have to use -f with -N."),
864 poptGetInvocationName(popt_context));
868 crypt_set_debug_level(-1);
869 _dbg_version_and_cmd(argc, argv);
872 r = run_reencrypt(action_argv[0]);
874 poptFreeContext(popt_context);
876 /* Translate exit code to simple codes */
878 case 0: r = EXIT_SUCCESS; break;
880 case -EBUSY: r = 5; break;
882 case -ENODEV: r = 4; break;
883 case -ENOMEM: r = 3; break;
884 case -EPERM: r = 2; break;
888 default: r = EXIT_FAILURE;