Skip to contentSkip to conversation

netfilter: conntrack: add shared port parser and use it in IRC and Amanda helpers

PATCH Superseded HACKE-RC 3 participants 14 messages May 1 – Sep 7

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

HACKE-RC

author
#0 lore raw

v2 cover netfilter: conntrack: add shared port parser and use it in IRC and Amanda helpers

Both nf_conntrack_irc and nf_conntrack_amanda parse port numbers from application-layer protocol data using simple_strtoul(), which relies on nul-terminated strings and returns unsigned long without range checking. Port values above 65535 silently truncate when stored in u16.

This v2 adds a shared nf_ct_helper_parse_port() function to the conntrack helper core, modeled after the approach in 8cf6809cddcb ("netfilter: nf_conntrack_sip: don't use simple_strtoul"), then converts both helpers to use it.

Changes since v1:
- Added shared nf_ct_helper_parse_port() in the helper core instead of open-coding range checks in each helper (Pablo)
- Parser does not rely on nul-terminated strings
- Dropped simple_strtoul usage entirely for port parsing

HACKE-RC (3):
netfilter: conntrack: add shared port parser for helpers
netfilter: nf_conntrack_irc: use nf_ct_helper_parse_port()
netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()

 include/net/netfilter/nf_conntrack_helper.h |  3 +++
 net/netfilter/nf_conntrack_amanda.c         | 11 ++++----
 net/netfilter/nf_conntrack_helper.c         | 28 +++++++++++++++++++++
 net/netfilter/nf_conntrack_irc.c            |  4 ++-
 4 files changed, 40 insertions(+), 6 deletions(-)
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v2 1/3 netfilter: conntrack: add shared port parser for helpers

Add nf_ct_helper_parse_port() to the conntrack helper core. This provides a port parser that does not rely on nul-terminated strings, taking an explicit length parameter and validating the result fits in the 1-65535 range.

Modeled after the approach in 8cf6809cddcb ("netfilter:
nf_conntrack_sip: don't use simple_strtoul") but as a shared function so IRC, Amanda, and other helpers can use it instead of open-coding simple_strtoul calls with ad-hoc range checks.

---
 include/net/netfilter/nf_conntrack_helper.h |  3 +++
 net/netfilter/nf_conntrack_helper.c         | 28 +++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/net/netfilter/nf_conntrack_helper.h +3 -0
index de2f956ab..db19fe25f 100644
@@ -160,6 +160,9 @@ nf_ct_helper_expectfn_find_by_name(const char *name);
 struct nf_ct_helper_expectfn *
 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
 
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp);
+
 extern struct hlist_head *nf_ct_helper_hash;
 extern unsigned int nf_ct_helper_hsize;
 
net/netfilter/nf_conntrack_helper.c +28 -0
index a715304a5..12f51670d 100644
@@ -499,6 +499,34 @@ void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
 }
 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
 
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp)
+{
+	unsigned long result = 0;
+	const char *start = cp;
+
+	while (len > 0 && *cp >= '0' && *cp <= '9') {
+		result = result * 10 + (*cp - '0');
+		if (result > 65535)
+			return -1;
+		cp++;
+		len--;
+	}
+
+	if (cp == start)
+		return -1;
+
+	if (result == 0)
+		return -1;
+
+	*port = result;
+	if (endp)
+		*endp = (char *)cp;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nf_ct_helper_parse_port);
+
 int nf_conntrack_helper_init(void)
 {
 	nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
Signature · 1 line
2.54.0
reviewer
#2 lore raw

Re: [PATCH net-next v2 1/3] netfilter: conntrack: add shared port parser for helpers

Hi,

Quoted 44 lines · HACKE-RC

Add nf_ct_helper_parse_port() to the conntrack helper core. This provides a port parser that does not rely on nul-terminated strings, taking an explicit length parameter and validating the result fits in the 1-65535 range.

Modeled after the approach in 8cf6809cddcb ("netfilter:
nf_conntrack_sip: don't use simple_strtoul") but as a shared function so IRC, Amanda, and other helpers can use it instead of open-coding simple_strtoul calls with ad-hoc range checks.

---
 include/net/netfilter/nf_conntrack_helper.h |  3 +++
 net/netfilter/nf_conntrack_helper.c         | 28 +++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/net/netfilter/nf_conntrack_helper.h +3 -0
index de2f956ab..db19fe25f 100644
@@ -160,6 +160,9 @@ nf_ct_helper_expectfn_find_by_name(const char *name);
 struct nf_ct_helper_expectfn *
 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
 
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp);
+
 extern struct hlist_head *nf_ct_helper_hash;
 extern unsigned int nf_ct_helper_hsize;
 
net/netfilter/nf_conntrack_helper.c +6 -0
index a715304a5..12f51670d 100644
@@ -499,6 +499,34 @@ void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
 }
 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
 
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp)
+{
+	unsigned long result = 0;
+	const char *start = cp;
+

net/netfilter/nf_conntrack_helper.c void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat) · line 517

Quoted 10 lines
net/netfilter/nf_conntrack_helper.c +10 -0
+	while (len > 0 && *cp >= '0' && *cp <= '9') {
+		result = result * 10 + (*cp - '0');
+		if (result > 65535)
+			return -1;
+		cp++;
+		len--;
+	}
+
+	if (cp == start)
+		return -1;

This check is redundant wrt. the following one: If the loop didn't increment 'cp', result must be zero. So you may just drop it entirely.

Cheers, Phil

Quoted 19 lines
net/netfilter/nf_conntrack_helper.c +12 -0
+
+	if (result == 0)
+		return -1;
+
+	*port = result;
+	if (endp)
+		*endp = (char *)cp;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nf_ct_helper_parse_port);
+
 int nf_conntrack_helper_init(void)
 {
 	nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
Signature · 1 line
2.54.0
author
#3 lore raw

PATCH v2 2/3 netfilter: nf_conntrack_irc: use nf_ct_helper_parse_port()

In reply to HACKE-RC

Replace the bare simple_strtoul() call for port parsing with the shared nf_ct_helper_parse_port(). This avoids reliance on the nul-terminated string guarantee (currently provided by the newline scan earlier in parse_dcc) and validates the port fits in u16.

The simple_strtoul() for the IP address field is left as-is since it returns unsigned long for a __be32 conversion, which is a separate concern.

Fixes 869f37d8e48f ("[NETFILTER]: nf_conntrack/nf_nat: add IRC helper port")
---
 net/netfilter/nf_conntrack_irc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_conntrack_irc.c +3 -1
index 522183b9a..1b51f5a6a 100644
@@ -93,7 +93,9 @@ static int parse_dcc(char *data, const char *data_end, __be32 *ip,
 		data++;
 	}
 
-	*port = simple_strtoul(data, &data, 10);
+	if (nf_ct_helper_parse_port(data, data_end - data, port, &data))
+		return -1;
+
 	*ad_end_p = data;
 
 	return 0;
Signature · 1 line
2.54.0
author
#4 lore raw

PATCH v2 3/3 netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()

In reply to HACKE-RC

Replace the bare simple_strtoul() call with the shared nf_ct_helper_parse_port(). This removes reliance on the nul-terminated pbuf string for parsing and validates the port range in a single call.

The len > 5 guard and port == 0 check are now handled by the shared parser, which rejects zero and values above 65535.

Reorder local variable declarations to reverse christmas tree.

Fixes 16958900578b ("[NETFILTER]: nf_conntrack/nf_nat: add amanda helper port")
---
 net/netfilter/nf_conntrack_amanda.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_amanda.c +6 -5
index d2c09e8dd..30b5c4b84 100644
@@ -88,11 +88,12 @@ static int amanda_help(struct sk_buff *skb,
 	struct nf_conntrack_expect *exp;
 	struct nf_conntrack_tuple *tuple;
 	unsigned int dataoff, start, stop, off, i;
+	nf_nat_amanda_hook_fn *nf_nat_amanda;
 	char pbuf[sizeof("65535")], *tmp;
+	int ret = NF_ACCEPT;
 	u_int16_t len;
+	u16 parsed_port;
 	__be16 port;
-	int ret = NF_ACCEPT;
-	nf_nat_amanda_hook_fn *nf_nat_amanda;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -132,10 +133,10 @@ static int amanda_help(struct sk_buff *skb,
 			break;
 		pbuf[len] = '\0';
 
-		port = htons(simple_strtoul(pbuf, &tmp, 10));
-		len = tmp - pbuf;
-		if (port == 0 || len > 5)
+		if (nf_ct_helper_parse_port(pbuf, len, &parsed_port, &tmp))
 			break;
+		port = htons(parsed_port);
+		len = tmp - pbuf;
 
 		exp = nf_ct_expect_alloc(ct);
 		if (exp == NULL) {
Signature · 1 line
2.54.0
reviewer
#5 lore raw
In reply to HACKE-RC
Quoted 10 lines · HACKE-RC

Both nf_conntrack_irc and nf_conntrack_amanda parse port numbers from application-layer protocol data using simple_strtoul(), which relies on nul-terminated strings and returns unsigned long without range checking. Port values above 65535 silently truncate when stored in u16.

This v2 adds a shared nf_ct_helper_parse_port() function to the conntrack helper core, modeled after the approach in 8cf6809cddcb ("netfilter: nf_conntrack_sip: don't use simple_strtoul"), then converts both helpers to use it.

Looking at Florian's patch, how about going the extra mile of implementing a shared nf_ct_helper_parse_uint() which is called by the new nf_ct_helper_parse_port(), then drop sip_strtouint() for the former and have sip_parse_port() call the latter (wrapped by the colon and min port value checks) in a fourth patch?

Cheers, Phil

Quoted 22 lines

Changes since v1:
- Added shared nf_ct_helper_parse_port() in the helper core instead of open-coding range checks in each helper (Pablo)
- Parser does not rely on nul-terminated strings
- Dropped simple_strtoul usage entirely for port parsing

HACKE-RC (3):
netfilter: conntrack: add shared port parser for helpers
netfilter: nf_conntrack_irc: use nf_ct_helper_parse_port()
netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()

 include/net/netfilter/nf_conntrack_helper.h |  3 +++
 net/netfilter/nf_conntrack_amanda.c         | 11 ++++----
 net/netfilter/nf_conntrack_helper.c         | 28 +++++++++++++++++++++
 net/netfilter/nf_conntrack_irc.c            |  4 ++-
 4 files changed, 40 insertions(+), 6 deletions(-)
Signature · 1 line
2.54.0
author
#6 lore raw

v3 cover netfilter: conntrack: shared port parser for helpers

Both nf_conntrack_irc and nf_conntrack_amanda parse port numbers from application-layer data using simple_strtoul(), which requires NUL-terminated input and returns unsigned long without range validation.

This series introduces two shared helpers in the conntrack core:

nf_ct_helper_parse_uint() -- generic bounded integer parser that
operates on a length-delimited buffer without requiring NUL termination.

nf_ct_helper_parse_port() -- calls nf_ct_helper_parse_uint() with
max=65535 and rejects port zero.

Patches 2 and 3 convert IRC and Amanda to use nf_ct_helper_parse_port(). Patch 4 converts the two port-parsing sites in nf_conntrack_sip to use nf_ct_helper_parse_port() as well, retaining the SIP-specific minimum port check (>= 1024).

v3: add nf_ct_helper_parse_uint() as the generic base; nf_ct_helper_parse_port()
is now a thin wrapper; extend the series with a fourth patch converting nf_conntrack_sip (Phil Sutter)
v2: replace simple_strtoul() with a shared nf_ct_helper_parse_port()
in the conntrack helper core, modelled on 8cf6809cddcb (Florian Westphal)
v1: inline range checks in IRC and Amanda

HACKE-RC (4):
netfilter: conntrack: add shared port and uint parsers for helpers
netfilter: nf_conntrack_irc: use nf_ct_helper_parse_port()
netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()
netfilter: nf_conntrack_sip: use nf_ct_helper_parse_port()

 include/net/netfilter/nf_conntrack_helper.h |  5 +++
 net/netfilter/nf_conntrack_amanda.c         | 11 +++---
 net/netfilter/nf_conntrack_helper.c         | 39 +++++++++++++++++++++
 net/netfilter/nf_conntrack_irc.c            |  4 ++-
 net/netfilter/nf_conntrack_sip.c            | 14 ++++----
 5 files changed, 61 insertions(+), 12 deletions(-)
Signature · 1 line
2.54.0
author
#7 lore raw

PATCH v3 1/4 netfilter: conntrack: add shared port and uint parsers for helpers

Add nf_ct_helper_parse_uint() for bounded unsigned integer parsing from an unterminated buffer, and nf_ct_helper_parse_port() which calls it with max=65535 and rejects port zero. Both helpers are exported so conntrack protocol helpers can replace ad-hoc simple_strtoul() usage.

---
 include/net/netfilter/nf_conntrack_helper.h |  5 +++
 net/netfilter/nf_conntrack_helper.c         | 39 +++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/net/netfilter/nf_conntrack_helper.h +5 -0
index de2f956ab..ab145fcd9 100644
@@ -160,6 +160,11 @@ nf_ct_helper_expectfn_find_by_name(const char *name);
 struct nf_ct_helper_expectfn *
 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
 
+int nf_ct_helper_parse_uint(const char *cp, unsigned int len,
+			    unsigned long max, unsigned long *val, char **endp);
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp);
+
 extern struct hlist_head *nf_ct_helper_hash;
 extern unsigned int nf_ct_helper_hsize;
 
net/netfilter/nf_conntrack_helper.c +39 -0
index a715304a5..f6229957c 100644
@@ -499,6 +499,45 @@ void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
 }
 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
 
+int nf_ct_helper_parse_uint(const char *cp, unsigned int len,
+			    unsigned long max, unsigned long *val, char **endp)
+{
+	unsigned long result = 0;
+
+	if (!len || *cp < '0' || *cp > '9')
+		return -1;
+
+	while (len > 0 && *cp >= '0' && *cp <= '9') {
+		result = result * 10 + (*cp - '0');
+		if (result > max)
+			return -1;
+		cp++;
+		len--;
+	}
+
+	*val = result;
+	if (endp)
+		*endp = (char *)cp;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nf_ct_helper_parse_uint);
+
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp)
+{
+	unsigned long val;
+
+	if (nf_ct_helper_parse_uint(cp, len, 65535, &val, endp))
+		return -1;
+	if (val == 0)
+		return -1;
+
+	*port = val;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nf_ct_helper_parse_port);
+
 int nf_conntrack_helper_init(void)
 {
 	nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH net-next v3 1/4] netfilter: conntrack: add shared port and uint parsers for helpers

Quoted 6 lines · HACKE-RC

Add nf_ct_helper_parse_uint() for bounded unsigned integer parsing from an unterminated buffer, and nf_ct_helper_parse_port() which calls it with max=65535 and rejects port zero. Both helpers are exported so conntrack protocol helpers can replace ad-hoc simple_strtoul() usage.

You will need a "real name" here.

Quoted 26 lines
---
 include/net/netfilter/nf_conntrack_helper.h |  5 +++
 net/netfilter/nf_conntrack_helper.c         | 39 +++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/net/netfilter/nf_conntrack_helper.h +5 -0
index de2f956ab..ab145fcd9 100644
@@ -160,6 +160,11 @@ nf_ct_helper_expectfn_find_by_name(const char *name);
 struct nf_ct_helper_expectfn *
 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
 
+int nf_ct_helper_parse_uint(const char *cp, unsigned int len,
+			    unsigned long max, unsigned long *val, char **endp);
+int nf_ct_helper_parse_port(const char *cp, unsigned int len,
+			    u16 *port, char **endp);
+
 extern struct hlist_head *nf_ct_helper_hash;
 extern unsigned int nf_ct_helper_hsize;
 
net/netfilter/nf_conntrack_helper.c +0 -0
index a715304a5..f6229957c 100644

net/netfilter/nf_conntrack_helper.c void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat) · line 503

Quoted 5 lines
net/netfilter/nf_conntrack_helper.c +2 -0
@@ -499,6 +499,45 @@ void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
 }
 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
 
+int nf_ct_helper_parse_uint(const char *cp, unsigned int len,
+			    unsigned long max, unsigned long *val, char **endp)

In nf.git, there is a new function sip_strtouint() that can possibly be moved to nf_conntrack_helper.c.

Thanks.

author
#9 lore raw

Re: [PATCH net-next v3 1/4] netfilter: conntrack: add shared port and uint parsers for helpers

Quoted 3 lines · Pablo Neira Ayuso

You will need a "real name" here.
In nf.git, there is a new function sip_strtouint() that can possibly be moved to nf_conntrack_helper.c.

Apologies — my name is Rahul. v4 will use "Rahul <[email protected]>" throughout.

I looked at 8cf6809cddcb ("netfilter: nf_conntrack_sip: don't use simple_strtoul"). Florian's patch adds two local helpers:

- sip_strtouint(): generic bounded uint parser, used for expires, cseq, clen, code, and port in process_sdp().
- sip_parse_port(): port-specific wrapper around sip_strtouint(), used in epaddr_len(), ct_sip_parse_request(), and ct_sip_parse_header_uri().

My v3 patch 4 already converts ct_sip_parse_request() and ct_sip_parse_header_uri() to use nf_ct_helper_parse_port(), which overlaps with what sip_parse_port() does.

Would it make sense to:

1. Align nf_ct_helper_parse_uint() with sip_strtouint()'s design (same endp-on-error semantics, UINT_MAX cap) and export it.

2. In patch 4, drop sip_strtouint() from nf_conntrack_sip.c and replace all its call sites with nf_ct_helper_parse_uint(), and replace sip_parse_port() with nf_ct_helper_parse_port()?

That would consolidate both local helpers into the shared core and avoid the overlap. I am happy to send a v4 along those lines if that is the direction you prefer.

Thanks,
Rahul

author
#10 lore raw

PATCH v3 2/4 netfilter: nf_conntrack_irc: use nf_ct_helper_parse_port()

In reply to HACKE-RC

Replace simple_strtoul() with the new nf_ct_helper_parse_port() helper. This removes the dependency on NUL-terminated strings and adds an explicit port range check, rejecting port 0 and values above 65535.

Fixes 869f37d8e48f ("netfilter: nf_conntrack_irc - Fix uninitialised variable warning")
---
 net/netfilter/nf_conntrack_irc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_conntrack_irc.c +3 -1
index 522183b9a..1b51f5a6a 100644
@@ -93,7 +93,9 @@ static int parse_dcc(char *data, const char *data_end, __be32 *ip,
 		data++;
 	}
 
-	*port = simple_strtoul(data, &data, 10);
+	if (nf_ct_helper_parse_port(data, data_end - data, port, &data))
+		return -1;
+
 	*ad_end_p = data;
 
 	return 0;
Signature · 1 line
2.54.0
author
#11 lore raw

PATCH v3 3/4 netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()

In reply to HACKE-RC

Replace simple_strtoul() with the new nf_ct_helper_parse_port() helper. This removes the dependency on NUL-terminated strings and adds an explicit port range check, rejecting port 0 and values above 65535.

Fixes 16958900578b ("netfilter: nf_conntrack_amanda: the match is called 'amanda', not 'AMANDA'")
---
 net/netfilter/nf_conntrack_amanda.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_amanda.c +6 -5
index d2c09e8dd..30b5c4b84 100644
@@ -88,11 +88,12 @@ static int amanda_help(struct sk_buff *skb,
 	struct nf_conntrack_expect *exp;
 	struct nf_conntrack_tuple *tuple;
 	unsigned int dataoff, start, stop, off, i;
+	nf_nat_amanda_hook_fn *nf_nat_amanda;
 	char pbuf[sizeof("65535")], *tmp;
+	int ret = NF_ACCEPT;
 	u_int16_t len;
+	u16 parsed_port;
 	__be16 port;
-	int ret = NF_ACCEPT;
-	nf_nat_amanda_hook_fn *nf_nat_amanda;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -132,10 +133,10 @@ static int amanda_help(struct sk_buff *skb,
 			break;
 		pbuf[len] = '\0';
 
-		port = htons(simple_strtoul(pbuf, &tmp, 10));
-		len = tmp - pbuf;
-		if (port == 0 || len > 5)
+		if (nf_ct_helper_parse_port(pbuf, len, &parsed_port, &tmp))
 			break;
+		port = htons(parsed_port);
+		len = tmp - pbuf;
 
 		exp = nf_ct_expect_alloc(ct);
 		if (exp == NULL) {
Signature · 1 line
2.54.0
#12 lore raw

Re: [PATCH net-next v3 3/4] netfilter: nf_conntrack_amanda: use nf_ct_helper_parse_port()

Hi Raoul,

Quoted 6 lines · HACKE-RC

Replace simple_strtoul() with the new nf_ct_helper_parse_port() helper. This removes the dependency on NUL-terminated strings and adds an explicit port range check, rejecting port 0 and values above 65535.

Fixes 16958900578b ("netfilter: nf_conntrack_amanda: the match is called 'amanda', not 'AMANDA'")

We need a real name here, please target this to the nf/nf-next trees.

Thanks.

Quoted 40 lines
---
 net/netfilter/nf_conntrack_amanda.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_amanda.c +6 -5
index d2c09e8dd..30b5c4b84 100644
@@ -88,11 +88,12 @@ static int amanda_help(struct sk_buff *skb,
 	struct nf_conntrack_expect *exp;
 	struct nf_conntrack_tuple *tuple;
 	unsigned int dataoff, start, stop, off, i;
+	nf_nat_amanda_hook_fn *nf_nat_amanda;
 	char pbuf[sizeof("65535")], *tmp;
+	int ret = NF_ACCEPT;
 	u_int16_t len;
+	u16 parsed_port;
 	__be16 port;
-	int ret = NF_ACCEPT;
-	nf_nat_amanda_hook_fn *nf_nat_amanda;
 
 	/* Only look at packets from the Amanda server */
 	if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
@@ -132,10 +133,10 @@ static int amanda_help(struct sk_buff *skb,
 			break;
 		pbuf[len] = '\0';
 
-		port = htons(simple_strtoul(pbuf, &tmp, 10));
-		len = tmp - pbuf;
-		if (port == 0 || len > 5)
+		if (nf_ct_helper_parse_port(pbuf, len, &parsed_port, &tmp))
 			break;
+		port = htons(parsed_port);
+		len = tmp - pbuf;
 
 		exp = nf_ct_expect_alloc(ct);
 		if (exp == NULL) {
Signature · 1 line
2.54.0
author
#13 lore raw

PATCH v3 4/4 netfilter: nf_conntrack_sip: use nf_ct_helper_parse_port()

In reply to HACKE-RC

Replace simple_strtoul() based port parsing in ct_sip_parse_request() and ct_sip_parse_header_uri() with nf_ct_helper_parse_port(), which handles the bounded parse without requiring NUL-termination. The SIP-specific minimum port check (>= 1024) is retained as before.

---
 net/netfilter/nf_conntrack_sip.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_sip.c +8 -6
index 182cfb119..ac29f0762 100644
@@ -241,7 +241,7 @@ int ct_sip_parse_request(const struct nf_conn *ct,
 {
 	const char *start = dptr, *limit = dptr + datalen, *end;
 	unsigned int mlen;
-	unsigned int p;
+	u16 p;
 	int shift = 0;
 
 	/* Skip method and following whitespace */
@@ -269,8 +269,9 @@ int ct_sip_parse_request(const struct nf_conn *ct,
 		return -1;
 	if (end < limit && *end == ':') {
 		end++;
-		p = simple_strtoul(end, (char **)&end, 10);
-		if (p < 1024 || p > 65535)
+		if (nf_ct_helper_parse_port(end, limit - end, &p, (char **)&end))
+			return -1;
+		if (p < 1024)
 			return -1;
 		*port = htons(p);
 	} else
@@ -509,7 +510,7 @@ int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
 			    union nf_inet_addr *addr, __be16 *port)
 {
 	const char *c, *limit = dptr + datalen;
-	unsigned int p;
+	u16 p;
 	int ret;
 
 	ret = ct_sip_walk_headers(ct, dptr, dataoff ? *dataoff : 0, datalen,
@@ -522,8 +523,9 @@ int ct_sip_parse_header_uri(const struct nf_conn *ct, const char *dptr,
 		return -1;
 	if (*c == ':') {
 		c++;
-		p = simple_strtoul(c, (char **)&c, 10);
-		if (p < 1024 || p > 65535)
+		if (nf_ct_helper_parse_port(c, limit - c, &p, (char **)&c))
+			return -1;
+		if (p < 1024)
 			return -1;
 		*port = htons(p);
 	} else
Signature · 1 line
2.54.0