Skip to content

Commit 80ccd40

Browse files
shravankumarrjwrdegoede
authored andcommitted
platform/mellanox: mlxbf-pmc: Fix potential buffer overflows
Replace sprintf with sysfs_emit where possible. Size check in mlxbf_pmc_event_list_show should account for "\0". Fixes: 1a218d3 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver") Signed-off-by: Shravan Kumar Ramani <shravankr@nvidia.com> Reviewed-by: Vadim Pasternak <vadimp@nvidia.com> Reviewed-by: David Thompson <davthompson@nvidia.com> Link: https://lore.kernel.org/r/bef39ef32319a31b32f999065911f61b0d3b17c3.1693917738.git.shravankr@nvidia.com Signed-off-by: Hans de Goede <hdegoede@redhat.com>
1 parent fc4c655 commit 80ccd40

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

drivers/platform/mellanox/mlxbf-pmc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ static ssize_t mlxbf_pmc_counter_show(struct device *dev,
10081008
} else
10091009
return -EINVAL;
10101010

1011-
return sprintf(buf, "0x%llx\n", value);
1011+
return sysfs_emit(buf, "0x%llx\n", value);
10121012
}
10131013

10141014
/* Store function for "counter" sysfs files */
@@ -1078,13 +1078,13 @@ static ssize_t mlxbf_pmc_event_show(struct device *dev,
10781078

10791079
err = mlxbf_pmc_read_event(blk_num, cnt_num, is_l3, &evt_num);
10801080
if (err)
1081-
return sprintf(buf, "No event being monitored\n");
1081+
return sysfs_emit(buf, "No event being monitored\n");
10821082

10831083
evt_name = mlxbf_pmc_get_event_name(pmc->block_name[blk_num], evt_num);
10841084
if (!evt_name)
10851085
return -EINVAL;
10861086

1087-
return sprintf(buf, "0x%llx: %s\n", evt_num, evt_name);
1087+
return sysfs_emit(buf, "0x%llx: %s\n", evt_num, evt_name);
10881088
}
10891089

10901090
/* Store function for "event" sysfs files */
@@ -1139,9 +1139,9 @@ static ssize_t mlxbf_pmc_event_list_show(struct device *dev,
11391139
return -EINVAL;
11401140

11411141
for (i = 0, buf[0] = '\0'; i < size; ++i) {
1142-
len += sprintf(e_info, "0x%x: %s\n", events[i].evt_num,
1143-
events[i].evt_name);
1144-
if (len > PAGE_SIZE)
1142+
len += snprintf(e_info, sizeof(e_info), "0x%x: %s\n",
1143+
events[i].evt_num, events[i].evt_name);
1144+
if (len >= PAGE_SIZE)
11451145
break;
11461146
strcat(buf, e_info);
11471147
ret = len;
@@ -1168,7 +1168,7 @@ static ssize_t mlxbf_pmc_enable_show(struct device *dev,
11681168

11691169
value = FIELD_GET(MLXBF_PMC_L3C_PERF_CNT_CFG_EN, perfcnt_cfg);
11701170

1171-
return sprintf(buf, "%d\n", value);
1171+
return sysfs_emit(buf, "%d\n", value);
11721172
}
11731173

11741174
/* Store function for "enable" sysfs files - only for l3cache */

0 commit comments

Comments
 (0)