Skip to contentSkip to conversation

iproute2: ss: escape all null bytes in abstract unix domain socket

PATCH Applied Isaac Boukris 4 participants 9 messages Oct 29, 2016 – Dec 5, 2016

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

PATCH 1/1 iproute2: ss: escape all null bytes in abstract unix domain socket

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed by ss the same way the null prefix is currently being translated.

---
 misc/ss.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
misc/ss.c +3 -1
index dd77b81..0e28998 100644
@@ -2895,7 +2895,9 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh,
 		memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
 		name[len] = '\0';
 		if (name[0] == '\0')
-			name[0] = '@';
+			for (int i = 0; i < len; i++)
+				if (name[i] == '\0')
+					name[i] = '@';
 		stat.name = &name[0];
 		memcpy(stat.local.data, &stat.name, sizeof(stat.name));
 	}
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 unix: escape all null bytes in abstract unix domain socket

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed out to proc the same way the null prefix is currently being translated.

This helps for tools such as netstat, lsof and the proc based implementation in ss to show all the significant bytes of the name (instead of getting cut at the first null occurrence).

---
 net/unix/af_unix.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
net/unix/af_unix.c +9 -4
index 145082e..9250b03 100644
@@ -2805,14 +2805,19 @@ static int unix_seq_show(struct seq_file *seq, void *v)
 
 			i = 0;
 			len = u->addr->len - sizeof(short);
-			if (!UNIX_ABSTRACT(s))
+			if (!UNIX_ABSTRACT(s)) {
 				len--;
-			else {
+				for ( ; i < len; i++)
+					seq_putc(seq,
+						 u->addr->name->sun_path[i]);
+			} else {
 				seq_putc(seq, '@');
 				i++;
+				for ( ; i < len; i++)
+					seq_putc(seq,
+						 u->addr->name->sun_path[i] ?:
+						 '@');
 			}
-			for ( ; i < len; i++)
-				seq_putc(seq, u->addr->name->sun_path[i]);
 		}
 		unix_state_unlock(s);
 		seq_putc(seq, '\n');
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH] unix: escape all null bytes in abstract unix domain socket

From: Isaac Boukris <[email protected]>
Date: Sat, 29 Oct 2016 22:20:20 +0300

Quoted 11 lines

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed out to proc the same way the null prefix is currently being translated.

This helps for tools such as netstat, lsof and the proc based implementation in ss to show all the significant bytes of the name (instead of getting cut at the first null occurrence).

...

Quoted 11 lines
net/unix/af_unix.c +4 -2
@@ -2805,14 +2805,19 @@ static int unix_seq_show(struct seq_file *seq, void *v)
 
 			i = 0;
 			len = u->addr->len - sizeof(short);
-			if (!UNIX_ABSTRACT(s))
+			if (!UNIX_ABSTRACT(s)) {
 				len--;
-			else {
+				for ( ; i < len; i++)
+					seq_putc(seq,
+						 u->addr->name->sun_path[i]);

net/unix/af_unix.c static int unix_seq_show(struct seq_file *seq, void *v) · line 2815

Quoted 10 lines
net/unix/af_unix.c +5 -2
+			} else {
 				seq_putc(seq, '@');
 				i++;
+				for ( ; i < len; i++)
+					seq_putc(seq,
+						 u->addr->name->sun_path[i] ?:
+						 '@');
 			}
-			for ( ; i < len; i++)
-				seq_putc(seq, u->addr->name->sun_path[i]);

I think this patch is simpler if you just do the "@" translation unconditionally, if it'll never trigger for the !UNIX_ABSTRACT case that is perfectly fine.

#3 lore raw

Re: [PATCH] unix: escape all null bytes in abstract unix domain socket

Hi David, thanks for looking at it.

Quoted 32 lines · David Miller

From: Isaac Boukris <[email protected]>
Date: Sat, 29 Oct 2016 22:20:20 +0300

Quoted 11 lines

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed out to proc the same way the null prefix is currently being translated.

This helps for tools such as netstat, lsof and the proc based implementation in ss to show all the significant bytes of the name (instead of getting cut at the first null occurrence).

...

Quoted 17 lines
net/unix/af_unix.c +8 -2
@@ -2805,14 +2805,19 @@ static int unix_seq_show(struct seq_file *seq, void *v)
 
                      i = 0;
                      len = u->addr->len - sizeof(short);
-                     if (!UNIX_ABSTRACT(s))
+                     if (!UNIX_ABSTRACT(s)) {
                              len--;
-                     else {
+                             for ( ; i < len; i++)
+                                     seq_putc(seq,
+                                              u->addr->name->sun_path[i]);
+                     } else {
                              seq_putc(seq, '@');
                              i++;
+                             for ( ; i < len; i++)
+                                     seq_putc(seq,
+                                              u->addr->name->sun_path[i] ?:
Quoted 8 lines

net/unix/af_unix.c static int unix_seq_show(struct seq_file *seq, void *v) · line 2815

Quoted 4 lines
net/unix/af_unix.c +1 -2
+                                              '@');
                      }
-                     for ( ; i < len; i++)
-                             seq_putc(seq, u->addr->name->sun_path[i]);

I think this patch is simpler if you just do the "@" translation unconditionally, if it'll never trigger for the !UNIX_ABSTRACT case that is perfectly fine.

I've sent an updated patch.
Logically now, the 'else' block just above could be removed, but it isn't obvious from the code that 'sun_path[0] == 0' so I left it as is.

#5 lore raw
Quoted 10 lines · Stephen Hemminger
Quoted 5 lines · On Sat, 29 Oct 2016 22:20:19 +0300 Isaac Boukris <[email protected]> wrote:

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed by ss the same way the null prefix is currently being translated.

Applied

Probably not a good idea to have :

                       for (int i = 0; i < len; i++)
                               if (name[i] == '\0')
                                       name[i] = '@';

ss.c: In function 'unix_show_sock':
ss.c:3128:4: error: 'for' loop initial declarations are only allowed in C99 mode ss.c:3128:4: note: use option -std=c99 or -std=gnu99 to compile your code make[1]: *** [ss.o] Error 1

#6 lore raw
Quoted 14 lines · On Fri, 02 Dec 2016 10:59:56 -0800 Eric Dumazet <[email protected]> wrote:
Quoted 10 lines · Stephen Hemminger
Quoted 5 lines · On Sat, 29 Oct 2016 22:20:19 +0300 Isaac Boukris <[email protected]> wrote:

Abstract unix domain socket may embed null characters, these should be translated to '@' when printed by ss the same way the null prefix is currently being translated.

Applied

Probably not a good idea to have :

Quoted 11 lines
                       for (int i = 0; i < len; i++)
                               if (name[i] == '\0')
                                       name[i] = '@';

ss.c: In function 'unix_show_sock':
ss.c:3128:4: error: 'for' loop initial declarations are only allowed in C99 mode ss.c:3128:4: note: use option -std=c99 or -std=gnu99 to compile your code make[1]: *** [ss.o] Error 1

Thanks, fixed by patch from Simon

#7 lore raw
Quoted 11 lines · Stephen Hemminger
                                    name[i] = '@';
Quoted 8 lines

ss.c: In function 'unix_show_sock':
ss.c:3128:4: error: 'for' loop initial declarations are only allowed in C99 mode ss.c:3128:4: note: use option -std=c99 or -std=gnu99 to compile your code make[1]: *** [ss.o] Error 1

Thanks, fixed by patch from Simon

Right, thanks !

#8 lore raw
Quoted 11 lines · Eric Dumazet
Quoted 10 lines · Stephen Hemminger
                                    name[i] = '@';
Quoted 8 lines

ss.c: In function 'unix_show_sock':
ss.c:3128:4: error: 'for' loop initial declarations are only allowed in C99 mode ss.c:3128:4: note: use option -std=c99 or -std=gnu99 to compile your code make[1]: *** [ss.o] Error 1

Quoted 3 lines
Quoted 1 line

Thanks, fixed by patch from Simon

Right, thanks !

Thanks for notifying me. Sorry for the bug and thanks for the fix!