From b798c9d59681c9304f24f28f4db4065530f375d8 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Sun, 29 Dec 2024 21:01:05 +0900 Subject: [PATCH] ocp: fix to check hwcomp log size if valid Since the log size value needed to reduce the descriptor length. Signed-off-by: Tokunori Ikegami --- plugins/ocp/ocp-hardware-component-log.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/ocp/ocp-hardware-component-log.c b/plugins/ocp/ocp-hardware-component-log.c index 4dfc61e443..3c6890af83 100644 --- a/plugins/ocp/ocp-hardware-component-log.c +++ b/plugins/ocp/ocp-hardware-component-log.c @@ -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) {