Merge branch 'work.mount0' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[sfrench/cifs-2.6.git] / net / socket.c
index 5061c5d2730c7f7722f9f32b3cef0636bd0743e8..6a9ab7a8b1d2c5059752a9147c2f0fd1aea4f44d 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * NET         An implementation of the SOCKET network access protocol.
  *
  *             Tigran Aivazian :       Made listen(2) backlog sanity checks
  *                                     protocol-independent
  *
- *
- *             This program is free software; you can redistribute it and/or
- *             modify it under the terms of the GNU General Public License
- *             as published by the Free Software Foundation; either version
- *             2 of the License, or (at your option) any later version.
- *
- *
  *     This module is effectively the top level interface to the BSD socket
  *     paradigm.
  *
 #include <net/busy_poll.h>
 #include <linux/errqueue.h>
 
-/* proto_ops for ipv4 and ipv6 use the same {recv,send}msg function */
-#if IS_ENABLED(CONFIG_INET)
-#define INDIRECT_CALL_INET4(f, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
-#else
-#define INDIRECT_CALL_INET4(f, f1, ...) f(__VA_ARGS__)
-#endif
-
 #ifdef CONFIG_NET_RX_BUSY_POLL
 unsigned int sysctl_net_busy_read __read_mostly;
 unsigned int sysctl_net_busy_poll __read_mostly;
@@ -248,20 +235,13 @@ static struct kmem_cache *sock_inode_cachep __ro_after_init;
 static struct inode *sock_alloc_inode(struct super_block *sb)
 {
        struct socket_alloc *ei;
-       struct socket_wq *wq;
 
        ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
        if (!ei)
                return NULL;
-       wq = kmalloc(sizeof(*wq), GFP_KERNEL);
-       if (!wq) {
-               kmem_cache_free(sock_inode_cachep, ei);
-               return NULL;
-       }
-       init_waitqueue_head(&wq->wait);
-       wq->fasync_list = NULL;
-       wq->flags = 0;
-       ei->socket.wq = wq;
+       init_waitqueue_head(&ei->socket.wq.wait);
+       ei->socket.wq.fasync_list = NULL;
+       ei->socket.wq.flags = 0;
 
        ei->socket.state = SS_UNCONNECTED;
        ei->socket.flags = 0;
@@ -272,12 +252,11 @@ static struct inode *sock_alloc_inode(struct super_block *sb)
        return &ei->vfs_inode;
 }
 
-static void sock_destroy_inode(struct inode *inode)
+static void sock_free_inode(struct inode *inode)
 {
        struct socket_alloc *ei;
 
        ei = container_of(inode, struct socket_alloc, vfs_inode);
-       kfree_rcu(ei->socket.wq, rcu);
        kmem_cache_free(sock_inode_cachep, ei);
 }
 
@@ -302,7 +281,7 @@ static void init_inodecache(void)
 
 static const struct super_operations sockfs_ops = {
        .alloc_inode    = sock_alloc_inode,
-       .destroy_inode  = sock_destroy_inode,
+       .free_inode     = sock_free_inode,
        .statfs         = simple_statfs,
 };
 
@@ -439,7 +418,7 @@ static int sock_map_fd(struct socket *sock, int flags)
        }
 
        newfile = sock_alloc_file(sock, flags, NULL);
-       if (likely(!IS_ERR(newfile))) {
+       if (!IS_ERR(newfile)) {
                fd_install(fd, newfile);
                return fd;
        }
@@ -616,7 +595,7 @@ static void __sock_release(struct socket *sock, struct inode *inode)
                module_put(owner);
        }
 
-       if (sock->wq->fasync_list)
+       if (sock->wq.fasync_list)
                pr_err("%s: fasync list not empty!\n", __func__);
 
        if (!sock->file) {
@@ -649,24 +628,27 @@ void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
 }
 EXPORT_SYMBOL(__sock_tx_timestamp);
 
-/**
- *     sock_sendmsg - send a message through @sock
- *     @sock: socket
- *     @msg: message to send
- *
- *     Sends @msg through @sock, passing through LSM.
- *     Returns the number of bytes sent, or an error code.
- */
 INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
                                           size_t));
+INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
+                                           size_t));
 static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
 {
-       int ret = INDIRECT_CALL_INET4(sock->ops->sendmsg, inet_sendmsg, sock,
-                                     msg, msg_data_left(msg));
+       int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
+                                    inet_sendmsg, sock, msg,
+                                    msg_data_left(msg));
        BUG_ON(ret == -EIOCBQUEUED);
        return ret;
 }
 
+/**
+ *     sock_sendmsg - send a message through @sock
+ *     @sock: socket
+ *     @msg: message to send
+ *
+ *     Sends @msg through @sock, passing through LSM.
+ *     Returns the number of bytes sent, or an error code.
+ */
 int sock_sendmsg(struct socket *sock, struct msghdr *msg)
 {
        int err = security_socket_sendmsg(sock, msg,
@@ -879,6 +861,18 @@ void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
 }
 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
 
+INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
+                                          size_t, int));
+INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
+                                           size_t, int));
+static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
+                                    int flags)
+{
+       return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
+                                 inet_recvmsg, sock, msg, msg_data_left(msg),
+                                 flags);
+}
+
 /**
  *     sock_recvmsg - receive a message from @sock
  *     @sock: socket
@@ -888,15 +882,6 @@ EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  *     Receives @msg from @sock, passing through LSM. Returns the total number
  *     of bytes received, or an error.
  */
-INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
-                                          size_t , int ));
-static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
-                                    int flags)
-{
-       return INDIRECT_CALL_INET4(sock->ops->recvmsg, inet_recvmsg, sock, msg,
-                                  msg_data_left(msg), flags);
-}
-
 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
 {
        int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
@@ -1299,13 +1284,12 @@ static int sock_fasync(int fd, struct file *filp, int on)
 {
        struct socket *sock = filp->private_data;
        struct sock *sk = sock->sk;
-       struct socket_wq *wq;
+       struct socket_wq *wq = &sock->wq;
 
        if (sk == NULL)
                return -EINVAL;
 
        lock_sock(sk);
-       wq = sock->wq;
        fasync_helper(fd, filp, on, &wq->fasync_list);
 
        if (!wq->fasync_list)
@@ -2061,6 +2045,8 @@ SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
 static int __sys_setsockopt(int fd, int level, int optname,
                            char __user *optval, int optlen)
 {
+       mm_segment_t oldfs = get_fs();
+       char *kernel_optval = NULL;
        int err, fput_needed;
        struct socket *sock;
 
@@ -2073,6 +2059,22 @@ static int __sys_setsockopt(int fd, int level, int optname,
                if (err)
                        goto out_put;
 
+               err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level,
+                                                    &optname, optval, &optlen,
+                                                    &kernel_optval);
+
+               if (err < 0) {
+                       goto out_put;
+               } else if (err > 0) {
+                       err = 0;
+                       goto out_put;
+               }
+
+               if (kernel_optval) {
+                       set_fs(KERNEL_DS);
+                       optval = (char __user __force *)kernel_optval;
+               }
+
                if (level == SOL_SOCKET)
                        err =
                            sock_setsockopt(sock, level, optname, optval,
@@ -2081,6 +2083,11 @@ static int __sys_setsockopt(int fd, int level, int optname,
                        err =
                            sock->ops->setsockopt(sock, level, optname, optval,
                                                  optlen);
+
+               if (kernel_optval) {
+                       set_fs(oldfs);
+                       kfree(kernel_optval);
+               }
 out_put:
                fput_light(sock->file, fput_needed);
        }
@@ -2103,6 +2110,7 @@ static int __sys_getsockopt(int fd, int level, int optname,
 {
        int err, fput_needed;
        struct socket *sock;
+       int max_optlen;
 
        sock = sockfd_lookup_light(fd, &err, &fput_needed);
        if (sock != NULL) {
@@ -2110,6 +2118,8 @@ static int __sys_getsockopt(int fd, int level, int optname,
                if (err)
                        goto out_put;
 
+               max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
+
                if (level == SOL_SOCKET)
                        err =
                            sock_getsockopt(sock, level, optname, optval,
@@ -2118,6 +2128,10 @@ static int __sys_getsockopt(int fd, int level, int optname,
                        err =
                            sock->ops->getsockopt(sock, level, optname, optval,
                                                  optlen);
+
+               err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
+                                                    optval, optlen,
+                                                    max_optlen, err);
 out_put:
                fput_light(sock->file, fput_needed);
        }
@@ -2212,9 +2226,10 @@ static int copy_msghdr_from_user(struct msghdr *kmsg,
 
        kmsg->msg_iocb = NULL;
 
-       return import_iovec(save_addr ? READ : WRITE,
+       err = import_iovec(save_addr ? READ : WRITE,
                            msg.msg_iov, msg.msg_iovlen,
                            UIO_FASTIOV, iov, &kmsg->msg_iter);
+       return err < 0 ? err : 0;
 }
 
 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
@@ -2316,6 +2331,13 @@ out_freeiov:
 /*
  *     BSD sendmsg interface
  */
+long __sys_sendmsg_sock(struct socket *sock, struct user_msghdr __user *msg,
+                       unsigned int flags)
+{
+       struct msghdr msg_sys;
+
+       return ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
+}
 
 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
                   bool forbid_cmsg_compat)
@@ -2490,6 +2512,14 @@ out_freeiov:
  *     BSD recvmsg interface
  */
 
+long __sys_recvmsg_sock(struct socket *sock, struct user_msghdr __user *msg,
+                       unsigned int flags)
+{
+       struct msghdr msg_sys;
+
+       return ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
+}
+
 long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
                   bool forbid_cmsg_compat)
 {