From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3017826C39F for ; Sat, 21 Mar 2026 14:16:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774102569; cv=none; b=YgljTjor0IsNj2EL3Yz5FeKE1TLzQw0XmW75myEYjY1rotXMUrbaAWXyd/n+CwRWoRzTFkhLWxw7mMxBiWLdFg48LeEu7POS7C0Ou9yp3c33GjpZbFI3KX1vKmIxEJ47sMn+4Oq57o1jgEU2LuR56gzNc5hZ0fIzkuHCYItH6t4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774102569; c=relaxed/simple; bh=6A4QenU1UEQgIfZKd0yawThkPFHMw6UFhWM9l9lCWFY=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=pGnF6vEJazuSuuIlHf/KCs0UHyXbl7HqJsI8Am1GTAdLKlqJb3syxRqd+kajIwW3rGE1GFes7TgUoruxTzOIkVSl+sfHUpk4DXtxqjkmh3z+PSiPtDJAnmNw6zfsQzujTKHQVvZVl2Lt1W3Piaw6iBHe0kwwqY73MrBx8wUVmCc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kvzxKFHm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kvzxKFHm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0250C19421; Sat, 21 Mar 2026 14:16:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774102568; bh=6A4QenU1UEQgIfZKd0yawThkPFHMw6UFhWM9l9lCWFY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=kvzxKFHmdzpDOPQsxcS+v91eGVmqZlskNvYa+/7a8zBKgLMyko5QM4cQyWT7D5Xrr WeUoLf7jQpo/FuwmYZzzC0raEgooIIyPGvwzwURYKeX6MbBdCwAdUzjo/IzMlwUqWH QLjcfEPZdmLPWPTQE0boqLusyiIRcLkHJGHa2IqQWpODzQOgnZ6lGvQVmFTxmjSH5R pugd5EdsvEyxIcg9P+6Zt0q6FU7r/MPKGLhwtgxdQFQexrEwN1JUEvUav8sFVikO7+ s6zrrSyNweEuAsONZqjio+b1lPyZb3ZASmnd+9pCtIx/BMUSyD45saA7ymsba97H3B XWam8p7S+0XDQ== Date: Sat, 21 Mar 2026 23:16:04 +0900 From: Masami Hiramatsu (Google) To: Petr Mladek Cc: Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , David Laight , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] lib/vsprintf: Limit the returning size to INT_MAX Message-Id: <20260321231604.d33c8cc172f3ef6fe0ef5895@kernel.org> In-Reply-To: References: <177397887883.33018.9867883986177366222.stgit@devnote2> <177397889735.33018.16696041032174901196.stgit@devnote2> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 20 Mar 2026 17:51:48 +0100 Petr Mladek wrote: > On Fri 2026-03-20 12:54:57, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) > > > > The return value of vsnprintf() can overflow INT_MAX and return > > a minus value. In the @size is checked input overflow, but it does > > not check the output, which is expected required size. > > > > This should never happen but it should be checked and limited. > > Great catch! > > > --- a/lib/vsprintf.c > > +++ b/lib/vsprintf.c > > @@ -2985,7 +2985,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) > > } > > > > /* the trailing null byte doesn't count towards the total */ > > - return str-buf; > > + return WARN_ON_ONCE(str - buf > INT_MAX) ? INT_MAX : str - buf; > > Is it guaranteed that the pointer arithmetic will be a big enough > unsigned number type? > > I would rather do a cast to be on the safe side, for example: > > return WARN_ON_ONCE((size_t)(str - buf) > INT_MAX) ? INT_MAX : str - buf; OK. > > or even use a variable to make it better readable: > > size_t ret_size; > > ret_size = str - buf; > if (WARN_ON_ONCE(ret_size > INT_MAX)) > ret_size = INT_MAX; > return ret_size; Ah, Indeed. Let me do this. Thanks! > > Best Regards, > Petr -- Masami Hiramatsu (Google)