From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Sagar Taunk" <sagartaunk@proton.me>
Cc: "David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: net: netlink: validate attribute length before casting to `c_int`
Date: Mon, 14 Sep 2026 10:10:45 +0900 [thread overview]
Message-ID: <DLEN0K4JVUB1.1G3JJZHEJXBBC@nvidia.com> (raw)
In-Reply-To: <20260913021730.18926-1-sagartaunk@proton.me>
On Sun Sep 13, 2026 at 11:17 AM JST, Sagar Taunk wrote:
> As pointed out by Sashiko, `put()` trusted an unchecked `as` cast from
> `usize` to `c_int`. When the length exceeds `i32::MAX`, that cast
> wraps around to a negative value.
>
> `nla_put()`'s `skb_tailroom()` check treats the length as signed, so
> the wrapped negative value slips past it. Further down, `__nla_reserve()`
> and `skb_put` reinterpret the same value as unsigned, turning it into an
> enormous length and triggering a kernel panic via `skb_over_panic()`.
>
> So, validate the cast with `c_int::try_from()` instead, and return
> `EMSGSIZE` when the length doesn't fit.
>
> Signed-off-by: Sagar Taunk <sagartaunk@proton.me>
> ---
> rust/kernel/net/netlink.rs | 18 +++++++++++++-----
> 1 file changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/rust/kernel/net/netlink.rs b/rust/kernel/net/netlink.rs
> index 3c2b142a7402..f929f63b32c1 100644
> --- a/rust/kernel/net/netlink.rs
> +++ b/rust/kernel/net/netlink.rs
> @@ -88,11 +88,19 @@ fn put<T>(&mut self, attrtype: c_int, value: &T) -> Result
> T: ?Sized + IntoBytes + Immutable,
> {
> let skb = self.skb.skb.as_ptr();
> - let len = size_of_val(value);
> - let ptr = core::ptr::from_ref(value).cast::<c_void>();
> - // SAFETY: `skb` is valid by `NetlinkSkBuff` type invariants, and the provided value is
> - // readable and initialized for its `size_of` bytes.
> - to_result(unsafe { bindings::nla_put(skb, attrtype, len as c_int, ptr) })
> + let bytes = value.as_bytes();
> + // `nla_put()` takes attrlen as a plain `c_int`. If `bytes.len()`
> + // doesn't fit, an `as` cast would wrap around a negative value.
> + // Which then, would feed a huge unsigned length to `__nla_reserve()`
> + // and `skb_put()` causing it to panic via `skb_over_panic()`. So,
> + // the following check will reject it instead.
> + let len = c_int::try_from(bytes.len()).map_err(|_| EMSGSIZE)?;
I understand that you wanted to reuse `bytes` here, but the original use
of `size_of_val` is clearer and keeping it would also reduce the diff,
so let's call `c_int::try_from` on that.
Also the comment is unneeded. The change of type is justified by the
fact that `nla_put` requires a `c_int`. Any further explanation adds
confusion, and the mentioned `__nla_reserve` doesn't even appear in the
chunk. This reads like what an LLM would produce, i.e. tediously.
> + let ptr = bytes.as_ptr().cast::<c_void>();
Same here, the original line was fine so no need to change it. So it
looks like `bytes` is not needed at all.
As a general rule, and to make reviewing easier, it is a good idea to
keep the diffs are small as possible. Here the commit message says this
patch validates a value, so it should limit itself to that.
prev parent reply other threads:[~2026-09-14 1:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-13 2:17 Sagar Taunk
2026-09-14 1:10 ` Alexandre Courbot [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=DLEN0K4JVUB1.1G3JJZHEJXBBC@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=gary@garyguo.net \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=pabeni@redhat.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=sagartaunk@proton.me \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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
all inboxes | Powered by JetHome®