Netdev List
 help / color / mirror / Atom feed
From: Prabhakar Pujeri <prabhakar.pujeri@dell.com>
To: netdev@vger.kernel.org
Cc: Prabhakar Pujeri <prabhakar.pujeri@dell.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 v2] ss: drop the /proc parsers for packet and netlink sockets
Date: Wed,  2 Sep 2026 03:56:33 +0000	[thread overview]
Message-ID: <20260902035633.1978-1-prabhakar.pujeri@dell.com> (raw)

packet_show() and netlink_show() fall back to parsing
/proc/net/packet and /proc/net/netlink only when the sock_diag
netlink dump fails.  That fallback exists for kernels without
sock_diag support and parses each record with sscanf() without
checking the conversion count, so truncated or malformed lines
leave fields used by filtering and printing uninitialized.

Remove both sscanf() based parsers instead of trying to validate
them; kernels new enough to matter answer the sock_diag request.
packet_show_line() and the PROC_NET_PACKET/PROC_NET_NETLINK hooks
have no other users.

Signed-off-by: Prabhakar Pujeri <prabhakar.pujeri@dell.com>
---
Changes in v2:
- Replace the sscanf() validation series: per Stephen's review of v1, remove
  the /proc fallback parsing for packet and netlink sockets instead of
  hardening it, so the unchecked conversions are gone with the code.
v1: https://lore.kernel.org/netdev/20260831104116.2190-1-prabhakar.pujeri@dell.com/

 misc/ss.c | 79 ++-----------------------------------------------------
 1 file changed, 2 insertions(+), 77 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index 26520cee..6ca6d406 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -480,10 +480,6 @@ static void filter_merge_defaults(struct filter *f)
 #define net_raw_open()		generic_proc_open("PROC_NET_RAW", "net/raw")
 #define net_raw6_open()		generic_proc_open("PROC_NET_RAW6", "net/raw6")
 #define net_unix_open()		generic_proc_open("PROC_NET_UNIX", "net/unix")
-#define net_packet_open()	generic_proc_open("PROC_NET_PACKET", \
-							"net/packet")
-#define net_netlink_open()	generic_proc_open("PROC_NET_NETLINK", \
-							"net/netlink")
 #define net_sockstat_open()	generic_proc_open("PROC_NET_SOCKSTAT", \
 							"net/sockstat")
 #define net_sockstat6_open()	generic_proc_open("PROC_NET_SOCKSTAT6", \
@@ -4916,56 +4912,12 @@ static int packet_show_netlink(struct filter *f)
 	return handle_netlink_request(f, &req.nlh, sizeof(req), packet_show_sock);
 }
 
-static int packet_show_line(char *buf, const struct filter *f, int fam)
-{
-	unsigned long long sk;
-	struct sockstat stat = {};
-	int type, prot, iface, state, rq, uid, ino;
-
-	sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
-			&sk,
-			&type, &prot, &iface, &state,
-			&rq, &uid, &ino);
-
-	if (type == SOCK_RAW && !(f->dbs & (1<<PACKET_R_DB)))
-		return 0;
-	if (type == SOCK_DGRAM && !(f->dbs & (1<<PACKET_DG_DB)))
-		return 0;
-
-	stat.type  = type;
-	stat.prot  = prot;
-	stat.lport = stat.iface = iface;
-	stat.state = state;
-	stat.rq    = rq;
-	stat.uid   = uid;
-	stat.ino   = ino;
-	stat.state = SS_CLOSE;
-
-	if (packet_stats_print(&stat, f))
-		return 0;
-
-	return 0;
-}
-
 static int packet_show(struct filter *f)
 {
-	FILE *fp;
-	int rc = 0;
-
 	if (!filter_af_get(f, AF_PACKET) || !(f->states & (1 << SS_CLOSE)))
 		return 0;
 
-	if (!getenv("PROC_NET_PACKET") && !getenv("PROC_ROOT") &&
-			packet_show_netlink(f) == 0)
-		return 0;
-
-	if ((fp = net_packet_open()) == NULL)
-		return -1;
-	if (generic_record_read(fp, packet_show_line, f, AF_PACKET))
-		rc = -1;
-
-	fclose(fp);
-	return rc;
+	return packet_show_netlink(f);
 }
 
 static int xdp_stats_print(struct sockstat *s, const struct filter *f)
@@ -5276,37 +5228,10 @@ static int netlink_show_netlink(struct filter *f)
 
 static int netlink_show(struct filter *f)
 {
-	FILE *fp;
-	char buf[256];
-	int prot, pid;
-	unsigned int groups;
-	int rq, wq, rc;
-	unsigned long long sk, cb;
-
 	if (!filter_af_get(f, AF_NETLINK) || !(f->states & (1 << SS_CLOSE)))
 		return 0;
 
-	if (!getenv("PROC_NET_NETLINK") && !getenv("PROC_ROOT") &&
-		netlink_show_netlink(f) == 0)
-		return 0;
-
-	if ((fp = net_netlink_open()) == NULL)
-		return -1;
-	if (!fgets(buf, sizeof(buf), fp)) {
-		fclose(fp);
-		return -1;
-	}
-
-	while (fgets(buf, sizeof(buf), fp)) {
-		sscanf(buf, "%llx %d %d %x %d %d %llx %d",
-		       &sk,
-		       &prot, &pid, &groups, &rq, &wq, &cb, &rc);
-
-		netlink_show_one(f, prot, pid, groups, 0, 0, 0, rq, wq, sk, cb);
-	}
-
-	fclose(fp);
-	return 0;
+	return netlink_show_netlink(f);
 }
 
 static bool vsock_type_skip(struct sockstat *s, struct filter *f)
-- 
2.55.0


                 reply	other threads:[~2026-09-02  3:56 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=20260902035633.1978-1-prabhakar.pujeri@dell.com \
    --to=prabhakar.pujeri@dell.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.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