Skip to contentSkip to conversation

ss: fix vsock port filter

PATCH In review Luigi Leonardi 3 participants 8 messages Apr 21 – Apr 23

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

PATCH 1/1 ss: fix vsock port filter

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")
---
 misc/ss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +1 -1
index 14e9f27a..6e3321ac 100644
@@ -2323,7 +2323,7 @@ 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))
+		    get_long(&a.port, port, 0))
 			return NULL;
 
 		if (!is_port && addr[0] && strcmp(addr, "*")) {

---
base-commit: e0517e612199cacaf2dc4d54cbed52deec640c94
change-id: 20260421-fix_vsock-40c2ef4928aa

Best regards,

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

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Quoted 6 lines

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")

---

Apparently this fixes sport but breaks dport filtering. Will send a v2. Please ignore this patch.

Luigi

#2 lore raw
In reply to Luigi Leonardi
Quoted 11 lines · Luigi Leonardi

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")

Can this more related to commit 012cb515 ("ss: change aafilter port from int to long (inode support)") ?

I don't know this code at all, just asking.

Stefano

Quoted 27 lines

---
misc/ss.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +1 -2
index 14e9f27a..6e3321ac 100644
@@ -2323,7 +2323,7 @@ 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))
+		    get_long(&a.port, port, 0))
 			return NULL;
 
 		if (!is_port && addr[0] && strcmp(addr, "*")) {
 
---

base-commit: e0517e612199cacaf2dc4d54cbed52deec640c94
change-id: 20260421-fix_vsock-40c2ef4928aa

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#3 lore raw
Quoted 11 lines · Stefano Garzarella
Quoted 10 lines · Luigi Leonardi

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Quoted 7 lines
Quoted 1 line
Fixes c759116a0b2b ("ss: add AF_VSOCK support")

Can this more related to commit 012cb515 ("ss: change aafilter port from int to long (inode support)") ?

I don't know this code at all, just asking.

Stefano

oh yes, you are right!

Luigi

#4 lore raw
In reply to Luigi Leonardi
Quoted 21 lines · On Tue, 21 Apr 2026 14:35:12 +0200 Luigi Leonardi <[email protected]> wrote:

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")
---
 misc/ss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +0 -0
index 14e9f27a..6e3321ac 100644

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2327

Quoted 6 lines
misc/ss.c +1 -1
@@ -2323,7 +2323,7 @@ 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))
+		    get_long(&a.port, port, 0))
 			return NULL;

If you use get_long() then the code could get negative values. Actually have port in ss as signed value seems like a mistake in original design.

The port in unix domain socket is inode number. Originally it was int, but got changed to long back in 6.6

The port in ss cache is int.

The ss code is one of those legacy dog piles that needs a major overhaul and refactoring.

#5 lore raw
Quoted 35 lines · Stephen Hemminger
Quoted 21 lines · On Tue, 21 Apr 2026 14:35:12 +0200 Luigi Leonardi <[email protected]> wrote:

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")
---
 misc/ss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +0 -0
index 14e9f27a..6e3321ac 100644

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2327

Quoted 6 lines
misc/ss.c +1 -1
@@ -2323,7 +2323,7 @@ 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))
+                 get_long(&a.port, port, 0))
                      return NULL;

If you use get_long() then the code could get negative values. Actually have port in ss as signed value seems like a mistake in original design.

Quoted 4 lines

The port in unix domain socket is inode number. Originally it was int, but got changed to long back in 6.6

The port in ss cache is int.

Yeah, as I mentioned I think the issue was introduced by commit 012cb515 ("ss: change aafilter port from int to long (inode support)").

After teverting it, the filtering on AF_VSOCK works correctly.

IMO (I don't know the code at all), commit 012cb515 is incomplete and we should also move from int to long lport and rport in struct sockstat.

Quoted 4 lines

The ss code is one of those legacy dog piles that needs a major overhaul and refactoring.

yeah, I see...

Thanks,
Stefano

#6 lore raw
Quoted 36 lines · On Wed, 22 Apr 2026 10:03:49 +0200 Stefano Garzarella <[email protected]>…
Quoted 35 lines · Stephen Hemminger
Quoted 21 lines · On Tue, 21 Apr 2026 14:35:12 +0200 Luigi Leonardi <[email protected]> wrote:

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")
---
 misc/ss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +0 -0
index 14e9f27a..6e3321ac 100644

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2327

Quoted 6 lines
misc/ss.c +1 -1
@@ -2323,7 +2323,7 @@ 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))
+                 get_long(&a.port, port, 0))
                      return NULL;  

If you use get_long() then the code could get negative values. Actually have port in ss as signed value seems like a mistake in original design.

Quoted 7 lines
Quoted 4 lines

The port in unix domain socket is inode number. Originally it was int, but got changed to long back in 6.6

The port in ss cache is int.

Yeah, as I mentioned I think the issue was introduced by commit 012cb515 ("ss: change aafilter port from int to long (inode support)").

What about this which avoids the cast but keeps the same semantics.

misc/ss.c +6 -4
index 14e9f27a..e830e146 100644
@@ -2317,14 +2317,16 @@ void *parse_hostcond(char *addr, bool is_port)
 
 	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
+		__u32 vport = 0;
 
 		a.addr.family = AF_VSOCK;
 
 		port = find_port(addr, is_port);
-
-		if (port && strcmp(port, "*") &&
-		    get_u32((__u32 *)&a.port, port, 0))
-			return NULL;
+		if (port && strcmp(port, "*")) {
+			if (get_u32(&vport, port, 0))
+				return NULL;
+		}
+		a.port = vport;
 
 		if (!is_port && addr[0] && strcmp(addr, "*")) {
 			a.addr.bitlen = 32;
#7 lore raw
Quoted 66 lines · Stephen Hemminger
Quoted 36 lines · On Wed, 22 Apr 2026 10:03:49 +0200 Stefano Garzarella <[email protected]>…
Quoted 35 lines · Stephen Hemminger
Quoted 21 lines · On Tue, 21 Apr 2026 14:35:12 +0200 Luigi Leonardi <[email protected]> wrote:

parse_hostcond() uses get_u32() to parse the vsock port into the aafilter.port field, which is a long. On 64-bit systems, get_u32() only writes the lower 32 bits, leaving the upper 32 bits set from the -1 initialization. This causes the port comparison "a->port != s->rport" in run_ssfilter() to always fail, since the corrupted long value never matches the int rport.

Fix by using get_long() instead, consistent with how AF_PACKET and AF_NETLINK handle the same field.

Fixes c759116a0b2b ("ss: add AF_VSOCK support")
---
 misc/ss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/ss.c +0 -0
index 14e9f27a..6e3321ac 100644

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2327

Quoted 6 lines
misc/ss.c +1 -1
@@ -2323,7 +2323,7 @@ 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))
+                 get_long(&a.port, port, 0))
                      return NULL;

If you use get_long() then the code could get negative values. Actually have port in ss as signed value seems like a mistake in original design.

Quoted 7 lines
Quoted 4 lines

The port in unix domain socket is inode number. Originally it was int, but got changed to long back in 6.6

The port in ss cache is int.

Yeah, as I mentioned I think the issue was introduced by commit 012cb515 ("ss: change aafilter port from int to long (inode support)").

What about this which avoids the cast but keeps the same semantics.

misc/ss.c +1 -2
index 14e9f27a..e830e146 100644
@@ -2317,14 +2317,16 @@ void *parse_hostcond(char *addr, bool is_port)
 
 	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
+		__u32 vport = 0;
 
 		a.addr.family = AF_VSOCK;
 
 		port = find_port(addr, is_port);
-
-		if (port && strcmp(port, "*") &&

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2332

Quoted 10 lines
misc/ss.c +5 -2
-		    get_u32((__u32 *)&a.port, port, 0))
-			return NULL;
+		if (port && strcmp(port, "*")) {
+			if (get_u32(&vport, port, 0))
+				return NULL;
+		}
+		a.port = vport;
 
 		if (!is_port && addr[0] && strcmp(addr, "*")) {
 			a.addr.bitlen = 32;

With high enough ports it's not working, I expect to be a problem with the sign. I'll try updating struct sockstat as Stefano suggested.

Luigi