Skip to contentSkip to conversation

ss: fix vsock port filtering

PATCH Applied Luigi Leonardi 3 participants 5 messages Aug 17 – Aug 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover ss: fix vsock port filtering

Vsock port filtering is broken for ports in [1, 2^31−1] (i.e. any port whose u32 representation has bit 31 clear, which includes all ordinarily assigned vsock ports).

To reproduce:

    ncat -l --vsock -p 27354
    ss --vsock -l src :27354   # shows nothing

    # Without the filter the socket is visible:
    ss --vsock -l

Ports in [2^31, UINT_MAX−1] are not affected by accident.

aafilter.port was extended to 8 bytes to support unix socket inode numbers larger than INT_MAX (see 012cb515). The vsock parsing path initialises a.port to -1L and then calls get_u32() via a cast to write the user-supplied port into it. get_u32() only writes 4 bytes, leaving the upper 32 bits as 0xFFFFFFFF. For ports < 2^31 this produces a mismatch with sockstat.lport.
For ports >= 2^31 the int-to-long sign extension also fills the upper 32 bits with 0xFFFFFFFF, accidentally matching the corrupted a.port and hiding the bug.

Commit 1 switches from int to long for sockstat.lport/rport Commit 2 fixes a bug in the parsing of the filter port, that didn't clear the upper bits.

---
Changes in v2:
- Expanded lport and rport in sockstat from int to long [Stefano]
- Applied suggestion from Stephen for port filtering.
- Link to v1: https://lore.kernel.org/all/[email protected]

---
Luigi Leonardi (2):
ss: move lport and rport in struct sockstat from int to long
ss: fix vsock port filter

 misc/ss.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

---
base-commit: da2ccdf862cb1eab45de082cc71fcb4e5d712e78
change-id: 20260817-fix_vsock-f7b374d5cadd

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#1 lore raw

PATCH v2 1/2 ss: move lport and rport in struct sockstat from int to long

Commit 012cb515 ("ss: change aafilter port from int to long (inode support)") widened aafilter.port to long so that unix socket inode numbers larger than INT_MAX can be used as port filter values, but left sockstat.lport and sockstat.rport as int. This means large u32 values (e.g. vsock ports or unix inodes > INT_MAX) are truncated on the socket-stat side, making the comparison against aafilter.port always fail.

Widen lport and rport to long and update the sscanf format specifiers in proc_parse_inet_addr() and unix_show() accordingly.

Fixes 012cb515 ("ss: change aafilter port from int to long (inode support)")
---
 misc/ss.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
misc/ss.c +10 -10
index 14e9f27a..ff1a88de 100644
@@ -809,8 +809,8 @@ struct sockstat {
 	uint16_t	    raw_prot;
 	inet_prefix	    local;
 	inet_prefix	    remote;
-	int		    lport;
-	int		    rport;
+	long		    lport;
+	long		    rport;
 	int		    state;
 	int		    rq, wq;
 	unsigned int ino;
@@ -2490,23 +2490,23 @@ static int proc_parse_inet_addr(char *loc, char *rem, int family, struct
 {
 	s->local.family = s->remote.family = family;
 	if (family == AF_INET) {
-		sscanf(loc, "%x:%x", s->local.data, (unsigned *)&s->lport);
-		sscanf(rem, "%x:%x", s->remote.data, (unsigned *)&s->rport);
+		sscanf(loc, "%x:%lx", s->local.data, (unsigned long *)&s->lport);
+		sscanf(rem, "%x:%lx", s->remote.data, (unsigned long *)&s->rport);
 		s->local.bytelen = s->remote.bytelen = 4;
 		return 0;
 	} else {
-		sscanf(loc, "%08x%08x%08x%08x:%x",
+		sscanf(loc, "%08x%08x%08x%08x:%lx",
 		       s->local.data,
 		       s->local.data + 1,
 		       s->local.data + 2,
 		       s->local.data + 3,
-		       &s->lport);
-		sscanf(rem, "%08x%08x%08x%08x:%x",
+		       (unsigned long *)&s->lport);
+		sscanf(rem, "%08x%08x%08x%08x:%lx",
 		       s->remote.data,
 		       s->remote.data + 1,
 		       s->remote.data + 2,
 		       s->remote.data + 3,
-		       &s->rport);
+		       (unsigned long *)&s->rport);
 		s->local.bytelen = s->remote.bytelen = 16;
 		return 0;
 	}
@@ -4638,8 +4638,8 @@ static int unix_show(struct filter *f)
 		if (!(u = calloc(1, sizeof(*u))))
 			break;
 
-		if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
-			   &u->rport, &u->rq, &u->wq, &flags, &u->type,
+		if (sscanf(buf, "%lx: %x %x %x %x %x %d %s",
+			   (unsigned long *)&u->rport, &u->rq, &u->wq, &flags, &u->type,
 			   &u->state, &u->ino, name) < 8)
 			name[0] = 0;
 
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH iproute2 v2 1/2] ss: move lport and rport in struct sockstat from int to long

Quoted 8 lines · On Mon, 17 Aug 2026 14:54:20 +0200 Luigi Leonardi <[email protected]> wrote:

Commit 012cb515 ("ss: change aafilter port from int to long (inode support)") widened aafilter.port to long so that unix socket inode numbers larger than INT_MAX can be used as port filter values, but left sockstat.lport and sockstat.rport as int. This means large u32 values (e.g. vsock ports or unix inodes > INT_MAX) are truncated on the socket-stat side, making the comparison against aafilter.port always fail.

Quoted 7 lines

Widen lport and rport to long and update the sscanf format specifiers in proc_parse_inet_addr() and unix_show() accordingly.

Fixes 012cb515 ("ss: change aafilter port from int to long (inode support)")

---

Applied to iproute2 but I had to fix the snprintf() warning and condense the overly verbose AI slop of a commit message.

#3 lore raw

PATCH v2 2/2 ss: fix vsock port filter

In reply to Luigi Leonardi

parse_hostcond() initializes aafilter.port to -1L (0xFFFFFFFFFFFFFFFF) as a sentinel for "no port filter". When parsing a vsock port it called get_u32() via a cast:

    get_u32((__u32 *)&a.port, port, 0)

get_u32() only writes 4 bytes, leaving the upper 32 bits of the 8-byte long set from the -1 initialization. For example, for port 27354 this produces 0xFFFFFFFF00006ADA, which never compares equal to sockstat.lport and causes all vsock port filters to silently match nothing.

Fix by parsing into a temporary __u32 and assigning it to a.port, which zero-extends the value correctly. When the user specifies '*' the if block is skipped entirely and the -1 sentinel is preserved.

Fixes 012cb515 ("ss: change aafilter port from int to long (inode support)")
---
 misc/ss.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
misc/ss.c +7 -3
index ff1a88de..a3570715 100644
@@ -2322,9 +2322,13 @@ void *parse_hostcond(char *addr, bool is_port)
 
 		port = find_port(addr, is_port);
 
-		if (port && strcmp(port, "*") &&
-		    get_u32((__u32 *)&a.port, port, 0))
-			return NULL;
+		if (port && strcmp(port, "*")) {
+			__u32 vport;
+
+			if (get_u32(&vport, port, 0))
+				return NULL;
+			a.port = vport;
+		}
 
 		if (!is_port && addr[0] && strcmp(addr, "*")) {
 			a.addr.bitlen = 32;
Signature · 1 line
2.55.0
#4 lore raw
In reply to Luigi Leonardi
This series was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This series was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 10 lines · On Mon, 17 Aug 2026 14:54:19 +0200 you wrote:

Vsock port filtering is broken for ports in [1, 2^31−1] (i.e. any port whose u32 representation has bit 31 clear, which includes all ordinarily assigned vsock ports).

To reproduce:

    ncat -l --vsock -p 27354
    ss --vsock -l src :27354   # shows nothing

[...]

Here is the summary with links:
- [iproute2,v2,1/2] ss: move lport and rport in struct sockstat from int to long (no matching commit)
- [iproute2,v2,2/2] ss: fix vsock port filter

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=8016d5a12e6c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html