Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3098690ybi; Thu, 18 Jul 2019 21:00:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZz3Bcv0vC8iPHzH2cGPHIiTxhJm1sLLzTEFSpObBsbE6yPMYxKuYrePCO8MtFmYkdXsaJ X-Received: by 2002:a17:902:28c9:: with SMTP id f67mr14622033plb.19.1563508849778; Thu, 18 Jul 2019 21:00:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563508849; cv=none; d=google.com; s=arc-20160816; b=ZdDYGfuHA+snhv/PEPcHnMwazG5PcZlEG4bNaRMiYtNBpd/3ohQr9NNQoY+DV1IwaK XIHKqBRf+JN3l7/9dbJrJ99EGp6fhNDmQIg1I3sttp/NWEKuGwl3mm6bD+fy7+cfVs5/ LAb6z0Vu3NGn43Qp6ezGwIDBhzZn3S2jhOD0YR+YPhk+Hc9arT/rK3bAjt3TaYAdJ7wm 1gtn5Nk2YbxkaB0T9VsPZw+QgUpaWM+8EjzvhD7+HdlsK1ZbyqP4EQVFzKUqrFwRUwr7 qHcidfe6rG4gLPRhZC138bcFvYl3phP77A20vNTxS8weOoEM/BYQMthSYpXUDYq/RlzO jTMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GkSloBiHfepZ/So3+4LopxRoeSBU3PfOUimBWkkUVHE=; b=IB3LOyyIqkMlWoAcKFAzIM6fStoOqbXg2Teh5o4p6WamItr5iOvB6YXX2J9OB/ztJf Etrhf7+xGvodbUWEJd00nK8sKjf+D4KCZ1yRjM77nWnRXqBbeEV5eKPwwsjhEbJn2Qu1 dn05JDXK6CBfYdI4HEIGA8V9DFURRwPieluO+omqbtTOF1QZu4SVDq28dDCFBSDJIP60 IaHAtIUBnd3zdSjPZ2y+IPQjsFBXtt1O7UYHYf6duKRaI7vIPsylpZISRc1sHWyM5HeG 2BuhDd6ijBbQudb+ZI3Cb2fLnzHgjdngw1CewUpx8CC+GQ3VxRTB+rJVYeEDtzLSAQWf UvQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CVecVRKO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15si535148pfi.48.2019.07.18.21.00.34; Thu, 18 Jul 2019 21:00:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CVecVRKO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728139AbfGSD6d (ORCPT + 99 others); Thu, 18 Jul 2019 23:58:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:57796 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728084AbfGSD62 (ORCPT ); Thu, 18 Jul 2019 23:58:28 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 94ADB21874; Fri, 19 Jul 2019 03:58:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508707; bh=3wayALKAbyaZVeZbjCrY015C06CrT6gtffI83ybrmsg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CVecVRKOfUR7f0tq5XuS7ktArKsKrNh32YMPWgHFnXwgqWMwF5R5gdlYUn6mI9JJN 3ICAZXsVxp1iCSvJ8I1vkoz0ai5bbJsxODOp97pe8foxzCww9rgy28kY8JK96WwKCq VtYrfI4pcJzUACTuCoVOigInBJhQOU6kcMQwtoAs= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jason Gunthorpe , Ira Weiny , John Hubbard , Ralph Campbell , Christoph Hellwig , Philip Yang , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.2 045/171] mm/hmm: fix use after free with struct hmm in the mmu notifiers Date: Thu, 18 Jul 2019 23:54:36 -0400 Message-Id: <20190719035643.14300-45-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190719035643.14300-1-sashal@kernel.org> References: <20190719035643.14300-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Gunthorpe [ Upstream commit 6d7c3cde93c1d9ac0b37f78ec3f2ff052159a242 ] mmu_notifier_unregister_no_release() is not a fence and the mmu_notifier system will continue to reference hmm->mn until the srcu grace period expires. Resulting in use after free races like this: CPU0 CPU1 __mmu_notifier_invalidate_range_start() srcu_read_lock hlist_for_each () // mn == hmm->mn hmm_mirror_unregister() hmm_put() hmm_free() mmu_notifier_unregister_no_release() hlist_del_init_rcu(hmm-mn->list) mn->ops->invalidate_range_start(mn, range); mm_get_hmm() mm->hmm = NULL; kfree(hmm) mutex_lock(&hmm->lock); Use SRCU to kfree the hmm memory so that the notifiers can rely on hmm existing. Get the now-safe hmm struct through container_of and directly check kref_get_unless_zero to lock it against free. Signed-off-by: Jason Gunthorpe Reviewed-by: Ira Weiny Reviewed-by: John Hubbard Reviewed-by: Ralph Campbell Reviewed-by: Christoph Hellwig Tested-by: Philip Yang Signed-off-by: Sasha Levin --- include/linux/hmm.h | 1 + mm/hmm.c | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 044a36d7c3f8..89508dc0795f 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -93,6 +93,7 @@ struct hmm { struct mmu_notifier mmu_notifier; struct rw_semaphore mirrors_sem; wait_queue_head_t wq; + struct rcu_head rcu; long notifiers; bool dead; }; diff --git a/mm/hmm.c b/mm/hmm.c index f702a3895d05..4c405dfbd2b3 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -104,6 +104,11 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm) return NULL; } +static void hmm_free_rcu(struct rcu_head *rcu) +{ + kfree(container_of(rcu, struct hmm, rcu)); +} + static void hmm_free(struct kref *kref) { struct hmm *hmm = container_of(kref, struct hmm, kref); @@ -116,7 +121,7 @@ static void hmm_free(struct kref *kref) mm->hmm = NULL; spin_unlock(&mm->page_table_lock); - kfree(hmm); + mmu_notifier_call_srcu(&hmm->rcu, hmm_free_rcu); } static inline void hmm_put(struct hmm *hmm) @@ -144,10 +149,14 @@ void hmm_mm_destroy(struct mm_struct *mm) static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) { - struct hmm *hmm = mm_get_hmm(mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); struct hmm_mirror *mirror; struct hmm_range *range; + /* Bail out if hmm is in the process of being freed */ + if (!kref_get_unless_zero(&hmm->kref)) + return; + /* Report this HMM as dying. */ hmm->dead = true; @@ -185,13 +194,14 @@ static void hmm_release(struct mmu_notifier *mn, struct mm_struct *mm) static int hmm_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *nrange) { - struct hmm *hmm = mm_get_hmm(nrange->mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); struct hmm_mirror *mirror; struct hmm_update update; struct hmm_range *range; int ret = 0; - VM_BUG_ON(!hmm); + if (!kref_get_unless_zero(&hmm->kref)) + return 0; update.start = nrange->start; update.end = nrange->end; @@ -239,9 +249,10 @@ static int hmm_invalidate_range_start(struct mmu_notifier *mn, static void hmm_invalidate_range_end(struct mmu_notifier *mn, const struct mmu_notifier_range *nrange) { - struct hmm *hmm = mm_get_hmm(nrange->mm); + struct hmm *hmm = container_of(mn, struct hmm, mmu_notifier); - VM_BUG_ON(!hmm); + if (!kref_get_unless_zero(&hmm->kref)) + return; mutex_lock(&hmm->lock); hmm->notifiers--; -- 2.20.1