Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <net/af_unix.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <net/scm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/io_uring.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "scm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) unsigned int unix_tot_inflight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) EXPORT_SYMBOL(unix_tot_inflight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) LIST_HEAD(gc_inflight_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) EXPORT_SYMBOL(gc_inflight_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) DEFINE_SPINLOCK(unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) EXPORT_SYMBOL(unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct sock *unix_get_socket(struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct sock *u_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	/* Socket ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		struct socket *sock = SOCKET_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		struct sock *s = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		/* PF_UNIX ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		if (s && sock->ops && sock->ops->family == PF_UNIX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			u_sock = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		/* Could be an io_uring instance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		u_sock = io_uring_get_socket(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	return u_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) EXPORT_SYMBOL(unix_get_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Keep the number of times in flight count for the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * descriptor if it is for an AF_UNIX socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) void unix_inflight(struct user_struct *user, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct sock *s = unix_get_socket(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	spin_lock(&unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		struct unix_sock *u = unix_sk(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		if (atomic_long_inc_return(&u->inflight) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			BUG_ON(!list_empty(&u->link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			list_add_tail(&u->link, &gc_inflight_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			BUG_ON(list_empty(&u->link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		/* Paired with READ_ONCE() in wait_for_unix_gc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	user->unix_inflight++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	spin_unlock(&unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) void unix_notinflight(struct user_struct *user, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct sock *s = unix_get_socket(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	spin_lock(&unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		struct unix_sock *u = unix_sk(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		BUG_ON(!atomic_long_read(&u->inflight));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		BUG_ON(list_empty(&u->link));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (atomic_long_dec_and_test(&u->inflight))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			list_del_init(&u->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		/* Paired with READ_ONCE() in wait_for_unix_gc() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	user->unix_inflight--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	spin_unlock(&unix_gc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * The "user->unix_inflight" variable is protected by the garbage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * collection lock, and we just read it locklessly here. If you go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * over the limit, there might be a tiny race in actually noticing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * it across threads. Tough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static inline bool too_many_unix_fds(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct user_struct *user = current_user();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (unlikely(user->unix_inflight > task_rlimit(p, RLIMIT_NOFILE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (too_many_unix_fds(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return -ETOOMANYREFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * Need to duplicate file references for the sake of garbage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * collection.  Otherwise a socket in the fps might become a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 * candidate for GC while the skb is not yet queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	UNIXCB(skb).fp = scm_fp_dup(scm->fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!UNIXCB(skb).fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	for (i = scm->fp->count - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		unix_inflight(scm->fp->user, scm->fp->fp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) EXPORT_SYMBOL(unix_attach_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	scm->fp = UNIXCB(skb).fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	UNIXCB(skb).fp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	for (i = scm->fp->count-1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		unix_notinflight(scm->fp->user, scm->fp->fp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) EXPORT_SYMBOL(unix_detach_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) void unix_destruct_scm(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct scm_cookie scm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	memset(&scm, 0, sizeof(scm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	scm.pid  = UNIXCB(skb).pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (UNIXCB(skb).fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		unix_detach_fds(&scm, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	/* Alas, it calls VFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	/* So fscking what? fput() had been SMP-safe since the last Summer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	scm_destroy(&scm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	sock_wfree(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) EXPORT_SYMBOL(unix_destruct_scm);