From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58994C83F05 for ; Sun, 6 Jul 2025 17:37:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA7DE6B03F8; Sun, 6 Jul 2025 13:37:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D804F6B03F9; Sun, 6 Jul 2025 13:37:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBCD76B03FA; Sun, 6 Jul 2025 13:37:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B79E46B03F8 for ; Sun, 6 Jul 2025 13:37:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4BC411401A4 for ; Sun, 6 Jul 2025 17:37:39 +0000 (UTC) X-FDA: 83634547038.20.947C652 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 93D6D1C0007 for ; Sun, 6 Jul 2025 17:37:37 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lJgCPADR; spf=pass (imf18.hostedemail.com: domain of alx@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751823457; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+ox21bF4kIjDKl8k0gbtV2JTxZR2UJ8wCg/9TILGXwg=; b=XOo/i+5pEGXe/uAakvK8N3swnmS83KSUrVg5iD32xnC4HBYHyLkHnv2E7gB7uP0whVR9sW sQCKr9kOqbgjbpMXYowKpsK0W3p1QSsC0Pa+6kXJ8ptgwAtojPVbYk+luKl7BmcKJJducv PhMCSAp24QPtlGpG8jYjtrFGw64no+o= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lJgCPADR; spf=pass (imf18.hostedemail.com: domain of alx@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751823457; a=rsa-sha256; cv=none; b=DfHnQH/fr1mqJHQmie1EGveNSMKKVDeYe05eVD0eapx4pCehKlXg/hsXgWq7L/cJW1yMLO h+eTsm7xkBbshQJYtAoR4A16iWrTLVvZLLfz2O9rbazeSrbvRVWpzOGXWSjmMrdHq1htbq cqmo0Sdg26b16eoLRvs272Qu/npEuso= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7BCB95C5716; Sun, 6 Jul 2025 17:37:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D591BC4CEEE; Sun, 6 Jul 2025 17:37:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751823456; bh=Yezh1PpcDSnMIsQt/1ue+JNou0BqpTIh4TMeGHqSd4o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lJgCPADR2ekdfgwOWk/dsELsGQzgTRkDGth8uwcI8eKSU21X/oqBbIscR+qFLH96s 4yU6AhXki8Dkv+qdFY5rUeO4XqXvtCqqlKQoEKWnUm1FC7IqeLP0xQQ7AHDBuPLXvi R1ryKK55/ujdlp9C9mqF993dg7PFuyyQPhAVhIsGErECvqXMeb9+3M0NiRldjOezLh zB646vD1yem0Vo0WlT/eVudEaML7JKwZtstdg4KWBZVXUZlYGeWcRXFsFbv/j2YKU/ 3VZJ2zHBf9CyM0t1si6JbNFmduZesKiE58rabaI8+eJFVe+uySZI8dPlQcgCJ4D1So 4vLdGj4K0bzag== Date: Sun, 6 Jul 2025 19:37:33 +0200 From: Alejandro Colomar To: linux-mm@kvack.org, linux-hardening@vger.kernel.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , shadow <~hallyn/shadow@lists.sr.ht>, linux-kernel@vger.kernel.org, Andrew Morton , kasan-dev@googlegroups.com, Dmitry Vyukov , Alexander Potapenko , Marco Elver , Christoph Lameter , David Rientjes , Vlastimil Babka , Roman Gushchin , Harry Yoo , Andrew Clayton Subject: [RFC v2 1/5] vsprintf: Add [v]seprintf(), [v]stprintf() Message-ID: <64334f0b94d6b853e6104ec4f89bcf910978db76.1751823326.git.alx@kernel.org> X-Mailer: git-send-email 2.50.0 References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 93D6D1C0007 X-Stat-Signature: e5rwdpyjoceh59kweuqkswz4m37yz1ut X-Rspam-User: X-HE-Tag: 1751823457-69681 X-HE-Meta: U2FsdGVkX19ktQusFzHJ+Q3Oznxc+4fwNa0NV4fuF+7HVrQKhEPf/VyhSWK7rKeDVpiJohf3HC2jZuiwsuPhD312msyFGhEHxUhCle2Ag55T1LJl9vSrx6kaqwC8wu216jOvv4SHXCY51WmxwYVHyRjvVs8iq+hqoKnOCztb5qarVrx9hXMIsYqbAGG4f3hzsYzTaW/WhUa2UYmm8V5OB6DMJn2J/GNhIuaiSjH7FZ+VP+fJr6TmwByUTehiugmUXGEsGOWPPTwm1dx2lLoWqmoFl+SkSYRw19PoSP+4CChuPB3/CNYIYOfZvwBCkL/BOm821WL9G30S10jkm9q63SyoEr/AQ/xaPmdCLeLsO3/QPjmso8NqgmH+EzJ3lAvu8Tzv8hrRAKxF8q/80E26CVYR2NKNbkVqgkgrbD04YJdxIPYRYOCvgu3OIo5H6FT2d+MBYGFKH6/bXpJpQNboNKyxRV2Ipz6PfvYmhdJ3vzhOsCmQ4I5n4POfaqNhMnraLdEegwEXzhNM72jlSWoyymjadmOi7uHHhHzTSQhGkqq9ajM38Bkc5F2mhljSIg7aw03DdMmVfbsjaSTf3qynkmOj05CCYFWFVs6Jseib+Au6/oifHJwIS8+4n3JKO0d1sVF9PKJIoLyOQh1TKRmSagvpduKjlkAk8v/IHnNEpp2539CP00jlRz8t0F3LzY+xWXuhVxNMJzsFophtzEeS6d22lnEPsNvyAdfcYuui6eJQRm52WaE8kXXiRiXwqSJMk1Wd5pYGwCelxGeN3lcXMuqo62glUhl+eQgoFE1ysuSZA0d4R0Z4Se7cRy07NJ7llxQC3sJb/pTtAf1Jxbdi1/vG3lbw44Kla4qFUAdU71rHkYNkUEQ5MnLku7c7W5WEKbIye2AXtCUt83ft477IlQkst0VbWHPUoE+z78QmRFcJxdV28EXzj/RSQEGGAHH94613NQ8FWhsHpip6wEY IReZESAC lFnetKzG9N2Kp1hfShDVlwTqtCPyf2xwSwSyJY62lezyE8L1v4zEwhypgMC1pGbfvJgykW326lsSDpI82UNIgM4RdgrBFkw5WDZYd+yBx0wlZhjTSe5kQv7IY1W/PR7cd1wTQ4oUwXrtIk00pZThyS9Aoy4Y8YroHrf2W17meoOJEdfH2y5ZDf2QAWHTwvkoOQK8HsgldERsO3knS9IPGYtX5TjqlKpSrBD0isgPxlkng37CM4//FiLaCizldH0YTjssq3SnaacfCzH++CaWzt6vLw6kUhnJGHaITdHK+c6CvvsiGmrczHanKeEZvKcmBzvjR5EhkYJMJIyTX3g05vm634A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: seprintf() ========== seprintf() is a function similar to stpcpy(3) in the sense that it returns a pointer that is suitable for chaining to other copy operations. It takes a pointer to the end of the buffer as a sentinel for when to truncate, which unlike a size, doesn't need to be updated after every call. This makes it much more ergonomic, avoiding manually calculating the size after each copy, which is error prone. It also makes error handling much easier, by reporting truncation with a null pointer, which is accepted and transparently passed down by subsequent seprintf() calls. This results in only needing to report errors once after a chain of seprintf() calls, unlike snprintf(3), which requires checking after every call. p = buf; e = buf + countof(buf); p = seprintf(p, e, foo); p = seprintf(p, e, bar); if (p == NULL) goto trunc; vs len = 0; size = countof(buf); len += snprintf(buf + len, size - len, foo); if (len >= size) goto trunc; len += snprintf(buf + len, size - len, bar); if (len >= size) goto trunc; And also better than scnprintf() calls: len = 0; size = countof(buf); len += scnprintf(buf + len, size - len, foo); len += scnprintf(buf + len, size - len, bar); if (len >= size) goto trunc; It seems aparent that it's a more elegant approach to string catenation. stprintf() ========== stprintf() is a helper that is needed for implementing seprintf() --although it could be open-coded within vseprintf(), of course--, but it's also useful by itself. It has the same interface properties as strscpy(): that is, it copies with truncation, and reports truncation with -E2BIG. It would be useful to replace some calls to snprintf(3) and scnprintf() which don't need chaining, and where it's simpler to pass a size. It is better than plain snprintf(3), because it results in simpler error detection (it doesn't need a check >=countof(buf), but rather <0). Cc: Kees Cook Cc: Christopher Bazley Signed-off-by: Alejandro Colomar --- include/linux/sprintf.h | 4 ++ lib/vsprintf.c | 109 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h index 51cab2def9ec..c3dbfd2efd2b 100644 --- a/include/linux/sprintf.h +++ b/include/linux/sprintf.h @@ -11,8 +11,12 @@ __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); __printf(2, 0) int vsprintf(char *buf, const char *, va_list); __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...); __printf(3, 0) int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); +__printf(3, 4) int stprintf(char *buf, size_t size, const char *fmt, ...); +__printf(3, 0) int vstprintf(char *buf, size_t size, const char *fmt, va_list args); __printf(3, 4) int scnprintf(char *buf, size_t size, const char *fmt, ...); __printf(3, 0) int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); +__printf(3, 4) char *seprintf(char *p, const char end[0], const char *fmt, ...); +__printf(3, 0) char *vseprintf(char *p, const char end[0], const char *fmt, va_list args); __printf(2, 3) __malloc char *kasprintf(gfp_t gfp, const char *fmt, ...); __printf(2, 0) __malloc char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); __printf(2, 0) const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 01699852f30c..a3efacadb5e5 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2892,6 +2892,37 @@ int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) } EXPORT_SYMBOL(vsnprintf); +/** + * vstprintf - Format a string and place it in a buffer + * @buf: The buffer to place the result into + * @size: The size of the buffer, including the trailing null space + * @fmt: The format string to use + * @args: Arguments for the format string + * + * The return value is the length of the new string. + * If the string is truncated, the function returns -E2BIG. + * + * If you're not already dealing with a va_list consider using stprintf(). + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +int vstprintf(char *buf, size_t size, const char *fmt, va_list args) +{ + int len; + + len = vsnprintf(buf, size, fmt, args); + + // It seems the kernel's vsnprintf() doesn't fail? + //if (unlikely(len < 0)) + // return -E2BIG; + + if (unlikely(len >= size)) + return -E2BIG; + + return len; +} +EXPORT_SYMBOL(vstprintf); + /** * vscnprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2923,6 +2954,36 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) } EXPORT_SYMBOL(vscnprintf); +/** + * vseprintf - Format a string and place it in a buffer + * @p: The buffer to place the result into + * @end: A pointer to one past the last character in the buffer + * @fmt: The format string to use + * @args: Arguments for the format string + * + * The return value is a pointer to the trailing '\0'. + * If @p is NULL, the function returns NULL. + * If the string is truncated, the function returns NULL. + * + * If you're not already dealing with a va_list consider using seprintf(). + * + * See the vsnprintf() documentation for format string extensions over C99. + */ +char *vseprintf(char *p, const char end[0], const char *fmt, va_list args) +{ + int len; + + if (unlikely(p == NULL)) + return NULL; + + len = vstprintf(p, end - p, fmt, args); + if (unlikely(len < 0)) + return NULL; + + return p + len; +} +EXPORT_SYMBOL(vseprintf); + /** * snprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2950,6 +3011,30 @@ int snprintf(char *buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(snprintf); +/** + * stprintf - Format a string and place it in a buffer + * @buf: The buffer to place the result into + * @size: The size of the buffer, including the trailing null space + * @fmt: The format string to use + * @...: Arguments for the format string + * + * The return value is the length of the new string. + * If the string is truncated, the function returns -E2BIG. + */ + +int stprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list args; + int len; + + va_start(args, fmt); + len = vstprintf(buf, size, fmt, args); + va_end(args); + + return len; +} +EXPORT_SYMBOL(stprintf); + /** * scnprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into @@ -2974,6 +3059,30 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...) } EXPORT_SYMBOL(scnprintf); +/** + * seprintf - Format a string and place it in a buffer + * @p: The buffer to place the result into + * @end: A pointer to one past the last character in the buffer + * @fmt: The format string to use + * @...: Arguments for the format string + * + * The return value is a pointer to the trailing '\0'. + * If @buf is NULL, the function returns NULL. + * If the string is truncated, the function returns NULL. + */ + +char *seprintf(char *p, const char end[0], const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + p = vseprintf(p, end, fmt, args); + va_end(args); + + return p; +} +EXPORT_SYMBOL(seprintf); + /** * vsprintf - Format a string and place it in a buffer * @buf: The buffer to place the result into -- 2.50.0