Skip to contentSkip to conversation

net/af_unix: don't create a path for a binded socket

PATCH Superseded Denis Kirjanov 2 participants 4 messages Nov 24, 2020 – Nov 27, 2020

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/af_unix: don't create a path for a binded socket

in the case of the socket which is 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

---
 net/unix/af_unix.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
net/unix/af_unix.c +1 -5
index 41c3303c3357..fd76a8fe3907 100644
@@ -1021,7 +1021,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 
 	err = -EINVAL;
 	if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
-	    sunaddr->sun_family != AF_UNIX)
+	    sunaddr->sun_family != AF_UNIX || u->addr)
 		goto out;
 
 	if (addr_len == sizeof(short)) {
@@ -1049,10 +1049,6 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 	if (err)
 		goto out_put;
 
-	err = -EINVAL;
-	if (u->addr)
-		goto out_up;
-
 	err = -ENOMEM;
 	addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
 	if (!addr)
Signature · 1 line
2.16.4
maintainer
#1 lore raw
Quoted 17 lines · On Tue, 24 Nov 2020 15:24:21 +0300 Denis Kirjanov wrote:

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

net/unix/af_unix.c +1 -1
index 41c3303c3357..fd76a8fe3907 100644
@@ -1021,7 +1021,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 
 	err = -EINVAL;
 	if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
-	    sunaddr->sun_family != AF_UNIX)
+	    sunaddr->sun_family != AF_UNIX || u->addr)
 		goto out;
 
 	if (addr_len == sizeof(short)) {

net/unix/af_unix.c static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) · line 1054

Quoted 10 lines
net/unix/af_unix.c +0 -4
@@ -1049,10 +1049,6 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 	if (err)
 		goto out_put;
 
-	err = -EINVAL;
-	if (u->addr)
-		goto out_up;
-
 	err = -ENOMEM;
 	addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);
 	if (!addr)

Well, after your change the check on u->addr is no longer protected by u->bindlock. Is that okay?

#2 lore raw
Quoted 23 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 24 Nov 2020 15:24:21 +0300 Denis Kirjanov wrote:

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

net/unix/af_unix.c +1 -1
index 41c3303c3357..fd76a8fe3907 100644
@@ -1021,7 +1021,7 @@ static int unix_bind(struct socket *sock, struct
 sockaddr *uaddr, int addr_len)
 
 	err = -EINVAL;
 	if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
-	    sunaddr->sun_family != AF_UNIX)
+	    sunaddr->sun_family != AF_UNIX || u->addr)
 		goto out;
 
net/unix/af_unix.c +0 -0
 	if (addr_len == sizeof(short)) {
@@ -1049,10 +1049,6 @@ static int unix_bind(struct socket *sock, struct
 sockaddr *uaddr, int addr_len)
 	if (err)
 		goto out_put;
 
Quoted 10 lines
Quoted 7 lines
net/unix/af_unix.c +0 -4
-	err = -EINVAL;
-	if (u->addr)
-		goto out_up;
-
 	err = -ENOMEM;
 	addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);

if (!addr)

Well, after your change the check on u->addr is no longer protected by u->bindlock. Is that okay?

Since we're just checking the assigned address and it's an atomic operation I think it's okay.
A process performing binding is still protected.

Thanks!

Quoted 1 line
maintainer
#3 lore raw
Quoted 35 lines · On Thu, 26 Nov 2020 17:22:08 +0300 Denis Kirjanov wrote:
Quoted 23 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 24 Nov 2020 15:24:21 +0300 Denis Kirjanov wrote:

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

net/unix/af_unix.c +1 -1
index 41c3303c3357..fd76a8fe3907 100644
@@ -1021,7 +1021,7 @@ static int unix_bind(struct socket *sock, struct
 sockaddr *uaddr, int addr_len)
 
 	err = -EINVAL;
 	if (addr_len < offsetofend(struct sockaddr_un, sun_family) ||
-	    sunaddr->sun_family != AF_UNIX)
+	    sunaddr->sun_family != AF_UNIX || u->addr)
 		goto out;
 
net/unix/af_unix.c +0 -0
 	if (addr_len == sizeof(short)) {
@@ -1049,10 +1049,6 @@ static int unix_bind(struct socket *sock, struct
 sockaddr *uaddr, int addr_len)
 	if (err)
 		goto out_put;
 
Quoted 10 lines
Quoted 7 lines
net/unix/af_unix.c +0 -4
-	err = -EINVAL;
-	if (u->addr)
-		goto out_up;
-
 	err = -ENOMEM;
 	addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL);

if (!addr)

Well, after your change the check on u->addr is no longer protected by u->bindlock. Is that okay?

Quoted 2 lines

Since we're just checking the assigned address and it's an atomic operation I think it's okay.

The access to the variable may be atomic, but what protects two concurrent binds() from progressing past the check and binding to different paths?

I don't know this code at all, but looks to me like the pattern is basically:

	lock()
	if (obj->thing)
		goto err; /* already bound to a thing */

	thing = alloc()
	setup_thing(thing);

	obj->thing = thing;

err:
unlock()

Quoted 1 line

A process performing binding is still protected.

Isn't checking "did someone already bind" not part of the process of binding?