From: David Laight <david.laight.linux@gmail.com>
To: Ian Rogers <irogers@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1] netlink: Remove implicit 32-bit trunction in nla_memcmp
Date: Wed, 10 Dec 2025 09:40:41 +0000 [thread overview]
Message-ID: <20251210094041.1cf3ad42@pumpkin> (raw)
In-Reply-To: <20251209224158.2322551-1-irogers@google.com>
On Tue, 9 Dec 2025 14:41:58 -0800
Ian Rogers <irogers@google.com> wrote:
> 64-bit truncation to 32-bit can result in the sign of the truncated
> value changing. The nla_memcmp function subtracts a 16-bit nla_len
> from a size_t size and so this shouldn't occur, but the code looks
> hazardous so change it to make the comparisons explicit and avoid a
> truncated subtract.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
> lib/nlattr.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/lib/nlattr.c b/lib/nlattr.c
> index be9c576b6e2d..6e1c40dd11e3 100644
> --- a/lib/nlattr.c
> +++ b/lib/nlattr.c
> @@ -854,12 +854,12 @@ EXPORT_SYMBOL(nla_memcpy);
> int nla_memcmp(const struct nlattr *nla, const void *data,
> size_t size)
> {
> - int d = nla_len(nla) - size;
> + u16 l = nla_len(nla);
Don't use u16 for locals.
Generates unnecessarily bad code.
David
>
> - if (d == 0)
> - d = memcmp(nla_data(nla), data, size);
> + if (l == size)
> + return memcmp(nla_data(nla), data, size);
>
> - return d;
> + return l > size ? 1 : -1;
> }
> EXPORT_SYMBOL(nla_memcmp);
>
prev parent reply other threads:[~2025-12-10 9:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-09 22:41 [PATCH v1] netlink: Remove implicit 32-bit trunction in nla_memcmp Ian Rogers
2025-12-10 9:40 ` David Laight [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=20251210094041.1cf3ad42@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=irogers@google.com \
--cc=linux-kernel@vger.kernel.org \
/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