From patchwork Tue Aug 25 14:00:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Desaulniers X-Patchwork-Id: 248253 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:522:0:0:0:0 with SMTP id h2csp3466915ils; Tue, 25 Aug 2020 07:00:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxq43cZpEqD/e3iwpj/76l/Rce/W/o1SSqIpkXJQrWevLmBZN51+8wzRBkoTQmHrKFl+t7j X-Received: by 2002:aa7:ce96:: with SMTP id y22mr7677117edv.180.1598364054901; Tue, 25 Aug 2020 07:00:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598364054; cv=none; d=google.com; s=arc-20160816; b=C6wO5/FvAcjwf73/tWuPkhVz0aDZflZyEDlmdE4/atOmFCeYEwz2ejA1YGQkt2VibM haajUGyGDoDrwKcntvEi3SiaH4UlU/ed3dlzPjVV++/0FVUReO3OXe40vENI78vSOAkT hbq1WwuMrRnupdJr1cDCqeDxfOepXWcgOl1OERh4TFQ29+2kM2P+kndw47M2qwt4n1cS zGKptQ4nfkbLqE2J8kERTa/MG0DOrL5bBbc44HKglb1CS/HK5DlUHRj6r7OsWTOo2ZmO 6AG5sFBlGOQRPrEiPgcxLCBmzvR0ocBSY1e6VBZH3P1uLQ6WFXz5wEur0UTVD3pOycJI cthQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=Us+zKhWOc0nF2Stw/sjqwIYhi1P3cVkrUO+fKBNUwt4=; b=bnQJWx8/URhewKLvGFB6R83waeFeQPZUg5rJX8BO8aZAA9BF7vTtbT2POTkq9+BYXz yybxWA8Db1EO4xvfHsJtLuunzJX5Q4Vd+ZbK+gTbOic2WEON1bwUqp2wNGWaxQ3Cm+Nn Pk00unqaOUKv6MhFPBJ3HHUpqFXdf6xR8J61m5gOn1WiuZKMA5uf2paKmfrF8PT855+q GIRzRRkScuBvZnZvJj0w9TUW9F0QYk562AfChRfiVOJYC5zD/Rp4vGegTeDYjZbxqRb6 r0lHG30NZ+xApAia6srEBawZrbZlieb2vrJ6uky2gJwdMVC8EOUjD8pGfg/Zj2NYn/JQ jsqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=aKhnLnCz; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=REJECT sp=REJECT dis=QUARANTINE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y13si2764158ejb.12.2020.08.25.07.00.54; Tue, 25 Aug 2020 07:00:54 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=aKhnLnCz; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=REJECT sp=REJECT dis=QUARANTINE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgHYOAk (ORCPT + 15 others); Tue, 25 Aug 2020 10:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726631AbgHYOAR (ORCPT ); Tue, 25 Aug 2020 10:00:17 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 784DCC061574 for ; Tue, 25 Aug 2020 07:00:17 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id lx6so1887633pjb.9 for ; Tue, 25 Aug 2020 07:00:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=Us+zKhWOc0nF2Stw/sjqwIYhi1P3cVkrUO+fKBNUwt4=; b=aKhnLnCz8EWY/O9EwQPxzUF7jffQUPREucAEu/8yKU3QJT4H7vWbyhQ79WNhCjEevC EWdjs6sr3iSqinEvLhq3LQjeiCyov9YbtBKKZdSDwNMNa01kfboS+9K1y9YJBIDd7/3b DcfN94LhoDlCUDPYrhKYXqRX/KEyuLJnzYMRpqotPiVTboFLvnuKf4Vy2Bwr5fkLVSX4 QRftCpolQAJIyqqfLeDkCj2PVgFBxu2bTT/WQhuoS+HZ5+4PUMI9Vl1XUUCmD4QCipG9 OgJ/KdWNJBMVquN4Gm5pJpD16KJvtnAB2fsC1H/gpgTZ3Dnz/I7ucCh9Uxr7yUC3LBtl 517A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=Us+zKhWOc0nF2Stw/sjqwIYhi1P3cVkrUO+fKBNUwt4=; b=eiScTsEX/aiiV2KSLT0vtI951df5G9i1Zzn/JRkTpWG0+LRktdvDHwG33lL2BgmR4a qWjOCTLxe4keXW95fof0XdGhQgxiBorIWyIcOmsrO8IDVSifvmvI21PPZwViyq9vN22/ r7CZQsioI8r6B9HjZZOWVVByK+u8XLTOkSO4Dx3hCjOQgva6zrihPJ8DQ9iBmgvUzjJX 274HkUHfsOeVVo00NmOC5T7a91JwgmD6rW8LAXnPFLREaS9NoF7LjhNAmLL9AtCwLbYN j1Ce/KtpfvbtNO4eODolo0rEAYsggEOSGqqeItrywsHTSpRhcClh5sKwjBc71GBdap8H wKKw== X-Gm-Message-State: AOAM531tjCzi2sOnCCj5rfHDA6A1jZuEOCI14aCLADYQyd+rJhOwudCX SosSvQsfFcfFxLqkknk3NPNGUTdDJ3yxR2x1Zsc= X-Received: from ndesaulniers1.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:4d25]) (user=ndesaulniers job=sendgmr) by 2002:a17:902:8f82:: with SMTP id z2mr7778537plo.177.1598364016898; Tue, 25 Aug 2020 07:00:16 -0700 (PDT) Date: Tue, 25 Aug 2020 07:00:00 -0700 Message-Id: <20200825140001.2941001-1-ndesaulniers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.28.0.297.g1956fa8f8d-goog Subject: [PATCH v3] lib/string.c: implement stpcpy From: Nick Desaulniers To: Masahiro Yamada Cc: clang-built-linux@googlegroups.com, Nick Desaulniers , stable@vger.kernel.org, Andy Lavr , Arvind Sankar , Joe Perches , Rasmus Villemoes , Sami Tolvanen , Andrew Morton , Andy Shevchenko , Kees Cook , Yury Norov , Alexandru Ardelean , linux-kernel@vger.kernel.org Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org 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 H. 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. Cc: stable@vger.kernel.org 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 Suggested-by: Andy Lavr Suggested-by: Arvind Sankar Suggested-by: Joe Perches Suggested-by: Masahiro Yamada Suggested-by: Rasmus Villemoes Reported-by: Sami Tolvanen Signed-off-by: Nick Desaulniers --- Changes V3: * Drop Sami's Tested by tag; newer patch. * Add EXPORT_SYMBOL as per Andy. * Rewrite commit message, rewrote part of what Masahiro said to be generic in terms of foo() and bar(). * Prefer %NUL-terminated to NULL terminated. NUL is the ASCII character '\0', as per Arvind and Rasmus. Changes V2: * Added Sami's Tested by; though the patch changed implementation, the missing symbol at link time was the problem Sami was observing. * Fix __restrict -> __restrict__ typo as per Joe. * Drop note about restrict from commit message as per Arvind. * Fix NULL -> NUL as per Arvind; NUL is ASCII '\0'. TIL * Fix off by one error as per Arvind; I had another off by one error in my test program that was masking this. lib/string.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) -- 2.28.0.297.g1956fa8f8d-goog Acked-by: Kees Cook diff --git a/lib/string.c b/lib/string.c index 6012c385fb31..6bd0cf0fb009 100644 --- a/lib/string.c +++ b/lib/string.c @@ -272,6 +272,30 @@ ssize_t strscpy_pad(char *dest, const char *src, size_t count) } EXPORT_SYMBOL(strscpy_pad); +/** + * 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 the new + * %NUL-terminated character. (for strcpy, the return value is a pointer to + * src. 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