From: Andrey Konovalov <andreyknvl@gmail.com>
To: Arnd Bergmann <arnd@kernel.org>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Andrey Konovalov <adech.fo@gmail.com>,
Arnd Bergmann <arnd@arndb.de>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
Marco Elver <elver@google.com>,
kasan-dev@googlegroups.com, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] kasan/test: avoid gcc warning for intentional overflow
Date: Wed, 14 Feb 2024 23:01:22 +0100 [thread overview]
Message-ID: <CA+fCnZe4Tr4FXruNgOzaXHR-u+M8h2MkZCOQMH0B8mwUy=wVig@mail.gmail.com> (raw)
In-Reply-To: <20240212111609.869266-1-arnd@kernel.org>
On Mon, Feb 12, 2024 at 12:16 PM Arnd Bergmann <arnd@kernel.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
>
> The out-of-bounds test allocates an object that is three bytes too
> short in order to validate the bounds checking. Starting with gcc-14,
> this causes a compile-time warning as gcc has grown smart enough to
> understand the sizeof() logic:
>
> mm/kasan/kasan_test.c: In function 'kmalloc_oob_16':
> mm/kasan/kasan_test.c:443:14: error: allocation of insufficient size '13' for type 'struct <anonymous>' with size '16' [-Werror=alloc-size]
> 443 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
> | ^
>
> Hide the actual computation behind a RELOC_HIDE() that ensures
> the compiler misses the intentional bug.
>
> Fixes: 3f15801cdc23 ("lib: add kasan test module")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
> mm/kasan/kasan_test.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mm/kasan/kasan_test.c b/mm/kasan/kasan_test.c
> index 318d9cec111a..2d8ae4fbe63b 100644
> --- a/mm/kasan/kasan_test.c
> +++ b/mm/kasan/kasan_test.c
> @@ -440,7 +440,8 @@ static void kmalloc_oob_16(struct kunit *test)
> /* This test is specifically crafted for the generic mode. */
> KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
>
> - ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
> + /* RELOC_HIDE to prevent gcc from warning about short alloc */
> + ptr1 = RELOC_HIDE(kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL), 0);
> KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
>
> ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
> --
> 2.39.2
>
Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com>
prev parent reply other threads:[~2024-02-14 22:01 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-12 11:15 Arnd Bergmann
2024-02-14 22:01 ` Andrey Konovalov [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+fCnZe4Tr4FXruNgOzaXHR-u+M8h2MkZCOQMH0B8mwUy=wVig@mail.gmail.com' \
--to=andreyknvl@gmail.com \
--cc=adech.fo@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryabinin.a.a@gmail.com \
--cc=vincenzo.frascino@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox