Skip to contentSkip to conversation

btrfs: Change iov_iter_alignment_iovec to check iovec count before accesses an iovec

PATCH Superseded Dennis Lam 1 participant 2 messages Jul 21, 2024

Subsystems (2)
lore
Share & tools
lore

Dennis Lam

author
#0 lore raw

PATCH 1/1 btrfs: Change iov_iter_alignment_iovec to check iovec count before accesses an iovec

When checking iov_iter_alignment_iovec for valid read and the iovec is empty, the function still tries to iterate non-existant values. In this case, the iov variable is empty and attempting to use iov->iov_len causes a kernel crash.

This patch fixes this by checking the size of the iovec before iterating.

---
 lib/iov_iter.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/iov_iter.c +2 -2
index 4a6a9f419bd7..2d82ecf1b622 100644
@@ -783,7 +783,7 @@ static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 	size_t size = i->count;
 	size_t skip = i->iov_offset;
 
-	do {
+	while(size) {
 		size_t len = iov->iov_len - skip;
 		if (len) {
 			res |= (unsigned long)iov->iov_base + skip;
@@ -794,7 +794,7 @@ static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 		}
 		iov++;
 		skip = 0;
-	} while (size);
+	} 
 	return res;
 }
 
Signature · 1 line
2.45.2
author
#1 lore raw

PATCH RESEND 1/1 btrfs: Change iov_iter_alignment_iovec to check iovec count before accesses an iovec

When checking iov_iter_alignment_iovec for valid read and the iovec is empty, the function still tries to iterate non-existant values. In this case, the iov variable is empty and attempting to use iov->iov_len causes a kernel crash.

This patch fixes this by checking the size of the iovec before iterating.

Resent this patch in thread because one of the emails I pasted was wrong.

---
 lib/iov_iter.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/iov_iter.c +2 -2
index 4a6a9f419bd7..2d82ecf1b622 100644
@@ -783,7 +783,7 @@ static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 	size_t size = i->count;
 	size_t skip = i->iov_offset;
 
-	do {
+	while(size) {
 		size_t len = iov->iov_len - skip;
 		if (len) {
 			res |= (unsigned long)iov->iov_base + skip;
@@ -794,7 +794,7 @@ static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 		}
 		iov++;
 		skip = 0;
-	} while (size);
+	} 
 	return res;
 }
 
Signature · 1 line
2.45.2