From: Daniel Wagner Date: Fri, 30 Sep 2022 10:30:03 +0000 (+0200) Subject: json: Verify JSON config file starts with an array X-Git-Tag: v1.2~16^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=15b5855d234b505a636e830784d401fc7c127e84;p=users%2Fsagi%2Flibnvme.git json: Verify JSON config file starts with an array Do not blindly assume the file starts with an array. This avoids a crash in json-c. Signed-off-by: Daniel Wagner --- diff --git a/src/nvme/json.c b/src/nvme/json.c index 38ac1be2..f0c2ab4f 100644 --- a/src/nvme/json.c +++ b/src/nvme/json.c @@ -207,9 +207,15 @@ int json_read_config(nvme_root_t r, const char *config_file) return fd; } json_root = parse_json(r, fd); + close(fd); if (!json_root) { errno = EPROTO; - close(fd); + return -1; + } + if (!json_object_is_type(json_root, json_type_array)) { + nvme_msg(r, LOG_DEBUG, "Wrong format, expected array\n"); + json_object_put(json_root); + errno = EPROTO; return -1; } for (h = 0; h < json_object_array_length(json_root); h++) { @@ -218,7 +224,6 @@ int json_read_config(nvme_root_t r, const char *config_file) json_parse_host(r, host_obj); } json_object_put(json_root); - close(fd); return 0; }