diff options
author | Mike Kravetz <mike.kravetz@oracle.com> | 2017-04-13 17:56:32 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-04-13 21:24:21 -0400 |
commit | 045c7a3f53d9403b62d396b6d051c4be5044cdb4 (patch) | |
tree | b869244cf7a46f3fe5aa25154eda7a1d8c751217 | |
parent | 5b7abeae3af8c08c577e599dd0578b9e3ee6687b (diff) |
hugetlbfs: fix offset overflow in hugetlbfs mmap
If mmap() maps a file, it can be passed an offset into the file at which
the mapping is to start. Offset could be a negative value when
represented as a loff_t. The offset plus length will be used to update
the file size (i_size) which is also a loff_t.
Validate the value of offset and offset + length to make sure they do
not overflow and appear as negative.
Found by syzcaller with commit ff8c0c53c475 ("mm/hugetlb.c: don't call
region_abort if region_chg fails") applied. Prior to this commit, the
overflow would still occur but we would luckily return ENOMEM.
To reproduce:
mmap(0, 0x2000, 0, 0x40021, 0xffffffffffffffffULL, 0x8000000000000000ULL);
Resulted in,
kernel BUG at mm/hugetlb.c:742!
Call Trace:
hugetlbfs_evict_inode+0x80/0xa0
evict+0x24a/0x620
iput+0x48f/0x8c0
dentry_unlink_inode+0x31f/0x4d0
__dentry_kill+0x292/0x5e0
dput+0x730/0x830
__fput+0x438/0x720
____fput+0x1a/0x20
task_work_run+0xfe/0x180
exit_to_usermode_loop+0x133/0x150
syscall_return_slowpath+0x184/0x1c0
entry_SYSCALL_64_fastpath+0xab/0xad
Fixes: ff8c0c53c475 ("mm/hugetlb.c: don't call region_abort if region_chg fails")
Link: http://lkml.kernel.org/r/1491951118-30678-1-git-send-email-mike.kravetz@oracle.com
Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Acked-by: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: "Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | fs/hugetlbfs/inode.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 7163fe014b57..dde861387a40 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c | |||
@@ -136,17 +136,26 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
136 | vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND; | 136 | vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND; |
137 | vma->vm_ops = &hugetlb_vm_ops; | 137 | vma->vm_ops = &hugetlb_vm_ops; |
138 | 138 | ||
139 | /* | ||
140 | * Offset passed to mmap (before page shift) could have been | ||
141 | * negative when represented as a (l)off_t. | ||
142 | */ | ||
143 | if (((loff_t)vma->vm_pgoff << PAGE_SHIFT) < 0) | ||
144 | return -EINVAL; | ||
145 | |||
139 | if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT)) | 146 | if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT)) |
140 | return -EINVAL; | 147 | return -EINVAL; |
141 | 148 | ||
142 | vma_len = (loff_t)(vma->vm_end - vma->vm_start); | 149 | vma_len = (loff_t)(vma->vm_end - vma->vm_start); |
150 | len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | ||
151 | /* check for overflow */ | ||
152 | if (len < vma_len) | ||
153 | return -EINVAL; | ||
143 | 154 | ||
144 | inode_lock(inode); | 155 | inode_lock(inode); |
145 | file_accessed(file); | 156 | file_accessed(file); |
146 | 157 | ||
147 | ret = -ENOMEM; | 158 | ret = -ENOMEM; |
148 | len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); | ||
149 | |||
150 | if (hugetlb_reserve_pages(inode, | 159 | if (hugetlb_reserve_pages(inode, |
151 | vma->vm_pgoff >> huge_page_order(h), | 160 | vma->vm_pgoff >> huge_page_order(h), |
152 | len >> huge_page_shift(h), vma, | 161 | len >> huge_page_shift(h), vma, |
@@ -155,7 +164,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
155 | 164 | ||
156 | ret = 0; | 165 | ret = 0; |
157 | if (vma->vm_flags & VM_WRITE && inode->i_size < len) | 166 | if (vma->vm_flags & VM_WRITE && inode->i_size < len) |
158 | inode->i_size = len; | 167 | i_size_write(inode, len); |
159 | out: | 168 | out: |
160 | inode_unlock(inode); | 169 | inode_unlock(inode); |
161 | 170 | ||