Skip to contentSkip to conversation

hugetlbfs: avoid overflow in hugetlbfs_fallocate

PATCH Superseded yangerkun 2 participants 3 messages Nov 24, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

yangerkun

author
#0 lore raw

PATCH 1/1 hugetlbfs: avoid overflow in hugetlbfs_fallocate

luojiajun report a problem[1] two years ago which seems still exists in mainline. vfs_fallocate can avoid 'offset + len' trigger overflow, but 'offset + len + hpage_size - 1' may overflow too and will lead to a wrong 'end'. luojiajun give a solution which can fix the wrong 'end' but leave the overflow still happened. We should fix it by transfer 'offset' to unsigned long long.

[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

---
 fs/hugetlbfs/inode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +2 -1
index 49d2e686be74..8012a14901de 100644
@@ -651,7 +651,8 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 	 * as well as being converted to page offsets.
 	 */
 	start = offset >> hpage_shift;
-	end = (offset + len + hpage_size - 1) >> hpage_shift;
+	end = ((unsigned long long)offset + len + hpage_size - 1)
+		>> hpage_shift;
 
 	inode_lock(inode);
 
Signature · 1 line
2.31.1
#1 lore raw

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 655

Quoted 4 lines · yangerkun
fs/hugetlbfs/inode.c +2 -1
 	start = offset >> hpage_shift;
-	end = (offset + len + hpage_size - 1) >> hpage_shift;
+	end = ((unsigned long long)offset + len + hpage_size - 1)
+		>> hpage_shift;

+ end = DIV_ROUND_UP_ULL(offset + len, hpage_size);

author
#2 lore raw

Review

Quoted 7 lines · Matthew Wilcox
Quoted 4 lines · yangerkun
  	start = offset >> hpage_shift;
fs/hugetlbfs/inode.c +2 -1
-	end = (offset + len + hpage_size - 1) >> hpage_shift;
+	end = ((unsigned long long)offset + len + hpage_size - 1)
+		>> hpage_shift;
(quoted diff) +1 -0
+	end = DIV_ROUND_UP_ULL(offset + len, hpage_size);

Thanks, will do it in v2!

Quoted 2 lines

.