From patchwork Thu Jul 8 06:23:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A394C07E99 for ; Thu, 8 Jul 2021 06:23:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1D9F2619A2 for ; Thu, 8 Jul 2021 06:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229780AbhGHG0K (ORCPT ); Thu, 8 Jul 2021 02:26:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbhGHG0K (ORCPT ); Thu, 8 Jul 2021 02:26:10 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A8D9C061574 for ; Wed, 7 Jul 2021 23:23:28 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id a6-20020a37b1060000b02903b488f9d348so3196250qkf.20 for ; Wed, 07 Jul 2021 23:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=4YlKuUBAVCzUya4qwzfjFYw41CES3ee8lmcRt7kpXbI=; b=YjpgoJ++8h/jrhemV9NdBxNEswKo0ShewItkHTOHerfRpB4zqK5pZmUaa1m/j4vVY9 +CnakWFM4MsxevmVOZDL4PUl7ugBS02g3KtIBml08PsU8Dvlf+amAN2JyPDQCq1mHVzG g3aJtF7CzYsn0dzHqmu9PWpnK0et60TJb2EZPZJo0PzdgfaLv9SDXTxJB1ozxJ/cdcMd PR+PdP7CU5sMK3RnUPbmFfUfFrH40kaBmKEv+4D0VUrdOQ9ViWr0Ck1R4nn0Ww4KdeF/ DoBlCOZOMcfQFfZPh/aQ8JhDRpE0DHt4R2hdYCewGrddsrA9Uk9XKF8WQnZV0S8knWrK aqFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4YlKuUBAVCzUya4qwzfjFYw41CES3ee8lmcRt7kpXbI=; b=Doh7CNYtRJBbZrBl0a+H3RGBf0ayEZrqf/qtKsbqAXslu35bVSu5zZ8NXZ22glfNu/ 6sln9z/gidHrAKQgqSEIlSG271FlDh5QpAo9DZtLAyfXB6XMwXoS5kJMN49XAWLvER2M VCgAx3Z+4vaIcj8M4Qa/lqkspU7+RBcq88KUNkxt5HB5lzNOj4NGzbowjOnzFb1X/Rmy G65EcNdul+iiuMmn+dJBAQGKaPiCk12khJ5JkINYnzr+4poUZygTJTFpZzTZz94wrT+5 efWNj+DkQIonvvJt20fl3y8H8qLDt86TbDh3hve1kMJtrYoUkwgoldHMugAQPFKhWfVi 4Tmg== X-Gm-Message-State: AOAM5301EPoomY4XH0ZenY8Rz3mczPYgiOuiOD4NSvcBOgZxnPA8ny8Y ZxgHdFdGLWbYVGn3+3km7dUeTrUbn2I1oB1DfqLJlj6F07yd4SGfVTFYOra81xe0yuhG7nMUoVt C3OXBRbDY4eWOeNYOLdXmrFL64NrURaejEZwLxN8lw7DZ6pg6IRZZYIHIKWVblNX2bAGlHLp1QF p8HZyUDS8pNW4= X-Google-Smtp-Source: ABdhPJzm8od7VacDhYINIXkVLRvPU+xAmRsb1Fo2MqJTqgrUoifTaJPxtV+cyQ4TNCuZ9VB8s8AqfY35PEEImWeFSA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a05:6214:15d0:: with SMTP id p16mr28088061qvz.21.1625725407489; Wed, 07 Jul 2021 23:23:27 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:01 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.1.I69278fab3bf86adb578c5cba0a39e5bcf7f9581e@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 01/14] lib: add hash functions for bt_uuid_t From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Howard Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds function GHashFunc and GEqualFunc for bt_uuid_t. With these functions, we can add uuids into a GHashTable with bt_uuid_t format. Reviewed-by: Miao-chen Chou --- lib/uuid.c | 27 +++++++++++++++++++++++++++ lib/uuid.h | 3 +++ 2 files changed, 30 insertions(+) diff --git a/lib/uuid.c b/lib/uuid.c index 3d97dc8359c7..a09f5c428b87 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "lib/bluetooth.h" #include "uuid.h" @@ -120,6 +121,32 @@ int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2) return bt_uuid128_cmp(&u1, &u2); } +guint bt_uuid_hash(gconstpointer key) +{ + const bt_uuid_t *uuid = key; + bt_uuid_t uuid_128; + uint64_t *val; + + if (!uuid) + return 0; + + bt_uuid_to_uuid128(uuid, &uuid_128); + val = (uint64_t *)&uuid_128.value.u128; + + return g_int64_hash(val) ^ g_int64_hash(val+1); +} + +gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2) +{ + const bt_uuid_t *uuid1 = v1; + const bt_uuid_t *uuid2 = v2; + + if (!uuid1 || !uuid2) + return !uuid1 && !uuid2; + + return bt_uuid_cmp(uuid1, uuid2) == 0; +} + /* * convert the UUID to string, copying a maximum of n characters. */ diff --git a/lib/uuid.h b/lib/uuid.h index 1a4029b68730..e47ccccb9fd2 100644 --- a/lib/uuid.h +++ b/lib/uuid.h @@ -17,6 +17,7 @@ extern "C" { #endif #include +#include #define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805f9b34fb" @@ -167,6 +168,8 @@ int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value); int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2); void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst); +guint bt_uuid_hash(gconstpointer key); +gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2); #define MAX_LEN_UUID_STR 37 From patchwork Thu Jul 8 06:23:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C4DCC07E96 for ; Thu, 8 Jul 2021 06:23:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E15B61C59 for ; Thu, 8 Jul 2021 06:23:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229821AbhGHG0N (ORCPT ); Thu, 8 Jul 2021 02:26:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbhGHG0M (ORCPT ); Thu, 8 Jul 2021 02:26:12 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4D57C061574 for ; Wed, 7 Jul 2021 23:23:31 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id x15-20020a170902e04fb02900f5295925dbso1536565plx.9 for ; Wed, 07 Jul 2021 23:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XQqRS0lanOdd0QqGLCcCybEElU8zxVKVl7sin01pi1s=; b=K7mZbhVjktcjxMZsvZ+ipOzarx/4gbsXUHzqq6dtzYXq//9J4gINY04Kg9BmR3mC5e be7vbt4tajHu66ZDDvD9lYtezvD3ZeCTBBFKaHuA9K+o/CpTH8IcrnC90RjGaBgc1QAO pM7fmwk7py7x5/EWUPKToLPSnRtyOO9BZV9MlEmsvAQ9EKvsZs8vs8pheGa4Xh/gkrxK d5oOblr2rzv6hrrhwBftgOXJ2/EnPzQ+qcVD8RXiuLJutOg0tZlvlQfXecuqkoqf5CRm qRpB7UELCpXWWeqD+RKFyKkHXeBchniLj6CF9nY26uFULIGQX8iL6QpynGPaqyTL3L0z yaiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XQqRS0lanOdd0QqGLCcCybEElU8zxVKVl7sin01pi1s=; b=OIvzXSMfkMvASaA3I1mU9KyLOHdArnU4oZSCPFVOswSeEP8iJTIGd/Met4yoXPg8lH 44RAVu1EMx/X5odNv7ZJC6T+eEoSjx7DCUysXoLzuGiV/lf+z+aOEYjZnXYbN0WKiF4c lmzT0E5eTTqq5Eo3HEQyvx6q43eHyj9jUZik4Cc1u+BiCBHO8fU7b0WEuHnJj3iCfOMI FyarvaN/WOmObFnDQ72k3pX3v9598OEP1sDvBwNiHeLHL0eFKXrO2x3b9I4nH91jtQbw miVaMroQAyb4vRXAA2w7/Gv+shd4E9jk++f7znG531kUusZ2HG0Hnyr0UZl7oH/AZ5MP UqVg== X-Gm-Message-State: AOAM532L1XBJnLVvOdx5vJQBdI85Ge8Z1yYinY5E5GWdx02fF63Ir7yj gYTKh6j2dS75D2I5CvNP7eBiOSnVnwNQZCvGffSWxh5w96G3PJMTDXHlwQ3yzPO9g4HASewfU+K 2cPrihX0bt1xxJoUzXlPBuk0RjJFPW5i6ELitlZgJF1lgGAQAQOZzJEpmmSQbR06Yp+7w3xThqv uLehhLkPMe+HA= X-Google-Smtp-Source: ABdhPJxFluaBXKGF/ZuAnnWNBLavirHqbdd1Qt4alFOqlUiFA1wQTaAOkgUS01CudWsCJaB7dCOCfUBSfLo4rhehEg== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a05:6a00:2293:b029:309:e87c:1f4a with SMTP id f19-20020a056a002293b0290309e87c1f4amr28753920pfe.36.1625725411084; Wed, 07 Jul 2021 23:23:31 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:02 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.2.Ifd6e18068b54a0c9c8f8422ff502e46167d8b348@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 02/14] unit: add uuid unit tests From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds uuid tests of bt_uuid_hash and bt_uuid_equal to unit/test-uuid.c Reviewed-by: Miao-chen Chou --- unit/test-uuid.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/unit/test-uuid.c b/unit/test-uuid.c index 0889630cfb34..ac613c5c2951 100644 --- a/unit/test-uuid.c +++ b/unit/test-uuid.c @@ -169,6 +169,34 @@ static void test_cmp(gconstpointer data) tester_test_passed(); } +static void test_hash(gconstpointer data) +{ + const struct uuid_test_data *test_data = data; + bt_uuid_t uuid1, uuid2; + guint uuid_h1, uuid_h2; + + g_assert(bt_string_to_uuid(&uuid1, test_data->str) == 0); + g_assert(bt_string_to_uuid(&uuid2, test_data->str128) == 0); + + uuid_h1 = bt_uuid_hash(&uuid1); + uuid_h2 = bt_uuid_hash(&uuid2); + + g_assert(uuid_h1 == uuid_h2); + tester_test_passed(); +} + +static void test_equal(gconstpointer data) +{ + const struct uuid_test_data *test_data = data; + bt_uuid_t uuid1, uuid2; + + g_assert(bt_string_to_uuid(&uuid1, test_data->str) == 0); + g_assert(bt_string_to_uuid(&uuid2, test_data->str128) == 0); + + g_assert(bt_uuid_equal(&uuid1, &uuid2) == 1); + tester_test_passed(); +} + static const struct uuid_test_data compress[] = { { .str = "00001234-0000-1000-8000-00805f9b34fb", @@ -226,26 +254,46 @@ int main(int argc, char *argv[]) tester_add("/uuid/base", &uuid_base, NULL, test_uuid, NULL); tester_add("/uuid/base/str", &uuid_base, NULL, test_str, NULL); tester_add("/uuid/base/cmp", &uuid_base, NULL, test_cmp, NULL); + tester_add("/uuid/base/hash", &uuid_base, NULL, test_hash, NULL); + tester_add("/uuid/base/equal", &uuid_base, NULL, test_equal, NULL); tester_add("/uuid/sixteen1", &uuid_sixteen1, NULL, test_uuid, NULL); tester_add("/uuid/sixteen1/str", &uuid_sixteen1, NULL, test_str, NULL); tester_add("/uuid/sixteen1/cmp", &uuid_sixteen1, NULL, test_cmp, NULL); + tester_add("/uuid/sixteen1/hash", &uuid_sixteen1, NULL, test_hash, + NULL); + tester_add("/uuid/sixteen1/equal", &uuid_sixteen1, NULL, test_equal, + NULL); tester_add("/uuid/sixteen2", &uuid_sixteen2, NULL, test_uuid, NULL); tester_add("/uuid/sixteen2/str", &uuid_sixteen2, NULL, test_str, NULL); tester_add("/uuid/sixteen2/cmp", &uuid_sixteen2, NULL, test_cmp, NULL); + tester_add("/uuid/sixteen2/hash", &uuid_sixteen2, NULL, test_hash, + NULL); + tester_add("/uuid/sixteen2/equal", &uuid_sixteen2, NULL, test_equal, + NULL); tester_add("/uuid/thirtytwo1", &uuid_32_1, NULL, test_uuid, NULL); tester_add("/uuid/thirtytwo1/str", &uuid_32_1, NULL, test_str, NULL); tester_add("/uuid/thirtytwo1/cmp", &uuid_32_1, NULL, test_cmp, NULL); + tester_add("/uuid/thirtytwo1/hash", &uuid_32_1, NULL, test_hash, NULL); + tester_add("/uuid/thirtytwo1/equal", &uuid_32_1, NULL, test_equal, + NULL); tester_add("/uuid/thirtytwo2", &uuid_32_2, NULL, test_uuid, NULL); tester_add("/uuid/thritytwo2/str", &uuid_32_2, NULL, test_str, NULL); tester_add("/uuid/thirtytwo2/cmp", &uuid_32_2, NULL, test_cmp, NULL); + tester_add("/uuid/thirtytwo2/hash", &uuid_32_2, NULL, test_hash, NULL); + tester_add("/uuid/thirtytwo2/equal", &uuid_32_2, NULL, test_equal, + NULL); tester_add("/uuid/onetwentyeight", &uuid_128, NULL, test_uuid, NULL); tester_add("/uuid/onetwentyeight/str", &uuid_128, NULL, test_str, NULL); tester_add("/uuid/onetwentyeight/cmp", &uuid_128, NULL, test_cmp, NULL); + tester_add("/uuid/onetwentyeight/hash", &uuid_128, NULL, test_hash, + NULL); + tester_add("/uuid/onetwentyeight/equal", &uuid_128, NULL, test_equal, + NULL); for (i = 0; malformed[i]; i++) { char *testpath; From patchwork Thu Jul 8 06:23:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364635 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 839BFC07E96 for ; Thu, 8 Jul 2021 06:23:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6445A619A2 for ; Thu, 8 Jul 2021 06:23:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229841AbhGHG0S (ORCPT ); Thu, 8 Jul 2021 02:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229819AbhGHG0R (ORCPT ); Thu, 8 Jul 2021 02:26:17 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1E5EC061574 for ; Wed, 7 Jul 2021 23:23:35 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id f12-20020a0562141d2cb02902ba007211abso3379884qvd.12 for ; Wed, 07 Jul 2021 23:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zfKJA5L1xFQjALWiYpGAANN9lAZTGIYlAjpH2h7Vxbc=; b=aKiT3o1iuWMqxvE6XmvC16ssn1NAWPpkASCy2k1GxMsPyZou/bLZsSpzYB1eAopwux X+IxGCh+cRVnfu5a1Gbw1on9zBzyqeGGp7yQZLzk+LlpaFjZ6Oi4h1fr+2rbub9thZqY 6kfsAj5WJbsY8CzoplJyPpUTOd1azWxAkKsx/F7AgIx7Lz9niAfpAoz2ycXQibuVGsBz SioLcc8yz0CeJfxqVe4dRZMFaf4CVejziEZZ478XhPWMN31kcTYzxsPbfqViNwjLsHfO 4Mn2w4YHchFcOuft3BhAHWI1w56ocYVXPVJp5RyqwgTWNhhXpLIuGC8dhGf3n1k51hBT iNtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zfKJA5L1xFQjALWiYpGAANN9lAZTGIYlAjpH2h7Vxbc=; b=dBgxu28BoO8jpCtTFuNIrLj97yMh3rFlp4XncI8l/EeYapjfCrKwrc3SDAUpjTr/5N jYon65QcCBDX9oqc7gYexctU7uJs9E83FxVnSWTbotLnphA4jY4wAWulFXNO+ThFSz+z nUWSwx/kCxeVQt9188aTl3WU1TwEh+GL+YyHMySp8pj8NrTTrZtarxRs7KzPfV3LoOGJ t6k08sXxdShyFZECie5DLGoUE51zC5f2c2zCvMsb+04i1LIuTSm4D0ySUrlRTzJFYs1f RxKmQniQbCGpLWAfJAEyu2ArTW5ALI9omKfUPZHOGrlo0O9cXO3wmGgKv2M0qtkduy35 lcEg== X-Gm-Message-State: AOAM5300Ft0U4sWuIvt1rFpSXXLF9lJ84W+ISo3/4mzj6mQgB3z0n5lK wMHKfl/AFKxJ6kR8HmmndWisBhl9E1lOkrL5yZgGc/QmR5WqSuz72fQE/HR8oMJQdhIhXvvr/Yb CXTJjoZ1JOCj+p2qpge/XlmE/o2tLh/+n6yn6asAb1B4ZQEymUSpfDVpT+JpdkBqf3/s9gmQ4DC +68Uej2iyjzy8= X-Google-Smtp-Source: ABdhPJzryXixkxENEmchADhzoJFqdKvcUnQ/vsIebLN/e2QuKGsVwGGwxIr9gegCrQ5q7DndA6w6KLJqHxjL9mqfjA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a05:6214:1028:: with SMTP id k8mr28426239qvr.13.1625725414671; Wed, 07 Jul 2021 23:23:34 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:03 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.3.Ic71b1ed97538a06d02425ba502690bdab1c5d836@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 03/14] core: add is_allowed property in btd_service From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds is_allowed property in btd_service. When is_allowed is set to false, calling btd_service_connect and service_accept will fail and the existing service connection gets disconnected. Reviewed-by: Miao-chen Chou --- src/service.c | 33 +++++++++++++++++++++++++++++++++ src/service.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/service.c b/src/service.c index 21a52762e637..84fbb208a7e9 100644 --- a/src/service.c +++ b/src/service.c @@ -41,6 +41,7 @@ struct btd_service { void *user_data; btd_service_state_t state; int err; + bool is_allowed; }; struct service_state_callback { @@ -133,6 +134,7 @@ struct btd_service *service_create(struct btd_device *device, service->device = device; /* Weak ref */ service->profile = profile; service->state = BTD_SERVICE_STATE_UNAVAILABLE; + service->is_allowed = true; return service; } @@ -186,6 +188,12 @@ int service_accept(struct btd_service *service) if (!service->profile->accept) return -ENOSYS; + if (!service->is_allowed) { + info("service %s is not allowed", + service->profile->remote_uuid); + return -ECONNABORTED; + } + err = service->profile->accept(service); if (!err) goto done; @@ -245,6 +253,12 @@ int btd_service_connect(struct btd_service *service) return -EBUSY; } + if (!service->is_allowed) { + info("service %s is not allowed", + service->profile->remote_uuid); + return -ECONNABORTED; + } + err = profile->connect(service); if (err == 0) { change_state(service, BTD_SERVICE_STATE_CONNECTING, 0); @@ -361,6 +375,25 @@ bool btd_service_remove_state_cb(unsigned int id) return false; } +void btd_service_set_allowed(struct btd_service *service, bool allowed) +{ + if (allowed == service->is_allowed) + return; + + service->is_allowed = allowed; + + if (!allowed && (service->state == BTD_SERVICE_STATE_CONNECTING || + service->state == BTD_SERVICE_STATE_CONNECTED)) { + btd_service_disconnect(service); + return; + } +} + +bool btd_service_is_allowed(struct btd_service *service) +{ + return service->is_allowed; +} + void btd_service_connecting_complete(struct btd_service *service, int err) { if (service->state != BTD_SERVICE_STATE_DISCONNECTED && diff --git a/src/service.h b/src/service.h index 88530cc17d53..5a2a02447b24 100644 --- a/src/service.h +++ b/src/service.h @@ -51,6 +51,8 @@ int btd_service_get_error(const struct btd_service *service); unsigned int btd_service_add_state_cb(btd_service_state_cb cb, void *user_data); bool btd_service_remove_state_cb(unsigned int id); +void btd_service_set_allowed(struct btd_service *service, bool allowed); +bool btd_service_is_allowed(struct btd_service *service); /* Functions used by profile implementation */ void btd_service_connecting_complete(struct btd_service *service, int err); From patchwork Thu Jul 8 06:23:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364637 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFB46C07E96 for ; Thu, 8 Jul 2021 06:23:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AE2CD61C59 for ; Thu, 8 Jul 2021 06:23:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229819AbhGHG0U (ORCPT ); Thu, 8 Jul 2021 02:26:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbhGHG0U (ORCPT ); Thu, 8 Jul 2021 02:26:20 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4566C061574 for ; Wed, 7 Jul 2021 23:23:38 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id h7-20020a0ceda70000b02902af042354f1so3315714qvr.9 for ; Wed, 07 Jul 2021 23:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=6P+E9E5SrdjwSPVgTtZ9bRRLdrfkNgtqlsaUuV2ebuw=; b=ZA0/5Lv3Va8CCAr7cssEfRj4SefClAh1vzwpEj0dHFsorE/OYVptQ/rPbIN5nlC67i kXyLEWTSoxXW5UIhRy5c7lkiuZ/OcJhUpeAI6duVb5JAdUGZ5qJ1HUGt1WSo70nptPpZ HYwgP7Bmqeypt8QF/FgajBKXbNwu1OtbAp49U64tKzRvADnOy2GXsdWQf7hNUiqwxyeK tHmaRAd2CTm1kDDBxa+H7R0fR+zG0WgT4noDfKxDjfAWtbrYFg96cYXlovAPKAVC2Cae 2oY/zrVsFMzzNEKgso4nAdjKQhCEjS4YFr0E73r9bQG1gEzFWXIBEtx78WOmUAlu9Vj1 53hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=6P+E9E5SrdjwSPVgTtZ9bRRLdrfkNgtqlsaUuV2ebuw=; b=VyBtuKyD1IgnMAEsHBB8Vr/0MQpJPnmMLP8WX9nWKN7KhKXJ+dyIr9ChBOYIGOsgqE o+EhkMtYw8fE9Odu4N7yVZl3GAlHczB5K9pLBrRpi3fk7lXQOEnPJVvdjEEhdSXTSqdW 0LGWaaFHLKMEQocB25+bpz36CS4aEe4FBzjmd3WxrXfbEQwNxk/O2jUCSUA6An5Ldg+x XdhqOQmavN66zz4PyaSeMZzKikgboj6D4APp5xBK4Tm31qObJ0Wk5pM9SZSqFDtalOhv 4kWeGxepc7wO9LjgpyWGyv1elfhA8prikKNAe9SVXWP15EjWP/q6nEcB/Utn4CqZY/4v IWCA== X-Gm-Message-State: AOAM533XOzNUgP49OazKSAfV6wvOQAWRcsB0x92+GP4M3uZEBUpKuQwB qdfTSeevRzG3t+yE/k2kEpxaDPTKqQoMTZdbdHIrccHPtezgIkFWJFhEf+tKfHFmrP/Xfgs1/y1 NQwn6daAgOSNGBoATGXyVnzQUgcayb43bV2ETqnUQCbrEXWpfUhjlr7GepqhwSQalI3tRhXA1lD p/zrMB1vESuoM= X-Google-Smtp-Source: ABdhPJztHK6A2LChZvQqrP8oRRMQsdtz6czxqEh61yM+6Iw5001pI7rYy49VJF2zWV0dmdZ1Kr8zaaz3fAToDUgSNg== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a0c:a321:: with SMTP id u30mr20911139qvu.57.1625725418048; Wed, 07 Jul 2021 23:23:38 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:04 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.4.Ibc0b5f02cb249f9aca9efe45e2dadc5e50b7d89e@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 04/14] core: add adapter and device allowed_uuid functions From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This implements functions in src/adapter.c and src/device.c for plugins setting a list of allowed services. Reviewed-by: Miao-chen Chou --- src/adapter.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/adapter.h | 8 +++++ src/device.c | 59 ++++++++++++++++++++++++++++++++- src/device.h | 2 ++ 4 files changed, 158 insertions(+), 1 deletion(-) diff --git a/src/adapter.c b/src/adapter.c index 84bc5a1b09eb..f75b8acf1ffb 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -260,6 +260,8 @@ struct btd_adapter { struct btd_battery_provider_manager *battery_provider_manager; + GHashTable *allowed_uuid_set; /* Set of allowed service UUIDs */ + gboolean initialized; GSList *pin_callbacks; @@ -3480,6 +3482,87 @@ static DBusMessage *connect_device(DBusConnection *conn, return NULL; } +static void update_adapter_profile(struct btd_profile *profile, void *data) +{ + struct btd_adapter *adapter = data; + bool is_allowed; + + is_allowed = btd_adapter_is_uuid_allowed(adapter, profile->remote_uuid); + + if (is_allowed && !g_slist_find(adapter->profiles, profile)) { + adapter_add_profile(adapter, profile); + + info("service %s is allowed", profile->remote_uuid); + + } else if (!is_allowed && g_slist_find(adapter->profiles, profile)) { + adapter_remove_profile(adapter, profile); + + info("service %s is blocked", profile->remote_uuid); + } +} + +static void update_device_allowed_services(void *data, void *user_data) +{ + struct btd_device *device = data; + + btd_device_update_allowed_services(device); +} + +static void add_uuid_to_uuid_set(void *data, void *user_data) +{ + bt_uuid_t *uuid = data; + GHashTable *uuid_set = user_data; + + if (!uuid) { + error("Found NULL in UUID allowed list"); + return; + } + + g_hash_table_add(uuid_set, uuid); +} + +bool btd_adapter_set_allowed_uuids(struct btd_adapter *adapter, + struct queue *uuids) +{ + if (!adapter) + return false; + + if (adapter->allowed_uuid_set) + g_hash_table_destroy(adapter->allowed_uuid_set); + + adapter->allowed_uuid_set = g_hash_table_new(bt_uuid_hash, + bt_uuid_equal); + if (!adapter->allowed_uuid_set) { + btd_error(adapter->dev_id, + "Failed to allocate allowed_uuid_set"); + return false; + } + + queue_foreach(uuids, add_uuid_to_uuid_set, adapter->allowed_uuid_set); + btd_profile_foreach(update_adapter_profile, adapter); + g_slist_foreach(adapter->devices, update_device_allowed_services, NULL); + + return true; +} + +bool btd_adapter_is_uuid_allowed(struct btd_adapter *adapter, + const char *uuid_str) +{ + bt_uuid_t uuid; + + if (!adapter || !adapter->allowed_uuid_set) + return true; + + if (bt_string_to_uuid(&uuid, uuid_str)) { + btd_error(adapter->dev_id, + "Failed to parse UUID string '%s'", uuid_str); + return false; + } + + return !g_hash_table_size(adapter->allowed_uuid_set) || + g_hash_table_contains(adapter->allowed_uuid_set, &uuid); +} + static const GDBusMethodTable adapter_methods[] = { { GDBUS_ASYNC_METHOD("StartDiscovery", NULL, NULL, start_discovery) }, { GDBUS_METHOD("SetDiscoveryFilter", @@ -4691,6 +4774,12 @@ static void probe_profile(struct btd_profile *profile, void *data) if (profile->adapter_probe == NULL) return; + if (!btd_adapter_is_uuid_allowed(adapter, profile->remote_uuid)) { + info("service %s is not allowed to probe", + profile->remote_uuid); + return; + } + err = profile->adapter_probe(profile, adapter); if (err < 0) { btd_error(adapter->dev_id, "%s: %s (%d)", profile->name, @@ -5395,6 +5484,7 @@ static void adapter_free(gpointer user_data) g_free(adapter->stored_alias); g_free(adapter->current_alias); free(adapter->modalias); + g_hash_table_destroy(adapter->allowed_uuid_set); g_free(adapter); } diff --git a/src/adapter.h b/src/adapter.h index 60b5e3bcca34..7cac51451249 100644 --- a/src/adapter.h +++ b/src/adapter.h @@ -25,6 +25,7 @@ struct btd_adapter; struct btd_device; +struct queue; struct btd_adapter *btd_adapter_get_default(void); bool btd_adapter_is_default(struct btd_adapter *adapter); @@ -97,6 +98,8 @@ void adapter_service_remove(struct btd_adapter *adapter, uint32_t handle); struct agent *adapter_get_agent(struct btd_adapter *adapter); +bool btd_adapter_uuid_is_allowed(struct btd_adapter *adapter, const char *uuid); + struct btd_adapter *btd_adapter_ref(struct btd_adapter *adapter); void btd_adapter_unref(struct btd_adapter *adapter); @@ -240,3 +243,8 @@ enum kernel_features { }; bool btd_has_kernel_features(uint32_t feature); + +bool btd_adapter_set_allowed_uuids(struct btd_adapter *adapter, + struct queue *uuids); +bool btd_adapter_is_uuid_allowed(struct btd_adapter *adapter, + const char *uuid_str); diff --git a/src/device.c b/src/device.c index faf07ba22270..e1d82eab0988 100644 --- a/src/device.c +++ b/src/device.c @@ -1929,6 +1929,51 @@ static int service_prio_cmp(gconstpointer a, gconstpointer b) return p2->priority - p1->priority; } +bool btd_device_all_services_allowed(struct btd_device *dev) +{ + GSList *l; + struct btd_adapter *adapter = dev->adapter; + char *uuid; + + for (l = dev->uuids; l != NULL; l = g_slist_next(l)) { + uuid = l->data; + + if (!btd_adapter_is_uuid_allowed(adapter, uuid)) + return false; + } + + return true; +} + +void btd_device_update_allowed_services(struct btd_device *dev) +{ + struct btd_adapter *adapter = dev->adapter; + struct btd_service *service; + struct btd_profile *profile; + GSList *l; + bool is_allowed; + char addr[18]; + + /* If service discovery is ongoing, let the service discovery complete + * callback call this function. + */ + if (dev->browse) { + ba2str(&dev->bdaddr, addr); + DBG("service discovery of %s is ongoing. Skip updating allowed " + "services", addr); + return; + } + + for (l = dev->services; l != NULL; l = g_slist_next(l)) { + service = l->data; + profile = btd_service_get_profile(service); + + is_allowed = btd_adapter_is_uuid_allowed(adapter, + profile->remote_uuid); + btd_service_set_allowed(service, is_allowed); + } +} + static GSList *create_pending_list(struct btd_device *dev, const char *uuid) { struct btd_service *service; @@ -1937,9 +1982,14 @@ static GSList *create_pending_list(struct btd_device *dev, const char *uuid) if (uuid) { service = find_connectable_service(dev, uuid); - if (service) + + if (!service) + return dev->pending; + + if (btd_service_is_allowed(service)) return g_slist_prepend(dev->pending, service); + info("service %s is blocked", uuid); return dev->pending; } @@ -1950,6 +2000,11 @@ static GSList *create_pending_list(struct btd_device *dev, const char *uuid) if (!p->auto_connect) continue; + if (!btd_service_is_allowed(service)) { + info("service %s is blocked", p->remote_uuid); + continue; + } + if (g_slist_find(dev->pending, service)) continue; @@ -2633,6 +2688,8 @@ static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type, dev->svc_callbacks); g_free(cb); } + + btd_device_update_allowed_services(dev); } static struct bonding_req *bonding_request_new(DBusMessage *msg, diff --git a/src/device.h b/src/device.h index 4ae9abe0dbb4..5f615cb4b6b2 100644 --- a/src/device.h +++ b/src/device.h @@ -175,5 +175,7 @@ uint32_t btd_device_get_current_flags(struct btd_device *dev); void btd_device_flags_changed(struct btd_device *dev, uint32_t supported_flags, uint32_t current_flags); +bool btd_device_all_services_allowed(struct btd_device *dev); +void btd_device_update_allowed_services(struct btd_device *dev); void btd_device_init(void); void btd_device_cleanup(void); From patchwork Thu Jul 8 06:23:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364639 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D1CBC07E96 for ; Thu, 8 Jul 2021 06:23:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6369061C69 for ; Thu, 8 Jul 2021 06:23:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229851AbhGHG0Y (ORCPT ); Thu, 8 Jul 2021 02:26:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhGHG0X (ORCPT ); Thu, 8 Jul 2021 02:26:23 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D1D7C061760 for ; Wed, 7 Jul 2021 23:23:42 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d29-20020aa797bd0000b02903147a50038aso3125600pfq.15 for ; Wed, 07 Jul 2021 23:23:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=89CsUx6ZxjoLORyodXtlUrvys2/HZJi76DGq4cY+uvk=; b=QeOnthgZNHbA2UwGTN5d4Wzcp5z9TVZ7Qr9U9yOGw/xTIwagoEebEkHH8ya7NrNvdV /eLYRZU38GqPhTkTEEa8c8WDuKxRo2BR9yosTaaS49v5Tldp0co8bxNMGuXzM/w0u3Xv 9B60yLXCUk/mxJvF9xQTwk3cHbncRkmLcmvF2238hRwHtE7Zl2H8hmIY/hfsMGPQRmij yXx8AoaG4y6XlZkBPaUnaA/Eazl47LyrQL/Lfw+/YDQlGTkfAYvCFTWFVE/t6mUZotTR nbZ7lp4+nEfx9VGYj+HH4Evmd2mfNl+v8vpTPDsXVTYHxIuKOCN1fb74MF/v7Yh8KzO6 ZsPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=89CsUx6ZxjoLORyodXtlUrvys2/HZJi76DGq4cY+uvk=; b=GsnbmjUDA6fkU+XMCMxeUlrwBztNwk9lFhBjH795y5qLgjfJQLCvuMS6wZnWIidceQ sPCVelWuxcYgGBRC90QyRF+x/HiEQ8mko1yINSAUy2fbyQEEG7dkQh+MrGZvh1UB1Sp1 T5NI3/MQjMW9iMSeXtBvMr5c8tc4mipnAXsPY0GhuPAUF9NTFWeXbiolBSeTvF37l012 67m9rET86Ie5ACfQPXikvtOWX8EMaNlPxVZkgm6HVGY9t0hvOVDfxqdw3Esj1js3zY8G Ft+Lzn10hThylPRba7C5qoFQIH9ZNA63u2rjIrb0yIk4tmrZhTLGlvuKC6jLAL9O41Gd a5Yg== X-Gm-Message-State: AOAM530x6qauh0ctWUCaU0tBZMr4QERB+u0Zca2lAe7ojzu5xoWGTuh5 RREUIjLmOslZN4U7BSg9gp8n6IL1Ig81j1mg6nqcwEgTmuLPbRHtuHoV0mouNVhEq51xVWxZm2Z fZE+mbzbZZQ9KxIUk8vWyQpNG1pHYF2byqOIY8DfspeG6xAR2l2Xm5IA0tVeHfNQbvHXRTzkHOZ 1rQWCoMaaY9to= X-Google-Smtp-Source: ABdhPJxpokYe/KYcUMl+bHf7P/4sp0oJkpzMhNn8IcwknRMKQupFVOA7I0NxdQ2zuJLvWo32GBmmO5myfslE8WIWQQ== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a17:902:e843:b029:129:acb4:2464 with SMTP id t3-20020a170902e843b0290129acb42464mr10262571plg.77.1625725421797; Wed, 07 Jul 2021 23:23:41 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:05 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.5.I699a3837a5a18d9f889635300f6c717879f19dd2@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 05/14] core: add device state and state callbacks From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This implements functions to register/unregister state changed callback functions, the functions will be called when a device's state changed. Currently the state only shows initializing, available and removing, which is enough for plugins to register dbus objects upon device creation and unregister it upon device removal. Reviewed-by: Miao-chen Chou --- src/device.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/device.h | 13 +++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/device.c b/src/device.c index e1d82eab0988..0d7444706336 100644 --- a/src/device.c +++ b/src/device.c @@ -81,6 +81,13 @@ static DBusConnection *dbus_conn = NULL; static unsigned service_state_cb_id; +static GSList *device_state_callbacks; + +struct device_state_callback { + btd_device_state_cb cb; + void *user_data; + unsigned int id; +}; struct btd_disconnect_data { guint id; @@ -272,6 +279,8 @@ struct btd_device { GIOChannel *att_io; guint store_id; + + enum btd_device_state_t state; }; static const uint16_t uuid_list[] = { @@ -4095,6 +4104,23 @@ static void gatt_service_removed(struct gatt_db_attribute *attr, gatt_services_changed(device); } +static void device_change_state(struct btd_device *device, + enum btd_device_state_t new_state) +{ + GSList *l; + struct device_state_callback *cb_data; + + if (device->state == new_state) + return; + + for (l = device_state_callbacks; l != NULL; l = g_slist_next(l)) { + cb_data = l->data; + cb_data->cb(device, new_state, cb_data->user_data); + } + + device->state = new_state; +} + static struct btd_device *device_new(struct btd_adapter *adapter, const char *address) { @@ -4158,6 +4184,8 @@ static struct btd_device *device_new(struct btd_adapter *adapter, device->refresh_discovery = btd_opts.refresh_discovery; + device_change_state(device, BTD_DEVICE_STATE_AVAILABLE); + return btd_device_ref(device); } @@ -6839,6 +6867,7 @@ void btd_device_unref(struct btd_device *device) DBG("Freeing device %s", device->path); + device_change_state(device, BTD_DEVICE_STATE_REMOVING); g_dbus_unregister_interface(dbus_conn, device->path, DEVICE_INTERFACE); } @@ -6980,3 +7009,38 @@ void btd_device_cleanup(void) { btd_service_remove_state_cb(service_state_cb_id); } + +unsigned int btd_device_add_state_cb(btd_device_state_cb cb, void *user_data) +{ + struct device_state_callback *cb_data; + static unsigned int id; + + cb_data = g_new0(struct device_state_callback, 1); + cb_data->cb = cb; + cb_data->user_data = user_data; + cb_data->id = ++id; + + device_state_callbacks = g_slist_append(device_state_callbacks, + cb_data); + + return cb_data->id; +} + +bool btd_device_remove_state_cb(unsigned int id) +{ + GSList *l; + + for (l = device_state_callbacks; l != NULL; l = g_slist_next(l)) { + struct device_state_callback *cb_data = l->data; + + if (cb_data && cb_data->id == id) { + device_state_callbacks = g_slist_remove( + device_state_callbacks, + cb_data); + g_free(cb_data); + return true; + } + } + + return false; +} diff --git a/src/device.h b/src/device.h index 5f615cb4b6b2..a8424aa4f098 100644 --- a/src/device.h +++ b/src/device.h @@ -11,8 +11,18 @@ #define DEVICE_INTERFACE "org.bluez.Device1" +enum btd_device_state_t { + BTD_DEVICE_STATE_INITIALIZING, /* Device object is creating */ + BTD_DEVICE_STATE_AVAILABLE, /* Device object is registered */ + BTD_DEVICE_STATE_REMOVING, /* Device object is being removed */ +}; + struct btd_device; +typedef void (*btd_device_state_cb) (struct btd_device *device, + enum btd_device_state_t new_state, + void *user_data); + struct btd_device *device_create(struct btd_adapter *adapter, const bdaddr_t *address, uint8_t bdaddr_type); struct btd_device *device_create_from_storage(struct btd_adapter *adapter, @@ -179,3 +189,6 @@ bool btd_device_all_services_allowed(struct btd_device *dev); void btd_device_update_allowed_services(struct btd_device *dev); void btd_device_init(void); void btd_device_cleanup(void); + +unsigned int btd_device_add_state_cb(btd_device_state_cb cb, void *user_data); +bool btd_device_remove_state_cb(unsigned int id); From patchwork Thu Jul 8 06:23:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364641 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12208C07E96 for ; Thu, 8 Jul 2021 06:23:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E4FE8619A2 for ; Thu, 8 Jul 2021 06:23:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229845AbhGHG01 (ORCPT ); Thu, 8 Jul 2021 02:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhGHG01 (ORCPT ); Thu, 8 Jul 2021 02:26:27 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B8C4C061574 for ; Wed, 7 Jul 2021 23:23:46 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id y35-20020a0cb8a30000b0290270c2da88e8so3369976qvf.13 for ; Wed, 07 Jul 2021 23:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=aOqA8U5BHV+P1I3p5Zk/+AMkeiZJOsUk1qheCnJv3iU=; b=Ztz7RQ0zfm73o30rz29q3wGXZEcjU+jNzJ9LPUgN3iDDlflEm8AZaxuByBmZY2FOQ5 nDPVElfXKP0P+vukODyFYj2cycGu4MAoDMsqLNbTCnWdggsLkl3jnuhgWMDo/eCyR9Pk kjT1CG4RWk8OVbGUZdyx7+PRGEVXRsVmdw7UDij5ig70RJWlyXFNXbavo2kkh/uEpQ9W vJEJoIwzf0bxluP964LYNI3EfaOT1DGhohI0N/8fQRa/UpQg49+p6FvHX8mqZljuS+nj 10WJyXbw9NHR+E6sW3OPVSGtdn10fvZUGHplkC3CgoV+Zm2ySveYwyRXovB+nwkTxdaU HXUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=aOqA8U5BHV+P1I3p5Zk/+AMkeiZJOsUk1qheCnJv3iU=; b=jAuNWojqPG6CyvpnkTh987U+t25t0JV+PL01RrTD7HhIduLqh61xDwe4cd3ecqaMvi 4XMQwImInXdxTNhFY/Ysboak5oqH0KkGQqoE0/PgFDwyNB04AH+faT08ALz+DBy4vx2y 8v9WP+yl39xSXXroIhxzHE3MUJ5pEHHqp7zcOhvctO15EAykSmqiWAZI65M3zdgLtmDF BShHG3xZZCzF+mEaGJGD7eiRhCWCUXzs37gsOABUTwVjyF/KI17l4B928E/jcBkvAflS uVj5mhw2j/U3G3iiIjcAO6y0hMg6put9BkWdzih0ph3wd4oH1uv30PFzNOq8CHgJh9uy rJgQ== X-Gm-Message-State: AOAM530+pt3EIG/FCzZX1DoLvGgpdEpWMjxy9oGZr0/UllJx9SBVqoDT xChpHDUpcc19C7LWxIjzsQA1jLtQoRTX8R5R3ZvORtcw+gc0YkZry/5g+zFDQUiJolWIjeKcvhx M8807/Qoxv28szHLMS9kW/WTVrxGoG5KrOwDabTMnvzgs7POriqsQIrTFqhyxLuoQUqZk9VIQ+D MANsMV36kmh5Q= X-Google-Smtp-Source: ABdhPJz7FXIllfTDTavI0wln3o99V/+28Uun5VUJ4z1hnhWpkj6/SLe+GeyseZUbN1w6HsMXrXmUlFx3sPO/kvbiIA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:ad4:538c:: with SMTP id i12mr28407230qvv.51.1625725425227; Wed, 07 Jul 2021 23:23:45 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:06 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.6.I123189fa93aad2c2d1be645d38dbc494aa15563c@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 06/14] audio: Remove Media1 interface when a2dp source disallowed From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Howard Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org When A2DP source profile is removed from adapter, a2dp_server and everything inside the object will be removed, which also releases all MediaEndpoints and MediaPlayer. When A2DP source profile is re-added, although a2dp_server will be created, clients are not able to know they can register their endpoints and player by then. This patch handles this case by unregistering Media1 interface when we remove a2dp_server, and register it back when a2dp_source is allowed. Reviewed-by: Miao-chen Chou --- perform following steps 1. SetServiceAllowList to empty list 2. pair with an LE headset, then turn off the headset 3. SetServiceAllowList to "0x1234" 4. SetServiceAllowList to empty list 5. turn on the headset and check if it is reconnected. profiles/audio/a2dp.c | 2 ++ profiles/audio/avrcp.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/profiles/audio/a2dp.c b/profiles/audio/a2dp.c index d31ed845cbe7..26d4f365207e 100644 --- a/profiles/audio/a2dp.c +++ b/profiles/audio/a2dp.c @@ -3275,6 +3275,7 @@ static int a2dp_source_server_probe(struct btd_profile *p, { struct a2dp_server *server; + media_register(adapter); DBG("path %s", adapter_get_path(adapter)); server = find_server(servers, adapter); @@ -3315,6 +3316,7 @@ static void a2dp_source_server_remove(struct btd_profile *p, return; a2dp_server_unregister(server); + media_unregister(adapter); } static int a2dp_sink_server_probe(struct btd_profile *p, diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c index ccf34b2207a9..997a5be9a0f4 100644 --- a/profiles/audio/avrcp.c +++ b/profiles/audio/avrcp.c @@ -4751,6 +4751,8 @@ static void avrcp_controller_server_remove(struct btd_profile *p, if (server->tg_record_id == 0) avrcp_server_unregister(server); + + media_unregister(adapter); } static int avrcp_controller_server_probe(struct btd_profile *p, @@ -4761,6 +4763,7 @@ static int avrcp_controller_server_probe(struct btd_profile *p, DBG("path %s", adapter_get_path(adapter)); + media_register(adapter); server = find_server(servers, adapter); if (server != NULL) goto done; From patchwork Thu Jul 8 06:23:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364643 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6AEA6C07E96 for ; Thu, 8 Jul 2021 06:23:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4D26B61C59 for ; Thu, 8 Jul 2021 06:23:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbhGHG0b (ORCPT ); Thu, 8 Jul 2021 02:26:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbhGHG0a (ORCPT ); Thu, 8 Jul 2021 02:26:30 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87E93C061574 for ; Wed, 7 Jul 2021 23:23:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id k11-20020a056902070bb029055a2303fc2dso5718993ybt.11 for ; Wed, 07 Jul 2021 23:23:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=w16n9MQt4OHipTQ2Rw2GOOnH2k4YMLK6G//sUcAoKMU=; b=kkvXW4I3g8f/D/1Xb5QHu8Ahzq2iKwNVLywHzVslnd3xC+Oe7quG6JZsmic7Kj9CYd iEIZ3mHudshC6LH6vW1eJUoEm2XnIrQupZD6jjrpmkdAcb078Nf0WTPOtqjb6nXKpDgr 0BMJprbrOMa3Q0YEBF1+IGyQF3vdaoi34Ddnjm1VfoWC7V7qHpbaeJcjQNvH+ZM4+CKs DwE+6+jjD0FKap8xPT6c4AYf38O0N1TasFVWzIRyfCd9WRdouaB5myZpfK/CSHjbCu1S 26Jm7JKvjIUM4aM9Q3PUFIzVRmHrW8TEWouRNacCbmo39rJMjo0EVcx1r0rYw9E8yaik n8RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=w16n9MQt4OHipTQ2Rw2GOOnH2k4YMLK6G//sUcAoKMU=; b=kavhH6wbOizZ/dZ7p9Pu/SvGL1nbB8QcVZ6isfDZqKlZTR/34L6b7gSak6vqwc/EhF yjcwuEbYs0x6Sq1478EEMicRkpBjcd16Sv1swOl1HkGcoUSShUdyu/VjvXggor8f6nVa jXMma5g0vZ0FspvdEkKJGEnmMjx4McDcROqON6z60cedTpe2cmGb005HIumPTFxB6J0H 3xSDUq5aZ7RyuGSf+EDEkw+2lbrozsfcVTIVgztIO3Nrdxt5H8AZ30SIv4WSuyK2Niju Q7ulueeyIgmsuQv6c8Su2Sed9z6T5bVzWSpYLD+UppS7/SwghTmc97jw5PZ3xwGLPOV6 hZvw== X-Gm-Message-State: AOAM533z2g5acNQdMvdxd+K5qIDMuEc3o7Xve43jhse0BnfEJWwE1UkD LhpeuLTE3HP+VplJgpaFLJBHx0s4YXbNXONrpsurPRpK9KYPbcn08AESZ0JMH8Mvao7KKpIrsZg jLM35X9gJAWgzbTMZ0z6iXcVNcWyS5683ZhXzofjyCpAapE0F/MoLdikb9qX/MiIe1dGIhL7DrH OH38+CoDy6Czs= X-Google-Smtp-Source: ABdhPJzhW2Avk6PlIWoxSGo70MvRtRK82AVZ7uzJ6yQhOq5oVINIgKixdkuANBV/VLmFCWt0rclvW8n2p93HCFpVNA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a25:3486:: with SMTP id b128mr40693149yba.523.1625725428653; Wed, 07 Jul 2021 23:23:48 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:07 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.7.Id0842634d98a21fbdfa5cc72c76a462a98bf6f40@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 07/14] plugins: add a new plugin for admin_policy From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou , Shyh-In Hwang Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds an initial code for a new plugin admin_policy. Reviewed-by: Miao-chen Chou Reviewed-by: Shyh-In Hwang --- Makefile.plugins | 5 +++++ bootstrap-configure | 1 + configure.ac | 4 ++++ plugins/admin_policy.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 plugins/admin_policy.c diff --git a/Makefile.plugins b/Makefile.plugins index 4e6a72b0bdf6..b6be0e0d559d 100644 --- a/Makefile.plugins +++ b/Makefile.plugins @@ -11,6 +11,11 @@ builtin_sources += plugins/autopair.c builtin_modules += policy builtin_sources += plugins/policy.c +if ADMIN_POLICY +builtin_modules += admin_policy +builtin_sources += plugins/admin_policy.c +endif + if NFC builtin_modules += neard builtin_sources += plugins/neard.c diff --git a/bootstrap-configure b/bootstrap-configure index 0efd83abc2c4..89c0747b0256 100755 --- a/bootstrap-configure +++ b/bootstrap-configure @@ -30,4 +30,5 @@ fi --enable-pie \ --enable-cups \ --enable-library \ + --enable-admin_policy \ --disable-datafiles $* diff --git a/configure.ac b/configure.ac index be32782a641d..53ed8911f95c 100644 --- a/configure.ac +++ b/configure.ac @@ -364,6 +364,10 @@ AC_ARG_ENABLE(logger, AC_HELP_STRING([--enable-logger], [enable HCI logger service]), [enable_logger=${enableval}]) AM_CONDITIONAL(LOGGER, test "${enable_logger}" = "yes") +AC_ARG_ENABLE(admin_policy, AC_HELP_STRING([--enable-admin_policy], + [enable admin policy plugin]), [enable_admin_policy=${enableval}]) +AM_CONDITIONAL(ADMIN_POLICY, test "${enable_admin_policy}" = "yes") + if (test "${prefix}" = "NONE"); then dnl no prefix and no localstatedir, so default to /var if (test "$localstatedir" = '${prefix}/var'); then diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c new file mode 100644 index 000000000000..dd8d8973636f --- /dev/null +++ b/plugins/admin_policy.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2021 Google LLC + * + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "src/log.h" +#include "src/plugin.h" + +static int admin_policy_init(void) +{ + DBG(""); +} + +static void admin_policy_exit(void) +{ + DBG(""); +} + +BLUETOOTH_PLUGIN_DEFINE(admin_policy, VERSION, + BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, + admin_policy_init, admin_policy_exit) From patchwork Thu Jul 8 06:23:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364645 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8716C07E96 for ; Thu, 8 Jul 2021 06:23:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BF26A619A2 for ; Thu, 8 Jul 2021 06:23:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229866AbhGHG0e (ORCPT ); Thu, 8 Jul 2021 02:26:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbhGHG0e (ORCPT ); Thu, 8 Jul 2021 02:26:34 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F027FC061574 for ; Wed, 7 Jul 2021 23:23:52 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id i190-20020a3786c70000b02903b54f40b442so3253205qkd.0 for ; Wed, 07 Jul 2021 23:23:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=CYGomBsBa31x4CSWwMfChvCx9H5IkyuUUeENh4oF3Zw=; b=vQNKxKg6OT2X7E/fh+1B6UanU9Fy45pfFC4CBhpI+79I7lvtj0p+i0O3BmdaB5pZlB 8CBBLubYz+YkXVdNbI605ccOcwasfVe9E5TDEnTsMqZ+xd4v4eK1cSdknGTR5iMQdGpK LRIbTehPmwbrBxTkOOXHg3hhCOfHhbySr8u1/YJB4Ccb253gIoDftnzQsl4YwSyrku8h Vaf40ZKupBSrfi8qBeB7xyxTfChglcs+KJQ4t83CTi+iAPcaHYvvTpcxWDgKQXnbPngA kTeH3bHRRdrYoqRyt492+2RJIwo0Yt0guQ9snn7uYFYRzntVtqy4QUxyuiChgn0dquK5 7pCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=CYGomBsBa31x4CSWwMfChvCx9H5IkyuUUeENh4oF3Zw=; b=Ys81EwzBv5RXWcZWuCB5QpnpKs25DFFq6NPqlwwDczKdK22rEy622La69WgJXjk10E dZsJgMMpGgRzLidz1B4eFlLr4NaBIgXEsckCqJ6U6HxTKyFrZsu00d43ZeeX+fhDW7wg vk8oTBe88Uxa54QefIQs1pvpD+vUmDz8TOHyvhYP5m6dgBdQc6top0J/SoYlcGyNGq60 VexSzhB1nxL87km2j1amwODuo4g5tAlHEed62K7TuMA0bcGb+oKj5jOUYJOnV2bKXVcs kbW6K/m4uT91wIfxp/gi9aeZr68tAJEjTDI052ktb7leEpmk+bXpkNHt6+T/dIjHvdpI bfeA== X-Gm-Message-State: AOAM53212JPeu4iYOgDlCMq/uvR1pnHZzGnP7ahQdfHJZ1sWwqXj5wzi 505DW5w4RZSdZyRObVbB8PCCDceN6tDYsRfHQG9tj6Z3SzuuDJi7d4vzKkz/njUNZJrWkHoTcjI 0AtwAKIiy4d9QDH55FxXkA63diGjr3eSULUxrcZaTwiBxYldrmOF4q8qeQJ8jAJoN/FalBQERQe NQWzTsiamE6PA= X-Google-Smtp-Source: ABdhPJzDTaTrXMbN9/QW+5MXGS5VY3EElQnN8BWnzFDZIzw9GsvbviVVBZXmzEXqd2VZa2MtHSgEKQMMEYB7ciWYzg== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a05:6214:1028:: with SMTP id k8mr28427293qvr.13.1625725432052; Wed, 07 Jul 2021 23:23:52 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:08 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.8.I29607be7ac91b0a494ab51713ba14f583eb858ed@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 08/14] plugins/admin_policy: add admin_policy adapter driver From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds code to register admin_policy driver to adapter when admin_policy plugin is enabled. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. restart bluetoothd 2. check if "Admin Policy is enabled" in system log plugins/admin_policy.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index dd8d8973636f..2ece871564e6 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -12,17 +12,84 @@ #include #endif +#include "lib/bluetooth.h" + +#include "src/adapter.h" +#include "src/error.h" #include "src/log.h" #include "src/plugin.h" +#include "src/shared/queue.h" + +/* |policy_data| has the same life cycle as btd_adapter */ +static struct btd_admin_policy { + struct btd_adapter *adapter; + uint16_t adapter_id; +} *policy_data = NULL; + +static struct btd_admin_policy *admin_policy_new(struct btd_adapter *adapter) +{ + struct btd_admin_policy *admin_policy = NULL; + + admin_policy = g_try_malloc(sizeof(*admin_policy)); + if (!admin_policy) { + btd_error(btd_adapter_get_index(adapter), + "Failed to allocate memory for admin_policy"); + return NULL; + } + + admin_policy->adapter = adapter; + admin_policy->adapter_id = btd_adapter_get_index(adapter); + + return admin_policy; +} + +static void admin_policy_free(void *data) +{ + struct btd_admin_policy *admin_policy = data; + + g_free(admin_policy); +} + +static int admin_policy_adapter_probe(struct btd_adapter *adapter) +{ + if (policy_data) { + btd_warn(policy_data->adapter_id, + "Policy data already exists"); + admin_policy_free(policy_data); + policy_data = NULL; + } + + policy_data = admin_policy_new(adapter); + if (!policy_data) + return -ENOMEM; + + btd_info(policy_data->adapter_id, "Admin Policy has been enabled"); + + return 0; +} + +static struct btd_adapter_driver admin_policy_driver = { + .name = "admin_policy", + .probe = admin_policy_adapter_probe, + .resume = NULL, +}; + static int admin_policy_init(void) { DBG(""); + + return btd_register_adapter_driver(&admin_policy_driver); } static void admin_policy_exit(void) { DBG(""); + + btd_unregister_adapter_driver(&admin_policy_driver); + + if (policy_data) + admin_policy_free(policy_data); } BLUETOOTH_PLUGIN_DEFINE(admin_policy, VERSION, From patchwork Thu Jul 8 06:23:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364647 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 442B1C07E96 for ; Thu, 8 Jul 2021 06:23:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1FC8561C59 for ; Thu, 8 Jul 2021 06:23:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229868AbhGHG0i (ORCPT ); Thu, 8 Jul 2021 02:26:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbhGHG0i (ORCPT ); Thu, 8 Jul 2021 02:26:38 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68BBEC061760 for ; Wed, 7 Jul 2021 23:23:56 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id lb18-20020a17090b4a52b02901727f0a3730so2985325pjb.0 for ; Wed, 07 Jul 2021 23:23:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=vyf95nCL7Sye+qsPxqCTZqDMucxaeaWHhZOvyy0owIM=; b=AhSRfzrZvMdTuDKAQVbH6S61F+/9PIVAGRBfkRs04QyAn93xqOE9gThwDQoy/9FPIV 5gUSQr9EdXZzl3DIcLMEqAitLsd4e9kwxUA3/2VECefGqAzGRXrSuCDligGcahMtpKeI reuq0m2DzBku1QhkL9WAu4TdztWr60amwB9XhjTqT94jCW7Luf8txCZ/tedRKiiDPVwE hjSrKKPwK3nSsQbaGGEiiKVhMzjhud81aj179kkGH1/WGSzBnG2IrfwyIE7NV5yKn7fO JGP5EZNVWOJbbYEc3JvMscV4vrlz3jQu5ruMu0js50k/4in89cNCDnQvRQxZYre0gjxi Wl5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=vyf95nCL7Sye+qsPxqCTZqDMucxaeaWHhZOvyy0owIM=; b=Tu41oadbAOvmujXkcLCtYbRmwJQyZ6nylHYP8EJ0QyU943aP4PkFMr3dHjRG102/UA Ft0f5MML6UWdxKHnibJWAVTimoW7wLLCQ6ffO3bNsloLB5NgFC+4BZwKUWXbMmiXrX0M 5KtdSGjDYjP/fJ5J6ymZi2WiP7hcpu+QSMLgXlir8actsW9o7RVaBS+3EJXz21VBTXlF nnS3BUQxCXppVL5S3ifoSxTI9zcf/rkrDuzur+BzGqdgWch7/7V6J6IQrjjKhBJzOGHB 12NPmRXxBYhmZswi+EBNS3DmZh9r0vkfn3D6L3HNKA2QZ7htBSSj8UQx6x0DVOS2H0GT AE0g== X-Gm-Message-State: AOAM530xqUuNwOiddA6/ewf7uZOe01ahWoD0jvCw5bRuTsoLBQBwz0eM JodnTZO8ojIAPnHqGpPk5pe/7DiA+dMipHDD1FdHKF4PdEHfb30KGLydv9mlb4P/FOxlKySNtaQ +fabvoaeJiFmWJ5S4RtBaTpfQLJDb4f4Zu9kKKhdP4IplXkGkiEmMe+2cEtzGFuCyXK4HVyPTfg ohMSQZFa3gA94= X-Google-Smtp-Source: ABdhPJw4cPPvcMC0nOhyo3G7gS2JMT96i5Iovl8L2UNLoj8vGp3ZG8bhIdKZNYJmZz3yqakqbcFV+pi+7ngMf1k4Dw== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a62:5285:0:b029:2e9:e0d5:67dc with SMTP id g127-20020a6252850000b02902e9e0d567dcmr29352694pfb.79.1625725435815; Wed, 07 Jul 2021 23:23:55 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:09 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.9.Ifbb69dd6e371da3a914049a94615064479b9024b@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 09/14] plugins/admin_policy: add ServiceAllowList method From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds code to register interface org.bluez.AdminPolicySet1. The interface will provide methods to limit users to operate certain functions of bluez, such as allow/disallow user to taggle adapter power, or only allow users to connect services in the specified list, etc. This patch also implements ServiceAllowlist in org.bluez.AdminPolicySet1. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. Set ServiceAllowList to ["0x1108","0x110A","0x110B","0x110C","0x110D","0x110E", "0x110F","0x1112","0x111E","0x111F","0x1203"] ( users are only allowed to connect headset ) 2. Turn on paired WF1000XM3, and listen music on Youtube. 3. Turn on paired K830 (LE device), press any key on keyboard. 4. Turn on paired Samsung Bluetooth Keyboard EE-BT550 (BREDR device), press any key on keyboard. 5. Set ServiceAllowList to ["1124","180A","180F","1812"] ( users are only allowed to connect HID devices ) 6. Turn on paired WF1000XM3, and listen music on Youtube. 7. Turn on paired K830 (LE device), press any key on keyboard. 8. Turn on paired Samsung Bluetooth Keyboard EE-BT550 (BREDR device), press any key on keyboard. 9. Set ServiceAllowList to [] ( users are only allowed to connect any device. ) 10. Turn on paired WF1000XM3, and listen music on Youtube. 11. Turn on paired K830 (LE device), press any key on keyboard. 12. Turn on paired Samsung Bluetooth Keyboard EE-BT550 (BREDR device), press any key on keyboard. Expected results: Step 2,7,8,9,10,11 should success, and step 3,4,6 should fail. plugins/admin_policy.c | 123 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index 2ece871564e6..242b8d5dacb0 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -12,19 +12,29 @@ #include #endif +#include +#include + #include "lib/bluetooth.h" +#include "lib/uuid.h" #include "src/adapter.h" +#include "src/dbus-common.h" #include "src/error.h" #include "src/log.h" #include "src/plugin.h" #include "src/shared/queue.h" +#define ADMIN_POLICY_SET_INTERFACE "org.bluez.AdminPolicySet1" + +static DBusConnection *dbus_conn; + /* |policy_data| has the same life cycle as btd_adapter */ static struct btd_admin_policy { struct btd_adapter *adapter; uint16_t adapter_id; + struct queue *service_allowlist; } *policy_data = NULL; static struct btd_admin_policy *admin_policy_new(struct btd_adapter *adapter) @@ -40,17 +50,116 @@ static struct btd_admin_policy *admin_policy_new(struct btd_adapter *adapter) admin_policy->adapter = adapter; admin_policy->adapter_id = btd_adapter_get_index(adapter); + admin_policy->service_allowlist = NULL; return admin_policy; } +static void free_service_allowlist(struct queue *q) +{ + queue_destroy(q, g_free); +} + static void admin_policy_free(void *data) { struct btd_admin_policy *admin_policy = data; + free_service_allowlist(admin_policy->service_allowlist); g_free(admin_policy); } +static struct queue *parse_allow_service_list(struct btd_adapter *adapter, + DBusMessage *msg) +{ + DBusMessageIter iter, arr_iter; + struct queue *uuid_list = NULL; + + dbus_message_iter_init(msg, &iter); + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) + return NULL; + + uuid_list = queue_new(); + dbus_message_iter_recurse(&iter, &arr_iter); + do { + const int type = dbus_message_iter_get_arg_type(&arr_iter); + char *uuid_param; + bt_uuid_t *uuid; + + if (type == DBUS_TYPE_INVALID) + break; + + if (type != DBUS_TYPE_STRING) + goto failed; + + dbus_message_iter_get_basic(&arr_iter, &uuid_param); + + uuid = g_try_malloc(sizeof(*uuid)); + if (!uuid) + goto failed; + + if (bt_string_to_uuid(uuid, uuid_param)) { + g_free(uuid); + goto failed; + } + + queue_push_head(uuid_list, uuid); + + dbus_message_iter_next(&arr_iter); + } while (true); + + return uuid_list; + +failed: + queue_destroy(uuid_list, g_free); + return NULL; +} + +static bool service_allowlist_set(struct btd_admin_policy *admin_policy, + struct queue *uuid_list) +{ + struct btd_adapter *adapter = admin_policy->adapter; + + if (!btd_adapter_set_allowed_uuids(adapter, uuid_list)) + return false; + + free_service_allowlist(admin_policy->service_allowlist); + admin_policy->service_allowlist = uuid_list; + + return true; +} + +static DBusMessage *set_service_allowlist(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + struct btd_admin_policy *admin_policy = user_data; + struct btd_adapter *adapter = admin_policy->adapter; + struct queue *uuid_list = NULL; + const char *sender = dbus_message_get_sender(msg); + + DBG("sender %s", sender); + + /* Parse parameters */ + uuid_list = parse_allow_service_list(adapter, msg); + if (!uuid_list) { + btd_error(admin_policy->adapter_id, + "Failed on parsing allowed service list"); + return btd_error_invalid_args(msg); + } + + if (!service_allowlist_set(admin_policy, uuid_list)) { + free_service_allowlist(uuid_list); + return btd_error_failed(msg, "service_allowlist_set failed"); + } + + return dbus_message_new_method_return(msg); +} + +static const GDBusMethodTable admin_policy_adapter_methods[] = { + { GDBUS_METHOD("SetServiceAllowList", GDBUS_ARGS({ "UUIDs", "as" }), + NULL, set_service_allowlist) }, + { } +}; + static int admin_policy_adapter_probe(struct btd_adapter *adapter) { if (policy_data) { @@ -64,8 +173,18 @@ static int admin_policy_adapter_probe(struct btd_adapter *adapter) if (!policy_data) return -ENOMEM; - btd_info(policy_data->adapter_id, "Admin Policy has been enabled"); + if (!g_dbus_register_interface(dbus_conn, adapter_get_path(adapter), + ADMIN_POLICY_SET_INTERFACE, + admin_policy_adapter_methods, NULL, + NULL, policy_data, admin_policy_free)) { + btd_error(policy_data->adapter_id, + "Admin Policy Set interface init failed on path %s", + adapter_get_path(adapter)); + return -EINVAL; + } + btd_info(policy_data->adapter_id, + "Admin Policy Set interface registered"); return 0; } @@ -79,6 +198,8 @@ static int admin_policy_init(void) { DBG(""); + dbus_conn = btd_get_dbus_connection(); + return btd_register_adapter_driver(&admin_policy_driver); } From patchwork Thu Jul 8 06:23:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364649 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21DAEC07E99 for ; Thu, 8 Jul 2021 06:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0857261C69 for ; Thu, 8 Jul 2021 06:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229877AbhGHG0m (ORCPT ); Thu, 8 Jul 2021 02:26:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229872AbhGHG0m (ORCPT ); Thu, 8 Jul 2021 02:26:42 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DC30C061574 for ; Wed, 7 Jul 2021 23:24:00 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id f3-20020a6310030000b029022c3e789d78so196352pgl.6 for ; Wed, 07 Jul 2021 23:24:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=diaiaBahWKD4SU9ie43Sk9OFBSaieqno3X9F2q/9zjo=; b=A4zk4KGWtXo2YVQ7ePib6VRMEY2sQKaPfnYg/6PMuo4viF+ydJUYiO6QppNx/9wOQH 3dkFVdIfYIEVHDF5I+wDyRNKohYMYO/wwaILRZD1J/HjuC3wwmbaGBw0knrgDMbWmjT5 3nmPt14uJbz61Udb7xUkPKgeuTDo+nonpqBrjaL9HK0MQvrjFPNJE7JxYX++CuA2MMl2 rQjHT/NmXSZNf03s6W1+mrb0RyvnAQFvedJcIivCiKuqn2sm4faK7eOZDtmAvRvJww11 U0fD20Nyq/+Qrfno2RLDsxVwIUA4DKZdt3+6J1XEY8wXmoiBbHjR/1X06kGHS8l5Uo2Z Gk5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=diaiaBahWKD4SU9ie43Sk9OFBSaieqno3X9F2q/9zjo=; b=U0SziEXeaTR3C9GCATaU9KTvZENysjQt/G9WQrToB79Tb6t7XvcZJFv9qb9ybd27WJ TPmUQ3ccHYRby+wACYQ5mmJY1pWqr0xkaE7jhZAgvi1BKRZoCmSWcPKNd4KOnnWk8NoV NWvT7xbawMx6CYYfftaXqeStQzF8ovlRmTKxoidMcgRwJCoBHfotA7LSjTrlm1d9KttQ MTqyc3LHnsfUZhEWDBYJi9oEuoAgOGr4NUZKy8Yha87LHI4iM0dwHP2ADIfBNPUlLc1z lc+onil7osWQyYfWgSj70vCXZUDT7cPZy1PZVlAtoZ3D0BZ6anCZr0nKb8WY/8Cfi9Nt btNg== X-Gm-Message-State: AOAM531suHE8oreM/BRnmMkoZIFRyMgD94SKlqpYMUltsHwuKpvjC7wF mNK93zcMeEhCPXYy6Y67S8TGkOb93mXJXMRV5dqW31ihA+wVY7RmZmi2cxtmX1UG5bB54UYvHfN fAUM/cMfUg5366YyD0Lbb8Kr+WPd5+m8Jhi2ScrQ0R9Qmm0dTZaurxElJUeLY2dKiR1oGCMRTbs b+vliHzu3nQ5s= X-Google-Smtp-Source: ABdhPJyu3+mBL9U5pUveaIRBsk2SKUITaonEAL6Ix+hS87YJTF7CIaVqZN9zb7iOaBi9mnDMPyb/D6qT1zxdHWrhUA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a17:902:7e4d:b029:129:8fd3:b30f with SMTP id a13-20020a1709027e4db02901298fd3b30fmr16174810pln.85.1625725439655; Wed, 07 Jul 2021 23:23:59 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:10 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.10.I00fd6c348e4c93501de6de0eae0d23436fd3895b@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 10/14] plugins/admin_policy: add ServiceAllowList property From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds code to register interface org.bluez.AdminPolicyStatus. The interface will provide read-only properties to indicate the current settings of admin policies. We separate this from AdminPolicySet so that normal clients can check current policy settings while only a few clients can change policies. This patch also adds readonly property ServiceAllowlist to AdminPolicyStatus1, which indicates the current setting of service allowlist. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. Set ServiceAllowList to ["1124","180A","180F","1812"] 2. Verify ServiceAllowList is ["1124","180A","180F","1812"] in UUID-128 form 3. Set ServiceAllowList to [] 4. Verify ServiceAllowList is [] plugins/admin_policy.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index 242b8d5dacb0..c5ad31f761d9 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -27,6 +27,7 @@ #include "src/shared/queue.h" #define ADMIN_POLICY_SET_INTERFACE "org.bluez.AdminPolicySet1" +#define ADMIN_POLICY_STATUS_INTERFACE "org.bluez.AdminPolicyStatus1" static DBusConnection *dbus_conn; @@ -151,6 +152,10 @@ static DBusMessage *set_service_allowlist(DBusConnection *conn, return btd_error_failed(msg, "service_allowlist_set failed"); } + g_dbus_emit_property_changed(dbus_conn, + adapter_get_path(policy_data->adapter), + ADMIN_POLICY_SET_INTERFACE, "ServiceAllowList"); + return dbus_message_new_method_return(msg); } @@ -160,6 +165,43 @@ static const GDBusMethodTable admin_policy_adapter_methods[] = { { } }; +void append_service_uuid(void *data, void *user_data) +{ + bt_uuid_t *uuid = data; + DBusMessageIter *entry = user_data; + char uuid_str[MAX_LEN_UUID_STR]; + const char *uuid_str_ptr = uuid_str; + + if (!uuid) { + error("Unexpected NULL uuid data in service_allowlist"); + return; + } + + bt_uuid_to_string(uuid, uuid_str, MAX_LEN_UUID_STR); + dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &uuid_str_ptr); +} + +static gboolean property_get_service_allowlist( + const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct btd_admin_policy *admin_policy = user_data; + DBusMessageIter entry; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_STRING_AS_STRING, &entry); + queue_foreach(admin_policy->service_allowlist, append_service_uuid, + &entry); + dbus_message_iter_close_container(iter, &entry); + + return TRUE; +} + +static const GDBusPropertyTable admin_policy_adapter_properties[] = { + { "ServiceAllowList", "as", property_get_service_allowlist }, + { } +}; + static int admin_policy_adapter_probe(struct btd_adapter *adapter) { if (policy_data) { @@ -185,6 +227,21 @@ static int admin_policy_adapter_probe(struct btd_adapter *adapter) btd_info(policy_data->adapter_id, "Admin Policy Set interface registered"); + + if (!g_dbus_register_interface(dbus_conn, adapter_get_path(adapter), + ADMIN_POLICY_STATUS_INTERFACE, + NULL, NULL, + admin_policy_adapter_properties, + policy_data, admin_policy_free)) { + btd_error(policy_data->adapter_id, + "Admin Policy Status interface init failed on path %s", + adapter_get_path(adapter)); + return -EINVAL; + } + + btd_info(policy_data->adapter_id, + "Admin Policy Status interface registered"); + return 0; } From patchwork Thu Jul 8 06:23:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364651 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F4E6C07E96 for ; Thu, 8 Jul 2021 06:24:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2752161C59 for ; Thu, 8 Jul 2021 06:24:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229901AbhGHG0p (ORCPT ); Thu, 8 Jul 2021 02:26:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229872AbhGHG0p (ORCPT ); Thu, 8 Jul 2021 02:26:45 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07DC2C061574 for ; Wed, 7 Jul 2021 23:24:04 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id eo5-20020ad459450000b02902d60d0f0744so387057qvb.2 for ; Wed, 07 Jul 2021 23:24:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=TJCDUU+XB3aEdDYTTNZHmC5Pd9cnwtGzJfl+G0OsRkk=; b=oH38xudHwDoFduby2q7apvV+9KLZxTLhTIKbNSXv2mgzDsS+FyysqOjloIqhOM4YAd JqBfDVhRavczML0nujtpTtQpwnpqV57m9qxpO7tN8uTzed1U0Rlgb94msKpXktQb7Atd fbgZL8sTzZz2c9wOZKGwdcNoEurejMvfIXD5enDmUIF2Lww1BroKo3wne6BaqhufeVk/ wnMJRwgEudpkXwZGT3C1TldkUg+Sewp1TDL18mN6fcRQNsO0MHizubatOBRXECkOzFsm mbZnHU1qTkh0fQoZlMmC82OaYvNkN9YiR6U6kIfk+14Uc/Pvoc8lXr1AVYc26NE6mtXz 3ebw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=TJCDUU+XB3aEdDYTTNZHmC5Pd9cnwtGzJfl+G0OsRkk=; b=K+UTEIqFjKOG+SZFmyWNLZ2DDEZwgUBwhJBCNT/GBAX6wPlKYXQuU9Tt27QvbpaxQx 0OKnjvr0lVZoYWGAW3fFXciUIS0Rdd1RM88YgG007OHFuROeA1qH4uyy0ZljJ506Tj+o ulQGVdlPQ43s4gkPRJTTI0wJZsmj8xM/Prtbu3+QeYpzpIL8sMAzK7O7iHrO9QK4jKF4 n4kGHffQSpyMI5wIiDvKu2tsN88rthomKsL8hDmtS27dNgwTdoTdod+qOOJK/ZftJsqZ rs5NTQDdtHQW6DIuFR9go9HoY8iscfbzzvqXQY990C6yrUGr7bNJSkYujxvaz0DXaL1y LBQw== X-Gm-Message-State: AOAM530ZcwuzitKDqLXmUC2OAA85A54R9NSP7vhc7tpfa/b5oOOxs8jv Ml2cFB6rXRgV7waxfuxFxwbS3i3Dbdg6Ot6Wwn/iuLRBBeu4/cSd8fVIKJUpEKoOFSurROB1uAL NBBsDg/HLB+6mSFwBCzlwXphdKKT4SFEzqPAXU6cJ7pUQfiZcQTVwNZRjzf8JufOKCVBF3TJIdt RPy0ZxY495o0s= X-Google-Smtp-Source: ABdhPJyk4LY8QX0NwWBlmTHQxoOU46RfDX9Jr/V2azCqJPcw8ta7+G6l9uPXlansLmQmivNexh6ggbV4s5hRrI4hDA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a0c:c249:: with SMTP id w9mr28296697qvh.32.1625725443110; Wed, 07 Jul 2021 23:24:03 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:11 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.11.I517e5199ac8019b770c7ee8c92a294ec1c752748@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 11/14] plugins/admin_policy: add device state callback From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This registers a device state callback function. It will be used to implement "AffectedByPolicy" property which indicates if there is any service in a device that is being blocked by admin policy. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. start discovery using UI 2. verify device_data were added by checking system log 3. stop discovery 4. verify device_data were removed after a few seconds by checking system log plugins/admin_policy.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index c5ad31f761d9..852e79aaa07a 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -20,6 +20,7 @@ #include "src/adapter.h" #include "src/dbus-common.h" +#include "src/device.h" #include "src/error.h" #include "src/log.h" #include "src/plugin.h" @@ -30,6 +31,8 @@ #define ADMIN_POLICY_STATUS_INTERFACE "org.bluez.AdminPolicyStatus1" static DBusConnection *dbus_conn; +static struct queue *devices; /* List of struct device_data objects */ +static unsigned int device_cb_id; /* |policy_data| has the same life cycle as btd_adapter */ static struct btd_admin_policy { @@ -38,6 +41,11 @@ static struct btd_admin_policy { struct queue *service_allowlist; } *policy_data = NULL; +struct device_data { + struct btd_device *device; + char *path; +}; + static struct btd_admin_policy *admin_policy_new(struct btd_adapter *adapter) { struct btd_admin_policy *admin_policy = NULL; @@ -245,6 +253,79 @@ static int admin_policy_adapter_probe(struct btd_adapter *adapter) return 0; } +static bool device_data_match(const void *a, const void *b) +{ + const struct device_data *data = a; + const struct btd_device *dev = b; + + if (!data) { + error("Unexpected NULL device_data"); + return false; + } + + return data->device == dev; +} + +static void free_device_data(struct device_data *data) +{ + g_free(data->path); + g_free(data); +} + +static void remove_device_data(void *data) +{ + struct device_data *device_data = data; + + DBG("device_data for %s removing", device_data->path); + + queue_remove(devices, device_data); + free_device_data(device_data); +} + +static void add_device_data(struct btd_device *device) +{ + struct btd_adapter *adapter = device_get_adapter(device); + struct device_data *data; + + if (queue_find(devices, device_data_match, device)) + return; + + data = g_new0(struct device_data, 1); + if (!data) { + btd_error(btd_adapter_get_index(adapter), + "Failed to allocate memory for device_data"); + return; + } + + data->device = device; + data->path = g_strdup(device_get_path(device)); + queue_push_tail(devices, data); + + DBG("device_data for %s added", data->path); +} + +static void admin_policy_device_state_cb(struct btd_device *device, + enum btd_device_state_t new_state, + void *user_data) +{ + struct device_data *data = NULL; + + switch (new_state) { + case BTD_DEVICE_STATE_INITIALIZING: + warn("Unexpected new state %d", new_state); + return; + case BTD_DEVICE_STATE_AVAILABLE: + add_device_data(device); + break; + case BTD_DEVICE_STATE_REMOVING: + data = queue_find(devices, device_data_match, device); + + if (data) + remove_device_data(data); + break; + } +} + static struct btd_adapter_driver admin_policy_driver = { .name = "admin_policy", .probe = admin_policy_adapter_probe, @@ -256,6 +337,10 @@ static int admin_policy_init(void) DBG(""); dbus_conn = btd_get_dbus_connection(); + devices = queue_new(); + + device_cb_id = btd_device_add_state_cb(admin_policy_device_state_cb, + NULL); return btd_register_adapter_driver(&admin_policy_driver); } @@ -265,6 +350,8 @@ static void admin_policy_exit(void) DBG(""); btd_unregister_adapter_driver(&admin_policy_driver); + queue_destroy(devices, free_device_data); + btd_device_remove_state_cb(device_cb_id); if (policy_data) admin_policy_free(policy_data); From patchwork Thu Jul 8 06:23:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364653 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-27.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 30170C07E96 for ; Thu, 8 Jul 2021 06:24:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 18A8461C59 for ; Thu, 8 Jul 2021 06:24:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229966AbhGHG0s (ORCPT ); Thu, 8 Jul 2021 02:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbhGHG0s (ORCPT ); Thu, 8 Jul 2021 02:26:48 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 700DCC061574 for ; Wed, 7 Jul 2021 23:24:07 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id o11-20020a62f90b0000b02902db3045f898so3132729pfh.23 for ; Wed, 07 Jul 2021 23:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=VyXHTtvy32iIeRBIdw6ZHes2fzV1g/gDjPGzdkXsXsY=; b=oczNY2z89CYZ76FNlU5tn/jpfXqsZZ4rAGpq6uxthuJFOFNf4BRG/qTttDoNJTG6A6 6i1wsZXilibxuzTUoevMyqz0WUFLhdujPPFJNV+85AJR+mqjaYfKxpY/JGGSOzcsae24 3WYTcAfj058+xGEXJL92XX9ftHWqAAqZArDowxxptfYoSQkTg7VzZkXe6LgM7P2tVqgU 6rJEEspe2eO6JyzWUtLCT47uqelRzdLaM4iFeswmdDNuZyypvD2qUorKAIG5ymnLP6ca MoATUx+eum+MMqZfmrMgdostv7bJJ8M3uvYbn7PCqyRXPmK+oZgHx+5Jhl7BIzAli5ay AUEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VyXHTtvy32iIeRBIdw6ZHes2fzV1g/gDjPGzdkXsXsY=; b=cf48z+KqHyGEYnJV1gut1MSnNldxYje/Ds/se+fwWxYybJtt6BbPBPsZGtFl9vJEWF 0yQOi8HX6h9yAT7jhj9z8YsGi92K7g+rJq0Y3YQmpg2OsX4cYToKIvRmEvcum+TlFgrb fk6leAplBZ+qkO5W9Cv3gkka3vlq0m1QYLOTWeC/KUBS+JY93+gluRO4vJkz/kE/MSuV +GxRTrIIIVZIDuJvdvRAIOIS6WffrGmv1t4ID42vSe0u293fkEswFqIzsFJ1Qd32mjlD 8ZTV1LDGJOoNWO0dyfIr0EoID4WQH5EGrb/BjQvW6dpoaMAn8qZxd7Cl2EPAVKyL6qnF UTGw== X-Gm-Message-State: AOAM530sT8qGqobTPcQsDe2l90wxMZzfGgdUQDBv2UGUp/Mh+SHvF1y8 eCxf6MF39ZqxsX5M6GuMomaUm9tKhM4LgSk916dWJOK0MkOJTNQsTUBOzYcWIcGd1QdhPJvRd7i uj+q0CcQQaEqOk23JkzV3dbIGagB30Pxd5lnrwEr2/Lg7vMEIVaeuS0iOBQZJEmRSI00O3dshyq YB/1sSauVS/KI= X-Google-Smtp-Source: ABdhPJzyHLAoPUWeakqN2PnzBSWtuqDIOoFn4j6/uibxDq5T/DoXCi37FNY+7RcVKrCWQ64NOyHFRpwPW30Z3yE0wA== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a62:bd14:0:b029:2de:8bf7:2df8 with SMTP id a20-20020a62bd140000b02902de8bf72df8mr29365345pff.60.1625725446900; Wed, 07 Jul 2021 23:24:06 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:12 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.12.I570c860f59c8ed66ddb31aa54584ee08080aa10c@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 12/14] plugins/admin_policy: add AffectedByPolicy property From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds property to indicate if a device has any service that is being blocked by admin policy. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. Set ServiceAllowList to [] 2. Verify AffectedByPolicy of K830 is False 3. Set ServiceAllowList to ["1800","1801","180A","180F","1812", "00010000-0000-1000-8000-011f2000046d" 4. Verify AffectedByPolicy of K830 is False 5. Set ServiceAllowList to ["1800","1801","180A","180F","1812"] 6. Verify AffectedByPolicy of K830 is True plugins/admin_policy.c | 78 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index 852e79aaa07a..be4ba096a8b9 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -44,6 +44,7 @@ static struct btd_admin_policy { struct device_data { struct btd_device *device; char *path; + bool affected; }; static struct btd_admin_policy *admin_policy_new(struct btd_adapter *adapter) @@ -137,6 +138,27 @@ static bool service_allowlist_set(struct btd_admin_policy *admin_policy, return true; } +static void update_device_affected(void *data, void *user_data) +{ + struct device_data *dev_data = data; + bool affected; + + if (!dev_data) { + error("Unexpected NULL device_data when updating device"); + return; + } + + affected = btd_device_all_services_allowed(dev_data->device); + + if (affected == dev_data->affected) + return; + + dev_data->affected = affected; + + g_dbus_emit_property_changed(dbus_conn, dev_data->path, + ADMIN_POLICY_STATUS_INTERFACE, "AffectedByPolicy"); +} + static DBusMessage *set_service_allowlist(DBusConnection *conn, DBusMessage *msg, void *user_data) { @@ -164,6 +186,8 @@ static DBusMessage *set_service_allowlist(DBusConnection *conn, adapter_get_path(policy_data->adapter), ADMIN_POLICY_SET_INTERFACE, "ServiceAllowList"); + queue_foreach(devices, update_device_affected, NULL); + return dbus_message_new_method_return(msg); } @@ -266,6 +290,29 @@ static bool device_data_match(const void *a, const void *b) return data->device == dev; } +static gboolean property_get_affected_by_policy( + const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + struct device_data *data = user_data; + dbus_bool_t affected; + + if (!data) { + error("Unexpected error: device_data is NULL"); + return FALSE; + } + + dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, + &data->affected); + + return TRUE; +} + +static const GDBusPropertyTable admin_policy_device_properties[] = { + { "AffectedByPolicy", "b", property_get_affected_by_policy }, + { } +}; + static void free_device_data(struct device_data *data) { g_free(data->path); @@ -299,11 +346,33 @@ static void add_device_data(struct btd_device *device) data->device = device; data->path = g_strdup(device_get_path(device)); + data->affected = btd_device_all_services_allowed(data->device); + + if (!g_dbus_register_interface(dbus_conn, data->path, + ADMIN_POLICY_STATUS_INTERFACE, + NULL, NULL, + admin_policy_device_properties, + data, remove_device_data)) { + btd_error(btd_adapter_get_index(adapter), + "Admin Policy Status interface init failed on path %s", + device_get_path(device)); + free_device_data(data); + return; + } + queue_push_tail(devices, data); DBG("device_data for %s added", data->path); } +static void unregister_device_data(void *data, void *user_data) +{ + struct device_data *dev_data = data; + + g_dbus_unregister_interface(dbus_conn, dev_data->path, + ADMIN_POLICY_STATUS_INTERFACE); +} + static void admin_policy_device_state_cb(struct btd_device *device, enum btd_device_state_t new_state, void *user_data) @@ -321,7 +390,7 @@ static void admin_policy_device_state_cb(struct btd_device *device, data = queue_find(devices, device_data_match, device); if (data) - remove_device_data(data); + unregister_device_data(data, NULL); break; } } @@ -339,6 +408,9 @@ static int admin_policy_init(void) dbus_conn = btd_get_dbus_connection(); devices = queue_new(); + device_cb_id = btd_device_add_state_cb(admin_policy_device_state_cb, + NULL); + device_cb_id = btd_device_add_state_cb(admin_policy_device_state_cb, NULL); @@ -350,9 +422,11 @@ static void admin_policy_exit(void) DBG(""); btd_unregister_adapter_driver(&admin_policy_driver); - queue_destroy(devices, free_device_data); + queue_foreach(devices, unregister_device_data, NULL); + queue_destroy(devices, g_free); btd_device_remove_state_cb(device_cb_id); + btd_device_remove_state_cb(device_cb_id); if (policy_data) admin_policy_free(policy_data); } From patchwork Thu Jul 8 06:23:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364655 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-24.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63CF9C07E96 for ; Thu, 8 Jul 2021 06:24:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49B5B61C59 for ; Thu, 8 Jul 2021 06:24:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230376AbhGHG0y (ORCPT ); Thu, 8 Jul 2021 02:26:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230388AbhGHG0x (ORCPT ); Thu, 8 Jul 2021 02:26:53 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60405C061574 for ; Wed, 7 Jul 2021 23:24:11 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id k3-20020a6568c30000b029022887377adeso3580020pgt.15 for ; Wed, 07 Jul 2021 23:24:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=0SBfrOcBIQ93k3CvJxMiKTB55WCmnlBqLqaw3gB6WrI=; b=c+TT0zjEfq/GiIJpOrYk9AdRqIFoXWiWOSKnpzMchg+KEgWkuEW1fhvhLfSMEHgACI jsIZxDDex4SceASqs7OA1W0Lc0uaIciS8yrehTsl4V/MCPupqgAU6qg6dnP7R4SUMgGT KR/Mi24YapeIV5Lc0bAMGGdfwq4frQKpVafOHQ/j6hj+J4f8BWmot1eHiY2/vUmqAiYm SQ6YGuY1P/zqmR0iIB1+umxh3i1Uc5ZP/iQtX1JqHuq8JhkLOXGhyOUaxloGvjDgkDsH Ko/y/t0biKucjpYeu2UnMi3uVA8QowqhsMEvri1QPd79g5HgftP+oh5GVLfhE2V8lPQm 0EBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=0SBfrOcBIQ93k3CvJxMiKTB55WCmnlBqLqaw3gB6WrI=; b=OQnzoImv6+N6NOdY+r9ybnjOnP+TCp+Ajlw89vP+gCN4rLCge0oniJZ+iIlaGjEeG4 yeOzcjBgrNn5ixakrXYX79JFeHHsAe32ZfV9k6Ar18ipx+8K/lWY5pdxMIROBl1iteDf 31JxUr485lHiV0yJ4JYM/EOeAP8rbcWZyZWCHhGx5FvvOr+9YKpe1ARi1IE3MC2pcQnn Y+osksP/EpuWp4qQGztOQaTIrVLyWn6jZ0zxL6FS+2kwf0ec2TTUWr0N7Yc16TIcdifx 2eDC9rCdmDL5PmAuW/e4bg/sgEqhY1C7cFDqOqqIlKe1BRwgQPGiGVChHDk3YQ3xP3KP 6ZmQ== X-Gm-Message-State: AOAM533kP8hgGR7llqiFQOC01naP39d4rZW+/+dFppJ2/7R/+XC7Yqjm Sz+qEisBQ9EPMbar6uK1bcrGB4s4antf9gOppoptP0G5/KuUv8fhpwtuMZMIjcFeZ2+UneiaT39 hS/JfYhL0dpa0kLXXYPkgrsfnGLWRJeYyWFlawlwbq/b3wy8zGim95dM+S18Z8eQQ+k30Cd4ezQ nvwXq74BBr9PU= X-Google-Smtp-Source: ABdhPJyoe8NcP0Xw78vfWZJhlhTM/pGRcwOYWl33/vTh7QBlQQY2qFpTVhcjAu22geH6uJIhT49YRadtjYXZtGjZPQ== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a17:902:c181:b029:129:1833:4e6b with SMTP id d1-20020a170902c181b029012918334e6bmr24782679pld.61.1625725450758; Wed, 07 Jul 2021 23:24:10 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:13 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.13.Ib26c0abdbd417673a8b5788c175c06110726a68c@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 13/14] plugins/admin_policy: persist policy settings From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This adds code to store the ServiceAllowlist to file /var/lib/bluetooth/{MAC_ADDR}/admin_policy The stored settings will be loaded upon admin_policy initialized. Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. Set ServiceAllowlist to ["1124","180A","180F","1812", "1801"] 2. restart bluetoothd 3. Verify ServiceAllowlist is ["1124","180A","180F","1812","1801"] in UUID-128 form 4. Set ServiceAllowlist to [] 5. restart bluetoothd 6. Verify ServiceAllowlist is [] plugins/admin_policy.c | 165 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 1 deletion(-) diff --git a/plugins/admin_policy.c b/plugins/admin_policy.c index be4ba096a8b9..168848c82a63 100644 --- a/plugins/admin_policy.c +++ b/plugins/admin_policy.c @@ -14,6 +14,8 @@ #include #include +#include +#include #include "lib/bluetooth.h" #include "lib/uuid.h" @@ -24,12 +26,15 @@ #include "src/error.h" #include "src/log.h" #include "src/plugin.h" +#include "src/textfile.h" #include "src/shared/queue.h" #define ADMIN_POLICY_SET_INTERFACE "org.bluez.AdminPolicySet1" #define ADMIN_POLICY_STATUS_INTERFACE "org.bluez.AdminPolicyStatus1" +#define ADMIN_POLICY_STORAGE STORAGEDIR "/admin_policy_settings" + static DBusConnection *dbus_conn; static struct queue *devices; /* List of struct device_data objects */ static unsigned int device_cb_id; @@ -159,6 +164,8 @@ static void update_device_affected(void *data, void *user_data) ADMIN_POLICY_STATUS_INTERFACE, "AffectedByPolicy"); } +static void store_policy_settings(struct btd_admin_policy *admin_policy); + static DBusMessage *set_service_allowlist(DBusConnection *conn, DBusMessage *msg, void *user_data) { @@ -177,7 +184,9 @@ static DBusMessage *set_service_allowlist(DBusConnection *conn, return btd_error_invalid_args(msg); } - if (!service_allowlist_set(admin_policy, uuid_list)) { + if (service_allowlist_set(admin_policy, uuid_list)) { + store_policy_settings(admin_policy); + } else { free_service_allowlist(uuid_list); return btd_error_failed(msg, "service_allowlist_set failed"); } @@ -234,6 +243,158 @@ static const GDBusPropertyTable admin_policy_adapter_properties[] = { { } }; +static void free_uuid_strings(char **uuid_strs, int num) +{ + gsize i; + + for (i = 0; i < num; i++) + g_free(uuid_strs[i]); + g_free(uuid_strs); +} + +static char **new_uuid_strings(struct queue *allowlist, gsize *num) +{ + const struct queue_entry *entry = NULL; + bt_uuid_t *uuid = NULL; + char **uuid_strs = NULL; + gsize i = 0, allowlist_num; + + allowlist_num = queue_length(allowlist); + uuid_strs = g_try_malloc_n(allowlist_num, sizeof(char *)); + if (!uuid_strs) + return NULL; + + for (entry = queue_get_entries(allowlist); entry != NULL; + entry = entry->next) { + uuid = entry->data; + uuid_strs[i] = g_try_malloc0(MAX_LEN_UUID_STR * sizeof(char)); + + if (!uuid_strs[i]) + goto failed; + + bt_uuid_to_string(uuid, uuid_strs[i], MAX_LEN_UUID_STR); + i++; + } + + *num = allowlist_num; + return uuid_strs; + +failed: + free_uuid_strings(uuid_strs, i); + + return NULL; +} + +static void store_policy_settings(struct btd_admin_policy *admin_policy) +{ + GKeyFile *key_file = NULL; + char *filename = ADMIN_POLICY_STORAGE; + char *key_file_data = NULL; + char **uuid_strs = NULL; + gsize length, num_uuids; + + key_file = g_key_file_new(); + + if (num_uuids) { + uuid_strs = new_uuid_strings(admin_policy->service_allowlist, + &num_uuids); + } + + if (!uuid_strs && num_uuids) { + btd_error(admin_policy->adapter_id, + "Failed to allocate uuid strings"); + goto failed; + } + + g_key_file_set_string_list(key_file, "General", "ServiceAllowlist", + (const gchar * const *)uuid_strs, + num_uuids); + + if (create_file(ADMIN_POLICY_STORAGE, 0600) < 0) { + btd_error(admin_policy->adapter_id, "create %s failed, %s", + filename, strerror(errno)); + goto failed; + } + + key_file_data = g_key_file_to_data(key_file, &length, NULL); + g_file_set_contents(ADMIN_POLICY_STORAGE, key_file_data, length, NULL); + + g_free(key_file_data); + free_uuid_strings(uuid_strs, num_uuids); + +failed: + g_key_file_free(key_file); +} + +static void key_file_load_service_allowlist(GKeyFile *key_file, + struct btd_admin_policy *admin_policy) +{ + GError *gerr = NULL; + struct queue *uuid_list = NULL; + gchar **uuids = NULL; + gsize num, i; + + uuids = g_key_file_get_string_list(key_file, "General", + "ServiceAllowlist", &num, &gerr); + + if (gerr) { + btd_error(admin_policy->adapter_id, + "Failed to load ServiceAllowlist"); + g_error_free(gerr); + return; + } + + uuid_list = queue_new(); + for (i = 0; i < num; i++) { + bt_uuid_t *uuid = g_try_malloc(sizeof(*uuid)); + + if (!uuid) + goto failed; + + if (bt_string_to_uuid(uuid, *uuids)) { + + btd_error(admin_policy->adapter_id, + "Failed to convert '%s' to uuid struct", + *uuids); + + g_free(uuid); + goto failed; + } + + queue_push_tail(uuid_list, uuid); + uuids++; + } + + if (!service_allowlist_set(admin_policy, uuid_list)) + goto failed; + + return; +failed: + free_service_allowlist(uuid_list); +} + +static void load_policy_settings(struct btd_admin_policy *admin_policy) +{ + GKeyFile *key_file; + char *filename = ADMIN_POLICY_STORAGE; + struct stat st; + + if (stat(filename, &st) < 0) { + btd_error(admin_policy->adapter_id, + "Failed to get file %s information", + filename); + return; + } + + key_file = g_key_file_new(); + + g_key_file_load_from_file(key_file, filename, 0, NULL); + + key_file_load_service_allowlist(key_file, admin_policy); + + g_key_file_free(key_file); +} + static int admin_policy_adapter_probe(struct btd_adapter *adapter) { if (policy_data) { @@ -247,6 +408,8 @@ static int admin_policy_adapter_probe(struct btd_adapter *adapter) if (!policy_data) return -ENOMEM; + load_policy_settings(policy_data); + if (!g_dbus_register_interface(dbus_conn, adapter_get_path(adapter), ADMIN_POLICY_SET_INTERFACE, admin_policy_adapter_methods, NULL, From patchwork Thu Jul 8 06:23:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Howard Chung X-Patchwork-Id: 12364657 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-22.0 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54451C07E96 for ; Thu, 8 Jul 2021 06:24:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3BE3D61C69 for ; Thu, 8 Jul 2021 06:24:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230404AbhGHG04 (ORCPT ); Thu, 8 Jul 2021 02:26:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230388AbhGHG04 (ORCPT ); Thu, 8 Jul 2021 02:26:56 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E838C061574 for ; Wed, 7 Jul 2021 23:24:15 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id z13-20020a170903408db0290129a6155d3cso1533648plc.2 for ; Wed, 07 Jul 2021 23:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=FA/zELt0dKCtzElqqEMLMgkIf4ij5eY0jDxfzz2qSTg=; b=FLmPkLBAUkkmpLXpTw5C+EottRvtDvkQs8+CpwMWe1eTsQbk40OFAObFPDoBQ+RjFI tIks/u2k9Qap0qCzGnEWpXQVzRTQjIEsb9pAZX4g2Mf8ADfoZ5Xl6+K8rT5gHcHZWZpU v+NYzz9IPjQr2kw1v0EDksyA2SDHF8O1n+jnH+OVSMRkYzSkbndK4nxPhFHPLdpeeOoV fBqMM+aw5TRmFBruAXZi6YomrCxG4rIc56jFRsX2cotyB/nvLzxvWPv04bgxo3MI4fi5 /9LXl13kv8aaEvzKChL7Ee0sj5gfmIRn81bG9WwGHyAlxkcATS2v9IrFMcu/uzdLiIML d78g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=FA/zELt0dKCtzElqqEMLMgkIf4ij5eY0jDxfzz2qSTg=; b=b9MFWaEHm4X2rdTISicZNM4e7GtwysXbA5srmZV9Fk3doziVH1iLIWlueOgLJvWEAV by35xcZyEr20NMBB/LTFb1q6uE7BkWgOluEkZ7Hgd8x2+tA969bNnZOhLJOPcODumBq2 02dum2yR7B20nTWKe7m9B1V3M+xX4uzv/JXTax9z8wt3mSjvE3DbS89KdE53HN9mabrL ozEs27K2CNc1jYYJQS6lyTHGz37WskTH2mHnJwQ9nGehohcwak+++R5CbrwMdejSXTzy sRJ8wMz+1iMTPzdSebDbpjMic26LQ4esGqup+/+GT6vHHOym7KrnPqj2r+hWEuaIdt1n /Lcw== X-Gm-Message-State: AOAM5311bNDOr2d7wZf5JdUD1GJUfyX0PYV5P2iF1qXlwSpqi++7LG+Y reC+fYCaW5lIrN5CesMv7ywhEc4vcN9/QOfZYrU4Aucl0a2P6+Y7yj2htBFdcGy+UHlMTPIn8Ba KCmSZuMP7CH/XAxTLdaqW7JeNRS9eSRNSwGK29PWF1nVK8QU1AmlcUxm+gGs9nwfn8Xt+E23JHw kfALIkG8dEU9k= X-Google-Smtp-Source: ABdhPJzZOeJNMBJGPeut210seJHIUAar6aDb8zhVfWmOX3LnUlyRXejnEc9ABP3b2U/NS/OBZbpl6dC3V0wa4veksg== X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:de70:7033:67f7:8d0b]) (user=howardchung job=sendgmr) by 2002:a62:dd12:0:b029:314:96ff:4e9b with SMTP id w18-20020a62dd120000b029031496ff4e9bmr29394060pff.71.1625725454692; Wed, 07 Jul 2021 23:24:14 -0700 (PDT) Date: Thu, 8 Jul 2021 14:23:14 +0800 In-Reply-To: <20210708062314.245754-1-howardchung@google.com> Message-Id: <20210708142059.Bluez.v1.14.Ib24a67a8a849f311d5213f83eaac3cfbc54b7b58@changeid> Mime-Version: 1.0 References: <20210708062314.245754-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [Bluez PATCH v1 14/14] core: fix a possible crash when removing devices From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This patch changes the logic of probe_service so that the same service will not be added to a device. --- The crash can be reproduced in the following steps 1. set service allowlist to ['aaaa'] 2. pair with any device 3. after the device is disconnected, set service allowlist to an empty list 4. remove the device from adapter In step 3, when allowlist is set to empty, profile that was blocked will be added to each devices. However, in step 2, profiles the device provides had already been added. Due the logic of device.c:probe_service, there will be 2 identical services in device->services, which causes a double-free error when removing the device. src/device.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/device.c b/src/device.c index 0d7444706336..dba26f787066 100644 --- a/src/device.c +++ b/src/device.c @@ -4709,8 +4709,11 @@ static struct btd_service *probe_service(struct btd_device *device, return NULL; l = find_service_with_profile(device->services, profile); + /* If the service already exists, return NULL so that it won't be added + * to the device->services. + */ if (l) - return l->data; + return NULL; service = service_create(device, profile);