Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3563470pxb; Mon, 1 Mar 2021 13:28:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlV+0S5+3JOK6eMaSMG9KmqbCPf2udvfEJ9ftdj2ffyEm1mzFxuupAMJqX66kRudmYj5BE X-Received: by 2002:aa7:d6c2:: with SMTP id x2mr18319656edr.79.1614634102026; Mon, 01 Mar 2021 13:28:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614634102; cv=none; d=google.com; s=arc-20160816; b=ykUBv4/RNqH8ztNgWTAS15pVdruCFTJlz5HME6flZA7TyNtPdnUGof56pf1jzoqWPQ 7s8+6ZJFRW7RypZfXA+FaggbeokrWft6k3iTmfmYK5Y9FtGx+JSwh1JEWmvQPmg80NTD yeBqQTE7EFJmlO+8JTIunpeOsOxz4iuQJa+x19+vIgoB+OqbDs4Ac/2xwhQqiccrMdNX LsJI81IkE1OHhCCcESMchfk2nusURCm7haGfoqnRvD5nJfXX0bAnQ9x5n67/gmjAk1ar gh5K1eKa24swsxIsbmc39pN4PE+vUD/66ztJSvemOqlfvtBBFZFyD4E3VDCbiVaYFMUj +Anw== 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=pKcVnpXdj9dDcBQUodLi+FzUymVlnYp+PsqjPQmTwRU=; b=RWDeM6wA7z9U+6Hz21Puq0cD6qSCZaRjHo5M5q2zQ/Hk70BwDhl7ULubpT5j04MLNb 10OBz7JVMzbnaE5fkla90mRf1/RNULzcyQm8Rp00unDFU+nY7uU+6c+w0ImS+OJdB4CF Jw+izC69wWWly2Tc1CbQ8gvLXSokb/eVwR2OVmBw+QinAQwZEQbsikCGgnyCq3/xA4NX hIVr71yRZTe/eh1b8OHE1ApC40rxIsoZn4jQneZ68+ocnkm5ZogLhC3RAtlIpk79j/b+ BYtZ2thI4/e4NFNGirDiuHIfoCDKe0VylZ/LF0GJxWFxR5rNFVA5q1oByN25dWnXY92T 5w+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BEdVA3IC; 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 d11si11243209edv.530.2021.03.01.13.27.59; Mon, 01 Mar 2021 13:28:22 -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=BEdVA3IC; 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 S231749AbhCAVTp (ORCPT + 99 others); Mon, 1 Mar 2021 16:19:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:57230 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237989AbhCARQE (ORCPT ); Mon, 1 Mar 2021 12:16:04 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 88A086504B; Mon, 1 Mar 2021 16:46:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614617174; bh=5MKwQfX3WVWHiU2ZKh8rkd/E6VooJndjZa/fl8rCdmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BEdVA3IC9YRNt5G7LhliJTQdoFDQDzrU65tdmRZ5T8GYIDfpMUvi+qS/4Qkvakk5P YvVBx6VN8aoYCQ3W+7eysHshZ3BsReMuOsx82sYRgxsNXRcizzyhNIWmdcpGx0Yzd4 DRiRaK+KT1ANCzbmynKX2SMyc84jf4lKx3lVQ5GM= 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 4.19 221/247] hugetlb: fix copy_huge_page_from_user contig page struct assumption Date: Mon, 1 Mar 2021 17:14:01 +0100 Message-Id: <20210301161042.500881839@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161031.684018251@linuxfoundation.org> References: <20210301161031.684018251@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 @@ -4885,17 +4885,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);