All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2-next] bpf: fix snprintf truncation warning
@ 2019-09-04 15:50 Andrea Claudi
  2019-09-04 16:28 ` Andrea Claudi
  2019-09-04 22:15 ` David Ahern
  0 siblings, 2 replies; 6+ messages in thread
From: Andrea Claudi @ 2019-09-04 15:50 UTC (permalink / raw)
  To: netdev; +Cc: stephen, dsahern

gcc v9.2.1 produces the following warning compiling iproute2:

bpf.c: In function ‘bpf_get_work_dir’:
bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
  784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
      |                                                 ^
bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
  784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.

Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 lib/bpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/bpf.c b/lib/bpf.c
index 7d2a322ffbaec..95de7894a93ce 100644
--- a/lib/bpf.c
+++ b/lib/bpf.c
@@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
 static const char *bpf_get_work_dir(enum bpf_prog_type type)
 {
 	static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
-	static char bpf_wrk_dir[PATH_MAX];
+	static char bpf_wrk_dir[PATH_MAX + 1];
 	static const char *mnt;
 	static bool bpf_mnt_cached;
 	const char *mnt_env = getenv(BPF_ENV_MNT);
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH iproute2-next] bpf: fix snprintf truncation warning
  2019-09-04 15:50 [PATCH iproute2-next] bpf: fix snprintf truncation warning Andrea Claudi
@ 2019-09-04 16:28 ` Andrea Claudi
  2019-09-04 22:15 ` David Ahern
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Claudi @ 2019-09-04 16:28 UTC (permalink / raw)
  To: linux-netdev; +Cc: Stephen Hemminger, David Ahern

On Wed, Sep 4, 2019 at 5:50 PM Andrea Claudi <aclaudi@redhat.com> wrote:
>
> gcc v9.2.1 produces the following warning compiling iproute2:
>
> bpf.c: In function ‘bpf_get_work_dir’:
> bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
>       |                                                 ^
> bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.
>
> Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> ---
>  lib/bpf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/bpf.c b/lib/bpf.c
> index 7d2a322ffbaec..95de7894a93ce 100644
> --- a/lib/bpf.c
> +++ b/lib/bpf.c
> @@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
>  static const char *bpf_get_work_dir(enum bpf_prog_type type)
>  {
>         static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
> -       static char bpf_wrk_dir[PATH_MAX];
> +       static char bpf_wrk_dir[PATH_MAX + 1];
>         static const char *mnt;
>         static bool bpf_mnt_cached;
>         const char *mnt_env = getenv(BPF_ENV_MNT);
> --
> 2.21.0
>

Sorry, I forgot to add:

Fixes: e42256699cac ("bpf: make tc's bpf loader generic and move into lib")

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH iproute2-next] bpf: fix snprintf truncation warning
  2019-09-04 15:50 [PATCH iproute2-next] bpf: fix snprintf truncation warning Andrea Claudi
  2019-09-04 16:28 ` Andrea Claudi
@ 2019-09-04 22:15 ` David Ahern
  2019-09-05 11:44   ` Andrea Claudi
  1 sibling, 1 reply; 6+ messages in thread
From: David Ahern @ 2019-09-04 22:15 UTC (permalink / raw)
  To: Andrea Claudi, netdev; +Cc: stephen, dsahern

On 9/4/19 9:50 AM, Andrea Claudi wrote:
> gcc v9.2.1 produces the following warning compiling iproute2:
> 
> bpf.c: In function ‘bpf_get_work_dir’:
> bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
>       |                                                 ^
> bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.
> 
> Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> ---
>  lib/bpf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/bpf.c b/lib/bpf.c
> index 7d2a322ffbaec..95de7894a93ce 100644
> --- a/lib/bpf.c
> +++ b/lib/bpf.c
> @@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
>  static const char *bpf_get_work_dir(enum bpf_prog_type type)
>  {
>  	static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
> -	static char bpf_wrk_dir[PATH_MAX];
> +	static char bpf_wrk_dir[PATH_MAX + 1];
>  	static const char *mnt;
>  	static bool bpf_mnt_cached;
>  	const char *mnt_env = getenv(BPF_ENV_MNT);
> 

PATH_MAX is meant to be the max length for a filesystem path including
the null terminator, so I think it would be better to change the
snprintf to 'sizeof(bpf_wrk_dir) - 1'.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH iproute2-next] bpf: fix snprintf truncation warning
  2019-09-04 22:15 ` David Ahern
@ 2019-09-05 11:44   ` Andrea Claudi
  2019-09-05 15:51     ` Stephen Hemminger
  0 siblings, 1 reply; 6+ messages in thread
From: Andrea Claudi @ 2019-09-05 11:44 UTC (permalink / raw)
  To: David Ahern; +Cc: linux-netdev, Stephen Hemminger, David Ahern

On Thu, Sep 5, 2019 at 12:15 AM David Ahern <dsahern@gmail.com> wrote:
>
> On 9/4/19 9:50 AM, Andrea Claudi wrote:
> > gcc v9.2.1 produces the following warning compiling iproute2:
> >
> > bpf.c: In function ‘bpf_get_work_dir’:
> > bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
> >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> >       |                                                 ^
> > bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
> >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> >       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >
> > Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.
> >
> > Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> > ---
> >  lib/bpf.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/lib/bpf.c b/lib/bpf.c
> > index 7d2a322ffbaec..95de7894a93ce 100644
> > --- a/lib/bpf.c
> > +++ b/lib/bpf.c
> > @@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
> >  static const char *bpf_get_work_dir(enum bpf_prog_type type)
> >  {
> >       static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
> > -     static char bpf_wrk_dir[PATH_MAX];
> > +     static char bpf_wrk_dir[PATH_MAX + 1];
> >       static const char *mnt;
> >       static bool bpf_mnt_cached;
> >       const char *mnt_env = getenv(BPF_ENV_MNT);
> >
>
> PATH_MAX is meant to be the max length for a filesystem path including
> the null terminator, so I think it would be better to change the
> snprintf to 'sizeof(bpf_wrk_dir) - 1'.

With 'sizeof(bpf_wrk_dir) - 1' snprintf simply truncates at byte 4095
instead of byte 4096.
This means that bpf_wrk_dir can again be truncated before the final
"/", as it is by now.
Am I missing something?

Trying your suggestion I have this slightly different warning message:

bpf.c: In function ‘bpf_get_work_dir’:
bpf.c:784:52: warning: ‘/’ directive output may be truncated writing 1
byte into a region of size between 0 and 4095 [-Wformat-truncation=]
  784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
      |                                                    ^
bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a
destination of size 4095
  784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH iproute2-next] bpf: fix snprintf truncation warning
  2019-09-05 11:44   ` Andrea Claudi
@ 2019-09-05 15:51     ` Stephen Hemminger
  2019-09-06 10:19       ` Andrea Claudi
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2019-09-05 15:51 UTC (permalink / raw)
  To: Andrea Claudi; +Cc: David Ahern, linux-netdev, David Ahern

On Thu, 5 Sep 2019 13:44:55 +0200
Andrea Claudi <aclaudi@redhat.com> wrote:

> On Thu, Sep 5, 2019 at 12:15 AM David Ahern <dsahern@gmail.com> wrote:
> >
> > On 9/4/19 9:50 AM, Andrea Claudi wrote:  
> > > gcc v9.2.1 produces the following warning compiling iproute2:
> > >
> > > bpf.c: In function ‘bpf_get_work_dir’:
> > > bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
> > >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> > >       |                                                 ^
> > > bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
> > >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> > >       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > >
> > > Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.
> > >
> > > Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> > > ---
> > >  lib/bpf.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/lib/bpf.c b/lib/bpf.c
> > > index 7d2a322ffbaec..95de7894a93ce 100644
> > > --- a/lib/bpf.c
> > > +++ b/lib/bpf.c
> > > @@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
> > >  static const char *bpf_get_work_dir(enum bpf_prog_type type)
> > >  {
> > >       static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
> > > -     static char bpf_wrk_dir[PATH_MAX];
> > > +     static char bpf_wrk_dir[PATH_MAX + 1];
> > >       static const char *mnt;
> > >       static bool bpf_mnt_cached;
> > >       const char *mnt_env = getenv(BPF_ENV_MNT);
> > >  
> >
> > PATH_MAX is meant to be the max length for a filesystem path including
> > the null terminator, so I think it would be better to change the
> > snprintf to 'sizeof(bpf_wrk_dir) - 1'.  
> 
> With 'sizeof(bpf_wrk_dir) - 1' snprintf simply truncates at byte 4095
> instead of byte 4096.
> This means that bpf_wrk_dir can again be truncated before the final
> "/", as it is by now.
> Am I missing something?
> 
> Trying your suggestion I have this slightly different warning message:
> 
> bpf.c: In function ‘bpf_get_work_dir’:
> bpf.c:784:52: warning: ‘/’ directive output may be truncated writing 1
> byte into a region of size between 0 and 4095 [-Wformat-truncation=]
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
>       |                                                    ^
> bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a
> destination of size 4095
>   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
>       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Why not rework this to use asprintf and avoid having huge buffers on stack?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH iproute2-next] bpf: fix snprintf truncation warning
  2019-09-05 15:51     ` Stephen Hemminger
@ 2019-09-06 10:19       ` Andrea Claudi
  0 siblings, 0 replies; 6+ messages in thread
From: Andrea Claudi @ 2019-09-06 10:19 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: David Ahern, linux-netdev, David Ahern

On Thu, Sep 5, 2019 at 5:51 PM Stephen Hemminger
<stephen@networkplumber.org> wrote:
>
> On Thu, 5 Sep 2019 13:44:55 +0200
> Andrea Claudi <aclaudi@redhat.com> wrote:
>
> > On Thu, Sep 5, 2019 at 12:15 AM David Ahern <dsahern@gmail.com> wrote:
> > >
> > > On 9/4/19 9:50 AM, Andrea Claudi wrote:
> > > > gcc v9.2.1 produces the following warning compiling iproute2:
> > > >
> > > > bpf.c: In function ‘bpf_get_work_dir’:
> > > > bpf.c:784:49: warning: ‘snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
> > > >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> > > >       |                                                 ^
> > > > bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a destination of size 4096
> > > >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir), "%s/", mnt);
> > > >       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > >
> > > > Fix it extending bpf_wrk_dir size by 1 byte for the extra "/" char.
> > > >
> > > > Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
> > > > ---
> > > >  lib/bpf.c | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/lib/bpf.c b/lib/bpf.c
> > > > index 7d2a322ffbaec..95de7894a93ce 100644
> > > > --- a/lib/bpf.c
> > > > +++ b/lib/bpf.c
> > > > @@ -742,7 +742,7 @@ static int bpf_gen_hierarchy(const char *base)
> > > >  static const char *bpf_get_work_dir(enum bpf_prog_type type)
> > > >  {
> > > >       static char bpf_tmp[PATH_MAX] = BPF_DIR_MNT;
> > > > -     static char bpf_wrk_dir[PATH_MAX];
> > > > +     static char bpf_wrk_dir[PATH_MAX + 1];
> > > >       static const char *mnt;
> > > >       static bool bpf_mnt_cached;
> > > >       const char *mnt_env = getenv(BPF_ENV_MNT);
> > > >
> > >
> > > PATH_MAX is meant to be the max length for a filesystem path including
> > > the null terminator, so I think it would be better to change the
> > > snprintf to 'sizeof(bpf_wrk_dir) - 1'.
> >
> > With 'sizeof(bpf_wrk_dir) - 1' snprintf simply truncates at byte 4095
> > instead of byte 4096.
> > This means that bpf_wrk_dir can again be truncated before the final
> > "/", as it is by now.
> > Am I missing something?
> >
> > Trying your suggestion I have this slightly different warning message:
> >
> > bpf.c: In function ‘bpf_get_work_dir’:
> > bpf.c:784:52: warning: ‘/’ directive output may be truncated writing 1
> > byte into a region of size between 0 and 4095 [-Wformat-truncation=]
> >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
> >       |                                                    ^
> > bpf.c:784:2: note: ‘snprintf’ output between 2 and 4097 bytes into a
> > destination of size 4095
> >   784 |  snprintf(bpf_wrk_dir, sizeof(bpf_wrk_dir) - 1, "%s/", mnt);
> >       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Why not rework this to use asprintf and avoid having huge buffers on stack?

Thanks for the suggestion. There are a lot of similar usages in
lib/bpf.c, I'll send a v2 to rework them all.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-09-06 10:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-04 15:50 [PATCH iproute2-next] bpf: fix snprintf truncation warning Andrea Claudi
2019-09-04 16:28 ` Andrea Claudi
2019-09-04 22:15 ` David Ahern
2019-09-05 11:44   ` Andrea Claudi
2019-09-05 15:51     ` Stephen Hemminger
2019-09-06 10:19       ` Andrea Claudi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.