hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
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)
....@@ -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 }
....@@ -712,6 +703,21 @@
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;
....@@ -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);
....@@ -2129,8 +2153,8 @@
21292153 struct unix_sock *u = unix_sk(sk);
21302154 struct sk_buff *skb, *last;
21312155 long timeo;
2156
+ int skip;
21322157 int err;
2133
- int peeked, skip;
21342158
21352159 err = -EOPNOTSUPP;
21362160 if (flags&MSG_OOB)
....@@ -2142,17 +2166,21 @@
21422166 mutex_lock(&u->iolock);
21432167
21442168 skip = sk_peek_offset(sk, flags);
2145
- skb = __skb_try_recv_datagram(sk, flags, NULL, &peeked, &skip,
2146
- &err, &last);
2147
- if (skb)
2169
+ skb = __skb_try_recv_datagram(sk, &sk->sk_receive_queue, flags,
2170
+ &skip, &err, &last);
2171
+ if (skb) {
2172
+ if (!(flags & MSG_PEEK))
2173
+ scm_stat_del(sk, skb);
21482174 break;
2175
+ }
21492176
21502177 mutex_unlock(&u->iolock);
21512178
21522179 if (err != -EAGAIN)
21532180 break;
21542181 } while (timeo &&
2155
- !__skb_wait_for_more_packets(sk, &err, &timeo, last));
2182
+ !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
2183
+ &err, &timeo, last));
21562184
21572185 if (!skb) { /* implies iolock unlocked */
21582186 unix_state_lock(sk);
....@@ -2437,8 +2465,10 @@
24372465
24382466 sk_peek_offset_bwd(sk, chunk);
24392467
2440
- if (UNIXCB(skb).fp)
2468
+ if (UNIXCB(skb).fp) {
2469
+ scm_stat_del(sk, skb);
24412470 unix_detach_fds(&scm, skb);
2471
+ }
24422472
24432473 if (unix_skb_len(skb))
24442474 break;
....@@ -2949,7 +2979,7 @@
29492979 {
29502980 int rc = -1;
29512981
2952
- BUILD_BUG_ON(sizeof(struct unix_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2982
+ BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof_field(struct sk_buff, cb));
29532983
29542984 rc = proto_register(&unix_proto, 1);
29552985 if (rc != 0) {
....@@ -2978,4 +3008,5 @@
29783008 module_exit(af_unix_exit);
29793009
29803010 MODULE_LICENSE("GPL");
3011
+MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
29813012 MODULE_ALIAS_NETPROTO(PF_UNIX);