The fortified implementation of strcat() in include/linux/fortify-string.h currently relies on strlcat() to perform bounded concatenation and detect buffer overflows. In preparation for removing the deprecated strlcat() API[1], replace the strlcat() call in strcat(). When the destination buffer size is unknown (SIZE_MAX), call __underlying_strcat() directly. Otherwise, determine the destination length with __fortify_strlen() and append the source string using __builtin_snprintf(), preserving the existing fortify_panic() check on overflow. Link: https://github.com/KSPP/linux/issues/370 [1] Cc: codemender-patching+linux@google.com Assisted-by: SecurityEngineer:Gemini1.5Pro [editor_update_file, shell_command_run, test_runner_run_kunit_test] Signed-off-by: Bill Wendling --- Cc: Russell King Cc: Huacai Chen Cc: WANG Xuerui Cc: Thomas Bogendoerfer Cc: "James E.J. Bottomley" Cc: Helge Deller Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Ian Abbott Cc: H Hartley Sweeten Cc: Tony Luck Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Simona Vetter Cc: Matthew Brost Cc: "Thomas Hellström" Cc: Rodrigo Vivi Cc: Dmitry Torokhov Cc: Matthias Schwarzott Cc: Mauro Carvalho Chehab Cc: Tony Nguyen Cc: Przemek Kitszel Cc: Andrew Lunn Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni Cc: Arend van Spriel Cc: Rob Herring Cc: Saravana Kannan Cc: Krzysztof Kozlowski Cc: Sylwester Nawrocki Cc: Peter Griffin Cc: Alim Akhtar Cc: Linus Walleij Cc: Anil Gurumurthy Cc: Sudarsana Kalluru Cc: "Martin K. Petersen" Cc: Trond Myklebust Cc: Anna Schumaker Cc: Mike Marshall Cc: Martin Brandenburg Cc: Kees Cook Cc: Jiri Pirko Cc: Simon Horman Cc: Chuck Lever Cc: Jeff Layton Cc: NeilBrown Cc: Olga Kornievskaia Cc: Dai Ngo Cc: Tom Talpey Cc: Jaroslav Kysela Cc: Takashi Iwai Cc: Bill Wendling Cc: Andrew Morton Cc: "Mike Rapoport (Microsoft)" Cc: Kanglong Wang Cc: Tiezhu Yang Cc: Qiang Ma Cc: Randy Dunlap Cc: Pengpeng Hou Cc: Ard Biesheuvel Cc: Breno Leitao Cc: Thorsten Blum Cc: Harshit Mogalapalli Cc: Greg Kroah-Hartman Cc: Lyude Paul Cc: Ashutosh Desai Cc: Imre Deak Cc: Dmitry Baryshkov Cc: Johan Hovold Cc: Johannes Berg Cc: Miri Korenblit Cc: Alexander Stein Cc: Cryolitia PukNgae Cc: Jiaming Zhang Cc: Will Porter Cc: Cen Zhang Cc: "Cássio Gabriel" Cc: Rong Zhang Cc: Arun Raghavan Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Cc: loongarch@lists.linux.dev Cc: linux-mips@vger.kernel.org Cc: linux-parisc@vger.kernel.org Cc: linux-edac@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: intel-xe@lists.freedesktop.org Cc: linux-input@vger.kernel.org Cc: linux-media@vger.kernel.org Cc: intel-wired-lan@lists.osuosl.org Cc: netdev@vger.kernel.org Cc: linux-wireless@vger.kernel.org Cc: brcm80211@lists.linux.dev Cc: brcm80211-dev-list.pdl@broadcom.com Cc: devicetree@vger.kernel.org Cc: linux-samsung-soc@vger.kernel.org Cc: linux-gpio@vger.kernel.org Cc: linux-scsi@vger.kernel.org Cc: linux-nfs@vger.kernel.org Cc: devel@lists.orangefs.org Cc: linux-hardening@vger.kernel.org Cc: linux-sound@vger.kernel.org --- include/linux/fortify-string.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h index cf841dc71fef..0b489124bfcb 100644 --- a/include/linux/fortify-string.h +++ b/include/linux/fortify-string.h @@ -363,7 +363,12 @@ __FORTIFY_INLINE __diagnose_as(__builtin_strcat, 1, 2) char *strcat(char * const POS p, const char *q) { const size_t p_size = __member_size(p); - const size_t wanted = strlcat(p, q, p_size); + + if (p_size == SIZE_MAX) + return __underlying_strcat(p, q); + + const size_t p_len = __fortify_strlen(p); + const size_t wanted = p_len + __builtin_snprintf(p + p_len, p_size - p_len, "%s", q); if (p_size <= wanted) fortify_panic(FORTIFY_FUNC_strcat, FORTIFY_WRITE, p_size, wanted + 1, p); -- 2.55.0.1032.g73a4cd73de-goog