From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1676EE7737E for ; Sat, 30 Sep 2023 17:41:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=JVjA42rapmIKTONE9fRVokmirc0pNgcQZdRIQ/BY40E=; b=25jfmJWFeE4qhQ jIDB2OaARvNdKKeAjOm3ohzgTvLblpNydprLVSMlP65Kh5CqtYFbI+NEf8jlDc9zCMFZEL6IseWkf dVMMhYn2PCrL8RHjaPZUOn2fPvZXeplPhaepPF/964nMNSwjS/ZorHZYc/cHoIUQkl6L7BAizCw8b zBSsLQNBUZ1aSP1i4FexlFhcrNSFSvcZTltd2bSdAvghxqMGEHQnMGt3gnsqNk0LhEaXsWEsqM4H9 Me/oeKL+hnYRl9tdjYOXWG7M4BNcoAwefBX4kwNfIKsjCe2KJxd3+PJwi5eKj5khUVhn9ZwTTMhgF p8DyZZMdiwZ3i8XYR2xQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qmdy1-009woo-3D; Sat, 30 Sep 2023 17:41:25 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qmdxy-009woH-15 for linux-arm-kernel@lists.infradead.org; Sat, 30 Sep 2023 17:41:24 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 88981B80010; Sat, 30 Sep 2023 17:41:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 958E0C433C8; Sat, 30 Sep 2023 17:41:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696095679; bh=+etRTCZPNPJTHh+N1MKyjYR8NQe0gI1JFDlR+ExjRJM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=mBeARBSn8fmfsR2i08eGBTauyugnNT7jKXny6cHo6Y6UuKmLBwA7OE1hzwLzty89c m3TMncW+2/Y+MrwZQ7ZPMSQDMs771zemoTlLVNF0ky0hbF1ZuKXbHW0Mp7RyWLypGt fUnA3jvf6gMkX2ySekmdZrxt2ghgjJkTbbl7RbZARkYQlhazUWdBJOQG8bA/FuZEaS ydjyOYcA4ifQOqIRtf75va8Sko3Vu8tsOD/68syYDf3eN5hxqiwY3JIBfD5vVF9Qkg 5iEwRZPV2DNNyDWEYHAtd8KAt6sowjLeULvNm20d/f1Z/J2NfDolaPvIt3T7pLNPLJ Lf2vIDHQ3qHBA== Date: Sat, 30 Sep 2023 18:41:18 +0100 From: Jonathan Cameron To: Kees Cook 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: <20230930184118.73d7465a@jic23-huawei> In-Reply-To: <202309232006.690F89A@keescook> References: <20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com> <202309232006.690F89A@keescook> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230930_104122_679290_EBEA133C X-CRM114-Status: GOOD ( 26.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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? Jonathan > > Reviewed-by: Kees Cook > > :) > > -Kees > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel