iio: adc: stm32-adc: Replace deprecated strncpy() with strscpy()
authorJustin Stitt <justinstitt@google.com>
Thu, 21 Sep 2023 04:54:00 +0000 (04:54 +0000)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Thu, 5 Oct 2023 14:01:08 +0000 (15:01 +0100)
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.

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
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/stm32-adc.c

index 25a912805439a33ceb5dedfd0ee8d8ec1dc79f8a..b5d3c9cea5c4e3a85f9470edc24bfd64e2b56784 100644 (file)
@@ -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);
                        ret = stm32_adc_populate_int_ch(indio_dev, name, val);
                        if (ret == -ENOENT)
                                continue;