Skip to contentSkip to conversation

misspell: Avoid segfault with anonymous chains

PATCH In review Phil Sutter 2 participants 4 messages Mar 4, 2022 – Mar 7, 2022 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 misspell: Avoid segfault with anonymous chains

When trying to add a rule which contains an anonymous chain to a non-existent chain, string_misspell_update() is called with a NULL string because the anonymous chain has no name. Avoid this by making the function NULL-pointer tolerant.

c330152b7f777 ("src: support for implicit chain bindings")

---
 src/misspell.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/misspell.c +2 -2
index 6536d7557a445..f213a240005e6 100644
@@ -80,8 +80,8 @@ int string_misspell_update(const char *a, const char *b,
 {
 	unsigned int len_a, len_b, max_len, min_len, distance, threshold;
 
-	len_a = strlen(a);
-	len_b = strlen(b);
+	len_a = a ? strlen(a) : 0;
+	len_b = b ? strlen(b) : 0;
 
 	max_len = max(len_a, len_b);
 	min_len = min(len_a, len_b);
Signature · 1 line
2.34.1
#1 lore raw

Hi Phil,

Quoted 17 lines · Phil Sutter

When trying to add a rule which contains an anonymous chain to a non-existent chain, string_misspell_update() is called with a NULL string because the anonymous chain has no name. Avoid this by making the function NULL-pointer tolerant.

c330152b7f777 ("src: support for implicit chain bindings")

---
 src/misspell.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/misspell.c +0 -0
index 6536d7557a445..f213a240005e6 100644

src/misspell.c int string_misspell_update(const char *a, const char *b, · line 84

Quoted 7 lines
src/misspell.c +2 -2
@@ -80,8 +80,8 @@ int string_misspell_update(const char *a, const char *b,
 {
 	unsigned int len_a, len_b, max_len, min_len, distance, threshold;
 
-	len_a = strlen(a);
-	len_b = strlen(b);
+	len_a = a ? strlen(a) : 0;
+	len_b = b ? strlen(b) : 0;

string_distance() assumes non-NULL too.

probably shortcircuit chain_lookup_fuzzy() earlier since h->chain.name is always NULL, to avoid the useless loop.

Patch attached.

Quoted 5 lines
src/misspell.c +0 -0
 	max_len = max(len_a, len_b);
 	min_len = min(len_a, len_b);
Signature · 1 line
2.34.1
src/rule.c +3 -0
index b1700c40079d..19b8cb0323ee 100644
@@ -758,6 +758,9 @@ struct chain *chain_lookup_fuzzy(const struct handle *h,
 	struct table *table;
 	struct chain *chain;
 
+	if (!h->chain.name)
+		return NULL;
+
 	string_misspell_init(&st);
 
 	list_for_each_entry(table, &cache->table_cache.list, cache.list) {
author reviewer
#2 lore raw
Quoted 23 lines · Pablo Neira Ayuso

Hi Phil,

Quoted 20 lines · Phil Sutter

When trying to add a rule which contains an anonymous chain to a non-existent chain, string_misspell_update() is called with a NULL string because the anonymous chain has no name. Avoid this by making the function NULL-pointer tolerant.

c330152b7f777 ("src: support for implicit chain bindings")

---
 src/misspell.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/misspell.c +0 -0
index 6536d7557a445..f213a240005e6 100644
@@ -80,8 +80,8 @@ int string_misspell_update(const char *a, const char *b,
 {
 	unsigned int len_a, len_b, max_len, min_len, distance, threshold;
 
Quoted 6 lines

src/misspell.c int string_misspell_update(const char *a, const char *b, · line 84

Quoted 4 lines
src/misspell.c +2 -2
-	len_a = strlen(a);
-	len_b = strlen(b);
+	len_a = a ? strlen(a) : 0;
+	len_b = b ? strlen(b) : 0;

string_distance() assumes non-NULL too.

Which is called from string_misspell_update() only which with my patch returns early due to 'max_len <= 1'.

Quoted 2 lines

probably shortcircuit chain_lookup_fuzzy() earlier since h->chain.name is always NULL, to avoid the useless loop.

Fine with me, too! What about allocating a name for the anonymous chain instead? I guess similar treatment as with sets would make sense. Might also help with netlink debug output:

| # nft --debug=netlink insert rule inet x y 'goto { accept; }'
| inet (null) (null) use 0
| inet x
|   [ immediate reg 0 accept ]
| 
|   inet x y
|     [ immediate reg 0 goto ]
| [...]

Thanks, Phil

#3 lore raw
Quoted 31 lines · Phil Sutter
Quoted 23 lines · Pablo Neira Ayuso

Hi Phil,

Quoted 20 lines · Phil Sutter

When trying to add a rule which contains an anonymous chain to a non-existent chain, string_misspell_update() is called with a NULL string because the anonymous chain has no name. Avoid this by making the function NULL-pointer tolerant.

c330152b7f777 ("src: support for implicit chain bindings")

---
 src/misspell.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/misspell.c +0 -0
index 6536d7557a445..f213a240005e6 100644
@@ -80,8 +80,8 @@ int string_misspell_update(const char *a, const char *b,
 {
 	unsigned int len_a, len_b, max_len, min_len, distance, threshold;
 
Quoted 6 lines

src/misspell.c int string_misspell_update(const char *a, const char *b, · line 84

Quoted 4 lines
src/misspell.c +2 -2
-	len_a = strlen(a);
-	len_b = strlen(b);
+	len_a = a ? strlen(a) : 0;
+	len_b = b ? strlen(b) : 0;

string_distance() assumes non-NULL too.

Quoted 8 lines

Which is called from string_misspell_update() only which with my patch returns early due to 'max_len <= 1'.

Quoted 2 lines

probably shortcircuit chain_lookup_fuzzy() earlier since h->chain.name is always NULL, to avoid the useless loop.

Fine with me, too! What about allocating a name for the anonymous chain instead?

A dummy name could be allocated, but the kernel does not need the chain name at this stage (it uses the ephemeral 32-bit chain ID instead which is only valid in the netlink batch).

Probably set_lookup_fuzzy() should also short-circuit early the misspell logic for anonymous sets.

Quoted 5 lines

I guess similar treatment as with sets would make sense. Might also help with netlink debug output:

| # nft --debug=netlink insert rule inet x y 'goto { accept; }' | inet (null) (null) use 0

# nft add table x
# nft --debug=netlink add chain x y
ip (null) (null) use 0

table is null, at least this one should be set on, but this is a partially different issue.

Quoted 8 lines
| inet x
|   [ immediate reg 0 accept ]
| 
|   inet x y
|     [ immediate reg 0 goto ]
| [...]

Thanks, Phil