• Kuniyuki Iwashima's avatar
    af_unix: Put pathname sockets in the global hash table. · 51bae889
    Kuniyuki Iwashima authored
    Commit cf2f225e ("af_unix: Put a socket into a per-netns hash table.")
    accidentally broke user API for pathname sockets.  A socket was able to
    connect() to a pathname socket whose file was visible even if they were in
    different network namespaces.
    
    The commit puts all sockets into a per-netns hash table.  As a result,
    connect() to a pathname socket in a different netns fails to find it in the
    caller's per-netns hash table and returns -ECONNREFUSED even when the task
    can view the peer socket file.
    
    We can reproduce this issue by:
    
      Console A:
    
        # python3
        >>> from socket import *
        >>> s = socket(AF_UNIX, SOCK_STREAM, 0)
        >>> s.bind('test')
        >>> s.listen(32)
    
      Console B:
    
        # ip netns add test
        # ip netns exec test sh
        # python3
        >>> from socket import *
        >>> s = socket(AF_UNIX, SOCK_STREAM, 0)
        >>> s.connect('test')
    
    Note when dumping sockets by sock_diag, procfs, and bpf_iter, they are
    filtered only by netns.  In other words, even if they are visible and
    connect()able, all sockets in different netns are skipped while iterating
    sockets.  Thus, we need a fix only for finding a peer pathname socket.
    
    This patch adds a global hash table for pathname sockets, links them with
    sk_bind_node, and uses it in unix_find_socket_byinode().  By doing so, we
    can keep sockets in per-netns hash tables and dump them easily.
    
    Thanks to Sachin Sant and Leonard Crestez for reports, logs and a reproducer.
    
    Fixes: cf2f225e ("af_unix: Put a socket into a per-netns hash table.")
    Reported-by: default avatarSachin Sant <sachinp@linux.ibm.com>
    Reported-by: default avatarLeonard Crestez <cdleonard@gmail.com>
    Tested-by: default avatarSachin Sant <sachinp@linux.ibm.com>
    Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
    Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
    Tested-by: default avatarLeonard Crestez <cdleonard@gmail.com>
    Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
    51bae889
af_unix.c 87.2 KB