From nobody Fri Sep 25 07:23:44 2026 Received: from mail-10629.protonmail.ch (mail-10629.protonmail.ch [79.135.106.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5166B4A8FEC for ; Tue, 15 Sep 2026 16:40:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.29 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789490424; cv=none; b=QJgWvzy+fi0XWVOZWi2HE6zy+QAQ+x2E1jzTstqKoahcTJT8Z2qD/Rc1phBpxcorc0u4OdgEBG+DdSVhsE9/GM0PpP1XFWl4y8AxcM2XfzTM9haed2gsmVw1iX5esIATN4s4Asr8gPSP5r6Ypt3Cn6lA9dcpFh3gRCWU7BVAs5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789490424; c=relaxed/simple; bh=S1DZx7X55e2WW66cR3XPfdNeDoS82+Wq4VfjwuWWrVM=; h=Date:To:From:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=oICmLbLfYnC+c5TxTGC+/wJQaCK5cGAnqJHSO9VNY1jymhjhW+bQyx0qySyyOjG1HJxdtA7M9rOvjOF8q9RsBwrdrf94C0lBAZXp9roZoPBeIPip3d+32jQx+KEyn7heafV9yn7M0Em/8SgOmRZ5rAjut8x07AJy3MbVvGlXB4U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=no3A9ZrD; arc=none smtp.client-ip=79.135.106.29 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="no3A9ZrD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1789490416; x=1789749616; bh=dnEl1BcBNFLTKuLcIgJ5HH7MiCpVZFsVbpLITtItNxM=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=no3A9ZrDFxBfOjyFFHzlKl8rwkbsWcWm7Ay9n8wdVSMr9xI5Wd2co8c3u58aIyYas Dl6BrN/qHtH91r3PptbeB1+ISkLej8XuqDffaUl5VXW6ljSW4UYnlgUSMGpDioduMt pMZzoGnvhbKnf7pRMxWMaK+wL2tv8QHLZ579XRZTHvlGxx7GHPFB7icrk2pLQBLIi1 uy/BHUr2+Idcvw7dwzgfzR8xco3429Xs6vT4b44f/OXHRBuTOf/dJgUdKNk+xrO2LA vCd16ra+WBym7Ar8P4NeEn+CdyaNGYUctvnA4ttWRD+NV7tQdG9iZCeZigHIsx3v7Q iJ021nFDs2JhQ== Date: Tue, 15 Sep 2026 16:40:10 +0000 To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?Q?Onur_=C3=96zkan?= , netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org From: Sagar Taunk Cc: Sagar Taunk Subject: [PATCH v2] rust: net: netlink: validate attribute length before casting to `c_int` Message-ID: <20260915164000.124340-1-sagartaunk@proton.me> Feedback-ID: 130121444:user:proton X-Pm-Message-ID: 1147f2041977a0213fbf642dabc16417746aaf94 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" `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`. Signed-off-by: Sagar Taunk Reviewed-by: Alice Ryhl --- Changes Since V1:=20 Tried to make the diff smaller as pointed out by Alexandre Courbot. Moreover, as pointed out by Sashiko,`nlattr` is stored in a 16-bit=20 field which houses both the header and the payload, so make the check=20 verify that there is enough space left for the header to fit with the=20 payload.=20 rust/kernel/net/netlink.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/kernel/net/netlink.rs b/rust/kernel/net/netlink.rs index a2f4bd171dcf..667667346f96 100644 --- a/rust/kernel/net/netlink.rs +++ b/rust/kernel/net/netlink.rs @@ -90,9 +90,14 @@ fn put(&mut self, attrtype: c_int, value: &T) -> Resu= lt where T: ?Sized + IntoBytes + Immutable, { + let max_payload_len =3D u16::MAX as usize - size_of::(); + let skb =3D self.skb.skb.as_ptr(); let len =3D size_of_val(value); let ptr =3D core::ptr::from_ref(value).cast::(); + 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) }) --=20 2.55.0