set: Fix for array overrun when setting NFTNL_SET_DESC_CONCAT
Phil Sutter
reviewerPATCH 1/3 set: Fix for array overrun when setting NFTNL_SET_DESC_CONCAT
Assuming max data_len of 16 * 4B and no zero bytes in 'data':
The while loop will increment field_count, use it as index for the field_len array and afterwards make sure it hasn't increased to NFT_REG32_COUNT. Thus a value of NFT_REG32_COUNT - 1 (= 15) will pass the check, get incremented to 16 and used as index to the 16 fields long array.
Use a less fancy for-loop to avoid the increment vs. check problem.
--- src/set.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
src/set.c +4 -2
@@ -185,8 +185,10 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,return -1 ;memcpy (&s->desc.field_len, data, data_len); -while (s->desc.field_len[++s->desc.field_count]) { -if (s->desc.field_count >=NFT_REG32_COUNT ) +for (s->desc.field_count =0 ; + s->desc.field_count <NFT_REG32_COUNT ; + s->desc.field_count++) { +if (!s->desc.field_len[s->desc.field_count])break ; }break ;
Signature · 1 line
2.47.0
Phil Sutter
reviewerPATCH 2/3 tests: Extend set test by NFTNL_SET_DESC_CONCAT
Just to cover setter and getter code for that attribute.
--- tests/nft-set-test.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tests/nft-set-test.c +17 -0
@@ -21,6 +21,9 @@ static void print_err(const char *msg)static void cmp_nftnl_set (struct nftnl_set *a,struct nftnl_set *b) { +const uint8_t *data_a, *data_b; +uint32_t datalen_a, datalen_b; +if (strcmp (nftnl_set_get_str (a,NFTNL_SET_TABLE ),nftnl_set_get_str (b,NFTNL_SET_TABLE )) !=0 )print_err ("Set table mismatches" );
@@ -45,11 +48,18 @@ static void cmp_nftnl_set(struct nftnl_set *a, struct nftnl_set *b)if (strcmp (nftnl_set_get_str (a,NFTNL_SET_USERDATA ),nftnl_set_get_str (b,NFTNL_SET_USERDATA )) !=0 )print_err ("Set userdata mismatches" ); + + data_a =nftnl_set_get_data (a,NFTNL_SET_DESC_CONCAT , &datalen_a); + data_b =nftnl_set_get_data (b,NFTNL_SET_DESC_CONCAT , &datalen_b); +if (datalen_a != datalen_b || +memcmp (data_a, data_b, datalen_a)) +print_err ("Set desc concat mismatches" ); }int main (int argc,char *argv[]) {struct nftnl_set *a, *b =NULL ; +uint8_t field_lengths[16 ];char buf[4096 ];struct nlmsghdr *nlh;
@@ -68,6 +78,13 @@ int main(int argc, char *argv[])nftnl_set_set_u32 (a,NFTNL_SET_FAMILY ,0x12345678 );nftnl_set_set_str (a,NFTNL_SET_USERDATA ,"testing user data" ); +memset (field_lengths,0xff ,sizeof (field_lengths)); +if (!nftnl_set_set_data (a,NFTNL_SET_DESC_CONCAT , field_lengths,17 )) +print_err ("oversized NFTNL_SET_DESC_CONCAT data accepted" ); +if (nftnl_set_set_data (a,NFTNL_SET_DESC_CONCAT , field_lengths,16 )) +print_err ("setting NFTNL_SET_DESC_CONCAT failed" ); + +/* cmd extracted from include/linux/netfilter/nf_tables.h */ nlh =nftnl_nlmsg_build_hdr (buf,NFT_MSG_NEWSET ,AF_INET ,0 ,1234 );nftnl_set_nlmsg_build_payload (nlh, a);
Signature · 1 line
2.47.0
Phil Sutter
reviewerPATCH 3/3 tests: Fix for ASAN
In reply to Phil SutterThe 'data' arrays in match and target expression tests were undersized as they did not cover for the terminating NUL-char of the string used to initialize them. When passing such array to strdup(), the latter reads until after the defined array boundary.
--- 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
@@ -54,7 +54,7 @@ int main(int argc, char *argv[])char buf[4096 ];struct nftnl_expr_iter *iter_a, *iter_b;struct nftnl_expr *rule_a, *rule_b; -char data[16 ] ="0123456789abcdef" ; +char data[] ="0123456789abcdef" ; a =nftnl_rule_alloc (); b =nftnl_rule_alloc ();
tests/nft-expr_target-test.c +1 -1
@@ -53,7 +53,7 @@ int main(int argc, char *argv[])char buf[4096 ];struct nftnl_expr_iter *iter_a, *iter_b;struct nftnl_expr *rule_a, *rule_b; -char data[16 ] ="0123456789abcdef" ; +char data[] ="0123456789abcdef" ; a =nftnl_rule_alloc (); b =nftnl_rule_alloc ();
Signature · 1 line
2.47.0
Hi Phil,
Quoted 7 lines · Phil Sutter
Assuming max
data_lenof 16 * 4B and no zero bytes in 'data':
The while loop will incrementfield_count, use it as index for thefield_lenarray and afterwards make sure it hasn't increased toNFT_REG32_COUNT. Thus a value ofNFT_REG32_COUNT- 1 (= 15) will pass the check, get incremented to 16 and used as index to the 16 fields long array.
Use a less fancy for-loop to avoid the increment vs. check problem.
for-loop is indeed better.
Patch LGTM, thanks.
Quoted 2 lines
Fixes 407f616ea5318 ("set: buffer overflow in NFTNL_SET_DESC_CONCAT setter")Signed-off-by Phil Sutter <[email protected]>
Quoted 24 lines
--- src/set.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)src/set.c +4
-2@@ -185,8 +185,10 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,return -1 ;memcpy (&s->desc.field_len, data, data_len); -while (s->desc.field_len[++s->desc.field_count]) { -if (s->desc.field_count >=NFT_REG32_COUNT ) +for (s->desc.field_count =0 ; + s->desc.field_count <NFT_REG32_COUNT ; + s->desc.field_count++) { +if (!s->desc.field_len[s->desc.field_count])break ; }break ;Signature · 1 line
2.47.0
Phil Sutter
reviewerQuoted 11 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 7 lines · Phil Sutter
Assuming max
data_lenof 16 * 4B and no zero bytes in 'data':
The while loop will incrementfield_count, use it as index for thefield_lenarray and afterwards make sure it hasn't increased toNFT_REG32_COUNT. Thus a value ofNFT_REG32_COUNT- 1 (= 15) will pass the check, get incremented to 16 and used as index to the 16 fields long array.
Use a less fancy for-loop to avoid the increment vs. check problem.
Quoted 8 lines
for-loop is indeed better.
Patch LGTM, thanks.
Quoted 2 lines
Fixes 407f616ea5318 ("set: buffer overflow in NFTNL_SET_DESC_CONCAT setter")Signed-off-by Phil Sutter <[email protected]>Reviewed-by Pablo Neira Ayuso <[email protected]>
Series applied, thanks!