Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1610262pxb; Fri, 6 Nov 2020 14:26:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2+4I51N4jeveqz3EovsEUPHspXvZgAtt5N9I+mGJgRoyLQQnMonC0wrYE/vVZkkvuvDkq X-Received: by 2002:a05:6402:3064:: with SMTP id bs4mr4571934edb.258.1604701596798; Fri, 06 Nov 2020 14:26:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604701596; cv=none; d=google.com; s=arc-20160816; b=0bJVbNO8OX7Rza3+Q4mJ8C6Qlj6fzw62sZeNTA3EGpNnUyN9uYfKgGoiuctrMQ2ktp Amn6u4Pl3H4ahoTo6nzZ9Eol/Gi65OxtaXDaXUudYIJGccLusM0YHtpoFJQTY0gQCV3L bysJwDZJ/Qe6xCy0NCTEOVx1/8j1QKyoSOsvwr2AYdADLy8zPFvT83aobOz4Ozcvynqz vmqpSmNf4Y1sRaiHGFAEeTEQnaGoowuOeVob7w7WWSiTf3mv05Z+uNc/alVFIf4Xd3/y wi0QRBQlC6ATzf1UKpwOQDhzsDjWL5/BctNkSy1rk14oKDOU9ILAGPMW6SSwJdPkblLj XWVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=Y+ElpTaor0yvsymeF4BTcy7WU1PCj7jW1SsgPnl/MQs=; b=yZo13ypuKQJ+LG1m5/xaaUb8tBJJ6BuTNNocybLDmmH2VnneGMGg3mV/dGkJQZo0oB sb+K5nOVNWcoazPeI+V3aPG3Ni3wKwbJRTHG+1rkzwqM9leoxsnlWXXFM+puns/kfTbR ocaUqGPnH2lWx3kNaZ2Gmfn+Rs7w1smUQvdCW845+4r7oNhd73h8UhI6YmVazVUKsJ/R XmD6bmtCK9RSOGI/GwI5pvy9z0ZBeSiJXYhG48yzWeS1dac6CxfiUT3Vvf8HUJBNg7nO 5derIgl+WfSuS7fPpnrTY8+Uar/porYBgayHB3QlENHLEwHbbA/l2mGyZ5z8ISSLNliD cYCA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a12si144985edf.2.2020.11.06.14.25.53; Fri, 06 Nov 2020 14:26:36 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728828AbgKFWWz (ORCPT + 99 others); Fri, 6 Nov 2020 17:22:55 -0500 Received: from mga12.intel.com ([192.55.52.136]:51363 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728831AbgKFWWy (ORCPT ); Fri, 6 Nov 2020 17:22:54 -0500 IronPort-SDR: lDBlG4a219rlbryTquIOgAuq1hdo32hXqrLYYPW26ovJnkIZuJ7f9U/DKMhNhU53k3uOo4u009 QGU0proU11cA== X-IronPort-AV: E=McAfee;i="6000,8403,9797"; a="148888570" X-IronPort-AV: E=Sophos;i="5.77,457,1596524400"; d="scan'208";a="148888570" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Nov 2020 14:22:54 -0800 IronPort-SDR: /SRzxm8sZNlOi2G1KZsXCJM2yQiKDi7bcsWBpocz3wJePcBLOaFMXm5PU9ySAeFb3Rv6dPCZ04 xjiUrQ7/so7Q== X-IronPort-AV: E=Sophos;i="5.77,457,1596524400"; d="scan'208";a="472218718" Received: from davidche-mobl1.amr.corp.intel.com (HELO ingas-nuc1.intel.com) ([10.213.177.187]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Nov 2020 14:22:54 -0800 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ] mesh/mesh-config-json: Fix errors found by static analysis Date: Fri, 6 Nov 2020 14:22:45 -0800 Message-Id: <20201106222245.137189-1-inga.stotland@intel.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This fixes miscellaneous issues related to unchecked return status and (temporary) allocated memory leaks due to daemon's exit on startup failure. --- mesh/mesh-config-json.c | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c index ec9ecb786..f142f0e1f 100644 --- a/mesh/mesh-config-json.c +++ b/mesh/mesh-config-json.c @@ -475,6 +475,7 @@ static bool read_app_keys(json_object *jobj, struct mesh_config_node *node) struct mesh_config_appkey *appkey; appkey = l_new(struct mesh_config_appkey, 1); + l_queue_push_tail(node->appkeys, appkey); jtemp = json_object_array_get_idx(jarray, i); @@ -496,8 +497,6 @@ static bool read_app_keys(json_object *jobj, struct mesh_config_node *node) if (!str2hex(str, strlen(str), appkey->key, 16)) goto fail; - - l_queue_push_tail(node->appkeys, appkey); } return true; @@ -532,6 +531,8 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node) netkey = l_new(struct mesh_config_netkey, 1); + l_queue_push_tail(node->netkeys, netkey); + jtemp = json_object_array_get_idx(jarray, i); if (!get_key_index(jtemp, "index", &netkey->idx)) @@ -561,8 +562,6 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node) if (!str2hex(str, strlen(str), netkey->key, 16)) goto fail; - - l_queue_push_tail(node->netkeys, netkey); } return true; @@ -1036,7 +1035,7 @@ static bool parse_model_subscriptions(json_object *jsubs, jvalue = json_object_array_get_idx(jsubs, i); if (!jvalue) - return false; + goto fail; str = (char *)json_object_get_string(jvalue); len = strlen(str); @@ -1045,7 +1044,7 @@ static bool parse_model_subscriptions(json_object *jsubs, case 4: if (sscanf(str, "%04hx", &subs[i].addr.grp) != 1) goto fail; - break; + break; case 32: if (!str2hex(str, len, subs[i].addr.label, 16)) goto fail; @@ -1086,6 +1085,8 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele) mod = l_new(struct mesh_config_model, 1); + l_queue_push_tail(ele->models, mod); + if (!json_object_object_get_ex(jmodel, "modelId", &jvalue)) goto fail; @@ -1093,13 +1094,9 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele) len = strlen(str); - if (len != 4 && len != 8) - goto fail; - if (len == 4) { if (sscanf(str, "%04x", &id) != 1) goto fail; - } else if (len == 8) { if (sscanf(str, "%08x", &id) != 1) goto fail; @@ -1135,8 +1132,6 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele) if (!parse_model_subscriptions(jarray, mod)) goto fail; } - - l_queue_push_tail(ele->models, mod); } return true; @@ -1584,16 +1579,19 @@ static void add_model(void *a, void *b) { struct mesh_config_model *mod = a; json_object *jmodels = b, *jmodel, *jval; + bool result; jmodel = json_object_new_object(); if (!jmodel) return; - if (!mod->vendor) - write_uint16_hex(jmodel, "modelId", - (uint16_t) mod->id); - else - write_uint32_hex(jmodel, "modelId", mod->id); + result = (mod->vendor) ? write_uint32_hex(jmodel, "modelId", mod->id) : + write_uint16_hex(jmodel, "modelId", (uint16_t) mod->id); + + if (!result) { + json_object_put(jmodel); + return; + } jval = json_object_new_boolean(mod->sub_enabled); json_object_object_add(jmodel, "subEnabled", jval); @@ -1671,24 +1669,24 @@ static struct mesh_config *create_config(const char *cfg_path, jelement = json_object_new_object(); - if (!jelement) { - json_object_put(jelems); - return NULL; - } + if (!jelement) + goto fail; - write_int(jelement, "elementIndex", ele->index); - write_uint16_hex(jelement, "location", ele->location); json_object_array_add(jelems, jelement); + if (!write_int(jelement, "elementIndex", ele->index)) + goto fail; + + if (!write_uint16_hex(jelement, "location", ele->location)) + goto fail; + /* Models */ if (l_queue_isempty(ele->models)) continue; jmodels = json_object_new_array(); - if (!jmodels) { - json_object_put(jelems); - return NULL; - } + if (!jmodels) + goto fail; json_object_object_add(jelement, "models", jmodels); l_queue_foreach(ele->models, add_model, jmodels); @@ -1706,6 +1704,10 @@ static struct mesh_config *create_config(const char *cfg_path, gettimeofday(&cfg->write_time, NULL); return cfg; + +fail: + json_object_put(jelems); + return NULL; } struct mesh_config *mesh_config_create(const char *cfgdir_name, @@ -2404,8 +2406,10 @@ static void idle_save_config(struct l_idle *idle, void *user_data) if (result) { remove(fname_bak); - rename(fname_cfg, fname_bak); - rename(fname_tmp, fname_cfg); + + if (rename(fname_cfg, fname_bak) < 0 || + rename(fname_tmp, fname_cfg) < 0) + result = false; } remove(fname_tmp); -- 2.26.2