Skip to content

Commit 348c713

Browse files
kjain101mpe
authored andcommitted
powerpc/papr_scm: Fix buffer overflow issue with CONFIG_FORTIFY_SOURCE
With CONFIG_FORTIFY_SOURCE enabled, string functions will also perform dynamic checks for string size which can panic the kernel, like incase of overflow detection. In papr_scm, papr_scm_pmu_check_events function uses stat->stat_id with string operations, to populate the nvdimm_events_map array. Since stat_id variable is not NULL terminated, the kernel panics with CONFIG_FORTIFY_SOURCE enabled at boot time. Below are the logs of kernel panic: detected buffer overflow in __fortify_strlen ------------[ cut here ]------------ kernel BUG at lib/string_helpers.c:980! Oops: Exception in kernel mode, sig: 5 [rib#1] NIP [c00000000077dad0] fortify_panic+0x28/0x38 LR [c00000000077dacc] fortify_panic+0x24/0x38 Call Trace: [c0000022d77836e0] [c00000000077dacc] fortify_panic+0x24/0x38 (unreliable) [c00800000deb2660] papr_scm_pmu_check_events.constprop.0+0x118/0x220 [papr_scm] [c00800000deb2cb0] papr_scm_probe+0x288/0x62c [papr_scm] [c0000000009b46a8] platform_probe+0x98/0x150 Fix this issue by using kmemdup_nul() to copy the content of stat->stat_id directly to the nvdimm_events_map array. mpe: stat->stat_id comes from the hypervisor, not userspace, so there is no security exposure. Fixes: 4c08d4b ("powerpc/papr_scm: Add perf interface support") Signed-off-by: Kajol Jain <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6d65028 commit 348c713

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

arch/powerpc/platforms/pseries/papr_scm.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ static int papr_scm_pmu_check_events(struct papr_scm_priv *p, struct nvdimm_pmu
462462
{
463463
struct papr_scm_perf_stat *stat;
464464
struct papr_scm_perf_stats *stats;
465-
char *statid;
466465
int index, rc, count;
467466
u32 available_events;
468467

@@ -493,14 +492,12 @@ static int papr_scm_pmu_check_events(struct papr_scm_priv *p, struct nvdimm_pmu
493492

494493
for (index = 0, stat = stats->scm_statistic, count = 0;
495494
index < available_events; index++, ++stat) {
496-
statid = kzalloc(strlen(stat->stat_id) + 1, GFP_KERNEL);
497-
if (!statid) {
495+
p->nvdimm_events_map[count] = kmemdup_nul(stat->stat_id, 8, GFP_KERNEL);
496+
if (!p->nvdimm_events_map[count]) {
498497
rc = -ENOMEM;
499498
goto out_nvdimm_events_map;
500499
}
501500

502-
strcpy(statid, stat->stat_id);
503-
p->nvdimm_events_map[count] = statid;
504501
count++;
505502
}
506503
p->nvdimm_events_map[count] = NULL;

0 commit comments

Comments
 (0)