Skip to content

Commit 1b364f8

Browse files
Thadeu Lima de Souza Cascardogregkh
authored andcommitted
Bluetooth: cmtp: fix file refcount when cmtp_attach_device fails
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 <cascardo@canonical.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 635ac38 commit 1b364f8

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

‎net/bluetooth/cmtp/core.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
392392
if (!(session->flags & BIT(CMTP_LOOPBACK))) {
393393
err = cmtp_attach_device(session);
394394
if (err < 0) {
395+
/* Caller will call fput in case of failure, and so
396+
* will cmtp_session kthread.
397+
*/
398+
get_file(session->sock->file);
399+
395400
atomic_inc(&session->terminate);
396401
wake_up_interruptible(sk_sleep(session->sock->sk));
397402
up_write(&cmtp_session_sem);

0 commit comments

Comments
 (0)