powerpc: Add out of bounds check to crash_shutdown_unregister()
PATCH 1/1 powerpc: Add out of bounds check to crash_shutdown_unregister()
When unregistering a crash_shutdown_handle in the function crash_shutdown_unregister() the other handles are shifted down in the array to replace the unregistered handle. The for loop assumes that the last element in the array is null and uses this as the stop condition, however in the case that the last element is not null there is no check to ensure that an out of bounds access is not performed.
Add a check to terminate the shift operation when CRASH_HANDLER_MAX is reached in order to protect against out of bounds accesses.
--- arch/powerpc/kernel/crash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/crash.c +1 -1
@@ -288,7 +288,7 @@ int crash_shutdown_unregister(crash_shutdown_t handler) rc =1 ; }else {/* Shift handles down */ -for (; crash_shutdown_handles[i]; i++) +for (; crash_shutdown_handles[i] && i <CRASH_HANDLER_MAX ; i++) crash_shutdown_handles[i] = crash_shutdown_handles[i+1 ]; rc =0 ;
Signature · 1 line
2.5.0
Quoted 21 lines · Suraj Jitindar Singh
When unregistering a
crash_shutdown_handlein the functioncrash_shutdown_unregister()the other handles are shifted down in the array to replace the unregistered handle. The for loop assumes that the last element in the array is null and uses this as the stop condition, however in the case that the last element is not null there is no check to ensure that an out of bounds access is not performed.Add a check to terminate the shift operation when
CRASH_HANDLER_MAXis reached in order to protect against out of bounds accesses.Signed-off-by Suraj Jitindar Singh <[email protected]>--- arch/powerpc/kernel/crash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kernel/crash.c +0
-0
arch/powerpc/kernel/crash.c int crash_shutdown_unregister(crash_shutdown_t handler) · line 294
Quoted 8 lines
arch/powerpc/kernel/crash.c +1
-1@@ -288,7 +288,7 @@ int crash_shutdown_unregister(crash_shutdown_t handler) rc =1 ; }else {/* Shift handles down */ -for (; crash_shutdown_handles[i]; i++) +for (; crash_shutdown_handles[i] && i <CRASH_HANDLER_MAX ; i++) crash_shutdown_handles[i] = crash_shutdown_handles[i+1 ]; rc =0 ;
with i = CRASH_HANDLER_MAX-1 we could end up with crash_shutdown_handles[i+1] already out of bounds I think you need to check that i+1 does not overflow
Balbir
Quoted 36 lines · On Thu, 28 Apr 2016 16:55:13 +1000 Balbir Singh <[email protected]> wrote:
Quoted 28 lines · Suraj Jitindar Singh
When unregistering a
crash_shutdown_handlein the functioncrash_shutdown_unregister()the other handles are shifted down in the array to replace the unregistered handle. The for loop assumes that the last element in the array is null and uses this as the stop condition, however in the case that the last element is not null there is no check to ensure that an out of bounds access is not performed.Add a check to terminate the shift operation when
CRASH_HANDLER_MAXis reached in order to protect against out of bounds accesses.Signed-off-by Suraj Jitindar Singh <[email protected]>--- arch/powerpc/kernel/crash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)(quoted diff) +0
-0
b/arch/powerpc/kernel/crash.cindex 2bb252c..6b267af 100644arch/powerpc/kernel/crash.c +1
-1@@ -288,7 +288,7 @@ int crash_shutdown_unregister(crash_shutdown_t handler) rc = 1; } else { /* Shift handles down */ -for (; crash_shutdown_handles[i]; i++) + for (; crash_shutdown_handles[i] && i <
CRASH_HANDLER_MAX; i++) crash_shutdown_handles[i] =arch/powerpc/kernel/crash.c int crash_shutdown_unregister(crash_shutdown_t handler) · line 294
Quoted 2 lines
arch/powerpc/kernel/crash.c +0
-0crash_shutdown_handles[i+1 ]; rc =0 ;with i = CRASH_HANDLER_MAX-1 we could end up with crash_shutdown_handles[i+1] already out of bounds I think you need to check that i+1 does not overflow
Quoted 1 line
Balbir
Thanks for taking a look Balbir, the size of crash_shutdown_handles is actually CRASH_HANDLER_MAX+1.
Balbir Singh
Acked-byQuoted 4 lines
Thanks for taking a look Balbir, the size of
crash_shutdown_handlesis actually CRASH_HANDLER_MAX+1.
Michael Ellerman
reviewerQuoted 6 lines · Suraj Jitindar Singh
When unregistering a
crash_shutdown_handlein the functioncrash_shutdown_unregister()the other handles are shifted down in the array to replace the unregistered handle. The for loop assumes that the last element in the array is null and uses this as the stop condition, however in the case that the last element is not null there is no check to ensure that an out of bounds access is not performed.
But AFAICS the code ensures that entry will always be NULL. So there's no bug at the moment.
Quoted 2 lines
Add a check to terminate the shift operation when
CRASH_HANDLER_MAXis reached in order to protect against out of bounds accesses.
Doing it this way is more robust though. The chance of the NULL terminator being corrupted is definitely higher than the code being corrupted, and if the latter happens we're probably toast anyway.
arch/powerpc/kernel/crash.c int crash_shutdown_unregister(crash_shutdown_t handler) · line 294
Quoted 13 lines
arch/powerpc/kernel/crash.c +1
-1@@ -288,7 +288,7 @@ int crash_shutdown_unregister(crash_shutdown_t handler) rc =1 ; }else {/* Shift handles down */ -for (; crash_shutdown_handles[i]; i++) +for (; crash_shutdown_handles[i] && i <CRASH_HANDLER_MAX ; i++) crash_shutdown_handles[i] = crash_shutdown_handles[i+1 ]; rc =0 ;
So if I'm reading it right, with this change we have removed all the code that uses the NULL-terminated property of the list.
If so we should also shrink the array to be only CRASH_HANDLER_MAX in size, and remove any references to it being NULL terminated.
cheers
Quoted 32 lines · Michael Ellerman
Quoted 6 lines · Suraj Jitindar Singh
When unregistering a
crash_shutdown_handlein the functioncrash_shutdown_unregister()the other handles are shifted down in the array to replace the unregistered handle. The for loop assumes that the last element in the array is null and uses this as the stop condition, however in the case that the last element is not null there is no check to ensure that an out of bounds access is not performed.But AFAICS the code ensures that entry will always be NULL. So there's no bug at the moment.
Quoted 2 lines
Add a check to terminate the shift operation when
CRASH_HANDLER_MAXis reached in order to protect against out of bounds accesses.Doing it this way is more robust though. The chance of the NULL terminator being corrupted is definitely higher than the code being corrupted, and if the latter happens we're probably toast anyway.
arch/powerpc/kernel/crash.c int crash_shutdown_unregister(crash_shutdown_t handler) · line 294
Quoted 13 lines
arch/powerpc/kernel/crash.c +1
-1@@ -288,7 +288,7 @@ int crash_shutdown_unregister(crash_shutdown_t handler) rc =1 ; }else {/* Shift handles down */ -for (; crash_shutdown_handles[i]; i++) +for (; crash_shutdown_handles[i] && i <CRASH_HANDLER_MAX ; i++) crash_shutdown_handles[i] = crash_shutdown_handles[i+1 ]; rc =0 ;So if I'm reading it right, with this change we have removed all the code that uses the NULL-terminated property of the list.
Quoted 4 lines
If so we should also shrink the array to be only
CRASH_HANDLER_MAXin size, and remove any references to it being NULL terminated.cheers
Thanks Michael, will change this to reflect that the list is no longer assumed to be null terminated.