From nobody Thu Apr 2 22:21:29 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. From nobody Thu Apr 2 22:21:29 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 D941C3F65EE for ; Thu, 26 Mar 2026 12:12:14 +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=1774527134; cv=none; b=DROBfSdwz2urA/wFaBRX2y+ofX3VjnQVuoGTV8Rxc5yS+zJEdxh/hiortk3A8sgWcZOS44lY2//O0GbkgPs1hiAwvXJuJnGcohQFH7Kch49YtM8b6YtXq+VFnq3TH0Os3JS1g4z+64AB2t1+Pa7cpXb/hC1DbDaZbgy8/zfGSaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774527134; c=relaxed/simple; bh=oQwsNh5/G01L4HSsZoWn8fQhwt95dNW/JC4gv6dXiGQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BkppcnauT4GU3Hw8fK0sAGA/aOIt0VZ2SBIO0TilFMjyIbi9ZTG7SxO2PU0tu4mrLLU4JTUbfS3Jc/rz7bLDPJrtuEHXaHJkhBEhePNPFdBXiO7fr8pquToXTWOLq52UXwRTlWlVMW04SGr2Kf6iGnEL6uhEnDXXLxyHQJaLD7I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Zjt/UP3r; 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="Zjt/UP3r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B627C116C6; Thu, 26 Mar 2026 12:12:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774527134; bh=oQwsNh5/G01L4HSsZoWn8fQhwt95dNW/JC4gv6dXiGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zjt/UP3rEiW6/l9uUp93nAHO/WkNzEap5fEDYWeWgr7rc8RlH3pTK7NhbVmB0ybbT apGm2MO16kdUn771ZlpBmFyb+ccRQPo385bb3bHoy4s9tCzr7j3/Qm32o5c88wRt95 Ad35dBPnuHYFfHdH25Z6UojTLAbeSRL4frwl4koZYh2XxsYAoOSOsG8of3FfvLXQXp YgpvQCZ0+fta9PGvfPcbmp6+vP3PgvdDrjoNF7GP8k+0vca1E3zKczcu+KfY/dQDDM lDVemRKp9dA46dI0Ovj09+YI8y6Dx0grrafbmIB/6+4bjami0K1vhm4a6g0D04ENTl Pa8f5/x2ScFqg== 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 2/2] lib/vsprintf: Limit the returning size to INT_MAX Date: Thu, 26 Mar 2026 21:12:10 +0900 Message-ID: <177452713020.197965.3164174544083829000.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) The return value of vsnprintf() and bstr_printf() 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. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v5: - Fix bstr_printf() too. Changes in v4: - Add Petr's reviewed-by. (Thanks!) Changes in v3: - Use local variable for better readability. --- lib/vsprintf.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 10a1b21c8920..d0aa6b34f0e4 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2856,6 +2856,7 @@ static unsigned long long convert_num_spec(unsigned i= nt val, int size, struct pr int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) { char *str, *end; + size_t ret_size; struct printf_spec spec =3D {0}; struct fmt fmt =3D { .str =3D fmt_str, @@ -2975,8 +2976,12 @@ int vsnprintf(char *buf, size_t size, const char *fm= t_str, va_list args) } =20 /* the trailing null byte doesn't count towards the total */ - return str-buf; + ret_size =3D str - buf; =20 + /* Make sure the return value is within the positive integer range */ + if (WARN_ON_ONCE(ret_size > INT_MAX)) + ret_size =3D INT_MAX; + return ret_size; } EXPORT_SYMBOL(vsnprintf); =20 @@ -3280,6 +3285,7 @@ int bstr_printf(char *buf, size_t size, const char *f= mt_str, const u32 *bin_buf) struct printf_spec spec =3D {0}; char *str, *end; const char *args =3D (const char *)bin_buf; + size_t ret_size; =20 if (WARN_ON_ONCE(size > INT_MAX)) return 0; @@ -3428,7 +3434,12 @@ int bstr_printf(char *buf, size_t size, const char *= fmt_str, const u32 *bin_buf) #undef get_arg =20 /* the trailing null byte doesn't count towards the total */ - return str - buf; + ret_size =3D str - buf; + + /* Make sure the return value is within the positive integer range */ + if (WARN_ON_ONCE(ret_size > INT_MAX)) + ret_size =3D INT_MAX; + return ret_size; } EXPORT_SYMBOL_GPL(bstr_printf);