Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp903128yba; Thu, 18 Apr 2019 11:30:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8+UwxZExomVVeey3N0o7GRgIpE6TKZGHbfB/bQ11nC1O60dZuDxqVeCHR9+pqBBJY8S0+ X-Received: by 2002:a63:4620:: with SMTP id t32mr88980670pga.363.1555612250012; Thu, 18 Apr 2019 11:30:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555612250; cv=none; d=google.com; s=arc-20160816; b=Er7n4J4VKFw1pv1wcGOl9XGgDWjqf553BLYROTOZ/ESAxnw4j9h3A1WyNM5Vcrx2sP CyCgYn0+bjLWxa4FqCByGGtNtTG+mM5fTjTWv0fQokPjXmOxk+7DtzS9IbedNtuceSWU EclITuyu4Ot3Zc9pLriI08kDbbIAtKdIEPRbOiToQnIKvqksciZ3AWwWElDDlSf/pe1p q1hibM0E5RN7Rw+O74YeNso5Ov8918YJ7jewD+Rxuz/l6biP6mSB82p3DZSyOz7iZxQf cm94KJ8KkzhUpEEUREivPCp1WKJe/UCqfSuII1D8aq5Zr4PO9nFDuVkF8/cjglrZHZP0 +UlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5Igk0usYW4xI9GxkL8hugfxJ9xUgWJnNylBdEAw4QPc=; b=X2oY1Tc4geP5HOIDO9dYDjTvd0pzffFpygJ2uZEncNR+KTVoytafZf+tHje8FM2Gdk k8C/ppn1AfxR2x9aTZVrx2VuSecBfD1UkpjwnDweJo3/7CzUGPfhuEOU4rY0d/8L/QNJ lW0EsjriQSyrW7cRVX+r+lVOf5wENi4a5AiQAFII0RAELmgJLi1UBJiE+WndVhSU1xdg 9836J3nOwxCu6BR/4LfJlPTuIP4FbXkY0kNprnWJ+Y9YMKvYZgwND5O50ptIa7IIheo2 wmH1wciOIM86hlLLPwAySxHPhZI2wV5Jwau0HxEyc7Md5g5E20ZqLtxQCSJex0m2pR8+ yyzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="1HmrB2s/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v10si2712462plo.439.2019.04.18.11.30.33; Thu, 18 Apr 2019 11:30:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="1HmrB2s/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390687AbfDRS3i (ORCPT + 99 others); Thu, 18 Apr 2019 14:29:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:58590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390110AbfDRSC2 (ORCPT ); Thu, 18 Apr 2019 14:02:28 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4B2B8217FA; Thu, 18 Apr 2019 18:02:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555610546; bh=vaHdLYsIgzmR2z/eJl8FaDyukq+rorcealy9iB4ZIAU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1HmrB2s/l+0ydWOB/ZpssaPRi8gzb7NAwqMb/S/X85cMGFn2UBT5C3YNPG95kABMz 2RQKYij53OedhuAtY9sCEfqan+Mwwsag2p4MMH4pNL1EMiMN3luxnYvVbnShSqDQkN Y6cHNC52kFpjZjEeCznvMOZf4MJMnEOSbMPXJkPs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Howells , Marc Dionne , "David S. Miller" , Sasha Levin Subject: [PATCH 4.19 103/110] rxrpc: Fix client call connect/disconnect race Date: Thu, 18 Apr 2019 19:57:32 +0200 Message-Id: <20190418160447.368256784@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190418160437.484158340@linuxfoundation.org> References: <20190418160437.484158340@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 930c9f9125c85b5134b3e711bc252ecc094708e3 ] rxrpc_disconnect_client_call() reads the call's connection ID protocol value (call->cid) as part of that function's variable declarations. This is bad because it's not inside the locked section and so may race with someone granting use of the channel to the call. This manifests as an assertion failure (see below) where the call in the presumed channel (0 because call->cid wasn't set when we read it) doesn't match the call attached to the channel we were actually granted (if 1, 2 or 3). Fix this by moving the read and dependent calculations inside of the channel_lock section. Also, only set the channel number and pointer variables if cid is not zero (ie. unset). This problem can be induced by injecting an occasional error in rxrpc_wait_for_channel() before the call to schedule(). Make two further changes also: (1) Add a trace for wait failure in rxrpc_connect_call(). (2) Drop channel_lock before BUG'ing in the case of the assertion failure. The failure causes a trace akin to the following: rxrpc: Assertion failed - 18446612685268945920(0xffff8880beab8c00) == 18446612685268621312(0xffff8880bea69800) is false ------------[ cut here ]------------ kernel BUG at net/rxrpc/conn_client.c:824! ... RIP: 0010:rxrpc_disconnect_client_call+0x2bf/0x99d ... Call Trace: rxrpc_connect_call+0x902/0x9b3 ? wake_up_q+0x54/0x54 rxrpc_new_client_call+0x3a0/0x751 ? rxrpc_kernel_begin_call+0x141/0x1bc ? afs_alloc_call+0x1b5/0x1b5 rxrpc_kernel_begin_call+0x141/0x1bc afs_make_call+0x20c/0x525 ? afs_alloc_call+0x1b5/0x1b5 ? __lock_is_held+0x40/0x71 ? lockdep_init_map+0xaf/0x193 ? lockdep_init_map+0xaf/0x193 ? __lock_is_held+0x40/0x71 ? yfs_fs_fetch_data+0x33b/0x34a yfs_fs_fetch_data+0x33b/0x34a afs_fetch_data+0xdc/0x3b7 afs_read_dir+0x52d/0x97f afs_dir_iterate+0xa0/0x661 ? iterate_dir+0x63/0x141 iterate_dir+0xa2/0x141 ksys_getdents64+0x9f/0x11b ? filldir+0x111/0x111 ? do_syscall_64+0x3e/0x1a0 __x64_sys_getdents64+0x16/0x19 do_syscall_64+0x7d/0x1a0 entry_SYSCALL_64_after_hwframe+0x49/0xbe Fixes: 45025bceef17 ("rxrpc: Improve management and caching of client connection objects") Signed-off-by: David Howells Reviewed-by: Marc Dionne Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/trace/events/rxrpc.h | 2 ++ net/rxrpc/conn_client.c | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/trace/events/rxrpc.h b/include/trace/events/rxrpc.h index 573d5b901fb1..6d182746afab 100644 --- a/include/trace/events/rxrpc.h +++ b/include/trace/events/rxrpc.h @@ -76,6 +76,7 @@ enum rxrpc_client_trace { rxrpc_client_chan_disconnect, rxrpc_client_chan_pass, rxrpc_client_chan_unstarted, + rxrpc_client_chan_wait_failed, rxrpc_client_cleanup, rxrpc_client_count, rxrpc_client_discard, @@ -275,6 +276,7 @@ enum rxrpc_tx_point { EM(rxrpc_client_chan_disconnect, "ChDisc") \ EM(rxrpc_client_chan_pass, "ChPass") \ EM(rxrpc_client_chan_unstarted, "ChUnst") \ + EM(rxrpc_client_chan_wait_failed, "ChWtFl") \ EM(rxrpc_client_cleanup, "Clean ") \ EM(rxrpc_client_count, "Count ") \ EM(rxrpc_client_discard, "Discar") \ diff --git a/net/rxrpc/conn_client.c b/net/rxrpc/conn_client.c index 6e419b15a9f8..c979a56faaef 100644 --- a/net/rxrpc/conn_client.c +++ b/net/rxrpc/conn_client.c @@ -707,6 +707,7 @@ int rxrpc_connect_call(struct rxrpc_sock *rx, ret = rxrpc_wait_for_channel(call, gfp); if (ret < 0) { + trace_rxrpc_client(call->conn, ret, rxrpc_client_chan_wait_failed); rxrpc_disconnect_client_call(call); goto out; } @@ -777,16 +778,22 @@ static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet) */ void rxrpc_disconnect_client_call(struct rxrpc_call *call) { - unsigned int channel = call->cid & RXRPC_CHANNELMASK; struct rxrpc_connection *conn = call->conn; - struct rxrpc_channel *chan = &conn->channels[channel]; + struct rxrpc_channel *chan = NULL; struct rxrpc_net *rxnet = conn->params.local->rxnet; + unsigned int channel = -1; + u32 cid; + spin_lock(&conn->channel_lock); + + cid = call->cid; + if (cid) { + channel = cid & RXRPC_CHANNELMASK; + chan = &conn->channels[channel]; + } trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect); call->conn = NULL; - spin_lock(&conn->channel_lock); - /* Calls that have never actually been assigned a channel can simply be * discarded. If the conn didn't get used either, it will follow * immediately unless someone else grabs it in the meantime. @@ -810,7 +817,10 @@ void rxrpc_disconnect_client_call(struct rxrpc_call *call) goto out; } - ASSERTCMP(rcu_access_pointer(chan->call), ==, call); + if (rcu_access_pointer(chan->call) != call) { + spin_unlock(&conn->channel_lock); + BUG(); + } /* If a client call was exposed to the world, we save the result for * retransmission. -- 2.19.1