From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758535AbcIURn2 (ORCPT ); Wed, 21 Sep 2016 13:43:28 -0400 Received: from mail-yw0-f182.google.com ([209.85.161.182]:32780 "EHLO mail-yw0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753954AbcIURn1 (ORCPT ); Wed, 21 Sep 2016 13:43:27 -0400 Subject: Re: [PATCH] arm64: Correctly bounds check virt_addr_valid To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Mark Rutland References: <1474478928-25022-1-git-send-email-labbott@redhat.com> Cc: Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org From: Laura Abbott Message-ID: <2d9d63c9-ce12-d9b8-205a-19fcb72d846b@redhat.com> Date: Wed, 21 Sep 2016 10:43:23 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 In-Reply-To: <1474478928-25022-1-git-send-email-labbott@redhat.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/21/2016 10:28 AM, Laura Abbott wrote: > virt_addr_valid is supposed to return true if and only if virt_to_page > returns a valid page structure. The current macro does math on whatever > address is given and passes that to pfn_valid to verify. vmalloc and > module addresses can happen to generate a pfn that 'happens' to be > valid. Fix this by only performing the pfn_valid check on addresses that > have the potential to be valid. > > Signed-off-by: Laura Abbott > --- > This caused a bug at least twice in hardened usercopy so it is an > actual problem. A further TODO is full DEBUG_VIRTUAL support to > catch these types of mistakes. > --- > arch/arm64/include/asm/memory.h | 6 +++--- > 1 file changed, 3 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h > index 31b7322..f741e19 100644 > --- a/arch/arm64/include/asm/memory.h > +++ b/arch/arm64/include/asm/memory.h > @@ -214,7 +214,7 @@ static inline void *phys_to_virt(phys_addr_t x) > > #ifndef CONFIG_SPARSEMEM_VMEMMAP > #define virt_to_page(kaddr) pfn_to_page(__pa(kaddr) >> PAGE_SHIFT) > -#define virt_addr_valid(kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT) > +#define virt_addr_valid(kaddr) (((u64)kaddr) >= PAGE_OFFSET && pfn_valid(__pa(kaddr) >> PAGE_SHIFT)) > #else > #define __virt_to_pgoff(kaddr) (((u64)(kaddr) & ~PAGE_OFFSET) / PAGE_SIZE * sizeof(struct page)) > #define __page_to_voff(kaddr) (((u64)(page) & ~VMEMMAP_START) * PAGE_SIZE / sizeof(struct page)) > @@ -222,8 +222,8 @@ static inline void *phys_to_virt(phys_addr_t x) > #define page_to_virt(page) ((void *)((__page_to_voff(page)) | PAGE_OFFSET)) > #define virt_to_page(vaddr) ((struct page *)((__virt_to_pgoff(vaddr)) | VMEMMAP_START)) > > -#define virt_addr_valid(kaddr) pfn_valid((((u64)(kaddr) & ~PAGE_OFFSET) \ > - + PHYS_OFFSET) >> PAGE_SHIFT) > +#define virt_addr_valid(kaddr) (((u64)kaddr) >= PAGE_OFFSET && pfn_valid((((u64)(kaddr) & ~PAGE_OFFSET) \ > + + PHYS_OFFSET) >> PAGE_SHIFT)) > #endif > #endif > > Bah, I realized I butchered the macro parenthesization. I'll fix that in a v2. I'll wait for comments on this first. Thanks, Laura