[PATCH v5] net/af_unix: don't create a path for a bound socket

Subsystems: networking [general], networking [unix sockets], the rest

STALE2031d

2 messages, 2 authors, 2021-01-19 · open the first message on its own page

[PATCH v5] net/af_unix: don't create a path for a bound socket

From: Denis Kirjanov <hidden>
Date: 2021-01-19 14:07:34

In the case of a socket which is already bound to an adress
there is no sense to create a path in the next attempts

here is a program that shows the issue:

int main()
{
    int s;
    struct sockaddr_un a;

    s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s<0)
        perror("socket() failed\n");

    printf("First bind()\n");

    memset(&a, 0, sizeof(a));
    a.sun_family = AF_UNIX;
    strncpy(a.sun_path, "/tmp/.first_bind", sizeof(a.sun_path));

    if ((bind(s, (const struct sockaddr*) &a, sizeof(a))) == -1)
        perror("bind() failed\n");

    printf("Second bind()\n");

    memset(&a, 0, sizeof(a));
    a.sun_family = AF_UNIX;
    strncpy(a.sun_path, "/tmp/.first_bind_failed", sizeof(a.sun_path));

    if ((bind(s, (const struct sockaddr*) &a, sizeof(a))) == -1)
        perror("bind() failed\n");
}

kda@SLES15-SP2:~> ./test
First bind()
Second bind()
bind() failed
: Invalid argument

kda@SLES15-SP2:~> ls -la /tmp/.first_bind
.first_bind         .first_bind_failed

Signed-off-by: Denis Kirjanov <redacted>
---
 net/unix/af_unix.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 41c3303c3357..d8b1cfd872a3 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1091,8 +1091,23 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 out_up:
 	mutex_unlock(&u->bindlock);
 out_put:
-	if (err)
+	if (err) {
+		struct path parent = { };
+		struct dentry *dentry;
+		int ret;
+
+		dentry = kern_path_locked(sun_path, &parent);
+		if (IS_ERR(dentry))
+			return PTR_ERR(dentry);
+
+		ret = vfs_unlink(d_inode(parent.dentry), path.dentry, NULL);
+		if (ret)
+			err = ret;
+		dput(path.dentry);
+		inode_unlock(d_inode(parent.dentry));
+		path_put(&parent);
 		path_put(&path);
+	}
 out:
 	return err;
 }
-- 
2.16.4

Re: [PATCH v5] net/af_unix: don't create a path for a bound socket

From: Jakub Kicinski <kuba@kernel.org>
Date: 2021-01-19 22:27:48

On Tue, 19 Jan 2021 14:24:46 +0300 Denis Kirjanov wrote:
In the case of a socket which is already bound to an adress
there is no sense to create a path in the next attempts

here is a program that shows the issue:
Appears to not build in allmodconfig net-next:

ERROR: modpost: "kern_path_locked" [net/unix/unix.ko] undefined!
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help