Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2656280pxj; Mon, 31 May 2021 07:32:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrFQswmpc7/w53nUTTP7mlrA0Yf4uL8GWEAI9UUiU8IZ+q3t2AjyfIou8NWtMgWaxGN9pX X-Received: by 2002:a6b:5a16:: with SMTP id o22mr17535910iob.63.1622471553519; Mon, 31 May 2021 07:32:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622471553; cv=none; d=google.com; s=arc-20160816; b=QBuGrbBTtQYuMQTqqmre+Gh/7wVfXCMyhR4iTf7nsmftCrPbxR65kXfFLLZOpJOYEP AsiY9RJnoQjolZbmZ6xeHIco5uVd6YllPUWfEEGQo4RkDOILiIQmSN5XpKW3WSxTz1+m JrEebUmezTgbec+dSRlu9aurzxUNnvIey0NhUDftYB0EbNAGnRojLgE9LXpj3dKp6Zb7 as3AfIRdN8lWGGcL+1w2WEr1QntQpiQ5VXZACo5Z2if5xRBd8Y+xq8FuEQw9lvITYNom P+ERkPklbVkYEDknl8NshMa+u4371diqhQRmGRslvLpJZmxx3mdeFt0CbQXB5m073kmX fAJQ== 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=SuPyN31BbwzulvxeEJ+PO+mC2+ipEbNYzGM7zIftRjrYvIi7RXL1zGqG1/qdY9ac7F HaU1kn1NiWW0EsRlrK2hyUkGd6hYbKlOFYnjRoDcSvAihZDej8IdB+fcYJttbFv36VJ6 3fH7J7mVyQKvV2Mnr4wSquVysTNsfVWrVj9xMbE/guXlsGqiToYKlcKK3aukIdADG79m EU1DwhL4yF3cOfihv3/iQo1owsnDYJX7PZpKe7A3cJnTsoZXWm8BfzX28cFM4Fuf9o4f lR/NLiDJUJPnr5Vqq0bSriXALbHs+BooJWOnpXYtLKuEI27abxNUHigq4HBGOGd/QSoR gXoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=K+BVNwDw; 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 z8si6226624jao.3.2021.05.31.07.32.20; Mon, 31 May 2021 07:32:33 -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=K+BVNwDw; 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 S233113AbhEaOcW (ORCPT + 99 others); Mon, 31 May 2021 10:32:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:55720 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233118AbhEaNya (ORCPT ); Mon, 31 May 2021 09:54:30 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2774061439; Mon, 31 May 2021 13:33:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622468027; bh=IQ7IhCWeAmbY4PS5fgPbkHkaNBhyHOKfgSAxpYDpQB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K+BVNwDw2VnC81GDOBi7Z4Ox3buYMbUIGeNbroSM5Buz3Dkka5o+uJUEY8Tf22Yz1 craLb+4288r/QX9Dsnu5bJiReA+0Y3cwguyNASs9p4AGiZDZRpR7hpSBY8gtAJm/Oa Xv7Ij4TxHE1LYhTWEHUsN4UWG3x/lqxY/OG2jz8Y= 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.10 090/252] Bluetooth: cmtp: fix file refcount when cmtp_attach_device fails Date: Mon, 31 May 2021 15:12:35 +0200 Message-Id: <20210531130701.027736633@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130657.971257589@linuxfoundation.org> References: <20210531130657.971257589@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);