linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset
@ 2023-12-12 23:26 Nico Pache
  2023-12-13 14:34 ` Andrey Konovalov
  0 siblings, 1 reply; 4+ messages in thread
From: Nico Pache @ 2023-12-12 23:26 UTC (permalink / raw)
  To: linux-kernel, linux-mm, kasan-dev
  Cc: akpm, vincenzo.frascino, dvyukov, andreyknvl, glider, ryabinin.a.a

similar to commit 09c6304e38e4 ("kasan: test: fix compatibility with
FORTIFY_SOURCE") the kernel is panicing in kmalloc_oob_memset_*.

This is due to the `ptr` not being hidden from the optimizer which would
disable the runtime fortify string checker.

kernel BUG at lib/string_helpers.c:1048!
Call Trace:
[<00000000272502e2>] fortify_panic+0x2a/0x30
([<00000000272502de>] fortify_panic+0x26/0x30)
[<001bffff817045c4>] kmalloc_oob_memset_2+0x22c/0x230 [kasan_test]

Hide the `ptr` variable from the optimizer to fix the kernel panic.
Also define a size2 variable and hide that as well. This cleans up
the code and follows the same convention as other tests.

Signed-off-by: Nico Pache <npache@redhat.com>
---
 mm/kasan/kasan_test.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
index 8281eb42464b..5aeba810ba70 100644
--- a/mm/kasan/kasan_test.c
+++ b/mm/kasan/kasan_test.c
@@ -493,14 +493,17 @@ static void kmalloc_oob_memset_2(struct kunit *test)
 {
 	char *ptr;
 	size_t size = 128 - KASAN_GRANULE_SIZE;
+	size_t size2 = 2;
 
 	KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
 
 	ptr = kmalloc(size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+	OPTIMIZER_HIDE_VAR(ptr);
 	OPTIMIZER_HIDE_VAR(size);
-	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
+	OPTIMIZER_HIDE_VAR(size2);
+	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, size2));
 	kfree(ptr);
 }
 
@@ -508,14 +511,17 @@ static void kmalloc_oob_memset_4(struct kunit *test)
 {
 	char *ptr;
 	size_t size = 128 - KASAN_GRANULE_SIZE;
+	size_t size2 = 4;
 
 	KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
 
 	ptr = kmalloc(size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+	OPTIMIZER_HIDE_VAR(ptr);
 	OPTIMIZER_HIDE_VAR(size);
-	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
+	OPTIMIZER_HIDE_VAR(size2);
+	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, size2));
 	kfree(ptr);
 }
 
@@ -523,14 +529,17 @@ static void kmalloc_oob_memset_8(struct kunit *test)
 {
 	char *ptr;
 	size_t size = 128 - KASAN_GRANULE_SIZE;
+	size_t size2 = 8;
 
 	KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
 
 	ptr = kmalloc(size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+	OPTIMIZER_HIDE_VAR(ptr);
 	OPTIMIZER_HIDE_VAR(size);
-	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
+	OPTIMIZER_HIDE_VAR(size2);
+	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, size2));
 	kfree(ptr);
 }
 
@@ -538,14 +547,17 @@ static void kmalloc_oob_memset_16(struct kunit *test)
 {
 	char *ptr;
 	size_t size = 128 - KASAN_GRANULE_SIZE;
+	size_t size2 = 16;
 
 	KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
 
 	ptr = kmalloc(size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+	OPTIMIZER_HIDE_VAR(ptr);
 	OPTIMIZER_HIDE_VAR(size);
-	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
+	OPTIMIZER_HIDE_VAR(size2);
+	KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, size2));
 	kfree(ptr);
 }
 
-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset
  2023-12-12 23:26 [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset Nico Pache
@ 2023-12-13 14:34 ` Andrey Konovalov
  2023-12-13 21:42   ` Nico Pache
  0 siblings, 1 reply; 4+ messages in thread
From: Andrey Konovalov @ 2023-12-13 14:34 UTC (permalink / raw)
  To: Nico Pache
  Cc: linux-kernel, linux-mm, kasan-dev, akpm, vincenzo.frascino,
	dvyukov, glider, ryabinin.a.a

On Wed, Dec 13, 2023 at 12:27 AM Nico Pache <npache@redhat.com> wrote:
>
> similar to commit 09c6304e38e4 ("kasan: test: fix compatibility with
> FORTIFY_SOURCE") the kernel is panicing in kmalloc_oob_memset_*.
>
> This is due to the `ptr` not being hidden from the optimizer which would
> disable the runtime fortify string checker.
>
> kernel BUG at lib/string_helpers.c:1048!
> Call Trace:
> [<00000000272502e2>] fortify_panic+0x2a/0x30
> ([<00000000272502de>] fortify_panic+0x26/0x30)
> [<001bffff817045c4>] kmalloc_oob_memset_2+0x22c/0x230 [kasan_test]
>
> Hide the `ptr` variable from the optimizer to fix the kernel panic.
> Also define a size2 variable and hide that as well. This cleans up
> the code and follows the same convention as other tests.
>
> Signed-off-by: Nico Pache <npache@redhat.com>
> ---
>  mm/kasan/kasan_test.c | 20 ++++++++++++++++----
>  1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
> index 8281eb42464b..5aeba810ba70 100644
> --- a/mm/kasan/kasan_test.c
> +++ b/mm/kasan/kasan_test.c
> @@ -493,14 +493,17 @@ static void kmalloc_oob_memset_2(struct kunit *test)
>  {
>         char *ptr;
>         size_t size = 128 - KASAN_GRANULE_SIZE;
> +       size_t size2 = 2;

Let's name this variable access_size or memset_size. Here and in the
other changed tests.

>         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
>
> +       OPTIMIZER_HIDE_VAR(ptr);
>         OPTIMIZER_HIDE_VAR(size);
> -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
> +       OPTIMIZER_HIDE_VAR(size2);
> +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, size2));
>         kfree(ptr);
>  }
>
> @@ -508,14 +511,17 @@ static void kmalloc_oob_memset_4(struct kunit *test)
>  {
>         char *ptr;
>         size_t size = 128 - KASAN_GRANULE_SIZE;
> +       size_t size2 = 4;
>
>         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
>
> +       OPTIMIZER_HIDE_VAR(ptr);
>         OPTIMIZER_HIDE_VAR(size);
> -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
> +       OPTIMIZER_HIDE_VAR(size2);
> +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, size2));
>         kfree(ptr);
>  }
>
> @@ -523,14 +529,17 @@ static void kmalloc_oob_memset_8(struct kunit *test)
>  {
>         char *ptr;
>         size_t size = 128 - KASAN_GRANULE_SIZE;
> +       size_t size2 = 8;
>
>         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
>
> +       OPTIMIZER_HIDE_VAR(ptr);
>         OPTIMIZER_HIDE_VAR(size);
> -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
> +       OPTIMIZER_HIDE_VAR(size2);
> +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, size2));
>         kfree(ptr);
>  }
>
> @@ -538,14 +547,17 @@ static void kmalloc_oob_memset_16(struct kunit *test)
>  {
>         char *ptr;
>         size_t size = 128 - KASAN_GRANULE_SIZE;
> +       size_t size2 = 16;
>
>         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
>
> +       OPTIMIZER_HIDE_VAR(ptr);
>         OPTIMIZER_HIDE_VAR(size);
> -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
> +       OPTIMIZER_HIDE_VAR(size2);
> +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, size2));
>         kfree(ptr);
>  }
>
> --
> 2.43.0
>

With the fix mentioned above addressed:

Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset
  2023-12-13 14:34 ` Andrey Konovalov
@ 2023-12-13 21:42   ` Nico Pache
  2023-12-13 23:42     ` Andrey Konovalov
  0 siblings, 1 reply; 4+ messages in thread
From: Nico Pache @ 2023-12-13 21:42 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: linux-kernel, linux-mm, kasan-dev, akpm, vincenzo.frascino,
	dvyukov, glider, ryabinin.a.a

On Wed, Dec 13, 2023 at 7:34 AM Andrey Konovalov <andreyknvl@gmail.com> wrote:
>
> On Wed, Dec 13, 2023 at 12:27 AM Nico Pache <npache@redhat.com> wrote:
> >
> > similar to commit 09c6304e38e4 ("kasan: test: fix compatibility with
> > FORTIFY_SOURCE") the kernel is panicing in kmalloc_oob_memset_*.
> >
> > This is due to the `ptr` not being hidden from the optimizer which would
> > disable the runtime fortify string checker.
> >
> > kernel BUG at lib/string_helpers.c:1048!
> > Call Trace:
> > [<00000000272502e2>] fortify_panic+0x2a/0x30
> > ([<00000000272502de>] fortify_panic+0x26/0x30)
> > [<001bffff817045c4>] kmalloc_oob_memset_2+0x22c/0x230 [kasan_test]
> >
> > Hide the `ptr` variable from the optimizer to fix the kernel panic.
> > Also define a size2 variable and hide that as well. This cleans up
> > the code and follows the same convention as other tests.
> >
> > Signed-off-by: Nico Pache <npache@redhat.com>
> > ---
> >  mm/kasan/kasan_test.c | 20 ++++++++++++++++----
> >  1 file changed, 16 insertions(+), 4 deletions(-)
> >
> > diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
> > index 8281eb42464b..5aeba810ba70 100644
> > --- a/mm/kasan/kasan_test.c
> > +++ b/mm/kasan/kasan_test.c
> > @@ -493,14 +493,17 @@ static void kmalloc_oob_memset_2(struct kunit *test)
> >  {
> >         char *ptr;
> >         size_t size = 128 - KASAN_GRANULE_SIZE;
> > +       size_t size2 = 2;
>
> Let's name this variable access_size or memset_size. Here and in the
> other changed tests.

Hi Andrey,

I agree that is a better variable name, but I chose size2 because
other kasan tests follow the same pattern.

Please let me know if you still want me to update it given that info
and I'll send a V2.

Cheers,
-- Nico

>
> >         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
> >
> >         ptr = kmalloc(size, GFP_KERNEL);
> >         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> >
> > +       OPTIMIZER_HIDE_VAR(ptr);
> >         OPTIMIZER_HIDE_VAR(size);
> > -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, 2));
> > +       OPTIMIZER_HIDE_VAR(size2);
> > +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 1, 0, size2));
> >         kfree(ptr);
> >  }
> >
> > @@ -508,14 +511,17 @@ static void kmalloc_oob_memset_4(struct kunit *test)
> >  {
> >         char *ptr;
> >         size_t size = 128 - KASAN_GRANULE_SIZE;
> > +       size_t size2 = 4;
> >
> >         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
> >
> >         ptr = kmalloc(size, GFP_KERNEL);
> >         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> >
> > +       OPTIMIZER_HIDE_VAR(ptr);
> >         OPTIMIZER_HIDE_VAR(size);
> > -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, 4));
> > +       OPTIMIZER_HIDE_VAR(size2);
> > +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 3, 0, size2));
> >         kfree(ptr);
> >  }
> >
> > @@ -523,14 +529,17 @@ static void kmalloc_oob_memset_8(struct kunit *test)
> >  {
> >         char *ptr;
> >         size_t size = 128 - KASAN_GRANULE_SIZE;
> > +       size_t size2 = 8;
> >
> >         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
> >
> >         ptr = kmalloc(size, GFP_KERNEL);
> >         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> >
> > +       OPTIMIZER_HIDE_VAR(ptr);
> >         OPTIMIZER_HIDE_VAR(size);
> > -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, 8));
> > +       OPTIMIZER_HIDE_VAR(size2);
> > +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 7, 0, size2));
> >         kfree(ptr);
> >  }
> >
> > @@ -538,14 +547,17 @@ static void kmalloc_oob_memset_16(struct kunit *test)
> >  {
> >         char *ptr;
> >         size_t size = 128 - KASAN_GRANULE_SIZE;
> > +       size_t size2 = 16;
> >
> >         KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test);
> >
> >         ptr = kmalloc(size, GFP_KERNEL);
> >         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> >
> > +       OPTIMIZER_HIDE_VAR(ptr);
> >         OPTIMIZER_HIDE_VAR(size);
> > -       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, 16));
> > +       OPTIMIZER_HIDE_VAR(size2);
> > +       KUNIT_EXPECT_KASAN_FAIL(test, memset(ptr + size - 15, 0, size2));
> >         kfree(ptr);
> >  }
> >
> > --
> > 2.43.0
> >
>
> With the fix mentioned above addressed:
>
> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset
  2023-12-13 21:42   ` Nico Pache
@ 2023-12-13 23:42     ` Andrey Konovalov
  0 siblings, 0 replies; 4+ messages in thread
From: Andrey Konovalov @ 2023-12-13 23:42 UTC (permalink / raw)
  To: Nico Pache
  Cc: linux-kernel, linux-mm, kasan-dev, akpm, vincenzo.frascino,
	dvyukov, glider, ryabinin.a.a

On Wed, Dec 13, 2023 at 10:42 PM Nico Pache <npache@redhat.com> wrote:
>
> > > diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
> > > index 8281eb42464b..5aeba810ba70 100644
> > > --- a/mm/kasan/kasan_test.c
> > > +++ b/mm/kasan/kasan_test.c
> > > @@ -493,14 +493,17 @@ static void kmalloc_oob_memset_2(struct kunit *test)
> > >  {
> > >         char *ptr;
> > >         size_t size = 128 - KASAN_GRANULE_SIZE;
> > > +       size_t size2 = 2;
> >
> > Let's name this variable access_size or memset_size. Here and in the
> > other changed tests.
>
> Hi Andrey,
>
> I agree that is a better variable name, but I chose size2 because
> other kasan tests follow the same pattern.

These other tests use size1 and size2 to refer to different sizes of
krealloc allocations, which seems reasonable.

> Please let me know if you still want me to update it given that info
> and I'll send a V2.

Yes, please update the name.

Thank you!


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-12-13 23:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-12 23:26 [PATCH] kunit: kasan_test: disable fortify string checker on kmalloc_oob_memset Nico Pache
2023-12-13 14:34 ` Andrey Konovalov
2023-12-13 21:42   ` Nico Pache
2023-12-13 23:42     ` Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox