From patchwork Thu May 12 15:49:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Steve French X-Patchwork-Id: 12847850 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 E5562C433F5 for ; Thu, 12 May 2022 15:49:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349369AbiELPtX (ORCPT ); Thu, 12 May 2022 11:49:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356108AbiELPtW (ORCPT ); Thu, 12 May 2022 11:49:22 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BBBBC9EFB for ; Thu, 12 May 2022 08:49:21 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id y32so9826471lfa.6 for ; Thu, 12 May 2022 08:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to:cc; bh=hIek/J6WRVp1VcpYA+pPPTz/t8K/ZfRth38UzsHQXQU=; b=gKBk3IIMLNczoQ7To1iL3ogqDr/Od/08v5lHTJqfsL8HpwtXPBTkvGXHIVsabvIPln uuT2p6xJCxJjCTGhQGIsV1D98SCN67U2vLELYEMu+IxbAND1hIMb5gmyYp1kYJ9o31OD CPGkZtpfPnpDaNODIWPiUPp+X9ksKS4MLwiQyF62oDgrIG9zYrSNw7eJCI5B1rhJAprz XQTcq2JcH6c9zbe1RWf3SSgL9KwIoZT9HDttUqGAAuAsCOyED3iHZGOG5JMF6WCiuh0O bxz+MmP0W+U/RZLaN2oHsdzs6uIF/k5eCJFD4MGh/LNLItvhpd1y32CA+sIodBU2OeMf 7X6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=hIek/J6WRVp1VcpYA+pPPTz/t8K/ZfRth38UzsHQXQU=; b=UbCXjNRcxtCMLVk2p8VUzeDfNIsm+Wlq8v5F0tCNVsTu75mTWO2Je0WYH6dRC7haGD KZ5dUXYpgTEXnD51vbaD4C4cTNrGPvgY+jxWDAf3y0l4+UDyyEXUELDTvbtTUKcJofdu +8VI9ZUwx6KEXAtAPNbkhOYrMMzkfWwO+2mnhvhNiTXjqVL1fkWIDI46SR6UfvOJAgLE UDC9xS6CREBTTFc7MLVUrQCsYLUOg3rk3M8RHyrEIhUtATKw3U9QI06xolgabUOLEMhL QsHQWAW1Wte4d+zHLLQJPRSU+Dr51wHOGrNwjFhpTaSfgt3WvRDJM8CdVjtT2Hl5aZPa WauA== X-Gm-Message-State: AOAM533OJm77k82x3GilnR4MNaEcTqXML37jNd3DBo22q28cp5QOVy1m DrVoPLbJI2VltIiVwJna6j7883rQeKPlZUg1npotRmhECgo= X-Google-Smtp-Source: ABdhPJzGugCI3Ifr7gKGTimP5nSBQyb8Ap2L4ngToqACFsPXn+S1v5YVKSEULZHiGLk9ZSQ+4XjFAsIwzJwK4u6h1gs= X-Received: by 2002:ac2:561b:0:b0:472:586b:3209 with SMTP id v27-20020ac2561b000000b00472586b3209mr308044lfd.234.1652370558853; Thu, 12 May 2022 08:49:18 -0700 (PDT) MIME-Version: 1.0 From: Steve French Date: Thu, 12 May 2022 10:49:07 -0500 Message-ID: Subject: [PATCH][SMB3] EBADF/EIO errors in rename/open caused by race condition in smb2_compound_op To: CIFS , Ondrej Hubsch , Ralph Boehme Cc: ronnie sahlberg , Paulo Alcantara Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org Created patch and merged into cifs-2.6.git for-next (pending the usual regression testing runs) from the good work Ondrej did on https://bugzilla.samba.org/show_bug.cgi?id=15051 See attached. 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. --- Thanks, Steve From 465301e39c1f7268b6edfb16085bebb31c507dd6 Mon Sep 17 00:00:00 2001 From: Steve French Date: Thu, 12 May 2022 10:18:00 -0500 Subject: [PATCH] SMB3: EBADF/EIO errors in rename/open caused by race condition in smb2_compound_op MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- fs/cifs/smb2inode.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/fs/cifs/smb2inode.c b/fs/cifs/smb2inode.c index fe5bfa245fa7..1b89b9b8a212 100644 --- a/fs/cifs/smb2inode.c +++ b/fs/cifs/smb2inode.c @@ -362,8 +362,6 @@ smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, 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], -- 2.34.1