Fix segv on "-r" option if unknown rpc service
Subsystems (2)
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
@@ -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]>
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
@@ -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;
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 reinventingasprintf().
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@@ -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]>