public inbox for netfilter-devel@vger.kernel.org
 help / color / mirror / Atom feed
From: Cyber-JA <giuseppecaruso0990@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Cyber-JA <giuseppecaruso0990@gmail.com>
Subject: [PATCH 2/2] netfilter: validate values parsed by try_number
Date: Fri, 10 Apr 2026 10:08:43 -0400	[thread overview]
Message-ID: <20260410140843.52027-1-giuseppecaruso0990@gmail.com> (raw)

try_number() parses comma-separated decimal values from FTP PORT and
EPRT commands into a u_int32_t array, but does not validate that each
value fits in a single octet. RFC 959 specifies that PORT parameters
are decimal integers in the range 0-255, representing the four octets
of an IP address followed by two octets encoding the port number.

Values exceeding 255 are silently accepted. In try_rfc959(), the raw
u32 values are combined via shift-and-OR to form the IP and port:

  cmd->u3.ip = htonl((array[0] << 24) | (array[1] << 16) |
                     (array[2] << 8) | array[3]);
  cmd->u.tcp.port = htons((array[4] << 8) | array[5]);

When array elements exceed 255, bits from one field bleed into adjacent
fields after shifting, producing IP addresses and port numbers that
differ from what the text representation suggests. For example,
"PORT 10,0,1,2,256,22" yields port (256<<8)|22 = 65558, truncated to
u16 = 22. This mismatch between the textual and computed values can
confuse network monitoring tools that parse FTP commands independently.

Reject the command by returning 0 (no match) when any accumulated
value exceeds 255.

Signed-off-by: Giuseppe Caruso <giuseppecaruso0990@gmail.com>
---
 net/netfilter/nf_conntrack_ftp.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/netfilter/nf_conntrack_ftp.c b/net/netfilter/nf_conntrack_ftp.c
index 5e00f9123c38..12a6d9dd16a5 100644
--- a/net/netfilter/nf_conntrack_ftp.c
+++ b/net/netfilter/nf_conntrack_ftp.c
@@ -126,6 +126,10 @@ static int try_number(const char *data, size_t dlen, u_int32_t array[],
 	for (i = 0, len = 0; len < dlen && i < array_size; len++, data++) {
 		if (*data >= '0' && *data <= '9') {
 			array[i] = array[i]*10 + *data - '0';
+			if (array[i] > 255) {
+				pr_debug("try_number: %u > 255\n", array[i]);
+				return 0;
+			}
 		}
 		else if (*data == sep)
 			i++;
-- 
2.53.0


                 reply	other threads:[~2026-04-10 14:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260410140843.52027-1-giuseppecaruso0990@gmail.com \
    --to=giuseppecaruso0990@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox