Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp673522pxb; Thu, 9 Sep 2021 09:26:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBF5WLpYr1jE6p6AAsvuZg4qPdzAEBw/i8LbjSCuxAinhoBywKOTKONn6TcmKgRRZFaJC1 X-Received: by 2002:a17:906:63ca:: with SMTP id u10mr4297145ejk.411.1631204782276; Thu, 09 Sep 2021 09:26:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631204782; cv=none; d=google.com; s=arc-20160816; b=Wscz3CdUlLmdiuv8dmPs2p5dJpwDr8dZmy6LrQfbFRunC3js+CNoulLJRpy8Xfhbuc pHKpbv99XOzzD+3mLCbr8p41/9buXnIjVnsq6Ae+rEFZw0Ij2/CWnl/rc9c5rzpf2Pr0 6n38vw9fYgwWiWkcpTWZKApH6dfwQ5KZKYE7/7PrctnboILWTk/fmZoxQQfz7Jp3/f+N XHxgnvD9hHuQ1mxCCJl8V4pCxHAFXlGYTG19m3R3Kh8zqzU9j6J5Ru0wcVG8UIgiQ0kw Ne0huGfIIhHHleD72q7XUC9Ppdvk+lCvnoQ4rWVBPMV0QBWznBJ69T45dkRxpefYU70r QT9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JN1gh/b5OY5DLuaHK7ojF2josCVOG87yIDqVi/iwvJY=; b=cXYjMC74Gyet7khl3cZ0Z/fX7v1/3rQsz4bVBVTt1lBLYquqVpEJBGw6+JEthfVH/n Q4ory5m7EEHDzcXIY1P0M1CY+MrSh9yl376TR/QFyd5cu0We1qdpUOfmD6ti8RNX6sTM dZKWTfajwc2b/WUBICh4GXbh5aaFV64T0JGptQSjk2QC8ZT2VVmonXjaoRBoiud+WJ6Q jD14J2DtFS7Z1k03bj7en00xQdu2GTvtWvNpwd7+XWm65xITRsRdBJLGhSm9mdhbkj2G f4gbq2Svmy41WUr7hvXSrXORtoqRVfeOYE8eK1ciFcVtfLv65pvpnB802JqlBJrY6Eg0 c80Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IlknmZ1I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e11si2259467ejs.185.2021.09.09.09.25.50; Thu, 09 Sep 2021 09:26:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IlknmZ1I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237256AbhIIQZR (ORCPT + 99 others); Thu, 9 Sep 2021 12:25:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:57536 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237069AbhIIQYx (ORCPT ); Thu, 9 Sep 2021 12:24:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631204623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JN1gh/b5OY5DLuaHK7ojF2josCVOG87yIDqVi/iwvJY=; b=IlknmZ1I5I4Pf3lZCZIVsk2zaJQNzE9tl9inE6SYWrf80dpWqGil3RRwlyfmAM6QKA8EDh 4ibYCeyNyksgehvwZFD9BETFmkPhY5pUnln0yqHJ69T5mophszbmA97tLOcujxYgDYtfk0 JJ2Qozv2Cx8F4MWwe/zHZ8Xr9hOXHHI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-359-Et78AoGNN7u6gQGCcIj0hQ-1; Thu, 09 Sep 2021 12:23:42 -0400 X-MC-Unique: Et78AoGNN7u6gQGCcIj0hQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EAA9910919F9; Thu, 9 Sep 2021 16:23:03 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.233]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1C2B18FD2; Thu, 9 Sep 2021 16:23:00 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-s390@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Cornelia Huck , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Niklas Schnelle , Gerald Schaefer , Ulrich Weigand Subject: [PATCH resend RFC 3/9] s390/mm: validate VMA in PGSTE manipulation functions Date: Thu, 9 Sep 2021 18:22:42 +0200 Message-Id: <20210909162248.14969-4-david@redhat.com> In-Reply-To: <20210909162248.14969-1-david@redhat.com> References: <20210909162248.14969-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We should not walk/touch page tables outside of VMA boundaries when holding only the mmap sem in read mode. Evil user space can modify the VMA layout just before this function runs and e.g., trigger races with page table removal code since commit dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap"). gfn_to_hva() will only translate using KVM memory regions, but won't validate the VMA. Further, we should not allocate page tables outside of VMA boundaries: if evil user space decides to map hugetlbfs to these ranges, bad things will happen because we suddenly have PTE or PMD page tables where we shouldn't have them. Similarly, we have to check if we suddenly find a hugetlbfs VMA, before calling get_locked_pte(). Fixes: 2d42f9477320 ("s390/kvm: Add PGSTE manipulation functions") Signed-off-by: David Hildenbrand --- arch/s390/mm/pgtable.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/s390/mm/pgtable.c b/arch/s390/mm/pgtable.c index eec3a9d7176e..54969e0f3a94 100644 --- a/arch/s390/mm/pgtable.c +++ b/arch/s390/mm/pgtable.c @@ -988,6 +988,7 @@ EXPORT_SYMBOL(get_guest_storage_key); int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc, unsigned long *oldpte, unsigned long *oldpgste) { + struct vm_area_struct *vma; unsigned long pgstev; spinlock_t *ptl; pgste_t pgste; @@ -997,6 +998,10 @@ int pgste_perform_essa(struct mm_struct *mm, unsigned long hva, int orc, WARN_ON_ONCE(orc > ESSA_MAX); if (unlikely(orc > ESSA_MAX)) return -EINVAL; + + vma = vma_lookup(mm, hva); + if (!vma || is_vm_hugetlb_page(vma)) + return -EFAULT; ptep = get_locked_pte(mm, hva, &ptl); if (unlikely(!ptep)) return -EFAULT; @@ -1089,10 +1094,14 @@ EXPORT_SYMBOL(pgste_perform_essa); int set_pgste_bits(struct mm_struct *mm, unsigned long hva, unsigned long bits, unsigned long value) { + struct vm_area_struct *vma; spinlock_t *ptl; pgste_t new; pte_t *ptep; + vma = vma_lookup(mm, hva); + if (!vma || is_vm_hugetlb_page(vma)) + return -EFAULT; ptep = get_locked_pte(mm, hva, &ptl); if (unlikely(!ptep)) return -EFAULT; @@ -1117,9 +1126,13 @@ EXPORT_SYMBOL(set_pgste_bits); */ int get_pgste(struct mm_struct *mm, unsigned long hva, unsigned long *pgstep) { + struct vm_area_struct *vma; spinlock_t *ptl; pte_t *ptep; + vma = vma_lookup(mm, hva); + if (!vma || is_vm_hugetlb_page(vma)) + return -EFAULT; ptep = get_locked_pte(mm, hva, &ptl); if (unlikely(!ptep)) return -EFAULT; -- 2.31.1