1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NET An implementation of the SOCKET network access protocol. 4 * 5 * Version: @(#)socket.c 1.1.93 18/02/95 6 * 7 * Authors: Orest Zborowski, <obz@Kodak.COM> 8 * Ross Biro 9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 10 * 11 * Fixes: 12 * Anonymous : NOTSOCK/BADF cleanup. Error fix in 13 * shutdown() 14 * Alan Cox : verify_area() fixes 15 * Alan Cox : Removed DDI 16 * Jonathan Kamens : SOCK_DGRAM reconnect bug 17 * Alan Cox : Moved a load of checks to the very 18 * top level. 19 * Alan Cox : Move address structures to/from user 20 * mode above the protocol layers. 21 * Rob Janssen : Allow 0 length sends. 22 * Alan Cox : Asynchronous I/O support (cribbed from the 23 * tty drivers). 24 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style) 25 * Jeff Uphoff : Made max number of sockets command-line 26 * configurable. 27 * Matti Aarnio : Made the number of sockets dynamic, 28 * to be allocated when needed, and mr. 29 * Uphoff's max is used as max to be 30 * allowed to allocate. 31 * Linus : Argh. removed all the socket allocation 32 * altogether: it's in the inode now. 33 * Alan Cox : Made sock_alloc()/sock_release() public 34 * for NetROM and future kernel nfsd type 35 * stuff. 36 * Alan Cox : sendmsg/recvmsg basics. 37 * Tom Dyas : Export net symbols. 38 * Marcin Dalecki : Fixed problems with CONFIG_NET="n". 39 * Alan Cox : Added thread locking to sys_* calls 40 * for sockets. May have errors at the 41 * moment. 42 * Kevin Buhr : Fixed the dumb errors in the above. 43 * Andi Kleen : Some small cleanups, optimizations, 44 * and fixed a copy_from_user() bug. 45 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0) 46 * Tigran Aivazian : Made listen(2) backlog sanity checks 47 * protocol-independent 48 * 49 * This module is effectively the top level interface to the BSD socket 50 * paradigm. 51 * 52 * Based upon Swansea University Computer Society NET3.039 53 */ 54 55 #include <linux/bpf-cgroup.h> 56 #include <linux/ethtool.h> 57 #include <linux/mm.h> 58 #include <linux/socket.h> 59 #include <linux/file.h> 60 #include <linux/splice.h> 61 #include <linux/net.h> 62 #include <linux/interrupt.h> 63 #include <linux/thread_info.h> 64 #include <linux/rcupdate.h> 65 #include <linux/netdevice.h> 66 #include <linux/proc_fs.h> 67 #include <linux/seq_file.h> 68 #include <linux/mutex.h> 69 #include <linux/if_bridge.h> 70 #include <linux/if_vlan.h> 71 #include <linux/ptp_classify.h> 72 #include <linux/init.h> 73 #include <linux/poll.h> 74 #include <linux/cache.h> 75 #include <linux/module.h> 76 #include <linux/highmem.h> 77 #include <linux/mount.h> 78 #include <linux/pseudo_fs.h> 79 #include <linux/security.h> 80 #include <linux/uio.h> 81 #include <linux/syscalls.h> 82 #include <linux/compat.h> 83 #include <linux/kmod.h> 84 #include <linux/audit.h> 85 #include <linux/wireless.h> 86 #include <linux/nsproxy.h> 87 #include <linux/magic.h> 88 #include <linux/slab.h> 89 #include <linux/xattr.h> 90 #include <linux/nospec.h> 91 #include <linux/indirect_call_wrapper.h> 92 #include <linux/io_uring/net.h> 93 94 #include <linux/uaccess.h> 95 #include <asm/unistd.h> 96 97 #include <net/compat.h> 98 #include <net/wext.h> 99 #include <net/cls_cgroup.h> 100 101 #include <net/sock.h> 102 #include <linux/netfilter.h> 103 104 #include <linux/if_tun.h> 105 #include <linux/ipv6_route.h> 106 #include <linux/route.h> 107 #include <linux/termios.h> 108 #include <linux/sockios.h> 109 #include <net/busy_poll.h> 110 #include <linux/errqueue.h> 111 #include <linux/ptp_clock_kernel.h> 112 #include <trace/events/sock.h> 113 114 #include "core/dev.h" 115 116 #ifdef CONFIG_NET_RX_BUSY_POLL 117 unsigned int sysctl_net_busy_read __read_mostly; 118 unsigned int sysctl_net_busy_poll __read_mostly; 119 #endif 120 121 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to); 122 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from); 123 static int sock_mmap(struct file *file, struct vm_area_struct *vma); 124 125 static int sock_close(struct inode *inode, struct file *file); 126 static __poll_t sock_poll(struct file *file, 127 struct poll_table_struct *wait); 128 static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 129 #ifdef CONFIG_COMPAT 130 static long compat_sock_ioctl(struct file *file, 131 unsigned int cmd, unsigned long arg); 132 #endif 133 static int sock_fasync(int fd, struct file *filp, int on); 134 static ssize_t sock_splice_read(struct file *file, loff_t *ppos, 135 struct pipe_inode_info *pipe, size_t len, 136 unsigned int flags); 137 static void sock_splice_eof(struct file *file); 138 139 #ifdef CONFIG_PROC_FS 140 static void sock_show_fdinfo(struct seq_file *m, struct file *f) 141 { 142 struct socket *sock = f->private_data; 143 const struct proto_ops *ops = READ_ONCE(sock->ops); 144 145 if (ops->show_fdinfo) 146 ops->show_fdinfo(m, sock); 147 } 148 #else 149 #define sock_show_fdinfo NULL 150 #endif 151 152 /* 153 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear 154 * in the operation structures but are done directly via the socketcall() multiplexor. 155 */ 156 157 static const struct file_operations socket_file_ops = { 158 .owner = THIS_MODULE, 159 .read_iter = sock_read_iter, 160 .write_iter = sock_write_iter, 161 .poll = sock_poll, 162 .unlocked_ioctl = sock_ioctl, 163 #ifdef CONFIG_COMPAT 164 .compat_ioctl = compat_sock_ioctl, 165 #endif 166 .uring_cmd = io_uring_cmd_sock, 167 .mmap = sock_mmap, 168 .release = sock_close, 169 .fasync = sock_fasync, 170 .splice_write = splice_to_socket, 171 .splice_read = sock_splice_read, 172 .splice_eof = sock_splice_eof, 173 .show_fdinfo = sock_show_fdinfo, 174 }; 175 176 static const char * const pf_family_names[] = { 177 [PF_UNSPEC] = "PF_UNSPEC", 178 [PF_UNIX] = "PF_UNIX/PF_LOCAL", 179 [PF_INET] = "PF_INET", 180 [PF_AX25] = "PF_AX25", 181 [PF_IPX] = "PF_IPX", 182 [PF_APPLETALK] = "PF_APPLETALK", 183 [PF_NETROM] = "PF_NETROM", 184 [PF_BRIDGE] = "PF_BRIDGE", 185 [PF_ATMPVC] = "PF_ATMPVC", 186 [PF_X25] = "PF_X25", 187 [PF_INET6] = "PF_INET6", 188 [PF_ROSE] = "PF_ROSE", 189 [PF_DECnet] = "PF_DECnet", 190 [PF_NETBEUI] = "PF_NETBEUI", 191 [PF_SECURITY] = "PF_SECURITY", 192 [PF_KEY] = "PF_KEY", 193 [PF_NETLINK] = "PF_NETLINK/PF_ROUTE", 194 [PF_PACKET] = "PF_PACKET", 195 [PF_ASH] = "PF_ASH", 196 [PF_ECONET] = "PF_ECONET", 197 [PF_ATMSVC] = "PF_ATMSVC", 198 [PF_RDS] = "PF_RDS", 199 [PF_SNA] = "PF_SNA", 200 [PF_IRDA] = "PF_IRDA", 201 [PF_PPPOX] = "PF_PPPOX", 202 [PF_WANPIPE] = "PF_WANPIPE", 203 [PF_LLC] = "PF_LLC", 204 [PF_IB] = "PF_IB", 205 [PF_MPLS] = "PF_MPLS", 206 [PF_CAN] = "PF_CAN", 207 [PF_TIPC] = "PF_TIPC", 208 [PF_BLUETOOTH] = "PF_BLUETOOTH", 209 [PF_IUCV] = "PF_IUCV", 210 [PF_RXRPC] = "PF_RXRPC", 211 [PF_ISDN] = "PF_ISDN", 212 [PF_PHONET] = "PF_PHONET", 213 [PF_IEEE802154] = "PF_IEEE802154", 214 [PF_CAIF] = "PF_CAIF", 215 [PF_ALG] = "PF_ALG", 216 [PF_NFC] = "PF_NFC", 217 [PF_VSOCK] = "PF_VSOCK", 218 [PF_KCM] = "PF_KCM", 219 [PF_QIPCRTR] = "PF_QIPCRTR", 220 [PF_SMC] = "PF_SMC", 221 [PF_XDP] = "PF_XDP", 222 [PF_MCTP] = "PF_MCTP", 223 }; 224 225 /* 226 * The protocol list. Each protocol is registered in here. 227 */ 228 229 static DEFINE_SPINLOCK(net_family_lock); 230 static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly; 231 232 /* 233 * Support routines. 234 * Move socket addresses back and forth across the kernel/user 235 * divide and look after the messy bits. 236 */ 237 238 /** 239 * move_addr_to_kernel - copy a socket address into kernel space 240 * @uaddr: Address in user space 241 * @kaddr: Address in kernel space 242 * @ulen: Length in user space 243 * 244 * The address is copied into kernel space. If the provided address is 245 * too long an error code of -EINVAL is returned. If the copy gives 246 * invalid addresses -EFAULT is returned. On a success 0 is returned. 247 */ 248 249 int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr) 250 { 251 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage)) 252 return -EINVAL; 253 if (ulen == 0) 254 return 0; 255 if (copy_from_user(kaddr, uaddr, ulen)) 256 return -EFAULT; 257 return audit_sockaddr(ulen, kaddr); 258 } 259 260 /** 261 * move_addr_to_user - copy an address to user space 262 * @kaddr: kernel space address 263 * @klen: length of address in kernel 264 * @uaddr: user space address 265 * @ulen: pointer to user length field 266 * 267 * The value pointed to by ulen on entry is the buffer length available. 268 * This is overwritten with the buffer space used. -EINVAL is returned 269 * if an overlong buffer is specified or a negative buffer size. -EFAULT 270 * is returned if either the buffer or the length field are not 271 * accessible. 272 * After copying the data up to the limit the user specifies, the true 273 * length of the data is written over the length limit the user 274 * specified. Zero is returned for a success. 275 */ 276 277 static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen, 278 void __user *uaddr, int __user *ulen) 279 { 280 int len; 281 282 BUG_ON(klen > sizeof(struct sockaddr_storage)); 283 284 scoped_user_rw_access_size(ulen, 4, efault_end) { 285 unsafe_get_user(len, ulen, efault_end); 286 287 if (len > klen) 288 len = klen; 289 /* 290 * "fromlen shall refer to the value before truncation.." 291 * 1003.1g 292 */ 293 if (len >= 0) 294 unsafe_put_user(klen, ulen, efault_end); 295 } 296 297 if (len) { 298 if (len < 0) 299 return -EINVAL; 300 if (audit_sockaddr(klen, kaddr)) 301 return -ENOMEM; 302 if (copy_to_user(uaddr, kaddr, len)) 303 return -EFAULT; 304 } 305 return 0; 306 307 efault_end: 308 return -EFAULT; 309 } 310 311 static struct kmem_cache *sock_inode_cachep __ro_after_init; 312 313 static struct simple_xattr_cache sockfs_xa_cache; 314 315 struct sockfs_inode { 316 struct list_head xattrs; 317 struct simple_xattr_limits xattr_limits; 318 struct socket_alloc; 319 }; 320 321 static struct sockfs_inode *SOCKFS_I(struct inode *inode) 322 { 323 return container_of(inode, struct sockfs_inode, vfs_inode); 324 } 325 326 static struct inode *sock_alloc_inode(struct super_block *sb) 327 { 328 struct sockfs_inode *si; 329 330 si = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL); 331 if (!si) 332 return NULL; 333 INIT_LIST_HEAD_RCU(&si->xattrs); 334 simple_xattr_limits_init(&si->xattr_limits); 335 336 init_waitqueue_head(&si->socket.wq.wait); 337 si->socket.wq.fasync_list = NULL; 338 si->socket.wq.flags = 0; 339 340 si->socket.state = SS_UNCONNECTED; 341 si->socket.flags = 0; 342 si->socket.ops = NULL; 343 si->socket.sk = NULL; 344 si->socket.file = NULL; 345 346 return &si->vfs_inode; 347 } 348 349 static void sock_evict_inode(struct inode *inode) 350 { 351 struct sockfs_inode *si = SOCKFS_I(inode); 352 353 simple_xattrs_free(&sockfs_xa_cache, &si->xattrs, NULL); 354 clear_inode(inode); 355 } 356 357 static void sock_free_inode(struct inode *inode) 358 { 359 struct sockfs_inode *si = SOCKFS_I(inode); 360 361 kmem_cache_free(sock_inode_cachep, si); 362 } 363 364 static void init_once(void *foo) 365 { 366 struct sockfs_inode *si = (struct sockfs_inode *)foo; 367 368 inode_init_once(&si->vfs_inode); 369 } 370 371 static void init_inodecache(void) 372 { 373 sock_inode_cachep = kmem_cache_create("sock_inode_cache", 374 sizeof(struct sockfs_inode), 375 0, 376 (SLAB_HWCACHE_ALIGN | 377 SLAB_RECLAIM_ACCOUNT | 378 SLAB_ACCOUNT), 379 init_once); 380 BUG_ON(sock_inode_cachep == NULL); 381 } 382 383 static const struct super_operations sockfs_ops = { 384 .alloc_inode = sock_alloc_inode, 385 .free_inode = sock_free_inode, 386 .evict_inode = sock_evict_inode, 387 .statfs = simple_statfs, 388 }; 389 390 /* 391 * sockfs_dname() is called from d_path(). 392 */ 393 static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen) 394 { 395 return dynamic_dname(buffer, buflen, "socket:[%llu]", 396 d_inode(dentry)->i_ino); 397 } 398 399 static const struct dentry_operations sockfs_dentry_operations = { 400 .d_dname = sockfs_dname, 401 }; 402 403 static int sockfs_xattr_get(const struct xattr_handler *handler, 404 struct dentry *dentry, struct inode *inode, 405 const char *suffix, void *value, size_t size) 406 { 407 if (value) { 408 if (dentry->d_name.len + 1 > size) 409 return -ERANGE; 410 memcpy(value, dentry->d_name.name, dentry->d_name.len + 1); 411 } 412 return dentry->d_name.len + 1; 413 } 414 415 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname" 416 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX) 417 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1) 418 419 static const struct xattr_handler sockfs_xattr_handler = { 420 .name = XATTR_NAME_SOCKPROTONAME, 421 .get = sockfs_xattr_get, 422 }; 423 424 static int sockfs_security_xattr_set(const struct xattr_handler *handler, 425 const struct mnt_idmap *idmap, 426 struct dentry *dentry, struct inode *inode, 427 const char *suffix, const void *value, 428 size_t size, int flags) 429 { 430 /* Handled by LSM. */ 431 return -EAGAIN; 432 } 433 434 static const struct xattr_handler sockfs_security_xattr_handler = { 435 .prefix = XATTR_SECURITY_PREFIX, 436 .set = sockfs_security_xattr_set, 437 }; 438 439 static int sockfs_user_xattr_get(const struct xattr_handler *handler, 440 struct dentry *dentry, struct inode *inode, 441 const char *suffix, void *value, size_t size) 442 { 443 const char *name = xattr_full_name(handler, suffix); 444 struct sockfs_inode *si = SOCKFS_I(inode); 445 446 return simple_xattr_get(&sockfs_xa_cache, &si->xattrs, name, value, size); 447 } 448 449 static int sockfs_user_xattr_set(const struct xattr_handler *handler, 450 const struct mnt_idmap *idmap, 451 struct dentry *dentry, struct inode *inode, 452 const char *suffix, const void *value, 453 size_t size, int flags) 454 { 455 const char *name = xattr_full_name(handler, suffix); 456 struct sockfs_inode *si = SOCKFS_I(inode); 457 458 return simple_xattr_set_limited(&sockfs_xa_cache, &si->xattrs, &si->xattr_limits, 459 name, value, size, flags); 460 } 461 462 static const struct xattr_handler sockfs_user_xattr_handler = { 463 .prefix = XATTR_USER_PREFIX, 464 .get = sockfs_user_xattr_get, 465 .set = sockfs_user_xattr_set, 466 }; 467 468 /** 469 * sock_read_xattr - read a user.* xattr from a socket's sockfs inode 470 * @sock: socket whose inode holds the xattr 471 * @name: full xattr name, e.g. "user.bpf_test" 472 * @value: output buffer 473 * @size: size of @value in bytes 474 * 475 * SOCK_INODE() is valid only for sockfs sockets; sock_from_file() rejects 476 * anything else (e.g. tun, tap). 477 * Lockless: simple_xattr_get() looks up the value under RCU, no inode lock. 478 * 479 * Return: length of the value on success, a negative errno on error. 480 */ 481 int sock_read_xattr(struct socket *sock, const char *name, void *value, size_t size) 482 { 483 struct file *file = sock->file; 484 struct sockfs_inode *si; 485 486 if (!file || sock_from_file(file) != sock) 487 return -EOPNOTSUPP; 488 489 si = SOCKFS_I(SOCK_INODE(sock)); 490 return simple_xattr_get(&sockfs_xa_cache, &si->xattrs, name, value, size); 491 } 492 493 static const struct xattr_handler * const sockfs_xattr_handlers[] = { 494 &sockfs_xattr_handler, 495 &sockfs_security_xattr_handler, 496 &sockfs_user_xattr_handler, 497 NULL 498 }; 499 500 static int sockfs_init_fs_context(struct fs_context *fc) 501 { 502 struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC); 503 if (!ctx) 504 return -ENOMEM; 505 ctx->ops = &sockfs_ops; 506 ctx->dops = &sockfs_dentry_operations; 507 ctx->xattr = sockfs_xattr_handlers; 508 return 0; 509 } 510 511 static struct vfsmount *sock_mnt __read_mostly; 512 513 static struct file_system_type sock_fs_type = { 514 .name = "sockfs", 515 .init_fs_context = sockfs_init_fs_context, 516 .kill_sb = kill_anon_super, 517 }; 518 519 /* 520 * Obtains the first available file descriptor and sets it up for use. 521 * 522 * These functions create file structures and maps them to fd space 523 * of the current process. On success it returns file descriptor 524 * and file struct implicitly stored in sock->file. 525 * Note that another thread may close file descriptor before we return 526 * from this function. We use the fact that now we do not refer 527 * to socket after mapping. If one day we will need it, this 528 * function will increment ref. count on file by 1. 529 * 530 * In any case returned fd MAY BE not valid! 531 * This race condition is unavoidable 532 * with shared fd spaces, we cannot solve it inside kernel, 533 * but we take care of internal coherence yet. 534 */ 535 536 /** 537 * sock_alloc_file - Bind a &socket to a &file 538 * @sock: socket 539 * @flags: file status flags 540 * @dname: protocol name 541 * 542 * Returns the &file bound with @sock, implicitly storing it 543 * in sock->file. If dname is %NULL, sets to "". 544 * 545 * On failure @sock is released, and an ERR pointer is returned. 546 * 547 * This function uses GFP_KERNEL internally. 548 */ 549 550 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname) 551 { 552 struct file *file; 553 554 if (!dname) 555 dname = sock->sk ? sock->sk->sk_prot_creator->name : ""; 556 557 file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname, 558 O_RDWR | (flags & O_NONBLOCK), 559 &socket_file_ops); 560 if (IS_ERR(file)) { 561 sock_release(sock); 562 return file; 563 } 564 565 file->f_mode |= FMODE_NOWAIT; 566 sock->file = file; 567 file->private_data = sock; 568 stream_open(SOCK_INODE(sock), file); 569 /* 570 * Disable permission and pre-content events, but enable legacy 571 * inotify events for legacy users. 572 */ 573 file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM); 574 return file; 575 } 576 EXPORT_SYMBOL(sock_alloc_file); 577 578 static int sock_map_fd(struct socket *sock, int flags) 579 { 580 struct file *newfile; 581 int fd = get_unused_fd_flags(flags); 582 if (unlikely(fd < 0)) { 583 sock_release(sock); 584 return fd; 585 } 586 587 newfile = sock_alloc_file(sock, flags, NULL); 588 if (!IS_ERR(newfile)) { 589 fd_install(fd, newfile); 590 return fd; 591 } 592 593 put_unused_fd(fd); 594 return PTR_ERR(newfile); 595 } 596 597 /** 598 * sock_from_file - Return the &socket bounded to @file. 599 * @file: file 600 * 601 * On failure returns %NULL. 602 */ 603 604 struct socket *sock_from_file(struct file *file) 605 { 606 if (likely(file->f_op == &socket_file_ops)) 607 return file->private_data; /* set in sock_alloc_file */ 608 609 return NULL; 610 } 611 EXPORT_SYMBOL(sock_from_file); 612 613 /** 614 * sockfd_lookup - Go from a file number to its socket slot 615 * @fd: file handle 616 * @err: pointer to an error code return 617 * 618 * The file handle passed in is locked and the socket it is bound 619 * to is returned. If an error occurs the err pointer is overwritten 620 * with a negative errno code and NULL is returned. The function checks 621 * for both invalid handles and passing a handle which is not a socket. 622 * 623 * On a success the socket object pointer is returned. 624 */ 625 626 struct socket *sockfd_lookup(int fd, int *err) 627 { 628 struct file *file; 629 struct socket *sock; 630 631 file = fget(fd); 632 if (!file) { 633 *err = -EBADF; 634 return NULL; 635 } 636 637 sock = sock_from_file(file); 638 if (!sock) { 639 *err = -ENOTSOCK; 640 fput(file); 641 } 642 return sock; 643 } 644 EXPORT_SYMBOL(sockfd_lookup); 645 646 static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer, 647 size_t size) 648 { 649 struct sockfs_inode *si = SOCKFS_I(d_inode(dentry)); 650 ssize_t len, used; 651 652 len = simple_xattr_list(d_inode(dentry), &si->xattrs, buffer, size); 653 if (len < 0) 654 return len; 655 656 used = len; 657 if (buffer) { 658 buffer += len; 659 size -= len; 660 } 661 662 len = XATTR_NAME_SOCKPROTONAME_LEN + 1; 663 used += len; 664 if (buffer) { 665 if (size < len) 666 return -ERANGE; 667 memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len); 668 } 669 670 return used; 671 } 672 673 static int sockfs_setattr(const struct mnt_idmap *idmap, 674 struct dentry *dentry, struct iattr *iattr) 675 { 676 int err = simple_setattr(&nop_mnt_idmap, dentry, iattr); 677 678 if (!err && (iattr->ia_valid & ATTR_UID)) { 679 struct socket *sock = SOCKET_I(d_inode(dentry)); 680 681 if (sock->sk) { 682 /* Paired with READ_ONCE() in sk_uid() */ 683 WRITE_ONCE(sock->sk->sk_uid, iattr->ia_uid); 684 } else { 685 err = -ENOENT; 686 } 687 } 688 689 return err; 690 } 691 692 static const struct inode_operations sockfs_inode_ops = { 693 .listxattr = sockfs_listxattr, 694 .setattr = sockfs_setattr, 695 }; 696 697 /** 698 * sock_alloc - allocate a socket 699 * 700 * Allocate a new inode and socket object. The two are bound together 701 * and initialised. The socket is then returned. If we are out of inodes 702 * NULL is returned. This functions uses GFP_KERNEL internally. 703 */ 704 705 struct socket *sock_alloc(void) 706 { 707 struct inode *inode; 708 struct socket *sock; 709 710 inode = new_inode_pseudo(sock_mnt->mnt_sb); 711 if (!inode) 712 return NULL; 713 714 sock = SOCKET_I(inode); 715 716 inode->i_ino = get_next_ino(); 717 inode->i_mode = S_IFSOCK | S_IRWXUGO; 718 inode->i_uid = current_fsuid(); 719 inode->i_gid = current_fsgid(); 720 inode->i_op = &sockfs_inode_ops; 721 722 return sock; 723 } 724 EXPORT_SYMBOL(sock_alloc); 725 726 static void __sock_release(struct socket *sock, struct inode *inode) 727 { 728 const struct proto_ops *ops = READ_ONCE(sock->ops); 729 730 if (ops) { 731 struct module *owner = ops->owner; 732 733 if (inode) 734 inode_lock(inode); 735 ops->release(sock); 736 sock->sk = NULL; 737 if (inode) 738 inode_unlock(inode); 739 sock->ops = NULL; 740 module_put(owner); 741 } 742 743 if (sock->wq.fasync_list) 744 pr_err("%s: fasync list not empty!\n", __func__); 745 746 if (!sock->file) { 747 iput(SOCK_INODE(sock)); 748 return; 749 } 750 WRITE_ONCE(sock->file, NULL); 751 } 752 753 /** 754 * sock_release - close a socket 755 * @sock: socket to close 756 * 757 * The socket is released from the protocol stack if it has a release 758 * callback, and the inode is then released if the socket is bound to 759 * an inode not a file. 760 */ 761 void sock_release(struct socket *sock) 762 { 763 __sock_release(sock, NULL); 764 } 765 EXPORT_SYMBOL(sock_release); 766 767 void __sock_tx_timestamp(__u32 tsflags, __u8 *tx_flags) 768 { 769 u8 flags = *tx_flags; 770 771 if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) 772 flags |= SKBTX_HW_TSTAMP_NOBPF; 773 774 if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE) 775 flags |= SKBTX_SW_TSTAMP; 776 777 if (tsflags & SOF_TIMESTAMPING_TX_SCHED) 778 flags |= SKBTX_SCHED_TSTAMP; 779 780 if (tsflags & SOF_TIMESTAMPING_TX_COMPLETION) 781 flags |= SKBTX_COMPLETION_TSTAMP; 782 783 *tx_flags = flags; 784 } 785 EXPORT_SYMBOL(__sock_tx_timestamp); 786 787 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *, 788 size_t)); 789 INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *, 790 size_t)); 791 792 static noinline void call_trace_sock_send_length(struct sock *sk, int ret, 793 int flags) 794 { 795 trace_sock_send_length(sk, ret, 0); 796 } 797 798 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg) 799 { 800 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg, 801 inet_sendmsg, sock, msg, 802 msg_data_left(msg)); 803 if (unlikely(ret == -EIOCBQUEUED)) { 804 DEBUG_NET_WARN_ON_ONCE(1); 805 ret = -EIO; 806 } 807 808 if (trace_sock_send_length_enabled()) 809 call_trace_sock_send_length(sock->sk, ret, 0); 810 return ret; 811 } 812 813 static int __sock_sendmsg(struct socket *sock, struct msghdr *msg) 814 { 815 int err = security_socket_sendmsg(sock, msg, 816 msg_data_left(msg)); 817 818 return err ?: sock_sendmsg_nosec(sock, msg); 819 } 820 821 /** 822 * sock_sendmsg - send a message through @sock 823 * @sock: socket 824 * @msg: message to send 825 * 826 * Sends @msg through @sock, passing through LSM. 827 * Returns the number of bytes sent, or an error code. 828 */ 829 int sock_sendmsg(struct socket *sock, struct msghdr *msg) 830 { 831 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name; 832 struct sockaddr_storage address; 833 int save_len = msg->msg_namelen; 834 int ret; 835 836 if (msg->msg_name) { 837 memcpy(&address, msg->msg_name, msg->msg_namelen); 838 msg->msg_name = &address; 839 } 840 841 ret = __sock_sendmsg(sock, msg); 842 msg->msg_name = save_addr; 843 msg->msg_namelen = save_len; 844 845 return ret; 846 } 847 EXPORT_SYMBOL(sock_sendmsg); 848 849 /** 850 * kernel_sendmsg - send a message through @sock (kernel-space) 851 * @sock: socket 852 * @msg: message header 853 * @vec: kernel vec 854 * @num: vec array length 855 * @size: total message data size 856 * 857 * Builds the message data with @vec and sends it through @sock. 858 * Returns the number of bytes sent, or an error code. 859 */ 860 861 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 862 struct kvec *vec, size_t num, size_t size) 863 { 864 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size); 865 return sock_sendmsg(sock, msg); 866 } 867 EXPORT_SYMBOL(kernel_sendmsg); 868 869 static bool skb_is_err_queue(const struct sk_buff *skb) 870 { 871 /* Error-queue skbs are marked as PACKET_OUTGOING in 872 * skb_set_err_queue() and use the destructor installed by 873 * sock_queue_err_skb(). PACKET_OUTGOING alone is not unique: 874 * AF_PACKET outgoing taps use the same pkt_type. 875 */ 876 return skb->pkt_type == PACKET_OUTGOING && 877 skb->destructor == sock_rmem_free; 878 } 879 880 /* On transmit, software and hardware timestamps are returned independently. 881 * As the two skb clones share the hardware timestamp, which may be updated 882 * before the software timestamp is received, a hardware TX timestamp may be 883 * returned only if there is no software TX timestamp. Ignore false software 884 * timestamps, which may be made in the __sock_recv_timestamp() call when the 885 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a 886 * hardware timestamp. 887 */ 888 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) 889 { 890 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb); 891 } 892 893 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index) 894 { 895 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC; 896 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 897 struct net_device *orig_dev; 898 ktime_t hwtstamp; 899 900 rcu_read_lock(); 901 orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); 902 if (orig_dev) { 903 *if_index = orig_dev->ifindex; 904 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles); 905 } else { 906 hwtstamp = shhwtstamps->hwtstamp; 907 } 908 rcu_read_unlock(); 909 910 return hwtstamp; 911 } 912 913 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb, 914 int if_index) 915 { 916 struct scm_ts_pktinfo ts_pktinfo; 917 struct net_device *orig_dev; 918 919 if (!skb_mac_header_was_set(skb)) 920 return; 921 922 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo)); 923 924 if (!if_index) { 925 rcu_read_lock(); 926 orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); 927 if (orig_dev) 928 if_index = orig_dev->ifindex; 929 rcu_read_unlock(); 930 } 931 ts_pktinfo.if_index = if_index; 932 933 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb); 934 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO, 935 sizeof(ts_pktinfo), &ts_pktinfo); 936 } 937 938 bool skb_has_tx_timestamp(struct sk_buff *skb, const struct sock *sk) 939 { 940 const struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 941 u32 tsflags = READ_ONCE(sk->sk_tsflags); 942 943 if (serr->ee.ee_errno != ENOMSG || 944 serr->ee.ee_origin != SO_EE_ORIGIN_TIMESTAMPING) 945 return false; 946 947 /* software time stamp available and wanted */ 948 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && skb->tstamp) 949 return true; 950 /* hardware time stamps available and wanted */ 951 return (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && 952 skb_hwtstamps(skb)->hwtstamp; 953 } 954 955 int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk, 956 struct timespec64 *ts) 957 { 958 u32 tsflags = READ_ONCE(sk->sk_tsflags); 959 ktime_t hwtstamp; 960 int if_index = 0; 961 962 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && 963 ktime_to_timespec64_cond(skb->tstamp, ts)) 964 return SOF_TIMESTAMPING_TX_SOFTWARE; 965 966 if (!(tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) || 967 skb_is_swtx_tstamp(skb, false)) 968 return -ENOENT; 969 970 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV) 971 hwtstamp = get_timestamp(sk, skb, &if_index); 972 else 973 hwtstamp = skb_hwtstamps(skb)->hwtstamp; 974 975 if (tsflags & SOF_TIMESTAMPING_BIND_PHC) 976 hwtstamp = ptp_convert_timestamp(&hwtstamp, 977 READ_ONCE(sk->sk_bind_phc)); 978 if (!ktime_to_timespec64_cond(hwtstamp, ts)) 979 return -ENOENT; 980 981 return SOF_TIMESTAMPING_TX_HARDWARE; 982 } 983 984 /* 985 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP) 986 */ 987 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, 988 struct sk_buff *skb) 989 { 990 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP); 991 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); 992 struct skb_shared_hwtstamps *shhwtstamps = 993 skb_hwtstamps(skb); 994 struct scm_timestamping_internal tss; 995 int if_index, false_tstamp = 0; 996 ktime_t hwtstamp; 997 u32 tsflags; 998 999 /* Race occurred between timestamp enabling and packet 1000 receiving. Fill in the current time for now. */ 1001 if (need_software_tstamp && skb->tstamp == 0) { 1002 __net_timestamp(skb); 1003 false_tstamp = 1; 1004 } 1005 1006 if (need_software_tstamp) { 1007 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) { 1008 if (new_tstamp) { 1009 struct __kernel_sock_timeval tv; 1010 1011 skb_get_new_timestamp(skb, &tv); 1012 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW, 1013 sizeof(tv), &tv); 1014 } else { 1015 struct __kernel_old_timeval tv; 1016 1017 skb_get_timestamp(skb, &tv); 1018 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD, 1019 sizeof(tv), &tv); 1020 } 1021 } else { 1022 if (new_tstamp) { 1023 struct __kernel_timespec ts; 1024 1025 skb_get_new_timestampns(skb, &ts); 1026 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW, 1027 sizeof(ts), &ts); 1028 } else { 1029 struct __kernel_old_timespec ts; 1030 1031 skb_get_timestampns(skb, &ts); 1032 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD, 1033 sizeof(ts), &ts); 1034 } 1035 } 1036 } 1037 1038 memset(&tss, 0, sizeof(tss)); 1039 tsflags = READ_ONCE(sk->sk_tsflags); 1040 if (tsflags & SOF_TIMESTAMPING_SOFTWARE && 1041 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE || 1042 skb_is_err_queue(skb) || 1043 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) 1044 tss.ts[0] = skb->tstamp; 1045 1046 if (shhwtstamps && 1047 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE && 1048 (tsflags & SOF_TIMESTAMPING_RX_HARDWARE || 1049 skb_is_err_queue(skb) || 1050 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) && 1051 !skb_is_swtx_tstamp(skb, false_tstamp)) { 1052 if_index = 0; 1053 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV) 1054 hwtstamp = get_timestamp(sk, skb, &if_index); 1055 else 1056 hwtstamp = shhwtstamps->hwtstamp; 1057 1058 if (tsflags & SOF_TIMESTAMPING_BIND_PHC) 1059 hwtstamp = ptp_convert_timestamp(&hwtstamp, 1060 READ_ONCE(sk->sk_bind_phc)); 1061 1062 if (hwtstamp) { 1063 tss.ts[2] = hwtstamp; 1064 1065 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) && 1066 !skb_is_err_queue(skb)) 1067 put_ts_pktinfo(msg, skb, if_index); 1068 } 1069 } 1070 if (tss.ts[0] | tss.ts[2]) { 1071 if (sock_flag(sk, SOCK_TSTAMP_NEW)) 1072 put_cmsg_scm_timestamping64(msg, &tss); 1073 else 1074 put_cmsg_scm_timestamping(msg, &tss); 1075 1076 if (skb_is_err_queue(skb) && skb->len && 1077 SKB_EXT_ERR(skb)->opt_stats) 1078 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS, 1079 skb->len, skb->data); 1080 } 1081 } 1082 EXPORT_SYMBOL_GPL(__sock_recv_timestamp); 1083 1084 #ifdef CONFIG_WIRELESS 1085 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk, 1086 struct sk_buff *skb) 1087 { 1088 int ack; 1089 1090 if (!sock_flag(sk, SOCK_WIFI_STATUS)) 1091 return; 1092 if (!skb->wifi_acked_valid) 1093 return; 1094 1095 ack = skb->wifi_acked; 1096 1097 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack); 1098 } 1099 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status); 1100 #endif 1101 1102 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, 1103 struct sk_buff *skb) 1104 { 1105 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount) 1106 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL, 1107 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount); 1108 } 1109 1110 static void sock_recv_mark(struct msghdr *msg, struct sock *sk, 1111 struct sk_buff *skb) 1112 { 1113 if (sock_flag(sk, SOCK_RCVMARK) && skb) { 1114 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */ 1115 __u32 mark = skb->mark; 1116 1117 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark); 1118 } 1119 } 1120 1121 static void sock_recv_priority(struct msghdr *msg, struct sock *sk, 1122 struct sk_buff *skb) 1123 { 1124 if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) { 1125 __u32 priority = skb->priority; 1126 1127 put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority); 1128 } 1129 } 1130 1131 void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk, 1132 struct sk_buff *skb) 1133 { 1134 sock_recv_timestamp(msg, sk, skb); 1135 sock_recv_drops(msg, sk, skb); 1136 sock_recv_mark(msg, sk, skb); 1137 sock_recv_priority(msg, sk, skb); 1138 } 1139 EXPORT_SYMBOL_GPL(__sock_recv_cmsgs); 1140 1141 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *, 1142 size_t, int)); 1143 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *, 1144 size_t, int)); 1145 1146 static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags) 1147 { 1148 trace_sock_recv_length(sk, ret, flags); 1149 } 1150 1151 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg, 1152 int flags) 1153 { 1154 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg, 1155 inet6_recvmsg, 1156 inet_recvmsg, sock, msg, 1157 msg_data_left(msg), flags); 1158 if (trace_sock_recv_length_enabled()) 1159 call_trace_sock_recv_length(sock->sk, ret, flags); 1160 return ret; 1161 } 1162 1163 /** 1164 * sock_recvmsg - receive a message from @sock 1165 * @sock: socket 1166 * @msg: message to receive 1167 * @flags: message flags 1168 * 1169 * Receives @msg from @sock, passing through LSM. Returns the total number 1170 * of bytes received, or an error. 1171 */ 1172 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags) 1173 { 1174 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags); 1175 1176 return err ?: sock_recvmsg_nosec(sock, msg, flags); 1177 } 1178 EXPORT_SYMBOL(sock_recvmsg); 1179 1180 /** 1181 * kernel_recvmsg - Receive a message from a socket (kernel space) 1182 * @sock: The socket to receive the message from 1183 * @msg: Received message 1184 * @vec: Input s/g array for message data 1185 * @num: Size of input s/g array 1186 * @size: Number of bytes to read 1187 * @flags: Message flags (MSG_DONTWAIT, etc...) 1188 * 1189 * On return the msg structure contains the scatter/gather array passed in the 1190 * vec argument. The array is modified so that it consists of the unfilled 1191 * portion of the original array. 1192 * 1193 * The returned value is the total number of bytes received, or an error. 1194 */ 1195 1196 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 1197 struct kvec *vec, size_t num, size_t size, int flags) 1198 { 1199 msg->msg_control_is_user = false; 1200 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size); 1201 return sock_recvmsg(sock, msg, flags); 1202 } 1203 EXPORT_SYMBOL(kernel_recvmsg); 1204 1205 static ssize_t sock_splice_read(struct file *file, loff_t *ppos, 1206 struct pipe_inode_info *pipe, size_t len, 1207 unsigned int flags) 1208 { 1209 struct socket *sock = file->private_data; 1210 const struct proto_ops *ops; 1211 1212 ops = READ_ONCE(sock->ops); 1213 if (unlikely(!ops->splice_read)) 1214 return copy_splice_read(file, ppos, pipe, len, flags); 1215 1216 return ops->splice_read(sock, ppos, pipe, len, flags); 1217 } 1218 1219 static void sock_splice_eof(struct file *file) 1220 { 1221 struct socket *sock = file->private_data; 1222 const struct proto_ops *ops; 1223 1224 ops = READ_ONCE(sock->ops); 1225 if (ops->splice_eof) 1226 ops->splice_eof(sock); 1227 } 1228 1229 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to) 1230 { 1231 struct file *file = iocb->ki_filp; 1232 struct socket *sock = file->private_data; 1233 struct msghdr msg = {.msg_iter = *to}; 1234 ssize_t res; 1235 1236 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT)) 1237 msg.msg_flags = MSG_DONTWAIT; 1238 1239 if (iocb->ki_pos != 0) 1240 return -ESPIPE; 1241 1242 if (!iov_iter_count(to)) /* Match SYS5 behaviour */ 1243 return 0; 1244 1245 res = sock_recvmsg(sock, &msg, msg.msg_flags); 1246 *to = msg.msg_iter; 1247 return res; 1248 } 1249 1250 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from) 1251 { 1252 struct file *file = iocb->ki_filp; 1253 struct socket *sock = file->private_data; 1254 struct msghdr msg = {.msg_iter = *from}; 1255 ssize_t res; 1256 1257 if (iocb->ki_pos != 0) 1258 return -ESPIPE; 1259 1260 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT)) 1261 msg.msg_flags = MSG_DONTWAIT; 1262 1263 if (sock->type == SOCK_SEQPACKET) 1264 msg.msg_flags |= MSG_EOR; 1265 1266 if (iocb->ki_flags & IOCB_NOSIGNAL) 1267 msg.msg_flags |= MSG_NOSIGNAL; 1268 1269 res = __sock_sendmsg(sock, &msg); 1270 *from = msg.msg_iter; 1271 return res; 1272 } 1273 1274 /* 1275 * Atomic setting of ioctl hooks to avoid race 1276 * with module unload. 1277 */ 1278 1279 static DEFINE_MUTEX(br_ioctl_mutex); 1280 static int (*br_ioctl_hook)(struct net *net, unsigned int cmd, 1281 void __user *uarg); 1282 1283 void brioctl_set(int (*hook)(struct net *net, unsigned int cmd, 1284 void __user *uarg)) 1285 { 1286 mutex_lock(&br_ioctl_mutex); 1287 br_ioctl_hook = hook; 1288 mutex_unlock(&br_ioctl_mutex); 1289 } 1290 EXPORT_SYMBOL(brioctl_set); 1291 1292 int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg) 1293 { 1294 int err = -ENOPKG; 1295 1296 if (!br_ioctl_hook) 1297 request_module("bridge"); 1298 1299 mutex_lock(&br_ioctl_mutex); 1300 if (br_ioctl_hook) 1301 err = br_ioctl_hook(net, cmd, uarg); 1302 mutex_unlock(&br_ioctl_mutex); 1303 1304 return err; 1305 } 1306 1307 static DEFINE_MUTEX(vlan_ioctl_mutex); 1308 static int (*vlan_ioctl_hook) (struct net *, void __user *arg); 1309 1310 void vlan_ioctl_set(int (*hook) (struct net *, void __user *)) 1311 { 1312 mutex_lock(&vlan_ioctl_mutex); 1313 vlan_ioctl_hook = hook; 1314 mutex_unlock(&vlan_ioctl_mutex); 1315 } 1316 EXPORT_SYMBOL(vlan_ioctl_set); 1317 1318 static long sock_do_ioctl(struct net *net, struct socket *sock, 1319 unsigned int cmd, unsigned long arg) 1320 { 1321 const struct proto_ops *ops = READ_ONCE(sock->ops); 1322 struct ifreq ifr; 1323 bool need_copyout; 1324 int err; 1325 void __user *argp = (void __user *)arg; 1326 void __user *data; 1327 1328 err = ops->ioctl(sock, cmd, arg); 1329 1330 /* 1331 * If this ioctl is unknown try to hand it down 1332 * to the NIC driver. 1333 */ 1334 if (err != -ENOIOCTLCMD) 1335 return err; 1336 1337 if (!is_socket_ioctl_cmd(cmd)) 1338 return -ENOTTY; 1339 1340 if (get_user_ifreq(&ifr, &data, argp)) 1341 return -EFAULT; 1342 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout); 1343 if (!err && need_copyout) 1344 if (put_user_ifreq(&ifr, argp)) 1345 return -EFAULT; 1346 1347 return err; 1348 } 1349 1350 /* 1351 * With an ioctl, arg may well be a user mode pointer, but we don't know 1352 * what to do with it - that's up to the protocol still. 1353 */ 1354 1355 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) 1356 { 1357 const struct proto_ops *ops; 1358 struct socket *sock; 1359 struct sock *sk; 1360 void __user *argp = (void __user *)arg; 1361 int pid, err; 1362 struct net *net; 1363 1364 sock = file->private_data; 1365 ops = READ_ONCE(sock->ops); 1366 sk = sock->sk; 1367 net = sock_net(sk); 1368 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) { 1369 struct ifreq ifr; 1370 void __user *data; 1371 bool need_copyout; 1372 if (get_user_ifreq(&ifr, &data, argp)) 1373 return -EFAULT; 1374 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout); 1375 if (!err && need_copyout) 1376 if (put_user_ifreq(&ifr, argp)) 1377 return -EFAULT; 1378 } else 1379 #ifdef CONFIG_WEXT_CORE 1380 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 1381 err = wext_handle_ioctl(net, cmd, argp); 1382 } else 1383 #endif 1384 switch (cmd) { 1385 case FIOSETOWN: 1386 case SIOCSPGRP: 1387 err = -EFAULT; 1388 if (get_user(pid, (int __user *)argp)) 1389 break; 1390 err = f_setown(sock->file, pid, 1); 1391 break; 1392 case FIOGETOWN: 1393 case SIOCGPGRP: 1394 err = put_user(f_getown(sock->file), 1395 (int __user *)argp); 1396 break; 1397 case SIOCGIFBR: 1398 case SIOCSIFBR: 1399 case SIOCBRADDBR: 1400 case SIOCBRDELBR: 1401 case SIOCBRADDIF: 1402 case SIOCBRDELIF: 1403 err = br_ioctl_call(net, cmd, argp); 1404 break; 1405 case SIOCGIFVLAN: 1406 case SIOCSIFVLAN: 1407 err = -ENOPKG; 1408 if (!vlan_ioctl_hook) 1409 request_module("8021q"); 1410 1411 mutex_lock(&vlan_ioctl_mutex); 1412 if (vlan_ioctl_hook) 1413 err = vlan_ioctl_hook(net, argp); 1414 mutex_unlock(&vlan_ioctl_mutex); 1415 break; 1416 case SIOCGSKNS: 1417 err = -EPERM; 1418 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1419 break; 1420 1421 err = open_related_ns(&net->ns, get_net_ns); 1422 break; 1423 case SIOCGSTAMP_OLD: 1424 case SIOCGSTAMPNS_OLD: 1425 if (!ops->gettstamp) { 1426 err = -ENOIOCTLCMD; 1427 break; 1428 } 1429 err = ops->gettstamp(sock, argp, 1430 cmd == SIOCGSTAMP_OLD, 1431 !IS_ENABLED(CONFIG_64BIT)); 1432 break; 1433 case SIOCGSTAMP_NEW: 1434 case SIOCGSTAMPNS_NEW: 1435 if (!ops->gettstamp) { 1436 err = -ENOIOCTLCMD; 1437 break; 1438 } 1439 err = ops->gettstamp(sock, argp, 1440 cmd == SIOCGSTAMP_NEW, 1441 false); 1442 break; 1443 1444 case SIOCGIFCONF: 1445 err = dev_ifconf(net, argp); 1446 break; 1447 1448 default: 1449 err = sock_do_ioctl(net, sock, cmd, arg); 1450 break; 1451 } 1452 return err; 1453 } 1454 1455 /** 1456 * sock_create_lite - creates a socket 1457 * @family: protocol family (AF_INET, ...) 1458 * @type: communication type (SOCK_STREAM, ...) 1459 * @protocol: protocol (0, ...) 1460 * @res: new socket 1461 * 1462 * Creates a new socket and assigns it to @res, passing through LSM. 1463 * The new socket initialization is not complete, see kernel_accept(). 1464 * Returns 0 or an error. On failure @res is set to %NULL. 1465 * This function internally uses GFP_KERNEL. 1466 */ 1467 1468 int sock_create_lite(int family, int type, int protocol, struct socket **res) 1469 { 1470 int err; 1471 struct socket *sock = NULL; 1472 1473 err = security_socket_create(family, type, protocol, 1); 1474 if (err) 1475 goto out; 1476 1477 sock = sock_alloc(); 1478 if (!sock) { 1479 err = -ENOMEM; 1480 goto out; 1481 } 1482 1483 sock->type = type; 1484 err = security_socket_post_create(sock, family, type, protocol, 1); 1485 if (err) 1486 goto out_release; 1487 1488 out: 1489 *res = sock; 1490 return err; 1491 out_release: 1492 sock_release(sock); 1493 sock = NULL; 1494 goto out; 1495 } 1496 EXPORT_SYMBOL(sock_create_lite); 1497 1498 /* No kernel lock held - perfect */ 1499 static __poll_t sock_poll(struct file *file, poll_table *wait) 1500 { 1501 struct socket *sock = file->private_data; 1502 const struct proto_ops *ops = READ_ONCE(sock->ops); 1503 __poll_t events = poll_requested_events(wait), flag = 0; 1504 1505 if (!ops->poll) 1506 return 0; 1507 1508 if (sk_can_busy_loop(sock->sk)) { 1509 /* poll once if requested by the syscall */ 1510 if (events & POLL_BUSY_LOOP) 1511 sk_busy_loop(sock->sk, 1); 1512 1513 /* if this socket can poll_ll, tell the system call */ 1514 flag = POLL_BUSY_LOOP; 1515 } 1516 1517 return ops->poll(file, sock, wait) | flag; 1518 } 1519 1520 static int sock_mmap(struct file *file, struct vm_area_struct *vma) 1521 { 1522 struct socket *sock = file->private_data; 1523 1524 return READ_ONCE(sock->ops)->mmap(file, sock, vma); 1525 } 1526 1527 static int sock_close(struct inode *inode, struct file *filp) 1528 { 1529 __sock_release(SOCKET_I(inode), inode); 1530 return 0; 1531 } 1532 1533 /* 1534 * Update the socket async list 1535 * 1536 * Fasync_list locking strategy. 1537 * 1538 * 1. fasync_list is modified only under process context socket lock 1539 * i.e. under semaphore. 1540 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock) 1541 * or under socket lock 1542 */ 1543 1544 static int sock_fasync(int fd, struct file *filp, int on) 1545 { 1546 struct socket *sock = filp->private_data; 1547 struct sock *sk = sock->sk; 1548 struct socket_wq *wq = &sock->wq; 1549 1550 if (sk == NULL) 1551 return -EINVAL; 1552 1553 lock_sock(sk); 1554 fasync_helper(fd, filp, on, &wq->fasync_list); 1555 1556 if (!wq->fasync_list) 1557 sock_reset_flag(sk, SOCK_FASYNC); 1558 else 1559 sock_set_flag(sk, SOCK_FASYNC); 1560 1561 release_sock(sk); 1562 return 0; 1563 } 1564 1565 /* This function may be called only under rcu_lock */ 1566 1567 int sock_wake_async(struct socket_wq *wq, int how, int band) 1568 { 1569 if (!wq || !wq->fasync_list) 1570 return -1; 1571 1572 switch (how) { 1573 case SOCK_WAKE_WAITD: 1574 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags)) 1575 break; 1576 goto call_kill; 1577 case SOCK_WAKE_SPACE: 1578 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags)) 1579 break; 1580 fallthrough; 1581 case SOCK_WAKE_IO: 1582 call_kill: 1583 kill_fasync(&wq->fasync_list, SIGIO, band); 1584 break; 1585 case SOCK_WAKE_URG: 1586 kill_fasync(&wq->fasync_list, SIGURG, band); 1587 } 1588 1589 return 0; 1590 } 1591 EXPORT_SYMBOL(sock_wake_async); 1592 1593 /** 1594 * __sock_create - creates a socket 1595 * @net: net namespace 1596 * @family: protocol family (AF_INET, ...) 1597 * @type: communication type (SOCK_STREAM, ...) 1598 * @protocol: protocol (0, ...) 1599 * @res: new socket 1600 * @kern: boolean for kernel space sockets 1601 * 1602 * Creates a new socket and assigns it to @res, passing through LSM. 1603 * Returns 0 or an error. On failure @res is set to %NULL. @kern must 1604 * be set to true if the socket resides in kernel space. 1605 * This function internally uses GFP_KERNEL. 1606 */ 1607 1608 int __sock_create(struct net *net, int family, int type, int protocol, 1609 struct socket **res, int kern) 1610 { 1611 int err; 1612 struct socket *sock; 1613 const struct net_proto_family *pf; 1614 1615 /* 1616 * Check protocol is in range 1617 */ 1618 if (family < 0 || family >= NPROTO) 1619 return -EAFNOSUPPORT; 1620 if (type < 0 || type >= SOCK_MAX) 1621 return -EINVAL; 1622 1623 /* Compatibility. 1624 1625 This uglymoron is moved from INET layer to here to avoid 1626 deadlock in module load. 1627 */ 1628 if (family == PF_INET && type == SOCK_PACKET) { 1629 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n", 1630 current->comm); 1631 family = PF_PACKET; 1632 } 1633 1634 err = security_socket_create(family, type, protocol, kern); 1635 if (err) 1636 return err; 1637 1638 /* 1639 * Allocate the socket and allow the family to set things up. if 1640 * the protocol is 0, the family is instructed to select an appropriate 1641 * default. 1642 */ 1643 sock = sock_alloc(); 1644 if (!sock) { 1645 net_warn_ratelimited("socket: no more sockets\n"); 1646 return -ENFILE; /* Not exactly a match, but its the 1647 closest posix thing */ 1648 } 1649 1650 sock->type = type; 1651 1652 #ifdef CONFIG_MODULES 1653 /* Attempt to load a protocol module if the find failed. 1654 * 1655 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 1656 * requested real, full-featured networking support upon configuration. 1657 * Otherwise module support will break! 1658 */ 1659 if (rcu_access_pointer(net_families[family]) == NULL) 1660 request_module("net-pf-%d", family); 1661 #endif 1662 1663 rcu_read_lock(); 1664 pf = rcu_dereference(net_families[family]); 1665 err = -EAFNOSUPPORT; 1666 if (!pf) 1667 goto out_release; 1668 1669 /* 1670 * We will call the ->create function, that possibly is in a loadable 1671 * module, so we have to bump that loadable module refcnt first. 1672 */ 1673 if (!try_module_get(pf->owner)) 1674 goto out_release; 1675 1676 /* Now protected by module ref count */ 1677 rcu_read_unlock(); 1678 1679 err = pf->create(net, sock, protocol, kern); 1680 if (err < 0) { 1681 /* ->create should release the allocated sock->sk object on error 1682 * and make sure sock->sk is set to NULL to avoid use-after-free 1683 */ 1684 DEBUG_NET_WARN_ONCE(sock->sk, 1685 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n", 1686 pf->create, family, type, protocol); 1687 goto out_module_put; 1688 } 1689 1690 /* 1691 * Now to bump the refcnt of the [loadable] module that owns this 1692 * socket at sock_release time we decrement its refcnt. 1693 */ 1694 if (!try_module_get(sock->ops->owner)) 1695 goto out_module_busy; 1696 1697 /* 1698 * Now that we're done with the ->create function, the [loadable] 1699 * module can have its refcnt decremented 1700 */ 1701 module_put(pf->owner); 1702 err = security_socket_post_create(sock, family, type, protocol, kern); 1703 if (err) 1704 goto out_sock_release; 1705 *res = sock; 1706 1707 return 0; 1708 1709 out_module_busy: 1710 err = -EAFNOSUPPORT; 1711 out_module_put: 1712 sock->ops = NULL; 1713 module_put(pf->owner); 1714 out_sock_release: 1715 sock_release(sock); 1716 return err; 1717 1718 out_release: 1719 rcu_read_unlock(); 1720 goto out_sock_release; 1721 } 1722 EXPORT_SYMBOL(__sock_create); 1723 1724 /** 1725 * sock_create - creates a socket 1726 * @family: protocol family (AF_INET, ...) 1727 * @type: communication type (SOCK_STREAM, ...) 1728 * @protocol: protocol (0, ...) 1729 * @res: new socket 1730 * 1731 * A wrapper around __sock_create(). 1732 * Returns 0 or an error. This function internally uses GFP_KERNEL. 1733 */ 1734 1735 int sock_create(int family, int type, int protocol, struct socket **res) 1736 { 1737 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0); 1738 } 1739 EXPORT_SYMBOL(sock_create); 1740 1741 /** 1742 * sock_create_kern - creates a socket (kernel space) 1743 * @net: net namespace 1744 * @family: protocol family (AF_INET, ...) 1745 * @type: communication type (SOCK_STREAM, ...) 1746 * @protocol: protocol (0, ...) 1747 * @res: new socket 1748 * 1749 * A wrapper around __sock_create(). 1750 * Returns 0 or an error. This function internally uses GFP_KERNEL. 1751 */ 1752 1753 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res) 1754 { 1755 return __sock_create(net, family, type, protocol, res, 1); 1756 } 1757 EXPORT_SYMBOL(sock_create_kern); 1758 1759 static struct socket *__sys_socket_create(int family, int type, int protocol) 1760 { 1761 struct socket *sock; 1762 int retval; 1763 1764 /* Check the SOCK_* constants for consistency. */ 1765 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC); 1766 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK); 1767 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK); 1768 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK); 1769 1770 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1771 return ERR_PTR(-EINVAL); 1772 type &= SOCK_TYPE_MASK; 1773 1774 retval = sock_create(family, type, protocol, &sock); 1775 if (retval < 0) 1776 return ERR_PTR(retval); 1777 1778 return sock; 1779 } 1780 1781 struct file *__sys_socket_file(int family, int type, int protocol) 1782 { 1783 struct socket *sock; 1784 int flags; 1785 1786 sock = __sys_socket_create(family, type, protocol); 1787 if (IS_ERR(sock)) 1788 return ERR_CAST(sock); 1789 1790 flags = type & ~SOCK_TYPE_MASK; 1791 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1792 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1793 1794 return sock_alloc_file(sock, flags, NULL); 1795 } 1796 1797 /* A hook for bpf progs to attach to and update socket protocol. 1798 * 1799 * A static noinline declaration here could cause the compiler to 1800 * optimize away the function. A global noinline declaration will 1801 * keep the definition, but may optimize away the callsite. 1802 * Therefore, __weak is needed to ensure that the call is still 1803 * emitted, by telling the compiler that we don't know what the 1804 * function might eventually be. 1805 */ 1806 1807 __bpf_hook_start(); 1808 1809 __weak noinline int update_socket_protocol(int family, int type, int protocol) 1810 { 1811 return protocol; 1812 } 1813 1814 __bpf_hook_end(); 1815 1816 int __sys_socket(int family, int type, int protocol) 1817 { 1818 struct socket *sock; 1819 int flags; 1820 1821 sock = __sys_socket_create(family, type, 1822 update_socket_protocol(family, type, protocol)); 1823 if (IS_ERR(sock)) 1824 return PTR_ERR(sock); 1825 1826 flags = type & ~SOCK_TYPE_MASK; 1827 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1828 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1829 1830 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK)); 1831 } 1832 1833 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) 1834 { 1835 return __sys_socket(family, type, protocol); 1836 } 1837 1838 /* 1839 * Create a pair of connected sockets. 1840 */ 1841 1842 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec) 1843 { 1844 struct socket *sock1, *sock2; 1845 int fd1, fd2, err; 1846 struct file *newfile1, *newfile2; 1847 int flags; 1848 1849 flags = type & ~SOCK_TYPE_MASK; 1850 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1851 return -EINVAL; 1852 type &= SOCK_TYPE_MASK; 1853 1854 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1855 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1856 1857 /* Reserve both descriptors before creating the sockets. */ 1858 fd1 = get_unused_fd_flags(flags); 1859 if (unlikely(fd1 < 0)) 1860 return fd1; 1861 1862 fd2 = get_unused_fd_flags(flags); 1863 if (unlikely(fd2 < 0)) { 1864 put_unused_fd(fd1); 1865 return fd2; 1866 } 1867 1868 /* 1869 * Obtain the first socket and check if the underlying protocol 1870 * supports the socketpair call. 1871 */ 1872 1873 err = sock_create(family, type, protocol, &sock1); 1874 if (unlikely(err < 0)) 1875 goto out; 1876 1877 err = sock_create(family, type, protocol, &sock2); 1878 if (unlikely(err < 0)) { 1879 sock_release(sock1); 1880 goto out; 1881 } 1882 1883 err = security_socket_socketpair(sock1, sock2); 1884 if (unlikely(err)) { 1885 sock_release(sock2); 1886 sock_release(sock1); 1887 goto out; 1888 } 1889 1890 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2); 1891 if (unlikely(err < 0)) { 1892 sock_release(sock2); 1893 sock_release(sock1); 1894 goto out; 1895 } 1896 1897 newfile1 = sock_alloc_file(sock1, flags, NULL); 1898 if (IS_ERR(newfile1)) { 1899 err = PTR_ERR(newfile1); 1900 sock_release(sock2); 1901 goto out; 1902 } 1903 1904 newfile2 = sock_alloc_file(sock2, flags, NULL); 1905 if (IS_ERR(newfile2)) { 1906 err = PTR_ERR(newfile2); 1907 fput(newfile1); 1908 goto out; 1909 } 1910 1911 /* Publish the descriptors now that it shouldn't fail. */ 1912 err = put_user(fd1, &usockvec[0]); 1913 if (!err) 1914 err = put_user(fd2, &usockvec[1]); 1915 if (err) { 1916 fput(newfile2); 1917 fput(newfile1); 1918 goto out; 1919 } 1920 1921 audit_fd_pair(fd1, fd2); 1922 1923 fd_install(fd1, newfile1); 1924 fd_install(fd2, newfile2); 1925 return 0; 1926 1927 out: 1928 put_unused_fd(fd2); 1929 put_unused_fd(fd1); 1930 return err; 1931 } 1932 1933 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol, 1934 int __user *, usockvec) 1935 { 1936 return __sys_socketpair(family, type, protocol, usockvec); 1937 } 1938 1939 int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address, 1940 int addrlen) 1941 { 1942 int err; 1943 1944 err = security_socket_bind(sock, (struct sockaddr *)address, 1945 addrlen); 1946 if (!err) 1947 err = READ_ONCE(sock->ops)->bind(sock, 1948 (struct sockaddr_unsized *)address, 1949 addrlen); 1950 return err; 1951 } 1952 1953 /* 1954 * Bind a name to a socket. Nothing much to do here since it's 1955 * the protocol's responsibility to handle the local address. 1956 * 1957 * We move the socket address to kernel space before we call 1958 * the protocol layer (having also checked the address is ok). 1959 */ 1960 1961 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) 1962 { 1963 struct socket *sock; 1964 struct sockaddr_storage address; 1965 CLASS(fd, f)(fd); 1966 int err; 1967 1968 if (fd_empty(f)) 1969 return -EBADF; 1970 sock = sock_from_file(fd_file(f)); 1971 if (unlikely(!sock)) 1972 return -ENOTSOCK; 1973 1974 err = move_addr_to_kernel(umyaddr, addrlen, &address); 1975 if (unlikely(err)) 1976 return err; 1977 1978 return __sys_bind_socket(sock, &address, addrlen); 1979 } 1980 1981 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) 1982 { 1983 return __sys_bind(fd, umyaddr, addrlen); 1984 } 1985 1986 /* 1987 * Perform a listen. Basically, we allow the protocol to do anything 1988 * necessary for a listen, and if that works, we mark the socket as 1989 * ready for listening. 1990 */ 1991 int __sys_listen_socket(struct socket *sock, int backlog) 1992 { 1993 int somaxconn, err; 1994 1995 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn); 1996 if ((unsigned int)backlog > somaxconn) 1997 backlog = somaxconn; 1998 1999 err = security_socket_listen(sock, backlog); 2000 if (!err) 2001 err = READ_ONCE(sock->ops)->listen(sock, backlog); 2002 return err; 2003 } 2004 2005 int __sys_listen(int fd, int backlog) 2006 { 2007 CLASS(fd, f)(fd); 2008 struct socket *sock; 2009 2010 if (fd_empty(f)) 2011 return -EBADF; 2012 sock = sock_from_file(fd_file(f)); 2013 if (unlikely(!sock)) 2014 return -ENOTSOCK; 2015 2016 return __sys_listen_socket(sock, backlog); 2017 } 2018 2019 SYSCALL_DEFINE2(listen, int, fd, int, backlog) 2020 { 2021 return __sys_listen(fd, backlog); 2022 } 2023 2024 struct file *do_accept(struct file *file, struct proto_accept_arg *arg, 2025 struct sockaddr __user *upeer_sockaddr, 2026 int __user *upeer_addrlen, int flags) 2027 { 2028 struct socket *sock, *newsock; 2029 struct file *newfile; 2030 int err, len; 2031 struct sockaddr_storage address; 2032 const struct proto_ops *ops; 2033 2034 sock = sock_from_file(file); 2035 if (!sock) 2036 return ERR_PTR(-ENOTSOCK); 2037 2038 newsock = sock_alloc(); 2039 if (!newsock) 2040 return ERR_PTR(-ENFILE); 2041 ops = READ_ONCE(sock->ops); 2042 2043 newsock->type = sock->type; 2044 newsock->ops = ops; 2045 2046 /* 2047 * We don't need try_module_get here, as the listening socket (sock) 2048 * has the protocol module (sock->ops->owner) held. 2049 */ 2050 __module_get(ops->owner); 2051 2052 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name); 2053 if (IS_ERR(newfile)) 2054 return newfile; 2055 2056 err = security_socket_accept(sock, newsock); 2057 if (err) 2058 goto out_fd; 2059 2060 arg->flags |= sock->file->f_flags; 2061 err = ops->accept(sock, newsock, arg); 2062 if (err < 0) 2063 goto out_fd; 2064 2065 if (upeer_sockaddr) { 2066 len = ops->getname(newsock, (struct sockaddr *)&address, 2); 2067 if (len < 0) { 2068 err = -ECONNABORTED; 2069 goto out_fd; 2070 } 2071 err = move_addr_to_user(&address, 2072 len, upeer_sockaddr, upeer_addrlen); 2073 if (err < 0) 2074 goto out_fd; 2075 } 2076 2077 /* File flags are not inherited via accept() unlike another OSes. */ 2078 return newfile; 2079 out_fd: 2080 fput(newfile); 2081 return ERR_PTR(err); 2082 } 2083 2084 static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr, 2085 int __user *upeer_addrlen, int flags) 2086 { 2087 struct proto_accept_arg arg = { }; 2088 2089 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 2090 return -EINVAL; 2091 2092 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 2093 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 2094 2095 return FD_ADD(flags, do_accept(file, &arg, upeer_sockaddr, upeer_addrlen, flags)); 2096 } 2097 2098 /* 2099 * For accept, we attempt to create a new socket, set up the link 2100 * with the client, wake up the client, then return the new 2101 * connected fd. We collect the address of the connector in kernel 2102 * space and move it to user at the very end. This is unclean because 2103 * we open the socket then return an error. 2104 * 2105 * 1003.1g adds the ability to recvmsg() to query connection pending 2106 * status. We need to add that support in a way that's 2107 * clean when we restructure accept also. 2108 */ 2109 2110 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, 2111 int __user *upeer_addrlen, int flags) 2112 { 2113 CLASS(fd, f)(fd); 2114 2115 if (fd_empty(f)) 2116 return -EBADF; 2117 return __sys_accept4_file(fd_file(f), upeer_sockaddr, 2118 upeer_addrlen, flags); 2119 } 2120 2121 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, 2122 int __user *, upeer_addrlen, int, flags) 2123 { 2124 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags); 2125 } 2126 2127 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, 2128 int __user *, upeer_addrlen) 2129 { 2130 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0); 2131 } 2132 2133 int connect_socket(struct socket *sock, struct sockaddr_storage *address, 2134 int addrlen, int flags) 2135 { 2136 int err; 2137 2138 err = security_socket_connect(sock, (struct sockaddr *)address, 2139 addrlen); 2140 if (err) 2141 return err; 2142 2143 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address, 2144 addrlen, flags); 2145 } 2146 2147 /* 2148 * Attempt to connect to a socket with the server address. The address 2149 * is in user space so we verify it is OK and move it to kernel space. 2150 * 2151 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to 2152 * break bindings 2153 * 2154 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and 2155 * other SEQPACKET protocols that take time to connect() as it doesn't 2156 * include the -EINPROGRESS status for such sockets. 2157 */ 2158 2159 int __sys_connect_file(struct file *file, struct sockaddr_storage *address, 2160 int addrlen, int file_flags) 2161 { 2162 struct socket *sock; 2163 2164 sock = sock_from_file(file); 2165 if (!sock) 2166 return -ENOTSOCK; 2167 2168 return connect_socket(sock, address, addrlen, 2169 sock->file->f_flags | file_flags); 2170 } 2171 2172 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) 2173 { 2174 struct sockaddr_storage address; 2175 CLASS(fd, f)(fd); 2176 int ret; 2177 2178 if (fd_empty(f)) 2179 return -EBADF; 2180 2181 ret = move_addr_to_kernel(uservaddr, addrlen, &address); 2182 if (ret) 2183 return ret; 2184 2185 return __sys_connect_file(fd_file(f), &address, addrlen, 0); 2186 } 2187 2188 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr, 2189 int, addrlen) 2190 { 2191 return __sys_connect(fd, uservaddr, addrlen); 2192 } 2193 2194 int do_getsockname(struct socket *sock, int peer, 2195 struct sockaddr __user *usockaddr, int __user *usockaddr_len) 2196 { 2197 struct sockaddr_storage address; 2198 int err; 2199 2200 if (peer) 2201 err = security_socket_getpeername(sock); 2202 else 2203 err = security_socket_getsockname(sock); 2204 if (err) 2205 return err; 2206 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer); 2207 if (err < 0) 2208 return err; 2209 /* "err" is actually length in this case */ 2210 return move_addr_to_user(&address, err, usockaddr, usockaddr_len); 2211 } 2212 2213 /* 2214 * Get the remote or local address ('name') of a socket object. Move the 2215 * obtained name to user space. 2216 */ 2217 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr, 2218 int __user *usockaddr_len, int peer) 2219 { 2220 struct socket *sock; 2221 CLASS(fd, f)(fd); 2222 2223 if (fd_empty(f)) 2224 return -EBADF; 2225 sock = sock_from_file(fd_file(f)); 2226 if (unlikely(!sock)) 2227 return -ENOTSOCK; 2228 return do_getsockname(sock, peer, usockaddr, usockaddr_len); 2229 } 2230 2231 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr, 2232 int __user *, usockaddr_len) 2233 { 2234 return __sys_getsockname(fd, usockaddr, usockaddr_len, 0); 2235 } 2236 2237 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, 2238 int __user *, usockaddr_len) 2239 { 2240 return __sys_getsockname(fd, usockaddr, usockaddr_len, 1); 2241 } 2242 2243 /* 2244 * Send a datagram to a given address. We move the address into kernel 2245 * space and check the user space data area is readable before invoking 2246 * the protocol. 2247 */ 2248 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags, 2249 struct sockaddr __user *addr, int addr_len) 2250 { 2251 struct socket *sock; 2252 struct sockaddr_storage address; 2253 int err; 2254 struct msghdr msg; 2255 2256 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter); 2257 if (unlikely(err)) 2258 return err; 2259 2260 CLASS(fd, f)(fd); 2261 if (fd_empty(f)) 2262 return -EBADF; 2263 sock = sock_from_file(fd_file(f)); 2264 if (unlikely(!sock)) 2265 return -ENOTSOCK; 2266 2267 msg.msg_name = NULL; 2268 msg.msg_control = NULL; 2269 msg.msg_controllen = 0; 2270 msg.msg_namelen = 0; 2271 msg.msg_ubuf = NULL; 2272 if (addr) { 2273 err = move_addr_to_kernel(addr, addr_len, &address); 2274 if (err < 0) 2275 return err; 2276 msg.msg_name = (struct sockaddr *)&address; 2277 msg.msg_namelen = addr_len; 2278 } 2279 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2280 if (sock->file->f_flags & O_NONBLOCK) 2281 flags |= MSG_DONTWAIT; 2282 msg.msg_flags = flags; 2283 return __sock_sendmsg(sock, &msg); 2284 } 2285 2286 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, 2287 unsigned int, flags, struct sockaddr __user *, addr, 2288 int, addr_len) 2289 { 2290 return __sys_sendto(fd, buff, len, flags, addr, addr_len); 2291 } 2292 2293 /* 2294 * Send a datagram down a socket. 2295 */ 2296 2297 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, 2298 unsigned int, flags) 2299 { 2300 return __sys_sendto(fd, buff, len, flags, NULL, 0); 2301 } 2302 2303 /* 2304 * Receive a frame from the socket and optionally record the address of the 2305 * sender. We verify the buffers are writable and if needed move the 2306 * sender address from kernel to user space. 2307 */ 2308 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags, 2309 struct sockaddr __user *addr, int __user *addr_len) 2310 { 2311 struct sockaddr_storage address; 2312 struct msghdr msg = { 2313 /* Save some cycles and don't copy the address if not needed */ 2314 .msg_name = addr ? (struct sockaddr *)&address : NULL, 2315 }; 2316 struct socket *sock; 2317 int err, err2; 2318 2319 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter); 2320 if (unlikely(err)) 2321 return err; 2322 2323 CLASS(fd, f)(fd); 2324 2325 if (fd_empty(f)) 2326 return -EBADF; 2327 sock = sock_from_file(fd_file(f)); 2328 if (unlikely(!sock)) 2329 return -ENOTSOCK; 2330 2331 if (sock->file->f_flags & O_NONBLOCK) 2332 flags |= MSG_DONTWAIT; 2333 err = sock_recvmsg(sock, &msg, flags); 2334 2335 if (err >= 0 && addr != NULL) { 2336 err2 = move_addr_to_user(&address, 2337 msg.msg_namelen, addr, addr_len); 2338 if (err2 < 0) 2339 err = err2; 2340 } 2341 return err; 2342 } 2343 2344 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size, 2345 unsigned int, flags, struct sockaddr __user *, addr, 2346 int __user *, addr_len) 2347 { 2348 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len); 2349 } 2350 2351 /* 2352 * Receive a datagram from a socket. 2353 */ 2354 2355 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size, 2356 unsigned int, flags) 2357 { 2358 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 2359 } 2360 2361 static bool sock_use_custom_sol_socket(const struct socket *sock) 2362 { 2363 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags); 2364 } 2365 2366 int do_sock_setsockopt(struct socket *sock, bool compat, int level, 2367 int optname, sockptr_t optval, int optlen) 2368 { 2369 const struct proto_ops *ops; 2370 char *kernel_optval = NULL; 2371 int err; 2372 2373 if (optlen < 0) 2374 return -EINVAL; 2375 2376 err = security_socket_setsockopt(sock, level, optname); 2377 if (err) 2378 goto out_put; 2379 2380 if (!compat) 2381 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname, 2382 optval, &optlen, 2383 &kernel_optval); 2384 if (err < 0) 2385 goto out_put; 2386 if (err > 0) { 2387 err = 0; 2388 goto out_put; 2389 } 2390 2391 if (kernel_optval) 2392 optval = KERNEL_SOCKPTR(kernel_optval); 2393 ops = READ_ONCE(sock->ops); 2394 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock)) 2395 err = sock_setsockopt(sock, level, optname, optval, optlen); 2396 else if (unlikely(!ops->setsockopt)) 2397 err = -EOPNOTSUPP; 2398 else 2399 err = ops->setsockopt(sock, level, optname, optval, 2400 optlen); 2401 kfree(kernel_optval); 2402 out_put: 2403 return err; 2404 } 2405 EXPORT_SYMBOL(do_sock_setsockopt); 2406 2407 /* Set a socket option. Because we don't know the option lengths we have 2408 * to pass the user mode parameter for the protocols to sort out. 2409 */ 2410 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, 2411 int optlen) 2412 { 2413 sockptr_t optval = USER_SOCKPTR(user_optval); 2414 bool compat = in_compat_syscall(); 2415 struct socket *sock; 2416 CLASS(fd, f)(fd); 2417 2418 if (fd_empty(f)) 2419 return -EBADF; 2420 sock = sock_from_file(fd_file(f)); 2421 if (unlikely(!sock)) 2422 return -ENOTSOCK; 2423 2424 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen); 2425 } 2426 2427 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, 2428 char __user *, optval, int, optlen) 2429 { 2430 return __sys_setsockopt(fd, level, optname, optval, optlen); 2431 } 2432 2433 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level, 2434 int optname)); 2435 2436 /* 2437 * Initialize a sockopt_t from sockptr optval/optlen, setting up iov_iter 2438 * for both input and output directions. 2439 * It is important to remember that both iov points to the same data, but, 2440 * .iter_in is read-only and .iter_out is write-only by the protocol callbacks 2441 */ 2442 int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval, 2443 sockptr_t optlen, struct kvec *kvec) 2444 { 2445 int koptlen; 2446 2447 if (copy_from_sockptr(&koptlen, optlen, sizeof(int))) 2448 return -EFAULT; 2449 2450 if (koptlen < 0) 2451 return -EINVAL; 2452 2453 if (optval.is_kernel) { 2454 kvec->iov_base = optval.kernel; 2455 kvec->iov_len = koptlen; 2456 iov_iter_kvec(&opt->iter_out, ITER_DEST, kvec, 1, koptlen); 2457 iov_iter_kvec(&opt->iter_in, ITER_SOURCE, kvec, 1, koptlen); 2458 } else { 2459 iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval.user, koptlen); 2460 iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval.user, 2461 koptlen); 2462 } 2463 opt->optlen = koptlen; 2464 2465 return 0; 2466 } 2467 2468 int do_sock_getsockopt(struct socket *sock, bool compat, int level, 2469 int optname, sockptr_t optval, sockptr_t optlen) 2470 { 2471 int max_optlen __maybe_unused = 0; 2472 const struct proto_ops *ops; 2473 struct kvec kvec; 2474 sockopt_t opt; 2475 int err; 2476 2477 err = security_socket_getsockopt(sock, level, optname); 2478 if (err) 2479 return err; 2480 2481 if (!compat) 2482 copy_from_sockptr(&max_optlen, optlen, sizeof(int)); 2483 2484 ops = READ_ONCE(sock->ops); 2485 if (level == SOL_SOCKET) { 2486 err = sk_getsockopt(sock->sk, level, optname, optval, optlen); 2487 } else if (ops->getsockopt_iter) { 2488 err = sockptr_to_sockopt(&opt, optval, optlen, &kvec); 2489 if (err) 2490 return err; 2491 2492 err = ops->getsockopt_iter(sock, level, optname, &opt); 2493 2494 /* Always write back optlen, even on failure. Some protocols 2495 * (e.g. CAN raw) return -ERANGE and set optlen to the 2496 * required buffer size so userspace can discover it. 2497 */ 2498 if (copy_to_sockptr(optlen, &opt.optlen, sizeof(int))) 2499 return -EFAULT; 2500 } else if (ops->getsockopt) { 2501 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel, 2502 "Invalid argument type")) 2503 return -EOPNOTSUPP; 2504 2505 err = ops->getsockopt(sock, level, optname, optval.user, 2506 optlen.user); 2507 } else { 2508 err = -EOPNOTSUPP; 2509 } 2510 2511 if (!compat) 2512 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, 2513 optval, optlen, max_optlen, 2514 err); 2515 2516 return err; 2517 } 2518 EXPORT_SYMBOL(do_sock_getsockopt); 2519 2520 /* 2521 * Get a socket option. Because we don't know the option lengths we have 2522 * to pass a user mode parameter for the protocols to sort out. 2523 */ 2524 int __sys_getsockopt(int fd, int level, int optname, char __user *optval, 2525 int __user *optlen) 2526 { 2527 struct socket *sock; 2528 CLASS(fd, f)(fd); 2529 2530 if (fd_empty(f)) 2531 return -EBADF; 2532 sock = sock_from_file(fd_file(f)); 2533 if (unlikely(!sock)) 2534 return -ENOTSOCK; 2535 2536 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname, 2537 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 2538 } 2539 2540 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, 2541 char __user *, optval, int __user *, optlen) 2542 { 2543 return __sys_getsockopt(fd, level, optname, optval, optlen); 2544 } 2545 2546 /* 2547 * Shutdown a socket. 2548 */ 2549 2550 int __sys_shutdown_sock(struct socket *sock, int how) 2551 { 2552 int err; 2553 2554 err = security_socket_shutdown(sock, how); 2555 if (!err) 2556 err = READ_ONCE(sock->ops)->shutdown(sock, how); 2557 2558 return err; 2559 } 2560 2561 int __sys_shutdown(int fd, int how) 2562 { 2563 struct socket *sock; 2564 CLASS(fd, f)(fd); 2565 2566 if (fd_empty(f)) 2567 return -EBADF; 2568 sock = sock_from_file(fd_file(f)); 2569 if (unlikely(!sock)) 2570 return -ENOTSOCK; 2571 2572 return __sys_shutdown_sock(sock, how); 2573 } 2574 2575 SYSCALL_DEFINE2(shutdown, int, fd, int, how) 2576 { 2577 return __sys_shutdown(fd, how); 2578 } 2579 2580 /* A couple of helpful macros for getting the address of the 32/64 bit 2581 * fields which are the same type (int / unsigned) on our platforms. 2582 */ 2583 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 2584 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 2585 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 2586 2587 struct used_address { 2588 struct sockaddr_storage name; 2589 unsigned int name_len; 2590 }; 2591 2592 int __copy_msghdr(struct msghdr *kmsg, 2593 struct user_msghdr *msg, 2594 struct sockaddr __user **save_addr) 2595 { 2596 ssize_t err; 2597 2598 kmsg->msg_control_is_user = true; 2599 kmsg->msg_get_inq = 0; 2600 kmsg->msg_control_user = msg->msg_control; 2601 kmsg->msg_controllen = msg->msg_controllen; 2602 kmsg->msg_flags = msg->msg_flags; 2603 2604 kmsg->msg_namelen = msg->msg_namelen; 2605 if (!msg->msg_name) 2606 kmsg->msg_namelen = 0; 2607 2608 if (kmsg->msg_namelen < 0) 2609 return -EINVAL; 2610 2611 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 2612 kmsg->msg_namelen = sizeof(struct sockaddr_storage); 2613 2614 if (save_addr) 2615 *save_addr = msg->msg_name; 2616 2617 if (msg->msg_name && kmsg->msg_namelen) { 2618 if (!save_addr) { 2619 err = move_addr_to_kernel(msg->msg_name, 2620 kmsg->msg_namelen, 2621 kmsg->msg_name); 2622 if (err < 0) 2623 return err; 2624 } 2625 } else { 2626 kmsg->msg_name = NULL; 2627 kmsg->msg_namelen = 0; 2628 } 2629 2630 if (msg->msg_iovlen > UIO_MAXIOV) 2631 return -EMSGSIZE; 2632 2633 kmsg->msg_ubuf = NULL; 2634 return 0; 2635 } 2636 2637 static int copy_msghdr_from_user(struct msghdr *kmsg, 2638 struct user_msghdr __user *umsg, 2639 struct sockaddr __user **save_addr, 2640 struct iovec **iov) 2641 { 2642 struct user_msghdr msg; 2643 ssize_t err; 2644 2645 if (copy_from_user(&msg, umsg, sizeof(*umsg))) 2646 return -EFAULT; 2647 2648 err = __copy_msghdr(kmsg, &msg, save_addr); 2649 if (err) 2650 return err; 2651 2652 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE, 2653 msg.msg_iov, msg.msg_iovlen, 2654 UIO_FASTIOV, iov, &kmsg->msg_iter); 2655 return err < 0 ? err : 0; 2656 } 2657 2658 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys, 2659 unsigned int flags, struct used_address *used_address, 2660 unsigned int allowed_msghdr_flags) 2661 { 2662 unsigned char ctl[sizeof(struct cmsghdr) + 20] 2663 __aligned(sizeof(__kernel_size_t)); 2664 /* 20 is size of ipv6_pktinfo */ 2665 unsigned char *ctl_buf = ctl; 2666 int ctl_len; 2667 ssize_t err; 2668 2669 err = -ENOBUFS; 2670 2671 if (msg_sys->msg_controllen > INT_MAX) 2672 goto out; 2673 flags |= (msg_sys->msg_flags & allowed_msghdr_flags); 2674 ctl_len = msg_sys->msg_controllen; 2675 if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 2676 err = 2677 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl, 2678 sizeof(ctl)); 2679 if (err) 2680 goto out; 2681 ctl_buf = msg_sys->msg_control; 2682 ctl_len = msg_sys->msg_controllen; 2683 } else if (ctl_len) { 2684 BUILD_BUG_ON(sizeof(struct cmsghdr) != 2685 CMSG_ALIGN(sizeof(struct cmsghdr))); 2686 if (ctl_len > sizeof(ctl)) { 2687 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 2688 if (ctl_buf == NULL) 2689 goto out; 2690 } 2691 err = -EFAULT; 2692 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len)) 2693 goto out_freectl; 2694 msg_sys->msg_control = ctl_buf; 2695 msg_sys->msg_control_is_user = false; 2696 } 2697 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2698 msg_sys->msg_flags = flags; 2699 2700 if (sock->file->f_flags & O_NONBLOCK) 2701 msg_sys->msg_flags |= MSG_DONTWAIT; 2702 /* 2703 * If this is sendmmsg() and current destination address is same as 2704 * previously succeeded address, omit asking LSM's decision. 2705 * used_address->name_len is initialized to UINT_MAX so that the first 2706 * destination address never matches. 2707 */ 2708 if (used_address && msg_sys->msg_name && 2709 used_address->name_len == msg_sys->msg_namelen && 2710 !memcmp(&used_address->name, msg_sys->msg_name, 2711 used_address->name_len)) { 2712 err = sock_sendmsg_nosec(sock, msg_sys); 2713 goto out_freectl; 2714 } 2715 err = __sock_sendmsg(sock, msg_sys); 2716 /* 2717 * If this is sendmmsg() and sending to current destination address was 2718 * successful, remember it. 2719 */ 2720 if (used_address && err >= 0) { 2721 used_address->name_len = msg_sys->msg_namelen; 2722 if (msg_sys->msg_name) 2723 memcpy(&used_address->name, msg_sys->msg_name, 2724 used_address->name_len); 2725 } 2726 2727 out_freectl: 2728 if (ctl_buf != ctl) 2729 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 2730 out: 2731 return err; 2732 } 2733 2734 static int sendmsg_copy_msghdr(struct msghdr *msg, 2735 struct user_msghdr __user *umsg, unsigned flags, 2736 struct iovec **iov) 2737 { 2738 int err; 2739 2740 if (flags & MSG_CMSG_COMPAT) { 2741 struct compat_msghdr __user *msg_compat; 2742 2743 msg_compat = (struct compat_msghdr __user *) umsg; 2744 err = get_compat_msghdr(msg, msg_compat, NULL, iov); 2745 } else { 2746 err = copy_msghdr_from_user(msg, umsg, NULL, iov); 2747 } 2748 if (err < 0) 2749 return err; 2750 2751 return 0; 2752 } 2753 2754 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg, 2755 struct msghdr *msg_sys, unsigned int flags, 2756 struct used_address *used_address, 2757 unsigned int allowed_msghdr_flags) 2758 { 2759 struct sockaddr_storage address; 2760 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2761 ssize_t err; 2762 2763 msg_sys->msg_name = &address; 2764 2765 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov); 2766 if (err < 0) 2767 return err; 2768 2769 err = ____sys_sendmsg(sock, msg_sys, flags, used_address, 2770 allowed_msghdr_flags); 2771 kfree(iov); 2772 return err; 2773 } 2774 2775 /* 2776 * BSD sendmsg interface 2777 */ 2778 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg, 2779 unsigned int flags) 2780 { 2781 return ____sys_sendmsg(sock, msg, flags, NULL, 0); 2782 } 2783 2784 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2785 bool forbid_cmsg_compat) 2786 { 2787 struct msghdr msg_sys; 2788 struct socket *sock; 2789 2790 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2791 return -EINVAL; 2792 2793 CLASS(fd, f)(fd); 2794 2795 if (fd_empty(f)) 2796 return -EBADF; 2797 sock = sock_from_file(fd_file(f)); 2798 if (unlikely(!sock)) 2799 return -ENOTSOCK; 2800 2801 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0); 2802 } 2803 2804 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags) 2805 { 2806 return __sys_sendmsg(fd, msg, flags, true); 2807 } 2808 2809 /* 2810 * Linux sendmmsg interface 2811 */ 2812 2813 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, 2814 unsigned int flags, bool forbid_cmsg_compat) 2815 { 2816 int err, datagrams; 2817 struct socket *sock; 2818 struct mmsghdr __user *entry; 2819 struct compat_mmsghdr __user *compat_entry; 2820 struct msghdr msg_sys; 2821 struct used_address used_address; 2822 unsigned int oflags = flags; 2823 2824 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2825 return -EINVAL; 2826 2827 if (vlen > UIO_MAXIOV) 2828 vlen = UIO_MAXIOV; 2829 2830 datagrams = 0; 2831 2832 CLASS(fd, f)(fd); 2833 2834 if (fd_empty(f)) 2835 return -EBADF; 2836 sock = sock_from_file(fd_file(f)); 2837 if (unlikely(!sock)) 2838 return -ENOTSOCK; 2839 2840 used_address.name_len = UINT_MAX; 2841 entry = mmsg; 2842 compat_entry = (struct compat_mmsghdr __user *)mmsg; 2843 err = 0; 2844 flags |= MSG_BATCH; 2845 2846 while (datagrams < vlen) { 2847 if (datagrams == vlen - 1) 2848 flags = oflags; 2849 2850 if (MSG_CMSG_COMPAT & flags) { 2851 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry, 2852 &msg_sys, flags, &used_address, MSG_EOR); 2853 if (err < 0) 2854 break; 2855 err = put_user(err, &compat_entry->msg_len); 2856 ++compat_entry; 2857 } else { 2858 err = ___sys_sendmsg(sock, 2859 (struct user_msghdr __user *)entry, 2860 &msg_sys, flags, &used_address, MSG_EOR); 2861 if (err < 0) 2862 break; 2863 err = put_user(err, &entry->msg_len); 2864 ++entry; 2865 } 2866 2867 if (err) 2868 break; 2869 ++datagrams; 2870 if (msg_data_left(&msg_sys)) 2871 break; 2872 cond_resched(); 2873 } 2874 2875 /* We only return an error if no datagrams were able to be sent */ 2876 if (datagrams != 0) 2877 return datagrams; 2878 2879 return err; 2880 } 2881 2882 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, 2883 unsigned int, vlen, unsigned int, flags) 2884 { 2885 return __sys_sendmmsg(fd, mmsg, vlen, flags, true); 2886 } 2887 2888 static int recvmsg_copy_msghdr(struct msghdr *msg, 2889 struct user_msghdr __user *umsg, unsigned flags, 2890 struct sockaddr __user **uaddr, 2891 struct iovec **iov) 2892 { 2893 ssize_t err; 2894 2895 if (MSG_CMSG_COMPAT & flags) { 2896 struct compat_msghdr __user *msg_compat; 2897 2898 msg_compat = (struct compat_msghdr __user *) umsg; 2899 err = get_compat_msghdr(msg, msg_compat, uaddr, iov); 2900 } else { 2901 err = copy_msghdr_from_user(msg, umsg, uaddr, iov); 2902 } 2903 if (err < 0) 2904 return err; 2905 2906 return 0; 2907 } 2908 2909 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys, 2910 struct user_msghdr __user *msg, 2911 struct sockaddr __user *uaddr, 2912 unsigned int flags, int nosec) 2913 { 2914 struct compat_msghdr __user *msg_compat = 2915 (struct compat_msghdr __user *) msg; 2916 int __user *uaddr_len = COMPAT_NAMELEN(msg); 2917 struct sockaddr_storage addr; 2918 unsigned long cmsg_ptr; 2919 int len; 2920 ssize_t err; 2921 2922 msg_sys->msg_name = &addr; 2923 cmsg_ptr = (unsigned long)msg_sys->msg_control; 2924 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 2925 2926 /* We assume all kernel code knows the size of sockaddr_storage */ 2927 msg_sys->msg_namelen = 0; 2928 2929 if (sock->file->f_flags & O_NONBLOCK) 2930 flags |= MSG_DONTWAIT; 2931 2932 if (unlikely(nosec)) 2933 err = sock_recvmsg_nosec(sock, msg_sys, flags); 2934 else 2935 err = sock_recvmsg(sock, msg_sys, flags); 2936 2937 if (err < 0) 2938 goto out; 2939 len = err; 2940 2941 if (uaddr != NULL) { 2942 err = move_addr_to_user(&addr, 2943 msg_sys->msg_namelen, uaddr, 2944 uaddr_len); 2945 if (err < 0) 2946 goto out; 2947 } 2948 err = put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT), 2949 COMPAT_FLAGS(msg)); 2950 if (err) 2951 goto out; 2952 if (MSG_CMSG_COMPAT & flags) 2953 err = put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2954 &msg_compat->msg_controllen); 2955 else 2956 err = put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2957 &msg->msg_controllen); 2958 if (err) 2959 goto out; 2960 err = len; 2961 out: 2962 return err; 2963 } 2964 2965 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg, 2966 struct msghdr *msg_sys, unsigned int flags, int nosec) 2967 { 2968 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2969 /* user mode address pointers */ 2970 struct sockaddr __user *uaddr; 2971 ssize_t err; 2972 2973 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov); 2974 if (err < 0) 2975 return err; 2976 2977 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec); 2978 kfree(iov); 2979 return err; 2980 } 2981 2982 /* 2983 * BSD recvmsg interface 2984 */ 2985 2986 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg, 2987 struct user_msghdr __user *umsg, 2988 struct sockaddr __user *uaddr, unsigned int flags) 2989 { 2990 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0); 2991 } 2992 2993 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2994 bool forbid_cmsg_compat) 2995 { 2996 struct msghdr msg_sys; 2997 struct socket *sock; 2998 2999 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 3000 return -EINVAL; 3001 3002 CLASS(fd, f)(fd); 3003 3004 if (fd_empty(f)) 3005 return -EBADF; 3006 sock = sock_from_file(fd_file(f)); 3007 if (unlikely(!sock)) 3008 return -ENOTSOCK; 3009 3010 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0); 3011 } 3012 3013 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg, 3014 unsigned int, flags) 3015 { 3016 return __sys_recvmsg(fd, msg, flags, true); 3017 } 3018 3019 /* 3020 * Linux recvmmsg interface 3021 */ 3022 3023 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3024 unsigned int vlen, unsigned int flags, 3025 struct timespec64 *timeout) 3026 { 3027 int err = 0, datagrams; 3028 struct socket *sock; 3029 struct mmsghdr __user *entry; 3030 struct compat_mmsghdr __user *compat_entry; 3031 struct msghdr msg_sys; 3032 struct timespec64 end_time; 3033 struct timespec64 timeout64; 3034 3035 if (timeout && 3036 poll_select_set_timeout(&end_time, timeout->tv_sec, 3037 timeout->tv_nsec)) 3038 return -EINVAL; 3039 3040 datagrams = 0; 3041 3042 CLASS(fd, f)(fd); 3043 3044 if (fd_empty(f)) 3045 return -EBADF; 3046 sock = sock_from_file(fd_file(f)); 3047 if (unlikely(!sock)) 3048 return -ENOTSOCK; 3049 3050 if (likely(!(flags & MSG_ERRQUEUE))) { 3051 err = sock_error(sock->sk); 3052 if (err) 3053 return err; 3054 } 3055 3056 entry = mmsg; 3057 compat_entry = (struct compat_mmsghdr __user *)mmsg; 3058 3059 while (datagrams < vlen) { 3060 /* 3061 * No need to ask LSM for more than the first datagram. 3062 */ 3063 if (MSG_CMSG_COMPAT & flags) { 3064 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry, 3065 &msg_sys, flags & ~MSG_WAITFORONE, 3066 datagrams); 3067 if (err < 0) 3068 break; 3069 err = put_user(err, &compat_entry->msg_len); 3070 ++compat_entry; 3071 } else { 3072 err = ___sys_recvmsg(sock, 3073 (struct user_msghdr __user *)entry, 3074 &msg_sys, flags & ~MSG_WAITFORONE, 3075 datagrams); 3076 if (err < 0) 3077 break; 3078 err = put_user(err, &entry->msg_len); 3079 ++entry; 3080 } 3081 3082 if (err) 3083 break; 3084 ++datagrams; 3085 3086 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ 3087 if (flags & MSG_WAITFORONE) 3088 flags |= MSG_DONTWAIT; 3089 3090 if (timeout) { 3091 ktime_get_ts64(&timeout64); 3092 *timeout = timespec64_sub(end_time, timeout64); 3093 if (timeout->tv_sec < 0) { 3094 timeout->tv_sec = timeout->tv_nsec = 0; 3095 break; 3096 } 3097 3098 /* Timeout, return less than vlen datagrams */ 3099 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0) 3100 break; 3101 } 3102 3103 /* Out of band data, return right away */ 3104 if (msg_sys.msg_flags & MSG_OOB) 3105 break; 3106 cond_resched(); 3107 } 3108 3109 if (err == 0) 3110 return datagrams; 3111 3112 if (datagrams == 0) 3113 return err; 3114 3115 /* 3116 * We may return less entries than requested (vlen) if the 3117 * sock is non block and there aren't enough datagrams... 3118 */ 3119 if (err != -EAGAIN) { 3120 /* 3121 * ... or if recvmsg returns an error after we 3122 * received some datagrams, where we record the 3123 * error to return on the next call or if the 3124 * app asks about it using getsockopt(SO_ERROR). 3125 */ 3126 WRITE_ONCE(sock->sk->sk_err, -err); 3127 } 3128 return datagrams; 3129 } 3130 3131 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3132 unsigned int vlen, unsigned int flags, 3133 struct __kernel_timespec __user *timeout, 3134 struct old_timespec32 __user *timeout32) 3135 { 3136 int datagrams; 3137 struct timespec64 timeout_sys; 3138 3139 if (timeout && get_timespec64(&timeout_sys, timeout)) 3140 return -EFAULT; 3141 3142 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32)) 3143 return -EFAULT; 3144 3145 if (!timeout && !timeout32) 3146 return do_recvmmsg(fd, mmsg, vlen, flags, NULL); 3147 3148 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys); 3149 3150 if (datagrams <= 0) 3151 return datagrams; 3152 3153 if (timeout && put_timespec64(&timeout_sys, timeout)) 3154 datagrams = -EFAULT; 3155 3156 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32)) 3157 datagrams = -EFAULT; 3158 3159 return datagrams; 3160 } 3161 3162 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg, 3163 unsigned int, vlen, unsigned int, flags, 3164 struct __kernel_timespec __user *, timeout) 3165 { 3166 if (flags & MSG_CMSG_COMPAT) 3167 return -EINVAL; 3168 3169 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL); 3170 } 3171 3172 #ifdef CONFIG_COMPAT_32BIT_TIME 3173 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg, 3174 unsigned int, vlen, unsigned int, flags, 3175 struct old_timespec32 __user *, timeout) 3176 { 3177 if (flags & MSG_CMSG_COMPAT) 3178 return -EINVAL; 3179 3180 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout); 3181 } 3182 #endif 3183 3184 #ifdef __ARCH_WANT_SYS_SOCKETCALL 3185 /* Argument list sizes for sys_socketcall */ 3186 #define AL(x) ((x) * sizeof(unsigned long)) 3187 static const unsigned char nargs[21] = { 3188 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), 3189 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), 3190 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), 3191 AL(4), AL(5), AL(4) 3192 }; 3193 3194 #undef AL 3195 3196 /* 3197 * System call vectors. 3198 * 3199 * Argument checking cleaned up. Saved 20% in size. 3200 * This function doesn't need to set the kernel lock because 3201 * it is set by the callees. 3202 */ 3203 3204 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) 3205 { 3206 unsigned long a[AUDITSC_ARGS]; 3207 unsigned long a0, a1; 3208 int err; 3209 unsigned int len; 3210 3211 if (call < 1 || call > SYS_SENDMMSG) 3212 return -EINVAL; 3213 call = array_index_nospec(call, SYS_SENDMMSG + 1); 3214 3215 len = nargs[call]; 3216 if (len > sizeof(a)) 3217 return -EINVAL; 3218 3219 /* copy_from_user should be SMP safe. */ 3220 if (copy_from_user(a, args, len)) 3221 return -EFAULT; 3222 3223 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a); 3224 if (err) 3225 return err; 3226 3227 a0 = a[0]; 3228 a1 = a[1]; 3229 3230 switch (call) { 3231 case SYS_SOCKET: 3232 err = __sys_socket(a0, a1, a[2]); 3233 break; 3234 case SYS_BIND: 3235 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]); 3236 break; 3237 case SYS_CONNECT: 3238 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 3239 break; 3240 case SYS_LISTEN: 3241 err = __sys_listen(a0, a1); 3242 break; 3243 case SYS_ACCEPT: 3244 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3245 (int __user *)a[2], 0); 3246 break; 3247 case SYS_GETSOCKNAME: 3248 err = 3249 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3250 (int __user *)a[2], 0); 3251 break; 3252 case SYS_GETPEERNAME: 3253 err = 3254 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3255 (int __user *)a[2], 1); 3256 break; 3257 case SYS_SOCKETPAIR: 3258 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]); 3259 break; 3260 case SYS_SEND: 3261 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3262 NULL, 0); 3263 break; 3264 case SYS_SENDTO: 3265 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3266 (struct sockaddr __user *)a[4], a[5]); 3267 break; 3268 case SYS_RECV: 3269 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3270 NULL, NULL); 3271 break; 3272 case SYS_RECVFROM: 3273 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3274 (struct sockaddr __user *)a[4], 3275 (int __user *)a[5]); 3276 break; 3277 case SYS_SHUTDOWN: 3278 err = __sys_shutdown(a0, a1); 3279 break; 3280 case SYS_SETSOCKOPT: 3281 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3], 3282 a[4]); 3283 break; 3284 case SYS_GETSOCKOPT: 3285 err = 3286 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3], 3287 (int __user *)a[4]); 3288 break; 3289 case SYS_SENDMSG: 3290 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1, 3291 a[2], true); 3292 break; 3293 case SYS_SENDMMSG: 3294 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], 3295 a[3], true); 3296 break; 3297 case SYS_RECVMSG: 3298 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1, 3299 a[2], true); 3300 break; 3301 case SYS_RECVMMSG: 3302 if (IS_ENABLED(CONFIG_64BIT)) 3303 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3304 a[2], a[3], 3305 (struct __kernel_timespec __user *)a[4], 3306 NULL); 3307 else 3308 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3309 a[2], a[3], NULL, 3310 (struct old_timespec32 __user *)a[4]); 3311 break; 3312 case SYS_ACCEPT4: 3313 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3314 (int __user *)a[2], a[3]); 3315 break; 3316 default: 3317 err = -EINVAL; 3318 break; 3319 } 3320 return err; 3321 } 3322 3323 #endif /* __ARCH_WANT_SYS_SOCKETCALL */ 3324 3325 /** 3326 * sock_register - add a socket protocol handler 3327 * @ops: description of protocol 3328 * 3329 * This function is called by a protocol handler that wants to 3330 * advertise its address family, and have it linked into the 3331 * socket interface. The value ops->family corresponds to the 3332 * socket system call protocol family. 3333 */ 3334 int sock_register(const struct net_proto_family *ops) 3335 { 3336 int err; 3337 3338 if (ops->family >= NPROTO) { 3339 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); 3340 return -ENOBUFS; 3341 } 3342 3343 spin_lock(&net_family_lock); 3344 if (rcu_dereference_protected(net_families[ops->family], 3345 lockdep_is_held(&net_family_lock))) 3346 err = -EEXIST; 3347 else { 3348 rcu_assign_pointer(net_families[ops->family], ops); 3349 err = 0; 3350 } 3351 spin_unlock(&net_family_lock); 3352 3353 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]); 3354 return err; 3355 } 3356 EXPORT_SYMBOL(sock_register); 3357 3358 /** 3359 * sock_unregister - remove a protocol handler 3360 * @family: protocol family to remove 3361 * 3362 * This function is called by a protocol handler that wants to 3363 * remove its address family, and have it unlinked from the 3364 * new socket creation. 3365 * 3366 * If protocol handler is a module, then it can use module reference 3367 * counts to protect against new references. If protocol handler is not 3368 * a module then it needs to provide its own protection in 3369 * the ops->create routine. 3370 */ 3371 void sock_unregister(int family) 3372 { 3373 BUG_ON(family < 0 || family >= NPROTO); 3374 3375 spin_lock(&net_family_lock); 3376 RCU_INIT_POINTER(net_families[family], NULL); 3377 spin_unlock(&net_family_lock); 3378 3379 synchronize_rcu(); 3380 3381 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]); 3382 } 3383 EXPORT_SYMBOL(sock_unregister); 3384 3385 bool sock_is_registered(int family) 3386 { 3387 return family < NPROTO && rcu_access_pointer(net_families[family]); 3388 } 3389 3390 static int __init sock_init(void) 3391 { 3392 int err; 3393 /* 3394 * Initialize the network sysctl infrastructure. 3395 */ 3396 err = net_sysctl_init(); 3397 if (err) 3398 goto out; 3399 3400 /* 3401 * Initialize skbuff SLAB cache 3402 */ 3403 skb_init(); 3404 3405 /* 3406 * Initialize the protocols module. 3407 */ 3408 3409 init_inodecache(); 3410 3411 err = register_filesystem(&sock_fs_type); 3412 if (err) 3413 goto out; 3414 sock_mnt = kern_mount(&sock_fs_type); 3415 if (IS_ERR(sock_mnt)) { 3416 err = PTR_ERR(sock_mnt); 3417 goto out_mount; 3418 } 3419 3420 /* The real protocol initialization is performed in later initcalls. 3421 */ 3422 3423 #ifdef CONFIG_NETFILTER 3424 err = netfilter_init(); 3425 if (err) 3426 goto out; 3427 #endif 3428 3429 ptp_classifier_init(); 3430 3431 out: 3432 return err; 3433 3434 out_mount: 3435 unregister_filesystem(&sock_fs_type); 3436 goto out; 3437 } 3438 3439 core_initcall(sock_init); /* early initcall */ 3440 3441 #ifdef CONFIG_PROC_FS 3442 void socket_seq_show(struct seq_file *seq) 3443 { 3444 seq_printf(seq, "sockets: used %d\n", 3445 sock_inuse_get(seq->private)); 3446 } 3447 #endif /* CONFIG_PROC_FS */ 3448 3449 /* Handle the fact that while struct ifreq has the same *layout* on 3450 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data, 3451 * which are handled elsewhere, it still has different *size* due to 3452 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit, 3453 * resulting in struct ifreq being 32 and 40 bytes respectively). 3454 * As a result, if the struct happens to be at the end of a page and 3455 * the next page isn't readable/writable, we get a fault. To prevent 3456 * that, copy back and forth to the full size. 3457 */ 3458 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg) 3459 { 3460 if (in_compat_syscall()) { 3461 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr; 3462 3463 memset(ifr, 0, sizeof(*ifr)); 3464 if (copy_from_user(ifr32, arg, sizeof(*ifr32))) 3465 return -EFAULT; 3466 3467 if (ifrdata) 3468 *ifrdata = compat_ptr(ifr32->ifr_data); 3469 3470 return 0; 3471 } 3472 3473 if (copy_from_user(ifr, arg, sizeof(*ifr))) 3474 return -EFAULT; 3475 3476 if (ifrdata) 3477 *ifrdata = ifr->ifr_data; 3478 3479 return 0; 3480 } 3481 EXPORT_SYMBOL(get_user_ifreq); 3482 3483 int put_user_ifreq(struct ifreq *ifr, void __user *arg) 3484 { 3485 size_t size = sizeof(*ifr); 3486 3487 if (in_compat_syscall()) 3488 size = sizeof(struct compat_ifreq); 3489 3490 if (copy_to_user(arg, ifr, size)) 3491 return -EFAULT; 3492 3493 return 0; 3494 } 3495 EXPORT_SYMBOL(put_user_ifreq); 3496 3497 #ifdef CONFIG_COMPAT 3498 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32) 3499 { 3500 compat_uptr_t uptr32; 3501 struct ifreq ifr; 3502 void __user *saved; 3503 int err; 3504 3505 if (get_user_ifreq(&ifr, NULL, uifr32)) 3506 return -EFAULT; 3507 3508 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu)) 3509 return -EFAULT; 3510 3511 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc; 3512 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32); 3513 3514 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL); 3515 if (!err) { 3516 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved; 3517 if (put_user_ifreq(&ifr, uifr32)) 3518 err = -EFAULT; 3519 } 3520 return err; 3521 } 3522 3523 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */ 3524 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd, 3525 struct compat_ifreq __user *u_ifreq32) 3526 { 3527 struct ifreq ifreq; 3528 void __user *data; 3529 3530 if (!is_socket_ioctl_cmd(cmd)) 3531 return -ENOTTY; 3532 if (get_user_ifreq(&ifreq, &data, u_ifreq32)) 3533 return -EFAULT; 3534 ifreq.ifr_data = data; 3535 3536 return dev_ioctl(net, cmd, &ifreq, data, NULL); 3537 } 3538 3539 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock, 3540 unsigned int cmd, unsigned long arg) 3541 { 3542 void __user *argp = compat_ptr(arg); 3543 struct sock *sk = sock->sk; 3544 struct net *net = sock_net(sk); 3545 const struct proto_ops *ops; 3546 3547 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) 3548 return sock_ioctl(file, cmd, (unsigned long)argp); 3549 3550 switch (cmd) { 3551 case SIOCWANDEV: 3552 return compat_siocwandev(net, argp); 3553 case SIOCGSTAMP_OLD: 3554 case SIOCGSTAMPNS_OLD: 3555 ops = READ_ONCE(sock->ops); 3556 if (!ops->gettstamp) 3557 return -ENOIOCTLCMD; 3558 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD, 3559 !COMPAT_USE_64BIT_TIME); 3560 3561 case SIOCETHTOOL: 3562 case SIOCBONDSLAVEINFOQUERY: 3563 case SIOCBONDINFOQUERY: 3564 case SIOCSHWTSTAMP: 3565 case SIOCGHWTSTAMP: 3566 return compat_ifr_data_ioctl(net, cmd, argp); 3567 3568 case FIOSETOWN: 3569 case SIOCSPGRP: 3570 case FIOGETOWN: 3571 case SIOCGPGRP: 3572 case SIOCBRADDBR: 3573 case SIOCBRDELBR: 3574 case SIOCBRADDIF: 3575 case SIOCBRDELIF: 3576 case SIOCGIFVLAN: 3577 case SIOCSIFVLAN: 3578 case SIOCGSKNS: 3579 case SIOCGSTAMP_NEW: 3580 case SIOCGSTAMPNS_NEW: 3581 case SIOCGIFCONF: 3582 case SIOCSIFBR: 3583 case SIOCGIFBR: 3584 return sock_ioctl(file, cmd, arg); 3585 3586 case SIOCGIFFLAGS: 3587 case SIOCSIFFLAGS: 3588 case SIOCGIFMAP: 3589 case SIOCSIFMAP: 3590 case SIOCGIFMETRIC: 3591 case SIOCSIFMETRIC: 3592 case SIOCGIFMTU: 3593 case SIOCSIFMTU: 3594 case SIOCGIFMEM: 3595 case SIOCSIFMEM: 3596 case SIOCGIFHWADDR: 3597 case SIOCSIFHWADDR: 3598 case SIOCADDMULTI: 3599 case SIOCDELMULTI: 3600 case SIOCGIFINDEX: 3601 case SIOCGIFADDR: 3602 case SIOCSIFADDR: 3603 case SIOCSIFHWBROADCAST: 3604 case SIOCDIFADDR: 3605 case SIOCGIFBRDADDR: 3606 case SIOCSIFBRDADDR: 3607 case SIOCGIFDSTADDR: 3608 case SIOCSIFDSTADDR: 3609 case SIOCGIFNETMASK: 3610 case SIOCSIFNETMASK: 3611 case SIOCSIFPFLAGS: 3612 case SIOCGIFPFLAGS: 3613 case SIOCGIFTXQLEN: 3614 case SIOCSIFTXQLEN: 3615 case SIOCGIFNAME: 3616 case SIOCSIFNAME: 3617 case SIOCGMIIPHY: 3618 case SIOCGMIIREG: 3619 case SIOCSMIIREG: 3620 case SIOCBONDENSLAVE: 3621 case SIOCBONDRELEASE: 3622 case SIOCBONDSETHWADDR: 3623 case SIOCBONDCHANGEACTIVE: 3624 case SIOCSARP: 3625 case SIOCGARP: 3626 case SIOCDARP: 3627 case SIOCOUTQ: 3628 case SIOCOUTQNSD: 3629 case SIOCATMARK: 3630 return sock_do_ioctl(net, sock, cmd, arg); 3631 } 3632 3633 return -ENOIOCTLCMD; 3634 } 3635 3636 static long compat_sock_ioctl(struct file *file, unsigned int cmd, 3637 unsigned long arg) 3638 { 3639 struct socket *sock = file->private_data; 3640 const struct proto_ops *ops = READ_ONCE(sock->ops); 3641 int ret = -ENOIOCTLCMD; 3642 struct sock *sk; 3643 struct net *net; 3644 3645 sk = sock->sk; 3646 net = sock_net(sk); 3647 3648 if (ops->compat_ioctl) 3649 ret = ops->compat_ioctl(sock, cmd, arg); 3650 3651 if (ret == -ENOIOCTLCMD && 3652 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)) 3653 ret = compat_wext_handle_ioctl(net, cmd, arg); 3654 3655 if (ret == -ENOIOCTLCMD) 3656 ret = compat_sock_ioctl_trans(file, sock, cmd, arg); 3657 3658 return ret; 3659 } 3660 #endif 3661 3662 /** 3663 * kernel_bind - bind an address to a socket (kernel space) 3664 * @sock: socket 3665 * @addr: address 3666 * @addrlen: length of address 3667 * 3668 * Returns 0 or an error. 3669 */ 3670 3671 int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen) 3672 { 3673 struct sockaddr_storage address; 3674 3675 memcpy(&address, addr, addrlen); 3676 3677 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr_unsized *)&address, 3678 addrlen); 3679 } 3680 EXPORT_SYMBOL(kernel_bind); 3681 3682 /** 3683 * kernel_listen - move socket to listening state (kernel space) 3684 * @sock: socket 3685 * @backlog: pending connections queue size 3686 * 3687 * Returns 0 or an error. 3688 */ 3689 3690 int kernel_listen(struct socket *sock, int backlog) 3691 { 3692 return READ_ONCE(sock->ops)->listen(sock, backlog); 3693 } 3694 EXPORT_SYMBOL(kernel_listen); 3695 3696 /** 3697 * kernel_accept - accept a connection (kernel space) 3698 * @sock: listening socket 3699 * @newsock: new connected socket 3700 * @flags: flags 3701 * 3702 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0. 3703 * If it fails, @newsock is guaranteed to be %NULL. 3704 * Returns 0 or an error. 3705 */ 3706 3707 int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 3708 { 3709 struct sock *sk = sock->sk; 3710 const struct proto_ops *ops = READ_ONCE(sock->ops); 3711 struct proto_accept_arg arg = { 3712 .flags = flags, 3713 .kern = true, 3714 }; 3715 int err; 3716 3717 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, 3718 newsock); 3719 if (err < 0) 3720 goto done; 3721 3722 err = ops->accept(sock, *newsock, &arg); 3723 if (err < 0) { 3724 sock_release(*newsock); 3725 *newsock = NULL; 3726 goto done; 3727 } 3728 3729 (*newsock)->ops = ops; 3730 __module_get(ops->owner); 3731 3732 done: 3733 return err; 3734 } 3735 EXPORT_SYMBOL(kernel_accept); 3736 3737 /** 3738 * kernel_connect - connect a socket (kernel space) 3739 * @sock: socket 3740 * @addr: address 3741 * @addrlen: address length 3742 * @flags: flags (O_NONBLOCK, ...) 3743 * 3744 * For datagram sockets, @addr is the address to which datagrams are sent 3745 * by default, and the only address from which datagrams are received. 3746 * For stream sockets, attempts to connect to @addr. 3747 * Returns 0 or an error code. 3748 */ 3749 3750 int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen, 3751 int flags) 3752 { 3753 struct sockaddr_storage address; 3754 3755 memcpy(&address, addr, addrlen); 3756 3757 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)&address, 3758 addrlen, flags); 3759 } 3760 EXPORT_SYMBOL(kernel_connect); 3761 3762 /** 3763 * kernel_getsockname - get the address which the socket is bound (kernel space) 3764 * @sock: socket 3765 * @addr: address holder 3766 * 3767 * Fills the @addr pointer with the address which the socket is bound. 3768 * Returns the length of the address in bytes or an error code. 3769 */ 3770 3771 int kernel_getsockname(struct socket *sock, struct sockaddr *addr) 3772 { 3773 return READ_ONCE(sock->ops)->getname(sock, addr, 0); 3774 } 3775 EXPORT_SYMBOL(kernel_getsockname); 3776 3777 /** 3778 * kernel_getpeername - get the address which the socket is connected (kernel space) 3779 * @sock: socket 3780 * @addr: address holder 3781 * 3782 * Fills the @addr pointer with the address which the socket is connected. 3783 * Returns the length of the address in bytes or an error code. 3784 */ 3785 3786 int kernel_getpeername(struct socket *sock, struct sockaddr *addr) 3787 { 3788 return READ_ONCE(sock->ops)->getname(sock, addr, 1); 3789 } 3790 EXPORT_SYMBOL(kernel_getpeername); 3791 3792 /** 3793 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space) 3794 * @sock: socket 3795 * @how: connection part 3796 * 3797 * Returns 0 or an error. 3798 */ 3799 3800 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how) 3801 { 3802 return READ_ONCE(sock->ops)->shutdown(sock, how); 3803 } 3804 EXPORT_SYMBOL(kernel_sock_shutdown); 3805 3806 /** 3807 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket 3808 * @sk: socket 3809 * 3810 * This routine returns the IP overhead imposed by a socket i.e. 3811 * the length of the underlying IP header, depending on whether 3812 * this is an IPv4 or IPv6 socket and the length from IP options turned 3813 * on at the socket. Assumes that the caller has a lock on the socket. 3814 */ 3815 3816 u32 kernel_sock_ip_overhead(struct sock *sk) 3817 { 3818 struct inet_sock *inet; 3819 struct ip_options_rcu *opt; 3820 u32 overhead = 0; 3821 #if IS_ENABLED(CONFIG_IPV6) 3822 struct ipv6_pinfo *np; 3823 struct ipv6_txoptions *optv6 = NULL; 3824 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3825 3826 if (!sk) 3827 return overhead; 3828 3829 switch (sk->sk_family) { 3830 case AF_INET: 3831 inet = inet_sk(sk); 3832 overhead += sizeof(struct iphdr); 3833 opt = rcu_dereference_protected(inet->inet_opt, 3834 sock_owned_by_user(sk)); 3835 if (opt) 3836 overhead += opt->opt.optlen; 3837 return overhead; 3838 #if IS_ENABLED(CONFIG_IPV6) 3839 case AF_INET6: 3840 np = inet6_sk(sk); 3841 overhead += sizeof(struct ipv6hdr); 3842 if (np) 3843 optv6 = rcu_dereference_protected(np->opt, 3844 sock_owned_by_user(sk)); 3845 if (optv6) 3846 overhead += (optv6->opt_flen + optv6->opt_nflen); 3847 return overhead; 3848 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3849 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */ 3850 return overhead; 3851 } 3852 } 3853 EXPORT_SYMBOL(kernel_sock_ip_overhead); 3854