Skip to content

Commit 66691e2

Browse files
veygaxlumag
authored andcommitted
drm/msm: Replace unsafe snprintf usage with scnprintf
The refill_buf function uses snprintf to append to a fixed-size buffer. snprintf returns the length that would have been written, which can exceed the remaining buffer size. If this happens, ptr advances beyond the buffer and rem becomes negative. In the 2nd iteration, rem is treated as a large unsigned integer, causing snprintf to write oob. While this behavior is technically mitigated by num_perfcntrs being locked at 5, it's still unsafe if num_perfcntrs were ever to change/a second source was added. Signed-off-by: Evan Lambert <veyga@veygax.dev> Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com> Patchwork: https://patchwork.freedesktop.org/patch/696358/ Link: https://lore.kernel.org/r/20251224124254.17920-3-veyga@veygax.dev Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>
1 parent 88733a0 commit 66691e2

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

drivers/gpu/drm/msm/msm_perf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ static int refill_buf(struct msm_perf_state *perf)
6565

6666
if ((perf->cnt++ % 32) == 0) {
6767
/* Header line: */
68-
n = snprintf(ptr, rem, "%%BUSY");
68+
n = scnprintf(ptr, rem, "%%BUSY");
6969
ptr += n;
7070
rem -= n;
7171

7272
for (i = 0; i < gpu->num_perfcntrs; i++) {
7373
const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
74-
n = snprintf(ptr, rem, "\t%s", perfcntr->name);
74+
n = scnprintf(ptr, rem, "\t%s", perfcntr->name);
7575
ptr += n;
7676
rem -= n;
7777
}
@@ -93,21 +93,21 @@ static int refill_buf(struct msm_perf_state *perf)
9393
return ret;
9494

9595
val = totaltime ? 1000 * activetime / totaltime : 0;
96-
n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
96+
n = scnprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
9797
ptr += n;
9898
rem -= n;
9999

100100
for (i = 0; i < ret; i++) {
101101
/* cycle counters (I think).. convert to MHz.. */
102102
val = cntrs[i] / 10000;
103-
n = snprintf(ptr, rem, "\t%5d.%02d",
103+
n = scnprintf(ptr, rem, "\t%5d.%02d",
104104
val / 100, val % 100);
105105
ptr += n;
106106
rem -= n;
107107
}
108108
}
109109

110-
n = snprintf(ptr, rem, "\n");
110+
n = scnprintf(ptr, rem, "\n");
111111
ptr += n;
112112
rem -= n;
113113

0 commit comments

Comments
 (0)