[PATCH mm] kasan: fix array-bounds warnings in tests

1 view
Skip to first unread message

andrey.k...@linux.dev

unread,
Sep 24, 2022, 4:13:30 PM9/24/22
to Marco Elver, Andrew Morton, Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasa...@googlegroups.com, linu...@kvack.org, Kees Cook, linux-...@vger.kernel.org, Andrey Konovalov, kernel test robot
From: Andrey Konovalov <andre...@google.com>

GCC's -Warray-bounds option detects out-of-bounds accesses to
statically-sized allocations in krealloc out-of-bounds tests.

Use OPTIMIZER_HIDE_VAR to suppress the warning.

Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR
instead of a volatile variable.

Reported-by: kernel test robot <l...@intel.com>
Signed-off-by: Andrey Konovalov <andre...@google.com>
---
mm/kasan/kasan_test.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index 71cb402c404f..1d51efe131db 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -324,6 +324,9 @@ static void krealloc_more_oob_helper(struct kunit *test,
char *ptr1, *ptr2;
size_t middle;

+ OPTIMIZER_HIDE_VAR(size1);
+ OPTIMIZER_HIDE_VAR(size2);
+
KUNIT_ASSERT_LT(test, size1, size2);
middle = size1 + (size2 - size1) / 2;

@@ -356,6 +359,9 @@ static void krealloc_less_oob_helper(struct kunit *test,
char *ptr1, *ptr2;
size_t middle;

+ OPTIMIZER_HIDE_VAR(size1);
+ OPTIMIZER_HIDE_VAR(size2);
+
KUNIT_ASSERT_LT(test, size2, size1);
middle = size2 + (size1 - size2) / 2;

@@ -578,13 +584,14 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)
{
char *ptr;
size_t size = 64;
- volatile size_t invalid_size = size;
+ size_t invalid_size = size;

ptr = kmalloc(size, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);

memset((char *)ptr, 0, 64);
OPTIMIZER_HIDE_VAR(ptr);
+ OPTIMIZER_HIDE_VAR(invalid_size);
KUNIT_EXPECT_KASAN_FAIL(test,
memmove((char *)ptr, (char *)ptr + 4, invalid_size));
kfree(ptr);
--
2.25.1

andrey.k...@linux.dev

unread,
Sep 24, 2022, 4:23:25 PM9/24/22
to Marco Elver, Andrew Morton, Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasa...@googlegroups.com, linu...@kvack.org, Kees Cook, linux-...@vger.kernel.org, Andrey Konovalov, kernel test robot
From: Andrey Konovalov <andre...@google.com>

GCC's -Warray-bounds option detects out-of-bounds accesses to
statically-sized allocations in krealloc out-of-bounds tests.

Use OPTIMIZER_HIDE_VAR to suppress the warning.

Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR
instead of a volatile variable.

Reported-by: kernel test robot <l...@intel.com>
Signed-off-by: Andrey Konovalov <andre...@google.com>

---

Changes v1->v2:
- Hide ptr2 instead of size1 and size2 to be consistent with other
uses of OPTIMIZER_HIDE_VAR in KASAN tests.
---
mm/kasan/kasan_test.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index 71cb402c404f..dbb0a672380f 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -333,6 +333,8 @@ static void krealloc_more_oob_helper(struct kunit *test,
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);

+ OPTIMIZER_HIDE_VAR(ptr2);
+
/* All offsets up to size2 must be accessible. */
ptr2[size1 - 1] = 'x';
ptr2[size1] = 'x';
@@ -365,6 +367,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);

+ OPTIMIZER_HIDE_VAR(ptr2);
+
/* Must be accessible for all modes. */
ptr2[size2 - 1] = 'x';

@@ -578,13 +582,14 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)

Andrew Morton

unread,
Sep 25, 2022, 1:03:18 PM9/25/22
to andrey.k...@linux.dev, Marco Elver, Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasa...@googlegroups.com, linu...@kvack.org, Kees Cook, linux-...@vger.kernel.org, Andrey Konovalov, kernel test robot
On Sat, 24 Sep 2022 22:23:21 +0200 andrey.k...@linux.dev wrote:

> From: Andrey Konovalov <andre...@google.com>
>
> GCC's -Warray-bounds option detects out-of-bounds accesses to
> statically-sized allocations in krealloc out-of-bounds tests.
>
> Use OPTIMIZER_HIDE_VAR to suppress the warning.
>
> Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR
> instead of a volatile variable.
>
> ...
>
> --- a/mm/kasan/kasan_test.c
> +++ b/mm/kasan/kasan_test.c
> @@ -333,6 +333,8 @@ static void krealloc_more_oob_helper(struct kunit *test,
> ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
> KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
>
> + OPTIMIZER_HIDE_VAR(ptr2);
> +
> /* All offsets up to size2 must be accessible. */
> ptr2[size1 - 1] = 'x';
> ptr2[size1] = 'x';
> @@ -365,6 +367,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
> ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
> KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
>
> + OPTIMIZER_HIDE_VAR(ptr2);

What chance does a reader have of working out why this is here? If
"little" then a code comment would be a nice way of saving that poor
person for having to dive into the git history.

Andrey Konovalov

unread,
Sep 26, 2022, 2:08:11 PM9/26/22
to Andrew Morton, andrey.k...@linux.dev, Marco Elver, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasan-dev, Linux Memory Management List, Kees Cook, LKML, Andrey Konovalov, kernel test robot
On Sun, Sep 25, 2022 at 7:03 PM Andrew Morton <ak...@linux-foundation.org> wrote:
>
> > --- a/mm/kasan/kasan_test.c
> > +++ b/mm/kasan/kasan_test.c
> > @@ -333,6 +333,8 @@ static void krealloc_more_oob_helper(struct kunit *test,
> > ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
> > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
> >
> > + OPTIMIZER_HIDE_VAR(ptr2);
> > +
> > /* All offsets up to size2 must be accessible. */
> > ptr2[size1 - 1] = 'x';
> > ptr2[size1] = 'x';
> > @@ -365,6 +367,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
> > ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
> > KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
> >
> > + OPTIMIZER_HIDE_VAR(ptr2);
>
> What chance does a reader have of working out why this is here? If
> "little" then a code comment would be a nice way of saving that poor
> person for having to dive into the git history.

Will add in v3. Thank you, Andrew!

andrey.k...@linux.dev

unread,
Sep 26, 2022, 2:08:53 PM9/26/22
to Marco Elver, Andrew Morton, Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasa...@googlegroups.com, linu...@kvack.org, Kees Cook, linux-...@vger.kernel.org, Andrey Konovalov, kernel test robot
From: Andrey Konovalov <andre...@google.com>

GCC's -Warray-bounds option detects out-of-bounds accesses to
statically-sized allocations in krealloc out-of-bounds tests.

Use OPTIMIZER_HIDE_VAR to suppress the warning.

Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR
instead of a volatile variable.

Reported-by: kernel test robot <l...@intel.com>
Signed-off-by: Andrey Konovalov <andre...@google.com>

---

Changes v2->v3:
- Add comment for added -Warray-bounds-related OPTIMIZER_HIDE_VAR.

Changes v1->v2:
- Hide ptr2 instead of size1 and size2 to be consistent with other
uses of OPTIMIZER_HIDE_VAR in KASAN tests.
---
mm/kasan/kasan_test.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index 71cb402c404f..f50b11d84f41 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -333,6 +333,9 @@ static void krealloc_more_oob_helper(struct kunit *test,
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);

+ /* Suppress -Warray-bounds warnings. */
+ OPTIMIZER_HIDE_VAR(ptr2);
+
/* All offsets up to size2 must be accessible. */
ptr2[size1 - 1] = 'x';
ptr2[size1] = 'x';
@@ -365,6 +368,9 @@ static void krealloc_less_oob_helper(struct kunit *test,
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);

+ /* Suppress -Warray-bounds warnings. */
+ OPTIMIZER_HIDE_VAR(ptr2);
+
/* Must be accessible for all modes. */
ptr2[size2 - 1] = 'x';

@@ -578,13 +584,14 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)

Kees Cook

unread,
Sep 26, 2022, 2:23:14 PM9/26/22
to andrey.k...@linux.dev, Marco Elver, Andrew Morton, Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov, Andrey Ryabinin, kasa...@googlegroups.com, linu...@kvack.org, linux-...@vger.kernel.org, Andrey Konovalov, kernel test robot
On Mon, Sep 26, 2022 at 08:08:47PM +0200, andrey.k...@linux.dev wrote:
> From: Andrey Konovalov <andre...@google.com>
>
> GCC's -Warray-bounds option detects out-of-bounds accesses to
> statically-sized allocations in krealloc out-of-bounds tests.
>
> Use OPTIMIZER_HIDE_VAR to suppress the warning.
>
> Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR
> instead of a volatile variable.
>
> Reported-by: kernel test robot <l...@intel.com>
> Signed-off-by: Andrey Konovalov <andre...@google.com>

Reviewed-by: Kees Cook <kees...@chromium.org>

--
Kees Cook
Reply all
Reply to author
Forward
0 new messages