aboutsummaryrefslogtreecommitdiffstats
path: root/net/unix/af_unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/unix/af_unix.c')
-rw-r--r--net/unix/af_unix.c267
1 files changed, 237 insertions, 30 deletions
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 06430598cf51..03ee4d359f6a 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -140,12 +140,17 @@ static struct hlist_head *unix_sockets_unbound(void *addr)
140#ifdef CONFIG_SECURITY_NETWORK 140#ifdef CONFIG_SECURITY_NETWORK
141static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) 141static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
142{ 142{
143 memcpy(UNIXSID(skb), &scm->secid, sizeof(u32)); 143 UNIXCB(skb).secid = scm->secid;
144} 144}
145 145
146static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) 146static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
147{ 147{
148 scm->secid = *UNIXSID(skb); 148 scm->secid = UNIXCB(skb).secid;
149}
150
151static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
152{
153 return (scm->secid == UNIXCB(skb).secid);
149} 154}
150#else 155#else
151static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) 156static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
@@ -153,6 +158,11 @@ static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb)
153 158
154static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) 159static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb)
155{ } 160{ }
161
162static inline bool unix_secdata_eq(struct scm_cookie *scm, struct sk_buff *skb)
163{
164 return true;
165}
156#endif /* CONFIG_SECURITY_NETWORK */ 166#endif /* CONFIG_SECURITY_NETWORK */
157 167
158/* 168/*
@@ -518,6 +528,11 @@ static int unix_ioctl(struct socket *, unsigned int, unsigned long);
518static int unix_shutdown(struct socket *, int); 528static int unix_shutdown(struct socket *, int);
519static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t); 529static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
520static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int); 530static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
531static ssize_t unix_stream_sendpage(struct socket *, struct page *, int offset,
532 size_t size, int flags);
533static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos,
534 struct pipe_inode_info *, size_t size,
535 unsigned int flags);
521static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t); 536static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
522static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int); 537static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
523static int unix_dgram_connect(struct socket *, struct sockaddr *, 538static int unix_dgram_connect(struct socket *, struct sockaddr *,
@@ -558,7 +573,8 @@ static const struct proto_ops unix_stream_ops = {
558 .sendmsg = unix_stream_sendmsg, 573 .sendmsg = unix_stream_sendmsg,
559 .recvmsg = unix_stream_recvmsg, 574 .recvmsg = unix_stream_recvmsg,
560 .mmap = sock_no_mmap, 575 .mmap = sock_no_mmap,
561 .sendpage = sock_no_sendpage, 576 .sendpage = unix_stream_sendpage,
577 .splice_read = unix_stream_splice_read,
562 .set_peek_off = unix_set_peek_off, 578 .set_peek_off = unix_set_peek_off,
563}; 579};
564 580
@@ -620,7 +636,7 @@ static struct proto unix_proto = {
620 */ 636 */
621static struct lock_class_key af_unix_sk_receive_queue_lock_key; 637static struct lock_class_key af_unix_sk_receive_queue_lock_key;
622 638
623static struct sock *unix_create1(struct net *net, struct socket *sock) 639static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
624{ 640{
625 struct sock *sk = NULL; 641 struct sock *sk = NULL;
626 struct unix_sock *u; 642 struct unix_sock *u;
@@ -629,7 +645,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock)
629 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) 645 if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files())
630 goto out; 646 goto out;
631 647
632 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto); 648 sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto, kern);
633 if (!sk) 649 if (!sk)
634 goto out; 650 goto out;
635 651
@@ -688,7 +704,7 @@ static int unix_create(struct net *net, struct socket *sock, int protocol,
688 return -ESOCKTNOSUPPORT; 704 return -ESOCKTNOSUPPORT;
689 } 705 }
690 706
691 return unix_create1(net, sock) ? 0 : -ENOMEM; 707 return unix_create1(net, sock, kern) ? 0 : -ENOMEM;
692} 708}
693 709
694static int unix_release(struct socket *sock) 710static int unix_release(struct socket *sock)
@@ -1088,7 +1104,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
1088 err = -ENOMEM; 1104 err = -ENOMEM;
1089 1105
1090 /* create new sock for complete connection */ 1106 /* create new sock for complete connection */
1091 newsk = unix_create1(sock_net(sk), NULL); 1107 newsk = unix_create1(sock_net(sk), NULL, 0);
1092 if (newsk == NULL) 1108 if (newsk == NULL)
1093 goto out; 1109 goto out;
1094 1110
@@ -1408,6 +1424,7 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
1408 UNIXCB(skb).uid = scm->creds.uid; 1424 UNIXCB(skb).uid = scm->creds.uid;
1409 UNIXCB(skb).gid = scm->creds.gid; 1425 UNIXCB(skb).gid = scm->creds.gid;
1410 UNIXCB(skb).fp = NULL; 1426 UNIXCB(skb).fp = NULL;
1427 unix_get_secdata(scm, skb);
1411 if (scm->fp && send_fds) 1428 if (scm->fp && send_fds)
1412 err = unix_attach_fds(scm, skb); 1429 err = unix_attach_fds(scm, skb);
1413 1430
@@ -1503,7 +1520,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1503 if (err < 0) 1520 if (err < 0)
1504 goto out_free; 1521 goto out_free;
1505 max_level = err + 1; 1522 max_level = err + 1;
1506 unix_get_secdata(&scm, skb);
1507 1523
1508 skb_put(skb, len - data_len); 1524 skb_put(skb, len - data_len);
1509 skb->data_len = data_len; 1525 skb->data_len = data_len;
@@ -1720,6 +1736,101 @@ out_err:
1720 return sent ? : err; 1736 return sent ? : err;
1721} 1737}
1722 1738
1739static ssize_t unix_stream_sendpage(struct socket *socket, struct page *page,
1740 int offset, size_t size, int flags)
1741{
1742 int err = 0;
1743 bool send_sigpipe = true;
1744 struct sock *other, *sk = socket->sk;
1745 struct sk_buff *skb, *newskb = NULL, *tail = NULL;
1746
1747 if (flags & MSG_OOB)
1748 return -EOPNOTSUPP;
1749
1750 other = unix_peer(sk);
1751 if (!other || sk->sk_state != TCP_ESTABLISHED)
1752 return -ENOTCONN;
1753
1754 if (false) {
1755alloc_skb:
1756 unix_state_unlock(other);
1757 mutex_unlock(&unix_sk(other)->readlock);
1758 newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
1759 &err, 0);
1760 if (!newskb)
1761 return err;
1762 }
1763
1764 /* we must acquire readlock as we modify already present
1765 * skbs in the sk_receive_queue and mess with skb->len
1766 */
1767 err = mutex_lock_interruptible(&unix_sk(other)->readlock);
1768 if (err) {
1769 err = flags & MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS;
1770 send_sigpipe = false;
1771 goto err;
1772 }
1773
1774 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1775 err = -EPIPE;
1776 goto err_unlock;
1777 }
1778
1779 unix_state_lock(other);
1780
1781 if (sock_flag(other, SOCK_DEAD) ||
1782 other->sk_shutdown & RCV_SHUTDOWN) {
1783 err = -EPIPE;
1784 goto err_state_unlock;
1785 }
1786
1787 skb = skb_peek_tail(&other->sk_receive_queue);
1788 if (tail && tail == skb) {
1789 skb = newskb;
1790 } else if (!skb) {
1791 if (newskb)
1792 skb = newskb;
1793 else
1794 goto alloc_skb;
1795 } else if (newskb) {
1796 /* this is fast path, we don't necessarily need to
1797 * call to kfree_skb even though with newskb == NULL
1798 * this - does no harm
1799 */
1800 consume_skb(newskb);
1801 }
1802
1803 if (skb_append_pagefrags(skb, page, offset, size)) {
1804 tail = skb;
1805 goto alloc_skb;
1806 }
1807
1808 skb->len += size;
1809 skb->data_len += size;
1810 skb->truesize += size;
1811 atomic_add(size, &sk->sk_wmem_alloc);
1812
1813 if (newskb)
1814 __skb_queue_tail(&other->sk_receive_queue, newskb);
1815
1816 unix_state_unlock(other);
1817 mutex_unlock(&unix_sk(other)->readlock);
1818
1819 other->sk_data_ready(other);
1820
1821 return size;
1822
1823err_state_unlock:
1824 unix_state_unlock(other);
1825err_unlock:
1826 mutex_unlock(&unix_sk(other)->readlock);
1827err:
1828 kfree_skb(newskb);
1829 if (send_sigpipe && !(flags & MSG_NOSIGNAL))
1830 send_sig(SIGPIPE, current, 0);
1831 return err;
1832}
1833
1723static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg, 1834static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
1724 size_t len) 1835 size_t len)
1725{ 1836{
@@ -1860,8 +1971,9 @@ out:
1860 * Sleep until more data has arrived. But check for races.. 1971 * Sleep until more data has arrived. But check for races..
1861 */ 1972 */
1862static long unix_stream_data_wait(struct sock *sk, long timeo, 1973static long unix_stream_data_wait(struct sock *sk, long timeo,
1863 struct sk_buff *last) 1974 struct sk_buff *last, unsigned int last_len)
1864{ 1975{
1976 struct sk_buff *tail;
1865 DEFINE_WAIT(wait); 1977 DEFINE_WAIT(wait);
1866 1978
1867 unix_state_lock(sk); 1979 unix_state_lock(sk);
@@ -1869,7 +1981,9 @@ static long unix_stream_data_wait(struct sock *sk, long timeo,
1869 for (;;) { 1981 for (;;) {
1870 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1982 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1871 1983
1872 if (skb_peek_tail(&sk->sk_receive_queue) != last || 1984 tail = skb_peek_tail(&sk->sk_receive_queue);
1985 if (tail != last ||
1986 (tail && tail->len != last_len) ||
1873 sk->sk_err || 1987 sk->sk_err ||
1874 (sk->sk_shutdown & RCV_SHUTDOWN) || 1988 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1875 signal_pending(current) || 1989 signal_pending(current) ||
@@ -1897,38 +2011,50 @@ static unsigned int unix_skb_len(const struct sk_buff *skb)
1897 return skb->len - UNIXCB(skb).consumed; 2011 return skb->len - UNIXCB(skb).consumed;
1898} 2012}
1899 2013
1900static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg, 2014struct unix_stream_read_state {
1901 size_t size, int flags) 2015 int (*recv_actor)(struct sk_buff *, int, int,
2016 struct unix_stream_read_state *);
2017 struct socket *socket;
2018 struct msghdr *msg;
2019 struct pipe_inode_info *pipe;
2020 size_t size;
2021 int flags;
2022 unsigned int splice_flags;
2023};
2024
2025static int unix_stream_read_generic(struct unix_stream_read_state *state)
1902{ 2026{
1903 struct scm_cookie scm; 2027 struct scm_cookie scm;
2028 struct socket *sock = state->socket;
1904 struct sock *sk = sock->sk; 2029 struct sock *sk = sock->sk;
1905 struct unix_sock *u = unix_sk(sk); 2030 struct unix_sock *u = unix_sk(sk);
1906 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, msg->msg_name);
1907 int copied = 0; 2031 int copied = 0;
2032 int flags = state->flags;
1908 int noblock = flags & MSG_DONTWAIT; 2033 int noblock = flags & MSG_DONTWAIT;
1909 int check_creds = 0; 2034 bool check_creds = false;
1910 int target; 2035 int target;
1911 int err = 0; 2036 int err = 0;
1912 long timeo; 2037 long timeo;
1913 int skip; 2038 int skip;
2039 size_t size = state->size;
2040 unsigned int last_len;
1914 2041
1915 err = -EINVAL; 2042 err = -EINVAL;
1916 if (sk->sk_state != TCP_ESTABLISHED) 2043 if (sk->sk_state != TCP_ESTABLISHED)
1917 goto out; 2044 goto out;
1918 2045
1919 err = -EOPNOTSUPP; 2046 err = -EOPNOTSUPP;
1920 if (flags&MSG_OOB) 2047 if (flags & MSG_OOB)
1921 goto out; 2048 goto out;
1922 2049
1923 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); 2050 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
1924 timeo = sock_rcvtimeo(sk, noblock); 2051 timeo = sock_rcvtimeo(sk, noblock);
1925 2052
2053 memset(&scm, 0, sizeof(scm));
2054
1926 /* Lock the socket to prevent queue disordering 2055 /* Lock the socket to prevent queue disordering
1927 * while sleeps in memcpy_tomsg 2056 * while sleeps in memcpy_tomsg
1928 */ 2057 */
1929
1930 memset(&scm, 0, sizeof(scm));
1931
1932 err = mutex_lock_interruptible(&u->readlock); 2058 err = mutex_lock_interruptible(&u->readlock);
1933 if (unlikely(err)) { 2059 if (unlikely(err)) {
1934 /* recvmsg() in non blocking mode is supposed to return -EAGAIN 2060 /* recvmsg() in non blocking mode is supposed to return -EAGAIN
@@ -1948,6 +2074,7 @@ static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
1948 goto unlock; 2074 goto unlock;
1949 } 2075 }
1950 last = skb = skb_peek(&sk->sk_receive_queue); 2076 last = skb = skb_peek(&sk->sk_receive_queue);
2077 last_len = last ? last->len : 0;
1951again: 2078again:
1952 if (skb == NULL) { 2079 if (skb == NULL) {
1953 unix_sk(sk)->recursion_level = 0; 2080 unix_sk(sk)->recursion_level = 0;
@@ -1970,16 +2097,17 @@ again:
1970 break; 2097 break;
1971 mutex_unlock(&u->readlock); 2098 mutex_unlock(&u->readlock);
1972 2099
1973 timeo = unix_stream_data_wait(sk, timeo, last); 2100 timeo = unix_stream_data_wait(sk, timeo, last,
2101 last_len);
1974 2102
1975 if (signal_pending(current) 2103 if (signal_pending(current) ||
1976 || mutex_lock_interruptible(&u->readlock)) { 2104 mutex_lock_interruptible(&u->readlock)) {
1977 err = sock_intr_errno(timeo); 2105 err = sock_intr_errno(timeo);
1978 goto out; 2106 goto out;
1979 } 2107 }
1980 2108
1981 continue; 2109 continue;
1982 unlock: 2110unlock:
1983 unix_state_unlock(sk); 2111 unix_state_unlock(sk);
1984 break; 2112 break;
1985 } 2113 }
@@ -1988,6 +2116,7 @@ again:
1988 while (skip >= unix_skb_len(skb)) { 2116 while (skip >= unix_skb_len(skb)) {
1989 skip -= unix_skb_len(skb); 2117 skip -= unix_skb_len(skb);
1990 last = skb; 2118 last = skb;
2119 last_len = skb->len;
1991 skb = skb_peek_next(skb, &sk->sk_receive_queue); 2120 skb = skb_peek_next(skb, &sk->sk_receive_queue);
1992 if (!skb) 2121 if (!skb)
1993 goto again; 2122 goto again;
@@ -1999,23 +2128,27 @@ again:
1999 /* Never glue messages from different writers */ 2128 /* Never glue messages from different writers */
2000 if ((UNIXCB(skb).pid != scm.pid) || 2129 if ((UNIXCB(skb).pid != scm.pid) ||
2001 !uid_eq(UNIXCB(skb).uid, scm.creds.uid) || 2130 !uid_eq(UNIXCB(skb).uid, scm.creds.uid) ||
2002 !gid_eq(UNIXCB(skb).gid, scm.creds.gid)) 2131 !gid_eq(UNIXCB(skb).gid, scm.creds.gid) ||
2132 !unix_secdata_eq(&scm, skb))
2003 break; 2133 break;
2004 } else if (test_bit(SOCK_PASSCRED, &sock->flags)) { 2134 } else if (test_bit(SOCK_PASSCRED, &sock->flags)) {
2005 /* Copy credentials */ 2135 /* Copy credentials */
2006 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid); 2136 scm_set_cred(&scm, UNIXCB(skb).pid, UNIXCB(skb).uid, UNIXCB(skb).gid);
2007 check_creds = 1; 2137 unix_set_secdata(&scm, skb);
2138 check_creds = true;
2008 } 2139 }
2009 2140
2010 /* Copy address just once */ 2141 /* Copy address just once */
2011 if (sunaddr) { 2142 if (state->msg && state->msg->msg_name) {
2012 unix_copy_addr(msg, skb->sk); 2143 DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr,
2144 state->msg->msg_name);
2145 unix_copy_addr(state->msg, skb->sk);
2013 sunaddr = NULL; 2146 sunaddr = NULL;
2014 } 2147 }
2015 2148
2016 chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size); 2149 chunk = min_t(unsigned int, unix_skb_len(skb) - skip, size);
2017 if (skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip, 2150 chunk = state->recv_actor(skb, skip, chunk, state);
2018 msg, chunk)) { 2151 if (chunk < 0) {
2019 if (copied == 0) 2152 if (copied == 0)
2020 copied = -EFAULT; 2153 copied = -EFAULT;
2021 break; 2154 break;
@@ -2053,11 +2186,85 @@ again:
2053 } while (size); 2186 } while (size);
2054 2187
2055 mutex_unlock(&u->readlock); 2188 mutex_unlock(&u->readlock);
2056 scm_recv(sock, msg, &scm, flags); 2189 if (state->msg)
2190 scm_recv(sock, state->msg, &scm, flags);
2191 else
2192 scm_destroy(&scm);
2057out: 2193out:
2058 return copied ? : err; 2194 return copied ? : err;
2059} 2195}
2060 2196
2197static int unix_stream_read_actor(struct sk_buff *skb,
2198 int skip, int chunk,
2199 struct unix_stream_read_state *state)
2200{
2201 int ret;
2202
2203 ret = skb_copy_datagram_msg(skb, UNIXCB(skb).consumed + skip,
2204 state->msg, chunk);
2205 return ret ?: chunk;
2206}
2207
2208static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
2209 size_t size, int flags)
2210{
2211 struct unix_stream_read_state state = {
2212 .recv_actor = unix_stream_read_actor,
2213 .socket = sock,
2214 .msg = msg,
2215 .size = size,
2216 .flags = flags
2217 };
2218
2219 return unix_stream_read_generic(&state);
2220}
2221
2222static ssize_t skb_unix_socket_splice(struct sock *sk,
2223 struct pipe_inode_info *pipe,
2224 struct splice_pipe_desc *spd)
2225{
2226 int ret;
2227 struct unix_sock *u = unix_sk(sk);
2228
2229 mutex_unlock(&u->readlock);
2230 ret = splice_to_pipe(pipe, spd);
2231 mutex_lock(&u->readlock);
2232
2233 return ret;
2234}
2235
2236static int unix_stream_splice_actor(struct sk_buff *skb,
2237 int skip, int chunk,
2238 struct unix_stream_read_state *state)
2239{
2240 return skb_splice_bits(skb, state->socket->sk,
2241 UNIXCB(skb).consumed + skip,
2242 state->pipe, chunk, state->splice_flags,
2243 skb_unix_socket_splice);
2244}
2245
2246static ssize_t unix_stream_splice_read(struct socket *sock, loff_t *ppos,
2247 struct pipe_inode_info *pipe,
2248 size_t size, unsigned int flags)
2249{
2250 struct unix_stream_read_state state = {
2251 .recv_actor = unix_stream_splice_actor,
2252 .socket = sock,
2253 .pipe = pipe,
2254 .size = size,
2255 .splice_flags = flags,
2256 };
2257
2258 if (unlikely(*ppos))
2259 return -ESPIPE;
2260
2261 if (sock->file->f_flags & O_NONBLOCK ||
2262 flags & SPLICE_F_NONBLOCK)
2263 state.flags = MSG_DONTWAIT;
2264
2265 return unix_stream_read_generic(&state);
2266}
2267
2061static int unix_shutdown(struct socket *sock, int mode) 2268static int unix_shutdown(struct socket *sock, int mode)
2062{ 2269{
2063 struct sock *sk = sock->sk; 2270 struct sock *sk = sock->sk;