From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 BD1E41427A; Wed, 27 May 2026 17:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779902311; cv=none; b=InTT7BdN5X+PDwniffkT8I+W8BTO978BIGhgPMLFn5WcMemk6oAZKbwJexn9zfmJ964XUj7CSrLNJ/JqC/c+7Q2f8iIBWo/QH4J6MIQol/GrdZ2A/fzTgCEeAkLVmtL6U7S7vtWlCqzR6B3+eUdJFKoykjOf5/riH/9mga8VPfY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779902311; c=relaxed/simple; bh=fEMibwf+y8t5knkzcbcbwoZgVm/ad7LJRmoe20cOiRo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=B7dSoQDvwsDYfFnttf9MurcPLDNevdHUGqImzo3aHuPtN9NfB6Be/l3qXGL1aPW8Mbua+F9iY/Ez3S+1Rh1+hMc1sYdhH8ZvPQvwIQvaXUfmYH21YuQYyVLI4ChnqYIN+JdQbA1QBuXc+8A4M8+E8gvKPP/Q8h1DsoBExNhIfnM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SIp9istw; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SIp9istw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01D221F000E9; Wed, 27 May 2026 17:18:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779902310; bh=OaYEVa01C1fbNgO8Mv5tGt8BTdhgdWVcSikLut0VmS8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=SIp9istw/5p+c+Le+xoL8j20HDFX4V4aqhBpKMprcc5cY+etxRqsaG1uegeHpuEQj 4ciC7G3QrzVZBqYT6tevwO2hOUYuJdFph3e3p7VcLfj0eCQJDf9uo35nBTDgngqAPh ILQeZfo8/f5t/j2qc4G9hjlG00bMOmlxlDzH4p6qxqu95N7tBCIxEFCtrNj8Sg5pVc T9gS3I9mW/0ZNxo+dhb69cTPs8IewviJ8x70fLVEZpjasNOBZNumu6EU4Nx/V2WL+N u9LHp2gx/0FBa5vaNVwQbdHD80B3e4jcJkOnji2xCLWTeLzm8RUXrkJerxYvvxlCZq FCHESrk9Sr8Qg== Date: Wed, 27 May 2026 10:18:23 -0700 From: Nathan Chancellor To: Alexandre Courbot Cc: Miguel Ojeda , Nicolas Schier , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , David Airlie , Simona Vetter , John Hubbard , Alistair Popple , Timur Tabi , Zhi Wang , Eliot Courtney , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org Subject: Re: [PATCH v2 1/7] scripts: modpost: detect and report truncated buf_printf() output Message-ID: <20260527171823.GA1893026@ax162> References: <20260527-nova-exports-v2-0-06de4c556d55@nvidia.com> <20260527-nova-exports-v2-1-06de4c556d55@nvidia.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260527-nova-exports-v2-1-06de4c556d55@nvidia.com> On Wed, May 27, 2026 at 08:52:17PM +0900, Alexandre Courbot wrote: > buf_printf() uses a fixed-size stack buffer. vsnprintf() returns the > number of bytes that *would* have been written to that buffer, which can > be larger than the size of said buffer if the formatted string is too > long. > > The problem is that whenever this happens buf_printf() currently passes > this length, unchecked, to buf_write(), which silently reads past the > stack buffer and copies invalid data into the output buffer. > > Fix this by detecting vsnprintf() failures and truncations before > appending to the output buffer, and report a fatal error instead of > producing corrupt symbol names. > > Signed-off-by: Alexandre Courbot Acked-by: Nathan Chancellor > --- > scripts/mod/modpost.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c > index abbcd3fc1394..0d2f1f09019b 100644 > --- a/scripts/mod/modpost.c > +++ b/scripts/mod/modpost.c > @@ -1689,8 +1689,17 @@ void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, > > va_start(ap, fmt); > len = vsnprintf(tmp, SZ, fmt, ap); > - buf_write(buf, tmp, len); > va_end(ap); > + > + if (len < 0) { > + perror("vsnprintf failed"); > + exit(1); > + } > + if (len >= SZ) > + fatal("buf_printf output truncated for string %s: %d bytes needed, %d available\n", > + tmp, len + 1, SZ); > + > + buf_write(buf, tmp, len); > } > > void buf_write(struct buffer *buf, const char *s, int len) > > -- > 2.54.0 > -- Cheers, Nathan