Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp534255pxb; Wed, 3 Mar 2021 09:03:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJy4V1xO0v5TSWakEnXa+gNAPKVusGqaYSU7x6Et8rhIddZwqsQmSfP6+ma/DFmBGL9unRX3 X-Received: by 2002:a17:906:2314:: with SMTP id l20mr27463488eja.178.1614791005610; Wed, 03 Mar 2021 09:03:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614791005; cv=none; d=google.com; s=arc-20160816; b=lkDgzvSwtzPrv0ByTA+MHoSoyFAtuutfs9QYsGHvGn3jtEpcq/9O52JwOom9SbL1Gg cu/lu2D/In1UULx2xxJolJu8boLlVmOO6ce3s8/iLh/ddxItI9+auJyG8BYElrRg2vV6 J5YFu/vSFfxTlO0iosDqwo+8K+kMekxLZ9thdE5FZb8pNpj6pkqn+z/g8icSHbOlcXoM MgLDiWW4KOR5IhMOrnLHgqxXAYEaCHeApCpK/byrTBQ+qBv6bbeoWWfX+NIC6Yloc9UW 6oUY4LY+d3rf8/jDwzN0j1nspENQwgu44iSYPCgjWKwHFErsIbbHragz2/TWzC/qLZAo Ap7g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=rjFoHWTXfUBZgKH2acaee/W8+yEVnxXExmyk5siPviE=; b=PriskSEBXYB91BZZ8UeM76mvnIOCaCbBpd/uq0mkNJ5B/wWtzPVAqcEKtcil05qjH4 pmGDsezqItnIYmUzBah/Bn+8U5cwbPaiayQy/F0ne/epe32aZzZ30VnaBEvh2JFzUa3q jifajTsIH2yGRCIiMnYjUdrnCmbxlg8yK7K2CBbvEVPVWbUjRmkzudqfuVhQIPTyHMNT yZU3z5urWA6hhg+YYdtroo+71UY4LOZxGFREDEKu2mg/yXJt5MzT7t3kvpJW0CWl9mRu CXSpnEiI/0TYkzbFokaTSrudMbxPU1cGVrpEYx2OK+BgBTV8cJZXZG23VIc0KMamaHbW KZ6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R75gqU5X; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i12si14969749ejr.344.2021.03.03.09.02.18; Wed, 03 Mar 2021 09:03:25 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=R75gqU5X; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348149AbhCBA2t (ORCPT + 99 others); Mon, 1 Mar 2021 19:28:49 -0500 Received: from mail.kernel.org ([198.145.29.99]:44682 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238030AbhCAS31 (ORCPT ); Mon, 1 Mar 2021 13:29:27 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id DA9036527D; Mon, 1 Mar 2021 17:30:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619836; bh=okeCtSG7Ka7Bj1vHqCXyN3O/rjwda6/+h163fwI1cZw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R75gqU5Xsp+4/MZ9KAssZumM/B5UEbi9o8ylSQ8P4lmmKCubpdvcmJtwgLFjPejMQ sdDb6JZZ/Fjevbt9kGTSwVfOV71lYHjWON8f7TPuMa4YtsLJ0WWc+viUtUSgVvs+vn rMUkyFbeKhIjLcG1ph51GFltBEhGm6Jiuwhzkke8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Kravetz , Zi Yan , Davidlohr Bueso , "Kirill A . Shutemov" , Andrea Arcangeli , Matthew Wilcox , Oscar Salvador , Joao Martins , Andrew Morton , Linus Torvalds Subject: [PATCH 5.10 599/663] hugetlb: fix copy_huge_page_from_user contig page struct assumption Date: Mon, 1 Mar 2021 17:14:07 +0100 Message-Id: <20210301161211.501704308@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Kravetz commit 3272cfc2525b3a2810a59312d7a1e6f04a0ca3ef upstream. page structs are not guaranteed to be contiguous for gigantic pages. The routine copy_huge_page_from_user can encounter gigantic pages, yet it assumes page structs are contiguous when copying pages from user space. Since page structs for the target gigantic page are not contiguous, the data copied from user space could overwrite other pages not associated with the gigantic page and cause data corruption. Non-contiguous page structs are generally not an issue. However, they can exist with a specific kernel configuration and hotplug operations. For example: Configure the kernel with CONFIG_SPARSEMEM and !CONFIG_SPARSEMEM_VMEMMAP. Then, hotplug add memory for the area where the gigantic page will be allocated. Link: https://lkml.kernel.org/r/20210217184926.33567-2-mike.kravetz@oracle.com Fixes: 8fb5debc5fcd ("userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for userfaultfd support") Signed-off-by: Mike Kravetz Cc: Zi Yan Cc: Davidlohr Bueso Cc: "Kirill A . Shutemov" Cc: Andrea Arcangeli Cc: Matthew Wilcox Cc: Oscar Salvador Cc: Joao Martins Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/memory.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) --- a/mm/memory.c +++ b/mm/memory.c @@ -5203,17 +5203,19 @@ long copy_huge_page_from_user(struct pag void *page_kaddr; unsigned long i, rc = 0; unsigned long ret_val = pages_per_huge_page * PAGE_SIZE; + struct page *subpage = dst_page; - for (i = 0; i < pages_per_huge_page; i++) { + for (i = 0; i < pages_per_huge_page; + i++, subpage = mem_map_next(subpage, dst_page, i)) { if (allow_pagefault) - page_kaddr = kmap(dst_page + i); + page_kaddr = kmap(subpage); else - page_kaddr = kmap_atomic(dst_page + i); + page_kaddr = kmap_atomic(subpage); rc = copy_from_user(page_kaddr, (const void __user *)(src + i * PAGE_SIZE), PAGE_SIZE); if (allow_pagefault) - kunmap(dst_page + i); + kunmap(subpage); else kunmap_atomic(page_kaddr);