Avoid out of bound reads in tests.
PATCH 1/2 Avoid out of bound reads in tests.
Our string isn't NUL-terminated. To avoid reading past the last character, use strndup.
--- tests/nft-expr_match-test.c | 2 +- tests/nft-expr_target-test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tests/nft-expr_match-test.c +1 -1
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])nftnl_expr_set_str (ex,NFTNL_EXPR_MT_NAME ,"Tests" );nftnl_expr_set_u32 (ex,NFTNL_EXPR_MT_REV ,0x12345678 ); -nftnl_expr_set (ex,NFTNL_EXPR_MT_INFO ,strdup (data),sizeof (data)); +nftnl_expr_set (ex,NFTNL_EXPR_MT_INFO ,strndup (data,sizeof (data)),sizeof (data));nftnl_rule_add_expr (a, ex); nlh =nftnl_rule_nlmsg_build_hdr (buf,NFT_MSG_NEWRULE ,AF_INET ,0 ,1234 );
tests/nft-expr_target-test.c +1 -1
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])nftnl_expr_set (ex,NFTNL_EXPR_TG_NAME ,"test" ,strlen ("test" ));nftnl_expr_set_u32 (ex,NFTNL_EXPR_TG_REV ,0x56781234 ); -nftnl_expr_set (ex,NFTNL_EXPR_TG_INFO ,strdup (data),sizeof (data)); +nftnl_expr_set (ex,NFTNL_EXPR_TG_INFO ,strndup (data,sizeof (data)),sizeof (data));nftnl_rule_add_expr (a, ex); nlh =nftnl_rule_nlmsg_build_hdr (buf,NFT_MSG_NEWRULE ,AF_INET ,0 ,1234 );
Signature · 1 line
2.29.2
Pablo Neira Ayuso
maintainerHi Maya,
Quoted 2 lines · Maya Rashish
Our string isn't NUL-terminated. To avoid reading past the last character, use strndup.
Is this a theoretical problem or some static analisys tool is reporting out-of-bound memread?
Quoted 35 lines
Signed-off-by Maya Rashish <[email protected]>--- tests/nft-expr_match-test.c | 2 +- tests/nft-expr_target-test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)tests/nft-expr_match-test.c +1
-1@@ -71,7 +71,7 @@ int main(int argc, char *argv[])nftnl_expr_set_str (ex,NFTNL_EXPR_MT_NAME ,"Tests" );nftnl_expr_set_u32 (ex,NFTNL_EXPR_MT_REV ,0x12345678 ); -nftnl_expr_set (ex,NFTNL_EXPR_MT_INFO ,strdup (data),sizeof (data)); +nftnl_expr_set (ex,NFTNL_EXPR_MT_INFO ,strndup (data,sizeof (data)),sizeof (data));nftnl_rule_add_expr (a, ex); nlh =nftnl_rule_nlmsg_build_hdr (buf,NFT_MSG_NEWRULE ,AF_INET ,0 ,1234 );tests/nft-expr_target-test.c +1
-1@@ -71,7 +71,7 @@ int main(int argc, char *argv[])nftnl_expr_set (ex,NFTNL_EXPR_TG_NAME ,"test" ,strlen ("test" ));nftnl_expr_set_u32 (ex,NFTNL_EXPR_TG_REV ,0x56781234 ); -nftnl_expr_set (ex,NFTNL_EXPR_TG_INFO ,strdup (data),sizeof (data)); +nftnl_expr_set (ex,NFTNL_EXPR_TG_INFO ,strndup (data,sizeof (data)),sizeof (data));nftnl_rule_add_expr (a, ex); nlh =nftnl_rule_nlmsg_build_hdr (buf,NFT_MSG_NEWRULE ,AF_INET ,0 ,1234 );Signature · 1 line
2.29.2
Hi Pablo,
Quoted 8 lines · Pablo Neira Ayuso
Hi Maya,
Quoted 2 lines · Maya Rashish
Our string isn't NUL-terminated. To avoid reading past the last character, use strndup.
Is this a theoretical problem or some static analisys tool is reporting out-of-bound memread?
As background, I had a difficult to diagnose stack corruption with a patched older version. I was hoping it'd just show up by running the tests with address sanitizer (I edited the Makefiles to add CFLAGS=-fsanitize=address and LDFLAGS=-lasan after configure) but it didn't.
Address sanitizer usually reports actual problems, it runs the actual code with some elaborate memory map tricks that lets it detect violations.
But might as well make the tests all run without complaints from address sanitizer while I am doing this.