hw_breakpoints: Fix a bunch of adress range fixes
Subsystems (6)
cover hw_breakpoints: Fix a bunch of adress range fixes
It was initially a single patch that Oleg sent me a few weeks ago. Thinking about it I think it may need a stable backport even though it doesn't look very dangerous, but just in case.
So I've splitted the patch in 4 different parts because it may need backporting on different tree version for each arch concerned.
Of course we need to consolidate that range check somewhere but after the stable backport.
Comments.
Thanks,
Frederic
---
Oleg Nesterov (4):
arm: Fix the hw_breakpoint range check
x86: Fix the hw_breakpoint range check
arm64: Fix the hw_breakpoint range check
sh: Fix hw_breakpoint the range check
arch/arm/kernel/hw_breakpoint.c | 2 +- arch/arm64/kernel/hw_breakpoint.c | 2 +- arch/sh/kernel/hw_breakpoint.c | 2 +- arch/x86/kernel/hw_breakpoint.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
PATCH 1/4 arm: Fix the hw_breakpoint range check
From: Oleg Nesterov <[email protected]>
arch_check_bp_in_kernelspace() tries to avoid the overflow and does 2 TASK_SIZE checks but it needs OR, not AND. Consider va = TASK_SIZE -1 and len = 2 case.
--- arch/arm/kernel/hw_breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/hw_breakpoint.c +1 -1
@@ -465,7 +465,7 @@ int arch_check_bp_in_kernelspace(struct perf_event *bp) va = info->address; len =get_hbp_len (info->ctrl.len); -return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE ); }/*
Signature · 1 line
1.8.3.1
PATCH 2/4 x86: Fix the hw_breakpoint range check
In reply to Frederic WeisbeckerFrom: Oleg Nesterov <[email protected]>
arch_check_bp_in_kernelspace() tries to avoid the overflow and does 2 TASK_SIZE checks but it needs OR, not AND. Consider va = TASK_SIZE -1 and len = 2 case.
Note: TASK_SIZE doesn't look right at least on x86, I think it should be replaced by TASK_SIZE_MAX.
--- arch/x86/kernel/hw_breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/hw_breakpoint.c +1 -1
@@ -200,7 +200,7 @@ int arch_check_bp_in_kernelspace(struct perf_event *bp) va = info->address; len =get_hbp_len (info->len); -return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE ); } int arch_bp_generic_fields(int x86_len, int x86_type,
Signature · 1 line
1.8.3.1
Borislav Petkov
maintainerRe: [PATCH 2/4] x86: Fix the hw_breakpoint range check
Quoted 22 lines · Frederic Weisbecker
From: Oleg Nesterov <[email protected]>
arch_check_bp_in_kernelspace()tries to avoid the overflow and does 2TASK_SIZEchecks but it needs OR, not AND. Consider va =TASK_SIZE-1 and len = 2 case.Note:
TASK_SIZEdoesn't look right at least on x86, I think it should be replaced byTASK_SIZE_MAX.Signed-off-by Oleg Nesterov <[email protected]>Fixes 0067f1297241ea567f2b22a455519752d70fcca9Cc <[email protected]>Signed-off-by Frederic Weisbecker <[email protected]>--- arch/x86/kernel/hw_breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/hw_breakpoint.c +0
-0
arch/x86/kernel/hw_breakpoint.c int arch_check_bp_in_kernelspace(struct perf_event *bp) · line 203
Quoted 5 lines
arch/x86/kernel/hw_breakpoint.c +1
-1@@ -200,7 +200,7 @@ int arch_check_bp_in_kernelspace(struct perf_event *bp) va = info->address; len =get_hbp_len (info->len); -return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE );
Well, can't you simplify it even further?
return (va + len - 1) >= TASK_SIZE;
AFAICT, the high end of the range matters, no?
Unless the original code was meant to short-circuit at the first comparison already...
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Re: [PATCH 2/4] x86: Fix the hw_breakpoint range check
Frederic. Thanks for doing this ;)
Quoted 9 lines · Borislav Petkov
arch/x86/kernel/hw_breakpoint.c int arch_check_bp_in_kernelspace(struct perf_event *bp) · line 203
Quoted 3 lines · Frederic Weisbecker
arch/x86/kernel/hw_breakpoint.c +1
-1-return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE );Well, can't you simplify it even further?
return (va + len - 1) >= TASK_SIZE;
This won't work if va + len overflows?
Perhaps we should makes this clear, and we can even check the overflow in the generic code (iirc Linus suggested to do this).
But to me it would be better to add the generic helper, they all do the same check. Even arch/powerpc/kernel/hw_breakpoint.c whch doesn't look right. Or make it __weak, or turn it into arch_check_bp_in_kernelspace(start, end).
Oleg.
Borislav Petkov
maintainerRe: [PATCH 2/4] x86: Fix the hw_breakpoint range check
Quoted 1 line · Oleg Nesterov
This won't work if va + len overflows?
Oh, right,
Quoted 2 lines
Perhaps we should makes this clear, and we can even check the overflow in the generic code (iirc Linus suggested to do this).
maybe something like
((va + len - 1) >= TASK_SIZE) || ((va + len - 1) < va)
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--Re: [PATCH 2/4] x86: Fix the hw_breakpoint range check
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Oleg Nesterov
This won't work if va + len overflows?
Quoted 8 lines
Oh, right,
Quoted 2 lines
Perhaps we should makes this clear, and we can even check the overflow in the generic code (iirc Linus suggested to do this).
maybe something like
((va + len - 1) >= TASK_SIZE) || ((va + len - 1) < va)
Yes. But again, it makes sense to do this in the caller. And kill the stupid get_hbp_len(). And other cleanups.
But this patch just tries to fix the typo in the security check.
Oleg.
PATCH 3/4 arm64: Fix the hw_breakpoint range check
In reply to Frederic WeisbeckerFrom: Oleg Nesterov <[email protected]>
arch_check_bp_in_kernelspace() tries to avoid the overflow and does 2 TASK_SIZE checks but it needs OR, not AND. Consider va = TASK_SIZE -1 and len = 2 case.
--- arch/arm64/kernel/hw_breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/hw_breakpoint.c +1 -1
@@ -293,7 +293,7 @@ int arch_check_bp_in_kernelspace(struct perf_event *bp) va = info->address; len =get_hbp_len (info->ctrl.len); -return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE ); }/*
Signature · 1 line
1.8.3.1
PATCH 4/4 sh: Fix hw_breakpoint the range check
In reply to Frederic WeisbeckerFrom: Oleg Nesterov <[email protected]>
arch_check_bp_in_kernelspace() tries to avoid the overflow and does 2 TASK_SIZE checks but it needs OR, not AND. Consider va = TASK_SIZE -1 and len = 2 case.
--- arch/sh/kernel/hw_breakpoint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/kernel/hw_breakpoint.c +1 -1
@@ -132,7 +132,7 @@ int arch_check_bp_in_kernelspace(struct perf_event *bp) va = info->address; len =get_hbp_len (info->len); -return (va >=TASK_SIZE ) && ((va + len -1 ) >=TASK_SIZE ); +return (va >=TASK_SIZE ) || ((va + len -1 ) >=TASK_SIZE ); } int arch_bp_generic_fields(int sh_len, int sh_type,
Signature · 1 line
1.8.3.1