Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2212362iof; Tue, 7 Jun 2022 23:22:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxaGnqGSRIc1sAt1QKTSDbS8d0mlP7Q23PHTqzLOBXpfZCCPt8UEm9PFXHTocQdQUGueBso X-Received: by 2002:a17:902:dacb:b0:167:621b:f2ec with SMTP id q11-20020a170902dacb00b00167621bf2ecmr20474864plx.19.1654669323341; Tue, 07 Jun 2022 23:22:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654669323; cv=none; d=google.com; s=arc-20160816; b=cyjxHsGj0rooH36mU7xy2ZaPnjgfBOblvr+nFnM3ffO3Oi/oLEagPIJD1Tejvb9x43 XmqcejGt0ovvs+E/66QRBwfpb5TaPwab5vuATCc64Y0j2x7P/HPqf4sMyyHMPFWPPDnN NHKsLiaWCAwd7IhcRc7l3I80Huhe4EyJlpRH2HSmfc9NMuaqkNlswJ9M33ax72akYUHq Y6jxSi7UACVhUcpU4dCoj14VASV83KPmZvmbS9q4UgjoG/UdVaIxEp9fbEKMU/jelrLr /UyjFqlUaXJj9VgDkYzHGnGyAdnNPr7dSDs4D/x954kUFefVIiZ3cFoFX1vdSQW1Rpf3 SrvQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pFXFeRQ37/QHbFl+8gpmlP3fCWgheEOoNrXg7XuKoNI=; b=UCiyfno9lvJTe8kzFJnoLzClYPM1zkKM5ufXiO7sJQAzjsqeukKx9Zbm830pMExruj A3sNOYg388W+gGM/umJNDlSQFF5heVNxmuXkKJaDIPjcbUJw/S4MUfxQgGtns6rzjKFA CmJqjeAC75b+SdA27XFSm5y/F08uvwDklRprA0OBJ58qVBVI8E2pa1Ebd4BsNcjJ1dh+ IqcKTh9cMyoYeOxz77sRRn4QuycM6XQuJt9kJ4rGdm7FplEtVa5yWfTCGG8HszrhpB5j Tb45I3Q62FGiP+U4gqManNcmrww1p+y9NwLo5DlT/5IKZY77ylrkkUcqz5elETtohU+k L06A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cogw5Yqo; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id n7-20020aa79047000000b0051bdde894c6si19218534pfo.108.2022.06.07.23.22.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 23:22:03 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cogw5Yqo; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E55872BCCB7; Tue, 7 Jun 2022 22:42:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443801AbiFHCDP (ORCPT + 99 others); Tue, 7 Jun 2022 22:03:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1843648AbiFHALR (ORCPT ); Tue, 7 Jun 2022 20:11:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D30119CB77; Tue, 7 Jun 2022 12:26:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 452D8B8237B; Tue, 7 Jun 2022 19:26:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D06F9C385A5; Tue, 7 Jun 2022 19:26:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629971; bh=2b3EwAxT/IxY3yiIFVjw+wSEWXH1+Ouo/kRi/T0Wbxw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cogw5YqoEGLYQmkzpPP4C1Fv2Fc3DI0mxUDFk+U436W4NkEku7/SxttcoNt2yANyp T33MUY2fzABT+Z7Op3h32kU8DdtMJi5j+ioonp4+qDgwvqhyyoR/OGPq1VWLqXJPmm E/39LJwRAnbWItLOO9QQH+su6+GZqtCZXf4ry3Ng= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ondrej Hubsch , Ronnie Sahlberg , "Paulo Alcantara (SUSE)" , Steve French Subject: [PATCH 5.18 854/879] SMB3: EBADF/EIO errors in rename/open caused by race condition in smb2_compound_op Date: Tue, 7 Jun 2022 19:06:12 +0200 Message-Id: <20220607165027.643252462@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Steve French commit 0a55cf74ffb5d004b93647e4389096880ce37d6b upstream. There is a race condition in smb2_compound_op: after_close: num_rqst++; if (cfile) { cifsFileInfo_put(cfile); // sends SMB2_CLOSE to the server cfile = NULL; This is triggered by smb2_query_path_info operation that happens during revalidate_dentry. In smb2_query_path_info, get_readable_path is called to load the cfile, increasing the reference counter. If in the meantime, this reference becomes the very last, this call to cifsFileInfo_put(cfile) will trigger a SMB2_CLOSE request sent to the server just before sending this compound request – and so then the compound request fails either with EBADF/EIO depending on the timing at the server, because the handle is already closed. In the first scenario, the race seems to be happening between smb2_query_path_info triggered by the rename operation, and between “cleanup” of asynchronous writes – while fsync(fd) likely waits for the asynchronous writes to complete, releasing the writeback structures can happen after the close(fd) call. So the EBADF/EIO errors will pop up if the timing is such that: 1) There are still outstanding references after close(fd) in the writeback structures 2) smb2_query_path_info successfully fetches the cfile, increasing the refcounter by 1 3) All writeback structures release the same cfile, reducing refcounter to 1 4) smb2_compound_op is called with that cfile In the second scenario, the race seems to be similar – here open triggers the smb2_query_path_info operation, and if all other threads in the meantime decrease the refcounter to 1 similarly to the first scenario, again SMB2_CLOSE will be sent to the server just before issuing the compound request. This case is harder to reproduce. See https://bugzilla.samba.org/show_bug.cgi?id=15051 Cc: stable@vger.kernel.org Fixes: 8de9e86c67ba ("cifs: create a helper to find a writeable handle by path name") Signed-off-by: Ondrej Hubsch Reviewed-by: Ronnie Sahlberg Reviewed-by: Paulo Alcantara (SUSE) Signed-off-by: Steve French Signed-off-by: Greg Kroah-Hartman --- fs/cifs/smb2inode.c | 2 -- 1 file changed, 2 deletions(-) --- a/fs/cifs/smb2inode.c +++ b/fs/cifs/smb2inode.c @@ -362,8 +362,6 @@ smb2_compound_op(const unsigned int xid, num_rqst++; if (cfile) { - cifsFileInfo_put(cfile); - cfile = NULL; rc = compound_send_recv(xid, ses, server, flags, num_rqst - 2, &rqst[1], &resp_buftype[1],