Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp148667rdb; Wed, 18 Oct 2023 23:11:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXukwKYF+3NwJBdk5z2UM0V7+hJVY+WsfQceTlExV4paZMT2GD0vuE4Vu4WZPJbvpfGbaL X-Received: by 2002:a05:6359:318b:b0:14c:6ba4:e1a6 with SMTP id ri11-20020a056359318b00b0014c6ba4e1a6mr913977rwb.2.1697695893696; Wed, 18 Oct 2023 23:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697695893; cv=none; d=google.com; s=arc-20160816; b=aJO6k6/JhEzW7tZgtGBom4Vy8SHHCL3DWJCV44S+5AvFpUKv3MDyEzG3F0oMHRYC5I VLtIMeazZWrUADj6k7DACplTwM4gsQbJ9R0hUQxg67SeswBaAU0Gam3LvrjAL6NamtF/ a72IVkN1T7Or6LdSPhENXvL7L8gyoTvUPuLmnNj6Y6scnba0vlps0poArrLAErYopnbq stRiJo+W61QX7CewMQiQFam9cKbOHcpziH90UiKtDP7pUoi/DiDXLu+pSOOXDiZHpt+t nwSpI1hdYVa/QDtm8srop2JixkybkAEn1R1t97PcBvqKNDzyq2rdDh84obf90/dzOQbB SoYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=U7yDeCbKK7RJMvZiIVbGogXmcuCznb6lg3vpgldc7l0=; fh=PTU+KAwjjs8OGo5r07lWHPtCKbsHUCDiLJmjAcyNddQ=; b=zyOBFrI40eKLXTtv9w/RvJEltMTcbeFofdFuroZ5edj2BWqyfH+UZ0dxcOfM3ZY1XL KvutFitkHoAvMDXqKdt0gXmU50BM8nI+pECDpEsnCcZj5J1PpzkoyFSCXxk+PhWuv3E0 xDv0/sV9MdconDIsJ6FbXSDYJIbrdoE9dYPyBVWMVonJwnXSRW6owEhkWXZ/DNc8OtoB FjjG2bpNAOTXfIyCmATGIk8wBnTJ5r73GRm5fyuw1IlqGI9pEtO4n3IoO6gedy9XY2oJ Mguw0P4zkO5+BiQ+9+AH+7zQ8Uls89TUa1t8Ja9yp8OWau8FyCbV9UgXYm/HWZXUAORe 35MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kDxXRTAe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id j27-20020a63fc1b000000b00589867d220asi3813173pgi.254.2023.10.18.23.11.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 23:11:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kDxXRTAe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id BA07881143C3; Wed, 18 Oct 2023 23:11:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232714AbjJSGLF (ORCPT + 99 others); Thu, 19 Oct 2023 02:11:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232733AbjJSGK4 (ORCPT ); Thu, 19 Oct 2023 02:10:56 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E5E8D71 for ; Wed, 18 Oct 2023 23:01:56 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6b1ef786b7fso5651717b3a.3 for ; Wed, 18 Oct 2023 23:01:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1697695316; x=1698300116; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=U7yDeCbKK7RJMvZiIVbGogXmcuCznb6lg3vpgldc7l0=; b=kDxXRTAe+Pae7ibWBYLaO5zaM26mXUHLIeTfARzKL3y8pZdbuyII7QyG2ApgkuiDU8 Oc4dL/4l5JYs+LC94AY0bhrjRlHsx0gKWW87fPc6E8kSnW6Bcx4NvuNa9NeQ6OyCIlhV UjyWBuKJJemeseBAO4L7fQyIqfJ5R9ZWj28aI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697695316; x=1698300116; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=U7yDeCbKK7RJMvZiIVbGogXmcuCznb6lg3vpgldc7l0=; b=C9WKcWbKdrIfurTFz+Re+kftRnc5bcraR0CwV7u6BbaKKO0BIARdJoNtnX/jiRbjdc YsQRRNaI30b/IYlSu8TEWU7Q5UpVtNr3qMrSGDPwg4gWU6eZY9J2Jt+FtFyuaMdy0K3p lErVem5omNZOlwbXMLw4qQcpxQZVEKA+N5KS1v0FGpwrbeRukapwgy6tR1mnW3rG73tg T9tADElL2CtIneTKAJITBA9yBAddSprKWiA2Sm0n0rR1ry/jd82b0IZFCF9fo25Mce2M Sjz8C5HgyQZ0L7aCg9Mw5ogesAJ0GdC2DplZPBxddWQwdnv0W3ySNOkfxlTbHpnrrPE3 +LiQ== X-Gm-Message-State: AOJu0Yxa+lgIdxjNFCIrTOdjfhSOKiihKwg1kVW3RodWzDH87NFer8Y1 wfJ4n8BIrDvIdsRww1mQBZurrg== X-Received: by 2002:a05:6a00:cd6:b0:6b8:a6d6:f51a with SMTP id b22-20020a056a000cd600b006b8a6d6f51amr1126768pfv.31.1697695315885; Wed, 18 Oct 2023 23:01:55 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id h3-20020a056a00000300b006933866fd21sm4291678pfk.117.2023.10.18.23.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 23:01:55 -0700 (PDT) Date: Wed, 18 Oct 2023 23:01:54 -0700 From: Kees Cook To: Christoph Hellwig Cc: Justin Stitt , Keith Busch , Jens Axboe , Sagi Grimberg , linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, ksummit@lists.linux.dev Subject: Re: the nul-terminated string helper desk chair rearrangement Message-ID: <202310182248.9E197FFD5@keescook> References: <20231018-strncpy-drivers-nvme-host-fabrics-c-v1-1-b6677df40a35@google.com> <20231019054642.GF14346@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231019054642.GF14346@lst.de> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 18 Oct 2023 23:11:30 -0700 (PDT) On Thu, Oct 19, 2023 at 07:46:42AM +0200, Christoph Hellwig wrote: > On Wed, Oct 18, 2023 at 10:48:49PM +0000, Justin Stitt wrote: > > strncpy() is deprecated for use on NUL-terminated destination strings > > [1] and as such we should prefer more robust and less ambiguous string > > interfaces. > > If we want that we need to stop pretendening direct manipulation of > nul-terminate strings is a good idea. I suspect the churn of replacing > one helper with another, maybe slightly better, one probably > introduces more bugs than it fixes. > > If we want to attack the issue for real we need to use something > better. > > lib/seq_buf.c is a good start for a lot of simple cases that just > append to strings including creating complex ones. Kent had a bunch > of good ideas on how to improve it, but couldn't be convinced to > contribute to it instead of duplicating the functionality which > is a bit sad, but I think we need to switch to something like > seq_buf that actually has a counted string instead of all this messing > around with the null-terminated strings. When doing more complex string creation, I agree. I spent some time doing this while I was looking at removing strcat() and strlcat(); this is where seq_buf shines. (And seq_buf is actually both: it maintains its %NUL termination _and_ does the length counting.) The only thing clunky about it was initialization, but all the conversions I experimented with were way cleaner using seq_buf. I even added a comment to strlcat()'s kern-doc to aim folks at seq_buf. :) /** * strlcat - Append a string to an existing string ... * Do not use this function. While FORTIFY_SOURCE tries to avoid * read and write overflows, this is only possible when the sizes * of @p and @q are known to the compiler. Prefer building the * string with formatting, via scnprintf(), seq_buf, or similar. Almost all of the remaining strncpy() usage is just string to string copying, but the corner cases that are being spun out that aren't strscpy() or strscpy_pad() are covered by strtomem(), kmemdup_nul(), and memcpy(). Each of these are a clear improvement since they remove the ambiguity of the intended behavior. Using seq_buf ends up being way more overhead than is needed. -Kees -- Kees Cook