From patchwork Thu Jun 22 18:16:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Prasad N X-Patchwork-Id: 13289518 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 6551DEB64D8 for ; Thu, 22 Jun 2023 18:16:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229916AbjFVSQU (ORCPT ); Thu, 22 Jun 2023 14:16:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230047AbjFVSQT (ORCPT ); Thu, 22 Jun 2023 14:16:19 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12C201FEF for ; Thu, 22 Jun 2023 11:16:16 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6686708c986so5299783b3a.0 for ; Thu, 22 Jun 2023 11:16:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687457775; x=1690049775; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=jJoIo3eKf/KUkaUsmxpRYsVA8vyogyaviB/pOsNHsE4=; b=sT5E9gk8lO1O6EQnHrSgRqui3o20QjF4RBr/GanEn2QYHlm499g0XcFniC7PLRTpXB QCq/zkAc+Yw8imR2wL8xgvG1q/FXYQg2wJS3ECjmZsDea22OoSsz4p6gjUAVbPpqwFMN wgYxV4ay5p+sos1fVvS0ZSVzpufKoYUD/EZE9Ad171PUoN0vPTK4eVz81O3QZzULIOxH +7G9J9SxRTj8oLXS+SCTCBPyfGKuWPJNTyS+0qC6NQ50nhpWnWiRq2jGf8m/bGNWVbyd K71Fgsz9DdXxDlOc9H5OLg6GjO7XO8pyT1Yfs89R1wdd3lBBhKRowoeRBSCcz4j2VHjO BSxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687457775; x=1690049775; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jJoIo3eKf/KUkaUsmxpRYsVA8vyogyaviB/pOsNHsE4=; b=cv9ZFZioNAojT6ahtyU1nq5JKungAps5Mfrvzdc+ohRb2go1IfazYCcXt6JLICFb+R tc03EoAvEJKfO1j5+OYYuGbOYOL3b3KDAO/Ur6wNcha8rVkQn5Z0cFvyP9edwEmW2c7P f+QgXapSMzp1kDAKenzJ0iH8Ii5B9qUdjpWukHeYddA0VumfmWhgUDKrKNAqqJADREet iu2j1XpYRt8NgOVGFlHmXzIMKvt7e23nwHdBGyoeySQmvxbqiZk4YE2nF3oWnajYaVlf GFRMfpJAotpFAHc5zRcs/XYKVjIfSzVLx/rnoH598UQVdyQMbvAByNHM8KGFP3MQsgKF uXeQ== X-Gm-Message-State: AC+VfDxwiYhJNALgdFvplEZh3YQ5X6b9+FoxE9Zk5MMHANkv85Xnyj/j 9jZ7RqZu7dT0UC0yP1+BeuMlTpLBhOxyCcfj X-Google-Smtp-Source: ACHHUZ6CibvVv/PNGZ1NWMt4kCtxwz6Nxq6no1s+lIcbZHJMc5vKqkK58iWs8F1JSmgdQ0tU68WwQg== X-Received: by 2002:a05:6a00:991:b0:662:4041:30ba with SMTP id u17-20020a056a00099100b00662404130bamr25691655pfg.0.1687457774941; Thu, 22 Jun 2023 11:16:14 -0700 (PDT) Received: from lindev-local-latest.corp.microsoft.com ([2404:f801:8028:1:7e0e:5dff:fea8:2c14]) by smtp.gmail.com with ESMTPSA id h4-20020aa786c4000000b0064fdf576421sm4996536pfo.142.2023.06.22.11.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jun 2023 11:16:14 -0700 (PDT) From: Shyam Prasad N X-Google-Original-From: Shyam Prasad N To: linux-cifs@vger.kernel.org, smfrench@gmail.com, pc@cjr.nz, ematsumiya@suse.de, bharathsm.hsk@gmail.com Cc: Shyam Prasad N Subject: [PATCH 1/3] cifs: log session id when a matching ses is not found Date: Thu, 22 Jun 2023 18:16:02 +0000 Message-Id: <20230622181604.4788-1-sprasad@microsoft.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org We do not log the session id in crypt_setup when a matching session is not found. Printing the session id helps debugging here. This change does just that. Signed-off-by: Shyam Prasad N --- fs/smb/client/smb2ops.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index a8bb9d00d33a..8e4900f6cd53 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -4439,8 +4439,8 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst, rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key); if (rc) { - cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__, - enc ? "en" : "de"); + cifs_server_dbg(VFS, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__, + enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId)); return rc; } From patchwork Thu Jun 22 18:16:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Prasad N X-Patchwork-Id: 13289519 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 B7D31EB64DA for ; Thu, 22 Jun 2023 18:16:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229564AbjFVSQU (ORCPT ); Thu, 22 Jun 2023 14:16:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230120AbjFVSQT (ORCPT ); Thu, 22 Jun 2023 14:16:19 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2D721FF0 for ; Thu, 22 Jun 2023 11:16:18 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-666ecf9a081so5585884b3a.2 for ; Thu, 22 Jun 2023 11:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687457778; x=1690049778; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b7lXteBg2E4gDENb3x9RL2pvskXHgp5VsHkqQb2YtFE=; b=Ei83WuwrlqiIP/dYjUGlMSYveEpBXhi4y34VO3zHU1rU4LyZqagXbp18W0PZM1X4CE 9Ym/mVAbLyjy8Z9o1oU6CH0iEqvxikZ1fx/UoFkXv/lDeKAewuC3ZlYkZCr/Ofiina8U oSLNw/rpDqwqhIa/4E20VS1IbnOCBW9cb3rekdKwoT27JtloEIaneKuenG7fXczDTO6t 6L8Jt02+uZVgvTldur6v83xdGiBY4HLLIfoTRrS1+yB0z0GsokikuntkoKOnDqa+FbDP an4mIibmWBWQbVvxhBUJzTUi3eakNTI1PdbyoANY1M62MMyXjNCqcUPLj1egnrGy57EX uRjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687457778; x=1690049778; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b7lXteBg2E4gDENb3x9RL2pvskXHgp5VsHkqQb2YtFE=; b=ie8VQeDT7LRh9DJSkb8ZbSmC7vWk5Y5njEgireH+3V6mnjvRPTeqtd8TOzKlgWxwX+ mi2FCCnjB+JKHEE/ovZ4KvoN4wfq4mW0RcIbDRxtZ/KQLmeVzB+uUeFhubME/GgElefN Dg9T5CRLE0L6GN5J69dMmg0zWjLFr9AjE/wHFsQKnO2w/YVDi4NyfyY0thLGhNlPvNQ2 yCVYe8WagxOVcVuO5bcrnBcJzLfVzNjSAnNKKs/OJm7Kh9whBMhAAmP2rpETCoSa0zq5 M8PcBqls7HHxl4wwtsIg9t3YTCMY/bV5kfYrkmS/pQ0IF9ywYRTvDQ9cFc8fETAkCPQk jJCw== X-Gm-Message-State: AC+VfDzfTiLGKv8JHGP5dGD4XKPWbDxo479MSLl1CWBQwTwUah5vXYh1 iv3IVdoVD6u8KHn4dV4JsywBMrYZaB9wDCGd X-Google-Smtp-Source: ACHHUZ5L2/N4O/zJH1piEYGjyPG7slNetWS/WQzQYdjy/p5SwFWLJaFFjhIFnlq8tqp5WVLTJX/VkA== X-Received: by 2002:a05:6a00:2d0e:b0:668:96c8:a8ee with SMTP id fa14-20020a056a002d0e00b0066896c8a8eemr9781217pfb.30.1687457777923; Thu, 22 Jun 2023 11:16:17 -0700 (PDT) Received: from lindev-local-latest.corp.microsoft.com ([2404:f801:8028:1:7e0e:5dff:fea8:2c14]) by smtp.gmail.com with ESMTPSA id h4-20020aa786c4000000b0064fdf576421sm4996536pfo.142.2023.06.22.11.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jun 2023 11:16:17 -0700 (PDT) From: Shyam Prasad N X-Google-Original-From: Shyam Prasad N To: linux-cifs@vger.kernel.org, smfrench@gmail.com, pc@cjr.nz, ematsumiya@suse.de, bharathsm.hsk@gmail.com Cc: Shyam Prasad N Subject: [PATCH 2/3] cifs: prevent use-after-free by freeing the cfile later Date: Thu, 22 Jun 2023 18:16:03 +0000 Message-Id: <20230622181604.4788-2-sprasad@microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230622181604.4788-1-sprasad@microsoft.com> References: <20230622181604.4788-1-sprasad@microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org In smb2_compound_op we have a possible use-after-free which can cause hard to debug problems later on. This was revealed during stress testing with KASAN enabled kernel. Fixing it by moving the cfile free call to a few lines below, after the usage. Fixes: 76894f3e2f71 ("cifs: improve symlink handling for smb2+") CC: Paulo Alcantara (SUSE) Signed-off-by: Shyam Prasad N Reviewed-by: Paulo Alcantara (SUSE) --- fs/smb/client/smb2inode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/smb/client/smb2inode.c b/fs/smb/client/smb2inode.c index 163a03298430..7e3ac4cb4efa 100644 --- a/fs/smb/client/smb2inode.c +++ b/fs/smb/client/smb2inode.c @@ -398,9 +398,6 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, rsp_iov); finished: - if (cfile) - cifsFileInfo_put(cfile); - SMB2_open_free(&rqst[0]); if (rc == -EREMCHG) { pr_warn_once("server share %s deleted\n", tcon->tree_name); @@ -529,6 +526,9 @@ static int smb2_compound_op(const unsigned int xid, struct cifs_tcon *tcon, break; } + if (cfile) + cifsFileInfo_put(cfile); + if (rc && err_iov && err_buftype) { memcpy(err_iov, rsp_iov, 3 * sizeof(*err_iov)); memcpy(err_buftype, resp_buftype, 3 * sizeof(*err_buftype)); From patchwork Thu Jun 22 18:16:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shyam Prasad N X-Patchwork-Id: 13289520 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 8C373EB64DA for ; Thu, 22 Jun 2023 18:16:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230062AbjFVSQX (ORCPT ); Thu, 22 Jun 2023 14:16:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230047AbjFVSQX (ORCPT ); Thu, 22 Jun 2023 14:16:23 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B21E1FE9 for ; Thu, 22 Jun 2023 11:16:22 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-666fb8b1bc8so5619799b3a.1 for ; Thu, 22 Jun 2023 11:16:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687457781; x=1690049781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3PJzkvfJJ/w9PB8YUM8fK+3muONP6HcrChuABvZAryg=; b=A50JQOyowBkbW4faJ1cn115+eb9bmIKPqMYXNct2+sGAS5pteFfJldG2/TsW+3i7nK RospZLaSpOQjSlEOOvvUEQBvtDjVQtoXQ2mTDHiTDBk+xPTCbHszKdzQBegFYHCJJs2Q phkgLlrOeZJWVsMRDATssmjPRhNXRlnlH9cNnTuW98iXvfX76XqiwbIiff6E+2J3zNEb eo0BDD0hz5Y32yZWrFz3oAySryA489bou9jSVuvcQut07784SoCYByB9GeBTuIFA+OAk 2c0+ExkJto5ZyoWlHvksgUnU2l/UBGF+Tkt6mx/85Mr9EcjFqupRmpapFlFsO3kkdL6+ mvpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687457781; x=1690049781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3PJzkvfJJ/w9PB8YUM8fK+3muONP6HcrChuABvZAryg=; b=iwu/I31EZgeTDt2wvG8JVojrVFeV8iMoeFG7QRMSY65OXlc3Oi2Jf4GpCbS5rmD3B5 cgL5bzZwzhnqzszC6aajqS7hjSUrpxNDLf3rHUeWIsTfa5Z9pqhbiDaMIoe2pzMuy4Bv KF+2g7PQ+i7rNQpZqqX0ngHnUNFbMKOxscMMlRUAr4QoN1n/62ZrM6YEB0PtzSgHhbem ZZzxoXHA41nG3nPTXtKZ8Pcc26ZdEubmyHvWcbpbMMuIw1q83gTee//ppKybnC/HQ8Q1 xIZfZeBVMg+yNPAUfkOq0JhY+9qChYE8YAmHuNXhRwExgVoZjTuPuGrXBGYYCHrTLFzt V+yw== X-Gm-Message-State: AC+VfDxEEZUY+AeZNEP2UyAs6XwkZz+BpFz6HSXFrEp0y7jZkWoH6tna cMk3Q9RKPyfml6+kXzA+opleb6YMmeJRyvKk X-Google-Smtp-Source: ACHHUZ7DUFv0Gd8EDawH/84ndz+EWH1LkKbS2MEC2a5MiRjiLz8Qo7Ketom/mIKRjAqkvQeIhviDdg== X-Received: by 2002:a05:6a00:801:b0:668:6eed:7c1a with SMTP id m1-20020a056a00080100b006686eed7c1amr19129485pfk.7.1687457780867; Thu, 22 Jun 2023 11:16:20 -0700 (PDT) Received: from lindev-local-latest.corp.microsoft.com ([2404:f801:8028:1:7e0e:5dff:fea8:2c14]) by smtp.gmail.com with ESMTPSA id h4-20020aa786c4000000b0064fdf576421sm4996536pfo.142.2023.06.22.11.16.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Jun 2023 11:16:20 -0700 (PDT) From: Shyam Prasad N X-Google-Original-From: Shyam Prasad N To: linux-cifs@vger.kernel.org, smfrench@gmail.com, pc@cjr.nz, ematsumiya@suse.de, bharathsm.hsk@gmail.com Cc: Shyam Prasad N Subject: [PATCH 3/3] cifs: do all necessary checks for credits within or before locking Date: Thu, 22 Jun 2023 18:16:04 +0000 Message-Id: <20230622181604.4788-3-sprasad@microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230622181604.4788-1-sprasad@microsoft.com> References: <20230622181604.4788-1-sprasad@microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org All the server credits and in-flight info is protected by req_lock. Once the req_lock is held, and we've determined that we have enough credits to continue, this lock cannot be dropped till we've made the changes to credits and in-flight count. However, we used to drop the lock in order to avoid deadlock with the recent srv_lock. This could cause the checks already made to be invalidated. Fixed it by moving the server status check to before locking req_lock. Fixes: d7d7a66aacd6 ("cifs: avoid use of global locks for high contention data") Signed-off-by: Shyam Prasad N --- fs/smb/client/smb2ops.c | 19 ++++++++++--------- fs/smb/client/transport.c | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 8e4900f6cd53..cb07ac32cf38 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -211,6 +211,16 @@ smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size, spin_lock(&server->req_lock); while (1) { + spin_unlock(&server->req_lock); + + spin_lock(&server->srv_lock); + if (server->tcpStatus == CifsExiting) { + spin_unlock(&server->srv_lock); + return -ENOENT; + } + spin_unlock(&server->srv_lock); + + spin_lock(&server->req_lock); if (server->credits <= 0) { spin_unlock(&server->req_lock); cifs_num_waiters_inc(server); @@ -221,15 +231,6 @@ smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size, return rc; spin_lock(&server->req_lock); } else { - spin_unlock(&server->req_lock); - spin_lock(&server->srv_lock); - if (server->tcpStatus == CifsExiting) { - spin_unlock(&server->srv_lock); - return -ENOENT; - } - spin_unlock(&server->srv_lock); - - spin_lock(&server->req_lock); scredits = server->credits; /* can deadlock with reopen */ if (scredits <= 8) { diff --git a/fs/smb/client/transport.c b/fs/smb/client/transport.c index 0474d0bba0a2..f280502a2aee 100644 --- a/fs/smb/client/transport.c +++ b/fs/smb/client/transport.c @@ -522,6 +522,16 @@ wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits, } while (1) { + spin_unlock(&server->req_lock); + + spin_lock(&server->srv_lock); + if (server->tcpStatus == CifsExiting) { + spin_unlock(&server->srv_lock); + return -ENOENT; + } + spin_unlock(&server->srv_lock); + + spin_lock(&server->req_lock); if (*credits < num_credits) { scredits = *credits; spin_unlock(&server->req_lock); @@ -547,15 +557,6 @@ wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits, return -ERESTARTSYS; spin_lock(&server->req_lock); } else { - spin_unlock(&server->req_lock); - - spin_lock(&server->srv_lock); - if (server->tcpStatus == CifsExiting) { - spin_unlock(&server->srv_lock); - return -ENOENT; - } - spin_unlock(&server->srv_lock); - /* * For normal commands, reserve the last MAX_COMPOUND * credits to compound requests. @@ -569,7 +570,6 @@ wait_for_free_credits(struct TCP_Server_Info *server, const int num_credits, * for servers that are slow to hand out credits on * new sessions. */ - spin_lock(&server->req_lock); if (!optype && num_credits == 1 && server->in_flight > 2 * MAX_COMPOUND && *credits <= MAX_COMPOUND) {