59bd9ded4d780 (Thomas Gleixner 2019-05-28 10:10:12 -0700 1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * linux/fs/namespace.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * (C) Copyright Al Viro 2000, 2001
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7) * Based on code from fs/super.c, copyright Linus Torvalds and others.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) * Heavily rewritten.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) #include <linux/syscalls.h>
d10577a8d86a0 (Al Viro 2011-12-07 13:06:11 -0500 12) #include <linux/export.h>
16f7e0fe2ecc3 (Randy Dunlap 2006-01-11 12:17:46 -0800 13) #include <linux/capability.h>
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 14) #include <linux/mnt_namespace.h>
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 15) #include <linux/user_namespace.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) #include <linux/namei.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17) #include <linux/security.h>
5b825c3af1d8a (Ingo Molnar 2017-02-02 17:54:15 +0100 18) #include <linux/cred.h>
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 19) #include <linux/idr.h>
57f150a58c40c (Rob Landley 2013-09-11 14:26:10 -0700 20) #include <linux/init.h> /* init_rootfs */
d10577a8d86a0 (Al Viro 2011-12-07 13:06:11 -0500 21) #include <linux/fs_struct.h> /* get_fs_root et.al. */
d10577a8d86a0 (Al Viro 2011-12-07 13:06:11 -0500 22) #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 23) #include <linux/file.h>
d10577a8d86a0 (Al Viro 2011-12-07 13:06:11 -0500 24) #include <linux/uaccess.h>
0bb80f240520c (David Howells 2013-04-12 01:50:06 +0100 25) #include <linux/proc_ns.h>
20b4fb4852274 (Linus Torvalds 2013-05-01 17:51:54 -0700 26) #include <linux/magic.h>
57c8a661d95df (Mike Rapoport 2018-10-30 15:09:49 -0700 27) #include <linux/memblock.h>
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 28) #include <linux/proc_fs.h>
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 29) #include <linux/task_work.h>
9164bb4a18dfa (Ingo Molnar 2017-02-04 01:20:53 +0100 30) #include <linux/sched/task.h>
e262e32d6bde0 (David Howells 2018-11-01 23:07:23 +0000 31) #include <uapi/linux/mount.h>
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 32) #include <linux/fs_context.h>
037f11b4752f7 (Al Viro 2019-06-01 18:09:44 -0400 33) #include <linux/shmem_fs.h>
9164bb4a18dfa (Ingo Molnar 2017-02-04 01:20:53 +0100 34)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 35) #include "pnode.h"
948730b0e39fb (Adrian Bunk 2007-07-15 23:41:25 -0700 36) #include "internal.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 38) /* Maximum number of mounts in a mount namespace */
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 39) unsigned int sysctl_mount_max __read_mostly = 100000;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 40)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 41) static unsigned int m_hash_mask __read_mostly;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 42) static unsigned int m_hash_shift __read_mostly;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 43) static unsigned int mp_hash_mask __read_mostly;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 44) static unsigned int mp_hash_shift __read_mostly;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 45)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 46) static __initdata unsigned long mhash_entries;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 47) static int __init set_mhash_entries(char *str)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 48) {
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 49) if (!str)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 50) return 0;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 51) mhash_entries = simple_strtoul(str, &str, 0);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 52) return 1;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 53) }
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 54) __setup("mhash_entries=", set_mhash_entries);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 55)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 56) static __initdata unsigned long mphash_entries;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 57) static int __init set_mphash_entries(char *str)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 58) {
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 59) if (!str)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 60) return 0;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 61) mphash_entries = simple_strtoul(str, &str, 0);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 62) return 1;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 63) }
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 64) __setup("mphash_entries=", set_mphash_entries);
13f14b4d8be22 (Eric Dumazet 2008-02-06 01:37:57 -0800 65)
c7999c3627bc6 (Al Viro 2014-02-27 14:40:10 -0500 66) static u64 event;
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 67) static DEFINE_IDA(mnt_id_ida);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 68) static DEFINE_IDA(mnt_group_ida);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69)
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 70) static struct hlist_head *mount_hashtable __read_mostly;
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 71) static struct hlist_head *mountpoint_hashtable __read_mostly;
e18b890bb0881 (Christoph Lameter 2006-12-06 20:33:20 -0800 72) static struct kmem_cache *mnt_cache __read_mostly;
59aa0da8e232f (Al Viro 2013-09-16 21:34:53 -0400 73) static DECLARE_RWSEM(namespace_sem);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 74) static HLIST_HEAD(unmounted); /* protected by namespace_sem */
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 75) static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 76)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 77) struct mount_kattr {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 78) unsigned int attr_set;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 79) unsigned int attr_clr;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 80) unsigned int propagation;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 81) unsigned int lookup_flags;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 82) bool recurse;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 83) struct user_namespace *mnt_userns;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 84) };
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 85)
f87fd4c2a0c4f (Miklos Szeredi 2006-01-16 22:14:23 -0800 86) /* /sys/fs */
00d2666623368 (Greg Kroah-Hartman 2007-10-29 14:17:23 -0600 87) struct kobject *fs_kobj;
00d2666623368 (Greg Kroah-Hartman 2007-10-29 14:17:23 -0600 88) EXPORT_SYMBOL_GPL(fs_kobj);
f87fd4c2a0c4f (Miklos Szeredi 2006-01-16 22:14:23 -0800 89)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 90) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 91) * vfsmount lock may be taken for read to prevent changes to the
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 92) * vfsmount hash, ie. during mountpoint lookups or walking back
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 93) * up the tree.
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 94) *
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 95) * It should be taken for write in all cases where the vfsmount
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 96) * tree or hash is modified or when a vfsmount structure is modified.
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 97) */
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 98) __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 99)
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 100) static inline void lock_mount_hash(void)
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 101) {
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 102) write_seqlock(&mount_lock);
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 103) }
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 104)
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 105) static inline void unlock_mount_hash(void)
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 106) {
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 107) write_sequnlock(&mount_lock);
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 108) }
d033cb6784c4f (Christian Brauner 2021-01-21 14:19:49 +0100 109)
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 110) static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 111) {
b58fed8b1959d (Ram Pai 2005-11-07 17:16:09 -0500 112) unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
b58fed8b1959d (Ram Pai 2005-11-07 17:16:09 -0500 113) tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 114) tmp = tmp + (tmp >> m_hash_shift);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 115) return &mount_hashtable[tmp & m_hash_mask];
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 116) }
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 117)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 118) static inline struct hlist_head *mp_hash(struct dentry *dentry)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 119) {
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 120) unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 121) tmp = tmp + (tmp >> mp_hash_shift);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 122) return &mountpoint_hashtable[tmp & mp_hash_mask];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 123) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 124)
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 125) static int mnt_alloc_id(struct mount *mnt)
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 126) {
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 127) int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 128)
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 129) if (res < 0)
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 130) return res;
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 131) mnt->mnt_id = res;
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 132) return 0;
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 133) }
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 134)
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 135) static void mnt_free_id(struct mount *mnt)
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 136) {
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 137) ida_free(&mnt_id_ida, mnt->mnt_id);
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 138) }
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 139)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 140) /*
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 141) * Allocate a new peer group ID
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 142) */
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 143) static int mnt_alloc_group_id(struct mount *mnt)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 144) {
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 145) int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
f21f62208a6f6 (Al Viro 2009-06-24 03:12:00 -0400 146)
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 147) if (res < 0)
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 148) return res;
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 149) mnt->mnt_group_id = res;
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 150) return 0;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 151) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 152)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 153) /*
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 154) * Release a peer group ID
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 155) */
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 156) void mnt_release_group_id(struct mount *mnt)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 157) {
169b480e4c8b7 (Matthew Wilcox 2018-06-11 12:31:36 -0400 158) ida_free(&mnt_group_ida, mnt->mnt_group_id);
15169fe784a98 (Al Viro 2011-11-25 00:50:41 -0500 159) mnt->mnt_group_id = 0;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 160) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 161)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 162) /*
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 163) * vfsmount lock must be held for read
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 164) */
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 165) static inline void mnt_add_count(struct mount *mnt, int n)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 166) {
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 167) #ifdef CONFIG_SMP
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 168) this_cpu_add(mnt->mnt_pcp->mnt_count, n);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 169) #else
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 170) preempt_disable();
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 171) mnt->mnt_count += n;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 172) preempt_enable();
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 173) #endif
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 174) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 175)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 176) /*
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 177) * vfsmount lock must be held for write
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 178) */
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 179) int mnt_get_count(struct mount *mnt)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 180) {
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 181) #ifdef CONFIG_SMP
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 182) int count = 0;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 183) int cpu;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 184)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 185) for_each_possible_cpu(cpu) {
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 186) count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 187) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 188)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 189) return count;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 190) #else
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 191) return mnt->mnt_count;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 192) #endif
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 193) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 194)
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 195) static struct mount *alloc_vfsmnt(const char *name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 196) {
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 197) struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 198) if (mnt) {
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 199) int err;
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 200)
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 201) err = mnt_alloc_id(mnt);
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 202) if (err)
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 203) goto out_free_cache;
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 204)
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 205) if (name) {
fcc139ae227b9 (Andrzej Hajda 2015-02-13 14:36:41 -0800 206) mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 207) if (!mnt->mnt_devname)
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 208) goto out_free_id;
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 209) }
73cd49ecdde92 (Miklos Szeredi 2008-03-26 22:11:34 +0100 210)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 211) #ifdef CONFIG_SMP
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 212) mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 213) if (!mnt->mnt_pcp)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 214) goto out_free_devname;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 215)
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 216) this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 217) #else
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 218) mnt->mnt_count = 1;
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 219) mnt->mnt_writers = 0;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 220) #endif
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 221)
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 222) INIT_HLIST_NODE(&mnt->mnt_hash);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 223) INIT_LIST_HEAD(&mnt->mnt_child);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 224) INIT_LIST_HEAD(&mnt->mnt_mounts);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 225) INIT_LIST_HEAD(&mnt->mnt_list);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 226) INIT_LIST_HEAD(&mnt->mnt_expire);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 227) INIT_LIST_HEAD(&mnt->mnt_share);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 228) INIT_LIST_HEAD(&mnt->mnt_slave_list);
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 229) INIT_LIST_HEAD(&mnt->mnt_slave);
0a5eb7c818992 (Eric W. Biederman 2013-09-22 19:37:01 -0700 230) INIT_HLIST_NODE(&mnt->mnt_mp_list);
99b19d16471e9 (Eric W. Biederman 2016-10-24 16:16:13 -0500 231) INIT_LIST_HEAD(&mnt->mnt_umounting);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 232) INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 233) mnt->mnt.mnt_userns = &init_user_ns;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 234) }
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 235) return mnt;
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 236)
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 237) #ifdef CONFIG_SMP
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 238) out_free_devname:
fcc139ae227b9 (Andrzej Hajda 2015-02-13 14:36:41 -0800 239) kfree_const(mnt->mnt_devname);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 240) #endif
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 241) out_free_id:
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 242) mnt_free_id(mnt);
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 243) out_free_cache:
c63181e6b6df8 (Al Viro 2011-11-25 02:35:16 -0500 244) kmem_cache_free(mnt_cache, mnt);
88b387824fdae (Li Zefan 2008-07-21 18:06:36 +0800 245) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 246) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 247)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 248) /*
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 249) * Most r/o checks on a fs are for operations that take
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 250) * discrete amounts of time, like a write() or unlink().
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 251) * We must keep track of when those operations start
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 252) * (for permission checks) and when they end, so that
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 253) * we can determine when writes are able to occur to
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 254) * a filesystem.
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 255) */
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 256) /*
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 257) * __mnt_is_readonly: check whether a mount is read-only
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 258) * @mnt: the mount to check for its write status
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 259) *
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 260) * This shouldn't be used directly ouside of the VFS.
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 261) * It does not guarantee that the filesystem will stay
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 262) * r/w, just that it is right *now*. This can not and
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 263) * should not be used in place of IS_RDONLY(inode).
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 264) * mnt_want/drop_write() will _keep_ the filesystem
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 265) * r/w.
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 266) */
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 267) bool __mnt_is_readonly(struct vfsmount *mnt)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 268) {
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 269) return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 270) }
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 271) EXPORT_SYMBOL_GPL(__mnt_is_readonly);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 272)
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 273) static inline void mnt_inc_writers(struct mount *mnt)
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 274) {
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 275) #ifdef CONFIG_SMP
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 276) this_cpu_inc(mnt->mnt_pcp->mnt_writers);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 277) #else
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 278) mnt->mnt_writers++;
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 279) #endif
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 280) }
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 281)
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 282) static inline void mnt_dec_writers(struct mount *mnt)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 283) {
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 284) #ifdef CONFIG_SMP
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 285) this_cpu_dec(mnt->mnt_pcp->mnt_writers);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 286) #else
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 287) mnt->mnt_writers--;
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 288) #endif
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 289) }
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 290)
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 291) static unsigned int mnt_get_writers(struct mount *mnt)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 292) {
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 293) #ifdef CONFIG_SMP
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 294) unsigned int count = 0;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 295) int cpu;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 296)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 297) for_each_possible_cpu(cpu) {
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 298) count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 299) }
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 300)
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 301) return count;
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 302) #else
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 303) return mnt->mnt_writers;
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 304) #endif
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 305) }
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 306)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 307) static int mnt_is_readonly(struct vfsmount *mnt)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 308) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 309) if (mnt->mnt_sb->s_readonly_remount)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 310) return 1;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 311) /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 312) smp_rmb();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 313) return __mnt_is_readonly(mnt);
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 314) }
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 315)
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 316) /*
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 317) * Most r/o & frozen checks on a fs are for operations that take discrete
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 318) * amounts of time, like a write() or unlink(). We must keep track of when
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 319) * those operations start (for permission checks) and when they end, so that we
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 320) * can determine when writes are able to occur to a filesystem.
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 321) */
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 322) /**
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 323) * __mnt_want_write - get write access to a mount without freeze protection
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 324) * @m: the mount on which to take a write
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 325) *
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 326) * This tells the low-level filesystem that a write is about to be performed to
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 327) * it, and makes sure that writes are allowed (mnt it read-write) before
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 328) * returning success. This operation does not protect against filesystem being
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 329) * frozen. When the write operation is finished, __mnt_drop_write() must be
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 330) * called. This is effectively a refcount.
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 331) */
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 332) int __mnt_want_write(struct vfsmount *m)
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 333) {
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 334) struct mount *mnt = real_mount(m);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 335) int ret = 0;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 336)
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 337) preempt_disable();
c6653a838b1b2 (Nicholas Piggin 2011-01-07 17:50:10 +1100 338) mnt_inc_writers(mnt);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 339) /*
c6653a838b1b2 (Nicholas Piggin 2011-01-07 17:50:10 +1100 340) * The store to mnt_inc_writers must be visible before we pass
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 341) * MNT_WRITE_HOLD loop below, so that the slowpath can see our
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 342) * incremented count after it has set MNT_WRITE_HOLD.
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 343) */
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 344) smp_mb();
6aa7de059173a (Mark Rutland 2017-10-23 14:07:29 -0700 345) while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 346) cpu_relax();
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 347) /*
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 348) * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 349) * be set to match its requirements. So we must not load that until
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 350) * MNT_WRITE_HOLD is cleared.
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 351) */
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 352) smp_rmb();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 353) if (mnt_is_readonly(m)) {
c6653a838b1b2 (Nicholas Piggin 2011-01-07 17:50:10 +1100 354) mnt_dec_writers(mnt);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 355) ret = -EROFS;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 356) }
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 357) preempt_enable();
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 358)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 359) return ret;
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 360) }
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 361)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 362) /**
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 363) * mnt_want_write - get write access to a mount
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 364) * @m: the mount on which to take a write
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 365) *
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 366) * This tells the low-level filesystem that a write is about to be performed to
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 367) * it, and makes sure that writes are allowed (mount is read-write, filesystem
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 368) * is not frozen) before returning success. When the write operation is
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 369) * finished, mnt_drop_write() must be called. This is effectively a refcount.
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 370) */
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 371) int mnt_want_write(struct vfsmount *m)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 372) {
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 373) int ret;
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 374)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 375) sb_start_write(m->mnt_sb);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 376) ret = __mnt_want_write(m);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 377) if (ret)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 378) sb_end_write(m->mnt_sb);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 379) return ret;
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 380) }
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 381) EXPORT_SYMBOL_GPL(mnt_want_write);
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 382)
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 383) /**
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 384) * __mnt_want_write_file - get write access to a file's mount
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 385) * @file: the file who's mount on which to take a write
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 386) *
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 387) * This is like __mnt_want_write, but if the file is already open for writing it
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 388) * skips incrementing mnt_writers (since the open file already has a reference)
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 389) * and instead only does the check for emergency r/o remounts. This must be
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 390) * paired with __mnt_drop_write_file.
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 391) */
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 392) int __mnt_want_write_file(struct file *file)
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 393) {
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 394) if (file->f_mode & FMODE_WRITER) {
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 395) /*
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 396) * Superblock may have become readonly while there are still
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 397) * writable fd's, e.g. due to a fs error with errors=remount-ro
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 398) */
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 399) if (__mnt_is_readonly(file->f_path.mnt))
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 400) return -EROFS;
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 401) return 0;
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 402) }
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 403) return __mnt_want_write(file->f_path.mnt);
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 404) }
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 405)
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 406) /**
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 407) * mnt_want_write_file - get write access to a file's mount
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 408) * @file: the file who's mount on which to take a write
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 409) *
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 410) * This is like mnt_want_write, but if the file is already open for writing it
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 411) * skips incrementing mnt_writers (since the open file already has a reference)
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 412) * and instead only does the freeze protection and the check for emergency r/o
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 413) * remounts. This must be paired with mnt_drop_write_file.
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 414) */
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 415) int mnt_want_write_file(struct file *file)
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 416) {
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 417) int ret;
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 418)
a6795a585929d (Miklos Szeredi 2018-07-18 15:44:43 +0200 419) sb_start_write(file_inode(file)->i_sb);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 420) ret = __mnt_want_write_file(file);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 421) if (ret)
a6795a585929d (Miklos Szeredi 2018-07-18 15:44:43 +0200 422) sb_end_write(file_inode(file)->i_sb);
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 423) return ret;
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 424) }
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 425) EXPORT_SYMBOL_GPL(mnt_want_write_file);
96029c4e09ccb (Nicholas Piggin 2009-04-26 20:25:55 +1000 426)
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 427) /**
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 428) * __mnt_drop_write - give up write access to a mount
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 429) * @mnt: the mount on which to give up write access
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 430) *
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 431) * Tells the low-level filesystem that we are done
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 432) * performing writes to it. Must be matched with
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 433) * __mnt_want_write() call above.
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 434) */
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 435) void __mnt_drop_write(struct vfsmount *mnt)
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 436) {
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 437) preempt_disable();
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 438) mnt_dec_writers(real_mount(mnt));
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 439) preempt_enable();
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 440) }
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 441)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 442) /**
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 443) * mnt_drop_write - give up write access to a mount
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 444) * @mnt: the mount on which to give up write access
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 445) *
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 446) * Tells the low-level filesystem that we are done performing writes to it and
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 447) * also allows filesystem to be frozen again. Must be matched with
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 448) * mnt_want_write() call above.
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 449) */
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 450) void mnt_drop_write(struct vfsmount *mnt)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 451) {
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 452) __mnt_drop_write(mnt);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 453) sb_end_write(mnt->mnt_sb);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 454) }
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 455) EXPORT_SYMBOL_GPL(mnt_drop_write);
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 456)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 457) void __mnt_drop_write_file(struct file *file)
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 458) {
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 459) if (!(file->f_mode & FMODE_WRITER))
14e43bf435612 (Eric Biggers 2020-09-22 09:44:18 -0700 460) __mnt_drop_write(file->f_path.mnt);
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 461) }
eb04c28288bb0 (Jan Kara 2012-06-12 16:20:35 +0200 462)
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 463) void mnt_drop_write_file(struct file *file)
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 464) {
a6795a585929d (Miklos Szeredi 2018-07-18 15:44:43 +0200 465) __mnt_drop_write_file(file);
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 466) sb_end_write(file_inode(file)->i_sb);
7c6893e3c9abf (Miklos Szeredi 2017-09-05 12:53:12 +0200 467) }
2a79f17e4a641 (Al Viro 2011-12-09 08:06:57 -0500 468) EXPORT_SYMBOL(mnt_drop_write_file);
2a79f17e4a641 (Al Viro 2011-12-09 08:06:57 -0500 469)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 470) static inline int mnt_hold_writers(struct mount *mnt)
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 471) {
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 472) mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 473) /*
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 474) * After storing MNT_WRITE_HOLD, we'll read the counters. This store
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 475) * should be visible before we do.
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 476) */
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 477) smp_mb();
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 478)
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 479) /*
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 480) * With writers on hold, if this value is zero, then there are
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 481) * definitely no active writers (although held writers may subsequently
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 482) * increment the count, they'll have to wait, and decrement it after
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 483) * seeing MNT_READONLY).
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 484) *
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 485) * It is OK to have counter incremented on one CPU and decremented on
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 486) * another: the sum will add up correctly. The danger would be when we
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 487) * sum up each counter, if we read a counter before it is incremented,
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 488) * but then read another CPU's count which it has been subsequently
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 489) * decremented from -- we would see more decrements than we should.
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 490) * MNT_WRITE_HOLD protects against this scenario, because
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 491) * mnt_want_write first increments count, then smp_mb, then spins on
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 492) * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 493) * we're counting up here.
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 494) */
c6653a838b1b2 (Nicholas Piggin 2011-01-07 17:50:10 +1100 495) if (mnt_get_writers(mnt) > 0)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 496) return -EBUSY;
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 497)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 498) return 0;
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 499) }
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 500)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 501) static inline void mnt_unhold_writers(struct mount *mnt)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 502) {
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 503) /*
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 504) * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 505) * that become unheld will see MNT_READONLY.
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 506) */
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 507) smp_wmb();
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 508) mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 509) }
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 510)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 511) static int mnt_make_readonly(struct mount *mnt)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 512) {
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 513) int ret;
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 514)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 515) ret = mnt_hold_writers(mnt);
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 516) if (!ret)
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 517) mnt->mnt.mnt_flags |= MNT_READONLY;
fbdc2f6c40f65 (Christian Brauner 2021-01-21 14:19:51 +0100 518) mnt_unhold_writers(mnt);
3d733633a6330 (Dave Hansen 2008-02-15 14:37:59 -0800 519) return ret;
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 520) }
8366025eb80df (Dave Hansen 2008-02-15 14:37:30 -0800 521)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 522) int sb_prepare_remount_readonly(struct super_block *sb)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 523) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 524) struct mount *mnt;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 525) int err = 0;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 526)
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 527) /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 528) if (atomic_long_read(&sb->s_remove_count))
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 529) return -EBUSY;
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 530)
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 531) lock_mount_hash();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 532) list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 533) if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 534) mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 535) smp_mb();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 536) if (mnt_get_writers(mnt) > 0) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 537) err = -EBUSY;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 538) break;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 539) }
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 540) }
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 541) }
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 542) if (!err && atomic_long_read(&sb->s_remove_count))
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 543) err = -EBUSY;
8e8b87964bc8d (Miklos Szeredi 2011-11-21 12:11:33 +0100 544)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 545) if (!err) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 546) sb->s_readonly_remount = 1;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 547) smp_wmb();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 548) }
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 549) list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 550) if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 551) mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 552) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 553) unlock_mount_hash();
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 554)
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 555) return err;
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 556) }
4ed5e82fe77f4 (Miklos Szeredi 2011-11-21 12:11:31 +0100 557)
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 558) static void free_vfsmnt(struct mount *mnt)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 559) {
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 560) struct user_namespace *mnt_userns;
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 561)
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 562) mnt_userns = mnt_user_ns(&mnt->mnt);
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 563) if (mnt_userns != &init_user_ns)
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 564) put_user_ns(mnt_userns);
fcc139ae227b9 (Andrzej Hajda 2015-02-13 14:36:41 -0800 565) kfree_const(mnt->mnt_devname);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 566) #ifdef CONFIG_SMP
68e8a9feab251 (Al Viro 2011-11-24 22:53:09 -0500 567) free_percpu(mnt->mnt_pcp);
d3ef3d7351ccf (Nicholas Piggin 2009-04-26 20:25:54 +1000 568) #endif
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 569) kmem_cache_free(mnt_cache, mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 570) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 571)
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 572) static void delayed_free_vfsmnt(struct rcu_head *head)
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 573) {
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 574) free_vfsmnt(container_of(head, struct mount, mnt_rcu));
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 575) }
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 576)
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 577) /* call under rcu_read_lock */
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 578) int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 579) {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 580) struct mount *mnt;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 581) if (read_seqretry(&mount_lock, seq))
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 582) return 1;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 583) if (bastard == NULL)
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 584) return 0;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 585) mnt = real_mount(bastard);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 586) mnt_add_count(mnt, 1);
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 587) smp_mb(); // see mntput_no_expire()
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 588) if (likely(!read_seqretry(&mount_lock, seq)))
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 589) return 0;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 590) if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 591) mnt_add_count(mnt, -1);
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 592) return 1;
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 593) }
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 594) lock_mount_hash();
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 595) if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 596) mnt_add_count(mnt, -1);
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 597) unlock_mount_hash();
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 598) return 1;
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 599) }
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 600) unlock_mount_hash();
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 601) /* caller will mntput() */
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 602) return -1;
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 603) }
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 604)
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 605) /* call under rcu_read_lock */
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 606) bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 607) {
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 608) int res = __legitimize_mnt(bastard, seq);
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 609) if (likely(!res))
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 610) return true;
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 611) if (unlikely(res < 0)) {
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 612) rcu_read_unlock();
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 613) mntput(bastard);
294d71ff2f020 (Al Viro 2015-05-08 11:43:53 -0400 614) rcu_read_lock();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 615) }
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 616) return false;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 617) }
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 618)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 619) /*
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 620) * find the first mount at @dentry on vfsmount @mnt.
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 621) * call under rcu_read_lock()
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 622) */
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 623) struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 624) {
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 625) struct hlist_head *head = m_hash(mnt, dentry);
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 626) struct mount *p;
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 627)
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 628) hlist_for_each_entry_rcu(p, head, mnt_hash)
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 629) if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 630) return p;
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 631) return NULL;
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 632) }
474279dc0f774 (Al Viro 2013-10-01 16:11:26 -0400 633)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 634) /*
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 635) * lookup_mnt - Return the first child mount mounted at path
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 636) *
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 637) * "First" means first mounted chronologically. If you create the
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 638) * following mounts:
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 639) *
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 640) * mount /dev/sda1 /mnt
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 641) * mount /dev/sda2 /mnt
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 642) * mount /dev/sda3 /mnt
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 643) *
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 644) * Then lookup_mnt() on the base /mnt dentry in the root mount will
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 645) * return successively the root dentry and vfsmount of /dev/sda1, then
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 646) * /dev/sda2, then /dev/sda3, then NULL.
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 647) *
f015f1267b23d (David Howells 2012-06-25 12:55:28 +0100 648) * lookup_mnt takes a reference to the found vfsmount.
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 649) */
ca71cf71eeda0 (Al Viro 2016-11-20 19:45:28 -0500 650) struct vfsmount *lookup_mnt(const struct path *path)
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 651) {
c71053659e3bb (Al Viro 2011-11-24 18:22:03 -0500 652) struct mount *child_mnt;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 653) struct vfsmount *m;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 654) unsigned seq;
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 655)
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 656) rcu_read_lock();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 657) do {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 658) seq = read_seqbegin(&mount_lock);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 659) child_mnt = __lookup_mnt(path->mnt, path->dentry);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 660) m = child_mnt ? &child_mnt->mnt : NULL;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 661) } while (!legitimize_mnt(m, seq));
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 662) rcu_read_unlock();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 663) return m;
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 664) }
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 665)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 666) static inline void lock_ns_list(struct mnt_namespace *ns)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 667) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 668) spin_lock(&ns->ns_lock);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 669) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 670)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 671) static inline void unlock_ns_list(struct mnt_namespace *ns)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 672) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 673) spin_unlock(&ns->ns_lock);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 674) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 675)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 676) static inline bool mnt_is_cursor(struct mount *mnt)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 677) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 678) return mnt->mnt.mnt_flags & MNT_CURSOR;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 679) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 680)
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 681) /*
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 682) * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 683) * current mount namespace.
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 684) *
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 685) * The common case is dentries are not mountpoints at all and that
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 686) * test is handled inline. For the slow case when we are actually
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 687) * dealing with a mountpoint of some kind, walk through all of the
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 688) * mounts in the current mount namespace and test to see if the dentry
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 689) * is a mountpoint.
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 690) *
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 691) * The mount_hashtable is not usable in the context because we
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 692) * need to identify all mounts that may be in the current mount
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 693) * namespace not just a mount that happens to have some specified
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 694) * parent mount.
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 695) */
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 696) bool __is_local_mountpoint(struct dentry *dentry)
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 697) {
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 698) struct mnt_namespace *ns = current->nsproxy->mnt_ns;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 699) struct mount *mnt;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 700) bool is_covered = false;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 701)
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 702) down_read(&namespace_sem);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 703) lock_ns_list(ns);
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 704) list_for_each_entry(mnt, &ns->list, mnt_list) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 705) if (mnt_is_cursor(mnt))
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 706) continue;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 707) is_covered = (mnt->mnt_mountpoint == dentry);
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 708) if (is_covered)
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 709) break;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 710) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 711) unlock_ns_list(ns);
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 712) up_read(&namespace_sem);
5ad05cc8e0463 (Nikolay Borisov 2020-03-04 18:12:45 +0200 713)
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 714) return is_covered;
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 715) }
7af1364ffa64d (Eric W. Biederman 2013-10-04 19:15:13 -0700 716)
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 717) static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 718) {
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 719) struct hlist_head *chain = mp_hash(dentry);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 720) struct mountpoint *mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 721)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 722) hlist_for_each_entry(mp, chain, m_hash) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 723) if (mp->m_dentry == dentry) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 724) mp->m_count++;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 725) return mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 726) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 727) }
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 728) return NULL;
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 729) }
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 730)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 731) static struct mountpoint *get_mountpoint(struct dentry *dentry)
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 732) {
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 733) struct mountpoint *mp, *new = NULL;
e2dfa93546427 (Eric W. Biederman 2014-02-24 17:32:34 -0800 734) int ret;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 735)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 736) if (d_mountpoint(dentry)) {
1e9c75fb9c47a (Benjamin Coddington 2018-10-03 10:18:33 -0400 737) /* might be worth a WARN_ON() */
1e9c75fb9c47a (Benjamin Coddington 2018-10-03 10:18:33 -0400 738) if (d_unlinked(dentry))
1e9c75fb9c47a (Benjamin Coddington 2018-10-03 10:18:33 -0400 739) return ERR_PTR(-ENOENT);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 740) mountpoint:
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 741) read_seqlock_excl(&mount_lock);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 742) mp = lookup_mountpoint(dentry);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 743) read_sequnlock_excl(&mount_lock);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 744) if (mp)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 745) goto done;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 746) }
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 747)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 748) if (!new)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 749) new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 750) if (!new)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 751) return ERR_PTR(-ENOMEM);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 752)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 753)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 754) /* Exactly one processes may set d_mounted */
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 755) ret = d_set_mounted(dentry);
eed810076685c (Miklos Szeredi 2013-09-05 14:39:11 +0200 756)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 757) /* Someone else set d_mounted? */
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 758) if (ret == -EBUSY)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 759) goto mountpoint;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 760)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 761) /* The dentry is not available as a mountpoint? */
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 762) mp = ERR_PTR(ret);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 763) if (ret)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 764) goto done;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 765)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 766) /* Add the new mountpoint to the hash table */
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 767) read_seqlock_excl(&mount_lock);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 768) new->m_dentry = dget(dentry);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 769) new->m_count = 1;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 770) hlist_add_head(&new->m_hash, mp_hash(dentry));
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 771) INIT_HLIST_HEAD(&new->m_list);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 772) read_sequnlock_excl(&mount_lock);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 773)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 774) mp = new;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 775) new = NULL;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 776) done:
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 777) kfree(new);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 778) return mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 779) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 780)
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 781) /*
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 782) * vfsmount lock must be held. Additionally, the caller is responsible
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 783) * for serializing calls for given disposal list.
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 784) */
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 785) static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 786) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 787) if (!--mp->m_count) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 788) struct dentry *dentry = mp->m_dentry;
0a5eb7c818992 (Eric W. Biederman 2013-09-22 19:37:01 -0700 789) BUG_ON(!hlist_empty(&mp->m_list));
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 790) spin_lock(&dentry->d_lock);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 791) dentry->d_flags &= ~DCACHE_MOUNTED;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 792) spin_unlock(&dentry->d_lock);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 793) dput_to_list(dentry, list);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 794) hlist_del(&mp->m_hash);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 795) kfree(mp);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 796) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 797) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 798)
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 799) /* called with namespace_lock and vfsmount lock */
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 800) static void put_mountpoint(struct mountpoint *mp)
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 801) {
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 802) __put_mountpoint(mp, &ex_mountpoints);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 803) }
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 804)
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 805) static inline int check_mnt(struct mount *mnt)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 806) {
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 807) return mnt->mnt_ns == current->nsproxy->mnt_ns;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 808) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 809)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 810) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 811) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 812) */
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 813) static void touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 814) {
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 815) if (ns) {
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 816) ns->event = ++event;
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 817) wake_up_interruptible(&ns->poll);
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 818) }
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 819) }
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 820)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 821) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 822) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 823) */
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 824) static void __touch_mnt_namespace(struct mnt_namespace *ns)
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 825) {
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 826) if (ns && ns->event != event) {
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 827) ns->event = event;
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 828) wake_up_interruptible(&ns->poll);
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 829) }
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 830) }
5addc5dd8836a (Al Viro 2005-11-07 17:15:49 -0500 831)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 832) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 833) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 834) */
e4e59906cfe4e (Al Viro 2019-06-29 12:58:42 -0400 835) static struct mountpoint *unhash_mnt(struct mount *mnt)
419148da6e76d (Al Viro 2011-11-24 19:41:16 -0500 836) {
e4e59906cfe4e (Al Viro 2019-06-29 12:58:42 -0400 837) struct mountpoint *mp;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 838) mnt->mnt_parent = mnt;
a73324da7af40 (Al Viro 2011-11-24 22:25:07 -0500 839) mnt->mnt_mountpoint = mnt->mnt.mnt_root;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 840) list_del_init(&mnt->mnt_child);
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 841) hlist_del_init_rcu(&mnt->mnt_hash);
0a5eb7c818992 (Eric W. Biederman 2013-09-22 19:37:01 -0700 842) hlist_del_init(&mnt->mnt_mp_list);
e4e59906cfe4e (Al Viro 2019-06-29 12:58:42 -0400 843) mp = mnt->mnt_mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 844) mnt->mnt_mp = NULL;
e4e59906cfe4e (Al Viro 2019-06-29 12:58:42 -0400 845) return mp;
7bdb11de8ee4f (Eric W. Biederman 2014-12-29 13:03:41 -0600 846) }
7bdb11de8ee4f (Eric W. Biederman 2014-12-29 13:03:41 -0600 847)
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 848) /*
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 849) * vfsmount lock must be held for write
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 850) */
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 851) static void umount_mnt(struct mount *mnt)
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 852) {
e4e59906cfe4e (Al Viro 2019-06-29 12:58:42 -0400 853) put_mountpoint(unhash_mnt(mnt));
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 854) }
6a46c5735c291 (Eric W. Biederman 2015-01-15 22:58:33 -0600 855)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 856) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 857) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 858) */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 859) void mnt_set_mountpoint(struct mount *mnt,
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 860) struct mountpoint *mp,
44d964d609c7c (Al Viro 2011-11-24 21:28:22 -0500 861) struct mount *child_mnt)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 862) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 863) mp->m_count++;
3a2393d71d77b (Al Viro 2011-11-25 03:19:09 -0500 864) mnt_add_count(mnt, 1); /* essentially, that's mntget */
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 865) child_mnt->mnt_mountpoint = mp->m_dentry;
3a2393d71d77b (Al Viro 2011-11-25 03:19:09 -0500 866) child_mnt->mnt_parent = mnt;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 867) child_mnt->mnt_mp = mp;
0a5eb7c818992 (Eric W. Biederman 2013-09-22 19:37:01 -0700 868) hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 869) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 870)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 871) static void __attach_mnt(struct mount *mnt, struct mount *parent)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 872) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 873) hlist_add_head_rcu(&mnt->mnt_hash,
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 874) m_hash(&parent->mnt, mnt->mnt_mountpoint));
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 875) list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 876) }
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 877)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 878) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 879) * vfsmount lock must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 880) */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 881) static void attach_mnt(struct mount *mnt,
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 882) struct mount *parent,
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 883) struct mountpoint *mp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 884) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 885) mnt_set_mountpoint(parent, mp, mnt);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 886) __attach_mnt(mnt, parent);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 887) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 888)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 889) void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
12a5b5294cb18 (Al Viro 2014-08-10 03:44:55 -0400 890) {
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 891) struct mountpoint *old_mp = mnt->mnt_mp;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 892) struct mount *old_parent = mnt->mnt_parent;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 893)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 894) list_del_init(&mnt->mnt_child);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 895) hlist_del_init(&mnt->mnt_mp_list);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 896) hlist_del_init_rcu(&mnt->mnt_hash);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 897)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 898) attach_mnt(mnt, parent, mp);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 899)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 900) put_mountpoint(old_mp);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 901) mnt_add_count(old_parent, -1);
12a5b5294cb18 (Al Viro 2014-08-10 03:44:55 -0400 902) }
12a5b5294cb18 (Al Viro 2014-08-10 03:44:55 -0400 903)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 904) /*
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 905) * vfsmount lock must be held for write
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 906) */
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 907) static void commit_tree(struct mount *mnt)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 908) {
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 909) struct mount *parent = mnt->mnt_parent;
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 910) struct mount *m;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 911) LIST_HEAD(head);
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 912) struct mnt_namespace *n = parent->mnt_ns;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 913)
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 914) BUG_ON(parent == mnt);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 915)
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 916) list_add_tail(&head, &mnt->mnt_list);
f7a99c5b7c8bd (Al Viro 2012-06-09 00:59:08 -0400 917) list_for_each_entry(m, &head, mnt_list)
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 918) m->mnt_ns = n;
f03c65993b98e (Al Viro 2011-01-14 22:30:21 -0500 919)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 920) list_splice(&head, n->list.prev);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 921)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 922) n->mounts += n->pending_mounts;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 923) n->pending_mounts = 0;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 924)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 925) __attach_mnt(mnt, parent);
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 926) touch_mnt_namespace(n);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 927) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 928)
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 929) static struct mount *next_mnt(struct mount *p, struct mount *root)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 930) {
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 931) struct list_head *next = p->mnt_mounts.next;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 932) if (next == &p->mnt_mounts) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 933) while (1) {
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 934) if (p == root)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 935) return NULL;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 936) next = p->mnt_child.next;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 937) if (next != &p->mnt_parent->mnt_mounts)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 938) break;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 939) p = p->mnt_parent;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 940) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 941) }
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 942) return list_entry(next, struct mount, mnt_child);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 943) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 944)
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 945) static struct mount *skip_mnt_tree(struct mount *p)
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 946) {
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 947) struct list_head *prev = p->mnt_mounts.prev;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 948) while (prev != &p->mnt_mounts) {
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 949) p = list_entry(prev, struct mount, mnt_child);
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 950) prev = p->mnt_mounts.prev;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 951) }
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 952) return p;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 953) }
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 954)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 955) /**
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 956) * vfs_create_mount - Create a mount for a configured superblock
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 957) * @fc: The configuration context with the superblock attached
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 958) *
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 959) * Create a mount to an already configured superblock. If necessary, the
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 960) * caller should invoke vfs_get_tree() before calling this.
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 961) *
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 962) * Note that this does not attach the mount to anything.
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 963) */
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 964) struct vfsmount *vfs_create_mount(struct fs_context *fc)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 965) {
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 966) struct mount *mnt;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 967)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 968) if (!fc->root)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 969) return ERR_PTR(-EINVAL);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 970)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 971) mnt = alloc_vfsmnt(fc->source ?: "none");
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 972) if (!mnt)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 973) return ERR_PTR(-ENOMEM);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 974)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 975) if (fc->sb_flags & SB_KERNMOUNT)
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 976) mnt->mnt.mnt_flags = MNT_INTERNAL;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 977)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 978) atomic_inc(&fc->root->d_sb->s_active);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 979) mnt->mnt.mnt_sb = fc->root->d_sb;
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 980) mnt->mnt.mnt_root = dget(fc->root);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 981) mnt->mnt_mountpoint = mnt->mnt.mnt_root;
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 982) mnt->mnt_parent = mnt;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 983)
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 984) lock_mount_hash();
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 985) list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 986) unlock_mount_hash();
b105e270b4e94 (Al Viro 2011-11-24 20:38:33 -0500 987) return &mnt->mnt;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 988) }
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 989) EXPORT_SYMBOL(vfs_create_mount);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 990)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 991) struct vfsmount *fc_mount(struct fs_context *fc)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 992) {
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 993) int err = vfs_get_tree(fc);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 994) if (!err) {
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 995) up_write(&fc->root->d_sb->s_umount);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 996) return vfs_create_mount(fc);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 997) }
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 998) return ERR_PTR(err);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 999) }
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1000) EXPORT_SYMBOL(fc_mount);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1001)
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1002) struct vfsmount *vfs_kern_mount(struct file_system_type *type,
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1003) int flags, const char *name,
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1004) void *data)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1005) {
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1006) struct fs_context *fc;
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1007) struct vfsmount *mnt;
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1008) int ret = 0;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1009)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1010) if (!type)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 1011) return ERR_PTR(-EINVAL);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1012)
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1013) fc = fs_context_for_mount(type, flags);
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1014) if (IS_ERR(fc))
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1015) return ERR_CAST(fc);
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1016)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 1017) if (name)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 1018) ret = vfs_parse_fs_string(fc, "source",
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 1019) name, strlen(name));
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1020) if (!ret)
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1021) ret = parse_monolithic_mount_data(fc, data);
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1022) if (!ret)
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1023) mnt = fc_mount(fc);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1024) else
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1025) mnt = ERR_PTR(ret);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1026)
9bc61ab18b1d4 (David Howells 2018-11-04 03:19:03 -0500 1027) put_fs_context(fc);
8f2918898eb5f (Al Viro 2018-11-04 06:48:34 -0500 1028) return mnt;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1029) }
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1030) EXPORT_SYMBOL_GPL(vfs_kern_mount);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 1031)
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1032) struct vfsmount *
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1033) vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1034) const char *name, void *data)
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1035) {
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1036) /* Until it is worked out how to pass the user namespace
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1037) * through from the parent mount to the submount don't support
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1038) * unprivileged mounts with submounts.
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1039) */
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1040) if (mountpoint->d_sb->s_user_ns != &init_user_ns)
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1041) return ERR_PTR(-EPERM);
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1042)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 1043) return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1044) }
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1045) EXPORT_SYMBOL_GPL(vfs_submount);
93faccbbfa958 (Eric W. Biederman 2017-02-01 06:06:16 +1300 1046)
87129cc0e3fcd (Al Viro 2011-11-24 21:24:27 -0500 1047) static struct mount *clone_mnt(struct mount *old, struct dentry *root,
36341f64569b0 (Ram Pai 2005-11-07 17:17:22 -0500 1048) int flag)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1049) {
87129cc0e3fcd (Al Viro 2011-11-24 21:24:27 -0500 1050) struct super_block *sb = old->mnt.mnt_sb;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1051) struct mount *mnt;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1052) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1053)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1054) mnt = alloc_vfsmnt(old->mnt_devname);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1055) if (!mnt)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1056) return ERR_PTR(-ENOMEM);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 1057)
7a472ef4be838 (Eric W. Biederman 2012-07-31 13:13:04 -0700 1058) if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1059) mnt->mnt_group_id = 0; /* not a peer of original */
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1060) else
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1061) mnt->mnt_group_id = old->mnt_group_id;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 1062)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1063) if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1064) err = mnt_alloc_group_id(mnt);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1065) if (err)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1066) goto out_free;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1067) }
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1068)
16a34adb9392b (Al Viro 2018-04-19 22:03:08 -0400 1069) mnt->mnt.mnt_flags = old->mnt.mnt_flags;
16a34adb9392b (Al Viro 2018-04-19 22:03:08 -0400 1070) mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 1071)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1072) atomic_inc(&sb->s_active);
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 1073) mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 1074) if (mnt->mnt.mnt_userns != &init_user_ns)
a6435940b62f8 (Christian Brauner 2021-01-21 14:19:20 +0100 1075) mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1076) mnt->mnt.mnt_sb = sb;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1077) mnt->mnt.mnt_root = dget(root);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1078) mnt->mnt_mountpoint = mnt->mnt.mnt_root;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1079) mnt->mnt_parent = mnt;
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1080) lock_mount_hash();
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1081) list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1082) unlock_mount_hash();
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1083)
7a472ef4be838 (Eric W. Biederman 2012-07-31 13:13:04 -0700 1084) if ((flag & CL_SLAVE) ||
7a472ef4be838 (Eric W. Biederman 2012-07-31 13:13:04 -0700 1085) ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1086) list_add(&mnt->mnt_slave, &old->mnt_slave_list);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1087) mnt->mnt_master = old;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1088) CLEAR_MNT_SHARED(mnt);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1089) } else if (!(flag & CL_PRIVATE)) {
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1090) if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1091) list_add(&mnt->mnt_share, &old->mnt_share);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1092) if (IS_MNT_SLAVE(old))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1093) list_add(&mnt->mnt_slave, &old->mnt_slave);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1094) mnt->mnt_master = old->mnt_master;
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 1095) } else {
5235d448c48e1 (Al Viro 2016-11-20 19:33:09 -0500 1096) CLEAR_MNT_SHARED(mnt);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1097) }
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1098) if (flag & CL_MAKE_SHARED)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1099) set_mnt_shared(mnt);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1100)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1101) /* stick the duplicate mount on the same expiry list
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1102) * as the original if that was on one */
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1103) if (flag & CL_EXPIRE) {
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1104) if (!list_empty(&old->mnt_expire))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1105) list_add(&mnt->mnt_expire, &old->mnt_expire);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1106) }
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1107)
cb338d06e9716 (Al Viro 2011-11-24 20:55:08 -0500 1108) return mnt;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 1109)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 1110) out_free:
8ffcb32e05239 (David Howells 2014-01-24 12:17:54 +0000 1111) mnt_free_id(mnt);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 1112) free_vfsmnt(mnt);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1113) return ERR_PTR(err);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1114) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1115)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1116) static void cleanup_mnt(struct mount *mnt)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1117) {
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1118) struct hlist_node *p;
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1119) struct mount *m;
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1120) /*
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1121) * The warning here probably indicates that somebody messed
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1122) * up a mnt_want/drop_write() pair. If this happens, the
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1123) * filesystem was probably unable to make r/w->r/o transitions.
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1124) * The locking used to deal with mnt_count decrement provides barriers,
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1125) * so mnt_get_writers() below is safe.
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1126) */
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1127) WARN_ON(mnt_get_writers(mnt));
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1128) if (unlikely(mnt->mnt_pins.first))
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1129) mnt_pin_kill(mnt);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1130) hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1131) hlist_del(&m->mnt_umount);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1132) mntput(&m->mnt);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1133) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1134) fsnotify_vfsmount_delete(&mnt->mnt);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1135) dput(mnt->mnt.mnt_root);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1136) deactivate_super(mnt->mnt.mnt_sb);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1137) mnt_free_id(mnt);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1138) call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1139) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1140)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1141) static void __cleanup_mnt(struct rcu_head *head)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1142) {
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1143) cleanup_mnt(container_of(head, struct mount, mnt_rcu));
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1144) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1145)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1146) static LLIST_HEAD(delayed_mntput_list);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1147) static void delayed_mntput(struct work_struct *unused)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1148) {
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1149) struct llist_node *node = llist_del_all(&delayed_mntput_list);
29785735788be (Byungchul Park 2017-08-07 17:44:45 +0900 1150) struct mount *m, *t;
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1151)
29785735788be (Byungchul Park 2017-08-07 17:44:45 +0900 1152) llist_for_each_entry_safe(m, t, node, mnt_llist)
29785735788be (Byungchul Park 2017-08-07 17:44:45 +0900 1153) cleanup_mnt(m);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1154) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1155) static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1156)
900148dcac6bc (Al Viro 2011-11-25 00:33:11 -0500 1157) static void mntput_no_expire(struct mount *mnt)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1158) {
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1159) LIST_HEAD(list);
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 1160) int count;
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1161)
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1162) rcu_read_lock();
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1163) if (likely(READ_ONCE(mnt->mnt_ns))) {
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1164) /*
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1165) * Since we don't do lock_mount_hash() here,
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1166) * ->mnt_ns can change under us. However, if it's
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1167) * non-NULL, then there's a reference that won't
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1168) * be dropped until after an RCU delay done after
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1169) * turning ->mnt_ns NULL. So if we observe it
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1170) * non-NULL under rcu_read_lock(), the reference
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1171) * we are dropping is not the final one.
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1172) */
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1173) mnt_add_count(mnt, -1);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1174) rcu_read_unlock();
f03c65993b98e (Al Viro 2011-01-14 22:30:21 -0500 1175) return;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1176) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1177) lock_mount_hash();
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 1178) /*
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 1179) * make sure that if __legitimize_mnt() has not seen us grab
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 1180) * mount_lock, we'll see their refcount increment here.
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 1181) */
119e1ef80ecfe (Al Viro 2018-08-09 17:51:32 -0400 1182) smp_mb();
9ea0a46ca2c31 (Al Viro 2018-08-09 17:21:17 -0400 1183) mnt_add_count(mnt, -1);
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 1184) count = mnt_get_count(mnt);
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 1185) if (count != 0) {
edf7ddbf1c5eb (Eric Biggers 2020-10-31 21:40:21 -0700 1186) WARN_ON(count < 0);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1187) rcu_read_unlock();
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1188) unlock_mount_hash();
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 1189) return;
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 1190) }
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1191) if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1192) rcu_read_unlock();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1193) unlock_mount_hash();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1194) return;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1195) }
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1196) mnt->mnt.mnt_flags |= MNT_DOOMED;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1197) rcu_read_unlock();
962830df366b6 (Andi Kleen 2012-05-08 13:32:02 +0930 1198)
39f7c4db1d2d9 (Miklos Szeredi 2011-11-21 12:11:30 +0100 1199) list_del(&mnt->mnt_instance);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1200)
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1201) if (unlikely(!list_empty(&mnt->mnt_mounts))) {
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1202) struct mount *p, *tmp;
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1203) list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1204) __put_mountpoint(unhash_mnt(p), &list);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1205) hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1206) }
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1207) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1208) unlock_mount_hash();
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1209) shrink_dentry_list(&list);
649a795affac8 (Al Viro 2013-09-28 12:41:25 -0400 1210)
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1211) if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1212) struct task_struct *task = current;
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1213) if (likely(!(task->flags & PF_KTHREAD))) {
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1214) init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
91989c707884e (Jens Axboe 2020-10-16 09:02:26 -0600 1215) if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1216) return;
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1217) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1218) if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1219) schedule_delayed_work(&delayed_mntput_work, 1);
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1220) return;
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1221) }
9ea459e110df3 (Al Viro 2014-08-08 13:08:20 -0400 1222) cleanup_mnt(mnt);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1223) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1224)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1225) void mntput(struct vfsmount *mnt)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1226) {
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1227) if (mnt) {
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 1228) struct mount *m = real_mount(mnt);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1229) /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 1230) if (unlikely(m->mnt_expiry_mark))
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 1231) m->mnt_expiry_mark = 0;
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 1232) mntput_no_expire(m);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1233) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1234) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1235) EXPORT_SYMBOL(mntput);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1236)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1237) struct vfsmount *mntget(struct vfsmount *mnt)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1238) {
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1239) if (mnt)
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 1240) mnt_add_count(real_mount(mnt), 1);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1241) return mnt;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1242) }
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1243) EXPORT_SYMBOL(mntget);
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1244)
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1245) /**
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1246) * path_is_mountpoint() - Check if path is a mount in the current namespace.
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1247) * @path: path to check
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1248) *
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1249) * d_mountpoint() can only be used reliably to establish if a dentry is
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1250) * not mounted in any namespace and that common case is handled inline.
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1251) * d_mountpoint() isn't aware of the possibility there may be multiple
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1252) * mounts using a given dentry in a different namespace. This function
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1253) * checks if the passed in path is a mountpoint rather than the dentry
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1254) * alone.
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1255) */
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1256) bool path_is_mountpoint(const struct path *path)
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1257) {
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1258) unsigned seq;
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1259) bool res;
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1260)
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1261) if (!d_mountpoint(path->dentry))
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1262) return false;
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1263)
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1264) rcu_read_lock();
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1265) do {
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1266) seq = read_seqbegin(&mount_lock);
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1267) res = __path_is_mountpoint(path);
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1268) } while (read_seqretry(&mount_lock, seq));
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1269) rcu_read_unlock();
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1270)
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1271) return res;
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1272) }
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1273) EXPORT_SYMBOL(path_is_mountpoint);
c6609c0a1c34f (Ian Kent 2016-11-24 08:03:41 +1100 1274)
ca71cf71eeda0 (Al Viro 2016-11-20 19:45:28 -0500 1275) struct vfsmount *mnt_clone_internal(const struct path *path)
7b7b1ace2d9d0 (Al Viro 2005-11-07 17:13:39 -0500 1276) {
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1277) struct mount *p;
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1278) p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1279) if (IS_ERR(p))
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1280) return ERR_CAST(p);
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1281) p->mnt.mnt_flags |= MNT_INTERNAL;
3064c3563ba4c (Al Viro 2014-08-07 09:12:31 -0400 1282) return &p->mnt;
7b7b1ace2d9d0 (Al Viro 2005-11-07 17:13:39 -0500 1283) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1284)
a1a2c409b666b (Miklos Szeredi 2008-03-27 13:06:24 +0100 1285) #ifdef CONFIG_PROC_FS
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1286) static struct mount *mnt_list_next(struct mnt_namespace *ns,
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1287) struct list_head *p)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1288) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1289) struct mount *mnt, *ret = NULL;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1290)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1291) lock_ns_list(ns);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1292) list_for_each_continue(p, &ns->list) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1293) mnt = list_entry(p, typeof(*mnt), mnt_list);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1294) if (!mnt_is_cursor(mnt)) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1295) ret = mnt;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1296) break;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1297) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1298) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1299) unlock_ns_list(ns);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1300)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1301) return ret;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1302) }
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1303)
0226f4923f6c9 (Al Viro 2011-12-06 12:21:54 -0500 1304) /* iterator; we want it to have access to namespace_sem, thus here... */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1305) static void *m_start(struct seq_file *m, loff_t *pos)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1306) {
ede1bf0dcff2b (Yann Droneaud 2015-06-30 14:57:30 -0700 1307) struct proc_mounts *p = m->private;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1308) struct list_head *prev;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1309)
390c684367de3 (Ram Pai 2005-11-07 17:17:51 -0500 1310) down_read(&namespace_sem);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1311) if (!*pos) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1312) prev = &p->ns->list;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1313) } else {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1314) prev = &p->cursor.mnt_list;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1315)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1316) /* Read after we'd reached the end? */
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1317) if (list_empty(prev))
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1318) return NULL;
c7999c3627bc6 (Al Viro 2014-02-27 14:40:10 -0500 1319) }
c7999c3627bc6 (Al Viro 2014-02-27 14:40:10 -0500 1320)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1321) return mnt_list_next(p->ns, prev);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1322) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1323)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1324) static void *m_next(struct seq_file *m, void *v, loff_t *pos)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1325) {
ede1bf0dcff2b (Yann Droneaud 2015-06-30 14:57:30 -0700 1326) struct proc_mounts *p = m->private;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1327) struct mount *mnt = v;
b0765fb85782d (Pavel Emelianov 2007-07-15 23:39:55 -0700 1328)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1329) ++*pos;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1330) return mnt_list_next(p->ns, &mnt->mnt_list);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1331) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1332)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1333) static void m_stop(struct seq_file *m, void *v)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1334) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1335) struct proc_mounts *p = m->private;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1336) struct mount *mnt = v;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1337)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1338) lock_ns_list(p->ns);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1339) if (mnt)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1340) list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1341) else
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1342) list_del_init(&p->cursor.mnt_list);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1343) unlock_ns_list(p->ns);
390c684367de3 (Ram Pai 2005-11-07 17:17:51 -0500 1344) up_read(&namespace_sem);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1345) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1346)
0226f4923f6c9 (Al Viro 2011-12-06 12:21:54 -0500 1347) static int m_show(struct seq_file *m, void *v)
2d4d4864ac08c (Ram Pai 2008-03-27 13:06:25 +0100 1348) {
ede1bf0dcff2b (Yann Droneaud 2015-06-30 14:57:30 -0700 1349) struct proc_mounts *p = m->private;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1350) struct mount *r = v;
0226f4923f6c9 (Al Viro 2011-12-06 12:21:54 -0500 1351) return p->show(m, &r->mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1352) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1353)
a1a2c409b666b (Miklos Szeredi 2008-03-27 13:06:24 +0100 1354) const struct seq_operations mounts_op = {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1355) .start = m_start,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1356) .next = m_next,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1357) .stop = m_stop,
0226f4923f6c9 (Al Viro 2011-12-06 12:21:54 -0500 1358) .show = m_show,
b4629fe2f094b (Chuck Lever 2006-03-20 13:44:12 -0500 1359) };
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1360)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1361) void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1362) {
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1363) down_read(&namespace_sem);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1364) lock_ns_list(ns);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1365) list_del(&cursor->mnt_list);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1366) unlock_ns_list(ns);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1367) up_read(&namespace_sem);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 1368) }
a1a2c409b666b (Miklos Szeredi 2008-03-27 13:06:24 +0100 1369) #endif /* CONFIG_PROC_FS */
b4629fe2f094b (Chuck Lever 2006-03-20 13:44:12 -0500 1370)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1371) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1372) * may_umount_tree - check if a mount tree is busy
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1373) * @m: root of mount tree
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1374) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1375) * This is called to check if a tree of mounts has any
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1376) * open files, pwds, chroots or sub mounts that are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1377) * busy.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1378) */
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 1379) int may_umount_tree(struct vfsmount *m)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1380) {
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 1381) struct mount *mnt = real_mount(m);
36341f64569b0 (Ram Pai 2005-11-07 17:17:22 -0500 1382) int actual_refs = 0;
36341f64569b0 (Ram Pai 2005-11-07 17:17:22 -0500 1383) int minimum_refs = 0;
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 1384) struct mount *p;
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 1385) BUG_ON(!m);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1386)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1387) /* write lock needed for mnt_get_count */
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1388) lock_mount_hash();
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 1389) for (p = mnt; p; p = next_mnt(p, mnt)) {
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 1390) actual_refs += mnt_get_count(p);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1391) minimum_refs += 2;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1392) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1393) unlock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1394)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1395) if (actual_refs > minimum_refs)
e3474a8eb38e4 (Ian Kent 2006-03-27 01:14:51 -0800 1396) return 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1397)
e3474a8eb38e4 (Ian Kent 2006-03-27 01:14:51 -0800 1398) return 1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1399) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1400)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1401) EXPORT_SYMBOL(may_umount_tree);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1402)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1403) /**
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1404) * may_umount - check if a mount point is busy
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1405) * @mnt: root of mount
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1406) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1407) * This is called to check if a mount point has any
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1408) * open files, pwds, chroots or sub mounts. If the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1409) * mount has sub mounts this will return busy
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1410) * regardless of whether the sub mounts are busy.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1411) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1412) * Doesn't take quota and stuff into account. IOW, in some cases it will
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1413) * give false negatives. The main reason why it's here is that we need
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1414) * a non-destructive way to look for easily umountable filesystems.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1415) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1416) int may_umount(struct vfsmount *mnt)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1417) {
e3474a8eb38e4 (Ian Kent 2006-03-27 01:14:51 -0800 1418) int ret = 1;
8ad08d8a0c382 (Al Viro 2010-01-16 12:56:08 -0500 1419) down_read(&namespace_sem);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1420) lock_mount_hash();
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1421) if (propagate_mount_busy(real_mount(mnt), 2))
e3474a8eb38e4 (Ian Kent 2006-03-27 01:14:51 -0800 1422) ret = 0;
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1423) unlock_mount_hash();
8ad08d8a0c382 (Al Viro 2010-01-16 12:56:08 -0500 1424) up_read(&namespace_sem);
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 1425) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1426) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1427)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1428) EXPORT_SYMBOL(may_umount);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1429)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1430) static void namespace_unlock(void)
70fbcdf4d252c (Ram Pai 2005-11-07 17:17:04 -0500 1431) {
a3b3c5627c830 (Eric W. Biederman 2015-04-02 20:33:53 -0500 1432) struct hlist_head head;
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1433) struct hlist_node *p;
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1434) struct mount *m;
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1435) LIST_HEAD(list);
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1436)
a3b3c5627c830 (Eric W. Biederman 2015-04-02 20:33:53 -0500 1437) hlist_move_list(&unmounted, &head);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1438) list_splice_init(&ex_mountpoints, &list);
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1439)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1440) up_write(&namespace_sem);
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1441)
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1442) shrink_dentry_list(&list);
4edbe133f851c (Al Viro 2019-06-30 10:39:08 -0400 1443)
a3b3c5627c830 (Eric W. Biederman 2015-04-02 20:33:53 -0500 1444) if (likely(hlist_empty(&head)))
a3b3c5627c830 (Eric W. Biederman 2015-04-02 20:33:53 -0500 1445) return;
a3b3c5627c830 (Eric W. Biederman 2015-04-02 20:33:53 -0500 1446)
22cb7405fada5 (NeilBrown 2018-11-30 10:33:18 +1100 1447) synchronize_rcu_expedited();
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1448)
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1449) hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1450) hlist_del(&m->mnt_umount);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1451) mntput(&m->mnt);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1452) }
70fbcdf4d252c (Ram Pai 2005-11-07 17:17:04 -0500 1453) }
70fbcdf4d252c (Ram Pai 2005-11-07 17:17:04 -0500 1454)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1455) static inline void namespace_lock(void)
e3197d83d6f5b (Al Viro 2013-03-16 14:35:16 -0400 1456) {
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1457) down_write(&namespace_sem);
e3197d83d6f5b (Al Viro 2013-03-16 14:35:16 -0400 1458) }
e3197d83d6f5b (Al Viro 2013-03-16 14:35:16 -0400 1459)
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1460) enum umount_tree_flags {
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1461) UMOUNT_SYNC = 1,
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1462) UMOUNT_PROPAGATE = 2,
e0c9c0afd2fc9 (Eric W. Biederman 2015-04-01 18:30:06 -0500 1463) UMOUNT_CONNECTED = 4,
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1464) };
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1465)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1466) static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1467) {
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1468) /* Leaving mounts connected is only valid for lazy umounts */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1469) if (how & UMOUNT_SYNC)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1470) return true;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1471)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1472) /* A mount without a parent has nothing to be connected to */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1473) if (!mnt_has_parent(mnt))
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1474) return true;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1475)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1476) /* Because the reference counting rules change when mounts are
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1477) * unmounted and connected, umounted mounts may not be
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1478) * connected to mounted mounts.
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1479) */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1480) if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1481) return true;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1482)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1483) /* Has it been requested that the mount remain connected? */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1484) if (how & UMOUNT_CONNECTED)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1485) return false;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1486)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1487) /* Is the mount locked such that it needs to remain connected? */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1488) if (IS_MNT_LOCKED(mnt))
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1489) return false;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1490)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1491) /* By default disconnect the mount */
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1492) return true;
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1493) }
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1494)
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 1495) /*
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1496) * mount_lock must be held
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 1497) * namespace_sem must be held for write
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 1498) */
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1499) static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1500) {
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1501) LIST_HEAD(tmp_list);
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 1502) struct mount *p;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1503)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 1504) if (how & UMOUNT_PROPAGATE)
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 1505) propagate_mount_unlock(mnt);
5d88457eb5b86 (Eric W. Biederman 2015-01-03 05:39:35 -0600 1506)
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1507) /* Gather the mounts to umount */
590ce4bcbfb4e (Eric W. Biederman 2014-12-22 18:30:08 -0600 1508) for (p = mnt; p; p = next_mnt(p, mnt)) {
590ce4bcbfb4e (Eric W. Biederman 2014-12-22 18:30:08 -0600 1509) p->mnt.mnt_flags |= MNT_UMOUNT;
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1510) list_move(&p->mnt_list, &tmp_list);
590ce4bcbfb4e (Eric W. Biederman 2014-12-22 18:30:08 -0600 1511) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1512)
411a938b5abc9 (Eric W. Biederman 2014-12-22 19:12:07 -0600 1513) /* Hide the mounts from mnt_mounts */
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1514) list_for_each_entry(p, &tmp_list, mnt_list) {
88b368f27a094 (Al Viro 2014-08-18 15:09:26 -0400 1515) list_del_init(&p->mnt_child);
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1516) }
88b368f27a094 (Al Viro 2014-08-18 15:09:26 -0400 1517)
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1518) /* Add propogated mounts to the tmp_list */
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1519) if (how & UMOUNT_PROPAGATE)
7b8a53fd815de (Al Viro 2011-01-15 20:08:44 -0500 1520) propagate_umount(&tmp_list);
a05964f3917c7 (Ram Pai 2005-11-07 17:20:17 -0500 1521)
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1522) while (!list_empty(&tmp_list)) {
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1523) struct mnt_namespace *ns;
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1524) bool disconnect;
c003b26ff98ca (Eric W. Biederman 2014-12-18 13:10:48 -0600 1525) p = list_first_entry(&tmp_list, struct mount, mnt_list);
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 1526) list_del_init(&p->mnt_expire);
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 1527) list_del_init(&p->mnt_list);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1528) ns = p->mnt_ns;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1529) if (ns) {
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1530) ns->mounts--;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1531) __touch_mnt_namespace(ns);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 1532) }
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 1533) p->mnt_ns = NULL;
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1534) if (how & UMOUNT_SYNC)
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1535) p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
87b95ce0964c0 (Al Viro 2015-01-10 19:01:08 -0500 1536)
f2d0a123bcf16 (Eric W. Biederman 2015-07-17 14:15:30 -0500 1537) disconnect = disconnect_mount(p, how);
676da58df740f (Al Viro 2011-11-24 21:47:05 -0500 1538) if (mnt_has_parent(p)) {
81b6b06197606 (Al Viro 2014-08-30 18:32:05 -0400 1539) mnt_add_count(p->mnt_parent, -1);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1540) if (!disconnect) {
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1541) /* Don't forget about p */
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1542) list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1543) } else {
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1544) umount_mnt(p);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1545) }
7c4b93d8269b9 (Al Viro 2008-03-21 23:59:49 -0400 1546) }
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 1547) change_mnt_propagation(p, MS_PRIVATE);
19a1c4092e7ca (Al Viro 2019-07-24 12:45:46 -0400 1548) if (disconnect)
19a1c4092e7ca (Al Viro 2019-07-24 12:45:46 -0400 1549) hlist_add_head(&p->mnt_umount, &unmounted);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1550) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1551) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1552)
b54b9be7824d8 (Al Viro 2013-03-16 14:39:34 -0400 1553) static void shrink_submounts(struct mount *mnt);
c35038becad0a (Al Viro 2008-03-22 00:46:23 -0400 1554)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1555) static int do_umount_root(struct super_block *sb)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1556) {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1557) int ret = 0;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1558)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1559) down_write(&sb->s_umount);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1560) if (!sb_rdonly(sb)) {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1561) struct fs_context *fc;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1562)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1563) fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1564) SB_RDONLY);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1565) if (IS_ERR(fc)) {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1566) ret = PTR_ERR(fc);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1567) } else {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1568) ret = parse_monolithic_mount_data(fc, NULL);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1569) if (!ret)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1570) ret = reconfigure_super(fc);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1571) put_fs_context(fc);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1572) }
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1573) }
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1574) up_write(&sb->s_umount);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1575) return ret;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1576) }
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1577)
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1578) static int do_umount(struct mount *mnt, int flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1579) {
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1580) struct super_block *sb = mnt->mnt.mnt_sb;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1581) int retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1582)
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1583) retval = security_sb_umount(&mnt->mnt, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1584) if (retval)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1585) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1586)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1587) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1588) * Allow userspace to request a mountpoint be expired rather than
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1589) * unmounting unconditionally. Unmount only happens if:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1590) * (1) the mark is already set (the mark is cleared by mntput())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1591) * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1592) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1593) if (flags & MNT_EXPIRE) {
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1594) if (&mnt->mnt == current->fs->root.mnt ||
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1595) flags & (MNT_FORCE | MNT_DETACH))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1596) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1597)
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1598) /*
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1599) * probably don't strictly need the lock here if we examined
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1600) * all race cases, but it's a slowpath.
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1601) */
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1602) lock_mount_hash();
83adc7532229f (Al Viro 2011-11-24 22:37:54 -0500 1603) if (mnt_get_count(mnt) != 2) {
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1604) unlock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1605) return -EBUSY;
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 1606) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1607) unlock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1608)
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 1609) if (!xchg(&mnt->mnt_expiry_mark, 1))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1610) return -EAGAIN;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1611) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1612)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1613) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1614) * If we may have to abort operations to get out of this
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1615) * mount, and they will themselves hold resources we must
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1616) * allow the fs to do things. In the Unix tradition of
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1617) * 'Gee thats tricky lets do it in userspace' the umount_begin
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1618) * might fail to complete on the first run through as other tasks
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1619) * must return, and the like. Thats for the mount program to worry
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1620) * about for the moment.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1621) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1622)
42faad99658ee (Al Viro 2008-04-24 07:21:56 -0400 1623) if (flags & MNT_FORCE && sb->s_op->umount_begin) {
42faad99658ee (Al Viro 2008-04-24 07:21:56 -0400 1624) sb->s_op->umount_begin(sb);
42faad99658ee (Al Viro 2008-04-24 07:21:56 -0400 1625) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1626)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1627) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1628) * No sense to grab the lock for this test, but test itself looks
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1629) * somewhat bogus. Suggestions for better replacement?
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1630) * Ho-hum... In principle, we might treat that as umount + switch
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1631) * to rootfs. GC would eventually take care of the old vfsmount.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1632) * Actually it makes sense, especially if rootfs would contain a
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1633) * /reboot - static binary that would close all descriptors and
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1634) * call reboot(9). Then init(8) could umount root and exec /reboot.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1635) */
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 1636) if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1637) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1638) * Special case for "unmounting" root ...
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1639) * we just try to remount it readonly.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1640) */
bc6155d132609 (Eric W. Biederman 2017-09-18 17:58:08 -0500 1641) if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
a1480dcc3c706 (Andy Lutomirski 2014-10-08 12:32:47 -0700 1642) return -EPERM;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 1643) return do_umount_root(sb);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1644) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1645)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1646) namespace_lock();
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1647) lock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1648)
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1649) /* Recheck MNT_LOCKED with the locks held */
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1650) retval = -EINVAL;
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1651) if (mnt->mnt.mnt_flags & MNT_LOCKED)
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1652) goto out;
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1653)
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1654) event++;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1655) if (flags & MNT_DETACH) {
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 1656) if (!list_empty(&mnt->mnt_list))
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1657) umount_tree(mnt, UMOUNT_PROPAGATE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1658) retval = 0;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1659) } else {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1660) shrink_submounts(mnt);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1661) retval = -EBUSY;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1662) if (!propagate_mount_busy(mnt, 2)) {
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1663) if (!list_empty(&mnt->mnt_list))
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1664) umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1665) retval = 0;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 1666) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1667) }
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1668) out:
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1669) unlock_mount_hash();
e3197d83d6f5b (Al Viro 2013-03-16 14:35:16 -0400 1670) namespace_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1671) return retval;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1672) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1673)
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1674) /*
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1675) * __detach_mounts - lazily unmount all mounts on the specified dentry
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1676) *
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1677) * During unlink, rmdir, and d_drop it is possible to loose the path
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1678) * to an existing mountpoint, and wind up leaking the mount.
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1679) * detach_mounts allows lazily unmounting those mounts instead of
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1680) * leaking them.
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1681) *
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1682) * The caller may hold dentry->d_inode->i_mutex.
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1683) */
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1684) void __detach_mounts(struct dentry *dentry)
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1685) {
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1686) struct mountpoint *mp;
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1687) struct mount *mnt;
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1688)
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1689) namespace_lock();
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1690) lock_mount_hash();
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1691) mp = lookup_mountpoint(dentry);
adc9b5c09129f (Al Viro 2019-06-29 12:06:51 -0400 1692) if (!mp)
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1693) goto out_unlock;
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1694)
e06b933e6ded4 (Andrey Ulanov 2016-04-15 14:24:41 -0700 1695) event++;
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1696) while (!hlist_empty(&mp->m_list)) {
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1697) mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1698) if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
fe78fcc85a204 (Eric W. Biederman 2015-07-17 14:54:27 -0500 1699) umount_mnt(mnt);
56cbb429d9119 (Al Viro 2019-07-04 16:57:51 -0400 1700) hlist_add_head(&mnt->mnt_umount, &unmounted);
ce07d891a0891 (Eric W. Biederman 2014-12-23 21:37:03 -0600 1701) }
e0c9c0afd2fc9 (Eric W. Biederman 2015-04-01 18:30:06 -0500 1702) else umount_tree(mnt, UMOUNT_CONNECTED);
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1703) }
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1704) put_mountpoint(mp);
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1705) out_unlock:
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 1706) unlock_mount_hash();
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1707) namespace_unlock();
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1708) }
80b5dce8c59b0 (Eric W. Biederman 2013-10-03 01:31:18 -0700 1709)
dd111b31e951c (David Howells 2017-07-04 17:25:09 +0100 1710) /*
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1711) * Is the caller allowed to modify his namespace?
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1712) */
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1713) static inline bool may_mount(void)
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1714) {
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1715) return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1716) }
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1717)
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1718) #ifdef CONFIG_MANDATORY_FILE_LOCKING
2ff626b140036 (Jeff Layton 2021-08-20 09:29:50 -0400 1719) static bool may_mandlock(void)
9e8925b67a809 (Jeff Layton 2015-11-16 09:49:34 -0500 1720) {
2ff626b140036 (Jeff Layton 2021-08-20 09:29:50 -0400 1721) pr_warn_once("======================================================\n"
2ff626b140036 (Jeff Layton 2021-08-20 09:29:50 -0400 1722) "WARNING: the mand mount option is being deprecated and\n"
2ff626b140036 (Jeff Layton 2021-08-20 09:29:50 -0400 1723) " will be removed in v5.15!\n"
2ff626b140036 (Jeff Layton 2021-08-20 09:29:50 -0400 1724) "======================================================\n");
95ace75414f31 (Eric W. Biederman 2015-11-11 17:22:33 -0600 1725) return capable(CAP_SYS_ADMIN);
9e8925b67a809 (Jeff Layton 2015-11-16 09:49:34 -0500 1726) }
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1727) #else
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1728) static inline bool may_mandlock(void)
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1729) {
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1730) pr_warn("VFS: \"mand\" mount option not supported");
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1731) return false;
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1732) }
df2474a22c42c (Jeff Layton 2019-08-15 15:21:17 -0400 1733) #endif
9e8925b67a809 (Jeff Layton 2015-11-16 09:49:34 -0500 1734)
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1735) static int can_umount(const struct path *path, int flags)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1736) {
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1737) struct mount *mnt = real_mount(path->mnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1738)
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1739) if (!may_mount())
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 1740) return -EPERM;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1741) if (path->dentry != path->mnt->mnt_root)
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1742) return -EINVAL;
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 1743) if (!check_mnt(mnt))
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1744) return -EINVAL;
25d202ed820ee (Eric W. Biederman 2018-10-22 10:21:38 -0500 1745) if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1746) return -EINVAL;
b2f5d4dc38e03 (Eric W. Biederman 2014-10-04 14:44:03 -0700 1747) if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1748) return -EPERM;
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1749) return 0;
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1750) }
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1751)
a0a6df9afcaf4 (Al Viro 2021-01-04 15:25:34 -0500 1752) // caller is responsible for flags being sane
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1753) int path_umount(struct path *path, int flags)
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1754) {
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1755) struct mount *mnt = real_mount(path->mnt);
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1756) int ret;
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1757)
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1758) ret = can_umount(path, flags);
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1759) if (!ret)
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1760) ret = do_umount(mnt, flags);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1761)
429731b1553ba (Jan Blunck 2008-02-14 19:34:31 -0800 1762) /* we mustn't call path_put() as that would clear mnt_expiry_mark */
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1763) dput(path->dentry);
900148dcac6bc (Al Viro 2011-11-25 00:33:11 -0500 1764) mntput_no_expire(mnt);
25ccd24ffd911 (Christoph Hellwig 2020-08-06 16:07:10 +0200 1765) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1766) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1767)
09267defa36aa (Christoph Hellwig 2020-07-23 08:23:08 +0200 1768) static int ksys_umount(char __user *name, int flags)
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1769) {
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1770) int lookup_flags = LOOKUP_MOUNTPOINT;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1771) struct path path;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1772) int ret;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1773)
a0a6df9afcaf4 (Al Viro 2021-01-04 15:25:34 -0500 1774) // basic validity checks done first
a0a6df9afcaf4 (Al Viro 2021-01-04 15:25:34 -0500 1775) if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
a0a6df9afcaf4 (Al Viro 2021-01-04 15:25:34 -0500 1776) return -EINVAL;
a0a6df9afcaf4 (Al Viro 2021-01-04 15:25:34 -0500 1777)
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1778) if (!(flags & UMOUNT_NOFOLLOW))
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1779) lookup_flags |= LOOKUP_FOLLOW;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1780) ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1781) if (ret)
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1782) return ret;
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1783) return path_umount(&path, flags);
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1784) }
41525f56e2564 (Christoph Hellwig 2020-07-21 10:54:34 +0200 1785)
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1786) SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1787) {
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1788) return ksys_umount(name, flags);
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1789) }
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1790)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1791) #ifdef __ARCH_WANT_SYS_OLDUMOUNT
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1792)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1793) /*
b58fed8b1959d (Ram Pai 2005-11-07 17:16:09 -0500 1794) * The 2.0 compatible umount. No flags.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1795) */
bdc480e3bef6e (Heiko Carstens 2009-01-14 14:14:12 +0100 1796) SYSCALL_DEFINE1(oldumount, char __user *, name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1797) {
3a18ef5c1b393 (Dominik Brodowski 2018-03-11 11:34:40 +0100 1798) return ksys_umount(name, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1799) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1800)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1801) #endif
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1802)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1803) static bool is_mnt_ns_file(struct dentry *dentry)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 1804) {
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1805) /* Is this a proxy for a mount namespace? */
e149ed2b805fe (Al Viro 2014-11-01 10:57:28 -0400 1806) return dentry->d_op == &ns_dentry_operations &&
e149ed2b805fe (Al Viro 2014-11-01 10:57:28 -0400 1807) dentry->d_fsdata == &mntns_operations;
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1808) }
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1809)
213921f967cf4 (Eric Biggers 2020-01-04 12:59:55 -0800 1810) static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 1811) {
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 1812) return container_of(ns, struct mnt_namespace, ns);
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 1813) }
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 1814)
303cc571d107b (Christian Brauner 2020-05-05 16:04:31 +0200 1815) struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
303cc571d107b (Christian Brauner 2020-05-05 16:04:31 +0200 1816) {
303cc571d107b (Christian Brauner 2020-05-05 16:04:31 +0200 1817) return &mnt->ns;
303cc571d107b (Christian Brauner 2020-05-05 16:04:31 +0200 1818) }
303cc571d107b (Christian Brauner 2020-05-05 16:04:31 +0200 1819)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1820) static bool mnt_ns_loop(struct dentry *dentry)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1821) {
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1822) /* Could bind mounting the mount namespace inode cause a
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1823) * mount namespace loop?
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1824) */
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1825) struct mnt_namespace *mnt_ns;
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1826) if (!is_mnt_ns_file(dentry))
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1827) return false;
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1828)
f77c80142e1af (Al Viro 2014-11-01 03:13:17 -0400 1829) mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 1830) return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 1831) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 1832)
87129cc0e3fcd (Al Viro 2011-11-24 21:24:27 -0500 1833) struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
36341f64569b0 (Ram Pai 2005-11-07 17:17:22 -0500 1834) int flag)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1835) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 1836) struct mount *res, *p, *q, *r, *parent;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1837)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1838) if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1839) return ERR_PTR(-EINVAL);
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1840)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1841) if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1842) return ERR_PTR(-EINVAL);
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 1843)
36341f64569b0 (Ram Pai 2005-11-07 17:17:22 -0500 1844) res = q = clone_mnt(mnt, dentry, flag);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1845) if (IS_ERR(q))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1846) return q;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1847)
a73324da7af40 (Al Viro 2011-11-24 22:25:07 -0500 1848) q->mnt_mountpoint = mnt->mnt_mountpoint;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1849)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1850) p = mnt;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 1851) list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 1852) struct mount *s;
7ec02ef1596bb (Jan Blunck 2008-04-29 00:59:40 -0700 1853) if (!is_subdir(r->mnt_mountpoint, dentry))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1854) continue;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1855)
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 1856) for (s = r; s; s = next_mnt(s, r)) {
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1857) if (!(flag & CL_COPY_UNBINDABLE) &&
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1858) IS_MNT_UNBINDABLE(s)) {
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1859) if (s->mnt.mnt_flags & MNT_LOCKED) {
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1860) /* Both unbindable and locked. */
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1861) q = ERR_PTR(-EPERM);
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1862) goto out;
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1863) } else {
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1864) s = skip_mnt_tree(s);
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1865) continue;
df7342b240185 (Eric W. Biederman 2018-10-25 09:04:18 -0500 1866) }
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1867) }
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1868) if (!(flag & CL_COPY_MNT_NS_FILE) &&
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 1869) is_mnt_ns_file(s->mnt.mnt_root)) {
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 1870) s = skip_mnt_tree(s);
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 1871) continue;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 1872) }
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 1873) while (p != s->mnt_parent) {
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 1874) p = p->mnt_parent;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 1875) q = q->mnt_parent;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1876) }
87129cc0e3fcd (Al Viro 2011-11-24 21:24:27 -0500 1877) p = s;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 1878) parent = q;
87129cc0e3fcd (Al Viro 2011-11-24 21:24:27 -0500 1879) q = clone_mnt(p, p->mnt.mnt_root, flag);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1880) if (IS_ERR(q))
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1881) goto out;
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1882) lock_mount_hash();
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 1883) list_add_tail(&q->mnt_list, &res->mnt_list);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 1884) attach_mnt(q, parent, p->mnt_mp);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1885) unlock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1886) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1887) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1888) return res;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1889) out:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1890) if (res) {
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1891) lock_mount_hash();
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 1892) umount_tree(res, UMOUNT_SYNC);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1893) unlock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1894) }
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1895) return q;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1896) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1897)
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1898) /* Caller should check returned pointer for errors */
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1899)
ca71cf71eeda0 (Al Viro 2016-11-20 19:45:28 -0500 1900) struct vfsmount *collect_mounts(const struct path *path)
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1901) {
cb338d06e9716 (Al Viro 2011-11-24 20:55:08 -0500 1902) struct mount *tree;
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1903) namespace_lock();
cd4a40174b71a (Eric W. Biederman 2015-01-07 14:28:26 -0600 1904) if (!check_mnt(real_mount(path->mnt)))
cd4a40174b71a (Eric W. Biederman 2015-01-07 14:28:26 -0600 1905) tree = ERR_PTR(-EINVAL);
cd4a40174b71a (Eric W. Biederman 2015-01-07 14:28:26 -0600 1906) else
cd4a40174b71a (Eric W. Biederman 2015-01-07 14:28:26 -0600 1907) tree = copy_tree(real_mount(path->mnt), path->dentry,
cd4a40174b71a (Eric W. Biederman 2015-01-07 14:28:26 -0600 1908) CL_COPY_ALL | CL_PRIVATE);
328e6d9014636 (Al Viro 2013-03-16 14:49:45 -0400 1909) namespace_unlock();
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1910) if (IS_ERR(tree))
52e220d357a38 (Dan Carpenter 2013-08-14 12:44:39 +0300 1911) return ERR_CAST(tree);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 1912) return &tree->mnt;
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1913) }
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1914)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1915) static void free_mnt_ns(struct mnt_namespace *);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1916) static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1917)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1918) void dissolve_on_fput(struct vfsmount *mnt)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1919) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1920) struct mnt_namespace *ns;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1921) namespace_lock();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1922) lock_mount_hash();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1923) ns = real_mount(mnt)->mnt_ns;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1924) if (ns) {
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1925) if (is_anon_ns(ns))
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1926) umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1927) else
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1928) ns = NULL;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1929) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1930) unlock_mount_hash();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1931) namespace_unlock();
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1932) if (ns)
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 1933) free_mnt_ns(ns);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1934) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 1935)
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1936) void drop_collected_mounts(struct vfsmount *mnt)
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1937) {
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 1938) namespace_lock();
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1939) lock_mount_hash();
9c8e0a1b68352 (Eric W. Biederman 2018-10-25 12:05:11 -0500 1940) umount_tree(real_mount(mnt), 0);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 1941) unlock_mount_hash();
3ab6abee59ac9 (Al Viro 2013-03-16 14:42:19 -0400 1942) namespace_unlock();
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1943) }
8aec080945705 (Al Viro 2007-06-07 12:20:32 -0400 1944)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1945) static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1946) {
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1947) struct mount *child;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1948)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1949) list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1950) if (!is_subdir(child->mnt_mountpoint, dentry))
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1951) continue;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1952)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1953) if (child->mnt.mnt_flags & MNT_LOCKED)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1954) return true;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1955) }
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1956) return false;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1957) }
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1958)
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1959) /**
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1960) * clone_private_mount - create a private clone of a path
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1961) * @path: path to clone
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1962) *
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1963) * This creates a new vfsmount, which will be the clone of @path. The new mount
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1964) * will not be attached anywhere in the namespace and will be private (i.e.
1f287bc4e2d07 (Randy Dunlap 2021-03-17 19:52:25 -0700 1965) * changes to the originating mount won't be propagated into this).
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1966) *
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1967) * Release with mntput().
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1968) */
ca71cf71eeda0 (Al Viro 2016-11-20 19:45:28 -0500 1969) struct vfsmount *clone_private_mount(const struct path *path)
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1970) {
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1971) struct mount *old_mnt = real_mount(path->mnt);
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1972) struct mount *new_mnt;
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1973)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1974) down_read(&namespace_sem);
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1975) if (IS_MNT_UNBINDABLE(old_mnt))
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1976) goto invalid;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1977)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1978) if (!check_mnt(old_mnt))
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1979) goto invalid;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1980)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1981) if (has_locked_children(old_mnt, path->dentry))
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1982) goto invalid;
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1983)
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1984) new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1985) up_read(&namespace_sem);
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1986)
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1987) if (IS_ERR(new_mnt))
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1988) return ERR_CAST(new_mnt);
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1989)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 1990) /* Longterm mount to be removed by kern_unmount*() */
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 1991) new_mnt->mnt_ns = MNT_NS_INTERNAL;
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 1992)
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1993) return &new_mnt->mnt;
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1994)
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1995) invalid:
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1996) up_read(&namespace_sem);
41812f4b84484 (Miklos Szeredi 2021-08-09 10:19:47 +0200 1997) return ERR_PTR(-EINVAL);
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1998) }
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 1999) EXPORT_SYMBOL_GPL(clone_private_mount);
c771d683a62e5 (Miklos Szeredi 2014-10-24 00:14:36 +0200 2000)
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2001) int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2002) struct vfsmount *root)
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2003) {
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 2004) struct mount *mnt;
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2005) int res = f(root, arg);
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2006) if (res)
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2007) return res;
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 2008) list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 2009) res = f(&mnt->mnt, arg);
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2010) if (res)
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2011) return res;
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2012) }
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2013) return 0;
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2014) }
1f707137b5576 (Al Viro 2010-01-30 22:51:25 -0500 2015)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2016) static void lock_mnt_tree(struct mount *mnt)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2017) {
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2018) struct mount *p;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2019)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2020) for (p = mnt; p; p = next_mnt(p, mnt)) {
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2021) int flags = p->mnt.mnt_flags;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2022) /* Don't allow unprivileged users to change mount flags */
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2023) flags |= MNT_LOCK_ATIME;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2024)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2025) if (flags & MNT_READONLY)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2026) flags |= MNT_LOCK_READONLY;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2027)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2028) if (flags & MNT_NODEV)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2029) flags |= MNT_LOCK_NODEV;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2030)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2031) if (flags & MNT_NOSUID)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2032) flags |= MNT_LOCK_NOSUID;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2033)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2034) if (flags & MNT_NOEXEC)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2035) flags |= MNT_LOCK_NOEXEC;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2036) /* Don't allow unprivileged users to reveal what is under a mount */
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2037) if (list_empty(&p->mnt_expire))
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2038) flags |= MNT_LOCKED;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2039) p->mnt.mnt_flags = flags;
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2040) }
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2041) }
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2042)
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2043) static void cleanup_group_ids(struct mount *mnt, struct mount *end)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2044) {
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 2045) struct mount *p;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2046)
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 2047) for (p = mnt; p != end; p = next_mnt(p, mnt)) {
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2048) if (p->mnt_group_id && !IS_MNT_SHARED(p))
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2049) mnt_release_group_id(p);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2050) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2051) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2052)
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2053) static int invent_group_ids(struct mount *mnt, bool recurse)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2054) {
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 2055) struct mount *p;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2056)
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 2057) for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2058) if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2059) int err = mnt_alloc_group_id(p);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2060) if (err) {
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2061) cleanup_group_ids(mnt, p);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2062) return err;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2063) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2064) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2065) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2066)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2067) return 0;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2068) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2069)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2070) int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2071) {
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2072) unsigned int max = READ_ONCE(sysctl_mount_max);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2073) unsigned int mounts = 0, old, pending, sum;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2074) struct mount *p;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2075)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2076) for (p = mnt; p; p = next_mnt(p, mnt))
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2077) mounts++;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2078)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2079) old = ns->mounts;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2080) pending = ns->pending_mounts;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2081) sum = old + pending;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2082) if ((old > sum) ||
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2083) (pending > sum) ||
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2084) (max < sum) ||
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2085) (mounts > (max - sum)))
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2086) return -ENOSPC;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2087)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2088) ns->pending_mounts = pending + mounts;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2089) return 0;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2090) }
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2091)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2092) /*
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2093) * @source_mnt : mount tree to be attached
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2094) * @nd : place the mount tree @source_mnt is attached
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2095) * @parent_nd : if non-null, detach the source_mnt from its parent and
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2096) * store the parent mount and mountpoint dentry.
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2097) * (done when source_mnt is moved)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2098) *
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2099) * NOTE: in the table below explains the semantics when a source mount
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2100) * of a given type is attached to a destination mount of a given type.
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2101) * ---------------------------------------------------------------------------
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2102) * | BIND MOUNT OPERATION |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2103) * |**************************************************************************
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2104) * | source-->| shared | private | slave | unbindable |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2105) * | dest | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2106) * | | | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2107) * | v | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2108) * |**************************************************************************
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2109) * | shared | shared (++) | shared (+) | shared(+++)| invalid |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2110) * | | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2111) * |non-shared| shared (+) | private | slave (*) | invalid |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2112) * ***************************************************************************
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2113) * A bind operation clones the source mount and mounts the clone on the
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2114) * destination mount.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2115) *
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2116) * (++) the cloned mount is propagated to all the mounts in the propagation
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2117) * tree of the destination mount and the cloned mount is added to
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2118) * the peer group of the source mount.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2119) * (+) the cloned mount is created under the destination mount and is marked
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2120) * as shared. The cloned mount is added to the peer group of the source
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2121) * mount.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2122) * (+++) the mount is propagated to all the mounts in the propagation tree
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2123) * of the destination mount and the cloned mount is made slave
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2124) * of the same master as that of the source mount. The cloned mount
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2125) * is marked as 'shared and slave'.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2126) * (*) the cloned mount is made a slave of the same master as that of the
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2127) * source mount.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2128) *
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2129) * ---------------------------------------------------------------------------
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2130) * | MOVE MOUNT OPERATION |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2131) * |**************************************************************************
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2132) * | source-->| shared | private | slave | unbindable |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2133) * | dest | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2134) * | | | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2135) * | v | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2136) * |**************************************************************************
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2137) * | shared | shared (+) | shared (+) | shared(+++) | invalid |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2138) * | | | | | |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2139) * |non-shared| shared (+*) | private | slave (*) | unbindable |
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2140) * ***************************************************************************
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2141) *
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2142) * (+) the mount is moved to the destination. And is then propagated to
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2143) * all the mounts in the propagation tree of the destination mount.
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2144) * (+*) the mount is moved to the destination.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2145) * (+++) the mount is moved to the destination and is then propagated to
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2146) * all the mounts belonging to the destination mount's propagation tree.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2147) * the mount is marked as 'shared and slave'.
5afe002213899 (Ram Pai 2005-11-07 17:21:01 -0500 2148) * (*) the mount continues to be a slave at the new location.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2149) *
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2150) * if the source mount is a tree, the operations explained above is
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2151) * applied to each mount in the tree.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2152) * Must be called without spinlocks held, since this function can sleep
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2153) * in allocations.
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2154) */
0fb54e50562d8 (Al Viro 2011-11-24 19:59:16 -0500 2155) static int attach_recursive_mnt(struct mount *source_mnt,
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2156) struct mount *dest_mnt,
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2157) struct mountpoint *dest_mp,
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2158) bool moving)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2159) {
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2160) struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 2161) HLIST_HEAD(tree_list);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2162) struct mnt_namespace *ns = dest_mnt->mnt_ns;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2163) struct mountpoint *smp;
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 2164) struct mount *child, *p;
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 2165) struct hlist_node *n;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2166) int err;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2167)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2168) /* Preallocate a mountpoint in case the new mounts need
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2169) * to be tucked under other mounts.
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2170) */
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2171) smp = get_mountpoint(source_mnt->mnt.mnt_root);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2172) if (IS_ERR(smp))
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2173) return PTR_ERR(smp);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2174)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2175) /* Is there space to add these mounts to the mount namespace? */
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2176) if (!moving) {
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2177) err = count_mounts(ns, source_mnt);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2178) if (err)
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2179) goto out;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2180) }
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2181)
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2182) if (IS_MNT_SHARED(dest_mnt)) {
0fb54e50562d8 (Al Viro 2011-11-24 19:59:16 -0500 2183) err = invent_group_ids(source_mnt, true);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2184) if (err)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2185) goto out;
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2186) err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2187) lock_mount_hash();
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2188) if (err)
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2189) goto out_cleanup_ids;
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 2190) for (p = source_mnt; p; p = next_mnt(p, source_mnt))
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 2191) set_mnt_shared(p);
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2192) } else {
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2193) lock_mount_hash();
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2194) }
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2195) if (moving) {
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2196) unhash_mnt(source_mnt);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2197) attach_mnt(source_mnt, dest_mnt, dest_mp);
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 2198) touch_mnt_namespace(source_mnt->mnt_ns);
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2199) } else {
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2200) if (source_mnt->mnt_ns) {
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2201) /* move from anon - the caller will destroy */
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2202) list_del_init(&source_mnt->mnt_ns->list);
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2203) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2204) mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2205) commit_tree(source_mnt);
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2206) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2207)
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 2208) hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
1d6a32acd70ab (Al Viro 2014-03-20 20:34:43 -0400 2209) struct mount *q;
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 2210) hlist_del_init(&child->mnt_hash);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2211) q = __lookup_mnt(&child->mnt_parent->mnt,
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2212) child->mnt_mountpoint);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2213) if (q)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2214) mnt_change_mountpoint(child, smp, q);
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2215) /* Notice when we are propagating across user namespaces */
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2216) if (child->mnt_parent->mnt_ns->user_ns != user_ns)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 2217) lock_mnt_tree(child);
d728cf79164bb (Christian Brauner 2019-06-17 23:22:14 +0200 2218) child->mnt.mnt_flags &= ~MNT_LOCKED;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2219) commit_tree(child);
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2220) }
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2221) put_mountpoint(smp);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 2222) unlock_mount_hash();
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 2223)
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2224) return 0;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2225)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2226) out_cleanup_ids:
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2227) while (!hlist_empty(&tree_list)) {
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2228) child = hlist_entry(tree_list.first, struct mount, mnt_hash);
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2229) child->mnt_parent->mnt_ns->pending_mounts = 0;
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 2230) umount_tree(child, UMOUNT_SYNC);
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2231) }
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2232) unlock_mount_hash();
0b1b901b5a98b (Al Viro 2014-03-21 10:14:08 -0400 2233) cleanup_group_ids(source_mnt, NULL);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2234) out:
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 2235) ns->pending_mounts = 0;
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2236)
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2237) read_seqlock_excl(&mount_lock);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2238) put_mountpoint(smp);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2239) read_sequnlock_excl(&mount_lock);
1064f874abc0d (Eric W. Biederman 2017-01-20 18:28:35 +1300 2240)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2241) return err;
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2242) }
b90fa9ae8f51f (Ram Pai 2005-11-07 17:19:50 -0500 2243)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2244) static struct mountpoint *lock_mount(struct path *path)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2245) {
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2246) struct vfsmount *mnt;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2247) struct dentry *dentry = path->dentry;
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2248) retry:
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 2249) inode_lock(dentry->d_inode);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2250) if (unlikely(cant_mount(dentry))) {
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 2251) inode_unlock(dentry->d_inode);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2252) return ERR_PTR(-ENOENT);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2253) }
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2254) namespace_lock();
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2255) mnt = lookup_mnt(path);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2256) if (likely(!mnt)) {
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 2257) struct mountpoint *mp = get_mountpoint(dentry);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2258) if (IS_ERR(mp)) {
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2259) namespace_unlock();
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 2260) inode_unlock(dentry->d_inode);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2261) return mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2262) }
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2263) return mp;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2264) }
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2265) namespace_unlock();
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 2266) inode_unlock(path->dentry->d_inode);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2267) path_put(path);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2268) path->mnt = mnt;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2269) dentry = path->dentry = dget(mnt->mnt_root);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2270) goto retry;
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2271) }
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2272)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2273) static void unlock_mount(struct mountpoint *where)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2274) {
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2275) struct dentry *dentry = where->m_dentry;
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 2276)
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 2277) read_seqlock_excl(&mount_lock);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2278) put_mountpoint(where);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 2279) read_sequnlock_excl(&mount_lock);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 2280)
328e6d9014636 (Al Viro 2013-03-16 14:49:45 -0400 2281) namespace_unlock();
5955102c9984f (Al Viro 2016-01-22 15:40:57 -0500 2282) inode_unlock(dentry->d_inode);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2283) }
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2284)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2285) static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2286) {
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2287) if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2288) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2289)
e36cb0b89ce20 (David Howells 2015-01-29 12:02:35 +0000 2290) if (d_is_dir(mp->m_dentry) !=
e36cb0b89ce20 (David Howells 2015-01-29 12:02:35 +0000 2291) d_is_dir(mnt->mnt.mnt_root))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2292) return -ENOTDIR;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2293)
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2294) return attach_recursive_mnt(mnt, p, mp, false);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2295) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2296)
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2297) /*
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2298) * Sanity check the flags to change_mnt_propagation.
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2299) */
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2300)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2301) static int flags_to_propagation_type(int ms_flags)
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2302) {
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2303) int type = ms_flags & ~(MS_REC | MS_SILENT);
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2304)
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2305) /* Fail if any non-propagation flags are set */
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2306) if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2307) return 0;
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2308) /* Only one propagation flag should be set */
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2309) if (!is_power_of_2(type))
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2310) return 0;
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2311) return type;
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2312) }
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2313)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2314) /*
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2315) * recursively change the type of the mountpoint.
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2316) */
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2317) static int do_change_type(struct path *path, int ms_flags)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2318) {
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 2319) struct mount *m;
4b8b21f4fe16e (Al Viro 2011-11-24 19:54:23 -0500 2320) struct mount *mnt = real_mount(path->mnt);
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2321) int recurse = ms_flags & MS_REC;
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2322) int type;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2323) int err = 0;
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2324)
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2325) if (path->dentry != path->mnt->mnt_root)
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2326) return -EINVAL;
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2327)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2328) type = flags_to_propagation_type(ms_flags);
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2329) if (!type)
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2330) return -EINVAL;
7a2e8a8faab76 (Valerie Aurora 2010-08-26 11:07:22 -0700 2331)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2332) namespace_lock();
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2333) if (type == MS_SHARED) {
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2334) err = invent_group_ids(mnt, recurse);
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2335) if (err)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2336) goto out_unlock;
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2337) }
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2338)
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 2339) lock_mount_hash();
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 2340) for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
0f0afb1dcf01a (Al Viro 2011-11-24 20:43:10 -0500 2341) change_mnt_propagation(m, type);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 2342) unlock_mount_hash();
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2343)
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2344) out_unlock:
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2345) namespace_unlock();
719f5d7f0b90a (Miklos Szeredi 2008-03-27 13:06:23 +0100 2346) return err;
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2347) }
07b20889e3052 (Ram Pai 2005-11-07 17:19:07 -0500 2348)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2349) static struct mount *__do_loopback(struct path *old_path, int recurse)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2350) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2351) struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2352)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2353) if (IS_MNT_UNBINDABLE(old))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2354) return mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2355)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2356) if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2357) return mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2358)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2359) if (!recurse && has_locked_children(old, old_path->dentry))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2360) return mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2361)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2362) if (recurse)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2363) mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2364) else
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2365) mnt = clone_mnt(old, old_path->dentry, 0);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2366)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2367) if (!IS_ERR(mnt))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2368) mnt->mnt.mnt_flags &= ~MNT_LOCKED;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2369)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2370) return mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2371) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2372)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2373) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2374) * do loopback mount.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2375) */
808d4e3cfdcc5 (Al Viro 2012-10-11 11:42:01 -0400 2376) static int do_loopback(struct path *path, const char *old_name,
2dafe1c4d6934 (Eric Sandeen 2008-02-08 04:22:12 -0800 2377) int recurse)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2378) {
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2379) struct path old_path;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2380) struct mount *mnt = NULL, *parent;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2381) struct mountpoint *mp;
57eccb830f1cc (Al Viro 2013-02-22 22:49:10 -0500 2382) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2383) if (!old_name || !*old_name)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2384) return -EINVAL;
815d405ceff0d (Trond Myklebust 2011-09-26 20:36:09 -0400 2385) err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2386) if (err)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2387) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2388)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 2389) err = -EINVAL;
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 2390) if (mnt_ns_loop(old_path.dentry))
dd111b31e951c (David Howells 2017-07-04 17:25:09 +0100 2391) goto out;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 2392)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2393) mp = lock_mount(path);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2394) if (IS_ERR(mp)) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2395) err = PTR_ERR(mp);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2396) goto out;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2397) }
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2398)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2399) parent = real_mount(path->mnt);
e149ed2b805fe (Al Viro 2014-11-01 10:57:28 -0400 2400) if (!check_mnt(parent))
e149ed2b805fe (Al Viro 2014-11-01 10:57:28 -0400 2401) goto out2;
e149ed2b805fe (Al Viro 2014-11-01 10:57:28 -0400 2402)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2403) mnt = __do_loopback(&old_path, recurse);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 2404) if (IS_ERR(mnt)) {
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 2405) err = PTR_ERR(mnt);
e9c5d8a562f01 (Andrey Vagin 2013-04-09 17:33:29 +0400 2406) goto out2;
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 2407) }
ccd48bc7fac28 (Al Viro 2005-11-07 17:15:04 -0500 2408)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2409) err = graft_tree(mnt, parent, mp);
ccd48bc7fac28 (Al Viro 2005-11-07 17:15:04 -0500 2410) if (err) {
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 2411) lock_mount_hash();
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 2412) umount_tree(mnt, UMOUNT_SYNC);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 2413) unlock_mount_hash();
5b83d2c5c0afc (Ram Pai 2005-11-07 17:16:29 -0500 2414) }
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 2415) out2:
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2416) unlock_mount(mp);
ccd48bc7fac28 (Al Viro 2005-11-07 17:15:04 -0500 2417) out:
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2418) path_put(&old_path);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2419) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2420) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2421)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2422) static struct file *open_detached_copy(struct path *path, bool recursive)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2423) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2424) struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2425) struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2426) struct mount *mnt, *p;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2427) struct file *file;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2428)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2429) if (IS_ERR(ns))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2430) return ERR_CAST(ns);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2431)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2432) namespace_lock();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2433) mnt = __do_loopback(path, recursive);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2434) if (IS_ERR(mnt)) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2435) namespace_unlock();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2436) free_mnt_ns(ns);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2437) return ERR_CAST(mnt);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2438) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2439)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2440) lock_mount_hash();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2441) for (p = mnt; p; p = next_mnt(p, mnt)) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2442) p->mnt_ns = ns;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2443) ns->mounts++;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2444) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2445) ns->root = mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2446) list_add_tail(&ns->list, &mnt->mnt_list);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2447) mntget(&mnt->mnt);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2448) unlock_mount_hash();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2449) namespace_unlock();
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2450)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2451) mntput(path->mnt);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2452) path->mnt = &mnt->mnt;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2453) file = dentry_open(path, O_PATH, current_cred());
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2454) if (IS_ERR(file))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2455) dissolve_on_fput(path->mnt);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2456) else
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2457) file->f_mode |= FMODE_NEED_UNMOUNT;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2458) return file;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2459) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2460)
2658ce095df58 (Ben Dooks 2019-10-15 11:35:02 +0100 2461) SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2462) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2463) struct file *file;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2464) struct path path;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2465) int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2466) bool detached = flags & OPEN_TREE_CLONE;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2467) int error;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2468) int fd;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2469)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2470) BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2471)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2472) if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2473) AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2474) OPEN_TREE_CLOEXEC))
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2475) return -EINVAL;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2476)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2477) if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2478) return -EINVAL;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2479)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2480) if (flags & AT_NO_AUTOMOUNT)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2481) lookup_flags &= ~LOOKUP_AUTOMOUNT;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2482) if (flags & AT_SYMLINK_NOFOLLOW)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2483) lookup_flags &= ~LOOKUP_FOLLOW;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2484) if (flags & AT_EMPTY_PATH)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2485) lookup_flags |= LOOKUP_EMPTY;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2486)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2487) if (detached && !may_mount())
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2488) return -EPERM;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2489)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2490) fd = get_unused_fd_flags(flags & O_CLOEXEC);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2491) if (fd < 0)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2492) return fd;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2493)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2494) error = user_path_at(dfd, filename, lookup_flags, &path);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2495) if (unlikely(error)) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2496) file = ERR_PTR(error);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2497) } else {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2498) if (detached)
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2499) file = open_detached_copy(&path, flags & AT_RECURSIVE);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2500) else
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2501) file = dentry_open(&path, O_PATH, current_cred());
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2502) path_put(&path);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2503) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2504) if (IS_ERR(file)) {
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2505) put_unused_fd(fd);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2506) return PTR_ERR(file);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2507) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2508) fd_install(fd, file);
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2509) return fd;
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2510) }
a07b20004793d (Al Viro 2018-11-05 17:40:30 +0000 2511)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2512) /*
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2513) * Don't allow locked mount flags to be cleared.
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2514) *
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2515) * No locks need to be held here while testing the various MNT_LOCK
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2516) * flags because those flags can never be cleared once they are set.
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2517) */
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2518) static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2519) {
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2520) unsigned int fl = mnt->mnt.mnt_flags;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2521)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2522) if ((fl & MNT_LOCK_READONLY) &&
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2523) !(mnt_flags & MNT_READONLY))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2524) return false;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2525)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2526) if ((fl & MNT_LOCK_NODEV) &&
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2527) !(mnt_flags & MNT_NODEV))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2528) return false;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2529)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2530) if ((fl & MNT_LOCK_NOSUID) &&
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2531) !(mnt_flags & MNT_NOSUID))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2532) return false;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2533)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2534) if ((fl & MNT_LOCK_NOEXEC) &&
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2535) !(mnt_flags & MNT_NOEXEC))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2536) return false;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2537)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2538) if ((fl & MNT_LOCK_ATIME) &&
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2539) ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2540) return false;
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2541)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2542) return true;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2543) }
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2544)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2545) static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2546) {
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2547) bool readonly_request = (mnt_flags & MNT_READONLY);
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2548)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2549) if (readonly_request == __mnt_is_readonly(&mnt->mnt))
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2550) return 0;
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2551)
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2552) if (readonly_request)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2553) return mnt_make_readonly(mnt);
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2554)
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2555) mnt->mnt.mnt_flags &= ~MNT_READONLY;
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2556) return 0;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2557) }
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2558)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2559) static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2560) {
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2561) mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2562) mnt->mnt.mnt_flags = mnt_flags;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2563) touch_mnt_namespace(mnt->mnt_ns);
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2564) }
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2565)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2566) static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2567) {
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2568) struct super_block *sb = mnt->mnt_sb;
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2569)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2570) if (!__mnt_is_readonly(mnt) &&
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2571) (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2572) char *buf = (char *)__get_free_page(GFP_KERNEL);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2573) char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2574) struct tm tm;
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2575)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2576) time64_to_tm(sb->s_time_max, 0, &tm);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2577)
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2578) pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2579) sb->s_type->name,
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2580) is_mounted(mnt) ? "remounted" : "mounted",
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2581) mntpath,
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2582) tm.tm_year+1900, (unsigned long long)sb->s_time_max);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2583)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2584) free_page((unsigned long)buf);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2585) }
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2586) }
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2587)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2588) /*
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2589) * Handle reconfiguration of the mountpoint only without alteration of the
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2590) * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2591) * to mount(2).
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2592) */
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2593) static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2594) {
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2595) struct super_block *sb = path->mnt->mnt_sb;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2596) struct mount *mnt = real_mount(path->mnt);
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2597) int ret;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2598)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2599) if (!check_mnt(mnt))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2600) return -EINVAL;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2601)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2602) if (path->dentry != mnt->mnt.mnt_root)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2603) return -EINVAL;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2604)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2605) if (!can_change_locked_flags(mnt, mnt_flags))
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2606) return -EPERM;
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2607)
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2608) /*
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2609) * We're only checking whether the superblock is read-only not
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2610) * changing it, so only take down_read(&sb->s_umount).
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2611) */
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2612) down_read(&sb->s_umount);
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2613) lock_mount_hash();
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2614) ret = change_mount_ro_state(mnt, mnt_flags);
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2615) if (ret == 0)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2616) set_mount_attributes(mnt, mnt_flags);
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2617) unlock_mount_hash();
e58ace1a0fa9d (Christian Brauner 2021-01-21 14:19:50 +0100 2618) up_read(&sb->s_umount);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2619)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2620) mnt_warn_timestamp_expiry(path, &mnt->mnt);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2621)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2622) return ret;
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2623) }
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 2624)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2625) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2626) * change filesystem flags. dir should be a physical root of filesystem.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2627) * If you've mounted a non-root directory somewhere and want to do remount
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2628) * on it - tough luck.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2629) */
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2630) static int do_remount(struct path *path, int ms_flags, int sb_flags,
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2631) int mnt_flags, void *data)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2632) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2633) int err;
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2634) struct super_block *sb = path->mnt->mnt_sb;
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 2635) struct mount *mnt = real_mount(path->mnt);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2636) struct fs_context *fc;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2637)
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 2638) if (!check_mnt(mnt))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2639) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2640)
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2641) if (path->dentry != path->mnt->mnt_root)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2642) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2643)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 2644) if (!can_change_locked_flags(mnt, mnt_flags))
9566d6742852c (Eric W. Biederman 2014-07-28 17:26:07 -0700 2645) return -EPERM;
9566d6742852c (Eric W. Biederman 2014-07-28 17:26:07 -0700 2646)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2647) fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2648) if (IS_ERR(fc))
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2649) return PTR_ERR(fc);
ff36fe2c845ca (Eric Paris 2011-03-03 16:09:14 -0500 2650)
b330966f79fb4 (Miklos Szeredi 2020-07-14 14:45:41 +0200 2651) fc->oldapi = true;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2652) err = parse_monolithic_mount_data(fc, data);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2653) if (!err) {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2654) down_write(&sb->s_umount);
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2655) err = -EPERM;
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2656) if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2657) err = reconfigure_super(fc);
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2658) if (!err) {
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2659) lock_mount_hash();
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2660) set_mount_attributes(mnt, mnt_flags);
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2661) unlock_mount_hash();
68847c9417004 (Christian Brauner 2021-01-21 14:19:48 +0100 2662) }
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2663) }
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2664) up_write(&sb->s_umount);
0e55a7cca4b66 (Dan Williams 2008-09-26 19:01:20 -0700 2665) }
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2666)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2667) mnt_warn_timestamp_expiry(path, &mnt->mnt);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2668)
8d0347f6c3a9d (David Howells 2018-11-04 09:28:36 -0500 2669) put_fs_context(fc);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2670) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2671) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2672)
cbbe362cd6844 (Al Viro 2011-11-24 20:01:19 -0500 2673) static inline int tree_contains_unbindable(struct mount *mnt)
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2674) {
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 2675) struct mount *p;
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 2676) for (p = mnt; p; p = next_mnt(p, mnt)) {
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2677) if (IS_MNT_UNBINDABLE(p))
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2678) return 1;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2679) }
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2680) return 0;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2681) }
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2682)
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2683) /*
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2684) * Check that there aren't references to earlier/same mount namespaces in the
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2685) * specified subtree. Such references can act as pins for mount namespaces
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2686) * that aren't checked by the mount-cycle checking code, thereby allowing
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2687) * cycles to be made.
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2688) */
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2689) static bool check_for_nsfs_mounts(struct mount *subtree)
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2690) {
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2691) struct mount *p;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2692) bool ret = false;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2693)
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2694) lock_mount_hash();
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2695) for (p = subtree; p; p = next_mnt(p, subtree))
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2696) if (mnt_ns_loop(p->mnt.mnt_root))
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2697) goto out;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2698)
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2699) ret = true;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2700) out:
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2701) unlock_mount_hash();
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2702) return ret;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2703) }
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2704)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2705) static int do_move_mount(struct path *old_path, struct path *new_path)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2706) {
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2707) struct mnt_namespace *ns;
676da58df740f (Al Viro 2011-11-24 21:47:05 -0500 2708) struct mount *p;
0fb54e50562d8 (Al Viro 2011-11-24 19:59:16 -0500 2709) struct mount *old;
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2710) struct mount *parent;
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2711) struct mountpoint *mp, *old_mp;
57eccb830f1cc (Al Viro 2013-02-22 22:49:10 -0500 2712) int err;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2713) bool attached;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2714)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2715) mp = lock_mount(new_path);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2716) if (IS_ERR(mp))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2717) return PTR_ERR(mp);
cc53ce53c8692 (David Howells 2011-01-14 18:45:26 +0000 2718)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2719) old = real_mount(old_path->mnt);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2720) p = real_mount(new_path->mnt);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2721) parent = old->mnt_parent;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2722) attached = mnt_has_parent(old);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2723) old_mp = old->mnt_mp;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2724) ns = old->mnt_ns;
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 2725)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2726) err = -EINVAL;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2727) /* The mountpoint must be in our namespace. */
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2728) if (!check_mnt(p))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2729) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2730)
570d7a98e7d6d (Eric Biggers 2019-06-29 13:27:44 -0700 2731) /* The thing moved must be mounted... */
570d7a98e7d6d (Eric Biggers 2019-06-29 13:27:44 -0700 2732) if (!is_mounted(&old->mnt))
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2733) goto out;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2734)
570d7a98e7d6d (Eric Biggers 2019-06-29 13:27:44 -0700 2735) /* ... and either ours or the root of anon namespace */
570d7a98e7d6d (Eric Biggers 2019-06-29 13:27:44 -0700 2736) if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2737) goto out;
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 2738)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2739) if (old->mnt.mnt_flags & MNT_LOCKED)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2740) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2741)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2742) if (old_path->dentry != old_path->mnt->mnt_root)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2743) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2744)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2745) if (d_is_dir(new_path->dentry) !=
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2746) d_is_dir(old_path->dentry))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2747) goto out;
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2748) /*
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2749) * Don't move a mount residing in a shared parent.
2144440327fa0 (Ram Pai 2005-11-07 17:20:03 -0500 2750) */
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2751) if (attached && IS_MNT_SHARED(parent))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2752) goto out;
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2753) /*
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2754) * Don't move a mount tree containing unbindable mounts to a destination
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2755) * mount which is shared.
9676f0c6389b6 (Ram Pai 2005-11-07 17:21:20 -0500 2756) */
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2757) if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2758) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2759) err = -ELOOP;
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2760) if (!check_for_nsfs_mounts(old))
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2761) goto out;
fc7be130c7e91 (Al Viro 2011-11-25 01:05:37 -0500 2762) for (; mnt_has_parent(p); p = p->mnt_parent)
676da58df740f (Al Viro 2011-11-24 21:47:05 -0500 2763) if (p == old)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2764) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2765)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2766) err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2767) attached);
4ac9137858e08 (Jan Blunck 2008-02-14 19:34:32 -0800 2768) if (err)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2769) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2770)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2771) /* if the mount is moved, it should no longer be expire
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2772) * automatically */
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 2773) list_del_init(&old->mnt_expire);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2774) if (attached)
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2775) put_mountpoint(old_mp);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2776) out:
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2777) unlock_mount(mp);
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2778) if (!err) {
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2779) if (attached)
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2780) mntput_no_expire(parent);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 2781) else
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2782) free_mnt_ns(ns);
44dfd84a6d54a (David Howells 2018-11-05 17:40:31 +0000 2783) }
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2784) return err;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2785) }
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2786)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2787) static int do_move_mount_old(struct path *path, const char *old_name)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2788) {
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2789) struct path old_path;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2790) int err;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2791)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2792) if (!old_name || !*old_name)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2793) return -EINVAL;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2794)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2795) err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2796) if (err)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2797) return err;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2798)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 2799) err = do_move_mount(&old_path, path);
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 2800) path_put(&old_path);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2801) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2802) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2803)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2804) /*
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2805) * add a mount into a namespace's mount tree
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2806) */
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2807) static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2808) struct path *path, int mnt_flags)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2809) {
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2810) struct mount *parent = real_mount(path->mnt);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2811)
f2ebb3a921c1c (Al Viro 2014-02-27 09:35:45 -0500 2812) mnt_flags &= ~MNT_INTERNAL_FLAGS;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2813)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2814) if (unlikely(!check_mnt(parent))) {
156cacb1d0d36 (Al Viro 2012-09-21 08:19:02 -0400 2815) /* that's acceptable only for automounts done in private ns */
156cacb1d0d36 (Al Viro 2012-09-21 08:19:02 -0400 2816) if (!(mnt_flags & MNT_SHRINKABLE))
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2817) return -EINVAL;
156cacb1d0d36 (Al Viro 2012-09-21 08:19:02 -0400 2818) /* ... and for those we'd better have mountpoint still alive */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 2819) if (!parent->mnt_ns)
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2820) return -EINVAL;
156cacb1d0d36 (Al Viro 2012-09-21 08:19:02 -0400 2821) }
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2822)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2823) /* Refuse the same filesystem on the same mount point */
95bc5f25c10fc (Al Viro 2011-11-25 00:30:56 -0500 2824) if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2825) path->mnt->mnt_root == path->dentry)
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2826) return -EBUSY;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2827)
e36cb0b89ce20 (David Howells 2015-01-29 12:02:35 +0000 2828) if (d_is_symlink(newmnt->mnt.mnt_root))
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2829) return -EINVAL;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2830)
95bc5f25c10fc (Al Viro 2011-11-25 00:30:56 -0500 2831) newmnt->mnt.mnt_flags = mnt_flags;
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2832) return graft_tree(newmnt, parent, mp);
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 2833) }
b1e75df45a3d8 (Al Viro 2011-01-17 01:47:59 -0500 2834)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2835) static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2836)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2837) /*
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2838) * Create a new mount using a superblock configuration and request it
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2839) * be added to the namespace tree.
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2840) */
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2841) static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2842) unsigned int mnt_flags)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2843) {
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2844) struct vfsmount *mnt;
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2845) struct mountpoint *mp;
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2846) struct super_block *sb = fc->root->d_sb;
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2847) int error;
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2848)
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2849) error = security_sb_kern_mount(sb);
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2850) if (!error && mount_too_revealing(sb, &mnt_flags))
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2851) error = -EPERM;
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2852)
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2853) if (unlikely(error)) {
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2854) fc_drop_locked(fc);
c9ce29ed795fa (Al Viro 2018-12-20 15:04:50 -0500 2855) return error;
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2856) }
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2857)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2858) up_write(&sb->s_umount);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2859)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2860) mnt = vfs_create_mount(fc);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2861) if (IS_ERR(mnt))
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2862) return PTR_ERR(mnt);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2863)
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2864) mnt_warn_timestamp_expiry(mountpoint, mnt);
f8b92ba67c5d3 (Deepa Dinamani 2019-04-15 14:17:12 -0700 2865)
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2866) mp = lock_mount(mountpoint);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2867) if (IS_ERR(mp)) {
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2868) mntput(mnt);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2869) return PTR_ERR(mp);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2870) }
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2871) error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2872) unlock_mount(mp);
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2873) if (error < 0)
0ecee66990644 (Eric Biggers 2019-10-16 19:48:14 -0700 2874) mntput(mnt);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2875) return error;
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2876) }
1b852bceb0d11 (Eric W. Biederman 2015-05-08 23:22:29 -0500 2877)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2878) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2879) * create a new mount for userspace and request it to be added into the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2880) * namespace's tree
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2881) */
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 2882) static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
808d4e3cfdcc5 (Al Viro 2012-10-11 11:42:01 -0400 2883) int mnt_flags, const char *name, void *data)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2884) {
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2885) struct file_system_type *type;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2886) struct fs_context *fc;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2887) const char *subtype = NULL;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2888) int err = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2889)
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2890) if (!fstype)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2891) return -EINVAL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2892)
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2893) type = get_fs_type(fstype);
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2894) if (!type)
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2895) return -ENODEV;
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2896)
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2897) if (type->fs_flags & FS_HAS_SUBTYPE) {
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2898) subtype = strchr(fstype, '.');
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2899) if (subtype) {
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2900) subtype++;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2901) if (!*subtype) {
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2902) put_filesystem(type);
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2903) return -EINVAL;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2904) }
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2905) }
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2906) }
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2907)
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2908) fc = fs_context_for_mount(type, sb_flags);
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 2909) put_filesystem(type);
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2910) if (IS_ERR(fc))
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2911) return PTR_ERR(fc);
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2912)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 2913) if (subtype)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 2914) err = vfs_parse_fs_string(fc, "subtype",
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 2915) subtype, strlen(subtype));
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 2916) if (!err && name)
3e1aeb00e6d13 (David Howells 2018-11-01 23:07:25 +0000 2917) err = vfs_parse_fs_string(fc, "source", name, strlen(name));
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2918) if (!err)
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2919) err = parse_monolithic_mount_data(fc, data);
c3aabf0780a50 (Al Viro 2019-05-13 12:57:22 -0400 2920) if (!err && !mount_capable(fc))
c3aabf0780a50 (Al Viro 2019-05-13 12:57:22 -0400 2921) err = -EPERM;
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2922) if (!err)
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2923) err = vfs_get_tree(fc);
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2924) if (!err)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 2925) err = do_new_mount_fc(fc, path, mnt_flags);
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 2926)
a0c9a8b8fd9fd (Al Viro 2018-11-04 07:18:51 -0500 2927) put_fs_context(fc);
15f9a3f3e1996 (Al Viro 2011-01-17 01:41:58 -0500 2928) return err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2929) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2930)
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2931) int finish_automount(struct vfsmount *m, struct path *path)
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2932) {
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2933) struct dentry *dentry = path->dentry;
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2934) struct mountpoint *mp;
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2935) struct mount *mnt;
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2936) int err;
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2937)
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2938) if (!m)
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2939) return 0;
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2940) if (IS_ERR(m))
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2941) return PTR_ERR(m);
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2942)
25e195aa1e607 (Al Viro 2020-01-11 11:27:46 -0500 2943) mnt = real_mount(m);
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2944) /* The new mount record should have at least 2 refs to prevent it being
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2945) * expired before we get a chance to add it
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2946) */
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 2947) BUG_ON(mnt_get_count(mnt) < 2);
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2948)
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2949) if (m->mnt_sb == path->mnt->mnt_sb &&
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2950) m->mnt_root == dentry) {
b1e75df45a3d8 (Al Viro 2011-01-17 01:47:59 -0500 2951) err = -ELOOP;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2952) goto discard;
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2953) }
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2954)
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2955) /*
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2956) * we don't want to use lock_mount() - in this case finding something
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2957) * that overmounts our mountpoint to be means "quitely drop what we've
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2958) * got", not "try to mount it on top".
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2959) */
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2960) inode_lock(dentry->d_inode);
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2961) namespace_lock();
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2962) if (unlikely(cant_mount(dentry))) {
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2963) err = -ENOENT;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2964) goto discard_locked;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2965) }
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2966) rcu_read_lock();
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2967) if (unlikely(__lookup_mnt(path->mnt, dentry))) {
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2968) rcu_read_unlock();
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2969) err = 0;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2970) goto discard_locked;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2971) }
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2972) rcu_read_unlock();
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2973) mp = get_mountpoint(dentry);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2974) if (IS_ERR(mp)) {
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2975) err = PTR_ERR(mp);
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2976) goto discard_locked;
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2977) }
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2978)
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2979) err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
8f11538ebe984 (Al Viro 2020-01-11 10:14:09 -0500 2980) unlock_mount(mp);
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2981) if (unlikely(err))
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2982) goto discard;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2983) mntput(m);
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2984) return 0;
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2985)
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2986) discard_locked:
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2987) namespace_unlock();
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2988) inode_unlock(dentry->d_inode);
26df6034fdb21 (Al Viro 2020-01-11 10:44:29 -0500 2989) discard:
b1e75df45a3d8 (Al Viro 2011-01-17 01:47:59 -0500 2990) /* remove m from any expiration list it may be on */
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 2991) if (!list_empty(&mnt->mnt_expire)) {
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2992) namespace_lock();
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 2993) list_del_init(&mnt->mnt_expire);
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 2994) namespace_unlock();
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2995) }
b1e75df45a3d8 (Al Viro 2011-01-17 01:47:59 -0500 2996) mntput(m);
b1e75df45a3d8 (Al Viro 2011-01-17 01:47:59 -0500 2997) mntput(m);
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2998) return err;
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 2999) }
19a167af7c972 (Al Viro 2011-01-17 01:35:23 -0500 3000)
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3001) /**
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3002) * mnt_set_expiry - Put a mount on an expiration list
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3003) * @mnt: The mount to list.
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3004) * @expiry_list: The list to add the mount to.
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3005) */
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3006) void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3007) {
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 3008) namespace_lock();
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3009)
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3010) list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3011)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 3012) namespace_unlock();
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3013) }
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3014) EXPORT_SYMBOL(mnt_set_expiry);
ea5b778a8b98c (David Howells 2011-01-14 19:10:03 +0000 3015)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3016) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3017) * process a list of expirable mountpoints with the intent of discarding any
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3018) * mountpoints that aren't in use and haven't been touched since last we came
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3019) * here
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3020) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3021) void mark_mounts_for_expiry(struct list_head *mounts)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3022) {
761d5c38eb3d8 (Al Viro 2011-11-24 21:07:43 -0500 3023) struct mount *mnt, *next;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3024) LIST_HEAD(graveyard);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3025)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3026) if (list_empty(mounts))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3027) return;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3028)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 3029) namespace_lock();
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 3030) lock_mount_hash();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3031)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3032) /* extract from the expiration list every vfsmount that matches the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3033) * following criteria:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3034) * - only referenced by its parent vfsmount
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3035) * - still marked for expiry (marked on the last call here; marks are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3036) * cleared by mntput())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3037) */
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3038) list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
863d684f946eb (Al Viro 2011-11-25 00:57:42 -0500 3039) if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 3040) propagate_mount_busy(mnt, 1))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3041) continue;
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3042) list_move(&mnt->mnt_expire, &graveyard);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3043) }
bcc5c7d2b692e (Al Viro 2008-03-22 00:21:53 -0400 3044) while (!list_empty(&graveyard)) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3045) mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 3046) touch_mnt_namespace(mnt->mnt_ns);
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 3047) umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2b692e (Al Viro 2008-03-22 00:21:53 -0400 3048) }
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 3049) unlock_mount_hash();
3ab6abee59ac9 (Al Viro 2013-03-16 14:42:19 -0400 3050) namespace_unlock();
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3051) }
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3052)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3053) EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3054)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3055) /*
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3056) * Ripoff of 'select_parent()'
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3057) *
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3058) * search the list of submounts for a given mountpoint, and move any
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3059) * shrinkable submounts to the 'graveyard' list.
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3060) */
692afc312b38c (Al Viro 2011-11-24 21:15:14 -0500 3061) static int select_submounts(struct mount *parent, struct list_head *graveyard)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3062) {
692afc312b38c (Al Viro 2011-11-24 21:15:14 -0500 3063) struct mount *this_parent = parent;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3064) struct list_head *next;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3065) int found = 0;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3066)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3067) repeat:
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 3068) next = this_parent->mnt_mounts.next;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3069) resume:
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 3070) while (next != &this_parent->mnt_mounts) {
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3071) struct list_head *tmp = next;
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 3072) struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3073)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3074) next = tmp->next;
692afc312b38c (Al Viro 2011-11-24 21:15:14 -0500 3075) if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3076) continue;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3077) /*
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3078) * Descend a level if the d_mounts list is non-empty.
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3079) */
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 3080) if (!list_empty(&mnt->mnt_mounts)) {
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3081) this_parent = mnt;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3082) goto repeat;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3083) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3084)
1ab597386205f (Al Viro 2011-11-24 21:35:16 -0500 3085) if (!propagate_mount_busy(mnt, 1)) {
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3086) list_move_tail(&mnt->mnt_expire, graveyard);
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3087) found++;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3088) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3089) }
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3090) /*
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3091) * All done at this level ... ascend and resume the search
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3092) */
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3093) if (this_parent != parent) {
6b41d536f7c84 (Al Viro 2011-11-24 23:24:33 -0500 3094) next = this_parent->mnt_child.next;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 3095) this_parent = this_parent->mnt_parent;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3096) goto resume;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3097) }
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3098) return found;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3099) }
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3100)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3101) /*
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3102) * process a list of expirable mountpoints with the intent of discarding any
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3103) * submounts of a specific parent mountpoint
99b7db7b8ffd6 (Nicholas Piggin 2010-08-18 04:37:39 +1000 3104) *
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 3105) * mount_lock must be held for write
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3106) */
b54b9be7824d8 (Al Viro 2013-03-16 14:39:34 -0400 3107) static void shrink_submounts(struct mount *mnt)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3108) {
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3109) LIST_HEAD(graveyard);
761d5c38eb3d8 (Al Viro 2011-11-24 21:07:43 -0500 3110) struct mount *m;
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3111)
5528f911b4c43 (Trond Myklebust 2006-06-09 09:34:17 -0400 3112) /* extract submounts of 'mountpoint' from the expiration list */
c35038becad0a (Al Viro 2008-03-22 00:46:23 -0400 3113) while (select_submounts(mnt, &graveyard)) {
bcc5c7d2b692e (Al Viro 2008-03-22 00:21:53 -0400 3114) while (!list_empty(&graveyard)) {
761d5c38eb3d8 (Al Viro 2011-11-24 21:07:43 -0500 3115) m = list_first_entry(&graveyard, struct mount,
6776db3d32b2a (Al Viro 2011-11-25 00:22:05 -0500 3116) mnt_expire);
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 3117) touch_mnt_namespace(m->mnt_ns);
e819f152104c9 (Eric W. Biederman 2014-12-24 07:20:01 -0600 3118) umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
bcc5c7d2b692e (Al Viro 2008-03-22 00:21:53 -0400 3119) }
bcc5c7d2b692e (Al Viro 2008-03-22 00:21:53 -0400 3120) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3121) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3122)
028abd9222df0 (Christoph Hellwig 2020-09-17 10:22:34 +0200 3123) static void *copy_mount_options(const void __user * data)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3124) {
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3125) char *copy;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3126) unsigned left, offset;
b58fed8b1959d (Ram Pai 2005-11-07 17:16:09 -0500 3127)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3128) if (!data)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3129) return NULL;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3130)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3131) copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3132) if (!copy)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3133) return ERR_PTR(-ENOMEM);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3134)
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3135) left = copy_from_user(copy, data, PAGE_SIZE);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3136)
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3137) /*
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3138) * Not all architectures have an exact copy_from_user(). Resort to
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3139) * byte at a time.
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3140) */
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3141) offset = PAGE_SIZE - left;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3142) while (left) {
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3143) char c;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3144) if (get_user(c, (const char __user *)data + offset))
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3145) break;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3146) copy[offset] = c;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3147) left--;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3148) offset++;
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3149) }
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3150)
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3151) if (left == PAGE_SIZE) {
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3152) kfree(copy);
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3153) return ERR_PTR(-EFAULT);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3154) }
d563d678aa0be (Catalin Marinas 2020-07-01 17:46:06 +0100 3155)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3156) return copy;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3157) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3158)
028abd9222df0 (Christoph Hellwig 2020-09-17 10:22:34 +0200 3159) static char *copy_mount_string(const void __user *data)
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3160) {
fbdb440132023 (Chandan Rajendra 2019-01-22 12:21:52 +0530 3161) return data ? strndup_user(data, PATH_MAX) : NULL;
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3162) }
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3163)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3164) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3165) * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3166) * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3167) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3168) * data is a (void *) that can point to any structure up to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3169) * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3170) * information (or be NULL).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3171) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3172) * Pre-0.97 versions of mount() didn't have a flags word.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3173) * When the flags word was introduced its top half was required
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3174) * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3175) * Therefore, if this magic number is present, it carries no information
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3176) * and must be discarded.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3177) */
c60166f04283f (Christoph Hellwig 2020-07-21 11:12:08 +0200 3178) int path_mount(const char *dev_name, struct path *path,
808d4e3cfdcc5 (Al Viro 2012-10-11 11:42:01 -0400 3179) const char *type_page, unsigned long flags, void *data_page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3180) {
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3181) unsigned int mnt_flags = 0, sb_flags;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3182) int ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3183)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3184) /* Discard magic */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3185) if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3186) flags &= ~MS_MGC_MSK;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3187)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3188) /* Basic sanity checks */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3189) if (data_page)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3190) ((char *)data_page)[PAGE_SIZE - 1] = 0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3191)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3192) if (flags & MS_NOUSER)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3193) return -EINVAL;
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3194)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3195) ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3196) if (ret)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3197) return ret;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3198) if (!may_mount())
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3199) return -EPERM;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3200) if ((flags & SB_MANDLOCK) && !may_mandlock())
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3201) return -EPERM;
a27ab9f26b729 (Tetsuo Handa 2009-10-04 21:49:49 +0900 3202)
613cbe3d48704 (Andi Kleen 2009-04-19 18:40:43 +0200 3203) /* Default to relatime unless overriden */
613cbe3d48704 (Andi Kleen 2009-04-19 18:40:43 +0200 3204) if (!(flags & MS_NOATIME))
613cbe3d48704 (Andi Kleen 2009-04-19 18:40:43 +0200 3205) mnt_flags |= MNT_RELATIME;
0a1c01c947760 (Matthew Garrett 2009-03-26 17:53:14 +0000 3206)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3207) /* Separate the per-mountpoint flags */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3208) if (flags & MS_NOSUID)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3209) mnt_flags |= MNT_NOSUID;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3210) if (flags & MS_NODEV)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3211) mnt_flags |= MNT_NODEV;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3212) if (flags & MS_NOEXEC)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3213) mnt_flags |= MNT_NOEXEC;
fc33a7bb9c6dd (Christoph Hellwig 2006-01-09 20:52:17 -0800 3214) if (flags & MS_NOATIME)
fc33a7bb9c6dd (Christoph Hellwig 2006-01-09 20:52:17 -0800 3215) mnt_flags |= MNT_NOATIME;
fc33a7bb9c6dd (Christoph Hellwig 2006-01-09 20:52:17 -0800 3216) if (flags & MS_NODIRATIME)
fc33a7bb9c6dd (Christoph Hellwig 2006-01-09 20:52:17 -0800 3217) mnt_flags |= MNT_NODIRATIME;
d0adde574b848 (Matthew Garrett 2009-03-26 17:49:56 +0000 3218) if (flags & MS_STRICTATIME)
d0adde574b848 (Matthew Garrett 2009-03-26 17:49:56 +0000 3219) mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
a9e5b73288cf1 (David Howells 2018-04-20 13:35:02 +0100 3220) if (flags & MS_RDONLY)
2e4b7fcd92600 (Dave Hansen 2008-02-15 14:38:00 -0800 3221) mnt_flags |= MNT_READONLY;
dab741e0e02bd (Mattias Nissler 2020-08-27 11:09:46 -0600 3222) if (flags & MS_NOSYMFOLLOW)
dab741e0e02bd (Mattias Nissler 2020-08-27 11:09:46 -0600 3223) mnt_flags |= MNT_NOSYMFOLLOW;
fc33a7bb9c6dd (Christoph Hellwig 2006-01-09 20:52:17 -0800 3224)
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3225) /* The default atime for remount is preservation */
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3226) if ((flags & MS_REMOUNT) &&
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3227) ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3228) MS_STRICTATIME)) == 0)) {
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3229) mnt_flags &= ~MNT_ATIME_MASK;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3230) mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3231) }
ffbc6f0ead47f (Eric W. Biederman 2014-07-28 17:36:04 -0700 3232)
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3233) sb_flags = flags & (SB_RDONLY |
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3234) SB_SYNCHRONOUS |
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3235) SB_MANDLOCK |
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3236) SB_DIRSYNC |
e462ec50cb5fa (David Howells 2017-07-17 08:45:35 +0100 3237) SB_SILENT |
917086ff231f6 (Mimi Zohar 2017-10-08 00:28:21 -0400 3238) SB_POSIXACL |
d7ee946942bdd (Markus Trippelsdorf 2017-10-11 07:01:31 +0200 3239) SB_LAZYTIME |
917086ff231f6 (Mimi Zohar 2017-10-08 00:28:21 -0400 3240) SB_I_VERSION);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3241)
43f5e655eff7e (David Howells 2018-11-01 23:07:25 +0000 3242) if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3243) return do_reconfigure_mnt(path, mnt_flags);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3244) if (flags & MS_REMOUNT)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3245) return do_remount(path, flags, sb_flags, mnt_flags, data_page);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3246) if (flags & MS_BIND)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3247) return do_loopback(path, dev_name, flags & MS_REC);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3248) if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3249) return do_change_type(path, flags);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3250) if (flags & MS_MOVE)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3251) return do_move_mount_old(path, dev_name);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3252)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3253) return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3254) data_page);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3255) }
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3256)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3257) long do_mount(const char *dev_name, const char __user *dir_name,
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3258) const char *type_page, unsigned long flags, void *data_page)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3259) {
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3260) struct path path;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3261) int ret;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3262)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3263) ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3264) if (ret)
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3265) return ret;
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3266) ret = path_mount(dev_name, &path, type_page, flags, data_page);
2d92ab3c6279f (Al Viro 2008-08-02 00:51:11 -0400 3267) path_put(&path);
a1e6aaa3743a3 (Christoph Hellwig 2020-06-04 09:41:08 +0200 3268) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3269) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3270)
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3271) static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3272) {
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3273) return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3274) }
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3275)
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3276) static void dec_mnt_namespaces(struct ucounts *ucounts)
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3277) {
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3278) dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3279) }
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3280)
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3281) static void free_mnt_ns(struct mnt_namespace *ns)
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3282) {
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3283) if (!is_anon_ns(ns))
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3284) ns_free_inum(&ns->ns);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3285) dec_mnt_namespaces(ns->ucounts);
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3286) put_user_ns(ns->user_ns);
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3287) kfree(ns);
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3288) }
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3289)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3290) /*
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3291) * Assign a sequence number so we can detect when we attempt to bind
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3292) * mount a reference to an older mount namespace into the current
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3293) * mount namespace, preventing reference counting loops. A 64bit
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3294) * number incrementing at 10Ghz will take 12,427 years to wrap which
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3295) * is effectively never, so we can ignore the possibility.
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3296) */
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3297) static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 3298)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3299) static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3300) {
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3301) struct mnt_namespace *new_ns;
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3302) struct ucounts *ucounts;
98f842e675f96 (Eric W. Biederman 2011-06-15 10:21:48 -0700 3303) int ret;
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3304)
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3305) ucounts = inc_mnt_namespaces(user_ns);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3306) if (!ucounts)
df75e7748bae1 (Eric W. Biederman 2016-09-22 13:08:36 -0500 3307) return ERR_PTR(-ENOSPC);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3308)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3309) new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3310) if (!new_ns) {
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3311) dec_mnt_namespaces(ucounts);
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3312) return ERR_PTR(-ENOMEM);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3313) }
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3314) if (!anon) {
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3315) ret = ns_alloc_inum(&new_ns->ns);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3316) if (ret) {
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3317) kfree(new_ns);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3318) dec_mnt_namespaces(ucounts);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3319) return ERR_PTR(ret);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3320) }
98f842e675f96 (Eric W. Biederman 2011-06-15 10:21:48 -0700 3321) }
33c429405a2c8 (Al Viro 2014-11-01 02:32:53 -0400 3322) new_ns->ns.ops = &mntns_operations;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3323) if (!anon)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3324) new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
1a7b8969e664d (Kirill Tkhai 2020-08-03 13:16:42 +0300 3325) refcount_set(&new_ns->ns.count, 1);
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3326) INIT_LIST_HEAD(&new_ns->list);
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3327) init_waitqueue_head(&new_ns->poll);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 3328) spin_lock_init(&new_ns->ns_lock);
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3329) new_ns->user_ns = get_user_ns(user_ns);
537f7ccb39680 (Eric W. Biederman 2016-08-08 14:37:37 -0500 3330) new_ns->ucounts = ucounts;
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3331) return new_ns;
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3332) }
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3333)
0766f788eb727 (Emese Revfy 2016-06-20 20:42:34 +0200 3334) __latent_entropy
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3335) struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3336) struct user_namespace *user_ns, struct fs_struct *new_fs)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3337) {
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 3338) struct mnt_namespace *new_ns;
7f2da1e7d0330 (Al Viro 2008-05-10 20:44:54 -0400 3339) struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 3340) struct mount *p, *q;
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3341) struct mount *old;
cb338d06e9716 (Al Viro 2011-11-24 20:55:08 -0500 3342) struct mount *new;
7a472ef4be838 (Eric W. Biederman 2012-07-31 13:13:04 -0700 3343) int copy_flags;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3344)
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3345) BUG_ON(!ns);
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3346)
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3347) if (likely(!(flags & CLONE_NEWNS))) {
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3348) get_mnt_ns(ns);
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3349) return ns;
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3350) }
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3351)
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3352) old = ns->root;
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3353)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3354) new_ns = alloc_mnt_ns(user_ns, false);
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3355) if (IS_ERR(new_ns))
cf8d2c11cb77f (Trond Myklebust 2009-06-22 15:09:13 -0400 3356) return new_ns;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3357)
97216be09efd4 (Al Viro 2013-03-16 15:12:40 -0400 3358) namespace_lock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3359) /* First pass: copy the tree topology */
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 3360) copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3361) if (user_ns != ns->user_ns)
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3362) copy_flags |= CL_SHARED_TO_SLAVE;
7a472ef4be838 (Eric W. Biederman 2012-07-31 13:13:04 -0700 3363) new = copy_tree(old, old->mnt.mnt_root, copy_flags);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 3364) if (IS_ERR(new)) {
328e6d9014636 (Al Viro 2013-03-16 14:49:45 -0400 3365) namespace_unlock();
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 3366) free_mnt_ns(new_ns);
be34d1a3bc4b6 (David Howells 2012-06-25 12:55:18 +0100 3367) return ERR_CAST(new);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3368) }
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3369) if (user_ns != ns->user_ns) {
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3370) lock_mount_hash();
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3371) lock_mnt_tree(new);
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3372) unlock_mount_hash();
3bd045cc9c4be (Al Viro 2019-01-30 13:15:45 -0500 3373) }
be08d6d260b6e (Al Viro 2011-12-06 13:32:36 -0500 3374) new_ns->root = new;
1a4eeaf2a8c07 (Al Viro 2011-11-25 02:19:55 -0500 3375) list_add_tail(&new_ns->list, &new->mnt_list);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3376)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3377) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3378) * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3379) * as belonging to new namespace. We have already acquired a private
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3380) * fs_struct, so tsk->fs->lock is not needed.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3381) */
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 3382) p = old;
cb338d06e9716 (Al Viro 2011-11-24 20:55:08 -0500 3383) q = new;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3384) while (p) {
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 3385) q->mnt_ns = new_ns;
d29216842a85c (Eric W. Biederman 2016-09-28 00:27:17 -0500 3386) new_ns->mounts++;
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3387) if (new_fs) {
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3388) if (&p->mnt == new_fs->root.mnt) {
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3389) new_fs->root.mnt = mntget(&q->mnt);
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 3390) rootmnt = &p->mnt;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3391) }
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3392) if (&p->mnt == new_fs->pwd.mnt) {
9559f68915024 (Al Viro 2013-09-28 20:47:57 -0400 3393) new_fs->pwd.mnt = mntget(&q->mnt);
315fc83e56c69 (Al Viro 2011-11-24 18:57:30 -0500 3394) pwdmnt = &p->mnt;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3395) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3396) }
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 3397) p = next_mnt(p, old);
909b0a88ef2dc (Al Viro 2011-11-25 03:06:56 -0500 3398) q = next_mnt(q, new);
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 3399) if (!q)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 3400) break;
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 3401) while (p->mnt.mnt_root != q->mnt.mnt_root)
4ce5d2b1a8fde (Eric W. Biederman 2013-03-30 01:35:18 -0700 3402) p = next_mnt(p, old);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3403) }
328e6d9014636 (Al Viro 2013-03-16 14:49:45 -0400 3404) namespace_unlock();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3405)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3406) if (rootmnt)
f03c65993b98e (Al Viro 2011-01-14 22:30:21 -0500 3407) mntput(rootmnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3408) if (pwdmnt)
f03c65993b98e (Al Viro 2011-01-14 22:30:21 -0500 3409) mntput(pwdmnt);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3410)
741a295130606 (JANAK DESAI 2006-02-07 12:59:00 -0800 3411) return new_ns;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3412) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3413)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3414) struct dentry *mount_subtree(struct vfsmount *m, const char *name)
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3415) {
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3416) struct mount *mnt = real_mount(m);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3417) struct mnt_namespace *ns;
d31da0f0ba3bc (Al Viro 2011-11-22 12:31:21 -0500 3418) struct super_block *s;
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3419) struct path path;
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3420) int err;
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3421)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3422) ns = alloc_mnt_ns(&init_user_ns, true);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3423) if (IS_ERR(ns)) {
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3424) mntput(m);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3425) return ERR_CAST(ns);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3426) }
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3427) mnt->mnt_ns = ns;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3428) ns->root = mnt;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3429) ns->mounts++;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3430) list_add(&mnt->mnt_list, &ns->list);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3431)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 3432) err = vfs_path_lookup(m->mnt_root, m,
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3433) name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3434)
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3435) put_mnt_ns(ns);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3436)
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3437) if (err)
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3438) return ERR_PTR(err);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3439)
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3440) /* trade a vfsmount reference for active sb one */
d31da0f0ba3bc (Al Viro 2011-11-22 12:31:21 -0500 3441) s = path.mnt->mnt_sb;
d31da0f0ba3bc (Al Viro 2011-11-22 12:31:21 -0500 3442) atomic_inc(&s->s_active);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3443) mntput(path.mnt);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3444) /* lock the sucker */
d31da0f0ba3bc (Al Viro 2011-11-22 12:31:21 -0500 3445) down_write(&s->s_umount);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3446) /* ... and return the root of (sub)tree on it */
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3447) return path.dentry;
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3448) }
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3449) EXPORT_SYMBOL(mount_subtree);
ea441d1104cf1 (Al Viro 2011-11-16 21:43:59 -0500 3450)
cccaa5e33525f (Dominik Brodowski 2018-10-23 22:41:09 +0200 3451) SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
cccaa5e33525f (Dominik Brodowski 2018-10-23 22:41:09 +0200 3452) char __user *, type, unsigned long, flags, void __user *, data)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3453) {
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3454) int ret;
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3455) char *kernel_type;
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3456) char *kernel_dev;
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3457) void *options;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3458)
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3459) kernel_type = copy_mount_string(type);
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3460) ret = PTR_ERR(kernel_type);
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3461) if (IS_ERR(kernel_type))
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3462) goto out_type;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3463)
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3464) kernel_dev = copy_mount_string(dev_name);
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3465) ret = PTR_ERR(kernel_dev);
b8850d1fa8e2f (Tim Gardner 2014-08-28 11:26:03 -0600 3466) if (IS_ERR(kernel_dev))
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3467) goto out_dev;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3468)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3469) options = copy_mount_options(data);
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3470) ret = PTR_ERR(options);
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3471) if (IS_ERR(options))
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3472) goto out_data;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3473)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3474) ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3475)
b40ef8696fbbb (Al Viro 2015-12-14 18:44:44 -0500 3476) kfree(options);
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3477) out_data:
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3478) kfree(kernel_dev);
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3479) out_dev:
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3480) kfree(kernel_type);
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3481) out_type:
eca6f534e6191 (Vegard Nossum 2009-09-18 13:05:45 -0700 3482) return ret;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3483) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3484)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3485) #define FSMOUNT_VALID_FLAGS \
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3486) (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3487) MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3488)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3489) #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3490)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3491) #define MOUNT_SETATTR_PROPAGATION_FLAGS \
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3492) (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3493)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3494) static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3495) {
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3496) unsigned int mnt_flags = 0;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3497)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3498) if (attr_flags & MOUNT_ATTR_RDONLY)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3499) mnt_flags |= MNT_READONLY;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3500) if (attr_flags & MOUNT_ATTR_NOSUID)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3501) mnt_flags |= MNT_NOSUID;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3502) if (attr_flags & MOUNT_ATTR_NODEV)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3503) mnt_flags |= MNT_NODEV;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3504) if (attr_flags & MOUNT_ATTR_NOEXEC)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3505) mnt_flags |= MNT_NOEXEC;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3506) if (attr_flags & MOUNT_ATTR_NODIRATIME)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3507) mnt_flags |= MNT_NODIRATIME;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3508)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3509) return mnt_flags;
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3510) }
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3511)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3512) /*
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3513) * Create a kernel mount representation for a new, prepared superblock
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3514) * (specified by fs_fd) and attach to an open_tree-like file descriptor.
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3515) */
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3516) SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3517) unsigned int, attr_flags)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3518) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3519) struct mnt_namespace *ns;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3520) struct fs_context *fc;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3521) struct file *file;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3522) struct path newmount;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3523) struct mount *mnt;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3524) struct fd f;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3525) unsigned int mnt_flags = 0;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3526) long ret;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3527)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3528) if (!may_mount())
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3529) return -EPERM;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3530)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3531) if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3532) return -EINVAL;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3533)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3534) if (attr_flags & ~FSMOUNT_VALID_FLAGS)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3535) return -EINVAL;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3536)
5b490500f91b2 (Christian Brauner 2021-01-21 14:19:52 +0100 3537) mnt_flags = attr_flags_to_mnt_flags(attr_flags);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3538)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3539) switch (attr_flags & MOUNT_ATTR__ATIME) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3540) case MOUNT_ATTR_STRICTATIME:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3541) break;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3542) case MOUNT_ATTR_NOATIME:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3543) mnt_flags |= MNT_NOATIME;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3544) break;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3545) case MOUNT_ATTR_RELATIME:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3546) mnt_flags |= MNT_RELATIME;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3547) break;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3548) default:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3549) return -EINVAL;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3550) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3551)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3552) f = fdget(fs_fd);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3553) if (!f.file)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3554) return -EBADF;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3555)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3556) ret = -EINVAL;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3557) if (f.file->f_op != &fscontext_fops)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3558) goto err_fsfd;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3559)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3560) fc = f.file->private_data;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3561)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3562) ret = mutex_lock_interruptible(&fc->uapi_mutex);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3563) if (ret < 0)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3564) goto err_fsfd;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3565)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3566) /* There must be a valid superblock or we can't mount it */
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3567) ret = -EINVAL;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3568) if (!fc->root)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3569) goto err_unlock;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3570)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3571) ret = -EPERM;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3572) if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3573) pr_warn("VFS: Mount too revealing\n");
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3574) goto err_unlock;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3575) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3576)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3577) ret = -EBUSY;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3578) if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3579) goto err_unlock;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3580)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3581) ret = -EPERM;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3582) if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3583) goto err_unlock;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3584)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3585) newmount.mnt = vfs_create_mount(fc);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3586) if (IS_ERR(newmount.mnt)) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3587) ret = PTR_ERR(newmount.mnt);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3588) goto err_unlock;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3589) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3590) newmount.dentry = dget(fc->root);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3591) newmount.mnt->mnt_flags = mnt_flags;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3592)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3593) /* We've done the mount bit - now move the file context into more or
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3594) * less the same state as if we'd done an fspick(). We don't want to
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3595) * do any memory allocation or anything like that at this point as we
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3596) * don't want to have to handle any errors incurred.
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3597) */
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3598) vfs_clean_context(fc);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3599)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3600) ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3601) if (IS_ERR(ns)) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3602) ret = PTR_ERR(ns);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3603) goto err_path;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3604) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3605) mnt = real_mount(newmount.mnt);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3606) mnt->mnt_ns = ns;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3607) ns->root = mnt;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3608) ns->mounts = 1;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3609) list_add(&mnt->mnt_list, &ns->list);
1b0b9cc8d3793 (Eric Biggers 2019-06-12 11:43:13 -0700 3610) mntget(newmount.mnt);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3611)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3612) /* Attach to an apparent O_PATH fd with a note that we need to unmount
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3613) * it, not just simply put it.
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3614) */
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3615) file = dentry_open(&newmount, O_PATH, fc->cred);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3616) if (IS_ERR(file)) {
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3617) dissolve_on_fput(newmount.mnt);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3618) ret = PTR_ERR(file);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3619) goto err_path;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3620) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3621) file->f_mode |= FMODE_NEED_UNMOUNT;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3622)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3623) ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3624) if (ret >= 0)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3625) fd_install(ret, file);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3626) else
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3627) fput(file);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3628)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3629) err_path:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3630) path_put(&newmount);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3631) err_unlock:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3632) mutex_unlock(&fc->uapi_mutex);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3633) err_fsfd:
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3634) fdput(f);
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3635) return ret;
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3636) }
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3637)
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3638) /*
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3639) * Move a mount from one place to another. In combination with
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3640) * fsopen()/fsmount() this is used to install a new mount and in combination
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3641) * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
93766fbd2696c (David Howells 2018-11-01 23:36:14 +0000 3642) * a mount subtree.
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3643) *
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3644) * Note the flags value is a combination of MOVE_MOUNT_* flags.
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3645) */
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3646) SYSCALL_DEFINE5(move_mount,
2658ce095df58 (Ben Dooks 2019-10-15 11:35:02 +0100 3647) int, from_dfd, const char __user *, from_pathname,
2658ce095df58 (Ben Dooks 2019-10-15 11:35:02 +0100 3648) int, to_dfd, const char __user *, to_pathname,
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3649) unsigned int, flags)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3650) {
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3651) struct path from_path, to_path;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3652) unsigned int lflags;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3653) int ret = 0;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3654)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3655) if (!may_mount())
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3656) return -EPERM;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3657)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3658) if (flags & ~MOVE_MOUNT__MASK)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3659) return -EINVAL;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3660)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3661) /* If someone gives a pathname, they aren't permitted to move
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3662) * from an fd that requires unmount as we can't get at the flag
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3663) * to clear it afterwards.
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3664) */
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3665) lflags = 0;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3666) if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3667) if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3668) if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3669)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3670) ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3671) if (ret < 0)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3672) return ret;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3673)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3674) lflags = 0;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3675) if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3676) if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3677) if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3678)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3679) ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3680) if (ret < 0)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3681) goto out_from;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3682)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3683) ret = security_move_mount(&from_path, &to_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3684) if (ret < 0)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3685) goto out_to;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3686)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3687) ret = do_move_mount(&from_path, &to_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3688)
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3689) out_to:
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3690) path_put(&to_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3691) out_from:
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3692) path_put(&from_path);
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3693) return ret;
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3694) }
2db154b3ea8e1 (David Howells 2018-11-05 17:40:30 +0000 3695)
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3696) /*
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3697) * Return true if path is reachable from root
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3698) *
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 3699) * namespace_sem or mount_lock is held
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3700) */
643822b41e5e0 (Al Viro 2011-11-24 22:00:28 -0500 3701) bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3702) const struct path *root)
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3703) {
643822b41e5e0 (Al Viro 2011-11-24 22:00:28 -0500 3704) while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
a73324da7af40 (Al Viro 2011-11-24 22:25:07 -0500 3705) dentry = mnt->mnt_mountpoint;
0714a533805a0 (Al Viro 2011-11-24 22:19:58 -0500 3706) mnt = mnt->mnt_parent;
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3707) }
643822b41e5e0 (Al Viro 2011-11-24 22:00:28 -0500 3708) return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3709) }
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3710)
640eb7e7b5242 (Mickaël Salaün 2016-11-14 22:14:35 +0100 3711) bool path_is_under(const struct path *path1, const struct path *path2)
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3712) {
25ab4c9b1ccb6 (Yaowei Bai 2015-11-17 14:40:10 +0800 3713) bool res;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 3714) read_seqlock_excl(&mount_lock);
643822b41e5e0 (Al Viro 2011-11-24 22:00:28 -0500 3715) res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 3716) read_sequnlock_excl(&mount_lock);
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3717) return res;
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3718) }
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3719) EXPORT_SYMBOL(path_is_under);
afac7cba7ed31 (Al Viro 2011-11-23 19:34:49 -0500 3720)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3721) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3722) * pivot_root Semantics:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3723) * Moves the root file system of the current process to the directory put_old,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3724) * makes new_root as the new root file system of the current process, and sets
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3725) * root/cwd of all processes which had them on the current root to new_root.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3726) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3727) * Restrictions:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3728) * The new_root and put_old must be directories, and must not be on the
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3729) * same file system as the current process root. The put_old must be
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3730) * underneath new_root, i.e. adding a non-zero number of /.. to the string
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3731) * pointed to by put_old must yield the same directory as new_root. No other
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3732) * file system may be mounted on put_old. After all, new_root is a mountpoint.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3733) *
4a0d11fae5798 (Neil Brown 2006-01-08 01:03:18 -0800 3734) * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
0c1bc6b84525b (Mauro Carvalho Chehab 2020-04-14 18:48:37 +0200 3735) * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
4a0d11fae5798 (Neil Brown 2006-01-08 01:03:18 -0800 3736) * in this situation.
4a0d11fae5798 (Neil Brown 2006-01-08 01:03:18 -0800 3737) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3738) * Notes:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3739) * - we don't move root/cwd if they are not at the root (reason: if something
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3740) * cared enough to change them, it's probably wrong to force them elsewhere)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3741) * - it's okay to pick a root that isn't the root of a file system, e.g.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3742) * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3743) * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3744) * first.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3745) */
3480b25743cb7 (Heiko Carstens 2009-01-14 14:14:16 +0100 3746) SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3480b25743cb7 (Heiko Carstens 2009-01-14 14:14:16 +0100 3747) const char __user *, put_old)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3748) {
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3749) struct path new, old, root;
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3750) struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3751) struct mountpoint *old_mp, *root_mp;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3752) int error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3753)
9b40bc90abd12 (Al Viro 2013-02-22 22:45:42 -0500 3754) if (!may_mount())
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3755) return -EPERM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3756)
ce6595a28a15c (Al Viro 2019-07-14 16:42:44 -0400 3757) error = user_path_at(AT_FDCWD, new_root,
ce6595a28a15c (Al Viro 2019-07-14 16:42:44 -0400 3758) LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3759) if (error)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3760) goto out0;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3761)
ce6595a28a15c (Al Viro 2019-07-14 16:42:44 -0400 3762) error = user_path_at(AT_FDCWD, put_old,
ce6595a28a15c (Al Viro 2019-07-14 16:42:44 -0400 3763) LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3764) if (error)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3765) goto out1;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3766)
2d8f30380ab8c (Al Viro 2008-07-22 09:59:21 -0400 3767) error = security_sb_pivotroot(&old, &new);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3768) if (error)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3769) goto out2;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3770)
f7ad3c6be9080 (Miklos Szeredi 2010-08-10 11:41:36 +0200 3771) get_fs_root(current->fs, &root);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3772) old_mp = lock_mount(&old);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3773) error = PTR_ERR(old_mp);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3774) if (IS_ERR(old_mp))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3775) goto out3;
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3776)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3777) error = -EINVAL;
419148da6e76d (Al Viro 2011-11-24 19:41:16 -0500 3778) new_mnt = real_mount(new.mnt);
419148da6e76d (Al Viro 2011-11-24 19:41:16 -0500 3779) root_mnt = real_mount(root.mnt);
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3780) old_mnt = real_mount(old.mnt);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3781) ex_parent = new_mnt->mnt_parent;
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3782) root_parent = root_mnt->mnt_parent;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3783) if (IS_MNT_SHARED(old_mnt) ||
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3784) IS_MNT_SHARED(ex_parent) ||
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3785) IS_MNT_SHARED(root_parent))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3786) goto out4;
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 3787) if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3788) goto out4;
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3789) if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3790) goto out4;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3791) error = -ENOENT;
f3da392e9ff14 (Alexey Dobriyan 2009-05-04 03:32:03 +0400 3792) if (d_unlinked(new.dentry))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3793) goto out4;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3794) error = -EBUSY;
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3795) if (new_mnt == root_mnt || old_mnt == root_mnt)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3796) goto out4; /* loop, on the same file system */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3797) error = -EINVAL;
8c3ee42e80cce (Al Viro 2008-03-22 18:00:39 -0400 3798) if (root.mnt->mnt_root != root.dentry)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3799) goto out4; /* not a mountpoint */
676da58df740f (Al Viro 2011-11-24 21:47:05 -0500 3800) if (!mnt_has_parent(root_mnt))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3801) goto out4; /* not attached */
2d8f30380ab8c (Al Viro 2008-07-22 09:59:21 -0400 3802) if (new.mnt->mnt_root != new.dentry)
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3803) goto out4; /* not a mountpoint */
676da58df740f (Al Viro 2011-11-24 21:47:05 -0500 3804) if (!mnt_has_parent(new_mnt))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3805) goto out4; /* not attached */
4ac9137858e08 (Jan Blunck 2008-02-14 19:34:32 -0800 3806) /* make sure we can reach put_old from new_root */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3807) if (!is_path_reachable(old_mnt, old.dentry, &new))
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3808) goto out4;
0d0826019e529 (Eric W. Biederman 2014-10-08 10:42:27 -0700 3809) /* make certain new is below the root */
0d0826019e529 (Eric W. Biederman 2014-10-08 10:42:27 -0700 3810) if (!is_path_reachable(new_mnt, new.dentry, &root))
0d0826019e529 (Eric W. Biederman 2014-10-08 10:42:27 -0700 3811) goto out4;
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 3812) lock_mount_hash();
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3813) umount_mnt(new_mnt);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3814) root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3815) if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3816) new_mnt->mnt.mnt_flags |= MNT_LOCKED;
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3817) root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
5ff9d8a65ce80 (Eric W. Biederman 2013-03-29 21:04:39 -0700 3818) }
4ac9137858e08 (Jan Blunck 2008-02-14 19:34:32 -0800 3819) /* mount old root on put_old */
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3820) attach_mnt(root_mnt, old_mnt, old_mp);
4ac9137858e08 (Jan Blunck 2008-02-14 19:34:32 -0800 3821) /* mount new_root on / */
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3822) attach_mnt(new_mnt, root_parent, root_mp);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3823) mnt_add_count(root_parent, -1);
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 3824) touch_mnt_namespace(current->nsproxy->mnt_ns);
4fed655c410cc (Eric W. Biederman 2014-10-08 10:42:57 -0700 3825) /* A moved mount should not expire automatically */
4fed655c410cc (Eric W. Biederman 2014-10-08 10:42:57 -0700 3826) list_del_init(&new_mnt->mnt_expire);
3895dbf8985f6 (Eric W. Biederman 2017-01-03 14:18:43 +1300 3827) put_mountpoint(root_mp);
719ea2fbb553a (Al Viro 2013-09-29 11:24:49 -0400 3828) unlock_mount_hash();
2d8f30380ab8c (Al Viro 2008-07-22 09:59:21 -0400 3829) chroot_fs_refs(&root, &new);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3830) error = 0;
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3831) out4:
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 3832) unlock_mount(old_mp);
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3833) if (!error)
2763d11912317 (Al Viro 2019-06-30 19:18:53 -0400 3834) mntput_no_expire(ex_parent);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3835) out3:
8c3ee42e80cce (Al Viro 2008-03-22 18:00:39 -0400 3836) path_put(&root);
b12cea9198fa9 (Al Viro 2011-03-18 08:55:38 -0400 3837) out2:
2d8f30380ab8c (Al Viro 2008-07-22 09:59:21 -0400 3838) path_put(&old);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3839) out1:
2d8f30380ab8c (Al Viro 2008-07-22 09:59:21 -0400 3840) path_put(&new);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3841) out0:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3842) return error;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3843) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3844)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3845) static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3846) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3847) unsigned int flags = mnt->mnt.mnt_flags;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3848)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3849) /* flags to clear */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3850) flags &= ~kattr->attr_clr;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3851) /* flags to raise */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3852) flags |= kattr->attr_set;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3853)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3854) return flags;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3855) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3856)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3857) static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3858) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3859) struct vfsmount *m = &mnt->mnt;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3860)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3861) if (!kattr->mnt_userns)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3862) return 0;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3863)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3864) /*
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3865) * Once a mount has been idmapped we don't allow it to change its
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3866) * mapping. It makes things simpler and callers can just create
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3867) * another bind-mount they can idmap if they want to.
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3868) */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3869) if (mnt_user_ns(m) != &init_user_ns)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3870) return -EPERM;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3871)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3872) /* The underlying filesystem doesn't support idmapped mounts yet. */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3873) if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3874) return -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3875)
2ca4dcc4909d7 (Christian Brauner 2021-05-11 16:30:15 +0200 3876) /* Don't yet support filesystem mountable in user namespaces. */
2ca4dcc4909d7 (Christian Brauner 2021-05-11 16:30:15 +0200 3877) if (m->mnt_sb->s_user_ns != &init_user_ns)
2ca4dcc4909d7 (Christian Brauner 2021-05-11 16:30:15 +0200 3878) return -EINVAL;
2ca4dcc4909d7 (Christian Brauner 2021-05-11 16:30:15 +0200 3879)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3880) /* We're not controlling the superblock. */
2ca4dcc4909d7 (Christian Brauner 2021-05-11 16:30:15 +0200 3881) if (!capable(CAP_SYS_ADMIN))
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3882) return -EPERM;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3883)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3884) /* Mount has already been visible in the filesystem hierarchy. */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3885) if (!is_anon_ns(mnt->mnt_ns))
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3886) return -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3887)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3888) return 0;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3889) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3890)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3891) static struct mount *mount_setattr_prepare(struct mount_kattr *kattr,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3892) struct mount *mnt, int *err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3893) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3894) struct mount *m = mnt, *last = NULL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3895)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3896) if (!is_mounted(&m->mnt)) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3897) *err = -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3898) goto out;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3899) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3900)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3901) if (!(mnt_has_parent(m) ? check_mnt(m) : is_anon_ns(m->mnt_ns))) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3902) *err = -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3903) goto out;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3904) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3905)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3906) do {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3907) unsigned int flags;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3908)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3909) flags = recalc_flags(kattr, m);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3910) if (!can_change_locked_flags(m, flags)) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3911) *err = -EPERM;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3912) goto out;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3913) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3914)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3915) *err = can_idmap_mount(kattr, m);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3916) if (*err)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3917) goto out;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3918)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3919) last = m;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3920)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3921) if ((kattr->attr_set & MNT_READONLY) &&
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3922) !(m->mnt.mnt_flags & MNT_READONLY)) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3923) *err = mnt_hold_writers(m);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3924) if (*err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3925) goto out;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3926) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3927) } while (kattr->recurse && (m = next_mnt(m, mnt)));
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3928)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3929) out:
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3930) return last;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3931) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3932)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3933) static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3934) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3935) struct user_namespace *mnt_userns;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3936)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3937) if (!kattr->mnt_userns)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3938) return;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3939)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3940) mnt_userns = get_user_ns(kattr->mnt_userns);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3941) /* Pairs with smp_load_acquire() in mnt_user_ns(). */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3942) smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3943) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3944)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3945) static void mount_setattr_commit(struct mount_kattr *kattr,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3946) struct mount *mnt, struct mount *last,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3947) int err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3948) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3949) struct mount *m = mnt;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3950)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3951) do {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3952) if (!err) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3953) unsigned int flags;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3954)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 3955) do_idmap_mount(kattr, m);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3956) flags = recalc_flags(kattr, m);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3957) WRITE_ONCE(m->mnt.mnt_flags, flags);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3958) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3959)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3960) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3961) * We either set MNT_READONLY above so make it visible
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3962) * before ~MNT_WRITE_HOLD or we failed to recursively
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3963) * apply mount options.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3964) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3965) if ((kattr->attr_set & MNT_READONLY) &&
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3966) (m->mnt.mnt_flags & MNT_WRITE_HOLD))
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3967) mnt_unhold_writers(m);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3968)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3969) if (!err && kattr->propagation)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3970) change_mnt_propagation(m, kattr->propagation);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3971)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3972) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3973) * On failure, only cleanup until we found the first mount
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3974) * we failed to handle.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3975) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3976) if (err && m == last)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3977) break;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3978) } while (kattr->recurse && (m = next_mnt(m, mnt)));
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3979)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3980) if (!err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3981) touch_mnt_namespace(mnt->mnt_ns);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3982) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3983)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3984) static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3985) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3986) struct mount *mnt = real_mount(path->mnt), *last = NULL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3987) int err = 0;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3988)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3989) if (path->dentry != mnt->mnt.mnt_root)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3990) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3991)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3992) if (kattr->propagation) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3993) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3994) * Only take namespace_lock() if we're actually changing
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3995) * propagation.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3996) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3997) namespace_lock();
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3998) if (kattr->propagation == MS_SHARED) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 3999) err = invent_group_ids(mnt, kattr->recurse);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4000) if (err) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4001) namespace_unlock();
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4002) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4003) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4004) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4005) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4006)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4007) lock_mount_hash();
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4008)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4009) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4010) * Get the mount tree in a shape where we can change mount
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4011) * properties without failure.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4012) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4013) last = mount_setattr_prepare(kattr, mnt, &err);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4014) if (last) /* Commit all changes or revert to the old state. */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4015) mount_setattr_commit(kattr, mnt, last, err);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4016)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4017) unlock_mount_hash();
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4018)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4019) if (kattr->propagation) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4020) namespace_unlock();
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4021) if (err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4022) cleanup_group_ids(mnt, NULL);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4023) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4024)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4025) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4026) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4027)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4028) static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4029) struct mount_kattr *kattr, unsigned int flags)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4030) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4031) int err = 0;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4032) struct ns_common *ns;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4033) struct user_namespace *mnt_userns;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4034) struct file *file;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4035)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4036) if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4037) return 0;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4038)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4039) /*
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4040) * We currently do not support clearing an idmapped mount. If this ever
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4041) * is a use-case we can revisit this but for now let's keep it simple
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4042) * and not allow it.
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4043) */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4044) if (attr->attr_clr & MOUNT_ATTR_IDMAP)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4045) return -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4046)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4047) if (attr->userns_fd > INT_MAX)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4048) return -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4049)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4050) file = fget(attr->userns_fd);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4051) if (!file)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4052) return -EBADF;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4053)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4054) if (!proc_ns_file(file)) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4055) err = -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4056) goto out_fput;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4057) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4058)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4059) ns = get_proc_ns(file_inode(file));
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4060) if (ns->ops->type != CLONE_NEWUSER) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4061) err = -EINVAL;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4062) goto out_fput;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4063) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4064)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4065) /*
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4066) * The init_user_ns is used to indicate that a vfsmount is not idmapped.
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4067) * This is simpler than just having to treat NULL as unmapped. Users
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4068) * wanting to idmap a mount to init_user_ns can just use a namespace
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4069) * with an identity mapping.
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4070) */
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4071) mnt_userns = container_of(ns, struct user_namespace, ns);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4072) if (mnt_userns == &init_user_ns) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4073) err = -EPERM;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4074) goto out_fput;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4075) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4076) kattr->mnt_userns = get_user_ns(mnt_userns);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4077)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4078) out_fput:
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4079) fput(file);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4080) return err;
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4081) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4082)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4083) static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4084) struct mount_kattr *kattr, unsigned int flags)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4085) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4086) unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4087)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4088) if (flags & AT_NO_AUTOMOUNT)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4089) lookup_flags &= ~LOOKUP_AUTOMOUNT;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4090) if (flags & AT_SYMLINK_NOFOLLOW)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4091) lookup_flags &= ~LOOKUP_FOLLOW;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4092) if (flags & AT_EMPTY_PATH)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4093) lookup_flags |= LOOKUP_EMPTY;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4094)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4095) *kattr = (struct mount_kattr) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4096) .lookup_flags = lookup_flags,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4097) .recurse = !!(flags & AT_RECURSIVE),
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4098) };
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4099)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4100) if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4101) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4102) if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4103) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4104) kattr->propagation = attr->propagation;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4105)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4106) if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4107) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4108)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4109) kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4110) kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4111)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4112) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4113) * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4114) * users wanting to transition to a different atime setting cannot
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4115) * simply specify the atime setting in @attr_set, but must also
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4116) * specify MOUNT_ATTR__ATIME in the @attr_clr field.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4117) * So ensure that MOUNT_ATTR__ATIME can't be partially set in
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4118) * @attr_clr and that @attr_set can't have any atime bits set if
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4119) * MOUNT_ATTR__ATIME isn't set in @attr_clr.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4120) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4121) if (attr->attr_clr & MOUNT_ATTR__ATIME) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4122) if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4123) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4124)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4125) /*
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4126) * Clear all previous time settings as they are mutually
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4127) * exclusive.
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4128) */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4129) kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4130) switch (attr->attr_set & MOUNT_ATTR__ATIME) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4131) case MOUNT_ATTR_RELATIME:
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4132) kattr->attr_set |= MNT_RELATIME;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4133) break;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4134) case MOUNT_ATTR_NOATIME:
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4135) kattr->attr_set |= MNT_NOATIME;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4136) break;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4137) case MOUNT_ATTR_STRICTATIME:
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4138) break;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4139) default:
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4140) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4141) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4142) } else {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4143) if (attr->attr_set & MOUNT_ATTR__ATIME)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4144) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4145) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4146)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4147) return build_mount_idmapped(attr, usize, kattr, flags);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4148) }
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4149)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4150) static void finish_mount_kattr(struct mount_kattr *kattr)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4151) {
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4152) put_user_ns(kattr->mnt_userns);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4153) kattr->mnt_userns = NULL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4154) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4155)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4156) SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4157) unsigned int, flags, struct mount_attr __user *, uattr,
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4158) size_t, usize)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4159) {
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4160) int err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4161) struct path target;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4162) struct mount_attr attr;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4163) struct mount_kattr kattr;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4164)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4165) BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4166)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4167) if (flags & ~(AT_EMPTY_PATH |
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4168) AT_RECURSIVE |
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4169) AT_SYMLINK_NOFOLLOW |
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4170) AT_NO_AUTOMOUNT))
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4171) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4172)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4173) if (unlikely(usize > PAGE_SIZE))
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4174) return -E2BIG;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4175) if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4176) return -EINVAL;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4177)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4178) if (!may_mount())
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4179) return -EPERM;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4180)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4181) err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4182) if (err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4183) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4184)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4185) /* Don't bother walking through the mounts if this is a nop. */
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4186) if (attr.attr_set == 0 &&
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4187) attr.attr_clr == 0 &&
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4188) attr.propagation == 0)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4189) return 0;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4190)
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4191) err = build_mount_kattr(&attr, usize, &kattr, flags);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4192) if (err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4193) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4194)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4195) err = user_path_at(dfd, path, kattr.lookup_flags, &target);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4196) if (err)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4197) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4198)
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4199) err = do_mount_setattr(&target, &kattr);
9caccd41541a6 (Christian Brauner 2021-01-21 14:19:54 +0100 4200) finish_mount_kattr(&kattr);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4201) path_put(&target);
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4202) return err;
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4203) }
2a1867219c7b2 (Christian Brauner 2021-01-21 14:19:53 +0100 4204)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4205) static void __init init_mount_tree(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4206) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4207) struct vfsmount *mnt;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4208) struct mount *m;
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 4209) struct mnt_namespace *ns;
ac748a09fc873 (Jan Blunck 2008-02-14 19:34:39 -0800 4210) struct path root;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4211)
fd3e007f6c6a0 (Al Viro 2019-05-30 17:48:35 -0400 4212) mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4213) if (IS_ERR(mnt))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4214) panic("Can't create rootfs");
b3e19d924b6ea (Nicholas Piggin 2011-01-07 17:50:11 +1100 4215)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4216) ns = alloc_mnt_ns(&init_user_ns, false);
3b22edc5730b8 (Trond Myklebust 2009-06-23 17:29:49 -0400 4217) if (IS_ERR(ns))
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4218) panic("Can't allocate initial namespace");
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4219) m = real_mount(mnt);
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4220) m->mnt_ns = ns;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4221) ns->root = m;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4222) ns->mounts = 1;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4223) list_add(&m->mnt_list, &ns->list);
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 4224) init_task.nsproxy->mnt_ns = ns;
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 4225) get_mnt_ns(ns);
6b3286ed1169d (Kirill Korotaev 2006-12-08 02:37:56 -0800 4226)
be08d6d260b6e (Al Viro 2011-12-06 13:32:36 -0500 4227) root.mnt = mnt;
be08d6d260b6e (Al Viro 2011-12-06 13:32:36 -0500 4228) root.dentry = mnt->mnt_root;
da362b09e42ee (Eric W. Biederman 2014-10-07 12:19:53 -0700 4229) mnt->mnt_flags |= MNT_LOCKED;
ac748a09fc873 (Jan Blunck 2008-02-14 19:34:39 -0800 4230)
ac748a09fc873 (Jan Blunck 2008-02-14 19:34:39 -0800 4231) set_fs_pwd(current->fs, &root);
ac748a09fc873 (Jan Blunck 2008-02-14 19:34:39 -0800 4232) set_fs_root(current->fs, &root);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4233) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4234)
74bf17cffc325 (Denis Cheng 2007-10-16 23:26:30 -0700 4235) void __init mnt_init(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4236) {
15a67dd8ccf69 (Randy Dunlap 2006-09-29 01:58:57 -0700 4237) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4238)
7d6fec45a5131 (Al Viro 2011-11-23 12:14:10 -0500 4239) mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
20c2df83d25c6 (Paul Mundt 2007-07-20 10:11:58 +0900 4240) 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4241)
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4242) mount_hashtable = alloc_large_system_hash("Mount-cache",
38129a13e6e71 (Al Viro 2014-03-20 21:10:51 -0400 4243) sizeof(struct hlist_head),
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4244) mhash_entries, 19,
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 4245) HASH_ZERO,
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4246) &m_hash_shift, &m_hash_mask, 0, 0);
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4247) mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4248) sizeof(struct hlist_head),
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4249) mphash_entries, 19,
3d375d78593cd (Pavel Tatashin 2017-07-06 15:39:11 -0700 4250) HASH_ZERO,
0818bf27c05b2 (Al Viro 2014-02-28 13:46:44 -0500 4251) &mp_hash_shift, &mp_hash_mask, 0, 0);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4252)
84d17192d2afd (Al Viro 2013-03-15 10:53:28 -0400 4253) if (!mount_hashtable || !mountpoint_hashtable)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4254) panic("Failed to allocate mount hash table\n");
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4255)
4b93dc9b1c684 (Tejun Heo 2013-11-28 14:54:43 -0500 4256) kernfs_init();
4b93dc9b1c684 (Tejun Heo 2013-11-28 14:54:43 -0500 4257)
15a67dd8ccf69 (Randy Dunlap 2006-09-29 01:58:57 -0700 4258) err = sysfs_init();
15a67dd8ccf69 (Randy Dunlap 2006-09-29 01:58:57 -0700 4259) if (err)
15a67dd8ccf69 (Randy Dunlap 2006-09-29 01:58:57 -0700 4260) printk(KERN_WARNING "%s: sysfs_init error: %d\n",
8e24eea728068 (Harvey Harrison 2008-04-30 00:55:09 -0700 4261) __func__, err);
00d2666623368 (Greg Kroah-Hartman 2007-10-29 14:17:23 -0600 4262) fs_kobj = kobject_create_and_add("fs", NULL);
00d2666623368 (Greg Kroah-Hartman 2007-10-29 14:17:23 -0600 4263) if (!fs_kobj)
8e24eea728068 (Harvey Harrison 2008-04-30 00:55:09 -0700 4264) printk(KERN_WARNING "%s: kobj create error\n", __func__);
037f11b4752f7 (Al Viro 2019-06-01 18:09:44 -0400 4265) shmem_init();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4266) init_rootfs();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4267) init_mount_tree();
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4268) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4269)
616511d039af4 (Trond Myklebust 2009-06-22 15:09:13 -0400 4270) void put_mnt_ns(struct mnt_namespace *ns)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4271) {
1a7b8969e664d (Kirill Tkhai 2020-08-03 13:16:42 +0300 4272) if (!refcount_dec_and_test(&ns->ns.count))
616511d039af4 (Trond Myklebust 2009-06-22 15:09:13 -0400 4273) return;
7b00ed6fe6321 (Al Viro 2013-09-16 21:19:20 -0400 4274) drop_collected_mounts(&ns->root->mnt);
771b1371686e0 (Eric W. Biederman 2012-07-26 21:08:32 -0700 4275) free_mnt_ns(ns);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4276) }
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 4277)
d911b4585eb35 (David Howells 2018-11-01 23:07:26 +0000 4278) struct vfsmount *kern_mount(struct file_system_type *type)
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 4279) {
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4280) struct vfsmount *mnt;
d911b4585eb35 (David Howells 2018-11-01 23:07:26 +0000 4281) mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4282) if (!IS_ERR(mnt)) {
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4283) /*
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4284) * it is a longterm mount, don't release mnt until
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4285) * we unmount before file sys is unregistered
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4286) */
f7a99c5b7c8bd (Al Viro 2012-06-09 00:59:08 -0400 4287) real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4288) }
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4289) return mnt;
9d412a43c3b26 (Al Viro 2011-03-17 22:08:28 -0400 4290) }
d911b4585eb35 (David Howells 2018-11-01 23:07:26 +0000 4291) EXPORT_SYMBOL_GPL(kern_mount);
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4292)
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4293) void kern_unmount(struct vfsmount *mnt)
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4294) {
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4295) /* release long term mount so mount point can be released */
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4296) if (!IS_ERR_OR_NULL(mnt)) {
f7a99c5b7c8bd (Al Viro 2012-06-09 00:59:08 -0400 4297) real_mount(mnt)->mnt_ns = NULL;
48a066e72d970 (Al Viro 2013-09-29 22:06:07 -0400 4298) synchronize_rcu(); /* yecchhh... */
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4299) mntput(mnt);
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4300) }
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4301) }
423e0ab086ad8 (Tim Chen 2011-07-19 09:32:38 -0700 4302) EXPORT_SYMBOL(kern_unmount);
02125a826459a (Al Viro 2011-12-05 08:43:34 -0500 4303)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4304) void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4305) {
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4306) unsigned int i;
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4307)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4308) for (i = 0; i < num; i++)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4309) if (mnt[i])
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4310) real_mount(mnt[i])->mnt_ns = NULL;
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4311) synchronize_rcu_expedited();
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4312) for (i = 0; i < num; i++)
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4313) mntput(mnt[i]);
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4314) }
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4315) EXPORT_SYMBOL(kern_unmount_array);
df820f8de4e48 (Miklos Szeredi 2020-06-04 10:48:19 +0200 4316)
02125a826459a (Al Viro 2011-12-05 08:43:34 -0500 4317) bool our_mnt(struct vfsmount *mnt)
02125a826459a (Al Viro 2011-12-05 08:43:34 -0500 4318) {
143c8c91cee7e (Al Viro 2011-11-25 00:46:35 -0500 4319) return check_mnt(real_mount(mnt));
02125a826459a (Al Viro 2011-12-05 08:43:34 -0500 4320) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4321)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4322) bool current_chrooted(void)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4323) {
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4324) /* Does the current process have a non-standard root */
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4325) struct path ns_root;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4326) struct path fs_root;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4327) bool chrooted;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4328)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4329) /* Find the namespace root */
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4330) ns_root.mnt = ¤t->nsproxy->mnt_ns->root->mnt;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4331) ns_root.dentry = ns_root.mnt->mnt_root;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4332) path_get(&ns_root);
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4333) while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4334) ;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4335)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4336) get_fs_root(current->fs, &fs_root);
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4337)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4338) chrooted = !path_equal(&fs_root, &ns_root);
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4339)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4340) path_put(&fs_root);
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4341) path_put(&ns_root);
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4342)
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4343) return chrooted;
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4344) }
3151527ee007b (Eric W. Biederman 2013-03-15 01:45:51 -0700 4345)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4346) static bool mnt_already_visible(struct mnt_namespace *ns,
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4347) const struct super_block *sb,
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4348) int *new_mnt_flags)
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4349) {
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4350) int new_flags = *new_mnt_flags;
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4351) struct mount *mnt;
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4352) bool visible = false;
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4353)
44bb4385ce1cf (Al Viro 2013-09-16 21:37:36 -0400 4354) down_read(&namespace_sem);
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 4355) lock_ns_list(ns);
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4356) list_for_each_entry(mnt, &ns->list, mnt_list) {
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4357) struct mount *child;
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4358) int mnt_flags;
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4359)
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 4360) if (mnt_is_cursor(mnt))
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 4361) continue;
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 4362)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4363) if (mnt->mnt.mnt_sb->s_type != sb->s_type)
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4364) continue;
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4365)
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4366) /* This mount is not fully visible if it's root directory
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4367) * is not the root directory of the filesystem.
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4368) */
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4369) if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4370) continue;
7e96c1b0e0f49 (Eric W. Biederman 2015-05-08 16:36:50 -0500 4371)
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4372) /* A local view of the mount flags */
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4373) mnt_flags = mnt->mnt.mnt_flags;
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4374)
695e9df010e40 (Eric W. Biederman 2016-06-10 12:21:40 -0500 4375) /* Don't miss readonly hidden in the superblock flags */
bc98a42c1f7d0 (David Howells 2017-07-17 08:45:34 +0100 4376) if (sb_rdonly(mnt->mnt.mnt_sb))
695e9df010e40 (Eric W. Biederman 2016-06-10 12:21:40 -0500 4377) mnt_flags |= MNT_LOCK_READONLY;
695e9df010e40 (Eric W. Biederman 2016-06-10 12:21:40 -0500 4378)
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4379) /* Verify the mount flags are equal to or more permissive
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4380) * than the proposed new mount.
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4381) */
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4382) if ((mnt_flags & MNT_LOCK_READONLY) &&
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4383) !(new_flags & MNT_READONLY))
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4384) continue;
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4385) if ((mnt_flags & MNT_LOCK_ATIME) &&
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4386) ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4387) continue;
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4388)
ceeb0e5d39fcd (Eric W. Biederman 2015-01-07 08:10:09 -0600 4389) /* This mount is not fully visible if there are any
ceeb0e5d39fcd (Eric W. Biederman 2015-01-07 08:10:09 -0600 4390) * locked child mounts that cover anything except for
ceeb0e5d39fcd (Eric W. Biederman 2015-01-07 08:10:09 -0600 4391) * empty directories.
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4392) */
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4393) list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4394) struct inode *inode = child->mnt_mountpoint->d_inode;
ceeb0e5d39fcd (Eric W. Biederman 2015-01-07 08:10:09 -0600 4395) /* Only worry about locked mounts */
d71ed6c930ac7 (Eric W. Biederman 2016-05-27 14:50:05 -0500 4396) if (!(child->mnt.mnt_flags & MNT_LOCKED))
ceeb0e5d39fcd (Eric W. Biederman 2015-01-07 08:10:09 -0600 4397) continue;
7236c85e1be51 (Eric W. Biederman 2015-05-13 20:51:09 -0500 4398) /* Is the directory permanetly empty? */
7236c85e1be51 (Eric W. Biederman 2015-05-13 20:51:09 -0500 4399) if (!is_empty_dir_inode(inode))
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4400) goto next;
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4401) }
8c6cf9cc829fc (Eric W. Biederman 2015-05-08 23:49:47 -0500 4402) /* Preserve the locked attributes */
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4403) *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
77b1a97d21827 (Eric W. Biederman 2015-06-04 09:43:11 -0500 4404) MNT_LOCK_ATIME);
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4405) visible = true;
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4406) goto found;
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4407) next: ;
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4408) }
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4409) found:
9f6c61f96f2d9 (Miklos Szeredi 2020-05-14 16:44:24 +0200 4410) unlock_ns_list(ns);
44bb4385ce1cf (Al Viro 2013-09-16 21:37:36 -0400 4411) up_read(&namespace_sem);
e51db73532955 (Eric W. Biederman 2013-03-30 19:57:41 -0700 4412) return visible;
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4413) }
87a8ebd637daf (Eric W. Biederman 2013-03-24 14:28:27 -0700 4414)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4415) static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4416) {
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4417) const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4418) struct mnt_namespace *ns = current->nsproxy->mnt_ns;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4419) unsigned long s_iflags;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4420)
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4421) if (ns->user_ns == &init_user_ns)
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4422) return false;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4423)
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4424) /* Can this filesystem be too revealing? */
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4425) s_iflags = sb->s_iflags;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4426) if (!(s_iflags & SB_I_USERNS_VISIBLE))
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4427) return false;
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4428)
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4429) if ((s_iflags & required_iflags) != required_iflags) {
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4430) WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4431) required_iflags);
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4432) return true;
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4433) }
a1935c1738af5 (Eric W. Biederman 2016-06-15 06:59:49 -0500 4434)
132e460848f42 (David Howells 2018-11-04 07:43:08 -0500 4435) return !mnt_already_visible(ns, sb, new_mnt_flags);
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4436) }
8654df4e2ac97 (Eric W. Biederman 2016-06-09 16:06:06 -0500 4437)
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4438) bool mnt_may_suid(struct vfsmount *mnt)
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4439) {
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4440) /*
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4441) * Foreign mounts (accessed via fchdir or through /proc
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4442) * symlinks) are always treated as if they are nosuid. This
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4443) * prevents namespaces from trusting potentially unsafe
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4444) * suid/sgid bits, file caps, or security labels that originate
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4445) * in other namespaces.
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4446) */
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4447) return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4448) current_in_userns(mnt->mnt_sb->s_user_ns);
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4449) }
380cf5ba6b0a0 (Andy Lutomirski 2016-06-23 16:41:05 -0500 4450)
64964528b24ea (Al Viro 2014-11-01 00:37:32 -0400 4451) static struct ns_common *mntns_get(struct task_struct *task)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4452) {
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 4453) struct ns_common *ns = NULL;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4454) struct nsproxy *nsproxy;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4455)
728dba3a39c66 (Eric W. Biederman 2014-02-03 19:13:49 -0800 4456) task_lock(task);
728dba3a39c66 (Eric W. Biederman 2014-02-03 19:13:49 -0800 4457) nsproxy = task->nsproxy;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4458) if (nsproxy) {
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 4459) ns = &nsproxy->mnt_ns->ns;
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 4460) get_mnt_ns(to_mnt_ns(ns));
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4461) }
728dba3a39c66 (Eric W. Biederman 2014-02-03 19:13:49 -0800 4462) task_unlock(task);
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4463)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4464) return ns;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4465) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4466)
64964528b24ea (Al Viro 2014-11-01 00:37:32 -0400 4467) static void mntns_put(struct ns_common *ns)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4468) {
58be28256d98a (Al Viro 2014-11-01 00:00:23 -0400 4469) put_mnt_ns(to_mnt_ns(ns));
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4470) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4471)
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4472) static int mntns_install(struct nsset *nsset, struct ns_common *ns)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4473) {
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4474) struct nsproxy *nsproxy = nsset->nsproxy;
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4475) struct fs_struct *fs = nsset->fs;
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4476) struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4477) struct user_namespace *user_ns = nsset->cred->user_ns;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4478) struct path root;
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4479) int err;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4480)
0c55cfc4166d9 (Eric W. Biederman 2012-07-26 21:42:03 -0700 4481) if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4482) !ns_capable(user_ns, CAP_SYS_CHROOT) ||
f2a8d52e0a4db (Christian Brauner 2020-05-05 16:04:30 +0200 4483) !ns_capable(user_ns, CAP_SYS_ADMIN))
ae11e0f18482b (Zhao Hongjiang 2012-09-13 16:38:03 +0800 4484) return -EPERM;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4485)
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4486) if (is_anon_ns(mnt_ns))
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4487) return -EINVAL;
74e831221cfd7 (Al Viro 2019-01-30 13:30:21 -0500 4488)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4489) if (fs->users != 1)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4490) return -EINVAL;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4491)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4492) get_mnt_ns(mnt_ns);
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4493) old_mnt_ns = nsproxy->mnt_ns;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4494) nsproxy->mnt_ns = mnt_ns;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4495)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4496) /* Find the root */
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4497) err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4498) "/", LOOKUP_DOWN, &root);
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4499) if (err) {
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4500) /* revert to old namespace */
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4501) nsproxy->mnt_ns = old_mnt_ns;
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4502) put_mnt_ns(mnt_ns);
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4503) return err;
4f757f3cbf54e (Al Viro 2017-04-15 17:31:22 -0400 4504) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4505)
4068367c9ca7b (Andrei Vagin 2017-06-08 17:32:29 -0700 4506) put_mnt_ns(old_mnt_ns);
4068367c9ca7b (Andrei Vagin 2017-06-08 17:32:29 -0700 4507)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4508) /* Update the pwd and root */
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4509) set_fs_pwd(fs, &root);
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4510) set_fs_root(fs, &root);
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4511)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4512) path_put(&root);
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4513) return 0;
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4514) }
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4515)
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4516) static struct user_namespace *mntns_owner(struct ns_common *ns)
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4517) {
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4518) return to_mnt_ns(ns)->user_ns;
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4519) }
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4520)
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4521) const struct proc_ns_operations mntns_operations = {
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4522) .name = "mnt",
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4523) .type = CLONE_NEWNS,
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4524) .get = mntns_get,
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4525) .put = mntns_put,
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4526) .install = mntns_install,
bcac25a58bfc6 (Andrey Vagin 2016-09-06 00:47:13 -0700 4527) .owner = mntns_owner,
8823c079ba713 (Eric W. Biederman 2010-03-07 18:49:36 -0800 4528) };