Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3763123pxk; Tue, 29 Sep 2020 05:48:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSldN0Kw/kyyrrZopuJo3FM7F8KLsT1oG2v/XUSr1nWz5Uw4yuY8xUyoaJyTPkaeaDXHSY X-Received: by 2002:a17:906:a415:: with SMTP id l21mr3449733ejz.431.1601383702874; Tue, 29 Sep 2020 05:48:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601383702; cv=none; d=google.com; s=arc-20160816; b=bck2FcIMihQ+y47LvgrWhg8ds7DB6EKszDJy+ROoopKEENUycBPlTZ++jBCQ2XbokY uqHnw16yBD9WhKrivfz4Cstdv4uBED5knnQ0ohc5GBE5dToU5cu3OiZeurkS9OJy6kLy 4V9sOTD748u9Vi9tZS7kxaL03oPQ/Rzs+hyy2mmClL7wcpYKqzhPf9qlL9KMY2uLhfkJ Za87AKTNXqM+c07EMAisQvHqm59bRnu0CJItPtsdrm+qXRa1cLEpJP5A1/klSR1GKLeu 1U+g8wWQ7BOMonkNDY4Lyjt5fR3WVPw3USEl+wTNWYP2ykEPbqI0EjEJsIGo+tz2Z1Jo ORrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r7uhdo6iUUNQhK1JRBerLUoRSEsAVkngkHMGSWdydGU=; b=UioAl7iIEuR1eULde2ze2k2LD6HeWOmXcVIApKwab55cCuPpWjlE8ns0F3i63YH4Cj 0wrP2NJHNrqfLZIDltWM8axs6gTrW/R9JkEbHQMXsgBr6wigHk+q2M2PndutfXvf3Nj1 xr2DXJadUJEEnIxXW/ST7q6qCeiDoP2hU1WC3+dqgpeBu6LUS+4Wf4Wfh1CIy6hnTQoW 37h8FLln1H6cDjpjZE9XaL+c1UjeRQLx2vC1UcdBiLLpHdebF7vw3PMDZZ5+LKlRU10U AsR/J8M6vLqIivqWbA4I6gam90N21WKxIwV8Ks7VazOkIG9qAHtybU2E9YmG3IsUmC5F +hrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRex9wsQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si2561961ejd.380.2020.09.29.05.47.59; Tue, 29 Sep 2020 05:48:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRex9wsQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733198AbgI2MrC (ORCPT + 99 others); Tue, 29 Sep 2020 08:47:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:43518 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728322AbgI2LGR (ORCPT ); Tue, 29 Sep 2020 07:06:17 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D0A0421941; Tue, 29 Sep 2020 11:06:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601377576; bh=yCotqWN0Fw+n8Uf8OK6nE+mQ+eHrxusx5x9ZCt53jwg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pRex9wsQrnTbpJn9seFjho3CMj5Fc67ol5Hv5mJ79m3h16fT6VZJFDPTz6V3xb55i UwWx/rzPSKlwjxuAxaMBzmqPSm9IqVIg6YAH1HHVVhIiLHOfPyXCgxprgyMW2Ito6/ CBCqL/5/JDGPFKp7h3gh090g1OjZFpwYugnDi4qc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sami Tolvanen , Andy Lavr , Arvind Sankar , Joe Perches , Kees Cook , Masahiro Yamada , Rasmus Villemoes , Nick Desaulniers , Andrew Morton , Nathan Chancellor , Linus Torvalds Subject: [PATCH 4.4 82/85] lib/string.c: implement stpcpy Date: Tue, 29 Sep 2020 13:00:49 +0200 Message-Id: <20200929105932.277931126@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200929105928.198942536@linuxfoundation.org> References: <20200929105928.198942536@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nick Desaulniers commit 1e1b6d63d6340764e00356873e5794225a2a03ea upstream. LLVM implemented a recent "libcall optimization" that lowers calls to `sprintf(dest, "%s", str)` where the return value is used to `stpcpy(dest, str) - dest`. This generally avoids the machinery involved in parsing format strings. `stpcpy` is just like `strcpy` except it returns the pointer to the new tail of `dest`. This optimization was introduced into clang-12. Implement this so that we don't observe linkage failures due to missing symbol definitions for `stpcpy`. Similar to last year's fire drill with: commit 5f074f3e192f ("lib/string.c: implement a basic bcmp") The kernel is somewhere between a "freestanding" environment (no full libc) and "hosted" environment (many symbols from libc exist with the same type, function signature, and semantics). As Peter Anvin notes, there's not really a great way to inform the compiler that you're targeting a freestanding environment but would like to opt-in to some libcall optimizations (see pr/47280 below), rather than opt-out. Arvind notes, -fno-builtin-* behaves slightly differently between GCC and Clang, and Clang is missing many __builtin_* definitions, which I consider a bug in Clang and am working on fixing. Masahiro summarizes the subtle distinction between compilers justly: To prevent transformation from foo() into bar(), there are two ways in Clang to do that; -fno-builtin-foo, and -fno-builtin-bar. There is only one in GCC; -fno-buitin-foo. (Any difference in that behavior in Clang is likely a bug from a missing __builtin_* definition.) Masahiro also notes: We want to disable optimization from foo() to bar(), but we may still benefit from the optimization from foo() into something else. If GCC implements the same transform, we would run into a problem because it is not -fno-builtin-bar, but -fno-builtin-foo that disables that optimization. In this regard, -fno-builtin-foo would be more future-proof than -fno-built-bar, but -fno-builtin-foo is still potentially overkill. We may want to prevent calls from foo() being optimized into calls to bar(), but we still may want other optimization on calls to foo(). It seems that compilers today don't quite provide the fine grain control over which libcall optimizations pseudo-freestanding environments would prefer. Finally, Kees notes that this interface is unsafe, so we should not encourage its use. As such, I've removed the declaration from any header, but it still needs to be exported to avoid linkage errors in modules. Reported-by: Sami Tolvanen Suggested-by: Andy Lavr Suggested-by: Arvind Sankar Suggested-by: Joe Perches Suggested-by: Kees Cook Suggested-by: Masahiro Yamada Suggested-by: Rasmus Villemoes Signed-off-by: Nick Desaulniers Signed-off-by: Andrew Morton Tested-by: Nathan Chancellor Cc: Link: https://lkml.kernel.org/r/20200914161643.938408-1-ndesaulniers@google.com Link: https://bugs.llvm.org/show_bug.cgi?id=47162 Link: https://bugs.llvm.org/show_bug.cgi?id=47280 Link: https://github.com/ClangBuiltLinux/linux/issues/1126 Link: https://man7.org/linux/man-pages/man3/stpcpy.3.html Link: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stpcpy.html Link: https://reviews.llvm.org/D85963 Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- lib/string.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) --- a/lib/string.c +++ b/lib/string.c @@ -235,6 +235,30 @@ ssize_t strscpy(char *dest, const char * EXPORT_SYMBOL(strscpy); #endif +/** + * stpcpy - copy a string from src to dest returning a pointer to the new end + * of dest, including src's %NUL-terminator. May overrun dest. + * @dest: pointer to end of string being copied into. Must be large enough + * to receive copy. + * @src: pointer to the beginning of string being copied from. Must not overlap + * dest. + * + * stpcpy differs from strcpy in a key way: the return value is a pointer + * to the new %NUL-terminating character in @dest. (For strcpy, the return + * value is a pointer to the start of @dest). This interface is considered + * unsafe as it doesn't perform bounds checking of the inputs. As such it's + * not recommended for usage. Instead, its definition is provided in case + * the compiler lowers other libcalls to stpcpy. + */ +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src); +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src) +{ + while ((*dest++ = *src++) != '\0') + /* nothing */; + return --dest; +} +EXPORT_SYMBOL(stpcpy); + #ifndef __HAVE_ARCH_STRCAT /** * strcat - Append one %NUL-terminated string to another