summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorSagar Taunk <sagartaunk@proton.me>2026-09-25 08:55:04 +0000
committerPaolo Abeni <pabeni@redhat.com>2026-09-29 12:28:03 +0200
commit0923198be4ae714b46350f742cb19ccd75bfeafa (patch)
tree28da8a6bbc408cbe2216fa1e10087a9a2b033cf8 /rust
parentf8932b2c0c9ae6fb577bcc05a2080c8498f91c0d (diff)
downloadlinux-next-0923198be4ae714b46350f742cb19ccd75bfeafa.tar.gz
linux-next-0923198be4ae714b46350f742cb19ccd75bfeafa.zip
rust: net: netlink: validate attribute length before casting to `c_int`
`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. This ultimately resulted in a kernel panic when the reinterpreted value via `__nla_reserve()` and `skb_put()` became enormous. Validate payload and header both fit together in a `u16`, rejecting any payload that wouldn't leave room for `NLA_HDRLEN`. Fixes: 5eaa5fbb6e6c ("rust: netlink: add raw netlink abstraction") Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Carlos Llamas <cmllamas@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Sagar Taunk <sagartaunk@proton.me> Link: https://patch.msgid.link/20260925085449.22553-1-sagartaunk@proton.me Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/net/netlink.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/rust/kernel/net/netlink.rs b/rust/kernel/net/netlink.rs
index 22ef3dde36fa..2f8733a694a9 100644
--- a/rust/kernel/net/netlink.rs
+++ b/rust/kernel/net/netlink.rs
@@ -11,6 +11,7 @@
use kernel::{
alloc::{self, AllocError},
error::to_result,
+ num::casts::u16_as_usize,
prelude::*,
transmute::AsBytes,
types::Opaque,
@@ -86,9 +87,17 @@ impl GenlMsg {
where
T: ?Sized + AsBytes,
{
+ // `nla_len` is a 16-bit field that encodes the total attribute length
+ // (header + payload). Subtracting the header size from `u16::MAX` gives
+ // the largest payload that still fits within that field.
+ const MAX_PAYLOAD_LEN: usize = u16_as_usize(u16::MAX) - size_of::<bindings::nlattr>();
+
let skb = self.skb.skb.as_ptr();
let len = size_of_val(value);
let ptr = core::ptr::from_ref(value).cast::<c_void>();
+ if len > MAX_PAYLOAD_LEN {
+ return Err(EMSGSIZE);
+ }
// 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) })