Fix regression in header backup (1.5.1).
authorMilan Broz <gmazyland@gmail.com>
Wed, 19 Dec 2012 12:19:05 +0000 (13:19 +0100)
committerMilan Broz <gmazyland@gmail.com>
Wed, 19 Dec 2012 12:19:05 +0000 (13:19 +0100)
Access to backup file must handle write to regular files too.

lib/luks1/keymanage.c
lib/utils.c
tests/compat-test

index 72d3f88..f369303 100644 (file)
@@ -283,7 +283,7 @@ int LUKS_hdr_restore(
        log_dbg("Storing backup of header (%u bytes) and keyslot area (%u bytes) to device %s.",
                sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device));
 
-       devfd = open(device_path(device), O_WRONLY | O_DIRECT | O_SYNC);
+       devfd = open(device_path(device), O_RDWR | O_DIRECT | O_SYNC);
        if (devfd == -1) {
                if (errno == EACCES)
                        log_err(ctx, _("Cannot write to device %s, permission denied.\n"),
index eef7891..67032f2 100644 (file)
@@ -97,9 +97,12 @@ ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
                        goto out;
 
                r = read(fd, hangover_buf, bsize);
-               if (r < 0 || r != bsize)
+               if (r < 0 || r < hangover)
                        goto out;
 
+               if (r < bsize)
+                       bsize = r;
+
                r = lseek(fd, -bsize, SEEK_CUR);
                if (r < 0)
                        goto out;
index c1003d6..f02fbc6 100755 (executable)
@@ -149,10 +149,12 @@ ORG_SHA1=$(sha1sum -b /dev/mapper/$DEV_NAME | cut -f 1 -d' ')
 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
 
 # Check it can be opened from header backup as well
-$CRYPTSETUP luksHeaderBackup $IMG --header-backup-file $HEADER_IMG
+$CRYPTSETUP luksHeaderBackup $IMG --header-backup-file $HEADER_IMG || fail
 echo "compatkey" | $CRYPTSETUP luksOpen $IMG10 $DEV_NAME --header $HEADER_IMG || fail
 check_exists
 $CRYPTSETUP -q luksClose  $DEV_NAME || fail
+# Check restore
+$CRYPTSETUP luksHeaderRestore -q $IMG --header-backup-file $HEADER_IMG || fail
 
 # Repeat for V1.0 header - not aligned first keyslot
 echo "compatkey" | $CRYPTSETUP luksOpen $IMG10 $DEV_NAME || fail