Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1500734pxa; Sun, 16 Aug 2020 00:17:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy56J9AgcvEgYHWqRsc1e4/GsQmeTY+GdEBa9E8BqoveCHsYAZ7ZjdyxCOS+umqiNyy0SCf X-Received: by 2002:a05:6402:7d5:: with SMTP id u21mr10282248edy.341.1597562255438; Sun, 16 Aug 2020 00:17:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597562255; cv=none; d=google.com; s=arc-20160816; b=OIkCbi8ztFqwEYorYsZJat//Dv1lGAdA5QRZIJmBWeT+6ck8oEEmwWnfwGADM2eRsh +4hI3t5ElNhw1GzfPNAw0MFypE0hd/9Lqmj6+cvLwOvuQ2iI5q1svyfcfvIZJIqM0DXV 5pC4uv/sVSSS3GPaWEfrBgBbU6bzUkvzFCOG+OaxpsvNwJj+G7maNqEKY4wxZk/1PB2X kzv7KYPJrAcIBp++X77b0S24sadOeVyjM5g07JqXd7B946tpMJj5MLo2xNsb1TXDaP6d cFm0dfISiY1X9OrqmPVcNkIKZDpmsnuXP71V4NRoiMGwpENVjy4Fq+JNLlorqeZz3vZq c3Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=GdNLwnlPFkQHFOd6Xy60wVgrBimGSKOXtSoP9QOa/Ns=; b=BxusLAbN1sxRvT05lWIv02TIYOtEXezU5Ykdb7Ix2QH9R8Q2ee/m23ZqaN87F6zPNM DwWrvn2ob1RivA80qUSv/V4GugZuX5NbFT362LtebejgyB+1/OnHSjdgzOj4ikiCKy3q XT3XcIbbdbjpmI6mLFHpCYyg+8wCeW40wlrNE8idMcubyFnukfExdAaIWngmJUQ08XVm II6XqQe8V04hlCb3bTz0MG2c4qC3chx0+9oXw1rTnY+7F3FQiCfC2CFYlWcdmFpi/68j XhOyCjJ3f2EKVZfCKMtqKljwmLdfppaH+FoVGMdgYkO2qsjvka+NUyMco9cLf/BrQTQ/ gruA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=XQiMWXwG; 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=pass (p=REJECT sp=REJECT dis=NONE) 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 g16si9304790ejf.103.2020.08.16.00.17.12; Sun, 16 Aug 2020 00:17:35 -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=@google.com header.s=20161025 header.b=XQiMWXwG; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727913AbgHPATY (ORCPT + 99 others); Sat, 15 Aug 2020 20:19:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726641AbgHPATY (ORCPT ); Sat, 15 Aug 2020 20:19:24 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29446C061786 for ; Sat, 15 Aug 2020 17:19:23 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id mt12so6028274pjb.4 for ; Sat, 15 Aug 2020 17:19:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=GdNLwnlPFkQHFOd6Xy60wVgrBimGSKOXtSoP9QOa/Ns=; b=XQiMWXwGHmbC34v17SmqPih9NvHLjX68PR7xIbiyZM6UG++TmXKlFLA1xjihqHJ16y 7yZvm2dN+S2b3RGXSiw4CT8n9xETzpC95wooaPCWTNIXp1qzBL0wAascCahjmRrm+HaR jprx3YKxQk+PX/vN2sIOw0J3ShvVuF0zOEyjXQPdnwdwGFP9r/vJd84k2wKAO/4g3EQy eyF2WkWoMmABhwtQQJj8xJAf9cLT1AvG+QWnyplTnmhg5L2xN0tAYSGMlzh2nbbGA3nQ /CsRFEXsTxhX2NeWWLfsHdn6r/SOBH6TLQLeakTDO+Gu0vSqAK6wTpeosnsbPRz4FkrS C17A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=GdNLwnlPFkQHFOd6Xy60wVgrBimGSKOXtSoP9QOa/Ns=; b=CFg224hJu5hsl03BEqhyxUARVn+A8AFM7eVrm7wfFwx+1w75TLf36kP4WDCxRawIS4 ADgUnNEfn6nk/wsyasyalSsPa6LnLipvUL7KwHoGbDvmN23kjASQ0wNcQGgJ8xqg5DTl uDSVjTqmRhkn6U/LRvfAe8ptp52bSgdIMDsscLNphYw4/0NbstyB80Yb+ePkyLxB/aIa ABChor9hDrei3Jomb9yeeiNMRHtuQreMwWXLPkwPVlYG+z+KCiPZ46Gsz2/apN8juAES +Mc0ug+O/XI2SHqeGJiMJDMI9vGXagyHOFGmbsB4yxBA+zmt4y2XldzHFXJGDIf+TmEX gibQ== X-Gm-Message-State: AOAM531si9FAS9HvcY0HqQlozGS2YA/aUOtxQqn9aGrau8kSwBdIbcpt TjB7MTmvpcdUn4sYK4lvcxjMjw== X-Received: by 2002:a17:902:7609:: with SMTP id k9mr6530697pll.187.1597537162547; Sat, 15 Aug 2020 17:19:22 -0700 (PDT) Received: from google.com ([2620:15c:2ce:0:a6ae:11ff:fe11:4abb]) by smtp.gmail.com with ESMTPSA id b63sm13070308pfg.43.2020.08.15.17.19.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Aug 2020 17:19:21 -0700 (PDT) Date: Sat, 15 Aug 2020 17:19:17 -0700 From: Fangrui Song To: Nick Desaulniers Cc: Joe Perches , Kees Cook , Andrew Morton , =?utf-8?B?RMOhdmlkIEJvbHZhbnNrw70=?= , Eli Friedman , "# 3.4.x" , Arvind Sankar , Sami Tolvanen , Vishal Verma , Dan Williams , Andy Shevchenko , "Joel Fernandes (Google)" , Daniel Axtens , Ingo Molnar , Yury Norov , Alexandru Ardelean , LKML , clang-built-linux , Rasmus Villemoes Subject: Re: [PATCH v2] lib/string.c: implement stpcpy Message-ID: <20200816001917.4krsnrik7hxxfqfm@google.com> References: <20200815014006.GB99152@rani.riverdale.lan> <20200815020946.1538085-1-ndesaulniers@google.com> <202008150921.B70721A359@keescook> <457a91183581509abfa00575d0392be543acbe07.camel@perches.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-08-15, 'Nick Desaulniers' via Clang Built Linux wrote: >On Sat, Aug 15, 2020 at 2:31 PM Joe Perches wrote: >> >> On Sat, 2020-08-15 at 14:28 -0700, Nick Desaulniers wrote: >> > On Sat, Aug 15, 2020 at 2:24 PM Joe Perches wrote: >> > > On Sat, 2020-08-15 at 13:47 -0700, Nick Desaulniers wrote: >> > > > On Sat, Aug 15, 2020 at 9:34 AM Kees Cook wrote: >> > > > > On Fri, Aug 14, 2020 at 07:09:44PM -0700, Nick Desaulniers wrote: >> > > > > > 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. Calling `sprintf` with overlapping arguments >> > > > > > was clarified in ISO C99 and POSIX.1-2001 to be undefined behavior. >> > > > > > >> > > > > > `stpcpy` is just like `strcpy` except it returns the pointer to the new >> > > > > > tail of `dest`. This allows you to chain multiple calls to `stpcpy` in >> > > > > > one statement. >> > > > > >> > > > > O_O What? >> > > > > >> > > > > No; this is a _terrible_ API: there is no bounds checking, there are no >> > > > > buffer sizes. Anything using the example sprintf() pattern is _already_ >> > > > > wrong and must be removed from the kernel. (Yes, I realize that the >> > > > > kernel is *filled* with this bad assumption that "I'll never write more >> > > > > than PAGE_SIZE bytes to this buffer", but that's both theoretically >> > > > > wrong ("640k is enough for anybody") and has been known to be wrong in >> > > > > practice too (e.g. when suddenly your writing routine is reachable by >> > > > > splice(2) and you may not have a PAGE_SIZE buffer). >> > > > > >> > > > > But we cannot _add_ another dangerous string API. We're already in a >> > > > > terrible mess trying to remove strcpy[1], strlcpy[2], and strncpy[3]. This >> > > > > needs to be addressed up by removing the unbounded sprintf() uses. (And >> > > > > to do so without introducing bugs related to using snprintf() when >> > > > > scnprintf() is expected[4].) >> > > > >> > > > Well, everything (-next, mainline, stable) is broken right now (with >> > > > ToT Clang) without providing this symbol. I'm not going to go clean >> > > > the entire kernel's use of sprintf to get our CI back to being green. >> > > >> > > Maybe this should get place in compiler-clang.h so it isn't >> > > generic and public. >> > >> > https://bugs.llvm.org/show_bug.cgi?id=47162#c7 and >> > https://bugs.llvm.org/show_bug.cgi?id=47144 >> > Seem to imply that Clang is not the only compiler that can lower a >> > sequence of libcalls to stpcpy. Do we want to wait until we have a >> > fire drill w/ GCC to move such an implementation from >> > include/linux/compiler-clang.h back in to lib/string.c? >> >> My guess is yes, wait until gcc, if ever, needs it. > >The suggestion to use static inline doesn't even make sense. The >compiler is lowering calls to other library routines; `stpcpy` isn't >being explicitly called. Even if it was, not sure we want it being >inlined. No symbol definition will be emitted; problem not solved. >And I refuse to add any more code using `extern inline`. Putting the >definition in lib/string.c is the most straightforward and avoids >revisiting this issue in the future for other toolchains. I'll limit >access by removing the declaration, and adding a comment to avoid its >use. But if you're going to use a gnu target triple without using >-ffreestanding because you *want* libcall optimizations, then you have >to provide symbols for all possible library routines! Adding a definition without a declaration for stpcpy looks good. Clang LTO will work. (If the kernel does not want to provide these routines, is http://git.kernel.org/linus/6edfba1b33c701108717f4e036320fc39abe1912 probably wrong? (why remove -ffreestanding from the main Makefile) )