Covscan: Misc fixes
Subsystems (3)
v2 cover Covscan: Misc fixes
This series collects patches from v1 addressing miscellaneous issues detected by covscan.
No changes to the actual patches, just splitting into smaller series.
Phil Sutter (7):
nstat: Avoid passing negative fd to fdopen()
ss: Make sure index variable is >= 0
ss: Make sure scanned index value to unix_state_map is sane netem/maketable: Check return value of fscanf() lib/bpf: Check return value of write()
lib/fs: Fix and simplify make_path() lib/libnetlink: Don't pass NULL parameter to memcpy()
lib/bpf.c | 3 ++- lib/fs.c | 20 +++++--------------- lib/libnetlink.c | 6 ++++-- misc/nstat.c | 15 +++++++++++---- misc/ss.c | 5 +++-- netem/maketable.c | 4 ++-- 6 files changed, 27 insertions(+), 26 deletions(-)
Signature · 1 line
2.13.1
PATCH v2 1/7 nstat: Avoid passing negative fd to fdopen()
Introduce a wrapper which does the sanity checking and returns NULL in case fd is invalid.
--- misc/nstat.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
misc/nstat.c +11 -4
@@ -252,9 +252,16 @@ static void load_ugly_table(FILE *fp) } } +static FILE *fdopen_null (int fd,const char *mode) +{ +if (fd <0 ) +return NULL ; +return fdopen (fd, mode); +} +static void load_sctp_snmp (void ) { -FILE *fp =fdopen (net_sctp_snmp_open (),"r" ); +FILE *fp =fdopen_null (net_sctp_snmp_open (),"r" );if (fp) {load_good_table (fp);
@@ -264,7 +271,7 @@ static void load_sctp_snmp(void)static void load_snmp (void ) { -FILE *fp =fdopen (net_snmp_open (),"r" ); +FILE *fp =fdopen_null (net_snmp_open (),"r" );if (fp) {load_ugly_table (fp);
@@ -274,7 +281,7 @@ static void load_snmp(void)static void load_snmp6 (void ) { -FILE *fp =fdopen (net_snmp6_open (),"r" ); +FILE *fp =fdopen_null (net_snmp6_open (),"r" );if (fp) {load_good_table (fp);
@@ -284,7 +291,7 @@ static void load_snmp6(void)static void load_netstat (void ) { -FILE *fp =fdopen (net_netstat_open (),"r" ); +FILE *fp =fdopen_null (net_netstat_open (),"r" );if (fp) {load_ugly_table (fp);
Signature · 1 line
2.13.1
Re: [iproute PATCH v2 1/7] nstat: Avoid passing negative fd to fdopen()
Quoted 49 lines · On Mon, 21 Aug 2017 19:08:07 +0200 Phil Sutter <[email protected]> wrote:
Introduce a wrapper which does the sanity checking and returns NULL in case fd is invalid.
Signed-off-by Phil Sutter <[email protected]>--- misc/nstat.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)misc/nstat.c +10
-3@@ -252,9 +252,16 @@ static void load_ugly_table(FILE *fp) } } +static FILE *fdopen_null (int fd,const char *mode) +{ +if (fd <0 ) +return NULL ; +return fdopen (fd, mode); +} +static void load_sctp_snmp (void ) { -FILE *fp =fdopen (net_sctp_snmp_open (),"r" ); +FILE *fp =fdopen_null (net_sctp_snmp_open (),"r" );if (fp) {load_good_table (fp);@@ -264,7 +271,7 @@ static void load_sctp_snmp(void)static void load_snmp (void ) { -FILE *fp =fdopen (net_snmp_open (),"r" ); +FILE *fp =fdopen_null (net_snmp_open (),"r" );if (fp) {load_ugly_table (fp);@@ -274,7 +281,7 @@ static void load_snmp(void)static void load_snmp6 (void ) { -FILE *fp =fdopen (net_snmp6_open (),"r" ); +FILE *fp =fdopen_null (net_snmp6_open (),"r" );if (fp) {load_good_table (fp);
misc/nstat.c static void load_snmp6(void) · line 297
Quoted 8 lines
misc/nstat.c +1
-1@@ -284,7 +291,7 @@ static void load_snmp6(void)static void load_netstat (void ) { -FILE *fp =fdopen (net_netstat_open (),"r" ); +FILE *fp =fdopen_null (net_netstat_open (),"r" );if (fp) {load_ugly_table (fp);
Why not just fix it at the source of the open.
I.e
static FILE *generic_proc_open(condt char * env, const char *name) {
...
return fopen(p, "r"); }
Re: [iproute PATCH v2 1/7] nstat: Avoid passing negative fd to fdopen()
Quoted 33 lines · Stephen Hemminger
Quoted 30 lines · On Mon, 21 Aug 2017 19:08:07 +0200 Phil Sutter <[email protected]> wrote:
Introduce a wrapper which does the sanity checking and returns NULL in case fd is invalid.
Signed-off-by Phil Sutter <[email protected]>--- misc/nstat.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)misc/nstat.c +8
-1@@ -252,9 +252,16 @@ static void load_ugly_table(FILE *fp) } } +static FILE *fdopen_null (int fd,const char *mode) +{ +if (fd <0 ) +return NULL ; +return fdopen (fd, mode); +} +static void load_sctp_snmp (void ) { -FILE *fp =fdopen (net_sctp_snmp_open (),"r" ); +FILE *fp =fdopen_null (net_sctp_snmp_open (),"r" );if (fp) {load_good_table (fp);
[...]
Quoted 8 lines
Why not just fix it at the source of the open.
I.e
static FILE *generic_proc_open(condt char * env, const char *name) {
...return fopen(p, "r"); }
What should it do? All the load_*() functions are supposed to be fault-tolerant, so calling abort() or something is not an option.
Thanks, Phil
PATCH v2 2/7 ss: Make sure index variable is >= 0
In reply to Phil SutterThis shouldn't happen but relying upon external data without checking may lead to unexpected results.
--- misc/ss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +1 -1
@@ -2004,7 +2004,7 @@ static void tcp_timer_print(struct tcpstat *s)"unknown" }; -if (s->timer) { +if (s->timer >=0 ) {if (s->timer >4 ) s->timer =5 ; printf(" timer:(%s,%s,%d)",
Signature · 1 line
2.13.1
Re: [iproute PATCH v2 2/7] ss: Make sure index variable is >= 0
Quoted 13 lines · On Mon, 21 Aug 2017 19:08:08 +0200 Phil Sutter <[email protected]> wrote:
This shouldn't happen but relying upon external data without checking may lead to unexpected results.
Signed-off-by Phil Sutter <[email protected]>--- misc/ss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)misc/ss.c +0
-0
misc/ss.c static void tcp_timer_print(struct tcpstat *s) · line 2010
Quoted 8 lines
misc/ss.c +1
-1@@ -2004,7 +2004,7 @@ static void tcp_timer_print(struct tcpstat *s)"unknown" }; -if (s->timer) { +if (s->timer >=0 ) {if (s->timer >4 ) s->timer =5 ; printf(" timer:(%s,%s,%d)",
Let's go one step deeper on this.
Why is s->timer an int, it should be unsigned. In which case the code in print would not have to change.
PATCH v2 3/7 ss: Make sure scanned index value to unix_state_map is sane
In reply to Phil Sutter--- misc/ss.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
misc/ss.c +2 -1
@@ -3151,7 +3151,8 @@ static int unix_show(struct filter *f)if (flags & (1 <<16 )) { u->state =SS_LISTEN ; - }else { + }else if (u->state >0 && + u->state <=ARRAY_SIZE (unix_state_map)) { u->state = unix_state_map[u->state-1 ];if (u->type ==SOCK_DGRAM && u->state ==SS_CLOSE && u->rport) u->state =SS_ESTABLISHED ;
Signature · 1 line
2.13.1
PATCH v2 4/7 netem/maketable: Check return value of fscanf()
In reply to Phil Sutter--- netem/maketable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
netem/maketable.c +2 -2
@@ -38,8 +38,8 @@ readdoubles(FILE *fp, int *number) }for (i=0 ; i<limit; ++i){ -fscanf (fp,"%lf" , &x[i]); -if (feof (fp)) +if (fscanf (fp,"%lf" , &x[i]) !=1 || +feof (fp))break ; ++n; }
Signature · 1 line
2.13.1
PATCH v2 5/7 lib/bpf: Check return value of write()
In reply to Phil SutterThis is merely to silence the compiler warning. If write to stderr failed, assume that printing an error message will fail as well so don't even try.
--- lib/bpf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/bpf.c +2 -1
@@ -588,7 +588,8 @@ int bpf_trace_pipe(void) ret =read (fd, buff,sizeof (buff) -1 );if (ret >0 ) { -write (2 , buff, ret); +if (write (STDERR_FILENO , buff, ret) != ret) +return -1 ;fflush (stderr); } }
Signature · 1 line
2.13.1
PATCH v2 6/7 lib/fs: Fix and simplify make_path()
In reply to Phil SutterCalling stat() before mkdir() is racey: The entry might change in between. Also, the call to stat() seems to exist only to check if the directory exists already. So simply call mkdir() unconditionally and catch only errors other than EEXIST.
--- lib/fs.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-)
lib/fs.c +5 -15
@@ -102,7 +102,6 @@ out:int make_path (const char *path,mode_t mode) {char *dir, *delim; -struct stat sbuf;int rc =-1 ; delim = dir =strdup (path);
@@ -120,20 +119,11 @@ int make_path(const char *path, mode_t mode)if (delim) *delim ='\0' ; -if (stat (dir, &sbuf) !=0 ) { -if (errno !=ENOENT ) { -fprintf (stderr, -"stat failed for %s: %s\n" , - dir,strerror (errno)); - goto out; - } - -if (mkdir (dir, mode) !=0 ) { -fprintf (stderr, -"mkdir failed for %s: %s\n" , - dir,strerror (errno)); - goto out; - } + rc =mkdir (dir, mode); +if (mkdir (dir, mode) !=0 && errno !=EEXIST ) { +fprintf (stderr,"mkdir failed for %s: %s\n" , + dir,strerror (errno)); + goto out; }if (delim ==NULL )
Signature · 1 line
2.13.1
PATCH v2 7/7 lib/libnetlink: Don't pass NULL parameter to memcpy()
In reply to Phil SutterBoth addattr_l() and rta_addattr_l() may be called with NULL data pointer and 0 alen parameters. Avoid calling memcpy() in that case.
--- lib/libnetlink.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/libnetlink.c +4 -2
@@ -871,7 +871,8 @@ int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, rta =NLMSG_TAIL (n); rta->rta_type = type; rta->rta_len = len; -memcpy (RTA_DATA (rta), data, alen); +if (alen) +memcpy (RTA_DATA (rta), data, alen); n->nlmsg_len =NLMSG_ALIGN (n->nlmsg_len) +RTA_ALIGN (len);return 0 ; }
@@ -958,7 +959,8 @@ int rta_addattr_l(struct rtattr *rta, int maxlen, int type, subrta = (struct rtattr *)(((char *)rta) +RTA_ALIGN (rta->rta_len)); subrta->rta_type = type; subrta->rta_len = len; -memcpy (RTA_DATA (subrta), data, alen); +if (alen) +memcpy (RTA_DATA (subrta), data, alen); rta->rta_len =NLMSG_ALIGN (rta->rta_len) +RTA_ALIGN (len);return 0 ; }
Signature · 1 line
2.13.1