Skip to contentSkip to conversation

fs: Fix overflow in block_page_mkwrite

PATCH In review Andreas Gruenbacher 3 participants 4 messages Nov 6, 2019 – Dec 18, 2019 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: Fix overflow in block_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/buffer.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/buffer.c +5 -5
index 86a38b979323..da3f33b70249 100644
@@ -2459,21 +2459,21 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
 	struct page *page = vmf->page;
 	struct inode *inode = file_inode(vma->vm_file);
 	unsigned long end;
-	loff_t size;
+	loff_t offset, size;
 	int ret;
 
 	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)
-		end = size & ~PAGE_MASK;
+	if (offset > size - PAGE_SIZE)
+		end = offset_in_page(size);
 	else
 		end = PAGE_SIZE;
 
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Andreas Gruenbacher

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.

Looks good modulo the s/ssize_t/loff_t/ mentioned in the other patch:

reviewer
#2 lore raw
In reply to Andreas Gruenbacher
Quoted 5 lines · Andreas Gruenbacher

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.

This patch seems to have fallen through the cracks? Al?

Honza

Quoted 38 lines
---
 fs/buffer.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/buffer.c +5 -5
index 86a38b979323..da3f33b70249 100644
@@ -2459,21 +2459,21 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
 	struct page *page = vmf->page;
 	struct inode *inode = file_inode(vma->vm_file);
 	unsigned long end;
-	loff_t size;
+	loff_t offset, size;
 	int ret;
 
 	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)
-		end = size & ~PAGE_MASK;
+	if (offset > size - PAGE_SIZE)
+		end = offset_in_page(size);
 	else
 		end = PAGE_SIZE;
 
Signature · 1 line
2.20.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Hi Jan,

Quoted 8 lines · Jan Kara
Quoted 5 lines · Andreas Gruenbacher

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.

This patch seems to have fallen through the cracks? Al?

There's been a v2 of this patch [*] and I'm about to send out a v3, so please ignore this first version.

Thanks,
Andreas

[*] https://lore.kernel.org/linux-fsdevel/[email protected]/