Skip to contentSkip to conversation

hw_breakpoints: Fix a bunch of adress range fixes

PATCH In review Frederic Weisbecker 3 participants 9 messages Nov 24, 2013 – Nov 26, 2013

Subsystems (6)
Stable branch Arm (32-bit) x86 arm64 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

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(-)
#1 lore raw

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.

Fixes f81ef4a920c8e1af75adf9f15042c2daa49d3cb3
---
 arch/arm/kernel/hw_breakpoint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/kernel/hw_breakpoint.c +1 -1
index 3d44660..8b38001 100644
@@ -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
#2 lore raw

PATCH 2/4 x86: Fix the hw_breakpoint range check

In reply to Frederic Weisbecker

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.

Note: TASK_SIZE doesn't look right at least on x86, I think it should be replaced by TASK_SIZE_MAX.

Fixes 0067f1297241ea567f2b22a455519752d70fcca9
---
 arch/x86/kernel/hw_breakpoint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/hw_breakpoint.c +1 -1
index f66ff16..1131c1f 100644
@@ -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
maintainer
#3 lore raw

Re: [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 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.

Fixes 0067f1297241ea567f2b22a455519752d70fcca9
---
 arch/x86/kernel/hw_breakpoint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/hw_breakpoint.c +0 -0
index f66ff16..1131c1f 100644

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.
--
#4 lore raw

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.

maintainer
#5 lore raw

Re: [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.
--
#6 lore raw

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.

#7 lore raw

PATCH 3/4 arm64: Fix the hw_breakpoint range check

In reply to Frederic Weisbecker

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.

Fixes 478fcb2cdb2351dcfc3fb23f42d76f4436ee4149
---
 arch/arm64/kernel/hw_breakpoint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/hw_breakpoint.c +1 -1
index ff516f6..4bbbfde 100644
@@ -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
#8 lore raw

PATCH 4/4 sh: Fix hw_breakpoint the range check

In reply to Frederic Weisbecker

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.

Fixes 09a072947791088b88ae15111cf68fc5aaaf758d
---
 arch/sh/kernel/hw_breakpoint.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sh/kernel/hw_breakpoint.c +1 -1
index f917376..9801674 100644
@@ -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