From patchwork Fri Nov 25 21:37:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 13056269 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 7B3C9C4167B for ; Fri, 25 Nov 2022 21:37:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 033346B0082; Fri, 25 Nov 2022 16:37:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F25496B0083; Fri, 25 Nov 2022 16:37:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D28E76B0085; Fri, 25 Nov 2022 16:37:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C3C166B0083 for ; Fri, 25 Nov 2022 16:37:48 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 936C81A0255 for ; Fri, 25 Nov 2022 21:37:48 +0000 (UTC) X-FDA: 80173277016.04.7EB0F92 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf06.hostedemail.com (Postfix) with ESMTP id 29DE8180009 for ; Fri, 25 Nov 2022 21:37:47 +0000 (UTC) Received: by mail-wr1-f52.google.com with SMTP id bs21so8519149wrb.4 for ; Fri, 25 Nov 2022 13:37:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NOnwsTARUoatlgwSRumO8KX+asIUrP0Tz9G19w9mrYE=; b=F18wqhUOYlIX6J6ZsQFtBJSCT9eGiwguPQmjVFDZS7DKaV/hKfBW3zmQdVbyWmz0uh 1qn6FwsVbEYLb+xyfH7ZZDkMM6uZzqnljapoiX58tD5RA3MCm3fzaxHGzkfPd3KCl72H eLo3OgIuOrR6ZHT0e5Vs6f3N7tbNhtobUBQo7V1CM1IAM8bmN6iM8aOJPykUrX6giarR xd4kwOdZ+Eq1KakuVhnZd5qSnVHmGaIw7gDT+yJUNeydDyPLcBSlzYbaId82H7eBHzlc fn5z8Iu3LtcKkbFACMjoo5wH0GZrIpPMcDx6MXGiwHzlNQBftGPREzMGphe0eLA01xqh 4L7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NOnwsTARUoatlgwSRumO8KX+asIUrP0Tz9G19w9mrYE=; b=gFYAS8e/rBfKiUG7uJDoxh9DsWefnhluB91Fj1nk2KTCcD2M01cjUZ4SoJibgcJNIO GeJfIALcq+o/IkbZAJ/sHQx3ZnvMtTwtKP1Jig3LdAWEdVXGdB6Oi6KkdqxOYvqf8lY9 ERZ/phewda/k+neTdtU/BYYVNhaBPtAK4bCDwIouXEBoxLipX3fs83gGCbcByW1coo8a PjVWyNJViw4qgTvduzowhjI6GQA/0ik97gJy7ZtqB+bi/JrkNd8JSbVK+2bXqoMv21mh nv9SCFGsHVpxRBmkNuXTxuz6w73kneh49ewnBCL9onAx3HaPucxCG/YNtlh7unTDxB6K xqTA== X-Gm-Message-State: ANoB5pno1sO5YKN8qf4fB1TS8CQasxsbDtYl/YjPBUQH8xfswmW1DX/l PCD866GLjAMrRsD4ROF4TDfSjA== X-Google-Smtp-Source: AA0mqf7a8Vcy4n5comCXosZv/VZNsAUlq9hK2P6zFM9IR6J9bTMU0oSmYK5q5Wf0ZQYoI23b3wkX4Q== X-Received: by 2002:a5d:6f02:0:b0:241:fdb5:50f7 with SMTP id ay2-20020a5d6f02000000b00241fdb550f7mr6563623wrb.170.1669412266904; Fri, 25 Nov 2022 13:37:46 -0800 (PST) Received: from localhost ([2a00:79e0:9d:4:9104:e316:5846:858e]) by smtp.gmail.com with ESMTPSA id z6-20020adfe546000000b0023655e51c33sm4675291wrm.4.2022.11.25.13.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Nov 2022 13:37:46 -0800 (PST) From: Jann Horn To: security@kernel.org, Andrew Morton Cc: Yang Shi , David Hildenbrand , Peter Xu , John Hubbard , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 3/3] mm/khugepaged: Invoke MMU notifiers in shmem/file collapse paths Date: Fri, 25 Nov 2022 22:37:14 +0100 Message-Id: <20221125213714.4115729-3-jannh@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog In-Reply-To: <20221125213714.4115729-1-jannh@google.com> References: <20221125213714.4115729-1-jannh@google.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669412268; a=rsa-sha256; cv=none; b=fScZXLhIoGO+qGuaX2+mHGZnfHe+oY4mY2p3kqNIMZBlyTVqe8cECbvqODfgVQmeGqW5c9 2oMJ/m9nKZicJXbWJ5bDZXOh6MJpHGbxW580Fk/tNaGzXYk0clXBiyThBoQeEdzNLJnYn+ iuEjFfKKgHZIIVfj3GH4BHerAKnBusM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=F18wqhUO; spf=pass (imf06.hostedemail.com: domain of jannh@google.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669412268; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NOnwsTARUoatlgwSRumO8KX+asIUrP0Tz9G19w9mrYE=; b=ZEwgmrrIP6iXTG0DKUiJezMFqndCPGjD4pt3XH7Sc0lDdHjuyEpxFGCAFMucEAiLEEns+E 9POCcGGm3gd8KnEM+GN5vybKnanU2CRqnt0FhZ3E3nIjLwY160EXVfP1M2Eh388A/GGvNe uv7uNJT4PX/XVAJt07cfJ9LczAsljkw= X-Rspamd-Queue-Id: 29DE8180009 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=F18wqhUO; spf=pass (imf06.hostedemail.com: domain of jannh@google.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam07 X-Stat-Signature: wjydp3qnycnej96paqt5745p3r3id4my X-HE-Tag: 1669412267-505220 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: Any codepath that zaps page table entries must invoke MMU notifiers to ensure that secondary MMUs (like KVM) don't keep accessing pages which aren't mapped anymore. Secondary MMUs don't hold their own references to pages that are mirrored over, so failing to notify them can lead to page use-after-free. I'm marking this as addressing an issue introduced in commit f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages"), but most of the security impact of this only came in commit 27e1f8273113 ("khugepaged: enable collapse pmd for pte-mapped THP"), which actually omitted flushes for the removal of present PTEs, not just for the removal of empty page tables. Cc: stable@kernel.org Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Jann Horn --- mm/khugepaged.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index c3d3ce596bff7..49eb4b4981d88 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1404,6 +1404,7 @@ static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *v unsigned long addr, pmd_t *pmdp) { pmd_t pmd; + struct mmu_notifier_range range; mmap_assert_write_locked(mm); if (vma->vm_file) @@ -1415,8 +1416,12 @@ static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *v if (vma->anon_vma) lockdep_assert_held_write(&vma->anon_vma->root->rwsem); + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm, addr, + addr + HPAGE_PMD_SIZE); + mmu_notifier_invalidate_range_start(&range); pmd = pmdp_collapse_flush(vma, addr, pmdp); tlb_remove_table_sync_one(); + mmu_notifier_invalidate_range_end(&range); mm_dec_nr_ptes(mm); page_table_check_pte_clear_range(mm, addr, pmd); pte_free(mm, pmd_pgtable(pmd));