Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp5902266imb; Fri, 8 Mar 2019 05:07:24 -0800 (PST) X-Google-Smtp-Source: APXvYqxyN51Yzrq/U2HDz7chtO2WoBh732YPClZO3ea3tVbeg7FJg3vChDCibnFA2F9OvJ+ReEil X-Received: by 2002:a62:14c6:: with SMTP id 189mr18766470pfu.23.1552050444821; Fri, 08 Mar 2019 05:07:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1552050444; cv=none; d=google.com; s=arc-20160816; b=OG8G51aMymJpqN31QhyjhGEsT5QI4cgHeOjd+Tg7Cf/Z2hmZEdtc8cZG6fllT91pz2 teOMUO44yoZdoPJfelNBQBJfVadSfpPisjULGpC/wRpARMHr05Fd2/+A4xqaIi6x6zWM CtHa+E9SX9I+xA1q+FSsaktOf9f4wtsa6Ce5W7+jMb9XSmXLQAvzo91oA9EtVK5JBCPI xxdCuFa/myW9fXDsLGqvU18Gh2XfufcxH40lLzZTAjZw0DFPVV3w9AcPLjalU5L8Olvy ElCuMNGreIOmAXZ6r8048RRUXZjadaWM/YJGSpIDjsxruxtUWRTkXhOc6zedhGSmSy6I AdHw== 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=Wr1/lclRrLHPC5CD/DzfiFdN15c+uhdqGEYr15as0U0=; b=z7Ut431EwGEaKXWUmQoSbwi0O10afNRnyjkYXAzmY0uA9Ac7yaj4jKHX4YwfMkz6NF VnP1zZUP2bHISNdkkdI3RlqmAFb/JnolIWR8hrGTy2vKJBQngo0O54W7xQSlLviLQbnD qMlYZb4ZCpDU1+LrYwT+kvs2hvBYpUseQAAq55r7LOVPzTHrPYlSLiUKlIlzHh3FwkzT jk8YEfMglJEzWuUWxDSQOIZtva6LoYnRJiDqSqjwzKb16oo3l7br/ZyDLN854qATsBIV fvg2JNufjmeQ/Ohjxwrve3d1StqOIq1U1NBe0Kd6Sn2yDiF8gXCYx3DrADyIBRfy1IcC LiPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ruVemCxV; 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 e20si6669696pfi.237.2019.03.08.05.07.09; Fri, 08 Mar 2019 05:07:24 -0800 (PST) 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=ruVemCxV; 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 S1727803AbfCHM5H (ORCPT + 99 others); Fri, 8 Mar 2019 07:57:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:33394 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727777AbfCHM5F (ORCPT ); Fri, 8 Mar 2019 07:57:05 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 E9A0320449; Fri, 8 Mar 2019 12:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552049824; bh=ee7F66Pj9seVyptRfG0AhNvbHcZhSMt0VbayY23RpNw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ruVemCxVKOH8S6y8S5qxCan0RJOZ/AlrJZm59DJkER63RZm+/x1zSk1FVc9OXDBTF BAbXVSSfmNZPDIM8yQVjvcBkUZL/kCh63j7vqGCjKCEV4Lg4cot9G+xUsPJrtVw0Xs dXStvCvbjoVxHcIMkk/onCt9VUxn94RSk0JFSpRQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Matthias Kaehlcke , Douglas Anderson , Marcel Holtmann Subject: [PATCH 4.20 73/76] Bluetooth: Fix locking in bt_accept_enqueue() for BH context Date: Fri, 8 Mar 2019 13:50:25 +0100 Message-Id: <20190308124917.378888670@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190308124914.789210760@linuxfoundation.org> References: <20190308124914.789210760@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Matthias Kaehlcke commit c4f5627f7eeecde1bb6b646d8c0907b96dc2b2a6 upstream. With commit e16337622016 ("Bluetooth: Handle bt_accept_enqueue() socket atomically") lock_sock[_nested]() is used to acquire the socket lock before manipulating the socket. lock_sock[_nested]() may block, which is problematic since bt_accept_enqueue() can be called in bottom half context (e.g. from rfcomm_connect_ind()): [] __might_sleep+0x4c/0x80 [] lock_sock_nested+0x24/0x58 [] bt_accept_enqueue+0x48/0xd4 [bluetooth] [] rfcomm_connect_ind+0x190/0x218 [rfcomm] Add a parameter to bt_accept_enqueue() to indicate whether the function is called from BH context, and acquire the socket lock with bh_lock_sock_nested() if that's the case. Also adapt all callers of bt_accept_enqueue() to pass the new parameter: - l2cap_sock_new_connection_cb() - uses lock_sock() to lock the parent socket => process context - rfcomm_connect_ind() - acquires the parent socket lock with bh_lock_sock() => BH context - __sco_chan_add() - called from sco_chan_add(), which is called from sco_connect(). parent is NULL, hence bt_accept_enqueue() isn't called in this code path and we can ignore it - also called from sco_conn_ready(). uses bh_lock_sock() to acquire the parent lock => BH context Fixes: e16337622016 ("Bluetooth: Handle bt_accept_enqueue() socket atomically") Signed-off-by: Matthias Kaehlcke Reviewed-by: Douglas Anderson Signed-off-by: Marcel Holtmann Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- include/net/bluetooth/bluetooth.h | 2 +- net/bluetooth/af_bluetooth.c | 16 +++++++++++++--- net/bluetooth/l2cap_sock.c | 2 +- net/bluetooth/rfcomm/sock.c | 2 +- net/bluetooth/sco.c | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -276,7 +276,7 @@ int bt_sock_ioctl(struct socket *sock, int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo); int bt_sock_wait_ready(struct sock *sk, unsigned long flags); -void bt_accept_enqueue(struct sock *parent, struct sock *sk); +void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh); void bt_accept_unlink(struct sock *sk); struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock); --- a/net/bluetooth/af_bluetooth.c +++ b/net/bluetooth/af_bluetooth.c @@ -154,15 +154,25 @@ void bt_sock_unlink(struct bt_sock_list } EXPORT_SYMBOL(bt_sock_unlink); -void bt_accept_enqueue(struct sock *parent, struct sock *sk) +void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh) { BT_DBG("parent %p, sk %p", parent, sk); sock_hold(sk); - lock_sock_nested(sk, SINGLE_DEPTH_NESTING); + + if (bh) + bh_lock_sock_nested(sk); + else + lock_sock_nested(sk, SINGLE_DEPTH_NESTING); + list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q); bt_sk(sk)->parent = parent; - release_sock(sk); + + if (bh) + bh_unlock_sock(sk); + else + release_sock(sk); + parent->sk_ack_backlog++; } EXPORT_SYMBOL(bt_accept_enqueue); --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -1252,7 +1252,7 @@ static struct l2cap_chan *l2cap_sock_new l2cap_sock_init(sk, parent); - bt_accept_enqueue(parent, sk); + bt_accept_enqueue(parent, sk, false); release_sock(parent); --- a/net/bluetooth/rfcomm/sock.c +++ b/net/bluetooth/rfcomm/sock.c @@ -988,7 +988,7 @@ int rfcomm_connect_ind(struct rfcomm_ses rfcomm_pi(sk)->channel = channel; sk->sk_state = BT_CONFIG; - bt_accept_enqueue(parent, sk); + bt_accept_enqueue(parent, sk, true); /* Accept connection and return socket DLC */ *d = rfcomm_pi(sk)->dlc; --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -193,7 +193,7 @@ static void __sco_chan_add(struct sco_co conn->sk = sk; if (parent) - bt_accept_enqueue(parent, sk); + bt_accept_enqueue(parent, sk, true); } static int sco_chan_add(struct sco_conn *conn, struct sock *sk,