Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1435445rdh; Mon, 25 Sep 2023 12:43:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGJUzFWinLJESHDVnLTVc9I7uOhnXGXWC6VU9/XKUHOnRqa461SIj3U+oASxCioPLaWtPn4 X-Received: by 2002:a05:6300:808b:b0:160:c1b9:a759 with SMTP id ap11-20020a056300808b00b00160c1b9a759mr852948pzc.20.1695671013050; Mon, 25 Sep 2023 12:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695671013; cv=none; d=google.com; s=arc-20160816; b=0TW2amzb3OTILV2+QO2xVgTBCoHa4sGLSoWzBq7y7j5Vb8ttHlJkAxBvSCTUoJgz54 LITPPvV+UjRygDdJCxYdGUhjCwwrs5kED7jW70Eh03RryIoJ0sTdTo1Yv9oeZRmET5CA Ue2nilfm7IP5dR0VALHu0DXtjB9sOieRZIq6ifTN/6N5wXqbl5Dpz0rXdN9wfCsuSvct g499/6MpZW55/IOVkTeQvJ0wyOmRLAlpwKhpgjejkufqfmoAlLmjfjzVqmOCYs1QzJe1 S6X21aUqqvtPYRUH81usC107LL93drttXQbxxW6olG/AtNjC71kIraKIfHvA9WcaEJg5 XSAg== 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=SpNbWbvzS2q57dWxeknHKSNngDyC+YXA3SyUyIcqgV0=; fh=nQJVjjt7p3GcFMCH6QEgnOeYsiyloQHquesHnOjCoAw=; b=mVWPCYXPkd3avUODMs2N1n92G9ZOIUUyp0ii0iGleCYWg0B9MGooanZ6kIdmzTy//K LqpiIg1YtohJ5KgKFernKkFmklC/xa5WgQJVMUUuUbzPSSXaEG1YbI+MgHIa7LU04M2y 6xFxKWKScZacoli5Mr4LBJGg6R36kABSfWt5IHjJmzZsdmnopEUQVZIaaTg+5qjVQ6m2 z31/z+41UHX3Xm8IaJrYAArCkMCpTnYnnzLVZgYwVadlb9+8tJ/CvSKw046In1HCA6qW mqdnLraUCwbmMnaGN3k0SsQrJSURQEV7WRwnDOC35gVkJhDbmqGILkT6Vh8Y4JUDOq6L UPVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oEdicces; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id kf16-20020a17090305d000b001b02e8d8976si9972386plb.300.2023.09.25.12.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 12:43:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=oEdicces; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 949E78029576; Mon, 25 Sep 2023 11:22:37 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232593AbjIYSWi (ORCPT + 99 others); Mon, 25 Sep 2023 14:22:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232466AbjIYSWh (ORCPT ); Mon, 25 Sep 2023 14:22:37 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7AFD95 for ; Mon, 25 Sep 2023 11:22:30 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-690bf8fdd1aso5397595b3a.2 for ; Mon, 25 Sep 2023 11:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695666150; x=1696270950; 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=SpNbWbvzS2q57dWxeknHKSNngDyC+YXA3SyUyIcqgV0=; b=oEdicces7SIKnRCNSI+ZmPuThpa0jogvbZ/fpoOl9Pkw2L+opVdOwVeI85NQGP/dHv vAdlU91eRrWLRkphF5JVOvrs7K4MV+L/YH0RVXyxGP8SkM/UjFg3CGSykotzbIU9xnis E9Y1p6I2tJWluky7QdVy3jEdw7oQR4IcrONQk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695666150; x=1696270950; 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=SpNbWbvzS2q57dWxeknHKSNngDyC+YXA3SyUyIcqgV0=; b=hqQ5o2I2MtP3dkteB0Wd1teLgjX0/i6PI34KW940d6+NOZG+dnnrl1SaQS1oyg4fO4 PDK+gb1OKhG5kSw1jUceEknbjz6Do6EygTo2F0dDhxZDSGuWiIus5B9t5on0i/Q7VaLW EXUV2G0RfzY0CVGb/AE617EyUnJ2tPA7EyRSACfb9gvAENLBWT7xIthZIk+ZKj6GxyY/ IHXbsBdE1IJfBQIocFNdY9ZbL5f9InzOuMkVn88QwQ2aWQSnOtjrqvRGoFIZDqNl8waU QUbpxqQsmX5hKNSj8R3g54CEKlFmVLC81aWTur3w8bF6w+QLEfp/fgUOqBjTpIWUIJdr rudQ== X-Gm-Message-State: AOJu0Yw/FzsSeaCTcB00BqnwBZUvMSFMNd0M9X+X9yjmdyblpI7zjklW nbwHp3Wxv/ZLB7h/0MDuqa3N8A== X-Received: by 2002:a05:6a00:134b:b0:692:780a:de89 with SMTP id k11-20020a056a00134b00b00692780ade89mr6391028pfu.33.1695666150071; Mon, 25 Sep 2023 11:22:30 -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 fk22-20020a056a003a9600b0068a30f6cf32sm8604713pfb.143.2023.09.25.11.22.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 11:22:29 -0700 (PDT) Date: Mon, 25 Sep 2023 11:22:29 -0700 From: Kees Cook To: Justin Stitt Cc: Song Liu , linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] md: replace deprecated strncpy with memcpy Message-ID: <202309251122.6E3E678140@keescook> References: <20230925-strncpy-drivers-md-md-c-v1-1-2b0093b89c2b@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230925-strncpy-drivers-md-md-c-v1-1-2b0093b89c2b@google.com> 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 groat.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 (groat.vger.email [0.0.0.0]); Mon, 25 Sep 2023 11:22:37 -0700 (PDT) On Mon, Sep 25, 2023 at 09:49:17AM +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. > > There are three such strncpy uses that this patch addresses: > > The respective destination buffers are: > 1) mddev->clevel > 2) clevel > 3) mddev->metadata_type > > We expect mddev->clevel to be NUL-terminated due to its use with format > strings: > | ret = sprintf(page, "%s\n", mddev->clevel); > > Furthermore, we can see that mddev->clevel is not expected to be > NUL-padded as `md_clean()` merely set's its first byte to NULL -- not > the entire buffer: > | static void md_clean(struct mddev *mddev) > | { > | mddev->array_sectors = 0; > | mddev->external_size = 0; > | ... > | mddev->level = LEVEL_NONE; > | mddev->clevel[0] = 0; > | ... > > A suitable replacement for this instance is `memcpy` as we know the > number of bytes to copy and perform manual NUL-termination at a > specified offset. This really decays to just a byte copy from one buffer > to another. `strscpy` is also a considerable replacement but using > `slen` as the length argument would result in truncation of the last > byte unless something like `slen + 1` was provided which isn't the most > idiomatic strscpy usage. > > For the next case, the destination buffer `clevel` is expected to be > NUL-terminated based on its usage within kstrtol() which expects > NUL-terminated strings. Note that, in context, this code removes a > trailing newline which is seemingly not required as kstrtol() can handle > trailing newlines implicitly. However, there exists further usage of > clevel (or buf) that would also like to have the newline removed. All in > all, with similar reasoning to the first case, let's just use memcpy as > this is just a byte copy and NUL-termination is handled manually. > > The third and final case concerning `mddev->metadata_type` is more or > less the same as the other two. We expect that it be NUL-terminated > based on its usage with seq_printf: > | seq_printf(seq, " super external:%s", > | mddev->metadata_type); > ... and we can surmise that NUL-padding isn't required either due to how > it is handled in md_clean(): > | static void md_clean(struct mddev *mddev) > | { > | ... > | mddev->metadata_type[0] = 0; > | ... > > So really, all these instances have precisely calculated lengths and > purposeful NUL-termination so we can just use memcpy to remove ambiguity > surrounding strncpy. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Signed-off-by: Justin Stitt I agree on the analysis of the replacements. Thanks for all the detail! Reviewed-by: Kees Cook -- Kees Cook