]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
json: Verify JSON config file starts with an array
authorDaniel Wagner <dwagner@suse.de>
Fri, 30 Sep 2022 10:30:03 +0000 (12:30 +0200)
committerDaniel Wagner <dwagner@suse.de>
Fri, 30 Sep 2022 10:35:41 +0000 (12:35 +0200)
Do not blindly assume the file starts with an array. This avoids a
crash in json-c.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
src/nvme/json.c

index 38ac1be2305ff8241dde0a793e7fa2ade341f2d5..f0c2ab4f38043ede2bd4ed099707ece2c56b7c05 100644 (file)
@@ -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;
 }