Skip to content

Commit ab8f879

Browse files
committed
mm/hugetlb: make detecting shared pte more reliable
jira VULN-46929 cve-bf CVE-2024-57883 commit-author Miaohe Lin <linmiaohe@huawei.com> commit 3aa4ed8 upstream-diff Accounted for e95a985 not being backported to ciqlts9_2 - dropped the unnecessary braces in a one-statement `if' conditional. If the pagetables are shared, we shouldn't copy or take references. Since src could have unshared and dst shares with another vma, huge_pte_none() is thus used to determine whether dst_pte is shared. But this check isn't reliable. A shared pte could have pte none in pagetable in fact. The page count of ptep page should be checked here in order to reliably determine whether pte is shared. [lukas.bulwahn@gmail.com: remove unused local variable dst_entry in copy_hugetlb_page_range()] Link: https://lkml.kernel.org/r/20220822082525.26071-1-lukas.bulwahn@gmail.com Link: https://lkml.kernel.org/r/20220816130553.31406-7-linmiaohe@huawei.com Signed-off-by: Miaohe Lin <linmiaohe@huawei.com> Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit 3aa4ed8) Signed-off-by: Marcin Wcisło <marcin.wcislo@conclusive.pl>
1 parent 40ea049 commit ab8f879

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

mm/hugetlb.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,7 +4769,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
47694769
struct vm_area_struct *dst_vma,
47704770
struct vm_area_struct *src_vma)
47714771
{
4772-
pte_t *src_pte, *dst_pte, entry, dst_entry;
4772+
pte_t *src_pte, *dst_pte, entry;
47734773
struct page *ptepage;
47744774
unsigned long addr;
47754775
bool cow = is_cow_mapping(src_vma->vm_flags);
@@ -4810,28 +4810,23 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
48104810

48114811
/*
48124812
* If the pagetables are shared don't copy or take references.
4813-
* dst_pte == src_pte is the common case of src/dest sharing.
48144813
*
4814+
* dst_pte == src_pte is the common case of src/dest sharing.
48154815
* However, src could have 'unshared' and dst shares with
4816-
* another vma. If dst_pte !none, this implies sharing.
4817-
* Check here before taking page table lock, and once again
4818-
* after taking the lock below.
4816+
* another vma. So page_count of ptep page is checked instead
4817+
* to reliably determine whether pte is shared.
48194818
*/
4820-
dst_entry = huge_ptep_get(dst_pte);
4821-
if ((dst_pte == src_pte) || !huge_pte_none(dst_entry))
4819+
if (page_count(virt_to_page(dst_pte)) > 1)
48224820
continue;
48234821

48244822
dst_ptl = huge_pte_lock(h, dst, dst_pte);
48254823
src_ptl = huge_pte_lockptr(h, src, src_pte);
48264824
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
48274825
entry = huge_ptep_get(src_pte);
4828-
dst_entry = huge_ptep_get(dst_pte);
48294826
again:
4830-
if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
4827+
if (huge_pte_none(entry)) {
48314828
/*
4832-
* Skip if src entry none. Also, skip in the
4833-
* unlikely case dst entry !none as this implies
4834-
* sharing with another vma.
4829+
* Skip if src entry none.
48354830
*/
48364831
;
48374832
} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
@@ -4911,7 +4906,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
49114906
restore_reserve_on_error(h, dst_vma, addr,
49124907
new);
49134908
put_page(new);
4914-
/* dst_entry won't change as in child */
4909+
/* huge_ptep of dst_pte won't change as in child */
49154910
goto again;
49164911
}
49174912
hugetlb_install_page(dst_vma, dst_pte, addr, new);

0 commit comments

Comments
 (0)