diff mbox series

iio: adc: stm32-adc: replace deprecated strncpy

Message ID 20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com (mailing list archive)
State Accepted
Headers show
Series iio: adc: stm32-adc: replace deprecated strncpy | expand

Commit Message

Justin Stitt Sept. 21, 2023, 4:54 a.m. UTC
`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 [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 <justinstitt@google.com>
---
Note: build-tested
---
 drivers/iio/adc/stm32-adc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


---
base-commit: 2cf0f715623872823a72e451243bbf555d10d032
change-id: 20230921-strncpy-drivers-iio-adc-stm32-adc-c-1bf936a4ffbb

Best regards,
--
Justin Stitt <justinstitt@google.com>

Comments

Kees Cook Sept. 24, 2023, 3:15 a.m. UTC | #1
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 <justinstitt@google.com>
> ---
> 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 <keescook@chromium.org>

:)

-Kees
Jonathan Cameron Sept. 30, 2023, 5:41 p.m. UTC | #2
On Sat, 23 Sep 2023 20:15:09 -0700
Kees Cook <keescook@chromium.org> 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 <justinstitt@google.com>
> > ---
> > 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 <keescook@chromium.org>
> 
> :)
> 
> -Kees
>
Kees Cook Sept. 30, 2023, 5:57 p.m. UTC | #3
On Sat, Sep 30, 2023 at 06:41:18PM +0100, Jonathan Cameron wrote:
> On Sat, 23 Sep 2023 20:15:09 -0700
> Kees Cook <keescook@chromium.org> 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 <justinstitt@google.com>
> > > ---
> > > 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
Jonathan Cameron Oct. 5, 2023, 2:02 p.m. UTC | #4
On Sat, 30 Sep 2023 10:57:03 -0700
Kees Cook <keescook@chromium.org> wrote:

> On Sat, Sep 30, 2023 at 06:41:18PM +0100, Jonathan Cameron wrote:
> > On Sat, 23 Sep 2023 20:15:09 -0700
> > Kees Cook <keescook@chromium.org> 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 <justinstitt@google.com>
> > > > ---
> > > > 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()"
Makes sense. I also used the () approach to identify functions in the
text of the patch description instead of `funcname`

Applied with those tweaks to the togreg branch of iio.git but initially
just pushed out as testing to let 0-day take a look at it.

Thanks,

Jonathan

> 
> Thanks!
> 
> -Kees
>
diff mbox series

Patch

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);
 			ret = stm32_adc_populate_int_ch(indio_dev, name, val);
 			if (ret == -ENOENT)
 				continue;