Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2698320pxj; Mon, 31 May 2021 08:31:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqChtlNmVfqKlmkD+cde68kG04rXVwUziPAumnaSU69TfCJzhrEPEXxYz9iWJjoeo/pD4Q X-Received: by 2002:a17:906:6d43:: with SMTP id a3mr23586608ejt.142.1622475090736; Mon, 31 May 2021 08:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622475090; cv=none; d=google.com; s=arc-20160816; b=a9HCqPlZXKd25m3Evfu57CufKLE7ZbAiFzxp4RAZF+Li1ncuBD4viPUSWw1XjsS/Fz UbrBSswD2KEkvqfxLUhlpabrQZsC2JLFcdN8BISSTIzvLQUSLaOnrqBC3Odi7bsrP/9Q uk5CpgAaP2N8B/kjXYqRfuyJi64FIyEuMwtjC2vAVmmSWkrg5eKfncBaDB5PuW7VQNSk GaPKcj0ZJfUZgoZ4qTuTWXpAEBzyKK+quq00Y3GBoptDBnO2S7MBP1/2unyGuHvIaflD NNCoMXGkBaKbnT5iE7+pYPYo2o6LtopN2QWl9V68rnHGMIIk5BFAjPx/3ZI/j8m3Ksg5 4Z9A== 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=cBdb4hcFtUs5TvL/Wlt6KRoldt1X+v7yy75qLKiwJW4=; b=V0ArA4lmX8RJMXzg9+LePon3kFedoQSBvfp9BrOgcKDCN7qAI5OmqUvNMT1+OmJWzo 0/zVsKZJFUXOB7Txbd0fuf4gEXJVCkX5hNZkPpEaLMxHGF+fHhxPZPXcRRWf39VXHUh8 WeYv8hAshR8AggKIZK043eOa+bJJT2I/EJ3Nyoy6i1GqM5AB99LatFVZeeY+btgPLyNJ B0CkcakICzjsaD/21mL4Kp2uTAuRuMfZ5/ENFH3TAdZwJlXY++V/uBHmSVw8+WgJeZNS m0zxsFU7Ztsku8cCsTV+zPPHu4qqwgnF0u1R463C2h2Kvyowxkx/8cwHhW+lxgM5Dp6E s+Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BH6YM1lp; 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 w7si11433839edc.254.2021.05.31.08.31.08; Mon, 31 May 2021 08:31:30 -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=BH6YM1lp; 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 S234210AbhEaPbR (ORCPT + 99 others); Mon, 31 May 2021 11:31:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:43310 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232712AbhEaORn (ORCPT ); Mon, 31 May 2021 10:17:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 58E2B619B3; Mon, 31 May 2021 13:43:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622468628; bh=IQ7IhCWeAmbY4PS5fgPbkHkaNBhyHOKfgSAxpYDpQB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BH6YM1lp/3bPNPY36tQU5M3VR6AmE/vOPTlfWJE5ZdIurgn4BYsVObxrnIZKZcVwJ NZ2ZHhCvdIDZYdcA33ljxyXK/KUYOSxdORLgTqxZzjQDPC1KAHkXRhnO+rlApdkBVm rkVE7cGWcakM+rrtgaYtpWZF6wuTHgvZUPZXmDGA= 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 5.4 065/177] Bluetooth: cmtp: fix file refcount when cmtp_attach_device fails Date: Mon, 31 May 2021 15:13:42 +0200 Message-Id: <20210531130650.150859991@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130647.887605866@linuxfoundation.org> References: <20210531130647.887605866@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 @@ -392,6 +392,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);