]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: read the UNIX socket path including white spaces correctly
authorMasatake YAMATO <yamato@redhat.com>
Mon, 13 Feb 2023 19:03:27 +0000 (04:03 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Tue, 14 Feb 2023 20:52:58 +0000 (05:52 +0900)
The original code uses '%s' scanf specifier to read lines in /proc/net/unix.
If white space is included in a path, lsfd could read only the first word of the path.
This commit fixes this bug.

Specifying the maximum length of path is suggested by Karel Zak <kzak@redhat.com>.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-sock-xinfo.c
tests/expected/lsfd/mkfds-unix-stream
tests/ts/lsfd/mkfds-unix-stream

index 749a41102639975f0c0b95d90b63b75b99b7023a..2316fc90ec8eab52232000734e0301e89e447f1c 100644 (file)
@@ -347,7 +347,12 @@ static const struct sock_xinfo_class unix_xinfo_class = {
        .free = NULL,
 };
 
-/* #define UNIX_LINE_LEN 54 + 21 + UNIX_LINE_LEN + 1 */
+/* #define UNIX_LINE_LEN 54 + 21 + UNIX_LINE_LEN + 1
+ *
+ * An actual number must be used in this definition
+ * since UNIX_LINE_LEN is specified as an argument for
+ * stringify_value().
+ */
 #define UNIX_LINE_LEN 256
 static void load_xinfo_from_proc_unix(ino_t netns_inode)
 {
@@ -370,10 +375,11 @@ static void load_xinfo_from_proc_unix(ino_t netns_inode)
                unsigned int st;
                unsigned long inode;
                struct unix_xinfo *ux;
-               char path[UNIX_LINE_LEN] = { 0 };
+               char path[UNIX_LINE_LEN + 1] = { 0 };
 
 
-               if (sscanf(line, "%*x: %*x %*x %" SCNx64 " %x %x %lu %s",
+               if (sscanf(line, "%*x: %*x %*x %" SCNx64 " %x %x %lu %"
+                          stringify_value(UNIX_LINE_LEN) "[^\n]",
                           &flags, &type, &st, &inode, path) < 4)
                        continue;
 
index 281467a8719aaa9dbab61ae32c03b4a6faaea30d..c2e64a780de4d96266f246fa61dfa34545203cca 100644 (file)
@@ -10,6 +10,10 @@ ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
     4   SOCK state=connected                                       connected    stream              0 
     5   SOCK state=connected path=test_mkfds-unix-stream-shutdown  connected    stream              0 test_mkfds-unix-stream-shutdown
 (shutdown) ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
+    3   SOCK state=listen path=test_mkfds-unix with spaces stream        listen    stream              1 test_mkfds-unix with spaces stream
+    4   SOCK state=connected                                          connected    stream              0 
+    5   SOCK state=connected path=test_mkfds-unix with spaces stream  connected    stream              0 test_mkfds-unix with spaces stream
+ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
     3   SOCK state=listen path=test_mkfds-unix-seqpacket type=seqpacket        listen seqpacket              1 test_mkfds-unix-seqpacket
     4   SOCK state=connected type=seqpacket                                 connected seqpacket              0 
     5   SOCK state=connected path=test_mkfds-unix-seqpacket type=seqpacket  connected seqpacket              0 test_mkfds-unix-seqpacket
@@ -22,3 +26,7 @@ ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
     4   SOCK state=connected type=seqpacket                                          connected seqpacket              0 
     5   SOCK state=connected path=test_mkfds-unix-seqpacket-shutdown type=seqpacket  connected seqpacket              0 test_mkfds-unix-seqpacket-shutdown
 (shutdown) ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
+    3   SOCK state=listen path=test_mkfds-unix with spaces seqpacket type=seqpacket        listen seqpacket              1 test_mkfds-unix with spaces seqpacket
+    4   SOCK state=connected type=seqpacket                                             connected seqpacket              0 
+    5   SOCK state=connected path=test_mkfds-unix with spaces seqpacket type=seqpacket  connected seqpacket              0 test_mkfds-unix with spaces seqpacket
+ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH: 0
index 3d7b31890461140a8b4da04be3da72a80d4f2439..0bc69aeac6caa1bf3b769843f97b583e645a2b94 100755 (executable)
@@ -76,6 +76,20 @@ EXPR='(((TYPE == "UNIX-STREAM") or (TYPE == "UNIX")) and (FD >= 3) and (FD <= 5)
            kill -CONT "${PID}"
            wait "${MKFDS_PID}"
        fi
+
+       coproc MKFDS { "$TS_HELPER_MKFDS" unix-stream $FDS $FDC $FDA \
+                                         path="test_mkfds-unix with spaces ${t}" \
+                                         type=$t ; }
+       if read -r -u "${MKFDS[0]}" PID; then
+           ${TS_CMD_LSFD} -n \
+                          -o ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH \
+                          -p "${PID}" -Q "${EXPR}"
+           echo 'ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH': $?
+
+           kill -CONT "${PID}"
+           wait "${MKFDS_PID}"
+       fi
+
     done
 } > "$TS_OUTPUT" 2>&1