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 0D5AD23B612 for ; Wed, 25 Mar 2026 13:27:10 +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=1774445231; cv=none; b=D+Awt5LXrY8erslfUTYU6gkV8k8gd6MKNwhX39cl2ef8geRkM16qPJe3+6I99vRuj+lx/5Z2fVcp2CLSj8kUHqdieqVU+YTHcH/4traU6Guk68Ti8UCGxxOF92tm1RSQpoZyWiHYEylEPHPDaKNqXsKXjgU/L5Bu/TdCzlGsfAQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774445231; c=relaxed/simple; bh=SsoLMPUT14vLflYY3Sk583QfLmG3Ikt04D5+rckan1k=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=Kp9tMNUelIiLr8caocKb1WLFjNs9HL+JHfaho95YWX+rxvMP4UVvs7NB2MsBGReQ3KHKB1GPOHsIGSzS0rrEWvgosrzAkBS5pd7l/gImvvCfwwZEWfwaPIZHBNYK0CcqX0mDISvnHum928B9oRwRltoAA4OKvONS3R2HsMAS4NE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Dz8tGtXP; 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="Dz8tGtXP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B907C4CEF7; Wed, 25 Mar 2026 13:27:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774445230; bh=SsoLMPUT14vLflYY3Sk583QfLmG3Ikt04D5+rckan1k=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Dz8tGtXPhMlK2dFUPxgFH8s5yK50ttSy/bZlfirvtH3eGAXmEY43gVkpnyIbf+m39 oMNoQ48GKTlHlJm6vIx/fOWTw5OaJEew5kwuX/EyfYWjmMDliyUuf9rH5rvvblq8jO ooCL3EJrHoxjxqasmgAmTHUL/hNS5Q8IFIEeEq2eV8O4Bca3MGVQCtclTXkJs8SUwb Y9lG4uc/trieRx7c6rNkmIImEggafphkiWVzDT6+Og46GaYiYgCkEJIvkySJhtsPuN 9bCr+jyp9l7fpAOfyv1vzQ2QVJcQfc2sDeu63zg7aEnF3X5oRhd1Wt9mSes8ZWj0DL JhYYNk3tMowEA== Date: Wed, 25 Mar 2026 22:27:05 +0900 From: Masami Hiramatsu (Google) To: "Masami Hiramatsu (Google)" Cc: Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , David Laight , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 1/2] lib/vsprintf: Fix to check field_width and precision Message-Id: <20260325222705.9bf10b7654a68e0009443715@kernel.org> In-Reply-To: <177440551685.147866.4375769344976474036.stgit@devnote2> References: <177440550682.147866.1854734911195480940.stgit@devnote2> <177440551685.147866.4375769344976474036.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 Wed, 25 Mar 2026 11:25:16 +0900 "Masami Hiramatsu (Google)" wrote: > From: Masami Hiramatsu (Google) > > Check the field_width and presition correctly. Previously it depends > on the bitfield conversion from int to check out-of-range error. > However, commit 938df695e98d ("vsprintf: associate the format state > with the format pointer") changed those fields to int. > We need to check the out-of-range correctly without bitfield > conversion. > Hmm, I also found that width/precision passed as string literals also missed the range check. Thanks, > Fixes: 938df695e98d ("vsprintf: associate the format state with the format pointer") > Reported-by: David Laight > Closes: https://lore.kernel.org/all/20260318151250.40fef0ab@pumpkin/ > Signed-off-by: Masami Hiramatsu (Google) > --- > Changes in v4: > - Do clamp() first. > - Accept negative precision (this means no precision) . > - Change the warning message for width. > Changes in v3: > - Check and update width and precision before assigning to spec. > Changes in v2: > - Fix to use logical split. > --- > lib/vsprintf.c | 17 ++++++----------- > 1 file changed, 6 insertions(+), 11 deletions(-) > > diff --git a/lib/vsprintf.c b/lib/vsprintf.c > index 800b8ac49f53..5fa8f69030be 100644 > --- a/lib/vsprintf.c > +++ b/lib/vsprintf.c > @@ -2679,9 +2679,6 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) > > /* we finished early by reading the precision */ > if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) { > - if (spec->precision < 0) > - spec->precision = 0; > - > fmt.state = FORMAT_STATE_NONE; > goto qualifier; > } > @@ -2802,19 +2799,17 @@ struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) > static void > set_field_width(struct printf_spec *spec, int width) > { > - spec->field_width = width; > - if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { > - spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); > - } > + spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); > + WARN_ONCE(spec->field_width != width, "field width %d out of range", > + width); > } > > static void > set_precision(struct printf_spec *spec, int prec) > { > - spec->precision = prec; > - if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { > - spec->precision = clamp(prec, 0, PRECISION_MAX); > - } > + /* We allow negative precision, but treat it as if there was no precision. */ > + spec->precision = clamp(prec, -1, PRECISION_MAX); > + WARN_ONCE(spec->precision < prec, "precision %d too large", prec); > } > > /* > > -- Masami Hiramatsu (Google)