Blame SOURCES/cryptsetup-1.7.5-fix-unaligned-access-to-hidden-truecrypt.patch

82b9cf
From a117f431179a2747f2b1d5293f43d9e198f1bac9 Mon Sep 17 00:00:00 2001
82b9cf
From: Ondrej Kozina <okozina@redhat.com>
82b9cf
Date: Mon, 30 Nov 2015 16:44:15 +0100
82b9cf
Subject: [PATCH] Fix access to unaligned hidden TrueCrypt header.
82b9cf
82b9cf
backport all changes needed to fix unaligned access
82b9cf
to hidden TrueCrypt hedaer.
82b9cf
---
82b9cf
 lib/internal.h        |   7 ++-
82b9cf
 lib/luks1/keymanage.c |   6 +-
82b9cf
 lib/tcrypt/tcrypt.c   |  24 ++++----
82b9cf
 lib/utils.c           | 155 +++++++++++++++++++++++++++++++++++++++++++-------
82b9cf
 4 files changed, 152 insertions(+), 40 deletions(-)
82b9cf
82b9cf
diff --git a/lib/internal.h b/lib/internal.h
82b9cf
index 382a600..f1525f2 100644
82b9cf
--- a/lib/internal.h
82b9cf
+++ b/lib/internal.h
82b9cf
@@ -101,9 +101,12 @@ char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t
82b9cf
 char *crypt_get_base_device(const char *dev_path);
82b9cf
 uint64_t crypt_dev_partition_offset(const char *dev_path);
82b9cf
 
82b9cf
+ssize_t write_buffer(int fd, const void *buf, size_t count);
82b9cf
+ssize_t read_buffer(int fd, void *buf, size_t count);
82b9cf
 ssize_t write_blockwise(int fd, int bsize, void *buf, size_t count);
82b9cf
-ssize_t read_blockwise(int fd, int bsize, void *_buf, size_t count);
82b9cf
-ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset);
82b9cf
+ssize_t read_blockwise(int fd, int bsize, void *buf, size_t count);
82b9cf
+ssize_t write_lseek_blockwise(int fd, int bsize, void *buf, size_t count, off_t offset);
82b9cf
+ssize_t read_lseek_blockwise(int fd, int bsize, void *buf, size_t count, off_t offset);
82b9cf
 
82b9cf
 unsigned crypt_getpagesize(void);
82b9cf
 int init_crypto(struct crypt_device *ctx);
82b9cf
diff --git a/lib/luks1/keymanage.c b/lib/luks1/keymanage.c
82b9cf
index 23e3fe2..b193ee9 100644
82b9cf
--- a/lib/luks1/keymanage.c
82b9cf
+++ b/lib/luks1/keymanage.c
82b9cf
@@ -201,7 +201,7 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
82b9cf
 		r = -EINVAL;
82b9cf
 		goto out;
82b9cf
 	}
82b9cf
-	if (write(devfd, buffer, buffer_size) < buffer_size) {
82b9cf
+	if (write_buffer(devfd, buffer, buffer_size) < buffer_size) {
82b9cf
 		log_err(ctx, _("Cannot write header backup file %s.\n"), backup_file);
82b9cf
 		r = -EIO;
82b9cf
 		goto out;
82b9cf
@@ -253,7 +253,7 @@ int LUKS_hdr_restore(
82b9cf
 		goto out;
82b9cf
 	}
82b9cf
 
82b9cf
-	if (read(devfd, buffer, buffer_size) < buffer_size) {
82b9cf
+	if (read_buffer(devfd, buffer, buffer_size) < buffer_size) {
82b9cf
 		log_err(ctx, _("Cannot read header backup file %s.\n"), backup_file);
82b9cf
 		r = -EIO;
82b9cf
 		goto out;
82b9cf
@@ -498,7 +498,7 @@ int LUKS_read_phdr_backup(const char *backup_file,
82b9cf
 		return -ENOENT;
82b9cf
 	}
82b9cf
 
82b9cf
-	if (read(devfd, hdr, hdr_size) < hdr_size)
82b9cf
+	if (read_buffer(devfd, hdr, hdr_size) < hdr_size)
82b9cf
 		r = -EIO;
82b9cf
 	else {
82b9cf
 		LUKS_fix_header_compatible(hdr);
82b9cf
diff --git a/lib/tcrypt/tcrypt.c b/lib/tcrypt/tcrypt.c
82b9cf
index 45154ed..9ff7157 100644
82b9cf
--- a/lib/tcrypt/tcrypt.c
82b9cf
+++ b/lib/tcrypt/tcrypt.c
82b9cf
@@ -469,8 +469,7 @@ static int TCRYPT_pool_keyfile(struct crypt_device *cd,
82b9cf
 		return -EIO;
82b9cf
 	}
82b9cf
 
82b9cf
-	/* FIXME: add while */
82b9cf
-	data_size = read(fd, data, TCRYPT_KEYFILE_LEN);
82b9cf
+	data_size = read_buffer(fd, data, TCRYPT_KEYFILE_LEN);
82b9cf
 	close(fd);
82b9cf
 	if (data_size < 0) {
82b9cf
 		log_err(cd, _("Error reading keyfile %s.\n"), keyfile);
82b9cf
@@ -628,27 +627,26 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
82b9cf
 
82b9cf
 	r = -EIO;
82b9cf
 	if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER) {
82b9cf
-		if (lseek(devfd, TCRYPT_HDR_SYSTEM_OFFSET, SEEK_SET) >= 0 &&
82b9cf
-		    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size) {
82b9cf
+		if (read_lseek_blockwise(devfd, bs, hdr, hdr_size,
82b9cf
+			TCRYPT_HDR_SYSTEM_OFFSET) == hdr_size) {
82b9cf
 			r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
 		}
82b9cf
 	} else if (params->flags & CRYPT_TCRYPT_HIDDEN_HEADER) {
82b9cf
 		if (params->flags & CRYPT_TCRYPT_BACKUP_HEADER) {
82b9cf
-			if (lseek(devfd, TCRYPT_HDR_HIDDEN_OFFSET_BCK, SEEK_END) >= 0 &&
82b9cf
-			    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size)
82b9cf
+			if (read_lseek_blockwise(devfd, bs, hdr, hdr_size,
82b9cf
+				TCRYPT_HDR_HIDDEN_OFFSET_BCK) == hdr_size)
82b9cf
 				r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
 		} else {
82b9cf
-			if (lseek(devfd, TCRYPT_HDR_HIDDEN_OFFSET, SEEK_SET) >= 0 &&
82b9cf
-			    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size)
82b9cf
+			if (read_lseek_blockwise(devfd, bs, hdr, hdr_size,
82b9cf
+				TCRYPT_HDR_HIDDEN_OFFSET) == hdr_size)
82b9cf
 				r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
-			if (r &&
82b9cf
-			    lseek(devfd, TCRYPT_HDR_HIDDEN_OFFSET_OLD, SEEK_END) >= 0 &&
82b9cf
-			    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size)
82b9cf
+			if (r && read_lseek_blockwise(devfd, bs, hdr, hdr_size,
82b9cf
+				TCRYPT_HDR_HIDDEN_OFFSET_OLD) == hdr_size)
82b9cf
 				r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
 		}
82b9cf
 	} else if (params->flags & CRYPT_TCRYPT_BACKUP_HEADER) {
82b9cf
-		if (lseek(devfd, TCRYPT_HDR_OFFSET_BCK, SEEK_END) >= 0 &&
82b9cf
-			    read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size)
82b9cf
+		if (read_lseek_blockwise(devfd, bs, hdr, hdr_size,
82b9cf
+			TCRYPT_HDR_OFFSET_BCK) == hdr_size)
82b9cf
 			r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
 	} else if (read_blockwise(devfd, bs, hdr, hdr_size) == hdr_size)
82b9cf
 		r = TCRYPT_init_hdr(cd, hdr, params);
82b9cf
diff --git a/lib/utils.c b/lib/utils.c
82b9cf
index 2dcf753..802ba55 100644
82b9cf
--- a/lib/utils.c
82b9cf
+++ b/lib/utils.c
82b9cf
@@ -56,22 +56,70 @@ static void *aligned_malloc(void **base, int size, int alignment)
82b9cf
 /* Credits go to Michal's padlock patches for this alignment code */
82b9cf
 	char *ptr;
82b9cf
 
82b9cf
-	ptr  = malloc(size + alignment);
82b9cf
-	if(ptr == NULL) return NULL;
82b9cf
+	ptr = malloc(size + alignment);
82b9cf
+	if (!ptr)
82b9cf
+		return NULL;
82b9cf
 
82b9cf
 	*base = ptr;
82b9cf
-	if(alignment > 1 && ((long)ptr & (alignment - 1))) {
82b9cf
+	if (alignment > 1 && ((long)ptr & (alignment - 1)))
82b9cf
 		ptr += alignment - ((long)(ptr) & (alignment - 1));
82b9cf
-	}
82b9cf
+
82b9cf
 	return ptr;
82b9cf
 #endif
82b9cf
 }
82b9cf
 
82b9cf
+ssize_t read_buffer(int fd, void *buf, size_t count)
82b9cf
+{
82b9cf
+	size_t read_size = 0;
82b9cf
+	ssize_t r;
82b9cf
+
82b9cf
+	if (fd < 0 || !buf)
82b9cf
+		return -EINVAL;
82b9cf
+
82b9cf
+	do {
82b9cf
+		r = read(fd, buf, count - read_size);
82b9cf
+		if (r == -1 && errno != EINTR)
82b9cf
+			return r;
82b9cf
+		if (r == 0)
82b9cf
+			return (ssize_t)read_size;
82b9cf
+		if (r > 0) {
82b9cf
+			read_size += (size_t)r;
82b9cf
+			buf = (uint8_t*)buf + r;
82b9cf
+		}
82b9cf
+	} while (read_size != count);
82b9cf
+
82b9cf
+	return (ssize_t)count;
82b9cf
+}
82b9cf
+
82b9cf
+ssize_t write_buffer(int fd, const void *buf, size_t count)
82b9cf
+{
82b9cf
+	size_t write_size = 0;
82b9cf
+	ssize_t w;
82b9cf
+
82b9cf
+	if (fd < 0 || !buf || !count)
82b9cf
+		return -EINVAL;
82b9cf
+
82b9cf
+	do {
82b9cf
+		w = write(fd, buf, count - write_size);
82b9cf
+		if (w < 0 && errno != EINTR)
82b9cf
+			return w;
82b9cf
+		if (w == 0)
82b9cf
+			return (ssize_t)write_size;
82b9cf
+		if (w > 0) {
82b9cf
+			write_size += (size_t) w;
82b9cf
+			buf = (const uint8_t*)buf + w;
82b9cf
+		}
82b9cf
+	} while (write_size != count);
82b9cf
+
82b9cf
+	return (ssize_t)write_size;
82b9cf
+}
82b9cf
+
82b9cf
 ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
82b9cf
 {
82b9cf
 	void *hangover_buf, *hangover_buf_base = NULL;
82b9cf
 	void *buf, *buf_base = NULL;
82b9cf
-	int r, hangover, solid, alignment;
82b9cf
+	int r, alignment;
82b9cf
+	size_t hangover, solid;
82b9cf
 	ssize_t ret = -1;
82b9cf
 
82b9cf
 	if (fd == -1 || !orig_buf || bsize <= 0)
82b9cf
@@ -89,17 +137,19 @@ ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
82b9cf
 	} else
82b9cf
 		buf = orig_buf;
82b9cf
 
82b9cf
-	r = write(fd, buf, solid);
82b9cf
-	if (r < 0 || r != solid)
82b9cf
-		goto out;
82b9cf
+	if (solid) {
82b9cf
+		r = write_buffer(fd, buf, solid);
82b9cf
+		if (r < 0 || r != (ssize_t)solid)
82b9cf
+			goto out;
82b9cf
+	}
82b9cf
 
82b9cf
 	if (hangover) {
82b9cf
 		hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
82b9cf
 		if (!hangover_buf)
82b9cf
 			goto out;
82b9cf
 
82b9cf
-		r = read(fd, hangover_buf, bsize);
82b9cf
-		if (r < 0 || r < hangover)
82b9cf
+		r = read_buffer(fd, hangover_buf, bsize);
82b9cf
+		if (r < 0 || r < (ssize_t)hangover)
82b9cf
 			goto out;
82b9cf
 
82b9cf
 		if (r < bsize)
82b9cf
@@ -110,8 +160,8 @@ ssize_t write_blockwise(int fd, int bsize, void *orig_buf, size_t count)
82b9cf
 
82b9cf
 		memcpy(hangover_buf, (char*)buf + solid, hangover);
82b9cf
 
82b9cf
-		r = write(fd, hangover_buf, bsize);
82b9cf
-		if (r < 0 || r < hangover)
82b9cf
+		r = write_buffer(fd, hangover_buf, bsize);
82b9cf
+		if (r < 0 || r < (ssize_t)hangover)
82b9cf
 			goto out;
82b9cf
 	}
82b9cf
 	ret = count;
82b9cf
@@ -122,10 +172,12 @@ out:
82b9cf
 	return ret;
82b9cf
 }
82b9cf
 
82b9cf
-ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
82b9cf
+ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count)
82b9cf
+{
82b9cf
 	void *hangover_buf, *hangover_buf_base = NULL;
82b9cf
 	void *buf, *buf_base = NULL;
82b9cf
-	int r, hangover, solid, alignment;
82b9cf
+	int r, alignment;
82b9cf
+	size_t hangover, solid;
82b9cf
 	ssize_t ret = -1;
82b9cf
 
82b9cf
 	if (fd == -1 || !orig_buf || bsize <= 0)
82b9cf
@@ -142,16 +194,16 @@ ssize_t read_blockwise(int fd, int bsize, void *orig_buf, size_t count) {
82b9cf
 	} else
82b9cf
 		buf = orig_buf;
82b9cf
 
82b9cf
-	r = read(fd, buf, solid);
82b9cf
-	if(r < 0 || r != solid)
82b9cf
+	r = read_buffer(fd, buf, solid);
82b9cf
+	if (r < 0 || r != (ssize_t)solid)
82b9cf
 		goto out;
82b9cf
 
82b9cf
 	if (hangover) {
82b9cf
 		hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
82b9cf
 		if (!hangover_buf)
82b9cf
 			goto out;
82b9cf
-		r = read(fd, hangover_buf, bsize);
82b9cf
-		if (r <  0 || r < hangover)
82b9cf
+		r = read_buffer(fd, hangover_buf, bsize);
82b9cf
+		if (r <  0 || r < (ssize_t)hangover)
82b9cf
 			goto out;
82b9cf
 
82b9cf
 		memcpy((char *)buf + solid, hangover_buf, hangover);
82b9cf
@@ -172,7 +224,8 @@ out:
82b9cf
  * is implicitly included in the read/write offset, which can not be set to non-aligned
82b9cf
  * boundaries. Hence, we combine llseek with write.
82b9cf
  */
82b9cf
-ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t offset) {
82b9cf
+ssize_t write_lseek_blockwise(int fd, int bsize, void *buf, size_t count, off_t offset)
82b9cf
+{
82b9cf
 	char *frontPadBuf;
82b9cf
 	void *frontPadBuf_base = NULL;
82b9cf
 	int r, frontHang;
82b9cf
@@ -182,6 +235,12 @@ ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t
82b9cf
 	if (fd == -1 || !buf || bsize <= 0)
82b9cf
 		return -1;
82b9cf
 
82b9cf
+	if (offset < 0)
82b9cf
+		offset = lseek(fd, offset, SEEK_END);
82b9cf
+
82b9cf
+	if (offset < 0)
82b9cf
+		return -1;
82b9cf
+
82b9cf
 	frontHang = offset % bsize;
82b9cf
 
82b9cf
 	if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
82b9cf
@@ -193,7 +252,7 @@ ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t
82b9cf
 		if (!frontPadBuf)
82b9cf
 			goto out;
82b9cf
 
82b9cf
-		r = read(fd, frontPadBuf, bsize);
82b9cf
+		r = read_buffer(fd, frontPadBuf, bsize);
82b9cf
 		if (r < 0 || r != bsize)
82b9cf
 			goto out;
82b9cf
 
82b9cf
@@ -206,11 +265,11 @@ ssize_t write_lseek_blockwise(int fd, int bsize, char *buf, size_t count, off_t
82b9cf
 		if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
82b9cf
 			goto out;
82b9cf
 
82b9cf
-		r = write(fd, frontPadBuf, bsize);
82b9cf
+		r = write_buffer(fd, frontPadBuf, bsize);
82b9cf
 		if (r < 0 || r != bsize)
82b9cf
 			goto out;
82b9cf
 
82b9cf
-		buf += innerCount;
82b9cf
+		buf = (char*)buf + innerCount;
82b9cf
 		count -= innerCount;
82b9cf
 	}
82b9cf
 
82b9cf
@@ -223,6 +282,58 @@ out:
82b9cf
 	return ret;
82b9cf
 }
82b9cf
 
82b9cf
+ssize_t read_lseek_blockwise(int fd, int bsize, void *buf, size_t count, off_t offset)
82b9cf
+{
82b9cf
+	char *frontPadBuf;
82b9cf
+	void *frontPadBuf_base = NULL;
82b9cf
+	int r, frontHang;
82b9cf
+	size_t innerCount = 0;
82b9cf
+	ssize_t ret = -1;
82b9cf
+
82b9cf
+	if (fd == -1 || !buf || bsize <= 0)
82b9cf
+		return -1;
82b9cf
+
82b9cf
+	if (offset < 0)
82b9cf
+		offset = lseek(fd, offset, SEEK_END);
82b9cf
+
82b9cf
+	if (offset < 0)
82b9cf
+		return -1;
82b9cf
+
82b9cf
+	frontHang = offset % bsize;
82b9cf
+
82b9cf
+	if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
82b9cf
+		return ret;
82b9cf
+
82b9cf
+	if (frontHang) {
82b9cf
+		frontPadBuf = aligned_malloc(&frontPadBuf_base,
82b9cf
+					     bsize, get_alignment(fd));
82b9cf
+
82b9cf
+		if (!frontPadBuf)
82b9cf
+			return ret;
82b9cf
+
82b9cf
+		r = read_buffer(fd, frontPadBuf, bsize);
82b9cf
+		if (r < 0 || r != bsize)
82b9cf
+			goto out;
82b9cf
+
82b9cf
+		innerCount = bsize - frontHang;
82b9cf
+		if (innerCount > count)
82b9cf
+			innerCount = count;
82b9cf
+
82b9cf
+		memcpy(buf, frontPadBuf + frontHang, innerCount);
82b9cf
+
82b9cf
+		buf = (char*)buf + innerCount;
82b9cf
+		count -= innerCount;
82b9cf
+	}
82b9cf
+
82b9cf
+	ret = read_blockwise(fd, bsize, buf, count);
82b9cf
+	if (ret >= 0)
82b9cf
+		ret += innerCount;
82b9cf
+out:
82b9cf
+	free(frontPadBuf_base);
82b9cf
+
82b9cf
+	return ret;
82b9cf
+}
82b9cf
+
82b9cf
 /* MEMLOCK */
82b9cf
 #define DEFAULT_PROCESS_PRIORITY -18
82b9cf
 
82b9cf
-- 
82b9cf
2.7.4
82b9cf