Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp84471pxb; Thu, 2 Sep 2021 20:05:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEn11MqltTyvseNW86o/Wno8zCtPLv4TE/GjAueRuTfvfa+rXMECijEbeIveSIKSIojGlL X-Received: by 2002:a17:906:a0c9:: with SMTP id bh9mr1593532ejb.51.1630638313691; Thu, 02 Sep 2021 20:05:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630638313; cv=none; d=google.com; s=arc-20160816; b=YenvkS99brjE3DCI3ziE41kRp8CyncAme6wbAOhCdTk4ATvmJm8c8DcFD1EHrvyEiV rRwWmPY0l8MQjHvNuynhkFZoiCamlAFS6+2xaD1a+2t3eKQ359V6vTQk8yQpSMwSg89r 6jhE3UfV/nGKQcHiN96zJJmxAugNLY1CbP+UKAje2RiTeFMI4ijjM/pCMtK4XYpC6GsT FsOYQkr2PG5uURfTKbDY8eZvBdDRn9x+JIxNeVtxqi0bv0hw3oLatzunUM167lSYSVfO h5tv7u5uwndzffnWhpVr0FHk+SSRJz3JRXEO8X1hXrSDH6O/uFT1i0aGVwQuuznzC73n v1Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:cc:references :to:from:subject; bh=P6uHPMjSQuvm+28Qd4us19m0NrWHBBhi0WErgTdUaWE=; b=E0qQ5IW8wDSkxUlI3CR1TK6oldMMjv04LiY7yW0/z3rVlBeZ7Vg5AmBNDXZKSRh4VX w83ocH03UMIOx1mIDyF1Y1wR0BJv4/r6fb0Sh7xpxebnyGqSE3tTK+BHXRxjCRujQuVn OCz4vHSraLmq7hZ9+u6RiLlsD7z7FV/5ADEjzCpF9mHXNPPyvrw1GOZAe5a6t5W1JEEk tdEQFTYoxmnk1luKoEV7wJ7Zhe9iB/IK3V1WecTGdUo9qqWTsU/amjlUm1h5d6pII9mE 2Xwf1KBo3NaxIFnIGrULXh+nU8SUGfXT3/QmHhU7R0xz64RdK1PWOHKb9TBQPvcr4vyh B/HQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bo26si3880841edb.294.2021.09.02.20.04.31; Thu, 02 Sep 2021 20:05:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345195AbhICCpo (ORCPT + 99 others); Thu, 2 Sep 2021 22:45:44 -0400 Received: from www262.sakura.ne.jp ([202.181.97.72]:63755 "EHLO www262.sakura.ne.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232101AbhICCpn (ORCPT ); Thu, 2 Sep 2021 22:45:43 -0400 Received: from fsav415.sakura.ne.jp (fsav415.sakura.ne.jp [133.242.250.114]) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTP id 1832iWme057819; Fri, 3 Sep 2021 11:44:33 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Received: from www262.sakura.ne.jp (202.181.97.72) by fsav415.sakura.ne.jp (F-Secure/fsigk_smtp/550/fsav415.sakura.ne.jp); Fri, 03 Sep 2021 11:44:32 +0900 (JST) X-Virus-Status: clean(F-Secure/fsigk_smtp/550/fsav415.sakura.ne.jp) Received: from [192.168.1.9] (M106072142033.v4.enabler.ne.jp [106.72.142.33]) (authenticated bits=0) by www262.sakura.ne.jp (8.15.2/8.15.2) with ESMTPSA id 1832iWRB057815 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NO); Fri, 3 Sep 2021 11:44:32 +0900 (JST) (envelope-from penguin-kernel@i-love.sakura.ne.jp) Subject: [PATCH] Bluetooth: avoid page fault from sco_send_frame() From: Tetsuo Handa To: LinMa , linux-bluetooth@vger.kernel.org, Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz References: <15f5a46.b79d9.17ba6802ccd.Coremail.linma@zju.edu.cn> Cc: Linus Torvalds Message-ID: <60f604f8-2a89-fd3f-996f-9d9e4a229427@i-love.sakura.ne.jp> Date: Fri, 3 Sep 2021 11:44:33 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Since userfaultfd mechanism allows sleeping with kernel lock held, avoiding page fault with kernel lock held where possible will make the module more robust. This patch just brings memcpy_from_msg() calls to out of sock lock. This patch is an instant mitigation for CVE-2021-3640. To fully close the race window for this use-after-free problem, we need more changes. Signed-off-by: Tetsuo Handa --- net/bluetooth/sco.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index d9a4e88dacbb..e4b079b31ce9 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -273,7 +273,7 @@ static int sco_connect(struct sock *sk) return err; } -static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) +static int sco_send_frame(struct sock *sk, const void *buf, int len, int flags) { struct sco_conn *conn = sco_pi(sk)->conn; struct sk_buff *skb; @@ -285,14 +285,11 @@ static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) BT_DBG("sk %p len %d", sk, len); - skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); + skb = bt_skb_send_alloc(sk, len, flags & MSG_DONTWAIT, &err); if (!skb) return err; - if (memcpy_from_msg(skb_put(skb, len), msg, len)) { - kfree_skb(skb); - return -EFAULT; - } + memcpy(skb_put(skb, len), buf, len); hci_send_sco(conn->hcon, skb); @@ -714,6 +711,7 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; + void *buf; int err; BT_DBG("sock %p, sk %p", sock, sk); @@ -725,14 +723,23 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, if (msg->msg_flags & MSG_OOB) return -EOPNOTSUPP; + buf = kmalloc(len, GFP_KERNEL | __GFP_NOWARN); + if (!buf) + return -ENOMEM; + if (memcpy_from_msg(buf, msg, len)) { + kfree(buf); + return -EFAULT; + } + lock_sock(sk); if (sk->sk_state == BT_CONNECTED) - err = sco_send_frame(sk, msg, len); + err = sco_send_frame(sk, buf, len, msg->msg_flags); else err = -ENOTCONN; release_sock(sk); + kfree(buf); return err; } -- 2.30.2