A series of covscan-indicated fixes
Phil Sutter
reviewercover A series of covscan-indicated fixes
I had to dig through a huge coverity tool report, these are the things I found worth fixing.
Phil Sutter (6):
xtables-restore: Avoid access of uninitialized data
extensions: time: Avoid undefined shift
extensions: cluster: Avoid undefined shift
libxtables: Avoid buffer overrun in xtables_compatible_revision()
xtables-translate: Guard strcpy() call in xlate_ifname()
extensions: among: Check call to fstat()
extensions/libebt_among.c | 6 +++++- extensions/libxt_cluster.c | 2 +- extensions/libxt_time.c | 2 +- iptables/xtables-restore.c | 2 +- iptables/xtables-translate.c | 5 ++--- libxtables/xtables.c | 3 ++- 6 files changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 1/6 xtables-restore: Avoid access of uninitialized data
When flushing, 'buffer' is not written to prior to checking its first byte's value. Therefore it needs to be initialized upon declaration.
--- iptables/xtables-restore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/xtables-restore.c +1 -1
@@ -281,7 +281,7 @@ void xtables_restore_parse(struct nft_handle *h, const struct nft_xt_restore_parse *p) {struct nft_xt_restore_state state = {}; -char preload_buffer[PREBUFSIZ ] = {}, buffer[10240 ], *ptr; +char preload_buffer[PREBUFSIZ ] = {}, buffer[10240 ] = {}, *ptr;if (!h->noflush) {nft_fake_cache (h);
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 2/6 extensions: time: Avoid undefined shift
In reply to Phil SutterValue 1 is signed 32-bit by default and left-shifting that by 31 is undefined. Fix this by marking the value as unsigned.
--- extensions/libxt_time.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
extensions/libxt_time.c +1 -1
@@ -330,7 +330,7 @@ static void time_print_monthdays(uint32_t mask, bool human_readable)printf (" " );for (i =1 ; i <=31 ; ++i) -if (mask & (1 << i)) { +if (mask & (1u << i)) {if (nbdays++ >0 )printf ("," );printf ("%u" , i);
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 3/6 extensions: cluster: Avoid undefined shift
In reply to Phil SutterValue 1 is signed 32-bit by default and left-shifting that by 31 is undefined. Fix this by marking the value as unsigned.
--- extensions/libxt_cluster.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
extensions/libxt_cluster.c +1 -1
@@ -156,7 +156,7 @@ static int cluster_xlate(struct xt_xlate *xl,xt_xlate_add (xl,"%s %u seed 0x%08x " , jhash_st, info->total_nodes, info->hash_seed);for (node =0 ; node <32 ; node++) { -if (info->node_mask & (1 << node)) { +if (info->node_mask & (1u << node)) {if (needs_set ==0 ) {xt_xlate_add (xl,"{ " ); needs_set =1 ;
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 4/6 libxtables: Avoid buffer overrun in xtables_compatible_revision()
In reply to Phil SutterThe function is exported and accepts arbitrary strings as input. Calling strcpy() without length checks is not OK.
--- libxtables/xtables.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
libxtables/xtables.c +2 -1
@@ -856,7 +856,8 @@ int xtables_compatible_revision(const char *name, uint8_t revision, int opt)xtables_load_ko (xtables_modprobe_program, true); -strcpy (rev.name, name); +strncpy (rev.name, name,XT_EXTENSION_MAXNAMELEN -1 ); + rev.name[XT_EXTENSION_MAXNAMELEN -1 ] ='\0' ; rev.revision = revision; max_rev =getsockopt (sockfd, afinfo->ipproto, opt, &rev, &s);
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 5/6 xtables-translate: Guard strcpy() call in xlate_ifname()
In reply to Phil SutterThe function potentially fed overlong strings to strcpy(). Given that everything needed to avoid this is there, reorder code a bit to prevent those inputs, too.
--- iptables/xtables-translate.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/xtables-translate.c +2 -3
@@ -32,14 +32,13 @@void xlate_ifname (struct xt_xlate *xl,const char *nftmeta,const char *ifname,bool invert) { +int ifaclen =strlen (ifname);char iface[IFNAMSIZ ]; -int ifaclen; -if (ifname[0 ] =='\0' ) +if (ifaclen <1 || ifaclen >=IFNAMSIZ )return ;strcpy (iface, ifname); - ifaclen =strlen (iface);if (iface[ifaclen -1 ] =='+' ) iface[ifaclen -1 ] ='*' ;
Signature · 1 line
2.24.0
Phil Sutter
reviewerPATCH 6/6 extensions: among: Check call to fstat()
In reply to Phil SutterIf this fails, a bogus length value may be passed to mmap().
--- extensions/libebt_among.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
extensions/libebt_among.c +5 -1
@@ -6,6 +6,7 @@* August, 2003 */ +#include <errno.h> #include <ctype.h> #include <fcntl.h> #include <getopt.h>
@@ -137,7 +138,10 @@ static int bramong_parse(int c, char **argv, int invert,if ((fd =open (optarg,O_RDONLY )) ==-1 )xtables_error (PARAMETER_PROBLEM ,"Couldn't open file '%s'" , optarg); -fstat (fd, &stats); +if (fstat (fd, &stats) <0 ) +xtables_error (PARAMETER_PROBLEM , +"fstat(%s) failed: '%s'" , + optarg,strerror (errno)); flen = stats.st_size;/* use mmap because the file will probably be big */ optarg = mmap(0, flen, PROT_READ | PROT_WRITE,
Signature · 1 line
2.24.0
Quoted 2 lines · Phil Sutter
I had to dig through a huge coverity tool report, these are the things I found worth fixing.
Thanks Phil.