18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/module.h>
38c2ecf20Sopenharmony_ci#include <linux/kernel.h>
48c2ecf20Sopenharmony_ci#include <linux/string.h>
58c2ecf20Sopenharmony_ci#include <linux/socket.h>
68c2ecf20Sopenharmony_ci#include <linux/net.h>
78c2ecf20Sopenharmony_ci#include <linux/fs.h>
88c2ecf20Sopenharmony_ci#include <net/af_unix.h>
98c2ecf20Sopenharmony_ci#include <net/scm.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/io_uring.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "scm.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciunsigned int unix_tot_inflight;
168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_tot_inflight);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciLIST_HEAD(gc_inflight_list);
198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gc_inflight_list);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciDEFINE_SPINLOCK(unix_gc_lock);
228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_gc_lock);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistruct sock *unix_get_socket(struct file *filp)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct sock *u_sock = NULL;
278c2ecf20Sopenharmony_ci	struct inode *inode = file_inode(filp);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	/* Socket ? */
308c2ecf20Sopenharmony_ci	if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
318c2ecf20Sopenharmony_ci		struct socket *sock = SOCKET_I(inode);
328c2ecf20Sopenharmony_ci		struct sock *s = sock->sk;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci		/* PF_UNIX ? */
358c2ecf20Sopenharmony_ci		if (s && sock->ops && sock->ops->family == PF_UNIX)
368c2ecf20Sopenharmony_ci			u_sock = s;
378c2ecf20Sopenharmony_ci	} else {
388c2ecf20Sopenharmony_ci		/* Could be an io_uring instance */
398c2ecf20Sopenharmony_ci		u_sock = io_uring_get_socket(filp);
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci	return u_sock;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_get_socket);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Keep the number of times in flight count for the file
468c2ecf20Sopenharmony_ci * descriptor if it is for an AF_UNIX socket.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_civoid unix_inflight(struct user_struct *user, struct file *fp)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct sock *s = unix_get_socket(fp);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	spin_lock(&unix_gc_lock);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (s) {
558c2ecf20Sopenharmony_ci		struct unix_sock *u = unix_sk(s);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci		if (atomic_long_inc_return(&u->inflight) == 1) {
588c2ecf20Sopenharmony_ci			BUG_ON(!list_empty(&u->link));
598c2ecf20Sopenharmony_ci			list_add_tail(&u->link, &gc_inflight_list);
608c2ecf20Sopenharmony_ci		} else {
618c2ecf20Sopenharmony_ci			BUG_ON(list_empty(&u->link));
628c2ecf20Sopenharmony_ci		}
638c2ecf20Sopenharmony_ci		/* Paired with READ_ONCE() in wait_for_unix_gc() */
648c2ecf20Sopenharmony_ci		WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci	WRITE_ONCE(user->unix_inflight, user->unix_inflight + 1);
678c2ecf20Sopenharmony_ci	spin_unlock(&unix_gc_lock);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_civoid unix_notinflight(struct user_struct *user, struct file *fp)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct sock *s = unix_get_socket(fp);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	spin_lock(&unix_gc_lock);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (s) {
778c2ecf20Sopenharmony_ci		struct unix_sock *u = unix_sk(s);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci		BUG_ON(!atomic_long_read(&u->inflight));
808c2ecf20Sopenharmony_ci		BUG_ON(list_empty(&u->link));
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci		if (atomic_long_dec_and_test(&u->inflight))
838c2ecf20Sopenharmony_ci			list_del_init(&u->link);
848c2ecf20Sopenharmony_ci		/* Paired with READ_ONCE() in wait_for_unix_gc() */
858c2ecf20Sopenharmony_ci		WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci	WRITE_ONCE(user->unix_inflight, user->unix_inflight - 1);
888c2ecf20Sopenharmony_ci	spin_unlock(&unix_gc_lock);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*
928c2ecf20Sopenharmony_ci * The "user->unix_inflight" variable is protected by the garbage
938c2ecf20Sopenharmony_ci * collection lock, and we just read it locklessly here. If you go
948c2ecf20Sopenharmony_ci * over the limit, there might be a tiny race in actually noticing
958c2ecf20Sopenharmony_ci * it across threads. Tough.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_cistatic inline bool too_many_unix_fds(struct task_struct *p)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct user_struct *user = current_user();
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (unlikely(READ_ONCE(user->unix_inflight) > task_rlimit(p, RLIMIT_NOFILE)))
1028c2ecf20Sopenharmony_ci		return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
1038c2ecf20Sopenharmony_ci	return false;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciint unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	int i;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (too_many_unix_fds(current))
1118c2ecf20Sopenharmony_ci		return -ETOOMANYREFS;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * Need to duplicate file references for the sake of garbage
1158c2ecf20Sopenharmony_ci	 * collection.  Otherwise a socket in the fps might become a
1168c2ecf20Sopenharmony_ci	 * candidate for GC while the skb is not yet queued.
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	UNIXCB(skb).fp = scm_fp_dup(scm->fp);
1198c2ecf20Sopenharmony_ci	if (!UNIXCB(skb).fp)
1208c2ecf20Sopenharmony_ci		return -ENOMEM;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	for (i = scm->fp->count - 1; i >= 0; i--)
1238c2ecf20Sopenharmony_ci		unix_inflight(scm->fp->user, scm->fp->fp[i]);
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_attach_fds);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_civoid unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	int i;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	scm->fp = UNIXCB(skb).fp;
1338c2ecf20Sopenharmony_ci	UNIXCB(skb).fp = NULL;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for (i = scm->fp->count-1; i >= 0; i--)
1368c2ecf20Sopenharmony_ci		unix_notinflight(scm->fp->user, scm->fp->fp[i]);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_detach_fds);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_civoid unix_destruct_scm(struct sk_buff *skb)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct scm_cookie scm;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	memset(&scm, 0, sizeof(scm));
1458c2ecf20Sopenharmony_ci	scm.pid  = UNIXCB(skb).pid;
1468c2ecf20Sopenharmony_ci	if (UNIXCB(skb).fp)
1478c2ecf20Sopenharmony_ci		unix_detach_fds(&scm, skb);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* Alas, it calls VFS */
1508c2ecf20Sopenharmony_ci	/* So fscking what? fput() had been SMP-safe since the last Summer */
1518c2ecf20Sopenharmony_ci	scm_destroy(&scm);
1528c2ecf20Sopenharmony_ci	sock_wfree(skb);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unix_destruct_scm);
155