From patchwork Mon Oct 28 14:56:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kefeng Wang X-Patchwork-Id: 13853640 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E762D33990 for ; Mon, 28 Oct 2024 14:57:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F1ED86B008C; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED0186B0092; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D96896B0093; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BE8F36B008C for ; Mon, 28 Oct 2024 10:57:44 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 6D613A02FA for ; Mon, 28 Oct 2024 14:57:44 +0000 (UTC) X-FDA: 82723315038.14.77A35D0 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf02.hostedemail.com (Postfix) with ESMTP id 9077780009 for ; Mon, 28 Oct 2024 14:56:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730127252; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1mPTJyGQcEm5j63O67VUTQq3hLo8NkV5QGizMH85nQ0=; b=WSbd3C8fnjFZ95NMOKS8aTAUMBckKdq/syMJ8azh/sWTxowjAdZxsYGKPTY6O41rRl0xCy 3iN2z3vM80nwVsvxnJEHS+uCeBiLH83LhkQRcSBjkjaC9b6myjiLm+1TGCcFeMrlXm9R8S A0QC6Y8liz13ovdM3zyNaEtplLdqroQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730127252; a=rsa-sha256; cv=none; b=Cis/RuHfaBAoinsVcytduPFM+buxVplQHPeYFYT9LRQW4q3BZs8tNPOYKpKhgPJ8AmJEDc ajjw7xJaI0RAo0LMgLXa7isDiK312eywjPxboJcM633Rey7tvMfQFD4oY7kE/YmnrHQWht c28qka1v3VKkATVcDiTycTKm5tKHsSM= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Xcc0h3CLwzQsNG; Mon, 28 Oct 2024 22:56:36 +0800 (CST) Received: from dggpemf100008.china.huawei.com (unknown [7.185.36.138]) by mail.maildlp.com (Postfix) with ESMTPS id 0C0A01800DE; Mon, 28 Oct 2024 22:57:34 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemf100008.china.huawei.com (7.185.36.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 28 Oct 2024 22:57:33 +0800 From: Kefeng Wang To: Andrew Morton CC: David Hildenbrand , Matthew Wilcox , Muchun Song , "Huang, Ying" , , Kefeng Wang Subject: [PATCH v3 2/2] mm: use aligned address in copy_user_gigantic_page() Date: Mon, 28 Oct 2024 22:56:56 +0800 Message-ID: <20241028145656.932941-2-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241028145656.932941-1-wangkefeng.wang@huawei.com> References: <20241028145656.932941-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemf100008.china.huawei.com (7.185.36.138) X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9077780009 X-Stat-Signature: 3oc9wsgbsa5tpt83gtqzdir5gemo7erz X-Rspam-User: X-HE-Tag: 1730127416-698699 X-HE-Meta: U2FsdGVkX1/Ly26bdOB5ggN0rDVHpmZqI4MC3ec4JEtvSpehPNm9FbF/gtBjurbsOAygIw2PnEVRN9QsWYoEZXavcgl9eDAOem5PpGXMbXxEjS0PdzOueyrrIZtPaPy63p8+K5g/Eh/y1dGgVDsk5E4ndQgiKnRb2iIDPpLBiUW8hU0TerQnUmb5oZty/SFJ+oQftbJrZ7nCuH5rOxfN+ekmiq/ewX8wTYD8xI656yJ9V5TTseVYgAYRJ0bjAaT0OeIbYWy1DSoRSyBP1lUAwE7mOY7cQVcH8HmWMp3UTO+bcWi46Tt03KVHW+qox7iWKJW9aGpCTYGg1LRNDvL1aGaBewzhibR/gPAp9RbmM6ktSztqOAAs1shXbRxjN+o+CRldDlbkrd3VtOGthCfi7jZKyFI/5AAk6vEl3agrTsYielm0E/YkisCfdki5Y//6Y2bKbBiWmsmDbjsD10fnNOLkaZQ8Rhhiw61uuGMoES8RH0sTOZ3PG0TZ7lCrLrN+3Z7t1YdxHEyeU3vOq8NqHG3AWEXHhodJ+5ZfOZB7XJvwVMH42tvpdHPZC+xzrLmPHd+X7bNm/St1tBSOCLP8WRTzQpp6bg1Y03XZNLIroXEJ9lRKGpokJVQ6vsuPG5RmmUopYWCgi5UTovFGyA4UPlHB10zHbDmhfTKDUqEvk+zfPakEPWImVgYvUhZOK6uieH1NOl7VPjelsYhUVysm+R6UVOgcUeoA+XQBFrAkw199ZGY9PzrYgcNxPnsMCJaXlrtMieM7ROpvH7jbnNkhIBasblk/dYVrNoJ73pJtNSxxCTRUgU/q2mo7UWnxAMD6LI0xx7AbwxRLfwLgtcds48diOYSNF1XTiQ/5hevxXktIAACpf4K3GjjJcNEL40xdXu+99exoRo1/R7vtMSX1NOw9h8RirZ+jt+YdgVRRbmw9EAe/Uw/y1ZcY/cLAxRL8THd3NiDr9Rz6g0uzdyS otcz1XVW MXCaUBJktLCltWBe9yfJ8IzlrIfv+HNkiC10Gyw9ZDIjoKWaS49EI2N59cYUZGZhPXHDDt/U7eivxbOmEqlgPpgoM8XefObvFqVmVEwk9WGRepjpclQ8gHQb6vEeXHn6EQXgYfgORNGIwJJy4B23Z9FRjRYneUgX05CC23VMTnzjkNS4dyTfBN6aQYYfco75DS8KL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In current kernel, hugetlb_wp() calls copy_user_large_folio() with the fault address. Where the fault address may be not aligned with the huge page size. Then, copy_user_large_folio() may call copy_user_gigantic_page() with the address, while copy_user_gigantic_page() requires the address to be huge page size aligned. So, this may cause memory corruption or information leak, addtional, use more obvious naming 'addr_hint' instead of 'addr' for copy_user_gigantic_page(). Fixes: 530dd9926dc1 ("mm: memory: improve copy_user_large_folio()") Signed-off-by: Kefeng Wang Reviewed-by: David Hildenbrand --- v3: - revise patch description, suggested by Huang Ying - use addr_hint for copy_user_gigantic_page(), suggested by David v2: - update changelog to clarify the impact, per Andrew mm/hugetlb.c | 5 ++--- mm/memory.c | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 2c8c5da0f5d3..15b5d46d49d2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5338,7 +5338,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, break; } ret = copy_user_large_folio(new_folio, pte_folio, - ALIGN_DOWN(addr, sz), dst_vma); + addr, dst_vma); folio_put(pte_folio); if (ret) { folio_put(new_folio); @@ -6641,8 +6641,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, *foliop = NULL; goto out; } - ret = copy_user_large_folio(folio, *foliop, - ALIGN_DOWN(dst_addr, size), dst_vma); + ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma); folio_put(*foliop); *foliop = NULL; if (ret) { diff --git a/mm/memory.c b/mm/memory.c index 84864387f965..209885a4134f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6852,13 +6852,14 @@ void folio_zero_user(struct folio *folio, unsigned long addr_hint) } static int copy_user_gigantic_page(struct folio *dst, struct folio *src, - unsigned long addr, + unsigned long addr_hint, struct vm_area_struct *vma, unsigned int nr_pages) { - int i; + unsigned long addr = ALIGN_DOWN(addr_hint, folio_size(dst)); struct page *dst_page; struct page *src_page; + int i; for (i = 0; i < nr_pages; i++) { dst_page = folio_page(dst, i);