Message ID | 20170706220114.142438-2-ghackmann@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On 07/06/2017 03:01 PM, Greg Hackmann wrote: > @@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info) > break; > case KASAN_USE_AFTER_SCOPE: > bug_type = "use-after-scope"; > + case KASAN_ALLOCA_LEFT: > + case KASAN_ALLOCA_RIGHT: > + bug_type = "alloca-out-of-bounds"; > break; > } There needs to be a "break" above the new case statements. I'll wait to see if there's any other feedback, then send out a V2 patch that fixes this. -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Fri, Jul 7, 2017 at 12:01 AM, Greg Hackmann <ghackmann@google.com> wrote: > clang's AddressSanitizer implementation adds redzones on either side of > alloca()ed buffers. These redzones are 32-byte aligned and at least 32 > bytes long. > > __asan_alloca_poison() is passed the size and address of the allocated > buffer, *excluding* the redzones on either side. The left redzone will > always be to the immediate left of this buffer; but AddressSanitizer may > need to add padding between the end of the buffer and the right redzone. > If there are any 8-byte chunks inside this padding, we should poison > those too. > > __asan_allocas_unpoison() is just passed the top and bottom of the > dynamic stack area, so unpoisoning is simpler. > > Signed-off-by: Greg Hackmann <ghackmann@google.com> > --- > lib/test_kasan.c | 22 ++++++++++++++++++++++ > mm/kasan/kasan.c | 26 ++++++++++++++++++++++++++ > mm/kasan/kasan.h | 8 ++++++++ > mm/kasan/report.c | 3 +++ > 4 files changed, 59 insertions(+) > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > index a25c9763fce1..f774fcafb696 100644 > --- a/lib/test_kasan.c > +++ b/lib/test_kasan.c > @@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void) > p[1023] = 1; > } > > +static noinline void __init kasan_alloca_oob_left(void) > +{ > + volatile int i = 10; > + char alloca_array[i]; > + char *p = alloca_array - 1; > + > + pr_info("out-of-bounds to left on alloca\n"); > + *(volatile char *)p; > +} > + > +static noinline void __init kasan_alloca_oob_right(void) > +{ > + volatile int i = 10; > + char alloca_array[i]; > + char *p = alloca_array + round_up(i, 8); > + > + pr_info("out-of-bounds to right on alloca\n"); > + *(volatile char *)p; > +} > + > static int __init kmalloc_tests_init(void) > { > /* > @@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void) > memcg_accounted_kmem_cache(); > kasan_stack_oob(); > kasan_global_oob(); > + kasan_alloca_oob_left(); > + kasan_alloca_oob_right(); > ksize_unpoisons_memory(); > copy_user_test(); > use_after_scope_test(); > diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c > index c81549d5c833..892b626f564b 100644 > --- a/mm/kasan/kasan.c > +++ b/mm/kasan/kasan.c > @@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size) > } > EXPORT_SYMBOL(__asan_unpoison_stack_memory); > > +/* Emitted by compiler to poison alloca()ed objects. */ > +void __asan_alloca_poison(unsigned long addr, size_t size) > +{ > + size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE); > + size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) - > + round_up(size, KASAN_SHADOW_SCALE_SIZE); Perhaps s/round_up(size, KASAN_SHADOW_SCALE_SIZE)/rounded_up_size/ because we already calculated that. > + > + const void *left_redzone = (const void *)(addr - > + KASAN_ALLOCA_REDZONE_SIZE); > + const void *right_redzone = (const void *)(addr + rounded_up_size); Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's the expectation, right? That can change is clang silently. > + kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, > + KASAN_ALLOCA_LEFT); > + kasan_poison_shadow(right_redzone, > + padding_size + KASAN_ALLOCA_REDZONE_SIZE, > + KASAN_ALLOCA_RIGHT); We also need to poison the unaligned part at the end of the object from size to rounded_up_size. You can see how we do it for heap objects. > +} > +EXPORT_SYMBOL(__asan_alloca_poison); > +/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */ > +void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) > +{ > + kasan_unpoison_shadow(stack_top, stack_bottom - stack_top); > +} > +EXPORT_SYMBOL(__asan_allocas_unpoison); > + > #ifdef CONFIG_MEMORY_HOTPLUG > static int kasan_mem_notifier(struct notifier_block *nb, > unsigned long action, void *data) > diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h > index 1229298cce64..b857dc70d6a2 100644 > --- a/mm/kasan/kasan.h > +++ b/mm/kasan/kasan.h > @@ -23,6 +23,14 @@ > #define KASAN_STACK_PARTIAL 0xF4 > #define KASAN_USE_AFTER_SCOPE 0xF8 > > +/* > + * alloca redzone shadow values > + */ > +#define KASAN_ALLOCA_LEFT 0xCA > +#define KASAN_ALLOCA_RIGHT 0xCB > + > +#define KASAN_ALLOCA_REDZONE_SIZE 32 > + > /* Don't break randconfig/all*config builds */ > #ifndef KASAN_ABI_VERSION > #define KASAN_ABI_VERSION 1 > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index beee0e980e2d..c6a5b7ab9e3a 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info) > break; > case KASAN_USE_AFTER_SCOPE: > bug_type = "use-after-scope"; > + case KASAN_ALLOCA_LEFT: > + case KASAN_ALLOCA_RIGHT: > + bug_type = "alloca-out-of-bounds"; > break; > } > > -- > 2.13.2.725.g09c95d1e9-goog > -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 07/07/2017 01:01 AM, Greg Hackmann wrote: > clang's AddressSanitizer implementation adds redzones on either side of > alloca()ed buffers. These redzones are 32-byte aligned and at least 32 > bytes long. gcc now supports this too. So I think this patch should enable it. It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags to make it work > > __asan_alloca_poison() is passed the size and address of the allocated > buffer, *excluding* the redzones on either side. The left redzone will > always be to the immediate left of this buffer; but AddressSanitizer may > need to add padding between the end of the buffer and the right redzone. > If there are any 8-byte chunks inside this padding, we should poison > those too. > > __asan_allocas_unpoison() is just passed the top and bottom of the > dynamic stack area, so unpoisoning is simpler. > > Signed-off-by: Greg Hackmann <ghackmann@google.com> > --- > lib/test_kasan.c | 22 ++++++++++++++++++++++ Tests would be better as a separate patch. > mm/kasan/kasan.c | 26 ++++++++++++++++++++++++++ > mm/kasan/kasan.h | 8 ++++++++ > mm/kasan/report.c | 3 +++ > 4 files changed, 59 insertions(+) > > diff --git a/lib/test_kasan.c b/lib/test_kasan.c > index a25c9763fce1..f774fcafb696 100644 > --- a/lib/test_kasan.c > +++ b/lib/test_kasan.c > @@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void) > p[1023] = 1; > } > > +static noinline void __init kasan_alloca_oob_left(void) > +{ > + volatile int i = 10; > + char alloca_array[i]; > + char *p = alloca_array - 1; > + > + pr_info("out-of-bounds to left on alloca\n"); > + *(volatile char *)p; > +} > + > +static noinline void __init kasan_alloca_oob_right(void) > +{ > + volatile int i = 10; > + char alloca_array[i]; > + char *p = alloca_array + round_up(i, 8); Why round_up() ? > + > + pr_info("out-of-bounds to right on alloca\n"); > + *(volatile char *)p; > +} > + > static int __init kmalloc_tests_init(void) > { > /* > @@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void) > memcg_accounted_kmem_cache(); > kasan_stack_oob(); > kasan_global_oob(); > + kasan_alloca_oob_left(); > + kasan_alloca_oob_right(); > ksize_unpoisons_memory(); > copy_user_test(); > use_after_scope_test(); > diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c > index c81549d5c833..892b626f564b 100644 > --- a/mm/kasan/kasan.c > +++ b/mm/kasan/kasan.c > @@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size) > } > EXPORT_SYMBOL(__asan_unpoison_stack_memory); > > +/* Emitted by compiler to poison alloca()ed objects. */ > +void __asan_alloca_poison(unsigned long addr, size_t size) > +{ > + size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE); > + size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) - > + round_up(size, KASAN_SHADOW_SCALE_SIZE); > + > + const void *left_redzone = (const void *)(addr - > + KASAN_ALLOCA_REDZONE_SIZE); > + const void *right_redzone = (const void *)(addr + rounded_up_size); > + > + kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, > + KASAN_ALLOCA_LEFT); > + kasan_poison_shadow(right_redzone, > + padding_size + KASAN_ALLOCA_REDZONE_SIZE, > + KASAN_ALLOCA_RIGHT); As Dmitry pointed out, the memory between [addr+size, addr+rounded_up_size) is left unpoisoned. kasan_alloca_oob_right() without round_up() would have caught this. -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi, Thanks for taking a look at this patchstack. I apologize for the delay in responding. On 07/10/2017 01:44 AM, Dmitry Vyukov wrote: >> + >> + const void *left_redzone = (const void *)(addr - >> + KASAN_ALLOCA_REDZONE_SIZE); >> + const void *right_redzone = (const void *)(addr + rounded_up_size); > > Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's > the expectation, right? That can change is clang silently. > >> + kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, >> + KASAN_ALLOCA_LEFT); >> + kasan_poison_shadow(right_redzone, >> + padding_size + KASAN_ALLOCA_REDZONE_SIZE, >> + KASAN_ALLOCA_RIGHT); > > We also need to poison the unaligned part at the end of the object > from size to rounded_up_size. You can see how we do it for heap > objects. The expectation is that `size' is the exact size of the alloca()ed object. `rounded_up_size' then adds the 0-7 bytes needed to adjust the size to the ASAN shadow scale. So `addr + rounded_up_size' should be the correct place to start poisoning. In retrospect this part of the code was pretty confusing. How about this? I think its intent is clearer, plus it's a closer match for the description in my commit message: unsigned long left_redzone_start; unsigned long object_end; unsigned long right_redzone_start, right_redzone_end; left_redzone_start = addr - KASAN_ALLOCA_REDZONE_SIZE; kasan_poison_shadow((const void *)left_redzone_start, KASAN_ALLOCA_REDZONE_SIZE, KASAN_ALLOCA_LEFT); object_end = round_up(addr + size, KASAN_SHADOW_SCALE_SIZE); right_redzone_start = round_up(object_end, KASAN_ALLOCA_REDZONE_SIZE); right_redzone_end = right_redzone_start + KASAN_ALLOCA_REDZONE_SIZE; kasan_poison_shadow((const void *)object_end, right_redzone_end - object_end, KASAN_ALLOCA_RIGHT); -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 07/10/2017 03:30 AM, Andrey Ryabinin wrote: > gcc now supports this too. So I think this patch should enable it. > It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags > to make it work Thanks, will fix. For now, it looks like I'll need to build gcc from git to test this? >> lib/test_kasan.c | 22 ++++++++++++++++++++++ > > Tests would be better as a separate patch. I was following the precedent in 828347f8f9a5 ("kasan: support use-after-scope detection") which added both at the same time. But I can split the test off into a separate patch if you feel really strongly about it. -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Fri, Jul 14, 2017 at 12:40 AM, Greg Hackmann <ghackmann@google.com> wrote: > Hi, > > Thanks for taking a look at this patchstack. I apologize for the delay in > responding. > > On 07/10/2017 01:44 AM, Dmitry Vyukov wrote: >>> >>> + >>> + const void *left_redzone = (const void *)(addr - >>> + KASAN_ALLOCA_REDZONE_SIZE); >>> + const void *right_redzone = (const void *)(addr + >>> rounded_up_size); >> >> >> Please check that size is rounded to KASAN_ALLOCA_REDZONE_SIZE. That's >> the expectation, right? That can change is clang silently. >> >>> + kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, >>> + KASAN_ALLOCA_LEFT); >>> + kasan_poison_shadow(right_redzone, >>> + padding_size + KASAN_ALLOCA_REDZONE_SIZE, >>> + KASAN_ALLOCA_RIGHT); >> >> >> We also need to poison the unaligned part at the end of the object >> from size to rounded_up_size. You can see how we do it for heap >> objects. > > > The expectation is that `size' is the exact size of the alloca()ed object. > `rounded_up_size' then adds the 0-7 bytes needed to adjust the size to the > ASAN shadow scale. So `addr + rounded_up_size' should be the correct place > to start poisoning. We need to start poisoning at addr+size exactly. Asan shadow scheme supports this. It's not possible to poison beginning of an aligned 8-byte block, but leave tail unpoisoned. But it is possible to poison tail of an aligned 8-byte block and leave beginning unpoisoned. Look at what we do for kmalloc. > In retrospect this part of the code was pretty confusing. How about this? > I think its intent is clearer, plus it's a closer match for the description > in my commit message: > > unsigned long left_redzone_start; > unsigned long object_end; > unsigned long right_redzone_start, right_redzone_end; > > left_redzone_start = addr - KASAN_ALLOCA_REDZONE_SIZE; > kasan_poison_shadow((const void *)left_redzone_start, > KASAN_ALLOCA_REDZONE_SIZE, > KASAN_ALLOCA_LEFT); > > object_end = round_up(addr + size, KASAN_SHADOW_SCALE_SIZE); > right_redzone_start = round_up(object_end, > KASAN_ALLOCA_REDZONE_SIZE); > right_redzone_end = right_redzone_start + KASAN_ALLOCA_REDZONE_SIZE; > kasan_poison_shadow((const void *)object_end, > right_redzone_end - object_end, > KASAN_ALLOCA_RIGHT); -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 07/14/2017 01:49 AM, Greg Hackmann wrote: > On 07/10/2017 03:30 AM, Andrey Ryabinin wrote: >> gcc now supports this too. So I think this patch should enable it. >> It's off by default so you'll have to add --param asan-instrument-allocas=1 into cflags >> to make it work > > Thanks, will fix. For now, it looks like I'll need to build gcc from git to test this? > Right, you'll need quite fresh revision >= 250032 >>> lib/test_kasan.c | 22 ++++++++++++++++++++++ >> >> Tests would be better as a separate patch. > > I was following the precedent in 828347f8f9a5 ("kasan: support use-after-scope detection") which added both at the same time. But I can split the test off into a separate patch if you feel really strongly about it. Please, do the split. -- To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
diff --git a/lib/test_kasan.c b/lib/test_kasan.c index a25c9763fce1..f774fcafb696 100644 --- a/lib/test_kasan.c +++ b/lib/test_kasan.c @@ -473,6 +473,26 @@ static noinline void __init use_after_scope_test(void) p[1023] = 1; } +static noinline void __init kasan_alloca_oob_left(void) +{ + volatile int i = 10; + char alloca_array[i]; + char *p = alloca_array - 1; + + pr_info("out-of-bounds to left on alloca\n"); + *(volatile char *)p; +} + +static noinline void __init kasan_alloca_oob_right(void) +{ + volatile int i = 10; + char alloca_array[i]; + char *p = alloca_array + round_up(i, 8); + + pr_info("out-of-bounds to right on alloca\n"); + *(volatile char *)p; +} + static int __init kmalloc_tests_init(void) { /* @@ -503,6 +523,8 @@ static int __init kmalloc_tests_init(void) memcg_accounted_kmem_cache(); kasan_stack_oob(); kasan_global_oob(); + kasan_alloca_oob_left(); + kasan_alloca_oob_right(); ksize_unpoisons_memory(); copy_user_test(); use_after_scope_test(); diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index c81549d5c833..892b626f564b 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -802,6 +802,32 @@ void __asan_unpoison_stack_memory(const void *addr, size_t size) } EXPORT_SYMBOL(__asan_unpoison_stack_memory); +/* Emitted by compiler to poison alloca()ed objects. */ +void __asan_alloca_poison(unsigned long addr, size_t size) +{ + size_t rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE); + size_t padding_size = round_up(size, KASAN_ALLOCA_REDZONE_SIZE) - + round_up(size, KASAN_SHADOW_SCALE_SIZE); + + const void *left_redzone = (const void *)(addr - + KASAN_ALLOCA_REDZONE_SIZE); + const void *right_redzone = (const void *)(addr + rounded_up_size); + + kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, + KASAN_ALLOCA_LEFT); + kasan_poison_shadow(right_redzone, + padding_size + KASAN_ALLOCA_REDZONE_SIZE, + KASAN_ALLOCA_RIGHT); +} +EXPORT_SYMBOL(__asan_alloca_poison); + +/* Emitted by compiler to unpoison alloca()ed areas when the stack unwinds. */ +void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) +{ + kasan_unpoison_shadow(stack_top, stack_bottom - stack_top); +} +EXPORT_SYMBOL(__asan_allocas_unpoison); + #ifdef CONFIG_MEMORY_HOTPLUG static int kasan_mem_notifier(struct notifier_block *nb, unsigned long action, void *data) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 1229298cce64..b857dc70d6a2 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -23,6 +23,14 @@ #define KASAN_STACK_PARTIAL 0xF4 #define KASAN_USE_AFTER_SCOPE 0xF8 +/* + * alloca redzone shadow values + */ +#define KASAN_ALLOCA_LEFT 0xCA +#define KASAN_ALLOCA_RIGHT 0xCB + +#define KASAN_ALLOCA_REDZONE_SIZE 32 + /* Don't break randconfig/all*config builds */ #ifndef KASAN_ABI_VERSION #define KASAN_ABI_VERSION 1 diff --git a/mm/kasan/report.c b/mm/kasan/report.c index beee0e980e2d..c6a5b7ab9e3a 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -101,6 +101,9 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info) break; case KASAN_USE_AFTER_SCOPE: bug_type = "use-after-scope"; + case KASAN_ALLOCA_LEFT: + case KASAN_ALLOCA_RIGHT: + bug_type = "alloca-out-of-bounds"; break; }
clang's AddressSanitizer implementation adds redzones on either side of alloca()ed buffers. These redzones are 32-byte aligned and at least 32 bytes long. __asan_alloca_poison() is passed the size and address of the allocated buffer, *excluding* the redzones on either side. The left redzone will always be to the immediate left of this buffer; but AddressSanitizer may need to add padding between the end of the buffer and the right redzone. If there are any 8-byte chunks inside this padding, we should poison those too. __asan_allocas_unpoison() is just passed the top and bottom of the dynamic stack area, so unpoisoning is simpler. Signed-off-by: Greg Hackmann <ghackmann@google.com> --- lib/test_kasan.c | 22 ++++++++++++++++++++++ mm/kasan/kasan.c | 26 ++++++++++++++++++++++++++ mm/kasan/kasan.h | 8 ++++++++ mm/kasan/report.c | 3 +++ 4 files changed, 59 insertions(+)