Skip to contentSkip to conversation

Make LIST_POISON less deadly

PATCH In review Avi Kivity 5 participants 16 messages May 18, 2008 – May 20, 2008

Subsystems (2)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 Make LIST_POISON less deadly

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

---
 arch/x86/Kconfig         |    1 +
 include/asm-x86/poison.h |   14 ++++++++++++++
 include/linux/poison.h   |    4 ++++
 lib/Kconfig              |    3 +++
 4 files changed, 22 insertions(+), 0 deletions(-)
 create mode 100644 include/asm-x86/poison.h
arch/x86/Kconfig +1 -0
index fe361ae..0eb34a9 100644
@@ -25,6 +25,7 @@ config X86
 	select HAVE_KRETPROBES
 	select HAVE_KVM if ((X86_32 && !X86_VOYAGER && !X86_VISWS && !X86_NUMAQ) || X86_64)
 	select HAVE_ARCH_KGDB if !X86_VOYAGER
+	select HAVE_ARCH_POISON
 
 config DEFCONFIG_LIST
 	string
include/asm-x86/poison.h new file +14 -0
new file mode 100644
index 0000000..26b95dd
@@ -0,0 +1,14 @@
+#ifndef _ASM_X86_POISON_H
+#define _ASM_X86_POISON_H
+
+/*
+ * Define LIST_POISON[12] as pointers that cannot be dereferenced.
+ */
+#ifdef CONFIG_X86_84
+#  undef LIST_POISON1
+#  undef LIST_POISON2
+#  define LIST_POISON1 ((void *)0x8001000100010001L)
+#  define LIST_POISON2 ((void *)0x8002000200020002L)
+#endif
+
+#endif
include/linux/poison.h +4 -0
index 9f31683..8bb6ad9 100644
@@ -68,4 +68,8 @@
 /********** sound/oss/ **********/
 #define OSS_POISON_FREE		0xAB
 
+#ifdef CONFIG_HAVE_ARCH_POISON
+#include <asm/poison.h>
+#endif
+
 #endif
lib/Kconfig +3 -0
index 8cc8e87..31b9457 100644
@@ -150,4 +150,7 @@ config CHECK_SIGNATURE
 config HAVE_LMB
 	boolean
 
+config HAVE_ARCH_POISON
+	boolean
+
 endmenu
Signature · 1 line
1.5.5.1
#1 lore raw
Quoted 9 lines · Avi Kivity

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

nice idea!

i'd suggest a slightly different solution:

include/asm-x86/poison.h line 12

Quoted 9 lines
include/asm-x86/poison.h new file +9 -0
+/*
+ * Define LIST_POISON[12] as pointers that cannot be dereferenced.
+ */
+#ifdef CONFIG_X86_84
+#  undef LIST_POISON1
+#  undef LIST_POISON2
+#  define LIST_POISON1 ((void *)0x8001000100010001L)
+#  define LIST_POISON2 ((void *)0x8002000200020002L)
+#endif

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

hm?

Ingo

author
#2 lore raw
Quoted 14 lines · Ingo Molnar

include/asm-x86/poison.h line 12

Quoted 11 lines
include/asm-x86/poison.h new file +9 -0
+/*
+ * Define LIST_POISON[12] as pointers that cannot be dereferenced.
+ */
+#ifdef CONFIG_X86_84
+#  undef LIST_POISON1
+#  undef LIST_POISON2
+#  define LIST_POISON1 ((void *)0x8001000100010001L)
+#  define LIST_POISON2 ((void *)0x8002000200020002L)
+#endif

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

Right.

How much unmapped space do we have on i386 or other archs? The deltas added to ARCH_ILLEGAL_POINTER will have to be limited to that.

Maybe have ARCH_ILLEGAL_POINTER_BASE and ARCH_ILLEGAL_POINTER_RANGE, defaulting to 0 and (say) 4MB, and use the range to mask the deltas to avoid overflows into legal pointer range.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 10 lines · Avi Kivity
Quoted 5 lines

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

Right.

How much unmapped space do we have on i386 or other archs? The deltas added to ARCH_ILLEGAL_POINTER will have to be limited to that.

can we get away with having the offset default to zero, and all poison values remain unchanged if that? Then 64-bit x86 could just set the offset to 2^63 and we wont have to worry about 32-bit x86 at all ...

i.e. start small with this and only have an effect on 64-bit x86.

Ingo

author
#4 lore raw
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Avi Kivity
Quoted 2 lines

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

Quoted 9 lines
Quoted 4 lines

Right.

How much unmapped space do we have on i386 or other archs? The deltas added to ARCH_ILLEGAL_POINTER will have to be limited to that.

can we get away with having the offset default to zero, and all poison values remain unchanged if that? Then 64-bit x86 could just set the offset to 2^63 and we wont have to worry about 32-bit x86 at all ...

i.e. start small with this and only have an effect on 64-bit x86.

Sure, will post an updated patch.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#5 lore raw
In reply to Ingo Molnar
Quoted 2 lines · Ingo Molnar

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

Any idea how to override a value in Kconfig? We can do it for bool values but not strings.

e.g.

lib/Kconfig:
config ILLEGAL_POINTER_VALUE
hex
default 0

arch/x86/Kconfig:
config X86
select ILLEGAL_POINTER_VALUE=0x80000000000000000000000000 if X86_64

?

I could do it with a boolean signifying existence and a hex for the value, but I'd rather not.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 17 lines · Avi Kivity
Quoted 5 lines · Ingo Molnar

i'd suggest to add an ARCH_ILLEGAL_POINTER define instead, which defaults to zero and gets added to pointer-ish poison values. That makes it both simpler and also it does not need any include/asm changes because ARCH_ILLEGAL_POINTER can be set from the Kconfig space.

Any idea how to override a value in Kconfig? We can do it for bool values but not strings.

e.g.

lib/Kconfig:
config ILLEGAL_POINTER_VALUE
hex
default 0

Quoted 8 lines

arch/x86/Kconfig:
config X86
select ILLEGAL_POINTER_VALUE=0x80000000000000000000000000 if X86_64

?

I could do it with a boolean signifying existence and a hex for the value, but I'd rather not.

Sorry - no help here. I have not tried to work that much with int in Kconfig.

That said I really think stuff like this belongs in a header file. I do not agree to hide this as part of the configuration.

Sam

author
#7 lore raw
Quoted 13 lines · Sam Ravnborg
Quoted 13 lines

Any idea how to override a value in Kconfig? We can do it for bool values but not strings.

e.g.

lib/Kconfig:
config ILLEGAL_POINTER_VALUE
hex
default 0

arch/x86/Kconfig:
config X86
select ILLEGAL_POINTER_VALUE=0x80000000000000000000000000 if X86_64

Quoted 9 lines
Quoted 4 lines

?

I could do it with a boolean signifying existence and a hex for the value, but I'd rather not.

Sorry - no help here. I have not tried to work that much with int in Kconfig.

I found some workaround (define the variable only on archs that want it, use #ifdef to check).

Quoted 3 lines

That said I really think stuff like this belongs in a header file. I do not agree to hide this as part of the configuration.

While I agree with you, I defer to Ingo on this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

Hmm, thought I had sent a reply earlier, but don't see it so again. My apologies if you see it twice.

The problem with your address values is that they're non canonical and will result in a #GP, not #PF and oops handler cannot display the address which will make them much less obvious.

I would rather use a guaranteed to be unmapped but canonical address like in the ffffc10000000000 - ffffc1ffffffffff range so that you still get page faults.

-Andi

author
#9 lore raw
Quoted 8 lines · Andi Kleen
Quoted 5 lines · Avi Kivity

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

Quoted 9 lines
Quoted 5 lines

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

Hmm, thought I had sent a reply earlier, but don't see it so again. My apologies if you see it twice.

No, this is the first one I see.

Quoted 8 lines

The problem with your address values is that they're non canonical and will result in a #GP, not #PF and oops handler cannot display the address which will make them much less obvious.

I would rather use a guaranteed to be unmapped but canonical address like in the ffffc10000000000 - ffffc1ffffffffff range so that you still get page faults.

Makes sense. I'll send out v3.

Is there a similar range on i386?

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw
Quoted 4 lines

Makes sense. I'll send out v3.

Is there a similar range on i386?

Don't think so. Address space is too precious here.

What you could probably do is to unmap one fixed page in some range that is always split to 4K pages anyways and use that. Perhaps somewhere in the 640k-1MB range. But if you're unlucky this might require a variable, not a constant.

-Andi

author
#11 lore raw
Quoted 5 lines · Andi Kleen
Quoted 4 lines

Makes sense. I'll send out v3.

Is there a similar range on i386?

Quoted 7 lines

Don't think so. Address space is too precious here.

What you could probably do is to unmap one fixed page in some range that is always split to 4K pages anyways and use that. Perhaps somewhere in the 640k-1MB range. But if you're unlucky this might require a variable, not a constant.

I guess a fixmap would work for this. But then the offsets added to that page would need to be limited to 4K.

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Andi Kleen
Quoted 4 lines

Makes sense. I'll send out v3.

Is there a similar range on i386?

Don't think so. Address space is too precious here.

Quoted 8 lines
Quoted 5 lines

What you could probably do is to unmap one fixed page in some range that is always split to 4K pages anyways and use that. Perhaps somewhere in the 640k-1MB range. But if you're unlucky this might require a variable, not a constant.

I guess a fixmap would work for this. But then the offsets added to that page would need to be limited to 4K.

You could make a 3 page fixmap and point to the middle page. 3 because negative offsets are also especially importants for list_heads due to list_entry

-Andi

#13 lore raw
In reply to Avi Kivity
Quoted 2 lines · Avi Kivity

I guess a fixmap would work for this. But then the offsets added to that page would need to be limited to 4K.

i dont think it's worth going for 32-bit here. On 32-bit the poison value gets skewed into hard to recognize values which might make oops analysis harder. Lets start small with 64-bit-only - there it's a quite plausible change, besides the exponentially larger address space on 64-bit it might realistically happen that an attacker can control a 32-bit-is offset but not a full 64-bit offset.

Ingo

#14 lore raw
In reply to Avi Kivity
Quoted 9 lines · Avi Kivity

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

"Security hole unless arch maintainer does _foo_" sounds scary. Especially when i386 is hard to fix...

(And very nice catch, btw).
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
author
#15 lore raw
Quoted 7 lines · Pavel Machek
Quoted 5 lines · Avi Kivity

The list macros use LIST_POISON1 and LIST_POISON2 as undereferencable pointers in order to trap erronous use of freed list_heads. Unfortunately userspace can arrange for those pointers to actually be dereferencable, potentially turning an oops to an expolit.

Quoted 9 lines
Quoted 5 lines

To avoid this allow architectures (currently x86_64 only) to override the default values for these pointers with truly-undereferncable values. This is easy on x86_64 as the virtual address space is smaller than the range spanned by pointer values.

"Security hole unless arch maintainer does _foo_" sounds scary. Especially when i386 is hard to fix...

It's a potential security hole. You need to find a list corruption first. The patch prevents escalation of the oops into a code injection.

i386 is fixable, though it will take more work than x86_64.

Signature · 1 line
error compiling committee.c: too many arguments to function