datatype: fix ether address parsing of integer values
PATCH 1/1 datatype: fix ether address parsing of integer values
When parsing "ether daddr 0x64", integer_expr converts the hex value to decimal string "100". lladdr_type_parse then interprets this as hex, yielding 0x100 = 256 which exceeds the single-byte limit.
Values below 0x64 (100) worked because their decimal representation happens to be valid hex under 256.
Fix by detecting integer strings (no colons, known size) and parsing as base-0 to handle both decimal and hex input correctly.
Bugzilla: https://bugzilla.netfilter.org/show_bug.cgi?id=1824
--- src/datatype.c | 17 ++++++++ .../testcases/nft-f/0033ether_addr_hex_0 | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 tests/shell/testcases/nft-f/0033ether_addr_hex_0
src/datatype.c +17 -0
@@ -581,6 +581,23 @@ static struct error_record *lladdr_type_parse(struct parse_ctx *ctx,const char *s = sym->identifier;unsigned int len, n; +if (sym->dtype->size && !strchr (s,':' )) { +unsigned int size = sym->dtype->size /BITS_PER_BYTE ; +uint64_t val; + + errno =0 ; + val =strtoull (s, &p,0 ); +if (!errno && !*p && !(val >> (size *8 ))) { +for (n =0 ; n < size; n++) + buf[size - n -1 ] = (val >> (n *8 )) &0xff ; + + *res =constant_expr_alloc (&sym->location, sym->dtype, +BYTEORDER_BIG_ENDIAN , + sym->dtype->size, buf); +return NULL ; + } + } +for (len =0 ;;) { n =strtoul (s, &p,16 );if (s == p || n >0xff )
tests/shell/testcases/nft-f/0033ether_addr_hex_0 new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.47.3
Florian Westphal
maintainerQuoted 3 lines · Brian Witte
When parsing "ether daddr
0x64",integer_exprconverts the hex value to decimal string "100".lladdr_type_parsethen interprets this as hex, yielding0x100= 256 which exceeds the single-byte limit.
Yes, but it looks like it breaks backwards compatibility.
'ether daddr 99' is parsed as 00:00:00:00:00:99. After your patch, its parsed as 00:00:00:00:00:63.
So I'm not sure we can fix this.
Quoted 6 lines · Florian Westphal
Yes, but it looks like it breaks backwards compatibility.
'ether daddr 99' is parsed as 00:00:00:00:00:99. After your patch, its parsed as 00:00:00:00:00:63.
So I'm not sure we can fix this.
You're right, I hadn't considered that case. Thanks for the review.
Brian