Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2946353pxb; Sat, 26 Mar 2022 07:44:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1Amek6CZ+xVVtImt9DQrbOnVk7Kg3fmKw2djhFnMAkV7hDd9gHg5ktXI1GTIp7qDDr/VK X-Received: by 2002:a17:906:3a04:b0:6d0:8d78:2758 with SMTP id z4-20020a1709063a0400b006d08d782758mr17599526eje.685.1648305864924; Sat, 26 Mar 2022 07:44:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648305864; cv=none; d=google.com; s=arc-20160816; b=bkUH48hr+28hq4qSVsCxGEJebAeeXwFkcbcRrW5+SoGhxZHC5MADUsjl+2agVvgLYM VL6hYBMA39l3RxioJ9plnT8daf7timipJNE00PEXsoBtfb3jHsfbnpgqZ9wUSthK+Jr1 zdEVMbJQb8LMIq5FOY9d/0/3zILGx+1MxXuOWAXRUhCAcNCFQuvJ9cw9hS+8Jm1sRs6h ytzLQQhHXLF2bRFj/5coiL2KqEpdAY1Z8vFKBB6KL+62hmxQkfCqANH9aErDAPx6YX2P cUfZvgJwHuI8I7Ww5hDmdDlV7U+/9z0EpvG5GQI3FlKmYF4qK2ssV8mYIsRUW2ErRkAk HE2g== 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:dkim-signature; bh=aY7Qm3duZzz4/ANMI52LqTxkdMO5YlOW9+CWZKbNUDQ=; b=z6gQkO7D/dlKCpdnFnbE4yWitL6iNzho4+8fLI2MyyWwB+HAL1PnTBBsmmVmnSjZ0w s+LcANngV/0CWL7MD2/KlrCx98AlPK4XE3T5mivK7IWrOLmzkzexd9efnLbfhCEtIt/q 5agYrRGpY2aQHpT0Say4UVybJyoiOvAHUiCaNxx+XipqkIVgm+1yXYjZunlfuN2eRfHj eco987G2jvzFGQjazgLn9/mVcm6twT2LF/JfACnGqPp1Ktgml/Uk2Y0MNwhLy3BDgLs4 3GHhnNxGEgfi6cnLrf8lbX9nNxOxMUhOZcnU3kZAUJlPpgF51yj/u5MTXHHv3NKR9GvL nf5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=I05Mab03; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e2-20020a17090618e200b006df7cdba9e4si6101255ejf.385.2022.03.26.07.43.57; Sat, 26 Mar 2022 07:44:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=I05Mab03; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229882AbiCZHLM (ORCPT + 99 others); Sat, 26 Mar 2022 03:11:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230024AbiCZHLK (ORCPT ); Sat, 26 Mar 2022 03:11:10 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 167FA26D124 for ; Sat, 26 Mar 2022 00:09:35 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id bc27so8191591pgb.4 for ; Sat, 26 Mar 2022 00:09:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aY7Qm3duZzz4/ANMI52LqTxkdMO5YlOW9+CWZKbNUDQ=; b=I05Mab032aTpoTeimJ2ZOfF1xo8R9P0uXq5lBgIRm+2ywDCJ7TRBiojiTa2Sl4OyrW ENUppUa56EY/52pmYjwVkvLFtLbIrXRiqjns6jQiDFovrx+lkvG4JwIweL0YU8mdQjko 42818OZdanjVU+H50AQcXJKz2sx+o0LKLoabM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aY7Qm3duZzz4/ANMI52LqTxkdMO5YlOW9+CWZKbNUDQ=; b=2YlNK0LCsxwU5lB/EWiq0ZRG7F88GgCkchK45VN3Fr6cl8QVXzWMEH9SwSTgwQSVGb r5KYYVCh5rcmOr63MgchwHH5H9EewiH2o6lMx9WTTgfv2VrO9SZ6VddxYSbTDlvI6pqA U7x8OQF16GpJaabxVNjieswIVYGR1cHEfaumfBpOAKU95P4cSzj3Iw2lq4k+ry9y6JHo UU1EtFCfgJ5XkyXEwnjh9QPjroJ4PUC0jgSnDLk2BqGdXtRf0A7Z8z9WdaZ1HQUOgLDb e2XUXqvmfFhc56a5bgxmj8aUpUKlH9ykTDy8EOoCPkwzKwh9proxnMG50SSo3sxwUiXP hFwA== X-Gm-Message-State: AOAM533madUaGSM1Mphu3k6WV6BSXq3wXOznxMzWg7/aAVIgkcGQZn7o OoGqrTrHZ2HT6UPth1biFC/kjg== X-Received: by 2002:a05:6a00:815:b0:4fb:e46:511c with SMTP id m21-20020a056a00081500b004fb0e46511cmr7185978pfk.54.1648278574592; Sat, 26 Mar 2022 00:09:34 -0700 (PDT) Received: from localhost (0.223.81.34.bc.googleusercontent.com. [34.81.223.0]) by smtp.gmail.com with UTF8SMTPSA id j6-20020a17090a588600b001c699d77503sm7565679pji.2.2022.03.26.00.09.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 26 Mar 2022 00:09:33 -0700 (PDT) From: Ying Hsu To: marcel@holtmann.org Cc: chromeos-bluetooth-upstreaming@chromium.org, Ying Hsu , syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com, Joseph Hwang , "David S. Miller" , Desmond Cheong Zhi Xi , Jakub Kicinski , Johan Hedberg , Luiz Augusto von Dentz , Paolo Abeni , linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2] Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout Date: Sat, 26 Mar 2022 07:09:28 +0000 Message-Id: <20220326070853.v2.1.I67f8ad854ac2f48701902bfb34d6e2070011b779@changeid> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Connecting the same socket twice consecutively in sco_sock_connect() could lead to a race condition where two sco_conn objects are created but only one is associated with the socket. If the socket is closed before the SCO connection is established, the timer associated with the dangling sco_conn object won't be canceled. As the sock object is being freed, the use-after-free problem happens when the timer callback function sco_sock_timeout() accesses the socket. Here's the call trace: dump_stack+0x107/0x163 ? refcount_inc+0x1c/ print_address_description.constprop.0+0x1c/0x47e ? refcount_inc+0x1c/0x7b kasan_report+0x13a/0x173 ? refcount_inc+0x1c/0x7b check_memory_region+0x132/0x139 refcount_inc+0x1c/0x7b sco_sock_timeout+0xb2/0x1ba process_one_work+0x739/0xbd1 ? cancel_delayed_work+0x13f/0x13f ? __raw_spin_lock_init+0xf0/0xf0 ? to_kthread+0x59/0x85 worker_thread+0x593/0x70e kthread+0x346/0x35a ? drain_workqueue+0x31a/0x31a ? kthread_bind+0x4b/0x4b ret_from_fork+0x1f/0x30 Link: https://syzkaller.appspot.com/bug?extid=2bef95d3ab4daa10155b Reported-by: syzbot+2bef95d3ab4daa10155b@syzkaller.appspotmail.com Fixes: e1dee2c1de2b ("Bluetooth: fix repeated calls to sco_sock_kill") Signed-off-by: Ying Hsu Reviewed-by: Joseph Hwang --- Tested this commit using a C reproducer on qemu-x86_64 for 8 hours. Changes in v2: - Adding Link, Reported-by, and Fixes tags in comment. net/bluetooth/sco.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index 8eabf41b2993..380c63194736 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -574,19 +574,24 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen addr->sa_family != AF_BLUETOOTH) return -EINVAL; - if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) - return -EBADFD; + lock_sock(sk); + if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { + err = -EBADFD; + goto done; + } - if (sk->sk_type != SOCK_SEQPACKET) - return -EINVAL; + if (sk->sk_type != SOCK_SEQPACKET) { + err = -EINVAL; + goto done; + } hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR); - if (!hdev) - return -EHOSTUNREACH; + if (!hdev) { + err = -EHOSTUNREACH; + goto done; + } hci_dev_lock(hdev); - lock_sock(sk); - /* Set destination address and psm */ bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr); -- 2.35.1.1021.g381101b075-goog