diff options
Diffstat (limited to 'net/unix/af_unix.c')
-rw-r--r-- | net/unix/af_unix.c | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 0b39b2451ea..2268e679812 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c | |||
@@ -117,7 +117,7 @@ | |||
117 | 117 | ||
118 | static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1]; | 118 | static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1]; |
119 | static DEFINE_SPINLOCK(unix_table_lock); | 119 | static DEFINE_SPINLOCK(unix_table_lock); |
120 | static atomic_t unix_nr_socks = ATOMIC_INIT(0); | 120 | static atomic_long_t unix_nr_socks; |
121 | 121 | ||
122 | #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE]) | 122 | #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE]) |
123 | 123 | ||
@@ -360,13 +360,13 @@ static void unix_sock_destructor(struct sock *sk) | |||
360 | if (u->addr) | 360 | if (u->addr) |
361 | unix_release_addr(u->addr); | 361 | unix_release_addr(u->addr); |
362 | 362 | ||
363 | atomic_dec(&unix_nr_socks); | 363 | atomic_long_dec(&unix_nr_socks); |
364 | local_bh_disable(); | 364 | local_bh_disable(); |
365 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 365 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
366 | local_bh_enable(); | 366 | local_bh_enable(); |
367 | #ifdef UNIX_REFCNT_DEBUG | 367 | #ifdef UNIX_REFCNT_DEBUG |
368 | printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, | 368 | printk(KERN_DEBUG "UNIX %p is destroyed, %ld are still alive.\n", sk, |
369 | atomic_read(&unix_nr_socks)); | 369 | atomic_long_read(&unix_nr_socks)); |
370 | #endif | 370 | #endif |
371 | } | 371 | } |
372 | 372 | ||
@@ -606,8 +606,8 @@ static struct sock *unix_create1(struct net *net, struct socket *sock) | |||
606 | struct sock *sk = NULL; | 606 | struct sock *sk = NULL; |
607 | struct unix_sock *u; | 607 | struct unix_sock *u; |
608 | 608 | ||
609 | atomic_inc(&unix_nr_socks); | 609 | atomic_long_inc(&unix_nr_socks); |
610 | if (atomic_read(&unix_nr_socks) > 2 * get_max_files()) | 610 | if (atomic_long_read(&unix_nr_socks) > 2 * get_max_files()) |
611 | goto out; | 611 | goto out; |
612 | 612 | ||
613 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto); | 613 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto); |
@@ -632,7 +632,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock) | |||
632 | unix_insert_socket(unix_sockets_unbound, sk); | 632 | unix_insert_socket(unix_sockets_unbound, sk); |
633 | out: | 633 | out: |
634 | if (sk == NULL) | 634 | if (sk == NULL) |
635 | atomic_dec(&unix_nr_socks); | 635 | atomic_long_dec(&unix_nr_socks); |
636 | else { | 636 | else { |
637 | local_bh_disable(); | 637 | local_bh_disable(); |
638 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 638 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
@@ -1343,9 +1343,25 @@ static void unix_destruct_scm(struct sk_buff *skb) | |||
1343 | sock_wfree(skb); | 1343 | sock_wfree(skb); |
1344 | } | 1344 | } |
1345 | 1345 | ||
1346 | #define MAX_RECURSION_LEVEL 4 | ||
1347 | |||
1346 | static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) | 1348 | static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) |
1347 | { | 1349 | { |
1348 | int i; | 1350 | int i; |
1351 | unsigned char max_level = 0; | ||
1352 | int unix_sock_count = 0; | ||
1353 | |||
1354 | for (i = scm->fp->count - 1; i >= 0; i--) { | ||
1355 | struct sock *sk = unix_get_socket(scm->fp->fp[i]); | ||
1356 | |||
1357 | if (sk) { | ||
1358 | unix_sock_count++; | ||
1359 | max_level = max(max_level, | ||
1360 | unix_sk(sk)->recursion_level); | ||
1361 | } | ||
1362 | } | ||
1363 | if (unlikely(max_level > MAX_RECURSION_LEVEL)) | ||
1364 | return -ETOOMANYREFS; | ||
1349 | 1365 | ||
1350 | /* | 1366 | /* |
1351 | * Need to duplicate file references for the sake of garbage | 1367 | * Need to duplicate file references for the sake of garbage |
@@ -1356,9 +1372,11 @@ static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) | |||
1356 | if (!UNIXCB(skb).fp) | 1372 | if (!UNIXCB(skb).fp) |
1357 | return -ENOMEM; | 1373 | return -ENOMEM; |
1358 | 1374 | ||
1359 | for (i = scm->fp->count-1; i >= 0; i--) | 1375 | if (unix_sock_count) { |
1360 | unix_inflight(scm->fp->fp[i]); | 1376 | for (i = scm->fp->count - 1; i >= 0; i--) |
1361 | return 0; | 1377 | unix_inflight(scm->fp->fp[i]); |
1378 | } | ||
1379 | return max_level; | ||
1362 | } | 1380 | } |
1363 | 1381 | ||
1364 | static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds) | 1382 | static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds) |
@@ -1393,6 +1411,7 @@ static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, | |||
1393 | struct sk_buff *skb; | 1411 | struct sk_buff *skb; |
1394 | long timeo; | 1412 | long timeo; |
1395 | struct scm_cookie tmp_scm; | 1413 | struct scm_cookie tmp_scm; |
1414 | int max_level; | ||
1396 | 1415 | ||
1397 | if (NULL == siocb->scm) | 1416 | if (NULL == siocb->scm) |
1398 | siocb->scm = &tmp_scm; | 1417 | siocb->scm = &tmp_scm; |
@@ -1431,8 +1450,9 @@ static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, | |||
1431 | goto out; | 1450 | goto out; |
1432 | 1451 | ||
1433 | err = unix_scm_to_skb(siocb->scm, skb, true); | 1452 | err = unix_scm_to_skb(siocb->scm, skb, true); |
1434 | if (err) | 1453 | if (err < 0) |
1435 | goto out_free; | 1454 | goto out_free; |
1455 | max_level = err + 1; | ||
1436 | unix_get_secdata(siocb->scm, skb); | 1456 | unix_get_secdata(siocb->scm, skb); |
1437 | 1457 | ||
1438 | skb_reset_transport_header(skb); | 1458 | skb_reset_transport_header(skb); |
@@ -1511,7 +1531,11 @@ restart: | |||
1511 | goto restart; | 1531 | goto restart; |
1512 | } | 1532 | } |
1513 | 1533 | ||
1534 | if (sock_flag(other, SOCK_RCVTSTAMP)) | ||
1535 | __net_timestamp(skb); | ||
1514 | skb_queue_tail(&other->sk_receive_queue, skb); | 1536 | skb_queue_tail(&other->sk_receive_queue, skb); |
1537 | if (max_level > unix_sk(other)->recursion_level) | ||
1538 | unix_sk(other)->recursion_level = max_level; | ||
1515 | unix_state_unlock(other); | 1539 | unix_state_unlock(other); |
1516 | other->sk_data_ready(other, len); | 1540 | other->sk_data_ready(other, len); |
1517 | sock_put(other); | 1541 | sock_put(other); |
@@ -1542,6 +1566,7 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, | |||
1542 | int sent = 0; | 1566 | int sent = 0; |
1543 | struct scm_cookie tmp_scm; | 1567 | struct scm_cookie tmp_scm; |
1544 | bool fds_sent = false; | 1568 | bool fds_sent = false; |
1569 | int max_level; | ||
1545 | 1570 | ||
1546 | if (NULL == siocb->scm) | 1571 | if (NULL == siocb->scm) |
1547 | siocb->scm = &tmp_scm; | 1572 | siocb->scm = &tmp_scm; |
@@ -1605,10 +1630,11 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, | |||
1605 | 1630 | ||
1606 | /* Only send the fds in the first buffer */ | 1631 | /* Only send the fds in the first buffer */ |
1607 | err = unix_scm_to_skb(siocb->scm, skb, !fds_sent); | 1632 | err = unix_scm_to_skb(siocb->scm, skb, !fds_sent); |
1608 | if (err) { | 1633 | if (err < 0) { |
1609 | kfree_skb(skb); | 1634 | kfree_skb(skb); |
1610 | goto out_err; | 1635 | goto out_err; |
1611 | } | 1636 | } |
1637 | max_level = err + 1; | ||
1612 | fds_sent = true; | 1638 | fds_sent = true; |
1613 | 1639 | ||
1614 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); | 1640 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
@@ -1624,6 +1650,8 @@ static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, | |||
1624 | goto pipe_err_free; | 1650 | goto pipe_err_free; |
1625 | 1651 | ||
1626 | skb_queue_tail(&other->sk_receive_queue, skb); | 1652 | skb_queue_tail(&other->sk_receive_queue, skb); |
1653 | if (max_level > unix_sk(other)->recursion_level) | ||
1654 | unix_sk(other)->recursion_level = max_level; | ||
1627 | unix_state_unlock(other); | 1655 | unix_state_unlock(other); |
1628 | other->sk_data_ready(other, size); | 1656 | other->sk_data_ready(other, size); |
1629 | sent += size; | 1657 | sent += size; |
@@ -1722,6 +1750,9 @@ static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock, | |||
1722 | if (err) | 1750 | if (err) |
1723 | goto out_free; | 1751 | goto out_free; |
1724 | 1752 | ||
1753 | if (sock_flag(sk, SOCK_RCVTSTAMP)) | ||
1754 | __sock_recv_timestamp(msg, sk, skb); | ||
1755 | |||
1725 | if (!siocb->scm) { | 1756 | if (!siocb->scm) { |
1726 | siocb->scm = &tmp_scm; | 1757 | siocb->scm = &tmp_scm; |
1727 | memset(&tmp_scm, 0, sizeof(tmp_scm)); | 1758 | memset(&tmp_scm, 0, sizeof(tmp_scm)); |
@@ -1840,6 +1871,7 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock, | |||
1840 | unix_state_lock(sk); | 1871 | unix_state_lock(sk); |
1841 | skb = skb_dequeue(&sk->sk_receive_queue); | 1872 | skb = skb_dequeue(&sk->sk_receive_queue); |
1842 | if (skb == NULL) { | 1873 | if (skb == NULL) { |
1874 | unix_sk(sk)->recursion_level = 0; | ||
1843 | if (copied >= target) | 1875 | if (copied >= target) |
1844 | goto unlock; | 1876 | goto unlock; |
1845 | 1877 | ||
@@ -2033,11 +2065,10 @@ static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table | |||
2033 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 2065 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
2034 | mask |= POLLHUP; | 2066 | mask |= POLLHUP; |
2035 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 2067 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
2036 | mask |= POLLRDHUP; | 2068 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
2037 | 2069 | ||
2038 | /* readable? */ | 2070 | /* readable? */ |
2039 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 2071 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
2040 | (sk->sk_shutdown & RCV_SHUTDOWN)) | ||
2041 | mask |= POLLIN | POLLRDNORM; | 2072 | mask |= POLLIN | POLLRDNORM; |
2042 | 2073 | ||
2043 | /* Connection-based need to check for termination and startup */ | 2074 | /* Connection-based need to check for termination and startup */ |