Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Alexander Mikhalitsyn and committed by
Christian Brauner
9bedee7c 86767306

+25 -7
+25 -7
net/unix/af_unix.c
··· 1955 1955 return err; 1956 1956 } 1957 1957 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 + * 1959 1964 * Some apps rely on write() giving SCM_CREDENTIALS 1960 1965 * We include credentials if source or destination socket 1961 1966 * asserted SOCK_PASSCRED. 1967 + * 1968 + * Return: On success zero, on error a negative error code is returned. 1962 1969 */ 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) 1965 1972 { 1966 1973 if (UNIXCB(skb).pid) 1967 - return; 1974 + return 0; 1968 1975 1969 1976 if (unix_may_passcred(sk) || unix_may_passcred(other)) { 1970 1977 UNIXCB(skb).pid = get_pid(task_tgid(current)); 1971 1978 current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid); 1972 1979 } 1980 + 1981 + return 0; 1973 1982 } 1974 1983 1975 1984 static bool unix_skb_scm_eq(struct sk_buff *skb, ··· 2113 2104 goto out_sock_put; 2114 2105 } 2115 2106 2107 + err = unix_maybe_add_creds(skb, sk, other); 2108 + if (err) 2109 + goto out_sock_put; 2110 + 2116 2111 restart: 2117 2112 sk_locked = 0; 2118 2113 unix_state_lock(other); ··· 2225 2212 if (sock_flag(other, SOCK_RCVTSTAMP)) 2226 2213 __net_timestamp(skb); 2227 2214 2228 - unix_maybe_add_creds(skb, sk, other); 2229 2215 scm_stat_add(other, skb); 2230 2216 skb_queue_tail(&other->sk_receive_queue, skb); 2231 2217 unix_state_unlock(other); ··· 2268 2256 if (err < 0) 2269 2257 goto out; 2270 2258 2259 + err = unix_maybe_add_creds(skb, sk, other); 2260 + if (err) 2261 + goto out; 2262 + 2271 2263 skb_put(skb, 1); 2272 2264 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1); 2273 2265 ··· 2291 2275 goto out_unlock; 2292 2276 } 2293 2277 2294 - unix_maybe_add_creds(skb, sk, other); 2295 2278 scm_stat_add(other, skb); 2296 2279 2297 2280 spin_lock(&other->sk_receive_queue.lock); ··· 2384 2369 2385 2370 fds_sent = true; 2386 2371 2372 + err = unix_maybe_add_creds(skb, sk, other); 2373 + if (err) 2374 + goto out_free; 2375 + 2387 2376 if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { 2388 2377 skb->ip_summed = CHECKSUM_UNNECESSARY; 2389 2378 err = skb_splice_from_iter(skb, &msg->msg_iter, size, ··· 2418 2399 goto out_free; 2419 2400 } 2420 2401 2421 - unix_maybe_add_creds(skb, sk, other); 2422 2402 scm_stat_add(other, skb); 2423 2403 skb_queue_tail(&other->sk_receive_queue, skb); 2424 2404 unix_state_unlock(other);