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