Skip to contentSkip to conversation

iomap: Fix overflow in iomap_page_mkwrite

PATCH In review Andreas Gruenbacher 3 participants 6 messages Nov 6, 2019 – Nov 7, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iomap: Fix overflow in iomap_page_mkwrite

On architectures where ssize_t is wider than pgoff_t, the expression ((page->index + 1) << PAGE_SHIFT) can overflow. Rewrite to use the page offset, which we already compute here anyway.

---
 fs/iomap/buffered-io.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/iomap/buffered-io.c +3 -4
index e25901ae3ff4..a30ea7ecb790 100644
@@ -1040,20 +1040,19 @@ vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
 
 	lock_page(page);
 	size = i_size_read(inode);
-	if ((page->mapping != inode->i_mapping) ||
-	    (page_offset(page) > size)) {
+	offset = page_offset(page);
+	if (page->mapping != inode->i_mapping || offset > size) {
 		/* We overload EFAULT to mean page got truncated */
 		ret = -EFAULT;
 		goto out_unlock;
 	}
 
 	/* page is wholly or partially inside EOF */
-	if (((page->index + 1) << PAGE_SHIFT) > size)
+	if (offset > size - PAGE_SIZE)
 		length = offset_in_page(size);
 	else
 		length = PAGE_SIZE;
 
-	offset = page_offset(page);
 	while (length > 0) {
 		ret = iomap_apply(inode, offset, length,
 				IOMAP_WRITE | IOMAP_FAULT, ops, page,
Signature · 1 line
2.20.1
#1 lore raw
Quoted 1 line · Andreas Gruenbacher

On architectures where ssize_t is wider than pgoff_t, the expression

ssize_t? But you're changing @offset, which is loff_t. I'm confused.

Also, which architectures are you talking about here?

--D

Quoted 39 lines

((page->index + 1) << PAGE_SHIFT) can overflow. Rewrite to use the page offset, which we already compute here anyway.

---
 fs/iomap/buffered-io.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/iomap/buffered-io.c +3 -4
index e25901ae3ff4..a30ea7ecb790 100644
@@ -1040,20 +1040,19 @@ vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
 
 	lock_page(page);
 	size = i_size_read(inode);
-	if ((page->mapping != inode->i_mapping) ||
-	    (page_offset(page) > size)) {
+	offset = page_offset(page);
+	if (page->mapping != inode->i_mapping || offset > size) {
 		/* We overload EFAULT to mean page got truncated */
 		ret = -EFAULT;
 		goto out_unlock;
 	}
 
 	/* page is wholly or partially inside EOF */
-	if (((page->index + 1) << PAGE_SHIFT) > size)
+	if (offset > size - PAGE_SIZE)
 		length = offset_in_page(size);
 	else
 		length = PAGE_SIZE;
 
-	offset = page_offset(page);
 	while (length > 0) {
 		ret = iomap_apply(inode, offset, length,
 				IOMAP_WRITE | IOMAP_FAULT, ops, page,
Signature · 1 line
2.20.1
#2 lore raw
Quoted 4 lines · Darrick J. Wong
Quoted 1 line · Andreas Gruenbacher

On architectures where ssize_t is wider than pgoff_t, the expression

ssize_t? But you're changing @offset, which is loff_t. I'm confused.

Oops, should have been loff_t instead of ssize_t.

Quoted 1 line

Also, which architectures are you talking about here?

From the kernel headers:

#define pgoff_t unsigned long
typedef long long __kernel_loff_t;
typedef __kernel_loff_t loff_t;

So for example, sizeof(loff_t) > sizeof(pgoff_t) on x86.

Thanks,
Andreas

Reviewed-by
#3 lore raw
Quoted 7 lines · Andreas Gruenbacher
Quoted 4 lines · Darrick J. Wong
Quoted 1 line · Andreas Gruenbacher

On architectures where ssize_t is wider than pgoff_t, the expression

ssize_t? But you're changing @offset, which is loff_t. I'm confused.

Oops, should have been loff_t instead of ssize_t.

I'll fix it on commit.

Quoted 9 lines
Quoted 1 line

Also, which architectures are you talking about here?

From the kernel headers:

#define pgoff_t unsigned long
typedef long long __kernel_loff_t;
typedef __kernel_loff_t loff_t;

So for example, sizeof(loff_t) > sizeof(pgoff_t) on x86.

Ok, that's what I thought.

--D

Quoted 3 lines

Thanks,
Andreas

#4 lore raw
Quoted 1 line · Darrick J. Wong

I'll fix it on commit.

Thanks.

So now the one remaining issue I have with those two functions is why we check for (offset > size) instead of (offset >= size) in

  if (page->mapping != inode->i_mapping || offset > size)

When (offset == size), we're clearly outside the page, and so we should fail?

Thanks,
Andreas