Various memory-safety related fixes
Hi,
here's a slew of fixes for issues discovered while working on adding a parser for pretty printed conntrack entries in snprintf_default.c format.
Most of these were found while debugging my libtheft property tests with ASan.
--Daniel
PS: Resending because of problems with unencoded 8bit garbage in mail header.
PATCH RESEND 1/8 Handle negative snprintf return values properly
Currently the BUFFER_SIZE macro doesn't take negative 'ret' values into account. A negative return should just be passed through to the caller, snprintf will already have set 'errno' properly.
--- include/internal/internal.h | 2 ++ src/conntrack/api.c | 6 +++--- src/conntrack/snprintf.c | 3 +++ src/expect/snprintf.c | 3 +++ 4 files changed, 11 insertions(+), 3 deletions(-)
include/internal/internal.h +2 -0
@@ -41,6 +41,8 @@#endif #define BUFFER_SIZE (ret, size, len, offset) \ + if(ret < 0) \ + return -1; \ size += ret; \ if (ret > len) \ ret = len; \
src/conntrack/api.c +3 -3
@@ -1099,9 +1099,9 @@ int nfct_catch(struct nfct_handle *h)* print the message just after you receive the destroy event. If you want * more accurate timestamping, use NFCT_OF_TIMESTAMP. * -* This function returns the size of the information that _would_ have been -* written to the buffer, even if there was no room for it. Thus, the -* behaviour is similar to snprintf. +* On error, -1 is returned and errno is set appropiately. Otherwise the +* size of what _would_ be written is returned, even if the size of the +* buffer is insufficient. This behaviour is similar to snprintf. */ int nfct_snprintf(char *buf, unsigned int size,
src/conntrack/snprintf.c +3 -0
@@ -85,6 +85,9 @@ int __snprintf_conntrack(char *buf,return -1 ; } +if (size <0 ) +return size; +/* NULL terminated string */ buf[size+1 > len ? len-1 : size] ='\0' ;
src/expect/snprintf.c +3 -0
@@ -30,6 +30,9 @@ int __snprintf_expect(char *buf,return -1 ; } +if (size <0 ) +return size; +/* NULL terminated string */ buf[size+1 > len ? len-1 : size] ='\0' ;
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerRe: [libnf_ct resend PATCH 1/8] Handle negative snprintf return values properly
Quoted 17 lines · Daniel Gröber
Currently the
BUFFER_SIZEmacro doesn't take negative 'ret' values into account. A negative return should just be passed through to the caller, snprintf will already have set 'errno' properly.Signed-off-by Daniel Gröber <[email protected]>--- include/internal/internal.h | 2 ++ src/conntrack/api.c | 6 +++--- src/conntrack/snprintf.c | 3 +++ src/expect/snprintf.c | 3 +++ 4 files changed, 11 insertions(+), 3 deletions(-)include/internal/internal.h +0
-0
include/internal/internal.h line 44
Quoted 4 lines
include/internal/internal.h +1
-0@@ -41,6 +41,8 @@#endif #define BUFFER_SIZE (ret, size, len, offset) \ + if(ret < 0) \
if (ret < 0)
^missing space
Quoted 26 lines
include/internal/internal.h +1
-0+return -1 ; \ size += ret; \if (ret > len) \ ret = len; \src/conntrack/api.c +3
-3@@ -1099,9 +1099,9 @@ int nfct_catch(struct nfct_handle *h)* print the message just after you receive the destroy event. If you want * more accurate timestamping, use NFCT_OF_TIMESTAMP. * -* This function returns the size of the information that _would_ have been -* written to the buffer, even if there was no room for it. Thus, the -* behaviour is similar to snprintf. +* On error, -1 is returned and errno is set appropiately. Otherwise the +* size of what _would_ be written is returned, even if the size of the +* buffer is insufficient. This behaviour is similar to snprintf. */ int nfct_snprintf(char *buf, unsigned int size,src/conntrack/snprintf.c +0
-0
src/conntrack/snprintf.c int __snprintf_conntrack(char *buf, · line 88
Quoted 4 lines
src/conntrack/snprintf.c +1
-0@@ -85,6 +85,9 @@ int __snprintf_conntrack(char *buf,return -1 ; } +if (size <0 )
^^^^^^^^
I can see spaces here as indentations.
Quoted 22 lines
src/conntrack/snprintf.c +2
-0+return size; +/* NULL terminated string */ buf[size+1 > len ? len-1 : size] ='\0' ;src/expect/snprintf.c +3
-0@@ -30,6 +30,9 @@ int __snprintf_expect(char *buf,return -1 ; } +if (size <0 ) +return size; +/* NULL terminated string */ buf[size+1 > len ? len-1 : size] ='\0' ;Signature · 1 line
2.20.1
PATCH RESEND 2/8 Fix nfexp_snprintf return value docs
In reply to Daniel GröberThe docs currently say "[...] Otherwise, 0 is returned." which is just completely wrong. Just like nfct_snprintf the expected buffer size is returned.
--- src/expect/api.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
src/expect/api.c +3 -2
@@ -795,8 +795,9 @@ int nfexp_catch(struct nfct_handle *h)* - NFEXP_O_LAYER: include layer 3 information in the output, this is * *only* required by NFEXP_O_DEFAULT. * -* On error, -1 is returned and errno is set appropiately. Otherwise, -* 0 is returned. +* On error, -1 is returned and errno is set appropiately. Otherwise the +* size of what _would_ be written is returned, even if the size of the +* buffer is insufficient. This behaviour is similar to snprintf. */ int nfexp_snprintf(char *buf, unsigned int size,
Signature · 1 line
2.20.1
PATCH RESEND 3/8 Replace strncpy with snprintf to improve null byte handling
In reply to Daniel GröberWe currently use strncpy in a bunch of places which has this weird quirk where it doesn't write a terminating null byte if the input string is >= the max length. To mitigate this we write a null byte to the last character manually.
While this works it is easy to forget. Instead we should just be using snprintf which has more sensible behaviour as it always writes a null byte even when truncating the string.
--- src/conntrack/copy.c | 4 ++-- src/conntrack/parse_mnl.c | 5 ++--- src/conntrack/setter.c | 3 +-- src/expect/parse_mnl.c | 16 ++++++++-------- src/expect/setter.c | 6 ++---- 5 files changed, 15 insertions(+), 19 deletions(-)
src/conntrack/copy.c +2 -2
@@ -427,8 +427,8 @@ static void copy_attr_repl_off_aft(struct nf_conntrack *dest,static void copy_attr_helper_name (struct nf_conntrack *dest,const struct nf_conntrack *orig) { -strncpy (dest->helper_name, orig->helper_name,NFCT_HELPER_NAME_MAX ); - dest->helper_name[NFCT_HELPER_NAME_MAX -1 ] ='\0' ; +snprintf (dest->helper_name,NFCT_HELPER_NAME_MAX ,"%s" , + orig->helper_name); } static void copy_attr_zone(struct nf_conntrack *dest,
src/conntrack/parse_mnl.c +2 -3
@@ -690,9 +690,8 @@ nfct_parse_helper(const struct nlattr *attr, struct nf_conntrack *ct)if (!tb[CTA_HELP_NAME ])return 0 ; -strncpy (ct->helper_name,mnl_attr_get_str (tb[CTA_HELP_NAME ]), -NFCT_HELPER_NAME_MAX ); - ct->helper_name[NFCT_HELPER_NAME_MAX -1 ] ='\0' ; +snprintf (ct->helper_name,NFCT_HELPER_NAME_MAX ,"%s" , +mnl_attr_get_str (tb[CTA_HELP_NAME ]));set_bit (ATTR_HELPER_NAME , ct->head.set);if (!tb[CTA_HELP_INFO ])
src/conntrack/setter.c +1 -2
@@ -389,8 +389,7 @@ set_attr_repl_off_aft(struct nf_conntrack *ct, const void *value, size_t len)static void set_attr_helper_name (struct nf_conntrack *ct,const void *value,size_t len) { -strncpy (ct->helper_name, value,NFCT_HELPER_NAME_MAX ); - ct->helper_name[NFCT_HELPER_NAME_MAX -1 ] ='\0' ; +snprintf (ct->helper_name,NFCT_HELPER_NAME_MAX ,"%s" , (char *)value); } static void
src/expect/parse_mnl.c +8 -8
@@ -10,6 +10,7 @@*/ #include "internal/internal.h" +#include <assert.h> #include <libmnl/libmnl.h> static int nlmsg_parse_expection_attr_cb (const struct nlattr *attr,void *data)
@@ -139,11 +140,9 @@ int nfexp_nlmsg_parse(const struct nlmsghdr *nlh, struct nf_expect *exp)set_bit (ATTR_EXP_FLAGS , exp->set); }if (tb[CTA_EXPECT_HELP_NAME ]) { -strncpy (exp->helper_name, -mnl_attr_get_str (tb[CTA_EXPECT_HELP_NAME ]), -NFCT_HELPER_NAME_MAX ); - exp->helper_name[NFCT_HELPER_NAME_MAX -1 ] ='\0' ; -set_bit (ATTR_EXP_HELPER_NAME , exp->set); +snprintf (exp->helper_name,NFCT_HELPER_NAME_MAX ,"%s" , +mnl_attr_get_str (tb[CTA_EXPECT_HELP_NAME ])); +set_bit (ATTR_EXP_HELPER_NAME , exp->set); }if (tb[CTA_EXPECT_CLASS ]) { exp->class =ntohl (mnl_attr_get_u32 (tb[CTA_EXPECT_CLASS ]));
@@ -153,9 +152,10 @@ int nfexp_nlmsg_parse(const struct nlmsghdr *nlh, struct nf_expect *exp)nfexp_nlmsg_parse_nat (nfg, tb[CTA_EXPECT_NAT ], exp);if (tb[CTA_EXPECT_FN ]) { -strncpy (exp->expectfn,mnl_attr_get_payload (tb[CTA_EXPECT_FN ]), - __NFCT_EXPECTFN_MAX); - exp->expectfn[__NFCT_EXPECTFN_MAX -1 ] ='\0' ; +int len =mnl_attr_get_payload_len (tb[CTA_EXPECT_FN ]); +assert (len <= __NFCT_EXPECTFN_MAX);/* the kernel doesn't impose a max lenght on this str */ +snprintf (exp->expectfn, __NFCT_EXPECTFN_MAX,"%s" , + (char *)mnl_attr_get_payload (tb[CTA_EXPECT_FN ]));set_bit (ATTR_EXP_FN , exp->set); }
src/expect/setter.c +2 -4
@@ -46,8 +46,7 @@ static void set_exp_attr_class(struct nf_expect *exp, const void *value)static void set_exp_attr_helper_name (struct nf_expect *exp,const void *value) { -strncpy (exp->helper_name, value,NFCT_HELPER_NAME_MAX ); - exp->helper_name[NFCT_HELPER_NAME_MAX -1 ] ='\0' ; +snprintf (exp->helper_name,NFCT_HELPER_NAME_MAX ,"%s" , (char *)value); }static void set_exp_attr_nat_dir (struct nf_expect *exp,const void *value)
@@ -62,8 +61,7 @@ static void set_exp_attr_nat_tuple(struct nf_expect *exp, const void *value)static void set_exp_attr_expectfn (struct nf_expect *exp,const void *value) { -strncpy (exp->expectfn, value, __NFCT_EXPECTFN_MAX); - exp->expectfn[__NFCT_EXPECTFN_MAX-1 ] ='\0' ; +snprintf (exp->expectfn, __NFCT_EXPECTFN_MAX,"%s" , (char *)value); }const set_exp_attr set_exp_attr_array[ATTR_EXP_MAX ] = {
Signature · 1 line
2.20.1
PATCH RESEND 4/8 Fix incorrect snprintf size calculation
In reply to Daniel GröberThe previous BUFFER_SIZE() call already updated the remaining 'len'. So there is no need to subtract 'size' again. While this just makes the buffer appear smaller than it is, which is mostly harmless, the subtraction might underflow as 'size > len' is not checked like BUFFER_SIZE() does.
--- src/conntrack/snprintf_default.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/conntrack/snprintf_default.c +1 -1
@@ -108,7 +108,7 @@ static int __snprintf_address_ipv6(char *buf,if (!inet_ntop (AF_INET6 , &dst, tmp,sizeof (tmp)))return -1 ; - ret =snprintf (buf+offset, len-size,"%s=%s " , dst_tag, tmp); + ret =snprintf (buf+offset, len,"%s=%s " , dst_tag, tmp);BUFFER_SIZE (ret, size, len, offset);return size;
Signature · 1 line
2.20.1
PATCH RESEND 5/8 Add asizeof() macro
In reply to Daniel Gröber--- include/internal/internal.h | 2 ++ 1 file changed, 2 insertions(+)
include/internal/internal.h +2 -0
@@ -40,6 +40,8 @@#define IPPROTO_DCCP 33#endif +#define asizeof (x) (sizeof(x) / sizeof(*x)) +#define BUFFER_SIZE (ret, size, len, offset) \ if(ret < 0) \ return -1; \
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerRe: [libnf_ct resend PATCH 5/8] Add asizeof() macro
Quoted 10 lines · Daniel Gröber
Signed-off-by Daniel Gröber <[email protected]>--- include/internal/internal.h | 2 ++ 1 file changed, 2 insertions(+)include/internal/internal.h +0
-0
include/internal/internal.h line 43
Quoted 4 lines
include/internal/internal.h +1
-0@@ -40,6 +40,8 @@#define IPPROTO_DCCP 33#endif +#define asizeof (x) (sizeof(x) / sizeof(*x))
Please call this ARRAY_SIZE.
Thanks.
PATCH RESEND 6/8 Fix buffer overflow on invalid icmp type in setters
In reply to Daniel GröberWhen type is out of range for the invmap_icmp{,v6} array we leave rtype at zero which will map to type=255 just like other error cases in this function.
--- src/conntrack/grp_setter.c | 8 +++++--- src/conntrack/setter.c | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-)
src/conntrack/grp_setter.c +5 -3
@@ -85,18 +85,20 @@ static void set_attr_grp_repl_port(struct nf_conntrack *ct, const void *value)static void set_attr_grp_icmp (struct nf_conntrack *ct,const void *value) { -uint8_t rtype; +uint8_t rtype =0 ;const struct nfct_attr_grp_icmp *this = value; ct->head.orig.l4dst.icmp.type = this->type;switch (ct->head.orig.l3protonum) {case AF_INET : - rtype = invmap_icmp[this->type]; +if (this->type <asizeof (invmap_icmp)) + rtype = invmap_icmp[this->type];break ;case AF_INET6 : - rtype = invmap_icmpv6[this->type -128 ]; +if (this->type -128 <asizeof (invmap_icmp)) + rtype = invmap_icmpv6[this->type -128 ];break ;default :
src/conntrack/setter.c +7 -4
@@ -124,17 +124,20 @@ set_attr_repl_zone(struct nf_conntrack *ct, const void *value, size_t len)static void set_attr_icmp_type (struct nf_conntrack *ct,const void *value,size_t len) { -uint8_t rtype; +uint8_t type = *((uint8_t *) value); +uint8_t rtype =0 ; - ct->head.orig.l4dst.icmp.type = *((uint8_t *) value); + ct->head.orig.l4dst.icmp.type = type;switch (ct->head.orig.l3protonum) {case AF_INET : - rtype = invmap_icmp[*((uint8_t *) value)]; +if (type <asizeof (invmap_icmp)) + rtype = invmap_icmp[type];break ;case AF_INET6 : - rtype = invmap_icmpv6[*((uint8_t *) value) -128 ]; +if (type -128 <asizeof (invmap_icmpv6)) + rtype = invmap_icmpv6[type -128 ];break ;default :
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerRe: [libnf_ct resend PATCH 6/8] Fix buffer overflow on invalid icmp type in setters
Quoted 19 lines · Daniel Gröber
When type is out of range for the invmap_icmp{,v6} array we leave rtype at zero which will map to type=255 just like other error cases in this function.
Signed-off-by Daniel Gröber <[email protected]>--- src/conntrack/grp_setter.c | 8 +++++--- src/conntrack/setter.c | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-)src/conntrack/grp_setter.c +0
-1@@ -85,18 +85,20 @@ static void set_attr_grp_repl_port(struct nf_conntrack *ct, const void *value)static void set_attr_grp_icmp (struct nf_conntrack *ct,const void *value) { -uint8_t rtype;
src/conntrack/grp_setter.c static void set_attr_grp_repl_port(struct nf_conntrack *ct, const void *value) · line 96
Quoted 10 lines
src/conntrack/grp_setter.c +3
-1+uint8_t rtype =0 ;const struct nfct_attr_grp_icmp *this = value; ct->head.orig.l4dst.icmp.type = this->type;switch (ct->head.orig.l3protonum) {case AF_INET : - rtype = invmap_icmp[this->type]; +if (this->type <asizeof (invmap_icmp)) + rtype = invmap_icmp[this->type];
Probably add a small helper function for this?
static inline uint8_t icmp_invmap(uint8_t type)
{
assert(this->type < ARRAY_SIZE(invmap_icmp));
return invmap_icmp[this->type];
}Quoted 10 lines
src/conntrack/setter.c +1
-1@@ -124,17 +124,20 @@ set_attr_repl_zone(struct nf_conntrack *ct, const void *value, size_t len)static void set_attr_icmp_type (struct nf_conntrack *ct,const void *value,size_t len) { -uint8_t rtype; +uint8_t type = *((uint8_t *) value);
src/conntrack/setter.c set_attr_repl_zone(struct nf_conntrack *ct, const void *value, size_t len) · line 135
Quoted 10 lines
src/conntrack/setter.c +4
-2+uint8_t rtype =0 ; - ct->head.orig.l4dst.icmp.type = *((uint8_t *) value); + ct->head.orig.l4dst.icmp.type = type;switch (ct->head.orig.l3protonum) {case AF_INET : - rtype = invmap_icmp[*((uint8_t *) value)]; +if (type <asizeof (invmap_icmp)) + rtype = invmap_icmp[type];
Make sure indentation tab is 8-char instead of spaces.
Thanks.
PATCH RESEND 7/8 Fix buffer overflow in protocol related snprintf functions
In reply to Daniel Gröber--- src/conntrack/snprintf_default.c | 12 ++++-------- src/conntrack/snprintf_xml.c | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 10 deletions(-)
src/conntrack/snprintf_default.c +4 -8
@@ -13,20 +13,16 @@ static int __snprintf_l3protocol(char *buf,unsigned int len, const struct nf_conntrack *ct) { - return (snprintf(buf, len, "%-8s %u ", - l3proto2str[ct->head.orig.l3protonum] == NULL ? - "unknown" : l3proto2str[ct->head.orig.l3protonum], - ct->head.orig.l3protonum)); + uint8_t num = ct->head.orig.l3protonum; +return snprintf (buf, len,"%-8s %u " ,__l3proto2str (num), num); }int __snprintf_protocol (char *buf,unsigned int len,const struct nf_conntrack *ct) { -return (snprintf (buf, len,"%-8s %u " , - proto2str[ct->head.orig.protonum] ==NULL ? -"unknown" : proto2str[ct->head.orig.protonum], - ct->head.orig.protonum)); +uint8_t num = ct->head.orig.protonum; +return snprintf (buf, len,"%-8s %u " ,__proto2str (num), num); } static int __snprintf_timeout(char *buf,
src/conntrack/snprintf_xml.c +18 -2
@@ -55,12 +55,28 @@const char *__proto2str (uint8_t protonum) { -return proto2str[protonum] ? proto2str[protonum] :"unknown" ; +const char *str =NULL ; + +if (protonum <asizeof (proto2str)) + str = proto2str[protonum]; + +if (str ==NULL ) + str ="unknown" ; + +return str; }const char *__l3proto2str (uint8_t protonum) { -return l3proto2str[protonum] ? l3proto2str[protonum] :"unknown" ; +const char *str =NULL ; + +if (protonum <asizeof (l3proto2str)) + str = l3proto2str[protonum]; + +if (str ==NULL ) + str ="unknown" ; + +return str; } static int __snprintf_ipv4_xml(char *buf,
Signature · 1 line
2.20.1
PATCH RESEND 8/8 Fix buffer overflows in __snprintf_protoinfo* like in *2str fns
In reply to Daniel Gröber--- src/conntrack/snprintf_default.c | 42 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-)
src/conntrack/snprintf_default.c +30 -12
@@ -36,30 +36,48 @@ static int __snprintf_protoinfo(char *buf,unsigned int len, const struct nf_conntrack *ct) { - return snprintf(buf, len, "%s ", - ct->protoinfo.tcp.state < TCP_CONNTRACK_MAX ? - states[ct->protoinfo.tcp.state] : - states[TCP_CONNTRACK_NONE ]); + const char *str = NULL; +uint8_t state = ct->protoinfo.tcp.state; + +if (state <asizeof (states)) + str = states[state]; + +if (str ==NULL ) + str = states[TCP_CONNTRACK_NONE ]; + +return snprintf (buf, len,"%s " , str); }static int __snprintf_protoinfo_sctp (char *buf,unsigned int len,const struct nf_conntrack *ct) { -return snprintf (buf, len,"%s " , - ct->protoinfo.sctp.state <SCTP_CONNTRACK_MAX ? - sctp_states[ct->protoinfo.sctp.state] : - sctp_states[SCTP_CONNTRACK_NONE ]); +const char *str =NULL ; +uint8_t state = ct->protoinfo.sctp.state; + +if (state <asizeof (sctp_states)) + str = sctp_states[state]; + +if (str ==NULL ) + str = sctp_states[SCTP_CONNTRACK_NONE ]; + +return snprintf (buf, len,"%s " , str); }static int __snprintf_protoinfo_dccp (char *buf,unsigned int len,const struct nf_conntrack *ct) { -return snprintf (buf, len,"%s " , - ct->protoinfo.dccp.state <DCCP_CONNTRACK_MAX ? - sctp_states[ct->protoinfo.dccp.state] : - sctp_states[DCCP_CONNTRACK_NONE ]); +const char *str =NULL ; +uint8_t state = ct->protoinfo.dccp.state; + +if (state <asizeof (dccp_states)) + str = dccp_states[state]; + +if (str ==NULL ) + str = dccp_states[SCTP_CONNTRACK_NONE ]; + +return snprintf (buf, len,"%s " , str); } static int __snprintf_address_ipv4(char *buf,
Signature · 1 line
2.20.1