Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp170161pxv; Wed, 7 Jul 2021 23:25:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkYT5juJuUc+APy0e1zQLZJqBT556LgQE1XdhpU1Fb0I4nUnwT0hxL1UzQzmhCLdx6GZW7 X-Received: by 2002:a17:906:974e:: with SMTP id o14mr18611290ejy.122.1625725550222; Wed, 07 Jul 2021 23:25:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625725550; cv=none; d=google.com; s=arc-20160816; b=s6efnOgfgq9bNYcuOE8u5Ri7TC7WDDjzpPJoJQI0sYUluMMbjPQ2FGyZ6m1XskEklD eygmbZb0AgBlY6Z8qSn9wLqSxUW+ZjEmw4ZuaL2/obyuuTu9KDW6+hQSAz3lRCapSFBy klie4eKovnykpecqpOwnDtCd59QbOxJ4WyvbeLhrCIlZ2nExTQZeOyL+t6CFzv31LPZO ODZ6rJpeEsUS/NlKMbMLPTGrHGGjGZh+mBm1lxn6L3wsWvDNequ2FcJMtauv19R31/hq N83wg/OildXDTuJ//4X9YMQ5ngMERtxRH8RNBR1q2Zoj+2fid8W8hZpshcxU6WTECzcA RKrw== 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=0SBfrOcBIQ93k3CvJxMiKTB55WCmnlBqLqaw3gB6WrI=; b=VKtRz8cFpdmtTNAFn0nYRBPWTG7WLJggElguypdMdDduq2iCSwqHQhupgK+wh5kIDK B6o9Nd6N7/F6/LYlwFKA5gkLdg6UxMmfCRD6lq6Ls+F+wOaK0stkAlpqwefuSR0VQDdK 4n0KpES95lrRzCdVTukzNyGWleVuBYpPEds/MX2OQ9Z8fZHwbxwb1hhtFgSmyUQ3mNyA EnI1getevI5aWOSO0IZr1K3udEoq5xCilYF5t/yW3ViKIw1G959G0oWsxYRkkpgZIT7N 5n694mR1W0wcHduWKcUik+bfUW6qxg1I/VeSGXxAuaNsJ4JPqYP5XmAmgi4vzv2YQ9CN HbgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=c+TT0zjE; 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 ho15si1950531ejc.49.2021.07.07.23.25.13; Wed, 07 Jul 2021 23:25:50 -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=c+TT0zjE; 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 S229863AbhGHG0y (ORCPT + 99 others); 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-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 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 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, -- 2.32.0.93.g670b81a890-goog