From patchwork Fri Apr 14 02:41:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiubo Li X-Patchwork-Id: 13210878 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A384C77B6E for ; Fri, 14 Apr 2023 02:42:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229577AbjDNCm0 (ORCPT ); Thu, 13 Apr 2023 22:42:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbjDNCmZ (ORCPT ); Thu, 13 Apr 2023 22:42:25 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BE3E12E for ; Thu, 13 Apr 2023 19:41:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681440100; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=y6ditWs4JzErEXvsdo9zUGkxKuaELZkB/oSNNnzgxms=; b=EzbD97nfQ5q/2T4iemTpZ+wen2CzZQQhgNxesLmmU0jLpr8DMDAkZJ5Aq008Z69YqIZni4 fpS3n2ugaUYwv4N10rtzqywsyh9pcSV32+JnbN/GyG9ZnAm2oKqezFg3N4bP0Ih4xkAYsb Nx2NJGpVm6BKsECRvdND16RfPo4+oKI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-82-J54qgVVEMhq3l1xXlTX4iw-1; Thu, 13 Apr 2023 22:41:36 -0400 X-MC-Unique: J54qgVVEMhq3l1xXlTX4iw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8ABF78996E3; Fri, 14 Apr 2023 02:41:36 +0000 (UTC) Received: from li-a71a4dcc-35d1-11b2-a85c-951838863c8d.ibm.com.com (ovpn-12-157.pek2.redhat.com [10.72.12.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88438405DBB6; Fri, 14 Apr 2023 02:41:30 +0000 (UTC) From: xiubli@redhat.com To: idryomov@gmail.com, ceph-devel@vger.kernel.org Cc: jlayton@kernel.org, vshankar@redhat.com, mchangir@redhat.com, Xiubo Li , stable@vger.kernel.org Subject: [PATCH] ceph: do not touch cap when trimming the caps Date: Fri, 14 Apr 2023 10:41:23 +0800 Message-Id: <20230414024123.263120-1-xiubli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org From: Xiubo Li When trimming the caps it maybe queued to release in the next loop, and just after the 'session->s_cap_lock' lock is released the 'session->s_cap_iterator' will be set to NULL and the cap also has been removed from 'session->s_caps' list, then the '__touch_cap()' could continue and add the cap back to the 'session->s_caps' list. That means this cap could be iterated twice to call 'trim_caps_cb()' and the second time will trigger use-after-free bug. Cc: stable@vger.kernel.org URL: https://bugzilla.redhat.com/show_bug.cgi?id=2186264 Signed-off-by: Xiubo Li --- fs/ceph/caps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c index cf29e395af23..186c9818ab0d 100644 --- a/fs/ceph/caps.c +++ b/fs/ceph/caps.c @@ -846,7 +846,7 @@ static void __touch_cap(struct ceph_cap *cap) struct ceph_mds_session *s = cap->session; spin_lock(&s->s_cap_lock); - if (!s->s_cap_iterator) { + if (!s->s_cap_iterator && !list_empty(&cap->session_caps) && !cap->queue_release) { dout("__touch_cap %p cap %p mds%d\n", &cap->ci->netfs.inode, cap, s->s_mds); list_move_tail(&cap->session_caps, &s->s_caps);