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 /* Reserve both descriptors before creating the sockets. */ 1855 fd1 = get_unused_fd_flags(flags); 1856 if (unlikely(fd1 < 0)) 1857 return fd1; 1858 1859 fd2 = get_unused_fd_flags(flags); 1860 if (unlikely(fd2 < 0)) { 1861 put_unused_fd(fd1); 1862 return fd2; 1863 } 1864 1865 /* 1866 * Obtain the first socket and check if the underlying protocol 1867 * supports the socketpair call. 1868 */ 1869 1870 err = sock_create(family, type, protocol, &sock1); 1871 if (unlikely(err < 0)) 1872 goto out; 1873 1874 err = sock_create(family, type, protocol, &sock2); 1875 if (unlikely(err < 0)) { 1876 sock_release(sock1); 1877 goto out; 1878 } 1879 1880 err = security_socket_socketpair(sock1, sock2); 1881 if (unlikely(err)) { 1882 sock_release(sock2); 1883 sock_release(sock1); 1884 goto out; 1885 } 1886 1887 err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2); 1888 if (unlikely(err < 0)) { 1889 sock_release(sock2); 1890 sock_release(sock1); 1891 goto out; 1892 } 1893 1894 newfile1 = sock_alloc_file(sock1, flags, NULL); 1895 if (IS_ERR(newfile1)) { 1896 err = PTR_ERR(newfile1); 1897 sock_release(sock2); 1898 goto out; 1899 } 1900 1901 newfile2 = sock_alloc_file(sock2, flags, NULL); 1902 if (IS_ERR(newfile2)) { 1903 err = PTR_ERR(newfile2); 1904 fput(newfile1); 1905 goto out; 1906 } 1907 1908 /* Publish the descriptors now that it shouldn't fail. */ 1909 err = put_user(fd1, &usockvec[0]); 1910 if (!err) 1911 err = put_user(fd2, &usockvec[1]); 1912 if (err) { 1913 fput(newfile2); 1914 fput(newfile1); 1915 goto out; 1916 } 1917 1918 audit_fd_pair(fd1, fd2); 1919 1920 fd_install(fd1, newfile1); 1921 fd_install(fd2, newfile2); 1922 return 0; 1923 1924 out: 1925 put_unused_fd(fd2); 1926 put_unused_fd(fd1); 1927 return err; 1928 } 1929 1930 SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol, 1931 int __user *, usockvec) 1932 { 1933 return __sys_socketpair(family, type, protocol, usockvec); 1934 } 1935 1936 int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address, 1937 int addrlen) 1938 { 1939 int err; 1940 1941 err = security_socket_bind(sock, (struct sockaddr *)address, 1942 addrlen); 1943 if (!err) 1944 err = READ_ONCE(sock->ops)->bind(sock, 1945 (struct sockaddr_unsized *)address, 1946 addrlen); 1947 return err; 1948 } 1949 1950 /* 1951 * Bind a name to a socket. Nothing much to do here since it's 1952 * the protocol's responsibility to handle the local address. 1953 * 1954 * We move the socket address to kernel space before we call 1955 * the protocol layer (having also checked the address is ok). 1956 */ 1957 1958 int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) 1959 { 1960 struct socket *sock; 1961 struct sockaddr_storage address; 1962 CLASS(fd, f)(fd); 1963 int err; 1964 1965 if (fd_empty(f)) 1966 return -EBADF; 1967 sock = sock_from_file(fd_file(f)); 1968 if (unlikely(!sock)) 1969 return -ENOTSOCK; 1970 1971 err = move_addr_to_kernel(umyaddr, addrlen, &address); 1972 if (unlikely(err)) 1973 return err; 1974 1975 return __sys_bind_socket(sock, &address, addrlen); 1976 } 1977 1978 SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen) 1979 { 1980 return __sys_bind(fd, umyaddr, addrlen); 1981 } 1982 1983 /* 1984 * Perform a listen. Basically, we allow the protocol to do anything 1985 * necessary for a listen, and if that works, we mark the socket as 1986 * ready for listening. 1987 */ 1988 int __sys_listen_socket(struct socket *sock, int backlog) 1989 { 1990 int somaxconn, err; 1991 1992 somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn); 1993 if ((unsigned int)backlog > somaxconn) 1994 backlog = somaxconn; 1995 1996 err = security_socket_listen(sock, backlog); 1997 if (!err) 1998 err = READ_ONCE(sock->ops)->listen(sock, backlog); 1999 return err; 2000 } 2001 2002 int __sys_listen(int fd, int backlog) 2003 { 2004 CLASS(fd, f)(fd); 2005 struct socket *sock; 2006 2007 if (fd_empty(f)) 2008 return -EBADF; 2009 sock = sock_from_file(fd_file(f)); 2010 if (unlikely(!sock)) 2011 return -ENOTSOCK; 2012 2013 return __sys_listen_socket(sock, backlog); 2014 } 2015 2016 SYSCALL_DEFINE2(listen, int, fd, int, backlog) 2017 { 2018 return __sys_listen(fd, backlog); 2019 } 2020 2021 struct file *do_accept(struct file *file, struct proto_accept_arg *arg, 2022 struct sockaddr __user *upeer_sockaddr, 2023 int __user *upeer_addrlen, int flags) 2024 { 2025 struct socket *sock, *newsock; 2026 struct file *newfile; 2027 int err, len; 2028 struct sockaddr_storage address; 2029 const struct proto_ops *ops; 2030 2031 sock = sock_from_file(file); 2032 if (!sock) 2033 return ERR_PTR(-ENOTSOCK); 2034 2035 newsock = sock_alloc(); 2036 if (!newsock) 2037 return ERR_PTR(-ENFILE); 2038 ops = READ_ONCE(sock->ops); 2039 2040 newsock->type = sock->type; 2041 newsock->ops = ops; 2042 2043 /* 2044 * We don't need try_module_get here, as the listening socket (sock) 2045 * has the protocol module (sock->ops->owner) held. 2046 */ 2047 __module_get(ops->owner); 2048 2049 newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name); 2050 if (IS_ERR(newfile)) 2051 return newfile; 2052 2053 err = security_socket_accept(sock, newsock); 2054 if (err) 2055 goto out_fd; 2056 2057 arg->flags |= sock->file->f_flags; 2058 err = ops->accept(sock, newsock, arg); 2059 if (err < 0) 2060 goto out_fd; 2061 2062 if (upeer_sockaddr) { 2063 len = ops->getname(newsock, (struct sockaddr *)&address, 2); 2064 if (len < 0) { 2065 err = -ECONNABORTED; 2066 goto out_fd; 2067 } 2068 err = move_addr_to_user(&address, 2069 len, upeer_sockaddr, upeer_addrlen); 2070 if (err < 0) 2071 goto out_fd; 2072 } 2073 2074 /* File flags are not inherited via accept() unlike another OSes. */ 2075 return newfile; 2076 out_fd: 2077 fput(newfile); 2078 return ERR_PTR(err); 2079 } 2080 2081 static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr, 2082 int __user *upeer_addrlen, int flags) 2083 { 2084 struct proto_accept_arg arg = { }; 2085 2086 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) 2087 return -EINVAL; 2088 2089 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) 2090 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK; 2091 2092 return FD_ADD(flags, do_accept(file, &arg, upeer_sockaddr, upeer_addrlen, flags)); 2093 } 2094 2095 /* 2096 * For accept, we attempt to create a new socket, set up the link 2097 * with the client, wake up the client, then return the new 2098 * connected fd. We collect the address of the connector in kernel 2099 * space and move it to user at the very end. This is unclean because 2100 * we open the socket then return an error. 2101 * 2102 * 1003.1g adds the ability to recvmsg() to query connection pending 2103 * status. We need to add that support in a way that's 2104 * clean when we restructure accept also. 2105 */ 2106 2107 int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr, 2108 int __user *upeer_addrlen, int flags) 2109 { 2110 CLASS(fd, f)(fd); 2111 2112 if (fd_empty(f)) 2113 return -EBADF; 2114 return __sys_accept4_file(fd_file(f), upeer_sockaddr, 2115 upeer_addrlen, flags); 2116 } 2117 2118 SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr, 2119 int __user *, upeer_addrlen, int, flags) 2120 { 2121 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags); 2122 } 2123 2124 SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, 2125 int __user *, upeer_addrlen) 2126 { 2127 return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0); 2128 } 2129 2130 int connect_socket(struct socket *sock, struct sockaddr_storage *address, 2131 int addrlen, int flags) 2132 { 2133 int err; 2134 2135 err = security_socket_connect(sock, (struct sockaddr *)address, 2136 addrlen); 2137 if (err) 2138 return err; 2139 2140 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address, 2141 addrlen, flags); 2142 } 2143 2144 /* 2145 * Attempt to connect to a socket with the server address. The address 2146 * is in user space so we verify it is OK and move it to kernel space. 2147 * 2148 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to 2149 * break bindings 2150 * 2151 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and 2152 * other SEQPACKET protocols that take time to connect() as it doesn't 2153 * include the -EINPROGRESS status for such sockets. 2154 */ 2155 2156 int __sys_connect_file(struct file *file, struct sockaddr_storage *address, 2157 int addrlen, int file_flags) 2158 { 2159 struct socket *sock; 2160 2161 sock = sock_from_file(file); 2162 if (!sock) 2163 return -ENOTSOCK; 2164 2165 return connect_socket(sock, address, addrlen, 2166 sock->file->f_flags | file_flags); 2167 } 2168 2169 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) 2170 { 2171 struct sockaddr_storage address; 2172 CLASS(fd, f)(fd); 2173 int ret; 2174 2175 if (fd_empty(f)) 2176 return -EBADF; 2177 2178 ret = move_addr_to_kernel(uservaddr, addrlen, &address); 2179 if (ret) 2180 return ret; 2181 2182 return __sys_connect_file(fd_file(f), &address, addrlen, 0); 2183 } 2184 2185 SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr, 2186 int, addrlen) 2187 { 2188 return __sys_connect(fd, uservaddr, addrlen); 2189 } 2190 2191 int do_getsockname(struct socket *sock, int peer, 2192 struct sockaddr __user *usockaddr, int __user *usockaddr_len) 2193 { 2194 struct sockaddr_storage address; 2195 int err; 2196 2197 if (peer) 2198 err = security_socket_getpeername(sock); 2199 else 2200 err = security_socket_getsockname(sock); 2201 if (err) 2202 return err; 2203 err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer); 2204 if (err < 0) 2205 return err; 2206 /* "err" is actually length in this case */ 2207 return move_addr_to_user(&address, err, usockaddr, usockaddr_len); 2208 } 2209 2210 /* 2211 * Get the remote or local address ('name') of a socket object. Move the 2212 * obtained name to user space. 2213 */ 2214 int __sys_getsockname(int fd, struct sockaddr __user *usockaddr, 2215 int __user *usockaddr_len, int peer) 2216 { 2217 struct socket *sock; 2218 CLASS(fd, f)(fd); 2219 2220 if (fd_empty(f)) 2221 return -EBADF; 2222 sock = sock_from_file(fd_file(f)); 2223 if (unlikely(!sock)) 2224 return -ENOTSOCK; 2225 return do_getsockname(sock, peer, usockaddr, usockaddr_len); 2226 } 2227 2228 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr, 2229 int __user *, usockaddr_len) 2230 { 2231 return __sys_getsockname(fd, usockaddr, usockaddr_len, 0); 2232 } 2233 2234 SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr, 2235 int __user *, usockaddr_len) 2236 { 2237 return __sys_getsockname(fd, usockaddr, usockaddr_len, 1); 2238 } 2239 2240 /* 2241 * Send a datagram to a given address. We move the address into kernel 2242 * space and check the user space data area is readable before invoking 2243 * the protocol. 2244 */ 2245 int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags, 2246 struct sockaddr __user *addr, int addr_len) 2247 { 2248 struct socket *sock; 2249 struct sockaddr_storage address; 2250 int err; 2251 struct msghdr msg; 2252 2253 err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter); 2254 if (unlikely(err)) 2255 return err; 2256 2257 CLASS(fd, f)(fd); 2258 if (fd_empty(f)) 2259 return -EBADF; 2260 sock = sock_from_file(fd_file(f)); 2261 if (unlikely(!sock)) 2262 return -ENOTSOCK; 2263 2264 msg.msg_name = NULL; 2265 msg.msg_control = NULL; 2266 msg.msg_controllen = 0; 2267 msg.msg_namelen = 0; 2268 msg.msg_ubuf = NULL; 2269 if (addr) { 2270 err = move_addr_to_kernel(addr, addr_len, &address); 2271 if (err < 0) 2272 return err; 2273 msg.msg_name = (struct sockaddr *)&address; 2274 msg.msg_namelen = addr_len; 2275 } 2276 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2277 if (sock->file->f_flags & O_NONBLOCK) 2278 flags |= MSG_DONTWAIT; 2279 msg.msg_flags = flags; 2280 return __sock_sendmsg(sock, &msg); 2281 } 2282 2283 SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, 2284 unsigned int, flags, struct sockaddr __user *, addr, 2285 int, addr_len) 2286 { 2287 return __sys_sendto(fd, buff, len, flags, addr, addr_len); 2288 } 2289 2290 /* 2291 * Send a datagram down a socket. 2292 */ 2293 2294 SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, 2295 unsigned int, flags) 2296 { 2297 return __sys_sendto(fd, buff, len, flags, NULL, 0); 2298 } 2299 2300 /* 2301 * Receive a frame from the socket and optionally record the address of the 2302 * sender. We verify the buffers are writable and if needed move the 2303 * sender address from kernel to user space. 2304 */ 2305 int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags, 2306 struct sockaddr __user *addr, int __user *addr_len) 2307 { 2308 struct sockaddr_storage address; 2309 struct msghdr msg = { 2310 /* Save some cycles and don't copy the address if not needed */ 2311 .msg_name = addr ? (struct sockaddr *)&address : NULL, 2312 }; 2313 struct socket *sock; 2314 int err, err2; 2315 2316 err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter); 2317 if (unlikely(err)) 2318 return err; 2319 2320 CLASS(fd, f)(fd); 2321 2322 if (fd_empty(f)) 2323 return -EBADF; 2324 sock = sock_from_file(fd_file(f)); 2325 if (unlikely(!sock)) 2326 return -ENOTSOCK; 2327 2328 if (sock->file->f_flags & O_NONBLOCK) 2329 flags |= MSG_DONTWAIT; 2330 err = sock_recvmsg(sock, &msg, flags); 2331 2332 if (err >= 0 && addr != NULL) { 2333 err2 = move_addr_to_user(&address, 2334 msg.msg_namelen, addr, addr_len); 2335 if (err2 < 0) 2336 err = err2; 2337 } 2338 return err; 2339 } 2340 2341 SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size, 2342 unsigned int, flags, struct sockaddr __user *, addr, 2343 int __user *, addr_len) 2344 { 2345 return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len); 2346 } 2347 2348 /* 2349 * Receive a datagram from a socket. 2350 */ 2351 2352 SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size, 2353 unsigned int, flags) 2354 { 2355 return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 2356 } 2357 2358 static bool sock_use_custom_sol_socket(const struct socket *sock) 2359 { 2360 return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags); 2361 } 2362 2363 int do_sock_setsockopt(struct socket *sock, bool compat, int level, 2364 int optname, sockptr_t optval, int optlen) 2365 { 2366 const struct proto_ops *ops; 2367 char *kernel_optval = NULL; 2368 int err; 2369 2370 if (optlen < 0) 2371 return -EINVAL; 2372 2373 err = security_socket_setsockopt(sock, level, optname); 2374 if (err) 2375 goto out_put; 2376 2377 if (!compat) 2378 err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname, 2379 optval, &optlen, 2380 &kernel_optval); 2381 if (err < 0) 2382 goto out_put; 2383 if (err > 0) { 2384 err = 0; 2385 goto out_put; 2386 } 2387 2388 if (kernel_optval) 2389 optval = KERNEL_SOCKPTR(kernel_optval); 2390 ops = READ_ONCE(sock->ops); 2391 if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock)) 2392 err = sock_setsockopt(sock, level, optname, optval, optlen); 2393 else if (unlikely(!ops->setsockopt)) 2394 err = -EOPNOTSUPP; 2395 else 2396 err = ops->setsockopt(sock, level, optname, optval, 2397 optlen); 2398 kfree(kernel_optval); 2399 out_put: 2400 return err; 2401 } 2402 EXPORT_SYMBOL(do_sock_setsockopt); 2403 2404 /* Set a socket option. Because we don't know the option lengths we have 2405 * to pass the user mode parameter for the protocols to sort out. 2406 */ 2407 int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval, 2408 int optlen) 2409 { 2410 sockptr_t optval = USER_SOCKPTR(user_optval); 2411 bool compat = in_compat_syscall(); 2412 struct socket *sock; 2413 CLASS(fd, f)(fd); 2414 2415 if (fd_empty(f)) 2416 return -EBADF; 2417 sock = sock_from_file(fd_file(f)); 2418 if (unlikely(!sock)) 2419 return -ENOTSOCK; 2420 2421 return do_sock_setsockopt(sock, compat, level, optname, optval, optlen); 2422 } 2423 2424 SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, 2425 char __user *, optval, int, optlen) 2426 { 2427 return __sys_setsockopt(fd, level, optname, optval, optlen); 2428 } 2429 2430 INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level, 2431 int optname)); 2432 2433 /* 2434 * Initialize a sockopt_t from sockptr optval/optlen, setting up iov_iter 2435 * for both input and output directions. 2436 * It is important to remember that both iov points to the same data, but, 2437 * .iter_in is read-only and .iter_out is write-only by the protocol callbacks 2438 */ 2439 int sockptr_to_sockopt(sockopt_t *opt, sockptr_t optval, 2440 sockptr_t optlen, struct kvec *kvec) 2441 { 2442 int koptlen; 2443 2444 if (copy_from_sockptr(&koptlen, optlen, sizeof(int))) 2445 return -EFAULT; 2446 2447 if (koptlen < 0) 2448 return -EINVAL; 2449 2450 if (optval.is_kernel) { 2451 kvec->iov_base = optval.kernel; 2452 kvec->iov_len = koptlen; 2453 iov_iter_kvec(&opt->iter_out, ITER_DEST, kvec, 1, koptlen); 2454 iov_iter_kvec(&opt->iter_in, ITER_SOURCE, kvec, 1, koptlen); 2455 } else { 2456 iov_iter_ubuf(&opt->iter_out, ITER_DEST, optval.user, koptlen); 2457 iov_iter_ubuf(&opt->iter_in, ITER_SOURCE, optval.user, 2458 koptlen); 2459 } 2460 opt->optlen = koptlen; 2461 2462 return 0; 2463 } 2464 2465 int do_sock_getsockopt(struct socket *sock, bool compat, int level, 2466 int optname, sockptr_t optval, sockptr_t optlen) 2467 { 2468 int max_optlen __maybe_unused = 0; 2469 const struct proto_ops *ops; 2470 struct kvec kvec; 2471 sockopt_t opt; 2472 int err; 2473 2474 err = security_socket_getsockopt(sock, level, optname); 2475 if (err) 2476 return err; 2477 2478 if (!compat) 2479 copy_from_sockptr(&max_optlen, optlen, sizeof(int)); 2480 2481 ops = READ_ONCE(sock->ops); 2482 if (level == SOL_SOCKET) { 2483 err = sk_getsockopt(sock->sk, level, optname, optval, optlen); 2484 } else if (ops->getsockopt_iter) { 2485 err = sockptr_to_sockopt(&opt, optval, optlen, &kvec); 2486 if (err) 2487 return err; 2488 2489 err = ops->getsockopt_iter(sock, level, optname, &opt); 2490 2491 /* Always write back optlen, even on failure. Some protocols 2492 * (e.g. CAN raw) return -ERANGE and set optlen to the 2493 * required buffer size so userspace can discover it. 2494 */ 2495 if (copy_to_sockptr(optlen, &opt.optlen, sizeof(int))) 2496 return -EFAULT; 2497 } else if (ops->getsockopt) { 2498 if (WARN_ONCE(optval.is_kernel || optlen.is_kernel, 2499 "Invalid argument type")) 2500 return -EOPNOTSUPP; 2501 2502 err = ops->getsockopt(sock, level, optname, optval.user, 2503 optlen.user); 2504 } else { 2505 err = -EOPNOTSUPP; 2506 } 2507 2508 if (!compat) 2509 err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname, 2510 optval, optlen, max_optlen, 2511 err); 2512 2513 return err; 2514 } 2515 EXPORT_SYMBOL(do_sock_getsockopt); 2516 2517 /* 2518 * Get a socket option. Because we don't know the option lengths we have 2519 * to pass a user mode parameter for the protocols to sort out. 2520 */ 2521 int __sys_getsockopt(int fd, int level, int optname, char __user *optval, 2522 int __user *optlen) 2523 { 2524 struct socket *sock; 2525 CLASS(fd, f)(fd); 2526 2527 if (fd_empty(f)) 2528 return -EBADF; 2529 sock = sock_from_file(fd_file(f)); 2530 if (unlikely(!sock)) 2531 return -ENOTSOCK; 2532 2533 return do_sock_getsockopt(sock, in_compat_syscall(), level, optname, 2534 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 2535 } 2536 2537 SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, 2538 char __user *, optval, int __user *, optlen) 2539 { 2540 return __sys_getsockopt(fd, level, optname, optval, optlen); 2541 } 2542 2543 /* 2544 * Shutdown a socket. 2545 */ 2546 2547 int __sys_shutdown_sock(struct socket *sock, int how) 2548 { 2549 int err; 2550 2551 err = security_socket_shutdown(sock, how); 2552 if (!err) 2553 err = READ_ONCE(sock->ops)->shutdown(sock, how); 2554 2555 return err; 2556 } 2557 2558 int __sys_shutdown(int fd, int how) 2559 { 2560 struct socket *sock; 2561 CLASS(fd, f)(fd); 2562 2563 if (fd_empty(f)) 2564 return -EBADF; 2565 sock = sock_from_file(fd_file(f)); 2566 if (unlikely(!sock)) 2567 return -ENOTSOCK; 2568 2569 return __sys_shutdown_sock(sock, how); 2570 } 2571 2572 SYSCALL_DEFINE2(shutdown, int, fd, int, how) 2573 { 2574 return __sys_shutdown(fd, how); 2575 } 2576 2577 /* A couple of helpful macros for getting the address of the 32/64 bit 2578 * fields which are the same type (int / unsigned) on our platforms. 2579 */ 2580 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 2581 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 2582 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 2583 2584 struct used_address { 2585 struct sockaddr_storage name; 2586 unsigned int name_len; 2587 }; 2588 2589 int __copy_msghdr(struct msghdr *kmsg, 2590 struct user_msghdr *msg, 2591 struct sockaddr __user **save_addr) 2592 { 2593 ssize_t err; 2594 2595 kmsg->msg_control_is_user = true; 2596 kmsg->msg_get_inq = 0; 2597 kmsg->msg_control_user = msg->msg_control; 2598 kmsg->msg_controllen = msg->msg_controllen; 2599 kmsg->msg_flags = msg->msg_flags; 2600 2601 kmsg->msg_namelen = msg->msg_namelen; 2602 if (!msg->msg_name) 2603 kmsg->msg_namelen = 0; 2604 2605 if (kmsg->msg_namelen < 0) 2606 return -EINVAL; 2607 2608 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 2609 kmsg->msg_namelen = sizeof(struct sockaddr_storage); 2610 2611 if (save_addr) 2612 *save_addr = msg->msg_name; 2613 2614 if (msg->msg_name && kmsg->msg_namelen) { 2615 if (!save_addr) { 2616 err = move_addr_to_kernel(msg->msg_name, 2617 kmsg->msg_namelen, 2618 kmsg->msg_name); 2619 if (err < 0) 2620 return err; 2621 } 2622 } else { 2623 kmsg->msg_name = NULL; 2624 kmsg->msg_namelen = 0; 2625 } 2626 2627 if (msg->msg_iovlen > UIO_MAXIOV) 2628 return -EMSGSIZE; 2629 2630 kmsg->msg_ubuf = NULL; 2631 return 0; 2632 } 2633 2634 static int copy_msghdr_from_user(struct msghdr *kmsg, 2635 struct user_msghdr __user *umsg, 2636 struct sockaddr __user **save_addr, 2637 struct iovec **iov) 2638 { 2639 struct user_msghdr msg; 2640 ssize_t err; 2641 2642 if (copy_from_user(&msg, umsg, sizeof(*umsg))) 2643 return -EFAULT; 2644 2645 err = __copy_msghdr(kmsg, &msg, save_addr); 2646 if (err) 2647 return err; 2648 2649 err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE, 2650 msg.msg_iov, msg.msg_iovlen, 2651 UIO_FASTIOV, iov, &kmsg->msg_iter); 2652 return err < 0 ? err : 0; 2653 } 2654 2655 static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys, 2656 unsigned int flags, struct used_address *used_address, 2657 unsigned int allowed_msghdr_flags) 2658 { 2659 unsigned char ctl[sizeof(struct cmsghdr) + 20] 2660 __aligned(sizeof(__kernel_size_t)); 2661 /* 20 is size of ipv6_pktinfo */ 2662 unsigned char *ctl_buf = ctl; 2663 int ctl_len; 2664 ssize_t err; 2665 2666 err = -ENOBUFS; 2667 2668 if (msg_sys->msg_controllen > INT_MAX) 2669 goto out; 2670 flags |= (msg_sys->msg_flags & allowed_msghdr_flags); 2671 ctl_len = msg_sys->msg_controllen; 2672 if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 2673 err = 2674 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl, 2675 sizeof(ctl)); 2676 if (err) 2677 goto out; 2678 ctl_buf = msg_sys->msg_control; 2679 ctl_len = msg_sys->msg_controllen; 2680 } else if (ctl_len) { 2681 BUILD_BUG_ON(sizeof(struct cmsghdr) != 2682 CMSG_ALIGN(sizeof(struct cmsghdr))); 2683 if (ctl_len > sizeof(ctl)) { 2684 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 2685 if (ctl_buf == NULL) 2686 goto out; 2687 } 2688 err = -EFAULT; 2689 if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len)) 2690 goto out_freectl; 2691 msg_sys->msg_control = ctl_buf; 2692 msg_sys->msg_control_is_user = false; 2693 } 2694 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS; 2695 msg_sys->msg_flags = flags; 2696 2697 if (sock->file->f_flags & O_NONBLOCK) 2698 msg_sys->msg_flags |= MSG_DONTWAIT; 2699 /* 2700 * If this is sendmmsg() and current destination address is same as 2701 * previously succeeded address, omit asking LSM's decision. 2702 * used_address->name_len is initialized to UINT_MAX so that the first 2703 * destination address never matches. 2704 */ 2705 if (used_address && msg_sys->msg_name && 2706 used_address->name_len == msg_sys->msg_namelen && 2707 !memcmp(&used_address->name, msg_sys->msg_name, 2708 used_address->name_len)) { 2709 err = sock_sendmsg_nosec(sock, msg_sys); 2710 goto out_freectl; 2711 } 2712 err = __sock_sendmsg(sock, msg_sys); 2713 /* 2714 * If this is sendmmsg() and sending to current destination address was 2715 * successful, remember it. 2716 */ 2717 if (used_address && err >= 0) { 2718 used_address->name_len = msg_sys->msg_namelen; 2719 if (msg_sys->msg_name) 2720 memcpy(&used_address->name, msg_sys->msg_name, 2721 used_address->name_len); 2722 } 2723 2724 out_freectl: 2725 if (ctl_buf != ctl) 2726 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 2727 out: 2728 return err; 2729 } 2730 2731 static int sendmsg_copy_msghdr(struct msghdr *msg, 2732 struct user_msghdr __user *umsg, unsigned flags, 2733 struct iovec **iov) 2734 { 2735 int err; 2736 2737 if (flags & MSG_CMSG_COMPAT) { 2738 struct compat_msghdr __user *msg_compat; 2739 2740 msg_compat = (struct compat_msghdr __user *) umsg; 2741 err = get_compat_msghdr(msg, msg_compat, NULL, iov); 2742 } else { 2743 err = copy_msghdr_from_user(msg, umsg, NULL, iov); 2744 } 2745 if (err < 0) 2746 return err; 2747 2748 return 0; 2749 } 2750 2751 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg, 2752 struct msghdr *msg_sys, unsigned int flags, 2753 struct used_address *used_address, 2754 unsigned int allowed_msghdr_flags) 2755 { 2756 struct sockaddr_storage address; 2757 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2758 ssize_t err; 2759 2760 msg_sys->msg_name = &address; 2761 2762 err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov); 2763 if (err < 0) 2764 return err; 2765 2766 err = ____sys_sendmsg(sock, msg_sys, flags, used_address, 2767 allowed_msghdr_flags); 2768 kfree(iov); 2769 return err; 2770 } 2771 2772 /* 2773 * BSD sendmsg interface 2774 */ 2775 long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg, 2776 unsigned int flags) 2777 { 2778 return ____sys_sendmsg(sock, msg, flags, NULL, 0); 2779 } 2780 2781 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2782 bool forbid_cmsg_compat) 2783 { 2784 struct msghdr msg_sys; 2785 struct socket *sock; 2786 2787 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2788 return -EINVAL; 2789 2790 CLASS(fd, f)(fd); 2791 2792 if (fd_empty(f)) 2793 return -EBADF; 2794 sock = sock_from_file(fd_file(f)); 2795 if (unlikely(!sock)) 2796 return -ENOTSOCK; 2797 2798 return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0); 2799 } 2800 2801 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags) 2802 { 2803 return __sys_sendmsg(fd, msg, flags, true); 2804 } 2805 2806 /* 2807 * Linux sendmmsg interface 2808 */ 2809 2810 int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen, 2811 unsigned int flags, bool forbid_cmsg_compat) 2812 { 2813 int err, datagrams; 2814 struct socket *sock; 2815 struct mmsghdr __user *entry; 2816 struct compat_mmsghdr __user *compat_entry; 2817 struct msghdr msg_sys; 2818 struct used_address used_address; 2819 unsigned int oflags = flags; 2820 2821 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2822 return -EINVAL; 2823 2824 if (vlen > UIO_MAXIOV) 2825 vlen = UIO_MAXIOV; 2826 2827 datagrams = 0; 2828 2829 CLASS(fd, f)(fd); 2830 2831 if (fd_empty(f)) 2832 return -EBADF; 2833 sock = sock_from_file(fd_file(f)); 2834 if (unlikely(!sock)) 2835 return -ENOTSOCK; 2836 2837 used_address.name_len = UINT_MAX; 2838 entry = mmsg; 2839 compat_entry = (struct compat_mmsghdr __user *)mmsg; 2840 err = 0; 2841 flags |= MSG_BATCH; 2842 2843 while (datagrams < vlen) { 2844 if (datagrams == vlen - 1) 2845 flags = oflags; 2846 2847 if (MSG_CMSG_COMPAT & flags) { 2848 err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry, 2849 &msg_sys, flags, &used_address, MSG_EOR); 2850 if (err < 0) 2851 break; 2852 err = __put_user(err, &compat_entry->msg_len); 2853 ++compat_entry; 2854 } else { 2855 err = ___sys_sendmsg(sock, 2856 (struct user_msghdr __user *)entry, 2857 &msg_sys, flags, &used_address, MSG_EOR); 2858 if (err < 0) 2859 break; 2860 err = put_user(err, &entry->msg_len); 2861 ++entry; 2862 } 2863 2864 if (err) 2865 break; 2866 ++datagrams; 2867 if (msg_data_left(&msg_sys)) 2868 break; 2869 cond_resched(); 2870 } 2871 2872 /* We only return an error if no datagrams were able to be sent */ 2873 if (datagrams != 0) 2874 return datagrams; 2875 2876 return err; 2877 } 2878 2879 SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg, 2880 unsigned int, vlen, unsigned int, flags) 2881 { 2882 return __sys_sendmmsg(fd, mmsg, vlen, flags, true); 2883 } 2884 2885 static int recvmsg_copy_msghdr(struct msghdr *msg, 2886 struct user_msghdr __user *umsg, unsigned flags, 2887 struct sockaddr __user **uaddr, 2888 struct iovec **iov) 2889 { 2890 ssize_t err; 2891 2892 if (MSG_CMSG_COMPAT & flags) { 2893 struct compat_msghdr __user *msg_compat; 2894 2895 msg_compat = (struct compat_msghdr __user *) umsg; 2896 err = get_compat_msghdr(msg, msg_compat, uaddr, iov); 2897 } else { 2898 err = copy_msghdr_from_user(msg, umsg, uaddr, iov); 2899 } 2900 if (err < 0) 2901 return err; 2902 2903 return 0; 2904 } 2905 2906 static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys, 2907 struct user_msghdr __user *msg, 2908 struct sockaddr __user *uaddr, 2909 unsigned int flags, int nosec) 2910 { 2911 struct compat_msghdr __user *msg_compat = 2912 (struct compat_msghdr __user *) msg; 2913 int __user *uaddr_len = COMPAT_NAMELEN(msg); 2914 struct sockaddr_storage addr; 2915 unsigned long cmsg_ptr; 2916 int len; 2917 ssize_t err; 2918 2919 msg_sys->msg_name = &addr; 2920 cmsg_ptr = (unsigned long)msg_sys->msg_control; 2921 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 2922 2923 /* We assume all kernel code knows the size of sockaddr_storage */ 2924 msg_sys->msg_namelen = 0; 2925 2926 if (sock->file->f_flags & O_NONBLOCK) 2927 flags |= MSG_DONTWAIT; 2928 2929 if (unlikely(nosec)) 2930 err = sock_recvmsg_nosec(sock, msg_sys, flags); 2931 else 2932 err = sock_recvmsg(sock, msg_sys, flags); 2933 2934 if (err < 0) 2935 goto out; 2936 len = err; 2937 2938 if (uaddr != NULL) { 2939 err = move_addr_to_user(&addr, 2940 msg_sys->msg_namelen, uaddr, 2941 uaddr_len); 2942 if (err < 0) 2943 goto out; 2944 } 2945 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT), 2946 COMPAT_FLAGS(msg)); 2947 if (err) 2948 goto out; 2949 if (MSG_CMSG_COMPAT & flags) 2950 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2951 &msg_compat->msg_controllen); 2952 else 2953 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr, 2954 &msg->msg_controllen); 2955 if (err) 2956 goto out; 2957 err = len; 2958 out: 2959 return err; 2960 } 2961 2962 static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg, 2963 struct msghdr *msg_sys, unsigned int flags, int nosec) 2964 { 2965 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 2966 /* user mode address pointers */ 2967 struct sockaddr __user *uaddr; 2968 ssize_t err; 2969 2970 err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov); 2971 if (err < 0) 2972 return err; 2973 2974 err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec); 2975 kfree(iov); 2976 return err; 2977 } 2978 2979 /* 2980 * BSD recvmsg interface 2981 */ 2982 2983 long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg, 2984 struct user_msghdr __user *umsg, 2985 struct sockaddr __user *uaddr, unsigned int flags) 2986 { 2987 return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0); 2988 } 2989 2990 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags, 2991 bool forbid_cmsg_compat) 2992 { 2993 struct msghdr msg_sys; 2994 struct socket *sock; 2995 2996 if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT)) 2997 return -EINVAL; 2998 2999 CLASS(fd, f)(fd); 3000 3001 if (fd_empty(f)) 3002 return -EBADF; 3003 sock = sock_from_file(fd_file(f)); 3004 if (unlikely(!sock)) 3005 return -ENOTSOCK; 3006 3007 return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0); 3008 } 3009 3010 SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg, 3011 unsigned int, flags) 3012 { 3013 return __sys_recvmsg(fd, msg, flags, true); 3014 } 3015 3016 /* 3017 * Linux recvmmsg interface 3018 */ 3019 3020 static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3021 unsigned int vlen, unsigned int flags, 3022 struct timespec64 *timeout) 3023 { 3024 int err = 0, datagrams; 3025 struct socket *sock; 3026 struct mmsghdr __user *entry; 3027 struct compat_mmsghdr __user *compat_entry; 3028 struct msghdr msg_sys; 3029 struct timespec64 end_time; 3030 struct timespec64 timeout64; 3031 3032 if (timeout && 3033 poll_select_set_timeout(&end_time, timeout->tv_sec, 3034 timeout->tv_nsec)) 3035 return -EINVAL; 3036 3037 datagrams = 0; 3038 3039 CLASS(fd, f)(fd); 3040 3041 if (fd_empty(f)) 3042 return -EBADF; 3043 sock = sock_from_file(fd_file(f)); 3044 if (unlikely(!sock)) 3045 return -ENOTSOCK; 3046 3047 if (likely(!(flags & MSG_ERRQUEUE))) { 3048 err = sock_error(sock->sk); 3049 if (err) 3050 return err; 3051 } 3052 3053 entry = mmsg; 3054 compat_entry = (struct compat_mmsghdr __user *)mmsg; 3055 3056 while (datagrams < vlen) { 3057 /* 3058 * No need to ask LSM for more than the first datagram. 3059 */ 3060 if (MSG_CMSG_COMPAT & flags) { 3061 err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry, 3062 &msg_sys, flags & ~MSG_WAITFORONE, 3063 datagrams); 3064 if (err < 0) 3065 break; 3066 err = __put_user(err, &compat_entry->msg_len); 3067 ++compat_entry; 3068 } else { 3069 err = ___sys_recvmsg(sock, 3070 (struct user_msghdr __user *)entry, 3071 &msg_sys, flags & ~MSG_WAITFORONE, 3072 datagrams); 3073 if (err < 0) 3074 break; 3075 err = put_user(err, &entry->msg_len); 3076 ++entry; 3077 } 3078 3079 if (err) 3080 break; 3081 ++datagrams; 3082 3083 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */ 3084 if (flags & MSG_WAITFORONE) 3085 flags |= MSG_DONTWAIT; 3086 3087 if (timeout) { 3088 ktime_get_ts64(&timeout64); 3089 *timeout = timespec64_sub(end_time, timeout64); 3090 if (timeout->tv_sec < 0) { 3091 timeout->tv_sec = timeout->tv_nsec = 0; 3092 break; 3093 } 3094 3095 /* Timeout, return less than vlen datagrams */ 3096 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0) 3097 break; 3098 } 3099 3100 /* Out of band data, return right away */ 3101 if (msg_sys.msg_flags & MSG_OOB) 3102 break; 3103 cond_resched(); 3104 } 3105 3106 if (err == 0) 3107 return datagrams; 3108 3109 if (datagrams == 0) 3110 return err; 3111 3112 /* 3113 * We may return less entries than requested (vlen) if the 3114 * sock is non block and there aren't enough datagrams... 3115 */ 3116 if (err != -EAGAIN) { 3117 /* 3118 * ... or if recvmsg returns an error after we 3119 * received some datagrams, where we record the 3120 * error to return on the next call or if the 3121 * app asks about it using getsockopt(SO_ERROR). 3122 */ 3123 WRITE_ONCE(sock->sk->sk_err, -err); 3124 } 3125 return datagrams; 3126 } 3127 3128 int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, 3129 unsigned int vlen, unsigned int flags, 3130 struct __kernel_timespec __user *timeout, 3131 struct old_timespec32 __user *timeout32) 3132 { 3133 int datagrams; 3134 struct timespec64 timeout_sys; 3135 3136 if (timeout && get_timespec64(&timeout_sys, timeout)) 3137 return -EFAULT; 3138 3139 if (timeout32 && get_old_timespec32(&timeout_sys, timeout32)) 3140 return -EFAULT; 3141 3142 if (!timeout && !timeout32) 3143 return do_recvmmsg(fd, mmsg, vlen, flags, NULL); 3144 3145 datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys); 3146 3147 if (datagrams <= 0) 3148 return datagrams; 3149 3150 if (timeout && put_timespec64(&timeout_sys, timeout)) 3151 datagrams = -EFAULT; 3152 3153 if (timeout32 && put_old_timespec32(&timeout_sys, timeout32)) 3154 datagrams = -EFAULT; 3155 3156 return datagrams; 3157 } 3158 3159 SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg, 3160 unsigned int, vlen, unsigned int, flags, 3161 struct __kernel_timespec __user *, timeout) 3162 { 3163 if (flags & MSG_CMSG_COMPAT) 3164 return -EINVAL; 3165 3166 return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL); 3167 } 3168 3169 #ifdef CONFIG_COMPAT_32BIT_TIME 3170 SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg, 3171 unsigned int, vlen, unsigned int, flags, 3172 struct old_timespec32 __user *, timeout) 3173 { 3174 if (flags & MSG_CMSG_COMPAT) 3175 return -EINVAL; 3176 3177 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout); 3178 } 3179 #endif 3180 3181 #ifdef __ARCH_WANT_SYS_SOCKETCALL 3182 /* Argument list sizes for sys_socketcall */ 3183 #define AL(x) ((x) * sizeof(unsigned long)) 3184 static const unsigned char nargs[21] = { 3185 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), 3186 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), 3187 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), 3188 AL(4), AL(5), AL(4) 3189 }; 3190 3191 #undef AL 3192 3193 /* 3194 * System call vectors. 3195 * 3196 * Argument checking cleaned up. Saved 20% in size. 3197 * This function doesn't need to set the kernel lock because 3198 * it is set by the callees. 3199 */ 3200 3201 SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args) 3202 { 3203 unsigned long a[AUDITSC_ARGS]; 3204 unsigned long a0, a1; 3205 int err; 3206 unsigned int len; 3207 3208 if (call < 1 || call > SYS_SENDMMSG) 3209 return -EINVAL; 3210 call = array_index_nospec(call, SYS_SENDMMSG + 1); 3211 3212 len = nargs[call]; 3213 if (len > sizeof(a)) 3214 return -EINVAL; 3215 3216 /* copy_from_user should be SMP safe. */ 3217 if (copy_from_user(a, args, len)) 3218 return -EFAULT; 3219 3220 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a); 3221 if (err) 3222 return err; 3223 3224 a0 = a[0]; 3225 a1 = a[1]; 3226 3227 switch (call) { 3228 case SYS_SOCKET: 3229 err = __sys_socket(a0, a1, a[2]); 3230 break; 3231 case SYS_BIND: 3232 err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]); 3233 break; 3234 case SYS_CONNECT: 3235 err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 3236 break; 3237 case SYS_LISTEN: 3238 err = __sys_listen(a0, a1); 3239 break; 3240 case SYS_ACCEPT: 3241 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3242 (int __user *)a[2], 0); 3243 break; 3244 case SYS_GETSOCKNAME: 3245 err = 3246 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3247 (int __user *)a[2], 0); 3248 break; 3249 case SYS_GETPEERNAME: 3250 err = 3251 __sys_getsockname(a0, (struct sockaddr __user *)a1, 3252 (int __user *)a[2], 1); 3253 break; 3254 case SYS_SOCKETPAIR: 3255 err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]); 3256 break; 3257 case SYS_SEND: 3258 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3259 NULL, 0); 3260 break; 3261 case SYS_SENDTO: 3262 err = __sys_sendto(a0, (void __user *)a1, a[2], a[3], 3263 (struct sockaddr __user *)a[4], a[5]); 3264 break; 3265 case SYS_RECV: 3266 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3267 NULL, NULL); 3268 break; 3269 case SYS_RECVFROM: 3270 err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 3271 (struct sockaddr __user *)a[4], 3272 (int __user *)a[5]); 3273 break; 3274 case SYS_SHUTDOWN: 3275 err = __sys_shutdown(a0, a1); 3276 break; 3277 case SYS_SETSOCKOPT: 3278 err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3], 3279 a[4]); 3280 break; 3281 case SYS_GETSOCKOPT: 3282 err = 3283 __sys_getsockopt(a0, a1, a[2], (char __user *)a[3], 3284 (int __user *)a[4]); 3285 break; 3286 case SYS_SENDMSG: 3287 err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1, 3288 a[2], true); 3289 break; 3290 case SYS_SENDMMSG: 3291 err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], 3292 a[3], true); 3293 break; 3294 case SYS_RECVMSG: 3295 err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1, 3296 a[2], true); 3297 break; 3298 case SYS_RECVMMSG: 3299 if (IS_ENABLED(CONFIG_64BIT)) 3300 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3301 a[2], a[3], 3302 (struct __kernel_timespec __user *)a[4], 3303 NULL); 3304 else 3305 err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1, 3306 a[2], a[3], NULL, 3307 (struct old_timespec32 __user *)a[4]); 3308 break; 3309 case SYS_ACCEPT4: 3310 err = __sys_accept4(a0, (struct sockaddr __user *)a1, 3311 (int __user *)a[2], a[3]); 3312 break; 3313 default: 3314 err = -EINVAL; 3315 break; 3316 } 3317 return err; 3318 } 3319 3320 #endif /* __ARCH_WANT_SYS_SOCKETCALL */ 3321 3322 /** 3323 * sock_register - add a socket protocol handler 3324 * @ops: description of protocol 3325 * 3326 * This function is called by a protocol handler that wants to 3327 * advertise its address family, and have it linked into the 3328 * socket interface. The value ops->family corresponds to the 3329 * socket system call protocol family. 3330 */ 3331 int sock_register(const struct net_proto_family *ops) 3332 { 3333 int err; 3334 3335 if (ops->family >= NPROTO) { 3336 pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); 3337 return -ENOBUFS; 3338 } 3339 3340 spin_lock(&net_family_lock); 3341 if (rcu_dereference_protected(net_families[ops->family], 3342 lockdep_is_held(&net_family_lock))) 3343 err = -EEXIST; 3344 else { 3345 rcu_assign_pointer(net_families[ops->family], ops); 3346 err = 0; 3347 } 3348 spin_unlock(&net_family_lock); 3349 3350 pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]); 3351 return err; 3352 } 3353 EXPORT_SYMBOL(sock_register); 3354 3355 /** 3356 * sock_unregister - remove a protocol handler 3357 * @family: protocol family to remove 3358 * 3359 * This function is called by a protocol handler that wants to 3360 * remove its address family, and have it unlinked from the 3361 * new socket creation. 3362 * 3363 * If protocol handler is a module, then it can use module reference 3364 * counts to protect against new references. If protocol handler is not 3365 * a module then it needs to provide its own protection in 3366 * the ops->create routine. 3367 */ 3368 void sock_unregister(int family) 3369 { 3370 BUG_ON(family < 0 || family >= NPROTO); 3371 3372 spin_lock(&net_family_lock); 3373 RCU_INIT_POINTER(net_families[family], NULL); 3374 spin_unlock(&net_family_lock); 3375 3376 synchronize_rcu(); 3377 3378 pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]); 3379 } 3380 EXPORT_SYMBOL(sock_unregister); 3381 3382 bool sock_is_registered(int family) 3383 { 3384 return family < NPROTO && rcu_access_pointer(net_families[family]); 3385 } 3386 3387 static int __init sock_init(void) 3388 { 3389 int err; 3390 /* 3391 * Initialize the network sysctl infrastructure. 3392 */ 3393 err = net_sysctl_init(); 3394 if (err) 3395 goto out; 3396 3397 /* 3398 * Initialize skbuff SLAB cache 3399 */ 3400 skb_init(); 3401 3402 /* 3403 * Initialize the protocols module. 3404 */ 3405 3406 init_inodecache(); 3407 3408 err = register_filesystem(&sock_fs_type); 3409 if (err) 3410 goto out; 3411 sock_mnt = kern_mount(&sock_fs_type); 3412 if (IS_ERR(sock_mnt)) { 3413 err = PTR_ERR(sock_mnt); 3414 goto out_mount; 3415 } 3416 3417 /* The real protocol initialization is performed in later initcalls. 3418 */ 3419 3420 #ifdef CONFIG_NETFILTER 3421 err = netfilter_init(); 3422 if (err) 3423 goto out; 3424 #endif 3425 3426 ptp_classifier_init(); 3427 3428 out: 3429 return err; 3430 3431 out_mount: 3432 unregister_filesystem(&sock_fs_type); 3433 goto out; 3434 } 3435 3436 core_initcall(sock_init); /* early initcall */ 3437 3438 #ifdef CONFIG_PROC_FS 3439 void socket_seq_show(struct seq_file *seq) 3440 { 3441 seq_printf(seq, "sockets: used %d\n", 3442 sock_inuse_get(seq->private)); 3443 } 3444 #endif /* CONFIG_PROC_FS */ 3445 3446 /* Handle the fact that while struct ifreq has the same *layout* on 3447 * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data, 3448 * which are handled elsewhere, it still has different *size* due to 3449 * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit, 3450 * resulting in struct ifreq being 32 and 40 bytes respectively). 3451 * As a result, if the struct happens to be at the end of a page and 3452 * the next page isn't readable/writable, we get a fault. To prevent 3453 * that, copy back and forth to the full size. 3454 */ 3455 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg) 3456 { 3457 if (in_compat_syscall()) { 3458 struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr; 3459 3460 memset(ifr, 0, sizeof(*ifr)); 3461 if (copy_from_user(ifr32, arg, sizeof(*ifr32))) 3462 return -EFAULT; 3463 3464 if (ifrdata) 3465 *ifrdata = compat_ptr(ifr32->ifr_data); 3466 3467 return 0; 3468 } 3469 3470 if (copy_from_user(ifr, arg, sizeof(*ifr))) 3471 return -EFAULT; 3472 3473 if (ifrdata) 3474 *ifrdata = ifr->ifr_data; 3475 3476 return 0; 3477 } 3478 EXPORT_SYMBOL(get_user_ifreq); 3479 3480 int put_user_ifreq(struct ifreq *ifr, void __user *arg) 3481 { 3482 size_t size = sizeof(*ifr); 3483 3484 if (in_compat_syscall()) 3485 size = sizeof(struct compat_ifreq); 3486 3487 if (copy_to_user(arg, ifr, size)) 3488 return -EFAULT; 3489 3490 return 0; 3491 } 3492 EXPORT_SYMBOL(put_user_ifreq); 3493 3494 #ifdef CONFIG_COMPAT 3495 static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32) 3496 { 3497 compat_uptr_t uptr32; 3498 struct ifreq ifr; 3499 void __user *saved; 3500 int err; 3501 3502 if (get_user_ifreq(&ifr, NULL, uifr32)) 3503 return -EFAULT; 3504 3505 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu)) 3506 return -EFAULT; 3507 3508 saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc; 3509 ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32); 3510 3511 err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL); 3512 if (!err) { 3513 ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved; 3514 if (put_user_ifreq(&ifr, uifr32)) 3515 err = -EFAULT; 3516 } 3517 return err; 3518 } 3519 3520 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */ 3521 static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd, 3522 struct compat_ifreq __user *u_ifreq32) 3523 { 3524 struct ifreq ifreq; 3525 void __user *data; 3526 3527 if (!is_socket_ioctl_cmd(cmd)) 3528 return -ENOTTY; 3529 if (get_user_ifreq(&ifreq, &data, u_ifreq32)) 3530 return -EFAULT; 3531 ifreq.ifr_data = data; 3532 3533 return dev_ioctl(net, cmd, &ifreq, data, NULL); 3534 } 3535 3536 static int compat_sock_ioctl_trans(struct file *file, struct socket *sock, 3537 unsigned int cmd, unsigned long arg) 3538 { 3539 void __user *argp = compat_ptr(arg); 3540 struct sock *sk = sock->sk; 3541 struct net *net = sock_net(sk); 3542 const struct proto_ops *ops; 3543 3544 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) 3545 return sock_ioctl(file, cmd, (unsigned long)argp); 3546 3547 switch (cmd) { 3548 case SIOCWANDEV: 3549 return compat_siocwandev(net, argp); 3550 case SIOCGSTAMP_OLD: 3551 case SIOCGSTAMPNS_OLD: 3552 ops = READ_ONCE(sock->ops); 3553 if (!ops->gettstamp) 3554 return -ENOIOCTLCMD; 3555 return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD, 3556 !COMPAT_USE_64BIT_TIME); 3557 3558 case SIOCETHTOOL: 3559 case SIOCBONDSLAVEINFOQUERY: 3560 case SIOCBONDINFOQUERY: 3561 case SIOCSHWTSTAMP: 3562 case SIOCGHWTSTAMP: 3563 return compat_ifr_data_ioctl(net, cmd, argp); 3564 3565 case FIOSETOWN: 3566 case SIOCSPGRP: 3567 case FIOGETOWN: 3568 case SIOCGPGRP: 3569 case SIOCBRADDBR: 3570 case SIOCBRDELBR: 3571 case SIOCBRADDIF: 3572 case SIOCBRDELIF: 3573 case SIOCGIFVLAN: 3574 case SIOCSIFVLAN: 3575 case SIOCGSKNS: 3576 case SIOCGSTAMP_NEW: 3577 case SIOCGSTAMPNS_NEW: 3578 case SIOCGIFCONF: 3579 case SIOCSIFBR: 3580 case SIOCGIFBR: 3581 return sock_ioctl(file, cmd, arg); 3582 3583 case SIOCGIFFLAGS: 3584 case SIOCSIFFLAGS: 3585 case SIOCGIFMAP: 3586 case SIOCSIFMAP: 3587 case SIOCGIFMETRIC: 3588 case SIOCSIFMETRIC: 3589 case SIOCGIFMTU: 3590 case SIOCSIFMTU: 3591 case SIOCGIFMEM: 3592 case SIOCSIFMEM: 3593 case SIOCGIFHWADDR: 3594 case SIOCSIFHWADDR: 3595 case SIOCADDMULTI: 3596 case SIOCDELMULTI: 3597 case SIOCGIFINDEX: 3598 case SIOCGIFADDR: 3599 case SIOCSIFADDR: 3600 case SIOCSIFHWBROADCAST: 3601 case SIOCDIFADDR: 3602 case SIOCGIFBRDADDR: 3603 case SIOCSIFBRDADDR: 3604 case SIOCGIFDSTADDR: 3605 case SIOCSIFDSTADDR: 3606 case SIOCGIFNETMASK: 3607 case SIOCSIFNETMASK: 3608 case SIOCSIFPFLAGS: 3609 case SIOCGIFPFLAGS: 3610 case SIOCGIFTXQLEN: 3611 case SIOCSIFTXQLEN: 3612 case SIOCGIFNAME: 3613 case SIOCSIFNAME: 3614 case SIOCGMIIPHY: 3615 case SIOCGMIIREG: 3616 case SIOCSMIIREG: 3617 case SIOCBONDENSLAVE: 3618 case SIOCBONDRELEASE: 3619 case SIOCBONDSETHWADDR: 3620 case SIOCBONDCHANGEACTIVE: 3621 case SIOCSARP: 3622 case SIOCGARP: 3623 case SIOCDARP: 3624 case SIOCOUTQ: 3625 case SIOCOUTQNSD: 3626 case SIOCATMARK: 3627 return sock_do_ioctl(net, sock, cmd, arg); 3628 } 3629 3630 return -ENOIOCTLCMD; 3631 } 3632 3633 static long compat_sock_ioctl(struct file *file, unsigned int cmd, 3634 unsigned long arg) 3635 { 3636 struct socket *sock = file->private_data; 3637 const struct proto_ops *ops = READ_ONCE(sock->ops); 3638 int ret = -ENOIOCTLCMD; 3639 struct sock *sk; 3640 struct net *net; 3641 3642 sk = sock->sk; 3643 net = sock_net(sk); 3644 3645 if (ops->compat_ioctl) 3646 ret = ops->compat_ioctl(sock, cmd, arg); 3647 3648 if (ret == -ENOIOCTLCMD && 3649 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)) 3650 ret = compat_wext_handle_ioctl(net, cmd, arg); 3651 3652 if (ret == -ENOIOCTLCMD) 3653 ret = compat_sock_ioctl_trans(file, sock, cmd, arg); 3654 3655 return ret; 3656 } 3657 #endif 3658 3659 /** 3660 * kernel_bind - bind an address to a socket (kernel space) 3661 * @sock: socket 3662 * @addr: address 3663 * @addrlen: length of address 3664 * 3665 * Returns 0 or an error. 3666 */ 3667 3668 int kernel_bind(struct socket *sock, struct sockaddr_unsized *addr, int addrlen) 3669 { 3670 struct sockaddr_storage address; 3671 3672 memcpy(&address, addr, addrlen); 3673 3674 return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr_unsized *)&address, 3675 addrlen); 3676 } 3677 EXPORT_SYMBOL(kernel_bind); 3678 3679 /** 3680 * kernel_listen - move socket to listening state (kernel space) 3681 * @sock: socket 3682 * @backlog: pending connections queue size 3683 * 3684 * Returns 0 or an error. 3685 */ 3686 3687 int kernel_listen(struct socket *sock, int backlog) 3688 { 3689 return READ_ONCE(sock->ops)->listen(sock, backlog); 3690 } 3691 EXPORT_SYMBOL(kernel_listen); 3692 3693 /** 3694 * kernel_accept - accept a connection (kernel space) 3695 * @sock: listening socket 3696 * @newsock: new connected socket 3697 * @flags: flags 3698 * 3699 * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0. 3700 * If it fails, @newsock is guaranteed to be %NULL. 3701 * Returns 0 or an error. 3702 */ 3703 3704 int kernel_accept(struct socket *sock, struct socket **newsock, int flags) 3705 { 3706 struct sock *sk = sock->sk; 3707 const struct proto_ops *ops = READ_ONCE(sock->ops); 3708 struct proto_accept_arg arg = { 3709 .flags = flags, 3710 .kern = true, 3711 }; 3712 int err; 3713 3714 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol, 3715 newsock); 3716 if (err < 0) 3717 goto done; 3718 3719 err = ops->accept(sock, *newsock, &arg); 3720 if (err < 0) { 3721 sock_release(*newsock); 3722 *newsock = NULL; 3723 goto done; 3724 } 3725 3726 (*newsock)->ops = ops; 3727 __module_get(ops->owner); 3728 3729 done: 3730 return err; 3731 } 3732 EXPORT_SYMBOL(kernel_accept); 3733 3734 /** 3735 * kernel_connect - connect a socket (kernel space) 3736 * @sock: socket 3737 * @addr: address 3738 * @addrlen: address length 3739 * @flags: flags (O_NONBLOCK, ...) 3740 * 3741 * For datagram sockets, @addr is the address to which datagrams are sent 3742 * by default, and the only address from which datagrams are received. 3743 * For stream sockets, attempts to connect to @addr. 3744 * Returns 0 or an error code. 3745 */ 3746 3747 int kernel_connect(struct socket *sock, struct sockaddr_unsized *addr, int addrlen, 3748 int flags) 3749 { 3750 struct sockaddr_storage address; 3751 3752 memcpy(&address, addr, addrlen); 3753 3754 return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)&address, 3755 addrlen, flags); 3756 } 3757 EXPORT_SYMBOL(kernel_connect); 3758 3759 /** 3760 * kernel_getsockname - get the address which the socket is bound (kernel space) 3761 * @sock: socket 3762 * @addr: address holder 3763 * 3764 * Fills the @addr pointer with the address which the socket is bound. 3765 * Returns the length of the address in bytes or an error code. 3766 */ 3767 3768 int kernel_getsockname(struct socket *sock, struct sockaddr *addr) 3769 { 3770 return READ_ONCE(sock->ops)->getname(sock, addr, 0); 3771 } 3772 EXPORT_SYMBOL(kernel_getsockname); 3773 3774 /** 3775 * kernel_getpeername - get the address which the socket is connected (kernel space) 3776 * @sock: socket 3777 * @addr: address holder 3778 * 3779 * Fills the @addr pointer with the address which the socket is connected. 3780 * Returns the length of the address in bytes or an error code. 3781 */ 3782 3783 int kernel_getpeername(struct socket *sock, struct sockaddr *addr) 3784 { 3785 return READ_ONCE(sock->ops)->getname(sock, addr, 1); 3786 } 3787 EXPORT_SYMBOL(kernel_getpeername); 3788 3789 /** 3790 * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space) 3791 * @sock: socket 3792 * @how: connection part 3793 * 3794 * Returns 0 or an error. 3795 */ 3796 3797 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how) 3798 { 3799 return READ_ONCE(sock->ops)->shutdown(sock, how); 3800 } 3801 EXPORT_SYMBOL(kernel_sock_shutdown); 3802 3803 /** 3804 * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket 3805 * @sk: socket 3806 * 3807 * This routine returns the IP overhead imposed by a socket i.e. 3808 * the length of the underlying IP header, depending on whether 3809 * this is an IPv4 or IPv6 socket and the length from IP options turned 3810 * on at the socket. Assumes that the caller has a lock on the socket. 3811 */ 3812 3813 u32 kernel_sock_ip_overhead(struct sock *sk) 3814 { 3815 struct inet_sock *inet; 3816 struct ip_options_rcu *opt; 3817 u32 overhead = 0; 3818 #if IS_ENABLED(CONFIG_IPV6) 3819 struct ipv6_pinfo *np; 3820 struct ipv6_txoptions *optv6 = NULL; 3821 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3822 3823 if (!sk) 3824 return overhead; 3825 3826 switch (sk->sk_family) { 3827 case AF_INET: 3828 inet = inet_sk(sk); 3829 overhead += sizeof(struct iphdr); 3830 opt = rcu_dereference_protected(inet->inet_opt, 3831 sock_owned_by_user(sk)); 3832 if (opt) 3833 overhead += opt->opt.optlen; 3834 return overhead; 3835 #if IS_ENABLED(CONFIG_IPV6) 3836 case AF_INET6: 3837 np = inet6_sk(sk); 3838 overhead += sizeof(struct ipv6hdr); 3839 if (np) 3840 optv6 = rcu_dereference_protected(np->opt, 3841 sock_owned_by_user(sk)); 3842 if (optv6) 3843 overhead += (optv6->opt_flen + optv6->opt_nflen); 3844 return overhead; 3845 #endif /* IS_ENABLED(CONFIG_IPV6) */ 3846 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */ 3847 return overhead; 3848 } 3849 } 3850 EXPORT_SYMBOL(kernel_sock_ip_overhead); 3851