hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/net/unix/af_unix.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * NET4: Implementation of BSD Unix domain sockets.
34 *
45 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk>
5
- *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU General Public License
8
- * as published by the Free Software Foundation; either version
9
- * 2 of the License, or (at your option) any later version.
106 *
117 * Fixes:
128 * Linus Torvalds : Assorted bug cures.
....@@ -48,7 +44,6 @@
4844 * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT,
4945 * the core infrastructure is doing that
5046 * for all net proto families now (2.5.69+)
51
- *
5247 *
5348 * Known differences from reference BSD that was tested:
5449 *
....@@ -295,11 +290,9 @@
295290
296291 if (u->addr->len == len &&
297292 !memcmp(u->addr->name, sunname, len))
298
- goto found;
293
+ return s;
299294 }
300
- s = NULL;
301
-found:
302
- return s;
295
+ return NULL;
303296 }
304297
305298 static inline struct sock *unix_find_socket_byname(struct net *net,
....@@ -445,7 +438,7 @@
445438 * -ECONNREFUSED. Otherwise, if we haven't queued any skbs
446439 * to other and its full, we will hang waiting for POLLOUT.
447440 */
448
- if (unix_recvq_full(other) && !sock_flag(other, SOCK_DEAD))
441
+ if (unix_recvq_full_lockless(other) && !sock_flag(other, SOCK_DEAD))
449442 return 1;
450443
451444 if (connected)
....@@ -536,7 +529,7 @@
536529 /* Clear state */
537530 unix_state_lock(sk);
538531 sock_orphan(sk);
539
- sk->sk_shutdown = SHUTDOWN_MASK;
532
+ WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
540533 path = u->path;
541534 u->path.dentry = NULL;
542535 u->path.mnt = NULL;
....@@ -554,7 +547,7 @@
554547 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
555548 unix_state_lock(skpair);
556549 /* No more writes */
557
- skpair->sk_shutdown = SHUTDOWN_MASK;
550
+ WRITE_ONCE(skpair->sk_shutdown, SHUTDOWN_MASK);
558551 if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
559552 skpair->sk_err = ECONNRESET;
560553 unix_state_unlock(skpair);
....@@ -594,7 +587,7 @@
594587 * What the above comment does talk about? --ANK(980817)
595588 */
596589
597
- if (unix_tot_inflight)
590
+ if (READ_ONCE(unix_tot_inflight))
598591 unix_gc(); /* Garbage collect fds */
599592 }
600593
....@@ -643,7 +636,6 @@
643636 int err;
644637 struct sock *sk = sock->sk;
645638 struct unix_sock *u = unix_sk(sk);
646
- struct pid *old_pid = NULL;
647639
648640 err = -EOPNOTSUPP;
649641 if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET)
....@@ -664,7 +656,6 @@
664656
665657 out_unlock:
666658 unix_state_unlock(sk);
667
- put_pid(old_pid);
668659 out:
669660 return err;
670661 }
....@@ -706,12 +697,27 @@
706697 if (mutex_lock_interruptible(&u->iolock))
707698 return -EINTR;
708699
709
- sk->sk_peek_off = val;
700
+ WRITE_ONCE(sk->sk_peek_off, val);
710701 mutex_unlock(&u->iolock);
711702
712703 return 0;
713704 }
714705
706
+#ifdef CONFIG_PROC_FS
707
+static void unix_show_fdinfo(struct seq_file *m, struct socket *sock)
708
+{
709
+ struct sock *sk = sock->sk;
710
+ struct unix_sock *u;
711
+
712
+ if (sk) {
713
+ u = unix_sk(sock->sk);
714
+ seq_printf(m, "scm_fds: %u\n",
715
+ atomic_read(&u->scm_stat.nr_fds));
716
+ }
717
+}
718
+#else
719
+#define unix_show_fdinfo NULL
720
+#endif
715721
716722 static const struct proto_ops unix_stream_ops = {
717723 .family = PF_UNIX,
....@@ -729,14 +735,13 @@
729735 #endif
730736 .listen = unix_listen,
731737 .shutdown = unix_shutdown,
732
- .setsockopt = sock_no_setsockopt,
733
- .getsockopt = sock_no_getsockopt,
734738 .sendmsg = unix_stream_sendmsg,
735739 .recvmsg = unix_stream_recvmsg,
736740 .mmap = sock_no_mmap,
737741 .sendpage = unix_stream_sendpage,
738742 .splice_read = unix_stream_splice_read,
739743 .set_peek_off = unix_set_peek_off,
744
+ .show_fdinfo = unix_show_fdinfo,
740745 };
741746
742747 static const struct proto_ops unix_dgram_ops = {
....@@ -755,13 +760,12 @@
755760 #endif
756761 .listen = sock_no_listen,
757762 .shutdown = unix_shutdown,
758
- .setsockopt = sock_no_setsockopt,
759
- .getsockopt = sock_no_getsockopt,
760763 .sendmsg = unix_dgram_sendmsg,
761764 .recvmsg = unix_dgram_recvmsg,
762765 .mmap = sock_no_mmap,
763766 .sendpage = sock_no_sendpage,
764767 .set_peek_off = unix_set_peek_off,
768
+ .show_fdinfo = unix_show_fdinfo,
765769 };
766770
767771 static const struct proto_ops unix_seqpacket_ops = {
....@@ -780,13 +784,12 @@
780784 #endif
781785 .listen = unix_listen,
782786 .shutdown = unix_shutdown,
783
- .setsockopt = sock_no_setsockopt,
784
- .getsockopt = sock_no_getsockopt,
785787 .sendmsg = unix_seqpacket_sendmsg,
786788 .recvmsg = unix_seqpacket_recvmsg,
787789 .mmap = sock_no_mmap,
788790 .sendpage = sock_no_sendpage,
789791 .set_peek_off = unix_set_peek_off,
792
+ .show_fdinfo = unix_show_fdinfo,
790793 };
791794
792795 static struct proto unix_proto = {
....@@ -824,6 +827,7 @@
824827 mutex_init(&u->bindlock); /* single task binding lock */
825828 init_waitqueue_head(&u->peer_wait);
826829 init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
830
+ memset(&u->scm_stat, 0, sizeof(struct scm_stat));
827831 unix_insert_socket(unix_sockets_unbound(sk), sk);
828832 out:
829833 if (sk == NULL)
....@@ -854,7 +858,7 @@
854858 */
855859 case SOCK_RAW:
856860 sock->type = SOCK_DGRAM;
857
- /* fall through */
861
+ fallthrough;
858862 case SOCK_DGRAM:
859863 sock->ops = &unix_dgram_ops;
860864 break;
....@@ -895,7 +899,6 @@
895899 if (err)
896900 return err;
897901
898
- err = 0;
899902 if (u->addr)
900903 goto out;
901904
....@@ -1223,6 +1226,7 @@
12231226 }
12241227
12251228 static long unix_wait_for_peer(struct sock *other, long timeo)
1229
+ __releases(&unix_sk(other)->lock)
12261230 {
12271231 struct unix_sock *u = unix_sk(other);
12281232 int sched;
....@@ -1232,7 +1236,7 @@
12321236
12331237 sched = !sock_flag(other, SOCK_DEAD) &&
12341238 !(other->sk_shutdown & RCV_SHUTDOWN) &&
1235
- unix_recvq_full(other);
1239
+ unix_recvq_full_lockless(other);
12361240
12371241 unix_state_unlock(other);
12381242
....@@ -1655,6 +1659,24 @@
16551659 unix_secdata_eq(scm, skb);
16561660 }
16571661
1662
+static void scm_stat_add(struct sock *sk, struct sk_buff *skb)
1663
+{
1664
+ struct scm_fp_list *fp = UNIXCB(skb).fp;
1665
+ struct unix_sock *u = unix_sk(sk);
1666
+
1667
+ if (unlikely(fp && fp->count))
1668
+ atomic_add(fp->count, &u->scm_stat.nr_fds);
1669
+}
1670
+
1671
+static void scm_stat_del(struct sock *sk, struct sk_buff *skb)
1672
+{
1673
+ struct scm_fp_list *fp = UNIXCB(skb).fp;
1674
+ struct unix_sock *u = unix_sk(sk);
1675
+
1676
+ if (unlikely(fp && fp->count))
1677
+ atomic_sub(fp->count, &u->scm_stat.nr_fds);
1678
+}
1679
+
16581680 /*
16591681 * Send AF_UNIX data.
16601682 */
....@@ -1841,6 +1863,7 @@
18411863 if (sock_flag(other, SOCK_RCVTSTAMP))
18421864 __net_timestamp(skb);
18431865 maybe_add_creds(skb, sock, other);
1866
+ scm_stat_add(other, skb);
18441867 skb_queue_tail(&other->sk_receive_queue, skb);
18451868 unix_state_unlock(other);
18461869 other->sk_data_ready(other);
....@@ -1943,6 +1966,7 @@
19431966 goto pipe_err_free;
19441967
19451968 maybe_add_creds(skb, sock, other);
1969
+ scm_stat_add(other, skb);
19461970 skb_queue_tail(&other->sk_receive_queue, skb);
19471971 unix_state_unlock(other);
19481972 other->sk_data_ready(other);
....@@ -1984,6 +2008,7 @@
19842008
19852009 if (false) {
19862010 alloc_skb:
2011
+ spin_unlock(&other->sk_receive_queue.lock);
19872012 unix_state_unlock(other);
19882013 mutex_unlock(&unix_sk(other)->iolock);
19892014 newskb = sock_alloc_send_pskb(sk, 0, 0, flags & MSG_DONTWAIT,
....@@ -2023,6 +2048,7 @@
20232048 init_scm = false;
20242049 }
20252050
2051
+ spin_lock(&other->sk_receive_queue.lock);
20262052 skb = skb_peek_tail(&other->sk_receive_queue);
20272053 if (tail && tail == skb) {
20282054 skb = newskb;
....@@ -2053,14 +2079,11 @@
20532079 refcount_add(size, &sk->sk_wmem_alloc);
20542080
20552081 if (newskb) {
2056
- err = unix_scm_to_skb(&scm, skb, false);
2057
- if (err)
2058
- goto err_state_unlock;
2059
- spin_lock(&other->sk_receive_queue.lock);
2082
+ unix_scm_to_skb(&scm, skb, false);
20602083 __skb_queue_tail(&other->sk_receive_queue, newskb);
2061
- spin_unlock(&other->sk_receive_queue.lock);
20622084 }
20632085
2086
+ spin_unlock(&other->sk_receive_queue.lock);
20642087 unix_state_unlock(other);
20652088 mutex_unlock(&unix_sk(other)->iolock);
20662089
....@@ -2129,8 +2152,8 @@
21292152 struct unix_sock *u = unix_sk(sk);
21302153 struct sk_buff *skb, *last;
21312154 long timeo;
2155
+ int skip;
21322156 int err;
2133
- int peeked, skip;
21342157
21352158 err = -EOPNOTSUPP;
21362159 if (flags&MSG_OOB)
....@@ -2142,17 +2165,21 @@
21422165 mutex_lock(&u->iolock);
21432166
21442167 skip = sk_peek_offset(sk, flags);
2145
- skb = __skb_try_recv_datagram(sk, flags, NULL, &peeked, &skip,
2146
- &err, &last);
2147
- if (skb)
2168
+ skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
2169
+ &skip, &err, &last);
2170
+ if (skb) {
2171
+ if (!(flags & MSG_PEEK))
2172
+ scm_stat_del(sk, skb);
21482173 break;
2174
+ }
21492175
21502176 mutex_unlock(&u->iolock);
21512177
21522178 if (err != -EAGAIN)
21532179 break;
21542180 } while (timeo &&
2155
- !__skb_wait_for_more_packets(sk, &err, &timeo, last));
2181
+ !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
2182
+ &err, &timeo, last));
21562183
21572184 if (!skb) { /* implies iolock unlocked */
21582185 unix_state_lock(sk);
....@@ -2437,8 +2464,10 @@
24372464
24382465 sk_peek_offset_bwd(sk, chunk);
24392466
2440
- if (UNIXCB(skb).fp)
2467
+ if (UNIXCB(skb).fp) {
2468
+ scm_stat_del(sk, skb);
24412469 unix_detach_fds(&scm, skb);
2470
+ }
24422471
24432472 if (unix_skb_len(skb))
24442473 break;
....@@ -2551,7 +2580,7 @@
25512580 ++mode;
25522581
25532582 unix_state_lock(sk);
2554
- sk->sk_shutdown |= mode;
2583
+ WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | mode);
25552584 other = unix_peer(sk);
25562585 if (other)
25572586 sock_hold(other);
....@@ -2568,7 +2597,7 @@
25682597 if (mode&SEND_SHUTDOWN)
25692598 peer_mode |= RCV_SHUTDOWN;
25702599 unix_state_lock(other);
2571
- other->sk_shutdown |= peer_mode;
2600
+ WRITE_ONCE(other->sk_shutdown, other->sk_shutdown | peer_mode);
25722601 unix_state_unlock(other);
25732602 other->sk_state_change(other);
25742603 if (peer_mode == SHUTDOWN_MASK)
....@@ -2687,16 +2716,18 @@
26872716 {
26882717 struct sock *sk = sock->sk;
26892718 __poll_t mask;
2719
+ u8 shutdown;
26902720
26912721 sock_poll_wait(file, sock, wait);
26922722 mask = 0;
2723
+ shutdown = READ_ONCE(sk->sk_shutdown);
26932724
26942725 /* exceptional events? */
26952726 if (sk->sk_err)
26962727 mask |= EPOLLERR;
2697
- if (sk->sk_shutdown == SHUTDOWN_MASK)
2728
+ if (shutdown == SHUTDOWN_MASK)
26982729 mask |= EPOLLHUP;
2699
- if (sk->sk_shutdown & RCV_SHUTDOWN)
2730
+ if (shutdown & RCV_SHUTDOWN)
27002731 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
27012732
27022733 /* readable? */
....@@ -2724,18 +2755,20 @@
27242755 struct sock *sk = sock->sk, *other;
27252756 unsigned int writable;
27262757 __poll_t mask;
2758
+ u8 shutdown;
27272759
27282760 sock_poll_wait(file, sock, wait);
27292761 mask = 0;
2762
+ shutdown = READ_ONCE(sk->sk_shutdown);
27302763
27312764 /* exceptional events? */
27322765 if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
27332766 mask |= EPOLLERR |
27342767 (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
27352768
2736
- if (sk->sk_shutdown & RCV_SHUTDOWN)
2769
+ if (shutdown & RCV_SHUTDOWN)
27372770 mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
2738
- if (sk->sk_shutdown == SHUTDOWN_MASK)
2771
+ if (shutdown == SHUTDOWN_MASK)
27392772 mask |= EPOLLHUP;
27402773
27412774 /* readable? */
....@@ -2949,7 +2982,7 @@
29492982 {
29502983 int rc = -1;
29512984
2952
- BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2985
+ BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
29532986
29542987 rc = proto_register(&unix_proto, 1);
29552988 if (rc != 0) {
....@@ -2978,4 +3011,5 @@
29783011 module_exit(af_unix_exit);
29793012
29803013 MODULE_LICENSE("GPL");
3014
+MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
29813015 MODULE_ALIAS_NETPROTO(PF_UNIX);