Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp54471pxh; Thu, 7 Apr 2022 13:47:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysGXzaN8ETmhV0xwc4axwkXm1hbU98mOUEpKed6ObHJZTdBixLeQSAyssdqB9P6ufFlQkq X-Received: by 2002:a17:90a:b108:b0:1ca:c272:1a5f with SMTP id z8-20020a17090ab10800b001cac2721a5fmr17733300pjq.49.1649364440692; Thu, 07 Apr 2022 13:47:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649364440; cv=none; d=google.com; s=arc-20160816; b=bowd1jyT2BmDfQxyEOybDLzMZtuK/BAoxX22RvNo2CkRlqcvBe1GiqolYq2Y+Zs/zJ rZarPJ8CqU15MqbUlhNeSm3v6peQhMtmaIpZXs/uhGTNHOfwU3GHegr1jnDTEkkXuDcj IGLmIo1+oT7zJEbl15l+E99j5hkr1JxOW4L2sPGgHEOrw4P0s6P1XQ8ieCWFGAYt0D+m eO6pvk/sLkF+8+AJCfgYn0X2Ht8cpjbgAb35TTXCCqw7lcrOrXf7X0utFpiVzb87dD7g KiptR2oWvO9OYigqzUijpXuXiNa75jo+lfDBMXpNdqE8YUjuOUwiHAroDncFkaVNwLaD 0Gkg== 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=N786B+Z+5vlVwsQOnoiYjc/IkPQynqYX06I75P9c9ao=; b=MFKIP90pjxw7QoP7J5M3HPtF/K2qO1ZuHwTvvRktw90L32+wT8DebZuKQWkEugUugr D5q9uHckAw5zYqWuDg0E0ZstdT1wnt/GFQTPB5OqHuTVywDLsyRiTnknVbcZYNWKuUk8 Dqn636Io4I6PzT6CHED4+CvrKcAbis+I3eCZm6CdES+xYWkroGpptua4JRLS1T4Ba8Gp oLsFB7STwggOnBi/IysqB4vrJm7mBp/FjCqLEiR0v1oYZ/fxVPr4lA9aKVRGHDlKt09f EfhAb6HdXsPgyYoZdtfBTpxek+NrrKh19BrBBjQGwmtrvkhmbE5QGWuY23AjfDTDv2ZH TF1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RGKUDjdn; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id a38-20020a056a001d2600b004fa7782f609si11725907pfx.24.2022.04.07.13.47.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 13:47:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RGKUDjdn; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B38353D048C; Thu, 7 Apr 2022 12:57:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243897AbiDGSJO (ORCPT + 99 others); Thu, 7 Apr 2022 14:09:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244273AbiDGSJL (ORCPT ); Thu, 7 Apr 2022 14:09:11 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFE711FE24E for ; Thu, 7 Apr 2022 11:07:10 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id bq8so12507751ejb.10 for ; Thu, 07 Apr 2022 11:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=N786B+Z+5vlVwsQOnoiYjc/IkPQynqYX06I75P9c9ao=; b=RGKUDjdn1BLQZbXwmMfR4XiZnQ7FCC8wCW1zN/WxOMcYH54Kt3WA4D3QTyfwtgxBWs dqEcT1ED0iJXyvL5kUmpXWf7/5yqFygeHeqPxvy85kSOLmjQuC4xMFFJhjIAsm1uA2g7 57L0YcuNITiNg90eNkgXMHJN850+ZQ+0iDZ7ZEbCgXBywZ5VGSZhvJJEC2imgIWqHaY0 fy0HaTic1jSvEV1cpry7cNJ8O1eQUuJa3o3ZymscrL4ccIDxhJcKDqVMXHIdVUu4ENdy mtSBHq4rx9zajFEygovYgmHUXRTB7xSTRC2T4kOPM2rQ1StBrnZc/anapt3zihneUa1v hqxw== 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=N786B+Z+5vlVwsQOnoiYjc/IkPQynqYX06I75P9c9ao=; b=j85ua0htdZle9z96UEtXoTAK9E7lvUzNIK2zcCnbIfStGh1A0row0BUI838ZObnhOU 7ja6FBhFpQi04vSyQqngBPXZL/NFJz0SnUS3oIhtXra5KJsHPJS9EzNzYL9UlPM+u2AL duMUFIua7KOE3hqTIQHS5bCYFlBUtBHQuD9MAopZZX7Y7bCefExLd6a3Ap2kHOC6hMWj DX8agIasvmyTv34mfC7wv1Gk0sUqnsbH8K3Rq6wa3V9YZRvbe8kbNNn9v3PSc1WwvYqh PpXBT4r6E+ybTs413s75EijfpNXjwRVF1Si6I/s/19mkogVAj90aqO7kVwni8XXxZMem fRaA== X-Gm-Message-State: AOAM533KGcLLl/Q40YmM1tsfIqzT1LVA/T5jxCE4RNcHS91Tk2TEX1Me J409DHELxWcActik+Ss3yMNih7KueZw= X-Received: by 2002:a17:906:3a4f:b0:6cf:86e0:586c with SMTP id a15-20020a1709063a4f00b006cf86e0586cmr14523732ejf.626.1649354828848; Thu, 07 Apr 2022 11:07:08 -0700 (PDT) Received: from nlaptop.localdomain (ptr-dtfv0poj8u7zblqwbt6.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:eef0:f2b6:6987:9238:41ca]) by smtp.gmail.com with ESMTPSA id za11-20020a170906878b00b006e7f5d17d4dsm4495599ejb.140.2022.04.07.11.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 11:07:08 -0700 (PDT) From: Niels Dossche To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , Yun-Hao Chung , Niels Dossche Subject: [PATCH] Bluetooth: use hdev lock in activate_scan for hci_is_adv_monitoring Date: Thu, 7 Apr 2022 20:06:52 +0200 Message-Id: <20220407180651.14871-1-dossche.niels@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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-bluetooth@vger.kernel.org hci_is_adv_monitoring's function documentation states that it must be called under the hdev lock. Paths that leads to an unlocked call are: discov_update => start_discovery => interleaved_discov => active_scan and: discov_update => start_discovery => active_scan The solution is to take the lock in active_scan during the duration of the call to hci_is_adv_monitoring. Fixes: c32d624640fd ("Bluetooth: disable filter dup when scan for adv monitor") Signed-off-by: Niels Dossche --- Note: I am currently working on a static analyser to detect missing locks using type-based static analysis as my master's thesis in order to obtain my master's degree. If you would like to have more details, please let me know. This was a reported case. I manually verified the report by looking at the code, so that I do not send wrong information or patches. After concluding that this seems to be a true positive, I created this patch. I have only managed to compile-test this patch on x86_64. The effect on a running system could be a potential race condition in exceptional cases. This issue was found on Linux v5.17.1. net/bluetooth/hci_request.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c index 42c8047a9897..f4afe482e300 100644 --- a/net/bluetooth/hci_request.c +++ b/net/bluetooth/hci_request.c @@ -2260,6 +2260,7 @@ static int active_scan(struct hci_request *req, unsigned long opt) if (err < 0) own_addr_type = ADDR_LE_DEV_PUBLIC; + hci_dev_lock(hdev); if (hci_is_adv_monitoring(hdev)) { /* Duplicate filter should be disabled when some advertisement * monitor is activated, otherwise AdvMon can only receive one @@ -2276,6 +2277,7 @@ static int active_scan(struct hci_request *req, unsigned long opt) */ filter_dup = LE_SCAN_FILTER_DUP_DISABLE; } + hci_dev_unlock(hdev); hci_req_start_scan(req, LE_SCAN_ACTIVE, interval, hdev->le_scan_window_discovery, own_addr_type, -- 2.35.1