Skip to contentSkip to conversation

Fix segv on "-r" option if unknown rpc service

PATCH In review OGAWA Hirofumi 2 participants 3 messages Sep 17, 2022 – Sep 22, 2022

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

PATCH 1/1 Fix segv on "-r" option if unknown rpc service

In init_service_resolver(), if getrpcbynumber() returned NULL, c->name is pointing the undefined memory. So "-r" can segv for example.

So this patch uses "rpc.<r_prog>" format like the following if rpc name is unresolved, instead of segv or raw port number.

	tcp   LISTEN  0  64  0.0.0.0:rpc.100227  0.0.0.0:*

[Or we would be able to set c->name = NULL to use raw port number]

---
 misc/ss.c |    9 +++++++++
 1 file changed, 9 insertions(+)
misc/ss.c +9 -0
index ff985cd..866e278 100644
@@ -1596,6 +1596,15 @@ static void init_service_resolver(void)
 		if (rpc) {
 			strncat(prog, rpc->r_name, 128 - strlen(prog));
 			c->name = strdup(prog);
+		} else {
+			const char fmt[] = "%s%u";
+			char *buf = NULL;
+			int len = snprintf(buf, 0, fmt, prog,
+					   rhead->rpcb_map.r_prog);
+			len++;
+			buf = malloc(len);
+			snprintf(buf, len, fmt, prog, rhead->rpcb_map.r_prog);
+			c->name = buf;
 		}
 
 		c->next = rlist;

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#1 lore raw

Review

Quoted 10 lines · On Sun, 18 Sep 2022 02:50:54 +0900 OGAWA Hirofumi…
(quoted diff) +9 -0
+		} else {
+			const char fmt[] = "%s%u";
+			char *buf = NULL;
+			int len = snprintf(buf, 0, fmt, prog,
+					   rhead->rpcb_map.r_prog);
+			len++;
+			buf = malloc(len);
+			snprintf(buf, len, fmt, prog, rhead->rpcb_map.r_prog);
+			c->name = buf;
 		}

Thanks for finding the bug but this could be improved. This seems like the hard way to do this.
You are reinventing asprintf().

Would this work instead.

misc/ss.c +4 -0
index ff985cd8cae9..9d3d0bd84df3 100644
@@ -1596,6 +1596,10 @@ static void init_service_resolver(void)
                if (rpc) {
                        strncat(prog, rpc->r_name, 128 - strlen(prog));
                        c->name = strdup(prog);
+               } else if (asprintf(&c->name, "%s%u",
+                                   prog, rhead->rpcb_map.r_prog) < 0) {
+                       fprintf(stderr, "ss: asprintf failed to allocate buffer\n");
+                       abort();
                }
 
                c->next = rlist;
#2 lore raw
Quoted 17 lines · Stephen Hemminger

Review

Quoted 10 lines · On Sun, 18 Sep 2022 02:50:54 +0900 OGAWA Hirofumi…
(quoted diff) +9 -0
+		} else {
+			const char fmt[] = "%s%u";
+			char *buf = NULL;
+			int len = snprintf(buf, 0, fmt, prog,
+					   rhead->rpcb_map.r_prog);
+			len++;
+			buf = malloc(len);
+			snprintf(buf, len, fmt, prog, rhead->rpcb_map.r_prog);
+			c->name = buf;
 		}

Thanks for finding the bug but this could be improved. This seems like the hard way to do this.
You are reinventing asprintf().

Right, if this project is assuming the extension is available.

Quoted 1 line

Would this work instead.

Thanks.

Quoted 15 lines
misc/ss.c +4 -0
index ff985cd8cae9..9d3d0bd84df3 100644
@@ -1596,6 +1596,10 @@ static void init_service_resolver(void)
                if (rpc) {
                        strncat(prog, rpc->r_name, 128 - strlen(prog));
                        c->name = strdup(prog);
+               } else if (asprintf(&c->name, "%s%u",
+                                   prog, rhead->rpcb_map.r_prog) < 0) {
+                       fprintf(stderr, "ss: asprintf failed to allocate buffer\n");
+                       abort();
                }
 
                c->next = rlist;
Signature · 1 line
OGAWA Hirofumi <[email protected]>