Covscan: Fix potential NULL pointer dereferences
Subsystems (2)
PATCH v2 1/5 ifstat, nstat: Check fdopen() return value
In reply to Phil SutterPrevent passing NULL FILE pointer to fgets() later.
Fix both tools in a single patch since the code changes are basically identical.
--- misc/ifstat.c | 16 +++++++++++----- misc/nstat.c | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-)
misc/ifstat.c +11 -5
@@ -992,12 +992,18 @@ int main(int argc, char *argv[]) && verify_forging(fd) == 0) {FILE *sfp =fdopen (fd,"r" ); -load_raw_table (sfp); -if (hist_db && source_mismatch) { -fprintf (stderr,"ifstat: history is stale, ignoring it.\n" ); - hist_db =NULL ; +if (!sfp) { +fprintf (stderr,"ifstat: fdopen failed: %s\n" , +strerror (errno)); +close (fd); + }else { +load_raw_table (sfp); +if (hist_db && source_mismatch) { +fprintf (stderr,"ifstat: history is stale, ignoring it.\n" ); + hist_db =NULL ; + } +fclose (sfp); } -fclose (sfp); } else {if (fd >=0 )close (fd);
misc/nstat.c +11 -5
@@ -706,12 +706,18 @@ int main(int argc, char *argv[]) && verify_forging(fd) == 0) {FILE *sfp =fdopen (fd,"r" ); -load_good_table (sfp); -if (hist_db && source_mismatch) { -fprintf (stderr,"nstat: history is stale, ignoring it.\n" ); - hist_db =NULL ; +if (!sfp) { +fprintf (stderr,"nstat: fdopen failed: %s\n" , +strerror (errno)); +close (fd); + }else { +load_good_table (sfp); +if (hist_db && source_mismatch) { +fprintf (stderr,"nstat: history is stale, ignoring it.\n" ); + hist_db =NULL ; + } +fclose (sfp); } -fclose (sfp); } else {if (fd >=0 )close (fd);
Signature · 1 line
2.13.1
PATCH v2 5/5 tipc/bearer: Prevent NULL pointer dereference
In reply to Phil Sutter--- tipc/bearer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tipc/bearer.c +2 -2
@@ -438,8 +438,8 @@ static int cmd_bearer_enable(struct nlmsghdr *nlh, const struct cmd *cmd,if (err)return err; - opt =get_opt (opts,"media" ); -if (strcmp (opt->val,"udp" ) ==0 ) { +if ((opt =get_opt (opts,"media" )) && +strcmp (opt->val,"udp" ) ==0 ) { err =nl_add_udp_enable_opts (nlh, opts, cmdl);if (err)return err;
Signature · 1 line
2.13.1
PATCH v2 3/5 tc/q_netem: Don't dereference possibly NULL pointer
In reply to Phil SutterAssuming 'opt' might be NULL, move the call to RTA_PAYLOAD to after the check since it dereferences its parameter.
--- tc/q_netem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tc/q_netem.c +3 -1
@@ -538,7 +538,7 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)int *ecn =NULL ;struct tc_netem_qopt qopt;const struct tc_netem_rate *rate =NULL ; -int len =RTA_PAYLOAD (opt) -sizeof (qopt); +int len;__u64 rate64 =0 ;SPRINT_BUF (b1);
@@ -546,6 +546,8 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)if (opt ==NULL )return 0 ; + len =RTA_PAYLOAD (opt) -sizeof (qopt); +if (len <0 ) {fprintf (stderr,"options size error\n" );return -1 ;
Signature · 1 line
2.13.1
PATCH v2 4/5 tc/tc_filter: Make sure filter name is not empty
In reply to Phil SutterThe later check for 'k[0] != 0' requires a non-empty filter name, otherwise NULL pointer dereference in 'q' might happen.
--- tc/tc_filter.c | 3 +++ 1 file changed, 3 insertions(+)
tc/tc_filter.c +3 -0
@@ -412,6 +412,9 @@ static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)usage ();return 0 ; }else { +if (!strlen (*argv)) +invarg ("invalid filter name" , *argv); +strncpy (k, *argv,sizeof (k)-1 ); q =get_filter_kind (k);
Signature · 1 line
2.13.1
PATCH v2 2/5 nstat: Fix for potential NULL pointer dereference
In reply to Phil SutterIf the string at 'p' contains neither space not newline, 'p' will become NULL. Make sure this isn't the case before dereferencing it.
--- misc/nstat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
misc/nstat.c +1 -1
@@ -198,7 +198,7 @@ static void load_ugly_table(FILE *fp) off = p - buf; p +=2 ; -while (*p) { +while (p && *p) {char *next;if ((next =strchr (p,' ' )) !=NULL )
Signature · 1 line
2.13.1
v2 cover Covscan: Fix potential NULL pointer dereferences
This series collects patches from v1 which eliminate possible cases of NULL pointer dereferences.
No changes to the actual patches, just splitting into smaller series.
Phil Sutter (5):
ifstat, nstat: Check fdopen() return value
nstat: Fix for potential NULL pointer dereference tc/q_netem: Don't dereference possibly NULL pointer tc/tc_filter: Make sure filter name is not empty tipc/bearer: Prevent NULL pointer dereference
misc/ifstat.c | 16 +++++++++++----- misc/nstat.c | 18 ++++++++++++------ tc/q_netem.c | 4 +++- tc/tc_filter.c | 3 +++ tipc/bearer.c | 4 ++-- 5 files changed, 31 insertions(+), 14 deletions(-)
Signature · 1 line
2.13.1
RE: [iproute PATCH v2 4/5] tc/tc_filter: Make sure filter name is not empty
In reply to Phil SutterFrom: Phil Sutter
Quoted 14 lines
Sent: 17 August 2017 18:10
The later check for 'k[0] != 0' requires a non-empty filter name, otherwise NULL pointer dereference in 'q' might happen.Signed-off-by Phil Sutter <[email protected]>--- tc/tc_filter.c | 3 +++ 1 file changed, 3 insertions(+)tc/tc_filter.c +0
-0
tc/tc_filter.c static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv) · line 416
Quoted 5 lines
tc/tc_filter.c +2
-0@@ -412,6 +412,9 @@ static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)usage ();return 0 ; }else { +if (!strlen (*argv)) +invarg ("invalid filter name" , *argv);
That is nearly as bad as:
p[strlen(p)] = 0;
Quoted 6 lines
tc/tc_filter.c +1
-1+strncpy (k, *argv,sizeof (k)-1 ); q =get_filter_kind (k); --2.13.1
Re: [iproute PATCH v2 4/5] tc/tc_filter: Make sure filter name is not empty
Quoted 22 lines · David Laight
From: Phil Sutter
Quoted 14 lines
Sent: 17 August 2017 18:10
The later check for 'k[0] != 0' requires a non-empty filter name, otherwise NULL pointer dereference in 'q' might happen.Signed-off-by Phil Sutter <[email protected]>--- tc/tc_filter.c | 3 +++ 1 file changed, 3 insertions(+)tc/tc_filter.c +0
-0tc/tc_filter.c static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv) · line 416
Quoted 5 lines
tc/tc_filter.c +2
-0@@ -412,6 +412,9 @@ static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)usage ();return 0 ; }else { +if (!strlen (*argv)) +invarg ("invalid filter name" , *argv);That is nearly as bad as:
Quoted 1 line
p[strlen(p)] = 0;
Hey, it's not impossible! I could call tc like so:
| # tc filter get protocol ip prio 1 ""
What's funny about it is that the first call to matches() in tc_filter_get() will catch the empty last parameter:
| # tc filter get prio 1 protocol ip "" | Command line is not complete. Try option "help" | # ./tc/tc filter get prio 1 protocol ip "" "" | Error: duplicate "priority": "" is the second value.
Cheers, Phil
Re: [iproute PATCH v2 5/5] tipc/bearer: Prevent NULL pointer dereference
In reply to Phil Suttertipc/bearer.c static int cmd_bearer_enable(struct nlmsghdr *nlh, const struct cmd *cmd, · line 442
Quoted 4 lines · On Thu, 17 Aug 2017 19:09:31 +0200 Phil Sutter <[email protected]> wrote:
tipc/bearer.c +2
-2- opt =get_opt (opts,"media" ); -if (strcmp (opt->val,"udp" ) ==0 ) { +if ((opt =get_opt (opts,"media" )) && +strcmp (opt->val,"udp" ) ==0 ) {
Please don't merge assignment and comparison unless necessary for other reasons.
opt = get_opt(opts, "media");
if (opt && strcmp(opt->val, "udp") == 0) {Re: [iproute PATCH v2 3/5] tc/q_netem: Don't dereference possibly NULL pointer
In reply to Phil Suttertc/q_netem.c static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) · line 551
Quoted 7 lines · On Thu, 17 Aug 2017 19:09:29 +0200 Phil Sutter <[email protected]> wrote:
tc/q_netem.c +2
-0@@ -546,6 +546,8 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)if (opt ==NULL )return 0 ; + len =RTA_PAYLOAD (opt) -sizeof (qopt); +if (len <0 ) {
Dont add blank line between computation and conditional. Having them together reads better.
Re: [iproute PATCH v2 2/5] nstat: Fix for potential NULL pointer dereference
In reply to Phil SutterQuoted 4 lines · On Thu, 17 Aug 2017 19:09:28 +0200 Phil Sutter <[email protected]> wrote:
If the string at 'p' contains neither space not newline, 'p' will become NULL. Make sure this isn't the case before dereferencing it.
Signed-off-by Phil Sutter <[email protected]>
Why not fix the parsing code instead. Other places here call abort().
RE: [iproute PATCH v2 4/5] tc/tc_filter: Make sure filter name is not empty
In reply to Phil SutterFrom: Phil Sutter
Quoted 26 lines
Sent: 18 August 2017 12:16
Quoted 22 lines · David Laight
From: Phil Sutter
Quoted 14 lines
Sent: 17 August 2017 18:10
The later check for 'k[0] != 0' requires a non-empty filter name, otherwise NULL pointer dereference in 'q' might happen.Signed-off-by Phil Sutter <[email protected]>--- tc/tc_filter.c | 3 +++ 1 file changed, 3 insertions(+)tc/tc_filter.c +0
-0tc/tc_filter.c static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv) · line 416
Quoted 5 lines
tc/tc_filter.c +2
-0@@ -412,6 +412,9 @@ static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)usage ();return 0 ; }else { +if (!strlen (*argv)) +invarg ("invalid filter name" , *argv);That is nearly as bad as:
Quoted 1 line
p[strlen(p)] = 0;
Quoted 3 lines
Hey, it's not impossible! I could call tc like so:
| # tc filter get protocol ip prio 1 ""
You missed the point. Just check **argv there is no need to determine the length just to check it is non-zero.
David
Re: [iproute PATCH v2 4/5] tc/tc_filter: Make sure filter name is not empty
Quoted 27 lines · David Laight
From: Phil Sutter
Quoted 26 lines
Sent: 18 August 2017 12:16
Quoted 23 lines · David Laight
From: Phil Sutter
Quoted 14 lines
Sent: 17 August 2017 18:10
The later check for 'k[0] != 0' requires a non-empty filter name, otherwise NULL pointer dereference in 'q' might happen.Signed-off-by Phil Sutter <[email protected]>--- tc/tc_filter.c | 3 +++ 1 file changed, 3 insertions(+)tc/tc_filter.c +0
-0tc/tc_filter.c static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv) · line 416
Quoted 5 lines
tc/tc_filter.c +2
-0@@ -412,6 +412,9 @@ static int tc_filter_get(int cmd, unsigned int flags, int argc, char **argv)usage ();return 0 ; }else { +if (!strlen (*argv)) +invarg ("invalid filter name" , *argv);That is nearly as bad as:
p[strlen(p)] = 0;
Quoted 6 lines
Quoted 3 lines
Hey, it's not impossible! I could call tc like so:
| # tc filter get protocol ip prio 1 ""
You missed the point. Just check **argv there is no need to determine the length just to check it is non-zero.
Oh, hehe. Thanks for the short-cut!
Thanks, PHil