af_unix: Clean up error paths in unix_dgram_sendmsg().

The error path is complicated in unix_dgram_sendmsg() because there
are two timings when other could be non-NULL: when it's fetched from
unix_peer_get() and when it's looked up by unix_find_other().

Let's move unix_peer_get() to the else branch for unix_find_other()
and clean up the error paths.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
Kuniyuki Iwashima 2024-12-13 20:08:49 +09:00 committed by Paolo Abeni
parent 106d979b85
commit 62c6db251e

View File

@ -1993,12 +1993,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
NULL); NULL);
if (err) if (err)
goto out; goto out;
} else {
other = unix_peer_get(sk);
if (!other) {
err = -ENOTCONN;
goto out;
}
} }
if ((test_bit(SOCK_PASSCRED, &sock->flags) || if ((test_bit(SOCK_PASSCRED, &sock->flags) ||
@ -2026,7 +2020,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
skb = sock_alloc_send_pskb(sk, len - data_len, data_len, skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
msg->msg_flags & MSG_DONTWAIT, &err, msg->msg_flags & MSG_DONTWAIT, &err,
PAGE_ALLOC_COSTLY_ORDER); PAGE_ALLOC_COSTLY_ORDER);
if (skb == NULL) if (!skb)
goto out; goto out;
err = unix_scm_to_skb(&scm, skb, true); err = unix_scm_to_skb(&scm, skb, true);
@ -2042,13 +2036,18 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
if (!other) { if (msg->msg_namelen) {
lookup: lookup:
other = unix_find_other(sock_net(sk), msg->msg_name, other = unix_find_other(sock_net(sk), msg->msg_name,
msg->msg_namelen, sk->sk_type); msg->msg_namelen, sk->sk_type);
if (IS_ERR(other)) { if (IS_ERR(other)) {
err = PTR_ERR(other); err = PTR_ERR(other);
other = NULL; goto out_free;
}
} else {
other = unix_peer_get(sk);
if (!other) {
err = -ENOTCONN;
goto out_free; goto out_free;
} }
} }
@ -2056,7 +2055,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
if (sk_filter(other, skb) < 0) { if (sk_filter(other, skb) < 0) {
/* Toss the packet but do not return any error to the sender */ /* Toss the packet but do not return any error to the sender */
err = len; err = len;
goto out_free; goto out_sock_put;
} }
restart: restart:
@ -2080,7 +2079,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
* unlike SOCK_DGRAM wants. * unlike SOCK_DGRAM wants.
*/ */
err = -EPIPE; err = -EPIPE;
goto out_free; goto out_sock_put;
} }
if (!sk_locked) if (!sk_locked)
@ -2096,14 +2095,14 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
unix_dgram_disconnected(sk, other); unix_dgram_disconnected(sk, other);
sock_put(other); sock_put(other);
err = -ECONNREFUSED; err = -ECONNREFUSED;
goto out_free; goto out_sock_put;
} }
unix_state_unlock(sk); unix_state_unlock(sk);
if (!msg->msg_namelen) { if (!msg->msg_namelen) {
err = -ECONNRESET; err = -ECONNRESET;
goto out_free; goto out_sock_put;
} }
goto lookup; goto lookup;
@ -2132,7 +2131,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
err = sock_intr_errno(timeo); err = sock_intr_errno(timeo);
if (signal_pending(current)) if (signal_pending(current))
goto out_free; goto out_sock_put;
goto restart; goto restart;
} }
@ -2173,11 +2172,11 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
if (sk_locked) if (sk_locked)
unix_state_unlock(sk); unix_state_unlock(sk);
unix_state_unlock(other); unix_state_unlock(other);
out_sock_put:
sock_put(other);
out_free: out_free:
kfree_skb(skb); kfree_skb(skb);
out: out:
if (other)
sock_put(other);
scm_destroy(&scm); scm_destroy(&scm);
return err; return err;
} }