Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2950210pxb; Tue, 13 Apr 2021 14:32:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx21RSh6fwq2h2M0uIzGO75WMM3Z0IgBs+yBj+AAhSeSYO043yyoqFiFAoxBMdQLZ4XeJrJ X-Received: by 2002:a05:6402:4395:: with SMTP id o21mr36400030edc.22.1618349545235; Tue, 13 Apr 2021 14:32:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618349545; cv=none; d=google.com; s=arc-20160816; b=IqksSC8Hg+f5YijJkthrB/sxQu3VSoBrnIazOB0dI/kboX31iCHMpaFfZac8yKljS1 wehf4aRAZAMCNsqqlcMwdRlEhfpz+5put5BrnC6eYwmg8mU7ZHzxhWD99g1FQzoGT8aY hFhwm4GP/PY823z4tifLcKtx/8B1RvAkY+slODr95h5IRdUIhGPPCBsdsWr4uHA3LJNM ASSNa20wm7PG3C3Y0hzTpA91FDVyekFeqtfb5tkNkB3yJu/3OIM/xkzSgeKPb20NE6Qi aGhI+xi0iIqZbF4RHTJ+aRl55Sf6JdIDtc3CPwX9WB/pD2dDRmpSdY3jWBcC1OQuxaQP 3nbw== 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 :message-id:date:subject:cc:to:from; bh=sU4YAWLxZD3IDAOutbHGFmbSn/P8Mr9ND1b/bMznSh4=; b=OpjcUkNheiWTT4BdbkSMX4dNw1iz0lga2jKGcfKAjXv1164UotOZZu8GaCoG3rDJTn NXc4CO0I/PEhwD45mEfGeGZfkk1ykcy7p9Iy/FPepqL+0/pOaIdowVV5cpe6tRsZIY44 ZHrDwnbYJaGrys+E2q8yWC6qw1UUVmZZApkJZH6ny/4UaBEB6EKsdkHP3Pg2kirKnzQr XjMvUV/hUMOC89C9mEwg8URLbtnUi0Xqe7dHnSFdJ1SH2GbeDBqYK0ERN13pp3C2nCpf 4doRT4Wa5jUvphPjxfYvWlIXiHUsfHWW11vF1QShYdo4ls8G4FqSB7N0Kf6Hu/Z4fh75 wK2g== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w16si11133375edd.471.2021.04.13.14.31.46; Tue, 13 Apr 2021 14:32:25 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244149AbhDMQWO (ORCPT + 99 others); Tue, 13 Apr 2021 12:22:14 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:33289 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243945AbhDMQWM (ORCPT ); Tue, 13 Apr 2021 12:22:12 -0400 Received: from [179.93.186.178] (helo=localhost.localdomain) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1lWLnQ-0002gG-O5; Tue, 13 Apr 2021 16:21:49 +0000 From: Thadeu Lima de Souza Cascardo To: linux-bluetooth@vger.kernel.org Cc: netdev@vger.kernel.org, kuba@kernel.org, davem@davemloft.net, isdn@linux-pingi.de, marcel@holtmann.org, johan.hedberg@gmail.com, luiz.dentz@gmail.com, Thadeu Lima de Souza Cascardo Subject: [PATCH] net: bluetooth: cmtp: fix file refcount when cmtp_attach_device fails Date: Tue, 13 Apr 2021 13:21:03 -0300 Message-Id: <20210413162103.435467-1-cascardo@canonical.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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 --- net/bluetooth/cmtp/core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/bluetooth/cmtp/core.c b/net/bluetooth/cmtp/core.c index 07cfa3249f83..0a2d78e811cf 100644 --- a/net/bluetooth/cmtp/core.c +++ b/net/bluetooth/cmtp/core.c @@ -392,6 +392,11 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock) 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); -- 2.27.0