Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp500963rdb; Sat, 30 Sep 2023 13:13:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmrLy/cq/M8302/HSXHjBmbAVBWKFPsuMMw+Fre7wipRD8GW485HB9TcERKEOagkYPDYS5 X-Received: by 2002:a05:6a20:9798:b0:155:bff3:ea1a with SMTP id hx24-20020a056a20979800b00155bff3ea1amr7314390pzc.33.1696104782454; Sat, 30 Sep 2023 13:13:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696104782; cv=none; d=google.com; s=arc-20160816; b=rHNuVH6acQPboZLZJpYjfqqSSkrSLV4Ll1SXSXRL+TOWOuVLc3Jaa+8+dw4FEsJWXG eSS0wpaBHFkFPWpbrvbZyW1QoXU223Gy/0fICgEhnJGC7bhJBQD7K6tyX1dIW0OE/LU4 yn+i5aeNaB2ZabhQ9JvEmKi+dtpMJyPCANO2kYN3HRjh7Ob7zpIdqQmQVM/VCEOPzRyG daFyWxxyFURC1L4xhiV9eZV+iyduFU5r43mG81cHaHhuki98EdthDSgJJ1pI1TBihAlF 9dsT56s4fvQIsHvwFtcKVgK5HaDuFknuat4dc4Pyo0goxFGbw9Y1paIosN+BB+QIxhJB 3NfA== 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=Ye4XTAWb4daUNWZiDSISaHwk+X7iJoEzGWCL7sgi0rE=; fh=GCgrk0qFji53gGbIh4VYmapVVrFgAQG9aNTrRzW9tww=; b=ZXFivbCPYjkg4TgC+jJIXVXeSFdoc+/DSUshk+Gw67r+I6EB9BAuqoQV8iJzXo6UKX gItBNhG+a0hG9cQRtMbpaEc9YujwQKW6+1Ib6v+LGHDQ4ETrOIX1AW0l/gvpUixQL9Da oCDvWuLLnegqQKlMg4nrnfhdNb5hBUp8nmJrfC9ATN957B+m6GN6o2BH3KoNlckDglaG Xaf9fQW85AaPSDR5AXljmJ5lC3plrZ21IUopt9jlSwjwVj/RTPAJI3RLwLfnW0Y7VERR 9+BPzgRqSR7YzoEv79P+u3iTh2wh/bPC1dzFd/YS8/qUAZJw9lfRycSzRAgIAJ7HpKGG e74A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FggECs0m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id cu9-20020a056a00448900b00690b8833ccbsi23485216pfb.270.2023.09.30.13.13.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Sep 2023 13:13:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FggECs0m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 6065E819E144; Sat, 30 Sep 2023 10:58:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234695AbjI3R5M (ORCPT + 99 others); Sat, 30 Sep 2023 13:57:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234684AbjI3R5L (ORCPT ); Sat, 30 Sep 2023 13:57:11 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDF63E3 for ; Sat, 30 Sep 2023 10:57:08 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1c724577e1fso51598335ad.0 for ; Sat, 30 Sep 2023 10:57:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696096628; x=1696701428; 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=Ye4XTAWb4daUNWZiDSISaHwk+X7iJoEzGWCL7sgi0rE=; b=FggECs0mPLgxcWtFYGjkFui3NvNmMxZ9Xr9H8G4dQUZKr+MYBiC5Z9+4EOt6b2NFiq oybo/ah6svBaPp5fG67JFoOlCUFkroXPiBHV+1iK4ShllGYbaTaH6+Uvw+jsEfuxGJxb LZBz0cWqvi6bN3Ntz0QR+QIxAqbNiqSTWa7E0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696096628; x=1696701428; 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=Ye4XTAWb4daUNWZiDSISaHwk+X7iJoEzGWCL7sgi0rE=; b=ds1MBNwNYLG2Op3oXJ/Ux76JTzhEfIxsrb9HRg3kotSnKyyP+6/P2Q/weBSGf3oont qWvXysqINjzDtnyNqAbf3zqv902s4CCnkl1Q6tCP2V5BaqEH4DVwosJ68alRjJyXCZXx 2jDbCW0f6CVx7Rg7K2orOnB7QL+Zb+DiA6oBkuSLOh4g1DcTiNLBTOBamHVXCJIXdPFk crvRpxu0+DYAudc320qhkc5WOpHPFrNVfzmIL7QSb28FYN9j5yuFsDuycN6dTfwY+Yqs 00VAmSc8+pYBUji5GkBljnCRyoPH4anKqUeYx53gu3AHwM2JzK7cOGH2G7pHaulIHFHC oPIQ== X-Gm-Message-State: AOJu0YwvipDuTojfi6O9kj6CRCAJFcN4Wgb2GX4c33Of3TGDSsMbpmwf Qsmr7U5INukQmKIyEY7q6RV8dw== X-Received: by 2002:a17:902:ee89:b0:1c5:f68d:cec3 with SMTP id a9-20020a170902ee8900b001c5f68dcec3mr6686648pld.64.1696096628167; Sat, 30 Sep 2023 10:57:08 -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 x10-20020a170902820a00b001c1f161949fsm18920114pln.96.2023.09.30.10.57.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Sep 2023 10:57:07 -0700 (PDT) Date: Sat, 30 Sep 2023 10:57:03 -0700 From: Kees Cook To: Jonathan Cameron Cc: Justin Stitt , Lars-Peter Clausen , Maxime Coquelin , Alexandre Torgue , linux-iio@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] iio: adc: stm32-adc: replace deprecated strncpy Message-ID: <202309301056.5A21C37D5@keescook> References: <20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com> <202309232006.690F89A@keescook> <20230930184118.73d7465a@jic23-huawei> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230930184118.73d7465a@jic23-huawei> 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 agentk.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 (agentk.vger.email [0.0.0.0]); Sat, 30 Sep 2023 10:58:02 -0700 (PDT) On Sat, Sep 30, 2023 at 06:41:18PM +0100, Jonathan Cameron wrote: > On Sat, 23 Sep 2023 20:15:09 -0700 > Kees Cook wrote: > > > On Thu, Sep 21, 2023 at 04:54:00AM +0000, Justin Stitt wrote: > > > `strncpy` is deprecated for use on NUL-terminated destination strings [1]. > > > > > > We should prefer more robust and less ambiguous string interfaces. > > > > > > We expect adc->chan_name[val] to be NUL-terminated based on ch_name's > > > use within functions that expect NUL-terminated strings like strncmp and > > > printf-likes: > > > | if (!strncmp(stm32_adc_ic[i].name, ch_name, STM32_ADC_CH_SZ)) { > > > | /* Check internal channel availability */ > > > | switch (i) { > > > | case STM32_ADC_INT_CH_VDDCORE: > > > | if (!adc->cfg->regs->or_vddcore.reg) > > > | dev_warn(&indio_dev->dev, > > > | "%s channel not available\n", ch_name); > > > ... > > > > > > There is no evidence that NUL-padding is needed either. > > > > Agreed -- it's used as a C string everywhere I can see. > > > > > > > > Considering the above, a suitable replacement is `strscpy` [2] due to > > > the fact that it guarantees NUL-termination on the destination buffer > > > without unnecessarily NUL-padding. If, for any reason, NUL-padding _is_ > > > required we should go for `strscpy_pad`. > > > > > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > > > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > > > Link: https://github.com/KSPP/linux/issues/90 > > > Cc: linux-hardening@vger.kernel.org > > > Signed-off-by: Justin Stitt > > > --- > > > Note: build-tested > > > --- > > > drivers/iio/adc/stm32-adc.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c > > > index f7613efb870d..9cdcf396d901 100644 > > > --- a/drivers/iio/adc/stm32-adc.c > > > +++ b/drivers/iio/adc/stm32-adc.c > > > @@ -2209,7 +2209,7 @@ static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev, > > > ret = -EINVAL; > > > goto err; > > > } > > > - strncpy(adc->chan_name[val], name, STM32_ADC_CH_SZ); > > > + strscpy(adc->chan_name[val], name, STM32_ADC_CH_SZ); > > > > I still prefer sizeof($dst), but yes, these are the same: > > > > char chan_name[STM32_ADC_CH_MAX][STM32_ADC_CH_SZ]; > > > > If this needs a v2, please improve the Subject, but it is technically > > correct, so: > > Hi Kees, > > I can tweak the subject whilst applying. What did you have in mind > as a better one? I would use "iio: adc: stm32-adc: Replace deprecated strncpy() with strscpy()" Thanks! -Kees -- Kees Cook