Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp11346245rwb; Fri, 25 Nov 2022 14:30:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf6/YTHhsFCJlJq9v3UMwGP9yAgWKGXSXfHjbdOge0/k/iqepbvFqzF8Qqg0NdlvvL17aD9u X-Received: by 2002:a17:902:f7c1:b0:17f:66ae:b84 with SMTP id h1-20020a170902f7c100b0017f66ae0b84mr23447025plw.32.1669415456319; Fri, 25 Nov 2022 14:30:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669415456; cv=none; d=google.com; s=arc-20160816; b=TuYu9T6pLdRAFLfLT66HH4F89+ccAgfPrkKBBvBM/GSpAnL+IXukaxn/5OSGYZQWkW 1qavDvngK94kB+RfBcM8swxIyyY6hB+UAbUv47gVogh/chafJi9lU+nSbq++aXBphZDS q3SwbJSlVWKUYTW5sYHQOtFhcfSpNFkXTY2yENIgs6dNDwzTSJhi/oMZXNykpGNq4GBb Lugyo9rCZBRoZ4UcwwsjV/nFaQ4UuCiNKQ2JLZYs3vGwu4XzhCkMHl7qGCi+icmcSwjl +9XfyIV2HVuWNd/ls8fHY4oibCgOucrsFGYCezQeeGTi/FNQfar3+cDxHf87XcCBs8yR IYuw== 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=NOnwsTARUoatlgwSRumO8KX+asIUrP0Tz9G19w9mrYE=; b=P9jeY7jsrmm9f+cUPFfJvd0aeXrbE702e7BqPdx652gxiFrBFwXkc17CrFxT7b5RY4 p7d6oHUnZcW4ZWMSDitBjNGr8iXIFBFWd9rRFVJx6dgYMzRLNiDLAdVwmCQMnd8l3FSs VMfWu9pPvqImG3+4DdADQwxrj4lK7QwX/3v3HhPQ7gR/lTwsBN2VoFvdalLPtxUeXsi3 BBTtuAOE0jBEIgJy8OkFZHkyJQJMKvRVkLrbFi6t0P8bapVQPgYrF/JJcDbrQi+jXIJn xvEU/DpJ2A0ThGN1/q60c55Jn+5kHnJ2RCKwBQVHQgDdGATiJ+fBjByHn3Tc32lr1tB6 cxIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=F18wqhUO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d72-20020a63364b000000b004778463363csi4224018pga.76.2022.11.25.14.30.43; Fri, 25 Nov 2022 14:30:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=F18wqhUO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230097AbiKYVhy (ORCPT + 85 others); Fri, 25 Nov 2022 16:37:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbiKYVhu (ORCPT ); Fri, 25 Nov 2022 16:37:50 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7512FC745 for ; Fri, 25 Nov 2022 13:37:48 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id q7so7627492wrr.8 for ; Fri, 25 Nov 2022 13:37:48 -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=B1q7HXoj8ohQVxaNwv3QcBQD+c3V8mVl3OkRUvoPe4lcaLqUxPFX/KuVnSWvKZjAKD q+SByo2z2qt6Azw2uK9aIqYcxsCVQ9oAlqRoisQZhTwyZl59NikqSdSicr6yj1JAFEgf /FJi8IuntgC4PrsXxDnqxUVNvxE9Dm7jvb8tlCyp/W9cVGfLhXhjykrRuTahqKtr73Wx QQf2Pl6UNC8U1w/frIm5b/lBIa6uPezmozjQMnV1yGhmAHqAw8U+KGr2OBepnFWePzGG Cqvpev+Sh6Kutw/xY+hSNazbhlAPkTm70H+claP9N7n1jDRHuyAENqj9kz8JgRfOLrYe Br0w== X-Gm-Message-State: ANoB5plctGCZoPmWZ+SvAE23htHysZ03ndv7SlsF1NX1phDSPuCNHXY6 4NiNT8trBI0NUKdd7SWnoXFpxQ== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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)); -- 2.38.1.584.g0f3c55d4c2-goog