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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/fs/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  Manage the dynamic fd arrays in the process files_struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/fdtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/close_range.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) unsigned int sysctl_nr_open __read_mostly = 1024*1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) unsigned int sysctl_nr_open_min = BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) /* our min() is unusable in constant expressions ;-/ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define __const_min(x, y) ((x) < (y) ? (x) : (y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) unsigned int sysctl_nr_open_max =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) static void __free_fdtable(struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	kvfree(fdt->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	kvfree(fdt->open_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	kfree(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static void free_fdtable_rcu(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	__free_fdtable(container_of(rcu, struct fdtable, rcu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define BITBIT_NR(nr)	BITS_TO_LONGS(BITS_TO_LONGS(nr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define BITBIT_SIZE(nr)	(BITBIT_NR(nr) * sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * Copy 'count' fd bits from the old table to the new table and clear the extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * space if any.  This does not copy the file pointers.  Called with the files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * spinlock held for write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 			    unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	unsigned int cpy, set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	cpy = count / BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	set = (nfdt->max_fds - count) / BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	memset((char *)nfdt->open_fds + cpy, 0, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	memset((char *)nfdt->close_on_exec + cpy, 0, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	cpy = BITBIT_SIZE(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	set = BITBIT_SIZE(nfdt->max_fds) - cpy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	memcpy(nfdt->full_fds_bits, ofdt->full_fds_bits, cpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	memset((char *)nfdt->full_fds_bits + cpy, 0, set);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * Copy all file descriptors from the old table to the new, expanded table and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * clear the extra space.  Called with the files spinlock held for write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	size_t cpy, set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	BUG_ON(nfdt->max_fds < ofdt->max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	cpy = ofdt->max_fds * sizeof(struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	memcpy(nfdt->fd, ofdt->fd, cpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	memset((char *)nfdt->fd + cpy, 0, set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	copy_fd_bitmaps(nfdt, ofdt, ofdt->max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * Note how the fdtable bitmap allocations very much have to be a multiple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * BITS_PER_LONG. This is not only because we walk those things in chunks of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * 'unsigned long' in some places, but simply because that is how the Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * kernel bitmaps are defined to work: they are not "bits in an array of bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * they are very much "bits in an array of unsigned long".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * The ALIGN(nr, BITS_PER_LONG) here is for clarity: since we just multiplied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * by that "1024/sizeof(ptr)" before, we already know there are sufficient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * clear low bits. Clang seems to realize that, gcc ends up being confused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * On a 128-bit machine, the ALIGN() would actually matter. In the meantime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * let's consider it documentation (and maybe a test-case for gcc to improve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * its code generation ;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) static struct fdtable * alloc_fdtable(unsigned int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	 * Figure out how many fds we actually want to support in this fdtable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	 * Allocation steps are keyed to the size of the fdarray, since it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	 * grows far faster than any of the other dynamic data. We try to fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 * the fdarray into comfortable page-tuned chunks: starting at 1024B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 * and growing in powers of two from there on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	nr /= (1024 / sizeof(struct file *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	nr = roundup_pow_of_two(nr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	nr *= (1024 / sizeof(struct file *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	nr = ALIGN(nr, BITS_PER_LONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	 * had been set lower between the check in expand_files() and here.  Deal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	 * with that in caller, it's cheaper that way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	 * bitmaps handling below becomes unpleasant, to put it mildly...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (unlikely(nr > sysctl_nr_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	if (!fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	fdt->max_fds = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	data = kvmalloc_array(nr, sizeof(struct file *), GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		goto out_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	fdt->fd = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	data = kvmalloc(max_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 				 2 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 				 GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		goto out_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	fdt->open_fds = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	data += nr / BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	fdt->close_on_exec = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	data += nr / BITS_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	fdt->full_fds_bits = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	return fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) out_arr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	kvfree(fdt->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) out_fdt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	kfree(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * Expand the file descriptor table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * This function will allocate a new fdtable and both fd array and fdset, of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  * the given size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164)  * Return <0 error code on error; 1 on successful completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165)  * The files->file_lock should be held on entry, and will be held on exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static int expand_fdtable(struct files_struct *files, unsigned int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	__releases(files->file_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	__acquires(files->file_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	struct fdtable *new_fdt, *cur_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	new_fdt = alloc_fdtable(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	/* make sure all __fd_install() have seen resize_in_progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	 * or have finished their rcu_read_lock_sched() section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	if (atomic_read(&files->count) > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (!new_fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	 * extremely unlikely race - sysctl_nr_open decreased between the check in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	 * caller and alloc_fdtable().  Cheaper to catch it here...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	if (unlikely(new_fdt->max_fds <= nr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		__free_fdtable(new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return -EMFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	cur_fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	BUG_ON(nr < cur_fdt->max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	copy_fdtable(new_fdt, cur_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	rcu_assign_pointer(files->fdt, new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	if (cur_fdt != &files->fdtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	/* coupled with smp_rmb() in __fd_install() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * Expand files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  * This function will expand the file structures, if the requested size exceeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * the current capacity and there is room for expansion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * Return <0 error code on error; 0 when nothing done; 1 when files were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * expanded and execution may have blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * The files->file_lock should be held on entry, and will be held on exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) static int expand_files(struct files_struct *files, unsigned int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	__releases(files->file_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	__acquires(files->file_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	int expanded = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	/* Do we need to expand? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if (nr < fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		return expanded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	/* Can we expand? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (nr >= sysctl_nr_open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		return -EMFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (unlikely(files->resize_in_progress)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		expanded = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		wait_event(files->resize_wait, !files->resize_in_progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	/* All good, so we try */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	files->resize_in_progress = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	expanded = expand_fdtable(files, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	files->resize_in_progress = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	wake_up_all(&files->resize_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	return expanded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	__set_bit(fd, fdt->close_on_exec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	if (test_bit(fd, fdt->close_on_exec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		__clear_bit(fd, fdt->close_on_exec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	__set_bit(fd, fdt->open_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	fd /= BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	if (!~fdt->open_fds[fd])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		__set_bit(fd, fdt->full_fds_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	__clear_bit(fd, fdt->open_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	__clear_bit(fd / BITS_PER_LONG, fdt->full_fds_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) static unsigned int count_open_files(struct fdtable *fdt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	unsigned int size = fdt->max_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	/* Find the last open fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	for (i = size / BITS_PER_LONG; i > 0; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		if (fdt->open_fds[--i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	i = (i + 1) * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  * Note that a sane fdtable size always has to be a multiple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  * BITS_PER_LONG, since we have bitmaps that are sized by this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  * 'max_fds' will normally already be properly aligned, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  * turns out that in the close_range() -> __close_range() ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * unshare_fd() -> dup_fd() -> sane_fdtable_size() we can end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * up having a 'max_fds' value that isn't already aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * Rather than make close_range() have to worry about this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * just make that BITS_PER_LONG alignment be part of a sane
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * fdtable size. Becuase that's really what it is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) static unsigned int sane_fdtable_size(struct fdtable *fdt, unsigned int max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	count = count_open_files(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (max_fds < NR_OPEN_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		max_fds = NR_OPEN_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	return ALIGN(min(count, max_fds), BITS_PER_LONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  * Allocate a new files structure and copy contents from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311)  * passed in files structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  * errorp will be valid only when the returned files_struct is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) struct files_struct *dup_fd(struct files_struct *oldf, unsigned int max_fds, int *errorp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct files_struct *newf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	struct file **old_fds, **new_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	unsigned int open_files, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	struct fdtable *old_fdt, *new_fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	*errorp = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (!newf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	atomic_set(&newf->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	spin_lock_init(&newf->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	newf->resize_in_progress = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	init_waitqueue_head(&newf->resize_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	newf->next_fd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	new_fdt = &newf->fdtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	new_fdt->max_fds = NR_OPEN_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	new_fdt->close_on_exec = newf->close_on_exec_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	new_fdt->open_fds = newf->open_fds_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	new_fdt->full_fds_bits = newf->full_fds_bits_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	new_fdt->fd = &newf->fd_array[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	spin_lock(&oldf->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	old_fdt = files_fdtable(oldf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	open_files = sane_fdtable_size(old_fdt, max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	 * Check whether we need to allocate a larger fd array and fd set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	while (unlikely(open_files > new_fdt->max_fds)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		spin_unlock(&oldf->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		if (new_fdt != &newf->fdtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			__free_fdtable(new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		new_fdt = alloc_fdtable(open_files - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		if (!new_fdt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			*errorp = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		/* beyond sysctl_nr_open; nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		if (unlikely(new_fdt->max_fds < open_files)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			__free_fdtable(new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			*errorp = -EMFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		 * Reacquire the oldf lock and a pointer to its fd table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		 * who knows it may have a new bigger fd table. We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		 * the latest pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		spin_lock(&oldf->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		old_fdt = files_fdtable(oldf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		open_files = sane_fdtable_size(old_fdt, max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	copy_fd_bitmaps(new_fdt, old_fdt, open_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	old_fds = old_fdt->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	new_fds = new_fdt->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	for (i = open_files; i != 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		struct file *f = *old_fds++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		if (f) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 			get_file(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 			 * The fd may be claimed in the fd bitmap but not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 			 * instantiated in the files array if a sibling thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			 * is partway through open().  So make sure that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			 * fd is available to the new process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 			__clear_open_fd(open_files - i, new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		rcu_assign_pointer(*new_fds++, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	spin_unlock(&oldf->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	/* clear the remainder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	rcu_assign_pointer(newf->fdt, new_fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	return newf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	kmem_cache_free(files_cachep, newf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) static struct fdtable *close_files(struct files_struct * files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	 * It is safe to dereference the fd table without RCU or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	 * ->file_lock because this is the last reference to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	 * files structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	struct fdtable *fdt = rcu_dereference_raw(files->fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	unsigned int i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		unsigned long set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		i = j * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		if (i >= fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		set = fdt->open_fds[j++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		while (set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			if (set & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 				struct file * file = xchg(&fdt->fd[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 				if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 					filp_close(file, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 					cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			set >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	return fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) struct files_struct *get_files_struct(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct files_struct *files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	task_lock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	files = task->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		atomic_inc(&files->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	task_unlock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) void put_files_struct(struct files_struct *files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (atomic_dec_and_test(&files->count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		struct fdtable *fdt = close_files(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		/* free the arrays if they are not embedded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		if (fdt != &files->fdtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 			__free_fdtable(fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		kmem_cache_free(files_cachep, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) void reset_files_struct(struct files_struct *files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	struct files_struct *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	old = tsk->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	task_lock(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	tsk->files = files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	task_unlock(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	put_files_struct(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) void exit_files(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct files_struct * files = tsk->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		task_lock(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		tsk->files = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		task_unlock(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		put_files_struct(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) struct files_struct init_files = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	.count		= ATOMIC_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	.fdt		= &init_files.fdtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	.fdtab		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		.max_fds	= NR_OPEN_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		.fd		= &init_files.fd_array[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		.close_on_exec	= init_files.close_on_exec_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		.open_fds	= init_files.open_fds_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		.full_fds_bits	= init_files.full_fds_bits_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	.file_lock	= __SPIN_LOCK_UNLOCKED(init_files.file_lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	.resize_wait	= __WAIT_QUEUE_HEAD_INITIALIZER(init_files.resize_wait),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	unsigned int maxfd = fdt->max_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	unsigned int maxbit = maxfd / BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	unsigned int bitbit = start / BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	if (bitbit > maxfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		return maxfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (bitbit > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		start = bitbit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	return find_next_zero_bit(fdt->open_fds, maxfd, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  * allocate a file descriptor, mark it busy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) int __alloc_fd(struct files_struct *files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	       unsigned start, unsigned end, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	unsigned int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	fd = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (fd < files->next_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		fd = files->next_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (fd < fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		fd = find_next_fd(fdt, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	 * N.B. For clone tasks sharing a files structure, this test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	 * will limit the total number of files that can be opened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	error = -EMFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (fd >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	error = expand_files(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	 * If we needed to expand the fs array we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	 * might have blocked - try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (start <= files->next_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		files->next_fd = fd + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	__set_open_fd(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (flags & O_CLOEXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		__set_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		__clear_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	error = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) #if 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	/* Sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		rcu_assign_pointer(fdt->fd[fd], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static int alloc_fd(unsigned start, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) int __get_unused_fd_flags(unsigned flags, unsigned long nofile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	return __alloc_fd(current->files, 0, nofile, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) int get_unused_fd_flags(unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	return __get_unused_fd_flags(flags, rlimit(RLIMIT_NOFILE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) EXPORT_SYMBOL(get_unused_fd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) static void __put_unused_fd(struct files_struct *files, unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	struct fdtable *fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	__clear_open_fd(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if (fd < files->next_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		files->next_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) void put_unused_fd(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	__put_unused_fd(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) EXPORT_SYMBOL(put_unused_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615)  * Install a file pointer in the fd array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617)  * The VFS is full of places where we drop the files lock between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618)  * setting the open_fds bitmap and installing the file in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619)  * array.  At any such point, we are vulnerable to a dup2() race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620)  * installing a file in the array before us.  We need to detect this and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621)  * fput() the struct file we are about to overwrite in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623)  * It should never happen - if we allow dup2() do it, _really_ bad things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624)  * will follow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626)  * NOTE: __fd_install() variant is really, really low-level; don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627)  * use it unless you are forced to by truly lousy API shoved down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628)  * your throat.  'files' *MUST* be either current->files or obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * by get_files_struct(current) done by whoever had given it to you,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  * or really bad things will happen.  Normally you want to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * fd_install() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) void __fd_install(struct files_struct *files, unsigned int fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	rcu_read_lock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (unlikely(files->resize_in_progress)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		rcu_read_unlock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		BUG_ON(fdt->fd[fd] != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		rcu_assign_pointer(fdt->fd[fd], file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	/* coupled with smp_wmb() in expand_fdtable() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	fdt = rcu_dereference_sched(files->fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	BUG_ON(fdt->fd[fd] != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	rcu_assign_pointer(fdt->fd[fd], file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	rcu_read_unlock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659)  * This consumes the "file" refcount, so callers should treat it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660)  * as if they had called fput(file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) void fd_install(unsigned int fd, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	__fd_install(current->files, fd, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) EXPORT_SYMBOL(fd_install);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static struct file *pick_file(struct files_struct *files, unsigned fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct file *file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (fd >= fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	file = fdt->fd[fd];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	rcu_assign_pointer(fdt->fd[fd], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	__put_unused_fd(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  * The same warnings as for __alloc_fd()/__fd_install() apply here...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) int __close_fd(struct files_struct *files, unsigned fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	file = pick_file(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	return filp_close(file, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) EXPORT_SYMBOL(__close_fd); /* for ksys_close() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705)  * __close_range() - Close all file descriptors in a given range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * @fd:     starting file descriptor to close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * @max_fd: last file descriptor to close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * This closes a range of file descriptors. All file descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * from @fd up to and including @max_fd are closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	unsigned int cur_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	struct task_struct *me = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	struct files_struct *cur_fds = me->files, *fds = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (flags & ~CLOSE_RANGE_UNSHARE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	if (fd > max_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	cur_max = files_fdtable(cur_fds)->max_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	/* cap to last valid index into fdtable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	cur_max--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	if (flags & CLOSE_RANGE_UNSHARE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		unsigned int max_unshare_fds = NR_OPEN_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		 * If the requested range is greater than the current maximum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		 * we're closing everything so only copy all file descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		 * beneath the lowest file descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		if (max_fd >= cur_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			max_unshare_fds = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		ret = unshare_fd(CLONE_FILES, max_unshare_fds, &fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		 * We used to share our file descriptor table, and have now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		 * created a private one, make sure we're using it below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			swap(cur_fds, fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	max_fd = min(max_fd, cur_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	while (fd <= max_fd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		file = pick_file(cur_fds, fd++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		filp_close(file, cur_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (fds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		 * We're done closing the files we were supposed to. Time to install
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		 * the new file descriptor table and drop the old one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		task_lock(me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		me->files = cur_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		task_unlock(me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		put_files_struct(fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783)  * variant of __close_fd that gets a ref on the file for later fput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * The caller must ensure that filp_close() called on the file, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * an fput().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) int __close_fd_get_file(unsigned int fd, struct file **res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if (fd >= fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	file = fdt->fd[fd];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	rcu_assign_pointer(fdt->fd[fd], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	__put_unused_fd(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	get_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	*res = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	*res = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) void do_close_on_exec(struct files_struct *files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	/* exec unshares first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	for (i = 0; ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		unsigned long set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		unsigned fd = i * BITS_PER_LONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		if (fd >= fdt->max_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		set = fdt->close_on_exec[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		if (!set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		fdt->close_on_exec[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		for ( ; set ; fd++, set >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			if (!(set & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			file = fdt->fd[fd];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			rcu_assign_pointer(fdt->fd[fd], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			__put_unused_fd(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			filp_close(file, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) static inline struct file *__fget_files_rcu(struct files_struct *files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	unsigned int fd, fmode_t mask, unsigned int refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		struct fdtable *fdt = rcu_dereference_raw(files->fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		struct file __rcu **fdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (unlikely(fd >= fdt->max_fds))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		fdentry = fdt->fd + array_index_nospec(fd, fdt->max_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		file = rcu_dereference_raw(*fdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		if (unlikely(!file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		if (unlikely(file->f_mode & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		 * Ok, we have a file pointer. However, because we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		 * this all locklessly under RCU, we may be racing with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		 * that file being closed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		 * Such a race can take two forms:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		 *  (a) the file ref already went down to zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		 *      and get_file_rcu_many() fails. Just try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		 *      again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		if (unlikely(!get_file_rcu_many(file, refs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		 *  (b) the file table entry has changed under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		 *       Note that we don't need to re-check the 'fdt->fd'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		 *       pointer having changed, because it always goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		 *       hand-in-hand with 'fdt'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		 * If so, we need to put our refs and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		if (unlikely(rcu_dereference_raw(files->fdt) != fdt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		    unlikely(rcu_dereference_raw(*fdentry) != file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			fput_many(file, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		 * Ok, we have a ref to the file, and checked that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		 * still exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) static struct file *__fget_files(struct files_struct *files, unsigned int fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 				 fmode_t mask, unsigned int refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	file = __fget_files_rcu(files, fd, mask, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) static inline struct file *__fget(unsigned int fd, fmode_t mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				  unsigned int refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return __fget_files(current->files, fd, mask, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) struct file *fget_many(unsigned int fd, unsigned int refs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return __fget(fd, FMODE_PATH, refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) struct file *fget(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	return __fget(fd, FMODE_PATH, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) EXPORT_SYMBOL(fget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) struct file *fget_raw(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	return __fget(fd, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) EXPORT_SYMBOL(fget_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) struct file *fget_task(struct task_struct *task, unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct file *file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	task_lock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (task->files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		file = __fget_files(task->files, fd, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	task_unlock(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	return file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952)  * Lightweight file lookup - no refcnt increment if fd table isn't shared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954)  * You can use this instead of fget if you satisfy all of the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955)  * conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956)  * 1) You must call fput_light before exiting the syscall and returning control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957)  *    to userspace (i.e. you cannot remember the returned struct file * after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  *    returning to userspace).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  * 2) You must not call filp_close on the returned struct file * in between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  *    calls to fget_light and fput_light.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  * 3) You must not clone the current task in between the calls to fget_light
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  *    and fput_light.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  * The fput_needed flag returned by fget_light should be passed to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  * corresponding fput_light.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) static unsigned long __fget_light(unsigned int fd, fmode_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	if (atomic_read(&files->count) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		file = __fcheck_files(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		if (!file || unlikely(file->f_mode & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return (unsigned long)file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		file = __fget(fd, mask, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		return FDPUT_FPUT | (unsigned long)file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) unsigned long __fdget(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return __fget_light(fd, FMODE_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) EXPORT_SYMBOL(__fdget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) unsigned long __fdget_raw(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	return __fget_light(fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) unsigned long __fdget_pos(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	unsigned long v = __fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct file *file = (struct file *)(v & ~3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		if (file_count(file) > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			v |= FDPUT_POS_UNLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			mutex_lock(&file->f_pos_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) void __f_unlock_pos(struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	mutex_unlock(&f->f_pos_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  * We only lock f_pos if we have threads or if the file might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  * shared with another process. In both cases we'll have an elevated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  * file count (done either by fdget() or by fork()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) void set_close_on_exec(unsigned int fd, int flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	if (flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		__set_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		__clear_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) bool get_close_on_exec(unsigned int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	res = close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) static int do_dup2(struct files_struct *files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	struct file *file, unsigned fd, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) __releases(&files->file_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct file *tofree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	 * We need to detect attempts to do dup2() over allocated but still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	 * not finished descriptor.  NB: OpenBSD avoids that at the price of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	 * extra work in their equivalent of fget() - they insert struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 * file immediately after grabbing descriptor, mark it larval if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	 * more work (e.g. actual opening) is needed and make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 * fget() treats larval files as absent.  Potentially interesting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	 * but while extra work in fget() is trivial, locking implications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	 * and amount of surgery on open()-related paths in VFS are not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	 * deadlocks in rather amusing ways, AFAICS.  All of that is out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 * scope of POSIX or SUS, since neither considers shared descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	 * tables and this condition does not arise without those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	fdt = files_fdtable(files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	tofree = fdt->fd[fd];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	if (!tofree && fd_is_open(fd, fdt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		goto Ebusy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	get_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	rcu_assign_pointer(fdt->fd[fd], file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	__set_open_fd(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	if (flags & O_CLOEXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		__set_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		__clear_close_on_exec(fd, fdt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (tofree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		filp_close(tofree, files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) Ebusy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int replace_fd(unsigned fd, struct file *file, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		return __close_fd(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	if (fd >= rlimit(RLIMIT_NOFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	err = expand_files(files, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (unlikely(err < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	return do_dup2(files, file, fd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  * __receive_fd() - Install received file into file descriptor table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  * @fd: fd to install into (if negative, a new fd will be allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  * @file: struct file that was received from another process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)  * @ufd: __user pointer to write new fd number to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  * @o_flags: the O_* flags to apply to the new fd entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  * Installs a received file into the file descriptor table, with appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  * checks and count updates. Optionally writes the fd number to userspace, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  * @ufd is non-NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)  * This helper handles its own reference counting of the incoming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)  * struct file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)  * Returns newly install fd or -ve on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) int __receive_fd(int fd, struct file *file, int __user *ufd, unsigned int o_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	int new_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	error = security_file_receive(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		new_fd = get_unused_fd_flags(o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		if (new_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			return new_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		new_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	if (ufd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		error = put_user(new_fd, ufd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 				put_unused_fd(new_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		fd_install(new_fd, get_file(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		error = replace_fd(new_fd, file, o_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	/* Bump the sock usage counts, if any. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	__receive_sock(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	return new_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	int err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if ((flags & ~O_CLOEXEC) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (unlikely(oldfd == newfd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (newfd >= rlimit(RLIMIT_NOFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	err = expand_files(files, newfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	file = fcheck(oldfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (unlikely(!file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		goto Ebadf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	if (unlikely(err < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (err == -EMFILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			goto Ebadf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	return do_dup2(files, file, newfd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) Ebadf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	err = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	return ksys_dup3(oldfd, newfd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (unlikely(newfd == oldfd)) { /* corner case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		struct files_struct *files = current->files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		int retval = oldfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (!fcheck_files(files, oldfd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			retval = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	return ksys_dup3(oldfd, newfd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) SYSCALL_DEFINE1(dup, unsigned int, fildes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	int ret = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	struct file *file = fget_raw(fildes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		ret = get_unused_fd_flags(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			fd_install(ret, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 			fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) int f_dupfd(unsigned int from, struct file *file, unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	if (from >= rlimit(RLIMIT_NOFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	err = alloc_fd(from, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	if (err >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		get_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		fd_install(err, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) int iterate_fd(struct files_struct *files, unsigned n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		int (*f)(const void *, struct file *, unsigned),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		const void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct fdtable *fdt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	if (!files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	spin_lock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		res = f(p, file, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	spin_unlock(&files->file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) EXPORT_SYMBOL(iterate_fd);