[PATCH v2] f2fs: replace deprecated strcpy with strscpy

Daniel Yang posted 1 patch 2 weeks ago
There is a newer version of this series
fs/f2fs/super.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
[PATCH v2] f2fs: replace deprecated strcpy with strscpy
Posted by Daniel Yang 2 weeks ago
strcpy is deprecated. Kernel docs recommend replacing strcpy with
strscpy. The function strcpy() return value isn't used so there
shouldn't be an issue replacing with the safer alternative strscpy.

Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
---
V1 -> V2: handle strscpy errors, changed prefix to f2fs

 fs/f2fs/super.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 87ab5696b..4721a8a8f 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  *             http://www.samsung.com/
  */
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/fs.h>
@@ -1158,7 +1159,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
 				break;
 			}
 
-			strcpy(ext[ext_cnt], name);
+			if (strscpy(ext[ext_cnt], name) == -E2BIG) {
+				kfree(name);
+				return -EINVAL;
+			}
 			F2FS_OPTION(sbi).compress_ext_cnt++;
 			kfree(name);
 			break;
@@ -1187,7 +1191,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
 				break;
 			}
 
-			strcpy(noext[noext_cnt], name);
+			if (strscpy(noext[noext_cnt], name) == -E2BIG) {
+				kfree(name);
+				return -EINVAL;
+			}
 			F2FS_OPTION(sbi).nocompress_ext_cnt++;
 			kfree(name);
 			break;
-- 
2.39.5
Re: [PATCH v2] f2fs: replace deprecated strcpy with strscpy
Posted by Chao Yu 5 days, 18 hours ago
On 2024/11/9 9:38, Daniel Yang wrote:
> strcpy is deprecated. Kernel docs recommend replacing strcpy with
> strscpy. The function strcpy() return value isn't used so there
> shouldn't be an issue replacing with the safer alternative strscpy.
> 
> Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
> ---
> V1 -> V2: handle strscpy errors, changed prefix to f2fs
> 
>   fs/f2fs/super.c | 11 +++++++++--
>   1 file changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
> index 87ab5696b..4721a8a8f 100644
> --- a/fs/f2fs/super.c
> +++ b/fs/f2fs/super.c
> @@ -5,6 +5,7 @@
>    * Copyright (c) 2012 Samsung Electronics Co., Ltd.
>    *             http://www.samsung.com/
>    */
>   #include <linux/module.h>
>   #include <linux/init.h>
>   #include <linux/fs.h>
> @@ -1158,7 +1159,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
>   				break;
>   			}
>   
> -			strcpy(ext[ext_cnt], name);
> +			if (strscpy(ext[ext_cnt], name) == -E2BIG) {
> +				kfree(name);
> +				return -EINVAL;

How about?

ret = strscpy(ext[ext_cnt], name);
if (ret < 0) {
	kfree(name);
	return ret;
}

> +			}
>   			F2FS_OPTION(sbi).compress_ext_cnt++;
>   			kfree(name);
>   			break;
> @@ -1187,7 +1191,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
>   				break;
>   			}
>   
> -			strcpy(noext[noext_cnt], name);
> +			if (strscpy(noext[noext_cnt], name) == -E2BIG) {
> +				kfree(name);
> +				return -EINVAL;
> +			}

Ditto

Thanks,

>   			F2FS_OPTION(sbi).nocompress_ext_cnt++;
>   			kfree(name);
>   			break;
Re: [PATCH v2] f2fs: replace deprecated strcpy with strscpy
Posted by Daniel Yang 5 days, 12 hours ago
On Sun, Nov 17, 2024 at 5:24 PM Chao Yu <chao@kernel.org> wrote:
>
> On 2024/11/9 9:38, Daniel Yang wrote:
> > strcpy is deprecated. Kernel docs recommend replacing strcpy with
> > strscpy. The function strcpy() return value isn't used so there
> > shouldn't be an issue replacing with the safer alternative strscpy.
> >
> > Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
> > ---
> > V1 -> V2: handle strscpy errors, changed prefix to f2fs
> >
> >   fs/f2fs/super.c | 11 +++++++++--
> >   1 file changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
> > index 87ab5696b..4721a8a8f 100644
> > --- a/fs/f2fs/super.c
> > +++ b/fs/f2fs/super.c
> > @@ -5,6 +5,7 @@
> >    * Copyright (c) 2012 Samsung Electronics Co., Ltd.
> >    *             http://www.samsung.com/
> >    */
> >   #include <linux/module.h>
> >   #include <linux/init.h>
> >   #include <linux/fs.h>
> > @@ -1158,7 +1159,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
> >                               break;
> >                       }
> >
> > -                     strcpy(ext[ext_cnt], name);
> > +                     if (strscpy(ext[ext_cnt], name) == -E2BIG) {
> > +                             kfree(name);
> > +                             return -EINVAL;
>
> How about?
>
> ret = strscpy(ext[ext_cnt], name);
> if (ret < 0) {
>         kfree(name);
>         return ret;
> }
>
> > +                     }
> >                       F2FS_OPTION(sbi).compress_ext_cnt++;
> >                       kfree(name);
> >                       break;
> > @@ -1187,7 +1191,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
> >                               break;
> >                       }
> >
> > -                     strcpy(noext[noext_cnt], name);
> > +                     if (strscpy(noext[noext_cnt], name) == -E2BIG) {
> > +                             kfree(name);
> > +                             return -EINVAL;
> > +                     }
>
> Ditto
>
> Thanks,
>
> >                       F2FS_OPTION(sbi).nocompress_ext_cnt++;
> >                       kfree(name);
> >                       break;
>

Thanks for the feedback. That does look much cleaner. I've applied the
changes and sent v3.

- Daniel