diff mbox series

kunit/overflow: Adjust for __counted_by with DEFINE_RAW_FLEX()

Message ID 20240610182301.work.272-kees@kernel.org (mailing list archive)
State Mainlined
Commit 9dd5134c61580ba4c219296c37e08ff64c109a74
Headers show
Series kunit/overflow: Adjust for __counted_by with DEFINE_RAW_FLEX() | expand

Commit Message

Kees Cook June 10, 2024, 6:23 p.m. UTC
When a flexible array structure has a __counted_by annotation, its use
with DEFINE_RAW_FLEX() will result in the count being zero-initialized.
This is expected since one doesn't want to use RAW with a counted_by
struct. Adjust the tests to check for the condition and for compiler
support.

Reported-by: Christian Schrefl <chrisi.schrefl@gmail.com>
Closes: https://lore.kernel.org/all/0bfc6b38-8bc5-4971-b6fb-dc642a73fbfe@gmail.com/
Suggested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-hardening@vger.kernel.org
---
 lib/overflow_kunit.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

Comments

Nathan Chancellor June 10, 2024, 6:33 p.m. UTC | #1
On Mon, Jun 10, 2024 at 11:23:05AM -0700, Kees Cook wrote:
> When a flexible array structure has a __counted_by annotation, its use
> with DEFINE_RAW_FLEX() will result in the count being zero-initialized.
> This is expected since one doesn't want to use RAW with a counted_by
> struct. Adjust the tests to check for the condition and for compiler
> support.
> 
> Reported-by: Christian Schrefl <chrisi.schrefl@gmail.com>
> Closes: https://lore.kernel.org/all/0bfc6b38-8bc5-4971-b6fb-dc642a73fbfe@gmail.com/
> Suggested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Kees Cook <kees@kernel.org>

Seems reasonable to me.

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

> ---
> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: linux-hardening@vger.kernel.org
> ---
>  lib/overflow_kunit.c | 20 +++++++++++++++++---
>  1 file changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c
> index 4ef31b0bb74d..d305b0c054bb 100644
> --- a/lib/overflow_kunit.c
> +++ b/lib/overflow_kunit.c
> @@ -1178,14 +1178,28 @@ struct foo {
>  	s16 array[] __counted_by(counter);
>  };
>  
> +struct bar {
> +	int a;
> +	u32 counter;
> +	s16 array[];
> +};
> +
>  static void DEFINE_FLEX_test(struct kunit *test)
>  {
> -	DEFINE_RAW_FLEX(struct foo, two, array, 2);
> +	/* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
> +	DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2);
> +#if __has_attribute(__counted_by__)
> +	int expected_raw_size = sizeof(struct foo);
> +#else
> +	int expected_raw_size = sizeof(struct foo) + 2 * sizeof(s16);
> +#endif
> +	/* Without annotation, it will always be on-stack size. */
> +	DEFINE_RAW_FLEX(struct bar, two, array, 2);
>  	DEFINE_FLEX(struct foo, eight, array, counter, 8);
>  	DEFINE_FLEX(struct foo, empty, array, counter, 0);
>  
> -	KUNIT_EXPECT_EQ(test, __struct_size(two),
> -			sizeof(struct foo) + sizeof(s16) + sizeof(s16));
> +	KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), expected_raw_size);
> +	KUNIT_EXPECT_EQ(test, __struct_size(two), sizeof(struct bar) + 2 * sizeof(s16));
>  	KUNIT_EXPECT_EQ(test, __struct_size(eight), 24);
>  	KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo));
>  }
> -- 
> 2.34.1
>
Christian Schrefl June 10, 2024, 7:20 p.m. UTC | #2
On 10.06.24 8:23 PM, Kees Cook wrote:
> When a flexible array structure has a __counted_by annotation, its use
> with DEFINE_RAW_FLEX() will result in the count being zero-initialized.
> This is expected since one doesn't want to use RAW with a counted_by
> struct. Adjust the tests to check for the condition and for compiler
> support.
> 
> Reported-by: Christian Schrefl <chrisi.schrefl@gmail.com>
> Closes: https://lore.kernel.org/all/0bfc6b38-8bc5-4971-b6fb-dc642a73fbfe@gmail.com/
> Suggested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
Thanks for the quick fix!

Seems reasonable and the test passes now.

Tested-by: Christian Schrefl <chrisi.schrefl@gmail.com>
Reviewed-by: Christian Schrefl <chrisi.schrefl@gmail.com>

> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: linux-hardening@vger.kernel.org
> ---
>  lib/overflow_kunit.c | 20 +++++++++++++++++---
>  1 file changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c
> index 4ef31b0bb74d..d305b0c054bb 100644
> --- a/lib/overflow_kunit.c
> +++ b/lib/overflow_kunit.c
> @@ -1178,14 +1178,28 @@ struct foo {
>  	s16 array[] __counted_by(counter);
>  };
>  
> +struct bar {
> +	int a;
> +	u32 counter;
> +	s16 array[];
> +};
> +
>  static void DEFINE_FLEX_test(struct kunit *test)
>  {
> -	DEFINE_RAW_FLEX(struct foo, two, array, 2);
> +	/* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
> +	DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2);
> +#if __has_attribute(__counted_by__)
> +	int expected_raw_size = sizeof(struct foo);
> +#else
> +	int expected_raw_size = sizeof(struct foo) + 2 * sizeof(s16);
> +#endif
> +	/* Without annotation, it will always be on-stack size. */
> +	DEFINE_RAW_FLEX(struct bar, two, array, 2);
>  	DEFINE_FLEX(struct foo, eight, array, counter, 8);
>  	DEFINE_FLEX(struct foo, empty, array, counter, 0);
>  
> -	KUNIT_EXPECT_EQ(test, __struct_size(two),
> -			sizeof(struct foo) + sizeof(s16) + sizeof(s16));
> +	KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), expected_raw_size);
> +	KUNIT_EXPECT_EQ(test, __struct_size(two), sizeof(struct bar) + 2 * sizeof(s16));
>  	KUNIT_EXPECT_EQ(test, __struct_size(eight), 24);
>  	KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo));
>  }
diff mbox series

Patch

diff --git a/lib/overflow_kunit.c b/lib/overflow_kunit.c
index 4ef31b0bb74d..d305b0c054bb 100644
--- a/lib/overflow_kunit.c
+++ b/lib/overflow_kunit.c
@@ -1178,14 +1178,28 @@  struct foo {
 	s16 array[] __counted_by(counter);
 };
 
+struct bar {
+	int a;
+	u32 counter;
+	s16 array[];
+};
+
 static void DEFINE_FLEX_test(struct kunit *test)
 {
-	DEFINE_RAW_FLEX(struct foo, two, array, 2);
+	/* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
+	DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2);
+#if __has_attribute(__counted_by__)
+	int expected_raw_size = sizeof(struct foo);
+#else
+	int expected_raw_size = sizeof(struct foo) + 2 * sizeof(s16);
+#endif
+	/* Without annotation, it will always be on-stack size. */
+	DEFINE_RAW_FLEX(struct bar, two, array, 2);
 	DEFINE_FLEX(struct foo, eight, array, counter, 8);
 	DEFINE_FLEX(struct foo, empty, array, counter, 0);
 
-	KUNIT_EXPECT_EQ(test, __struct_size(two),
-			sizeof(struct foo) + sizeof(s16) + sizeof(s16));
+	KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), expected_raw_size);
+	KUNIT_EXPECT_EQ(test, __struct_size(two), sizeof(struct bar) + 2 * sizeof(s16));
 	KUNIT_EXPECT_EQ(test, __struct_size(eight), 24);
 	KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo));
 }