From: Denis Kirjanov <kda@linux-powerpc.org>
To: netdev@vger.kernel.org
Cc: kuba@kernel.org, davem@davemloft.net
Subject: [PATCH v2] net/af_unix: don't create a path for a binded socket
Date: Mon, 30 Nov 2020 16:27:47 +0300 [thread overview]
Message-ID: <20201130132747.29332-1-kda@linux-powerpc.org> (raw)
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
Signed-off-by: Denis Kirjanov <kda@linux-powerpc.org>
v2: move a new patch creation after the address assignment check.
---
net/unix/af_unix.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 41c3303c3357..ff2dd1d3536b 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1034,6 +1034,14 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
goto out;
addr_len = err;
+ err = mutex_lock_interruptible(&u->bindlock);
+ if (err)
+ goto out_put;
+
+ err = -EINVAL;
+ if (u->addr)
+ goto out_up;
+
if (sun_path[0]) {
umode_t mode = S_IFSOCK |
(SOCK_INODE(sock)->i_mode & ~current_umask());
@@ -1045,14 +1053,6 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
}
}
- err = mutex_lock_interruptible(&u->bindlock);
- 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)
--
2.16.4
next reply other threads:[~2020-11-30 13:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-30 13:27 Denis Kirjanov [this message]
2020-12-01 1:30 ` [PATCH v2] net/af_unix: don't create a path for a binded socket Jakub Kicinski
2020-12-01 10:04 ` Michal Kubecek
2020-12-01 5:28 ` [net/af_unix] 556d816147: WARNING:lock_held_when_returning_to_user_space kernel test robot
2020-12-01 8:50 ` Denis Kirjanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201130132747.29332-1-kda@linux-powerpc.org \
--to=kda@linux-powerpc.org \
--cc=davem@davemloft.net \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).