diff mbox series

tools lib api fs: fix gcc9 compilation error

Message ID 20191211080109.18765-1-andrey.zhizhikin@leica-geosystems.com
State Not Applicable
Delegated to: David Miller
Headers show
Series tools lib api fs: fix gcc9 compilation error | expand

Commit Message

Andrey Zhizhikin Dec. 11, 2019, 8:01 a.m. UTC
GCC9 introduced string hardening mechanisms, which exhibits the error
during fs api compilation:

error: '__builtin_strncpy' specified bound 4096 equals destination size
[-Werror=stringop-truncation]

This comes when the length of copy passed to strncpy is is equal to
destination size, which could potentially lead to buffer overflow.

There is a need to mitigate this potential issue by limiting the size of
destination by 1 and explicitly terminate the destination with NULL.

Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
---
 tools/lib/api/fs/fs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Andrey Zhizhikin Dec. 17, 2019, 9:21 p.m. UTC | #1
Hello all,

I'd like to have a gentle ping on this patch, if someone could review
and apply it - I'd really appreciate it.

On Wed, Dec 11, 2019 at 9:01 AM Andrey Zhizhikin <andrey.z@gmail.com> wrote:
>
> GCC9 introduced string hardening mechanisms, which exhibits the error
> during fs api compilation:
>
> error: '__builtin_strncpy' specified bound 4096 equals destination size
> [-Werror=stringop-truncation]
>
> This comes when the length of copy passed to strncpy is is equal to
> destination size, which could potentially lead to buffer overflow.
>
> There is a need to mitigate this potential issue by limiting the size of
> destination by 1 and explicitly terminate the destination with NULL.
>
> Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> ---
>  tools/lib/api/fs/fs.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
> index 11b3885e833e..027b18f7ed8c 100644
> --- a/tools/lib/api/fs/fs.c
> +++ b/tools/lib/api/fs/fs.c
> @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
>         size_t name_len = strlen(fs->name);
>         /* name + "_PATH" + '\0' */
>         char upper_name[name_len + 5 + 1];
> +
>         memcpy(upper_name, fs->name, name_len);
>         mem_toupper(upper_name, name_len);
>         strcpy(&upper_name[name_len], "_PATH");
> @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
>                 return false;
>
>         fs->found = true;
> -       strncpy(fs->path, override_path, sizeof(fs->path));
> +       strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> +       fs->path[sizeof(fs->path) - 1] = '\0';
>         return true;
>  }
>
> --
> 2.17.1
>
Sergey Senozhatsky Dec. 18, 2019, 3:10 a.m. UTC | #2
On (19/12/11 08:01), Andrey Zhizhikin wrote:
[..]
> @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
>  	size_t name_len = strlen(fs->name);
>  	/* name + "_PATH" + '\0' */
>  	char upper_name[name_len + 5 + 1];
> +
>  	memcpy(upper_name, fs->name, name_len);
>  	mem_toupper(upper_name, name_len);
>  	strcpy(&upper_name[name_len], "_PATH");
> @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
>  		return false;
>  
>  	fs->found = true;
> -	strncpy(fs->path, override_path, sizeof(fs->path));
> +	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> +	fs->path[sizeof(fs->path) - 1] = '\0';

I think the trend these days is to prefer stracpy/strscpy over
strcpy/strlcpy/strncpy.

	-ss
Sergey Senozhatsky Dec. 18, 2019, 4:40 a.m. UTC | #3
On (19/12/18 12:10), Sergey Senozhatsky wrote:
> On (19/12/11 08:01), Andrey Zhizhikin wrote:
> [..]
> > @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
> >  	size_t name_len = strlen(fs->name);
> >  	/* name + "_PATH" + '\0' */
> >  	char upper_name[name_len + 5 + 1];
> > +
> >  	memcpy(upper_name, fs->name, name_len);
> >  	mem_toupper(upper_name, name_len);
> >  	strcpy(&upper_name[name_len], "_PATH");
> > @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
> >  		return false;
> >  
> >  	fs->found = true;
> > -	strncpy(fs->path, override_path, sizeof(fs->path));
> > +	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> > +	fs->path[sizeof(fs->path) - 1] = '\0';
> 
> I think the trend these days is to prefer stracpy/strscpy over
> strcpy/strlcpy/strncpy.

Scratch that. This is user-space, I should pay more attention.

	-ss
Jiri Olsa Dec. 18, 2019, 7:29 a.m. UTC | #4
On Wed, Dec 11, 2019 at 08:01:09AM +0000, Andrey Zhizhikin wrote:
> GCC9 introduced string hardening mechanisms, which exhibits the error
> during fs api compilation:
> 
> error: '__builtin_strncpy' specified bound 4096 equals destination size
> [-Werror=stringop-truncation]
> 
> This comes when the length of copy passed to strncpy is is equal to
> destination size, which could potentially lead to buffer overflow.
> 
> There is a need to mitigate this potential issue by limiting the size of
> destination by 1 and explicitly terminate the destination with NULL.
> 
> Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Jiri Olsa <jolsa@kernel.org>

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> ---
>  tools/lib/api/fs/fs.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
> index 11b3885e833e..027b18f7ed8c 100644
> --- a/tools/lib/api/fs/fs.c
> +++ b/tools/lib/api/fs/fs.c
> @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
>  	size_t name_len = strlen(fs->name);
>  	/* name + "_PATH" + '\0' */
>  	char upper_name[name_len + 5 + 1];
> +
>  	memcpy(upper_name, fs->name, name_len);
>  	mem_toupper(upper_name, name_len);
>  	strcpy(&upper_name[name_len], "_PATH");
> @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
>  		return false;
>  
>  	fs->found = true;
> -	strncpy(fs->path, override_path, sizeof(fs->path));
> +	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> +	fs->path[sizeof(fs->path) - 1] = '\0';
>  	return true;
>  }
>  
> -- 
> 2.17.1
>
Petr Mladek Dec. 18, 2019, 11:36 a.m. UTC | #5
On Wed 2019-12-11 08:01:09, Andrey Zhizhikin wrote:
> GCC9 introduced string hardening mechanisms, which exhibits the error
> during fs api compilation:
> 
> error: '__builtin_strncpy' specified bound 4096 equals destination size
> [-Werror=stringop-truncation]
> 
> This comes when the length of copy passed to strncpy is is equal to
> destination size, which could potentially lead to buffer overflow.
> 
> There is a need to mitigate this potential issue by limiting the size of
> destination by 1 and explicitly terminate the destination with NULL.
> 
> Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Jiri Olsa <jolsa@kernel.org>

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
Arnaldo Carvalho de Melo Dec. 18, 2019, 2:33 p.m. UTC | #6
Em Wed, Dec 11, 2019 at 08:01:09AM +0000, Andrey Zhizhikin escreveu:
> GCC9 introduced string hardening mechanisms, which exhibits the error
> during fs api compilation:
> 
> error: '__builtin_strncpy' specified bound 4096 equals destination size
> [-Werror=stringop-truncation]
> 
> This comes when the length of copy passed to strncpy is is equal to
> destination size, which could potentially lead to buffer overflow.
> 
> There is a need to mitigate this potential issue by limiting the size of
> destination by 1 and explicitly terminate the destination with NULL.

Thanks, applied and collected the reviewed-by and acked-by provided,

- Arnaldo
 
> Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> ---
>  tools/lib/api/fs/fs.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
> index 11b3885e833e..027b18f7ed8c 100644
> --- a/tools/lib/api/fs/fs.c
> +++ b/tools/lib/api/fs/fs.c
> @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
>  	size_t name_len = strlen(fs->name);
>  	/* name + "_PATH" + '\0' */
>  	char upper_name[name_len + 5 + 1];
> +
>  	memcpy(upper_name, fs->name, name_len);
>  	mem_toupper(upper_name, name_len);
>  	strcpy(&upper_name[name_len], "_PATH");
> @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
>  		return false;
>  
>  	fs->found = true;
> -	strncpy(fs->path, override_path, sizeof(fs->path));
> +	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> +	fs->path[sizeof(fs->path) - 1] = '\0';
>  	return true;
>  }
>  
> -- 
> 2.17.1
Andrey Zhizhikin Dec. 18, 2019, 5:03 p.m. UTC | #7
On Wed, Dec 18, 2019 at 3:33 PM Arnaldo Carvalho de Melo
<arnaldo.melo@gmail.com> wrote:
>
> Em Wed, Dec 11, 2019 at 08:01:09AM +0000, Andrey Zhizhikin escreveu:
> > GCC9 introduced string hardening mechanisms, which exhibits the error
> > during fs api compilation:
> >
> > error: '__builtin_strncpy' specified bound 4096 equals destination size
> > [-Werror=stringop-truncation]
> >
> > This comes when the length of copy passed to strncpy is is equal to
> > destination size, which could potentially lead to buffer overflow.
> >
> > There is a need to mitigate this potential issue by limiting the size of
> > destination by 1 and explicitly terminate the destination with NULL.
>
> Thanks, applied and collected the reviewed-by and acked-by provided,

Thanks a lot for review and collecting this patch!

>
> - Arnaldo
>
> > Signed-off-by: Andrey Zhizhikin <andrey.zhizhikin@leica-geosystems.com>
> > Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  tools/lib/api/fs/fs.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
> > index 11b3885e833e..027b18f7ed8c 100644
> > --- a/tools/lib/api/fs/fs.c
> > +++ b/tools/lib/api/fs/fs.c
> > @@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
> >       size_t name_len = strlen(fs->name);
> >       /* name + "_PATH" + '\0' */
> >       char upper_name[name_len + 5 + 1];
> > +
> >       memcpy(upper_name, fs->name, name_len);
> >       mem_toupper(upper_name, name_len);
> >       strcpy(&upper_name[name_len], "_PATH");
> > @@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
> >               return false;
> >
> >       fs->found = true;
> > -     strncpy(fs->path, override_path, sizeof(fs->path));
> > +     strncpy(fs->path, override_path, sizeof(fs->path) - 1);
> > +     fs->path[sizeof(fs->path) - 1] = '\0';
> >       return true;
> >  }
> >
> > --
> > 2.17.1
>
> --
>
> - Arnaldo
diff mbox series

Patch

diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
index 11b3885e833e..027b18f7ed8c 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -210,6 +210,7 @@  static bool fs__env_override(struct fs *fs)
 	size_t name_len = strlen(fs->name);
 	/* name + "_PATH" + '\0' */
 	char upper_name[name_len + 5 + 1];
+
 	memcpy(upper_name, fs->name, name_len);
 	mem_toupper(upper_name, name_len);
 	strcpy(&upper_name[name_len], "_PATH");
@@ -219,7 +220,8 @@  static bool fs__env_override(struct fs *fs)
 		return false;
 
 	fs->found = true;
-	strncpy(fs->path, override_path, sizeof(fs->path));
+	strncpy(fs->path, override_path, sizeof(fs->path) - 1);
+	fs->path[sizeof(fs->path) - 1] = '\0';
 	return true;
 }