From nobody Thu Apr 2 23:53:17 2026 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 E05D339C011 for ; Thu, 26 Mar 2026 12:12:04 +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=1774527125; cv=none; b=eqT1+x2M0JDPTt3w3l0TyHWsQdfOlvzfMKPqy3DuJaSu87mhzRGebb4WavVNhEID+tpM5U0sULd3R/xqIKsUKVm2mEB51PyaD5dnymlz+cKbGgqKObOsZDdR8DPFUeJ5oiSKuXAqUy0rrPuXPKUc2qd0bGfGi0+fBbZuZ3UyLjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774527125; c=relaxed/simple; bh=uJztxX8Emur7umdvzpe6WBzoQPHKu9q6gCWvt6WvRS4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XuRfp9LG1muXEOGmfRmDeqFDkvjjFtHCVw12/mr5FAjiuyygtcGt49Dc/kpttARDFdovr2wazzWE9nwHnIyXhYWXGY/slEOHy7kZSORAoK2E9QLjafujd4aNowvIeblIWOQwd0AtQpJ1U+JiejkXUDZ6A3pMJMt1g5YLGeLZgjY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UJ9mWfWg; 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="UJ9mWfWg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2217C116C6; Thu, 26 Mar 2026 12:12:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774527124; bh=uJztxX8Emur7umdvzpe6WBzoQPHKu9q6gCWvt6WvRS4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UJ9mWfWgvcwPms+FjMdErzqZSSxDbQavarI7Gklb+bqrYrubneTn7N6xjT0FFTMnx hRo0hfPrG/fRZsl21AekMJ0edKN36TXmxd0j3WXEentlQjnC9a5f08TZ0TVNtm4ChQ SEnCrdEOoTasSmyuz82bO4a16BXnQCLTZNMdDsWfI5H8NnExrWqAB7moIXf2LPU/C0 MtYnjwZO7iC5opPj75H6eImfWHtZbMj1Us+Y4AzeI4OC/fboN9cdNELXBTbOT3FF/7 pj8nmWSEES7kCYjXiE01fAbu+mZNeblwyUqMWXyYFHsjrzTuGOSgQ1NY6Xw0N+Edt3 midkPvqhn9dvQ== From: "Masami Hiramatsu (Google)" To: Petr Mladek , Steven Rostedt , Andy Shevchenko Cc: Rasmus Villemoes , Sergey Senozhatsky , Andrew Morton , David Laight , linux-kernel@vger.kernel.org Subject: [PATCH v6 1/2] lib/vsprintf: Fix to check field_width and precision Date: Thu, 26 Mar 2026 21:12:00 +0900 Message-ID: <177452712047.197965.16376597502504928495.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <177452711082.197965.4952719534268072174.stgit@devnote2> References: <177452711082.197965.4952719534268072174.stgit@devnote2> User-Agent: StGit/0.19 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="utf-8" Content-Transfer-Encoding: quoted-printable 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. 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 v6: - Make the function header of set_field_width() and set_precision(), and WARN_ONCE() to be one line. Changes in v5: - Drop negative precision support. - The field and precision passed as string literals are also checked. 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 | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 800b8ac49f53..10a1b21c8920 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2640,6 +2640,18 @@ static unsigned char spec_flag(unsigned char c) return (c < sizeof(spec_flag_array)) ? spec_flag_array[c] : 0; } =20 +static void set_field_width(struct printf_spec *spec, int width) +{ + spec->field_width =3D clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); + WARN_ONCE(spec->field_width !=3D width, "field width %d out of range", wi= dth); +} + +static void set_precision(struct printf_spec *spec, int prec) +{ + spec->precision =3D clamp(prec, 0, PRECISION_MAX); + WARN_ONCE(spec->precision < prec, "precision %d too large", prec); +} + /* * Helper function to decode printf style format. * Each call decode a token from the format and return the @@ -2710,7 +2722,7 @@ struct fmt format_decode(struct fmt fmt, struct print= f_spec *spec) spec->field_width =3D -1; =20 if (isdigit(*fmt.str)) - spec->field_width =3D skip_atoi(&fmt.str); + set_field_width(spec, skip_atoi(&fmt.str)); else if (unlikely(*fmt.str =3D=3D '*')) { /* it's the next argument */ fmt.state =3D FORMAT_STATE_WIDTH; @@ -2724,9 +2736,7 @@ struct fmt format_decode(struct fmt fmt, struct print= f_spec *spec) if (unlikely(*fmt.str =3D=3D '.')) { fmt.str++; if (isdigit(*fmt.str)) { - spec->precision =3D skip_atoi(&fmt.str); - if (spec->precision < 0) - spec->precision =3D 0; + set_precision(spec, skip_atoi(&fmt.str)); } else if (*fmt.str =3D=3D '*') { /* it's the next argument */ fmt.state =3D FORMAT_STATE_PRECISION; @@ -2799,24 +2809,6 @@ struct fmt format_decode(struct fmt fmt, struct prin= tf_spec *spec) return fmt; } =20 -static void -set_field_width(struct printf_spec *spec, int width) -{ - spec->field_width =3D width; - if (WARN_ONCE(spec->field_width !=3D width, "field width %d too large", w= idth)) { - spec->field_width =3D clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); - } -} - -static void -set_precision(struct printf_spec *spec, int prec) -{ - spec->precision =3D prec; - if (WARN_ONCE(spec->precision !=3D prec, "precision %d too large", prec))= { - spec->precision =3D clamp(prec, 0, PRECISION_MAX); - } -} - /* * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate * as necessary and deal with signedness.