]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
ocp: fix to check hwcomp log size if valid
authorTokunori Ikegami <ikegami.t@gmail.com>
Sun, 29 Dec 2024 12:01:05 +0000 (21:01 +0900)
committerDaniel Wagner <wagi@monom.org>
Tue, 7 Jan 2025 08:27:07 +0000 (09:27 +0100)
Since the log size value needed to reduce the descriptor length.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
plugins/ocp/ocp-hardware-component-log.c

index 4dfc61e4433139e7440f542377ac26d7538f0f3d..3c6890af835f33aed390ba52f74e299b67387509 100644 (file)
@@ -171,6 +171,9 @@ static int get_hwcomp_log_data(struct nvme_dev *dev, struct hwcomp_log *log)
 {
        int ret = 0;
        size_t desc_offset = offsetof(struct hwcomp_log, desc);
+       long double log_bytes;
+       nvme_uint128_t log_size;
+
        struct nvme_get_log_args args = {
                .args_size = sizeof(args),
                .fd = dev_fd(dev),
@@ -193,16 +196,25 @@ static int get_hwcomp_log_data(struct nvme_dev *dev, struct hwcomp_log *log)
        }
 #endif /* HWCOMP_DUMMY */
 
+       log_size = le128_to_cpu(log->size);
+
        print_info("id: %02Xh\n", OCP_LID_HWCOMP);
        print_info("version: %04Xh\n", log->ver);
        print_info_array("guid", log->guid, ARRAY_SIZE(log->guid));
-       print_info("size: %s\n", uint128_t_to_string(le128_to_cpu(log->size)));
+       print_info("size: %s\n", uint128_t_to_string(log_size));
+
+       log_bytes = uint128_t_to_double(log_size);
+       if (log->ver == 1)
+               log_bytes *= sizeof(__le32);
+
+       if (log_bytes <= desc_offset) {
+               print_info_error("error: ocp: invalid hwcomp log size bytes: %.0Lf\n", log_bytes);
+               return -EINVAL;
+       }
+
+       args.len = log_bytes - desc_offset;
 
-       if (log->ver > 1)
-               args.len = uint128_t_to_double(le128_to_cpu(log->size)) - desc_offset;
-       else
-               args.len = uint128_t_to_double(le128_to_cpu(log->size)) * sizeof(__le32)
-                       - desc_offset;
+       print_info("args.len: %u\n", args.len);
 
        log->desc = calloc(1, args.len);
        if (!log->desc) {