Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp319047pxv; Thu, 22 Jul 2021 00:25:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoKKApWXgr977SjgFVJPNafxKtQCpwpXkDIc39o2p7q3WqAGYYtcNJnpqiAgdAOGPN9DqF X-Received: by 2002:a5e:a617:: with SMTP id q23mr13401634ioi.54.1626938714925; Thu, 22 Jul 2021 00:25:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626938714; cv=none; d=google.com; s=arc-20160816; b=q6XQEsJQtT8HWKwIKLc4LI6aW8vLnh0MWXLYM3lMZGTWaluLyG2KORS8pww6VxX7G9 W47FaADB3EakP/s/46OfKubDjCOWZ9ueuoR+wxSejQ3jVZBtKSeoYLnOfegVfYstCwfP ViAP+J9TiVzjSbJI3Dzs8P4r1eCzzRvHum5PCLJlHd8KYB/ZJl8dcvgmvBMfWXIYUZUj tWZgGqYuMWDQou9rWVK7ecNmiZty5zfEZKiHdNi4WnnoLctw8IlL0QC3MLWNI0okudAi tlDgbxwAwvFnQWk9pRzL26FsyYB2sUZAk6Swib/+S72zx6vtER3QhTpdukq7HiQjPLjf SdYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=3RzARyeA9VtTGtAa3E7y9kePca8BVndJk3b2KzsXxHU=; b=IEzEUKuEz1WfQ0W8/b7izdKHtUr5IzVU7FXRI118cpcNMv1eUi++xls9UVij970uYP c4bZOZ4gLtdSEB5lhkOxyEA4SLrPNvoR0QJ6c/pVjsWn1ecqzK4PwN7r+wqHre0ho/3/ 2Yt0lvYr6r/oJ9DyNv51GBlJXawMWxHnuo98+7ghY8kqhA+Ue5OkKrfmASfOxEiJa7CM 0IlMxKxYJsU3h/2TTcijBNSy0tnq88FEqYZ5QjR4NHY7akzMGL9vuE7D97B9BjrTnRrZ jlxF//YyYlbaAQ7tiXl0taUL3A8pGsx9/SH8lLbtdWOtczyglWODsO2e/+le8UtJ5BAM MiUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AbWcmIR1; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l3si28905981ils.48.2021.07.22.00.25.02; Thu, 22 Jul 2021 00:25:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AbWcmIR1; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230473AbhGVGnx (ORCPT + 99 others); Thu, 22 Jul 2021 02:43:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbhGVGnq (ORCPT ); Thu, 22 Jul 2021 02:43:46 -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 71AA7C0613C1 for ; Thu, 22 Jul 2021 00:23:48 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id b127-20020a3799850000b02903b960837cbfso3497681qke.10 for ; Thu, 22 Jul 2021 00:23:48 -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=3RzARyeA9VtTGtAa3E7y9kePca8BVndJk3b2KzsXxHU=; b=AbWcmIR150vCA4URhfHrLZLe6XJMrU4Bowi8ru7HnYiaG6n+8pHLe7IlX9gvOHJHUe ouoksfx/z/fjYV2akaIQ6QOoPL9deAYh5O3Jb1dDqZWNCdzpA5YMdUb+NVdjNal8WOAx PN12E8w2VSQaaKO7HDyVYBGY4IF0W8oAgQgsQH3RpBtpRSQJS1llYEcDQviYwUxBtseh gNKFWpcomTSt2ektsz7GJsMEZtw7x3IFhWtIhY5HgRluNDooCingeKAnBCRki2ZSwaBc yMSlnG6VpXYXBa/Vti4JSnOR6SiYQanRs2gPpwqtXnzPOAgipsR3rxF9s6JnThigm5WY mR0Q== 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=3RzARyeA9VtTGtAa3E7y9kePca8BVndJk3b2KzsXxHU=; b=n11TNtEaGGJU0zlvy1FgP6hTMxFnPMQhHdYETBQjt4cTcVmuC/m7kNjvQsA4gZ2PUP IYrvRmrU5WZXZcmZlKAE2zyNA4mNbeJAQkMJ/CRjfh1Mkt1kNC+kF/9wfdOhzSd9SV6J 9Cx2sCnnIdQSqaWoQDxl2psOAgYjh7jDKOyBDcMM2CUZukJmnjrp8X6d/A1Ur5dBG+Xh fy6qu9NJe8HdxFveugkx0hA00SnWEBRMe9qYUD+2JkL+s3Dtlz83WOBqB6x2wm9WEupE vj5eHarixAQIaLQ++XCpnxcOr/rsLoXgZM2j/Mphyh1xMiZzIbidLXP996xaOwK499tY qffQ== X-Gm-Message-State: AOAM532ddL/aaSsDeBuWxqF3SP0lfREobKpceRdgwBtsQI6p1eTul++h 32uCWQym07/RfCG+WNc42DQeuvxf853O1dbYzBq5miQFO6/ecgK7dCXgeUanj1h2rBsu66sMXJX npbo2Qr7PwUaA4CgvKPnBmEGl/20+wDB54ltT9uwqovbhVigeVtkKgb4oOQd+1Vyz4/1XNJsZtZ pfrqU+x6m1nas= X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:69a3:595f:8267:f7e0]) (user=howardchung job=sendgmr) by 2002:a05:6214:27e7:: with SMTP id jt7mr40384914qvb.28.1626938627453; Thu, 22 Jul 2021 00:23:47 -0700 (PDT) Date: Thu, 22 Jul 2021 15:23:16 +0800 In-Reply-To: <20210722072321.1225119-1-howardchung@google.com> Message-Id: <20210722152159.Bluez.v2.6.Ifbb69dd6e371da3a914049a94615064479b9024b@changeid> Mime-Version: 1.0 References: <20210722072321.1225119-1-howardchung@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [Bluez PATCH v2 06/11] 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 Content-Type: text/plain; charset="UTF-8" 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 ["1108","110A","110B","110C","110D","110E", "110F","1112","111E","111F","1203"] ( 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. (no changes since v1) 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); } -- 2.32.0.402.g57bb445576-goog