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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28F71CE7A8A for ; Sun, 24 Sep 2023 03:15:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229819AbjIXDPS (ORCPT ); Sat, 23 Sep 2023 23:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229712AbjIXDPS (ORCPT ); Sat, 23 Sep 2023 23:15:18 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 701BF180 for ; Sat, 23 Sep 2023 20:15:11 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1c5cd27b1acso38318745ad.2 for ; Sat, 23 Sep 2023 20:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695525311; x=1696130111; 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=amfWmJu/D4QCQg50FQhzHOc1h7E/bVbjM0QuAI+Q2Gk=; b=TIDf5zSN9LdiJvqXnghqlpKej7FcCscsssdJmQ384ZNH02UE8rDH75xcZp0ayoaGpw xX8qcBgTt5F4wb9jHB2w7i4Tie2DrTPeZnrjlcHmsq0UvcpOw1om/8zNQUqzOpeEF2t1 c9nBnN6ewUnBoOaDKMZzzoumvH5eygjf+LBys= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695525311; x=1696130111; 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=amfWmJu/D4QCQg50FQhzHOc1h7E/bVbjM0QuAI+Q2Gk=; b=RlTvy+WjawYgg6BVQLKX+qjwbsAiF5FePMeWiVtyvPSzlJ5nlabgML014IdtGNPmKS UEph206ZBwttzB+0LFX2Ijxdzi474nY096vbW6nzSODct4HsADlCxKq6lwdowET5LXtq eeVl3JuJ9MRdR/LqR8Zeg7rfOh6znvzsxHvG2cgVZvzlf2zZ899vaMDhQytgoCPJl/Jg 8AaEWq3ag7fOj1a2LWmgg1ClleVwmcypoTxY5YDf1Bd9ad68oqK/k8vuJXuRDO3tOxJr fqC32YlzoUSfIvGxT/jnKkjrG2lWpumvhiy7CpuyL8hQi7Q7w0azC7r1NwquFf3SDX5g fHHw== X-Gm-Message-State: AOJu0YwNn0OShXstxstD4g9rO0biledwvxzzAhEPs6u0uG3KhQtlJiWt lNB+8479NoieHDCXNKqFiY0J4Q== X-Google-Smtp-Source: AGHT+IGisq+WD0tay8J8+6P9QCEXyW0JTTlcVZMxuf3DfjOxdjEguwLeM2nc5M2gPZJSS04saXBlow== X-Received: by 2002:a17:903:11c6:b0:1c4:4251:68b3 with SMTP id q6-20020a17090311c600b001c4425168b3mr4201343plh.54.1695525310870; Sat, 23 Sep 2023 20:15:10 -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 i18-20020a170902eb5200b001bbdd44bbb6sm700006pli.136.2023.09.23.20.15.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 20:15:10 -0700 (PDT) Date: Sat, 23 Sep 2023 20:15:09 -0700 From: Kees Cook To: Justin Stitt Cc: Jonathan Cameron , 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: <202309232006.690F89A@keescook> References: <20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org 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: Reviewed-by: Kees Cook :) -Kees -- Kees Cook