Skip to content

Commit 9bedee7

Browse files
mihalicynbrauner
authored andcommitted
af_unix: rework unix_maybe_add_creds() to allow sleep
As a preparation for the next patches we need to allow sleeping in unix_maybe_add_creds() and also return err. Currently, we can't do that as unix_maybe_add_creds() is being called under unix_state_lock(). There is no need for this, really. So let's move call sites of this helper a bit and do necessary function signature changes. Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: David S. Miller <davem@davemloft.net> Cc: Eric Dumazet <edumazet@google.com> Cc: Jakub Kicinski <kuba@kernel.org> Cc: Paolo Abeni <pabeni@redhat.com> Cc: Simon Horman <horms@kernel.org> Cc: Leon Romanovsky <leon@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Christian Brauner <brauner@kernel.org> Cc: Kuniyuki Iwashima <kuniyu@google.com> Cc: Lennart Poettering <mzxreary@0pointer.de> Cc: Luca Boccassi <bluca@debian.org> Cc: David Rheinsberg <david@readahead.eu> Signed-off-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Link: https://lore.kernel.org/20250703222314.309967-2-aleksandr.mikhalitsyn@canonical.com Reviewed-by: Christian Brauner <brauner@kernel.org> Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 8676730 commit 9bedee7

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

net/unix/af_unix.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,21 +1955,30 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
19551955
return err;
19561956
}
19571957

1958-
/*
1958+
/**
1959+
* unix_maybe_add_creds() - Adds current task uid/gid and struct pid to skb if needed.
1960+
* @skb: skb to attach creds to.
1961+
* @sk: Sender sock.
1962+
* @other: Receiver sock.
1963+
*
19591964
* Some apps rely on write() giving SCM_CREDENTIALS
19601965
* We include credentials if source or destination socket
19611966
* asserted SOCK_PASSCRED.
1967+
*
1968+
* Return: On success zero, on error a negative error code is returned.
19621969
*/
1963-
static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
1964-
const struct sock *other)
1970+
static int unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
1971+
const struct sock *other)
19651972
{
19661973
if (UNIXCB(skb).pid)
1967-
return;
1974+
return 0;
19681975

19691976
if (unix_may_passcred(sk) || unix_may_passcred(other)) {
19701977
UNIXCB(skb).pid = get_pid(task_tgid(current));
19711978
current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
19721979
}
1980+
1981+
return 0;
19731982
}
19741983

19751984
static bool unix_skb_scm_eq(struct sk_buff *skb,
@@ -2104,6 +2113,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
21042113
goto out_sock_put;
21052114
}
21062115

2116+
err = unix_maybe_add_creds(skb, sk, other);
2117+
if (err)
2118+
goto out_sock_put;
2119+
21072120
restart:
21082121
sk_locked = 0;
21092122
unix_state_lock(other);
@@ -2212,7 +2225,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
22122225
if (sock_flag(other, SOCK_RCVTSTAMP))
22132226
__net_timestamp(skb);
22142227

2215-
unix_maybe_add_creds(skb, sk, other);
22162228
scm_stat_add(other, skb);
22172229
skb_queue_tail(&other->sk_receive_queue, skb);
22182230
unix_state_unlock(other);
@@ -2256,6 +2268,10 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,
22562268
if (err < 0)
22572269
goto out;
22582270

2271+
err = unix_maybe_add_creds(skb, sk, other);
2272+
if (err)
2273+
goto out;
2274+
22592275
skb_put(skb, 1);
22602276
err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1);
22612277

@@ -2275,7 +2291,6 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,
22752291
goto out_unlock;
22762292
}
22772293

2278-
unix_maybe_add_creds(skb, sk, other);
22792294
scm_stat_add(other, skb);
22802295

22812296
spin_lock(&other->sk_receive_queue.lock);
@@ -2369,6 +2384,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
23692384

23702385
fds_sent = true;
23712386

2387+
err = unix_maybe_add_creds(skb, sk, other);
2388+
if (err)
2389+
goto out_free;
2390+
23722391
if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) {
23732392
skb->ip_summed = CHECKSUM_UNNECESSARY;
23742393
err = skb_splice_from_iter(skb, &msg->msg_iter, size,
@@ -2399,7 +2418,6 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
23992418
goto out_free;
24002419
}
24012420

2402-
unix_maybe_add_creds(skb, sk, other);
24032421
scm_stat_add(other, skb);
24042422
skb_queue_tail(&other->sk_receive_queue, skb);
24052423
unix_state_unlock(other);

0 commit comments

Comments
 (0)