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 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 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(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 BUG_ON(ret == -EIOCBQUEUED); 804 805 if (trace_sock_send_length_enabled()) 806 call_trace_sock_send_length(sock->sk, ret, 0); 807 return ret; 808 } 809 810 static int __sock_sendmsg(struct socket *sock, struct msghdr *msg) 811 { 812 int err = security_socket_sendmsg(sock, msg, 813 msg_data_left(msg)); 814 815 return err ?: sock_sendmsg_nosec(sock, msg); 816 } 817 818 /** 819 * sock_sendmsg - send a message through @sock 820 * @sock: socket 821 * @msg: message to send 822 * 823 * Sends @msg through @sock, passing through LSM. 824 * Returns the number of bytes sent, or an error code. 825 */ 826 int sock_sendmsg(struct socket *sock, struct msghdr *msg) 827 { 828 struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name; 829 struct sockaddr_storage address; 830 int save_len = msg->msg_namelen; 831 int ret; 832 833 if (msg->msg_name) { 834 memcpy(&address, msg->msg_name, msg->msg_namelen); 835 msg->msg_name = &address; 836 } 837 838 ret = __sock_sendmsg(sock, msg); 839 msg->msg_name = save_addr; 840 msg->msg_namelen = save_len; 841 842 return ret; 843 } 844 EXPORT_SYMBOL(sock_sendmsg); 845 846 /** 847 * kernel_sendmsg - send a message through @sock (kernel-space) 848 * @sock: socket 849 * @msg: message header 850 * @vec: kernel vec 851 * @num: vec array length 852 * @size: total message data size 853 * 854 * Builds the message data with @vec and sends it through @sock. 855 * Returns the number of bytes sent, or an error code. 856 */ 857 858 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 859 struct kvec *vec, size_t num, size_t size) 860 { 861 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size); 862 return sock_sendmsg(sock, msg); 863 } 864 EXPORT_SYMBOL(kernel_sendmsg); 865 866 static bool skb_is_err_queue(const struct sk_buff *skb) 867 { 868 /* Error-queue skbs are marked as PACKET_OUTGOING in 869 * skb_set_err_queue() and use the destructor installed by 870 * sock_queue_err_skb(). PACKET_OUTGOING alone is not unique: 871 * AF_PACKET outgoing taps use the same pkt_type. 872 */ 873 return skb->pkt_type == PACKET_OUTGOING && 874 skb->destructor == sock_rmem_free; 875 } 876 877 /* On transmit, software and hardware timestamps are returned independently. 878 * As the two skb clones share the hardware timestamp, which may be updated 879 * before the software timestamp is received, a hardware TX timestamp may be 880 * returned only if there is no software TX timestamp. Ignore false software 881 * timestamps, which may be made in the __sock_recv_timestamp() call when the 882 * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a 883 * hardware timestamp. 884 */ 885 static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) 886 { 887 return skb->tstamp && !false_tstamp && skb_is_err_queue(skb); 888 } 889 890 static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index) 891 { 892 bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC; 893 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 894 struct net_device *orig_dev; 895 ktime_t hwtstamp; 896 897 rcu_read_lock(); 898 orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); 899 if (orig_dev) { 900 *if_index = orig_dev->ifindex; 901 hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles); 902 } else { 903 hwtstamp = shhwtstamps->hwtstamp; 904 } 905 rcu_read_unlock(); 906 907 return hwtstamp; 908 } 909 910 static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb, 911 int if_index) 912 { 913 struct scm_ts_pktinfo ts_pktinfo; 914 struct net_device *orig_dev; 915 916 if (!skb_mac_header_was_set(skb)) 917 return; 918 919 memset(&ts_pktinfo, 0, sizeof(ts_pktinfo)); 920 921 if (!if_index) { 922 rcu_read_lock(); 923 orig_dev = dev_get_by_napi_id(skb_napi_id(skb)); 924 if (orig_dev) 925 if_index = orig_dev->ifindex; 926 rcu_read_unlock(); 927 } 928 ts_pktinfo.if_index = if_index; 929 930 ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb); 931 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO, 932 sizeof(ts_pktinfo), &ts_pktinfo); 933 } 934 935 bool skb_has_tx_timestamp(struct sk_buff *skb, const struct sock *sk) 936 { 937 const struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 938 u32 tsflags = READ_ONCE(sk->sk_tsflags); 939 940 if (serr->ee.ee_errno != ENOMSG || 941 serr->ee.ee_origin != SO_EE_ORIGIN_TIMESTAMPING) 942 return false; 943 944 /* software time stamp available and wanted */ 945 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && skb->tstamp) 946 return true; 947 /* hardware time stamps available and wanted */ 948 return (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && 949 skb_hwtstamps(skb)->hwtstamp; 950 } 951 952 int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk, 953 struct timespec64 *ts) 954 { 955 u32 tsflags = READ_ONCE(sk->sk_tsflags); 956 ktime_t hwtstamp; 957 int if_index = 0; 958 959 if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) && 960 ktime_to_timespec64_cond(skb->tstamp, ts)) 961 return SOF_TIMESTAMPING_TX_SOFTWARE; 962 963 if (!(tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) || 964 skb_is_swtx_tstamp(skb, false)) 965 return -ENOENT; 966 967 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV) 968 hwtstamp = get_timestamp(sk, skb, &if_index); 969 else 970 hwtstamp = skb_hwtstamps(skb)->hwtstamp; 971 972 if (tsflags & SOF_TIMESTAMPING_BIND_PHC) 973 hwtstamp = ptp_convert_timestamp(&hwtstamp, 974 READ_ONCE(sk->sk_bind_phc)); 975 if (!ktime_to_timespec64_cond(hwtstamp, ts)) 976 return -ENOENT; 977 978 return SOF_TIMESTAMPING_TX_HARDWARE; 979 } 980 981 /* 982 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP) 983 */ 984 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, 985 struct sk_buff *skb) 986 { 987 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP); 988 int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); 989 struct skb_shared_hwtstamps *shhwtstamps = 990 skb_hwtstamps(skb); 991 struct scm_timestamping_internal tss; 992 int if_index, false_tstamp = 0; 993 ktime_t hwtstamp; 994 u32 tsflags; 995 996 /* Race occurred between timestamp enabling and packet 997 receiving. Fill in the current time for now. */ 998 if (need_software_tstamp && skb->tstamp == 0) { 999 __net_timestamp(skb); 1000 false_tstamp = 1; 1001 } 1002 1003 if (need_software_tstamp) { 1004 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) { 1005 if (new_tstamp) { 1006 struct __kernel_sock_timeval tv; 1007 1008 skb_get_new_timestamp(skb, &tv); 1009 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW, 1010 sizeof(tv), &tv); 1011 } else { 1012 struct __kernel_old_timeval tv; 1013 1014 skb_get_timestamp(skb, &tv); 1015 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD, 1016 sizeof(tv), &tv); 1017 } 1018 } else { 1019 if (new_tstamp) { 1020 struct __kernel_timespec ts; 1021 1022 skb_get_new_timestampns(skb, &ts); 1023 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW, 1024 sizeof(ts), &ts); 1025 } else { 1026 struct __kernel_old_timespec ts; 1027 1028 skb_get_timestampns(skb, &ts); 1029 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD, 1030 sizeof(ts), &ts); 1031 } 1032 } 1033 } 1034 1035 memset(&tss, 0, sizeof(tss)); 1036 tsflags = READ_ONCE(sk->sk_tsflags); 1037 if (tsflags & SOF_TIMESTAMPING_SOFTWARE && 1038 (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE || 1039 skb_is_err_queue(skb) || 1040 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) 1041 tss.ts[0] = skb->tstamp; 1042 1043 if (shhwtstamps && 1044 (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE && 1045 (tsflags & SOF_TIMESTAMPING_RX_HARDWARE || 1046 skb_is_err_queue(skb) || 1047 !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) && 1048 !skb_is_swtx_tstamp(skb, false_tstamp)) { 1049 if_index = 0; 1050 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV) 1051 hwtstamp = get_timestamp(sk, skb, &if_index); 1052 else 1053 hwtstamp = shhwtstamps->hwtstamp; 1054 1055 if (tsflags & SOF_TIMESTAMPING_BIND_PHC) 1056 hwtstamp = ptp_convert_timestamp(&hwtstamp, 1057 READ_ONCE(sk->sk_bind_phc)); 1058 1059 if (hwtstamp) { 1060 tss.ts[2] = hwtstamp; 1061 1062 if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) && 1063 !skb_is_err_queue(skb)) 1064 put_ts_pktinfo(msg, skb, if_index); 1065 } 1066 } 1067 if (tss.ts[0] | tss.ts[2]) { 1068 if (sock_flag(sk, SOCK_TSTAMP_NEW)) 1069 put_cmsg_scm_timestamping64(msg, &tss); 1070 else 1071 put_cmsg_scm_timestamping(msg, &tss); 1072 1073 if (skb_is_err_queue(skb) && skb->len && 1074 SKB_EXT_ERR(skb)->opt_stats) 1075 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS, 1076 skb->len, skb->data); 1077 } 1078 } 1079 EXPORT_SYMBOL_GPL(__sock_recv_timestamp); 1080 1081 #ifdef CONFIG_WIRELESS 1082 void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk, 1083 struct sk_buff *skb) 1084 { 1085 int ack; 1086 1087 if (!sock_flag(sk, SOCK_WIFI_STATUS)) 1088 return; 1089 if (!skb->wifi_acked_valid) 1090 return; 1091 1092 ack = skb->wifi_acked; 1093 1094 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack); 1095 } 1096 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status); 1097 #endif 1098 1099 static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, 1100 struct sk_buff *skb) 1101 { 1102 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount) 1103 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL, 1104 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount); 1105 } 1106 1107 static void sock_recv_mark(struct msghdr *msg, struct sock *sk, 1108 struct sk_buff *skb) 1109 { 1110 if (sock_flag(sk, SOCK_RCVMARK) && skb) { 1111 /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */ 1112 __u32 mark = skb->mark; 1113 1114 put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark); 1115 } 1116 } 1117 1118 static void sock_recv_priority(struct msghdr *msg, struct sock *sk, 1119 struct sk_buff *skb) 1120 { 1121 if (sock_flag(sk, SOCK_RCVPRIORITY) && skb) { 1122 __u32 priority = skb->priority; 1123 1124 put_cmsg(msg, SOL_SOCKET, SO_PRIORITY, sizeof(__u32), &priority); 1125 } 1126 } 1127 1128 void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk, 1129 struct sk_buff *skb) 1130 { 1131 sock_recv_timestamp(msg, sk, skb); 1132 sock_recv_drops(msg, sk, skb); 1133 sock_recv_mark(msg, sk, skb); 1134 sock_recv_priority(msg, sk, skb); 1135 } 1136 EXPORT_SYMBOL_GPL(__sock_recv_cmsgs); 1137 1138 INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *, 1139 size_t, int)); 1140 INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *, 1141 size_t, int)); 1142 1143 static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags) 1144 { 1145 trace_sock_recv_length(sk, ret, flags); 1146 } 1147 1148 static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg, 1149 int flags) 1150 { 1151 int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg, 1152 inet6_recvmsg, 1153 inet_recvmsg, sock, msg, 1154 msg_data_left(msg), flags); 1155 if (trace_sock_recv_length_enabled()) 1156 call_trace_sock_recv_length(sock->sk, ret, flags); 1157 return ret; 1158 } 1159 1160 /** 1161 * sock_recvmsg - receive a message from @sock 1162 * @sock: socket 1163 * @msg: message to receive 1164 * @flags: message flags 1165 * 1166 * Receives @msg from @sock, passing through LSM. Returns the total number 1167 * of bytes received, or an error. 1168 */ 1169 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags) 1170 { 1171 int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags); 1172 1173 return err ?: sock_recvmsg_nosec(sock, msg, flags); 1174 } 1175 EXPORT_SYMBOL(sock_recvmsg); 1176 1177 /** 1178 * kernel_recvmsg - Receive a message from a socket (kernel space) 1179 * @sock: The socket to receive the message from 1180 * @msg: Received message 1181 * @vec: Input s/g array for message data 1182 * @num: Size of input s/g array 1183 * @size: Number of bytes to read 1184 * @flags: Message flags (MSG_DONTWAIT, etc...) 1185 * 1186 * On return the msg structure contains the scatter/gather array passed in the 1187 * vec argument. The array is modified so that it consists of the unfilled 1188 * portion of the original array. 1189 * 1190 * The returned value is the total number of bytes received, or an error. 1191 */ 1192 1193 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 1194 struct kvec *vec, size_t num, size_t size, int flags) 1195 { 1196 msg->msg_control_is_user = false; 1197 iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size); 1198 return sock_recvmsg(sock, msg, flags); 1199 } 1200 EXPORT_SYMBOL(kernel_recvmsg); 1201 1202 static ssize_t sock_splice_read(struct file *file, loff_t *ppos, 1203 struct pipe_inode_info *pipe, size_t len, 1204 unsigned int flags) 1205 { 1206 struct socket *sock = file->private_data; 1207 const struct proto_ops *ops; 1208 1209 ops = READ_ONCE(sock->ops); 1210 if (unlikely(!ops->splice_read)) 1211 return copy_splice_read(file, ppos, pipe, len, flags); 1212 1213 return ops->splice_read(sock, ppos, pipe, len, flags); 1214 } 1215 1216 static void sock_splice_eof(struct file *file) 1217 { 1218 struct socket *sock = file->private_data; 1219 const struct proto_ops *ops; 1220 1221 ops = READ_ONCE(sock->ops); 1222 if (ops->splice_eof) 1223 ops->splice_eof(sock); 1224 } 1225 1226 static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to) 1227 { 1228 struct file *file = iocb->ki_filp; 1229 struct socket *sock = file->private_data; 1230 struct msghdr msg = {.msg_iter = *to}; 1231 ssize_t res; 1232 1233 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT)) 1234 msg.msg_flags = MSG_DONTWAIT; 1235 1236 if (iocb->ki_pos != 0) 1237 return -ESPIPE; 1238 1239 if (!iov_iter_count(to)) /* Match SYS5 behaviour */ 1240 return 0; 1241 1242 res = sock_recvmsg(sock, &msg, msg.msg_flags); 1243 *to = msg.msg_iter; 1244 return res; 1245 } 1246 1247 static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from) 1248 { 1249 struct file *file = iocb->ki_filp; 1250 struct socket *sock = file->private_data; 1251 struct msghdr msg = {.msg_iter = *from}; 1252 ssize_t res; 1253 1254 if (iocb->ki_pos != 0) 1255 return -ESPIPE; 1256 1257 if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT)) 1258 msg.msg_flags = MSG_DONTWAIT; 1259 1260 if (sock->type == SOCK_SEQPACKET) 1261 msg.msg_flags |= MSG_EOR; 1262 1263 if (iocb->ki_flags & IOCB_NOSIGNAL) 1264 msg.msg_flags |= MSG_NOSIGNAL; 1265 1266 res = __sock_sendmsg(sock, &msg); 1267 *from = msg.msg_iter; 1268 return res; 1269 } 1270 1271 /* 1272 * Atomic setting of ioctl hooks to avoid race 1273 * with module unload. 1274 */ 1275 1276 static DEFINE_MUTEX(br_ioctl_mutex); 1277 static int (*br_ioctl_hook)(struct net *net, unsigned int cmd, 1278 void __user *uarg); 1279 1280 void brioctl_set(int (*hook)(struct net *net, unsigned int cmd, 1281 void __user *uarg)) 1282 { 1283 mutex_lock(&br_ioctl_mutex); 1284 br_ioctl_hook = hook; 1285 mutex_unlock(&br_ioctl_mutex); 1286 } 1287 EXPORT_SYMBOL(brioctl_set); 1288 1289 int br_ioctl_call(struct net *net, unsigned int cmd, void __user *uarg) 1290 { 1291 int err = -ENOPKG; 1292 1293 if (!br_ioctl_hook) 1294 request_module("bridge"); 1295 1296 mutex_lock(&br_ioctl_mutex); 1297 if (br_ioctl_hook) 1298 err = br_ioctl_hook(net, cmd, uarg); 1299 mutex_unlock(&br_ioctl_mutex); 1300 1301 return err; 1302 } 1303 1304 static DEFINE_MUTEX(vlan_ioctl_mutex); 1305 static int (*vlan_ioctl_hook) (struct net *, void __user *arg); 1306 1307 void vlan_ioctl_set(int (*hook) (struct net *, void __user *)) 1308 { 1309 mutex_lock(&vlan_ioctl_mutex); 1310 vlan_ioctl_hook = hook; 1311 mutex_unlock(&vlan_ioctl_mutex); 1312 } 1313 EXPORT_SYMBOL(vlan_ioctl_set); 1314 1315 static long sock_do_ioctl(struct net *net, struct socket *sock, 1316 unsigned int cmd, unsigned long arg) 1317 { 1318 const struct proto_ops *ops = READ_ONCE(sock->ops); 1319 struct ifreq ifr; 1320 bool need_copyout; 1321 int err; 1322 void __user *argp = (void __user *)arg; 1323 void __user *data; 1324 1325 err = ops->ioctl(sock, cmd, arg); 1326 1327 /* 1328 * If this ioctl is unknown try to hand it down 1329 * to the NIC driver. 1330 */ 1331 if (err != -ENOIOCTLCMD) 1332 return err; 1333 1334 if (!is_socket_ioctl_cmd(cmd)) 1335 return -ENOTTY; 1336 1337 if (get_user_ifreq(&ifr, &data, argp)) 1338 return -EFAULT; 1339 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout); 1340 if (!err && need_copyout) 1341 if (put_user_ifreq(&ifr, argp)) 1342 return -EFAULT; 1343 1344 return err; 1345 } 1346 1347 /* 1348 * With an ioctl, arg may well be a user mode pointer, but we don't know 1349 * what to do with it - that's up to the protocol still. 1350 */ 1351 1352 static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) 1353 { 1354 const struct proto_ops *ops; 1355 struct socket *sock; 1356 struct sock *sk; 1357 void __user *argp = (void __user *)arg; 1358 int pid, err; 1359 struct net *net; 1360 1361 sock = file->private_data; 1362 ops = READ_ONCE(sock->ops); 1363 sk = sock->sk; 1364 net = sock_net(sk); 1365 if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) { 1366 struct ifreq ifr; 1367 void __user *data; 1368 bool need_copyout; 1369 if (get_user_ifreq(&ifr, &data, argp)) 1370 return -EFAULT; 1371 err = dev_ioctl(net, cmd, &ifr, data, &need_copyout); 1372 if (!err && need_copyout) 1373 if (put_user_ifreq(&ifr, argp)) 1374 return -EFAULT; 1375 } else 1376 #ifdef CONFIG_WEXT_CORE 1377 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 1378 err = wext_handle_ioctl(net, cmd, argp); 1379 } else 1380 #endif 1381 switch (cmd) { 1382 case FIOSETOWN: 1383 case SIOCSPGRP: 1384 err = -EFAULT; 1385 if (get_user(pid, (int __user *)argp)) 1386 break; 1387 err = f_setown(sock->file, pid, 1); 1388 break; 1389 case FIOGETOWN: 1390 case SIOCGPGRP: 1391 err = put_user(f_getown(sock->file), 1392 (int __user *)argp); 1393 break; 1394 case SIOCGIFBR: 1395 case SIOCSIFBR: 1396 case SIOCBRADDBR: 1397 case SIOCBRDELBR: 1398 case SIOCBRADDIF: 1399 case SIOCBRDELIF: 1400 err = br_ioctl_call(net, cmd, argp); 1401 break; 1402 case SIOCGIFVLAN: 1403 case SIOCSIFVLAN: 1404 err = -ENOPKG; 1405 if (!vlan_ioctl_hook) 1406 request_module("8021q"); 1407 1408 mutex_lock(&vlan_ioctl_mutex); 1409 if (vlan_ioctl_hook) 1410 err = vlan_ioctl_hook(net, argp); 1411 mutex_unlock(&vlan_ioctl_mutex); 1412 break; 1413 case SIOCGSKNS: 1414 err = -EPERM; 1415 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1416 break; 1417 1418 err = open_related_ns(&net->ns, get_net_ns); 1419 break; 1420 case SIOCGSTAMP_OLD: 1421 case SIOCGSTAMPNS_OLD: 1422 if (!ops->gettstamp) { 1423 err = -ENOIOCTLCMD; 1424 break; 1425 } 1426 err = ops->gettstamp(sock, argp, 1427 cmd == SIOCGSTAMP_OLD, 1428 !IS_ENABLED(CONFIG_64BIT)); 1429 break; 1430 case SIOCGSTAMP_NEW: 1431 case SIOCGSTAMPNS_NEW: 1432 if (!ops->gettstamp) { 1433 err = -ENOIOCTLCMD; 1434 break; 1435 } 1436 err = ops->gettstamp(sock, argp, 1437 cmd == SIOCGSTAMP_NEW, 1438 false); 1439 break; 1440 1441 case SIOCGIFCONF: 1442 err = dev_ifconf(net, argp); 1443 break; 1444 1445 default: 1446 err = sock_do_ioctl(net, sock, cmd, arg); 1447 break; 1448 } 1449 return err; 1450 } 1451 1452 /** 1453 * sock_create_lite - creates a socket 1454 * @family: protocol family (AF_INET, ...) 1455 * @type: communication type (SOCK_STREAM, ...) 1456 * @protocol: protocol (0, ...) 1457 * @res: new socket 1458 * 1459 * Creates a new socket and assigns it to @res, passing through LSM. 1460 * The new socket initialization is not complete, see kernel_accept(). 1461 * Returns 0 or an error. On failure @res is set to %NULL. 1462 * This function internally uses GFP_KERNEL. 1463 */ 1464 1465 int sock_create_lite(int family, int type, int protocol, struct socket **res) 1466 { 1467 int err; 1468 struct socket *sock = NULL; 1469 1470 err = security_socket_create(family, type, protocol, 1); 1471 if (err) 1472 goto out; 1473 1474 sock = sock_alloc(); 1475 if (!sock) { 1476 err = -ENOMEM; 1477 goto out; 1478 } 1479 1480 sock->type = type; 1481 err = security_socket_post_create(sock, family, type, protocol, 1); 1482 if (err) 1483 goto out_release; 1484 1485 out: 1486 *res = sock; 1487 return err; 1488 out_release: 1489 sock_release(sock); 1490 sock = NULL; 1491 goto out; 1492 } 1493 EXPORT_SYMBOL(sock_create_lite); 1494 1495 /* No kernel lock held - perfect */ 1496 static __poll_t sock_poll(struct file *file, poll_table *wait) 1497 { 1498 struct socket *sock = file->private_data; 1499 const struct proto_ops *ops = READ_ONCE(sock->ops); 1500 __poll_t events = poll_requested_events(wait), flag = 0; 1501 1502 if (!ops->poll) 1503 return 0; 1504 1505 if (sk_can_busy_loop(sock->sk)) { 1506 /* poll once if requested by the syscall */ 1507 if (events & POLL_BUSY_LOOP) 1508 sk_busy_loop(sock->sk, 1); 1509 1510 /* if this socket can poll_ll, tell the system call */ 1511 flag = POLL_BUSY_LOOP; 1512 } 1513 1514 return ops->poll(file, sock, wait) | flag; 1515 } 1516 1517 static int sock_mmap(struct file *file, struct vm_area_struct *vma) 1518 { 1519 struct socket *sock = file->private_data; 1520 1521 return READ_ONCE(sock->ops)->mmap(file, sock, vma); 1522 } 1523 1524 static int sock_close(struct inode *inode, struct file *filp) 1525 { 1526 __sock_release(SOCKET_I(inode), inode); 1527 return 0; 1528 } 1529 1530 /* 1531 * Update the socket async list 1532 * 1533 * Fasync_list locking strategy. 1534 * 1535 * 1. fasync_list is modified only under process context socket lock 1536 * i.e. under semaphore. 1537 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock) 1538 * or under socket lock 1539 */ 1540 1541 static int sock_fasync(int fd, struct file *filp, int on) 1542 { 1543 struct socket *sock = filp->private_data; 1544 struct sock *sk = sock->sk; 1545 struct socket_wq *wq = &sock->wq; 1546 1547 if (sk == NULL) 1548 return -EINVAL; 1549 1550 lock_sock(sk); 1551 fasync_helper(fd, filp, on, &wq->fasync_list); 1552 1553 if (!wq->fasync_list) 1554 sock_reset_flag(sk, SOCK_FASYNC); 1555 else 1556 sock_set_flag(sk, SOCK_FASYNC); 1557 1558 release_sock(sk); 1559 return 0; 1560 } 1561 1562 /* This function may be called only under rcu_lock */ 1563 1564 int sock_wake_async(struct socket_wq *wq, int how, int band) 1565 { 1566 if (!wq || !wq->fasync_list) 1567 return -1; 1568 1569 switch (how) { 1570 case SOCK_WAKE_WAITD: 1571 if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags)) 1572 break; 1573 goto call_kill; 1574 case SOCK_WAKE_SPACE: 1575 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags)) 1576 break; 1577 fallthrough; 1578 case SOCK_WAKE_IO: 1579 call_kill: 1580 kill_fasync(&wq->fasync_list, SIGIO, band); 1581 break; 1582 case SOCK_WAKE_URG: 1583 kill_fasync(&wq->fasync_list, SIGURG, band); 1584 } 1585 1586 return 0; 1587 } 1588 EXPORT_SYMBOL(sock_wake_async); 1589 1590 /** 1591 * __sock_create - creates a socket 1592 * @net: net namespace 1593 * @family: protocol family (AF_INET, ...) 1594 * @type: communication type (SOCK_STREAM, ...) 1595 * @protocol: protocol (0, ...) 1596 * @res: new socket 1597 * @kern: boolean for kernel space sockets 1598 * 1599 * Creates a new socket and assigns it to @res, passing through LSM. 1600 * Returns 0 or an error. On failure @res is set to %NULL. @kern must 1601 * be set to true if the socket resides in kernel space. 1602 * This function internally uses GFP_KERNEL. 1603 */ 1604 1605 int __sock_create(struct net *net, int family, int type, int protocol, 1606 struct socket **res, int kern) 1607 { 1608 int err; 1609 struct socket *sock; 1610 const struct net_proto_family *pf; 1611 1612 /* 1613 * Check protocol is in range 1614 */ 1615 if (family < 0 || family >= NPROTO) 1616 return -EAFNOSUPPORT; 1617 if (type < 0 || type >= SOCK_MAX) 1618 return -EINVAL; 1619 1620 /* Compatibility. 1621 1622 This uglymoron is moved from INET layer to here to avoid 1623 deadlock in module load. 1624 */ 1625 if (family == PF_INET && type == SOCK_PACKET) { 1626 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n", 1627 current->comm); 1628 family = PF_PACKET; 1629 } 1630 1631 err = security_socket_create(family, type, protocol, kern); 1632 if (err) 1633 return err; 1634 1635 /* 1636 * Allocate the socket and allow the family to set things up. if 1637 * the protocol is 0, the family is instructed to select an appropriate 1638 * default. 1639 */ 1640 sock = sock_alloc(); 1641 if (!sock) { 1642 net_warn_ratelimited("socket: no more sockets\n"); 1643 return -ENFILE; /* Not exactly a match, but its the 1644 closest posix thing */ 1645 } 1646 1647 sock->type = type; 1648 1649 #ifdef CONFIG_MODULES 1650 /* Attempt to load a protocol module if the find failed. 1651 * 1652 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 1653 * requested real, full-featured networking support upon configuration. 1654 * Otherwise module support will break! 1655 */ 1656 if (rcu_access_pointer(net_families[family]) == NULL) 1657 request_module("net-pf-%d", family); 1658 #endif 1659 1660 rcu_read_lock(); 1661 pf = rcu_dereference(net_families[family]); 1662 err = -EAFNOSUPPORT; 1663 if (!pf) 1664 goto out_release; 1665 1666 /* 1667 * We will call the ->create function, that possibly is in a loadable 1668 * module, so we have to bump that loadable module refcnt first. 1669 */ 1670 if (!try_module_get(pf->owner)) 1671 goto out_release; 1672 1673 /* Now protected by module ref count */ 1674 rcu_read_unlock(); 1675 1676 err = pf->create(net, sock, protocol, kern); 1677 if (err < 0) { 1678 /* ->create should release the allocated sock->sk object on error 1679 * and make sure sock->sk is set to NULL to avoid use-after-free 1680 */ 1681 DEBUG_NET_WARN_ONCE(sock->sk, 1682 "%ps must clear sock->sk on failure, family: %d, type: %d, protocol: %d\n", 1683 pf->create, family, type, protocol); 1684 goto out_module_put; 1685 } 1686 1687 /* 1688 * Now to bump the refcnt of the [loadable] module that owns this 1689 * socket at sock_release time we decrement its refcnt. 1690 */ 1691 if (!try_module_get(sock->ops->owner)) 1692 goto out_module_busy; 1693 1694 /* 1695 * Now that we're done with the ->create function, the [loadable] 1696 * module can have its refcnt decremented 1697 */ 1698 module_put(pf->owner); 1699 err = security_socket_post_create(sock, family, type, protocol, kern); 1700 if (err) 1701 goto out_sock_release; 1702 *res = sock; 1703 1704 return 0; 1705 1706 out_module_busy: 1707 err = -EAFNOSUPPORT; 1708 out_module_put: 1709 sock->ops = NULL; 1710 module_put(pf->owner); 1711 out_sock_release: 1712 sock_release(sock); 1713 return err; 1714 1715 out_release: 1716 rcu_read_unlock(); 1717 goto out_sock_release; 1718 } 1719 EXPORT_SYMBOL(__sock_create); 1720 1721 /** 1722 * sock_create - creates a socket 1723 * @family: protocol family (AF_INET, ...) 1724 * @type: communication type (SOCK_STREAM, ...) 1725 * @protocol: protocol (0, ...) 1726 * @res: new socket 1727 * 1728 * A wrapper around __sock_create(). 1729 * Returns 0 or an error. This function internally uses GFP_KERNEL. 1730 */ 1731 1732 int sock_create(int family, int type, int protocol, struct socket **res) 1733 { 1734 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0); 1735 } 1736 EXPORT_SYMBOL(sock_create); 1737 1738 /** 1739 * sock_create_kern - creates a socket (kernel space) 1740 * @net: net namespace 1741 * @family: protocol family (AF_INET, ...) 1742 * @type: communication type (SOCK_STREAM, ...) 1743 * @protocol: protocol (0, ...) 1744 * @res: new socket 1745 * 1746 * A wrapper around __sock_create(). 1747 * Returns 0 or an error. This function internally uses GFP_KERNEL. 1748 */ 1749 1750 int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res) 1751 { 1752 return __sock_create(net, family, type, protocol, res, 1); 1753 } 1754 EXPORT_SYMBOL(sock_create_kern); 1755 1756 static struct socket *__sys_socket_create(int family, int type, int protocol) 1757 { 1758 struct socket *sock; 1759 int retval; 1760 1761 /* Check the SOCK_* constants for consistency. */ 1762 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC); 1763 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK); 1764 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK); 1765 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK); 1766 1767 if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1768 return ERR_PTR(-EINVAL); 1769 type &= SOCK_TYPE_MASK; 1770 1771 retval = sock_create(family, type, protocol, &sock); 1772 if (retval < 0) 1773 return ERR_PTR(retval); 1774 1775 return sock; 1776 } 1777 1778 struct file *__sys_socket_file(int family, int type, int protocol) 1779 { 1780 struct socket *sock; 1781 int flags; 1782 1783 sock = __sys_socket_create(family, type, protocol); 1784 if (IS_ERR(sock)) 1785 return ERR_CAST(sock); 1786 1787 flags = type & ~SOCK_TYPE_MASK; 1788 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1789 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1790 1791 return sock_alloc_file(sock, flags, NULL); 1792 } 1793 1794 /* A hook for bpf progs to attach to and update socket protocol. 1795 * 1796 * A static noinline declaration here could cause the compiler to 1797 * optimize away the function. A global noinline declaration will 1798 * keep the definition, but may optimize away the callsite. 1799 * Therefore, __weak is needed to ensure that the call is still 1800 * emitted, by telling the compiler that we don't know what the 1801 * function might eventually be. 1802 */ 1803 1804 __bpf_hook_start(); 1805 1806 __weak noinline int update_socket_protocol(int family, int type, int protocol) 1807 { 1808 return protocol; 1809 } 1810 1811 __bpf_hook_end(); 1812 1813 int __sys_socket(int family, int type, int protocol) 1814 { 1815 struct socket *sock; 1816 int flags; 1817 1818 sock = __sys_socket_create(family, type, 1819 update_socket_protocol(family, type, protocol)); 1820 if (IS_ERR(sock)) 1821 return PTR_ERR(sock); 1822 1823 flags = type & ~SOCK_TYPE_MASK; 1824 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1825 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1826 1827 return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK)); 1828 } 1829 1830 SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) 1831 { 1832 return __sys_socket(family, type, protocol); 1833 } 1834 1835 /* 1836 * Create a pair of connected sockets. 1837 */ 1838 1839 int __sys_socketpair(int family, int type, int protocol, int __user *usockvec) 1840 { 1841 struct socket *sock1, *sock2; 1842 int fd1, fd2, err; 1843 struct file *newfile1, *newfile2; 1844 int flags; 1845 1846 flags = type & ~SOCK_TYPE_MASK; 1847 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 1848 return -EINVAL; 1849 type &= SOCK_TYPE_MASK; 1850 1851 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 1852 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 1853 1854 /* 1855 * reserve descriptors and make sure we won't fail 1856 * to return them to userland. 1857 */ 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 err = put_user(fd1, &usockvec[0]); 1869 if (err) 1870 goto out; 1871 1872 err = put_user(fd2, &usockvec[1]); 1873 if (err) 1874 goto out; 1875 1876 /* 1877 * Obtain the first socket and check if the underlying protocol 1878 * supports the socketpair call. 1879 */ 1880 1881 err = sock_create(family, type, protocol, &sock1); 1882 if (unlikely(err < 0)) 1883 goto out; 1884 1885 err = sock_create(family, type, protocol, &sock2); 1886 if (unlikely(err < 0)) { 1887 sock_release(sock1); 1888 goto out; 1889 } 1890 1891 err = security_socket_socketpair(sock1, sock2); 1892 if (unlikely(err)) { 1893 sock_release(sock2); 1894 sock_release(sock1); 1895 goto out; 1896 } 1897 1898 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2); 1899 if (unlikely(err < 0)) { 1900 sock_release(sock2); 1901 sock_release(sock1); 1902 goto out; 1903 } 1904 1905 newfile1 = sock_alloc_file(sock1, flags, NULL); 1906 if (IS_ERR(newfile1)) { 1907 err = PTR_ERR(newfile1); 1908 sock_release(sock2); 1909 goto out; 1910 } 1911 1912 newfile2 = sock_alloc_file(sock2, flags, NULL); 1913 if (IS_ERR(newfile2)) { 1914 err = PTR_ERR(newfile2); 1915 fput(newfile1); 1916 goto out; 1917 } 1918 1919 audit_fd_pair(fd1, fd2); 1920 1921 fd_install(fd1, newfile1); 1922 fd_install(fd2, newfile2); 1923 return 0; 1924 1925 out: 1926 put_unused_fd(fd2); 1927 put_unused_fd(fd1); 1928 return err; 1929 } 1930 1931 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol, 1932 int __user *, usockvec) 1933 { 1934 return __sys_socketpair(family, type, protocol, usockvec); 1935 } 1936 1937 int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address, 1938 int addrlen) 1939 { 1940 int err; 1941 1942 err = security_socket_bind(sock, (struct sockaddr *)address, 1943 addrlen); 1944 if (!err) 1945 err = READ_ONCE(sock->ops)->bind(sock, 1946 (struct sockaddr_unsized *)address, 1947 addrlen); 1948 return err; 1949 } 1950 1951 /* 1952 * Bind a name to a socket. Nothing much to do here since it's 1953 * the protocol's responsibility to handle the local address. 1954 * 1955 * We move the socket address to kernel space before we call 1956 * the protocol layer (having also checked the address is ok). 1957 */ 1958 1959 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) 1960 { 1961 struct socket *sock; 1962 struct sockaddr_storage address; 1963 CLASS(fd, f)(fd); 1964 int err; 1965 1966 if (fd_empty(f)) 1967 return -EBADF; 1968 sock = sock_from_file(fd_file(f)); 1969 if (unlikely(!sock)) 1970 return -ENOTSOCK; 1971 1972 err = move_addr_to_kernel(umyaddr, addrlen, &address); 1973 if (unlikely(err)) 1974 return err; 1975 1976 return __sys_bind_socket(sock, &address, addrlen); 1977 } 1978 1979 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) 1980 { 1981 return __sys_bind(fd, umyaddr, addrlen); 1982 } 1983 1984 /* 1985 * Perform a listen. Basically, we allow the protocol to do anything 1986 * necessary for a listen, and if that works, we mark the socket as 1987 * ready for listening. 1988 */ 1989 int __sys_listen_socket(struct socket *sock, int backlog) 1990 { 1991 int somaxconn, err; 1992 1993 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn); 1994 if ((unsigned int)backlog > somaxconn) 1995 backlog = somaxconn; 1996 1997 err = security_socket_listen(sock, backlog); 1998 if (!err) 1999 err = READ_ONCE(sock->ops)->listen(sock, backlog); 2000 return err; 2001 } 2002 2003 int __sys_listen(int fd, int backlog) 2004 { 2005 CLASS(fd, f)(fd); 2006 struct socket *sock; 2007 2008 if (fd_empty(f)) 2009 return -EBADF; 2010 sock = sock_from_file(fd_file(f)); 2011 if (unlikely(!sock)) 2012 return -ENOTSOCK; 2013 2014 return __sys_listen_socket(sock, backlog); 2015 } 2016 2017 SYSCALL_DEFINE2(listen, int, fd, int, backlog) 2018 { 2019 return __sys_listen(fd, backlog); 2020 } 2021 2022 struct file *do_accept(struct file *file, struct proto_accept_arg *arg, 2023 struct sockaddr __user *upeer_sockaddr, 2024 int __user *upeer_addrlen, int flags) 2025 { 2026 struct socket *sock, *newsock; 2027 struct file *newfile; 2028 int err, len; 2029 struct sockaddr_storage address; 2030 const struct proto_ops *ops; 2031 2032 sock = sock_from_file(file); 2033 if (!sock) 2034 return ERR_PTR(-ENOTSOCK); 2035 2036 newsock = sock_alloc(); 2037 if (!newsock) 2038 return ERR_PTR(-ENFILE); 2039 ops = READ_ONCE(sock->ops); 2040 2041 newsock->type = sock->type; 2042 newsock->ops = ops; 2043 2044 /* 2045 * We don't need try_module_get here, as the listening socket (sock) 2046 * has the protocol module (sock->ops->owner) held. 2047 */ 2048 __module_get(ops->owner); 2049 2050 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name); 2051 if (IS_ERR(newfile)) 2052 return newfile; 2053 2054 err = security_socket_accept(sock, newsock); 2055 if (err) 2056 goto out_fd; 2057 2058 arg->flags |= sock->file->f_flags; 2059 err = ops->accept(sock, newsock, arg); 2060 if (err < 0) 2061 goto out_fd; 2062 2063 if (upeer_sockaddr) { 2064 len = ops->getname(newsock, (struct sockaddr *)&address, 2); 2065 if (len < 0) { 2066 err = -ECONNABORTED; 2067 goto out_fd; 2068 } 2069 err = move_addr_to_user(&address, 2070 len, upeer_sockaddr, upeer_addrlen); 2071 if (err < 0) 2072 goto out_fd; 2073 } 2074 2075 /* File flags are not inherited via accept() unlike another OSes. */ 2076 return newfile; 2077 out_fd: 2078 fput(newfile); 2079 return ERR_PTR(err); 2080 } 2081 2082 static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr, 2083 int __user *upeer_addrlen, int flags) 2084 { 2085 struct proto_accept_arg arg = { }; 2086 2087 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 2088 return -EINVAL; 2089 2090 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 2091 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 2092 2093 return FD_ADD(flags, do_accept(file, &arg, upeer_sockaddr, upeer_addrlen, flags)); 2094 } 2095 2096 /* 2097 * For accept, we attempt to create a new socket, set up the link 2098 * with the client, wake up the client, then return the new 2099 * connected fd. We collect the address of the connector in kernel 2100 * space and move it to user at the very end. This is unclean because 2101 * we open the socket then return an error. 2102 * 2103 * 1003.1g adds the ability to recvmsg() to query connection pending 2104 * status. We need to add that support in a way that's 2105 * clean when we restructure accept also. 2106 */ 2107 2108 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, 2109 int __user *upeer_addrlen, int flags) 2110 { 2111 CLASS(fd, f)(fd); 2112 2113 if (fd_empty(f)) 2114 return -EBADF; 2115 return __sys_accept4_file(fd_file(f), upeer_sockaddr, 2116 upeer_addrlen, flags); 2117 } 2118 2119 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, 2120 int __user *, upeer_addrlen, int, flags) 2121 { 2122 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags); 2123 } 2124 2125 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, 2126 int __user *, upeer_addrlen) 2127 { 2128 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0); 2129 } 2130 2131 int connect_socket(struct socket *sock, struct sockaddr_storage *address, 2132 int addrlen, int flags) 2133 { 2134 int err; 2135 2136 err = security_socket_connect(sock, (struct sockaddr *)address, 2137 addrlen); 2138 if (err) 2139 return err; 2140 2141 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address, 2142 addrlen, flags); 2143 } 2144 2145 /* 2146 * Attempt to connect to a socket with the server address. The address 2147 * is in user space so we verify it is OK and move it to kernel space. 2148 * 2149 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to 2150 * break bindings 2151 * 2152 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and 2153 * other SEQPACKET protocols that take time to connect() as it doesn't 2154 * include the -EINPROGRESS status for such sockets. 2155 */ 2156 2157 int __sys_connect_file(struct file *file, struct sockaddr_storage *address, 2158 int addrlen, int file_flags) 2159 { 2160 struct socket *sock; 2161 2162 sock = sock_from_file(file); 2163 if (!sock) 2164 return -ENOTSOCK; 2165 2166 return connect_socket(sock, address, addrlen, 2167 sock->file->f_flags | file_flags); 2168 } 2169 2170 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) 2171 { 2172 struct sockaddr_storage address; 2173 CLASS(fd, f)(fd); 2174 int ret; 2175 2176 if (fd_empty(f)) 2177 return -EBADF; 2178 2179 ret = move_addr_to_kernel(uservaddr, addrlen, &address); 2180 if (ret) 2181 return ret; 2182 2183 return __sys_connect_file(fd_file(f), &address, addrlen, 0); 2184 } 2185 2186 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr, 2187 int, addrlen) 2188 { 2189 return __sys_connect(fd, uservaddr, addrlen); 2190 } 2191 2192 int do_getsockname(struct socket *sock, int peer, 2193 struct sockaddr __user *usockaddr, int __user *usockaddr_len) 2194 { 2195 struct sockaddr_storage address; 2196 int err; 2197 2198 if (peer) 2199 err = security_socket_getpeername(sock); 2200 else 2201 err = security_socket_getsockname(sock); 2202 if (err) 2203 return err; 2204 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer); 2205 if (err < 0) 2206 return err; 2207 /* "err" is actually length in this case */ 2208 return move_addr_to_user(&address, err, usockaddr, usockaddr_len); 2209 } 2210 2211 /* 2212 * Get the remote or local address ('name') of a socket object. Move the 2213 * obtained name to user space. 2214 */ 2215 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr, 2216 int __user *usockaddr_len, int peer) 2217 { 2218 struct socket *sock; 2219 CLASS(fd, f)(fd); 2220 2221 if (fd_empty(f)) 2222 return -EBADF; 2223 sock = sock_from_file(fd_file(f)); 2224 if (unlikely(!sock)) 2225 return -ENOTSOCK; 2226 return do_getsockname(sock, peer, usockaddr, usockaddr_len); 2227 } 2228 2229 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr, 2230 int __user *, usockaddr_len) 2231 { 2232 return __sys_getsockname(fd, usockaddr, usockaddr_len, 0); 2233 } 2234 2235 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, 2236 int __user *, usockaddr_len) 2237 { 2238 return __sys_getsockname(fd, usockaddr, usockaddr_len, 1); 2239 } 2240 2241 /* 2242 * Send a datagram to a given address. We move the address into kernel 2243 * space and check the user space data area is readable before invoking 2244 * the protocol. 2245 */ 2246 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags, 2247 struct sockaddr __user *addr, int addr_len) 2248 { 2249 struct socket *sock; 2250 struct sockaddr_storage address; 2251 int err; 2252 struct msghdr msg; 2253 2254 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter); 2255 if (unlikely(err)) 2256 return err; 2257 2258 CLASS(fd, f)(fd); 2259 if (fd_empty(f)) 2260 return -EBADF; 2261 sock = sock_from_file(fd_file(f)); 2262 if (unlikely(!sock)) 2263 return -ENOTSOCK; 2264 2265 msg.msg_name = NULL; 2266 msg.msg_control = NULL; 2267 msg.msg_controllen = 0; 2268 msg.msg_namelen = 0; 2269 msg.msg_ubuf = NULL; 2270 if (addr) { 2271 err = move_addr_to_kernel(addr, addr_len, &address); 2272 if (err < 0) 2273 return err; 2274 msg.msg_name = (struct sockaddr *)&address; 2275 msg.msg_namelen = addr_len; 2276 } 2277 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2278 if (sock->file->f_flags & O_NONBLOCK) 2279 flags |= MSG_DONTWAIT; 2280 msg.msg_flags = flags; 2281 return __sock_sendmsg(sock, &msg); 2282 } 2283 2284 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, 2285 unsigned int, flags, struct sockaddr __user *, addr, 2286 int, addr_len) 2287 { 2288 return __sys_sendto(fd, buff, len, flags, addr, addr_len); 2289 } 2290 2291 /* 2292 * Send a datagram down a socket. 2293 */ 2294 2295 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, 2296 unsigned int, flags) 2297 { 2298 return __sys_sendto(fd, buff, len, flags, NULL, 0); 2299 } 2300 2301 /* 2302 * Receive a frame from the socket and optionally record the address of the 2303 * sender. We verify the buffers are writable and if needed move the 2304 * sender address from kernel to user space. 2305 */ 2306 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags, 2307 struct sockaddr __user *addr, int __user *addr_len) 2308 { 2309 struct sockaddr_storage address; 2310 struct msghdr msg = { 2311 /* Save some cycles and don't copy the address if not needed */ 2312 .msg_name = addr ? (struct sockaddr *)&address : NULL, 2313 }; 2314 struct socket *sock; 2315 int err, err2; 2316 2317 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter); 2318 if (unlikely(err)) 2319 return err; 2320 2321 CLASS(fd, f)(fd); 2322 2323 if (fd_empty(f)) 2324 return -EBADF; 2325 sock = sock_from_file(fd_file(f)); 2326 if (unlikely(!sock)) 2327 return -ENOTSOCK; 2328 2329 if (sock->file->f_flags & O_NONBLOCK) 2330 flags |= MSG_DONTWAIT; 2331 err = sock_recvmsg(sock, &msg, flags); 2332 2333 if (err >= 0 && addr != NULL) { 2334 err2 = move_addr_to_user(&address, 2335 msg.msg_namelen, addr, addr_len); 2336 if (err2 < 0) 2337 err = err2; 2338 } 2339 return err; 2340 } 2341 2342 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size, 2343 unsigned int, flags, struct sockaddr __user *, addr, 2344 int __user *, addr_len) 2345 { 2346 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len); 2347 } 2348 2349 /* 2350 * Receive a datagram from a socket. 2351 */ 2352 2353 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size, 2354 unsigned int, flags) 2355 { 2356 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 2357 } 2358 2359 static bool sock_use_custom_sol_socket(const struct socket *sock) 2360 { 2361 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags); 2362 } 2363 2364 int do_sock_setsockopt(struct socket *sock, bool compat, int level, 2365 int optname, sockptr_t optval, int optlen) 2366 { 2367 const struct proto_ops *ops; 2368 char *kernel_optval = NULL; 2369 int err; 2370 2371 if (optlen < 0) 2372 return -EINVAL; 2373 2374 err = security_socket_setsockopt(sock, level, optname); 2375 if (err) 2376 goto out_put; 2377 2378 if (!compat) 2379 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname, 2380 optval, &optlen, 2381 &kernel_optval); 2382 if (err < 0) 2383 goto out_put; 2384 if (err > 0) { 2385 err = 0; 2386 goto out_put; 2387 } 2388 2389 if (kernel_optval) 2390 optval = KERNEL_SOCKPTR(kernel_optval); 2391 ops = READ_ONCE(sock->ops); 2392 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock)) 2393 err = sock_setsockopt(sock, level, optname, optval, optlen); 2394 else if (unlikely(!ops->setsockopt)) 2395 err = -EOPNOTSUPP; 2396 else 2397 err = ops->setsockopt(sock, level, optname, optval, 2398 optlen); 2399 kfree(kernel_optval); 2400 out_put: 2401 return err; 2402 } 2403 EXPORT_SYMBOL(do_sock_setsockopt); 2404 2405 /* Set a socket option. Because we don't know the option lengths we have 2406 * to pass the user mode parameter for the protocols to sort out. 2407 */ 2408 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, 2409 int optlen) 2410 { 2411 sockptr_t optval = USER_SOCKPTR(user_optval); 2412 bool compat = in_compat_syscall(); 2413 struct socket *sock; 2414 CLASS(fd, f)(fd); 2415 2416 if (fd_empty(f)) 2417 return -EBADF; 2418 sock = sock_from_file(fd_file(f)); 2419 if (unlikely(!sock)) 2420 return -ENOTSOCK; 2421 2422 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen); 2423 } 2424 2425 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, 2426 char __user *, optval, int, optlen) 2427 { 2428 return __sys_setsockopt(fd, level, optname, optval, optlen); 2429 } 2430 2431 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level, 2432 int optname)); 2433 2434 /* 2435 * Initialize a sockopt_t from sockptr optval/optlen, setting up iov_iter 2436 * for both input and output directions. 2437 * It is important to remember that both iov points to the same data, but, 2438 * .iter_in is read-only and .iter_out is write-only by the protocol callbacks 2439 */ 2440 static int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval, 2441 sockptr_t optlen, struct kvec *kvec) 2442 { 2443 int koptlen; 2444 2445 if (copy_from_sockptr(&koptlen, optlen, sizeof(int))) 2446 return -EFAULT; 2447 2448 if (koptlen < 0) 2449 return -EINVAL; 2450 2451 if (optval.is_kernel) { 2452 kvec->iov_base = optval.kernel; 2453 kvec->iov_len = koptlen; 2454 iov_iter_kvec(&opt->iter_out, ITER_DEST, kvec, 1, koptlen); 2455 iov_iter_kvec(&opt->iter_in, ITER_SOURCE, kvec, 1, koptlen); 2456 } else { 2457 iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval.user, koptlen); 2458 iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval.user, 2459 koptlen); 2460 } 2461 opt->optlen = koptlen; 2462 2463 return 0; 2464 } 2465 2466 int do_sock_getsockopt(struct socket *sock, bool compat, int level, 2467 int optname, sockptr_t optval, sockptr_t optlen) 2468 { 2469 int max_optlen __maybe_unused = 0; 2470 const struct proto_ops *ops; 2471 struct kvec kvec; 2472 sockopt_t opt; 2473 int err; 2474 2475 err = security_socket_getsockopt(sock, level, optname); 2476 if (err) 2477 return err; 2478 2479 if (!compat) 2480 copy_from_sockptr(&max_optlen, optlen, sizeof(int)); 2481 2482 ops = READ_ONCE(sock->ops); 2483 if (level == SOL_SOCKET) { 2484 err = sk_getsockopt(sock->sk, level, optname, optval, optlen); 2485 } else if (ops->getsockopt_iter) { 2486 err = sockptr_to_sockopt(&opt, optval, optlen, &kvec); 2487 if (err) 2488 return err; 2489 2490 err = ops->getsockopt_iter(sock, level, optname, &opt); 2491 2492 /* Always write back optlen, even on failure. Some protocols 2493 * (e.g. CAN raw) return -ERANGE and set optlen to the 2494 * required buffer size so userspace can discover it. 2495 */ 2496 if (copy_to_sockptr(optlen, &opt.optlen, sizeof(int))) 2497 return -EFAULT; 2498 } else if (ops->getsockopt) { 2499 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel, 2500 "Invalid argument type")) 2501 return -EOPNOTSUPP; 2502 2503 err = ops->getsockopt(sock, level, optname, optval.user, 2504 optlen.user); 2505 } else { 2506 err = -EOPNOTSUPP; 2507 } 2508 2509 if (!compat) 2510 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, 2511 optval, optlen, max_optlen, 2512 err); 2513 2514 return err; 2515 } 2516 EXPORT_SYMBOL(do_sock_getsockopt); 2517 2518 /* 2519 * Get a socket option. Because we don't know the option lengths we have 2520 * to pass a user mode parameter for the protocols to sort out. 2521 */ 2522 int __sys_getsockopt(int fd, int level, int optname, char __user *optval, 2523 int __user *optlen) 2524 { 2525 struct socket *sock; 2526 CLASS(fd, f)(fd); 2527 2528 if (fd_empty(f)) 2529 return -EBADF; 2530 sock = sock_from_file(fd_file(f)); 2531 if (unlikely(!sock)) 2532 return -ENOTSOCK; 2533 2534 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname, 2535 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 2536 } 2537 2538 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, 2539 char __user *, optval, int __user *, optlen) 2540 { 2541 return __sys_getsockopt(fd, level, optname, optval, optlen); 2542 } 2543 2544 /* 2545 * Shutdown a socket. 2546 */ 2547 2548 int __sys_shutdown_sock(struct socket *sock, int how) 2549 { 2550 int err; 2551 2552 err = security_socket_shutdown(sock, how); 2553 if (!err) 2554 err = READ_ONCE(sock->ops)->shutdown(sock, how); 2555 2556 return err; 2557 } 2558 2559 int __sys_shutdown(int fd, int how) 2560 { 2561 struct socket *sock; 2562 CLASS(fd, f)(fd); 2563 2564 if (fd_empty(f)) 2565 return -EBADF; 2566 sock = sock_from_file(fd_file(f)); 2567 if (unlikely(!sock)) 2568 return -ENOTSOCK; 2569 2570 return __sys_shutdown_sock(sock, how); 2571 } 2572 2573 SYSCALL_DEFINE2(shutdown, int, fd, int, how) 2574 { 2575 return __sys_shutdown(fd, how); 2576 } 2577 2578 /* A couple of helpful macros for getting the address of the 32/64 bit 2579 * fields which are the same type (int / unsigned) on our platforms. 2580 */ 2581 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 2582 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 2583 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 2584 2585 struct used_address { 2586 struct sockaddr_storage name; 2587 unsigned int name_len; 2588 }; 2589 2590 int __copy_msghdr(struct msghdr *kmsg, 2591 struct user_msghdr *msg, 2592 struct sockaddr __user **save_addr) 2593 { 2594 ssize_t err; 2595 2596 kmsg->msg_control_is_user = true; 2597 kmsg->msg_get_inq = 0; 2598 kmsg->msg_control_user = msg->msg_control; 2599 kmsg->msg_controllen = msg->msg_controllen; 2600 kmsg->msg_flags = msg->msg_flags; 2601 2602 kmsg->msg_namelen = msg->msg_namelen; 2603 if (!msg->msg_name) 2604 kmsg->msg_namelen = 0; 2605 2606 if (kmsg->msg_namelen < 0) 2607 return -EINVAL; 2608 2609 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 2610 kmsg->msg_namelen = sizeof(struct sockaddr_storage); 2611 2612 if (save_addr) 2613 *save_addr = msg->msg_name; 2614 2615 if (msg->msg_name && kmsg->msg_namelen) { 2616 if (!save_addr) { 2617 err = move_addr_to_kernel(msg->msg_name, 2618 kmsg->msg_namelen, 2619 kmsg->msg_name); 2620 if (err < 0) 2621 return err; 2622 } 2623 } else { 2624 kmsg->msg_name = NULL; 2625 kmsg->msg_namelen = 0; 2626 } 2627 2628 if (msg->msg_iovlen > UIO_MAXIOV) 2629 return -EMSGSIZE; 2630 2631 kmsg->msg_ubuf = NULL; 2632 return 0; 2633 } 2634 2635 static int copy_msghdr_from_user(struct msghdr *kmsg, 2636 struct user_msghdr __user *umsg, 2637 struct sockaddr __user **save_addr, 2638 struct iovec **iov) 2639 { 2640 struct user_msghdr msg; 2641 ssize_t err; 2642 2643 if (copy_from_user(&msg, umsg, sizeof(*umsg))) 2644 return -EFAULT; 2645 2646 err = __copy_msghdr(kmsg, &msg, save_addr); 2647 if (err) 2648 return err; 2649 2650 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE, 2651 msg.msg_iov, msg.msg_iovlen, 2652 UIO_FASTIOV, iov, &kmsg->msg_iter); 2653 return err < 0 ? err : 0; 2654 } 2655 2656 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys, 2657 unsigned int flags, struct used_address *used_address, 2658 unsigned int allowed_msghdr_flags) 2659 { 2660 unsigned char ctl[sizeof(struct cmsghdr) + 20] 2661 __aligned(sizeof(__kernel_size_t)); 2662 /* 20 is size of ipv6_pktinfo */ 2663 unsigned char *ctl_buf = ctl; 2664 int ctl_len; 2665 ssize_t err; 2666 2667 err = -ENOBUFS; 2668 2669 if (msg_sys->msg_controllen > INT_MAX) 2670 goto out; 2671 flags |= (msg_sys->msg_flags & allowed_msghdr_flags); 2672 ctl_len = msg_sys->msg_controllen; 2673 if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 2674 err = 2675 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl, 2676 sizeof(ctl)); 2677 if (err) 2678 goto out; 2679 ctl_buf = msg_sys->msg_control; 2680 ctl_len = msg_sys->msg_controllen; 2681 } else if (ctl_len) { 2682 BUILD_BUG_ON(sizeof(struct cmsghdr) != 2683 CMSG_ALIGN(sizeof(struct cmsghdr))); 2684 if (ctl_len > sizeof(ctl)) { 2685 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 2686 if (ctl_buf == NULL) 2687 goto out; 2688 } 2689 err = -EFAULT; 2690 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len)) 2691 goto out_freectl; 2692 msg_sys->msg_control = ctl_buf; 2693 msg_sys->msg_control_is_user = false; 2694 } 2695 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2696 msg_sys->msg_flags = flags; 2697 2698 if (sock->file->f_flags & O_NONBLOCK) 2699 msg_sys->msg_flags |= MSG_DONTWAIT; 2700 /* 2701 * If this is sendmmsg() and current destination address is same as 2702 * previously succeeded address, omit asking LSM's decision. 2703 * used_address->name_len is initialized to UINT_MAX so that the first 2704 * destination address never matches. 2705 */ 2706 if (used_address && msg_sys->msg_name && 2707 used_address->name_len == msg_sys->msg_namelen && 2708 !memcmp(&used_address->name, msg_sys->msg_name, 2709 used_address->name_len)) { 2710 err = sock_sendmsg_nosec(sock, msg_sys); 2711 goto out_freectl; 2712 } 2713 err = __sock_sendmsg(sock, msg_sys); 2714 /* 2715 * If this is sendmmsg() and sending to current destination address was 2716 * successful, remember it. 2717 */ 2718 if (used_address && err >= 0) { 2719 used_address->name_len = msg_sys->msg_namelen; 2720 if (msg_sys->msg_name) 2721 memcpy(&used_address->name, msg_sys->msg_name, 2722 used_address->name_len); 2723 } 2724 2725 out_freectl: 2726 if (ctl_buf != ctl) 2727 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 2728 out: 2729 return err; 2730 } 2731 2732 static int sendmsg_copy_msghdr(struct msghdr *msg, 2733 struct user_msghdr __user *umsg, unsigned flags, 2734 struct iovec **iov) 2735 { 2736 int err; 2737 2738 if (flags & MSG_CMSG_COMPAT) { 2739 struct compat_msghdr __user *msg_compat; 2740 2741 msg_compat = (struct compat_msghdr __user *) umsg; 2742 err = get_compat_msghdr(msg, msg_compat, NULL, iov); 2743 } else { 2744 err = copy_msghdr_from_user(msg, umsg, NULL, iov); 2745 } 2746 if (err < 0) 2747 return err; 2748 2749 return 0; 2750 } 2751 2752 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg, 2753 struct msghdr *msg_sys, unsigned int flags, 2754 struct used_address *used_address, 2755 unsigned int allowed_msghdr_flags) 2756 { 2757 struct sockaddr_storage address; 2758 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2759 ssize_t err; 2760 2761 msg_sys->msg_name = &address; 2762 2763 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov); 2764 if (err < 0) 2765 return err; 2766 2767 err = ____sys_sendmsg(sock, msg_sys, flags, used_address, 2768 allowed_msghdr_flags); 2769 kfree(iov); 2770 return err; 2771 } 2772 2773 /* 2774 * BSD sendmsg interface 2775 */ 2776 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg, 2777 unsigned int flags) 2778 { 2779 return ____sys_sendmsg(sock, msg, flags, NULL, 0); 2780 } 2781 2782 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2783 bool forbid_cmsg_compat) 2784 { 2785 struct msghdr msg_sys; 2786 struct socket *sock; 2787 2788 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2789 return -EINVAL; 2790 2791 CLASS(fd, f)(fd); 2792 2793 if (fd_empty(f)) 2794 return -EBADF; 2795 sock = sock_from_file(fd_file(f)); 2796 if (unlikely(!sock)) 2797 return -ENOTSOCK; 2798 2799 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0); 2800 } 2801 2802 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags) 2803 { 2804 return __sys_sendmsg(fd, msg, flags, true); 2805 } 2806 2807 /* 2808 * Linux sendmmsg interface 2809 */ 2810 2811 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, 2812 unsigned int flags, bool forbid_cmsg_compat) 2813 { 2814 int err, datagrams; 2815 struct socket *sock; 2816 struct mmsghdr __user *entry; 2817 struct compat_mmsghdr __user *compat_entry; 2818 struct msghdr msg_sys; 2819 struct used_address used_address; 2820 unsigned int oflags = flags; 2821 2822 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2823 return -EINVAL; 2824 2825 if (vlen > UIO_MAXIOV) 2826 vlen = UIO_MAXIOV; 2827 2828 datagrams = 0; 2829 2830 CLASS(fd, f)(fd); 2831 2832 if (fd_empty(f)) 2833 return -EBADF; 2834 sock = sock_from_file(fd_file(f)); 2835 if (unlikely(!sock)) 2836 return -ENOTSOCK; 2837 2838 used_address.name_len = UINT_MAX; 2839 entry = mmsg; 2840 compat_entry = (struct compat_mmsghdr __user *)mmsg; 2841 err = 0; 2842 flags |= MSG_BATCH; 2843 2844 while (datagrams < vlen) { 2845 if (datagrams == vlen - 1) 2846 flags = oflags; 2847 2848 if (MSG_CMSG_COMPAT & flags) { 2849 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry, 2850 &msg_sys, flags, &used_address, MSG_EOR); 2851 if (err < 0) 2852 break; 2853 err = __put_user(err, &compat_entry->msg_len); 2854 ++compat_entry; 2855 } else { 2856 err = ___sys_sendmsg(sock, 2857 (struct user_msghdr __user *)entry, 2858 &msg_sys, flags, &used_address, MSG_EOR); 2859 if (err < 0) 2860 break; 2861 err = put_user(err, &entry->msg_len); 2862 ++entry; 2863 } 2864 2865 if (err) 2866 break; 2867 ++datagrams; 2868 if (msg_data_left(&msg_sys)) 2869 break; 2870 cond_resched(); 2871 } 2872 2873 /* We only return an error if no datagrams were able to be sent */ 2874 if (datagrams != 0) 2875 return datagrams; 2876 2877 return err; 2878 } 2879 2880 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, 2881 unsigned int, vlen, unsigned int, flags) 2882 { 2883 return __sys_sendmmsg(fd, mmsg, vlen, flags, true); 2884 } 2885 2886 static int recvmsg_copy_msghdr(struct msghdr *msg, 2887 struct user_msghdr __user *umsg, unsigned flags, 2888 struct sockaddr __user **uaddr, 2889 struct iovec **iov) 2890 { 2891 ssize_t err; 2892 2893 if (MSG_CMSG_COMPAT & flags) { 2894 struct compat_msghdr __user *msg_compat; 2895 2896 msg_compat = (struct compat_msghdr __user *) umsg; 2897 err = get_compat_msghdr(msg, msg_compat, uaddr, iov); 2898 } else { 2899 err = copy_msghdr_from_user(msg, umsg, uaddr, iov); 2900 } 2901 if (err < 0) 2902 return err; 2903 2904 return 0; 2905 } 2906 2907 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys, 2908 struct user_msghdr __user *msg, 2909 struct sockaddr __user *uaddr, 2910 unsigned int flags, int nosec) 2911 { 2912 struct compat_msghdr __user *msg_compat = 2913 (struct compat_msghdr __user *) msg; 2914 int __user *uaddr_len = COMPAT_NAMELEN(msg); 2915 struct sockaddr_storage addr; 2916 unsigned long cmsg_ptr; 2917 int len; 2918 ssize_t err; 2919 2920 msg_sys->msg_name = &addr; 2921 cmsg_ptr = (unsigned long)msg_sys->msg_control; 2922 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 2923 2924 /* We assume all kernel code knows the size of sockaddr_storage */ 2925 msg_sys->msg_namelen = 0; 2926 2927 if (sock->file->f_flags & O_NONBLOCK) 2928 flags |= MSG_DONTWAIT; 2929 2930 if (unlikely(nosec)) 2931 err = sock_recvmsg_nosec(sock, msg_sys, flags); 2932 else 2933 err = sock_recvmsg(sock, msg_sys, flags); 2934 2935 if (err < 0) 2936 goto out; 2937 len = err; 2938 2939 if (uaddr != NULL) { 2940 err = move_addr_to_user(&addr, 2941 msg_sys->msg_namelen, uaddr, 2942 uaddr_len); 2943 if (err < 0) 2944 goto out; 2945 } 2946 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT), 2947 COMPAT_FLAGS(msg)); 2948 if (err) 2949 goto out; 2950 if (MSG_CMSG_COMPAT & flags) 2951 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2952 &msg_compat->msg_controllen); 2953 else 2954 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2955 &msg->msg_controllen); 2956 if (err) 2957 goto out; 2958 err = len; 2959 out: 2960 return err; 2961 } 2962 2963 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg, 2964 struct msghdr *msg_sys, unsigned int flags, int nosec) 2965 { 2966 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2967 /* user mode address pointers */ 2968 struct sockaddr __user *uaddr; 2969 ssize_t err; 2970 2971 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov); 2972 if (err < 0) 2973 return err; 2974 2975 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec); 2976 kfree(iov); 2977 return err; 2978 } 2979 2980 /* 2981 * BSD recvmsg interface 2982 */ 2983 2984 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg, 2985 struct user_msghdr __user *umsg, 2986 struct sockaddr __user *uaddr, unsigned int flags) 2987 { 2988 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0); 2989 } 2990 2991 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2992 bool forbid_cmsg_compat) 2993 { 2994 struct msghdr msg_sys; 2995 struct socket *sock; 2996 2997 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2998 return -EINVAL; 2999 3000 CLASS(fd, f)(fd); 3001 3002 if (fd_empty(f)) 3003 return -EBADF; 3004 sock = sock_from_file(fd_file(f)); 3005 if (unlikely(!sock)) 3006 return -ENOTSOCK; 3007 3008 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0); 3009 } 3010 3011 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg, 3012 unsigned int, flags) 3013 { 3014 return __sys_recvmsg(fd, msg, flags, true); 3015 } 3016 3017 /* 3018 * Linux recvmmsg interface 3019 */ 3020 3021 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3022 unsigned int vlen, unsigned int flags, 3023 struct timespec64 *timeout) 3024 { 3025 int err = 0, datagrams; 3026 struct socket *sock; 3027 struct mmsghdr __user *entry; 3028 struct compat_mmsghdr __user *compat_entry; 3029 struct msghdr msg_sys; 3030 struct timespec64 end_time; 3031 struct timespec64 timeout64; 3032 3033 if (timeout && 3034 poll_select_set_timeout(&end_time, timeout->tv_sec, 3035 timeout->tv_nsec)) 3036 return -EINVAL; 3037 3038 datagrams = 0; 3039 3040 CLASS(fd, f)(fd); 3041 3042 if (fd_empty(f)) 3043 return -EBADF; 3044 sock = sock_from_file(fd_file(f)); 3045 if (unlikely(!sock)) 3046 return -ENOTSOCK; 3047 3048 if (likely(!(flags & MSG_ERRQUEUE))) { 3049 err = sock_error(sock->sk); 3050 if (err) 3051 return err; 3052 } 3053 3054 entry = mmsg; 3055 compat_entry = (struct compat_mmsghdr __user *)mmsg; 3056 3057 while (datagrams < vlen) { 3058 /* 3059 * No need to ask LSM for more than the first datagram. 3060 */ 3061 if (MSG_CMSG_COMPAT & flags) { 3062 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry, 3063 &msg_sys, flags & ~MSG_WAITFORONE, 3064 datagrams); 3065 if (err < 0) 3066 break; 3067 err = __put_user(err, &compat_entry->msg_len); 3068 ++compat_entry; 3069 } else { 3070 err = ___sys_recvmsg(sock, 3071 (struct user_msghdr __user *)entry, 3072 &msg_sys, flags & ~MSG_WAITFORONE, 3073 datagrams); 3074 if (err < 0) 3075 break; 3076 err = put_user(err, &entry->msg_len); 3077 ++entry; 3078 } 3079 3080 if (err) 3081 break; 3082 ++datagrams; 3083 3084 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ 3085 if (flags & MSG_WAITFORONE) 3086 flags |= MSG_DONTWAIT; 3087 3088 if (timeout) { 3089 ktime_get_ts64(&timeout64); 3090 *timeout = timespec64_sub(end_time, timeout64); 3091 if (timeout->tv_sec < 0) { 3092 timeout->tv_sec = timeout->tv_nsec = 0; 3093 break; 3094 } 3095 3096 /* Timeout, return less than vlen datagrams */ 3097 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0) 3098 break; 3099 } 3100 3101 /* Out of band data, return right away */ 3102 if (msg_sys.msg_flags & MSG_OOB) 3103 break; 3104 cond_resched(); 3105 } 3106 3107 if (err == 0) 3108 return datagrams; 3109 3110 if (datagrams == 0) 3111 return err; 3112 3113 /* 3114 * We may return less entries than requested (vlen) if the 3115 * sock is non block and there aren't enough datagrams... 3116 */ 3117 if (err != -EAGAIN) { 3118 /* 3119 * ... or if recvmsg returns an error after we 3120 * received some datagrams, where we record the 3121 * error to return on the next call or if the 3122 * app asks about it using getsockopt(SO_ERROR). 3123 */ 3124 WRITE_ONCE(sock->sk->sk_err, -err); 3125 } 3126 return datagrams; 3127 } 3128 3129 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3130 unsigned int vlen, unsigned int flags, 3131 struct __kernel_timespec __user *timeout, 3132 struct old_timespec32 __user *timeout32) 3133 { 3134 int datagrams; 3135 struct timespec64 timeout_sys; 3136 3137 if (timeout && get_timespec64(&timeout_sys, timeout)) 3138 return -EFAULT; 3139 3140 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32)) 3141 return -EFAULT; 3142 3143 if (!timeout && !timeout32) 3144 return do_recvmmsg(fd, mmsg, vlen, flags, NULL); 3145 3146 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys); 3147 3148 if (datagrams <= 0) 3149 return datagrams; 3150 3151 if (timeout && put_timespec64(&timeout_sys, timeout)) 3152 datagrams = -EFAULT; 3153 3154 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32)) 3155 datagrams = -EFAULT; 3156 3157 return datagrams; 3158 } 3159 3160 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg, 3161 unsigned int, vlen, unsigned int, flags, 3162 struct __kernel_timespec __user *, timeout) 3163 { 3164 if (flags & MSG_CMSG_COMPAT) 3165 return -EINVAL; 3166 3167 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL); 3168 } 3169 3170 #ifdef CONFIG_COMPAT_32BIT_TIME 3171 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg, 3172 unsigned int, vlen, unsigned int, flags, 3173 struct old_timespec32 __user *, timeout) 3174 { 3175 if (flags & MSG_CMSG_COMPAT) 3176 return -EINVAL; 3177 3178 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout); 3179 } 3180 #endif 3181 3182 #ifdef __ARCH_WANT_SYS_SOCKETCALL 3183 /* Argument list sizes for sys_socketcall */ 3184 #define AL(x) ((x) * sizeof(unsigned long)) 3185 static const unsigned char nargs[21] = { 3186 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), 3187 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), 3188 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), 3189 AL(4), AL(5), AL(4) 3190 }; 3191 3192 #undef AL 3193 3194 /* 3195 * System call vectors. 3196 * 3197 * Argument checking cleaned up. Saved 20% in size. 3198 * This function doesn't need to set the kernel lock because 3199 * it is set by the callees. 3200 */ 3201 3202 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) 3203 { 3204 unsigned long a[AUDITSC_ARGS]; 3205 unsigned long a0, a1; 3206 int err; 3207 unsigned int len; 3208 3209 if (call < 1 || call > SYS_SENDMMSG) 3210 return -EINVAL; 3211 call = array_index_nospec(call, SYS_SENDMMSG + 1); 3212 3213 len = nargs[call]; 3214 if (len > sizeof(a)) 3215 return -EINVAL; 3216 3217 /* copy_from_user should be SMP safe. */ 3218 if (copy_from_user(a, args, len)) 3219 return -EFAULT; 3220 3221 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a); 3222 if (err) 3223 return err; 3224 3225 a0 = a[0]; 3226 a1 = a[1]; 3227 3228 switch (call) { 3229 case SYS_SOCKET: 3230 err = __sys_socket(a0, a1, a[2]); 3231 break; 3232 case SYS_BIND: 3233 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]); 3234 break; 3235 case SYS_CONNECT: 3236 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 3237 break; 3238 case SYS_LISTEN: 3239 err = __sys_listen(a0, a1); 3240 break; 3241 case SYS_ACCEPT: 3242 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3243 (int __user *)a[2], 0); 3244 break; 3245 case SYS_GETSOCKNAME: 3246 err = 3247 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3248 (int __user *)a[2], 0); 3249 break; 3250 case SYS_GETPEERNAME: 3251 err = 3252 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3253 (int __user *)a[2], 1); 3254 break; 3255 case SYS_SOCKETPAIR: 3256 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]); 3257 break; 3258 case SYS_SEND: 3259 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3260 NULL, 0); 3261 break; 3262 case SYS_SENDTO: 3263 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3264 (struct sockaddr __user *)a[4], a[5]); 3265 break; 3266 case SYS_RECV: 3267 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3268 NULL, NULL); 3269 break; 3270 case SYS_RECVFROM: 3271 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3272 (struct sockaddr __user *)a[4], 3273 (int __user *)a[5]); 3274 break; 3275 case SYS_SHUTDOWN: 3276 err = __sys_shutdown(a0, a1); 3277 break; 3278 case SYS_SETSOCKOPT: 3279 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3], 3280 a[4]); 3281 break; 3282 case SYS_GETSOCKOPT: 3283 err = 3284 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3], 3285 (int __user *)a[4]); 3286 break; 3287 case SYS_SENDMSG: 3288 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1, 3289 a[2], true); 3290 break; 3291 case SYS_SENDMMSG: 3292 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], 3293 a[3], true); 3294 break; 3295 case SYS_RECVMSG: 3296 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1, 3297 a[2], true); 3298 break; 3299 case SYS_RECVMMSG: 3300 if (IS_ENABLED(CONFIG_64BIT)) 3301 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3302 a[2], a[3], 3303 (struct __kernel_timespec __user *)a[4], 3304 NULL); 3305 else 3306 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3307 a[2], a[3], NULL, 3308 (struct old_timespec32 __user *)a[4]); 3309 break; 3310 case SYS_ACCEPT4: 3311 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3312 (int __user *)a[2], a[3]); 3313 break; 3314 default: 3315 err = -EINVAL; 3316 break; 3317 } 3318 return err; 3319 } 3320 3321 #endif /* __ARCH_WANT_SYS_SOCKETCALL */ 3322 3323 /** 3324 * sock_register - add a socket protocol handler 3325 * @ops: description of protocol 3326 * 3327 * This function is called by a protocol handler that wants to 3328 * advertise its address family, and have it linked into the 3329 * socket interface. The value ops->family corresponds to the 3330 * socket system call protocol family. 3331 */ 3332 int sock_register(const struct net_proto_family *ops) 3333 { 3334 int err; 3335 3336 if (ops->family >= NPROTO) { 3337 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); 3338 return -ENOBUFS; 3339 } 3340 3341 spin_lock(&net_family_lock); 3342 if (rcu_dereference_protected(net_families[ops->family], 3343 lockdep_is_held(&net_family_lock))) 3344 err = -EEXIST; 3345 else { 3346 rcu_assign_pointer(net_families[ops->family], ops); 3347 err = 0; 3348 } 3349 spin_unlock(&net_family_lock); 3350 3351 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]); 3352 return err; 3353 } 3354 EXPORT_SYMBOL(sock_register); 3355 3356 /** 3357 * sock_unregister - remove a protocol handler 3358 * @family: protocol family to remove 3359 * 3360 * This function is called by a protocol handler that wants to 3361 * remove its address family, and have it unlinked from the 3362 * new socket creation. 3363 * 3364 * If protocol handler is a module, then it can use module reference 3365 * counts to protect against new references. If protocol handler is not 3366 * a module then it needs to provide its own protection in 3367 * the ops->create routine. 3368 */ 3369 void sock_unregister(int family) 3370 { 3371 BUG_ON(family < 0 || family >= NPROTO); 3372 3373 spin_lock(&net_family_lock); 3374 RCU_INIT_POINTER(net_families[family], NULL); 3375 spin_unlock(&net_family_lock); 3376 3377 synchronize_rcu(); 3378 3379 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]); 3380 } 3381 EXPORT_SYMBOL(sock_unregister); 3382 3383 bool sock_is_registered(int family) 3384 { 3385 return family < NPROTO && rcu_access_pointer(net_families[family]); 3386 } 3387 3388 static int __init sock_init(void) 3389 { 3390 int err; 3391 /* 3392 * Initialize the network sysctl infrastructure. 3393 */ 3394 err = net_sysctl_init(); 3395 if (err) 3396 goto out; 3397 3398 /* 3399 * Initialize skbuff SLAB cache 3400 */ 3401 skb_init(); 3402 3403 /* 3404 * Initialize the protocols module. 3405 */ 3406 3407 init_inodecache(); 3408 3409 err = register_filesystem(&sock_fs_type); 3410 if (err) 3411 goto out; 3412 sock_mnt = kern_mount(&sock_fs_type); 3413 if (IS_ERR(sock_mnt)) { 3414 err = PTR_ERR(sock_mnt); 3415 goto out_mount; 3416 } 3417 3418 /* The real protocol initialization is performed in later initcalls. 3419 */ 3420 3421 #ifdef CONFIG_NETFILTER 3422 err = netfilter_init(); 3423 if (err) 3424 goto out; 3425 #endif 3426 3427 ptp_classifier_init(); 3428 3429 out: 3430 return err; 3431 3432 out_mount: 3433 unregister_filesystem(&sock_fs_type); 3434 goto out; 3435 } 3436 3437 core_initcall(sock_init); /* early initcall */ 3438 3439 #ifdef CONFIG_PROC_FS 3440 void socket_seq_show(struct seq_file *seq) 3441 { 3442 seq_printf(seq, "sockets: used %d\n", 3443 sock_inuse_get(seq->private)); 3444 } 3445 #endif /* CONFIG_PROC_FS */ 3446 3447 /* Handle the fact that while struct ifreq has the same *layout* on 3448 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data, 3449 * which are handled elsewhere, it still has different *size* due to 3450 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit, 3451 * resulting in struct ifreq being 32 and 40 bytes respectively). 3452 * As a result, if the struct happens to be at the end of a page and 3453 * the next page isn't readable/writable, we get a fault. To prevent 3454 * that, copy back and forth to the full size. 3455 */ 3456 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg) 3457 { 3458 if (in_compat_syscall()) { 3459 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr; 3460 3461 memset(ifr, 0, sizeof(*ifr)); 3462 if (copy_from_user(ifr32, arg, sizeof(*ifr32))) 3463 return -EFAULT; 3464 3465 if (ifrdata) 3466 *ifrdata = compat_ptr(ifr32->ifr_data); 3467 3468 return 0; 3469 } 3470 3471 if (copy_from_user(ifr, arg, sizeof(*ifr))) 3472 return -EFAULT; 3473 3474 if (ifrdata) 3475 *ifrdata = ifr->ifr_data; 3476 3477 return 0; 3478 } 3479 EXPORT_SYMBOL(get_user_ifreq); 3480 3481 int put_user_ifreq(struct ifreq *ifr, void __user *arg) 3482 { 3483 size_t size = sizeof(*ifr); 3484 3485 if (in_compat_syscall()) 3486 size = sizeof(struct compat_ifreq); 3487 3488 if (copy_to_user(arg, ifr, size)) 3489 return -EFAULT; 3490 3491 return 0; 3492 } 3493 EXPORT_SYMBOL(put_user_ifreq); 3494 3495 #ifdef CONFIG_COMPAT 3496 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32) 3497 { 3498 compat_uptr_t uptr32; 3499 struct ifreq ifr; 3500 void __user *saved; 3501 int err; 3502 3503 if (get_user_ifreq(&ifr, NULL, uifr32)) 3504 return -EFAULT; 3505 3506 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu)) 3507 return -EFAULT; 3508 3509 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc; 3510 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32); 3511 3512 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL); 3513 if (!err) { 3514 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved; 3515 if (put_user_ifreq(&ifr, uifr32)) 3516 err = -EFAULT; 3517 } 3518 return err; 3519 } 3520 3521 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */ 3522 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd, 3523 struct compat_ifreq __user *u_ifreq32) 3524 { 3525 struct ifreq ifreq; 3526 void __user *data; 3527 3528 if (!is_socket_ioctl_cmd(cmd)) 3529 return -ENOTTY; 3530 if (get_user_ifreq(&ifreq, &data, u_ifreq32)) 3531 return -EFAULT; 3532 ifreq.ifr_data = data; 3533 3534 return dev_ioctl(net, cmd, &ifreq, data, NULL); 3535 } 3536 3537 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock, 3538 unsigned int cmd, unsigned long arg) 3539 { 3540 void __user *argp = compat_ptr(arg); 3541 struct sock *sk = sock->sk; 3542 struct net *net = sock_net(sk); 3543 const struct proto_ops *ops; 3544 3545 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) 3546 return sock_ioctl(file, cmd, (unsigned long)argp); 3547 3548 switch (cmd) { 3549 case SIOCWANDEV: 3550 return compat_siocwandev(net, argp); 3551 case SIOCGSTAMP_OLD: 3552 case SIOCGSTAMPNS_OLD: 3553 ops = READ_ONCE(sock->ops); 3554 if (!ops->gettstamp) 3555 return -ENOIOCTLCMD; 3556 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD, 3557 !COMPAT_USE_64BIT_TIME); 3558 3559 case SIOCETHTOOL: 3560 case SIOCBONDSLAVEINFOQUERY: 3561 case SIOCBONDINFOQUERY: 3562 case SIOCSHWTSTAMP: 3563 case SIOCGHWTSTAMP: 3564 return compat_ifr_data_ioctl(net, cmd, argp); 3565 3566 case FIOSETOWN: 3567 case SIOCSPGRP: 3568 case FIOGETOWN: 3569 case SIOCGPGRP: 3570 case SIOCBRADDBR: 3571 case SIOCBRDELBR: 3572 case SIOCBRADDIF: 3573 case SIOCBRDELIF: 3574 case SIOCGIFVLAN: 3575 case SIOCSIFVLAN: 3576 case SIOCGSKNS: 3577 case SIOCGSTAMP_NEW: 3578 case SIOCGSTAMPNS_NEW: 3579 case SIOCGIFCONF: 3580 case SIOCSIFBR: 3581 case SIOCGIFBR: 3582 return sock_ioctl(file, cmd, arg); 3583 3584 case SIOCGIFFLAGS: 3585 case SIOCSIFFLAGS: 3586 case SIOCGIFMAP: 3587 case SIOCSIFMAP: 3588 case SIOCGIFMETRIC: 3589 case SIOCSIFMETRIC: 3590 case SIOCGIFMTU: 3591 case SIOCSIFMTU: 3592 case SIOCGIFMEM: 3593 case SIOCSIFMEM: 3594 case SIOCGIFHWADDR: 3595 case SIOCSIFHWADDR: 3596 case SIOCADDMULTI: 3597 case SIOCDELMULTI: 3598 case SIOCGIFINDEX: 3599 case SIOCGIFADDR: 3600 case SIOCSIFADDR: 3601 case SIOCSIFHWBROADCAST: 3602 case SIOCDIFADDR: 3603 case SIOCGIFBRDADDR: 3604 case SIOCSIFBRDADDR: 3605 case SIOCGIFDSTADDR: 3606 case SIOCSIFDSTADDR: 3607 case SIOCGIFNETMASK: 3608 case SIOCSIFNETMASK: 3609 case SIOCSIFPFLAGS: 3610 case SIOCGIFPFLAGS: 3611 case SIOCGIFTXQLEN: 3612 case SIOCSIFTXQLEN: 3613 case SIOCGIFNAME: 3614 case SIOCSIFNAME: 3615 case SIOCGMIIPHY: 3616 case SIOCGMIIREG: 3617 case SIOCSMIIREG: 3618 case SIOCBONDENSLAVE: 3619 case SIOCBONDRELEASE: 3620 case SIOCBONDSETHWADDR: 3621 case SIOCBONDCHANGEACTIVE: 3622 case SIOCSARP: 3623 case SIOCGARP: 3624 case SIOCDARP: 3625 case SIOCOUTQ: 3626 case SIOCOUTQNSD: 3627 case SIOCATMARK: 3628 return sock_do_ioctl(net, sock, cmd, arg); 3629 } 3630 3631 return -ENOIOCTLCMD; 3632 } 3633 3634 static long compat_sock_ioctl(struct file *file, unsigned int cmd, 3635 unsigned long arg) 3636 { 3637 struct socket *sock = file->private_data; 3638 const struct proto_ops *ops = READ_ONCE(sock->ops); 3639 int ret = -ENOIOCTLCMD; 3640 struct sock *sk; 3641 struct net *net; 3642 3643 sk = sock->sk; 3644 net = sock_net(sk); 3645 3646 if (ops->compat_ioctl) 3647 ret = ops->compat_ioctl(sock, cmd, arg); 3648 3649 if (ret == -ENOIOCTLCMD && 3650 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)) 3651 ret = compat_wext_handle_ioctl(net, cmd, arg); 3652 3653 if (ret == -ENOIOCTLCMD) 3654 ret = compat_sock_ioctl_trans(file, sock, cmd, arg); 3655 3656 return ret; 3657 } 3658 #endif 3659 3660 /** 3661 * kernel_bind - bind an address to a socket (kernel space) 3662 * @sock: socket 3663 * @addr: address 3664 * @addrlen: length of address 3665 * 3666 * Returns 0 or an error. 3667 */ 3668 3669 int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen) 3670 { 3671 struct sockaddr_storage address; 3672 3673 memcpy(&address, addr, addrlen); 3674 3675 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr_unsized *)&address, 3676 addrlen); 3677 } 3678 EXPORT_SYMBOL(kernel_bind); 3679 3680 /** 3681 * kernel_listen - move socket to listening state (kernel space) 3682 * @sock: socket 3683 * @backlog: pending connections queue size 3684 * 3685 * Returns 0 or an error. 3686 */ 3687 3688 int kernel_listen(struct socket *sock, int backlog) 3689 { 3690 return READ_ONCE(sock->ops)->listen(sock, backlog); 3691 } 3692 EXPORT_SYMBOL(kernel_listen); 3693 3694 /** 3695 * kernel_accept - accept a connection (kernel space) 3696 * @sock: listening socket 3697 * @newsock: new connected socket 3698 * @flags: flags 3699 * 3700 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0. 3701 * If it fails, @newsock is guaranteed to be %NULL. 3702 * Returns 0 or an error. 3703 */ 3704 3705 int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 3706 { 3707 struct sock *sk = sock->sk; 3708 const struct proto_ops *ops = READ_ONCE(sock->ops); 3709 struct proto_accept_arg arg = { 3710 .flags = flags, 3711 .kern = true, 3712 }; 3713 int err; 3714 3715 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, 3716 newsock); 3717 if (err < 0) 3718 goto done; 3719 3720 err = ops->accept(sock, *newsock, &arg); 3721 if (err < 0) { 3722 sock_release(*newsock); 3723 *newsock = NULL; 3724 goto done; 3725 } 3726 3727 (*newsock)->ops = ops; 3728 __module_get(ops->owner); 3729 3730 done: 3731 return err; 3732 } 3733 EXPORT_SYMBOL(kernel_accept); 3734 3735 /** 3736 * kernel_connect - connect a socket (kernel space) 3737 * @sock: socket 3738 * @addr: address 3739 * @addrlen: address length 3740 * @flags: flags (O_NONBLOCK, ...) 3741 * 3742 * For datagram sockets, @addr is the address to which datagrams are sent 3743 * by default, and the only address from which datagrams are received. 3744 * For stream sockets, attempts to connect to @addr. 3745 * Returns 0 or an error code. 3746 */ 3747 3748 int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen, 3749 int flags) 3750 { 3751 struct sockaddr_storage address; 3752 3753 memcpy(&address, addr, addrlen); 3754 3755 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)&address, 3756 addrlen, flags); 3757 } 3758 EXPORT_SYMBOL(kernel_connect); 3759 3760 /** 3761 * kernel_getsockname - get the address which the socket is bound (kernel space) 3762 * @sock: socket 3763 * @addr: address holder 3764 * 3765 * Fills the @addr pointer with the address which the socket is bound. 3766 * Returns the length of the address in bytes or an error code. 3767 */ 3768 3769 int kernel_getsockname(struct socket *sock, struct sockaddr *addr) 3770 { 3771 return READ_ONCE(sock->ops)->getname(sock, addr, 0); 3772 } 3773 EXPORT_SYMBOL(kernel_getsockname); 3774 3775 /** 3776 * kernel_getpeername - get the address which the socket is connected (kernel space) 3777 * @sock: socket 3778 * @addr: address holder 3779 * 3780 * Fills the @addr pointer with the address which the socket is connected. 3781 * Returns the length of the address in bytes or an error code. 3782 */ 3783 3784 int kernel_getpeername(struct socket *sock, struct sockaddr *addr) 3785 { 3786 return READ_ONCE(sock->ops)->getname(sock, addr, 1); 3787 } 3788 EXPORT_SYMBOL(kernel_getpeername); 3789 3790 /** 3791 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space) 3792 * @sock: socket 3793 * @how: connection part 3794 * 3795 * Returns 0 or an error. 3796 */ 3797 3798 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how) 3799 { 3800 return READ_ONCE(sock->ops)->shutdown(sock, how); 3801 } 3802 EXPORT_SYMBOL(kernel_sock_shutdown); 3803 3804 /** 3805 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket 3806 * @sk: socket 3807 * 3808 * This routine returns the IP overhead imposed by a socket i.e. 3809 * the length of the underlying IP header, depending on whether 3810 * this is an IPv4 or IPv6 socket and the length from IP options turned 3811 * on at the socket. Assumes that the caller has a lock on the socket. 3812 */ 3813 3814 u32 kernel_sock_ip_overhead(struct sock *sk) 3815 { 3816 struct inet_sock *inet; 3817 struct ip_options_rcu *opt; 3818 u32 overhead = 0; 3819 #if IS_ENABLED(CONFIG_IPV6) 3820 struct ipv6_pinfo *np; 3821 struct ipv6_txoptions *optv6 = NULL; 3822 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3823 3824 if (!sk) 3825 return overhead; 3826 3827 switch (sk->sk_family) { 3828 case AF_INET: 3829 inet = inet_sk(sk); 3830 overhead += sizeof(struct iphdr); 3831 opt = rcu_dereference_protected(inet->inet_opt, 3832 sock_owned_by_user(sk)); 3833 if (opt) 3834 overhead += opt->opt.optlen; 3835 return overhead; 3836 #if IS_ENABLED(CONFIG_IPV6) 3837 case AF_INET6: 3838 np = inet6_sk(sk); 3839 overhead += sizeof(struct ipv6hdr); 3840 if (np) 3841 optv6 = rcu_dereference_protected(np->opt, 3842 sock_owned_by_user(sk)); 3843 if (optv6) 3844 overhead += (optv6->opt_flen + optv6->opt_nflen); 3845 return overhead; 3846 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3847 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */ 3848 return overhead; 3849 } 3850 } 3851 EXPORT_SYMBOL(kernel_sock_ip_overhead); 3852