Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp913034ybp; Wed, 9 Oct 2019 06:13:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzBqsVkoDrkZmR+ruc60I74n0/7o5y8rq+cGCPOht2WIIZlixZsw9A2BSVTD2CxhYDCZgT X-Received: by 2002:a05:6402:150f:: with SMTP id f15mr3016593edw.240.1570626832309; Wed, 09 Oct 2019 06:13:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570626832; cv=none; d=google.com; s=arc-20160816; b=AWgQgyQ7v3FZC+YhL7xLBMib1F9zRYLJAs7GMLyVQJbSnTvmCbv/l9nluomlmrkKaY mb7k/7ThBEQrNhF6B7ZlUT2Ci957ICCaH9lo8zBsHfbKePu9fSz3vkC5MjDfxEO23X0P GW6l9TPKuPY8UHIqUyaURWOIw2RrdppTII9vuE7qfj+ZubvfJhTeD6muzluey0QG7JSd pTwPmr1UejLA1Jl2aswQg746Mxvd+rJJSDbgFBSC/siOYDOwc31czgSKM/fkrw1Ir92T /Bclk96324gVgTlmMKvSCBD0hnZzESsWN8zdJXVnawtHkZ4B/rKQccCS6dW53w4CuyAA M2Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:from:references:cc:to:subject; bh=nl1hPemravVtb70HHEd1FhKK0pQ699BU8ZRoufEXu/Q=; b=S+lOR3zG/pLsIwzlXkkmZRFT6zT++k7kqW878DPnBiADTaC5mfE5rFCe7mZDIOkQu9 ofCY8/RYoeR945nuTFr6NOvzKULu4smjB8tlJ/f7/RNM5NdXAfpIaAA+IdEkq5V65067 CNYmtgBdxpiX0bma1MjYiLl27zSiYySkGh63V+/nc77uC8Fe1LJs+iE28c/4sWuXeX1e tuTV5UYwgEkFyu9SjulkjLZwxoPfeqoqOADpYzPhjSiXVENFgbmCEQFfqPJxgt6LyF24 /U4HRjnHMY2N451wSATiognJ9xIlbF5lTxm3RC4skMjTqOp1iR3VRElitC9on7xNESjI Y6+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id hk1si1097488ejb.408.2019.10.09.06.13.28; Wed, 09 Oct 2019 06:13:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731296AbfJINNR (ORCPT + 99 others); Wed, 9 Oct 2019 09:13:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:37200 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731145AbfJINNR (ORCPT ); Wed, 9 Oct 2019 09:13:17 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id DD171ABCB; Wed, 9 Oct 2019 13:13:13 +0000 (UTC) Subject: Re: [PATCH v2 6/8] mm: prevent get_user_pages() from overflowing page refcount To: Ajay Kaher , gregkh@linuxfoundation.org Cc: torvalds@linux-foundation.org, punit.agrawal@arm.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, willy@infradead.org, will.deacon@arm.com, mszeredi@redhat.com, stable@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, srivatsab@vmware.com, srivatsa@csail.mit.edu, amakhalov@vmware.com, srinidhir@vmware.com, bvikas@vmware.com, anishs@vmware.com, vsirnapalli@vmware.com, srostedt@vmware.com, stable@kernel.org, Ben Hutchings References: <1570581863-12090-1-git-send-email-akaher@vmware.com> <1570581863-12090-7-git-send-email-akaher@vmware.com> From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz; prefer-encrypt=mutual; keydata= mQINBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9IF/R28zmh0ifLXyJQQz5ofdj4bPf8ecEW0rhcqHfTD8k4yK0xxt3xW+6Exqp9n9bydiy tcSAw/TahjW6yrA+6JhSBv1v2tIm+itQc073zjSX8OFL51qQVzRFr7H2UQG33lw2QrvHRXqD Ot7ViKam7v0Ho9wEWiQOOZlHItOOXFphWb2yq3nzrKe45oWoSgkxKb97MVsQ+q2SYjJRBBH4 8qKhphADYxkIP6yut/eaj9ImvRUZZRi0DTc8xfnvHGTjKbJzC2xpFcY0DQbZzuwsIZ8OPJCc LM4S7mT25NE5kUTG/TKQCk922vRdGVMoLA7dIQrgXnRXtyT61sg8PG4wcfOnuWf8577aXP1x 6mzw3/jh3F+oSBHb/GcLC7mvWreJifUL2gEdssGfXhGWBo6zLS3qhgtwjay0Jl+kza1lo+Cv BB2T79D4WGdDuVa4eOrQ02TxqGN7G0Biz5ZLRSFzQSQwLn8fbwARAQABtCBWbGFzdGltaWwg QmFia2EgPHZiYWJrYUBzdXNlLmN6PokCVAQTAQoAPgIbAwULCQgHAwUVCgkICwUWAgMBAAIe AQIXgBYhBKlA1DSZLC6OmRA9UCJPp+fMgqZkBQJcbbyGBQkH8VTqAAoJECJPp+fMgqZkpGoP /1jhVihakxw1d67kFhPgjWrbzaeAYOJu7Oi79D8BL8Vr5dmNP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vJOxv6XD6xcTtj5Az8t2JjdUX6SF9hxJpwhBU0wrCoGDkWp4Bbv6jnF7zP Nzftr4l8RuJoywDIiJpdaNpSlXKpj/K6KrnyAI/joYc7 Message-ID: Date: Wed, 9 Oct 2019 15:13:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.0 MIME-Version: 1.0 In-Reply-To: <1570581863-12090-7-git-send-email-akaher@vmware.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/9/19 2:44 AM, Ajay Kaher wrote: > From: Linus Torvalds > > commit 8fde12ca79aff9b5ba951fce1a2641901b8d8e64 upstream. > > If the page refcount wraps around past zero, it will be freed while > there are still four billion references to it. One of the possible > avenues for an attacker to try to make this happen is by doing direct IO > on a page multiple times. This patch makes get_user_pages() refuse to > take a new page reference if there are already more than two billion > references to the page. > > Reported-by: Jann Horn > Acked-by: Matthew Wilcox > Cc: stable@kernel.org > Signed-off-by: Linus Torvalds > [ 4.4.y backport notes: > Ajay: Added local variable 'err' with-in follow_hugetlb_page() > from 2be7cfed995e, to resolve compilation error > Srivatsa: Replaced call to get_page_foll() with try_get_page_foll() ] > Signed-off-by: Srivatsa S. Bhat (VMware) > Signed-off-by: Ajay Kaher > --- > mm/gup.c | 43 ++++++++++++++++++++++++++++++++----------- > mm/hugetlb.c | 16 +++++++++++++++- > 2 files changed, 47 insertions(+), 12 deletions(-) This seems to have the same issue as the 4.9 stable version [1], in not touching the arch-specific gup.c variants. [1] https://lore.kernel.org/lkml/6650323f-dbc9-f069-000b-f6b0f941a065@suse.cz/ > diff --git a/mm/gup.c b/mm/gup.c > index fae4d1e..171b460 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -126,8 +126,12 @@ retry: > } > } > > - if (flags & FOLL_GET) > - get_page_foll(page); > + if (flags & FOLL_GET) { > + if (unlikely(!try_get_page_foll(page))) { > + page = ERR_PTR(-ENOMEM); > + goto out; > + } > + } > if (flags & FOLL_TOUCH) { > if ((flags & FOLL_WRITE) && > !pte_dirty(pte) && !PageDirty(page)) > @@ -289,7 +293,10 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address, > goto unmap; > *page = pte_page(*pte); > } > - get_page(*page); > + if (unlikely(!try_get_page(*page))) { > + ret = -ENOMEM; > + goto unmap; > + } > out: > ret = 0; > unmap: > @@ -1053,6 +1060,20 @@ struct page *get_dump_page(unsigned long addr) > */ > #ifdef CONFIG_HAVE_GENERIC_RCU_GUP > > +/* > + * Return the compund head page with ref appropriately incremented, > + * or NULL if that failed. > + */ > +static inline struct page *try_get_compound_head(struct page *page, int refs) > +{ > + struct page *head = compound_head(page); > + if (WARN_ON_ONCE(atomic_read(&head->_count) < 0)) > + return NULL; > + if (unlikely(!page_cache_add_speculative(head, refs))) > + return NULL; > + return head; > +} > + > #ifdef __HAVE_ARCH_PTE_SPECIAL > static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > int write, struct page **pages, int *nr) > @@ -1082,9 +1103,9 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > > VM_BUG_ON(!pfn_valid(pte_pfn(pte))); > page = pte_page(pte); > - head = compound_head(page); > > - if (!page_cache_get_speculative(head)) > + head = try_get_compound_head(page, 1); > + if (!head) > goto pte_unmap; > > if (unlikely(pte_val(pte) != pte_val(*ptep))) { > @@ -1141,8 +1162,8 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, > refs++; > } while (addr += PAGE_SIZE, addr != end); > > - head = compound_head(pmd_page(orig)); > - if (!page_cache_add_speculative(head, refs)) { > + head = try_get_compound_head(pmd_page(orig), refs); > + if (!head) { > *nr -= refs; > return 0; > } > @@ -1187,8 +1208,8 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, > refs++; > } while (addr += PAGE_SIZE, addr != end); > > - head = compound_head(pud_page(orig)); > - if (!page_cache_add_speculative(head, refs)) { > + head = try_get_compound_head(pud_page(orig), refs); > + if (!head) { > *nr -= refs; > return 0; > } > @@ -1229,8 +1250,8 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, > refs++; > } while (addr += PAGE_SIZE, addr != end); > > - head = compound_head(pgd_page(orig)); > - if (!page_cache_add_speculative(head, refs)) { > + head = try_get_compound_head(pgd_page(orig), refs); > + if (!head) { > *nr -= refs; > return 0; > } > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index fd932e7..3a1501e 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -3886,6 +3886,7 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, > unsigned long vaddr = *position; > unsigned long remainder = *nr_pages; > struct hstate *h = hstate_vma(vma); > + int err = -EFAULT; > > while (vaddr < vma->vm_end && remainder) { > pte_t *pte; > @@ -3957,6 +3958,19 @@ long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, > > pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; > page = pte_page(huge_ptep_get(pte)); > + > + /* > + * Instead of doing 'try_get_page_foll()' below in the same_page > + * loop, just check the count once here. > + */ > + if (unlikely(page_count(page) <= 0)) { > + if (pages) { > + spin_unlock(ptl); > + remainder = 0; > + err = -ENOMEM; > + break; > + } > + } > same_page: > if (pages) { > pages[i] = mem_map_offset(page, pfn_offset); > @@ -3983,7 +3997,7 @@ same_page: > *nr_pages = remainder; > *position = vaddr; > > - return i ? i : -EFAULT; > + return i ? i : err; > } > > unsigned long hugetlb_change_protection(struct vm_area_struct *vma, >