提交 689d994f 编写于 作者: P Peter Collingbourne 提交者: Zheng Zengkai

kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled

stable inclusion
from stable-5.10.38
commit fe5c0a63ad22cc61498f2bc3164449a233e8c774
bugzilla: 51875
CVE: NA

--------------------------------

commit f649dc0e upstream.

These tests deliberately access these arrays out of bounds, which will
cause the dynamic local bounds checks inserted by
CONFIG_UBSAN_LOCAL_BOUNDS to fail and panic the kernel.  To avoid this
problem, access the arrays via volatile pointers, which will prevent the
compiler from being able to determine the array bounds.

These accesses use volatile pointers to char (char *volatile) rather than
the more conventional pointers to volatile char (volatile char *) because
we want to prevent the compiler from making inferences about the pointer
itself (i.e.  its array bounds), not the data that it refers to.

Link: https://lkml.kernel.org/r/20210507025915.1464056-1-pcc@google.com
Link: https://linux-review.googlesource.com/id/I90b1713fbfa1bf68ff895aef099ea77b98a7c3b9Signed-off-by: NPeter Collingbourne <pcc@google.com>
Tested-by: NAlexander Potapenko <glider@google.com>
Reviewed-by: NAndrey Konovalov <andreyknvl@gmail.com>
Cc: Peter Collingbourne <pcc@google.com>
Cc: George Popescu <georgepope@android.com>
Cc: Elena Petrova <lenaptr@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Acked-by: NWeilong Chen <chenweilong@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 4f895945
...@@ -449,8 +449,20 @@ static char global_array[10]; ...@@ -449,8 +449,20 @@ static char global_array[10];
static void kasan_global_oob(struct kunit *test) static void kasan_global_oob(struct kunit *test)
{ {
volatile int i = 3; /*
char *p = &global_array[ARRAY_SIZE(global_array) + i]; * Deliberate out-of-bounds access. To prevent CONFIG_UBSAN_LOCAL_BOUNDS
* from failing here and panicing the kernel, access the array via a
* volatile pointer, which will prevent the compiler from being able to
* determine the array bounds.
*
* This access uses a volatile pointer to char (char *volatile) rather
* than the more conventional pointer to volatile char (volatile char *)
* because we want to prevent the compiler from making inferences about
* the pointer itself (i.e. its array bounds), not the data that it
* refers to.
*/
char *volatile array = global_array;
char *p = &array[ARRAY_SIZE(global_array) + 3];
/* Only generic mode instruments globals. */ /* Only generic mode instruments globals. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
...@@ -479,8 +491,9 @@ static void ksize_unpoisons_memory(struct kunit *test) ...@@ -479,8 +491,9 @@ static void ksize_unpoisons_memory(struct kunit *test)
static void kasan_stack_oob(struct kunit *test) static void kasan_stack_oob(struct kunit *test)
{ {
char stack_array[10]; char stack_array[10];
volatile int i = OOB_TAG_OFF; /* See comment in kasan_global_oob. */
char *p = &stack_array[ARRAY_SIZE(stack_array) + i]; char *volatile array = stack_array;
char *p = &array[ARRAY_SIZE(stack_array) + OOB_TAG_OFF];
if (!IS_ENABLED(CONFIG_KASAN_STACK)) { if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
kunit_info(test, "CONFIG_KASAN_STACK is not enabled"); kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
...@@ -494,7 +507,9 @@ static void kasan_alloca_oob_left(struct kunit *test) ...@@ -494,7 +507,9 @@ static void kasan_alloca_oob_left(struct kunit *test)
{ {
volatile int i = 10; volatile int i = 10;
char alloca_array[i]; char alloca_array[i];
char *p = alloca_array - 1; /* See comment in kasan_global_oob. */
char *volatile array = alloca_array;
char *p = array - 1;
/* Only generic mode instruments dynamic allocas. */ /* Only generic mode instruments dynamic allocas. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
...@@ -514,7 +529,9 @@ static void kasan_alloca_oob_right(struct kunit *test) ...@@ -514,7 +529,9 @@ static void kasan_alloca_oob_right(struct kunit *test)
{ {
volatile int i = 10; volatile int i = 10;
char alloca_array[i]; char alloca_array[i];
char *p = alloca_array + i; /* See comment in kasan_global_oob. */
char *volatile array = alloca_array;
char *p = array + i;
/* Only generic mode instruments dynamic allocas. */ /* Only generic mode instruments dynamic allocas. */
if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) { if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册