Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * NET4: Implementation of BSD Unix domain sockets. |
| 4 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 5 | * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * 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 | |
wangweidong | 5cc208b | 2013-12-06 18:03:36 +0800 | [diff] [blame] | 78 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #include <linux/signal.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 83 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | #include <linux/errno.h> |
| 85 | #include <linux/string.h> |
| 86 | #include <linux/stat.h> |
| 87 | #include <linux/dcache.h> |
| 88 | #include <linux/namei.h> |
| 89 | #include <linux/socket.h> |
| 90 | #include <linux/un.h> |
| 91 | #include <linux/fcntl.h> |
| 92 | #include <linux/termios.h> |
| 93 | #include <linux/sockios.h> |
| 94 | #include <linux/net.h> |
| 95 | #include <linux/in.h> |
| 96 | #include <linux/fs.h> |
| 97 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 98 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #include <linux/skbuff.h> |
| 100 | #include <linux/netdevice.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 101 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 103 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #include <net/af_unix.h> |
| 105 | #include <linux/proc_fs.h> |
| 106 | #include <linux/seq_file.h> |
| 107 | #include <net/scm.h> |
| 108 | #include <linux/init.h> |
| 109 | #include <linux/poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #include <linux/rtnetlink.h> |
| 111 | #include <linux/mount.h> |
| 112 | #include <net/checksum.h> |
| 113 | #include <linux/security.h> |
Colin Cross | 2b15af6 | 2013-05-06 23:50:21 +0000 | [diff] [blame] | 114 | #include <linux/freezer.h> |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 115 | #include <linux/file.h> |
Kuniyuki Iwashima | 2c860a4 | 2021-08-14 10:57:15 +0900 | [diff] [blame] | 116 | #include <linux/btf_ids.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Jens Axboe | f4e6587 | 2019-02-08 09:01:44 -0700 | [diff] [blame] | 118 | #include "scm.h" |
| 119 | |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 120 | struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE]; |
Pavel Emelyanov | fa7ff56 | 2011-12-15 02:44:03 +0000 | [diff] [blame] | 121 | EXPORT_SYMBOL_GPL(unix_socket_table); |
| 122 | DEFINE_SPINLOCK(unix_table_lock); |
| 123 | EXPORT_SYMBOL_GPL(unix_table_lock); |
Eric Dumazet | 518de9b | 2010-10-26 14:22:44 -0700 | [diff] [blame] | 124 | static atomic_long_t unix_nr_socks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 126 | /* SMP locking strategy: |
| 127 | * hash table is protected with spinlock unix_table_lock |
| 128 | * each socket state is protected by separate spin lock. |
| 129 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 131 | static unsigned int unix_unbound_hash(struct sock *sk) |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 132 | { |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 133 | unsigned long hash = (unsigned long)sk; |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 134 | |
| 135 | hash ^= hash >> 16; |
| 136 | hash ^= hash >> 8; |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 137 | hash ^= sk->sk_type; |
| 138 | |
| 139 | return UNIX_HASH_SIZE + (hash & (UNIX_HASH_SIZE - 1)); |
| 140 | } |
| 141 | |
| 142 | static unsigned int unix_bsd_hash(struct inode *i) |
| 143 | { |
| 144 | return i->i_ino & (UNIX_HASH_SIZE - 1); |
| 145 | } |
| 146 | |
| 147 | static unsigned int unix_abstract_hash(struct sockaddr_un *sunaddr, |
| 148 | int addr_len, int type) |
| 149 | { |
| 150 | __wsum csum = csum_partial(sunaddr, addr_len, 0); |
| 151 | unsigned int hash; |
| 152 | |
| 153 | hash = (__force unsigned int)csum_fold(csum); |
| 154 | hash ^= hash >> 8; |
| 155 | hash ^= type; |
| 156 | |
| 157 | return hash & (UNIX_HASH_SIZE - 1); |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 160 | #ifdef CONFIG_SECURITY_NETWORK |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 161 | static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 162 | { |
Stephen Smalley | 37a9a8d | 2015-06-10 08:44:59 -0400 | [diff] [blame] | 163 | UNIXCB(skb).secid = scm->secid; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
| 167 | { |
Stephen Smalley | 37a9a8d | 2015-06-10 08:44:59 -0400 | [diff] [blame] | 168 | scm->secid = UNIXCB(skb).secid; |
| 169 | } |
| 170 | |
| 171 | static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb) |
| 172 | { |
| 173 | return (scm->secid == UNIXCB(skb).secid); |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 174 | } |
| 175 | #else |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 176 | static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 177 | { } |
| 178 | |
| 179 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
| 180 | { } |
Stephen Smalley | 37a9a8d | 2015-06-10 08:44:59 -0400 | [diff] [blame] | 181 | |
| 182 | static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb) |
| 183 | { |
| 184 | return true; |
| 185 | } |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 186 | #endif /* CONFIG_SECURITY_NETWORK */ |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | #define unix_peer(sk) (unix_sk(sk)->peer) |
| 189 | |
| 190 | static inline int unix_our_peer(struct sock *sk, struct sock *osk) |
| 191 | { |
| 192 | return unix_peer(osk) == sk; |
| 193 | } |
| 194 | |
| 195 | static inline int unix_may_send(struct sock *sk, struct sock *osk) |
| 196 | { |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 197 | return unix_peer(osk) == NULL || unix_our_peer(sk, osk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Qian Cai | 86b18aa | 2020-02-04 13:40:29 -0500 | [diff] [blame] | 200 | static inline int unix_recvq_full(const struct sock *sk) |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 201 | { |
| 202 | return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; |
| 203 | } |
| 204 | |
Qian Cai | 86b18aa | 2020-02-04 13:40:29 -0500 | [diff] [blame] | 205 | static inline int unix_recvq_full_lockless(const struct sock *sk) |
| 206 | { |
| 207 | return skb_queue_len_lockless(&sk->sk_receive_queue) > |
| 208 | READ_ONCE(sk->sk_max_ack_backlog); |
| 209 | } |
| 210 | |
Pavel Emelyanov | fa7ff56 | 2011-12-15 02:44:03 +0000 | [diff] [blame] | 211 | struct sock *unix_peer_get(struct sock *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | struct sock *peer; |
| 214 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 215 | unix_state_lock(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | peer = unix_peer(s); |
| 217 | if (peer) |
| 218 | sock_hold(peer); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 219 | unix_state_unlock(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return peer; |
| 221 | } |
Pavel Emelyanov | fa7ff56 | 2011-12-15 02:44:03 +0000 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(unix_peer_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 224 | static struct unix_address *unix_create_addr(struct sockaddr_un *sunaddr, |
| 225 | int addr_len) |
| 226 | { |
| 227 | struct unix_address *addr; |
| 228 | |
| 229 | addr = kmalloc(sizeof(*addr) + addr_len, GFP_KERNEL); |
| 230 | if (!addr) |
| 231 | return NULL; |
| 232 | |
| 233 | refcount_set(&addr->refcnt, 1); |
| 234 | addr->len = addr_len; |
| 235 | memcpy(addr->name, sunaddr, addr_len); |
| 236 | |
| 237 | return addr; |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | static inline void unix_release_addr(struct unix_address *addr) |
| 241 | { |
Reshetova, Elena | 8c9814b | 2017-06-30 13:08:05 +0300 | [diff] [blame] | 242 | if (refcount_dec_and_test(&addr->refcnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | kfree(addr); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Check unix socket name: |
| 248 | * - should be not zero length. |
| 249 | * - if started by not zero, should be NULL terminated (FS object) |
| 250 | * - if started by zero, it is abstract name. |
| 251 | */ |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 252 | |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 253 | static int unix_validate_addr(struct sockaddr_un *sunaddr, int addr_len) |
| 254 | { |
| 255 | if (addr_len <= offsetof(struct sockaddr_un, sun_path) || |
| 256 | addr_len > sizeof(*sunaddr)) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | if (sunaddr->sun_family != AF_UNIX) |
| 260 | return -EINVAL; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 265 | static void unix_mkname_bsd(struct sockaddr_un *sunaddr, int addr_len) |
| 266 | { |
| 267 | /* This may look like an off by one error but it is a bit more |
| 268 | * subtle. 108 is the longest valid AF_UNIX path for a binding. |
| 269 | * sun_path[108] doesn't as such exist. However in kernel space |
| 270 | * we are guaranteed that it is a valid memory location in our |
| 271 | * kernel address buffer because syscall functions always pass |
| 272 | * a pointer of struct sockaddr_storage which has a bigger buffer |
| 273 | * than 108. |
| 274 | */ |
| 275 | ((char *)sunaddr)[addr_len] = 0; |
| 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | static void __unix_remove_socket(struct sock *sk) |
| 279 | { |
| 280 | sk_del_node_init(sk); |
| 281 | } |
| 282 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 283 | static void __unix_insert_socket(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 285 | WARN_ON(!sk_unhashed(sk)); |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 286 | sk_add_node(sk, &unix_socket_table[sk->sk_hash]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 289 | static void __unix_set_addr_hash(struct sock *sk, struct unix_address *addr, |
| 290 | unsigned int hash) |
Al Viro | 185ab88 | 2021-06-19 03:50:26 +0000 | [diff] [blame] | 291 | { |
| 292 | __unix_remove_socket(sk); |
| 293 | smp_store_release(&unix_sk(sk)->addr, addr); |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 294 | |
| 295 | sk->sk_hash = hash; |
| 296 | __unix_insert_socket(sk); |
Al Viro | 185ab88 | 2021-06-19 03:50:26 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 299 | static void unix_remove_socket(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 301 | spin_lock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | __unix_remove_socket(sk); |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 303 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 306 | static void unix_insert_unbound_socket(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 308 | spin_lock(&unix_table_lock); |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 309 | __unix_insert_socket(sk); |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 310 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 313 | static struct sock *__unix_find_socket_byname(struct net *net, |
| 314 | struct sockaddr_un *sunname, |
Al Viro | be75228 | 2021-06-19 03:50:33 +0000 | [diff] [blame] | 315 | int len, unsigned int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Al Viro | be75228 | 2021-06-19 03:50:33 +0000 | [diff] [blame] | 319 | sk_for_each(s, &unix_socket_table[hash]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct unix_sock *u = unix_sk(s); |
| 321 | |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 322 | if (!net_eq(sock_net(s), net)) |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 323 | continue; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (u->addr->len == len && |
| 326 | !memcmp(u->addr->name, sunname, len)) |
Vito Caputo | 262ce0a | 2019-10-09 20:43:47 -0700 | [diff] [blame] | 327 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
Vito Caputo | 262ce0a | 2019-10-09 20:43:47 -0700 | [diff] [blame] | 329 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 332 | static inline struct sock *unix_find_socket_byname(struct net *net, |
| 333 | struct sockaddr_un *sunname, |
Al Viro | be75228 | 2021-06-19 03:50:33 +0000 | [diff] [blame] | 334 | int len, unsigned int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
| 336 | struct sock *s; |
| 337 | |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 338 | spin_lock(&unix_table_lock); |
Al Viro | be75228 | 2021-06-19 03:50:33 +0000 | [diff] [blame] | 339 | s = __unix_find_socket_byname(net, sunname, len, hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | if (s) |
| 341 | sock_hold(s); |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 342 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return s; |
| 344 | } |
| 345 | |
Eric W. Biederman | 6616f78 | 2010-06-13 03:35:48 +0000 | [diff] [blame] | 346 | static struct sock *unix_find_socket_byinode(struct inode *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 348 | unsigned int hash = unix_bsd_hash(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 351 | spin_lock(&unix_table_lock); |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 352 | sk_for_each(s, &unix_socket_table[hash]) { |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 353 | struct dentry *dentry = unix_sk(s)->path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Miklos Szeredi | beef512 | 2016-12-16 11:02:53 +0100 | [diff] [blame] | 355 | if (dentry && d_backing_inode(dentry) == i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | sock_hold(s); |
| 357 | goto found; |
| 358 | } |
| 359 | } |
| 360 | s = NULL; |
| 361 | found: |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 362 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return s; |
| 364 | } |
| 365 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 366 | /* Support code for asymmetrically connected dgram sockets |
| 367 | * |
| 368 | * If a datagram socket is connected to a socket not itself connected |
| 369 | * to the first socket (eg, /dev/log), clients may only enqueue more |
| 370 | * messages if the present receive queue of the server socket is not |
| 371 | * "too large". This means there's a second writeability condition |
| 372 | * poll and sendmsg need to test. The dgram recv code will do a wake |
| 373 | * up on the peer_wait wait queue of a socket upon reception of a |
| 374 | * datagram which needs to be propagated to sleeping would-be writers |
| 375 | * since these might not have sent anything so far. This can't be |
| 376 | * accomplished via poll_wait because the lifetime of the server |
| 377 | * socket might be less than that of its clients if these break their |
| 378 | * association with it or if the server socket is closed while clients |
| 379 | * are still connected to it and there's no way to inform "a polling |
| 380 | * implementation" that it should let go of a certain wait queue |
| 381 | * |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 382 | * In order to propagate a wake up, a wait_queue_entry_t of the client |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 383 | * socket is enqueued on the peer_wait queue of the server socket |
| 384 | * whose wake function does a wake_up on the ordinary client socket |
| 385 | * wait queue. This connection is established whenever a write (or |
| 386 | * poll for write) hit the flow control condition and broken when the |
| 387 | * association to the server socket is dissolved or after a wake up |
| 388 | * was relayed. |
| 389 | */ |
| 390 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 391 | static int unix_dgram_peer_wake_relay(wait_queue_entry_t *q, unsigned mode, int flags, |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 392 | void *key) |
| 393 | { |
| 394 | struct unix_sock *u; |
| 395 | wait_queue_head_t *u_sleep; |
| 396 | |
| 397 | u = container_of(q, struct unix_sock, peer_wake); |
| 398 | |
| 399 | __remove_wait_queue(&unix_sk(u->peer_wake.private)->peer_wait, |
| 400 | q); |
| 401 | u->peer_wake.private = NULL; |
| 402 | |
| 403 | /* relaying can only happen while the wq still exists */ |
| 404 | u_sleep = sk_sleep(&u->sk); |
| 405 | if (u_sleep) |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 406 | wake_up_interruptible_poll(u_sleep, key_to_poll(key)); |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int unix_dgram_peer_wake_connect(struct sock *sk, struct sock *other) |
| 412 | { |
| 413 | struct unix_sock *u, *u_other; |
| 414 | int rc; |
| 415 | |
| 416 | u = unix_sk(sk); |
| 417 | u_other = unix_sk(other); |
| 418 | rc = 0; |
| 419 | spin_lock(&u_other->peer_wait.lock); |
| 420 | |
| 421 | if (!u->peer_wake.private) { |
| 422 | u->peer_wake.private = other; |
| 423 | __add_wait_queue(&u_other->peer_wait, &u->peer_wake); |
| 424 | |
| 425 | rc = 1; |
| 426 | } |
| 427 | |
| 428 | spin_unlock(&u_other->peer_wait.lock); |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | static void unix_dgram_peer_wake_disconnect(struct sock *sk, |
| 433 | struct sock *other) |
| 434 | { |
| 435 | struct unix_sock *u, *u_other; |
| 436 | |
| 437 | u = unix_sk(sk); |
| 438 | u_other = unix_sk(other); |
| 439 | spin_lock(&u_other->peer_wait.lock); |
| 440 | |
| 441 | if (u->peer_wake.private == other) { |
| 442 | __remove_wait_queue(&u_other->peer_wait, &u->peer_wake); |
| 443 | u->peer_wake.private = NULL; |
| 444 | } |
| 445 | |
| 446 | spin_unlock(&u_other->peer_wait.lock); |
| 447 | } |
| 448 | |
| 449 | static void unix_dgram_peer_wake_disconnect_wakeup(struct sock *sk, |
| 450 | struct sock *other) |
| 451 | { |
| 452 | unix_dgram_peer_wake_disconnect(sk, other); |
| 453 | wake_up_interruptible_poll(sk_sleep(sk), |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 454 | EPOLLOUT | |
| 455 | EPOLLWRNORM | |
| 456 | EPOLLWRBAND); |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* preconditions: |
| 460 | * - unix_peer(sk) == other |
| 461 | * - association is stable |
| 462 | */ |
| 463 | static int unix_dgram_peer_wake_me(struct sock *sk, struct sock *other) |
| 464 | { |
| 465 | int connected; |
| 466 | |
| 467 | connected = unix_dgram_peer_wake_connect(sk, other); |
| 468 | |
Jason Baron | 51f7e95 | 2018-08-03 17:24:53 -0400 | [diff] [blame] | 469 | /* If other is SOCK_DEAD, we want to make sure we signal |
| 470 | * POLLOUT, such that a subsequent write() can get a |
| 471 | * -ECONNREFUSED. Otherwise, if we haven't queued any skbs |
| 472 | * to other and its full, we will hang waiting for POLLOUT. |
| 473 | */ |
| 474 | if (unix_recvq_full(other) && !sock_flag(other, SOCK_DEAD)) |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 475 | return 1; |
| 476 | |
| 477 | if (connected) |
| 478 | unix_dgram_peer_wake_disconnect(sk, other); |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
Eric Dumazet | 1586a58 | 2015-10-23 10:59:16 -0700 | [diff] [blame] | 483 | static int unix_writable(const struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Eric Dumazet | 1586a58 | 2015-10-23 10:59:16 -0700 | [diff] [blame] | 485 | return sk->sk_state != TCP_LISTEN && |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 486 | (refcount_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void unix_write_space(struct sock *sk) |
| 490 | { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 491 | struct socket_wq *wq; |
| 492 | |
| 493 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (unix_writable(sk)) { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 495 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 496 | if (skwq_has_sleeper(wq)) |
Eric Dumazet | 67426b7 | 2010-10-29 20:44:44 +0000 | [diff] [blame] | 497 | wake_up_interruptible_sync_poll(&wq->wait, |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 498 | EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND); |
Pavel Emelyanov | 8d8ad9d | 2007-11-26 20:10:50 +0800 | [diff] [blame] | 499 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 501 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* When dgram socket disconnects (or changes its peer), we clear its receive |
| 505 | * queue of packets arrived from previous peer. First, it allows to do |
| 506 | * flow control based only on wmem_alloc; second, sk connected to peer |
| 507 | * may receive messages only from that peer. */ |
| 508 | static void unix_dgram_disconnected(struct sock *sk, struct sock *other) |
| 509 | { |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 510 | if (!skb_queue_empty(&sk->sk_receive_queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | skb_queue_purge(&sk->sk_receive_queue); |
| 512 | wake_up_interruptible_all(&unix_sk(sk)->peer_wait); |
| 513 | |
| 514 | /* If one link of bidirectional dgram pipe is disconnected, |
| 515 | * we signal error. Messages are lost. Do not make this, |
| 516 | * when peer was not connected to us. |
| 517 | */ |
| 518 | if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) { |
| 519 | other->sk_err = ECONNRESET; |
Alexander Aring | e3ae236 | 2021-06-27 18:48:21 -0400 | [diff] [blame] | 520 | sk_error_report(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
Eric Dumazet | dc56ad7 | 2021-08-30 10:21:37 -0700 | [diff] [blame] | 523 | other->sk_state = TCP_CLOSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static void unix_sock_destructor(struct sock *sk) |
| 527 | { |
| 528 | struct unix_sock *u = unix_sk(sk); |
| 529 | |
| 530 | skb_queue_purge(&sk->sk_receive_queue); |
| 531 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 532 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
| 533 | if (u->oob_skb) { |
| 534 | kfree_skb(u->oob_skb); |
| 535 | u->oob_skb = NULL; |
| 536 | } |
| 537 | #endif |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 538 | WARN_ON(refcount_read(&sk->sk_wmem_alloc)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 539 | WARN_ON(!sk_unhashed(sk)); |
| 540 | WARN_ON(sk->sk_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (!sock_flag(sk, SOCK_DEAD)) { |
wangweidong | 5cc208b | 2013-12-06 18:03:36 +0800 | [diff] [blame] | 542 | pr_info("Attempt to release alive unix socket: %p\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
| 546 | if (u->addr) |
| 547 | unix_release_addr(u->addr); |
| 548 | |
Eric Dumazet | 518de9b | 2010-10-26 14:22:44 -0700 | [diff] [blame] | 549 | atomic_long_dec(&unix_nr_socks); |
Eric Dumazet | a8076d8 | 2008-11-17 02:38:49 -0800 | [diff] [blame] | 550 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | #ifdef UNIX_REFCNT_DEBUG |
wangweidong | 5cc208b | 2013-12-06 18:03:36 +0800 | [diff] [blame] | 552 | pr_debug("UNIX %p is destroyed, %ld are still alive.\n", sk, |
Eric Dumazet | 518de9b | 2010-10-26 14:22:44 -0700 | [diff] [blame] | 553 | atomic_long_read(&unix_nr_socks)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | #endif |
| 555 | } |
| 556 | |
Paul Moore | ded34e0 | 2013-03-25 03:18:33 +0000 | [diff] [blame] | 557 | static void unix_release_sock(struct sock *sk, int embrion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | struct unix_sock *u = unix_sk(sk); |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 560 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | struct sock *skpair; |
| 562 | struct sk_buff *skb; |
| 563 | int state; |
| 564 | |
| 565 | unix_remove_socket(sk); |
| 566 | |
| 567 | /* Clear state */ |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 568 | unix_state_lock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | sock_orphan(sk); |
| 570 | sk->sk_shutdown = SHUTDOWN_MASK; |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 571 | path = u->path; |
| 572 | u->path.dentry = NULL; |
| 573 | u->path.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | state = sk->sk_state; |
| 575 | sk->sk_state = TCP_CLOSE; |
Eric Dumazet | a494bd6 | 2021-06-16 07:47:15 -0700 | [diff] [blame] | 576 | |
| 577 | skpair = unix_peer(sk); |
| 578 | unix_peer(sk) = NULL; |
| 579 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 580 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | wake_up_interruptible_all(&u->peer_wait); |
| 583 | |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 584 | if (skpair != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) { |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 586 | unix_state_lock(skpair); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | /* No more writes */ |
| 588 | skpair->sk_shutdown = SHUTDOWN_MASK; |
| 589 | if (!skb_queue_empty(&sk->sk_receive_queue) || embrion) |
| 590 | skpair->sk_err = ECONNRESET; |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 591 | unix_state_unlock(skpair); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | skpair->sk_state_change(skpair); |
Pavel Emelyanov | 8d8ad9d | 2007-11-26 20:10:50 +0800 | [diff] [blame] | 593 | sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 595 | |
| 596 | unix_dgram_peer_wake_disconnect(sk, skpair); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | sock_put(skpair); /* It may now die */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* Try to flush out this socket. Throw out buffers at least */ |
| 601 | |
| 602 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 603 | if (state == TCP_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unix_release_sock(skb->sk, 1); |
| 605 | /* passed fds are erased in the kfree_skb hook */ |
Hannes Frederic Sowa | 73ed5d2 | 2015-11-10 16:23:15 +0100 | [diff] [blame] | 606 | UNIXCB(skb).consumed = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | kfree_skb(skb); |
| 608 | } |
| 609 | |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 610 | if (path.dentry) |
| 611 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | sock_put(sk); |
| 614 | |
| 615 | /* ---- Socket is dead now and most probably destroyed ---- */ |
| 616 | |
| 617 | /* |
Alan Cox | e04dae8 | 2012-09-17 00:52:41 +0000 | [diff] [blame] | 618 | * Fixme: BSD difference: In BSD all sockets connected to us get |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | * ECONNRESET and we die on the spot. In Linux we behave |
| 620 | * like files and pipes do and wait for the last |
| 621 | * dereference. |
| 622 | * |
| 623 | * Can't we simply set sock->err? |
| 624 | * |
| 625 | * What the above comment does talk about? --ANK(980817) |
| 626 | */ |
| 627 | |
Pavel Emelyanov | 9305cfa | 2007-11-10 22:06:01 -0800 | [diff] [blame] | 628 | if (unix_tot_inflight) |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 629 | unix_gc(); /* Garbage collect fds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 632 | static void init_peercred(struct sock *sk) |
| 633 | { |
Eric Dumazet | 35306eb | 2021-09-29 15:57:50 -0700 | [diff] [blame] | 634 | const struct cred *old_cred; |
| 635 | struct pid *old_pid; |
| 636 | |
| 637 | spin_lock(&sk->sk_peer_lock); |
| 638 | old_pid = sk->sk_peer_pid; |
| 639 | old_cred = sk->sk_peer_cred; |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 640 | sk->sk_peer_pid = get_pid(task_tgid(current)); |
| 641 | sk->sk_peer_cred = get_current_cred(); |
Eric Dumazet | 35306eb | 2021-09-29 15:57:50 -0700 | [diff] [blame] | 642 | spin_unlock(&sk->sk_peer_lock); |
| 643 | |
| 644 | put_pid(old_pid); |
| 645 | put_cred(old_cred); |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static void copy_peercred(struct sock *sk, struct sock *peersk) |
| 649 | { |
Eric Dumazet | 35306eb | 2021-09-29 15:57:50 -0700 | [diff] [blame] | 650 | const struct cred *old_cred; |
| 651 | struct pid *old_pid; |
| 652 | |
| 653 | if (sk < peersk) { |
| 654 | spin_lock(&sk->sk_peer_lock); |
| 655 | spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING); |
| 656 | } else { |
| 657 | spin_lock(&peersk->sk_peer_lock); |
| 658 | spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING); |
| 659 | } |
| 660 | old_pid = sk->sk_peer_pid; |
| 661 | old_cred = sk->sk_peer_cred; |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 662 | sk->sk_peer_pid = get_pid(peersk->sk_peer_pid); |
| 663 | sk->sk_peer_cred = get_cred(peersk->sk_peer_cred); |
Eric Dumazet | 35306eb | 2021-09-29 15:57:50 -0700 | [diff] [blame] | 664 | |
| 665 | spin_unlock(&sk->sk_peer_lock); |
| 666 | spin_unlock(&peersk->sk_peer_lock); |
| 667 | |
| 668 | put_pid(old_pid); |
| 669 | put_cred(old_cred); |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | static int unix_listen(struct socket *sock, int backlog) |
| 673 | { |
| 674 | int err; |
| 675 | struct sock *sk = sock->sk; |
| 676 | struct unix_sock *u = unix_sk(sk); |
| 677 | |
| 678 | err = -EOPNOTSUPP; |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 679 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
| 680 | goto out; /* Only stream/seqpacket sockets accept */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | err = -EINVAL; |
| 682 | if (!u->addr) |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 683 | goto out; /* No listens on an unbound socket */ |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 684 | unix_state_lock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN) |
| 686 | goto out_unlock; |
| 687 | if (backlog > sk->sk_max_ack_backlog) |
| 688 | wake_up_interruptible_all(&u->peer_wait); |
| 689 | sk->sk_max_ack_backlog = backlog; |
| 690 | sk->sk_state = TCP_LISTEN; |
| 691 | /* set credentials so connect can copy them */ |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 692 | init_peercred(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | err = 0; |
| 694 | |
| 695 | out_unlock: |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 696 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | out: |
| 698 | return err; |
| 699 | } |
| 700 | |
| 701 | static int unix_release(struct socket *); |
| 702 | static int unix_bind(struct socket *, struct sockaddr *, int); |
| 703 | static int unix_stream_connect(struct socket *, struct sockaddr *, |
| 704 | int addr_len, int flags); |
| 705 | static int unix_socketpair(struct socket *, struct socket *); |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 706 | static int unix_accept(struct socket *, struct socket *, int, bool); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 707 | static int unix_getname(struct socket *, struct sockaddr *, int); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 708 | static __poll_t unix_poll(struct file *, struct socket *, poll_table *); |
| 709 | static __poll_t unix_dgram_poll(struct file *, struct socket *, |
| 710 | poll_table *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | static int unix_ioctl(struct socket *, unsigned int, unsigned long); |
Arnd Bergmann | 5f6beb9 | 2019-06-03 22:03:44 +0200 | [diff] [blame] | 712 | #ifdef CONFIG_COMPAT |
| 713 | static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); |
| 714 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | static int unix_shutdown(struct socket *, int); |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 716 | static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t); |
| 717 | static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 718 | static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset, |
| 719 | size_t size, int flags); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 720 | static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos, |
| 721 | struct pipe_inode_info *, size_t size, |
| 722 | unsigned int flags); |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 723 | static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t); |
| 724 | static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int); |
Cong Wang | 29df44f | 2021-07-04 12:02:44 -0700 | [diff] [blame] | 725 | static int unix_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 726 | sk_read_actor_t recv_actor); |
Jiang Wang | 77462de | 2021-08-16 19:03:20 +0000 | [diff] [blame] | 727 | static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 728 | sk_read_actor_t recv_actor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | static int unix_dgram_connect(struct socket *, struct sockaddr *, |
| 730 | int, int); |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 731 | static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t); |
| 732 | static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t, |
| 733 | int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
Sasha Levin | 12663bf | 2013-12-07 17:26:27 -0500 | [diff] [blame] | 735 | static int unix_set_peek_off(struct sock *sk, int val) |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 736 | { |
| 737 | struct unix_sock *u = unix_sk(sk); |
| 738 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 739 | if (mutex_lock_interruptible(&u->iolock)) |
Sasha Levin | 12663bf | 2013-12-07 17:26:27 -0500 | [diff] [blame] | 740 | return -EINTR; |
| 741 | |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 742 | sk->sk_peek_off = val; |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 743 | mutex_unlock(&u->iolock); |
Sasha Levin | 12663bf | 2013-12-07 17:26:27 -0500 | [diff] [blame] | 744 | |
| 745 | return 0; |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 746 | } |
| 747 | |
David S. Miller | 5c05a16 | 2020-02-27 11:52:35 -0800 | [diff] [blame] | 748 | #ifdef CONFIG_PROC_FS |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 749 | static void unix_show_fdinfo(struct seq_file *m, struct socket *sock) |
| 750 | { |
| 751 | struct sock *sk = sock->sk; |
| 752 | struct unix_sock *u; |
| 753 | |
| 754 | if (sk) { |
| 755 | u = unix_sk(sock->sk); |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 756 | seq_printf(m, "scm_fds: %u\n", |
| 757 | atomic_read(&u->scm_stat.nr_fds)); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 758 | } |
| 759 | } |
Tobias Klauser | 3a12500 | 2020-02-26 18:29:53 +0100 | [diff] [blame] | 760 | #else |
| 761 | #define unix_show_fdinfo NULL |
| 762 | #endif |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 763 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 764 | static const struct proto_ops unix_stream_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | .family = PF_UNIX, |
| 766 | .owner = THIS_MODULE, |
| 767 | .release = unix_release, |
| 768 | .bind = unix_bind, |
| 769 | .connect = unix_stream_connect, |
| 770 | .socketpair = unix_socketpair, |
| 771 | .accept = unix_accept, |
| 772 | .getname = unix_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 773 | .poll = unix_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | .ioctl = unix_ioctl, |
Arnd Bergmann | 5f6beb9 | 2019-06-03 22:03:44 +0200 | [diff] [blame] | 775 | #ifdef CONFIG_COMPAT |
| 776 | .compat_ioctl = unix_compat_ioctl, |
| 777 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | .listen = unix_listen, |
| 779 | .shutdown = unix_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | .sendmsg = unix_stream_sendmsg, |
| 781 | .recvmsg = unix_stream_recvmsg, |
Jiang Wang | 77462de | 2021-08-16 19:03:20 +0000 | [diff] [blame] | 782 | .read_sock = unix_stream_read_sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | .mmap = sock_no_mmap, |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 784 | .sendpage = unix_stream_sendpage, |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 785 | .splice_read = unix_stream_splice_read, |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 786 | .set_peek_off = unix_set_peek_off, |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 787 | .show_fdinfo = unix_show_fdinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | }; |
| 789 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 790 | static const struct proto_ops unix_dgram_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | .family = PF_UNIX, |
| 792 | .owner = THIS_MODULE, |
| 793 | .release = unix_release, |
| 794 | .bind = unix_bind, |
| 795 | .connect = unix_dgram_connect, |
| 796 | .socketpair = unix_socketpair, |
| 797 | .accept = sock_no_accept, |
| 798 | .getname = unix_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 799 | .poll = unix_dgram_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | .ioctl = unix_ioctl, |
Arnd Bergmann | 5f6beb9 | 2019-06-03 22:03:44 +0200 | [diff] [blame] | 801 | #ifdef CONFIG_COMPAT |
| 802 | .compat_ioctl = unix_compat_ioctl, |
| 803 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | .listen = sock_no_listen, |
| 805 | .shutdown = unix_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | .sendmsg = unix_dgram_sendmsg, |
Cong Wang | 29df44f | 2021-07-04 12:02:44 -0700 | [diff] [blame] | 807 | .read_sock = unix_read_sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | .recvmsg = unix_dgram_recvmsg, |
| 809 | .mmap = sock_no_mmap, |
| 810 | .sendpage = sock_no_sendpage, |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 811 | .set_peek_off = unix_set_peek_off, |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 812 | .show_fdinfo = unix_show_fdinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | }; |
| 814 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 815 | static const struct proto_ops unix_seqpacket_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | .family = PF_UNIX, |
| 817 | .owner = THIS_MODULE, |
| 818 | .release = unix_release, |
| 819 | .bind = unix_bind, |
| 820 | .connect = unix_stream_connect, |
| 821 | .socketpair = unix_socketpair, |
| 822 | .accept = unix_accept, |
| 823 | .getname = unix_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 824 | .poll = unix_dgram_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | .ioctl = unix_ioctl, |
Arnd Bergmann | 5f6beb9 | 2019-06-03 22:03:44 +0200 | [diff] [blame] | 826 | #ifdef CONFIG_COMPAT |
| 827 | .compat_ioctl = unix_compat_ioctl, |
| 828 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | .listen = unix_listen, |
| 830 | .shutdown = unix_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | .sendmsg = unix_seqpacket_sendmsg, |
Eric W. Biederman | a05d2ad | 2011-04-24 01:54:57 +0000 | [diff] [blame] | 832 | .recvmsg = unix_seqpacket_recvmsg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | .mmap = sock_no_mmap, |
| 834 | .sendpage = sock_no_sendpage, |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 835 | .set_peek_off = unix_set_peek_off, |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 836 | .show_fdinfo = unix_show_fdinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | }; |
| 838 | |
Cong Wang | c7272e1 | 2021-07-04 12:02:46 -0700 | [diff] [blame] | 839 | static void unix_close(struct sock *sk, long timeout) |
| 840 | { |
| 841 | /* Nothing to do here, unix socket does not need a ->close(). |
| 842 | * This is merely for sockmap. |
| 843 | */ |
| 844 | } |
| 845 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 846 | static void unix_unhash(struct sock *sk) |
| 847 | { |
| 848 | /* Nothing to do here, unix socket does not need a ->unhash(). |
| 849 | * This is merely for sockmap. |
| 850 | */ |
| 851 | } |
| 852 | |
| 853 | struct proto unix_dgram_proto = { |
Stephen Boyd | 0edf082 | 2021-10-08 14:59:45 -0700 | [diff] [blame] | 854 | .name = "UNIX", |
Eric Dumazet | 248969a | 2008-11-17 00:00:30 -0800 | [diff] [blame] | 855 | .owner = THIS_MODULE, |
Eric Dumazet | 248969a | 2008-11-17 00:00:30 -0800 | [diff] [blame] | 856 | .obj_size = sizeof(struct unix_sock), |
Cong Wang | c7272e1 | 2021-07-04 12:02:46 -0700 | [diff] [blame] | 857 | .close = unix_close, |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 858 | #ifdef CONFIG_BPF_SYSCALL |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 859 | .psock_update_sk_prot = unix_dgram_bpf_update_proto, |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 860 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | }; |
| 862 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 863 | struct proto unix_stream_proto = { |
| 864 | .name = "UNIX-STREAM", |
| 865 | .owner = THIS_MODULE, |
| 866 | .obj_size = sizeof(struct unix_sock), |
| 867 | .close = unix_close, |
| 868 | .unhash = unix_unhash, |
| 869 | #ifdef CONFIG_BPF_SYSCALL |
| 870 | .psock_update_sk_prot = unix_stream_bpf_update_proto, |
| 871 | #endif |
| 872 | }; |
| 873 | |
| 874 | static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct unix_sock *u; |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 877 | struct sock *sk; |
| 878 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Eric Dumazet | 518de9b | 2010-10-26 14:22:44 -0700 | [diff] [blame] | 880 | atomic_long_inc(&unix_nr_socks); |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 881 | if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) { |
| 882 | err = -ENFILE; |
| 883 | goto err; |
| 884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 886 | if (type == SOCK_STREAM) |
| 887 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_stream_proto, kern); |
| 888 | else /*dgram and seqpacket */ |
| 889 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_dgram_proto, kern); |
| 890 | |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 891 | if (!sk) { |
| 892 | err = -ENOMEM; |
| 893 | goto err; |
| 894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 896 | sock_init_data(sock, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 898 | sk->sk_hash = unix_unbound_hash(sk); |
Vladimir Davydov | 3aa9799 | 2016-07-26 15:24:36 -0700 | [diff] [blame] | 899 | sk->sk_allocation = GFP_KERNEL_ACCOUNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sk->sk_write_space = unix_write_space; |
Denis V. Lunev | a0a53c8 | 2007-12-11 04:19:17 -0800 | [diff] [blame] | 901 | sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | sk->sk_destruct = unix_sock_destructor; |
| 903 | u = unix_sk(sk); |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 904 | u->path.dentry = NULL; |
| 905 | u->path.mnt = NULL; |
Benjamin LaHaise | fd19f32 | 2006-01-03 14:10:46 -0800 | [diff] [blame] | 906 | spin_lock_init(&u->lock); |
Al Viro | 516e0cc | 2008-07-26 00:39:17 -0400 | [diff] [blame] | 907 | atomic_long_set(&u->inflight, 0); |
Miklos Szeredi | 1fd05ba | 2007-07-11 14:22:39 -0700 | [diff] [blame] | 908 | INIT_LIST_HEAD(&u->link); |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 909 | mutex_init(&u->iolock); /* single task reading lock */ |
| 910 | mutex_init(&u->bindlock); /* single task binding lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | init_waitqueue_head(&u->peer_wait); |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 912 | init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 913 | memset(&u->scm_stat, 0, sizeof(struct scm_stat)); |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 914 | unix_insert_unbound_socket(sk); |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 915 | |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 916 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | return sk; |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 919 | |
| 920 | err: |
| 921 | atomic_long_dec(&unix_nr_socks); |
| 922 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 925 | static int unix_create(struct net *net, struct socket *sock, int protocol, |
| 926 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 928 | struct sock *sk; |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (protocol && protocol != PF_UNIX) |
| 931 | return -EPROTONOSUPPORT; |
| 932 | |
| 933 | sock->state = SS_UNCONNECTED; |
| 934 | |
| 935 | switch (sock->type) { |
| 936 | case SOCK_STREAM: |
| 937 | sock->ops = &unix_stream_ops; |
| 938 | break; |
| 939 | /* |
| 940 | * Believe it or not BSD has AF_UNIX, SOCK_RAW though |
| 941 | * nothing uses it. |
| 942 | */ |
| 943 | case SOCK_RAW: |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 944 | sock->type = SOCK_DGRAM; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 945 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | case SOCK_DGRAM: |
| 947 | sock->ops = &unix_dgram_ops; |
| 948 | break; |
| 949 | case SOCK_SEQPACKET: |
| 950 | sock->ops = &unix_seqpacket_ops; |
| 951 | break; |
| 952 | default: |
| 953 | return -ESOCKTNOSUPPORT; |
| 954 | } |
| 955 | |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 956 | sk = unix_create1(net, sock, kern, sock->type); |
| 957 | if (IS_ERR(sk)) |
| 958 | return PTR_ERR(sk); |
| 959 | |
| 960 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static int unix_release(struct socket *sock) |
| 964 | { |
| 965 | struct sock *sk = sock->sk; |
| 966 | |
| 967 | if (!sk) |
| 968 | return 0; |
| 969 | |
Cong Wang | c7272e1 | 2021-07-04 12:02:46 -0700 | [diff] [blame] | 970 | sk->sk_prot->close(sk, 0); |
Paul Moore | ded34e0 | 2013-03-25 03:18:33 +0000 | [diff] [blame] | 971 | unix_release_sock(sk, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | sock->sk = NULL; |
| 973 | |
Paul Moore | ded34e0 | 2013-03-25 03:18:33 +0000 | [diff] [blame] | 974 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 977 | static struct sock *unix_find_bsd(struct net *net, struct sockaddr_un *sunaddr, |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 978 | int addr_len, int type) |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 979 | { |
| 980 | struct inode *inode; |
| 981 | struct path path; |
| 982 | struct sock *sk; |
| 983 | int err; |
| 984 | |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 985 | unix_mkname_bsd(sunaddr, addr_len); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 986 | err = kern_path(sunaddr->sun_path, LOOKUP_FOLLOW, &path); |
| 987 | if (err) |
| 988 | goto fail; |
| 989 | |
| 990 | err = path_permission(&path, MAY_WRITE); |
| 991 | if (err) |
| 992 | goto path_put; |
| 993 | |
| 994 | err = -ECONNREFUSED; |
| 995 | inode = d_backing_inode(path.dentry); |
| 996 | if (!S_ISSOCK(inode->i_mode)) |
| 997 | goto path_put; |
| 998 | |
| 999 | sk = unix_find_socket_byinode(inode); |
| 1000 | if (!sk) |
| 1001 | goto path_put; |
| 1002 | |
| 1003 | err = -EPROTOTYPE; |
| 1004 | if (sk->sk_type == type) |
| 1005 | touch_atime(&path); |
| 1006 | else |
| 1007 | goto sock_put; |
| 1008 | |
| 1009 | path_put(&path); |
| 1010 | |
| 1011 | return sk; |
| 1012 | |
| 1013 | sock_put: |
| 1014 | sock_put(sk); |
| 1015 | path_put: |
| 1016 | path_put(&path); |
| 1017 | fail: |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1018 | return ERR_PTR(err); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | static struct sock *unix_find_abstract(struct net *net, |
| 1022 | struct sockaddr_un *sunaddr, |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1023 | int addr_len, int type) |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1024 | { |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 1025 | unsigned int hash = unix_abstract_hash(sunaddr, addr_len, type); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1026 | struct dentry *dentry; |
| 1027 | struct sock *sk; |
| 1028 | |
Kuniyuki Iwashima | f452be4 | 2021-11-24 11:14:28 +0900 | [diff] [blame] | 1029 | sk = unix_find_socket_byname(net, sunaddr, addr_len, hash); |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1030 | if (!sk) |
| 1031 | return ERR_PTR(-ECONNREFUSED); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1032 | |
| 1033 | dentry = unix_sk(sk)->path.dentry; |
| 1034 | if (dentry) |
| 1035 | touch_atime(&unix_sk(sk)->path); |
| 1036 | |
| 1037 | return sk; |
| 1038 | } |
| 1039 | |
| 1040 | static struct sock *unix_find_other(struct net *net, |
| 1041 | struct sockaddr_un *sunaddr, |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1042 | int addr_len, int type) |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1043 | { |
| 1044 | struct sock *sk; |
| 1045 | |
| 1046 | if (sunaddr->sun_path[0]) |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1047 | sk = unix_find_bsd(net, sunaddr, addr_len, type); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1048 | else |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1049 | sk = unix_find_abstract(net, sunaddr, addr_len, type); |
Kuniyuki Iwashima | fa39ef0 | 2021-11-24 11:14:21 +0900 | [diff] [blame] | 1050 | |
| 1051 | return sk; |
| 1052 | } |
| 1053 | |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1054 | static int unix_autobind(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | struct unix_sock *u = unix_sk(sk); |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 1057 | struct unix_address *addr; |
Tetsuo Handa | 8df73ff | 2010-09-04 01:34:28 +0000 | [diff] [blame] | 1058 | unsigned int retries = 0; |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1059 | static u32 ordernum = 1; |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1060 | unsigned int new_hash; |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1061 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 1063 | err = mutex_lock_interruptible(&u->bindlock); |
Sasha Levin | 37ab4fa | 2013-12-13 10:54:22 -0500 | [diff] [blame] | 1064 | if (err) |
| 1065 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (u->addr) |
| 1068 | goto out; |
| 1069 | |
| 1070 | err = -ENOMEM; |
Kuniyuki Iwashima | 755662c | 2021-11-24 11:14:19 +0900 | [diff] [blame] | 1071 | addr = kzalloc(sizeof(*addr) + |
| 1072 | offsetof(struct sockaddr_un, sun_path) + 16, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (!addr) |
| 1074 | goto out; |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | addr->name->sun_family = AF_UNIX; |
Reshetova, Elena | 8c9814b | 2017-06-30 13:08:05 +0300 | [diff] [blame] | 1077 | refcount_set(&addr->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
| 1079 | retry: |
Kuniyuki Iwashima | 755662c | 2021-11-24 11:14:19 +0900 | [diff] [blame] | 1080 | addr->len = sprintf(addr->name->sun_path + 1, "%05x", ordernum) + |
| 1081 | offsetof(struct sockaddr_un, sun_path) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1083 | new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type); |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 1084 | spin_lock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ordernum = (ordernum+1)&0xFFFFF; |
| 1086 | |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1087 | if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1088 | new_hash)) { |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 1089 | spin_unlock(&unix_table_lock); |
Tetsuo Handa | 8df73ff | 2010-09-04 01:34:28 +0000 | [diff] [blame] | 1090 | /* |
| 1091 | * __unix_find_socket_byname() may take long time if many names |
| 1092 | * are already in use. |
| 1093 | */ |
| 1094 | cond_resched(); |
| 1095 | /* Give up if all names seems to be in use. */ |
| 1096 | if (retries++ == 0xFFFFF) { |
| 1097 | err = -ENOSPC; |
| 1098 | kfree(addr); |
| 1099 | goto out; |
| 1100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | goto retry; |
| 1102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1104 | __unix_set_addr_hash(sk, addr, new_hash); |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 1105 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | err = 0; |
| 1107 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 1108 | out: mutex_unlock(&u->bindlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | return err; |
| 1110 | } |
| 1111 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1112 | static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr, |
| 1113 | int addr_len) |
Al Viro | faf0201 | 2012-07-20 02:37:29 +0400 | [diff] [blame] | 1114 | { |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1115 | umode_t mode = S_IFSOCK | |
| 1116 | (SOCK_INODE(sk->sk_socket)->i_mode & ~current_umask()); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1117 | struct unix_sock *u = unix_sk(sk); |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1118 | struct user_namespace *ns; // barf... |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1119 | struct unix_address *addr; |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1120 | unsigned int new_hash; |
Linus Torvalds | 38f7bd94 | 2016-09-01 14:56:49 -0700 | [diff] [blame] | 1121 | struct dentry *dentry; |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1122 | struct path parent; |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1123 | int err; |
| 1124 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1125 | unix_mkname_bsd(sunaddr, addr_len); |
| 1126 | addr_len = strlen(sunaddr->sun_path) + |
| 1127 | offsetof(struct sockaddr_un, sun_path) + 1; |
| 1128 | |
| 1129 | addr = unix_create_addr(sunaddr, addr_len); |
| 1130 | if (!addr) |
| 1131 | return -ENOMEM; |
| 1132 | |
Linus Torvalds | 38f7bd94 | 2016-09-01 14:56:49 -0700 | [diff] [blame] | 1133 | /* |
| 1134 | * Get the parent directory, calculate the hash for last |
| 1135 | * component. |
| 1136 | */ |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1137 | dentry = kern_path_create(AT_FDCWD, addr->name->sun_path, &parent, 0); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1138 | if (IS_ERR(dentry)) { |
| 1139 | err = PTR_ERR(dentry); |
| 1140 | goto out; |
| 1141 | } |
Al Viro | faf0201 | 2012-07-20 02:37:29 +0400 | [diff] [blame] | 1142 | |
Linus Torvalds | 38f7bd94 | 2016-09-01 14:56:49 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | * All right, let's create it. |
| 1145 | */ |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1146 | ns = mnt_user_ns(parent.mnt); |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1147 | err = security_path_mknod(&parent, dentry, mode, 0); |
Al Viro | 56c1731 | 2021-06-19 03:50:31 +0000 | [diff] [blame] | 1148 | if (!err) |
Al Viro | 71e6be6 | 2021-06-19 03:50:30 +0000 | [diff] [blame] | 1149 | err = vfs_mknod(ns, d_inode(parent.dentry), dentry, mode, 0); |
Al Viro | c0c3b8d | 2021-06-19 03:50:32 +0000 | [diff] [blame] | 1150 | if (err) |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1151 | goto out_path; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1152 | err = mutex_lock_interruptible(&u->bindlock); |
Al Viro | c0c3b8d | 2021-06-19 03:50:32 +0000 | [diff] [blame] | 1153 | if (err) |
| 1154 | goto out_unlink; |
| 1155 | if (u->addr) |
| 1156 | goto out_unlock; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1157 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1158 | new_hash = unix_bsd_hash(d_backing_inode(dentry)); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1159 | spin_lock(&unix_table_lock); |
Al Viro | 56c1731 | 2021-06-19 03:50:31 +0000 | [diff] [blame] | 1160 | u->path.mnt = mntget(parent.mnt); |
| 1161 | u->path.dentry = dget(dentry); |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1162 | __unix_set_addr_hash(sk, addr, new_hash); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1163 | spin_unlock(&unix_table_lock); |
| 1164 | mutex_unlock(&u->bindlock); |
Al Viro | 56c1731 | 2021-06-19 03:50:31 +0000 | [diff] [blame] | 1165 | done_path_create(&parent, dentry); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1166 | return 0; |
Al Viro | c0c3b8d | 2021-06-19 03:50:32 +0000 | [diff] [blame] | 1167 | |
| 1168 | out_unlock: |
| 1169 | mutex_unlock(&u->bindlock); |
| 1170 | err = -EINVAL; |
| 1171 | out_unlink: |
| 1172 | /* failed after successful mknod? unlink what we'd created... */ |
| 1173 | vfs_unlink(ns, d_inode(parent.dentry), dentry, NULL); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1174 | out_path: |
Al Viro | c0c3b8d | 2021-06-19 03:50:32 +0000 | [diff] [blame] | 1175 | done_path_create(&parent, dentry); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1176 | out: |
| 1177 | unix_release_addr(addr); |
| 1178 | return err == -EEXIST ? -EADDRINUSE : err; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1181 | static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr, |
| 1182 | int addr_len) |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1183 | { |
| 1184 | struct unix_sock *u = unix_sk(sk); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1185 | struct unix_address *addr; |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1186 | unsigned int new_hash; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1187 | int err; |
| 1188 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1189 | addr = unix_create_addr(sunaddr, addr_len); |
| 1190 | if (!addr) |
| 1191 | return -ENOMEM; |
| 1192 | |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1193 | err = mutex_lock_interruptible(&u->bindlock); |
| 1194 | if (err) |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1195 | goto out; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1196 | |
| 1197 | if (u->addr) { |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1198 | err = -EINVAL; |
| 1199 | goto out_mutex; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1202 | new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1203 | spin_lock(&unix_table_lock); |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1204 | |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1205 | if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1206 | new_hash)) |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1207 | goto out_spin; |
| 1208 | |
Kuniyuki Iwashima | e6b4b87 | 2021-11-24 11:14:29 +0900 | [diff] [blame^] | 1209 | __unix_set_addr_hash(sk, addr, new_hash); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1210 | spin_unlock(&unix_table_lock); |
| 1211 | mutex_unlock(&u->bindlock); |
| 1212 | return 0; |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1213 | |
| 1214 | out_spin: |
| 1215 | spin_unlock(&unix_table_lock); |
| 1216 | err = -EADDRINUSE; |
| 1217 | out_mutex: |
| 1218 | mutex_unlock(&u->bindlock); |
| 1219 | out: |
| 1220 | unix_release_addr(addr); |
| 1221 | return err; |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 1225 | { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1226 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; |
Kuniyuki Iwashima | 5c32a3e | 2021-11-24 11:14:25 +0900 | [diff] [blame] | 1227 | struct sock *sk = sock->sk; |
Kuniyuki Iwashima | 5c32a3e | 2021-11-24 11:14:25 +0900 | [diff] [blame] | 1228 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 1230 | if (addr_len == offsetof(struct sockaddr_un, sun_path) && |
| 1231 | sunaddr->sun_family == AF_UNIX) |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1232 | return unix_autobind(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 1234 | err = unix_validate_addr(sunaddr, addr_len); |
| 1235 | if (err) |
| 1236 | return err; |
| 1237 | |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1238 | if (sunaddr->sun_path[0]) |
| 1239 | err = unix_bind_bsd(sk, sunaddr, addr_len); |
Al Viro | fa42d91 | 2021-06-19 03:50:29 +0000 | [diff] [blame] | 1240 | else |
Kuniyuki Iwashima | 12f21c4 | 2021-11-24 11:14:26 +0900 | [diff] [blame] | 1241 | err = unix_bind_abstract(sk, sunaddr, addr_len); |
| 1242 | |
| 1243 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1246 | static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) |
| 1247 | { |
| 1248 | if (unlikely(sk1 == sk2) || !sk2) { |
| 1249 | unix_state_lock(sk1); |
| 1250 | return; |
| 1251 | } |
| 1252 | if (sk1 < sk2) { |
| 1253 | unix_state_lock(sk1); |
| 1254 | unix_state_lock_nested(sk2); |
| 1255 | } else { |
| 1256 | unix_state_lock(sk2); |
| 1257 | unix_state_lock_nested(sk1); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) |
| 1262 | { |
| 1263 | if (unlikely(sk1 == sk2) || !sk2) { |
| 1264 | unix_state_unlock(sk1); |
| 1265 | return; |
| 1266 | } |
| 1267 | unix_state_unlock(sk1); |
| 1268 | unix_state_unlock(sk2); |
| 1269 | } |
| 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, |
| 1272 | int alen, int flags) |
| 1273 | { |
| 1274 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1275 | struct net *net = sock_net(sk); |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1276 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | struct sock *other; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | int err; |
| 1279 | |
Mateusz Jurczyk | defbcf2 | 2017-06-08 11:13:36 +0200 | [diff] [blame] | 1280 | err = -EINVAL; |
| 1281 | if (alen < offsetofend(struct sockaddr, sa_family)) |
| 1282 | goto out; |
| 1283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | if (addr->sa_family != AF_UNSPEC) { |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 1285 | err = unix_validate_addr(sunaddr, alen); |
| 1286 | if (err) |
| 1287 | goto out; |
| 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (test_bit(SOCK_PASSCRED, &sock->flags) && |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1290 | !unix_sk(sk)->addr) { |
| 1291 | err = unix_autobind(sk); |
| 1292 | if (err) |
| 1293 | goto out; |
| 1294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1296 | restart: |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1297 | other = unix_find_other(net, sunaddr, alen, sock->type); |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1298 | if (IS_ERR(other)) { |
| 1299 | err = PTR_ERR(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | goto out; |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1303 | unix_state_double_lock(sk, other); |
| 1304 | |
| 1305 | /* Apparently VFS overslept socket death. Retry. */ |
| 1306 | if (sock_flag(other, SOCK_DEAD)) { |
| 1307 | unix_state_double_unlock(sk, other); |
| 1308 | sock_put(other); |
| 1309 | goto restart; |
| 1310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | err = -EPERM; |
| 1313 | if (!unix_may_send(sk, other)) |
| 1314 | goto out_unlock; |
| 1315 | |
| 1316 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); |
| 1317 | if (err) |
| 1318 | goto out_unlock; |
| 1319 | |
Eric Dumazet | dc56ad7 | 2021-08-30 10:21:37 -0700 | [diff] [blame] | 1320 | sk->sk_state = other->sk_state = TCP_ESTABLISHED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } else { |
| 1322 | /* |
| 1323 | * 1003.1g breaking connected state with AF_UNSPEC |
| 1324 | */ |
| 1325 | other = NULL; |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1326 | unix_state_double_lock(sk, other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * If it was connected, reconnect. |
| 1331 | */ |
| 1332 | if (unix_peer(sk)) { |
| 1333 | struct sock *old_peer = unix_peer(sk); |
Eric Dumazet | dc56ad7 | 2021-08-30 10:21:37 -0700 | [diff] [blame] | 1334 | |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1335 | unix_peer(sk) = other; |
Eric Dumazet | dc56ad7 | 2021-08-30 10:21:37 -0700 | [diff] [blame] | 1336 | if (!other) |
| 1337 | sk->sk_state = TCP_CLOSE; |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1338 | unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer); |
| 1339 | |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1340 | unix_state_double_unlock(sk, other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (other != old_peer) |
| 1343 | unix_dgram_disconnected(sk, old_peer); |
| 1344 | sock_put(old_peer); |
| 1345 | } else { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1346 | unix_peer(sk) = other; |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1347 | unix_state_double_unlock(sk, other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
Cong Wang | 83301b5 | 2021-07-04 12:02:45 -0700 | [diff] [blame] | 1349 | |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 1350 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | out_unlock: |
David S. Miller | 278a3de | 2007-05-31 15:19:20 -0700 | [diff] [blame] | 1353 | unix_state_double_unlock(sk, other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | sock_put(other); |
| 1355 | out: |
| 1356 | return err; |
| 1357 | } |
| 1358 | |
| 1359 | static long unix_wait_for_peer(struct sock *other, long timeo) |
Jules Irenge | 48851e9 | 2020-02-23 23:16:56 +0000 | [diff] [blame] | 1360 | __releases(&unix_sk(other)->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | { |
| 1362 | struct unix_sock *u = unix_sk(other); |
| 1363 | int sched; |
| 1364 | DEFINE_WAIT(wait); |
| 1365 | |
| 1366 | prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE); |
| 1367 | |
| 1368 | sched = !sock_flag(other, SOCK_DEAD) && |
| 1369 | !(other->sk_shutdown & RCV_SHUTDOWN) && |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 1370 | unix_recvq_full(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1372 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | if (sched) |
| 1375 | timeo = schedule_timeout(timeo); |
| 1376 | |
| 1377 | finish_wait(&u->peer_wait, &wait); |
| 1378 | return timeo; |
| 1379 | } |
| 1380 | |
| 1381 | static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, |
| 1382 | int addr_len, int flags) |
| 1383 | { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1384 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1386 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | struct unix_sock *u = unix_sk(sk), *newu, *otheru; |
| 1388 | struct sock *newsk = NULL; |
| 1389 | struct sock *other = NULL; |
| 1390 | struct sk_buff *skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | int st; |
| 1392 | int err; |
| 1393 | long timeo; |
| 1394 | |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 1395 | err = unix_validate_addr(sunaddr, addr_len); |
| 1396 | if (err) |
| 1397 | goto out; |
| 1398 | |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1399 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) { |
| 1400 | err = unix_autobind(sk); |
| 1401 | if (err) |
| 1402 | goto out; |
| 1403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
| 1406 | |
| 1407 | /* First of all allocate resources. |
| 1408 | If we will make it after state is locked, |
| 1409 | we will have to recheck all again in any case. |
| 1410 | */ |
| 1411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | /* create new sock for complete connection */ |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 1413 | newsk = unix_create1(sock_net(sk), NULL, 0, sock->type); |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 1414 | if (IS_ERR(newsk)) { |
| 1415 | err = PTR_ERR(newsk); |
| 1416 | newsk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | goto out; |
Kuniyuki Iwashima | f4bd73b | 2021-09-28 09:42:27 +0900 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | /* Allocate skb for sending to listening sock */ |
| 1423 | skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL); |
| 1424 | if (skb == NULL) |
| 1425 | goto out; |
| 1426 | |
| 1427 | restart: |
| 1428 | /* Find listening sock. */ |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1429 | other = unix_find_other(net, sunaddr, addr_len, sk->sk_type); |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1430 | if (IS_ERR(other)) { |
| 1431 | err = PTR_ERR(other); |
| 1432 | other = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | goto out; |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
| 1436 | /* Latch state of peer */ |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1437 | unix_state_lock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
| 1439 | /* Apparently VFS overslept socket death. Retry. */ |
| 1440 | if (sock_flag(other, SOCK_DEAD)) { |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1441 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | sock_put(other); |
| 1443 | goto restart; |
| 1444 | } |
| 1445 | |
| 1446 | err = -ECONNREFUSED; |
| 1447 | if (other->sk_state != TCP_LISTEN) |
| 1448 | goto out_unlock; |
Tomoki Sekiyama | 77238f2 | 2009-10-18 23:17:37 -0700 | [diff] [blame] | 1449 | if (other->sk_shutdown & RCV_SHUTDOWN) |
| 1450 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 1452 | if (unix_recvq_full(other)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | err = -EAGAIN; |
| 1454 | if (!timeo) |
| 1455 | goto out_unlock; |
| 1456 | |
| 1457 | timeo = unix_wait_for_peer(other, timeo); |
| 1458 | |
| 1459 | err = sock_intr_errno(timeo); |
| 1460 | if (signal_pending(current)) |
| 1461 | goto out; |
| 1462 | sock_put(other); |
| 1463 | goto restart; |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 1464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
| 1466 | /* Latch our state. |
| 1467 | |
Daniel Baluta | e5537bf | 2011-03-14 15:25:33 -0700 | [diff] [blame] | 1468 | It is tricky place. We need to grab our state lock and cannot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | drop lock on peer. It is dangerous because deadlock is |
| 1470 | possible. Connect to self case and simultaneous |
| 1471 | attempt to connect are eliminated by checking socket |
| 1472 | state. other is TCP_LISTEN, if sk is TCP_LISTEN we |
| 1473 | check this before attempt to grab lock. |
| 1474 | |
| 1475 | Well, and we have to recheck the state after socket locked. |
| 1476 | */ |
| 1477 | st = sk->sk_state; |
| 1478 | |
| 1479 | switch (st) { |
| 1480 | case TCP_CLOSE: |
| 1481 | /* This is ok... continue with connect */ |
| 1482 | break; |
| 1483 | case TCP_ESTABLISHED: |
| 1484 | /* Socket is already connected */ |
| 1485 | err = -EISCONN; |
| 1486 | goto out_unlock; |
| 1487 | default: |
| 1488 | err = -EINVAL; |
| 1489 | goto out_unlock; |
| 1490 | } |
| 1491 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1492 | unix_state_lock_nested(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | if (sk->sk_state != st) { |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1495 | unix_state_unlock(sk); |
| 1496 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | sock_put(other); |
| 1498 | goto restart; |
| 1499 | } |
| 1500 | |
David S. Miller | 3610cda | 2011-01-05 15:38:53 -0800 | [diff] [blame] | 1501 | err = security_unix_stream_connect(sk, other, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | if (err) { |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1503 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | goto out_unlock; |
| 1505 | } |
| 1506 | |
| 1507 | /* The way is open! Fastly set all the necessary fields... */ |
| 1508 | |
| 1509 | sock_hold(sk); |
| 1510 | unix_peer(newsk) = sk; |
| 1511 | newsk->sk_state = TCP_ESTABLISHED; |
| 1512 | newsk->sk_type = sk->sk_type; |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 1513 | init_peercred(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | newu = unix_sk(newsk); |
Eric Dumazet | eaefd110 | 2011-02-18 03:26:36 +0000 | [diff] [blame] | 1515 | RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | otheru = unix_sk(other); |
| 1517 | |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 1518 | /* copy address information from listening to new sock |
| 1519 | * |
| 1520 | * The contents of *(otheru->addr) and otheru->path |
| 1521 | * are seen fully set up here, since we have found |
| 1522 | * otheru in hash under unix_table_lock. Insertion |
| 1523 | * into the hash chain we'd found it in had been done |
| 1524 | * in an earlier critical area protected by unix_table_lock, |
| 1525 | * the same one where we'd set *(otheru->addr) contents, |
| 1526 | * as well as otheru->path and otheru->addr itself. |
| 1527 | * |
| 1528 | * Using smp_store_release() here to set newu->addr |
| 1529 | * is enough to make those stores, as well as stores |
| 1530 | * to newu->path visible to anyone who gets newu->addr |
| 1531 | * by smp_load_acquire(). IOW, the same warranties |
| 1532 | * as for unix_sock instances bound in unix_bind() or |
| 1533 | * in unix_autobind(). |
| 1534 | */ |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 1535 | if (otheru->path.dentry) { |
| 1536 | path_get(&otheru->path); |
| 1537 | newu->path = otheru->path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 1539 | refcount_inc(&otheru->addr->refcnt); |
| 1540 | smp_store_release(&newu->addr, otheru->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
| 1542 | /* Set credentials */ |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 1543 | copy_peercred(sk, other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | sock->state = SS_CONNECTED; |
| 1546 | sk->sk_state = TCP_ESTABLISHED; |
Benjamin LaHaise | 830a1e5 | 2005-12-13 23:22:32 -0800 | [diff] [blame] | 1547 | sock_hold(newsk); |
| 1548 | |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1549 | smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */ |
Benjamin LaHaise | 830a1e5 | 2005-12-13 23:22:32 -0800 | [diff] [blame] | 1550 | unix_peer(sk) = newsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1552 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
gushengxian | 4e03d07 | 2021-06-09 20:09:35 -0700 | [diff] [blame] | 1554 | /* take ten and send info to listening sock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | spin_lock(&other->sk_receive_queue.lock); |
| 1556 | __skb_queue_tail(&other->sk_receive_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | spin_unlock(&other->sk_receive_queue.lock); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1558 | unix_state_unlock(other); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 1559 | other->sk_data_ready(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | sock_put(other); |
| 1561 | return 0; |
| 1562 | |
| 1563 | out_unlock: |
| 1564 | if (other) |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1565 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
| 1567 | out: |
Wei Yongjun | 40d4444 | 2009-02-25 00:32:45 +0000 | [diff] [blame] | 1568 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if (newsk) |
| 1570 | unix_release_sock(newsk, 0); |
| 1571 | if (other) |
| 1572 | sock_put(other); |
| 1573 | return err; |
| 1574 | } |
| 1575 | |
| 1576 | static int unix_socketpair(struct socket *socka, struct socket *sockb) |
| 1577 | { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1578 | struct sock *ska = socka->sk, *skb = sockb->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
| 1580 | /* Join our sockets back to back */ |
| 1581 | sock_hold(ska); |
| 1582 | sock_hold(skb); |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1583 | unix_peer(ska) = skb; |
| 1584 | unix_peer(skb) = ska; |
Eric W. Biederman | 109f6e3 | 2010-06-13 03:30:14 +0000 | [diff] [blame] | 1585 | init_peercred(ska); |
| 1586 | init_peercred(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Cong Wang | 83301b5 | 2021-07-04 12:02:45 -0700 | [diff] [blame] | 1588 | ska->sk_state = TCP_ESTABLISHED; |
| 1589 | skb->sk_state = TCP_ESTABLISHED; |
| 1590 | socka->state = SS_CONNECTED; |
| 1591 | sockb->state = SS_CONNECTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | return 0; |
| 1593 | } |
| 1594 | |
Daniel Borkmann | 90c6bd3 | 2013-10-17 22:51:31 +0200 | [diff] [blame] | 1595 | static void unix_sock_inherit_flags(const struct socket *old, |
| 1596 | struct socket *new) |
| 1597 | { |
| 1598 | if (test_bit(SOCK_PASSCRED, &old->flags)) |
| 1599 | set_bit(SOCK_PASSCRED, &new->flags); |
| 1600 | if (test_bit(SOCK_PASSSEC, &old->flags)) |
| 1601 | set_bit(SOCK_PASSSEC, &new->flags); |
| 1602 | } |
| 1603 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1604 | static int unix_accept(struct socket *sock, struct socket *newsock, int flags, |
| 1605 | bool kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
| 1607 | struct sock *sk = sock->sk; |
| 1608 | struct sock *tsk; |
| 1609 | struct sk_buff *skb; |
| 1610 | int err; |
| 1611 | |
| 1612 | err = -EOPNOTSUPP; |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 1613 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | goto out; |
| 1615 | |
| 1616 | err = -EINVAL; |
| 1617 | if (sk->sk_state != TCP_LISTEN) |
| 1618 | goto out; |
| 1619 | |
| 1620 | /* If socket state is TCP_LISTEN it cannot change (for now...), |
| 1621 | * so that no locks are necessary. |
| 1622 | */ |
| 1623 | |
| 1624 | skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err); |
| 1625 | if (!skb) { |
| 1626 | /* This means receive shutdown. */ |
| 1627 | if (err == 0) |
| 1628 | err = -EINVAL; |
| 1629 | goto out; |
| 1630 | } |
| 1631 | |
| 1632 | tsk = skb->sk; |
| 1633 | skb_free_datagram(sk, skb); |
| 1634 | wake_up_interruptible(&unix_sk(sk)->peer_wait); |
| 1635 | |
| 1636 | /* attach accepted sock to socket */ |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1637 | unix_state_lock(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | newsock->state = SS_CONNECTED; |
Daniel Borkmann | 90c6bd3 | 2013-10-17 22:51:31 +0200 | [diff] [blame] | 1639 | unix_sock_inherit_flags(sock, newsock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | sock_graft(tsk, newsock); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1641 | unix_state_unlock(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | return 0; |
| 1643 | |
| 1644 | out: |
| 1645 | return err; |
| 1646 | } |
| 1647 | |
| 1648 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1649 | static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | { |
| 1651 | struct sock *sk = sock->sk; |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 1652 | struct unix_address *addr; |
Cyrill Gorcunov | 13cfa97 | 2009-11-08 05:51:19 +0000 | [diff] [blame] | 1653 | DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | int err = 0; |
| 1655 | |
| 1656 | if (peer) { |
| 1657 | sk = unix_peer_get(sk); |
| 1658 | |
| 1659 | err = -ENOTCONN; |
| 1660 | if (!sk) |
| 1661 | goto out; |
| 1662 | err = 0; |
| 1663 | } else { |
| 1664 | sock_hold(sk); |
| 1665 | } |
| 1666 | |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 1667 | addr = smp_load_acquire(&unix_sk(sk)->addr); |
| 1668 | if (!addr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | sunaddr->sun_family = AF_UNIX; |
| 1670 | sunaddr->sun_path[0] = 0; |
Kuniyuki Iwashima | 755662c | 2021-11-24 11:14:19 +0900 | [diff] [blame] | 1671 | err = offsetof(struct sockaddr_un, sun_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | } else { |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1673 | err = addr->len; |
| 1674 | memcpy(sunaddr, addr->name, addr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | sock_put(sk); |
| 1677 | out: |
| 1678 | return err; |
| 1679 | } |
| 1680 | |
Miklos Szeredi | cbcf011 | 2021-07-28 14:47:20 +0200 | [diff] [blame] | 1681 | static void unix_peek_fds(struct scm_cookie *scm, struct sk_buff *skb) |
| 1682 | { |
| 1683 | scm->fp = scm_fp_dup(UNIXCB(skb).fp); |
| 1684 | |
| 1685 | /* |
| 1686 | * Garbage collection of unix sockets starts by selecting a set of |
| 1687 | * candidate sockets which have reference only from being in flight |
| 1688 | * (total_refs == inflight_refs). This condition is checked once during |
| 1689 | * the candidate collection phase, and candidates are marked as such, so |
| 1690 | * that non-candidates can later be ignored. While inflight_refs is |
| 1691 | * protected by unix_gc_lock, total_refs (file count) is not, hence this |
| 1692 | * is an instantaneous decision. |
| 1693 | * |
| 1694 | * Once a candidate, however, the socket must not be reinstalled into a |
| 1695 | * file descriptor while the garbage collection is in progress. |
| 1696 | * |
| 1697 | * If the above conditions are met, then the directed graph of |
| 1698 | * candidates (*) does not change while unix_gc_lock is held. |
| 1699 | * |
| 1700 | * Any operations that changes the file count through file descriptors |
| 1701 | * (dup, close, sendmsg) does not change the graph since candidates are |
| 1702 | * not installed in fds. |
| 1703 | * |
| 1704 | * Dequeing a candidate via recvmsg would install it into an fd, but |
| 1705 | * that takes unix_gc_lock to decrement the inflight count, so it's |
| 1706 | * serialized with garbage collection. |
| 1707 | * |
| 1708 | * MSG_PEEK is special in that it does not change the inflight count, |
| 1709 | * yet does install the socket into an fd. The following lock/unlock |
| 1710 | * pair is to ensure serialization with garbage collection. It must be |
| 1711 | * done between incrementing the file count and installing the file into |
| 1712 | * an fd. |
| 1713 | * |
| 1714 | * If garbage collection starts after the barrier provided by the |
| 1715 | * lock/unlock, then it will see the elevated refcount and not mark this |
| 1716 | * as a candidate. If a garbage collection is already in progress |
| 1717 | * before the file count was incremented, then the lock/unlock pair will |
| 1718 | * ensure that garbage collection is finished before progressing to |
| 1719 | * installing the fd. |
| 1720 | * |
| 1721 | * (*) A -> B where B is on the queue of A or B is on the queue of C |
| 1722 | * which is on the queue of listening socket A. |
| 1723 | */ |
| 1724 | spin_lock(&unix_gc_lock); |
| 1725 | spin_unlock(&unix_gc_lock); |
| 1726 | } |
| 1727 | |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 1728 | static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds) |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 1729 | { |
| 1730 | int err = 0; |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1731 | |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 1732 | UNIXCB(skb).pid = get_pid(scm->pid); |
Eric W. Biederman | 6b0ee8c0 | 2013-04-03 17:28:16 +0000 | [diff] [blame] | 1733 | UNIXCB(skb).uid = scm->creds.uid; |
| 1734 | UNIXCB(skb).gid = scm->creds.gid; |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 1735 | UNIXCB(skb).fp = NULL; |
Stephen Smalley | 37a9a8d | 2015-06-10 08:44:59 -0400 | [diff] [blame] | 1736 | unix_get_secdata(scm, skb); |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 1737 | if (scm->fp && send_fds) |
| 1738 | err = unix_attach_fds(scm, skb); |
| 1739 | |
| 1740 | skb->destructor = unix_destruct_scm; |
| 1741 | return err; |
| 1742 | } |
| 1743 | |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 1744 | static bool unix_passcred_enabled(const struct socket *sock, |
| 1745 | const struct sock *other) |
| 1746 | { |
| 1747 | return test_bit(SOCK_PASSCRED, &sock->flags) || |
| 1748 | !other->sk_socket || |
| 1749 | test_bit(SOCK_PASSCRED, &other->sk_socket->flags); |
| 1750 | } |
| 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | /* |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1753 | * Some apps rely on write() giving SCM_CREDENTIALS |
| 1754 | * We include credentials if source or destination socket |
| 1755 | * asserted SOCK_PASSCRED. |
| 1756 | */ |
| 1757 | static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock, |
| 1758 | const struct sock *other) |
| 1759 | { |
Eric W. Biederman | 6b0ee8c0 | 2013-04-03 17:28:16 +0000 | [diff] [blame] | 1760 | if (UNIXCB(skb).pid) |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1761 | return; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 1762 | if (unix_passcred_enabled(sock, other)) { |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1763 | UNIXCB(skb).pid = get_pid(task_tgid(current)); |
David S. Miller | 6e0895c | 2013-04-22 20:32:51 -0400 | [diff] [blame] | 1764 | current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid); |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 1768 | static int maybe_init_creds(struct scm_cookie *scm, |
| 1769 | struct socket *socket, |
| 1770 | const struct sock *other) |
| 1771 | { |
| 1772 | int err; |
| 1773 | struct msghdr msg = { .msg_controllen = 0 }; |
| 1774 | |
| 1775 | err = scm_send(socket, &msg, scm, false); |
| 1776 | if (err) |
| 1777 | return err; |
| 1778 | |
| 1779 | if (unix_passcred_enabled(socket, other)) { |
| 1780 | scm->pid = get_pid(task_tgid(current)); |
| 1781 | current_uid_gid(&scm->creds.uid, &scm->creds.gid); |
| 1782 | } |
| 1783 | return err; |
| 1784 | } |
| 1785 | |
| 1786 | static bool unix_skb_scm_eq(struct sk_buff *skb, |
| 1787 | struct scm_cookie *scm) |
| 1788 | { |
| 1789 | const struct unix_skb_parms *u = &UNIXCB(skb); |
| 1790 | |
| 1791 | return u->pid == scm->pid && |
| 1792 | uid_eq(u->uid, scm->creds.uid) && |
| 1793 | gid_eq(u->gid, scm->creds.gid) && |
| 1794 | unix_secdata_eq(scm, skb); |
| 1795 | } |
| 1796 | |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 1797 | static void scm_stat_add(struct sock *sk, struct sk_buff *skb) |
| 1798 | { |
| 1799 | struct scm_fp_list *fp = UNIXCB(skb).fp; |
| 1800 | struct unix_sock *u = unix_sk(sk); |
| 1801 | |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 1802 | if (unlikely(fp && fp->count)) |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 1803 | atomic_add(fp->count, &u->scm_stat.nr_fds); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | static void scm_stat_del(struct sock *sk, struct sk_buff *skb) |
| 1807 | { |
| 1808 | struct scm_fp_list *fp = UNIXCB(skb).fp; |
| 1809 | struct unix_sock *u = unix_sk(sk); |
| 1810 | |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 1811 | if (unlikely(fp && fp->count)) |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 1812 | atomic_sub(fp->count, &u->scm_stat.nr_fds); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 1813 | } |
| 1814 | |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 1815 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | * Send AF_UNIX data. |
| 1817 | */ |
| 1818 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1819 | static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg, |
| 1820 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1823 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | struct unix_sock *u = unix_sk(sk); |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 1825 | DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | struct sock *other = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | int err; |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 1828 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | long timeo; |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 1830 | struct scm_cookie scm; |
Eric Dumazet | eb6a248 | 2012-04-03 05:28:28 +0000 | [diff] [blame] | 1831 | int data_len = 0; |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1832 | int sk_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
dann frazier | 5f23b73 | 2008-11-26 15:32:27 -0800 | [diff] [blame] | 1834 | wait_for_unix_gc(); |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 1835 | err = scm_send(sock, msg, &scm, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | if (err < 0) |
| 1837 | return err; |
| 1838 | |
| 1839 | err = -EOPNOTSUPP; |
| 1840 | if (msg->msg_flags&MSG_OOB) |
| 1841 | goto out; |
| 1842 | |
| 1843 | if (msg->msg_namelen) { |
Kuniyuki Iwashima | b8a58aa | 2021-11-24 11:14:23 +0900 | [diff] [blame] | 1844 | err = unix_validate_addr(sunaddr, msg->msg_namelen); |
| 1845 | if (err) |
| 1846 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } else { |
| 1848 | sunaddr = NULL; |
| 1849 | err = -ENOTCONN; |
| 1850 | other = unix_peer_get(sk); |
| 1851 | if (!other) |
| 1852 | goto out; |
| 1853 | } |
| 1854 | |
Kuniyuki Iwashima | f7ed31f | 2021-11-24 11:14:20 +0900 | [diff] [blame] | 1855 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr) { |
| 1856 | err = unix_autobind(sk); |
| 1857 | if (err) |
| 1858 | goto out; |
| 1859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
| 1861 | err = -EMSGSIZE; |
| 1862 | if (len > sk->sk_sndbuf - 32) |
| 1863 | goto out; |
| 1864 | |
Kirill Tkhai | 31ff6aa | 2014-05-15 19:56:28 +0400 | [diff] [blame] | 1865 | if (len > SKB_MAX_ALLOC) { |
Eric Dumazet | eb6a248 | 2012-04-03 05:28:28 +0000 | [diff] [blame] | 1866 | data_len = min_t(size_t, |
| 1867 | len - SKB_MAX_ALLOC, |
| 1868 | MAX_SKB_FRAGS * PAGE_SIZE); |
Kirill Tkhai | 31ff6aa | 2014-05-15 19:56:28 +0400 | [diff] [blame] | 1869 | data_len = PAGE_ALIGN(data_len); |
| 1870 | |
| 1871 | BUILD_BUG_ON(SKB_MAX_ALLOC < PAGE_SIZE); |
| 1872 | } |
Eric Dumazet | eb6a248 | 2012-04-03 05:28:28 +0000 | [diff] [blame] | 1873 | |
| 1874 | skb = sock_alloc_send_pskb(sk, len - data_len, data_len, |
Eric Dumazet | 28d6427 | 2013-08-08 14:38:47 -0700 | [diff] [blame] | 1875 | msg->msg_flags & MSG_DONTWAIT, &err, |
| 1876 | PAGE_ALLOC_COSTLY_ORDER); |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1877 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | goto out; |
| 1879 | |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 1880 | err = unix_scm_to_skb(&scm, skb, true); |
Eric Dumazet | 25888e3 | 2010-11-25 04:11:39 +0000 | [diff] [blame] | 1881 | if (err < 0) |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 1882 | goto out_free; |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 1883 | |
Eric Dumazet | eb6a248 | 2012-04-03 05:28:28 +0000 | [diff] [blame] | 1884 | skb_put(skb, len - data_len); |
| 1885 | skb->data_len = data_len; |
| 1886 | skb->len = len; |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 1887 | err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | if (err) |
| 1889 | goto out_free; |
| 1890 | |
| 1891 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 1892 | |
| 1893 | restart: |
| 1894 | if (!other) { |
| 1895 | err = -ECONNRESET; |
| 1896 | if (sunaddr == NULL) |
| 1897 | goto out_free; |
| 1898 | |
Kuniyuki Iwashima | d2d8c9f | 2021-11-24 11:14:24 +0900 | [diff] [blame] | 1899 | other = unix_find_other(net, sunaddr, msg->msg_namelen, |
| 1900 | sk->sk_type); |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1901 | if (IS_ERR(other)) { |
| 1902 | err = PTR_ERR(other); |
| 1903 | other = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | goto out_free; |
Kuniyuki Iwashima | aed26f5 | 2021-11-24 11:14:22 +0900 | [diff] [blame] | 1905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Alban Crequy | d6ae3ba | 2011-01-18 06:39:15 +0000 | [diff] [blame] | 1908 | if (sk_filter(other, skb) < 0) { |
| 1909 | /* Toss the packet but do not return any error to the sender */ |
| 1910 | err = len; |
| 1911 | goto out_free; |
| 1912 | } |
| 1913 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1914 | sk_locked = 0; |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1915 | unix_state_lock(other); |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1916 | restart_locked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | err = -EPERM; |
| 1918 | if (!unix_may_send(sk, other)) |
| 1919 | goto out_unlock; |
| 1920 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1921 | if (unlikely(sock_flag(other, SOCK_DEAD))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | /* |
| 1923 | * Check with 1003.1g - what should |
| 1924 | * datagram error |
| 1925 | */ |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1926 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | sock_put(other); |
| 1928 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1929 | if (!sk_locked) |
| 1930 | unix_state_lock(sk); |
| 1931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | if (unix_peer(sk) == other) { |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 1934 | unix_peer(sk) = NULL; |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1935 | unix_dgram_peer_wake_disconnect_wakeup(sk, other); |
| 1936 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1937 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
Eric Dumazet | dc56ad7 | 2021-08-30 10:21:37 -0700 | [diff] [blame] | 1939 | sk->sk_state = TCP_CLOSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | unix_dgram_disconnected(sk, other); |
| 1941 | sock_put(other); |
| 1942 | err = -ECONNREFUSED; |
| 1943 | } else { |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 1944 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | other = NULL; |
| 1948 | if (err) |
| 1949 | goto out_free; |
| 1950 | goto restart; |
| 1951 | } |
| 1952 | |
| 1953 | err = -EPIPE; |
| 1954 | if (other->sk_shutdown & RCV_SHUTDOWN) |
| 1955 | goto out_unlock; |
| 1956 | |
| 1957 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 1958 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); |
| 1959 | if (err) |
| 1960 | goto out_unlock; |
| 1961 | } |
| 1962 | |
Rainer Weikusat | a5527dd | 2016-02-11 19:37:27 +0000 | [diff] [blame] | 1963 | /* other == sk && unix_peer(other) != sk if |
| 1964 | * - unix_peer(sk) == NULL, destination address bound to sk |
| 1965 | * - unix_peer(sk) == sk by time of get but disconnected before lock |
| 1966 | */ |
| 1967 | if (other != sk && |
Qian Cai | 86b18aa | 2020-02-04 13:40:29 -0500 | [diff] [blame] | 1968 | unlikely(unix_peer(other) != sk && |
| 1969 | unix_recvq_full_lockless(other))) { |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1970 | if (timeo) { |
| 1971 | timeo = unix_wait_for_peer(other, timeo); |
| 1972 | |
| 1973 | err = sock_intr_errno(timeo); |
| 1974 | if (signal_pending(current)) |
| 1975 | goto out_free; |
| 1976 | |
| 1977 | goto restart; |
| 1978 | } |
| 1979 | |
| 1980 | if (!sk_locked) { |
| 1981 | unix_state_unlock(other); |
| 1982 | unix_state_double_lock(sk, other); |
| 1983 | } |
| 1984 | |
| 1985 | if (unix_peer(sk) != other || |
| 1986 | unix_dgram_peer_wake_me(sk, other)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | err = -EAGAIN; |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1988 | sk_locked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | goto out_unlock; |
| 1990 | } |
| 1991 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1992 | if (!sk_locked) { |
| 1993 | sk_locked = 1; |
| 1994 | goto restart_locked; |
| 1995 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 1998 | if (unlikely(sk_locked)) |
| 1999 | unix_state_unlock(sk); |
| 2000 | |
Alban Crequy | 3f66116 | 2010-10-04 08:48:28 +0000 | [diff] [blame] | 2001 | if (sock_flag(other, SOCK_RCVTSTAMP)) |
| 2002 | __net_timestamp(skb); |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2003 | maybe_add_creds(skb, sock, other); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 2004 | scm_stat_add(other, skb); |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 2005 | skb_queue_tail(&other->sk_receive_queue, skb); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2006 | unix_state_unlock(other); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2007 | other->sk_data_ready(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | sock_put(other); |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2009 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | return len; |
| 2011 | |
| 2012 | out_unlock: |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 2013 | if (sk_locked) |
| 2014 | unix_state_unlock(sk); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2015 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | out_free: |
| 2017 | kfree_skb(skb); |
| 2018 | out: |
| 2019 | if (other) |
| 2020 | sock_put(other); |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2021 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | return err; |
| 2023 | } |
| 2024 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2025 | /* We use paged skbs for stream sockets, and limit occupancy to 32768 |
Tobias Klauser | d4e9a40 | 2018-02-13 11:11:30 +0100 | [diff] [blame] | 2026 | * bytes, and a minimum of a full page. |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2027 | */ |
| 2028 | #define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768)) |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2029 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2030 | #if (IS_ENABLED(CONFIG_AF_UNIX_OOB)) |
| 2031 | static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other) |
| 2032 | { |
| 2033 | struct unix_sock *ousk = unix_sk(other); |
| 2034 | struct sk_buff *skb; |
| 2035 | int err = 0; |
| 2036 | |
| 2037 | skb = sock_alloc_send_skb(sock->sk, 1, msg->msg_flags & MSG_DONTWAIT, &err); |
| 2038 | |
| 2039 | if (!skb) |
| 2040 | return err; |
| 2041 | |
| 2042 | skb_put(skb, 1); |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2043 | err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, 1); |
| 2044 | |
| 2045 | if (err) { |
| 2046 | kfree_skb(skb); |
| 2047 | return err; |
| 2048 | } |
| 2049 | |
| 2050 | unix_state_lock(other); |
Rao Shoaib | 19eed72 | 2021-08-13 11:19:34 -0700 | [diff] [blame] | 2051 | |
| 2052 | if (sock_flag(other, SOCK_DEAD) || |
| 2053 | (other->sk_shutdown & RCV_SHUTDOWN)) { |
| 2054 | unix_state_unlock(other); |
| 2055 | kfree_skb(skb); |
| 2056 | return -EPIPE; |
| 2057 | } |
| 2058 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2059 | maybe_add_creds(skb, sock, other); |
| 2060 | skb_get(skb); |
| 2061 | |
| 2062 | if (ousk->oob_skb) |
Rao Shoaib | 19eed72 | 2021-08-13 11:19:34 -0700 | [diff] [blame] | 2063 | consume_skb(ousk->oob_skb); |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2064 | |
| 2065 | ousk->oob_skb = skb; |
| 2066 | |
| 2067 | scm_stat_add(other, skb); |
| 2068 | skb_queue_tail(&other->sk_receive_queue, skb); |
| 2069 | sk_send_sigurg(other); |
| 2070 | unix_state_unlock(other); |
| 2071 | other->sk_data_ready(other); |
| 2072 | |
| 2073 | return err; |
| 2074 | } |
| 2075 | #endif |
| 2076 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2077 | static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, |
| 2078 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | struct sock *sk = sock->sk; |
| 2081 | struct sock *other = NULL; |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2082 | int err, size; |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 2083 | struct sk_buff *skb; |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 2084 | int sent = 0; |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2085 | struct scm_cookie scm; |
Miklos Szeredi | 8ba69ba | 2009-09-11 11:31:45 -0700 | [diff] [blame] | 2086 | bool fds_sent = false; |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2087 | int data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
dann frazier | 5f23b73 | 2008-11-26 15:32:27 -0800 | [diff] [blame] | 2089 | wait_for_unix_gc(); |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2090 | err = scm_send(sock, msg, &scm, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | if (err < 0) |
| 2092 | return err; |
| 2093 | |
| 2094 | err = -EOPNOTSUPP; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2095 | if (msg->msg_flags & MSG_OOB) { |
| 2096 | #if (IS_ENABLED(CONFIG_AF_UNIX_OOB)) |
| 2097 | if (len) |
| 2098 | len--; |
| 2099 | else |
| 2100 | #endif |
| 2101 | goto out_err; |
| 2102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
| 2104 | if (msg->msg_namelen) { |
| 2105 | err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; |
| 2106 | goto out_err; |
| 2107 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | err = -ENOTCONN; |
Benjamin LaHaise | 830a1e5 | 2005-12-13 23:22:32 -0800 | [diff] [blame] | 2109 | other = unix_peer(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | if (!other) |
| 2111 | goto out_err; |
| 2112 | } |
| 2113 | |
| 2114 | if (sk->sk_shutdown & SEND_SHUTDOWN) |
| 2115 | goto pipe_err; |
| 2116 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2117 | while (sent < len) { |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2118 | size = len - sent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | /* Keep two messages in the pipe so it schedules better */ |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2121 | size = min_t(int, size, (sk->sk_sndbuf >> 1) - 64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2123 | /* allow fallback to order-0 allocations */ |
| 2124 | size = min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ); |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2125 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2126 | data_len = max_t(int, 0, size - SKB_MAX_HEAD(0)); |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2127 | |
Kirill Tkhai | 31ff6aa | 2014-05-15 19:56:28 +0400 | [diff] [blame] | 2128 | data_len = min_t(size_t, size, PAGE_ALIGN(data_len)); |
| 2129 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2130 | skb = sock_alloc_send_pskb(sk, size - data_len, data_len, |
Eric Dumazet | 28d6427 | 2013-08-08 14:38:47 -0700 | [diff] [blame] | 2131 | msg->msg_flags & MSG_DONTWAIT, &err, |
| 2132 | get_order(UNIX_SKB_FRAGS_SZ)); |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2133 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | goto out_err; |
| 2135 | |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 2136 | /* Only send the fds in the first buffer */ |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2137 | err = unix_scm_to_skb(&scm, skb, !fds_sent); |
Eric Dumazet | 25888e3 | 2010-11-25 04:11:39 +0000 | [diff] [blame] | 2138 | if (err < 0) { |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 2139 | kfree_skb(skb); |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 2140 | goto out_err; |
Miklos Szeredi | 6209344 | 2008-11-09 15:23:57 +0100 | [diff] [blame] | 2141 | } |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 2142 | fds_sent = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2144 | skb_put(skb, size - data_len); |
| 2145 | skb->data_len = data_len; |
| 2146 | skb->len = size; |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 2147 | err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2148 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | kfree_skb(skb); |
David S. Miller | f78a5fd | 2011-09-16 19:34:00 -0400 | [diff] [blame] | 2150 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2153 | unix_state_lock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | |
| 2155 | if (sock_flag(other, SOCK_DEAD) || |
| 2156 | (other->sk_shutdown & RCV_SHUTDOWN)) |
| 2157 | goto pipe_err_free; |
| 2158 | |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2159 | maybe_add_creds(skb, sock, other); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 2160 | scm_stat_add(other, skb); |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 2161 | skb_queue_tail(&other->sk_receive_queue, skb); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2162 | unix_state_unlock(other); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2163 | other->sk_data_ready(other); |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 2164 | sent += size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2167 | #if (IS_ENABLED(CONFIG_AF_UNIX_OOB)) |
| 2168 | if (msg->msg_flags & MSG_OOB) { |
| 2169 | err = queue_oob(sock, msg, other); |
| 2170 | if (err) |
| 2171 | goto out_err; |
| 2172 | sent++; |
| 2173 | } |
| 2174 | #endif |
| 2175 | |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2176 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
| 2178 | return sent; |
| 2179 | |
| 2180 | pipe_err_free: |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2181 | unix_state_unlock(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | kfree_skb(skb); |
| 2183 | pipe_err: |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2184 | if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) |
| 2185 | send_sig(SIGPIPE, current, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | err = -EPIPE; |
| 2187 | out_err: |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2188 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | return sent ? : err; |
| 2190 | } |
| 2191 | |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2192 | static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page, |
| 2193 | int offset, size_t size, int flags) |
| 2194 | { |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2195 | int err; |
| 2196 | bool send_sigpipe = false; |
| 2197 | bool init_scm = true; |
| 2198 | struct scm_cookie scm; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2199 | struct sock *other, *sk = socket->sk; |
| 2200 | struct sk_buff *skb, *newskb = NULL, *tail = NULL; |
| 2201 | |
| 2202 | if (flags & MSG_OOB) |
| 2203 | return -EOPNOTSUPP; |
| 2204 | |
| 2205 | other = unix_peer(sk); |
| 2206 | if (!other || sk->sk_state != TCP_ESTABLISHED) |
| 2207 | return -ENOTCONN; |
| 2208 | |
| 2209 | if (false) { |
| 2210 | alloc_skb: |
| 2211 | unix_state_unlock(other); |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2212 | mutex_unlock(&unix_sk(other)->iolock); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2213 | newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT, |
| 2214 | &err, 0); |
| 2215 | if (!newskb) |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2216 | goto err; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2219 | /* we must acquire iolock as we modify already present |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2220 | * skbs in the sk_receive_queue and mess with skb->len |
| 2221 | */ |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2222 | err = mutex_lock_interruptible(&unix_sk(other)->iolock); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2223 | if (err) { |
| 2224 | err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2225 | goto err; |
| 2226 | } |
| 2227 | |
| 2228 | if (sk->sk_shutdown & SEND_SHUTDOWN) { |
| 2229 | err = -EPIPE; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2230 | send_sigpipe = true; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2231 | goto err_unlock; |
| 2232 | } |
| 2233 | |
| 2234 | unix_state_lock(other); |
| 2235 | |
| 2236 | if (sock_flag(other, SOCK_DEAD) || |
| 2237 | other->sk_shutdown & RCV_SHUTDOWN) { |
| 2238 | err = -EPIPE; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2239 | send_sigpipe = true; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2240 | goto err_state_unlock; |
| 2241 | } |
| 2242 | |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2243 | if (init_scm) { |
| 2244 | err = maybe_init_creds(&scm, socket, other); |
| 2245 | if (err) |
| 2246 | goto err_state_unlock; |
| 2247 | init_scm = false; |
| 2248 | } |
| 2249 | |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2250 | skb = skb_peek_tail(&other->sk_receive_queue); |
| 2251 | if (tail && tail == skb) { |
| 2252 | skb = newskb; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2253 | } else if (!skb || !unix_skb_scm_eq(skb, &scm)) { |
| 2254 | if (newskb) { |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2255 | skb = newskb; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2256 | } else { |
| 2257 | tail = skb; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2258 | goto alloc_skb; |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2259 | } |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2260 | } else if (newskb) { |
| 2261 | /* this is fast path, we don't necessarily need to |
| 2262 | * call to kfree_skb even though with newskb == NULL |
| 2263 | * this - does no harm |
| 2264 | */ |
| 2265 | consume_skb(newskb); |
Hannes Frederic Sowa | 8844f97 | 2015-11-16 16:25:56 +0100 | [diff] [blame] | 2266 | newskb = NULL; |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | if (skb_append_pagefrags(skb, page, offset, size)) { |
| 2270 | tail = skb; |
| 2271 | goto alloc_skb; |
| 2272 | } |
| 2273 | |
| 2274 | skb->len += size; |
| 2275 | skb->data_len += size; |
| 2276 | skb->truesize += size; |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 2277 | refcount_add(size, &sk->sk_wmem_alloc); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2278 | |
Hannes Frederic Sowa | a3a116e | 2015-11-17 15:10:59 +0100 | [diff] [blame] | 2279 | if (newskb) { |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2280 | err = unix_scm_to_skb(&scm, skb, false); |
| 2281 | if (err) |
| 2282 | goto err_state_unlock; |
Hannes Frederic Sowa | a3a116e | 2015-11-17 15:10:59 +0100 | [diff] [blame] | 2283 | spin_lock(&other->sk_receive_queue.lock); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2284 | __skb_queue_tail(&other->sk_receive_queue, newskb); |
Hannes Frederic Sowa | a3a116e | 2015-11-17 15:10:59 +0100 | [diff] [blame] | 2285 | spin_unlock(&other->sk_receive_queue.lock); |
| 2286 | } |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2287 | |
| 2288 | unix_state_unlock(other); |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2289 | mutex_unlock(&unix_sk(other)->iolock); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2290 | |
| 2291 | other->sk_data_ready(other); |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2292 | scm_destroy(&scm); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2293 | return size; |
| 2294 | |
| 2295 | err_state_unlock: |
| 2296 | unix_state_unlock(other); |
| 2297 | err_unlock: |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2298 | mutex_unlock(&unix_sk(other)->iolock); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2299 | err: |
| 2300 | kfree_skb(newskb); |
| 2301 | if (send_sigpipe && !(flags & MSG_NOSIGNAL)) |
| 2302 | send_sig(SIGPIPE, current, 0); |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2303 | if (!init_scm) |
| 2304 | scm_destroy(&scm); |
Hannes Frederic Sowa | 869e7c6 | 2015-05-21 16:59:59 +0200 | [diff] [blame] | 2305 | return err; |
| 2306 | } |
| 2307 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2308 | static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg, |
| 2309 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | { |
| 2311 | int err; |
| 2312 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | err = sock_error(sk); |
| 2315 | if (err) |
| 2316 | return err; |
| 2317 | |
| 2318 | if (sk->sk_state != TCP_ESTABLISHED) |
| 2319 | return -ENOTCONN; |
| 2320 | |
| 2321 | if (msg->msg_namelen) |
| 2322 | msg->msg_namelen = 0; |
| 2323 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2324 | return unix_dgram_sendmsg(sock, msg, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | } |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2326 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2327 | static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg, |
| 2328 | size_t size, int flags) |
Eric W. Biederman | a05d2ad | 2011-04-24 01:54:57 +0000 | [diff] [blame] | 2329 | { |
| 2330 | struct sock *sk = sock->sk; |
| 2331 | |
| 2332 | if (sk->sk_state != TCP_ESTABLISHED) |
| 2333 | return -ENOTCONN; |
| 2334 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 2335 | return unix_dgram_recvmsg(sock, msg, size, flags); |
Eric W. Biederman | a05d2ad | 2011-04-24 01:54:57 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | static void unix_copy_addr(struct msghdr *msg, struct sock *sk) |
| 2339 | { |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 2340 | struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 2342 | if (addr) { |
| 2343 | msg->msg_namelen = addr->len; |
| 2344 | memcpy(msg->msg_name, addr->name, addr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | |
Cong Wang | 9825d86 | 2021-07-04 12:02:48 -0700 | [diff] [blame] | 2348 | int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, |
| 2349 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | { |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2351 | struct scm_cookie scm; |
Cong Wang | 9825d86 | 2021-07-04 12:02:48 -0700 | [diff] [blame] | 2352 | struct socket *sock = sk->sk_socket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | struct unix_sock *u = unix_sk(sk); |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2354 | struct sk_buff *skb, *last; |
| 2355 | long timeo; |
Paolo Abeni | fd69c39 | 2019-04-08 10:15:59 +0200 | [diff] [blame] | 2356 | int skip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | int err; |
| 2358 | |
| 2359 | err = -EOPNOTSUPP; |
| 2360 | if (flags&MSG_OOB) |
| 2361 | goto out; |
| 2362 | |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2363 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2365 | do { |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2366 | mutex_lock(&u->iolock); |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 2367 | |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2368 | skip = sk_peek_offset(sk, flags); |
Sabrina Dubroca | b50b058 | 2019-11-25 14:48:57 +0100 | [diff] [blame] | 2369 | skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags, |
Paolo Abeni | e427cad | 2020-02-28 14:45:22 +0100 | [diff] [blame] | 2370 | &skip, &err, &last); |
| 2371 | if (skb) { |
| 2372 | if (!(flags & MSG_PEEK)) |
| 2373 | scm_stat_del(sk, skb); |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2374 | break; |
Paolo Abeni | e427cad | 2020-02-28 14:45:22 +0100 | [diff] [blame] | 2375 | } |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2376 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2377 | mutex_unlock(&u->iolock); |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2378 | |
| 2379 | if (err != -EAGAIN) |
| 2380 | break; |
| 2381 | } while (timeo && |
Sabrina Dubroca | b50b058 | 2019-11-25 14:48:57 +0100 | [diff] [blame] | 2382 | !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue, |
| 2383 | &err, &timeo, last)); |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2384 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2385 | if (!skb) { /* implies iolock unlocked */ |
Florian Zumbiehl | 0a11225 | 2007-11-29 23:19:23 +1100 | [diff] [blame] | 2386 | unix_state_lock(sk); |
| 2387 | /* Signal EOF on disconnected non-blocking SEQPACKET socket. */ |
| 2388 | if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN && |
| 2389 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
| 2390 | err = 0; |
| 2391 | unix_state_unlock(sk); |
Rainer Weikusat | 6487428 | 2015-12-06 21:11:38 +0000 | [diff] [blame] | 2392 | goto out; |
Florian Zumbiehl | 0a11225 | 2007-11-29 23:19:23 +1100 | [diff] [blame] | 2393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
Rainer Weikusat | 77b75f4 | 2015-11-26 19:23:15 +0000 | [diff] [blame] | 2395 | if (wq_has_sleeper(&u->peer_wait)) |
| 2396 | wake_up_interruptible_sync_poll(&u->peer_wait, |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2397 | EPOLLOUT | EPOLLWRNORM | |
| 2398 | EPOLLWRBAND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | |
| 2400 | if (msg->msg_name) |
| 2401 | unix_copy_addr(msg, skb->sk); |
| 2402 | |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 2403 | if (size > skb->len - skip) |
| 2404 | size = skb->len - skip; |
| 2405 | else if (size < skb->len - skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | msg->msg_flags |= MSG_TRUNC; |
| 2407 | |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 2408 | err = skb_copy_datagram_msg(skb, skip, msg, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | if (err) |
| 2410 | goto out_free; |
| 2411 | |
Alban Crequy | 3f66116 | 2010-10-04 08:48:28 +0000 | [diff] [blame] | 2412 | if (sock_flag(sk, SOCK_RCVTSTAMP)) |
| 2413 | __sock_recv_timestamp(msg, sk, skb); |
| 2414 | |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2415 | memset(&scm, 0, sizeof(scm)); |
| 2416 | |
| 2417 | scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid); |
| 2418 | unix_set_secdata(&scm, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2420 | if (!(flags & MSG_PEEK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | if (UNIXCB(skb).fp) |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2422 | unix_detach_fds(&scm, skb); |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 2423 | |
| 2424 | sk_peek_offset_bwd(sk, skb->len); |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2425 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | /* It is questionable: on PEEK we could: |
| 2427 | - do not return fds - good, but too simple 8) |
| 2428 | - return fds, and do not return them on read (old strategy, |
| 2429 | apparently wrong) |
| 2430 | - clone fds (I chose it for now, it is the most universal |
| 2431 | solution) |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2432 | |
| 2433 | POSIX 1003.1g does not actually define this clearly |
| 2434 | at all. POSIX 1003.1g doesn't define a lot of things |
| 2435 | clearly however! |
| 2436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | */ |
Pavel Emelyanov | f55bb7f | 2012-02-21 07:31:51 +0000 | [diff] [blame] | 2438 | |
| 2439 | sk_peek_offset_fwd(sk, size); |
| 2440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | if (UNIXCB(skb).fp) |
Miklos Szeredi | cbcf011 | 2021-07-28 14:47:20 +0200 | [diff] [blame] | 2442 | unix_peek_fds(&scm, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | } |
Eric Dumazet | 9f6f9af | 2012-02-21 23:24:55 +0000 | [diff] [blame] | 2444 | err = (flags & MSG_TRUNC) ? skb->len - skip : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2446 | scm_recv(sock, msg, &scm, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | |
| 2448 | out_free: |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2449 | skb_free_datagram(sk, skb); |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2450 | mutex_unlock(&u->iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | out: |
| 2452 | return err; |
| 2453 | } |
| 2454 | |
Cong Wang | 9825d86 | 2021-07-04 12:02:48 -0700 | [diff] [blame] | 2455 | static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 2456 | int flags) |
| 2457 | { |
| 2458 | struct sock *sk = sock->sk; |
| 2459 | |
| 2460 | #ifdef CONFIG_BPF_SYSCALL |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 2461 | const struct proto *prot = READ_ONCE(sk->sk_prot); |
| 2462 | |
| 2463 | if (prot != &unix_dgram_proto) |
| 2464 | return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, |
Cong Wang | 9825d86 | 2021-07-04 12:02:48 -0700 | [diff] [blame] | 2465 | flags & ~MSG_DONTWAIT, NULL); |
| 2466 | #endif |
| 2467 | return __unix_dgram_recvmsg(sk, msg, size, flags); |
| 2468 | } |
| 2469 | |
Cong Wang | 29df44f | 2021-07-04 12:02:44 -0700 | [diff] [blame] | 2470 | static int unix_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 2471 | sk_read_actor_t recv_actor) |
| 2472 | { |
| 2473 | int copied = 0; |
| 2474 | |
| 2475 | while (1) { |
| 2476 | struct unix_sock *u = unix_sk(sk); |
| 2477 | struct sk_buff *skb; |
| 2478 | int used, err; |
| 2479 | |
| 2480 | mutex_lock(&u->iolock); |
| 2481 | skb = skb_recv_datagram(sk, 0, 1, &err); |
| 2482 | mutex_unlock(&u->iolock); |
| 2483 | if (!skb) |
| 2484 | return err; |
| 2485 | |
| 2486 | used = recv_actor(desc, skb, 0, skb->len); |
| 2487 | if (used <= 0) { |
| 2488 | if (!copied) |
| 2489 | copied = used; |
| 2490 | kfree_skb(skb); |
| 2491 | break; |
| 2492 | } else if (used <= skb->len) { |
| 2493 | copied += used; |
| 2494 | } |
| 2495 | |
| 2496 | kfree_skb(skb); |
| 2497 | if (!desc->count) |
| 2498 | break; |
| 2499 | } |
| 2500 | |
| 2501 | return copied; |
| 2502 | } |
| 2503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | /* |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2505 | * Sleep until more data has arrived. But check for races.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | */ |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2507 | static long unix_stream_data_wait(struct sock *sk, long timeo, |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2508 | struct sk_buff *last, unsigned int last_len, |
| 2509 | bool freezable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | { |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2511 | struct sk_buff *tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | DEFINE_WAIT(wait); |
| 2513 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2514 | unix_state_lock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | |
| 2516 | for (;;) { |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 2517 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2519 | tail = skb_peek_tail(&sk->sk_receive_queue); |
| 2520 | if (tail != last || |
| 2521 | (tail && tail->len != last_len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | sk->sk_err || |
| 2523 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
| 2524 | signal_pending(current) || |
| 2525 | !timeo) |
| 2526 | break; |
| 2527 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 2528 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2529 | unix_state_unlock(sk); |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2530 | if (freezable) |
| 2531 | timeo = freezable_schedule_timeout(timeo); |
| 2532 | else |
| 2533 | timeo = schedule_timeout(timeo); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2534 | unix_state_lock(sk); |
Mark Salyzyn | b48732e | 2015-05-26 08:22:19 -0700 | [diff] [blame] | 2535 | |
| 2536 | if (sock_flag(sk, SOCK_DEAD)) |
| 2537 | break; |
| 2538 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 2539 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 2542 | finish_wait(sk_sleep(sk), &wait); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 2543 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | return timeo; |
| 2545 | } |
| 2546 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2547 | static unsigned int unix_skb_len(const struct sk_buff *skb) |
| 2548 | { |
| 2549 | return skb->len - UNIXCB(skb).consumed; |
| 2550 | } |
| 2551 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2552 | struct unix_stream_read_state { |
| 2553 | int (*recv_actor)(struct sk_buff *, int, int, |
| 2554 | struct unix_stream_read_state *); |
| 2555 | struct socket *socket; |
| 2556 | struct msghdr *msg; |
| 2557 | struct pipe_inode_info *pipe; |
| 2558 | size_t size; |
| 2559 | int flags; |
| 2560 | unsigned int splice_flags; |
| 2561 | }; |
| 2562 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2563 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
| 2564 | static int unix_stream_recv_urg(struct unix_stream_read_state *state) |
| 2565 | { |
| 2566 | struct socket *sock = state->socket; |
| 2567 | struct sock *sk = sock->sk; |
| 2568 | struct unix_sock *u = unix_sk(sk); |
| 2569 | int chunk = 1; |
Rao Shoaib | 876c14a | 2021-08-11 15:06:52 -0700 | [diff] [blame] | 2570 | struct sk_buff *oob_skb; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2571 | |
Rao Shoaib | 876c14a | 2021-08-11 15:06:52 -0700 | [diff] [blame] | 2572 | mutex_lock(&u->iolock); |
| 2573 | unix_state_lock(sk); |
| 2574 | |
| 2575 | if (sock_flag(sk, SOCK_URGINLINE) || !u->oob_skb) { |
| 2576 | unix_state_unlock(sk); |
| 2577 | mutex_unlock(&u->iolock); |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2578 | return -EINVAL; |
Rao Shoaib | 876c14a | 2021-08-11 15:06:52 -0700 | [diff] [blame] | 2579 | } |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2580 | |
Rao Shoaib | 876c14a | 2021-08-11 15:06:52 -0700 | [diff] [blame] | 2581 | oob_skb = u->oob_skb; |
| 2582 | |
| 2583 | if (!(state->flags & MSG_PEEK)) { |
| 2584 | u->oob_skb = NULL; |
| 2585 | } |
| 2586 | |
| 2587 | unix_state_unlock(sk); |
| 2588 | |
| 2589 | chunk = state->recv_actor(oob_skb, 0, chunk, state); |
| 2590 | |
| 2591 | if (!(state->flags & MSG_PEEK)) { |
| 2592 | UNIXCB(oob_skb).consumed += 1; |
| 2593 | kfree_skb(oob_skb); |
| 2594 | } |
| 2595 | |
| 2596 | mutex_unlock(&u->iolock); |
| 2597 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2598 | if (chunk < 0) |
| 2599 | return -EFAULT; |
| 2600 | |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2601 | state->msg->msg_flags |= MSG_OOB; |
| 2602 | return 1; |
| 2603 | } |
| 2604 | |
| 2605 | static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk, |
| 2606 | int flags, int copied) |
| 2607 | { |
| 2608 | struct unix_sock *u = unix_sk(sk); |
| 2609 | |
| 2610 | if (!unix_skb_len(skb) && !(flags & MSG_PEEK)) { |
| 2611 | skb_unlink(skb, &sk->sk_receive_queue); |
| 2612 | consume_skb(skb); |
| 2613 | skb = NULL; |
| 2614 | } else { |
| 2615 | if (skb == u->oob_skb) { |
| 2616 | if (copied) { |
| 2617 | skb = NULL; |
| 2618 | } else if (sock_flag(sk, SOCK_URGINLINE)) { |
| 2619 | if (!(flags & MSG_PEEK)) { |
| 2620 | u->oob_skb = NULL; |
| 2621 | consume_skb(skb); |
| 2622 | } |
| 2623 | } else if (!(flags & MSG_PEEK)) { |
| 2624 | skb_unlink(skb, &sk->sk_receive_queue); |
| 2625 | consume_skb(skb); |
| 2626 | skb = skb_peek(&sk->sk_receive_queue); |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | return skb; |
| 2631 | } |
| 2632 | #endif |
| 2633 | |
Jiang Wang | 77462de | 2021-08-16 19:03:20 +0000 | [diff] [blame] | 2634 | static int unix_stream_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 2635 | sk_read_actor_t recv_actor) |
| 2636 | { |
| 2637 | if (unlikely(sk->sk_state != TCP_ESTABLISHED)) |
| 2638 | return -ENOTCONN; |
| 2639 | |
| 2640 | return unix_read_sock(sk, desc, recv_actor); |
| 2641 | } |
| 2642 | |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2643 | static int unix_stream_read_generic(struct unix_stream_read_state *state, |
| 2644 | bool freezable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | { |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2646 | struct scm_cookie scm; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2647 | struct socket *sock = state->socket; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | struct sock *sk = sock->sk; |
| 2649 | struct unix_sock *u = unix_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | int copied = 0; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2651 | int flags = state->flags; |
Eric Dumazet | de14439 | 2014-03-25 18:42:27 -0700 | [diff] [blame] | 2652 | int noblock = flags & MSG_DONTWAIT; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2653 | bool check_creds = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | int target; |
| 2655 | int err = 0; |
| 2656 | long timeo; |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2657 | int skip; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2658 | size_t size = state->size; |
| 2659 | unsigned int last_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2661 | if (unlikely(sk->sk_state != TCP_ESTABLISHED)) { |
| 2662 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | goto out; |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2666 | if (unlikely(flags & MSG_OOB)) { |
| 2667 | err = -EOPNOTSUPP; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2668 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2669 | err = unix_stream_recv_urg(state); |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2670 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | goto out; |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2674 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, size); |
Eric Dumazet | de14439 | 2014-03-25 18:42:27 -0700 | [diff] [blame] | 2675 | timeo = sock_rcvtimeo(sk, noblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2677 | memset(&scm, 0, sizeof(scm)); |
| 2678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | /* Lock the socket to prevent queue disordering |
| 2680 | * while sleeps in memcpy_tomsg |
| 2681 | */ |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2682 | mutex_lock(&u->iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | |
Matthew Dawson | a0917e0 | 2017-08-18 15:04:54 -0400 | [diff] [blame] | 2684 | skip = max(sk_peek_offset(sk, flags), 0); |
Andrey Vagin | e9193d6 | 2015-10-02 00:05:36 +0300 | [diff] [blame] | 2685 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2686 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | int chunk; |
Hannes Frederic Sowa | 73ed5d2 | 2015-11-10 16:23:15 +0100 | [diff] [blame] | 2688 | bool drop_skb; |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2689 | struct sk_buff *skb, *last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
Rainer Weikusat | 18eceb8 | 2016-02-18 12:39:46 +0000 | [diff] [blame] | 2691 | redo: |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2692 | unix_state_lock(sk); |
Mark Salyzyn | b48732e | 2015-05-26 08:22:19 -0700 | [diff] [blame] | 2693 | if (sock_flag(sk, SOCK_DEAD)) { |
| 2694 | err = -ECONNRESET; |
| 2695 | goto unlock; |
| 2696 | } |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2697 | last = skb = skb_peek(&sk->sk_receive_queue); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2698 | last_len = last ? last->len : 0; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 2699 | |
| 2700 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
| 2701 | if (skb) { |
| 2702 | skb = manage_oob(skb, sk, flags, copied); |
| 2703 | if (!skb) { |
| 2704 | unix_state_unlock(sk); |
| 2705 | if (copied) |
| 2706 | break; |
| 2707 | goto redo; |
| 2708 | } |
| 2709 | } |
| 2710 | #endif |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2711 | again: |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2712 | if (skb == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | if (copied >= target) |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2714 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | |
| 2716 | /* |
| 2717 | * POSIX 1003.1g mandates this order. |
| 2718 | */ |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 2719 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2720 | err = sock_error(sk); |
| 2721 | if (err) |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2722 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2724 | goto unlock; |
| 2725 | |
| 2726 | unix_state_unlock(sk); |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2727 | if (!timeo) { |
| 2728 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | break; |
Rainer Weikusat | 1b92ee3 | 2016-02-08 18:47:19 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2732 | mutex_unlock(&u->iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2734 | timeo = unix_stream_data_wait(sk, timeo, last, |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2735 | last_len, freezable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
Rainer Weikusat | 3822b5c | 2015-12-16 20:09:25 +0000 | [diff] [blame] | 2737 | if (signal_pending(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | err = sock_intr_errno(timeo); |
Eric Dumazet | fa0dc04 | 2016-01-24 13:53:50 -0800 | [diff] [blame] | 2739 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | goto out; |
| 2741 | } |
Rainer Weikusat | b3ca9b0 | 2011-02-28 04:50:55 +0000 | [diff] [blame] | 2742 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2743 | mutex_lock(&u->iolock); |
Rainer Weikusat | 18eceb8 | 2016-02-18 12:39:46 +0000 | [diff] [blame] | 2744 | goto redo; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2745 | unlock: |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2746 | unix_state_unlock(sk); |
| 2747 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2749 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2750 | while (skip >= unix_skb_len(skb)) { |
| 2751 | skip -= unix_skb_len(skb); |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2752 | last = skb; |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2753 | last_len = skb->len; |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2754 | skb = skb_peek_next(skb, &sk->sk_receive_queue); |
Benjamin Poirier | 79f632c | 2013-04-29 11:42:14 +0000 | [diff] [blame] | 2755 | if (!skb) |
| 2756 | goto again; |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Miklos Szeredi | 3c0d2f3 | 2007-06-05 13:10:29 -0700 | [diff] [blame] | 2759 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | |
| 2761 | if (check_creds) { |
| 2762 | /* Never glue messages from different writers */ |
Hannes Frederic Sowa | 9490f88 | 2015-11-26 12:08:18 +0100 | [diff] [blame] | 2763 | if (!unix_skb_scm_eq(skb, &scm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | break; |
Eric W. Biederman | 0e82e7f6d | 2013-04-03 16:14:47 +0000 | [diff] [blame] | 2765 | } else if (test_bit(SOCK_PASSCRED, &sock->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | /* Copy credentials */ |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2767 | scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid); |
Stephen Smalley | 37a9a8d | 2015-06-10 08:44:59 -0400 | [diff] [blame] | 2768 | unix_set_secdata(&scm, skb); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2769 | check_creds = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | /* Copy address just once */ |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2773 | if (state->msg && state->msg->msg_name) { |
| 2774 | DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, |
| 2775 | state->msg->msg_name); |
| 2776 | unix_copy_addr(state->msg, skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | sunaddr = NULL; |
| 2778 | } |
| 2779 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2780 | chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size); |
Hannes Frederic Sowa | 73ed5d2 | 2015-11-10 16:23:15 +0100 | [diff] [blame] | 2781 | skb_get(skb); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2782 | chunk = state->recv_actor(skb, skip, chunk, state); |
Hannes Frederic Sowa | 73ed5d2 | 2015-11-10 16:23:15 +0100 | [diff] [blame] | 2783 | drop_skb = !unix_skb_len(skb); |
| 2784 | /* skb is only safe to use if !drop_skb */ |
| 2785 | consume_skb(skb); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2786 | if (chunk < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | if (copied == 0) |
| 2788 | copied = -EFAULT; |
| 2789 | break; |
| 2790 | } |
| 2791 | copied += chunk; |
| 2792 | size -= chunk; |
| 2793 | |
Hannes Frederic Sowa | 73ed5d2 | 2015-11-10 16:23:15 +0100 | [diff] [blame] | 2794 | if (drop_skb) { |
| 2795 | /* the skb was touched by a concurrent reader; |
| 2796 | * we should not expect anything from this skb |
| 2797 | * anymore and assume it invalid - we can be |
| 2798 | * sure it was dropped from the socket queue |
| 2799 | * |
| 2800 | * let's report a short read |
| 2801 | */ |
| 2802 | err = 0; |
| 2803 | break; |
| 2804 | } |
| 2805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | /* Mark read part of skb as used */ |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2807 | if (!(flags & MSG_PEEK)) { |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2808 | UNIXCB(skb).consumed += chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2810 | sk_peek_offset_bwd(sk, chunk); |
| 2811 | |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 2812 | if (UNIXCB(skb).fp) { |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 2813 | scm_stat_del(sk, skb); |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2814 | unix_detach_fds(&scm, skb); |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 2815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2817 | if (unix_skb_len(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | |
Eric Dumazet | 6f01fd6 | 2012-01-28 16:11:03 +0000 | [diff] [blame] | 2820 | skb_unlink(skb, &sk->sk_receive_queue); |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 2821 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
Christoph Hellwig | 7cc0566 | 2015-01-28 18:04:53 +0100 | [diff] [blame] | 2823 | if (scm.fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | break; |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 2825 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | /* It is questionable, see note in unix_dgram_recvmsg. |
| 2827 | */ |
| 2828 | if (UNIXCB(skb).fp) |
Miklos Szeredi | cbcf011 | 2021-07-28 14:47:20 +0200 | [diff] [blame] | 2829 | unix_peek_fds(&scm, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | |
Andrey Vagin | e9193d6 | 2015-10-02 00:05:36 +0300 | [diff] [blame] | 2831 | sk_peek_offset_fwd(sk, chunk); |
Pavel Emelyanov | fc0d753 | 2012-02-21 07:32:06 +0000 | [diff] [blame] | 2832 | |
Aaron Conole | 9f389e3 | 2015-09-26 18:50:43 -0400 | [diff] [blame] | 2833 | if (UNIXCB(skb).fp) |
| 2834 | break; |
| 2835 | |
Andrey Vagin | e9193d6 | 2015-10-02 00:05:36 +0300 | [diff] [blame] | 2836 | skip = 0; |
Aaron Conole | 9f389e3 | 2015-09-26 18:50:43 -0400 | [diff] [blame] | 2837 | last = skb; |
| 2838 | last_len = skb->len; |
| 2839 | unix_state_lock(sk); |
| 2840 | skb = skb_peek_next(skb, &sk->sk_receive_queue); |
| 2841 | if (skb) |
| 2842 | goto again; |
| 2843 | unix_state_unlock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | break; |
| 2845 | } |
| 2846 | } while (size); |
| 2847 | |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 2848 | mutex_unlock(&u->iolock); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2849 | if (state->msg) |
| 2850 | scm_recv(sock, state->msg, &scm, flags); |
| 2851 | else |
| 2852 | scm_destroy(&scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | out: |
| 2854 | return copied ? : err; |
| 2855 | } |
| 2856 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2857 | static int unix_stream_read_actor(struct sk_buff *skb, |
| 2858 | int skip, int chunk, |
| 2859 | struct unix_stream_read_state *state) |
| 2860 | { |
| 2861 | int ret; |
| 2862 | |
| 2863 | ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip, |
| 2864 | state->msg, chunk); |
| 2865 | return ret ?: chunk; |
| 2866 | } |
| 2867 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 2868 | int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, |
| 2869 | size_t size, int flags) |
| 2870 | { |
| 2871 | struct unix_stream_read_state state = { |
| 2872 | .recv_actor = unix_stream_read_actor, |
| 2873 | .socket = sk->sk_socket, |
| 2874 | .msg = msg, |
| 2875 | .size = size, |
| 2876 | .flags = flags |
| 2877 | }; |
| 2878 | |
| 2879 | return unix_stream_read_generic(&state, true); |
| 2880 | } |
| 2881 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2882 | static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg, |
| 2883 | size_t size, int flags) |
| 2884 | { |
| 2885 | struct unix_stream_read_state state = { |
| 2886 | .recv_actor = unix_stream_read_actor, |
| 2887 | .socket = sock, |
| 2888 | .msg = msg, |
| 2889 | .size = size, |
| 2890 | .flags = flags |
| 2891 | }; |
| 2892 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 2893 | #ifdef CONFIG_BPF_SYSCALL |
| 2894 | struct sock *sk = sock->sk; |
| 2895 | const struct proto *prot = READ_ONCE(sk->sk_prot); |
| 2896 | |
| 2897 | if (prot != &unix_stream_proto) |
| 2898 | return prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, |
| 2899 | flags & ~MSG_DONTWAIT, NULL); |
| 2900 | #endif |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2901 | return unix_stream_read_generic(&state, true); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2904 | static int unix_stream_splice_actor(struct sk_buff *skb, |
| 2905 | int skip, int chunk, |
| 2906 | struct unix_stream_read_state *state) |
| 2907 | { |
| 2908 | return skb_splice_bits(skb, state->socket->sk, |
| 2909 | UNIXCB(skb).consumed + skip, |
Al Viro | 2586926 | 2016-09-17 21:02:10 -0400 | [diff] [blame] | 2910 | state->pipe, chunk, state->splice_flags); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos, |
| 2914 | struct pipe_inode_info *pipe, |
| 2915 | size_t size, unsigned int flags) |
| 2916 | { |
| 2917 | struct unix_stream_read_state state = { |
| 2918 | .recv_actor = unix_stream_splice_actor, |
| 2919 | .socket = sock, |
| 2920 | .pipe = pipe, |
| 2921 | .size = size, |
| 2922 | .splice_flags = flags, |
| 2923 | }; |
| 2924 | |
| 2925 | if (unlikely(*ppos)) |
| 2926 | return -ESPIPE; |
| 2927 | |
| 2928 | if (sock->file->f_flags & O_NONBLOCK || |
| 2929 | flags & SPLICE_F_NONBLOCK) |
| 2930 | state.flags = MSG_DONTWAIT; |
| 2931 | |
WANG Cong | 06a77b0 | 2016-11-17 15:55:26 -0800 | [diff] [blame] | 2932 | return unix_stream_read_generic(&state, false); |
Hannes Frederic Sowa | 2b51457 | 2015-05-21 17:00:01 +0200 | [diff] [blame] | 2933 | } |
| 2934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | static int unix_shutdown(struct socket *sock, int mode) |
| 2936 | { |
| 2937 | struct sock *sk = sock->sk; |
| 2938 | struct sock *other; |
| 2939 | |
Xi Wang | fc61b92 | 2012-08-26 16:47:13 +0000 | [diff] [blame] | 2940 | if (mode < SHUT_RD || mode > SHUT_RDWR) |
| 2941 | return -EINVAL; |
| 2942 | /* This maps: |
| 2943 | * SHUT_RD (0) -> RCV_SHUTDOWN (1) |
| 2944 | * SHUT_WR (1) -> SEND_SHUTDOWN (2) |
| 2945 | * SHUT_RDWR (2) -> SHUTDOWN_MASK (3) |
| 2946 | */ |
| 2947 | ++mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2949 | unix_state_lock(sk); |
| 2950 | sk->sk_shutdown |= mode; |
| 2951 | other = unix_peer(sk); |
| 2952 | if (other) |
| 2953 | sock_hold(other); |
| 2954 | unix_state_unlock(sk); |
| 2955 | sk->sk_state_change(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2957 | if (other && |
| 2958 | (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2960 | int peer_mode = 0; |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 2961 | const struct proto *prot = READ_ONCE(other->sk_prot); |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2962 | |
Jiang Wang | d359902 | 2021-08-21 18:07:36 +0000 | [diff] [blame] | 2963 | if (prot->unhash) |
| 2964 | prot->unhash(other); |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2965 | if (mode&RCV_SHUTDOWN) |
| 2966 | peer_mode |= SEND_SHUTDOWN; |
| 2967 | if (mode&SEND_SHUTDOWN) |
| 2968 | peer_mode |= RCV_SHUTDOWN; |
| 2969 | unix_state_lock(other); |
| 2970 | other->sk_shutdown |= peer_mode; |
| 2971 | unix_state_unlock(other); |
| 2972 | other->sk_state_change(other); |
Jiang Wang | d0c6416b | 2021-10-04 23:25:28 +0000 | [diff] [blame] | 2973 | if (peer_mode == SHUTDOWN_MASK) |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2974 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP); |
Jiang Wang | d0c6416b | 2021-10-04 23:25:28 +0000 | [diff] [blame] | 2975 | else if (peer_mode & RCV_SHUTDOWN) |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2976 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | } |
Alban Crequy | 7180a03 | 2011-01-19 04:56:36 +0000 | [diff] [blame] | 2978 | if (other) |
| 2979 | sock_put(other); |
| 2980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | return 0; |
| 2982 | } |
| 2983 | |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 2984 | long unix_inq_len(struct sock *sk) |
| 2985 | { |
| 2986 | struct sk_buff *skb; |
| 2987 | long amount = 0; |
| 2988 | |
| 2989 | if (sk->sk_state == TCP_LISTEN) |
| 2990 | return -EINVAL; |
| 2991 | |
| 2992 | spin_lock(&sk->sk_receive_queue.lock); |
| 2993 | if (sk->sk_type == SOCK_STREAM || |
| 2994 | sk->sk_type == SOCK_SEQPACKET) { |
| 2995 | skb_queue_walk(&sk->sk_receive_queue, skb) |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 2996 | amount += unix_skb_len(skb); |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 2997 | } else { |
| 2998 | skb = skb_peek(&sk->sk_receive_queue); |
| 2999 | if (skb) |
| 3000 | amount = skb->len; |
| 3001 | } |
| 3002 | spin_unlock(&sk->sk_receive_queue.lock); |
| 3003 | |
| 3004 | return amount; |
| 3005 | } |
| 3006 | EXPORT_SYMBOL_GPL(unix_inq_len); |
| 3007 | |
| 3008 | long unix_outq_len(struct sock *sk) |
| 3009 | { |
| 3010 | return sk_wmem_alloc_get(sk); |
| 3011 | } |
| 3012 | EXPORT_SYMBOL_GPL(unix_outq_len); |
| 3013 | |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 3014 | static int unix_open_file(struct sock *sk) |
| 3015 | { |
| 3016 | struct path path; |
| 3017 | struct file *f; |
| 3018 | int fd; |
| 3019 | |
| 3020 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
| 3021 | return -EPERM; |
| 3022 | |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 3023 | if (!smp_load_acquire(&unix_sk(sk)->addr)) |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 3024 | return -ENOENT; |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 3025 | |
| 3026 | path = unix_sk(sk)->path; |
| 3027 | if (!path.dentry) |
| 3028 | return -ENOENT; |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 3029 | |
| 3030 | path_get(&path); |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 3031 | |
| 3032 | fd = get_unused_fd_flags(O_CLOEXEC); |
| 3033 | if (fd < 0) |
| 3034 | goto out; |
| 3035 | |
| 3036 | f = dentry_open(&path, O_PATH, current_cred()); |
| 3037 | if (IS_ERR(f)) { |
| 3038 | put_unused_fd(fd); |
| 3039 | fd = PTR_ERR(f); |
| 3040 | goto out; |
| 3041 | } |
| 3042 | |
| 3043 | fd_install(fd, f); |
| 3044 | out: |
| 3045 | path_put(&path); |
| 3046 | |
| 3047 | return fd; |
| 3048 | } |
| 3049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 3051 | { |
| 3052 | struct sock *sk = sock->sk; |
Jianjun Kong | e27dfce | 2008-11-01 21:38:31 -0700 | [diff] [blame] | 3053 | long amount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | int err; |
| 3055 | |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 3056 | switch (cmd) { |
| 3057 | case SIOCOUTQ: |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 3058 | amount = unix_outq_len(sk); |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 3059 | err = put_user(amount, (int __user *)arg); |
| 3060 | break; |
| 3061 | case SIOCINQ: |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 3062 | amount = unix_inq_len(sk); |
| 3063 | if (amount < 0) |
| 3064 | err = amount; |
| 3065 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | err = put_user(amount, (int __user *)arg); |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 3067 | break; |
Andrey Vagin | ba94f30 | 2017-02-01 11:00:45 -0800 | [diff] [blame] | 3068 | case SIOCUNIXFILE: |
| 3069 | err = unix_open_file(sk); |
| 3070 | break; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 3071 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
| 3072 | case SIOCATMARK: |
| 3073 | { |
| 3074 | struct sk_buff *skb; |
| 3075 | struct unix_sock *u = unix_sk(sk); |
| 3076 | int answ = 0; |
| 3077 | |
| 3078 | skb = skb_peek(&sk->sk_receive_queue); |
| 3079 | if (skb && skb == u->oob_skb) |
| 3080 | answ = 1; |
| 3081 | err = put_user(answ, (int __user *)arg); |
| 3082 | } |
| 3083 | break; |
| 3084 | #endif |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 3085 | default: |
| 3086 | err = -ENOIOCTLCMD; |
| 3087 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | } |
| 3089 | return err; |
| 3090 | } |
| 3091 | |
Arnd Bergmann | 5f6beb9 | 2019-06-03 22:03:44 +0200 | [diff] [blame] | 3092 | #ifdef CONFIG_COMPAT |
| 3093 | static int unix_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 3094 | { |
| 3095 | return unix_ioctl(sock, cmd, (unsigned long)compat_ptr(arg)); |
| 3096 | } |
| 3097 | #endif |
| 3098 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3099 | static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | { |
| 3101 | struct sock *sk = sock->sk; |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3102 | __poll_t mask; |
| 3103 | |
Karsten Graul | 89ab066 | 2018-10-23 13:40:39 +0200 | [diff] [blame] | 3104 | sock_poll_wait(file, sock, wait); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3105 | mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | /* exceptional events? */ |
| 3108 | if (sk->sk_err) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3109 | mask |= EPOLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3111 | mask |= EPOLLHUP; |
Davide Libenzi | f348d70 | 2006-03-25 03:07:39 -0800 | [diff] [blame] | 3112 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3113 | mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
| 3115 | /* readable? */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 3116 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3117 | mask |= EPOLLIN | EPOLLRDNORM; |
Cong Wang | af49338 | 2021-10-08 13:33:05 -0700 | [diff] [blame] | 3118 | if (sk_is_readable(sk)) |
| 3119 | mask |= EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | |
| 3121 | /* Connection-based need to check for termination and startup */ |
Eric Dumazet | 6eba6a3 | 2008-11-16 22:58:44 -0800 | [diff] [blame] | 3122 | if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && |
| 3123 | sk->sk_state == TCP_CLOSE) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3124 | mask |= EPOLLHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | |
| 3126 | /* |
| 3127 | * we set writable also when the other side has shut down the |
| 3128 | * connection. This prevents stuck sockets. |
| 3129 | */ |
| 3130 | if (unix_writable(sk)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3131 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | |
| 3133 | return mask; |
| 3134 | } |
| 3135 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3136 | static __poll_t unix_dgram_poll(struct file *file, struct socket *sock, |
| 3137 | poll_table *wait) |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3138 | { |
Rainer Weikusat | ec0d215 | 2008-06-27 19:34:18 -0700 | [diff] [blame] | 3139 | struct sock *sk = sock->sk, *other; |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3140 | unsigned int writable; |
| 3141 | __poll_t mask; |
| 3142 | |
Karsten Graul | 89ab066 | 2018-10-23 13:40:39 +0200 | [diff] [blame] | 3143 | sock_poll_wait(file, sock, wait); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3144 | mask = 0; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3145 | |
| 3146 | /* exceptional events? */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 3147 | if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3148 | mask |= EPOLLERR | |
| 3149 | (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0); |
Keller, Jacob E | 7d4c04f | 2013-03-28 11:19:25 +0000 | [diff] [blame] | 3150 | |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3151 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3152 | mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3153 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3154 | mask |= EPOLLHUP; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3155 | |
| 3156 | /* readable? */ |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 3157 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3158 | mask |= EPOLLIN | EPOLLRDNORM; |
Cong Wang | af49338 | 2021-10-08 13:33:05 -0700 | [diff] [blame] | 3159 | if (sk_is_readable(sk)) |
| 3160 | mask |= EPOLLIN | EPOLLRDNORM; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3161 | |
| 3162 | /* Connection-based need to check for termination and startup */ |
| 3163 | if (sk->sk_type == SOCK_SEQPACKET) { |
| 3164 | if (sk->sk_state == TCP_CLOSE) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3165 | mask |= EPOLLHUP; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3166 | /* connection hasn't started yet? */ |
| 3167 | if (sk->sk_state == TCP_SYN_SENT) |
| 3168 | return mask; |
| 3169 | } |
| 3170 | |
Eric Dumazet | 973a34a | 2010-10-31 05:38:25 +0000 | [diff] [blame] | 3171 | /* No write status requested, avoid expensive OUT tests. */ |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3172 | if (!(poll_requested_events(wait) & (EPOLLWRBAND|EPOLLWRNORM|EPOLLOUT))) |
Eric Dumazet | 973a34a | 2010-10-31 05:38:25 +0000 | [diff] [blame] | 3173 | return mask; |
| 3174 | |
Rainer Weikusat | ec0d215 | 2008-06-27 19:34:18 -0700 | [diff] [blame] | 3175 | writable = unix_writable(sk); |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 3176 | if (writable) { |
| 3177 | unix_state_lock(sk); |
| 3178 | |
| 3179 | other = unix_peer(sk); |
| 3180 | if (other && unix_peer(other) != sk && |
Eric Dumazet | 04f08eb | 2021-09-08 17:00:29 -0700 | [diff] [blame] | 3181 | unix_recvq_full_lockless(other) && |
Rainer Weikusat | 7d26727 | 2015-11-20 22:07:23 +0000 | [diff] [blame] | 3182 | unix_dgram_peer_wake_me(sk, other)) |
| 3183 | writable = 0; |
| 3184 | |
| 3185 | unix_state_unlock(sk); |
Rainer Weikusat | ec0d215 | 2008-06-27 19:34:18 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | if (writable) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 3189 | mask |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND; |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3190 | else |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 3191 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3192 | |
Rainer Weikusat | 3c73419 | 2008-06-17 22:28:05 -0700 | [diff] [blame] | 3193 | return mask; |
| 3194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | |
| 3196 | #ifdef CONFIG_PROC_FS |
Pavel Emelyanov | a53eb3f | 2007-11-23 20:30:01 +0800 | [diff] [blame] | 3197 | |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3198 | #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1) |
| 3199 | |
| 3200 | #define get_bucket(x) ((x) >> BUCKET_SPACE) |
| 3201 | #define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1)) |
| 3202 | #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o)) |
Pavel Emelyanov | a53eb3f | 2007-11-23 20:30:01 +0800 | [diff] [blame] | 3203 | |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3204 | static struct sock *unix_from_bucket(struct seq_file *seq, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | { |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3206 | unsigned long offset = get_offset(*pos); |
| 3207 | unsigned long bucket = get_bucket(*pos); |
| 3208 | struct sock *sk; |
| 3209 | unsigned long count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3211 | for (sk = sk_head(&unix_socket_table[bucket]); sk; sk = sk_next(sk)) { |
| 3212 | if (sock_net(sk) != seq_file_net(seq)) |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3213 | continue; |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3214 | if (++count == offset) |
| 3215 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | } |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3217 | |
| 3218 | return sk; |
| 3219 | } |
| 3220 | |
| 3221 | static struct sock *unix_next_socket(struct seq_file *seq, |
| 3222 | struct sock *sk, |
| 3223 | loff_t *pos) |
| 3224 | { |
| 3225 | unsigned long bucket; |
| 3226 | |
| 3227 | while (sk > (struct sock *)SEQ_START_TOKEN) { |
| 3228 | sk = sk_next(sk); |
| 3229 | if (!sk) |
| 3230 | goto next_bucket; |
| 3231 | if (sock_net(sk) == seq_file_net(seq)) |
| 3232 | return sk; |
| 3233 | } |
| 3234 | |
| 3235 | do { |
| 3236 | sk = unix_from_bucket(seq, pos); |
| 3237 | if (sk) |
| 3238 | return sk; |
| 3239 | |
| 3240 | next_bucket: |
| 3241 | bucket = get_bucket(*pos) + 1; |
| 3242 | *pos = set_bucket_offset(bucket, 1); |
| 3243 | } while (bucket < ARRAY_SIZE(unix_socket_table)); |
| 3244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | return NULL; |
| 3246 | } |
| 3247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | static void *unix_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 3249 | __acquires(unix_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | { |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 3251 | spin_lock(&unix_table_lock); |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3252 | |
| 3253 | if (!*pos) |
| 3254 | return SEQ_START_TOKEN; |
| 3255 | |
| 3256 | if (get_bucket(*pos) >= ARRAY_SIZE(unix_socket_table)) |
| 3257 | return NULL; |
| 3258 | |
| 3259 | return unix_next_socket(seq, NULL, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 3263 | { |
| 3264 | ++*pos; |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 3265 | return unix_next_socket(seq, v, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | static void unix_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 3269 | __releases(unix_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | { |
David S. Miller | fbe9cc4 | 2005-12-13 23:26:29 -0800 | [diff] [blame] | 3271 | spin_unlock(&unix_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | static int unix_seq_show(struct seq_file *seq, void *v) |
| 3275 | { |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 3276 | |
Joe Perches | b9f3124 | 2008-04-12 19:04:38 -0700 | [diff] [blame] | 3277 | if (v == SEQ_START_TOKEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | seq_puts(seq, "Num RefCount Protocol Flags Type St " |
| 3279 | "Inode Path\n"); |
| 3280 | else { |
| 3281 | struct sock *s = v; |
| 3282 | struct unix_sock *u = unix_sk(s); |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 3283 | unix_state_lock(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | |
Dan Rosenberg | 71338aa | 2011-05-23 12:17:35 +0000 | [diff] [blame] | 3285 | seq_printf(seq, "%pK: %08X %08X %08X %04X %02X %5lu", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | s, |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 3287 | refcount_read(&s->sk_refcnt), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | 0, |
| 3289 | s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0, |
| 3290 | s->sk_type, |
| 3291 | s->sk_socket ? |
| 3292 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) : |
| 3293 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING), |
| 3294 | sock_i_ino(s)); |
| 3295 | |
Al Viro | ae3b564 | 2019-02-15 20:09:35 +0000 | [diff] [blame] | 3296 | if (u->addr) { // under unix_table_lock here |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | int i, len; |
| 3298 | seq_putc(seq, ' '); |
| 3299 | |
| 3300 | i = 0; |
Kuniyuki Iwashima | 755662c | 2021-11-24 11:14:19 +0900 | [diff] [blame] | 3301 | len = u->addr->len - |
| 3302 | offsetof(struct sockaddr_un, sun_path); |
Kuniyuki Iwashima | 5ce7ab4 | 2021-11-24 11:14:27 +0900 | [diff] [blame] | 3303 | if (u->addr->name->sun_path[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | len--; |
Kuniyuki Iwashima | 5ce7ab4 | 2021-11-24 11:14:27 +0900 | [diff] [blame] | 3305 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | seq_putc(seq, '@'); |
| 3307 | i++; |
| 3308 | } |
| 3309 | for ( ; i < len; i++) |
Isaac Boukris | e7947ea | 2016-11-01 02:41:35 +0200 | [diff] [blame] | 3310 | seq_putc(seq, u->addr->name->sun_path[i] ?: |
| 3311 | '@'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | } |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 3313 | unix_state_unlock(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | seq_putc(seq, '\n'); |
| 3315 | } |
| 3316 | |
| 3317 | return 0; |
| 3318 | } |
| 3319 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 3320 | static const struct seq_operations unix_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | .start = unix_seq_start, |
| 3322 | .next = unix_seq_next, |
| 3323 | .stop = unix_seq_stop, |
| 3324 | .show = unix_seq_show, |
| 3325 | }; |
Kuniyuki Iwashima | 2c860a4 | 2021-08-14 10:57:15 +0900 | [diff] [blame] | 3326 | |
| 3327 | #if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) |
| 3328 | struct bpf_iter__unix { |
| 3329 | __bpf_md_ptr(struct bpf_iter_meta *, meta); |
| 3330 | __bpf_md_ptr(struct unix_sock *, unix_sk); |
| 3331 | uid_t uid __aligned(8); |
| 3332 | }; |
| 3333 | |
| 3334 | static int unix_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta, |
| 3335 | struct unix_sock *unix_sk, uid_t uid) |
| 3336 | { |
| 3337 | struct bpf_iter__unix ctx; |
| 3338 | |
| 3339 | meta->seq_num--; /* skip SEQ_START_TOKEN */ |
| 3340 | ctx.meta = meta; |
| 3341 | ctx.unix_sk = unix_sk; |
| 3342 | ctx.uid = uid; |
| 3343 | return bpf_iter_run_prog(prog, &ctx); |
| 3344 | } |
| 3345 | |
| 3346 | static int bpf_iter_unix_seq_show(struct seq_file *seq, void *v) |
| 3347 | { |
| 3348 | struct bpf_iter_meta meta; |
| 3349 | struct bpf_prog *prog; |
| 3350 | struct sock *sk = v; |
| 3351 | uid_t uid; |
| 3352 | |
| 3353 | if (v == SEQ_START_TOKEN) |
| 3354 | return 0; |
| 3355 | |
| 3356 | uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)); |
| 3357 | meta.seq = seq; |
| 3358 | prog = bpf_iter_get_info(&meta, false); |
| 3359 | return unix_prog_seq_show(prog, &meta, v, uid); |
| 3360 | } |
| 3361 | |
| 3362 | static void bpf_iter_unix_seq_stop(struct seq_file *seq, void *v) |
| 3363 | { |
| 3364 | struct bpf_iter_meta meta; |
| 3365 | struct bpf_prog *prog; |
| 3366 | |
| 3367 | if (!v) { |
| 3368 | meta.seq = seq; |
| 3369 | prog = bpf_iter_get_info(&meta, true); |
| 3370 | if (prog) |
| 3371 | (void)unix_prog_seq_show(prog, &meta, v, 0); |
| 3372 | } |
| 3373 | |
| 3374 | unix_seq_stop(seq, v); |
| 3375 | } |
| 3376 | |
| 3377 | static const struct seq_operations bpf_iter_unix_seq_ops = { |
| 3378 | .start = unix_seq_start, |
| 3379 | .next = unix_seq_next, |
| 3380 | .stop = bpf_iter_unix_seq_stop, |
| 3381 | .show = bpf_iter_unix_seq_show, |
| 3382 | }; |
| 3383 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | #endif |
| 3385 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 3386 | static const struct net_proto_family unix_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | .family = PF_UNIX, |
| 3388 | .create = unix_create, |
| 3389 | .owner = THIS_MODULE, |
| 3390 | }; |
| 3391 | |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3392 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3393 | static int __net_init unix_net_init(struct net *net) |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3394 | { |
| 3395 | int error = -ENOMEM; |
| 3396 | |
Denis V. Lunev | a0a53c8 | 2007-12-11 04:19:17 -0800 | [diff] [blame] | 3397 | net->unx.sysctl_max_dgram_qlen = 10; |
Pavel Emelyanov | 1597fbc | 2007-12-01 23:51:01 +1100 | [diff] [blame] | 3398 | if (unix_sysctl_register(net)) |
| 3399 | goto out; |
Pavel Emelyanov | d392e49 | 2007-12-01 23:44:15 +1100 | [diff] [blame] | 3400 | |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3401 | #ifdef CONFIG_PROC_FS |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 3402 | if (!proc_create_net("unix", 0, net->proc_net, &unix_seq_ops, |
| 3403 | sizeof(struct seq_net_private))) { |
Pavel Emelyanov | 1597fbc | 2007-12-01 23:51:01 +1100 | [diff] [blame] | 3404 | unix_sysctl_unregister(net); |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3405 | goto out; |
Pavel Emelyanov | 1597fbc | 2007-12-01 23:51:01 +1100 | [diff] [blame] | 3406 | } |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3407 | #endif |
| 3408 | error = 0; |
| 3409 | out: |
Jianjun Kong | 48dcc33e | 2008-11-01 21:37:27 -0700 | [diff] [blame] | 3410 | return error; |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3411 | } |
| 3412 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 3413 | static void __net_exit unix_net_exit(struct net *net) |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3414 | { |
Pavel Emelyanov | 1597fbc | 2007-12-01 23:51:01 +1100 | [diff] [blame] | 3415 | unix_sysctl_unregister(net); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 3416 | remove_proc_entry("unix", net->proc_net); |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | static struct pernet_operations unix_net_ops = { |
| 3420 | .init = unix_net_init, |
| 3421 | .exit = unix_net_exit, |
| 3422 | }; |
| 3423 | |
Kuniyuki Iwashima | 2c860a4 | 2021-08-14 10:57:15 +0900 | [diff] [blame] | 3424 | #if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 3425 | DEFINE_BPF_ITER_FUNC(unix, struct bpf_iter_meta *meta, |
| 3426 | struct unix_sock *unix_sk, uid_t uid) |
| 3427 | |
| 3428 | static const struct bpf_iter_seq_info unix_seq_info = { |
| 3429 | .seq_ops = &bpf_iter_unix_seq_ops, |
| 3430 | .init_seq_private = bpf_iter_init_seq_net, |
| 3431 | .fini_seq_private = bpf_iter_fini_seq_net, |
| 3432 | .seq_priv_size = sizeof(struct seq_net_private), |
| 3433 | }; |
| 3434 | |
| 3435 | static struct bpf_iter_reg unix_reg_info = { |
| 3436 | .target = "unix", |
| 3437 | .ctx_arg_info_size = 1, |
| 3438 | .ctx_arg_info = { |
| 3439 | { offsetof(struct bpf_iter__unix, unix_sk), |
| 3440 | PTR_TO_BTF_ID_OR_NULL }, |
| 3441 | }, |
| 3442 | .seq_info = &unix_seq_info, |
| 3443 | }; |
| 3444 | |
| 3445 | static void __init bpf_iter_register(void) |
| 3446 | { |
| 3447 | unix_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UNIX]; |
| 3448 | if (bpf_iter_reg_target(&unix_reg_info)) |
| 3449 | pr_warn("Warning: could not register bpf iterator unix\n"); |
| 3450 | } |
| 3451 | #endif |
| 3452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | static int __init af_unix_init(void) |
| 3454 | { |
| 3455 | int rc = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 3457 | BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 3459 | rc = proto_register(&unix_dgram_proto, 1); |
| 3460 | if (rc != 0) { |
| 3461 | pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__); |
| 3462 | goto out; |
| 3463 | } |
| 3464 | |
| 3465 | rc = proto_register(&unix_stream_proto, 1); |
YOSHIFUJI Hideaki | ac7bfa6 | 2007-02-09 23:25:23 +0900 | [diff] [blame] | 3466 | if (rc != 0) { |
wangweidong | 5cc208b | 2013-12-06 18:03:36 +0800 | [diff] [blame] | 3467 | pr_crit("%s: Cannot create unix_sock SLAB cache!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | goto out; |
| 3469 | } |
| 3470 | |
| 3471 | sock_register(&unix_family_ops); |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3472 | register_pernet_subsys(&unix_net_ops); |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 3473 | unix_bpf_build_proto(); |
Kuniyuki Iwashima | 2c860a4 | 2021-08-14 10:57:15 +0900 | [diff] [blame] | 3474 | |
| 3475 | #if IS_BUILTIN(CONFIG_UNIX) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) |
| 3476 | bpf_iter_register(); |
| 3477 | #endif |
| 3478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | out: |
| 3480 | return rc; |
| 3481 | } |
| 3482 | |
| 3483 | static void __exit af_unix_exit(void) |
| 3484 | { |
| 3485 | sock_unregister(PF_UNIX); |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 3486 | proto_unregister(&unix_dgram_proto); |
| 3487 | proto_unregister(&unix_stream_proto); |
Denis V. Lunev | 097e66c | 2007-11-19 22:29:30 -0800 | [diff] [blame] | 3488 | unregister_pernet_subsys(&unix_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | } |
| 3490 | |
David Woodhouse | 3d36696 | 2008-04-24 00:59:25 -0700 | [diff] [blame] | 3491 | /* Earlier than device_initcall() so that other drivers invoking |
| 3492 | request_module() don't end up in a loop when modprobe tries |
| 3493 | to use a UNIX socket. But later than subsys_initcall() because |
| 3494 | we depend on stuff initialised there */ |
| 3495 | fs_initcall(af_unix_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | module_exit(af_unix_exit); |
| 3497 | |
| 3498 | MODULE_LICENSE("GPL"); |
| 3499 | MODULE_ALIAS_NETPROTO(PF_UNIX); |