blob: c19569819866eed2dc4c2c3fb90931fcf407acab [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>
Jakub Kicinskib6459412021-12-28 16:49:13 -080092#include <linux/filter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/termios.h>
94#include <linux/sockios.h>
95#include <linux/net.h>
96#include <linux/in.h>
97#include <linux/fs.h>
98#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080099#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/skbuff.h>
101#include <linux/netdevice.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200102#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -0700104#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <net/af_unix.h>
106#include <linux/proc_fs.h>
107#include <linux/seq_file.h>
108#include <net/scm.h>
109#include <linux/init.h>
110#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/rtnetlink.h>
112#include <linux/mount.h>
113#include <net/checksum.h>
114#include <linux/security.h>
Colin Cross2b15af62013-05-06 23:50:21 +0000115#include <linux/freezer.h>
Andrey Vaginba94f302017-02-01 11:00:45 -0800116#include <linux/file.h>
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +0900117#include <linux/btf_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jens Axboef4e65872019-02-08 09:01:44 -0700119#include "scm.h"
120
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900121spinlock_t unix_table_locks[2 * UNIX_HASH_SIZE];
122EXPORT_SYMBOL_GPL(unix_table_locks);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000123struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000124EXPORT_SYMBOL_GPL(unix_socket_table);
Eric Dumazet518de9b2010-10-26 14:22:44 -0700125static atomic_long_t unix_nr_socks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900127/* SMP locking strategy:
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900128 * hash table is protected with spinlock unix_table_locks
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900129 * each socket state is protected by separate spin lock.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900132static unsigned int unix_unbound_hash(struct sock *sk)
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000133{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900134 unsigned long hash = (unsigned long)sk;
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000135
136 hash ^= hash >> 16;
137 hash ^= hash >> 8;
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900138 hash ^= sk->sk_type;
139
140 return UNIX_HASH_SIZE + (hash & (UNIX_HASH_SIZE - 1));
141}
142
143static unsigned int unix_bsd_hash(struct inode *i)
144{
145 return i->i_ino & (UNIX_HASH_SIZE - 1);
146}
147
148static unsigned int unix_abstract_hash(struct sockaddr_un *sunaddr,
149 int addr_len, int type)
150{
151 __wsum csum = csum_partial(sunaddr, addr_len, 0);
152 unsigned int hash;
153
154 hash = (__force unsigned int)csum_fold(csum);
155 hash ^= hash >> 8;
156 hash ^= type;
157
158 return hash & (UNIX_HASH_SIZE - 1);
Eric Dumazet7123aaa2012-06-08 05:03:21 +0000159}
160
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900161static void unix_table_double_lock(unsigned int hash1, unsigned int hash2)
162{
163 /* hash1 and hash2 is never the same because
164 * one is between 0 and UNIX_HASH_SIZE - 1, and
165 * another is between UNIX_HASH_SIZE and UNIX_HASH_SIZE * 2.
166 */
167 if (hash1 > hash2)
168 swap(hash1, hash2);
169
170 spin_lock(&unix_table_locks[hash1]);
171 spin_lock_nested(&unix_table_locks[hash2], SINGLE_DEPTH_NESTING);
172}
173
174static void unix_table_double_unlock(unsigned int hash1, unsigned int hash2)
175{
176 spin_unlock(&unix_table_locks[hash1]);
177 spin_unlock(&unix_table_locks[hash2]);
178}
179
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700180#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700181static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700182{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400183 UNIXCB(skb).secid = scm->secid;
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700184}
185
186static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
187{
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400188 scm->secid = UNIXCB(skb).secid;
189}
190
191static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
192{
193 return (scm->secid == UNIXCB(skb).secid);
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700194}
195#else
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700196static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700197{ }
198
199static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
200{ }
Stephen Smalley37a9a8d2015-06-10 08:44:59 -0400201
202static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
203{
204 return true;
205}
Catherine Zhang877ce7c2006-06-29 12:27:47 -0700206#endif /* CONFIG_SECURITY_NETWORK */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define unix_peer(sk) (unix_sk(sk)->peer)
209
210static inline int unix_our_peer(struct sock *sk, struct sock *osk)
211{
212 return unix_peer(osk) == sk;
213}
214
215static inline int unix_may_send(struct sock *sk, struct sock *osk)
216{
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800217 return unix_peer(osk) == NULL || unix_our_peer(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Qian Cai86b18aa2020-02-04 13:40:29 -0500220static inline int unix_recvq_full(const struct sock *sk)
Rainer Weikusat3c734192008-06-17 22:28:05 -0700221{
222 return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
223}
224
Qian Cai86b18aa2020-02-04 13:40:29 -0500225static inline int unix_recvq_full_lockless(const struct sock *sk)
226{
227 return skb_queue_len_lockless(&sk->sk_receive_queue) >
228 READ_ONCE(sk->sk_max_ack_backlog);
229}
230
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000231struct sock *unix_peer_get(struct sock *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct sock *peer;
234
David S. Miller1c92b4e2007-05-31 13:24:26 -0700235 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 peer = unix_peer(s);
237 if (peer)
238 sock_hold(peer);
David S. Miller1c92b4e2007-05-31 13:24:26 -0700239 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return peer;
241}
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +0000242EXPORT_SYMBOL_GPL(unix_peer_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +0900244static struct unix_address *unix_create_addr(struct sockaddr_un *sunaddr,
245 int addr_len)
246{
247 struct unix_address *addr;
248
249 addr = kmalloc(sizeof(*addr) + addr_len, GFP_KERNEL);
250 if (!addr)
251 return NULL;
252
253 refcount_set(&addr->refcnt, 1);
254 addr->len = addr_len;
255 memcpy(addr->name, sunaddr, addr_len);
256
257 return addr;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static inline void unix_release_addr(struct unix_address *addr)
261{
Reshetova, Elena8c9814b2017-06-30 13:08:05 +0300262 if (refcount_dec_and_test(&addr->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(addr);
264}
265
266/*
267 * Check unix socket name:
268 * - should be not zero length.
269 * - if started by not zero, should be NULL terminated (FS object)
270 * - if started by zero, it is abstract name.
271 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900272
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +0900273static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len)
274{
275 if (addr_len <= offsetof(struct sockaddr_un, sun_path) ||
276 addr_len > sizeof(*sunaddr))
277 return -EINVAL;
278
279 if (sunaddr->sun_family != AF_UNIX)
280 return -EINVAL;
281
282 return 0;
283}
284
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +0900285static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len)
286{
287 /* This may look like an off by one error but it is a bit more
288 * subtle. 108 is the longest valid AF_UNIX path for a binding.
289 * sun_path[108] doesn't as such exist. However in kernel space
290 * we are guaranteed that it is a valid memory location in our
291 * kernel address buffer because syscall functions always pass
292 * a pointer of struct sockaddr_storage which has a bigger buffer
293 * than 108.
294 */
295 ((char *)sunaddr)[addr_len] = 0;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void __unix_remove_socket(struct sock *sk)
299{
300 sk_del_node_init(sk);
301}
302
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900303static void __unix_insert_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700305 WARN_ON(!sk_unhashed(sk));
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900306 sk_add_node(sk, &unix_socket_table[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900309static void __unix_set_addr_hash(struct sock *sk, struct unix_address *addr,
310 unsigned int hash)
Al Viro185ab882021-06-19 03:50:26 +0000311{
312 __unix_remove_socket(sk);
313 smp_store_release(&unix_sk(sk)->addr, addr);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900314
315 sk->sk_hash = hash;
316 __unix_insert_socket(sk);
Al Viro185ab882021-06-19 03:50:26 +0000317}
318
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900319static void unix_remove_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900321 spin_lock(&unix_table_locks[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 __unix_remove_socket(sk);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900323 spin_unlock(&unix_table_locks[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900326static void unix_insert_unbound_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900328 spin_lock(&unix_table_locks[sk->sk_hash]);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900329 __unix_insert_socket(sk);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900330 spin_unlock(&unix_table_locks[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800333static struct sock *__unix_find_socket_byname(struct net *net,
334 struct sockaddr_un *sunname,
Al Virobe752282021-06-19 03:50:33 +0000335 int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Al Virobe752282021-06-19 03:50:33 +0000339 sk_for_each(s, &unix_socket_table[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct unix_sock *u = unix_sk(s);
341
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900342 if (!net_eq(sock_net(s), net))
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800343 continue;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (u->addr->len == len &&
346 !memcmp(u->addr->name, sunname, len))
Vito Caputo262ce0a2019-10-09 20:43:47 -0700347 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Vito Caputo262ce0a2019-10-09 20:43:47 -0700349 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Denis V. Lunev097e66c2007-11-19 22:29:30 -0800352static inline struct sock *unix_find_socket_byname(struct net *net,
353 struct sockaddr_un *sunname,
Al Virobe752282021-06-19 03:50:33 +0000354 int len, unsigned int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct sock *s;
357
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900358 spin_lock(&unix_table_locks[hash]);
Al Virobe752282021-06-19 03:50:33 +0000359 s = __unix_find_socket_byname(net, sunname, len, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (s)
361 sock_hold(s);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900362 spin_unlock(&unix_table_locks[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return s;
364}
365
Eric W. Biederman6616f782010-06-13 03:35:48 +0000366static struct sock *unix_find_socket_byinode(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900368 unsigned int hash = unix_bsd_hash(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900371 spin_lock(&unix_table_locks[hash]);
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +0900372 sk_for_each(s, &unix_socket_table[hash]) {
Al Viro40ffe672012-03-14 21:54:32 -0400373 struct dentry *dentry = unix_sk(s)->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Miklos Szeredibeef5122016-12-16 11:02:53 +0100375 if (dentry && d_backing_inode(dentry) == i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sock_hold(s);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900377 spin_unlock(&unix_table_locks[hash]);
378 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +0900381 spin_unlock(&unix_table_locks[hash]);
382 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Rainer Weikusat7d267272015-11-20 22:07:23 +0000385/* Support code for asymmetrically connected dgram sockets
386 *
387 * If a datagram socket is connected to a socket not itself connected
388 * to the first socket (eg, /dev/log), clients may only enqueue more
389 * messages if the present receive queue of the server socket is not
390 * "too large". This means there's a second writeability condition
391 * poll and sendmsg need to test. The dgram recv code will do a wake
392 * up on the peer_wait wait queue of a socket upon reception of a
393 * datagram which needs to be propagated to sleeping would-be writers
394 * since these might not have sent anything so far. This can't be
395 * accomplished via poll_wait because the lifetime of the server
396 * socket might be less than that of its clients if these break their
397 * association with it or if the server socket is closed while clients
398 * are still connected to it and there's no way to inform "a polling
399 * implementation" that it should let go of a certain wait queue
400 *
Ingo Molnarac6424b2017-06-20 12:06:13 +0200401 * In order to propagate a wake up, a wait_queue_entry_t of the client
Rainer Weikusat7d267272015-11-20 22:07:23 +0000402 * socket is enqueued on the peer_wait queue of the server socket
403 * whose wake function does a wake_up on the ordinary client socket
404 * wait queue. This connection is established whenever a write (or
405 * poll for write) hit the flow control condition and broken when the
406 * association to the server socket is dissolved or after a wake up
407 * was relayed.
408 */
409
Ingo Molnarac6424b2017-06-20 12:06:13 +0200410static int unix_dgram_peer_wake_relay(wait_queue_entry_t *q, unsigned mode, int flags,
Rainer Weikusat7d267272015-11-20 22:07:23 +0000411 void *key)
412{
413 struct unix_sock *u;
414 wait_queue_head_t *u_sleep;
415
416 u = container_of(q, struct unix_sock, peer_wake);
417
418 __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait,
419 q);
420 u->peer_wake.private = NULL;
421
422 /* relaying can only happen while the wq still exists */
423 u_sleep = sk_sleep(&u->sk);
424 if (u_sleep)
Al Viro3ad6f932017-07-03 20:14:56 -0400425 wake_up_interruptible_poll(u_sleep, key_to_poll(key));
Rainer Weikusat7d267272015-11-20 22:07:23 +0000426
427 return 0;
428}
429
430static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other)
431{
432 struct unix_sock *u, *u_other;
433 int rc;
434
435 u = unix_sk(sk);
436 u_other = unix_sk(other);
437 rc = 0;
438 spin_lock(&u_other->peer_wait.lock);
439
440 if (!u->peer_wake.private) {
441 u->peer_wake.private = other;
442 __add_wait_queue(&u_other->peer_wait, &u->peer_wake);
443
444 rc = 1;
445 }
446
447 spin_unlock(&u_other->peer_wait.lock);
448 return rc;
449}
450
451static void unix_dgram_peer_wake_disconnect(struct sock *sk,
452 struct sock *other)
453{
454 struct unix_sock *u, *u_other;
455
456 u = unix_sk(sk);
457 u_other = unix_sk(other);
458 spin_lock(&u_other->peer_wait.lock);
459
460 if (u->peer_wake.private == other) {
461 __remove_wait_queue(&u_other->peer_wait, &u->peer_wake);
462 u->peer_wake.private = NULL;
463 }
464
465 spin_unlock(&u_other->peer_wait.lock);
466}
467
468static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk,
469 struct sock *other)
470{
471 unix_dgram_peer_wake_disconnect(sk, other);
472 wake_up_interruptible_poll(sk_sleep(sk),
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800473 EPOLLOUT |
474 EPOLLWRNORM |
475 EPOLLWRBAND);
Rainer Weikusat7d267272015-11-20 22:07:23 +0000476}
477
478/* preconditions:
479 * - unix_peer(sk) == other
480 * - association is stable
481 */
482static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other)
483{
484 int connected;
485
486 connected = unix_dgram_peer_wake_connect(sk, other);
487
Jason Baron51f7e952018-08-03 17:24:53 -0400488 /* If other is SOCK_DEAD, we want to make sure we signal
489 * POLLOUT, such that a subsequent write() can get a
490 * -ECONNREFUSED. Otherwise, if we haven't queued any skbs
491 * to other and its full, we will hang waiting for POLLOUT.
492 */
493 if (unix_recvq_full(other) && !sock_flag(other, SOCK_DEAD))
Rainer Weikusat7d267272015-11-20 22:07:23 +0000494 return 1;
495
496 if (connected)
497 unix_dgram_peer_wake_disconnect(sk, other);
498
499 return 0;
500}
501
Eric Dumazet1586a582015-10-23 10:59:16 -0700502static int unix_writable(const struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Eric Dumazet1586a582015-10-23 10:59:16 -0700504 return sk->sk_state != TCP_LISTEN &&
Reshetova, Elena14afee42017-06-30 13:08:00 +0300505 (refcount_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508static void unix_write_space(struct sock *sk)
509{
Eric Dumazet43815482010-04-29 11:01:49 +0000510 struct socket_wq *wq;
511
512 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (unix_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +0000514 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800515 if (skwq_has_sleeper(wq))
Eric Dumazet67426b72010-10-29 20:44:44 +0000516 wake_up_interruptible_sync_poll(&wq->wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800517 EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800518 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Eric Dumazet43815482010-04-29 11:01:49 +0000520 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/* When dgram socket disconnects (or changes its peer), we clear its receive
524 * queue of packets arrived from previous peer. First, it allows to do
525 * flow control based only on wmem_alloc; second, sk connected to peer
526 * may receive messages only from that peer. */
527static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
528{
David S. Millerb03efcf2005-07-08 14:57:23 -0700529 if (!skb_queue_empty(&sk->sk_receive_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 skb_queue_purge(&sk->sk_receive_queue);
531 wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
532
533 /* If one link of bidirectional dgram pipe is disconnected,
534 * we signal error. Messages are lost. Do not make this,
535 * when peer was not connected to us.
536 */
537 if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) {
538 other->sk_err = ECONNRESET;
Alexander Aringe3ae2362021-06-27 18:48:21 -0400539 sk_error_report(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 }
Eric Dumazetdc56ad72021-08-30 10:21:37 -0700542 other->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static void unix_sock_destructor(struct sock *sk)
546{
547 struct unix_sock *u = unix_sk(sk);
548
549 skb_queue_purge(&sk->sk_receive_queue);
550
Rao Shoaib314001f2021-08-01 00:57:07 -0700551#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
552 if (u->oob_skb) {
553 kfree_skb(u->oob_skb);
554 u->oob_skb = NULL;
555 }
556#endif
Reshetova, Elena14afee42017-06-30 13:08:00 +0300557 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700558 WARN_ON(!sk_unhashed(sk));
559 WARN_ON(sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!sock_flag(sk, SOCK_DEAD)) {
wangweidong5cc208b2013-12-06 18:03:36 +0800561 pr_info("Attempt to release alive unix socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return;
563 }
564
565 if (u->addr)
566 unix_release_addr(u->addr);
567
Eric Dumazet518de9b2010-10-26 14:22:44 -0700568 atomic_long_dec(&unix_nr_socks);
Eric Dumazeta8076d82008-11-17 02:38:49 -0800569 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#ifdef UNIX_REFCNT_DEBUG
wangweidong5cc208b2013-12-06 18:03:36 +0800571 pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk,
Eric Dumazet518de9b2010-10-26 14:22:44 -0700572 atomic_long_read(&unix_nr_socks));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#endif
574}
575
Paul Mooreded34e02013-03-25 03:18:33 +0000576static void unix_release_sock(struct sock *sk, int embrion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct unix_sock *u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400579 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct sock *skpair;
581 struct sk_buff *skb;
582 int state;
583
584 unix_remove_socket(sk);
585
586 /* Clear state */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700587 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 sock_orphan(sk);
589 sk->sk_shutdown = SHUTDOWN_MASK;
Al Viro40ffe672012-03-14 21:54:32 -0400590 path = u->path;
591 u->path.dentry = NULL;
592 u->path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 state = sk->sk_state;
594 sk->sk_state = TCP_CLOSE;
Eric Dumazeta494bd62021-06-16 07:47:15 -0700595
596 skpair = unix_peer(sk);
597 unix_peer(sk) = NULL;
598
David S. Miller1c92b4e2007-05-31 13:24:26 -0700599 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 wake_up_interruptible_all(&u->peer_wait);
602
Jianjun Konge27dfce2008-11-01 21:38:31 -0700603 if (skpair != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
David S. Miller1c92b4e2007-05-31 13:24:26 -0700605 unix_state_lock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* No more writes */
607 skpair->sk_shutdown = SHUTDOWN_MASK;
608 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
609 skpair->sk_err = ECONNRESET;
David S. Miller1c92b4e2007-05-31 13:24:26 -0700610 unix_state_unlock(skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 skpair->sk_state_change(skpair);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +0800612 sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Rainer Weikusat7d267272015-11-20 22:07:23 +0000614
615 unix_dgram_peer_wake_disconnect(sk, skpair);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sock_put(skpair); /* It may now die */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 /* Try to flush out this socket. Throw out buffers at least */
620
621 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Jianjun Konge27dfce2008-11-01 21:38:31 -0700622 if (state == TCP_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unix_release_sock(skb->sk, 1);
624 /* passed fds are erased in the kfree_skb hook */
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +0100625 UNIXCB(skb).consumed = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 kfree_skb(skb);
627 }
628
Al Viro40ffe672012-03-14 21:54:32 -0400629 if (path.dentry)
630 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 sock_put(sk);
633
634 /* ---- Socket is dead now and most probably destroyed ---- */
635
636 /*
Alan Coxe04dae82012-09-17 00:52:41 +0000637 * Fixme: BSD difference: In BSD all sockets connected to us get
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * ECONNRESET and we die on the spot. In Linux we behave
639 * like files and pipes do and wait for the last
640 * dereference.
641 *
642 * Can't we simply set sock->err?
643 *
644 * What the above comment does talk about? --ANK(980817)
645 */
646
Pavel Emelyanov9305cfa2007-11-10 22:06:01 -0800647 if (unix_tot_inflight)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +0900648 unix_gc(); /* Garbage collect fds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000651static void init_peercred(struct sock *sk)
652{
Eric Dumazet35306eb2021-09-29 15:57:50 -0700653 const struct cred *old_cred;
654 struct pid *old_pid;
655
656 spin_lock(&sk->sk_peer_lock);
657 old_pid = sk->sk_peer_pid;
658 old_cred = sk->sk_peer_cred;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000659 sk->sk_peer_pid = get_pid(task_tgid(current));
660 sk->sk_peer_cred = get_current_cred();
Eric Dumazet35306eb2021-09-29 15:57:50 -0700661 spin_unlock(&sk->sk_peer_lock);
662
663 put_pid(old_pid);
664 put_cred(old_cred);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000665}
666
667static void copy_peercred(struct sock *sk, struct sock *peersk)
668{
Eric Dumazet35306eb2021-09-29 15:57:50 -0700669 const struct cred *old_cred;
670 struct pid *old_pid;
671
672 if (sk < peersk) {
673 spin_lock(&sk->sk_peer_lock);
674 spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING);
675 } else {
676 spin_lock(&peersk->sk_peer_lock);
677 spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);
678 }
679 old_pid = sk->sk_peer_pid;
680 old_cred = sk->sk_peer_cred;
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000681 sk->sk_peer_pid = get_pid(peersk->sk_peer_pid);
682 sk->sk_peer_cred = get_cred(peersk->sk_peer_cred);
Eric Dumazet35306eb2021-09-29 15:57:50 -0700683
684 spin_unlock(&sk->sk_peer_lock);
685 spin_unlock(&peersk->sk_peer_lock);
686
687 put_pid(old_pid);
688 put_cred(old_cred);
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static int unix_listen(struct socket *sock, int backlog)
692{
693 int err;
694 struct sock *sk = sock->sk;
695 struct unix_sock *u = unix_sk(sk);
696
697 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800698 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
699 goto out; /* Only stream/seqpacket sockets accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 err = -EINVAL;
701 if (!u->addr)
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800702 goto out; /* No listens on an unbound socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -0700703 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN)
705 goto out_unlock;
706 if (backlog > sk->sk_max_ack_backlog)
707 wake_up_interruptible_all(&u->peer_wait);
708 sk->sk_max_ack_backlog = backlog;
709 sk->sk_state = TCP_LISTEN;
710 /* set credentials so connect can copy them */
Eric W. Biederman109f6e32010-06-13 03:30:14 +0000711 init_peercred(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 err = 0;
713
714out_unlock:
David S. Miller1c92b4e2007-05-31 13:24:26 -0700715 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716out:
717 return err;
718}
719
720static int unix_release(struct socket *);
721static int unix_bind(struct socket *, struct sockaddr *, int);
722static int unix_stream_connect(struct socket *, struct sockaddr *,
723 int addr_len, int flags);
724static int unix_socketpair(struct socket *, struct socket *);
David Howellscdfbabf2017-03-09 08:09:05 +0000725static int unix_accept(struct socket *, struct socket *, int, bool);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100726static int unix_getname(struct socket *, struct sockaddr *, int);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700727static __poll_t unix_poll(struct file *, struct socket *, poll_table *);
728static __poll_t unix_dgram_poll(struct file *, struct socket *,
729 poll_table *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int unix_ioctl(struct socket *, unsigned int, unsigned long);
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200731#ifdef CONFIG_COMPAT
732static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static int unix_shutdown(struct socket *, int);
Ying Xue1b784142015-03-02 15:37:48 +0800735static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
736static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200737static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
738 size_t size, int flags);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200739static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
740 struct pipe_inode_info *, size_t size,
741 unsigned int flags);
Ying Xue1b784142015-03-02 15:37:48 +0800742static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
743static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
Cong Wang29df44f2021-07-04 12:02:44 -0700744static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
745 sk_read_actor_t recv_actor);
Jiang Wang77462de2021-08-16 19:03:20 +0000746static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
747 sk_read_actor_t recv_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static int unix_dgram_connect(struct socket *, struct sockaddr *,
749 int, int);
Ying Xue1b784142015-03-02 15:37:48 +0800750static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
751static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
752 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Sasha Levin12663bf2013-12-07 17:26:27 -0500754static int unix_set_peek_off(struct sock *sk, int val)
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000755{
756 struct unix_sock *u = unix_sk(sk);
757
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700758 if (mutex_lock_interruptible(&u->iolock))
Sasha Levin12663bf2013-12-07 17:26:27 -0500759 return -EINTR;
760
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000761 sk->sk_peek_off = val;
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700762 mutex_unlock(&u->iolock);
Sasha Levin12663bf2013-12-07 17:26:27 -0500763
764 return 0;
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000765}
766
David S. Miller5c05a162020-02-27 11:52:35 -0800767#ifdef CONFIG_PROC_FS
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300768static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
769{
770 struct sock *sk = sock->sk;
771 struct unix_sock *u;
772
773 if (sk) {
774 u = unix_sk(sock->sk);
Paolo Abeni77820402020-02-28 14:45:21 +0100775 seq_printf(m, "scm_fds: %u\n",
776 atomic_read(&u->scm_stat.nr_fds));
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300777 }
778}
Tobias Klauser3a125002020-02-26 18:29:53 +0100779#else
780#define unix_show_fdinfo NULL
781#endif
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000782
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800783static const struct proto_ops unix_stream_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 .family = PF_UNIX,
785 .owner = THIS_MODULE,
786 .release = unix_release,
787 .bind = unix_bind,
788 .connect = unix_stream_connect,
789 .socketpair = unix_socketpair,
790 .accept = unix_accept,
791 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700792 .poll = unix_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200794#ifdef CONFIG_COMPAT
795 .compat_ioctl = unix_compat_ioctl,
796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 .listen = unix_listen,
798 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .sendmsg = unix_stream_sendmsg,
800 .recvmsg = unix_stream_recvmsg,
Jiang Wang77462de2021-08-16 19:03:20 +0000801 .read_sock = unix_stream_read_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .mmap = sock_no_mmap,
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +0200803 .sendpage = unix_stream_sendpage,
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +0200804 .splice_read = unix_stream_splice_read,
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +0000805 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300806 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807};
808
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800809static const struct proto_ops unix_dgram_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 .family = PF_UNIX,
811 .owner = THIS_MODULE,
812 .release = unix_release,
813 .bind = unix_bind,
814 .connect = unix_dgram_connect,
815 .socketpair = unix_socketpair,
816 .accept = sock_no_accept,
817 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700818 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200820#ifdef CONFIG_COMPAT
821 .compat_ioctl = unix_compat_ioctl,
822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .listen = sock_no_listen,
824 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .sendmsg = unix_dgram_sendmsg,
Cong Wang29df44f2021-07-04 12:02:44 -0700826 .read_sock = unix_read_sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 .recvmsg = unix_dgram_recvmsg,
828 .mmap = sock_no_mmap,
829 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000830 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300831 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800834static const struct proto_ops unix_seqpacket_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .family = PF_UNIX,
836 .owner = THIS_MODULE,
837 .release = unix_release,
838 .bind = unix_bind,
839 .connect = unix_stream_connect,
840 .socketpair = unix_socketpair,
841 .accept = unix_accept,
842 .getname = unix_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700843 .poll = unix_dgram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .ioctl = unix_ioctl,
Arnd Bergmann5f6beb92019-06-03 22:03:44 +0200845#ifdef CONFIG_COMPAT
846 .compat_ioctl = unix_compat_ioctl,
847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .listen = unix_listen,
849 .shutdown = unix_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .sendmsg = unix_seqpacket_sendmsg,
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +0000851 .recvmsg = unix_seqpacket_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .mmap = sock_no_mmap,
853 .sendpage = sock_no_sendpage,
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +0000854 .set_peek_off = unix_set_peek_off,
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300855 .show_fdinfo = unix_show_fdinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856};
857
Cong Wangc7272e12021-07-04 12:02:46 -0700858static void unix_close(struct sock *sk, long timeout)
859{
860 /* Nothing to do here, unix socket does not need a ->close().
861 * This is merely for sockmap.
862 */
863}
864
Jiang Wang94531cf2021-08-16 19:03:21 +0000865static void unix_unhash(struct sock *sk)
866{
867 /* Nothing to do here, unix socket does not need a ->unhash().
868 * This is merely for sockmap.
869 */
870}
871
872struct proto unix_dgram_proto = {
Stephen Boyd0edf0822021-10-08 14:59:45 -0700873 .name = "UNIX",
Eric Dumazet248969a2008-11-17 00:00:30 -0800874 .owner = THIS_MODULE,
Eric Dumazet248969a2008-11-17 00:00:30 -0800875 .obj_size = sizeof(struct unix_sock),
Cong Wangc7272e12021-07-04 12:02:46 -0700876 .close = unix_close,
Cong Wangc6382912021-07-04 12:02:47 -0700877#ifdef CONFIG_BPF_SYSCALL
Jiang Wang94531cf2021-08-16 19:03:21 +0000878 .psock_update_sk_prot = unix_dgram_bpf_update_proto,
Cong Wangc6382912021-07-04 12:02:47 -0700879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880};
881
Jiang Wang94531cf2021-08-16 19:03:21 +0000882struct proto unix_stream_proto = {
883 .name = "UNIX-STREAM",
884 .owner = THIS_MODULE,
885 .obj_size = sizeof(struct unix_sock),
886 .close = unix_close,
887 .unhash = unix_unhash,
888#ifdef CONFIG_BPF_SYSCALL
889 .psock_update_sk_prot = unix_stream_bpf_update_proto,
890#endif
891};
892
893static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct unix_sock *u;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900896 struct sock *sk;
897 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Eric Dumazet518de9b2010-10-26 14:22:44 -0700899 atomic_long_inc(&unix_nr_socks);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900900 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) {
901 err = -ENFILE;
902 goto err;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Jiang Wang94531cf2021-08-16 19:03:21 +0000905 if (type == SOCK_STREAM)
906 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_stream_proto, kern);
907 else /*dgram and seqpacket */
908 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_dgram_proto, kern);
909
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900910 if (!sk) {
911 err = -ENOMEM;
912 goto err;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric Dumazet6eba6a32008-11-16 22:58:44 -0800915 sock_init_data(sock, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900917 sk->sk_hash = unix_unbound_hash(sk);
Vladimir Davydov3aa97992016-07-26 15:24:36 -0700918 sk->sk_allocation = GFP_KERNEL_ACCOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sk->sk_write_space = unix_write_space;
Denis V. Luneva0a53c82007-12-11 04:19:17 -0800920 sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sk->sk_destruct = unix_sock_destructor;
922 u = unix_sk(sk);
Al Viro40ffe672012-03-14 21:54:32 -0400923 u->path.dentry = NULL;
924 u->path.mnt = NULL;
Benjamin LaHaisefd19f322006-01-03 14:10:46 -0800925 spin_lock_init(&u->lock);
Al Viro516e0cc2008-07-26 00:39:17 -0400926 atomic_long_set(&u->inflight, 0);
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -0700927 INIT_LIST_HEAD(&u->link);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -0700928 mutex_init(&u->iolock); /* single task reading lock */
929 mutex_init(&u->bindlock); /* single task binding lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 init_waitqueue_head(&u->peer_wait);
Rainer Weikusat7d267272015-11-20 22:07:23 +0000931 init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
Kirill Tkhai3c32da12019-12-09 13:03:46 +0300932 memset(&u->scm_stat, 0, sizeof(struct scm_stat));
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +0900933 unix_insert_unbound_socket(sk);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900934
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900935 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return sk;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900938
939err:
940 atomic_long_dec(&unix_nr_socks);
941 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Eric Paris3f378b62009-11-05 22:18:14 -0800944static int unix_create(struct net *net, struct socket *sock, int protocol,
945 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900947 struct sock *sk;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (protocol && protocol != PF_UNIX)
950 return -EPROTONOSUPPORT;
951
952 sock->state = SS_UNCONNECTED;
953
954 switch (sock->type) {
955 case SOCK_STREAM:
956 sock->ops = &unix_stream_ops;
957 break;
958 /*
959 * Believe it or not BSD has AF_UNIX, SOCK_RAW though
960 * nothing uses it.
961 */
962 case SOCK_RAW:
Jianjun Konge27dfce2008-11-01 21:38:31 -0700963 sock->type = SOCK_DGRAM;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500964 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case SOCK_DGRAM:
966 sock->ops = &unix_dgram_ops;
967 break;
968 case SOCK_SEQPACKET:
969 sock->ops = &unix_seqpacket_ops;
970 break;
971 default:
972 return -ESOCKTNOSUPPORT;
973 }
974
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +0900975 sk = unix_create1(net, sock, kern, sock->type);
976 if (IS_ERR(sk))
977 return PTR_ERR(sk);
978
979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static int unix_release(struct socket *sock)
983{
984 struct sock *sk = sock->sk;
985
986 if (!sk)
987 return 0;
988
Cong Wangc7272e12021-07-04 12:02:46 -0700989 sk->sk_prot->close(sk, 0);
Paul Mooreded34e02013-03-25 03:18:33 +0000990 unix_release_sock(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 sock->sk = NULL;
992
Paul Mooreded34e02013-03-25 03:18:33 +0000993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +0900996static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr,
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +0900997 int addr_len, int type)
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +0900998{
999 struct inode *inode;
1000 struct path path;
1001 struct sock *sk;
1002 int err;
1003
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001004 unix_mkname_bsd(sunaddr, addr_len);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001005 err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path);
1006 if (err)
1007 goto fail;
1008
1009 err = path_permission(&path, MAY_WRITE);
1010 if (err)
1011 goto path_put;
1012
1013 err = -ECONNREFUSED;
1014 inode = d_backing_inode(path.dentry);
1015 if (!S_ISSOCK(inode->i_mode))
1016 goto path_put;
1017
1018 sk = unix_find_socket_byinode(inode);
1019 if (!sk)
1020 goto path_put;
1021
1022 err = -EPROTOTYPE;
1023 if (sk->sk_type == type)
1024 touch_atime(&path);
1025 else
1026 goto sock_put;
1027
1028 path_put(&path);
1029
1030 return sk;
1031
1032sock_put:
1033 sock_put(sk);
1034path_put:
1035 path_put(&path);
1036fail:
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001037 return ERR_PTR(err);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001038}
1039
1040static struct sock *unix_find_abstract(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{
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +09001044 unsigned int hash = unix_abstract_hash(sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001045 struct dentry *dentry;
1046 struct sock *sk;
1047
Kuniyuki Iwashimaf452be42021-11-24 11:14:28 +09001048 sk = unix_find_socket_byname(net, sunaddr, addr_len, hash);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001049 if (!sk)
1050 return ERR_PTR(-ECONNREFUSED);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001051
1052 dentry = unix_sk(sk)->path.dentry;
1053 if (dentry)
1054 touch_atime(&unix_sk(sk)->path);
1055
1056 return sk;
1057}
1058
1059static struct sock *unix_find_other(struct net *net,
1060 struct sockaddr_un *sunaddr,
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001061 int addr_len, int type)
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001062{
1063 struct sock *sk;
1064
1065 if (sunaddr->sun_path[0])
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001066 sk = unix_find_bsd(net, sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001067 else
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001068 sk = unix_find_abstract(net, sunaddr, addr_len, type);
Kuniyuki Iwashimafa39ef02021-11-24 11:14:21 +09001069
1070 return sk;
1071}
1072
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001073static int unix_autobind(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001075 unsigned int new_hash, old_hash = sk->sk_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct unix_sock *u = unix_sk(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001077 struct unix_address *addr;
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001078 u32 lastnum, ordernum;
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001079 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07001081 err = mutex_lock_interruptible(&u->bindlock);
Sasha Levin37ab4fa2013-12-13 10:54:22 -05001082 if (err)
1083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (u->addr)
1086 goto out;
1087
1088 err = -ENOMEM;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09001089 addr = kzalloc(sizeof(*addr) +
1090 offsetof(struct sockaddr_un, sun_path) + 16, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!addr)
1092 goto out;
1093
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001094 addr->len = offsetof(struct sockaddr_un, sun_path) + 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 addr->name->sun_family = AF_UNIX;
Reshetova, Elena8c9814b2017-06-30 13:08:05 +03001096 refcount_set(&addr->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001098 ordernum = prandom_u32();
1099 lastnum = ordernum & 0xFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100retry:
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001101 ordernum = (ordernum + 1) & 0xFFFFF;
1102 sprintf(addr->name->sun_path + 1, "%05x", ordernum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001104 new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001105 unix_table_double_lock(old_hash, new_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001107 if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len,
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001108 new_hash)) {
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001109 unix_table_double_unlock(old_hash, new_hash);
1110
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001111 /* __unix_find_socket_byname() may take long time if many names
Tetsuo Handa8df73ff2010-09-04 01:34:28 +00001112 * are already in use.
1113 */
1114 cond_resched();
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001115
1116 if (ordernum == lastnum) {
1117 /* Give up if all names seems to be in use. */
Tetsuo Handa8df73ff2010-09-04 01:34:28 +00001118 err = -ENOSPC;
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001119 unix_release_addr(addr);
Tetsuo Handa8df73ff2010-09-04 01:34:28 +00001120 goto out;
1121 }
Kuniyuki Iwashima9acbc582021-11-24 11:14:31 +09001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto retry;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001126 __unix_set_addr_hash(sk, addr, new_hash);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001127 unix_table_double_unlock(old_hash, new_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 err = 0;
1129
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07001130out: mutex_unlock(&u->bindlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return err;
1132}
1133
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001134static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr,
1135 int addr_len)
Al Virofaf02012012-07-20 02:37:29 +04001136{
Al Viro71e6be62021-06-19 03:50:30 +00001137 umode_t mode = S_IFSOCK |
1138 (SOCK_INODE(sk->sk_socket)->i_mode & ~current_umask());
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001139 unsigned int new_hash, old_hash = sk->sk_hash;
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001140 struct unix_sock *u = unix_sk(sk);
Al Viro71e6be62021-06-19 03:50:30 +00001141 struct user_namespace *ns; // barf...
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001142 struct unix_address *addr;
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001143 struct dentry *dentry;
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001144 struct path parent;
Al Viro71e6be62021-06-19 03:50:30 +00001145 int err;
1146
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001147 unix_mkname_bsd(sunaddr, addr_len);
1148 addr_len = strlen(sunaddr->sun_path) +
1149 offsetof(struct sockaddr_un, sun_path) + 1;
1150
1151 addr = unix_create_addr(sunaddr, addr_len);
1152 if (!addr)
1153 return -ENOMEM;
1154
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001155 /*
1156 * Get the parent directory, calculate the hash for last
1157 * component.
1158 */
Al Viro71e6be62021-06-19 03:50:30 +00001159 dentry = kern_path_create(AT_FDCWD, addr->name->sun_path, &parent, 0);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001160 if (IS_ERR(dentry)) {
1161 err = PTR_ERR(dentry);
1162 goto out;
1163 }
Al Virofaf02012012-07-20 02:37:29 +04001164
Linus Torvalds38f7bd942016-09-01 14:56:49 -07001165 /*
1166 * All right, let's create it.
1167 */
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001168 ns = mnt_user_ns(parent.mnt);
Al Viro71e6be62021-06-19 03:50:30 +00001169 err = security_path_mknod(&parent, dentry, mode, 0);
Al Viro56c17312021-06-19 03:50:31 +00001170 if (!err)
Al Viro71e6be62021-06-19 03:50:30 +00001171 err = vfs_mknod(ns, d_inode(parent.dentry), dentry, mode, 0);
Al Viroc0c3b8d2021-06-19 03:50:32 +00001172 if (err)
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001173 goto out_path;
Al Virofa42d912021-06-19 03:50:29 +00001174 err = mutex_lock_interruptible(&u->bindlock);
Al Viroc0c3b8d2021-06-19 03:50:32 +00001175 if (err)
1176 goto out_unlink;
1177 if (u->addr)
1178 goto out_unlock;
Al Virofa42d912021-06-19 03:50:29 +00001179
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001180 new_hash = unix_bsd_hash(d_backing_inode(dentry));
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001181 unix_table_double_lock(old_hash, new_hash);
Al Viro56c17312021-06-19 03:50:31 +00001182 u->path.mnt = mntget(parent.mnt);
1183 u->path.dentry = dget(dentry);
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001184 __unix_set_addr_hash(sk, addr, new_hash);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001185 unix_table_double_unlock(old_hash, new_hash);
Al Virofa42d912021-06-19 03:50:29 +00001186 mutex_unlock(&u->bindlock);
Al Viro56c17312021-06-19 03:50:31 +00001187 done_path_create(&parent, dentry);
Al Virofa42d912021-06-19 03:50:29 +00001188 return 0;
Al Viroc0c3b8d2021-06-19 03:50:32 +00001189
1190out_unlock:
1191 mutex_unlock(&u->bindlock);
1192 err = -EINVAL;
1193out_unlink:
1194 /* failed after successful mknod? unlink what we'd created... */
1195 vfs_unlink(ns, d_inode(parent.dentry), dentry, NULL);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001196out_path:
Al Viroc0c3b8d2021-06-19 03:50:32 +00001197 done_path_create(&parent, dentry);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001198out:
1199 unix_release_addr(addr);
1200 return err == -EEXIST ? -EADDRINUSE : err;
Al Virofa42d912021-06-19 03:50:29 +00001201}
1202
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001203static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr,
1204 int addr_len)
Al Virofa42d912021-06-19 03:50:29 +00001205{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001206 unsigned int new_hash, old_hash = sk->sk_hash;
Al Virofa42d912021-06-19 03:50:29 +00001207 struct unix_sock *u = unix_sk(sk);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001208 struct unix_address *addr;
Al Virofa42d912021-06-19 03:50:29 +00001209 int err;
1210
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001211 addr = unix_create_addr(sunaddr, addr_len);
1212 if (!addr)
1213 return -ENOMEM;
1214
Al Virofa42d912021-06-19 03:50:29 +00001215 err = mutex_lock_interruptible(&u->bindlock);
1216 if (err)
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001217 goto out;
Al Virofa42d912021-06-19 03:50:29 +00001218
1219 if (u->addr) {
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001220 err = -EINVAL;
1221 goto out_mutex;
Al Virofa42d912021-06-19 03:50:29 +00001222 }
1223
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001224 new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001225 unix_table_double_lock(old_hash, new_hash);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001226
Al Virofa42d912021-06-19 03:50:29 +00001227 if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len,
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001228 new_hash))
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001229 goto out_spin;
1230
Kuniyuki Iwashimae6b4b872021-11-24 11:14:29 +09001231 __unix_set_addr_hash(sk, addr, new_hash);
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001232 unix_table_double_unlock(old_hash, new_hash);
Al Virofa42d912021-06-19 03:50:29 +00001233 mutex_unlock(&u->bindlock);
1234 return 0;
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001235
1236out_spin:
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001237 unix_table_double_unlock(old_hash, new_hash);
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001238 err = -EADDRINUSE;
1239out_mutex:
1240 mutex_unlock(&u->bindlock);
1241out:
1242 unix_release_addr(addr);
1243 return err;
Al Virofa42d912021-06-19 03:50:29 +00001244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1247{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001248 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Kuniyuki Iwashima5c32a3e2021-11-24 11:14:25 +09001249 struct sock *sk = sock->sk;
Kuniyuki Iwashima5c32a3e2021-11-24 11:14:25 +09001250 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001252 if (addr_len == offsetof(struct sockaddr_un, sun_path) &&
1253 sunaddr->sun_family == AF_UNIX)
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001254 return unix_autobind(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001256 err = unix_validate_addr(sunaddr, addr_len);
1257 if (err)
1258 return err;
1259
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001260 if (sunaddr->sun_path[0])
1261 err = unix_bind_bsd(sk, sunaddr, addr_len);
Al Virofa42d912021-06-19 03:50:29 +00001262 else
Kuniyuki Iwashima12f21c42021-11-24 11:14:26 +09001263 err = unix_bind_abstract(sk, sunaddr, addr_len);
1264
1265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
David S. Miller278a3de2007-05-31 15:19:20 -07001268static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)
1269{
1270 if (unlikely(sk1 == sk2) || !sk2) {
1271 unix_state_lock(sk1);
1272 return;
1273 }
1274 if (sk1 < sk2) {
1275 unix_state_lock(sk1);
1276 unix_state_lock_nested(sk2);
1277 } else {
1278 unix_state_lock(sk2);
1279 unix_state_lock_nested(sk1);
1280 }
1281}
1282
1283static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2)
1284{
1285 if (unlikely(sk1 == sk2) || !sk2) {
1286 unix_state_unlock(sk1);
1287 return;
1288 }
1289 unix_state_unlock(sk1);
1290 unix_state_unlock(sk2);
1291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
1294 int alen, int flags)
1295{
1296 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001297 struct net *net = sock_net(sk);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001298 struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct sock *other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int err;
1301
Mateusz Jurczykdefbcf22017-06-08 11:13:36 +02001302 err = -EINVAL;
1303 if (alen < offsetofend(struct sockaddr, sa_family))
1304 goto out;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (addr->sa_family != AF_UNSPEC) {
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001307 err = unix_validate_addr(sunaddr, alen);
1308 if (err)
1309 goto out;
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (test_bit(SOCK_PASSCRED, &sock->flags) &&
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001312 !unix_sk(sk)->addr) {
1313 err = unix_autobind(sk);
1314 if (err)
1315 goto out;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
David S. Miller278a3de2007-05-31 15:19:20 -07001318restart:
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001319 other = unix_find_other(net, sunaddr, alen, sock->type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001320 if (IS_ERR(other)) {
1321 err = PTR_ERR(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto out;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
David S. Miller278a3de2007-05-31 15:19:20 -07001325 unix_state_double_lock(sk, other);
1326
1327 /* Apparently VFS overslept socket death. Retry. */
1328 if (sock_flag(other, SOCK_DEAD)) {
1329 unix_state_double_unlock(sk, other);
1330 sock_put(other);
1331 goto restart;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 err = -EPERM;
1335 if (!unix_may_send(sk, other))
1336 goto out_unlock;
1337
1338 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1339 if (err)
1340 goto out_unlock;
1341
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001342 sk->sk_state = other->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 } else {
1344 /*
1345 * 1003.1g breaking connected state with AF_UNSPEC
1346 */
1347 other = NULL;
David S. Miller278a3de2007-05-31 15:19:20 -07001348 unix_state_double_lock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 /*
1352 * If it was connected, reconnect.
1353 */
1354 if (unix_peer(sk)) {
1355 struct sock *old_peer = unix_peer(sk);
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001356
Jianjun Konge27dfce2008-11-01 21:38:31 -07001357 unix_peer(sk) = other;
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001358 if (!other)
1359 sk->sk_state = TCP_CLOSE;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001360 unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
1361
David S. Miller278a3de2007-05-31 15:19:20 -07001362 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (other != old_peer)
1365 unix_dgram_disconnected(sk, old_peer);
1366 sock_put(old_peer);
1367 } else {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001368 unix_peer(sk) = other;
David S. Miller278a3de2007-05-31 15:19:20 -07001369 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Cong Wang83301b52021-07-04 12:02:45 -07001371
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374out_unlock:
David S. Miller278a3de2007-05-31 15:19:20 -07001375 unix_state_double_unlock(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sock_put(other);
1377out:
1378 return err;
1379}
1380
1381static long unix_wait_for_peer(struct sock *other, long timeo)
Jules Irenge48851e92020-02-23 23:16:56 +00001382 __releases(&unix_sk(other)->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct unix_sock *u = unix_sk(other);
1385 int sched;
1386 DEFINE_WAIT(wait);
1387
1388 prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE);
1389
1390 sched = !sock_flag(other, SOCK_DEAD) &&
1391 !(other->sk_shutdown & RCV_SHUTDOWN) &&
Rainer Weikusat3c734192008-06-17 22:28:05 -07001392 unix_recvq_full(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
David S. Miller1c92b4e2007-05-31 13:24:26 -07001394 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (sched)
1397 timeo = schedule_timeout(timeo);
1398
1399 finish_wait(&u->peer_wait, &wait);
1400 return timeo;
1401}
1402
1403static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1404 int addr_len, int flags)
1405{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001406 struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001408 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct unix_sock *u = unix_sk(sk), *newu, *otheru;
1410 struct sock *newsk = NULL;
1411 struct sock *other = NULL;
1412 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int st;
1414 int err;
1415 long timeo;
1416
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001417 err = unix_validate_addr(sunaddr, addr_len);
1418 if (err)
1419 goto out;
1420
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001421 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) {
1422 err = unix_autobind(sk);
1423 if (err)
1424 goto out;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
1428
1429 /* First of all allocate resources.
1430 If we will make it after state is locked,
1431 we will have to recheck all again in any case.
1432 */
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* create new sock for complete connection */
Jiang Wang94531cf2021-08-16 19:03:21 +00001435 newsk = unix_create1(sock_net(sk), NULL, 0, sock->type);
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +09001436 if (IS_ERR(newsk)) {
1437 err = PTR_ERR(newsk);
1438 newsk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto out;
Kuniyuki Iwashimaf4bd73b2021-09-28 09:42:27 +09001440 }
1441
1442 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /* Allocate skb for sending to listening sock */
1445 skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL);
1446 if (skb == NULL)
1447 goto out;
1448
1449restart:
1450 /* Find listening sock. */
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001451 other = unix_find_other(net, sunaddr, addr_len, sk->sk_type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001452 if (IS_ERR(other)) {
1453 err = PTR_ERR(other);
1454 other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto out;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /* Latch state of peer */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001459 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Apparently VFS overslept socket death. Retry. */
1462 if (sock_flag(other, SOCK_DEAD)) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001463 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 sock_put(other);
1465 goto restart;
1466 }
1467
1468 err = -ECONNREFUSED;
1469 if (other->sk_state != TCP_LISTEN)
1470 goto out_unlock;
Tomoki Sekiyama77238f22009-10-18 23:17:37 -07001471 if (other->sk_shutdown & RCV_SHUTDOWN)
1472 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Rainer Weikusat3c734192008-06-17 22:28:05 -07001474 if (unix_recvq_full(other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 err = -EAGAIN;
1476 if (!timeo)
1477 goto out_unlock;
1478
1479 timeo = unix_wait_for_peer(other, timeo);
1480
1481 err = sock_intr_errno(timeo);
1482 if (signal_pending(current))
1483 goto out;
1484 sock_put(other);
1485 goto restart;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /* Latch our state.
1489
Daniel Balutae5537bf2011-03-14 15:25:33 -07001490 It is tricky place. We need to grab our state lock and cannot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 drop lock on peer. It is dangerous because deadlock is
1492 possible. Connect to self case and simultaneous
1493 attempt to connect are eliminated by checking socket
1494 state. other is TCP_LISTEN, if sk is TCP_LISTEN we
1495 check this before attempt to grab lock.
1496
1497 Well, and we have to recheck the state after socket locked.
1498 */
1499 st = sk->sk_state;
1500
1501 switch (st) {
1502 case TCP_CLOSE:
1503 /* This is ok... continue with connect */
1504 break;
1505 case TCP_ESTABLISHED:
1506 /* Socket is already connected */
1507 err = -EISCONN;
1508 goto out_unlock;
1509 default:
1510 err = -EINVAL;
1511 goto out_unlock;
1512 }
1513
David S. Miller1c92b4e2007-05-31 13:24:26 -07001514 unix_state_lock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (sk->sk_state != st) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001517 unix_state_unlock(sk);
1518 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 sock_put(other);
1520 goto restart;
1521 }
1522
David S. Miller3610cda2011-01-05 15:38:53 -08001523 err = security_unix_stream_connect(sk, other, newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (err) {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001525 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 goto out_unlock;
1527 }
1528
1529 /* The way is open! Fastly set all the necessary fields... */
1530
1531 sock_hold(sk);
1532 unix_peer(newsk) = sk;
1533 newsk->sk_state = TCP_ESTABLISHED;
1534 newsk->sk_type = sk->sk_type;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001535 init_peercred(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 newu = unix_sk(newsk);
Eric Dumazeteaefd1102011-02-18 03:26:36 +00001537 RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 otheru = unix_sk(other);
1539
Al Viroae3b5642019-02-15 20:09:35 +00001540 /* copy address information from listening to new sock
1541 *
1542 * The contents of *(otheru->addr) and otheru->path
1543 * are seen fully set up here, since we have found
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001544 * otheru in hash under unix_table_locks. Insertion
Al Viroae3b5642019-02-15 20:09:35 +00001545 * into the hash chain we'd found it in had been done
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09001546 * in an earlier critical area protected by unix_table_locks,
Al Viroae3b5642019-02-15 20:09:35 +00001547 * the same one where we'd set *(otheru->addr) contents,
1548 * as well as otheru->path and otheru->addr itself.
1549 *
1550 * Using smp_store_release() here to set newu->addr
1551 * is enough to make those stores, as well as stores
1552 * to newu->path visible to anyone who gets newu->addr
1553 * by smp_load_acquire(). IOW, the same warranties
1554 * as for unix_sock instances bound in unix_bind() or
1555 * in unix_autobind().
1556 */
Al Viro40ffe672012-03-14 21:54:32 -04001557 if (otheru->path.dentry) {
1558 path_get(&otheru->path);
1559 newu->path = otheru->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Al Viroae3b5642019-02-15 20:09:35 +00001561 refcount_inc(&otheru->addr->refcnt);
1562 smp_store_release(&newu->addr, otheru->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 /* Set credentials */
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001565 copy_peercred(sk, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 sock->state = SS_CONNECTED;
1568 sk->sk_state = TCP_ESTABLISHED;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001569 sock_hold(newsk);
1570
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001571 smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08001572 unix_peer(sk) = newsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
David S. Miller1c92b4e2007-05-31 13:24:26 -07001574 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
gushengxian4e03d072021-06-09 20:09:35 -07001576 /* take ten and send info to listening sock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 spin_lock(&other->sk_receive_queue.lock);
1578 __skb_queue_tail(&other->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 spin_unlock(&other->sk_receive_queue.lock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001580 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04001581 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 sock_put(other);
1583 return 0;
1584
1585out_unlock:
1586 if (other)
David S. Miller1c92b4e2007-05-31 13:24:26 -07001587 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589out:
Wei Yongjun40d44442009-02-25 00:32:45 +00001590 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (newsk)
1592 unix_release_sock(newsk, 0);
1593 if (other)
1594 sock_put(other);
1595 return err;
1596}
1597
1598static int unix_socketpair(struct socket *socka, struct socket *sockb)
1599{
Jianjun Konge27dfce2008-11-01 21:38:31 -07001600 struct sock *ska = socka->sk, *skb = sockb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* Join our sockets back to back */
1603 sock_hold(ska);
1604 sock_hold(skb);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001605 unix_peer(ska) = skb;
1606 unix_peer(skb) = ska;
Eric W. Biederman109f6e32010-06-13 03:30:14 +00001607 init_peercred(ska);
1608 init_peercred(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Cong Wang83301b52021-07-04 12:02:45 -07001610 ska->sk_state = TCP_ESTABLISHED;
1611 skb->sk_state = TCP_ESTABLISHED;
1612 socka->state = SS_CONNECTED;
1613 sockb->state = SS_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
1616
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001617static void unix_sock_inherit_flags(const struct socket *old,
1618 struct socket *new)
1619{
1620 if (test_bit(SOCK_PASSCRED, &old->flags))
1621 set_bit(SOCK_PASSCRED, &new->flags);
1622 if (test_bit(SOCK_PASSSEC, &old->flags))
1623 set_bit(SOCK_PASSSEC, &new->flags);
1624}
1625
David Howellscdfbabf2017-03-09 08:09:05 +00001626static int unix_accept(struct socket *sock, struct socket *newsock, int flags,
1627 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct sock *sk = sock->sk;
1630 struct sock *tsk;
1631 struct sk_buff *skb;
1632 int err;
1633
1634 err = -EOPNOTSUPP;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08001635 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto out;
1637
1638 err = -EINVAL;
1639 if (sk->sk_state != TCP_LISTEN)
1640 goto out;
1641
1642 /* If socket state is TCP_LISTEN it cannot change (for now...),
1643 * so that no locks are necessary.
1644 */
1645
1646 skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err);
1647 if (!skb) {
1648 /* This means receive shutdown. */
1649 if (err == 0)
1650 err = -EINVAL;
1651 goto out;
1652 }
1653
1654 tsk = skb->sk;
1655 skb_free_datagram(sk, skb);
1656 wake_up_interruptible(&unix_sk(sk)->peer_wait);
1657
1658 /* attach accepted sock to socket */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001659 unix_state_lock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 newsock->state = SS_CONNECTED;
Daniel Borkmann90c6bd32013-10-17 22:51:31 +02001661 unix_sock_inherit_flags(sock, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 sock_graft(tsk, newsock);
David S. Miller1c92b4e2007-05-31 13:24:26 -07001663 unix_state_unlock(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665
1666out:
1667 return err;
1668}
1669
1670
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001671static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct sock *sk = sock->sk;
Al Viroae3b5642019-02-15 20:09:35 +00001674 struct unix_address *addr;
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001675 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int err = 0;
1677
1678 if (peer) {
1679 sk = unix_peer_get(sk);
1680
1681 err = -ENOTCONN;
1682 if (!sk)
1683 goto out;
1684 err = 0;
1685 } else {
1686 sock_hold(sk);
1687 }
1688
Al Viroae3b5642019-02-15 20:09:35 +00001689 addr = smp_load_acquire(&unix_sk(sk)->addr);
1690 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 sunaddr->sun_family = AF_UNIX;
1692 sunaddr->sun_path[0] = 0;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09001693 err = offsetof(struct sockaddr_un, sun_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001695 err = addr->len;
1696 memcpy(sunaddr, addr->name, addr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 sock_put(sk);
1699out:
1700 return err;
1701}
1702
Miklos Szeredicbcf0112021-07-28 14:47:20 +02001703static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb)
1704{
1705 scm->fp = scm_fp_dup(UNIXCB(skb).fp);
1706
1707 /*
1708 * Garbage collection of unix sockets starts by selecting a set of
1709 * candidate sockets which have reference only from being in flight
1710 * (total_refs == inflight_refs). This condition is checked once during
1711 * the candidate collection phase, and candidates are marked as such, so
1712 * that non-candidates can later be ignored. While inflight_refs is
1713 * protected by unix_gc_lock, total_refs (file count) is not, hence this
1714 * is an instantaneous decision.
1715 *
1716 * Once a candidate, however, the socket must not be reinstalled into a
1717 * file descriptor while the garbage collection is in progress.
1718 *
1719 * If the above conditions are met, then the directed graph of
1720 * candidates (*) does not change while unix_gc_lock is held.
1721 *
1722 * Any operations that changes the file count through file descriptors
1723 * (dup, close, sendmsg) does not change the graph since candidates are
1724 * not installed in fds.
1725 *
1726 * Dequeing a candidate via recvmsg would install it into an fd, but
1727 * that takes unix_gc_lock to decrement the inflight count, so it's
1728 * serialized with garbage collection.
1729 *
1730 * MSG_PEEK is special in that it does not change the inflight count,
1731 * yet does install the socket into an fd. The following lock/unlock
1732 * pair is to ensure serialization with garbage collection. It must be
1733 * done between incrementing the file count and installing the file into
1734 * an fd.
1735 *
1736 * If garbage collection starts after the barrier provided by the
1737 * lock/unlock, then it will see the elevated refcount and not mark this
1738 * as a candidate. If a garbage collection is already in progress
1739 * before the file count was incremented, then the lock/unlock pair will
1740 * ensure that garbage collection is finished before progressing to
1741 * installing the fd.
1742 *
1743 * (*) A -> B where B is on the queue of A or B is on the queue of C
1744 * which is on the queue of listening socket A.
1745 */
1746 spin_lock(&unix_gc_lock);
1747 spin_unlock(&unix_gc_lock);
1748}
1749
David S. Millerf78a5fd2011-09-16 19:34:00 -04001750static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001751{
1752 int err = 0;
Eric Dumazet16e57262011-09-19 05:52:27 +00001753
David S. Millerf78a5fd2011-09-16 19:34:00 -04001754 UNIXCB(skb).pid = get_pid(scm->pid);
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001755 UNIXCB(skb).uid = scm->creds.uid;
1756 UNIXCB(skb).gid = scm->creds.gid;
Eric W. Biederman7361c362010-06-13 03:34:33 +00001757 UNIXCB(skb).fp = NULL;
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04001758 unix_get_secdata(scm, skb);
Eric W. Biederman7361c362010-06-13 03:34:33 +00001759 if (scm->fp && send_fds)
1760 err = unix_attach_fds(scm, skb);
1761
1762 skb->destructor = unix_destruct_scm;
1763 return err;
1764}
1765
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001766static bool unix_passcred_enabled(const struct socket *sock,
1767 const struct sock *other)
1768{
1769 return test_bit(SOCK_PASSCRED, &sock->flags) ||
1770 !other->sk_socket ||
1771 test_bit(SOCK_PASSCRED, &other->sk_socket->flags);
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
Eric Dumazet16e57262011-09-19 05:52:27 +00001775 * Some apps rely on write() giving SCM_CREDENTIALS
1776 * We include credentials if source or destination socket
1777 * asserted SOCK_PASSCRED.
1778 */
1779static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1780 const struct sock *other)
1781{
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +00001782 if (UNIXCB(skb).pid)
Eric Dumazet16e57262011-09-19 05:52:27 +00001783 return;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001784 if (unix_passcred_enabled(sock, other)) {
Eric Dumazet16e57262011-09-19 05:52:27 +00001785 UNIXCB(skb).pid = get_pid(task_tgid(current));
David S. Miller6e0895c2013-04-22 20:32:51 -04001786 current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
Eric Dumazet16e57262011-09-19 05:52:27 +00001787 }
1788}
1789
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01001790static int maybe_init_creds(struct scm_cookie *scm,
1791 struct socket *socket,
1792 const struct sock *other)
1793{
1794 int err;
1795 struct msghdr msg = { .msg_controllen = 0 };
1796
1797 err = scm_send(socket, &msg, scm, false);
1798 if (err)
1799 return err;
1800
1801 if (unix_passcred_enabled(socket, other)) {
1802 scm->pid = get_pid(task_tgid(current));
1803 current_uid_gid(&scm->creds.uid, &scm->creds.gid);
1804 }
1805 return err;
1806}
1807
1808static bool unix_skb_scm_eq(struct sk_buff *skb,
1809 struct scm_cookie *scm)
1810{
1811 const struct unix_skb_parms *u = &UNIXCB(skb);
1812
1813 return u->pid == scm->pid &&
1814 uid_eq(u->uid, scm->creds.uid) &&
1815 gid_eq(u->gid, scm->creds.gid) &&
1816 unix_secdata_eq(scm, skb);
1817}
1818
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001819static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
1820{
1821 struct scm_fp_list *fp = UNIXCB(skb).fp;
1822 struct unix_sock *u = unix_sk(sk);
1823
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001824 if (unlikely(fp && fp->count))
Paolo Abeni77820402020-02-28 14:45:21 +01001825 atomic_add(fp->count, &u->scm_stat.nr_fds);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001826}
1827
1828static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
1829{
1830 struct scm_fp_list *fp = UNIXCB(skb).fp;
1831 struct unix_sock *u = unix_sk(sk);
1832
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001833 if (unlikely(fp && fp->count))
Paolo Abeni77820402020-02-28 14:45:21 +01001834 atomic_sub(fp->count, &u->scm_stat.nr_fds);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03001835}
1836
Eric Dumazet16e57262011-09-19 05:52:27 +00001837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * Send AF_UNIX data.
1839 */
1840
Ying Xue1b784142015-03-02 15:37:48 +08001841static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1842 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001845 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct unix_sock *u = unix_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001847 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct sock *other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 int err;
David S. Millerf78a5fd2011-09-16 19:34:00 -04001850 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 long timeo;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001852 struct scm_cookie scm;
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001853 int data_len = 0;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001854 int sk_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
dann frazier5f23b732008-11-26 15:32:27 -08001856 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001857 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (err < 0)
1859 return err;
1860
1861 err = -EOPNOTSUPP;
1862 if (msg->msg_flags&MSG_OOB)
1863 goto out;
1864
1865 if (msg->msg_namelen) {
Kuniyuki Iwashimab8a58aa2021-11-24 11:14:23 +09001866 err = unix_validate_addr(sunaddr, msg->msg_namelen);
1867 if (err)
1868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 } else {
1870 sunaddr = NULL;
1871 err = -ENOTCONN;
1872 other = unix_peer_get(sk);
1873 if (!other)
1874 goto out;
1875 }
1876
Kuniyuki Iwashimaf7ed31f2021-11-24 11:14:20 +09001877 if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) {
1878 err = unix_autobind(sk);
1879 if (err)
1880 goto out;
1881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 err = -EMSGSIZE;
1884 if (len > sk->sk_sndbuf - 32)
1885 goto out;
1886
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001887 if (len > SKB_MAX_ALLOC) {
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001888 data_len = min_t(size_t,
1889 len - SKB_MAX_ALLOC,
1890 MAX_SKB_FRAGS * PAGE_SIZE);
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04001891 data_len = PAGE_ALIGN(data_len);
1892
1893 BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE);
1894 }
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001895
1896 skb = sock_alloc_send_pskb(sk, len - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07001897 msg->msg_flags & MSG_DONTWAIT, &err,
1898 PAGE_ALLOC_COSTLY_ORDER);
Jianjun Konge27dfce2008-11-01 21:38:31 -07001899 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 goto out;
1901
Christoph Hellwig7cc05662015-01-28 18:04:53 +01001902 err = unix_scm_to_skb(&scm, skb, true);
Eric Dumazet25888e32010-11-25 04:11:39 +00001903 if (err < 0)
Eric W. Biederman7361c362010-06-13 03:34:33 +00001904 goto out_free;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07001905
Eric Dumazeteb6a2482012-04-03 05:28:28 +00001906 skb_put(skb, len - data_len);
1907 skb->data_len = data_len;
1908 skb->len = len;
Al Viroc0371da2014-11-24 10:42:55 -05001909 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (err)
1911 goto out_free;
1912
1913 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
1914
1915restart:
1916 if (!other) {
1917 err = -ECONNRESET;
1918 if (sunaddr == NULL)
1919 goto out_free;
1920
Kuniyuki Iwashimad2d8c9f2021-11-24 11:14:24 +09001921 other = unix_find_other(net, sunaddr, msg->msg_namelen,
1922 sk->sk_type);
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001923 if (IS_ERR(other)) {
1924 err = PTR_ERR(other);
1925 other = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 goto out_free;
Kuniyuki Iwashimaaed26f52021-11-24 11:14:22 +09001927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929
Alban Crequyd6ae3ba2011-01-18 06:39:15 +00001930 if (sk_filter(other, skb) < 0) {
1931 /* Toss the packet but do not return any error to the sender */
1932 err = len;
1933 goto out_free;
1934 }
1935
Rainer Weikusat7d267272015-11-20 22:07:23 +00001936 sk_locked = 0;
David S. Miller1c92b4e2007-05-31 13:24:26 -07001937 unix_state_lock(other);
Rainer Weikusat7d267272015-11-20 22:07:23 +00001938restart_locked:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 err = -EPERM;
1940 if (!unix_may_send(sk, other))
1941 goto out_unlock;
1942
Rainer Weikusat7d267272015-11-20 22:07:23 +00001943 if (unlikely(sock_flag(other, SOCK_DEAD))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /*
1945 * Check with 1003.1g - what should
1946 * datagram error
1947 */
David S. Miller1c92b4e2007-05-31 13:24:26 -07001948 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 sock_put(other);
1950
Rainer Weikusat7d267272015-11-20 22:07:23 +00001951 if (!sk_locked)
1952 unix_state_lock(sk);
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (unix_peer(sk) == other) {
Jianjun Konge27dfce2008-11-01 21:38:31 -07001956 unix_peer(sk) = NULL;
Rainer Weikusat7d267272015-11-20 22:07:23 +00001957 unix_dgram_peer_wake_disconnect_wakeup(sk, other);
1958
David S. Miller1c92b4e2007-05-31 13:24:26 -07001959 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Eric Dumazetdc56ad72021-08-30 10:21:37 -07001961 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 unix_dgram_disconnected(sk, other);
1963 sock_put(other);
1964 err = -ECONNREFUSED;
1965 } else {
David S. Miller1c92b4e2007-05-31 13:24:26 -07001966 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968
1969 other = NULL;
1970 if (err)
1971 goto out_free;
1972 goto restart;
1973 }
1974
1975 err = -EPIPE;
1976 if (other->sk_shutdown & RCV_SHUTDOWN)
1977 goto out_unlock;
1978
1979 if (sk->sk_type != SOCK_SEQPACKET) {
1980 err = security_unix_may_send(sk->sk_socket, other->sk_socket);
1981 if (err)
1982 goto out_unlock;
1983 }
1984
Rainer Weikusata5527dd2016-02-11 19:37:27 +00001985 /* other == sk && unix_peer(other) != sk if
1986 * - unix_peer(sk) == NULL, destination address bound to sk
1987 * - unix_peer(sk) == sk by time of get but disconnected before lock
1988 */
1989 if (other != sk &&
Qian Cai86b18aa2020-02-04 13:40:29 -05001990 unlikely(unix_peer(other) != sk &&
1991 unix_recvq_full_lockless(other))) {
Rainer Weikusat7d267272015-11-20 22:07:23 +00001992 if (timeo) {
1993 timeo = unix_wait_for_peer(other, timeo);
1994
1995 err = sock_intr_errno(timeo);
1996 if (signal_pending(current))
1997 goto out_free;
1998
1999 goto restart;
2000 }
2001
2002 if (!sk_locked) {
2003 unix_state_unlock(other);
2004 unix_state_double_lock(sk, other);
2005 }
2006
2007 if (unix_peer(sk) != other ||
2008 unix_dgram_peer_wake_me(sk, other)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 err = -EAGAIN;
Rainer Weikusat7d267272015-11-20 22:07:23 +00002010 sk_locked = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto out_unlock;
2012 }
2013
Rainer Weikusat7d267272015-11-20 22:07:23 +00002014 if (!sk_locked) {
2015 sk_locked = 1;
2016 goto restart_locked;
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Rainer Weikusat7d267272015-11-20 22:07:23 +00002020 if (unlikely(sk_locked))
2021 unix_state_unlock(sk);
2022
Alban Crequy3f661162010-10-04 08:48:28 +00002023 if (sock_flag(other, SOCK_RCVTSTAMP))
2024 __net_timestamp(skb);
Eric Dumazet16e57262011-09-19 05:52:27 +00002025 maybe_add_creds(skb, sock, other);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002026 scm_stat_add(other, skb);
Paolo Abeni77820402020-02-28 14:45:21 +01002027 skb_queue_tail(&other->sk_receive_queue, skb);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002028 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04002029 other->sk_data_ready(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002031 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return len;
2033
2034out_unlock:
Rainer Weikusat7d267272015-11-20 22:07:23 +00002035 if (sk_locked)
2036 unix_state_unlock(sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002037 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038out_free:
2039 kfree_skb(skb);
2040out:
2041 if (other)
2042 sock_put(other);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002043 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return err;
2045}
2046
Eric Dumazete370a722013-08-08 14:37:32 -07002047/* We use paged skbs for stream sockets, and limit occupancy to 32768
Tobias Klauserd4e9a402018-02-13 11:11:30 +01002048 * bytes, and a minimum of a full page.
Eric Dumazete370a722013-08-08 14:37:32 -07002049 */
2050#define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002051
Rao Shoaib314001f2021-08-01 00:57:07 -07002052#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2053static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other)
2054{
2055 struct unix_sock *ousk = unix_sk(other);
2056 struct sk_buff *skb;
2057 int err = 0;
2058
2059 skb = sock_alloc_send_skb(sock->sk, 1, msg->msg_flags & MSG_DONTWAIT, &err);
2060
2061 if (!skb)
2062 return err;
2063
2064 skb_put(skb, 1);
Rao Shoaib314001f2021-08-01 00:57:07 -07002065 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1);
2066
2067 if (err) {
2068 kfree_skb(skb);
2069 return err;
2070 }
2071
2072 unix_state_lock(other);
Rao Shoaib19eed722021-08-13 11:19:34 -07002073
2074 if (sock_flag(other, SOCK_DEAD) ||
2075 (other->sk_shutdown & RCV_SHUTDOWN)) {
2076 unix_state_unlock(other);
2077 kfree_skb(skb);
2078 return -EPIPE;
2079 }
2080
Rao Shoaib314001f2021-08-01 00:57:07 -07002081 maybe_add_creds(skb, sock, other);
2082 skb_get(skb);
2083
2084 if (ousk->oob_skb)
Rao Shoaib19eed722021-08-13 11:19:34 -07002085 consume_skb(ousk->oob_skb);
Rao Shoaib314001f2021-08-01 00:57:07 -07002086
2087 ousk->oob_skb = skb;
2088
2089 scm_stat_add(other, skb);
2090 skb_queue_tail(&other->sk_receive_queue, skb);
2091 sk_send_sigurg(other);
2092 unix_state_unlock(other);
2093 other->sk_data_ready(other);
2094
2095 return err;
2096}
2097#endif
2098
Ying Xue1b784142015-03-02 15:37:48 +08002099static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
2100 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct sock *sk = sock->sk;
2103 struct sock *other = NULL;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002104 int err, size;
David S. Millerf78a5fd2011-09-16 19:34:00 -04002105 struct sk_buff *skb;
Jianjun Konge27dfce2008-11-01 21:38:31 -07002106 int sent = 0;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002107 struct scm_cookie scm;
Miklos Szeredi8ba69ba2009-09-11 11:31:45 -07002108 bool fds_sent = false;
Eric Dumazete370a722013-08-08 14:37:32 -07002109 int data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
dann frazier5f23b732008-11-26 15:32:27 -08002111 wait_for_unix_gc();
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002112 err = scm_send(sock, msg, &scm, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (err < 0)
2114 return err;
2115
2116 err = -EOPNOTSUPP;
Rao Shoaib314001f2021-08-01 00:57:07 -07002117 if (msg->msg_flags & MSG_OOB) {
2118#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2119 if (len)
2120 len--;
2121 else
2122#endif
2123 goto out_err;
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 if (msg->msg_namelen) {
2127 err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
2128 goto out_err;
2129 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 err = -ENOTCONN;
Benjamin LaHaise830a1e52005-12-13 23:22:32 -08002131 other = unix_peer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!other)
2133 goto out_err;
2134 }
2135
2136 if (sk->sk_shutdown & SEND_SHUTDOWN)
2137 goto pipe_err;
2138
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002139 while (sent < len) {
Eric Dumazete370a722013-08-08 14:37:32 -07002140 size = len - sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /* Keep two messages in the pipe so it schedules better */
Eric Dumazete370a722013-08-08 14:37:32 -07002143 size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Eric Dumazete370a722013-08-08 14:37:32 -07002145 /* allow fallback to order-0 allocations */
2146 size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002147
Eric Dumazete370a722013-08-08 14:37:32 -07002148 data_len = max_t(int, 0, size - SKB_MAX_HEAD(0));
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002149
Kirill Tkhai31ff6aa2014-05-15 19:56:28 +04002150 data_len = min_t(size_t, size, PAGE_ALIGN(data_len));
2151
Eric Dumazete370a722013-08-08 14:37:32 -07002152 skb = sock_alloc_send_pskb(sk, size - data_len, data_len,
Eric Dumazet28d64272013-08-08 14:38:47 -07002153 msg->msg_flags & MSG_DONTWAIT, &err,
2154 get_order(UNIX_SKB_FRAGS_SZ));
Eric Dumazete370a722013-08-08 14:37:32 -07002155 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto out_err;
2157
David S. Millerf78a5fd2011-09-16 19:34:00 -04002158 /* Only send the fds in the first buffer */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002159 err = unix_scm_to_skb(&scm, skb, !fds_sent);
Eric Dumazet25888e32010-11-25 04:11:39 +00002160 if (err < 0) {
Eric W. Biederman7361c362010-06-13 03:34:33 +00002161 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04002162 goto out_err;
Miklos Szeredi62093442008-11-09 15:23:57 +01002163 }
Eric W. Biederman7361c362010-06-13 03:34:33 +00002164 fds_sent = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Eric Dumazete370a722013-08-08 14:37:32 -07002166 skb_put(skb, size - data_len);
2167 skb->data_len = data_len;
2168 skb->len = size;
Al Viroc0371da2014-11-24 10:42:55 -05002169 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002170 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 kfree_skb(skb);
David S. Millerf78a5fd2011-09-16 19:34:00 -04002172 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174
David S. Miller1c92b4e2007-05-31 13:24:26 -07002175 unix_state_lock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if (sock_flag(other, SOCK_DEAD) ||
2178 (other->sk_shutdown & RCV_SHUTDOWN))
2179 goto pipe_err_free;
2180
Eric Dumazet16e57262011-09-19 05:52:27 +00002181 maybe_add_creds(skb, sock, other);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002182 scm_stat_add(other, skb);
Paolo Abeni77820402020-02-28 14:45:21 +01002183 skb_queue_tail(&other->sk_receive_queue, skb);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002184 unix_state_unlock(other);
David S. Miller676d2362014-04-11 16:15:36 -04002185 other->sk_data_ready(other);
Jianjun Konge27dfce2008-11-01 21:38:31 -07002186 sent += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Rao Shoaib314001f2021-08-01 00:57:07 -07002189#if (IS_ENABLED(CONFIG_AF_UNIX_OOB))
2190 if (msg->msg_flags & MSG_OOB) {
2191 err = queue_oob(sock, msg, other);
2192 if (err)
2193 goto out_err;
2194 sent++;
2195 }
2196#endif
2197
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002198 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 return sent;
2201
2202pipe_err_free:
David S. Miller1c92b4e2007-05-31 13:24:26 -07002203 unix_state_unlock(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 kfree_skb(skb);
2205pipe_err:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002206 if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL))
2207 send_sig(SIGPIPE, current, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 err = -EPIPE;
2209out_err:
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002210 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return sent ? : err;
2212}
2213
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002214static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
2215 int offset, size_t size, int flags)
2216{
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002217 int err;
2218 bool send_sigpipe = false;
2219 bool init_scm = true;
2220 struct scm_cookie scm;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002221 struct sock *other, *sk = socket->sk;
2222 struct sk_buff *skb, *newskb = NULL, *tail = NULL;
2223
2224 if (flags & MSG_OOB)
2225 return -EOPNOTSUPP;
2226
2227 other = unix_peer(sk);
2228 if (!other || sk->sk_state != TCP_ESTABLISHED)
2229 return -ENOTCONN;
2230
2231 if (false) {
2232alloc_skb:
2233 unix_state_unlock(other);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002234 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002235 newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
2236 &err, 0);
2237 if (!newskb)
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002238 goto err;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002239 }
2240
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002241 /* we must acquire iolock as we modify already present
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002242 * skbs in the sk_receive_queue and mess with skb->len
2243 */
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002244 err = mutex_lock_interruptible(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002245 if (err) {
2246 err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002247 goto err;
2248 }
2249
2250 if (sk->sk_shutdown & SEND_SHUTDOWN) {
2251 err = -EPIPE;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002252 send_sigpipe = true;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002253 goto err_unlock;
2254 }
2255
2256 unix_state_lock(other);
2257
2258 if (sock_flag(other, SOCK_DEAD) ||
2259 other->sk_shutdown & RCV_SHUTDOWN) {
2260 err = -EPIPE;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002261 send_sigpipe = true;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002262 goto err_state_unlock;
2263 }
2264
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002265 if (init_scm) {
2266 err = maybe_init_creds(&scm, socket, other);
2267 if (err)
2268 goto err_state_unlock;
2269 init_scm = false;
2270 }
2271
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002272 skb = skb_peek_tail(&other->sk_receive_queue);
2273 if (tail && tail == skb) {
2274 skb = newskb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002275 } else if (!skb || !unix_skb_scm_eq(skb, &scm)) {
2276 if (newskb) {
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002277 skb = newskb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002278 } else {
2279 tail = skb;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002280 goto alloc_skb;
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002281 }
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002282 } else if (newskb) {
2283 /* this is fast path, we don't necessarily need to
2284 * call to kfree_skb even though with newskb == NULL
2285 * this - does no harm
2286 */
2287 consume_skb(newskb);
Hannes Frederic Sowa8844f972015-11-16 16:25:56 +01002288 newskb = NULL;
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002289 }
2290
2291 if (skb_append_pagefrags(skb, page, offset, size)) {
2292 tail = skb;
2293 goto alloc_skb;
2294 }
2295
2296 skb->len += size;
2297 skb->data_len += size;
2298 skb->truesize += size;
Reshetova, Elena14afee42017-06-30 13:08:00 +03002299 refcount_add(size, &sk->sk_wmem_alloc);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002300
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002301 if (newskb) {
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002302 err = unix_scm_to_skb(&scm, skb, false);
2303 if (err)
2304 goto err_state_unlock;
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002305 spin_lock(&other->sk_receive_queue.lock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002306 __skb_queue_tail(&other->sk_receive_queue, newskb);
Hannes Frederic Sowaa3a116e2015-11-17 15:10:59 +01002307 spin_unlock(&other->sk_receive_queue.lock);
2308 }
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002309
2310 unix_state_unlock(other);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002311 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002312
2313 other->sk_data_ready(other);
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002314 scm_destroy(&scm);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002315 return size;
2316
2317err_state_unlock:
2318 unix_state_unlock(other);
2319err_unlock:
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002320 mutex_unlock(&unix_sk(other)->iolock);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002321err:
2322 kfree_skb(newskb);
2323 if (send_sigpipe && !(flags & MSG_NOSIGNAL))
2324 send_sig(SIGPIPE, current, 0);
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002325 if (!init_scm)
2326 scm_destroy(&scm);
Hannes Frederic Sowa869e7c62015-05-21 16:59:59 +02002327 return err;
2328}
2329
Ying Xue1b784142015-03-02 15:37:48 +08002330static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
2331 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 int err;
2334 struct sock *sk = sock->sk;
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 err = sock_error(sk);
2337 if (err)
2338 return err;
2339
2340 if (sk->sk_state != TCP_ESTABLISHED)
2341 return -ENOTCONN;
2342
2343 if (msg->msg_namelen)
2344 msg->msg_namelen = 0;
2345
Ying Xue1b784142015-03-02 15:37:48 +08002346 return unix_dgram_sendmsg(sock, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002348
Ying Xue1b784142015-03-02 15:37:48 +08002349static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
2350 size_t size, int flags)
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00002351{
2352 struct sock *sk = sock->sk;
2353
2354 if (sk->sk_state != TCP_ESTABLISHED)
2355 return -ENOTCONN;
2356
Ying Xue1b784142015-03-02 15:37:48 +08002357 return unix_dgram_recvmsg(sock, msg, size, flags);
Eric W. Biedermana05d2ad2011-04-24 01:54:57 +00002358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
2361{
Al Viroae3b5642019-02-15 20:09:35 +00002362 struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Al Viroae3b5642019-02-15 20:09:35 +00002364 if (addr) {
2365 msg->msg_namelen = addr->len;
2366 memcpy(msg->msg_name, addr->name, addr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368}
2369
Cong Wang9825d862021-07-04 12:02:48 -07002370int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
2371 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002373 struct scm_cookie scm;
Cong Wang9825d862021-07-04 12:02:48 -07002374 struct socket *sock = sk->sk_socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct unix_sock *u = unix_sk(sk);
Rainer Weikusat64874282015-12-06 21:11:38 +00002376 struct sk_buff *skb, *last;
2377 long timeo;
Paolo Abenifd69c392019-04-08 10:15:59 +02002378 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 int err;
2380
2381 err = -EOPNOTSUPP;
2382 if (flags&MSG_OOB)
2383 goto out;
2384
Rainer Weikusat64874282015-12-06 21:11:38 +00002385 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Rainer Weikusat64874282015-12-06 21:11:38 +00002387 do {
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002388 mutex_lock(&u->iolock);
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002389
Rainer Weikusat64874282015-12-06 21:11:38 +00002390 skip = sk_peek_offset(sk, flags);
Sabrina Dubrocab50b0582019-11-25 14:48:57 +01002391 skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
Paolo Abenie427cad2020-02-28 14:45:22 +01002392 &skip, &err, &last);
2393 if (skb) {
2394 if (!(flags & MSG_PEEK))
2395 scm_stat_del(sk, skb);
Rainer Weikusat64874282015-12-06 21:11:38 +00002396 break;
Paolo Abenie427cad2020-02-28 14:45:22 +01002397 }
Rainer Weikusat64874282015-12-06 21:11:38 +00002398
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002399 mutex_unlock(&u->iolock);
Rainer Weikusat64874282015-12-06 21:11:38 +00002400
2401 if (err != -EAGAIN)
2402 break;
2403 } while (timeo &&
Sabrina Dubrocab50b0582019-11-25 14:48:57 +01002404 !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
2405 &err, &timeo, last));
Rainer Weikusat64874282015-12-06 21:11:38 +00002406
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002407 if (!skb) { /* implies iolock unlocked */
Florian Zumbiehl0a112252007-11-29 23:19:23 +11002408 unix_state_lock(sk);
2409 /* Signal EOF on disconnected non-blocking SEQPACKET socket. */
2410 if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN &&
2411 (sk->sk_shutdown & RCV_SHUTDOWN))
2412 err = 0;
2413 unix_state_unlock(sk);
Rainer Weikusat64874282015-12-06 21:11:38 +00002414 goto out;
Florian Zumbiehl0a112252007-11-29 23:19:23 +11002415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Rainer Weikusat77b75f42015-11-26 19:23:15 +00002417 if (wq_has_sleeper(&u->peer_wait))
2418 wake_up_interruptible_sync_poll(&u->peer_wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002419 EPOLLOUT | EPOLLWRNORM |
2420 EPOLLWRBAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (msg->msg_name)
2423 unix_copy_addr(msg, skb->sk);
2424
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002425 if (size > skb->len - skip)
2426 size = skb->len - skip;
2427 else if (size < skb->len - skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 msg->msg_flags |= MSG_TRUNC;
2429
David S. Miller51f3d022014-11-05 16:46:40 -05002430 err = skb_copy_datagram_msg(skb, skip, msg, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (err)
2432 goto out_free;
2433
Alban Crequy3f661162010-10-04 08:48:28 +00002434 if (sock_flag(sk, SOCK_RCVTSTAMP))
2435 __sock_recv_timestamp(msg, sk, skb);
2436
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002437 memset(&scm, 0, sizeof(scm));
2438
2439 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
2440 unix_set_secdata(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002442 if (!(flags & MSG_PEEK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (UNIXCB(skb).fp)
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002444 unix_detach_fds(&scm, skb);
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002445
2446 sk_peek_offset_bwd(sk, skb->len);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002447 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* It is questionable: on PEEK we could:
2449 - do not return fds - good, but too simple 8)
2450 - return fds, and do not return them on read (old strategy,
2451 apparently wrong)
2452 - clone fds (I chose it for now, it is the most universal
2453 solution)
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002454
2455 POSIX 1003.1g does not actually define this clearly
2456 at all. POSIX 1003.1g doesn't define a lot of things
2457 clearly however!
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
Pavel Emelyanovf55bb7f2012-02-21 07:31:51 +00002460
2461 sk_peek_offset_fwd(sk, size);
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (UNIXCB(skb).fp)
Miklos Szeredicbcf0112021-07-28 14:47:20 +02002464 unix_peek_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Eric Dumazet9f6f9af2012-02-21 23:24:55 +00002466 err = (flags & MSG_TRUNC) ? skb->len - skip : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002468 scm_recv(sock, msg, &scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470out_free:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002471 skb_free_datagram(sk, skb);
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002472 mutex_unlock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473out:
2474 return err;
2475}
2476
Cong Wang9825d862021-07-04 12:02:48 -07002477static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2478 int flags)
2479{
2480 struct sock *sk = sock->sk;
2481
2482#ifdef CONFIG_BPF_SYSCALL
Jiang Wang94531cf2021-08-16 19:03:21 +00002483 const struct proto *prot = READ_ONCE(sk->sk_prot);
2484
2485 if (prot != &unix_dgram_proto)
2486 return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
Cong Wang9825d862021-07-04 12:02:48 -07002487 flags & ~MSG_DONTWAIT, NULL);
2488#endif
2489 return __unix_dgram_recvmsg(sk, msg, size, flags);
2490}
2491
Cong Wang29df44f2021-07-04 12:02:44 -07002492static int unix_read_sock(struct sock *sk, read_descriptor_t *desc,
2493 sk_read_actor_t recv_actor)
2494{
2495 int copied = 0;
2496
2497 while (1) {
2498 struct unix_sock *u = unix_sk(sk);
2499 struct sk_buff *skb;
2500 int used, err;
2501
2502 mutex_lock(&u->iolock);
2503 skb = skb_recv_datagram(sk, 0, 1, &err);
2504 mutex_unlock(&u->iolock);
2505 if (!skb)
2506 return err;
2507
2508 used = recv_actor(desc, skb, 0, skb->len);
2509 if (used <= 0) {
2510 if (!copied)
2511 copied = used;
2512 kfree_skb(skb);
2513 break;
2514 } else if (used <= skb->len) {
2515 copied += used;
2516 }
2517
2518 kfree_skb(skb);
2519 if (!desc->count)
2520 break;
2521 }
2522
2523 return copied;
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526/*
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002527 * Sleep until more data has arrived. But check for races..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 */
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002529static long unix_stream_data_wait(struct sock *sk, long timeo,
WANG Cong06a77b02016-11-17 15:55:26 -08002530 struct sk_buff *last, unsigned int last_len,
2531 bool freezable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002533 struct sk_buff *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 DEFINE_WAIT(wait);
2535
David S. Miller1c92b4e2007-05-31 13:24:26 -07002536 unix_state_lock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 for (;;) {
Eric Dumazetaa395142010-04-20 13:03:51 +00002539 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002541 tail = skb_peek_tail(&sk->sk_receive_queue);
2542 if (tail != last ||
2543 (tail && tail->len != last_len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 sk->sk_err ||
2545 (sk->sk_shutdown & RCV_SHUTDOWN) ||
2546 signal_pending(current) ||
2547 !timeo)
2548 break;
2549
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002550 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002551 unix_state_unlock(sk);
WANG Cong06a77b02016-11-17 15:55:26 -08002552 if (freezable)
2553 timeo = freezable_schedule_timeout(timeo);
2554 else
2555 timeo = schedule_timeout(timeo);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002556 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002557
2558 if (sock_flag(sk, SOCK_DEAD))
2559 break;
2560
Eric Dumazet9cd3e072015-11-29 20:03:10 -08002561 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
2563
Eric Dumazetaa395142010-04-20 13:03:51 +00002564 finish_wait(sk_sleep(sk), &wait);
David S. Miller1c92b4e2007-05-31 13:24:26 -07002565 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return timeo;
2567}
2568
Eric Dumazete370a722013-08-08 14:37:32 -07002569static unsigned int unix_skb_len(const struct sk_buff *skb)
2570{
2571 return skb->len - UNIXCB(skb).consumed;
2572}
2573
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002574struct unix_stream_read_state {
2575 int (*recv_actor)(struct sk_buff *, int, int,
2576 struct unix_stream_read_state *);
2577 struct socket *socket;
2578 struct msghdr *msg;
2579 struct pipe_inode_info *pipe;
2580 size_t size;
2581 int flags;
2582 unsigned int splice_flags;
2583};
2584
Rao Shoaib314001f2021-08-01 00:57:07 -07002585#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
2586static int unix_stream_recv_urg(struct unix_stream_read_state *state)
2587{
2588 struct socket *sock = state->socket;
2589 struct sock *sk = sock->sk;
2590 struct unix_sock *u = unix_sk(sk);
2591 int chunk = 1;
Rao Shoaib876c14a2021-08-11 15:06:52 -07002592 struct sk_buff *oob_skb;
Rao Shoaib314001f2021-08-01 00:57:07 -07002593
Rao Shoaib876c14a2021-08-11 15:06:52 -07002594 mutex_lock(&u->iolock);
2595 unix_state_lock(sk);
2596
2597 if (sock_flag(sk, SOCK_URGINLINE) || !u->oob_skb) {
2598 unix_state_unlock(sk);
2599 mutex_unlock(&u->iolock);
Rao Shoaib314001f2021-08-01 00:57:07 -07002600 return -EINVAL;
Rao Shoaib876c14a2021-08-11 15:06:52 -07002601 }
Rao Shoaib314001f2021-08-01 00:57:07 -07002602
Rao Shoaib876c14a2021-08-11 15:06:52 -07002603 oob_skb = u->oob_skb;
2604
2605 if (!(state->flags & MSG_PEEK)) {
2606 u->oob_skb = NULL;
2607 }
2608
2609 unix_state_unlock(sk);
2610
2611 chunk = state->recv_actor(oob_skb, 0, chunk, state);
2612
2613 if (!(state->flags & MSG_PEEK)) {
2614 UNIXCB(oob_skb).consumed += 1;
2615 kfree_skb(oob_skb);
2616 }
2617
2618 mutex_unlock(&u->iolock);
2619
Rao Shoaib314001f2021-08-01 00:57:07 -07002620 if (chunk < 0)
2621 return -EFAULT;
2622
Rao Shoaib314001f2021-08-01 00:57:07 -07002623 state->msg->msg_flags |= MSG_OOB;
2624 return 1;
2625}
2626
2627static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
2628 int flags, int copied)
2629{
2630 struct unix_sock *u = unix_sk(sk);
2631
2632 if (!unix_skb_len(skb) && !(flags & MSG_PEEK)) {
2633 skb_unlink(skb, &sk->sk_receive_queue);
2634 consume_skb(skb);
2635 skb = NULL;
2636 } else {
2637 if (skb == u->oob_skb) {
2638 if (copied) {
2639 skb = NULL;
2640 } else if (sock_flag(sk, SOCK_URGINLINE)) {
2641 if (!(flags & MSG_PEEK)) {
2642 u->oob_skb = NULL;
2643 consume_skb(skb);
2644 }
2645 } else if (!(flags & MSG_PEEK)) {
2646 skb_unlink(skb, &sk->sk_receive_queue);
2647 consume_skb(skb);
2648 skb = skb_peek(&sk->sk_receive_queue);
2649 }
2650 }
2651 }
2652 return skb;
2653}
2654#endif
2655
Jiang Wang77462de2021-08-16 19:03:20 +00002656static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc,
2657 sk_read_actor_t recv_actor)
2658{
2659 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
2660 return -ENOTCONN;
2661
2662 return unix_read_sock(sk, desc, recv_actor);
2663}
2664
WANG Cong06a77b02016-11-17 15:55:26 -08002665static int unix_stream_read_generic(struct unix_stream_read_state *state,
2666 bool freezable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002668 struct scm_cookie scm;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002669 struct socket *sock = state->socket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 struct sock *sk = sock->sk;
2671 struct unix_sock *u = unix_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int copied = 0;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002673 int flags = state->flags;
Eric Dumazetde144392014-03-25 18:42:27 -07002674 int noblock = flags & MSG_DONTWAIT;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002675 bool check_creds = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 int target;
2677 int err = 0;
2678 long timeo;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002679 int skip;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002680 size_t size = state->size;
2681 unsigned int last_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002683 if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
2684 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 goto out;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002688 if (unlikely(flags & MSG_OOB)) {
2689 err = -EOPNOTSUPP;
Rao Shoaib314001f2021-08-01 00:57:07 -07002690#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
Rao Shoaib314001f2021-08-01 00:57:07 -07002691 err = unix_stream_recv_urg(state);
Rao Shoaib314001f2021-08-01 00:57:07 -07002692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 goto out;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002696 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
Eric Dumazetde144392014-03-25 18:42:27 -07002697 timeo = sock_rcvtimeo(sk, noblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002699 memset(&scm, 0, sizeof(scm));
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /* Lock the socket to prevent queue disordering
2702 * while sleeps in memcpy_tomsg
2703 */
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002704 mutex_lock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matthew Dawsona0917e02017-08-18 15:04:54 -04002706 skip = max(sk_peek_offset(sk, flags), 0);
Andrey Vagine9193d62015-10-02 00:05:36 +03002707
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002708 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 int chunk;
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002710 bool drop_skb;
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002711 struct sk_buff *skb, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Rainer Weikusat18eceb82016-02-18 12:39:46 +00002713redo:
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002714 unix_state_lock(sk);
Mark Salyzynb48732e2015-05-26 08:22:19 -07002715 if (sock_flag(sk, SOCK_DEAD)) {
2716 err = -ECONNRESET;
2717 goto unlock;
2718 }
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002719 last = skb = skb_peek(&sk->sk_receive_queue);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002720 last_len = last ? last->len : 0;
Rao Shoaib314001f2021-08-01 00:57:07 -07002721
2722#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
2723 if (skb) {
2724 skb = manage_oob(skb, sk, flags, copied);
2725 if (!skb) {
2726 unix_state_unlock(sk);
2727 if (copied)
2728 break;
2729 goto redo;
2730 }
2731 }
2732#endif
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002733again:
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002734 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 if (copied >= target)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002736 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /*
2739 * POSIX 1003.1g mandates this order.
2740 */
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09002741
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002742 err = sock_error(sk);
2743 if (err)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002744 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (sk->sk_shutdown & RCV_SHUTDOWN)
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002746 goto unlock;
2747
2748 unix_state_unlock(sk);
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002749 if (!timeo) {
2750 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 break;
Rainer Weikusat1b92ee32016-02-08 18:47:19 +00002752 }
2753
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002754 mutex_unlock(&u->iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002756 timeo = unix_stream_data_wait(sk, timeo, last,
WANG Cong06a77b02016-11-17 15:55:26 -08002757 last_len, freezable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Rainer Weikusat3822b5c2015-12-16 20:09:25 +00002759 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 err = sock_intr_errno(timeo);
Eric Dumazetfa0dc042016-01-24 13:53:50 -08002761 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 goto out;
2763 }
Rainer Weikusatb3ca9b02011-02-28 04:50:55 +00002764
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002765 mutex_lock(&u->iolock);
Rainer Weikusat18eceb82016-02-18 12:39:46 +00002766 goto redo;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002767unlock:
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002768 unix_state_unlock(sk);
2769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002771
Eric Dumazete370a722013-08-08 14:37:32 -07002772 while (skip >= unix_skb_len(skb)) {
2773 skip -= unix_skb_len(skb);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002774 last = skb;
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002775 last_len = skb->len;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002776 skb = skb_peek_next(skb, &sk->sk_receive_queue);
Benjamin Poirier79f632c2013-04-29 11:42:14 +00002777 if (!skb)
2778 goto again;
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002779 }
2780
Miklos Szeredi3c0d2f32007-06-05 13:10:29 -07002781 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 if (check_creds) {
2784 /* Never glue messages from different writers */
Hannes Frederic Sowa9490f882015-11-26 12:08:18 +01002785 if (!unix_skb_scm_eq(skb, &scm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 break;
Eric W. Biederman0e82e7f6d2013-04-03 16:14:47 +00002787 } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /* Copy credentials */
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002789 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
Stephen Smalley37a9a8d2015-06-10 08:44:59 -04002790 unix_set_secdata(&scm, skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002791 check_creds = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
2794 /* Copy address just once */
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002795 if (state->msg && state->msg->msg_name) {
2796 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
2797 state->msg->msg_name);
2798 unix_copy_addr(state->msg, skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 sunaddr = NULL;
2800 }
2801
Eric Dumazete370a722013-08-08 14:37:32 -07002802 chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002803 skb_get(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002804 chunk = state->recv_actor(skb, skip, chunk, state);
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002805 drop_skb = !unix_skb_len(skb);
2806 /* skb is only safe to use if !drop_skb */
2807 consume_skb(skb);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002808 if (chunk < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (copied == 0)
2810 copied = -EFAULT;
2811 break;
2812 }
2813 copied += chunk;
2814 size -= chunk;
2815
Hannes Frederic Sowa73ed5d22015-11-10 16:23:15 +01002816 if (drop_skb) {
2817 /* the skb was touched by a concurrent reader;
2818 * we should not expect anything from this skb
2819 * anymore and assume it invalid - we can be
2820 * sure it was dropped from the socket queue
2821 *
2822 * let's report a short read
2823 */
2824 err = 0;
2825 break;
2826 }
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /* Mark read part of skb as used */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002829 if (!(flags & MSG_PEEK)) {
Eric Dumazete370a722013-08-08 14:37:32 -07002830 UNIXCB(skb).consumed += chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002832 sk_peek_offset_bwd(sk, chunk);
2833
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002834 if (UNIXCB(skb).fp) {
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002835 scm_stat_del(sk, skb);
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002836 unix_detach_fds(&scm, skb);
Kirill Tkhai3c32da12019-12-09 13:03:46 +03002837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Eric Dumazete370a722013-08-08 14:37:32 -07002839 if (unix_skb_len(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Eric Dumazet6f01fd62012-01-28 16:11:03 +00002842 skb_unlink(skb, &sk->sk_receive_queue);
Neil Horman70d4bf62010-07-20 06:45:56 +00002843 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002845 if (scm.fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 break;
Eric Dumazet6eba6a32008-11-16 22:58:44 -08002847 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 /* It is questionable, see note in unix_dgram_recvmsg.
2849 */
2850 if (UNIXCB(skb).fp)
Miklos Szeredicbcf0112021-07-28 14:47:20 +02002851 unix_peek_fds(&scm, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Andrey Vagine9193d62015-10-02 00:05:36 +03002853 sk_peek_offset_fwd(sk, chunk);
Pavel Emelyanovfc0d7532012-02-21 07:32:06 +00002854
Aaron Conole9f389e32015-09-26 18:50:43 -04002855 if (UNIXCB(skb).fp)
2856 break;
2857
Andrey Vagine9193d62015-10-02 00:05:36 +03002858 skip = 0;
Aaron Conole9f389e32015-09-26 18:50:43 -04002859 last = skb;
2860 last_len = skb->len;
2861 unix_state_lock(sk);
2862 skb = skb_peek_next(skb, &sk->sk_receive_queue);
2863 if (skb)
2864 goto again;
2865 unix_state_unlock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 break;
2867 }
2868 } while (size);
2869
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -07002870 mutex_unlock(&u->iolock);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002871 if (state->msg)
2872 scm_recv(sock, state->msg, &scm, flags);
2873 else
2874 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875out:
2876 return copied ? : err;
2877}
2878
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002879static int unix_stream_read_actor(struct sk_buff *skb,
2880 int skip, int chunk,
2881 struct unix_stream_read_state *state)
2882{
2883 int ret;
2884
2885 ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
2886 state->msg, chunk);
2887 return ret ?: chunk;
2888}
2889
Jiang Wang94531cf2021-08-16 19:03:21 +00002890int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg,
2891 size_t size, int flags)
2892{
2893 struct unix_stream_read_state state = {
2894 .recv_actor = unix_stream_read_actor,
2895 .socket = sk->sk_socket,
2896 .msg = msg,
2897 .size = size,
2898 .flags = flags
2899 };
2900
2901 return unix_stream_read_generic(&state, true);
2902}
2903
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002904static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
2905 size_t size, int flags)
2906{
2907 struct unix_stream_read_state state = {
2908 .recv_actor = unix_stream_read_actor,
2909 .socket = sock,
2910 .msg = msg,
2911 .size = size,
2912 .flags = flags
2913 };
2914
Jiang Wang94531cf2021-08-16 19:03:21 +00002915#ifdef CONFIG_BPF_SYSCALL
2916 struct sock *sk = sock->sk;
2917 const struct proto *prot = READ_ONCE(sk->sk_prot);
2918
2919 if (prot != &unix_stream_proto)
2920 return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
2921 flags & ~MSG_DONTWAIT, NULL);
2922#endif
WANG Cong06a77b02016-11-17 15:55:26 -08002923 return unix_stream_read_generic(&state, true);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002924}
2925
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002926static int unix_stream_splice_actor(struct sk_buff *skb,
2927 int skip, int chunk,
2928 struct unix_stream_read_state *state)
2929{
2930 return skb_splice_bits(skb, state->socket->sk,
2931 UNIXCB(skb).consumed + skip,
Al Viro25869262016-09-17 21:02:10 -04002932 state->pipe, chunk, state->splice_flags);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002933}
2934
2935static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
2936 struct pipe_inode_info *pipe,
2937 size_t size, unsigned int flags)
2938{
2939 struct unix_stream_read_state state = {
2940 .recv_actor = unix_stream_splice_actor,
2941 .socket = sock,
2942 .pipe = pipe,
2943 .size = size,
2944 .splice_flags = flags,
2945 };
2946
2947 if (unlikely(*ppos))
2948 return -ESPIPE;
2949
2950 if (sock->file->f_flags & O_NONBLOCK ||
2951 flags & SPLICE_F_NONBLOCK)
2952 state.flags = MSG_DONTWAIT;
2953
WANG Cong06a77b02016-11-17 15:55:26 -08002954 return unix_stream_read_generic(&state, false);
Hannes Frederic Sowa2b514572015-05-21 17:00:01 +02002955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957static int unix_shutdown(struct socket *sock, int mode)
2958{
2959 struct sock *sk = sock->sk;
2960 struct sock *other;
2961
Xi Wangfc61b922012-08-26 16:47:13 +00002962 if (mode < SHUT_RD || mode > SHUT_RDWR)
2963 return -EINVAL;
2964 /* This maps:
2965 * SHUT_RD (0) -> RCV_SHUTDOWN (1)
2966 * SHUT_WR (1) -> SEND_SHUTDOWN (2)
2967 * SHUT_RDWR (2) -> SHUTDOWN_MASK (3)
2968 */
2969 ++mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Alban Crequy7180a032011-01-19 04:56:36 +00002971 unix_state_lock(sk);
2972 sk->sk_shutdown |= mode;
2973 other = unix_peer(sk);
2974 if (other)
2975 sock_hold(other);
2976 unix_state_unlock(sk);
2977 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Alban Crequy7180a032011-01-19 04:56:36 +00002979 if (other &&
2980 (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Alban Crequy7180a032011-01-19 04:56:36 +00002982 int peer_mode = 0;
Jiang Wang94531cf2021-08-16 19:03:21 +00002983 const struct proto *prot = READ_ONCE(other->sk_prot);
Alban Crequy7180a032011-01-19 04:56:36 +00002984
Jiang Wangd3599022021-08-21 18:07:36 +00002985 if (prot->unhash)
2986 prot->unhash(other);
Alban Crequy7180a032011-01-19 04:56:36 +00002987 if (mode&RCV_SHUTDOWN)
2988 peer_mode |= SEND_SHUTDOWN;
2989 if (mode&SEND_SHUTDOWN)
2990 peer_mode |= RCV_SHUTDOWN;
2991 unix_state_lock(other);
2992 other->sk_shutdown |= peer_mode;
2993 unix_state_unlock(other);
2994 other->sk_state_change(other);
Jiang Wangd0c6416b2021-10-04 23:25:28 +00002995 if (peer_mode == SHUTDOWN_MASK)
Alban Crequy7180a032011-01-19 04:56:36 +00002996 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP);
Jiang Wangd0c6416b2021-10-04 23:25:28 +00002997 else if (peer_mode & RCV_SHUTDOWN)
Alban Crequy7180a032011-01-19 04:56:36 +00002998 sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
Alban Crequy7180a032011-01-19 04:56:36 +00003000 if (other)
3001 sock_put(other);
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return 0;
3004}
3005
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003006long unix_inq_len(struct sock *sk)
3007{
3008 struct sk_buff *skb;
3009 long amount = 0;
3010
3011 if (sk->sk_state == TCP_LISTEN)
3012 return -EINVAL;
3013
3014 spin_lock(&sk->sk_receive_queue.lock);
3015 if (sk->sk_type == SOCK_STREAM ||
3016 sk->sk_type == SOCK_SEQPACKET) {
3017 skb_queue_walk(&sk->sk_receive_queue, skb)
Eric Dumazete370a722013-08-08 14:37:32 -07003018 amount += unix_skb_len(skb);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003019 } else {
3020 skb = skb_peek(&sk->sk_receive_queue);
3021 if (skb)
3022 amount = skb->len;
3023 }
3024 spin_unlock(&sk->sk_receive_queue.lock);
3025
3026 return amount;
3027}
3028EXPORT_SYMBOL_GPL(unix_inq_len);
3029
3030long unix_outq_len(struct sock *sk)
3031{
3032 return sk_wmem_alloc_get(sk);
3033}
3034EXPORT_SYMBOL_GPL(unix_outq_len);
3035
Andrey Vaginba94f302017-02-01 11:00:45 -08003036static int unix_open_file(struct sock *sk)
3037{
3038 struct path path;
3039 struct file *f;
3040 int fd;
3041
3042 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
3043 return -EPERM;
3044
Al Viroae3b5642019-02-15 20:09:35 +00003045 if (!smp_load_acquire(&unix_sk(sk)->addr))
Andrey Vaginba94f302017-02-01 11:00:45 -08003046 return -ENOENT;
Al Viroae3b5642019-02-15 20:09:35 +00003047
3048 path = unix_sk(sk)->path;
3049 if (!path.dentry)
3050 return -ENOENT;
Andrey Vaginba94f302017-02-01 11:00:45 -08003051
3052 path_get(&path);
Andrey Vaginba94f302017-02-01 11:00:45 -08003053
3054 fd = get_unused_fd_flags(O_CLOEXEC);
3055 if (fd < 0)
3056 goto out;
3057
3058 f = dentry_open(&path, O_PATH, current_cred());
3059 if (IS_ERR(f)) {
3060 put_unused_fd(fd);
3061 fd = PTR_ERR(f);
3062 goto out;
3063 }
3064
3065 fd_install(fd, f);
3066out:
3067 path_put(&path);
3068
3069 return fd;
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3073{
3074 struct sock *sk = sock->sk;
Jianjun Konge27dfce2008-11-01 21:38:31 -07003075 long amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 int err;
3077
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003078 switch (cmd) {
3079 case SIOCOUTQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003080 amount = unix_outq_len(sk);
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003081 err = put_user(amount, (int __user *)arg);
3082 break;
3083 case SIOCINQ:
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003084 amount = unix_inq_len(sk);
3085 if (amount < 0)
3086 err = amount;
3087 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 err = put_user(amount, (int __user *)arg);
Pavel Emelyanov885ee742011-12-30 00:54:11 +00003089 break;
Andrey Vaginba94f302017-02-01 11:00:45 -08003090 case SIOCUNIXFILE:
3091 err = unix_open_file(sk);
3092 break;
Rao Shoaib314001f2021-08-01 00:57:07 -07003093#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
3094 case SIOCATMARK:
3095 {
3096 struct sk_buff *skb;
3097 struct unix_sock *u = unix_sk(sk);
3098 int answ = 0;
3099
3100 skb = skb_peek(&sk->sk_receive_queue);
3101 if (skb && skb == u->oob_skb)
3102 answ = 1;
3103 err = put_user(answ, (int __user *)arg);
3104 }
3105 break;
3106#endif
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003107 default:
3108 err = -ENOIOCTLCMD;
3109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111 return err;
3112}
3113
Arnd Bergmann5f6beb92019-06-03 22:03:44 +02003114#ifdef CONFIG_COMPAT
3115static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3116{
3117 return unix_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));
3118}
3119#endif
3120
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003121static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 struct sock *sk = sock->sk;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003124 __poll_t mask;
3125
Karsten Graul89ab0662018-10-23 13:40:39 +02003126 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003127 mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 /* exceptional events? */
3130 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003131 mask |= EPOLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003133 mask |= EPOLLHUP;
Davide Libenzif348d702006-03-25 03:07:39 -08003134 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003135 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 /* readable? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003138 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003139 mask |= EPOLLIN | EPOLLRDNORM;
Cong Wangaf493382021-10-08 13:33:05 -07003140 if (sk_is_readable(sk))
3141 mask |= EPOLLIN | EPOLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 /* Connection-based need to check for termination and startup */
Eric Dumazet6eba6a32008-11-16 22:58:44 -08003144 if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
3145 sk->sk_state == TCP_CLOSE)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003146 mask |= EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 /*
3149 * we set writable also when the other side has shut down the
3150 * connection. This prevents stuck sockets.
3151 */
3152 if (unix_writable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003153 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 return mask;
3156}
3157
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003158static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
3159 poll_table *wait)
Rainer Weikusat3c734192008-06-17 22:28:05 -07003160{
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003161 struct sock *sk = sock->sk, *other;
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003162 unsigned int writable;
3163 __poll_t mask;
3164
Karsten Graul89ab0662018-10-23 13:40:39 +02003165 sock_poll_wait(file, sock, wait);
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003166 mask = 0;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003167
3168 /* exceptional events? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003169 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003170 mask |= EPOLLERR |
3171 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
Keller, Jacob E7d4c04f2013-03-28 11:19:25 +00003172
Rainer Weikusat3c734192008-06-17 22:28:05 -07003173 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003174 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003175 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003176 mask |= EPOLLHUP;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003177
3178 /* readable? */
Eric Dumazet3ef7cf52019-10-23 22:44:50 -07003179 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003180 mask |= EPOLLIN | EPOLLRDNORM;
Cong Wangaf493382021-10-08 13:33:05 -07003181 if (sk_is_readable(sk))
3182 mask |= EPOLLIN | EPOLLRDNORM;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003183
3184 /* Connection-based need to check for termination and startup */
3185 if (sk->sk_type == SOCK_SEQPACKET) {
3186 if (sk->sk_state == TCP_CLOSE)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003187 mask |= EPOLLHUP;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003188 /* connection hasn't started yet? */
3189 if (sk->sk_state == TCP_SYN_SENT)
3190 return mask;
3191 }
3192
Eric Dumazet973a34a2010-10-31 05:38:25 +00003193 /* No write status requested, avoid expensive OUT tests. */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003194 if (!(poll_requested_events(wait) & (EPOLLWRBAND|EPOLLWRNORM|EPOLLOUT)))
Eric Dumazet973a34a2010-10-31 05:38:25 +00003195 return mask;
3196
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003197 writable = unix_writable(sk);
Rainer Weikusat7d267272015-11-20 22:07:23 +00003198 if (writable) {
3199 unix_state_lock(sk);
3200
3201 other = unix_peer(sk);
3202 if (other && unix_peer(other) != sk &&
Eric Dumazet04f08eb2021-09-08 17:00:29 -07003203 unix_recvq_full_lockless(other) &&
Rainer Weikusat7d267272015-11-20 22:07:23 +00003204 unix_dgram_peer_wake_me(sk, other))
3205 writable = 0;
3206
3207 unix_state_unlock(sk);
Rainer Weikusatec0d2152008-06-27 19:34:18 -07003208 }
3209
3210 if (writable)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08003211 mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
Rainer Weikusat3c734192008-06-17 22:28:05 -07003212 else
Eric Dumazet9cd3e072015-11-29 20:03:10 -08003213 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Rainer Weikusat3c734192008-06-17 22:28:05 -07003214
Rainer Weikusat3c734192008-06-17 22:28:05 -07003215 return mask;
3216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218#ifdef CONFIG_PROC_FS
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08003219
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003220#define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
3221
3222#define get_bucket(x) ((x) >> BUCKET_SPACE)
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003223#define get_offset(x) ((x) & ((1UL << BUCKET_SPACE) - 1))
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003224#define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
Pavel Emelyanova53eb3f2007-11-23 20:30:01 +08003225
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003226static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003228 unsigned long offset = get_offset(*pos);
3229 unsigned long bucket = get_bucket(*pos);
3230 struct sock *sk;
3231 unsigned long count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003233 for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) {
3234 if (sock_net(sk) != seq_file_net(seq))
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003235 continue;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003236 if (++count == offset)
3237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003239
3240 return sk;
3241}
3242
3243static struct sock *unix_next_socket(struct seq_file *seq,
3244 struct sock *sk,
3245 loff_t *pos)
3246{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003247 unsigned long bucket = get_bucket(*pos);
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003248
3249 while (sk > (struct sock *)SEQ_START_TOKEN) {
3250 sk = sk_next(sk);
3251 if (!sk)
3252 goto next_bucket;
3253 if (sock_net(sk) == seq_file_net(seq))
3254 return sk;
3255 }
3256
3257 do {
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003258 spin_lock(&unix_table_locks[bucket]);
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003259 sk = unix_from_bucket(seq, pos);
3260 if (sk)
3261 return sk;
3262
3263next_bucket:
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003264 spin_unlock(&unix_table_locks[bucket++]);
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003265 *pos = set_bucket_offset(bucket, 1);
3266 } while (bucket < ARRAY_SIZE(unix_socket_table));
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 return NULL;
3269}
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271static void *unix_seq_start(struct seq_file *seq, loff_t *pos)
3272{
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003273 if (!*pos)
3274 return SEQ_START_TOKEN;
3275
3276 if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table))
3277 return NULL;
3278
3279 return unix_next_socket(seq, NULL, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
3282static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3283{
3284 ++*pos;
Eric Dumazet7123aaa2012-06-08 05:03:21 +00003285 return unix_next_socket(seq, v, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
3288static void unix_seq_stop(struct seq_file *seq, void *v)
3289{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003290 struct sock *sk = v;
3291
3292 if (sk)
3293 spin_unlock(&unix_table_locks[sk->sk_hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
3296static int unix_seq_show(struct seq_file *seq, void *v)
3297{
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09003298
Joe Perchesb9f31242008-04-12 19:04:38 -07003299 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 seq_puts(seq, "Num RefCount Protocol Flags Type St "
3301 "Inode Path\n");
3302 else {
3303 struct sock *s = v;
3304 struct unix_sock *u = unix_sk(s);
David S. Miller1c92b4e2007-05-31 13:24:26 -07003305 unix_state_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003307 seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 s,
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003309 refcount_read(&s->sk_refcnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 0,
3311 s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
3312 s->sk_type,
3313 s->sk_socket ?
3314 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
3315 (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
3316 sock_i_ino(s));
3317
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003318 if (u->addr) { // under unix_table_locks here
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 int i, len;
3320 seq_putc(seq, ' ');
3321
3322 i = 0;
Kuniyuki Iwashima755662c2021-11-24 11:14:19 +09003323 len = u->addr->len -
3324 offsetof(struct sockaddr_un, sun_path);
Kuniyuki Iwashima5ce7ab42021-11-24 11:14:27 +09003325 if (u->addr->name->sun_path[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 len--;
Kuniyuki Iwashima5ce7ab42021-11-24 11:14:27 +09003327 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 seq_putc(seq, '@');
3329 i++;
3330 }
3331 for ( ; i < len; i++)
Isaac Boukrise7947ea2016-11-01 02:41:35 +02003332 seq_putc(seq, u->addr->name->sun_path[i] ?:
3333 '@');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
David S. Miller1c92b4e2007-05-31 13:24:26 -07003335 unix_state_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 seq_putc(seq, '\n');
3337 }
3338
3339 return 0;
3340}
3341
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003342static const struct seq_operations unix_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 .start = unix_seq_start,
3344 .next = unix_seq_next,
3345 .stop = unix_seq_stop,
3346 .show = unix_seq_show,
3347};
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003348
3349#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL)
3350struct bpf_iter__unix {
3351 __bpf_md_ptr(struct bpf_iter_meta *, meta);
3352 __bpf_md_ptr(struct unix_sock *, unix_sk);
3353 uid_t uid __aligned(8);
3354};
3355
3356static int unix_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta,
3357 struct unix_sock *unix_sk, uid_t uid)
3358{
3359 struct bpf_iter__unix ctx;
3360
3361 meta->seq_num--; /* skip SEQ_START_TOKEN */
3362 ctx.meta = meta;
3363 ctx.unix_sk = unix_sk;
3364 ctx.uid = uid;
3365 return bpf_iter_run_prog(prog, &ctx);
3366}
3367
3368static int bpf_iter_unix_seq_show(struct seq_file *seq, void *v)
3369{
3370 struct bpf_iter_meta meta;
3371 struct bpf_prog *prog;
3372 struct sock *sk = v;
3373 uid_t uid;
3374
3375 if (v == SEQ_START_TOKEN)
3376 return 0;
3377
3378 uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
3379 meta.seq = seq;
3380 prog = bpf_iter_get_info(&meta, false);
3381 return unix_prog_seq_show(prog, &meta, v, uid);
3382}
3383
3384static void bpf_iter_unix_seq_stop(struct seq_file *seq, void *v)
3385{
3386 struct bpf_iter_meta meta;
3387 struct bpf_prog *prog;
3388
3389 if (!v) {
3390 meta.seq = seq;
3391 prog = bpf_iter_get_info(&meta, true);
3392 if (prog)
3393 (void)unix_prog_seq_show(prog, &meta, v, 0);
3394 }
3395
3396 unix_seq_stop(seq, v);
3397}
3398
3399static const struct seq_operations bpf_iter_unix_seq_ops = {
3400 .start = unix_seq_start,
3401 .next = unix_seq_next,
3402 .stop = bpf_iter_unix_seq_stop,
3403 .show = bpf_iter_unix_seq_show,
3404};
3405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406#endif
3407
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003408static const struct net_proto_family unix_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 .family = PF_UNIX,
3410 .create = unix_create,
3411 .owner = THIS_MODULE,
3412};
3413
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003414
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003415static int __net_init unix_net_init(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003416{
3417 int error = -ENOMEM;
3418
Denis V. Luneva0a53c82007-12-11 04:19:17 -08003419 net->unx.sysctl_max_dgram_qlen = 10;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003420 if (unix_sysctl_register(net))
3421 goto out;
Pavel Emelyanovd392e492007-12-01 23:44:15 +11003422
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003423#ifdef CONFIG_PROC_FS
Christoph Hellwigc3506372018-04-10 19:42:55 +02003424 if (!proc_create_net("unix", 0, net->proc_net, &unix_seq_ops,
3425 sizeof(struct seq_net_private))) {
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003426 unix_sysctl_unregister(net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003427 goto out;
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003428 }
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003429#endif
3430 error = 0;
3431out:
Jianjun Kong48dcc33e2008-11-01 21:37:27 -07003432 return error;
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003433}
3434
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003435static void __net_exit unix_net_exit(struct net *net)
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003436{
Pavel Emelyanov1597fbc2007-12-01 23:51:01 +11003437 unix_sysctl_unregister(net);
Gao fengece31ff2013-02-18 01:34:56 +00003438 remove_proc_entry("unix", net->proc_net);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003439}
3440
3441static struct pernet_operations unix_net_ops = {
3442 .init = unix_net_init,
3443 .exit = unix_net_exit,
3444};
3445
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003446#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3447DEFINE_BPF_ITER_FUNC(unix, struct bpf_iter_meta *meta,
3448 struct unix_sock *unix_sk, uid_t uid)
3449
3450static const struct bpf_iter_seq_info unix_seq_info = {
3451 .seq_ops = &bpf_iter_unix_seq_ops,
3452 .init_seq_private = bpf_iter_init_seq_net,
3453 .fini_seq_private = bpf_iter_fini_seq_net,
3454 .seq_priv_size = sizeof(struct seq_net_private),
3455};
3456
3457static struct bpf_iter_reg unix_reg_info = {
3458 .target = "unix",
3459 .ctx_arg_info_size = 1,
3460 .ctx_arg_info = {
3461 { offsetof(struct bpf_iter__unix, unix_sk),
3462 PTR_TO_BTF_ID_OR_NULL },
3463 },
3464 .seq_info = &unix_seq_info,
3465};
3466
3467static void __init bpf_iter_register(void)
3468{
3469 unix_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UNIX];
3470 if (bpf_iter_reg_target(&unix_reg_info))
3471 pr_warn("Warning: could not register bpf iterator unix\n");
3472}
3473#endif
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475static int __init af_unix_init(void)
3476{
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003477 int i, rc = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08003479 BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +09003481 for (i = 0; i < 2 * UNIX_HASH_SIZE; i++)
3482 spin_lock_init(&unix_table_locks[i]);
3483
Jiang Wang94531cf2021-08-16 19:03:21 +00003484 rc = proto_register(&unix_dgram_proto, 1);
3485 if (rc != 0) {
3486 pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
3487 goto out;
3488 }
3489
3490 rc = proto_register(&unix_stream_proto, 1);
YOSHIFUJI Hideakiac7bfa62007-02-09 23:25:23 +09003491 if (rc != 0) {
wangweidong5cc208b2013-12-06 18:03:36 +08003492 pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 goto out;
3494 }
3495
3496 sock_register(&unix_family_ops);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003497 register_pernet_subsys(&unix_net_ops);
Cong Wangc6382912021-07-04 12:02:47 -07003498 unix_bpf_build_proto();
Kuniyuki Iwashima2c860a42021-08-14 10:57:15 +09003499
3500#if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3501 bpf_iter_register();
3502#endif
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504out:
3505 return rc;
3506}
3507
3508static void __exit af_unix_exit(void)
3509{
3510 sock_unregister(PF_UNIX);
Jiang Wang94531cf2021-08-16 19:03:21 +00003511 proto_unregister(&unix_dgram_proto);
3512 proto_unregister(&unix_stream_proto);
Denis V. Lunev097e66c2007-11-19 22:29:30 -08003513 unregister_pernet_subsys(&unix_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514}
3515
David Woodhouse3d366962008-04-24 00:59:25 -07003516/* Earlier than device_initcall() so that other drivers invoking
3517 request_module() don't end up in a loop when modprobe tries
3518 to use a UNIX socket. But later than subsys_initcall() because
3519 we depend on stuff initialised there */
3520fs_initcall(af_unix_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521module_exit(af_unix_exit);
3522
3523MODULE_LICENSE("GPL");
3524MODULE_ALIAS_NETPROTO(PF_UNIX);