Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3467804ybg; Sun, 20 Oct 2019 14:30:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqx9lsis3U0wJl2gMvX/VUJzscOhm0faBd7+XVZroF2oTkZU3fOH9xVFGd6mP2lOs17w8COn X-Received: by 2002:a17:906:6b88:: with SMTP id l8mr19146366ejr.26.1571607033843; Sun, 20 Oct 2019 14:30:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571607033; cv=none; d=google.com; s=arc-20160816; b=K0eLYr/QRcCkBUBpL56MO45JbOJWbR1BPgYzP/cAJJdjKCOZzuwANqdQ23pk31wmLY 1ukORyyfS7kFzmZA0e6O7mLf+9anDDNObunAF8bECErIhYwbDToQdrRUhP525H/nlgI5 oAjDbePb6vzJ4a5x0wAhHPl8S68bwmdqjeW/EcYUg9k6tjm1Y4tFUQk9f4NGBRlwPssL xjypmyveSBEsFsuHQPitkAjlnMgCdeobeganhy4pe1J0ArmWJAqttV+8ZUTmdZvX4jWm C10YFJChF3Kq+O8g9r/xrnUo67cQNEF9lZgOU9n5cTYeAYqHTcK55Df7ljWlUQChkmy4 wGAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=bKmABUOGYxHZwfCNCpnxZXziVA+bfGADxFZxWfZSs8Y=; b=I9IYDfNyTiydbatq6Ruoug4hCTxMlm8rHWEgKRciI2yW6qYnECDfBwEWOwBtJlpK49 5ZfwLgeU6OkVa8MECZa8oOXSMuX/eyOrmQg+JrdXwBKJ31VVDdTQsv+CxyXn+CDTxmB6 yQvinpWIwynU0UqDLN9Il/Y30Nauxm/g4Xjqa9X5EN+NIkzCeX1D4KCSbYfWCnLj9YJy 6JNPHLfLl+2HvHdaPticYyMRPhr/MxtyK8XW9U0o1OVn/6xzm9BtRoRxfRisICkQ70B3 8QweYYxxzVV6O/dreTqF1JZJRHnfk+hABG8HXPrqslQorKv5gXagZrihGc4p62P7A43z mHyg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i11si7408670eja.317.2019.10.20.14.29.58; Sun, 20 Oct 2019 14:30:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726374AbfJTV3v (ORCPT + 99 others); Sun, 20 Oct 2019 17:29:51 -0400 Received: from mga14.intel.com ([192.55.52.115]:27478 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726301AbfJTV3v (ORCPT ); Sun, 20 Oct 2019 17:29:51 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2019 14:29:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,320,1566889200"; d="scan'208";a="398477997" Received: from achakked-mobl2.amr.corp.intel.com (HELO ingas-nuc1.sea.intel.com) ([10.254.104.9]) by fmsmga006.fm.intel.com with ESMTP; 20 Oct 2019 14:29:50 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ v2] mesh: Fix reading/writing key indices Date: Sun, 20 Oct 2019 14:29:49 -0700 Message-Id: <20191020212949.21492-1-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This fixes inconsistency when reading/writing NetKey and AppKey indices to/from JSON config storage: - when writing, convert an integer to hex string - when reading, convert hex string to uint16 integer --- mesh/mesh-config-json.c | 67 ++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c index df58cbd7d..865fbdf07 100644 --- a/mesh/mesh-config-json.c +++ b/mesh/mesh-config-json.c @@ -45,7 +45,7 @@ #define MIN_SEQ_CACHE_VALUE (2 * 32) #define MIN_SEQ_CACHE_TIME (5 * 60) -#define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095)) +#define CHECK_KEY_IDX_RANGE(x) ((x) <= 4095) struct mesh_config { json_object *jnode; @@ -263,13 +263,16 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx) for (i = 0; i < sz; ++i) { json_object *jentry, *jvalue; - uint32_t jidx; + const char *str; + uint16_t jidx; jentry = json_object_array_get_idx(jarray, i); if (!json_object_object_get_ex(jentry, "index", &jvalue)) return NULL; - jidx = json_object_get_int(jvalue); + str = json_object_get_string(jvalue); + if (sscanf(str, "%04hx", &jidx) != 1) + return NULL; if (jidx == idx) return jentry; @@ -278,6 +281,28 @@ static json_object *get_key_object(json_object *jarray, uint16_t idx) return NULL; } +static bool get_key_index(json_object *jobj, const char *keyword, + uint16_t *index) +{ + uint16_t idx; + json_object *jvalue; + const char *str; + + if (!json_object_object_get_ex(jobj, keyword, &jvalue)) + return false; + + str = json_object_get_string(jvalue); + + if (sscanf(str, "%04hx", &idx) != 1) + return false; + + if (!CHECK_KEY_IDX_RANGE(idx)) + return false; + + *index = (uint16_t) idx; + return true; +} + static json_object *jarray_key_del(json_object *jarray, int16_t idx) { json_object *jarray_new; @@ -288,16 +313,13 @@ static json_object *jarray_key_del(json_object *jarray, int16_t idx) return NULL; for (i = 0; i < sz; ++i) { - json_object *jentry, *jvalue; + json_object *jentry; + uint16_t nidx; jentry = json_object_array_get_idx(jarray, i); - if (json_object_object_get_ex(jentry, "index", &jvalue)) { - int tmp = json_object_get_int(jvalue); - - if (tmp == idx) - continue; - } + if (get_key_index(jentry, "index", &nidx) && nidx == idx) + continue; json_object_get(jentry); json_object_array_add(jarray_new, jentry); @@ -419,21 +441,6 @@ static bool read_device_key(json_object *jobj, uint8_t key_buf[16]) return true; } -static bool get_key_index(json_object *jobj, const char *keyword, - uint16_t *index) -{ - int idx; - - if (!get_int(jobj, keyword, &idx)) - return false; - - if (!CHECK_KEY_IDX_RANGE(idx)) - return false; - - *index = (uint16_t) idx; - return true; -} - static bool read_app_keys(json_object *jobj, struct mesh_config_node *node) { json_object *jarray; @@ -570,6 +577,7 @@ bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx, jnode = cfg->jnode; + l_debug("netKey %4.4x", idx); json_object_object_get_ex(jnode, "netKeys", &jarray); if (jarray) jentry = get_key_object(jarray, idx); @@ -965,14 +973,19 @@ static bool parse_bindings(json_object *jarray, struct mesh_config_model *mod) mod->bindings = l_new(uint16_t, cnt); for (i = 0; i < cnt; ++i) { - int idx; + uint16_t idx; + const char *str; json_object *jvalue; jvalue = json_object_array_get_idx(jarray, i); if (!jvalue) return false; - idx = json_object_get_int(jvalue); + str = json_object_get_string(jvalue); + + if (sscanf(str, "%04hx", &idx) != 1) + return false; + if (!CHECK_KEY_IDX_RANGE(idx)) return false; -- 2.21.0