Commit d2d8c9fd authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Jakub Kicinski

af_unix: Copy unix_mkname() into unix_find_(bsd|abstract)().

We should not call unix_mkname() before unix_find_other() and instead do
the same thing where necessary based on the address type:

  - terminating the address with '\0' in unix_find_bsd()
  - calculating the hash in unix_find_abstract().
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.co.jp>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent b8a58aa6
...@@ -239,19 +239,25 @@ static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len) ...@@ -239,19 +239,25 @@ static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len)
return 0; return 0;
} }
static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp) static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len)
{ {
*hashp = 0; /* This may look like an off by one error but it is a bit more
if (sunaddr->sun_path[0]) {
/*
* This may look like an off by one error but it is a bit more
* subtle. 108 is the longest valid AF_UNIX path for a binding. * subtle. 108 is the longest valid AF_UNIX path for a binding.
* sun_path[108] doesn't as such exist. However in kernel space * sun_path[108] doesn't as such exist. However in kernel space
* we are guaranteed that it is a valid memory location in our * we are guaranteed that it is a valid memory location in our
* kernel address buffer. * kernel address buffer because syscall functions always pass
* a pointer of struct sockaddr_storage which has a bigger buffer
* than 108.
*/ */
((char *)sunaddr)[len] = 0; ((char *)sunaddr)[addr_len] = 0;
}
static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp)
{
*hashp = 0;
if (sunaddr->sun_path[0]) {
unix_mkname_bsd(sunaddr, len);
len = strlen(sunaddr->sun_path) + len = strlen(sunaddr->sun_path) +
offsetof(struct sockaddr_un, sun_path) + 1; offsetof(struct sockaddr_un, sun_path) + 1;
return len; return len;
...@@ -958,13 +964,14 @@ static int unix_release(struct socket *sock) ...@@ -958,13 +964,14 @@ static int unix_release(struct socket *sock)
} }
static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr, static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr,
int type) int addr_len, int type)
{ {
struct inode *inode; struct inode *inode;
struct path path; struct path path;
struct sock *sk; struct sock *sk;
int err; int err;
unix_mkname_bsd(sunaddr, addr_len);
err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path); err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path);
if (err) if (err)
goto fail; goto fail;
...@@ -1002,9 +1009,9 @@ static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr, ...@@ -1002,9 +1009,9 @@ static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr,
static struct sock *unix_find_abstract(struct net *net, static struct sock *unix_find_abstract(struct net *net,
struct sockaddr_un *sunaddr, struct sockaddr_un *sunaddr,
int addr_len, int type, int addr_len, int type)
unsigned int hash)
{ {
unsigned int hash = unix_hash_fold(csum_partial(sunaddr, addr_len, 0));
struct dentry *dentry; struct dentry *dentry;
struct sock *sk; struct sock *sk;
...@@ -1021,15 +1028,14 @@ static struct sock *unix_find_abstract(struct net *net, ...@@ -1021,15 +1028,14 @@ static struct sock *unix_find_abstract(struct net *net,
static struct sock *unix_find_other(struct net *net, static struct sock *unix_find_other(struct net *net,
struct sockaddr_un *sunaddr, struct sockaddr_un *sunaddr,
int addr_len, int type, int addr_len, int type)
unsigned int hash)
{ {
struct sock *sk; struct sock *sk;
if (sunaddr->sun_path[0]) if (sunaddr->sun_path[0])
sk = unix_find_bsd(net, sunaddr, type); sk = unix_find_bsd(net, sunaddr, addr_len, type);
else else
sk = unix_find_abstract(net, sunaddr, addr_len, type, hash); sk = unix_find_abstract(net, sunaddr, addr_len, type);
return sk; return sk;
} }
...@@ -1246,7 +1252,6 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, ...@@ -1246,7 +1252,6 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
struct net *net = sock_net(sk); struct net *net = sock_net(sk);
struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
struct sock *other; struct sock *other;
unsigned int hash;
int err; int err;
err = -EINVAL; err = -EINVAL;
...@@ -1258,11 +1263,6 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, ...@@ -1258,11 +1263,6 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
if (err) if (err)
goto out; goto out;
err = unix_mkname(sunaddr, alen, &hash);
if (err < 0)
goto out;
alen = err;
if (test_bit(SOCK_PASSCRED, &sock->flags) && if (test_bit(SOCK_PASSCRED, &sock->flags) &&
!unix_sk(sk)->addr) { !unix_sk(sk)->addr) {
err = unix_autobind(sk); err = unix_autobind(sk);
...@@ -1271,7 +1271,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, ...@@ -1271,7 +1271,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
} }
restart: restart:
other = unix_find_other(net, sunaddr, alen, sock->type, hash); other = unix_find_other(net, sunaddr, alen, sock->type);
if (IS_ERR(other)) { if (IS_ERR(other)) {
err = PTR_ERR(other); err = PTR_ERR(other);
goto out; goto out;
...@@ -1365,7 +1365,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, ...@@ -1365,7 +1365,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
struct sock *newsk = NULL; struct sock *newsk = NULL;
struct sock *other = NULL; struct sock *other = NULL;
struct sk_buff *skb = NULL; struct sk_buff *skb = NULL;
unsigned int hash;
int st; int st;
int err; int err;
long timeo; long timeo;
...@@ -1374,11 +1373,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, ...@@ -1374,11 +1373,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
if (err) if (err)
goto out; goto out;
err = unix_mkname(sunaddr, addr_len, &hash);
if (err < 0)
goto out;
addr_len = err;
if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) { if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) {
err = unix_autobind(sk); err = unix_autobind(sk);
if (err) if (err)
...@@ -1409,7 +1403,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, ...@@ -1409,7 +1403,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
restart: restart:
/* Find listening sock. */ /* Find listening sock. */
other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash); other = unix_find_other(net, sunaddr, addr_len, sk->sk_type);
if (IS_ERR(other)) { if (IS_ERR(other)) {
err = PTR_ERR(other); err = PTR_ERR(other);
other = NULL; other = NULL;
...@@ -1807,9 +1801,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1807,9 +1801,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
struct unix_sock *u = unix_sk(sk); struct unix_sock *u = unix_sk(sk);
DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name); DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
struct sock *other = NULL; struct sock *other = NULL;
int namelen = 0; /* fake GCC */
int err; int err;
unsigned int hash;
struct sk_buff *skb; struct sk_buff *skb;
long timeo; long timeo;
struct scm_cookie scm; struct scm_cookie scm;
...@@ -1829,11 +1821,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1829,11 +1821,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
err = unix_validate_addr(sunaddr, msg->msg_namelen); err = unix_validate_addr(sunaddr, msg->msg_namelen);
if (err) if (err)
goto out; goto out;
err = unix_mkname(sunaddr, msg->msg_namelen, &hash);
if (err < 0)
goto out;
namelen = err;
} else { } else {
sunaddr = NULL; sunaddr = NULL;
err = -ENOTCONN; err = -ENOTCONN;
...@@ -1886,8 +1873,8 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, ...@@ -1886,8 +1873,8 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
if (sunaddr == NULL) if (sunaddr == NULL)
goto out_free; goto out_free;
other = unix_find_other(net, sunaddr, namelen, sk->sk_type, other = unix_find_other(net, sunaddr, msg->msg_namelen,
hash); sk->sk_type);
if (IS_ERR(other)) { if (IS_ERR(other)) {
err = PTR_ERR(other); err = PTR_ERR(other);
other = NULL; other = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment