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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * memfd_create system call and file sealing support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Code was originally included in shmem.c, and broken out to facilitate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * use by hugetlbfs as well as tmpfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This file is released under the GPL.
^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/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/file.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/khugepaged.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/hugetlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/shmem_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/memfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <uapi/linux/memfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * We need a tag: a new tag would expand every xa_node by 8 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * so reuse a tag which we firmly believe is never set or cleared on tmpfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * or hugetlbfs because they are memory only filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define MEMFD_TAG_PINNED        PAGECACHE_TAG_TOWRITE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define LAST_SCAN               4       /* about 150ms max */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static void memfd_tag_pins(struct xa_state *xas)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	int cache_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	lru_add_drain();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	xas_lock_irq(xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	xas_for_each(xas, page, ULONG_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		cache_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		if (!xa_is_value(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		    PageTransHuge(page) && !PageHuge(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			cache_count = HPAGE_PMD_NR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		if (!xa_is_value(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		    page_count(page) - total_mapcount(page) != cache_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			xas_set_mark(xas, MEMFD_TAG_PINNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		if (cache_count != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			xas_set(xas, page->index + cache_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		latency += cache_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		if (latency < XA_CHECK_SCHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		xas_pause(xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		xas_unlock_irq(xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		xas_lock_irq(xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	xas_unlock_irq(xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * via get_user_pages(), drivers might have some pending I/O without any active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * and see whether it has an elevated ref-count. If so, we tag them and wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * them to be dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * The caller must guarantee that no new user will acquire writable references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * to those pages to avoid races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int memfd_wait_for_pins(struct address_space *mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	XA_STATE(xas, &mapping->i_pages, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int error, scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	memfd_tag_pins(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	for (scan = 0; scan <= LAST_SCAN; scan++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		int latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		int cache_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (!xas_marked(&xas, MEMFD_TAG_PINNED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (!scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			lru_add_drain_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		else if (schedule_timeout_killable((HZ << scan) / 200))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			scan = LAST_SCAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		xas_set(&xas, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		xas_lock_irq(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		xas_for_each_marked(&xas, page, ULONG_MAX, MEMFD_TAG_PINNED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			bool clear = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			cache_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			if (!xa_is_value(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			    PageTransHuge(page) && !PageHuge(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				cache_count = HPAGE_PMD_NR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			if (!xa_is_value(page) && cache_count !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			    page_count(page) - total_mapcount(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				 * On the last scan, we clean up all those tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				 * we inserted; but make a note that we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				 * found pages pinned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 				if (scan == LAST_SCAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 					clear = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			if (clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				xas_clear_mark(&xas, MEMFD_TAG_PINNED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			latency += cache_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			if (latency < XA_CHECK_SCHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			xas_pause(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			xas_unlock_irq(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			xas_lock_irq(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		xas_unlock_irq(&xas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static unsigned int *memfd_file_seals_ptr(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (shmem_file(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return &SHMEM_I(file_inode(file))->seals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #ifdef CONFIG_HUGETLBFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (is_file_hugepages(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return &HUGETLBFS_I(file_inode(file))->seals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define F_ALL_SEALS (F_SEAL_SEAL | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		     F_SEAL_SHRINK | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		     F_SEAL_GROW | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		     F_SEAL_WRITE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		     F_SEAL_FUTURE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int memfd_add_seals(struct file *file, unsigned int seals)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unsigned int *file_seals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	 * SEALING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	 * Sealing allows multiple parties to share a tmpfs or hugetlbfs file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 * but restrict access to a specific subset of file operations. Seals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * can only be added, but never removed. This way, mutually untrusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * parties can share common memory regions with a well-defined policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * A malicious peer can thus never perform unwanted operations on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 * shared object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 * Seals are only supported on special tmpfs or hugetlbfs files and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * always affect the whole underlying inode. Once a seal is set, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * may prevent some kinds of access to the file. Currently, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * following seals are defined:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 *   SEAL_SEAL: Prevent further seals from being set on this file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 *   SEAL_SHRINK: Prevent the file from shrinking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 *   SEAL_GROW: Prevent the file from growing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 *   SEAL_WRITE: Prevent write access to the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * As we don't require any trust relationship between two parties, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * must prevent seals from being removed. Therefore, sealing a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * only adds a given set of seals to the file, it never touches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * existing seals. Furthermore, the "setting seals"-operation can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 * sealed itself, which basically prevents any further seal from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 * added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * Semantics of sealing are only defined on volatile files. Only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * anonymous tmpfs and hugetlbfs files support sealing. More
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * importantly, seals are never written to disk. Therefore, there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * no plan to support it on other file types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!(file->f_mode & FMODE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (seals & ~(unsigned int)F_ALL_SEALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	file_seals = memfd_file_seals_ptr(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (!file_seals) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		goto unlock;
^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) 	if (*file_seals & F_SEAL_SEAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if ((seals & F_SEAL_WRITE) && !(*file_seals & F_SEAL_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		error = mapping_deny_writable(file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		error = memfd_wait_for_pins(file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			mapping_allow_writable(file->f_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	*file_seals |= seals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int memfd_get_seals(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	unsigned int *seals = memfd_file_seals_ptr(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return seals ? *seals : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	long error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	case F_ADD_SEALS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		/* disallow upper 32bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (arg > UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		error = memfd_add_seals(file, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	case F_GET_SEALS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		error = memfd_get_seals(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define MFD_NAME_PREFIX "memfd:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) SYSCALL_DEFINE2(memfd_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		const char __user *, uname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		unsigned int, flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	unsigned int *file_seals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	int fd, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if (!(flags & MFD_HUGETLB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		if (flags & ~(unsigned int)MFD_ALL_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		/* Allow huge page size encoding in flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				(MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			return -EINVAL;
^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) 	/* length includes terminating zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (len > MFD_NAME_MAX_LEN + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	strcpy(name, MFD_NAME_PREFIX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/* terminating-zero may have changed after strnlen_user() returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		goto err_name;
^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) 	fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		error = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (flags & MFD_HUGETLB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		struct user_struct *user = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		file = hugetlb_file_setup(name, 0, VM_NORESERVE, &user,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 					HUGETLB_ANONHUGE_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 					(flags >> MFD_HUGE_SHIFT) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 					MFD_HUGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		file = shmem_file_setup(name, 0, VM_NORESERVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (IS_ERR(file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		error = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		goto err_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	file->f_flags |= O_LARGEFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (flags & MFD_ALLOW_SEALING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		file_seals = memfd_file_seals_ptr(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		*file_seals &= ~F_SEAL_SEAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	fd_install(fd, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err_fd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }