nfsd: simplify fh_verify access checks
[safe/jmp/linux-2.6] / net / socket.c
index fdd72c5..7565536 100644 (file)
@@ -86,6 +86,7 @@
 #include <linux/audit.h>
 #include <linux/wireless.h>
 #include <linux/nsproxy.h>
+#include <linux/magic.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -235,8 +236,6 @@ int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
        return __put_user(klen, ulen);
 }
 
-#define SOCKFS_MAGIC 0x534F434B
-
 static struct kmem_cache *sock_inode_cachep __read_mostly;
 
 static struct inode *sock_alloc_inode(struct super_block *sb)
@@ -285,7 +284,7 @@ static int init_inodecache(void)
        return 0;
 }
 
-static struct super_operations sockfs_ops = {
+static const struct super_operations sockfs_ops = {
        .alloc_inode =  sock_alloc_inode,
        .destroy_inode =sock_destroy_inode,
        .statfs =       simple_statfs,
@@ -328,7 +327,7 @@ static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
                                dentry->d_inode->i_ino);
 }
 
-static struct dentry_operations sockfs_dentry_operations = {
+static const struct dentry_operations sockfs_dentry_operations = {
        .d_delete = sockfs_delete_dentry,
        .d_dname  = sockfs_dname,
 };
@@ -489,12 +488,12 @@ static struct socket *sock_alloc(void)
 
        sock = SOCKET_I(inode);
 
+       kmemcheck_annotate_bitfield(sock, type);
        inode->i_mode = S_IFSOCK | S_IRWXUGO;
        inode->i_uid = current_fsuid();
        inode->i_gid = current_fsgid();
 
-       get_cpu_var(sockets_in_use)++;
-       put_cpu_var(sockets_in_use);
+       percpu_add(sockets_in_use, 1);
        return sock;
 }
 
@@ -536,8 +535,7 @@ void sock_release(struct socket *sock)
        if (sock->fasync_list)
                printk(KERN_ERR "sock_release: fasync list not empty!\n");
 
-       get_cpu_var(sockets_in_use)--;
-       put_cpu_var(sockets_in_use);
+       percpu_sub(sockets_in_use, 1);
        if (!sock->file) {
                iput(SOCK_INODE(sock));
                return;
@@ -545,6 +543,18 @@ void sock_release(struct socket *sock)
        sock->file = NULL;
 }
 
+int sock_tx_timestamp(struct msghdr *msg, struct sock *sk,
+                     union skb_shared_tx *shtx)
+{
+       shtx->flags = 0;
+       if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
+               shtx->hardware = 1;
+       if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
+               shtx->software = 1;
+       return 0;
+}
+EXPORT_SYMBOL(sock_tx_timestamp);
+
 static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
                                 struct msghdr *msg, size_t size)
 {
@@ -595,33 +605,65 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
        return result;
 }
 
+static int ktime2ts(ktime_t kt, struct timespec *ts)
+{
+       if (kt.tv64) {
+               *ts = ktime_to_timespec(kt);
+               return 1;
+       } else {
+               return 0;
+       }
+}
+
 /*
  * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  */
 void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
        struct sk_buff *skb)
 {
-       ktime_t kt = skb->tstamp;
-
-       if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
-               struct timeval tv;
-               /* Race occurred between timestamp enabling and packet
-                  receiving.  Fill in the current time for now. */
-               if (kt.tv64 == 0)
-                       kt = ktime_get_real();
-               skb->tstamp = kt;
-               tv = ktime_to_timeval(kt);
-               put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
-       } else {
-               struct timespec ts;
-               /* Race occurred between timestamp enabling and packet
-                  receiving.  Fill in the current time for now. */
-               if (kt.tv64 == 0)
-                       kt = ktime_get_real();
-               skb->tstamp = kt;
-               ts = ktime_to_timespec(kt);
-               put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
+       int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
+       struct timespec ts[3];
+       int empty = 1;
+       struct skb_shared_hwtstamps *shhwtstamps =
+               skb_hwtstamps(skb);
+
+       /* Race occurred between timestamp enabling and packet
+          receiving.  Fill in the current time for now. */
+       if (need_software_tstamp && skb->tstamp.tv64 == 0)
+               __net_timestamp(skb);
+
+       if (need_software_tstamp) {
+               if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
+                       struct timeval tv;
+                       skb_get_timestamp(skb, &tv);
+                       put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
+                                sizeof(tv), &tv);
+               } else {
+                       struct timespec ts;
+                       skb_get_timestampns(skb, &ts);
+                       put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
+                                sizeof(ts), &ts);
+               }
+       }
+
+
+       memset(ts, 0, sizeof(ts));
+       if (skb->tstamp.tv64 &&
+           sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
+               skb_get_timestampns(skb, ts + 0);
+               empty = 0;
+       }
+       if (shhwtstamps) {
+               if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
+                   ktime2ts(shhwtstamps->syststamp, ts + 1))
+                       empty = 0;
+               if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
+                   ktime2ts(shhwtstamps->hwtstamp, ts + 2))
+                       empty = 0;
        }
+       if (!empty)
+               put_cmsg(msg, SOL_SOCKET,
+                        SCM_TIMESTAMPING, sizeof(ts), &ts);
 }
 
 EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
@@ -694,7 +736,7 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
        if (more)
                flags |= MSG_MORE;
 
-       return sock->ops->sendpage(sock, page, offset, size, flags);
+       return kernel_sendpage(sock, page, offset, size, flags);
 }
 
 static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
@@ -1030,6 +1072,13 @@ static int sock_fasync(int fd, struct file *filp, int on)
 
        lock_sock(sk);
 
+       spin_lock(&filp->f_lock);
+       if (on)
+               filp->f_flags |= FASYNC;
+       else
+               filp->f_flags &= ~FASYNC;
+       spin_unlock(&filp->f_lock);
+
        prev = &(sock->fasync_list);
 
        for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
@@ -1214,7 +1263,7 @@ int sock_create_kern(int family, int type, int protocol, struct socket **res)
        return __sock_create(&init_net, family, type, protocol, res, 1);
 }
 
-asmlinkage long sys_socket(int family, int type, int protocol)
+SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
 {
        int retval;
        struct socket *sock;
@@ -1255,8 +1304,8 @@ out_release:
  *     Create a pair of connected sockets.
  */
 
-asmlinkage long sys_socketpair(int family, int type, int protocol,
-                              int __user *usockvec)
+SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
+               int __user *, usockvec)
 {
        struct socket *sock1, *sock2;
        int fd1, fd2, err;
@@ -1385,7 +1434,7 @@ SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  *     ready for listening.
  */
 
-asmlinkage long sys_listen(int fd, int backlog)
+SYSCALL_DEFINE2(listen, int, fd, int, backlog)
 {
        struct socket *sock;
        int err, fput_needed;
@@ -1485,8 +1534,6 @@ SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
        fd_install(newfd, newfile);
        err = newfd;
 
-       security_socket_post_accept(sock, newsock);
-
 out_put:
        fput_light(sock->file, fput_needed);
 out:
@@ -1615,9 +1662,9 @@ SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  *     the protocol.
  */
 
-asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
-                          unsigned flags, struct sockaddr __user *addr,
-                          int addr_len)
+SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
+               unsigned, flags, struct sockaddr __user *, addr,
+               int, addr_len)
 {
        struct socket *sock;
        struct sockaddr_storage address;
@@ -1660,7 +1707,8 @@ out:
  *     Send a datagram down a socket.
  */
 
-asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
+SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
+               unsigned, flags)
 {
        return sys_sendto(fd, buff, len, flags, NULL, 0);
 }
@@ -1671,9 +1719,9 @@ asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  *     sender address from kernel to user space.
  */
 
-asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
-                            unsigned flags, struct sockaddr __user *addr,
-                            int __user *addr_len)
+SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
+               unsigned, flags, struct sockaddr __user *, addr,
+               int __user *, addr_len)
 {
        struct socket *sock;
        struct iovec iov;
@@ -1815,7 +1863,7 @@ SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  *     BSD sendmsg interface
  */
 
-asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
+SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned, flags)
 {
        struct compat_msghdr __user *msg_compat =
            (struct compat_msghdr __user *)msg;
@@ -1921,8 +1969,8 @@ out:
  *     BSD recvmsg interface
  */
 
-asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
-                           unsigned int flags)
+SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
+               unsigned int, flags)
 {
        struct compat_msghdr __user *msg_compat =
            (struct compat_msghdr __user *)msg;
@@ -2045,17 +2093,22 @@ static const unsigned char nargs[19]={
  *  it is set by the callees.
  */
 
-asmlinkage long sys_socketcall(int call, unsigned long __user *args)
+SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
 {
        unsigned long a[6];
        unsigned long a0, a1;
        int err;
+       unsigned int len;
 
        if (call < 1 || call > SYS_ACCEPT4)
                return -EINVAL;
 
+       len = nargs[call];
+       if (len > sizeof(a))
+               return -EINVAL;
+
        /* copy_from_user should be SMP safe. */
-       if (copy_from_user(a, args, nargs[call]))
+       if (copy_from_user(a, args, len))
                return -EFAULT;
 
        audit_socketcall(nargs[call] / sizeof(unsigned long), a);
@@ -2338,7 +2391,7 @@ int kernel_getsockopt(struct socket *sock, int level, int optname,
 }
 
 int kernel_setsockopt(struct socket *sock, int level, int optname,
-                       char *optval, int optlen)
+                       char *optval, unsigned int optlen)
 {
        mm_segment_t oldfs = get_fs();
        int err;