blob: bd9fbfe0e7bbe8b502f90d4ccb57adb267f8eab8 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NET4: Implementation of BSD Unix domain sockets.
4 *
Alan Cox113aa832008-10-13 19:01:08 -07005 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Linus Torvalds : Assorted bug cures.
9 * Niibe Yutaka : async I/O support.
10 * Carsten Paeth : PF_UNIX check, address fixes.
11 * Alan Cox : Limit size of allocated blocks.
12 * Alan Cox : Fixed the stupid socketpair bug.
13 * Alan Cox : BSD compatibility fine tuning.
14 * Alan Cox : Fixed a bug in connect when interrupted.
15 * Alan Cox : Sorted out a proper draft version of
16 * file descriptor passing hacked up from
17 * Mike Shaver's work.
18 * Marty Leisner : Fixes to fd passing
19 * Nick Nevin : recvmsg bugfix.
20 * Alan Cox : Started proper garbage collector
21 * Heiko EiBfeldt : Missing verify_area check
22 * Alan Cox : Started POSIXisms
23 * Andreas Schwab : Replace inode by dentry for proper
24 * reference counting
25 * Kirk Petersen : Made this a module
26 * Christoph Rohland : Elegant non-blocking accept/connect algorithm.
27 * Lots of bug fixes.
28 * Alexey Kuznetosv : Repaired (I hope) bugs introduces
29 * by above two patches.
30 * Andrea Arcangeli : If possible we block in connect(2)
31 * if the max backlog of the listen socket
32 * is been reached. This won't break
33 * old apps and it will avoid huge amount
34 * of socks hashed (this for unix_gc()
35 * performances reasons).
36 * Security fix that limits the max
37 * number of socks to 2*max_files and
38 * the number of skb queueable in the
39 * dgram receiver.
40 * Artur Skawina : Hash function optimizations
41 * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8)
42 * Malcolm Beattie : Set peercred for socketpair
43 * Michal Ostrowski : Module initialization cleanup.
44 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
45 * the core infrastructure is doing that
46 * for all net proto families now (2.5.69+)
47 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * Known differences from reference BSD that was tested:
49 *
50 * [TO FIX]
51 * ECONNREFUSED is not returned from one end of a connected() socket to the
52 * other the moment one end closes.
53 * fstat() doesn't return st_dev=0, and give the blksize as high water mark
54 * and a fake inode identifier (nor the BSD first socket fstat twice bug).
55 * [NOT TO FIX]
56 * accept() returns a path name even if the connecting socket has closed
57 * in the meantime (BSD loses the path and gives up).
58 * accept() returns 0 length path for an unbound connector. BSD returns 16
59 * and a null first byte in the path (but not for gethost/peername - BSD bug ??)
60 * socketpair(...SOCK_RAW..) doesn't panic the kernel.
61 * BSD af_unix apparently has connect forgetting to block properly.
62 * (need to check this with the POSIX spec in detail)
63 *
64 * Differences from 2.0.0-11-... (ANK)
65 * Bug fixes and improvements.
66 * - client shutdown killed server socket.
67 * - removed all useless cli/sti pairs.
68 *
69 * Semantic changes/extensions.
70 * - generic control message passing.
71 * - SCM_CREDENTIALS control message.
72 * - "Abstract" (not FS based) socket bindings.
73 * Abstract names are sequences of bytes (not zero terminated)
74 * started by 0, so that this name space does not intersect
75 * with BSD names.
76 */
77
wangweidong5cc208b2013-12-06 18:03:36 +080078#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/signal.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010083#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/errno.h>
85#include <linux/string.h>
86#include <linux/stat.h>
87#include <linux/dcache.h>
88#include <linux/namei.h>
89#include <linux/socket.h>
90#include <linux/un.h>
91#include <linux/fcntl.h>
92#include <linux/termios.h>
93#include <linux/sockios.h>
94#include <linux/net.h>
95#include <linux/in.h>
96#include <linux/fs.h>
97#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080098#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/skbuff.h>
100#include <linux/netdevice.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200101#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -0700103#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <net/af_unix.h>
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#include <net/scm.h>
108#include <linux/init.h>
109#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#include <linux/rtnetlink.h>
111#include <linux/mount.h>
112#include <net/checksum.h>
113#include <linux/security.h>
Colin Cross2b15af62013-05-06 23:50:21 +0000114#include <linux/freezer.h>
Andrey Vaginba94f302017-02-01 11:00:45 -0800115#include <linux/file.h>
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +0900116#include <linux/btf_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jens Axboef4e65872019-02-08 09:01:44 -0700118#include "scm.h"
119
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000120struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000121EXPORT_SYMBOL_GPL(unix_socket_table);
122DEFINE_SPINLOCK(unix_table_lock);
123EXPORT_SYMBOL_GPL(unix_table_lock);
Eric Dumazet518de9b2010-10-26 14:22:44 -0700124static atomic_long_t unix_nr_socks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900126/* SMP locking strategy:
127 * hash table is protected with spinlock unix_table_lock
128 * each socket state is protected by separate spin lock.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900131static unsigned int unix_unbound_hash(struct sock *sk)
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000132{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900133 unsigned long hash = (unsigned long)sk;
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000134
135 hash ^= hash >> 16;
136 hash ^= hash >> 8;
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900137 hash ^= sk->sk_type;
138
139 return UNIX_HASH_SIZE + (hash & (UNIX_HASH_SIZE - 1));
140}
141
142static unsigned int unix_bsd_hash(struct inode *i)
143{
144 return i->i_ino & (UNIX_HASH_SIZE - 1);
145}
146
147static unsigned int unix_abstract_hash(struct sockaddr_un *sunaddr,
148 int addr_len, int type)
149{
150 __wsum csum = csum_partial(sunaddr, addr_len, 0);
151 unsigned int hash;
152
153 hash = (__force unsigned int)csum_fold(csum);
154 hash ^= hash >> 8;
155 hash ^= type;
156
157 return hash & (UNIX_HASH_SIZE - 1);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000158}
159
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700160#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700161static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700162{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400163 UNIXCB(skb).secid = scm->secid;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700164}
165
166static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
167{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400168 scm->secid = UNIXCB(skb).secid;
169}
170
171static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
172{
173 return (scm->secid == UNIXCB(skb).secid);
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700174}
175#else
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700176static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700177{ }
178
179static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
180{ }
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400181
182static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
183{
184 return true;
185}
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700186#endif /* CONFIG_SECURITY_NETWORK */
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#define unix_peer(sk) (unix_sk(sk)->peer)
189
190static inline int unix_our_peer(struct sock *sk, struct sock *osk)
191{
192 return unix_peer(osk) == sk;
193}
194
195static inline int unix_may_send(struct sock *sk, struct sock *osk)
196{
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800197 return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Qian Cai86b18aa2020-02-04 13:40:29 -0500200static inline int unix_recvq_full(const struct sock *sk)
Rainer Weikusat3c734192008-06-17 22:28:05 -0700201{
202 return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
203}
204
Qian Cai86b18aa2020-02-04 13:40:29 -0500205static inline int unix_recvq_full_lockless(const struct sock *sk)
206{
207 return skb_queue_len_lockless(&sk->sk_receive_queue) >
208 READ_ONCE(sk->sk_max_ack_backlog);
209}
210
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000211struct sock *unix_peer_get(struct sock *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct sock *peer;
214
David S. Miller1c92b4e2007-05-31 13:24:26 -0700215 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 peer = unix_peer(s);
217 if (peer)
218 sock_hold(peer);
David S. Miller1c92b4e2007-05-31 13:24:26 -0700219 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return peer;
221}
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000222EXPORT_SYMBOL_GPL(unix_peer_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +0900224static struct unix_address *unix_create_addr(struct sockaddr_un *sunaddr,
225 int addr_len)
226{
227 struct unix_address *addr;
228
229 addr = kmalloc(sizeof(*addr) + addr_len, GFP_KERNEL);
230 if (!addr)
231 return NULL;
232
233 refcount_set(&addr->refcnt, 1);
234 addr->len = addr_len;
235 memcpy(addr->name, sunaddr, addr_len);
236
237 return addr;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static inline void unix_release_addr(struct unix_address *addr)
241{
Reshetova, Elena8c9814b2017-06-30 13:08:05 +0300242 if (refcount_dec_and_test(&addr->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 kfree(addr);
244}
245
246/*
247 * Check unix socket name:
248 * - should be not zero length.
249 * - if started by not zero, should be NULL terminated (FS object)
250 * - if started by zero, it is abstract name.
251 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900252
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +0900253static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len)
254{
255 if (addr_len <= offsetof(struct sockaddr_un, sun_path) ||
256 addr_len > sizeof(*sunaddr))
257 return -EINVAL;
258
259 if (sunaddr->sun_family != AF_UNIX)
260 return -EINVAL;
261
262 return 0;
263}
264
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +0900265static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len)
266{
267 /* This may look like an off by one error but it is a bit more
268 * subtle. 108 is the longest valid AF_UNIX path for a binding.
269 * sun_path[108] doesn't as such exist. However in kernel space
270 * we are guaranteed that it is a valid memory location in our
271 * kernel address buffer because syscall functions always pass
272 * a pointer of struct sockaddr_storage which has a bigger buffer
273 * than 108.
274 */
275 ((char *)sunaddr)[addr_len] = 0;
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void __unix_remove_socket(struct sock *sk)
279{
280 sk_del_node_init(sk);
281}
282
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900283static void __unix_insert_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700285 WARN_ON(!sk_unhashed(sk));
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900286 sk_add_node(sk, &unix_socket_table[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900289static void __unix_set_addr_hash(struct sock *sk, struct unix_address *addr,
290 unsigned int hash)
Al Viro185ab882021-06-19 03:50:26 +0000291{
292 __unix_remove_socket(sk);
293 smp_store_release(&unix_sk(sk)->addr, addr);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900294
295 sk->sk_hash = hash;
296 __unix_insert_socket(sk);
Al Viro185ab882021-06-19 03:50:26 +0000297}
298
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900299static void unix_remove_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800301 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 __unix_remove_socket(sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800303 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900306static void unix_insert_unbound_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David S. Millerfbe9cc42005-12-13 23:26:29 -0800308 spin_lock(&unix_table_lock);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900309 __unix_insert_socket(sk);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800310 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800313static struct sock *__unix_find_socket_byname(struct net *net,
314 struct sockaddr_un *sunname,
Al Virobe752282021-06-19 03:50:33 +0000315 int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Al Virobe752282021-06-19 03:50:33 +0000319 sk_for_each(s, &unix_socket_table[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct unix_sock *u = unix_sk(s);
321
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900322 if (!net_eq(sock_net(s), net))
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800323 continue;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (u->addr->len == len &&
326 !memcmp(u->addr->name, sunname, len))
Vito Caputo262ce0a2019-10-09 20:43:47 -0700327 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Vito Caputo262ce0a2019-10-09 20:43:47 -0700329 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800332static inline struct sock *unix_find_socket_byname(struct net *net,
333 struct sockaddr_un *sunname,
Al Virobe752282021-06-19 03:50:33 +0000334 int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct sock *s;
337
David S. Millerfbe9cc42005-12-13 23:26:29 -0800338 spin_lock(&unix_table_lock);
Al Virobe752282021-06-19 03:50:33 +0000339 s = __unix_find_socket_byname(net, sunname, len, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (s)
341 sock_hold(s);
David S. Millerfbe9cc42005-12-13 23:26:29 -0800342 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return s;
344}
345
Eric W. Biederman6616f782010-06-13 03:35:48 +0000346static struct sock *unix_find_socket_byinode(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900348 unsigned int hash = unix_bsd_hash(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David S. Millerfbe9cc42005-12-13 23:26:29 -0800351 spin_lock(&unix_table_lock);
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900352 sk_for_each(s, &unix_socket_table[hash]) {
Al Viro40ffe672012-03-14 21:54:32 -0400353 struct dentry *dentry = unix_sk(s)->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Miklos Szeredibeef5122016-12-16 11:02:53 +0100355 if (dentry && d_backing_inode(dentry) == i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 sock_hold(s);
357 goto found;
358 }
359 }
360 s = NULL;
361found:
David S. Millerfbe9cc42005-12-13 23:26:29 -0800362 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return s;
364}
365
Rainer Weikusat7d267272015-11-20 22:07:23 +0000366/* Support code for asymmetrically connected dgram sockets
367 *
368 * If a datagram socket is connected to a socket not itself connected
369 * to the first socket (eg, /dev/log), clients may only enqueue more
370 * messages if the present receive queue of the server socket is not
371 * "too large". This means there's a second writeability condition
372 * poll and sendmsg need to test. The dgram recv code will do a wake
373 * up on the peer_wait wait queue of a socket upon reception of a
374 * datagram which needs to be propagated to sleeping would-be writers
375 * since these might not have sent anything so far. This can't be
376 * accomplished via poll_wait because the lifetime of the server
377 * socket might be less than that of its clients if these break their
378 * association with it or if the server socket is closed while clients
379 * are still connected to it and there's no way to inform "a polling
380 * implementation" that it should let go of a certain wait queue
381 *
Ingo Molnarac6424b2017-06-20 12:06:13 +0200382 * In order to propagate a wake up, a wait_queue_entry_t of the client
Rainer Weikusat7d267272015-11-20 22:07:23 +0000383 * socket is enqueued on the peer_wait queue of the server socket
384 * whose wake function does a wake_up on the ordinary client socket
385 * wait queue. This connection is established whenever a write (or
386 * poll for write) hit the flow control condition and broken when the
387 * association to the server socket is dissolved or after a wake up
388 * was relayed.
389 */
390
Ingo Molnarac6424b2017-06-20 12:06:13 +0200391static int unix_dgram_peer_wake_relay(wait_queue_entry_t *q, unsigned mode, int flags,
Rainer Weikusat7d267272015-11-20 22:07:23 +0000392 void *key)
393{
394 struct unix_sock *u;
395 wait_queue_head_t *u_sleep;
396
397 u = container_of(q, struct unix_sock, peer_wake);
398
399 __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait,
400 q);
401 u->peer_wake.private = NULL;
402
403 /* relaying can only happen while the wq still exists */
404 u_sleep = sk_sleep(&u->sk);
405 if (u_sleep)
Al Viro3ad6f932017-07-03 20:14:56 -0400406 wake_up_interruptible_poll(u_sleep, key_to_poll(key));
Rainer Weikusat7d267272015-11-20 22:07:23 +0000407
408 return 0;
409}
410
411static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other)
412{
413 struct unix_sock *u, *u_other;
414 int rc;
415
416 u = unix_sk(sk);
417 u_other = unix_sk(other);
418 rc = 0;
419 spin_lock(&u_other->peer_wait.lock);
420
421 if (!u->peer_wake.private) {
422 u->peer_wake.private = other;
423 __add_wait_queue(&u_other->peer_wait, &u->peer_wake);
424
425 rc = 1;
426 }
427
428 spin_unlock(&u_other->peer_wait.lock);
429 return rc;
430}
431
432static void unix_dgram_peer_wake_disconnect(struct sock *sk,
433 struct sock *other)
434{
435 struct unix_sock *u, *u_other;
436
437 u = unix_sk(sk);
438 u_other = unix_sk(other);
439 spin_lock(&u_other->peer_wait.lock);
440
441 if (u->peer_wake.private == other) {
442 __remove_wait_queue(&u_other->peer_wait, &u->peer_wake);
443 u->peer_wake.private = NULL;
444 }
445
446 spin_unlock(&u_other->peer_wait.lock);
447}
448
449static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk,
450 struct sock *other)
451{
452 unix_dgram_peer_wake_disconnect(sk, other);
453 wake_up_interruptible_poll(sk_sleep(sk),
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800454 EPOLLOUT |
455 EPOLLWRNORM |
456 EPOLLWRBAND);
Rainer Weikusat7d267272015-11-20 22:07:23 +0000457}
458
459/* preconditions:
460 * - unix_peer(sk) == other
461 * - association is stable
462 */
463static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other)
464{
465 int connected;
466
467 connected = unix_dgram_peer_wake_connect(sk, other);
468
Jason Baron51f7e952018-08-03 17:24:53 -0400469 /* If other is SOCK_DEAD, we want to make sure we signal
470 * POLLOUT, such that a subsequent write() can get a
471 * -ECONNREFUSED. Otherwise, if we haven't queued any skbs
472 * to other and its full, we will hang waiting for POLLOUT.
473 */
474 if (unix_recvq_full(other) && !sock_flag(other, SOCK_DEAD))
Rainer Weikusat7d267272015-11-20 22:07:23 +0000475 return 1;
476
477 if (connected)
478 unix_dgram_peer_wake_disconnect(sk, other);
479
480 return 0;
481}
482
Eric Dumazet1586a582015-10-23 10:59:16 -0700483static int unix_writable(const struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Eric Dumazet1586a582015-10-23 10:59:16 -0700485 return sk->sk_state != TCP_LISTEN &&
Reshetova, Elena14afee42017-06-30 13:08:00 +0300486 (refcount_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void unix_write_space(struct sock *sk)
490{
Eric Dumazet43815482010-04-29 11:01:49 +0000491 struct socket_wq *wq;
492
493 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (unix_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +0000495 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800496 if (skwq_has_sleeper(wq))
Eric Dumazet67426b72010-10-29 20:44:44 +0000497 wake_up_interruptible_sync_poll(&wq->wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800498 EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800499 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Eric Dumazet43815482010-04-29 11:01:49 +0000501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504/* When dgram socket disconnects (or changes its peer), we clear its receive
505 * queue of packets arrived from previous peer. First, it allows to do
506 * flow control based only on wmem_alloc; second, sk connected to peer
507 * may receive messages only from that peer. */
508static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
509{
David S. Millerb03efcf2005-07-08 14:57:23 -0700510 if (!skb_queue_empty(&sk->sk_receive_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 skb_queue_purge(&sk->sk_receive_queue);
512 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
513
514 /* If one link of bidirectional dgram pipe is disconnected,
515 * we signal error. Messages are lost. Do not make this,
516 * when peer was not connected to us.
517 */
518 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
519 other->sk_err = ECONNRESET;
Alexander Aringe3ae2362021-06-27 18:48:21 -0400520 sk_error_report(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 }
Eric Dumazetdc56ad72021-08-30 10:21:37 -0700523 other->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526static void unix_sock_destructor(struct sock *sk)
527{
528 struct unix_sock *u = unix_sk(sk);
529
530 skb_queue_purge(&sk->sk_receive_queue);
531
Rao Shoaib314001f2021-08-01 00:57:07 -0700532#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
533 if (u->oob_skb) {
534 kfree_skb(u->oob_skb);
535 u->oob_skb = NULL;
536 }
537#endif
Reshetova, Elena14afee42017-06-30 13:08:00 +0300538 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700539 WARN_ON(!sk_unhashed(sk));
540 WARN_ON(sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!sock_flag(sk, SOCK_DEAD)) {
wangweidong5cc208b2013-12-06 18:03:36 +0800542 pr_info("Attempt to release alive unix socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return;
544 }
545
546 if (u->addr)
547 unix_release_addr(u->addr);
548
Eric Dumazet518de9b2010-10-26 14:22:44 -0700549 atomic_long_dec(&unix_nr_socks);
Eric Dumazeta8076d82008-11-17 02:38:49 -0800550 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef UNIX_REFCNT_DEBUG
wangweidong5cc208b2013-12-06 18:03:36 +0800552 pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
Eric Dumazet518de9b2010-10-26 14:22:44 -0700553 atomic_long_read(&unix_nr_socks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555}
556
Paul Mooreded34e02013-03-25 03:18:33 +0000557static void unix_release_sock(struct sock *sk, int embrion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct unix_sock *u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400560 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct sock *skpair;
562 struct sk_buff *skb;
563 int state;
564
565 unix_remove_socket(sk);
566
567 /* Clear state */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700568 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 sock_orphan(sk);
570 sk->sk_shutdown = SHUTDOWN_MASK;
Al Viro40ffe672012-03-14 21:54:32 -0400571 path = u->path;
572 u->path.dentry = NULL;
573 u->path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 state = sk->sk_state;
575 sk->sk_state = TCP_CLOSE;
Eric Dumazeta494bd62021-06-16 07:47:15 -0700576
577 skpair = unix_peer(sk);
578 unix_peer(sk) = NULL;
579
David S. Miller1c92b4e2007-05-31 13:24:26 -0700580 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 wake_up_interruptible_all(&u->peer_wait);
583
Jianjun Konge27dfce2008-11-01 21:38:31 -0700584 if (skpair != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
David S. Miller1c92b4e2007-05-31 13:24:26 -0700586 unix_state_lock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* No more writes */
588 skpair->sk_shutdown = SHUTDOWN_MASK;
589 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
590 skpair->sk_err = ECONNRESET;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700591 unix_state_unlock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 skpair->sk_state_change(skpair);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800593 sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Rainer Weikusat7d267272015-11-20 22:07:23 +0000595
596 unix_dgram_peer_wake_disconnect(sk, skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 sock_put(skpair); /* It may now die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 /* Try to flush out this socket. Throw out buffers at least */
601
602 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Jianjun Konge27dfce2008-11-01 21:38:31 -0700603 if (state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unix_release_sock(skb->sk, 1);
605 /* passed fds are erased in the kfree_skb hook */
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +0100606 UNIXCB(skb).consumed = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 kfree_skb(skb);
608 }
609
Al Viro40ffe672012-03-14 21:54:32 -0400610 if (path.dentry)
611 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 sock_put(sk);
614
615 /* ---- Socket is dead now and most probably destroyed ---- */
616
617 /*
Alan Coxe04dae82012-09-17 00:52:41 +0000618 * Fixme: BSD difference: In BSD all sockets connected to us get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * ECONNRESET and we die on the spot. In Linux we behave
620 * like files and pipes do and wait for the last
621 * dereference.
622 *
623 * Can't we simply set sock->err?
624 *
625 * What the above comment does talk about? --ANK(980817)
626 */
627
Pavel Emelyanov9305cfa2007-11-10 22:06:01 -0800628 if (unix_tot_inflight)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900629 unix_gc(); /* Garbage collect fds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000632static void init_peercred(struct sock *sk)
633{
Eric Dumazet35306eb2021-09-29 15:57:50 -0700634 const struct cred *old_cred;
635 struct pid *old_pid;
636
637 spin_lock(&sk->sk_peer_lock);
638 old_pid = sk->sk_peer_pid;
639 old_cred = sk->sk_peer_cred;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000640 sk->sk_peer_pid = get_pid(task_tgid(current));
641 sk->sk_peer_cred = get_current_cred();
Eric Dumazet35306eb2021-09-29 15:57:50 -0700642 spin_unlock(&sk->sk_peer_lock);
643
644 put_pid(old_pid);
645 put_cred(old_cred);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000646}
647
648static void copy_peercred(struct sock *sk, struct sock *peersk)
649{
Eric Dumazet35306eb2021-09-29 15:57:50 -0700650 const struct cred *old_cred;
651 struct pid *old_pid;
652
653 if (sk < peersk) {
654 spin_lock(&sk->sk_peer_lock);
655 spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING);
656 } else {
657 spin_lock(&peersk->sk_peer_lock);
658 spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);
659 }
660 old_pid = sk->sk_peer_pid;
661 old_cred = sk->sk_peer_cred;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000662 sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
663 sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
Eric Dumazet35306eb2021-09-29 15:57:50 -0700664
665 spin_unlock(&sk->sk_peer_lock);
666 spin_unlock(&peersk->sk_peer_lock);
667
668 put_pid(old_pid);
669 put_cred(old_cred);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static int unix_listen(struct socket *sock, int backlog)
673{
674 int err;
675 struct sock *sk = sock->sk;
676 struct unix_sock *u = unix_sk(sk);
677
678 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800679 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
680 goto out; /* Only stream/seqpacket sockets accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 err = -EINVAL;
682 if (!u->addr)
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800683 goto out; /* No listens on an unbound socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700684 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
686 goto out_unlock;
687 if (backlog > sk->sk_max_ack_backlog)
688 wake_up_interruptible_all(&u->peer_wait);
689 sk->sk_max_ack_backlog = backlog;
690 sk->sk_state = TCP_LISTEN;
691 /* set credentials so connect can copy them */
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000692 init_peercred(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 err = 0;
694
695out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -0700696 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697out:
698 return err;
699}
700
701static int unix_release(struct socket *);
702static int unix_bind(struct socket *, struct sockaddr *, int);
703static int unix_stream_connect(struct socket *, struct sockaddr *,
704 int addr_len, int flags);
705static int unix_socketpair(struct socket *, struct socket *);
David Howellscdfbabf2017-03-09 08:09:05 +0000706static int unix_accept(struct socket *, struct socket *, int, bool);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100707static int unix_getname(struct socket *, struct sockaddr *, int);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700708static __poll_t unix_poll(struct file *, struct socket *, poll_table *);
709static __poll_t unix_dgram_poll(struct file *, struct socket *,
710 poll_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static int unix_ioctl(struct socket *, unsigned int, unsigned long);
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200712#ifdef CONFIG_COMPAT
713static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static int unix_shutdown(struct socket *, int);
Ying Xue1b784142015-03-02 15:37:48 +0800716static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
717static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200718static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
719 size_t size, int flags);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200720static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
721 struct pipe_inode_info *, size_t size,
722 unsigned int flags);
Ying Xue1b784142015-03-02 15:37:48 +0800723static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
724static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
Cong Wang29df44f2021-07-04 12:02:44 -0700725static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
726 sk_read_actor_t recv_actor);
Jiang Wang77462de2021-08-16 19:03:20 +0000727static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
728 sk_read_actor_t recv_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int unix_dgram_connect(struct socket *, struct sockaddr *,
730 int, int);
Ying Xue1b784142015-03-02 15:37:48 +0800731static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
732static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
733 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Sasha Levin12663bf2013-12-07 17:26:27 -0500735static int unix_set_peek_off(struct sock *sk, int val)
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000736{
737 struct unix_sock *u = unix_sk(sk);
738
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700739 if (mutex_lock_interruptible(&u->iolock))
Sasha Levin12663bf2013-12-07 17:26:27 -0500740 return -EINTR;
741
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000742 sk->sk_peek_off = val;
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700743 mutex_unlock(&u->iolock);
Sasha Levin12663bf2013-12-07 17:26:27 -0500744
745 return 0;
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000746}
747
David S. Miller5c05a162020-02-27 11:52:35 -0800748#ifdef CONFIG_PROC_FS
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300749static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
750{
751 struct sock *sk = sock->sk;
752 struct unix_sock *u;
753
754 if (sk) {
755 u = unix_sk(sock->sk);
Paolo Abeni77820402020-02-28 14:45:21 +0100756 seq_printf(m, "scm_fds: %u\n",
757 atomic_read(&u->scm_stat.nr_fds));
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300758 }
759}
Tobias Klauser3a125002020-02-26 18:29:53 +0100760#else
761#define unix_show_fdinfo NULL
762#endif
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000763
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800764static const struct proto_ops unix_stream_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .family = PF_UNIX,
766 .owner = THIS_MODULE,
767 .release = unix_release,
768 .bind = unix_bind,
769 .connect = unix_stream_connect,
770 .socketpair = unix_socketpair,
771 .accept = unix_accept,
772 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700773 .poll = unix_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200775#ifdef CONFIG_COMPAT
776 .compat_ioctl = unix_compat_ioctl,
777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .listen = unix_listen,
779 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 .sendmsg = unix_stream_sendmsg,
781 .recvmsg = unix_stream_recvmsg,
Jiang Wang77462de2021-08-16 19:03:20 +0000782 .read_sock = unix_stream_read_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .mmap = sock_no_mmap,
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200784 .sendpage = unix_stream_sendpage,
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200785 .splice_read = unix_stream_splice_read,
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +0000786 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300787 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788};
789
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800790static const struct proto_ops unix_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .family = PF_UNIX,
792 .owner = THIS_MODULE,
793 .release = unix_release,
794 .bind = unix_bind,
795 .connect = unix_dgram_connect,
796 .socketpair = unix_socketpair,
797 .accept = sock_no_accept,
798 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700799 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200801#ifdef CONFIG_COMPAT
802 .compat_ioctl = unix_compat_ioctl,
803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .listen = sock_no_listen,
805 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .sendmsg = unix_dgram_sendmsg,
Cong Wang29df44f2021-07-04 12:02:44 -0700807 .read_sock = unix_read_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .recvmsg = unix_dgram_recvmsg,
809 .mmap = sock_no_mmap,
810 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000811 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300812 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813};
814
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800815static const struct proto_ops unix_seqpacket_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .family = PF_UNIX,
817 .owner = THIS_MODULE,
818 .release = unix_release,
819 .bind = unix_bind,
820 .connect = unix_stream_connect,
821 .socketpair = unix_socketpair,
822 .accept = unix_accept,
823 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700824 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200826#ifdef CONFIG_COMPAT
827 .compat_ioctl = unix_compat_ioctl,
828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 .listen = unix_listen,
830 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .sendmsg = unix_seqpacket_sendmsg,
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +0000832 .recvmsg = unix_seqpacket_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .mmap = sock_no_mmap,
834 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000835 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300836 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837};
838
Cong Wangc7272e12021-07-04 12:02:46 -0700839static void unix_close(struct sock *sk, long timeout)
840{
841 /* Nothing to do here, unix socket does not need a ->close().
842 * This is merely for sockmap.
843 */
844}
845
Jiang Wang94531cf2021-08-16 19:03:21 +0000846static void unix_unhash(struct sock *sk)
847{
848 /* Nothing to do here, unix socket does not need a ->unhash().
849 * This is merely for sockmap.
850 */
851}
852
853struct proto unix_dgram_proto = {
Stephen Boyd0edf0822021-10-08 14:59:45 -0700854 .name = "UNIX",
Eric Dumazet248969a2008-11-17 00:00:30 -0800855 .owner = THIS_MODULE,
Eric Dumazet248969a2008-11-17 00:00:30 -0800856 .obj_size = sizeof(struct unix_sock),
Cong Wangc7272e12021-07-04 12:02:46 -0700857 .close = unix_close,
Cong Wangc6382912021-07-04 12:02:47 -0700858#ifdef CONFIG_BPF_SYSCALL
Jiang Wang94531cf2021-08-16 19:03:21 +0000859 .psock_update_sk_prot = unix_dgram_bpf_update_proto,
Cong Wangc6382912021-07-04 12:02:47 -0700860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861};
862
Jiang Wang94531cf2021-08-16 19:03:21 +0000863struct proto unix_stream_proto = {
864 .name = "UNIX-STREAM",
865 .owner = THIS_MODULE,
866 .obj_size = sizeof(struct unix_sock),
867 .close = unix_close,
868 .unhash = unix_unhash,
869#ifdef CONFIG_BPF_SYSCALL
870 .psock_update_sk_prot = unix_stream_bpf_update_proto,
871#endif
872};
873
874static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct unix_sock *u;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900877 struct sock *sk;
878 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Eric Dumazet518de9b2010-10-26 14:22:44 -0700880 atomic_long_inc(&unix_nr_socks);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900881 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) {
882 err = -ENFILE;
883 goto err;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Jiang Wang94531cf2021-08-16 19:03:21 +0000886 if (type == SOCK_STREAM)
887 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_stream_proto, kern);
888 else /*dgram and seqpacket */
889 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_dgram_proto, kern);
890
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900891 if (!sk) {
892 err = -ENOMEM;
893 goto err;
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800896 sock_init_data(sock, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900898 sk->sk_hash = unix_unbound_hash(sk);
Vladimir Davydov3aa97992016-07-26 15:24:36 -0700899 sk->sk_allocation = GFP_KERNEL_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sk->sk_write_space = unix_write_space;
Denis V. Luneva0a53c82007-12-11 04:19:17 -0800901 sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sk->sk_destruct = unix_sock_destructor;
903 u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400904 u->path.dentry = NULL;
905 u->path.mnt = NULL;
Benjamin LaHaisefd19f322006-01-03 14:10:46 -0800906 spin_lock_init(&u->lock);
Al Viro516e0cc2008-07-26 00:39:17 -0400907 atomic_long_set(&u->inflight, 0);
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -0700908 INIT_LIST_HEAD(&u->link);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700909 mutex_init(&u->iolock); /* single task reading lock */
910 mutex_init(&u->bindlock); /* single task binding lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 init_waitqueue_head(&u->peer_wait);
Rainer Weikusat7d267272015-11-20 22:07:23 +0000912 init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300913 memset(&u->scm_stat, 0, sizeof(struct scm_stat));
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900914 unix_insert_unbound_socket(sk);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900915
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900916 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return sk;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900919
920err:
921 atomic_long_dec(&unix_nr_socks);
922 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Eric Paris3f378b62009-11-05 22:18:14 -0800925static int unix_create(struct net *net, struct socket *sock, int protocol,
926 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900928 struct sock *sk;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (protocol && protocol != PF_UNIX)
931 return -EPROTONOSUPPORT;
932
933 sock->state = SS_UNCONNECTED;
934
935 switch (sock->type) {
936 case SOCK_STREAM:
937 sock->ops = &unix_stream_ops;
938 break;
939 /*
940 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
941 * nothing uses it.
942 */
943 case SOCK_RAW:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700944 sock->type = SOCK_DGRAM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500945 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case SOCK_DGRAM:
947 sock->ops = &unix_dgram_ops;
948 break;
949 case SOCK_SEQPACKET:
950 sock->ops = &unix_seqpacket_ops;
951 break;
952 default:
953 return -ESOCKTNOSUPPORT;
954 }
955
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900956 sk = unix_create1(net, sock, kern, sock->type);
957 if (IS_ERR(sk))
958 return PTR_ERR(sk);
959
960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static int unix_release(struct socket *sock)
964{
965 struct sock *sk = sock->sk;
966
967 if (!sk)
968 return 0;
969
Cong Wangc7272e12021-07-04 12:02:46 -0700970 sk->sk_prot->close(sk, 0);
Paul Mooreded34e02013-03-25 03:18:33 +0000971 unix_release_sock(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sock->sk = NULL;
973
Paul Mooreded34e02013-03-25 03:18:33 +0000974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +0900977static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr,
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +0900978 int addr_len, int type)
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +0900979{
980 struct inode *inode;
981 struct path path;
982 struct sock *sk;
983 int err;
984
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +0900985 unix_mkname_bsd(sunaddr, addr_len);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +0900986 err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path);
987 if (err)
988 goto fail;
989
990 err = path_permission(&path, MAY_WRITE);
991 if (err)
992 goto path_put;
993
994 err = -ECONNREFUSED;
995 inode = d_backing_inode(path.dentry);
996 if (!S_ISSOCK(inode->i_mode))
997 goto path_put;
998
999 sk = unix_find_socket_byinode(inode);
1000 if (!sk)
1001 goto path_put;
1002
1003 err = -EPROTOTYPE;
1004 if (sk->sk_type == type)
1005 touch_atime(&path);
1006 else
1007 goto sock_put;
1008
1009 path_put(&path);
1010
1011 return sk;
1012
1013sock_put:
1014 sock_put(sk);
1015path_put:
1016 path_put(&path);
1017fail:
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001018 return ERR_PTR(err);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001019}
1020
1021static struct sock *unix_find_abstract(struct net *net,
1022 struct sockaddr_un *sunaddr,
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001023 int addr_len, int type)
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001024{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +09001025 unsigned int hash = unix_abstract_hash(sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001026 struct dentry *dentry;
1027 struct sock *sk;
1028
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +09001029 sk = unix_find_socket_byname(net, sunaddr, addr_len, hash);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001030 if (!sk)
1031 return ERR_PTR(-ECONNREFUSED);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001032
1033 dentry = unix_sk(sk)->path.dentry;
1034 if (dentry)
1035 touch_atime(&unix_sk(sk)->path);
1036
1037 return sk;
1038}
1039
1040static struct sock *unix_find_other(struct net *net,
1041 struct sockaddr_un *sunaddr,
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001042 int addr_len, int type)
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001043{
1044 struct sock *sk;
1045
1046 if (sunaddr->sun_path[0])
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001047 sk = unix_find_bsd(net, sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001048 else
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001049 sk = unix_find_abstract(net, sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001050
1051 return sk;
1052}
1053
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001054static int unix_autobind(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct unix_sock *u = unix_sk(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001057 struct unix_address *addr;
Tetsuo Handa8df73ff2010-09-04 01:34:28 +00001058 unsigned int retries = 0;
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001059 static u32 ordernum = 1;
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001060 unsigned int new_hash;
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001061 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07001063 err = mutex_lock_interruptible(&u->bindlock);
Sasha Levin37ab4fa2013-12-13 10:54:22 -05001064 if (err)
1065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (u->addr)
1068 goto out;
1069
1070 err = -ENOMEM;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09001071 addr = kzalloc(sizeof(*addr) +
1072 offsetof(struct sockaddr_un, sun_path) + 16, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!addr)
1074 goto out;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 addr->name->sun_family = AF_UNIX;
Reshetova, Elena8c9814b2017-06-30 13:08:05 +03001077 refcount_set(&addr->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079retry:
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09001080 addr->len = sprintf(addr->name->sun_path + 1, "%05x", ordernum) +
1081 offsetof(struct sockaddr_un, sun_path) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001083 new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
David S. Millerfbe9cc42005-12-13 23:26:29 -08001084 spin_lock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ordernum = (ordernum+1)&0xFFFFF;
1086
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001087 if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len,
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001088 new_hash)) {
David S. Millerfbe9cc42005-12-13 23:26:29 -08001089 spin_unlock(&unix_table_lock);
Tetsuo Handa8df73ff2010-09-04 01:34:28 +00001090 /*
1091 * __unix_find_socket_byname() may take long time if many names
1092 * are already in use.
1093 */
1094 cond_resched();
1095 /* Give up if all names seems to be in use. */
1096 if (retries++ == 0xFFFFF) {
1097 err = -ENOSPC;
1098 kfree(addr);
1099 goto out;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 goto retry;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001104 __unix_set_addr_hash(sk, addr, new_hash);
David S. Millerfbe9cc42005-12-13 23:26:29 -08001105 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 err = 0;
1107
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07001108out: mutex_unlock(&u->bindlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return err;
1110}
1111
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001112static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
1113 int addr_len)
Al Virofaf02012012-07-20 02:37:29 +04001114{
Al Viro71e6be62021-06-19 03:50:30 +00001115 umode_t mode = S_IFSOCK |
1116 (SOCK_INODE(sk->sk_socket)->i_mode & ~current_umask());
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001117 struct unix_sock *u = unix_sk(sk);
Al Viro71e6be62021-06-19 03:50:30 +00001118 struct user_namespace *ns; // barf...
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001119 struct unix_address *addr;
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001120 unsigned int new_hash;
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001121 struct dentry *dentry;
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001122 struct path parent;
Al Viro71e6be62021-06-19 03:50:30 +00001123 int err;
1124
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001125 unix_mkname_bsd(sunaddr, addr_len);
1126 addr_len = strlen(sunaddr->sun_path) +
1127 offsetof(struct sockaddr_un, sun_path) + 1;
1128
1129 addr = unix_create_addr(sunaddr, addr_len);
1130 if (!addr)
1131 return -ENOMEM;
1132
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001133 /*
1134 * Get the parent directory, calculate the hash for last
1135 * component.
1136 */
Al Viro71e6be62021-06-19 03:50:30 +00001137 dentry = kern_path_create(AT_FDCWD, addr->name->sun_path, &parent, 0);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001138 if (IS_ERR(dentry)) {
1139 err = PTR_ERR(dentry);
1140 goto out;
1141 }
Al Virofaf02012012-07-20 02:37:29 +04001142
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001143 /*
1144 * All right, let's create it.
1145 */
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001146 ns = mnt_user_ns(parent.mnt);
Al Viro71e6be62021-06-19 03:50:30 +00001147 err = security_path_mknod(&parent, dentry, mode, 0);
Al Viro56c17312021-06-19 03:50:31 +00001148 if (!err)
Al Viro71e6be62021-06-19 03:50:30 +00001149 err = vfs_mknod(ns, d_inode(parent.dentry), dentry, mode, 0);
Al Viroc0c3b8d2021-06-19 03:50:32 +00001150 if (err)
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001151 goto out_path;
Al Virofa42d912021-06-19 03:50:29 +00001152 err = mutex_lock_interruptible(&u->bindlock);
Al Viroc0c3b8d2021-06-19 03:50:32 +00001153 if (err)
1154 goto out_unlink;
1155 if (u->addr)
1156 goto out_unlock;
Al Virofa42d912021-06-19 03:50:29 +00001157
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001158 new_hash = unix_bsd_hash(d_backing_inode(dentry));
Al Virofa42d912021-06-19 03:50:29 +00001159 spin_lock(&unix_table_lock);
Al Viro56c17312021-06-19 03:50:31 +00001160 u->path.mnt = mntget(parent.mnt);
1161 u->path.dentry = dget(dentry);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001162 __unix_set_addr_hash(sk, addr, new_hash);
Al Virofa42d912021-06-19 03:50:29 +00001163 spin_unlock(&unix_table_lock);
1164 mutex_unlock(&u->bindlock);
Al Viro56c17312021-06-19 03:50:31 +00001165 done_path_create(&parent, dentry);
Al Virofa42d912021-06-19 03:50:29 +00001166 return 0;
Al Viroc0c3b8d2021-06-19 03:50:32 +00001167
1168out_unlock:
1169 mutex_unlock(&u->bindlock);
1170 err = -EINVAL;
1171out_unlink:
1172 /* failed after successful mknod? unlink what we'd created... */
1173 vfs_unlink(ns, d_inode(parent.dentry), dentry, NULL);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001174out_path:
Al Viroc0c3b8d2021-06-19 03:50:32 +00001175 done_path_create(&parent, dentry);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001176out:
1177 unix_release_addr(addr);
1178 return err == -EEXIST ? -EADDRINUSE : err;
Al Virofa42d912021-06-19 03:50:29 +00001179}
1180
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001181static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr,
1182 int addr_len)
Al Virofa42d912021-06-19 03:50:29 +00001183{
1184 struct unix_sock *u = unix_sk(sk);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001185 struct unix_address *addr;
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001186 unsigned int new_hash;
Al Virofa42d912021-06-19 03:50:29 +00001187 int err;
1188
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001189 addr = unix_create_addr(sunaddr, addr_len);
1190 if (!addr)
1191 return -ENOMEM;
1192
Al Virofa42d912021-06-19 03:50:29 +00001193 err = mutex_lock_interruptible(&u->bindlock);
1194 if (err)
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001195 goto out;
Al Virofa42d912021-06-19 03:50:29 +00001196
1197 if (u->addr) {
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001198 err = -EINVAL;
1199 goto out_mutex;
Al Virofa42d912021-06-19 03:50:29 +00001200 }
1201
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001202 new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
Al Virofa42d912021-06-19 03:50:29 +00001203 spin_lock(&unix_table_lock);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001204
Al Virofa42d912021-06-19 03:50:29 +00001205 if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len,
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001206 new_hash))
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001207 goto out_spin;
1208
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001209 __unix_set_addr_hash(sk, addr, new_hash);
Al Virofa42d912021-06-19 03:50:29 +00001210 spin_unlock(&unix_table_lock);
1211 mutex_unlock(&u->bindlock);
1212 return 0;
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001213
1214out_spin:
1215 spin_unlock(&unix_table_lock);
1216 err = -EADDRINUSE;
1217out_mutex:
1218 mutex_unlock(&u->bindlock);
1219out:
1220 unix_release_addr(addr);
1221 return err;
Al Virofa42d912021-06-19 03:50:29 +00001222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1225{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001226 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Kuniyuki Iwashima5c32a3e2021-11-24 11:14:25 +09001227 struct sock *sk = sock->sk;
Kuniyuki Iwashima5c32a3e2021-11-24 11:14:25 +09001228 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001230 if (addr_len == offsetof(struct sockaddr_un, sun_path) &&
1231 sunaddr->sun_family == AF_UNIX)
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001232 return unix_autobind(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001234 err = unix_validate_addr(sunaddr, addr_len);
1235 if (err)
1236 return err;
1237
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001238 if (sunaddr->sun_path[0])
1239 err = unix_bind_bsd(sk, sunaddr, addr_len);
Al Virofa42d912021-06-19 03:50:29 +00001240 else
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001241 err = unix_bind_abstract(sk, sunaddr, addr_len);
1242
1243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
David S. Miller278a3de2007-05-31 15:19:20 -07001246static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
1247{
1248 if (unlikely(sk1 == sk2) || !sk2) {
1249 unix_state_lock(sk1);
1250 return;
1251 }
1252 if (sk1 < sk2) {
1253 unix_state_lock(sk1);
1254 unix_state_lock_nested(sk2);
1255 } else {
1256 unix_state_lock(sk2);
1257 unix_state_lock_nested(sk1);
1258 }
1259}
1260
1261static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
1262{
1263 if (unlikely(sk1 == sk2) || !sk2) {
1264 unix_state_unlock(sk1);
1265 return;
1266 }
1267 unix_state_unlock(sk1);
1268 unix_state_unlock(sk2);
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
1272 int alen, int flags)
1273{
1274 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001275 struct net *net = sock_net(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001276 struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct sock *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int err;
1279
Mateusz Jurczykdefbcf22017-06-08 11:13:36 +02001280 err = -EINVAL;
1281 if (alen < offsetofend(struct sockaddr, sa_family))
1282 goto out;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (addr->sa_family != AF_UNSPEC) {
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001285 err = unix_validate_addr(sunaddr, alen);
1286 if (err)
1287 goto out;
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001290 !unix_sk(sk)->addr) {
1291 err = unix_autobind(sk);
1292 if (err)
1293 goto out;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
David S. Miller278a3de2007-05-31 15:19:20 -07001296restart:
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001297 other = unix_find_other(net, sunaddr, alen, sock->type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001298 if (IS_ERR(other)) {
1299 err = PTR_ERR(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 goto out;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
David S. Miller278a3de2007-05-31 15:19:20 -07001303 unix_state_double_lock(sk, other);
1304
1305 /* Apparently VFS overslept socket death. Retry. */
1306 if (sock_flag(other, SOCK_DEAD)) {
1307 unix_state_double_unlock(sk, other);
1308 sock_put(other);
1309 goto restart;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 err = -EPERM;
1313 if (!unix_may_send(sk, other))
1314 goto out_unlock;
1315
1316 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1317 if (err)
1318 goto out_unlock;
1319
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001320 sk->sk_state = other->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 } else {
1322 /*
1323 * 1003.1g breaking connected state with AF_UNSPEC
1324 */
1325 other = NULL;
David S. Miller278a3de2007-05-31 15:19:20 -07001326 unix_state_double_lock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 /*
1330 * If it was connected, reconnect.
1331 */
1332 if (unix_peer(sk)) {
1333 struct sock *old_peer = unix_peer(sk);
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001334
Jianjun Konge27dfce2008-11-01 21:38:31 -07001335 unix_peer(sk) = other;
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001336 if (!other)
1337 sk->sk_state = TCP_CLOSE;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001338 unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
1339
David S. Miller278a3de2007-05-31 15:19:20 -07001340 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 if (other != old_peer)
1343 unix_dgram_disconnected(sk, old_peer);
1344 sock_put(old_peer);
1345 } else {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001346 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001347 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Cong Wang83301b52021-07-04 12:02:45 -07001349
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352out_unlock:
David S. Miller278a3de2007-05-31 15:19:20 -07001353 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 sock_put(other);
1355out:
1356 return err;
1357}
1358
1359static long unix_wait_for_peer(struct sock *other, long timeo)
Jules Irenge48851e92020-02-23 23:16:56 +00001360 __releases(&unix_sk(other)->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct unix_sock *u = unix_sk(other);
1363 int sched;
1364 DEFINE_WAIT(wait);
1365
1366 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
1367
1368 sched = !sock_flag(other, SOCK_DEAD) &&
1369 !(other->sk_shutdown & RCV_SHUTDOWN) &&
Rainer Weikusat3c734192008-06-17 22:28:05 -07001370 unix_recvq_full(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
David S. Miller1c92b4e2007-05-31 13:24:26 -07001372 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (sched)
1375 timeo = schedule_timeout(timeo);
1376
1377 finish_wait(&u->peer_wait, &wait);
1378 return timeo;
1379}
1380
1381static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1382 int addr_len, int flags)
1383{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001384 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001386 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1388 struct sock *newsk = NULL;
1389 struct sock *other = NULL;
1390 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int st;
1392 int err;
1393 long timeo;
1394
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001395 err = unix_validate_addr(sunaddr, addr_len);
1396 if (err)
1397 goto out;
1398
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001399 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) {
1400 err = unix_autobind(sk);
1401 if (err)
1402 goto out;
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1406
1407 /* First of all allocate resources.
1408 If we will make it after state is locked,
1409 we will have to recheck all again in any case.
1410 */
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /* create new sock for complete connection */
Jiang Wang94531cf2021-08-16 19:03:21 +00001413 newsk = unix_create1(sock_net(sk), NULL, 0, sock->type);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +09001414 if (IS_ERR(newsk)) {
1415 err = PTR_ERR(newsk);
1416 newsk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto out;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +09001418 }
1419
1420 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* Allocate skb for sending to listening sock */
1423 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1424 if (skb == NULL)
1425 goto out;
1426
1427restart:
1428 /* Find listening sock. */
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001429 other = unix_find_other(net, sunaddr, addr_len, sk->sk_type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001430 if (IS_ERR(other)) {
1431 err = PTR_ERR(other);
1432 other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto out;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /* Latch state of peer */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001437 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* Apparently VFS overslept socket death. Retry. */
1440 if (sock_flag(other, SOCK_DEAD)) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001441 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 sock_put(other);
1443 goto restart;
1444 }
1445
1446 err = -ECONNREFUSED;
1447 if (other->sk_state != TCP_LISTEN)
1448 goto out_unlock;
Tomoki Sekiyama77238f22009-10-18 23:17:37 -07001449 if (other->sk_shutdown & RCV_SHUTDOWN)
1450 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Rainer Weikusat3c734192008-06-17 22:28:05 -07001452 if (unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 err = -EAGAIN;
1454 if (!timeo)
1455 goto out_unlock;
1456
1457 timeo = unix_wait_for_peer(other, timeo);
1458
1459 err = sock_intr_errno(timeo);
1460 if (signal_pending(current))
1461 goto out;
1462 sock_put(other);
1463 goto restart;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /* Latch our state.
1467
Daniel Balutae5537bf2011-03-14 15:25:33 -07001468 It is tricky place. We need to grab our state lock and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 drop lock on peer. It is dangerous because deadlock is
1470 possible. Connect to self case and simultaneous
1471 attempt to connect are eliminated by checking socket
1472 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1473 check this before attempt to grab lock.
1474
1475 Well, and we have to recheck the state after socket locked.
1476 */
1477 st = sk->sk_state;
1478
1479 switch (st) {
1480 case TCP_CLOSE:
1481 /* This is ok... continue with connect */
1482 break;
1483 case TCP_ESTABLISHED:
1484 /* Socket is already connected */
1485 err = -EISCONN;
1486 goto out_unlock;
1487 default:
1488 err = -EINVAL;
1489 goto out_unlock;
1490 }
1491
David S. Miller1c92b4e2007-05-31 13:24:26 -07001492 unix_state_lock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (sk->sk_state != st) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001495 unix_state_unlock(sk);
1496 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sock_put(other);
1498 goto restart;
1499 }
1500
David S. Miller3610cda2011-01-05 15:38:53 -08001501 err = security_unix_stream_connect(sk, other, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (err) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001503 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto out_unlock;
1505 }
1506
1507 /* The way is open! Fastly set all the necessary fields... */
1508
1509 sock_hold(sk);
1510 unix_peer(newsk) = sk;
1511 newsk->sk_state = TCP_ESTABLISHED;
1512 newsk->sk_type = sk->sk_type;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001513 init_peercred(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 newu = unix_sk(newsk);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001515 RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 otheru = unix_sk(other);
1517
Al Viroae3b5642019-02-15 20:09:35 +00001518 /* copy address information from listening to new sock
1519 *
1520 * The contents of *(otheru->addr) and otheru->path
1521 * are seen fully set up here, since we have found
1522 * otheru in hash under unix_table_lock. Insertion
1523 * into the hash chain we'd found it in had been done
1524 * in an earlier critical area protected by unix_table_lock,
1525 * the same one where we'd set *(otheru->addr) contents,
1526 * as well as otheru->path and otheru->addr itself.
1527 *
1528 * Using smp_store_release() here to set newu->addr
1529 * is enough to make those stores, as well as stores
1530 * to newu->path visible to anyone who gets newu->addr
1531 * by smp_load_acquire(). IOW, the same warranties
1532 * as for unix_sock instances bound in unix_bind() or
1533 * in unix_autobind().
1534 */
Al Viro40ffe672012-03-14 21:54:32 -04001535 if (otheru->path.dentry) {
1536 path_get(&otheru->path);
1537 newu->path = otheru->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Al Viroae3b5642019-02-15 20:09:35 +00001539 refcount_inc(&otheru->addr->refcnt);
1540 smp_store_release(&newu->addr, otheru->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* Set credentials */
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001543 copy_peercred(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 sock->state = SS_CONNECTED;
1546 sk->sk_state = TCP_ESTABLISHED;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001547 sock_hold(newsk);
1548
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001549 smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001550 unix_peer(sk) = newsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David S. Miller1c92b4e2007-05-31 13:24:26 -07001552 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
gushengxian4e03d072021-06-09 20:09:35 -07001554 /* take ten and send info to listening sock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 spin_lock(&other->sk_receive_queue.lock);
1556 __skb_queue_tail(&other->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_unlock(&other->sk_receive_queue.lock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001558 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04001559 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 sock_put(other);
1561 return 0;
1562
1563out_unlock:
1564 if (other)
David S. Miller1c92b4e2007-05-31 13:24:26 -07001565 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567out:
Wei Yongjun40d44442009-02-25 00:32:45 +00001568 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (newsk)
1570 unix_release_sock(newsk, 0);
1571 if (other)
1572 sock_put(other);
1573 return err;
1574}
1575
1576static int unix_socketpair(struct socket *socka, struct socket *sockb)
1577{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001578 struct sock *ska = socka->sk, *skb = sockb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* Join our sockets back to back */
1581 sock_hold(ska);
1582 sock_hold(skb);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001583 unix_peer(ska) = skb;
1584 unix_peer(skb) = ska;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001585 init_peercred(ska);
1586 init_peercred(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Cong Wang83301b52021-07-04 12:02:45 -07001588 ska->sk_state = TCP_ESTABLISHED;
1589 skb->sk_state = TCP_ESTABLISHED;
1590 socka->state = SS_CONNECTED;
1591 sockb->state = SS_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593}
1594
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001595static void unix_sock_inherit_flags(const struct socket *old,
1596 struct socket *new)
1597{
1598 if (test_bit(SOCK_PASSCRED, &old->flags))
1599 set_bit(SOCK_PASSCRED, &new->flags);
1600 if (test_bit(SOCK_PASSSEC, &old->flags))
1601 set_bit(SOCK_PASSSEC, &new->flags);
1602}
1603
David Howellscdfbabf2017-03-09 08:09:05 +00001604static int unix_accept(struct socket *sock, struct socket *newsock, int flags,
1605 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 struct sock *sk = sock->sk;
1608 struct sock *tsk;
1609 struct sk_buff *skb;
1610 int err;
1611
1612 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001613 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto out;
1615
1616 err = -EINVAL;
1617 if (sk->sk_state != TCP_LISTEN)
1618 goto out;
1619
1620 /* If socket state is TCP_LISTEN it cannot change (for now...),
1621 * so that no locks are necessary.
1622 */
1623
1624 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1625 if (!skb) {
1626 /* This means receive shutdown. */
1627 if (err == 0)
1628 err = -EINVAL;
1629 goto out;
1630 }
1631
1632 tsk = skb->sk;
1633 skb_free_datagram(sk, skb);
1634 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1635
1636 /* attach accepted sock to socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001637 unix_state_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 newsock->state = SS_CONNECTED;
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001639 unix_sock_inherit_flags(sock, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sock_graft(tsk, newsock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001641 unix_state_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return 0;
1643
1644out:
1645 return err;
1646}
1647
1648
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001649static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct sock *sk = sock->sk;
Al Viroae3b5642019-02-15 20:09:35 +00001652 struct unix_address *addr;
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001653 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int err = 0;
1655
1656 if (peer) {
1657 sk = unix_peer_get(sk);
1658
1659 err = -ENOTCONN;
1660 if (!sk)
1661 goto out;
1662 err = 0;
1663 } else {
1664 sock_hold(sk);
1665 }
1666
Al Viroae3b5642019-02-15 20:09:35 +00001667 addr = smp_load_acquire(&unix_sk(sk)->addr);
1668 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 sunaddr->sun_family = AF_UNIX;
1670 sunaddr->sun_path[0] = 0;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09001671 err = offsetof(struct sockaddr_un, sun_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 } else {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001673 err = addr->len;
1674 memcpy(sunaddr, addr->name, addr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 sock_put(sk);
1677out:
1678 return err;
1679}
1680
Miklos Szeredicbcf0112021-07-28 14:47:20 +02001681static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
1682{
1683 scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1684
1685 /*
1686 * Garbage collection of unix sockets starts by selecting a set of
1687 * candidate sockets which have reference only from being in flight
1688 * (total_refs == inflight_refs). This condition is checked once during
1689 * the candidate collection phase, and candidates are marked as such, so
1690 * that non-candidates can later be ignored. While inflight_refs is
1691 * protected by unix_gc_lock, total_refs (file count) is not, hence this
1692 * is an instantaneous decision.
1693 *
1694 * Once a candidate, however, the socket must not be reinstalled into a
1695 * file descriptor while the garbage collection is in progress.
1696 *
1697 * If the above conditions are met, then the directed graph of
1698 * candidates (*) does not change while unix_gc_lock is held.
1699 *
1700 * Any operations that changes the file count through file descriptors
1701 * (dup, close, sendmsg) does not change the graph since candidates are
1702 * not installed in fds.
1703 *
1704 * Dequeing a candidate via recvmsg would install it into an fd, but
1705 * that takes unix_gc_lock to decrement the inflight count, so it's
1706 * serialized with garbage collection.
1707 *
1708 * MSG_PEEK is special in that it does not change the inflight count,
1709 * yet does install the socket into an fd. The following lock/unlock
1710 * pair is to ensure serialization with garbage collection. It must be
1711 * done between incrementing the file count and installing the file into
1712 * an fd.
1713 *
1714 * If garbage collection starts after the barrier provided by the
1715 * lock/unlock, then it will see the elevated refcount and not mark this
1716 * as a candidate. If a garbage collection is already in progress
1717 * before the file count was incremented, then the lock/unlock pair will
1718 * ensure that garbage collection is finished before progressing to
1719 * installing the fd.
1720 *
1721 * (*) A -> B where B is on the queue of A or B is on the queue of C
1722 * which is on the queue of listening socket A.
1723 */
1724 spin_lock(&unix_gc_lock);
1725 spin_unlock(&unix_gc_lock);
1726}
1727
David S. Millerf78a5fd2011-09-16 19:34:00 -04001728static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001729{
1730 int err = 0;
Eric Dumazet16e57262011-09-19 05:52:27 +00001731
David S. Millerf78a5fd2011-09-16 19:34:00 -04001732 UNIXCB(skb).pid = get_pid(scm->pid);
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001733 UNIXCB(skb).uid = scm->creds.uid;
1734 UNIXCB(skb).gid = scm->creds.gid;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001735 UNIXCB(skb).fp = NULL;
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04001736 unix_get_secdata(scm, skb);
Eric W. Biederman7361c362010-06-13 03:34:33 +00001737 if (scm->fp && send_fds)
1738 err = unix_attach_fds(scm, skb);
1739
1740 skb->destructor = unix_destruct_scm;
1741 return err;
1742}
1743
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001744static bool unix_passcred_enabled(const struct socket *sock,
1745 const struct sock *other)
1746{
1747 return test_bit(SOCK_PASSCRED, &sock->flags) ||
1748 !other->sk_socket ||
1749 test_bit(SOCK_PASSCRED, &other->sk_socket->flags);
1750}
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752/*
Eric Dumazet16e57262011-09-19 05:52:27 +00001753 * Some apps rely on write() giving SCM_CREDENTIALS
1754 * We include credentials if source or destination socket
1755 * asserted SOCK_PASSCRED.
1756 */
1757static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1758 const struct sock *other)
1759{
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001760 if (UNIXCB(skb).pid)
Eric Dumazet16e57262011-09-19 05:52:27 +00001761 return;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001762 if (unix_passcred_enabled(sock, other)) {
Eric Dumazet16e57262011-09-19 05:52:27 +00001763 UNIXCB(skb).pid = get_pid(task_tgid(current));
David S. Miller6e0895c2013-04-22 20:32:51 -04001764 current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
Eric Dumazet16e57262011-09-19 05:52:27 +00001765 }
1766}
1767
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001768static int maybe_init_creds(struct scm_cookie *scm,
1769 struct socket *socket,
1770 const struct sock *other)
1771{
1772 int err;
1773 struct msghdr msg = { .msg_controllen = 0 };
1774
1775 err = scm_send(socket, &msg, scm, false);
1776 if (err)
1777 return err;
1778
1779 if (unix_passcred_enabled(socket, other)) {
1780 scm->pid = get_pid(task_tgid(current));
1781 current_uid_gid(&scm->creds.uid, &scm->creds.gid);
1782 }
1783 return err;
1784}
1785
1786static bool unix_skb_scm_eq(struct sk_buff *skb,
1787 struct scm_cookie *scm)
1788{
1789 const struct unix_skb_parms *u = &UNIXCB(skb);
1790
1791 return u->pid == scm->pid &&
1792 uid_eq(u->uid, scm->creds.uid) &&
1793 gid_eq(u->gid, scm->creds.gid) &&
1794 unix_secdata_eq(scm, skb);
1795}
1796
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001797static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
1798{
1799 struct scm_fp_list *fp = UNIXCB(skb).fp;
1800 struct unix_sock *u = unix_sk(sk);
1801
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001802 if (unlikely(fp && fp->count))
Paolo Abeni77820402020-02-28 14:45:21 +01001803 atomic_add(fp->count, &u->scm_stat.nr_fds);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001804}
1805
1806static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
1807{
1808 struct scm_fp_list *fp = UNIXCB(skb).fp;
1809 struct unix_sock *u = unix_sk(sk);
1810
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001811 if (unlikely(fp && fp->count))
Paolo Abeni77820402020-02-28 14:45:21 +01001812 atomic_sub(fp->count, &u->scm_stat.nr_fds);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001813}
1814
Eric Dumazet16e57262011-09-19 05:52:27 +00001815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * Send AF_UNIX data.
1817 */
1818
Ying Xue1b784142015-03-02 15:37:48 +08001819static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1820 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001823 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct unix_sock *u = unix_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001825 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct sock *other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 int err;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001828 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 long timeo;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001830 struct scm_cookie scm;
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001831 int data_len = 0;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001832 int sk_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
dann frazier5f23b732008-11-26 15:32:27 -08001834 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001835 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (err < 0)
1837 return err;
1838
1839 err = -EOPNOTSUPP;
1840 if (msg->msg_flags&MSG_OOB)
1841 goto out;
1842
1843 if (msg->msg_namelen) {
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001844 err = unix_validate_addr(sunaddr, msg->msg_namelen);
1845 if (err)
1846 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 } else {
1848 sunaddr = NULL;
1849 err = -ENOTCONN;
1850 other = unix_peer_get(sk);
1851 if (!other)
1852 goto out;
1853 }
1854
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001855 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) {
1856 err = unix_autobind(sk);
1857 if (err)
1858 goto out;
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 err = -EMSGSIZE;
1862 if (len > sk->sk_sndbuf - 32)
1863 goto out;
1864
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001865 if (len > SKB_MAX_ALLOC) {
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001866 data_len = min_t(size_t,
1867 len - SKB_MAX_ALLOC,
1868 MAX_SKB_FRAGS * PAGE_SIZE);
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001869 data_len = PAGE_ALIGN(data_len);
1870
1871 BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
1872 }
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001873
1874 skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07001875 msg->msg_flags & MSG_DONTWAIT, &err,
1876 PAGE_ALLOC_COSTLY_ORDER);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001877 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto out;
1879
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001880 err = unix_scm_to_skb(&scm, skb, true);
Eric Dumazet25888e32010-11-25 04:11:39 +00001881 if (err < 0)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001882 goto out_free;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001883
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001884 skb_put(skb, len - data_len);
1885 skb->data_len = data_len;
1886 skb->len = len;
Al Viroc0371da2014-11-24 10:42:55 -05001887 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (err)
1889 goto out_free;
1890
1891 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1892
1893restart:
1894 if (!other) {
1895 err = -ECONNRESET;
1896 if (sunaddr == NULL)
1897 goto out_free;
1898
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001899 other = unix_find_other(net, sunaddr, msg->msg_namelen,
1900 sk->sk_type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001901 if (IS_ERR(other)) {
1902 err = PTR_ERR(other);
1903 other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto out_free;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
Alban Crequyd6ae3ba2011-01-18 06:39:15 +00001908 if (sk_filter(other, skb) < 0) {
1909 /* Toss the packet but do not return any error to the sender */
1910 err = len;
1911 goto out_free;
1912 }
1913
Rainer Weikusat7d267272015-11-20 22:07:23 +00001914 sk_locked = 0;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001915 unix_state_lock(other);
Rainer Weikusat7d267272015-11-20 22:07:23 +00001916restart_locked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 err = -EPERM;
1918 if (!unix_may_send(sk, other))
1919 goto out_unlock;
1920
Rainer Weikusat7d267272015-11-20 22:07:23 +00001921 if (unlikely(sock_flag(other, SOCK_DEAD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /*
1923 * Check with 1003.1g - what should
1924 * datagram error
1925 */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001926 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 sock_put(other);
1928
Rainer Weikusat7d267272015-11-20 22:07:23 +00001929 if (!sk_locked)
1930 unix_state_lock(sk);
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (unix_peer(sk) == other) {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001934 unix_peer(sk) = NULL;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001935 unix_dgram_peer_wake_disconnect_wakeup(sk, other);
1936
David S. Miller1c92b4e2007-05-31 13:24:26 -07001937 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001939 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unix_dgram_disconnected(sk, other);
1941 sock_put(other);
1942 err = -ECONNREFUSED;
1943 } else {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001944 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
1947 other = NULL;
1948 if (err)
1949 goto out_free;
1950 goto restart;
1951 }
1952
1953 err = -EPIPE;
1954 if (other->sk_shutdown & RCV_SHUTDOWN)
1955 goto out_unlock;
1956
1957 if (sk->sk_type != SOCK_SEQPACKET) {
1958 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1959 if (err)
1960 goto out_unlock;
1961 }
1962
Rainer Weikusata5527dd2016-02-11 19:37:27 +00001963 /* other == sk && unix_peer(other) != sk if
1964 * - unix_peer(sk) == NULL, destination address bound to sk
1965 * - unix_peer(sk) == sk by time of get but disconnected before lock
1966 */
1967 if (other != sk &&
Qian Cai86b18aa2020-02-04 13:40:29 -05001968 unlikely(unix_peer(other) != sk &&
1969 unix_recvq_full_lockless(other))) {
Rainer Weikusat7d267272015-11-20 22:07:23 +00001970 if (timeo) {
1971 timeo = unix_wait_for_peer(other, timeo);
1972
1973 err = sock_intr_errno(timeo);
1974 if (signal_pending(current))
1975 goto out_free;
1976
1977 goto restart;
1978 }
1979
1980 if (!sk_locked) {
1981 unix_state_unlock(other);
1982 unix_state_double_lock(sk, other);
1983 }
1984
1985 if (unix_peer(sk) != other ||
1986 unix_dgram_peer_wake_me(sk, other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 err = -EAGAIN;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001988 sk_locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto out_unlock;
1990 }
1991
Rainer Weikusat7d267272015-11-20 22:07:23 +00001992 if (!sk_locked) {
1993 sk_locked = 1;
1994 goto restart_locked;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Rainer Weikusat7d267272015-11-20 22:07:23 +00001998 if (unlikely(sk_locked))
1999 unix_state_unlock(sk);
2000
Alban Crequy3f661162010-10-04 08:48:28 +00002001 if (sock_flag(other, SOCK_RCVTSTAMP))
2002 __net_timestamp(skb);
Eric Dumazet16e57262011-09-19 05:52:27 +00002003 maybe_add_creds(skb, sock, other);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002004 scm_stat_add(other, skb);
Paolo Abeni77820402020-02-28 14:45:21 +01002005 skb_queue_tail(&other->sk_receive_queue, skb);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002006 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04002007 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002009 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return len;
2011
2012out_unlock:
Rainer Weikusat7d267272015-11-20 22:07:23 +00002013 if (sk_locked)
2014 unix_state_unlock(sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002015 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016out_free:
2017 kfree_skb(skb);
2018out:
2019 if (other)
2020 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002021 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return err;
2023}
2024
Eric Dumazete370a722013-08-08 14:37:32 -07002025/* We use paged skbs for stream sockets, and limit occupancy to 32768
Tobias Klauserd4e9a402018-02-13 11:11:30 +01002026 * bytes, and a minimum of a full page.
Eric Dumazete370a722013-08-08 14:37:32 -07002027 */
2028#define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002029
Rao Shoaib314001f2021-08-01 00:57:07 -07002030#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2031static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other)
2032{
2033 struct unix_sock *ousk = unix_sk(other);
2034 struct sk_buff *skb;
2035 int err = 0;
2036
2037 skb = sock_alloc_send_skb(sock->sk, 1, msg->msg_flags & MSG_DONTWAIT, &err);
2038
2039 if (!skb)
2040 return err;
2041
2042 skb_put(skb, 1);
Rao Shoaib314001f2021-08-01 00:57:07 -07002043 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1);
2044
2045 if (err) {
2046 kfree_skb(skb);
2047 return err;
2048 }
2049
2050 unix_state_lock(other);
Rao Shoaib19eed722021-08-13 11:19:34 -07002051
2052 if (sock_flag(other, SOCK_DEAD) ||
2053 (other->sk_shutdown & RCV_SHUTDOWN)) {
2054 unix_state_unlock(other);
2055 kfree_skb(skb);
2056 return -EPIPE;
2057 }
2058
Rao Shoaib314001f2021-08-01 00:57:07 -07002059 maybe_add_creds(skb, sock, other);
2060 skb_get(skb);
2061
2062 if (ousk->oob_skb)
Rao Shoaib19eed722021-08-13 11:19:34 -07002063 consume_skb(ousk->oob_skb);
Rao Shoaib314001f2021-08-01 00:57:07 -07002064
2065 ousk->oob_skb = skb;
2066
2067 scm_stat_add(other, skb);
2068 skb_queue_tail(&other->sk_receive_queue, skb);
2069 sk_send_sigurg(other);
2070 unix_state_unlock(other);
2071 other->sk_data_ready(other);
2072
2073 return err;
2074}
2075#endif
2076
Ying Xue1b784142015-03-02 15:37:48 +08002077static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
2078 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 struct sock *sk = sock->sk;
2081 struct sock *other = NULL;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002082 int err, size;
David S. Millerf78a5fd2011-09-16 19:34:00 -04002083 struct sk_buff *skb;
Jianjun Konge27dfce2008-11-01 21:38:31 -07002084 int sent = 0;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002085 struct scm_cookie scm;
Miklos Szeredi8ba69ba2009-09-11 11:31:45 -07002086 bool fds_sent = false;
Eric Dumazete370a722013-08-08 14:37:32 -07002087 int data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
dann frazier5f23b732008-11-26 15:32:27 -08002089 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002090 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (err < 0)
2092 return err;
2093
2094 err = -EOPNOTSUPP;
Rao Shoaib314001f2021-08-01 00:57:07 -07002095 if (msg->msg_flags & MSG_OOB) {
2096#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2097 if (len)
2098 len--;
2099 else
2100#endif
2101 goto out_err;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 if (msg->msg_namelen) {
2105 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
2106 goto out_err;
2107 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 err = -ENOTCONN;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08002109 other = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!other)
2111 goto out_err;
2112 }
2113
2114 if (sk->sk_shutdown & SEND_SHUTDOWN)
2115 goto pipe_err;
2116
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002117 while (sent < len) {
Eric Dumazete370a722013-08-08 14:37:32 -07002118 size = len - sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /* Keep two messages in the pipe so it schedules better */
Eric Dumazete370a722013-08-08 14:37:32 -07002121 size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Eric Dumazete370a722013-08-08 14:37:32 -07002123 /* allow fallback to order-0 allocations */
2124 size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002125
Eric Dumazete370a722013-08-08 14:37:32 -07002126 data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002127
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04002128 data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
2129
Eric Dumazete370a722013-08-08 14:37:32 -07002130 skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07002131 msg->msg_flags & MSG_DONTWAIT, &err,
2132 get_order(UNIX_SKB_FRAGS_SZ));
Eric Dumazete370a722013-08-08 14:37:32 -07002133 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto out_err;
2135
David S. Millerf78a5fd2011-09-16 19:34:00 -04002136 /* Only send the fds in the first buffer */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002137 err = unix_scm_to_skb(&scm, skb, !fds_sent);
Eric Dumazet25888e32010-11-25 04:11:39 +00002138 if (err < 0) {
Eric W. Biederman7361c362010-06-13 03:34:33 +00002139 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04002140 goto out_err;
Miklos Szeredi62093442008-11-09 15:23:57 +01002141 }
Eric W. Biederman7361c362010-06-13 03:34:33 +00002142 fds_sent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Eric Dumazete370a722013-08-08 14:37:32 -07002144 skb_put(skb, size - data_len);
2145 skb->data_len = data_len;
2146 skb->len = size;
Al Viroc0371da2014-11-24 10:42:55 -05002147 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002148 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04002150 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152
David S. Miller1c92b4e2007-05-31 13:24:26 -07002153 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 if (sock_flag(other, SOCK_DEAD) ||
2156 (other->sk_shutdown & RCV_SHUTDOWN))
2157 goto pipe_err_free;
2158
Eric Dumazet16e57262011-09-19 05:52:27 +00002159 maybe_add_creds(skb, sock, other);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002160 scm_stat_add(other, skb);
Paolo Abeni77820402020-02-28 14:45:21 +01002161 skb_queue_tail(&other->sk_receive_queue, skb);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002162 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04002163 other->sk_data_ready(other);
Jianjun Konge27dfce2008-11-01 21:38:31 -07002164 sent += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Rao Shoaib314001f2021-08-01 00:57:07 -07002167#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2168 if (msg->msg_flags & MSG_OOB) {
2169 err = queue_oob(sock, msg, other);
2170 if (err)
2171 goto out_err;
2172 sent++;
2173 }
2174#endif
2175
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002176 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 return sent;
2179
2180pipe_err_free:
David S. Miller1c92b4e2007-05-31 13:24:26 -07002181 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 kfree_skb(skb);
2183pipe_err:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002184 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
2185 send_sig(SIGPIPE, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 err = -EPIPE;
2187out_err:
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002188 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return sent ? : err;
2190}
2191
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002192static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
2193 int offset, size_t size, int flags)
2194{
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002195 int err;
2196 bool send_sigpipe = false;
2197 bool init_scm = true;
2198 struct scm_cookie scm;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002199 struct sock *other, *sk = socket->sk;
2200 struct sk_buff *skb, *newskb = NULL, *tail = NULL;
2201
2202 if (flags & MSG_OOB)
2203 return -EOPNOTSUPP;
2204
2205 other = unix_peer(sk);
2206 if (!other || sk->sk_state != TCP_ESTABLISHED)
2207 return -ENOTCONN;
2208
2209 if (false) {
2210alloc_skb:
2211 unix_state_unlock(other);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002212 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002213 newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
2214 &err, 0);
2215 if (!newskb)
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002216 goto err;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002217 }
2218
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002219 /* we must acquire iolock as we modify already present
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002220 * skbs in the sk_receive_queue and mess with skb->len
2221 */
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002222 err = mutex_lock_interruptible(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002223 if (err) {
2224 err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002225 goto err;
2226 }
2227
2228 if (sk->sk_shutdown & SEND_SHUTDOWN) {
2229 err = -EPIPE;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002230 send_sigpipe = true;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002231 goto err_unlock;
2232 }
2233
2234 unix_state_lock(other);
2235
2236 if (sock_flag(other, SOCK_DEAD) ||
2237 other->sk_shutdown & RCV_SHUTDOWN) {
2238 err = -EPIPE;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002239 send_sigpipe = true;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002240 goto err_state_unlock;
2241 }
2242
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002243 if (init_scm) {
2244 err = maybe_init_creds(&scm, socket, other);
2245 if (err)
2246 goto err_state_unlock;
2247 init_scm = false;
2248 }
2249
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002250 skb = skb_peek_tail(&other->sk_receive_queue);
2251 if (tail && tail == skb) {
2252 skb = newskb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002253 } else if (!skb || !unix_skb_scm_eq(skb, &scm)) {
2254 if (newskb) {
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002255 skb = newskb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002256 } else {
2257 tail = skb;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002258 goto alloc_skb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002259 }
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002260 } else if (newskb) {
2261 /* this is fast path, we don't necessarily need to
2262 * call to kfree_skb even though with newskb == NULL
2263 * this - does no harm
2264 */
2265 consume_skb(newskb);
Hannes Frederic Sowa8844f972015-11-16 16:25:56 +01002266 newskb = NULL;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002267 }
2268
2269 if (skb_append_pagefrags(skb, page, offset, size)) {
2270 tail = skb;
2271 goto alloc_skb;
2272 }
2273
2274 skb->len += size;
2275 skb->data_len += size;
2276 skb->truesize += size;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002277 refcount_add(size, &sk->sk_wmem_alloc);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002278
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002279 if (newskb) {
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002280 err = unix_scm_to_skb(&scm, skb, false);
2281 if (err)
2282 goto err_state_unlock;
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002283 spin_lock(&other->sk_receive_queue.lock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002284 __skb_queue_tail(&other->sk_receive_queue, newskb);
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002285 spin_unlock(&other->sk_receive_queue.lock);
2286 }
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002287
2288 unix_state_unlock(other);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002289 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002290
2291 other->sk_data_ready(other);
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002292 scm_destroy(&scm);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002293 return size;
2294
2295err_state_unlock:
2296 unix_state_unlock(other);
2297err_unlock:
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002298 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002299err:
2300 kfree_skb(newskb);
2301 if (send_sigpipe && !(flags & MSG_NOSIGNAL))
2302 send_sig(SIGPIPE, current, 0);
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002303 if (!init_scm)
2304 scm_destroy(&scm);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002305 return err;
2306}
2307
Ying Xue1b784142015-03-02 15:37:48 +08002308static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
2309 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 int err;
2312 struct sock *sk = sock->sk;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 err = sock_error(sk);
2315 if (err)
2316 return err;
2317
2318 if (sk->sk_state != TCP_ESTABLISHED)
2319 return -ENOTCONN;
2320
2321 if (msg->msg_namelen)
2322 msg->msg_namelen = 0;
2323
Ying Xue1b784142015-03-02 15:37:48 +08002324 return unix_dgram_sendmsg(sock, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002326
Ying Xue1b784142015-03-02 15:37:48 +08002327static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
2328 size_t size, int flags)
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00002329{
2330 struct sock *sk = sock->sk;
2331
2332 if (sk->sk_state != TCP_ESTABLISHED)
2333 return -ENOTCONN;
2334
Ying Xue1b784142015-03-02 15:37:48 +08002335 return unix_dgram_recvmsg(sock, msg, size, flags);
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00002336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
2339{
Al Viroae3b5642019-02-15 20:09:35 +00002340 struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Al Viroae3b5642019-02-15 20:09:35 +00002342 if (addr) {
2343 msg->msg_namelen = addr->len;
2344 memcpy(msg->msg_name, addr->name, addr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346}
2347
Cong Wang9825d862021-07-04 12:02:48 -07002348int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
2349 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002351 struct scm_cookie scm;
Cong Wang9825d862021-07-04 12:02:48 -07002352 struct socket *sock = sk->sk_socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct unix_sock *u = unix_sk(sk);
Rainer Weikusat64874282015-12-06 21:11:38 +00002354 struct sk_buff *skb, *last;
2355 long timeo;
Paolo Abenifd69c392019-04-08 10:15:59 +02002356 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 int err;
2358
2359 err = -EOPNOTSUPP;
2360 if (flags&MSG_OOB)
2361 goto out;
2362
Rainer Weikusat64874282015-12-06 21:11:38 +00002363 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Rainer Weikusat64874282015-12-06 21:11:38 +00002365 do {
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002366 mutex_lock(&u->iolock);
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002367
Rainer Weikusat64874282015-12-06 21:11:38 +00002368 skip = sk_peek_offset(sk, flags);
Sabrina Dubrocab50b0582019-11-25 14:48:57 +01002369 skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
Paolo Abenie427cad2020-02-28 14:45:22 +01002370 &skip, &err, &last);
2371 if (skb) {
2372 if (!(flags & MSG_PEEK))
2373 scm_stat_del(sk, skb);
Rainer Weikusat64874282015-12-06 21:11:38 +00002374 break;
Paolo Abenie427cad2020-02-28 14:45:22 +01002375 }
Rainer Weikusat64874282015-12-06 21:11:38 +00002376
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002377 mutex_unlock(&u->iolock);
Rainer Weikusat64874282015-12-06 21:11:38 +00002378
2379 if (err != -EAGAIN)
2380 break;
2381 } while (timeo &&
Sabrina Dubrocab50b0582019-11-25 14:48:57 +01002382 !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
2383 &err, &timeo, last));
Rainer Weikusat64874282015-12-06 21:11:38 +00002384
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002385 if (!skb) { /* implies iolock unlocked */
Florian Zumbiehl0a112252007-11-29 23:19:23 +11002386 unix_state_lock(sk);
2387 /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
2388 if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
2389 (sk->sk_shutdown & RCV_SHUTDOWN))
2390 err = 0;
2391 unix_state_unlock(sk);
Rainer Weikusat64874282015-12-06 21:11:38 +00002392 goto out;
Florian Zumbiehl0a112252007-11-29 23:19:23 +11002393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Rainer Weikusat77b75f42015-11-26 19:23:15 +00002395 if (wq_has_sleeper(&u->peer_wait))
2396 wake_up_interruptible_sync_poll(&u->peer_wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002397 EPOLLOUT | EPOLLWRNORM |
2398 EPOLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 if (msg->msg_name)
2401 unix_copy_addr(msg, skb->sk);
2402
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002403 if (size > skb->len - skip)
2404 size = skb->len - skip;
2405 else if (size < skb->len - skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 msg->msg_flags |= MSG_TRUNC;
2407
David S. Miller51f3d022014-11-05 16:46:40 -05002408 err = skb_copy_datagram_msg(skb, skip, msg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (err)
2410 goto out_free;
2411
Alban Crequy3f661162010-10-04 08:48:28 +00002412 if (sock_flag(sk, SOCK_RCVTSTAMP))
2413 __sock_recv_timestamp(msg, sk, skb);
2414
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002415 memset(&scm, 0, sizeof(scm));
2416
2417 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
2418 unix_set_secdata(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002420 if (!(flags & MSG_PEEK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002422 unix_detach_fds(&scm, skb);
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002423
2424 sk_peek_offset_bwd(sk, skb->len);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002425 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* It is questionable: on PEEK we could:
2427 - do not return fds - good, but too simple 8)
2428 - return fds, and do not return them on read (old strategy,
2429 apparently wrong)
2430 - clone fds (I chose it for now, it is the most universal
2431 solution)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002432
2433 POSIX 1003.1g does not actually define this clearly
2434 at all. POSIX 1003.1g doesn't define a lot of things
2435 clearly however!
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 */
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002438
2439 sk_peek_offset_fwd(sk, size);
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (UNIXCB(skb).fp)
Miklos Szeredicbcf0112021-07-28 14:47:20 +02002442 unix_peek_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
Eric Dumazet9f6f9af2012-02-21 23:24:55 +00002444 err = (flags & MSG_TRUNC) ? skb->len - skip : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002446 scm_recv(sock, msg, &scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448out_free:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002449 skb_free_datagram(sk, skb);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002450 mutex_unlock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451out:
2452 return err;
2453}
2454
Cong Wang9825d862021-07-04 12:02:48 -07002455static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2456 int flags)
2457{
2458 struct sock *sk = sock->sk;
2459
2460#ifdef CONFIG_BPF_SYSCALL
Jiang Wang94531cf2021-08-16 19:03:21 +00002461 const struct proto *prot = READ_ONCE(sk->sk_prot);
2462
2463 if (prot != &unix_dgram_proto)
2464 return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
Cong Wang9825d862021-07-04 12:02:48 -07002465 flags & ~MSG_DONTWAIT, NULL);
2466#endif
2467 return __unix_dgram_recvmsg(sk, msg, size, flags);
2468}
2469
Cong Wang29df44f2021-07-04 12:02:44 -07002470static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
2471 sk_read_actor_t recv_actor)
2472{
2473 int copied = 0;
2474
2475 while (1) {
2476 struct unix_sock *u = unix_sk(sk);
2477 struct sk_buff *skb;
2478 int used, err;
2479
2480 mutex_lock(&u->iolock);
2481 skb = skb_recv_datagram(sk, 0, 1, &err);
2482 mutex_unlock(&u->iolock);
2483 if (!skb)
2484 return err;
2485
2486 used = recv_actor(desc, skb, 0, skb->len);
2487 if (used <= 0) {
2488 if (!copied)
2489 copied = used;
2490 kfree_skb(skb);
2491 break;
2492 } else if (used <= skb->len) {
2493 copied += used;
2494 }
2495
2496 kfree_skb(skb);
2497 if (!desc->count)
2498 break;
2499 }
2500
2501 return copied;
2502}
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504/*
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002505 * Sleep until more data has arrived. But check for races..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002507static long unix_stream_data_wait(struct sock *sk, long timeo,
WANG Cong06a77b02016-11-17 15:55:26 -08002508 struct sk_buff *last, unsigned int last_len,
2509 bool freezable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002511 struct sk_buff *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 DEFINE_WAIT(wait);
2513
David S. Miller1c92b4e2007-05-31 13:24:26 -07002514 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00002517 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002519 tail = skb_peek_tail(&sk->sk_receive_queue);
2520 if (tail != last ||
2521 (tail && tail->len != last_len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 sk->sk_err ||
2523 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2524 signal_pending(current) ||
2525 !timeo)
2526 break;
2527
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002528 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002529 unix_state_unlock(sk);
WANG Cong06a77b02016-11-17 15:55:26 -08002530 if (freezable)
2531 timeo = freezable_schedule_timeout(timeo);
2532 else
2533 timeo = schedule_timeout(timeo);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002534 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002535
2536 if (sock_flag(sk, SOCK_DEAD))
2537 break;
2538
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002539 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541
Eric Dumazetaa395142010-04-20 13:03:51 +00002542 finish_wait(sk_sleep(sk), &wait);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002543 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return timeo;
2545}
2546
Eric Dumazete370a722013-08-08 14:37:32 -07002547static unsigned int unix_skb_len(const struct sk_buff *skb)
2548{
2549 return skb->len - UNIXCB(skb).consumed;
2550}
2551
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002552struct unix_stream_read_state {
2553 int (*recv_actor)(struct sk_buff *, int, int,
2554 struct unix_stream_read_state *);
2555 struct socket *socket;
2556 struct msghdr *msg;
2557 struct pipe_inode_info *pipe;
2558 size_t size;
2559 int flags;
2560 unsigned int splice_flags;
2561};
2562
Rao Shoaib314001f2021-08-01 00:57:07 -07002563#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
2564static int unix_stream_recv_urg(struct unix_stream_read_state *state)
2565{
2566 struct socket *sock = state->socket;
2567 struct sock *sk = sock->sk;
2568 struct unix_sock *u = unix_sk(sk);
2569 int chunk = 1;
Rao Shoaib876c14a2021-08-11 15:06:52 -07002570 struct sk_buff *oob_skb;
Rao Shoaib314001f2021-08-01 00:57:07 -07002571
Rao Shoaib876c14a2021-08-11 15:06:52 -07002572 mutex_lock(&u->iolock);
2573 unix_state_lock(sk);
2574
2575 if (sock_flag(sk, SOCK_URGINLINE) || !u->oob_skb) {
2576 unix_state_unlock(sk);
2577 mutex_unlock(&u->iolock);
Rao Shoaib314001f2021-08-01 00:57:07 -07002578 return -EINVAL;
Rao Shoaib876c14a2021-08-11 15:06:52 -07002579 }
Rao Shoaib314001f2021-08-01 00:57:07 -07002580
Rao Shoaib876c14a2021-08-11 15:06:52 -07002581 oob_skb = u->oob_skb;
2582
2583 if (!(state->flags & MSG_PEEK)) {
2584 u->oob_skb = NULL;
2585 }
2586
2587 unix_state_unlock(sk);
2588
2589 chunk = state->recv_actor(oob_skb, 0, chunk, state);
2590
2591 if (!(state->flags & MSG_PEEK)) {
2592 UNIXCB(oob_skb).consumed += 1;
2593 kfree_skb(oob_skb);
2594 }
2595
2596 mutex_unlock(&u->iolock);
2597
Rao Shoaib314001f2021-08-01 00:57:07 -07002598 if (chunk < 0)
2599 return -EFAULT;
2600
Rao Shoaib314001f2021-08-01 00:57:07 -07002601 state->msg->msg_flags |= MSG_OOB;
2602 return 1;
2603}
2604
2605static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
2606 int flags, int copied)
2607{
2608 struct unix_sock *u = unix_sk(sk);
2609
2610 if (!unix_skb_len(skb) && !(flags & MSG_PEEK)) {
2611 skb_unlink(skb, &sk->sk_receive_queue);
2612 consume_skb(skb);
2613 skb = NULL;
2614 } else {
2615 if (skb == u->oob_skb) {
2616 if (copied) {
2617 skb = NULL;
2618 } else if (sock_flag(sk, SOCK_URGINLINE)) {
2619 if (!(flags & MSG_PEEK)) {
2620 u->oob_skb = NULL;
2621 consume_skb(skb);
2622 }
2623 } else if (!(flags & MSG_PEEK)) {
2624 skb_unlink(skb, &sk->sk_receive_queue);
2625 consume_skb(skb);
2626 skb = skb_peek(&sk->sk_receive_queue);
2627 }
2628 }
2629 }
2630 return skb;
2631}
2632#endif
2633
Jiang Wang77462de2021-08-16 19:03:20 +00002634static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
2635 sk_read_actor_t recv_actor)
2636{
2637 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
2638 return -ENOTCONN;
2639
2640 return unix_read_sock(sk, desc, recv_actor);
2641}
2642
WANG Cong06a77b02016-11-17 15:55:26 -08002643static int unix_stream_read_generic(struct unix_stream_read_state *state,
2644 bool freezable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002646 struct scm_cookie scm;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002647 struct socket *sock = state->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct sock *sk = sock->sk;
2649 struct unix_sock *u = unix_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 int copied = 0;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002651 int flags = state->flags;
Eric Dumazetde144392014-03-25 18:42:27 -07002652 int noblock = flags & MSG_DONTWAIT;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002653 bool check_creds = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 int target;
2655 int err = 0;
2656 long timeo;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002657 int skip;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002658 size_t size = state->size;
2659 unsigned int last_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002661 if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
2662 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 goto out;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002666 if (unlikely(flags & MSG_OOB)) {
2667 err = -EOPNOTSUPP;
Rao Shoaib314001f2021-08-01 00:57:07 -07002668#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
Rao Shoaib314001f2021-08-01 00:57:07 -07002669 err = unix_stream_recv_urg(state);
Rao Shoaib314001f2021-08-01 00:57:07 -07002670#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto out;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002674 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
Eric Dumazetde144392014-03-25 18:42:27 -07002675 timeo = sock_rcvtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002677 memset(&scm, 0, sizeof(scm));
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /* Lock the socket to prevent queue disordering
2680 * while sleeps in memcpy_tomsg
2681 */
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002682 mutex_lock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Matthew Dawsona0917e02017-08-18 15:04:54 -04002684 skip = max(sk_peek_offset(sk, flags), 0);
Andrey Vagine9193d62015-10-02 00:05:36 +03002685
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002686 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 int chunk;
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002688 bool drop_skb;
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002689 struct sk_buff *skb, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Rainer Weikusat18eceb82016-02-18 12:39:46 +00002691redo:
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002692 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002693 if (sock_flag(sk, SOCK_DEAD)) {
2694 err = -ECONNRESET;
2695 goto unlock;
2696 }
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002697 last = skb = skb_peek(&sk->sk_receive_queue);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002698 last_len = last ? last->len : 0;
Rao Shoaib314001f2021-08-01 00:57:07 -07002699
2700#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
2701 if (skb) {
2702 skb = manage_oob(skb, sk, flags, copied);
2703 if (!skb) {
2704 unix_state_unlock(sk);
2705 if (copied)
2706 break;
2707 goto redo;
2708 }
2709 }
2710#endif
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002711again:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002712 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (copied >= target)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002714 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716 /*
2717 * POSIX 1003.1g mandates this order.
2718 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002719
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002720 err = sock_error(sk);
2721 if (err)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002722 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (sk->sk_shutdown & RCV_SHUTDOWN)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002724 goto unlock;
2725
2726 unix_state_unlock(sk);
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002727 if (!timeo) {
2728 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002730 }
2731
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002732 mutex_unlock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002734 timeo = unix_stream_data_wait(sk, timeo, last,
WANG Cong06a77b02016-11-17 15:55:26 -08002735 last_len, freezable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Rainer Weikusat3822b5c2015-12-16 20:09:25 +00002737 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 err = sock_intr_errno(timeo);
Eric Dumazetfa0dc042016-01-24 13:53:50 -08002739 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 goto out;
2741 }
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00002742
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002743 mutex_lock(&u->iolock);
Rainer Weikusat18eceb82016-02-18 12:39:46 +00002744 goto redo;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002745unlock:
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002746 unix_state_unlock(sk);
2747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002749
Eric Dumazete370a722013-08-08 14:37:32 -07002750 while (skip >= unix_skb_len(skb)) {
2751 skip -= unix_skb_len(skb);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002752 last = skb;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002753 last_len = skb->len;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002754 skb = skb_peek_next(skb, &sk->sk_receive_queue);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002755 if (!skb)
2756 goto again;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002757 }
2758
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002759 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (check_creds) {
2762 /* Never glue messages from different writers */
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002763 if (!unix_skb_scm_eq(skb, &scm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 break;
Eric W. Biederman0e82e7f6d2013-04-03 16:14:47 +00002765 } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* Copy credentials */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002767 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04002768 unix_set_secdata(&scm, skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002769 check_creds = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
2772 /* Copy address just once */
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002773 if (state->msg && state->msg->msg_name) {
2774 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
2775 state->msg->msg_name);
2776 unix_copy_addr(state->msg, skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 sunaddr = NULL;
2778 }
2779
Eric Dumazete370a722013-08-08 14:37:32 -07002780 chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002781 skb_get(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002782 chunk = state->recv_actor(skb, skip, chunk, state);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002783 drop_skb = !unix_skb_len(skb);
2784 /* skb is only safe to use if !drop_skb */
2785 consume_skb(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002786 if (chunk < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (copied == 0)
2788 copied = -EFAULT;
2789 break;
2790 }
2791 copied += chunk;
2792 size -= chunk;
2793
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002794 if (drop_skb) {
2795 /* the skb was touched by a concurrent reader;
2796 * we should not expect anything from this skb
2797 * anymore and assume it invalid - we can be
2798 * sure it was dropped from the socket queue
2799 *
2800 * let's report a short read
2801 */
2802 err = 0;
2803 break;
2804 }
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /* Mark read part of skb as used */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002807 if (!(flags & MSG_PEEK)) {
Eric Dumazete370a722013-08-08 14:37:32 -07002808 UNIXCB(skb).consumed += chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002810 sk_peek_offset_bwd(sk, chunk);
2811
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002812 if (UNIXCB(skb).fp) {
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002813 scm_stat_del(sk, skb);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002814 unix_detach_fds(&scm, skb);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Eric Dumazete370a722013-08-08 14:37:32 -07002817 if (unix_skb_len(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Eric Dumazet6f01fd62012-01-28 16:11:03 +00002820 skb_unlink(skb, &sk->sk_receive_queue);
Neil Horman70d4bf62010-07-20 06:45:56 +00002821 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002823 if (scm.fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 break;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* It is questionable, see note in unix_dgram_recvmsg.
2827 */
2828 if (UNIXCB(skb).fp)
Miklos Szeredicbcf0112021-07-28 14:47:20 +02002829 unix_peek_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Andrey Vagine9193d62015-10-02 00:05:36 +03002831 sk_peek_offset_fwd(sk, chunk);
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002832
Aaron Conole9f389e32015-09-26 18:50:43 -04002833 if (UNIXCB(skb).fp)
2834 break;
2835
Andrey Vagine9193d62015-10-02 00:05:36 +03002836 skip = 0;
Aaron Conole9f389e32015-09-26 18:50:43 -04002837 last = skb;
2838 last_len = skb->len;
2839 unix_state_lock(sk);
2840 skb = skb_peek_next(skb, &sk->sk_receive_queue);
2841 if (skb)
2842 goto again;
2843 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 break;
2845 }
2846 } while (size);
2847
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002848 mutex_unlock(&u->iolock);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002849 if (state->msg)
2850 scm_recv(sock, state->msg, &scm, flags);
2851 else
2852 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853out:
2854 return copied ? : err;
2855}
2856
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002857static int unix_stream_read_actor(struct sk_buff *skb,
2858 int skip, int chunk,
2859 struct unix_stream_read_state *state)
2860{
2861 int ret;
2862
2863 ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
2864 state->msg, chunk);
2865 return ret ?: chunk;
2866}
2867
Jiang Wang94531cf2021-08-16 19:03:21 +00002868int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg,
2869 size_t size, int flags)
2870{
2871 struct unix_stream_read_state state = {
2872 .recv_actor = unix_stream_read_actor,
2873 .socket = sk->sk_socket,
2874 .msg = msg,
2875 .size = size,
2876 .flags = flags
2877 };
2878
2879 return unix_stream_read_generic(&state, true);
2880}
2881
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002882static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
2883 size_t size, int flags)
2884{
2885 struct unix_stream_read_state state = {
2886 .recv_actor = unix_stream_read_actor,
2887 .socket = sock,
2888 .msg = msg,
2889 .size = size,
2890 .flags = flags
2891 };
2892
Jiang Wang94531cf2021-08-16 19:03:21 +00002893#ifdef CONFIG_BPF_SYSCALL
2894 struct sock *sk = sock->sk;
2895 const struct proto *prot = READ_ONCE(sk->sk_prot);
2896
2897 if (prot != &unix_stream_proto)
2898 return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
2899 flags & ~MSG_DONTWAIT, NULL);
2900#endif
WANG Cong06a77b02016-11-17 15:55:26 -08002901 return unix_stream_read_generic(&state, true);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002902}
2903
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002904static int unix_stream_splice_actor(struct sk_buff *skb,
2905 int skip, int chunk,
2906 struct unix_stream_read_state *state)
2907{
2908 return skb_splice_bits(skb, state->socket->sk,
2909 UNIXCB(skb).consumed + skip,
Al Viro25869262016-09-17 21:02:10 -04002910 state->pipe, chunk, state->splice_flags);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002911}
2912
2913static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
2914 struct pipe_inode_info *pipe,
2915 size_t size, unsigned int flags)
2916{
2917 struct unix_stream_read_state state = {
2918 .recv_actor = unix_stream_splice_actor,
2919 .socket = sock,
2920 .pipe = pipe,
2921 .size = size,
2922 .splice_flags = flags,
2923 };
2924
2925 if (unlikely(*ppos))
2926 return -ESPIPE;
2927
2928 if (sock->file->f_flags & O_NONBLOCK ||
2929 flags & SPLICE_F_NONBLOCK)
2930 state.flags = MSG_DONTWAIT;
2931
WANG Cong06a77b02016-11-17 15:55:26 -08002932 return unix_stream_read_generic(&state, false);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002933}
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935static int unix_shutdown(struct socket *sock, int mode)
2936{
2937 struct sock *sk = sock->sk;
2938 struct sock *other;
2939
Xi Wangfc61b922012-08-26 16:47:13 +00002940 if (mode < SHUT_RD || mode > SHUT_RDWR)
2941 return -EINVAL;
2942 /* This maps:
2943 * SHUT_RD (0) -> RCV_SHUTDOWN (1)
2944 * SHUT_WR (1) -> SEND_SHUTDOWN (2)
2945 * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
2946 */
2947 ++mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Alban Crequy7180a032011-01-19 04:56:36 +00002949 unix_state_lock(sk);
2950 sk->sk_shutdown |= mode;
2951 other = unix_peer(sk);
2952 if (other)
2953 sock_hold(other);
2954 unix_state_unlock(sk);
2955 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Alban Crequy7180a032011-01-19 04:56:36 +00002957 if (other &&
2958 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Alban Crequy7180a032011-01-19 04:56:36 +00002960 int peer_mode = 0;
Jiang Wang94531cf2021-08-16 19:03:21 +00002961 const struct proto *prot = READ_ONCE(other->sk_prot);
Alban Crequy7180a032011-01-19 04:56:36 +00002962
Jiang Wangd3599022021-08-21 18:07:36 +00002963 if (prot->unhash)
2964 prot->unhash(other);
Alban Crequy7180a032011-01-19 04:56:36 +00002965 if (mode&RCV_SHUTDOWN)
2966 peer_mode |= SEND_SHUTDOWN;
2967 if (mode&SEND_SHUTDOWN)
2968 peer_mode |= RCV_SHUTDOWN;
2969 unix_state_lock(other);
2970 other->sk_shutdown |= peer_mode;
2971 unix_state_unlock(other);
2972 other->sk_state_change(other);
Jiang Wangd0c6416b2021-10-04 23:25:28 +00002973 if (peer_mode == SHUTDOWN_MASK)
Alban Crequy7180a032011-01-19 04:56:36 +00002974 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
Jiang Wangd0c6416b2021-10-04 23:25:28 +00002975 else if (peer_mode & RCV_SHUTDOWN)
Alban Crequy7180a032011-01-19 04:56:36 +00002976 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Alban Crequy7180a032011-01-19 04:56:36 +00002978 if (other)
2979 sock_put(other);
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return 0;
2982}
2983
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002984long unix_inq_len(struct sock *sk)
2985{
2986 struct sk_buff *skb;
2987 long amount = 0;
2988
2989 if (sk->sk_state == TCP_LISTEN)
2990 return -EINVAL;
2991
2992 spin_lock(&sk->sk_receive_queue.lock);
2993 if (sk->sk_type == SOCK_STREAM ||
2994 sk->sk_type == SOCK_SEQPACKET) {
2995 skb_queue_walk(&sk->sk_receive_queue, skb)
Eric Dumazete370a722013-08-08 14:37:32 -07002996 amount += unix_skb_len(skb);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00002997 } else {
2998 skb = skb_peek(&sk->sk_receive_queue);
2999 if (skb)
3000 amount = skb->len;
3001 }
3002 spin_unlock(&sk->sk_receive_queue.lock);
3003
3004 return amount;
3005}
3006EXPORT_SYMBOL_GPL(unix_inq_len);
3007
3008long unix_outq_len(struct sock *sk)
3009{
3010 return sk_wmem_alloc_get(sk);
3011}
3012EXPORT_SYMBOL_GPL(unix_outq_len);
3013
Andrey Vaginba94f302017-02-01 11:00:45 -08003014static int unix_open_file(struct sock *sk)
3015{
3016 struct path path;
3017 struct file *f;
3018 int fd;
3019
3020 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
3021 return -EPERM;
3022
Al Viroae3b5642019-02-15 20:09:35 +00003023 if (!smp_load_acquire(&unix_sk(sk)->addr))
Andrey Vaginba94f302017-02-01 11:00:45 -08003024 return -ENOENT;
Al Viroae3b5642019-02-15 20:09:35 +00003025
3026 path = unix_sk(sk)->path;
3027 if (!path.dentry)
3028 return -ENOENT;
Andrey Vaginba94f302017-02-01 11:00:45 -08003029
3030 path_get(&path);
Andrey Vaginba94f302017-02-01 11:00:45 -08003031
3032 fd = get_unused_fd_flags(O_CLOEXEC);
3033 if (fd < 0)
3034 goto out;
3035
3036 f = dentry_open(&path, O_PATH, current_cred());
3037 if (IS_ERR(f)) {
3038 put_unused_fd(fd);
3039 fd = PTR_ERR(f);
3040 goto out;
3041 }
3042
3043 fd_install(fd, f);
3044out:
3045 path_put(&path);
3046
3047 return fd;
3048}
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3051{
3052 struct sock *sk = sock->sk;
Jianjun Konge27dfce2008-11-01 21:38:31 -07003053 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 int err;
3055
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003056 switch (cmd) {
3057 case SIOCOUTQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003058 amount = unix_outq_len(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003059 err = put_user(amount, (int __user *)arg);
3060 break;
3061 case SIOCINQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003062 amount = unix_inq_len(sk);
3063 if (amount < 0)
3064 err = amount;
3065 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 err = put_user(amount, (int __user *)arg);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003067 break;
Andrey Vaginba94f302017-02-01 11:00:45 -08003068 case SIOCUNIXFILE:
3069 err = unix_open_file(sk);
3070 break;
Rao Shoaib314001f2021-08-01 00:57:07 -07003071#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
3072 case SIOCATMARK:
3073 {
3074 struct sk_buff *skb;
3075 struct unix_sock *u = unix_sk(sk);
3076 int answ = 0;
3077
3078 skb = skb_peek(&sk->sk_receive_queue);
3079 if (skb && skb == u->oob_skb)
3080 answ = 1;
3081 err = put_user(answ, (int __user *)arg);
3082 }
3083 break;
3084#endif
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003085 default:
3086 err = -ENOIOCTLCMD;
3087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089 return err;
3090}
3091
Arnd Bergmann5f6beb92019-06-03 22:03:44 +02003092#ifdef CONFIG_COMPAT
3093static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3094{
3095 return unix_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
3096}
3097#endif
3098
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003099static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 struct sock *sk = sock->sk;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003102 __poll_t mask;
3103
Karsten Graul89ab0662018-10-23 13:40:39 +02003104 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003105 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 /* exceptional events? */
3108 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003109 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003111 mask |= EPOLLHUP;
Davide Libenzif348d702006-03-25 03:07:39 -08003112 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003113 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* readable? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003116 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003117 mask |= EPOLLIN | EPOLLRDNORM;
Cong Wangaf493382021-10-08 13:33:05 -07003118 if (sk_is_readable(sk))
3119 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /* Connection-based need to check for termination and startup */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003122 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
3123 sk->sk_state == TCP_CLOSE)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003124 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 /*
3127 * we set writable also when the other side has shut down the
3128 * connection. This prevents stuck sockets.
3129 */
3130 if (unix_writable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003131 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 return mask;
3134}
3135
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003136static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
3137 poll_table *wait)
Rainer Weikusat3c734192008-06-17 22:28:05 -07003138{
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003139 struct sock *sk = sock->sk, *other;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003140 unsigned int writable;
3141 __poll_t mask;
3142
Karsten Graul89ab0662018-10-23 13:40:39 +02003143 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003144 mask = 0;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003145
3146 /* exceptional events? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003147 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003148 mask |= EPOLLERR |
3149 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00003150
Rainer Weikusat3c734192008-06-17 22:28:05 -07003151 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003152 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003153 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003154 mask |= EPOLLHUP;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003155
3156 /* readable? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003157 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003158 mask |= EPOLLIN | EPOLLRDNORM;
Cong Wangaf493382021-10-08 13:33:05 -07003159 if (sk_is_readable(sk))
3160 mask |= EPOLLIN | EPOLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003161
3162 /* Connection-based need to check for termination and startup */
3163 if (sk->sk_type == SOCK_SEQPACKET) {
3164 if (sk->sk_state == TCP_CLOSE)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003165 mask |= EPOLLHUP;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003166 /* connection hasn't started yet? */
3167 if (sk->sk_state == TCP_SYN_SENT)
3168 return mask;
3169 }
3170
Eric Dumazet973a34a2010-10-31 05:38:25 +00003171 /* No write status requested, avoid expensive OUT tests. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003172 if (!(poll_requested_events(wait) & (EPOLLWRBAND|EPOLLWRNORM|EPOLLOUT)))
Eric Dumazet973a34a2010-10-31 05:38:25 +00003173 return mask;
3174
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003175 writable = unix_writable(sk);
Rainer Weikusat7d267272015-11-20 22:07:23 +00003176 if (writable) {
3177 unix_state_lock(sk);
3178
3179 other = unix_peer(sk);
3180 if (other && unix_peer(other) != sk &&
Eric Dumazet04f08eb2021-09-08 17:00:29 -07003181 unix_recvq_full_lockless(other) &&
Rainer Weikusat7d267272015-11-20 22:07:23 +00003182 unix_dgram_peer_wake_me(sk, other))
3183 writable = 0;
3184
3185 unix_state_unlock(sk);
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003186 }
3187
3188 if (writable)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003189 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003190 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08003191 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Rainer Weikusat3c734192008-06-17 22:28:05 -07003192
Rainer Weikusat3c734192008-06-17 22:28:05 -07003193 return mask;
3194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196#ifdef CONFIG_PROC_FS
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08003197
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003198#define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
3199
3200#define get_bucket(x) ((x) >> BUCKET_SPACE)
3201#define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
3202#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08003203
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003204static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003206 unsigned long offset = get_offset(*pos);
3207 unsigned long bucket = get_bucket(*pos);
3208 struct sock *sk;
3209 unsigned long count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003211 for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
3212 if (sock_net(sk) != seq_file_net(seq))
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003213 continue;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003214 if (++count == offset)
3215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003217
3218 return sk;
3219}
3220
3221static struct sock *unix_next_socket(struct seq_file *seq,
3222 struct sock *sk,
3223 loff_t *pos)
3224{
3225 unsigned long bucket;
3226
3227 while (sk > (struct sock *)SEQ_START_TOKEN) {
3228 sk = sk_next(sk);
3229 if (!sk)
3230 goto next_bucket;
3231 if (sock_net(sk) == seq_file_net(seq))
3232 return sk;
3233 }
3234
3235 do {
3236 sk = unix_from_bucket(seq, pos);
3237 if (sk)
3238 return sk;
3239
3240next_bucket:
3241 bucket = get_bucket(*pos) + 1;
3242 *pos = set_bucket_offset(bucket, 1);
3243 } while (bucket < ARRAY_SIZE(unix_socket_table));
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return NULL;
3246}
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08003249 __acquires(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
David S. Millerfbe9cc42005-12-13 23:26:29 -08003251 spin_lock(&unix_table_lock);
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003252
3253 if (!*pos)
3254 return SEQ_START_TOKEN;
3255
3256 if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
3257 return NULL;
3258
3259 return unix_next_socket(seq, NULL, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
3262static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3263{
3264 ++*pos;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003265 return unix_next_socket(seq, v, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
3268static void unix_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08003269 __releases(unix_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
David S. Millerfbe9cc42005-12-13 23:26:29 -08003271 spin_unlock(&unix_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
3274static int unix_seq_show(struct seq_file *seq, void *v)
3275{
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09003276
Joe Perchesb9f31242008-04-12 19:04:38 -07003277 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 seq_puts(seq, "Num RefCount Protocol Flags Type St "
3279 "Inode Path\n");
3280 else {
3281 struct sock *s = v;
3282 struct unix_sock *u = unix_sk(s);
David S. Miller1c92b4e2007-05-31 13:24:26 -07003283 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003285 seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003287 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 0,
3289 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
3290 s->sk_type,
3291 s->sk_socket ?
3292 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
3293 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
3294 sock_i_ino(s));
3295
Al Viroae3b5642019-02-15 20:09:35 +00003296 if (u->addr) { // under unix_table_lock here
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 int i, len;
3298 seq_putc(seq, ' ');
3299
3300 i = 0;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09003301 len = u->addr->len -
3302 offsetof(struct sockaddr_un, sun_path);
Kuniyuki Iwashima5ce7ab42021-11-24 11:14:27 +09003303 if (u->addr->name->sun_path[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 len--;
Kuniyuki Iwashima5ce7ab42021-11-24 11:14:27 +09003305 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 seq_putc(seq, '@');
3307 i++;
3308 }
3309 for ( ; i < len; i++)
Isaac Boukrise7947ea2016-11-01 02:41:35 +02003310 seq_putc(seq, u->addr->name->sun_path[i] ?:
3311 '@');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07003313 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 seq_putc(seq, '\n');
3315 }
3316
3317 return 0;
3318}
3319
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003320static const struct seq_operations unix_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 .start = unix_seq_start,
3322 .next = unix_seq_next,
3323 .stop = unix_seq_stop,
3324 .show = unix_seq_show,
3325};
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003326
3327#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL)
3328struct bpf_iter__unix {
3329 __bpf_md_ptr(struct bpf_iter_meta *, meta);
3330 __bpf_md_ptr(struct unix_sock *, unix_sk);
3331 uid_t uid __aligned(8);
3332};
3333
3334static int unix_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta,
3335 struct unix_sock *unix_sk, uid_t uid)
3336{
3337 struct bpf_iter__unix ctx;
3338
3339 meta->seq_num--; /* skip SEQ_START_TOKEN */
3340 ctx.meta = meta;
3341 ctx.unix_sk = unix_sk;
3342 ctx.uid = uid;
3343 return bpf_iter_run_prog(prog, &ctx);
3344}
3345
3346static int bpf_iter_unix_seq_show(struct seq_file *seq, void *v)
3347{
3348 struct bpf_iter_meta meta;
3349 struct bpf_prog *prog;
3350 struct sock *sk = v;
3351 uid_t uid;
3352
3353 if (v == SEQ_START_TOKEN)
3354 return 0;
3355
3356 uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
3357 meta.seq = seq;
3358 prog = bpf_iter_get_info(&meta, false);
3359 return unix_prog_seq_show(prog, &meta, v, uid);
3360}
3361
3362static void bpf_iter_unix_seq_stop(struct seq_file *seq, void *v)
3363{
3364 struct bpf_iter_meta meta;
3365 struct bpf_prog *prog;
3366
3367 if (!v) {
3368 meta.seq = seq;
3369 prog = bpf_iter_get_info(&meta, true);
3370 if (prog)
3371 (void)unix_prog_seq_show(prog, &meta, v, 0);
3372 }
3373
3374 unix_seq_stop(seq, v);
3375}
3376
3377static const struct seq_operations bpf_iter_unix_seq_ops = {
3378 .start = unix_seq_start,
3379 .next = unix_seq_next,
3380 .stop = bpf_iter_unix_seq_stop,
3381 .show = bpf_iter_unix_seq_show,
3382};
3383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384#endif
3385
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003386static const struct net_proto_family unix_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 .family = PF_UNIX,
3388 .create = unix_create,
3389 .owner = THIS_MODULE,
3390};
3391
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003392
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003393static int __net_init unix_net_init(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003394{
3395 int error = -ENOMEM;
3396
Denis V. Luneva0a53c82007-12-11 04:19:17 -08003397 net->unx.sysctl_max_dgram_qlen = 10;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003398 if (unix_sysctl_register(net))
3399 goto out;
Pavel Emelyanovd392e492007-12-01 23:44:15 +11003400
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003401#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02003402 if (!proc_create_net("unix", 0, net->proc_net, &unix_seq_ops,
3403 sizeof(struct seq_net_private))) {
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003404 unix_sysctl_unregister(net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003405 goto out;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003406 }
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003407#endif
3408 error = 0;
3409out:
Jianjun Kong48dcc33e2008-11-01 21:37:27 -07003410 return error;
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003411}
3412
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003413static void __net_exit unix_net_exit(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003414{
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003415 unix_sysctl_unregister(net);
Gao fengece31ff2013-02-18 01:34:56 +00003416 remove_proc_entry("unix", net->proc_net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003417}
3418
3419static struct pernet_operations unix_net_ops = {
3420 .init = unix_net_init,
3421 .exit = unix_net_exit,
3422};
3423
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003424#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3425DEFINE_BPF_ITER_FUNC(unix, struct bpf_iter_meta *meta,
3426 struct unix_sock *unix_sk, uid_t uid)
3427
3428static const struct bpf_iter_seq_info unix_seq_info = {
3429 .seq_ops = &bpf_iter_unix_seq_ops,
3430 .init_seq_private = bpf_iter_init_seq_net,
3431 .fini_seq_private = bpf_iter_fini_seq_net,
3432 .seq_priv_size = sizeof(struct seq_net_private),
3433};
3434
3435static struct bpf_iter_reg unix_reg_info = {
3436 .target = "unix",
3437 .ctx_arg_info_size = 1,
3438 .ctx_arg_info = {
3439 { offsetof(struct bpf_iter__unix, unix_sk),
3440 PTR_TO_BTF_ID_OR_NULL },
3441 },
3442 .seq_info = &unix_seq_info,
3443};
3444
3445static void __init bpf_iter_register(void)
3446{
3447 unix_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UNIX];
3448 if (bpf_iter_reg_target(&unix_reg_info))
3449 pr_warn("Warning: could not register bpf iterator unix\n");
3450}
3451#endif
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453static int __init af_unix_init(void)
3454{
3455 int rc = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003457 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Jiang Wang94531cf2021-08-16 19:03:21 +00003459 rc = proto_register(&unix_dgram_proto, 1);
3460 if (rc != 0) {
3461 pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
3462 goto out;
3463 }
3464
3465 rc = proto_register(&unix_stream_proto, 1);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09003466 if (rc != 0) {
wangweidong5cc208b2013-12-06 18:03:36 +08003467 pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 goto out;
3469 }
3470
3471 sock_register(&unix_family_ops);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003472 register_pernet_subsys(&unix_net_ops);
Cong Wangc6382912021-07-04 12:02:47 -07003473 unix_bpf_build_proto();
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003474
3475#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3476 bpf_iter_register();
3477#endif
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479out:
3480 return rc;
3481}
3482
3483static void __exit af_unix_exit(void)
3484{
3485 sock_unregister(PF_UNIX);
Jiang Wang94531cf2021-08-16 19:03:21 +00003486 proto_unregister(&unix_dgram_proto);
3487 proto_unregister(&unix_stream_proto);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003488 unregister_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
David Woodhouse3d366962008-04-24 00:59:25 -07003491/* Earlier than device_initcall() so that other drivers invoking
3492 request_module() don't end up in a loop when modprobe tries
3493 to use a UNIX socket. But later than subsys_initcall() because
3494 we depend on stuff initialised there */
3495fs_initcall(af_unix_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496module_exit(af_unix_exit);
3497
3498MODULE_LICENSE("GPL");
3499MODULE_ALIAS_NETPROTO(PF_UNIX);