Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2621663pxj; Mon, 31 May 2021 06:46:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJybdxYPOjs6mbs6gMZHhfGj5+lFJm9rlbb8iU223BbTBFwpKNR/lFO4b76xw/XEVl/BHiKp X-Received: by 2002:a92:bf10:: with SMTP id z16mr19002070ilh.304.1622468780844; Mon, 31 May 2021 06:46:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622468780; cv=none; d=google.com; s=arc-20160816; b=VHk20W3Vg5RMQ/xo8kSdEyRtyQMiZarowBmZ35ri+KA4muXLSJxuVEAhmZmz3XMavW BK6/Y5EKhoA3gaR+6LYtU82pr5mzdmirl9SX2T8gUpF74hC9wRvRQ7o9K/yj4Ws19fuT dcxethpx3JgCnL4hbtZUO8uVvnXRqRbrqSznRigByOxMY7xy0wqAsVgJdG3Gfo05FTPO eWMYV9rP2xzlNGHDSblRd8fy2ZKqhec1bii9oWCgBypivamzZyaQQR5qAnyPMNE7Sj+C QGUI1TaWV4In7KRmVBeQnSU42PNtwfOEpq7N0ils7Vfmr/SedV/BEj39iHdaCMJB2Xu+ 9vgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dZ3D+rA0NE1Bu7ALRRAI+Efi6LWL979pAVmTvnj9tpA=; b=LRSo+Oaa1bJUPx6WOmwD06tApH7Qn3u8aEl8XV79rrp6xiNEnbXjiDocstlrzG7XtS k8KFLkWYiSTNEREQrTcLHhb/JB/5dpizoturfRZHSuRkx1RUxusNXCmiK3U3BeJCk44g fvnQKauyvP/QZAu84GYyzLXxxT/iNZLUHC+L7fz8k5lToB0LAGa84N78EscMDxgx6YdO 78ARHIvrNKUhLq4bF1ZpQ4wRZ7vcQAeEheXkMD/tHAMGfa1MSZa6F7+1ZGSgX6th3N6y 8SoSw49yttf0Ns4s0TfdJzt1TUHuqg+SK7gxl43rdZoJ7GdpNJpGVBkztl6VG3fkzAet Xluw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="IJbu/YdH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u4si12333411jad.124.2021.05.31.06.46.07; Mon, 31 May 2021 06:46:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="IJbu/YdH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232107AbhEaNqr (ORCPT + 99 others); Mon, 31 May 2021 09:46:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:40214 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232130AbhEaNc2 (ORCPT ); Mon, 31 May 2021 09:32:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4D3536108D; Mon, 31 May 2021 13:23:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622467432; bh=MSDdwLgqXfSX0xnV9EaZQyWnNiZCGjU8vudd2Vy++EI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IJbu/YdHWJl7ehl0qTCg8YpfxCwzSvdW6ZqMy5Yz1zPkszThUwBEGVqZ4M+X1DOAZ ow8KSZVpLYkkDtswFSGmmbtrhb6M6YC1UNVTfTfmejZ20HMuxH1G3Ye8HwRecbAVpa I9EEC8eM0db5pN6zsZWBVE0J8wX3HhxBU3nh2hns= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thadeu Lima de Souza Cascardo , Marcel Holtmann Subject: [PATCH 4.19 062/116] Bluetooth: cmtp: fix file refcount when cmtp_attach_device fails Date: Mon, 31 May 2021 15:13:58 +0200 Message-Id: <20210531130642.271036712@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130640.131924542@linuxfoundation.org> References: <20210531130640.131924542@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thadeu Lima de Souza Cascardo commit 8da3a0b87f4f1c3a3bbc4bfb78cf68476e97d183 upstream. When cmtp_attach_device fails, cmtp_add_connection returns the error value which leads to the caller to doing fput through sockfd_put. But cmtp_session kthread, which is stopped in this path will also call fput, leading to a potential refcount underflow or a use-after-free. Add a refcount before we signal the kthread to stop. The kthread will try to grab the cmtp_session_sem mutex before doing the fput, which is held when get_file is called, so there should be no races there. Reported-by: Ryota Shiga Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Marcel Holtmann Signed-off-by: Greg Kroah-Hartman --- net/bluetooth/cmtp/core.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/net/bluetooth/cmtp/core.c +++ b/net/bluetooth/cmtp/core.c @@ -391,6 +391,11 @@ int cmtp_add_connection(struct cmtp_conn if (!(session->flags & BIT(CMTP_LOOPBACK))) { err = cmtp_attach_device(session); if (err < 0) { + /* Caller will call fput in case of failure, and so + * will cmtp_session kthread. + */ + get_file(session->sock->file); + atomic_inc(&session->terminate); wake_up_interruptible(sk_sleep(session->sock->sk)); up_write(&cmtp_session_sem);