x86/RAS/mce_amd_inj: fix signed wrap around when decrementing index i
PATCH v2 1/1 x86/RAS/mce_amd_inj: fix signed wrap around when decrementing index i
From: Colin Ian King <[email protected]>
Integer index i needs to be a signed int rather than unsigned to avoid a wrap-around when decrementing in the while loop. For example, if the debugfs_create_file fails when i is zero, the current situation will predecrement i in the while loop, wrapping i to the maximum signed integer and cause multiple out of bounds reads on dfs_fls[i].d as the loop interates to zero.
Also add (int) cast to fix warning that the original fix attempted to fix.
--- arch/x86/ras/mce_amd_inj.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/ras/mce_amd_inj.c +2 -2
@@ -440,7 +440,7 @@ static struct dfs_node {static int __init init_mce_inject (void ) { -unsigned int i; +int i;u64 cap;rdmsrl (MSR_IA32_MCG_CAP , cap);
@@ -450,7 +450,7 @@ static int __init init_mce_inject(void)if (!dfs_inj)return -EINVAL ; -for (i =0 ; i <ARRAY_SIZE (dfs_fls); i++) { +for (i =0 ; i < (int )ARRAY_SIZE (dfs_fls); i++) { dfs_fls[i].d = debugfs_create_file(dfs_fls[i].name, dfs_fls[i].perm, dfs_inj,
Signature · 1 line
2.9.3
(drop peterz from CC).
Quoted 4 lines · Colin King
From: Colin Ian King <[email protected]>
Integer index i needs to be a signed int rather than unsigned to avoid a wrap-around when decrementing in the while loop. For example, if the
Damn corner cases :-\
Quoted 7 lines
debugfs_create_filefails when i is zero, the current situation will predecrement i in the while loop, wrapping i to the maximum signed integer and cause multiple out of bounds reads on dfs_fls[i].d as the loop interates to zero.Also add (int) cast to fix warning that the original fix attempted to fix.
Thanks, that's a good catch!
But, can we keep the unsigned-ness of i as it is an index and do this instead?
(Oh, and make it return ENODEV - that ENOMEM is just plain wrong most of the time).
---
arch/x86/ras/mce_amd_inj.c +6 -2
@@ -457,8 +457,12 @@ static int __init init_mce_inject(void) &i_mce, dfs_fls[i].fops); -if (!dfs_fls[i].d) +if (!dfs_fls[i].d) { +if (!i) +return -ENODEV ; + goto err_dfs_add; + } }return 0 ;
@@ -470,7 +474,7 @@ err_dfs_add:debugfs_remove (dfs_inj); dfs_inj =NULL ; -return -ENOMEM ; +return -ENODEV ; } static void __exit exit_mce_inject(void)
Signature · 5 lines
Regards/Gruss,
Boris.
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
--