From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9BE4C43465 for ; Fri, 18 Sep 2020 02:30:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8BAF7206A1 for ; Fri, 18 Sep 2020 02:30:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600396234; bh=NqXmAPguqfn/+CJlyYYOcohnL5NwvrsAg7fbVs1Om6Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=rAuh+EKV0qs3TCOaLpShXI31hTDPx6yldZagbDby5ydeSys7ufwgv0pLukHH+nkLR ZyXppcMmwFa9QVzXmWoK5BGdpAE7pMZdEa9IFYBbN9+tVsHyQQSexRNOlbjE2LYkiQ m4ni7zKOQiJns+2ttrrz7BUBScRrn3kxqmKI0ePg= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729890AbgIRCad (ORCPT ); Thu, 17 Sep 2020 22:30:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:42974 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729092AbgIRCOk (ORCPT ); Thu, 17 Sep 2020 22:14:40 -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 2E8E0239D1; Fri, 18 Sep 2020 02:14:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600395279; bh=NqXmAPguqfn/+CJlyYYOcohnL5NwvrsAg7fbVs1Om6Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gYJDTSpNZLNcrVd8iw+rMpOCyfrkQweosNQT5BuCTEyPLo4IimoqdtlEHodfGXXZX MnXQ2G8RuL9WdRQEMUJJF4Iu0RujWIh4s8gkynBnUUUasXPBzelQ6yhgDwLMAuXp6x SUb1g2bWFQiTbvtpvbPV4596paFksmarqkods7dM= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jeff Layton , Ilya Dryomov , Sasha Levin , ceph-devel@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 116/127] ceph: fix potential race in ceph_check_caps Date: Thu, 17 Sep 2020 22:12:09 -0400 Message-Id: <20200918021220.2066485-116-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918021220.2066485-1-sashal@kernel.org> References: <20200918021220.2066485-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jeff Layton [ Upstream commit dc3da0461cc4b76f2d0c5b12247fcb3b520edbbf ] Nothing ensures that session will still be valid by the time we dereference the pointer. Take and put a reference. In principle, we should always be able to get a reference here, but throw a warning if that's ever not the case. Signed-off-by: Jeff Layton Signed-off-by: Ilya Dryomov Signed-off-by: Sasha Levin --- fs/ceph/caps.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c index 589cfe3ed873b..ce94d09f6abf9 100644 --- a/fs/ceph/caps.c +++ b/fs/ceph/caps.c @@ -1906,12 +1906,24 @@ ack: if (mutex_trylock(&session->s_mutex) == 0) { dout("inverting session/ino locks on %p\n", session); + session = ceph_get_mds_session(session); spin_unlock(&ci->i_ceph_lock); if (took_snap_rwsem) { up_read(&mdsc->snap_rwsem); took_snap_rwsem = 0; } - mutex_lock(&session->s_mutex); + if (session) { + mutex_lock(&session->s_mutex); + ceph_put_mds_session(session); + } else { + /* + * Because we take the reference while + * holding the i_ceph_lock, it should + * never be NULL. Throw a warning if it + * ever is. + */ + WARN_ON_ONCE(true); + } goto retry; } } -- 2.25.1