Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH] kunit: test_dev_action: Correctly cast 'priv' pointer to long*
@ 2025-10-15  7:23 Florian Schmaus
  2025-10-17  5:08 ` David Gow
  0 siblings, 1 reply; 2+ messages in thread
From: Florian Schmaus @ 2025-10-15  7:23 UTC (permalink / raw)
  To: Brendan Higgins, David Gow, Rae Moar
  Cc: linux-kselftest, kunit-dev, linux-kernel, Florian Schmaus

The previous implementation incorrectly assumed the original type of
'priv' was void**, leading to an unnecessary and misleading
cast. Correct the cast of the 'priv' pointer in test_dev_action() to
its actual type, long*, removing an unnecessary cast.

As an additional benefit, this fixes an out-of-bounds CHERI fault on
hardware with architectural capabilities. The original implementation
tried to store a capability-sized pointer using the 'priv'
pointer. However, the 'priv' pointer's capability only granted access
to the memory region of its original long type, leading to a bounds
violation since the size of a long is smaller than the size of a
capability. This change ensures that the pointer usage respects the
capabilities' bounds.

Signed-off-by: Florian Schmaus <florian.schmaus@codasip.com>
---
 lib/kunit/kunit-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
index 8c01eabd4eaf..957b67818489 100644
--- a/lib/kunit/kunit-test.c
+++ b/lib/kunit/kunit-test.c
@@ -739,7 +739,7 @@ static struct kunit_case kunit_current_test_cases[] = {
 
 static void test_dev_action(void *priv)
 {
-	*(void **)priv = (void *)1;
+	*(long*)priv = 1;
 }
 
 static void kunit_device_test(struct kunit *test)
-- 
2.51.0


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

* Re: [PATCH] kunit: test_dev_action: Correctly cast 'priv' pointer to long*
  2025-10-15  7:23 [PATCH] kunit: test_dev_action: Correctly cast 'priv' pointer to long* Florian Schmaus
@ 2025-10-17  5:08 ` David Gow
  0 siblings, 0 replies; 2+ messages in thread
From: David Gow @ 2025-10-17  5:08 UTC (permalink / raw)
  To: Florian Schmaus
  Cc: Brendan Higgins, Rae Moar, linux-kselftest, kunit-dev,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1821 bytes --]

On Wed, 15 Oct 2025 at 15:23, Florian Schmaus
<florian.schmaus@codasip.com> wrote:
>
> The previous implementation incorrectly assumed the original type of
> 'priv' was void**, leading to an unnecessary and misleading
> cast. Correct the cast of the 'priv' pointer in test_dev_action() to
> its actual type, long*, removing an unnecessary cast.
>
> As an additional benefit, this fixes an out-of-bounds CHERI fault on
> hardware with architectural capabilities. The original implementation
> tried to store a capability-sized pointer using the 'priv'
> pointer. However, the 'priv' pointer's capability only granted access
> to the memory region of its original long type, leading to a bounds
> violation since the size of a long is smaller than the size of a
> capability. This change ensures that the pointer usage respects the
> capabilities' bounds.
>
> Signed-off-by: Florian Schmaus <florian.schmaus@codasip.com>
> ---

Thanks for this.

One simple formatting issue below, otherwise this is fine.

Also, it'd be good to add a 'Fixes' tag:
Fixes: d03c720e03bd ("kunit: Add APIs for managing devices")

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  lib/kunit/kunit-test.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c
> index 8c01eabd4eaf..957b67818489 100644
> --- a/lib/kunit/kunit-test.c
> +++ b/lib/kunit/kunit-test.c
> @@ -739,7 +739,7 @@ static struct kunit_case kunit_current_test_cases[] = {
>
>  static void test_dev_action(void *priv)
>  {
> -       *(void **)priv = (void *)1;
> +       *(long*)priv = 1;

checkpatch whinges here:
ERROR: "(foo*)" should be "(foo *)"
#39: FILE: lib/kunit/kunit-test.c:742:
+       *(long*)priv = 1;


>  }
>
>  static void kunit_device_test(struct kunit *test)
> --
> 2.51.0
>

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5281 bytes --]

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

end of thread, other threads:[~2025-10-17  5:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-15  7:23 [PATCH] kunit: test_dev_action: Correctly cast 'priv' pointer to long* Florian Schmaus
2025-10-17  5:08 ` David Gow

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