blob: 062fe984a69789e7eb399ffceec065a91f038319 [file] [log] [blame]
Thomas Gleixner59bd9de2019-05-28 10:10:12 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/namespace.c
4 *
5 * (C) Copyright Al Viro 2000, 2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/syscalls.h>
Al Virod10577a2011-12-07 13:06:11 -050012#include <linux/export.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080014#include <linux/mnt_namespace.h>
Eric W. Biederman771b1372012-07-26 21:08:32 -070015#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
17#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010018#include <linux/cred.h>
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010019#include <linux/idr.h>
Rob Landley57f150a2013-09-11 14:26:10 -070020#include <linux/init.h> /* init_rootfs */
Al Virod10577a2011-12-07 13:06:11 -050021#include <linux/fs_struct.h> /* get_fs_root et.al. */
22#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
Al Viroa07b2002018-11-05 17:40:30 +000023#include <linux/file.h>
Al Virod10577a2011-12-07 13:06:11 -050024#include <linux/uaccess.h>
David Howells0bb80f22013-04-12 01:50:06 +010025#include <linux/proc_ns.h>
Linus Torvalds20b4fb42013-05-01 17:51:54 -070026#include <linux/magic.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070027#include <linux/memblock.h>
Christian Brauner9caccd42021-01-21 14:19:54 +010028#include <linux/proc_fs.h>
Al Viro9ea459e12014-08-08 13:08:20 -040029#include <linux/task_work.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010030#include <linux/sched/task.h>
David Howellse262e32d2018-11-01 23:07:23 +000031#include <uapi/linux/mount.h>
David Howells9bc61ab2018-11-04 03:19:03 -050032#include <linux/fs_context.h>
Al Viro037f11b2019-06-01 18:09:44 -040033#include <linux/shmem_fs.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010034
Ram Pai07b20882005-11-07 17:19:07 -050035#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric W. Biedermand2921682016-09-28 00:27:17 -050038/* Maximum number of mounts in a mount namespace */
39unsigned int sysctl_mount_max __read_mostly = 100000;
40
Al Viro0818bf22014-02-28 13:46:44 -050041static unsigned int m_hash_mask __read_mostly;
42static unsigned int m_hash_shift __read_mostly;
43static unsigned int mp_hash_mask __read_mostly;
44static unsigned int mp_hash_shift __read_mostly;
45
46static __initdata unsigned long mhash_entries;
47static int __init set_mhash_entries(char *str)
48{
49 if (!str)
50 return 0;
51 mhash_entries = simple_strtoul(str, &str, 0);
52 return 1;
53}
54__setup("mhash_entries=", set_mhash_entries);
55
56static __initdata unsigned long mphash_entries;
57static int __init set_mphash_entries(char *str)
58{
59 if (!str)
60 return 0;
61 mphash_entries = simple_strtoul(str, &str, 0);
62 return 1;
63}
64__setup("mphash_entries=", set_mphash_entries);
Eric Dumazet13f14b42008-02-06 01:37:57 -080065
Al Viroc7999c32014-02-27 14:40:10 -050066static u64 event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010067static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010068static DEFINE_IDA(mnt_group_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Al Viro38129a12014-03-20 21:10:51 -040070static struct hlist_head *mount_hashtable __read_mostly;
Al Viro0818bf22014-02-28 13:46:44 -050071static struct hlist_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080072static struct kmem_cache *mnt_cache __read_mostly;
Al Viro59aa0da2013-09-16 21:34:53 -040073static DECLARE_RWSEM(namespace_sem);
Al Viro4edbe132019-06-30 10:39:08 -040074static HLIST_HEAD(unmounted); /* protected by namespace_sem */
75static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Christian Brauner2a186722021-01-21 14:19:53 +010077struct mount_kattr {
78 unsigned int attr_set;
79 unsigned int attr_clr;
80 unsigned int propagation;
81 unsigned int lookup_flags;
82 bool recurse;
Christian Brauner9caccd42021-01-21 14:19:54 +010083 struct user_namespace *mnt_userns;
Christian Brauner2a186722021-01-21 14:19:53 +010084};
85
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080086/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060087struct kobject *fs_kobj;
88EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080089
Nick Piggin99b7db72010-08-18 04:37:39 +100090/*
91 * vfsmount lock may be taken for read to prevent changes to the
92 * vfsmount hash, ie. during mountpoint lookups or walking back
93 * up the tree.
94 *
95 * It should be taken for write in all cases where the vfsmount
96 * tree or hash is modified or when a vfsmount structure is modified.
97 */
Al Viro48a066e2013-09-29 22:06:07 -040098__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +100099
Christian Braunerd033cb62021-01-21 14:19:49 +0100100static inline void lock_mount_hash(void)
101{
102 write_seqlock(&mount_lock);
103}
104
105static inline void unlock_mount_hash(void)
106{
107 write_sequnlock(&mount_lock);
108}
109
Al Viro38129a12014-03-20 21:10:51 -0400110static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Ram Paib58fed82005-11-07 17:16:09 -0500112 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
113 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Al Viro0818bf22014-02-28 13:46:44 -0500114 tmp = tmp + (tmp >> m_hash_shift);
115 return &mount_hashtable[tmp & m_hash_mask];
116}
117
118static inline struct hlist_head *mp_hash(struct dentry *dentry)
119{
120 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
121 tmp = tmp + (tmp >> mp_hash_shift);
122 return &mountpoint_hashtable[tmp & mp_hash_mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Al Virob105e272011-11-24 20:38:33 -0500125static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100126{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400127 int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100128
Matthew Wilcox169b4802018-06-11 12:31:36 -0400129 if (res < 0)
130 return res;
131 mnt->mnt_id = res;
132 return 0;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100133}
134
Al Virob105e272011-11-24 20:38:33 -0500135static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100136{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400137 ida_free(&mnt_id_ida, mnt->mnt_id);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100138}
139
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100140/*
141 * Allocate a new peer group ID
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100142 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500143static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100144{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400145 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
Al Virof21f6222009-06-24 03:12:00 -0400146
Matthew Wilcox169b4802018-06-11 12:31:36 -0400147 if (res < 0)
148 return res;
149 mnt->mnt_group_id = res;
150 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100151}
152
153/*
154 * Release a peer group ID
155 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500156void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100157{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400158 ida_free(&mnt_group_ida, mnt->mnt_group_id);
Al Viro15169fe2011-11-25 00:50:41 -0500159 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100160}
161
Nick Pigginb3e19d92011-01-07 17:50:11 +1100162/*
163 * vfsmount lock must be held for read
164 */
Al Viro83adc752011-11-24 22:37:54 -0500165static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100166{
167#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500168 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100169#else
170 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500171 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100172 preempt_enable();
173#endif
174}
175
Nick Pigginb3e19d92011-01-07 17:50:11 +1100176/*
177 * vfsmount lock must be held for write
178 */
Eric Biggersedf7ddb2020-10-31 21:40:21 -0700179int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100180{
181#ifdef CONFIG_SMP
Eric Biggersedf7ddb2020-10-31 21:40:21 -0700182 int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100183 int cpu;
184
185 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500186 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100187 }
188
189 return count;
190#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500191 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100192#endif
193}
194
Al Virob105e272011-11-24 20:38:33 -0500195static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Al Viroc63181e2011-11-25 02:35:16 -0500197 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
198 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100199 int err;
200
Al Viroc63181e2011-11-25 02:35:16 -0500201 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800202 if (err)
203 goto out_free_cache;
204
205 if (name) {
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800206 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
Al Viroc63181e2011-11-25 02:35:16 -0500207 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800208 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100209 }
210
Nick Pigginb3e19d92011-01-07 17:50:11 +1100211#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500212 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
213 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100214 goto out_free_devname;
215
Al Viroc63181e2011-11-25 02:35:16 -0500216 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100217#else
Al Viroc63181e2011-11-25 02:35:16 -0500218 mnt->mnt_count = 1;
219 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100220#endif
221
Al Viro38129a12014-03-20 21:10:51 -0400222 INIT_HLIST_NODE(&mnt->mnt_hash);
Al Viroc63181e2011-11-25 02:35:16 -0500223 INIT_LIST_HEAD(&mnt->mnt_child);
224 INIT_LIST_HEAD(&mnt->mnt_mounts);
225 INIT_LIST_HEAD(&mnt->mnt_list);
226 INIT_LIST_HEAD(&mnt->mnt_expire);
227 INIT_LIST_HEAD(&mnt->mnt_share);
228 INIT_LIST_HEAD(&mnt->mnt_slave_list);
229 INIT_LIST_HEAD(&mnt->mnt_slave);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700230 INIT_HLIST_NODE(&mnt->mnt_mp_list);
Eric W. Biederman99b19d12016-10-24 16:16:13 -0500231 INIT_LIST_HEAD(&mnt->mnt_umounting);
Al Viro56cbb422019-07-04 16:57:51 -0400232 INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
Christian Braunera6435942021-01-21 14:19:20 +0100233 mnt->mnt.mnt_userns = &init_user_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Al Viroc63181e2011-11-25 02:35:16 -0500235 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800236
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000237#ifdef CONFIG_SMP
238out_free_devname:
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800239 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000240#endif
Li Zefan88b38782008-07-21 18:06:36 +0800241out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500242 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800243out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500244 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800245 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Dave Hansen83660252008-02-15 14:37:30 -0800248/*
249 * Most r/o checks on a fs are for operations that take
250 * discrete amounts of time, like a write() or unlink().
251 * We must keep track of when those operations start
252 * (for permission checks) and when they end, so that
253 * we can determine when writes are able to occur to
254 * a filesystem.
255 */
Dave Hansen3d733632008-02-15 14:37:59 -0800256/*
257 * __mnt_is_readonly: check whether a mount is read-only
258 * @mnt: the mount to check for its write status
259 *
260 * This shouldn't be used directly ouside of the VFS.
261 * It does not guarantee that the filesystem will stay
262 * r/w, just that it is right *now*. This can not and
263 * should not be used in place of IS_RDONLY(inode).
264 * mnt_want/drop_write() will _keep_ the filesystem
265 * r/w.
266 */
David Howells43f5e652018-11-01 23:07:25 +0000267bool __mnt_is_readonly(struct vfsmount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800268{
David Howells43f5e652018-11-01 23:07:25 +0000269 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800270}
271EXPORT_SYMBOL_GPL(__mnt_is_readonly);
272
Al Viro83adc752011-11-24 22:37:54 -0500273static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800274{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000275#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500276 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000277#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500278 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000279#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800280}
Dave Hansen3d733632008-02-15 14:37:59 -0800281
Al Viro83adc752011-11-24 22:37:54 -0500282static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800283{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000284#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500285 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000286#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500287 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000288#endif
289}
290
Al Viro83adc752011-11-24 22:37:54 -0500291static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000292{
293#ifdef CONFIG_SMP
294 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800295 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800296
297 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500298 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800299 }
Dave Hansen3d733632008-02-15 14:37:59 -0800300
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000301 return count;
302#else
303 return mnt->mnt_writers;
304#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800305}
306
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100307static int mnt_is_readonly(struct vfsmount *mnt)
308{
309 if (mnt->mnt_sb->s_readonly_remount)
310 return 1;
311 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
312 smp_rmb();
313 return __mnt_is_readonly(mnt);
314}
315
Dave Hansen3d733632008-02-15 14:37:59 -0800316/*
Jan Karaeb04c282012-06-12 16:20:35 +0200317 * Most r/o & frozen checks on a fs are for operations that take discrete
318 * amounts of time, like a write() or unlink(). We must keep track of when
319 * those operations start (for permission checks) and when they end, so that we
320 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800321 */
Dave Hansen83660252008-02-15 14:37:30 -0800322/**
Jan Karaeb04c282012-06-12 16:20:35 +0200323 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500324 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800325 *
Jan Karaeb04c282012-06-12 16:20:35 +0200326 * This tells the low-level filesystem that a write is about to be performed to
327 * it, and makes sure that writes are allowed (mnt it read-write) before
328 * returning success. This operation does not protect against filesystem being
329 * frozen. When the write operation is finished, __mnt_drop_write() must be
330 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800331 */
Jan Karaeb04c282012-06-12 16:20:35 +0200332int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800333{
Al Viro83adc752011-11-24 22:37:54 -0500334 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800335 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800336
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000337 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100338 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000339 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100340 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000341 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
342 * incremented count after it has set MNT_WRITE_HOLD.
343 */
344 smp_mb();
Mark Rutland6aa7de02017-10-23 14:07:29 -0700345 while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000346 cpu_relax();
347 /*
348 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
349 * be set to match its requirements. So we must not load that until
350 * MNT_WRITE_HOLD is cleared.
351 */
352 smp_rmb();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100353 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100354 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800355 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800356 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000357 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200358
359 return ret;
360}
361
362/**
363 * mnt_want_write - get write access to a mount
364 * @m: the mount on which to take a write
365 *
366 * This tells the low-level filesystem that a write is about to be performed to
367 * it, and makes sure that writes are allowed (mount is read-write, filesystem
368 * is not frozen) before returning success. When the write operation is
369 * finished, mnt_drop_write() must be called. This is effectively a refcount.
370 */
371int mnt_want_write(struct vfsmount *m)
372{
373 int ret;
374
375 sb_start_write(m->mnt_sb);
376 ret = __mnt_want_write(m);
377 if (ret)
378 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800379 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800380}
381EXPORT_SYMBOL_GPL(mnt_want_write);
382
383/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000384 * mnt_clone_write - get write access to a mount
385 * @mnt: the mount on which to take a write
386 *
387 * This is effectively like mnt_want_write, except
388 * it must only be used to take an extra write reference
389 * on a mountpoint that we already know has a write reference
390 * on it. This allows some optimisation.
391 *
392 * After finished, mnt_drop_write must be called as usual to
393 * drop the reference.
394 */
395int mnt_clone_write(struct vfsmount *mnt)
396{
397 /* superblock may be r/o */
398 if (__mnt_is_readonly(mnt))
399 return -EROFS;
400 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500401 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000402 preempt_enable();
403 return 0;
404}
405EXPORT_SYMBOL_GPL(mnt_clone_write);
406
407/**
Jan Karaeb04c282012-06-12 16:20:35 +0200408 * __mnt_want_write_file - get write access to a file's mount
409 * @file: the file who's mount on which to take a write
410 *
411 * This is like __mnt_want_write, but it takes a file and can
412 * do some optimisations if the file is open for write already
413 */
414int __mnt_want_write_file(struct file *file)
415{
Al Viro83f936c2014-03-14 12:02:47 -0400416 if (!(file->f_mode & FMODE_WRITER))
Jan Karaeb04c282012-06-12 16:20:35 +0200417 return __mnt_want_write(file->f_path.mnt);
418 else
419 return mnt_clone_write(file->f_path.mnt);
420}
421
422/**
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200423 * mnt_want_write_file - get write access to a file's mount
424 * @file: the file who's mount on which to take a write
425 *
426 * This is like mnt_want_write, but it takes a file and can
427 * do some optimisations if the file is open for write already
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200428 */
429int mnt_want_write_file(struct file *file)
430{
431 int ret;
432
Miklos Szeredia6795a52018-07-18 15:44:43 +0200433 sb_start_write(file_inode(file)->i_sb);
Jan Karaeb04c282012-06-12 16:20:35 +0200434 ret = __mnt_want_write_file(file);
435 if (ret)
Miklos Szeredia6795a52018-07-18 15:44:43 +0200436 sb_end_write(file_inode(file)->i_sb);
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200437 return ret;
438}
npiggin@suse.de96029c42009-04-26 20:25:55 +1000439EXPORT_SYMBOL_GPL(mnt_want_write_file);
440
441/**
Jan Karaeb04c282012-06-12 16:20:35 +0200442 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800443 * @mnt: the mount on which to give up write access
444 *
445 * Tells the low-level filesystem that we are done
446 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200447 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800448 */
Jan Karaeb04c282012-06-12 16:20:35 +0200449void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800450{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000451 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500452 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000453 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800454}
Jan Karaeb04c282012-06-12 16:20:35 +0200455
456/**
457 * mnt_drop_write - give up write access to a mount
458 * @mnt: the mount on which to give up write access
459 *
460 * Tells the low-level filesystem that we are done performing writes to it and
461 * also allows filesystem to be frozen again. Must be matched with
462 * mnt_want_write() call above.
463 */
464void mnt_drop_write(struct vfsmount *mnt)
465{
466 __mnt_drop_write(mnt);
467 sb_end_write(mnt->mnt_sb);
468}
Dave Hansen83660252008-02-15 14:37:30 -0800469EXPORT_SYMBOL_GPL(mnt_drop_write);
470
Jan Karaeb04c282012-06-12 16:20:35 +0200471void __mnt_drop_write_file(struct file *file)
472{
473 __mnt_drop_write(file->f_path.mnt);
474}
475
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200476void mnt_drop_write_file(struct file *file)
477{
Miklos Szeredia6795a52018-07-18 15:44:43 +0200478 __mnt_drop_write_file(file);
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200479 sb_end_write(file_inode(file)->i_sb);
480}
Al Viro2a79f172011-12-09 08:06:57 -0500481EXPORT_SYMBOL(mnt_drop_write_file);
482
Christian Braunerfbdc2f62021-01-21 14:19:51 +0100483static inline int mnt_hold_writers(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800484{
Al Viro83adc752011-11-24 22:37:54 -0500485 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000486 /*
487 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
488 * should be visible before we do.
489 */
490 smp_mb();
491
492 /*
493 * With writers on hold, if this value is zero, then there are
494 * definitely no active writers (although held writers may subsequently
495 * increment the count, they'll have to wait, and decrement it after
496 * seeing MNT_READONLY).
497 *
498 * It is OK to have counter incremented on one CPU and decremented on
499 * another: the sum will add up correctly. The danger would be when we
500 * sum up each counter, if we read a counter before it is incremented,
501 * but then read another CPU's count which it has been subsequently
502 * decremented from -- we would see more decrements than we should.
503 * MNT_WRITE_HOLD protects against this scenario, because
504 * mnt_want_write first increments count, then smp_mb, then spins on
505 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
506 * we're counting up here.
507 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100508 if (mnt_get_writers(mnt) > 0)
Christian Braunerfbdc2f62021-01-21 14:19:51 +0100509 return -EBUSY;
510
511 return 0;
512}
513
514static inline void mnt_unhold_writers(struct mount *mnt)
515{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000516 /*
517 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
518 * that become unheld will see MNT_READONLY.
519 */
520 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500521 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Christian Braunerfbdc2f62021-01-21 14:19:51 +0100522}
523
524static int mnt_make_readonly(struct mount *mnt)
525{
526 int ret;
527
528 ret = mnt_hold_writers(mnt);
529 if (!ret)
530 mnt->mnt.mnt_flags |= MNT_READONLY;
531 mnt_unhold_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800532 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800533}
Dave Hansen83660252008-02-15 14:37:30 -0800534
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100535int sb_prepare_remount_readonly(struct super_block *sb)
536{
537 struct mount *mnt;
538 int err = 0;
539
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100540 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
541 if (atomic_long_read(&sb->s_remove_count))
542 return -EBUSY;
543
Al Viro719ea2f2013-09-29 11:24:49 -0400544 lock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100545 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
546 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
547 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
548 smp_mb();
549 if (mnt_get_writers(mnt) > 0) {
550 err = -EBUSY;
551 break;
552 }
553 }
554 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100555 if (!err && atomic_long_read(&sb->s_remove_count))
556 err = -EBUSY;
557
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100558 if (!err) {
559 sb->s_readonly_remount = 1;
560 smp_wmb();
561 }
562 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
563 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
564 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
565 }
Al Viro719ea2f2013-09-29 11:24:49 -0400566 unlock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100567
568 return err;
569}
570
Al Virob105e272011-11-24 20:38:33 -0500571static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Christian Braunera6435942021-01-21 14:19:20 +0100573 struct user_namespace *mnt_userns;
574
575 mnt_userns = mnt_user_ns(&mnt->mnt);
576 if (mnt_userns != &init_user_ns)
577 put_user_ns(mnt_userns);
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800578 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000579#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500580 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000581#endif
Al Virob105e272011-11-24 20:38:33 -0500582 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
David Howells8ffcb322014-01-24 12:17:54 +0000585static void delayed_free_vfsmnt(struct rcu_head *head)
586{
587 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
588}
589
Al Viro48a066e2013-09-29 22:06:07 -0400590/* call under rcu_read_lock */
Al Viro294d71f2015-05-08 11:43:53 -0400591int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
Al Viro48a066e2013-09-29 22:06:07 -0400592{
593 struct mount *mnt;
594 if (read_seqretry(&mount_lock, seq))
Al Viro294d71f2015-05-08 11:43:53 -0400595 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400596 if (bastard == NULL)
Al Viro294d71f2015-05-08 11:43:53 -0400597 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400598 mnt = real_mount(bastard);
599 mnt_add_count(mnt, 1);
Al Viro119e1ef2018-08-09 17:51:32 -0400600 smp_mb(); // see mntput_no_expire()
Al Viro48a066e2013-09-29 22:06:07 -0400601 if (likely(!read_seqretry(&mount_lock, seq)))
Al Viro294d71f2015-05-08 11:43:53 -0400602 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400603 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
604 mnt_add_count(mnt, -1);
Al Viro294d71f2015-05-08 11:43:53 -0400605 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400606 }
Al Viro119e1ef2018-08-09 17:51:32 -0400607 lock_mount_hash();
608 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
609 mnt_add_count(mnt, -1);
610 unlock_mount_hash();
611 return 1;
612 }
613 unlock_mount_hash();
614 /* caller will mntput() */
Al Viro294d71f2015-05-08 11:43:53 -0400615 return -1;
616}
617
618/* call under rcu_read_lock */
619bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
620{
621 int res = __legitimize_mnt(bastard, seq);
622 if (likely(!res))
623 return true;
624 if (unlikely(res < 0)) {
625 rcu_read_unlock();
626 mntput(bastard);
627 rcu_read_lock();
628 }
Al Viro48a066e2013-09-29 22:06:07 -0400629 return false;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
Al Viro474279d2013-10-01 16:11:26 -0400633 * find the first mount at @dentry on vfsmount @mnt.
Al Viro48a066e2013-09-29 22:06:07 -0400634 * call under rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Al Viro474279d2013-10-01 16:11:26 -0400636struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Al Viro38129a12014-03-20 21:10:51 -0400638 struct hlist_head *head = m_hash(mnt, dentry);
Al Viro474279d2013-10-01 16:11:26 -0400639 struct mount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Al Viro38129a12014-03-20 21:10:51 -0400641 hlist_for_each_entry_rcu(p, head, mnt_hash)
Al Viro474279d2013-10-01 16:11:26 -0400642 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
643 return p;
644 return NULL;
645}
646
647/*
David Howellsf015f1262012-06-25 12:55:28 +0100648 * lookup_mnt - Return the first child mount mounted at path
649 *
650 * "First" means first mounted chronologically. If you create the
651 * following mounts:
652 *
653 * mount /dev/sda1 /mnt
654 * mount /dev/sda2 /mnt
655 * mount /dev/sda3 /mnt
656 *
657 * Then lookup_mnt() on the base /mnt dentry in the root mount will
658 * return successively the root dentry and vfsmount of /dev/sda1, then
659 * /dev/sda2, then /dev/sda3, then NULL.
660 *
661 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500662 */
Al Viroca71cf72016-11-20 19:45:28 -0500663struct vfsmount *lookup_mnt(const struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500664{
Al Viroc7105362011-11-24 18:22:03 -0500665 struct mount *child_mnt;
Al Viro48a066e2013-09-29 22:06:07 -0400666 struct vfsmount *m;
667 unsigned seq;
Nick Piggin99b7db72010-08-18 04:37:39 +1000668
Al Viro48a066e2013-09-29 22:06:07 -0400669 rcu_read_lock();
670 do {
671 seq = read_seqbegin(&mount_lock);
672 child_mnt = __lookup_mnt(path->mnt, path->dentry);
673 m = child_mnt ? &child_mnt->mnt : NULL;
674 } while (!legitimize_mnt(m, seq));
675 rcu_read_unlock();
676 return m;
Ram Paia05964f2005-11-07 17:20:17 -0500677}
678
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +0200679static inline void lock_ns_list(struct mnt_namespace *ns)
680{
681 spin_lock(&ns->ns_lock);
682}
683
684static inline void unlock_ns_list(struct mnt_namespace *ns)
685{
686 spin_unlock(&ns->ns_lock);
687}
688
689static inline bool mnt_is_cursor(struct mount *mnt)
690{
691 return mnt->mnt.mnt_flags & MNT_CURSOR;
692}
693
Eric W. Biederman7af13642013-10-04 19:15:13 -0700694/*
695 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
696 * current mount namespace.
697 *
698 * The common case is dentries are not mountpoints at all and that
699 * test is handled inline. For the slow case when we are actually
700 * dealing with a mountpoint of some kind, walk through all of the
701 * mounts in the current mount namespace and test to see if the dentry
702 * is a mountpoint.
703 *
704 * The mount_hashtable is not usable in the context because we
705 * need to identify all mounts that may be in the current mount
706 * namespace not just a mount that happens to have some specified
707 * parent mount.
708 */
709bool __is_local_mountpoint(struct dentry *dentry)
710{
711 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
712 struct mount *mnt;
713 bool is_covered = false;
714
Eric W. Biederman7af13642013-10-04 19:15:13 -0700715 down_read(&namespace_sem);
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +0200716 lock_ns_list(ns);
Eric W. Biederman7af13642013-10-04 19:15:13 -0700717 list_for_each_entry(mnt, &ns->list, mnt_list) {
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +0200718 if (mnt_is_cursor(mnt))
719 continue;
Eric W. Biederman7af13642013-10-04 19:15:13 -0700720 is_covered = (mnt->mnt_mountpoint == dentry);
721 if (is_covered)
722 break;
723 }
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +0200724 unlock_ns_list(ns);
Eric W. Biederman7af13642013-10-04 19:15:13 -0700725 up_read(&namespace_sem);
Nikolay Borisov5ad05cc2020-03-04 18:12:45 +0200726
Eric W. Biederman7af13642013-10-04 19:15:13 -0700727 return is_covered;
728}
729
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800730static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
Al Viro84d17192013-03-15 10:53:28 -0400731{
Al Viro0818bf22014-02-28 13:46:44 -0500732 struct hlist_head *chain = mp_hash(dentry);
Al Viro84d17192013-03-15 10:53:28 -0400733 struct mountpoint *mp;
734
Al Viro0818bf22014-02-28 13:46:44 -0500735 hlist_for_each_entry(mp, chain, m_hash) {
Al Viro84d17192013-03-15 10:53:28 -0400736 if (mp->m_dentry == dentry) {
Al Viro84d17192013-03-15 10:53:28 -0400737 mp->m_count++;
738 return mp;
739 }
740 }
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800741 return NULL;
742}
743
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300744static struct mountpoint *get_mountpoint(struct dentry *dentry)
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800745{
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300746 struct mountpoint *mp, *new = NULL;
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800747 int ret;
Al Viro84d17192013-03-15 10:53:28 -0400748
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300749 if (d_mountpoint(dentry)) {
Benjamin Coddington1e9c75f2018-10-03 10:18:33 -0400750 /* might be worth a WARN_ON() */
751 if (d_unlinked(dentry))
752 return ERR_PTR(-ENOENT);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300753mountpoint:
754 read_seqlock_excl(&mount_lock);
755 mp = lookup_mountpoint(dentry);
756 read_sequnlock_excl(&mount_lock);
757 if (mp)
758 goto done;
Al Viro84d17192013-03-15 10:53:28 -0400759 }
Miklos Szeredieed81002013-09-05 14:39:11 +0200760
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300761 if (!new)
762 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
763 if (!new)
764 return ERR_PTR(-ENOMEM);
765
766
767 /* Exactly one processes may set d_mounted */
768 ret = d_set_mounted(dentry);
769
770 /* Someone else set d_mounted? */
771 if (ret == -EBUSY)
772 goto mountpoint;
773
774 /* The dentry is not available as a mountpoint? */
775 mp = ERR_PTR(ret);
776 if (ret)
777 goto done;
778
779 /* Add the new mountpoint to the hash table */
780 read_seqlock_excl(&mount_lock);
Al Viro4edbe132019-06-30 10:39:08 -0400781 new->m_dentry = dget(dentry);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300782 new->m_count = 1;
783 hlist_add_head(&new->m_hash, mp_hash(dentry));
784 INIT_HLIST_HEAD(&new->m_list);
785 read_sequnlock_excl(&mount_lock);
786
787 mp = new;
788 new = NULL;
789done:
790 kfree(new);
Al Viro84d17192013-03-15 10:53:28 -0400791 return mp;
792}
793
Al Viro4edbe132019-06-30 10:39:08 -0400794/*
795 * vfsmount lock must be held. Additionally, the caller is responsible
796 * for serializing calls for given disposal list.
797 */
798static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
Al Viro84d17192013-03-15 10:53:28 -0400799{
800 if (!--mp->m_count) {
801 struct dentry *dentry = mp->m_dentry;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700802 BUG_ON(!hlist_empty(&mp->m_list));
Al Viro84d17192013-03-15 10:53:28 -0400803 spin_lock(&dentry->d_lock);
804 dentry->d_flags &= ~DCACHE_MOUNTED;
805 spin_unlock(&dentry->d_lock);
Al Viro4edbe132019-06-30 10:39:08 -0400806 dput_to_list(dentry, list);
Al Viro0818bf22014-02-28 13:46:44 -0500807 hlist_del(&mp->m_hash);
Al Viro84d17192013-03-15 10:53:28 -0400808 kfree(mp);
809 }
810}
811
Al Viro4edbe132019-06-30 10:39:08 -0400812/* called with namespace_lock and vfsmount lock */
813static void put_mountpoint(struct mountpoint *mp)
814{
815 __put_mountpoint(mp, &ex_mountpoints);
816}
817
Al Viro143c8c92011-11-25 00:46:35 -0500818static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800820 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Nick Piggin99b7db72010-08-18 04:37:39 +1000823/*
824 * vfsmount lock must be held for write
825 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800826static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500827{
828 if (ns) {
829 ns->event = ++event;
830 wake_up_interruptible(&ns->poll);
831 }
832}
833
Nick Piggin99b7db72010-08-18 04:37:39 +1000834/*
835 * vfsmount lock must be held for write
836 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800837static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500838{
839 if (ns && ns->event != event) {
840 ns->event = event;
841 wake_up_interruptible(&ns->poll);
842 }
843}
844
Nick Piggin99b7db72010-08-18 04:37:39 +1000845/*
846 * vfsmount lock must be held for write
847 */
Al Viroe4e59902019-06-29 12:58:42 -0400848static struct mountpoint *unhash_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Al Viroe4e59902019-06-29 12:58:42 -0400850 struct mountpoint *mp;
Al Viro0714a532011-11-24 22:19:58 -0500851 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500852 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500853 list_del_init(&mnt->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -0400854 hlist_del_init_rcu(&mnt->mnt_hash);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700855 hlist_del_init(&mnt->mnt_mp_list);
Al Viroe4e59902019-06-29 12:58:42 -0400856 mp = mnt->mnt_mp;
Al Viro84d17192013-03-15 10:53:28 -0400857 mnt->mnt_mp = NULL;
Al Viroe4e59902019-06-29 12:58:42 -0400858 return mp;
Eric W. Biederman7bdb11de2014-12-29 13:03:41 -0600859}
860
861/*
862 * vfsmount lock must be held for write
863 */
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600864static void umount_mnt(struct mount *mnt)
865{
Al Viroe4e59902019-06-29 12:58:42 -0400866 put_mountpoint(unhash_mnt(mnt));
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600867}
868
869/*
870 * vfsmount lock must be held for write
871 */
Al Viro84d17192013-03-15 10:53:28 -0400872void mnt_set_mountpoint(struct mount *mnt,
873 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500874 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500875{
Al Viro84d17192013-03-15 10:53:28 -0400876 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500877 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro4edbe132019-06-30 10:39:08 -0400878 child_mnt->mnt_mountpoint = mp->m_dentry;
Al Viro3a2393d2011-11-25 03:19:09 -0500879 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400880 child_mnt->mnt_mp = mp;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700881 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
Ram Paib90fa9a2005-11-07 17:19:50 -0500882}
883
Eric W. Biederman1064f872017-01-20 18:28:35 +1300884static void __attach_mnt(struct mount *mnt, struct mount *parent)
885{
886 hlist_add_head_rcu(&mnt->mnt_hash,
887 m_hash(&parent->mnt, mnt->mnt_mountpoint));
888 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
889}
890
Nick Piggin99b7db72010-08-18 04:37:39 +1000891/*
892 * vfsmount lock must be held for write
893 */
Al Viro84d17192013-03-15 10:53:28 -0400894static void attach_mnt(struct mount *mnt,
895 struct mount *parent,
896 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Al Viro84d17192013-03-15 10:53:28 -0400898 mnt_set_mountpoint(parent, mp, mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +1300899 __attach_mnt(mnt, parent);
Ram Paib90fa9a2005-11-07 17:19:50 -0500900}
901
Eric W. Biederman1064f872017-01-20 18:28:35 +1300902void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
Al Viro12a5b522014-08-10 03:44:55 -0400903{
Eric W. Biederman1064f872017-01-20 18:28:35 +1300904 struct mountpoint *old_mp = mnt->mnt_mp;
Eric W. Biederman1064f872017-01-20 18:28:35 +1300905 struct mount *old_parent = mnt->mnt_parent;
906
907 list_del_init(&mnt->mnt_child);
908 hlist_del_init(&mnt->mnt_mp_list);
909 hlist_del_init_rcu(&mnt->mnt_hash);
910
911 attach_mnt(mnt, parent, mp);
912
913 put_mountpoint(old_mp);
Eric W. Biederman1064f872017-01-20 18:28:35 +1300914 mnt_add_count(old_parent, -1);
Al Viro12a5b522014-08-10 03:44:55 -0400915}
916
Ram Paib90fa9a2005-11-07 17:19:50 -0500917/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000918 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500919 */
Eric W. Biederman1064f872017-01-20 18:28:35 +1300920static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500921{
Al Viro0714a532011-11-24 22:19:58 -0500922 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500923 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500924 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500925 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500926
Al Viro0714a532011-11-24 22:19:58 -0500927 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500928
Al Viro1a4eeaf2011-11-25 02:19:55 -0500929 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400930 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500931 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500932
Ram Paib90fa9a2005-11-07 17:19:50 -0500933 list_splice(&head, n->list.prev);
934
Eric W. Biedermand2921682016-09-28 00:27:17 -0500935 n->mounts += n->pending_mounts;
936 n->pending_mounts = 0;
937
Eric W. Biederman1064f872017-01-20 18:28:35 +1300938 __attach_mnt(mnt, parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800939 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Al Viro909b0a82011-11-25 03:06:56 -0500942static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Al Viro6b41d532011-11-24 23:24:33 -0500944 struct list_head *next = p->mnt_mounts.next;
945 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500947 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500949 next = p->mnt_child.next;
950 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
Al Viro0714a532011-11-24 22:19:58 -0500952 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 }
Al Viro6b41d532011-11-24 23:24:33 -0500955 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Al Viro315fc832011-11-24 18:57:30 -0500958static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500959{
Al Viro6b41d532011-11-24 23:24:33 -0500960 struct list_head *prev = p->mnt_mounts.prev;
961 while (prev != &p->mnt_mounts) {
962 p = list_entry(prev, struct mount, mnt_child);
963 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500964 }
965 return p;
966}
967
Al Viro8f2918892018-11-04 06:48:34 -0500968/**
969 * vfs_create_mount - Create a mount for a configured superblock
970 * @fc: The configuration context with the superblock attached
971 *
972 * Create a mount to an already configured superblock. If necessary, the
973 * caller should invoke vfs_get_tree() before calling this.
974 *
975 * Note that this does not attach the mount to anything.
976 */
977struct vfsmount *vfs_create_mount(struct fs_context *fc)
Al Viro9d412a42011-03-17 22:08:28 -0400978{
Al Virob105e272011-11-24 20:38:33 -0500979 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400980
Al Viro8f2918892018-11-04 06:48:34 -0500981 if (!fc->root)
982 return ERR_PTR(-EINVAL);
Al Viro9d412a42011-03-17 22:08:28 -0400983
Al Viro8f2918892018-11-04 06:48:34 -0500984 mnt = alloc_vfsmnt(fc->source ?: "none");
Al Viro9d412a42011-03-17 22:08:28 -0400985 if (!mnt)
986 return ERR_PTR(-ENOMEM);
987
Al Viro8f2918892018-11-04 06:48:34 -0500988 if (fc->sb_flags & SB_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500989 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400990
Al Viro8f2918892018-11-04 06:48:34 -0500991 atomic_inc(&fc->root->d_sb->s_active);
992 mnt->mnt.mnt_sb = fc->root->d_sb;
993 mnt->mnt.mnt_root = dget(fc->root);
994 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
995 mnt->mnt_parent = mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400996
Al Viro719ea2f2013-09-29 11:24:49 -0400997 lock_mount_hash();
Al Viro8f2918892018-11-04 06:48:34 -0500998 list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -0400999 unlock_mount_hash();
Al Virob105e272011-11-24 20:38:33 -05001000 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -04001001}
Al Viro8f2918892018-11-04 06:48:34 -05001002EXPORT_SYMBOL(vfs_create_mount);
1003
1004struct vfsmount *fc_mount(struct fs_context *fc)
1005{
1006 int err = vfs_get_tree(fc);
1007 if (!err) {
1008 up_write(&fc->root->d_sb->s_umount);
1009 return vfs_create_mount(fc);
1010 }
1011 return ERR_PTR(err);
1012}
1013EXPORT_SYMBOL(fc_mount);
1014
David Howells9bc61ab2018-11-04 03:19:03 -05001015struct vfsmount *vfs_kern_mount(struct file_system_type *type,
1016 int flags, const char *name,
1017 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
David Howells9bc61ab2018-11-04 03:19:03 -05001019 struct fs_context *fc;
Al Viro8f2918892018-11-04 06:48:34 -05001020 struct vfsmount *mnt;
David Howells9bc61ab2018-11-04 03:19:03 -05001021 int ret = 0;
Al Viro9d412a42011-03-17 22:08:28 -04001022
1023 if (!type)
David Howells3e1aeb02018-11-01 23:07:25 +00001024 return ERR_PTR(-EINVAL);
Al Viro9d412a42011-03-17 22:08:28 -04001025
David Howells9bc61ab2018-11-04 03:19:03 -05001026 fc = fs_context_for_mount(type, flags);
1027 if (IS_ERR(fc))
1028 return ERR_CAST(fc);
1029
David Howells3e1aeb02018-11-01 23:07:25 +00001030 if (name)
1031 ret = vfs_parse_fs_string(fc, "source",
1032 name, strlen(name));
David Howells9bc61ab2018-11-04 03:19:03 -05001033 if (!ret)
1034 ret = parse_monolithic_mount_data(fc, data);
1035 if (!ret)
Al Viro8f2918892018-11-04 06:48:34 -05001036 mnt = fc_mount(fc);
1037 else
1038 mnt = ERR_PTR(ret);
Al Viro9d412a42011-03-17 22:08:28 -04001039
David Howells9bc61ab2018-11-04 03:19:03 -05001040 put_fs_context(fc);
Al Viro8f2918892018-11-04 06:48:34 -05001041 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04001042}
1043EXPORT_SYMBOL_GPL(vfs_kern_mount);
1044
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13001045struct vfsmount *
1046vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1047 const char *name, void *data)
1048{
1049 /* Until it is worked out how to pass the user namespace
1050 * through from the parent mount to the submount don't support
1051 * unprivileged mounts with submounts.
1052 */
1053 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1054 return ERR_PTR(-EPERM);
1055
David Howellse462ec52017-07-17 08:45:35 +01001056 return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13001057}
1058EXPORT_SYMBOL_GPL(vfs_submount);
1059
Al Viro87129cc2011-11-24 21:24:27 -05001060static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -05001061 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Al Viro87129cc2011-11-24 21:24:27 -05001063 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +01001064 struct mount *mnt;
1065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
David Howellsbe34d1a2012-06-25 12:55:18 +01001067 mnt = alloc_vfsmnt(old->mnt_devname);
1068 if (!mnt)
1069 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001070
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001071 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +01001072 mnt->mnt_group_id = 0; /* not a peer of original */
1073 else
1074 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001075
David Howellsbe34d1a2012-06-25 12:55:18 +01001076 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1077 err = mnt_alloc_group_id(mnt);
1078 if (err)
1079 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001081
Al Viro16a34ad2018-04-19 22:03:08 -04001082 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1083 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001084
David Howellsbe34d1a2012-06-25 12:55:18 +01001085 atomic_inc(&sb->s_active);
Christian Braunera6435942021-01-21 14:19:20 +01001086 mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
1087 if (mnt->mnt.mnt_userns != &init_user_ns)
1088 mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
David Howellsbe34d1a2012-06-25 12:55:18 +01001089 mnt->mnt.mnt_sb = sb;
1090 mnt->mnt.mnt_root = dget(root);
1091 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1092 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -04001093 lock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001094 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -04001095 unlock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001096
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001097 if ((flag & CL_SLAVE) ||
1098 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +01001099 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1100 mnt->mnt_master = old;
1101 CLEAR_MNT_SHARED(mnt);
1102 } else if (!(flag & CL_PRIVATE)) {
1103 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1104 list_add(&mnt->mnt_share, &old->mnt_share);
1105 if (IS_MNT_SLAVE(old))
1106 list_add(&mnt->mnt_slave, &old->mnt_slave);
1107 mnt->mnt_master = old->mnt_master;
Al Viro5235d442016-11-20 19:33:09 -05001108 } else {
1109 CLEAR_MNT_SHARED(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001110 }
1111 if (flag & CL_MAKE_SHARED)
1112 set_mnt_shared(mnt);
1113
1114 /* stick the duplicate mount on the same expiry list
1115 * as the original if that was on one */
1116 if (flag & CL_EXPIRE) {
1117 if (!list_empty(&old->mnt_expire))
1118 list_add(&mnt->mnt_expire, &old->mnt_expire);
1119 }
1120
Al Virocb338d02011-11-24 20:55:08 -05001121 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001122
1123 out_free:
David Howells8ffcb322014-01-24 12:17:54 +00001124 mnt_free_id(mnt);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001125 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001126 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Al Viro9ea459e12014-08-08 13:08:20 -04001129static void cleanup_mnt(struct mount *mnt)
1130{
Al Viro56cbb422019-07-04 16:57:51 -04001131 struct hlist_node *p;
1132 struct mount *m;
Al Viro9ea459e12014-08-08 13:08:20 -04001133 /*
Al Viro56cbb422019-07-04 16:57:51 -04001134 * The warning here probably indicates that somebody messed
1135 * up a mnt_want/drop_write() pair. If this happens, the
1136 * filesystem was probably unable to make r/w->r/o transitions.
Al Viro9ea459e12014-08-08 13:08:20 -04001137 * The locking used to deal with mnt_count decrement provides barriers,
1138 * so mnt_get_writers() below is safe.
1139 */
1140 WARN_ON(mnt_get_writers(mnt));
1141 if (unlikely(mnt->mnt_pins.first))
1142 mnt_pin_kill(mnt);
Al Viro56cbb422019-07-04 16:57:51 -04001143 hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
1144 hlist_del(&m->mnt_umount);
1145 mntput(&m->mnt);
1146 }
Al Viro9ea459e12014-08-08 13:08:20 -04001147 fsnotify_vfsmount_delete(&mnt->mnt);
1148 dput(mnt->mnt.mnt_root);
1149 deactivate_super(mnt->mnt.mnt_sb);
1150 mnt_free_id(mnt);
1151 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1152}
1153
1154static void __cleanup_mnt(struct rcu_head *head)
1155{
1156 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1157}
1158
1159static LLIST_HEAD(delayed_mntput_list);
1160static void delayed_mntput(struct work_struct *unused)
1161{
1162 struct llist_node *node = llist_del_all(&delayed_mntput_list);
Byungchul Park29785732017-08-07 17:44:45 +09001163 struct mount *m, *t;
Al Viro9ea459e12014-08-08 13:08:20 -04001164
Byungchul Park29785732017-08-07 17:44:45 +09001165 llist_for_each_entry_safe(m, t, node, mnt_llist)
1166 cleanup_mnt(m);
Al Viro9ea459e12014-08-08 13:08:20 -04001167}
1168static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1169
Al Viro900148d2011-11-25 00:33:11 -05001170static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001171{
Al Viro4edbe132019-06-30 10:39:08 -04001172 LIST_HEAD(list);
Eric Biggersedf7ddb2020-10-31 21:40:21 -07001173 int count;
Al Viro4edbe132019-06-30 10:39:08 -04001174
Al Viro48a066e2013-09-29 22:06:07 -04001175 rcu_read_lock();
Al Viro9ea0a462018-08-09 17:21:17 -04001176 if (likely(READ_ONCE(mnt->mnt_ns))) {
1177 /*
1178 * Since we don't do lock_mount_hash() here,
1179 * ->mnt_ns can change under us. However, if it's
1180 * non-NULL, then there's a reference that won't
1181 * be dropped until after an RCU delay done after
1182 * turning ->mnt_ns NULL. So if we observe it
1183 * non-NULL under rcu_read_lock(), the reference
1184 * we are dropping is not the final one.
1185 */
1186 mnt_add_count(mnt, -1);
Al Viro48a066e2013-09-29 22:06:07 -04001187 rcu_read_unlock();
Al Virof03c6592011-01-14 22:30:21 -05001188 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001189 }
Al Viro719ea2f2013-09-29 11:24:49 -04001190 lock_mount_hash();
Al Viro119e1ef2018-08-09 17:51:32 -04001191 /*
1192 * make sure that if __legitimize_mnt() has not seen us grab
1193 * mount_lock, we'll see their refcount increment here.
1194 */
1195 smp_mb();
Al Viro9ea0a462018-08-09 17:21:17 -04001196 mnt_add_count(mnt, -1);
Eric Biggersedf7ddb2020-10-31 21:40:21 -07001197 count = mnt_get_count(mnt);
1198 if (count != 0) {
1199 WARN_ON(count < 0);
Al Viro48a066e2013-09-29 22:06:07 -04001200 rcu_read_unlock();
Al Viro719ea2f2013-09-29 11:24:49 -04001201 unlock_mount_hash();
Nick Pigginb3e19d92011-01-07 17:50:11 +11001202 return;
1203 }
Al Viro48a066e2013-09-29 22:06:07 -04001204 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1205 rcu_read_unlock();
1206 unlock_mount_hash();
1207 return;
1208 }
1209 mnt->mnt.mnt_flags |= MNT_DOOMED;
1210 rcu_read_unlock();
Andi Kleen962830d2012-05-08 13:32:02 +09301211
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +01001212 list_del(&mnt->mnt_instance);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001213
1214 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1215 struct mount *p, *tmp;
1216 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
Al Viro4edbe132019-06-30 10:39:08 -04001217 __put_mountpoint(unhash_mnt(p), &list);
Al Viro56cbb422019-07-04 16:57:51 -04001218 hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001219 }
1220 }
Al Viro719ea2f2013-09-29 11:24:49 -04001221 unlock_mount_hash();
Al Viro4edbe132019-06-30 10:39:08 -04001222 shrink_dentry_list(&list);
Al Viro649a7952013-09-28 12:41:25 -04001223
Al Viro9ea459e12014-08-08 13:08:20 -04001224 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1225 struct task_struct *task = current;
1226 if (likely(!(task->flags & PF_KTHREAD))) {
1227 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
Jens Axboe91989c72020-10-16 09:02:26 -06001228 if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
Al Viro9ea459e12014-08-08 13:08:20 -04001229 return;
1230 }
1231 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1232 schedule_delayed_work(&delayed_mntput_work, 1);
1233 return;
1234 }
1235 cleanup_mnt(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -05001236}
Nick Pigginb3e19d92011-01-07 17:50:11 +11001237
1238void mntput(struct vfsmount *mnt)
1239{
1240 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -05001241 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001242 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -05001243 if (unlikely(m->mnt_expiry_mark))
1244 m->mnt_expiry_mark = 0;
1245 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001246 }
1247}
1248EXPORT_SYMBOL(mntput);
1249
1250struct vfsmount *mntget(struct vfsmount *mnt)
1251{
1252 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -05001253 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001254 return mnt;
1255}
1256EXPORT_SYMBOL(mntget);
1257
Ian Kentc6609c02016-11-24 08:03:41 +11001258/* path_is_mountpoint() - Check if path is a mount in the current
1259 * namespace.
1260 *
1261 * d_mountpoint() can only be used reliably to establish if a dentry is
1262 * not mounted in any namespace and that common case is handled inline.
1263 * d_mountpoint() isn't aware of the possibility there may be multiple
1264 * mounts using a given dentry in a different namespace. This function
1265 * checks if the passed in path is a mountpoint rather than the dentry
1266 * alone.
1267 */
1268bool path_is_mountpoint(const struct path *path)
1269{
1270 unsigned seq;
1271 bool res;
1272
1273 if (!d_mountpoint(path->dentry))
1274 return false;
1275
1276 rcu_read_lock();
1277 do {
1278 seq = read_seqbegin(&mount_lock);
1279 res = __path_is_mountpoint(path);
1280 } while (read_seqretry(&mount_lock, seq));
1281 rcu_read_unlock();
1282
1283 return res;
1284}
1285EXPORT_SYMBOL(path_is_mountpoint);
1286
Al Viroca71cf72016-11-20 19:45:28 -05001287struct vfsmount *mnt_clone_internal(const struct path *path)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001288{
Al Viro3064c352014-08-07 09:12:31 -04001289 struct mount *p;
1290 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1291 if (IS_ERR(p))
1292 return ERR_CAST(p);
1293 p->mnt.mnt_flags |= MNT_INTERNAL;
1294 return &p->mnt;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001297#ifdef CONFIG_PROC_FS
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001298static struct mount *mnt_list_next(struct mnt_namespace *ns,
1299 struct list_head *p)
1300{
1301 struct mount *mnt, *ret = NULL;
1302
1303 lock_ns_list(ns);
1304 list_for_each_continue(p, &ns->list) {
1305 mnt = list_entry(p, typeof(*mnt), mnt_list);
1306 if (!mnt_is_cursor(mnt)) {
1307 ret = mnt;
1308 break;
1309 }
1310 }
1311 unlock_ns_list(ns);
1312
1313 return ret;
1314}
1315
Al Viro0226f492011-12-06 12:21:54 -05001316/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static void *m_start(struct seq_file *m, loff_t *pos)
1318{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001319 struct proc_mounts *p = m->private;
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001320 struct list_head *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Ram Pai390c6842005-11-07 17:17:51 -05001322 down_read(&namespace_sem);
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001323 if (!*pos) {
1324 prev = &p->ns->list;
1325 } else {
1326 prev = &p->cursor.mnt_list;
1327
1328 /* Read after we'd reached the end? */
1329 if (list_empty(prev))
1330 return NULL;
Al Viroc7999c32014-02-27 14:40:10 -05001331 }
1332
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001333 return mnt_list_next(p->ns, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1337{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001338 struct proc_mounts *p = m->private;
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001339 struct mount *mnt = v;
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001340
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001341 ++*pos;
1342 return mnt_list_next(p->ns, &mnt->mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345static void m_stop(struct seq_file *m, void *v)
1346{
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001347 struct proc_mounts *p = m->private;
1348 struct mount *mnt = v;
1349
1350 lock_ns_list(p->ns);
1351 if (mnt)
1352 list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
1353 else
1354 list_del_init(&p->cursor.mnt_list);
1355 unlock_ns_list(p->ns);
Ram Pai390c6842005-11-07 17:17:51 -05001356 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Al Viro0226f492011-12-06 12:21:54 -05001359static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001360{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001361 struct proc_mounts *p = m->private;
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001362 struct mount *r = v;
Al Viro0226f492011-12-06 12:21:54 -05001363 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001366const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 .start = m_start,
1368 .next = m_next,
1369 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001370 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001371};
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02001372
1373void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
1374{
1375 down_read(&namespace_sem);
1376 lock_ns_list(ns);
1377 list_del(&cursor->mnt_list);
1378 unlock_ns_list(ns);
1379 up_read(&namespace_sem);
1380}
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001381#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/**
1384 * may_umount_tree - check if a mount tree is busy
1385 * @mnt: root of mount tree
1386 *
1387 * This is called to check if a tree of mounts has any
1388 * open files, pwds, chroots or sub mounts that are
1389 * busy.
1390 */
Al Viro909b0a82011-11-25 03:06:56 -05001391int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Al Viro909b0a82011-11-25 03:06:56 -05001393 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001394 int actual_refs = 0;
1395 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001396 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001397 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Nick Pigginb3e19d92011-01-07 17:50:11 +11001399 /* write lock needed for mnt_get_count */
Al Viro719ea2f2013-09-29 11:24:49 -04001400 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05001401 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001402 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Al Viro719ea2f2013-09-29 11:24:49 -04001405 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Ian Kente3474a82006-03-27 01:14:51 -08001410 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413EXPORT_SYMBOL(may_umount_tree);
1414
1415/**
1416 * may_umount - check if a mount point is busy
1417 * @mnt: root of mount
1418 *
1419 * This is called to check if a mount point has any
1420 * open files, pwds, chroots or sub mounts. If the
1421 * mount has sub mounts this will return busy
1422 * regardless of whether the sub mounts are busy.
1423 *
1424 * Doesn't take quota and stuff into account. IOW, in some cases it will
1425 * give false negatives. The main reason why it's here is that we need
1426 * a non-destructive way to look for easily umountable filesystems.
1427 */
1428int may_umount(struct vfsmount *mnt)
1429{
Ian Kente3474a82006-03-27 01:14:51 -08001430 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001431 down_read(&namespace_sem);
Al Viro719ea2f2013-09-29 11:24:49 -04001432 lock_mount_hash();
Al Viro1ab59732011-11-24 21:35:16 -05001433 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001434 ret = 0;
Al Viro719ea2f2013-09-29 11:24:49 -04001435 unlock_mount_hash();
Al Viro8ad08d82010-01-16 12:56:08 -05001436 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001437 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440EXPORT_SYMBOL(may_umount);
1441
Al Viro97216be2013-03-16 15:12:40 -04001442static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001444 struct hlist_head head;
Al Viro56cbb422019-07-04 16:57:51 -04001445 struct hlist_node *p;
1446 struct mount *m;
Al Viro4edbe132019-06-30 10:39:08 -04001447 LIST_HEAD(list);
Al Viro97216be2013-03-16 15:12:40 -04001448
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001449 hlist_move_list(&unmounted, &head);
Al Viro4edbe132019-06-30 10:39:08 -04001450 list_splice_init(&ex_mountpoints, &list);
Al Viro97216be2013-03-16 15:12:40 -04001451
Al Viro97216be2013-03-16 15:12:40 -04001452 up_write(&namespace_sem);
1453
Al Viro4edbe132019-06-30 10:39:08 -04001454 shrink_dentry_list(&list);
1455
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001456 if (likely(hlist_empty(&head)))
1457 return;
1458
NeilBrown22cb7402018-11-30 10:33:18 +11001459 synchronize_rcu_expedited();
Al Viro48a066e2013-09-29 22:06:07 -04001460
Al Viro56cbb422019-07-04 16:57:51 -04001461 hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
1462 hlist_del(&m->mnt_umount);
1463 mntput(&m->mnt);
1464 }
Ram Pai70fbcdf2005-11-07 17:17:04 -05001465}
1466
Al Viro97216be2013-03-16 15:12:40 -04001467static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001468{
Al Viro97216be2013-03-16 15:12:40 -04001469 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001470}
1471
Eric W. Biedermane819f152014-12-24 07:20:01 -06001472enum umount_tree_flags {
1473 UMOUNT_SYNC = 1,
1474 UMOUNT_PROPAGATE = 2,
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001475 UMOUNT_CONNECTED = 4,
Eric W. Biedermane819f152014-12-24 07:20:01 -06001476};
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001477
1478static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1479{
1480 /* Leaving mounts connected is only valid for lazy umounts */
1481 if (how & UMOUNT_SYNC)
1482 return true;
1483
1484 /* A mount without a parent has nothing to be connected to */
1485 if (!mnt_has_parent(mnt))
1486 return true;
1487
1488 /* Because the reference counting rules change when mounts are
1489 * unmounted and connected, umounted mounts may not be
1490 * connected to mounted mounts.
1491 */
1492 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1493 return true;
1494
1495 /* Has it been requested that the mount remain connected? */
1496 if (how & UMOUNT_CONNECTED)
1497 return false;
1498
1499 /* Is the mount locked such that it needs to remain connected? */
1500 if (IS_MNT_LOCKED(mnt))
1501 return false;
1502
1503 /* By default disconnect the mount */
1504 return true;
1505}
1506
Nick Piggin99b7db72010-08-18 04:37:39 +10001507/*
Al Viro48a066e2013-09-29 22:06:07 -04001508 * mount_lock must be held
Nick Piggin99b7db72010-08-18 04:37:39 +10001509 * namespace_sem must be held for write
1510 */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001511static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001512{
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001513 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001514 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001515
Eric W. Biederman5d884572015-01-03 05:39:35 -06001516 if (how & UMOUNT_PROPAGATE)
1517 propagate_mount_unlock(mnt);
1518
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001519 /* Gather the mounts to umount */
Eric W. Biederman590ce4bc2014-12-22 18:30:08 -06001520 for (p = mnt; p; p = next_mnt(p, mnt)) {
1521 p->mnt.mnt_flags |= MNT_UMOUNT;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001522 list_move(&p->mnt_list, &tmp_list);
Eric W. Biederman590ce4bc2014-12-22 18:30:08 -06001523 }
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001524
Eric W. Biederman411a9382014-12-22 19:12:07 -06001525 /* Hide the mounts from mnt_mounts */
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001526 list_for_each_entry(p, &tmp_list, mnt_list) {
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001527 list_del_init(&p->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -04001528 }
Ram Pai70fbcdf2005-11-07 17:17:04 -05001529
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001530 /* Add propogated mounts to the tmp_list */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001531 if (how & UMOUNT_PROPAGATE)
Al Viro7b8a53f2011-01-15 20:08:44 -05001532 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001533
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001534 while (!list_empty(&tmp_list)) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05001535 struct mnt_namespace *ns;
Eric W. Biedermance07d892014-12-23 21:37:03 -06001536 bool disconnect;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001537 p = list_first_entry(&tmp_list, struct mount, mnt_list);
Al Viro6776db3d2011-11-25 00:22:05 -05001538 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001539 list_del_init(&p->mnt_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05001540 ns = p->mnt_ns;
1541 if (ns) {
1542 ns->mounts--;
1543 __touch_mnt_namespace(ns);
1544 }
Al Viro143c8c92011-11-25 00:46:35 -05001545 p->mnt_ns = NULL;
Eric W. Biedermane819f152014-12-24 07:20:01 -06001546 if (how & UMOUNT_SYNC)
Al Viro48a066e2013-09-29 22:06:07 -04001547 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
Al Viro87b95ce2015-01-10 19:01:08 -05001548
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001549 disconnect = disconnect_mount(p, how);
Al Viro676da582011-11-24 21:47:05 -05001550 if (mnt_has_parent(p)) {
Al Viro81b6b062014-08-30 18:32:05 -04001551 mnt_add_count(p->mnt_parent, -1);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001552 if (!disconnect) {
1553 /* Don't forget about p */
1554 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1555 } else {
1556 umount_mnt(p);
1557 }
Al Viro7c4b93d2008-03-21 23:59:49 -04001558 }
Al Viro0f0afb12011-11-24 20:43:10 -05001559 change_mnt_propagation(p, MS_PRIVATE);
Al Viro19a1c402019-07-24 12:45:46 -04001560 if (disconnect)
1561 hlist_add_head(&p->mnt_umount, &unmounted);
Al Viro38129a12014-03-20 21:10:51 -04001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Al Virob54b9be2013-03-16 14:39:34 -04001565static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001566
David Howells8d0347f2018-11-04 09:28:36 -05001567static int do_umount_root(struct super_block *sb)
1568{
1569 int ret = 0;
1570
1571 down_write(&sb->s_umount);
1572 if (!sb_rdonly(sb)) {
1573 struct fs_context *fc;
1574
1575 fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1576 SB_RDONLY);
1577 if (IS_ERR(fc)) {
1578 ret = PTR_ERR(fc);
1579 } else {
1580 ret = parse_monolithic_mount_data(fc, NULL);
1581 if (!ret)
1582 ret = reconfigure_super(fc);
1583 put_fs_context(fc);
1584 }
1585 }
1586 up_write(&sb->s_umount);
1587 return ret;
1588}
1589
Al Viro1ab59732011-11-24 21:35:16 -05001590static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Al Viro1ab59732011-11-24 21:35:16 -05001592 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int retval;
1594
Al Viro1ab59732011-11-24 21:35:16 -05001595 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (retval)
1597 return retval;
1598
1599 /*
1600 * Allow userspace to request a mountpoint be expired rather than
1601 * unmounting unconditionally. Unmount only happens if:
1602 * (1) the mark is already set (the mark is cleared by mntput())
1603 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1604 */
1605 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001606 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 flags & (MNT_FORCE | MNT_DETACH))
1608 return -EINVAL;
1609
Nick Pigginb3e19d92011-01-07 17:50:11 +11001610 /*
1611 * probably don't strictly need the lock here if we examined
1612 * all race cases, but it's a slowpath.
1613 */
Al Viro719ea2f2013-09-29 11:24:49 -04001614 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -05001615 if (mnt_get_count(mnt) != 2) {
Al Viro719ea2f2013-09-29 11:24:49 -04001616 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001618 }
Al Viro719ea2f2013-09-29 11:24:49 -04001619 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Al Viro863d6842011-11-25 00:57:42 -05001621 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return -EAGAIN;
1623 }
1624
1625 /*
1626 * If we may have to abort operations to get out of this
1627 * mount, and they will themselves hold resources we must
1628 * allow the fs to do things. In the Unix tradition of
1629 * 'Gee thats tricky lets do it in userspace' the umount_begin
1630 * might fail to complete on the first run through as other tasks
1631 * must return, and the like. Thats for the mount program to worry
1632 * about for the moment.
1633 */
1634
Al Viro42faad92008-04-24 07:21:56 -04001635 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001636 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /*
1640 * No sense to grab the lock for this test, but test itself looks
1641 * somewhat bogus. Suggestions for better replacement?
1642 * Ho-hum... In principle, we might treat that as umount + switch
1643 * to rootfs. GC would eventually take care of the old vfsmount.
1644 * Actually it makes sense, especially if rootfs would contain a
1645 * /reboot - static binary that would close all descriptors and
1646 * call reboot(9). Then init(8) could umount root and exec /reboot.
1647 */
Al Viro1ab59732011-11-24 21:35:16 -05001648 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /*
1650 * Special case for "unmounting" root ...
1651 * we just try to remount it readonly.
1652 */
Eric W. Biedermanbc6155d2017-09-18 17:58:08 -05001653 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Andy Lutomirskia1480dc2014-10-08 12:32:47 -07001654 return -EPERM;
David Howells8d0347f2018-11-04 09:28:36 -05001655 return do_umount_root(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657
Al Viro97216be2013-03-16 15:12:40 -04001658 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001659 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001661 /* Recheck MNT_LOCKED with the locks held */
1662 retval = -EINVAL;
1663 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1664 goto out;
1665
1666 event++;
Al Viro48a066e2013-09-29 22:06:07 -04001667 if (flags & MNT_DETACH) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001668 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001669 umount_tree(mnt, UMOUNT_PROPAGATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 retval = 0;
Al Viro48a066e2013-09-29 22:06:07 -04001671 } else {
1672 shrink_submounts(mnt);
1673 retval = -EBUSY;
1674 if (!propagate_mount_busy(mnt, 2)) {
1675 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001676 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Viro48a066e2013-09-29 22:06:07 -04001677 retval = 0;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001680out:
Al Viro719ea2f2013-09-29 11:24:49 -04001681 unlock_mount_hash();
Al Viroe3197d82013-03-16 14:35:16 -04001682 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return retval;
1684}
1685
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001686/*
1687 * __detach_mounts - lazily unmount all mounts on the specified dentry
1688 *
1689 * During unlink, rmdir, and d_drop it is possible to loose the path
1690 * to an existing mountpoint, and wind up leaking the mount.
1691 * detach_mounts allows lazily unmounting those mounts instead of
1692 * leaking them.
1693 *
1694 * The caller may hold dentry->d_inode->i_mutex.
1695 */
1696void __detach_mounts(struct dentry *dentry)
1697{
1698 struct mountpoint *mp;
1699 struct mount *mnt;
1700
1701 namespace_lock();
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001702 lock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001703 mp = lookup_mountpoint(dentry);
Al Viroadc9b5c2019-06-29 12:06:51 -04001704 if (!mp)
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001705 goto out_unlock;
1706
Andrey Ulanove06b9332016-04-15 14:24:41 -07001707 event++;
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001708 while (!hlist_empty(&mp->m_list)) {
1709 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001710 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
Eric W. Biedermanfe78fcc2015-07-17 14:54:27 -05001711 umount_mnt(mnt);
Al Viro56cbb422019-07-04 16:57:51 -04001712 hlist_add_head(&mnt->mnt_umount, &unmounted);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001713 }
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001714 else umount_tree(mnt, UMOUNT_CONNECTED);
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001715 }
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001716 put_mountpoint(mp);
1717out_unlock:
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001718 unlock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001719 namespace_unlock();
1720}
1721
David Howellsdd111b32017-07-04 17:25:09 +01001722/*
Al Viro9b40bc92013-02-22 22:45:42 -05001723 * Is the caller allowed to modify his namespace?
1724 */
1725static inline bool may_mount(void)
1726{
1727 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1728}
1729
Jeff Laytondf2474a2019-08-15 15:21:17 -04001730#ifdef CONFIG_MANDATORY_FILE_LOCKING
Jeff Layton9e8925b2015-11-16 09:49:34 -05001731static inline bool may_mandlock(void)
1732{
Eric W. Biederman95ace752015-11-11 17:22:33 -06001733 return capable(CAP_SYS_ADMIN);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001734}
Jeff Laytondf2474a2019-08-15 15:21:17 -04001735#else
1736static inline bool may_mandlock(void)
1737{
1738 pr_warn("VFS: \"mand\" mount option not supported");
1739 return false;
1740}
1741#endif
Jeff Layton9e8925b2015-11-16 09:49:34 -05001742
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001743static int can_umount(const struct path *path, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001745 struct mount *mnt = real_mount(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Al Viro9b40bc92013-02-22 22:45:42 -05001747 if (!may_mount())
1748 return -EPERM;
Christoph Hellwig41525f52020-07-21 10:54:34 +02001749 if (path->dentry != path->mnt->mnt_root)
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001750 return -EINVAL;
Al Viro143c8c92011-11-25 00:46:35 -05001751 if (!check_mnt(mnt))
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001752 return -EINVAL;
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001753 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001754 return -EINVAL;
Eric W. Biedermanb2f5d4d2014-10-04 14:44:03 -07001755 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001756 return -EPERM;
1757 return 0;
1758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Al Viroa0a6df92021-01-04 15:25:34 -05001760// caller is responsible for flags being sane
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001761int path_umount(struct path *path, int flags)
1762{
1763 struct mount *mnt = real_mount(path->mnt);
1764 int ret;
1765
1766 ret = can_umount(path, flags);
1767 if (!ret)
1768 ret = do_umount(mnt, flags);
1769
Jan Blunck429731b2008-02-14 19:34:31 -08001770 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Christoph Hellwig41525f52020-07-21 10:54:34 +02001771 dput(path->dentry);
Al Viro900148d2011-11-25 00:33:11 -05001772 mntput_no_expire(mnt);
Christoph Hellwig25ccd24f2020-08-06 16:07:10 +02001773 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Christoph Hellwig09267de2020-07-23 08:23:08 +02001776static int ksys_umount(char __user *name, int flags)
Christoph Hellwig41525f52020-07-21 10:54:34 +02001777{
1778 int lookup_flags = LOOKUP_MOUNTPOINT;
1779 struct path path;
1780 int ret;
1781
Al Viroa0a6df92021-01-04 15:25:34 -05001782 // basic validity checks done first
1783 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1784 return -EINVAL;
1785
Christoph Hellwig41525f52020-07-21 10:54:34 +02001786 if (!(flags & UMOUNT_NOFOLLOW))
1787 lookup_flags |= LOOKUP_FOLLOW;
1788 ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1789 if (ret)
1790 return ret;
1791 return path_umount(&path, flags);
1792}
1793
Dominik Brodowski3a18ef52018-03-11 11:34:40 +01001794SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1795{
1796 return ksys_umount(name, flags);
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1800
1801/*
Ram Paib58fed82005-11-07 17:16:09 -05001802 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001804SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Dominik Brodowski3a18ef52018-03-11 11:34:40 +01001806 return ksys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
1809#endif
1810
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001811static bool is_mnt_ns_file(struct dentry *dentry)
Eric W. Biederman8823c072010-03-07 18:49:36 -08001812{
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001813 /* Is this a proxy for a mount namespace? */
Al Viroe149ed22014-11-01 10:57:28 -04001814 return dentry->d_op == &ns_dentry_operations &&
1815 dentry->d_fsdata == &mntns_operations;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001816}
1817
Eric Biggers213921f2020-01-04 12:59:55 -08001818static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
Al Viro58be28252014-11-01 00:00:23 -04001819{
1820 return container_of(ns, struct mnt_namespace, ns);
1821}
1822
Christian Brauner303cc572020-05-05 16:04:31 +02001823struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
1824{
1825 return &mnt->ns;
1826}
1827
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001828static bool mnt_ns_loop(struct dentry *dentry)
1829{
1830 /* Could bind mounting the mount namespace inode cause a
1831 * mount namespace loop?
1832 */
1833 struct mnt_namespace *mnt_ns;
1834 if (!is_mnt_ns_file(dentry))
1835 return false;
1836
Al Virof77c8012014-11-01 03:13:17 -04001837 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
Eric W. Biederman8823c072010-03-07 18:49:36 -08001838 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1839}
1840
Al Viro87129cc2011-11-24 21:24:27 -05001841struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001842 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Al Viro84d17192013-03-15 10:53:28 -04001844 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001846 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1847 return ERR_PTR(-EINVAL);
1848
1849 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
David Howellsbe34d1a2012-06-25 12:55:18 +01001850 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001851
Ram Pai36341f62005-11-07 17:17:22 -05001852 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001853 if (IS_ERR(q))
1854 return q;
1855
Al Viroa73324d2011-11-24 22:25:07 -05001856 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001859 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001860 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001861 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 continue;
1863
Al Viro909b0a82011-11-25 03:06:56 -05001864 for (s = r; s; s = next_mnt(s, r)) {
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001865 if (!(flag & CL_COPY_UNBINDABLE) &&
1866 IS_MNT_UNBINDABLE(s)) {
Eric W. Biedermandf7342b2018-10-25 09:04:18 -05001867 if (s->mnt.mnt_flags & MNT_LOCKED) {
1868 /* Both unbindable and locked. */
1869 q = ERR_PTR(-EPERM);
1870 goto out;
1871 } else {
1872 s = skip_mnt_tree(s);
1873 continue;
1874 }
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001875 }
1876 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1877 is_mnt_ns_file(s->mnt.mnt_root)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001878 s = skip_mnt_tree(s);
1879 continue;
1880 }
Al Viro0714a532011-11-24 22:19:58 -05001881 while (p != s->mnt_parent) {
1882 p = p->mnt_parent;
1883 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Al Viro87129cc2011-11-24 21:24:27 -05001885 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001886 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001887 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001888 if (IS_ERR(q))
1889 goto out;
Al Viro719ea2f2013-09-29 11:24:49 -04001890 lock_mount_hash();
Al Viro1a4eeaf2011-11-25 02:19:55 -05001891 list_add_tail(&q->mnt_list, &res->mnt_list);
Eric W. Biederman1064f872017-01-20 18:28:35 +13001892 attach_mnt(q, parent, p->mnt_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04001893 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 }
1896 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001897out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (res) {
Al Viro719ea2f2013-09-29 11:24:49 -04001899 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06001900 umount_tree(res, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04001901 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001903 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
David Howellsbe34d1a2012-06-25 12:55:18 +01001906/* Caller should check returned pointer for errors */
1907
Al Viroca71cf72016-11-20 19:45:28 -05001908struct vfsmount *collect_mounts(const struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001909{
Al Virocb338d02011-11-24 20:55:08 -05001910 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001911 namespace_lock();
Eric W. Biedermancd4a4012015-01-07 14:28:26 -06001912 if (!check_mnt(real_mount(path->mnt)))
1913 tree = ERR_PTR(-EINVAL);
1914 else
1915 tree = copy_tree(real_mount(path->mnt), path->dentry,
1916 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001917 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001918 if (IS_ERR(tree))
Dan Carpenter52e220d2013-08-14 12:44:39 +03001919 return ERR_CAST(tree);
David Howellsbe34d1a2012-06-25 12:55:18 +01001920 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001921}
1922
Al Viroa07b2002018-11-05 17:40:30 +00001923static void free_mnt_ns(struct mnt_namespace *);
1924static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1925
1926void dissolve_on_fput(struct vfsmount *mnt)
1927{
1928 struct mnt_namespace *ns;
1929 namespace_lock();
1930 lock_mount_hash();
1931 ns = real_mount(mnt)->mnt_ns;
David Howells44dfd842018-11-05 17:40:31 +00001932 if (ns) {
1933 if (is_anon_ns(ns))
1934 umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
1935 else
1936 ns = NULL;
1937 }
Al Viroa07b2002018-11-05 17:40:30 +00001938 unlock_mount_hash();
1939 namespace_unlock();
David Howells44dfd842018-11-05 17:40:31 +00001940 if (ns)
1941 free_mnt_ns(ns);
Al Viroa07b2002018-11-05 17:40:30 +00001942}
1943
Al Viro8aec0802007-06-07 12:20:32 -04001944void drop_collected_mounts(struct vfsmount *mnt)
1945{
Al Viro97216be2013-03-16 15:12:40 -04001946 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001947 lock_mount_hash();
Eric W. Biederman9c8e0a12018-10-25 12:05:11 -05001948 umount_tree(real_mount(mnt), 0);
Al Viro719ea2f2013-09-29 11:24:49 -04001949 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04001950 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001951}
1952
Miklos Szeredic771d682014-10-24 00:14:36 +02001953/**
1954 * clone_private_mount - create a private clone of a path
1955 *
1956 * This creates a new vfsmount, which will be the clone of @path. The new will
1957 * not be attached anywhere in the namespace and will be private (i.e. changes
1958 * to the originating mount won't be propagated into this).
1959 *
1960 * Release with mntput().
1961 */
Al Viroca71cf72016-11-20 19:45:28 -05001962struct vfsmount *clone_private_mount(const struct path *path)
Miklos Szeredic771d682014-10-24 00:14:36 +02001963{
1964 struct mount *old_mnt = real_mount(path->mnt);
1965 struct mount *new_mnt;
1966
1967 if (IS_MNT_UNBINDABLE(old_mnt))
1968 return ERR_PTR(-EINVAL);
1969
Miklos Szeredic771d682014-10-24 00:14:36 +02001970 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
Miklos Szeredic771d682014-10-24 00:14:36 +02001971 if (IS_ERR(new_mnt))
1972 return ERR_CAST(new_mnt);
1973
Miklos Szeredidf820f82020-06-04 10:48:19 +02001974 /* Longterm mount to be removed by kern_unmount*() */
1975 new_mnt->mnt_ns = MNT_NS_INTERNAL;
1976
Miklos Szeredic771d682014-10-24 00:14:36 +02001977 return &new_mnt->mnt;
1978}
1979EXPORT_SYMBOL_GPL(clone_private_mount);
1980
Al Viro1f707132010-01-30 22:51:25 -05001981int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1982 struct vfsmount *root)
1983{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001984 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001985 int res = f(root, arg);
1986 if (res)
1987 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001988 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1989 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001990 if (res)
1991 return res;
1992 }
1993 return 0;
1994}
1995
Al Viro3bd045c2019-01-30 13:15:45 -05001996static void lock_mnt_tree(struct mount *mnt)
1997{
1998 struct mount *p;
1999
2000 for (p = mnt; p; p = next_mnt(p, mnt)) {
2001 int flags = p->mnt.mnt_flags;
2002 /* Don't allow unprivileged users to change mount flags */
2003 flags |= MNT_LOCK_ATIME;
2004
2005 if (flags & MNT_READONLY)
2006 flags |= MNT_LOCK_READONLY;
2007
2008 if (flags & MNT_NODEV)
2009 flags |= MNT_LOCK_NODEV;
2010
2011 if (flags & MNT_NOSUID)
2012 flags |= MNT_LOCK_NOSUID;
2013
2014 if (flags & MNT_NOEXEC)
2015 flags |= MNT_LOCK_NOEXEC;
2016 /* Don't allow unprivileged users to reveal what is under a mount */
2017 if (list_empty(&p->mnt_expire))
2018 flags |= MNT_LOCKED;
2019 p->mnt.mnt_flags = flags;
2020 }
2021}
2022
Al Viro4b8b21f2011-11-24 19:54:23 -05002023static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002024{
Al Viro315fc832011-11-24 18:57:30 -05002025 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002026
Al Viro909b0a82011-11-25 03:06:56 -05002027 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05002028 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05002029 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002030 }
2031}
2032
Al Viro4b8b21f2011-11-24 19:54:23 -05002033static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002034{
Al Viro315fc832011-11-24 18:57:30 -05002035 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002036
Al Viro909b0a82011-11-25 03:06:56 -05002037 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05002038 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05002039 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002040 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05002041 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002042 return err;
2043 }
2044 }
2045 }
2046
2047 return 0;
2048}
2049
Eric W. Biedermand2921682016-09-28 00:27:17 -05002050int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
2051{
2052 unsigned int max = READ_ONCE(sysctl_mount_max);
2053 unsigned int mounts = 0, old, pending, sum;
2054 struct mount *p;
2055
2056 for (p = mnt; p; p = next_mnt(p, mnt))
2057 mounts++;
2058
2059 old = ns->mounts;
2060 pending = ns->pending_mounts;
2061 sum = old + pending;
2062 if ((old > sum) ||
2063 (pending > sum) ||
2064 (max < sum) ||
2065 (mounts > (max - sum)))
2066 return -ENOSPC;
2067
2068 ns->pending_mounts = pending + mounts;
2069 return 0;
2070}
2071
Ram Paib90fa9a2005-11-07 17:19:50 -05002072/*
2073 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05002074 * @nd : place the mount tree @source_mnt is attached
2075 * @parent_nd : if non-null, detach the source_mnt from its parent and
2076 * store the parent mount and mountpoint dentry.
2077 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05002078 *
2079 * NOTE: in the table below explains the semantics when a source mount
2080 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05002081 * ---------------------------------------------------------------------------
2082 * | BIND MOUNT OPERATION |
2083 * |**************************************************************************
2084 * | source-->| shared | private | slave | unbindable |
2085 * | dest | | | | |
2086 * | | | | | | |
2087 * | v | | | | |
2088 * |**************************************************************************
2089 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
2090 * | | | | | |
2091 * |non-shared| shared (+) | private | slave (*) | invalid |
2092 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05002093 * A bind operation clones the source mount and mounts the clone on the
2094 * destination mount.
2095 *
2096 * (++) the cloned mount is propagated to all the mounts in the propagation
2097 * tree of the destination mount and the cloned mount is added to
2098 * the peer group of the source mount.
2099 * (+) the cloned mount is created under the destination mount and is marked
2100 * as shared. The cloned mount is added to the peer group of the source
2101 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05002102 * (+++) the mount is propagated to all the mounts in the propagation tree
2103 * of the destination mount and the cloned mount is made slave
2104 * of the same master as that of the source mount. The cloned mount
2105 * is marked as 'shared and slave'.
2106 * (*) the cloned mount is made a slave of the same master as that of the
2107 * source mount.
2108 *
Ram Pai9676f0c2005-11-07 17:21:20 -05002109 * ---------------------------------------------------------------------------
2110 * | MOVE MOUNT OPERATION |
2111 * |**************************************************************************
2112 * | source-->| shared | private | slave | unbindable |
2113 * | dest | | | | |
2114 * | | | | | | |
2115 * | v | | | | |
2116 * |**************************************************************************
2117 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2118 * | | | | | |
2119 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2120 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05002121 *
2122 * (+) the mount is moved to the destination. And is then propagated to
2123 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05002124 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05002125 * (+++) the mount is moved to the destination and is then propagated to
2126 * all the mounts belonging to the destination mount's propagation tree.
2127 * the mount is marked as 'shared and slave'.
2128 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05002129 *
2130 * if the source mount is a tree, the operations explained above is
2131 * applied to each mount in the tree.
2132 * Must be called without spinlocks held, since this function can sleep
2133 * in allocations.
2134 */
Al Viro0fb54e52011-11-24 19:59:16 -05002135static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04002136 struct mount *dest_mnt,
2137 struct mountpoint *dest_mp,
Al Viro2763d112019-06-30 19:18:53 -04002138 bool moving)
Ram Paib90fa9a2005-11-07 17:19:50 -05002139{
Al Viro3bd045c2019-01-30 13:15:45 -05002140 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
Al Viro38129a12014-03-20 21:10:51 -04002141 HLIST_HEAD(tree_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002142 struct mnt_namespace *ns = dest_mnt->mnt_ns;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002143 struct mountpoint *smp;
Al Viro315fc832011-11-24 18:57:30 -05002144 struct mount *child, *p;
Al Viro38129a12014-03-20 21:10:51 -04002145 struct hlist_node *n;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002146 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002147
Eric W. Biederman1064f872017-01-20 18:28:35 +13002148 /* Preallocate a mountpoint in case the new mounts need
2149 * to be tucked under other mounts.
2150 */
2151 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2152 if (IS_ERR(smp))
2153 return PTR_ERR(smp);
2154
Eric W. Biedermand2921682016-09-28 00:27:17 -05002155 /* Is there space to add these mounts to the mount namespace? */
Al Viro2763d112019-06-30 19:18:53 -04002156 if (!moving) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05002157 err = count_mounts(ns, source_mnt);
2158 if (err)
2159 goto out;
2160 }
2161
Al Virofc7be132011-11-25 01:05:37 -05002162 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05002163 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002164 if (err)
2165 goto out;
Al Viro0b1b9012014-03-21 10:14:08 -04002166 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Al Virof2ebb3a2014-02-27 09:35:45 -05002167 lock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002168 if (err)
2169 goto out_cleanup_ids;
Al Viro909b0a82011-11-25 03:06:56 -05002170 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05002171 set_mnt_shared(p);
Al Viro0b1b9012014-03-21 10:14:08 -04002172 } else {
2173 lock_mount_hash();
Ram Paib90fa9a2005-11-07 17:19:50 -05002174 }
Al Viro2763d112019-06-30 19:18:53 -04002175 if (moving) {
2176 unhash_mnt(source_mnt);
Al Viro84d17192013-03-15 10:53:28 -04002177 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05002178 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05002179 } else {
David Howells44dfd842018-11-05 17:40:31 +00002180 if (source_mnt->mnt_ns) {
2181 /* move from anon - the caller will destroy */
2182 list_del_init(&source_mnt->mnt_ns->list);
2183 }
Al Viro84d17192013-03-15 10:53:28 -04002184 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002185 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05002186 }
Ram Paib90fa9a2005-11-07 17:19:50 -05002187
Al Viro38129a12014-03-20 21:10:51 -04002188 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
Al Viro1d6a32a2014-03-20 20:34:43 -04002189 struct mount *q;
Al Viro38129a12014-03-20 21:10:51 -04002190 hlist_del_init(&child->mnt_hash);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002191 q = __lookup_mnt(&child->mnt_parent->mnt,
2192 child->mnt_mountpoint);
2193 if (q)
2194 mnt_change_mountpoint(child, smp, q);
Al Viro3bd045c2019-01-30 13:15:45 -05002195 /* Notice when we are propagating across user namespaces */
2196 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2197 lock_mnt_tree(child);
Christian Braunerd728cf72019-06-17 23:22:14 +02002198 child->mnt.mnt_flags &= ~MNT_LOCKED;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002199 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05002200 }
Eric W. Biederman1064f872017-01-20 18:28:35 +13002201 put_mountpoint(smp);
Al Viro719ea2f2013-09-29 11:24:49 -04002202 unlock_mount_hash();
Nick Piggin99b7db72010-08-18 04:37:39 +10002203
Ram Paib90fa9a2005-11-07 17:19:50 -05002204 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002205
2206 out_cleanup_ids:
Al Virof2ebb3a2014-02-27 09:35:45 -05002207 while (!hlist_empty(&tree_list)) {
2208 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002209 child->mnt_parent->mnt_ns->pending_mounts = 0;
Eric W. Biedermane819f152014-12-24 07:20:01 -06002210 umount_tree(child, UMOUNT_SYNC);
Al Virof2ebb3a2014-02-27 09:35:45 -05002211 }
2212 unlock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002213 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002214 out:
Eric W. Biedermand2921682016-09-28 00:27:17 -05002215 ns->pending_mounts = 0;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002216
2217 read_seqlock_excl(&mount_lock);
2218 put_mountpoint(smp);
2219 read_sequnlock_excl(&mount_lock);
2220
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002221 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002222}
2223
Al Viro84d17192013-03-15 10:53:28 -04002224static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04002225{
2226 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04002227 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04002228retry:
Al Viro59551022016-01-22 15:40:57 -05002229 inode_lock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002230 if (unlikely(cant_mount(dentry))) {
Al Viro59551022016-01-22 15:40:57 -05002231 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002232 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04002233 }
Al Viro97216be2013-03-16 15:12:40 -04002234 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04002235 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04002236 if (likely(!mnt)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002237 struct mountpoint *mp = get_mountpoint(dentry);
Al Viro84d17192013-03-15 10:53:28 -04002238 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04002239 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002240 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002241 return mp;
2242 }
2243 return mp;
2244 }
Al Viro97216be2013-03-16 15:12:40 -04002245 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002246 inode_unlock(path->dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002247 path_put(path);
2248 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04002249 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04002250 goto retry;
2251}
2252
Al Viro84d17192013-03-15 10:53:28 -04002253static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04002254{
Al Viro84d17192013-03-15 10:53:28 -04002255 struct dentry *dentry = where->m_dentry;
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002256
2257 read_seqlock_excl(&mount_lock);
Al Viro84d17192013-03-15 10:53:28 -04002258 put_mountpoint(where);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002259 read_sequnlock_excl(&mount_lock);
2260
Al Viro328e6d92013-03-16 14:49:45 -04002261 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002262 inode_unlock(dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002263}
2264
Al Viro84d17192013-03-15 10:53:28 -04002265static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
David Howellse462ec52017-07-17 08:45:35 +01002267 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return -EINVAL;
2269
David Howellse36cb0b2015-01-29 12:02:35 +00002270 if (d_is_dir(mp->m_dentry) !=
2271 d_is_dir(mnt->mnt.mnt_root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -ENOTDIR;
2273
Al Viro2763d112019-06-30 19:18:53 -04002274 return attach_recursive_mnt(mnt, p, mp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002278 * Sanity check the flags to change_mnt_propagation.
2279 */
2280
David Howellse462ec52017-07-17 08:45:35 +01002281static int flags_to_propagation_type(int ms_flags)
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002282{
David Howellse462ec52017-07-17 08:45:35 +01002283 int type = ms_flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002284
2285 /* Fail if any non-propagation flags are set */
2286 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2287 return 0;
2288 /* Only one propagation flag should be set */
2289 if (!is_power_of_2(type))
2290 return 0;
2291 return type;
2292}
2293
2294/*
Ram Pai07b20882005-11-07 17:19:07 -05002295 * recursively change the type of the mountpoint.
2296 */
David Howellse462ec52017-07-17 08:45:35 +01002297static int do_change_type(struct path *path, int ms_flags)
Ram Pai07b20882005-11-07 17:19:07 -05002298{
Al Viro315fc832011-11-24 18:57:30 -05002299 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05002300 struct mount *mnt = real_mount(path->mnt);
David Howellse462ec52017-07-17 08:45:35 +01002301 int recurse = ms_flags & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002302 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002303 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05002304
Al Viro2d92ab32008-08-02 00:51:11 -04002305 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05002306 return -EINVAL;
2307
David Howellse462ec52017-07-17 08:45:35 +01002308 type = flags_to_propagation_type(ms_flags);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002309 if (!type)
2310 return -EINVAL;
2311
Al Viro97216be2013-03-16 15:12:40 -04002312 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002313 if (type == MS_SHARED) {
2314 err = invent_group_ids(mnt, recurse);
2315 if (err)
2316 goto out_unlock;
2317 }
2318
Al Viro719ea2f2013-09-29 11:24:49 -04002319 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05002320 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05002321 change_mnt_propagation(m, type);
Al Viro719ea2f2013-09-29 11:24:49 -04002322 unlock_mount_hash();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002323
2324 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04002325 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002326 return err;
Ram Pai07b20882005-11-07 17:19:07 -05002327}
2328
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002329static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2330{
2331 struct mount *child;
2332 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2333 if (!is_subdir(child->mnt_mountpoint, dentry))
2334 continue;
2335
2336 if (child->mnt.mnt_flags & MNT_LOCKED)
2337 return true;
2338 }
2339 return false;
2340}
2341
Al Viroa07b2002018-11-05 17:40:30 +00002342static struct mount *__do_loopback(struct path *old_path, int recurse)
2343{
2344 struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2345
2346 if (IS_MNT_UNBINDABLE(old))
2347 return mnt;
2348
2349 if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2350 return mnt;
2351
2352 if (!recurse && has_locked_children(old, old_path->dentry))
2353 return mnt;
2354
2355 if (recurse)
2356 mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2357 else
2358 mnt = clone_mnt(old, old_path->dentry, 0);
2359
2360 if (!IS_ERR(mnt))
2361 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2362
2363 return mnt;
2364}
2365
Ram Pai07b20882005-11-07 17:19:07 -05002366/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * do loopback mount.
2368 */
Al Viro808d4e32012-10-11 11:42:01 -04002369static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08002370 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Al Viro2d92ab32008-08-02 00:51:11 -04002372 struct path old_path;
Al Viroa07b2002018-11-05 17:40:30 +00002373 struct mount *mnt = NULL, *parent;
Al Viro84d17192013-03-15 10:53:28 -04002374 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002375 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (!old_name || !*old_name)
2377 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04002378 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (err)
2380 return err;
2381
Eric W. Biederman8823c072010-03-07 18:49:36 -08002382 err = -EINVAL;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002383 if (mnt_ns_loop(old_path.dentry))
David Howellsdd111b32017-07-04 17:25:09 +01002384 goto out;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002385
Al Viro84d17192013-03-15 10:53:28 -04002386 mp = lock_mount(path);
Al Viroa07b2002018-11-05 17:40:30 +00002387 if (IS_ERR(mp)) {
2388 err = PTR_ERR(mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08002389 goto out;
Al Viroa07b2002018-11-05 17:40:30 +00002390 }
Ram Pai9676f0c2005-11-07 17:21:20 -05002391
Al Viro84d17192013-03-15 10:53:28 -04002392 parent = real_mount(path->mnt);
Al Viroe149ed22014-11-01 10:57:28 -04002393 if (!check_mnt(parent))
2394 goto out2;
2395
Al Viroa07b2002018-11-05 17:40:30 +00002396 mnt = __do_loopback(&old_path, recurse);
David Howellsbe34d1a2012-06-25 12:55:18 +01002397 if (IS_ERR(mnt)) {
2398 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04002399 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01002400 }
Al Viroccd48bc2005-11-07 17:15:04 -05002401
Al Viro84d17192013-03-15 10:53:28 -04002402 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002403 if (err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002404 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06002405 umount_tree(mnt, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04002406 unlock_mount_hash();
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05002407 }
Al Virob12cea92011-03-18 08:55:38 -04002408out2:
Al Viro84d17192013-03-15 10:53:28 -04002409 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002410out:
Al Viro2d92ab32008-08-02 00:51:11 -04002411 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return err;
2413}
2414
Al Viroa07b2002018-11-05 17:40:30 +00002415static struct file *open_detached_copy(struct path *path, bool recursive)
2416{
2417 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2418 struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2419 struct mount *mnt, *p;
2420 struct file *file;
2421
2422 if (IS_ERR(ns))
2423 return ERR_CAST(ns);
2424
2425 namespace_lock();
2426 mnt = __do_loopback(path, recursive);
2427 if (IS_ERR(mnt)) {
2428 namespace_unlock();
2429 free_mnt_ns(ns);
2430 return ERR_CAST(mnt);
2431 }
2432
2433 lock_mount_hash();
2434 for (p = mnt; p; p = next_mnt(p, mnt)) {
2435 p->mnt_ns = ns;
2436 ns->mounts++;
2437 }
2438 ns->root = mnt;
2439 list_add_tail(&ns->list, &mnt->mnt_list);
2440 mntget(&mnt->mnt);
2441 unlock_mount_hash();
2442 namespace_unlock();
2443
2444 mntput(path->mnt);
2445 path->mnt = &mnt->mnt;
2446 file = dentry_open(path, O_PATH, current_cred());
2447 if (IS_ERR(file))
2448 dissolve_on_fput(path->mnt);
2449 else
2450 file->f_mode |= FMODE_NEED_UNMOUNT;
2451 return file;
2452}
2453
Ben Dooks2658ce02019-10-15 11:35:02 +01002454SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
Al Viroa07b2002018-11-05 17:40:30 +00002455{
2456 struct file *file;
2457 struct path path;
2458 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2459 bool detached = flags & OPEN_TREE_CLONE;
2460 int error;
2461 int fd;
2462
2463 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2464
2465 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2466 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2467 OPEN_TREE_CLOEXEC))
2468 return -EINVAL;
2469
2470 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2471 return -EINVAL;
2472
2473 if (flags & AT_NO_AUTOMOUNT)
2474 lookup_flags &= ~LOOKUP_AUTOMOUNT;
2475 if (flags & AT_SYMLINK_NOFOLLOW)
2476 lookup_flags &= ~LOOKUP_FOLLOW;
2477 if (flags & AT_EMPTY_PATH)
2478 lookup_flags |= LOOKUP_EMPTY;
2479
2480 if (detached && !may_mount())
2481 return -EPERM;
2482
2483 fd = get_unused_fd_flags(flags & O_CLOEXEC);
2484 if (fd < 0)
2485 return fd;
2486
2487 error = user_path_at(dfd, filename, lookup_flags, &path);
2488 if (unlikely(error)) {
2489 file = ERR_PTR(error);
2490 } else {
2491 if (detached)
2492 file = open_detached_copy(&path, flags & AT_RECURSIVE);
2493 else
2494 file = dentry_open(&path, O_PATH, current_cred());
2495 path_put(&path);
2496 }
2497 if (IS_ERR(file)) {
2498 put_unused_fd(fd);
2499 return PTR_ERR(file);
2500 }
2501 fd_install(fd, file);
2502 return fd;
2503}
2504
David Howells43f5e652018-11-01 23:07:25 +00002505/*
2506 * Don't allow locked mount flags to be cleared.
2507 *
2508 * No locks need to be held here while testing the various MNT_LOCK
2509 * flags because those flags can never be cleared once they are set.
2510 */
2511static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002512{
David Howells43f5e652018-11-01 23:07:25 +00002513 unsigned int fl = mnt->mnt.mnt_flags;
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002514
David Howells43f5e652018-11-01 23:07:25 +00002515 if ((fl & MNT_LOCK_READONLY) &&
2516 !(mnt_flags & MNT_READONLY))
2517 return false;
2518
2519 if ((fl & MNT_LOCK_NODEV) &&
2520 !(mnt_flags & MNT_NODEV))
2521 return false;
2522
2523 if ((fl & MNT_LOCK_NOSUID) &&
2524 !(mnt_flags & MNT_NOSUID))
2525 return false;
2526
2527 if ((fl & MNT_LOCK_NOEXEC) &&
2528 !(mnt_flags & MNT_NOEXEC))
2529 return false;
2530
2531 if ((fl & MNT_LOCK_ATIME) &&
2532 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2533 return false;
2534
2535 return true;
2536}
2537
2538static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2539{
2540 bool readonly_request = (mnt_flags & MNT_READONLY);
2541
2542 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002543 return 0;
2544
2545 if (readonly_request)
David Howells43f5e652018-11-01 23:07:25 +00002546 return mnt_make_readonly(mnt);
2547
Christian Brauner68847c92021-01-21 14:19:48 +01002548 mnt->mnt.mnt_flags &= ~MNT_READONLY;
2549 return 0;
David Howells43f5e652018-11-01 23:07:25 +00002550}
2551
David Howells43f5e652018-11-01 23:07:25 +00002552static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2553{
David Howells43f5e652018-11-01 23:07:25 +00002554 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2555 mnt->mnt.mnt_flags = mnt_flags;
2556 touch_mnt_namespace(mnt->mnt_ns);
David Howells43f5e652018-11-01 23:07:25 +00002557}
2558
Deepa Dinamanif8b92ba2019-04-15 14:17:12 -07002559static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
2560{
2561 struct super_block *sb = mnt->mnt_sb;
2562
2563 if (!__mnt_is_readonly(mnt) &&
2564 (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
2565 char *buf = (char *)__get_free_page(GFP_KERNEL);
2566 char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
2567 struct tm tm;
2568
2569 time64_to_tm(sb->s_time_max, 0, &tm);
2570
Eric Biggers0ecee662019-10-16 19:48:14 -07002571 pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
2572 sb->s_type->name,
2573 is_mounted(mnt) ? "remounted" : "mounted",
2574 mntpath,
Deepa Dinamanif8b92ba2019-04-15 14:17:12 -07002575 tm.tm_year+1900, (unsigned long long)sb->s_time_max);
2576
2577 free_page((unsigned long)buf);
2578 }
2579}
2580
David Howells43f5e652018-11-01 23:07:25 +00002581/*
2582 * Handle reconfiguration of the mountpoint only without alteration of the
2583 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
2584 * to mount(2).
2585 */
2586static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2587{
2588 struct super_block *sb = path->mnt->mnt_sb;
2589 struct mount *mnt = real_mount(path->mnt);
2590 int ret;
2591
2592 if (!check_mnt(mnt))
2593 return -EINVAL;
2594
2595 if (path->dentry != mnt->mnt.mnt_root)
2596 return -EINVAL;
2597
2598 if (!can_change_locked_flags(mnt, mnt_flags))
2599 return -EPERM;
2600
Christian Braunere58ace12021-01-21 14:19:50 +01002601 /*
2602 * We're only checking whether the superblock is read-only not
2603 * changing it, so only take down_read(&sb->s_umount).
2604 */
2605 down_read(&sb->s_umount);
Christian Brauner68847c92021-01-21 14:19:48 +01002606 lock_mount_hash();
David Howells43f5e652018-11-01 23:07:25 +00002607 ret = change_mount_ro_state(mnt, mnt_flags);
2608 if (ret == 0)
2609 set_mount_attributes(mnt, mnt_flags);
Christian Brauner68847c92021-01-21 14:19:48 +01002610 unlock_mount_hash();
Christian Braunere58ace12021-01-21 14:19:50 +01002611 up_read(&sb->s_umount);
Deepa Dinamanif8b92ba2019-04-15 14:17:12 -07002612
2613 mnt_warn_timestamp_expiry(path, &mnt->mnt);
2614
David Howells43f5e652018-11-01 23:07:25 +00002615 return ret;
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
2619 * change filesystem flags. dir should be a physical root of filesystem.
2620 * If you've mounted a non-root directory somewhere and want to do remount
2621 * on it - tough luck.
2622 */
David Howellse462ec52017-07-17 08:45:35 +01002623static int do_remount(struct path *path, int ms_flags, int sb_flags,
2624 int mnt_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04002627 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05002628 struct mount *mnt = real_mount(path->mnt);
David Howells8d0347f2018-11-04 09:28:36 -05002629 struct fs_context *fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Al Viro143c8c92011-11-25 00:46:35 -05002631 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return -EINVAL;
2633
Al Viro2d92ab32008-08-02 00:51:11 -04002634 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return -EINVAL;
2636
David Howells43f5e652018-11-01 23:07:25 +00002637 if (!can_change_locked_flags(mnt, mnt_flags))
Eric W. Biederman07b64552014-07-28 17:10:56 -07002638 return -EPERM;
Eric W. Biederman9566d672014-07-28 17:26:07 -07002639
David Howells8d0347f2018-11-04 09:28:36 -05002640 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
2641 if (IS_ERR(fc))
2642 return PTR_ERR(fc);
Eric Parisff36fe22011-03-03 16:09:14 -05002643
Miklos Szeredib3309662020-07-14 14:45:41 +02002644 fc->oldapi = true;
David Howells8d0347f2018-11-04 09:28:36 -05002645 err = parse_monolithic_mount_data(fc, data);
2646 if (!err) {
2647 down_write(&sb->s_umount);
2648 err = -EPERM;
2649 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2650 err = reconfigure_super(fc);
Christian Brauner68847c92021-01-21 14:19:48 +01002651 if (!err) {
2652 lock_mount_hash();
David Howells8d0347f2018-11-04 09:28:36 -05002653 set_mount_attributes(mnt, mnt_flags);
Christian Brauner68847c92021-01-21 14:19:48 +01002654 unlock_mount_hash();
2655 }
David Howells8d0347f2018-11-04 09:28:36 -05002656 }
2657 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07002658 }
Deepa Dinamanif8b92ba2019-04-15 14:17:12 -07002659
2660 mnt_warn_timestamp_expiry(path, &mnt->mnt);
2661
David Howells8d0347f2018-11-04 09:28:36 -05002662 put_fs_context(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return err;
2664}
2665
Al Virocbbe3622011-11-24 20:01:19 -05002666static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05002667{
Al Viro315fc832011-11-24 18:57:30 -05002668 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05002669 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05002670 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05002671 return 1;
2672 }
2673 return 0;
2674}
2675
David Howells44dfd842018-11-05 17:40:31 +00002676/*
2677 * Check that there aren't references to earlier/same mount namespaces in the
2678 * specified subtree. Such references can act as pins for mount namespaces
2679 * that aren't checked by the mount-cycle checking code, thereby allowing
2680 * cycles to be made.
2681 */
2682static bool check_for_nsfs_mounts(struct mount *subtree)
2683{
2684 struct mount *p;
2685 bool ret = false;
2686
2687 lock_mount_hash();
2688 for (p = subtree; p; p = next_mnt(p, subtree))
2689 if (mnt_ns_loop(p->mnt.mnt_root))
2690 goto out;
2691
2692 ret = true;
2693out:
2694 unlock_mount_hash();
2695 return ret;
2696}
2697
David Howells2db154b2018-11-05 17:40:30 +00002698static int do_move_mount(struct path *old_path, struct path *new_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
David Howells44dfd842018-11-05 17:40:31 +00002700 struct mnt_namespace *ns;
Al Viro676da582011-11-24 21:47:05 -05002701 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05002702 struct mount *old;
Al Viro2763d112019-06-30 19:18:53 -04002703 struct mount *parent;
2704 struct mountpoint *mp, *old_mp;
Al Viro57eccb82013-02-22 22:49:10 -05002705 int err;
David Howells44dfd842018-11-05 17:40:31 +00002706 bool attached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
David Howells2db154b2018-11-05 17:40:30 +00002708 mp = lock_mount(new_path);
Al Viro84d17192013-03-15 10:53:28 -04002709 if (IS_ERR(mp))
David Howells2db154b2018-11-05 17:40:30 +00002710 return PTR_ERR(mp);
David Howellscc53ce52011-01-14 18:45:26 +00002711
David Howells2db154b2018-11-05 17:40:30 +00002712 old = real_mount(old_path->mnt);
2713 p = real_mount(new_path->mnt);
Al Viro2763d112019-06-30 19:18:53 -04002714 parent = old->mnt_parent;
David Howells44dfd842018-11-05 17:40:31 +00002715 attached = mnt_has_parent(old);
Al Viro2763d112019-06-30 19:18:53 -04002716 old_mp = old->mnt_mp;
David Howells44dfd842018-11-05 17:40:31 +00002717 ns = old->mnt_ns;
Al Viro143c8c92011-11-25 00:46:35 -05002718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 err = -EINVAL;
David Howells44dfd842018-11-05 17:40:31 +00002720 /* The mountpoint must be in our namespace. */
2721 if (!check_mnt(p))
David Howells2db154b2018-11-05 17:40:30 +00002722 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Eric Biggers570d7a92019-06-29 13:27:44 -07002724 /* The thing moved must be mounted... */
2725 if (!is_mounted(&old->mnt))
David Howells44dfd842018-11-05 17:40:31 +00002726 goto out;
2727
Eric Biggers570d7a92019-06-29 13:27:44 -07002728 /* ... and either ours or the root of anon namespace */
2729 if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
David Howells2db154b2018-11-05 17:40:30 +00002730 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
David Howells2db154b2018-11-05 17:40:30 +00002732 if (old->mnt.mnt_flags & MNT_LOCKED)
2733 goto out;
2734
2735 if (old_path->dentry != old_path->mnt->mnt_root)
2736 goto out;
2737
2738 if (d_is_dir(new_path->dentry) !=
2739 d_is_dir(old_path->dentry))
2740 goto out;
Ram Pai21444402005-11-07 17:20:03 -05002741 /*
2742 * Don't move a mount residing in a shared parent.
2743 */
Al Viro2763d112019-06-30 19:18:53 -04002744 if (attached && IS_MNT_SHARED(parent))
David Howells2db154b2018-11-05 17:40:30 +00002745 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05002746 /*
2747 * Don't move a mount tree containing unbindable mounts to a destination
2748 * mount which is shared.
2749 */
Al Virofc7be132011-11-25 01:05:37 -05002750 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
David Howells2db154b2018-11-05 17:40:30 +00002751 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 err = -ELOOP;
David Howells44dfd842018-11-05 17:40:31 +00002753 if (!check_for_nsfs_mounts(old))
2754 goto out;
Al Virofc7be132011-11-25 01:05:37 -05002755 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05002756 if (p == old)
David Howells2db154b2018-11-05 17:40:30 +00002757 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
David Howells2db154b2018-11-05 17:40:30 +00002759 err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
Al Viro2763d112019-06-30 19:18:53 -04002760 attached);
Jan Blunck4ac91372008-02-14 19:34:32 -08002761 if (err)
David Howells2db154b2018-11-05 17:40:30 +00002762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 /* if the mount is moved, it should no longer be expire
2765 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05002766 list_del_init(&old->mnt_expire);
Al Viro2763d112019-06-30 19:18:53 -04002767 if (attached)
2768 put_mountpoint(old_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769out:
David Howells2db154b2018-11-05 17:40:30 +00002770 unlock_mount(mp);
David Howells44dfd842018-11-05 17:40:31 +00002771 if (!err) {
Al Viro2763d112019-06-30 19:18:53 -04002772 if (attached)
2773 mntput_no_expire(parent);
2774 else
David Howells44dfd842018-11-05 17:40:31 +00002775 free_mnt_ns(ns);
2776 }
David Howells2db154b2018-11-05 17:40:30 +00002777 return err;
2778}
2779
2780static int do_move_mount_old(struct path *path, const char *old_name)
2781{
2782 struct path old_path;
2783 int err;
2784
2785 if (!old_name || !*old_name)
2786 return -EINVAL;
2787
2788 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2789 if (err)
2790 return err;
2791
2792 err = do_move_mount(&old_path, path);
Al Viro2d92ab32008-08-02 00:51:11 -04002793 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return err;
2795}
2796
Al Viro9d412a42011-03-17 22:08:28 -04002797/*
2798 * add a mount into a namespace's mount tree
2799 */
Al Viro8f115382020-01-11 10:14:09 -05002800static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
2801 struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04002802{
Al Viro8f115382020-01-11 10:14:09 -05002803 struct mount *parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04002804
Al Virof2ebb3a2014-02-27 09:35:45 -05002805 mnt_flags &= ~MNT_INTERNAL_FLAGS;
Al Viro9d412a42011-03-17 22:08:28 -04002806
Al Viro84d17192013-03-15 10:53:28 -04002807 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04002808 /* that's acceptable only for automounts done in private ns */
2809 if (!(mnt_flags & MNT_SHRINKABLE))
Al Viro8f115382020-01-11 10:14:09 -05002810 return -EINVAL;
Al Viro156cacb2012-09-21 08:19:02 -04002811 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04002812 if (!parent->mnt_ns)
Al Viro8f115382020-01-11 10:14:09 -05002813 return -EINVAL;
Al Viro156cacb2012-09-21 08:19:02 -04002814 }
Al Viro9d412a42011-03-17 22:08:28 -04002815
2816 /* Refuse the same filesystem on the same mount point */
Al Viro95bc5f22011-11-25 00:30:56 -05002817 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04002818 path->mnt->mnt_root == path->dentry)
Al Viro8f115382020-01-11 10:14:09 -05002819 return -EBUSY;
Al Viro9d412a42011-03-17 22:08:28 -04002820
David Howellse36cb0b2015-01-29 12:02:35 +00002821 if (d_is_symlink(newmnt->mnt.mnt_root))
Al Viro8f115382020-01-11 10:14:09 -05002822 return -EINVAL;
Al Viro9d412a42011-03-17 22:08:28 -04002823
Al Viro95bc5f22011-11-25 00:30:56 -05002824 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro8f115382020-01-11 10:14:09 -05002825 return graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002826}
Al Virob1e75df2011-01-17 01:47:59 -05002827
David Howells132e4602018-11-04 07:43:08 -05002828static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2829
2830/*
2831 * Create a new mount using a superblock configuration and request it
2832 * be added to the namespace tree.
2833 */
2834static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2835 unsigned int mnt_flags)
2836{
2837 struct vfsmount *mnt;
Al Viro8f115382020-01-11 10:14:09 -05002838 struct mountpoint *mp;
David Howells132e4602018-11-04 07:43:08 -05002839 struct super_block *sb = fc->root->d_sb;
2840 int error;
2841
Al Viroc9ce29e2018-12-20 15:04:50 -05002842 error = security_sb_kern_mount(sb);
2843 if (!error && mount_too_revealing(sb, &mnt_flags))
2844 error = -EPERM;
2845
2846 if (unlikely(error)) {
2847 fc_drop_locked(fc);
2848 return error;
David Howells132e4602018-11-04 07:43:08 -05002849 }
2850
2851 up_write(&sb->s_umount);
2852
2853 mnt = vfs_create_mount(fc);
2854 if (IS_ERR(mnt))
2855 return PTR_ERR(mnt);
2856
Deepa Dinamanif8b92ba2019-04-15 14:17:12 -07002857 mnt_warn_timestamp_expiry(mountpoint, mnt);
2858
Al Viro8f115382020-01-11 10:14:09 -05002859 mp = lock_mount(mountpoint);
2860 if (IS_ERR(mp)) {
2861 mntput(mnt);
2862 return PTR_ERR(mp);
2863 }
2864 error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
2865 unlock_mount(mp);
Eric Biggers0ecee662019-10-16 19:48:14 -07002866 if (error < 0)
2867 mntput(mnt);
David Howells132e4602018-11-04 07:43:08 -05002868 return error;
2869}
Eric W. Biederman1b852bc2015-05-08 23:22:29 -05002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871/*
2872 * create a new mount for userspace and request it to be added into the
2873 * namespace's tree
2874 */
David Howellse462ec52017-07-17 08:45:35 +01002875static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
Al Viro808d4e32012-10-11 11:42:01 -04002876 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002878 struct file_system_type *type;
Al Viroa0c9a8b2018-11-04 07:18:51 -05002879 struct fs_context *fc;
2880 const char *subtype = NULL;
2881 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002883 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return -EINVAL;
2885
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002886 type = get_fs_type(fstype);
2887 if (!type)
2888 return -ENODEV;
2889
Al Viroa0c9a8b2018-11-04 07:18:51 -05002890 if (type->fs_flags & FS_HAS_SUBTYPE) {
2891 subtype = strchr(fstype, '.');
2892 if (subtype) {
2893 subtype++;
2894 if (!*subtype) {
2895 put_filesystem(type);
2896 return -EINVAL;
2897 }
Al Viroa0c9a8b2018-11-04 07:18:51 -05002898 }
Eric W. Biederman8654df42016-06-09 16:06:06 -05002899 }
2900
Al Viroa0c9a8b2018-11-04 07:18:51 -05002901 fc = fs_context_for_mount(type, sb_flags);
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002902 put_filesystem(type);
Al Viroa0c9a8b2018-11-04 07:18:51 -05002903 if (IS_ERR(fc))
2904 return PTR_ERR(fc);
2905
David Howells3e1aeb02018-11-01 23:07:25 +00002906 if (subtype)
2907 err = vfs_parse_fs_string(fc, "subtype",
2908 subtype, strlen(subtype));
2909 if (!err && name)
2910 err = vfs_parse_fs_string(fc, "source", name, strlen(name));
Al Viroa0c9a8b2018-11-04 07:18:51 -05002911 if (!err)
2912 err = parse_monolithic_mount_data(fc, data);
Al Viroc3aabf02019-05-13 12:57:22 -04002913 if (!err && !mount_capable(fc))
2914 err = -EPERM;
Al Viroa0c9a8b2018-11-04 07:18:51 -05002915 if (!err)
2916 err = vfs_get_tree(fc);
David Howells132e4602018-11-04 07:43:08 -05002917 if (!err)
2918 err = do_new_mount_fc(fc, path, mnt_flags);
Al Viroa0c9a8b2018-11-04 07:18:51 -05002919
Al Viroa0c9a8b2018-11-04 07:18:51 -05002920 put_fs_context(fc);
Al Viro15f9a3f2011-01-17 01:41:58 -05002921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Al Viro19a167a2011-01-17 01:35:23 -05002924int finish_automount(struct vfsmount *m, struct path *path)
2925{
Al Viro26df6032020-01-11 10:44:29 -05002926 struct dentry *dentry = path->dentry;
Al Viro8f115382020-01-11 10:14:09 -05002927 struct mountpoint *mp;
Al Viro25e195a2020-01-11 11:27:46 -05002928 struct mount *mnt;
Al Viro19a167a2011-01-17 01:35:23 -05002929 int err;
Al Viro25e195a2020-01-11 11:27:46 -05002930
2931 if (!m)
2932 return 0;
2933 if (IS_ERR(m))
2934 return PTR_ERR(m);
2935
2936 mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002937 /* The new mount record should have at least 2 refs to prevent it being
2938 * expired before we get a chance to add it
2939 */
Al Viro6776db3d2011-11-25 00:22:05 -05002940 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002941
2942 if (m->mnt_sb == path->mnt->mnt_sb &&
Al Viro26df6032020-01-11 10:44:29 -05002943 m->mnt_root == dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002944 err = -ELOOP;
Al Viro26df6032020-01-11 10:44:29 -05002945 goto discard;
Al Viro19a167a2011-01-17 01:35:23 -05002946 }
2947
Al Viro26df6032020-01-11 10:44:29 -05002948 /*
2949 * we don't want to use lock_mount() - in this case finding something
2950 * that overmounts our mountpoint to be means "quitely drop what we've
2951 * got", not "try to mount it on top".
2952 */
2953 inode_lock(dentry->d_inode);
2954 namespace_lock();
2955 if (unlikely(cant_mount(dentry))) {
2956 err = -ENOENT;
2957 goto discard_locked;
2958 }
2959 rcu_read_lock();
2960 if (unlikely(__lookup_mnt(path->mnt, dentry))) {
2961 rcu_read_unlock();
2962 err = 0;
2963 goto discard_locked;
2964 }
2965 rcu_read_unlock();
2966 mp = get_mountpoint(dentry);
Al Viro8f115382020-01-11 10:14:09 -05002967 if (IS_ERR(mp)) {
2968 err = PTR_ERR(mp);
Al Viro26df6032020-01-11 10:44:29 -05002969 goto discard_locked;
Al Viro8f115382020-01-11 10:14:09 -05002970 }
Al Viro26df6032020-01-11 10:44:29 -05002971
Al Viro8f115382020-01-11 10:14:09 -05002972 err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
2973 unlock_mount(mp);
Al Viro26df6032020-01-11 10:44:29 -05002974 if (unlikely(err))
2975 goto discard;
2976 mntput(m);
2977 return 0;
2978
2979discard_locked:
2980 namespace_unlock();
2981 inode_unlock(dentry->d_inode);
2982discard:
Al Virob1e75df2011-01-17 01:47:59 -05002983 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002984 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002985 namespace_lock();
Al Viro6776db3d2011-11-25 00:22:05 -05002986 list_del_init(&mnt->mnt_expire);
Al Viro97216be2013-03-16 15:12:40 -04002987 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002988 }
Al Virob1e75df2011-01-17 01:47:59 -05002989 mntput(m);
2990 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002991 return err;
2992}
2993
David Howellsea5b7782011-01-14 19:10:03 +00002994/**
2995 * mnt_set_expiry - Put a mount on an expiration list
2996 * @mnt: The mount to list.
2997 * @expiry_list: The list to add the mount to.
2998 */
2999void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
3000{
Al Viro97216be2013-03-16 15:12:40 -04003001 namespace_lock();
David Howellsea5b7782011-01-14 19:10:03 +00003002
Al Viro6776db3d2011-11-25 00:22:05 -05003003 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00003004
Al Viro97216be2013-03-16 15:12:40 -04003005 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00003006}
3007EXPORT_SYMBOL(mnt_set_expiry);
3008
3009/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 * process a list of expirable mountpoints with the intent of discarding any
3011 * mountpoints that aren't in use and haven't been touched since last we came
3012 * here
3013 */
3014void mark_mounts_for_expiry(struct list_head *mounts)
3015{
Al Viro761d5c32011-11-24 21:07:43 -05003016 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 LIST_HEAD(graveyard);
3018
3019 if (list_empty(mounts))
3020 return;
3021
Al Viro97216be2013-03-16 15:12:40 -04003022 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04003023 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 /* extract from the expiration list every vfsmount that matches the
3026 * following criteria:
3027 * - only referenced by its parent vfsmount
3028 * - still marked for expiry (marked on the last call here; marks are
3029 * cleared by mntput())
3030 */
Al Viro6776db3d2011-11-25 00:22:05 -05003031 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05003032 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05003033 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05003035 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
Al Virobcc5c7d2008-03-22 00:21:53 -04003037 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05003038 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05003039 touch_mnt_namespace(mnt->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06003040 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04003041 }
Al Viro719ea2f2013-09-29 11:24:49 -04003042 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04003043 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
3046EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
3047
3048/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04003049 * Ripoff of 'select_parent()'
3050 *
3051 * search the list of submounts for a given mountpoint, and move any
3052 * shrinkable submounts to the 'graveyard' list.
3053 */
Al Viro692afc32011-11-24 21:15:14 -05003054static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04003055{
Al Viro692afc32011-11-24 21:15:14 -05003056 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04003057 struct list_head *next;
3058 int found = 0;
3059
3060repeat:
Al Viro6b41d532011-11-24 23:24:33 -05003061 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04003062resume:
Al Viro6b41d532011-11-24 23:24:33 -05003063 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04003064 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05003065 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04003066
3067 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05003068 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04003069 continue;
3070 /*
3071 * Descend a level if the d_mounts list is non-empty.
3072 */
Al Viro6b41d532011-11-24 23:24:33 -05003073 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04003074 this_parent = mnt;
3075 goto repeat;
3076 }
3077
Al Viro1ab59732011-11-24 21:35:16 -05003078 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05003079 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04003080 found++;
3081 }
3082 }
3083 /*
3084 * All done at this level ... ascend and resume the search
3085 */
3086 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05003087 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05003088 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04003089 goto resume;
3090 }
3091 return found;
3092}
3093
3094/*
3095 * process a list of expirable mountpoints with the intent of discarding any
3096 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10003097 *
Al Viro48a066e2013-09-29 22:06:07 -04003098 * mount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04003099 */
Al Virob54b9be2013-03-16 14:39:34 -04003100static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04003101{
3102 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05003103 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04003104
Trond Myklebust5528f9112006-06-09 09:34:17 -04003105 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04003106 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04003107 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05003108 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05003109 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05003110 touch_mnt_namespace(m->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06003111 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04003112 }
3113 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04003114}
3115
Christoph Hellwig028abd92020-09-17 10:22:34 +02003116static void *copy_mount_options(const void __user * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Al Virob40ef862015-12-14 18:44:44 -05003118 char *copy;
Catalin Marinasd563d672020-07-01 17:46:06 +01003119 unsigned left, offset;
Ram Paib58fed82005-11-07 17:16:09 -05003120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (!data)
Al Virob40ef862015-12-14 18:44:44 -05003122 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Al Virob40ef862015-12-14 18:44:44 -05003124 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
3125 if (!copy)
3126 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Catalin Marinasd563d672020-07-01 17:46:06 +01003128 left = copy_from_user(copy, data, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Catalin Marinasd563d672020-07-01 17:46:06 +01003130 /*
3131 * Not all architectures have an exact copy_from_user(). Resort to
3132 * byte at a time.
3133 */
3134 offset = PAGE_SIZE - left;
3135 while (left) {
3136 char c;
3137 if (get_user(c, (const char __user *)data + offset))
3138 break;
3139 copy[offset] = c;
3140 left--;
3141 offset++;
3142 }
3143
3144 if (left == PAGE_SIZE) {
Al Virob40ef862015-12-14 18:44:44 -05003145 kfree(copy);
3146 return ERR_PTR(-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Catalin Marinasd563d672020-07-01 17:46:06 +01003148
Al Virob40ef862015-12-14 18:44:44 -05003149 return copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Christoph Hellwig028abd92020-09-17 10:22:34 +02003152static char *copy_mount_string(const void __user *data)
Vegard Nossumeca6f532009-09-18 13:05:45 -07003153{
Chandan Rajendrafbdb4402019-01-22 12:21:52 +05303154 return data ? strndup_user(data, PATH_MAX) : NULL;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003155}
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157/*
3158 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
3159 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
3160 *
3161 * data is a (void *) that can point to any structure up to
3162 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
3163 * information (or be NULL).
3164 *
3165 * Pre-0.97 versions of mount() didn't have a flags word.
3166 * When the flags word was introduced its top half was required
3167 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
3168 * Therefore, if this magic number is present, it carries no information
3169 * and must be discarded.
3170 */
Christoph Hellwigc60166f2020-07-21 11:12:08 +02003171int path_mount(const char *dev_name, struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04003172 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
David Howellse462ec52017-07-17 08:45:35 +01003174 unsigned int mnt_flags = 0, sb_flags;
Christoph Hellwiga1e6aaa2020-06-04 09:41:08 +02003175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 /* Discard magic */
3178 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
3179 flags &= ~MS_MGC_MSK;
3180
3181 /* Basic sanity checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (data_page)
3183 ((char *)data_page)[PAGE_SIZE - 1] = 0;
3184
David Howellse462ec52017-07-17 08:45:35 +01003185 if (flags & MS_NOUSER)
3186 return -EINVAL;
3187
Christoph Hellwiga1e6aaa2020-06-04 09:41:08 +02003188 ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
3189 if (ret)
3190 return ret;
3191 if (!may_mount())
3192 return -EPERM;
3193 if ((flags & SB_MANDLOCK) && !may_mandlock())
3194 return -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09003195
Andi Kleen613cbe32009-04-19 18:40:43 +02003196 /* Default to relatime unless overriden */
3197 if (!(flags & MS_NOATIME))
3198 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 /* Separate the per-mountpoint flags */
3201 if (flags & MS_NOSUID)
3202 mnt_flags |= MNT_NOSUID;
3203 if (flags & MS_NODEV)
3204 mnt_flags |= MNT_NODEV;
3205 if (flags & MS_NOEXEC)
3206 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08003207 if (flags & MS_NOATIME)
3208 mnt_flags |= MNT_NOATIME;
3209 if (flags & MS_NODIRATIME)
3210 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00003211 if (flags & MS_STRICTATIME)
3212 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
David Howellsa9e5b732018-04-20 13:35:02 +01003213 if (flags & MS_RDONLY)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08003214 mnt_flags |= MNT_READONLY;
Mattias Nisslerdab741e02020-08-27 11:09:46 -06003215 if (flags & MS_NOSYMFOLLOW)
3216 mnt_flags |= MNT_NOSYMFOLLOW;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08003217
Eric W. Biedermanffbc6f02014-07-28 17:36:04 -07003218 /* The default atime for remount is preservation */
3219 if ((flags & MS_REMOUNT) &&
3220 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3221 MS_STRICTATIME)) == 0)) {
3222 mnt_flags &= ~MNT_ATIME_MASK;
Christoph Hellwiga1e6aaa2020-06-04 09:41:08 +02003223 mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
Eric W. Biedermanffbc6f02014-07-28 17:36:04 -07003224 }
3225
David Howellse462ec52017-07-17 08:45:35 +01003226 sb_flags = flags & (SB_RDONLY |
3227 SB_SYNCHRONOUS |
3228 SB_MANDLOCK |
3229 SB_DIRSYNC |
3230 SB_SILENT |
Mimi Zohar917086f2017-10-08 00:28:21 -04003231 SB_POSIXACL |
Markus Trippelsdorfd7ee9462017-10-11 07:01:31 +02003232 SB_LAZYTIME |
Mimi Zohar917086f2017-10-08 00:28:21 -04003233 SB_I_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
David Howells43f5e652018-11-01 23:07:25 +00003235 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
Christoph Hellwiga1e6aaa2020-06-04 09:41:08 +02003236 return do_reconfigure_mnt(path, mnt_flags);
3237 if (flags & MS_REMOUNT)
3238 return do_remount(path, flags, sb_flags, mnt_flags, data_page);
3239 if (flags & MS_BIND)
3240 return do_loopback(path, dev_name, flags & MS_REC);
3241 if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
3242 return do_change_type(path, flags);
3243 if (flags & MS_MOVE)
3244 return do_move_mount_old(path, dev_name);
3245
3246 return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
3247 data_page);
3248}
3249
3250long do_mount(const char *dev_name, const char __user *dir_name,
3251 const char *type_page, unsigned long flags, void *data_page)
3252{
3253 struct path path;
3254 int ret;
3255
3256 ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
3257 if (ret)
3258 return ret;
3259 ret = path_mount(dev_name, &path, type_page, flags, data_page);
Al Viro2d92ab32008-08-02 00:51:11 -04003260 path_put(&path);
Christoph Hellwiga1e6aaa2020-06-04 09:41:08 +02003261 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003264static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3265{
3266 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3267}
3268
3269static void dec_mnt_namespaces(struct ucounts *ucounts)
3270{
3271 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3272}
3273
Eric W. Biederman771b1372012-07-26 21:08:32 -07003274static void free_mnt_ns(struct mnt_namespace *ns)
3275{
Al Viro74e83122019-01-30 13:30:21 -05003276 if (!is_anon_ns(ns))
3277 ns_free_inum(&ns->ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003278 dec_mnt_namespaces(ns->ucounts);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003279 put_user_ns(ns->user_ns);
3280 kfree(ns);
3281}
3282
Eric W. Biederman8823c072010-03-07 18:49:36 -08003283/*
3284 * Assign a sequence number so we can detect when we attempt to bind
3285 * mount a reference to an older mount namespace into the current
3286 * mount namespace, preventing reference counting loops. A 64bit
3287 * number incrementing at 10Ghz will take 12,427 years to wrap which
3288 * is effectively never, so we can ignore the possibility.
3289 */
3290static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
3291
Al Viro74e83122019-01-30 13:30:21 -05003292static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003293{
3294 struct mnt_namespace *new_ns;
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003295 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07003296 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003297
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003298 ucounts = inc_mnt_namespaces(user_ns);
3299 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -05003300 return ERR_PTR(-ENOSPC);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003301
Al Viro74e83122019-01-30 13:30:21 -05003302 new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003303 if (!new_ns) {
3304 dec_mnt_namespaces(ucounts);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003305 return ERR_PTR(-ENOMEM);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003306 }
Al Viro74e83122019-01-30 13:30:21 -05003307 if (!anon) {
3308 ret = ns_alloc_inum(&new_ns->ns);
3309 if (ret) {
3310 kfree(new_ns);
3311 dec_mnt_namespaces(ucounts);
3312 return ERR_PTR(ret);
3313 }
Eric W. Biederman98f842e2011-06-15 10:21:48 -07003314 }
Al Viro33c42942014-11-01 02:32:53 -04003315 new_ns->ns.ops = &mntns_operations;
Al Viro74e83122019-01-30 13:30:21 -05003316 if (!anon)
3317 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Kirill Tkhai1a7b89692020-08-03 13:16:42 +03003318 refcount_set(&new_ns->ns.count, 1);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003319 INIT_LIST_HEAD(&new_ns->list);
3320 init_waitqueue_head(&new_ns->poll);
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02003321 spin_lock_init(&new_ns->ns_lock);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003322 new_ns->user_ns = get_user_ns(user_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003323 new_ns->ucounts = ucounts;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003324 return new_ns;
3325}
3326
Emese Revfy0766f782016-06-20 20:42:34 +02003327__latent_entropy
Al Viro9559f682013-09-28 20:47:57 -04003328struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
3329 struct user_namespace *user_ns, struct fs_struct *new_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003331 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04003332 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05003333 struct mount *p, *q;
Al Viro9559f682013-09-28 20:47:57 -04003334 struct mount *old;
Al Virocb338d02011-11-24 20:55:08 -05003335 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003336 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Al Viro9559f682013-09-28 20:47:57 -04003338 BUG_ON(!ns);
3339
3340 if (likely(!(flags & CLONE_NEWNS))) {
3341 get_mnt_ns(ns);
3342 return ns;
3343 }
3344
3345 old = ns->root;
3346
Al Viro74e83122019-01-30 13:30:21 -05003347 new_ns = alloc_mnt_ns(user_ns, false);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003348 if (IS_ERR(new_ns))
3349 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Al Viro97216be2013-03-16 15:12:40 -04003351 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /* First pass: copy the tree topology */
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003353 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
Al Viro9559f682013-09-28 20:47:57 -04003354 if (user_ns != ns->user_ns)
Al Viro3bd045c2019-01-30 13:15:45 -05003355 copy_flags |= CL_SHARED_TO_SLAVE;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003356 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01003357 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04003358 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07003359 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01003360 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 }
Al Viro3bd045c2019-01-30 13:15:45 -05003362 if (user_ns != ns->user_ns) {
3363 lock_mount_hash();
3364 lock_mnt_tree(new);
3365 unlock_mount_hash();
3366 }
Al Virobe08d6d2011-12-06 13:32:36 -05003367 new_ns->root = new;
Al Viro1a4eeaf2011-11-25 02:19:55 -05003368 list_add_tail(&new_ns->list, &new->mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 /*
3371 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3372 * as belonging to new namespace. We have already acquired a private
3373 * fs_struct, so tsk->fs->lock is not needed.
3374 */
Al Viro909b0a82011-11-25 03:06:56 -05003375 p = old;
Al Virocb338d02011-11-24 20:55:08 -05003376 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05003378 q->mnt_ns = new_ns;
Eric W. Biedermand2921682016-09-28 00:27:17 -05003379 new_ns->mounts++;
Al Viro9559f682013-09-28 20:47:57 -04003380 if (new_fs) {
3381 if (&p->mnt == new_fs->root.mnt) {
3382 new_fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003383 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
Al Viro9559f682013-09-28 20:47:57 -04003385 if (&p->mnt == new_fs->pwd.mnt) {
3386 new_fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003387 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
Al Viro909b0a82011-11-25 03:06:56 -05003390 p = next_mnt(p, old);
3391 q = next_mnt(q, new);
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003392 if (!q)
3393 break;
3394 while (p->mnt.mnt_root != q->mnt.mnt_root)
3395 p = next_mnt(p, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
Al Viro328e6d92013-03-16 14:49:45 -04003397 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05003400 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05003402 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
JANAK DESAI741a2952006-02-07 12:59:00 -08003404 return new_ns;
3405}
3406
Al Viro74e83122019-01-30 13:30:21 -05003407struct dentry *mount_subtree(struct vfsmount *m, const char *name)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003408{
Al Viro74e83122019-01-30 13:30:21 -05003409 struct mount *mnt = real_mount(m);
Al Viroea441d12011-11-16 21:43:59 -05003410 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05003411 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05003412 struct path path;
3413 int err;
3414
Al Viro74e83122019-01-30 13:30:21 -05003415 ns = alloc_mnt_ns(&init_user_ns, true);
3416 if (IS_ERR(ns)) {
3417 mntput(m);
Al Viroea441d12011-11-16 21:43:59 -05003418 return ERR_CAST(ns);
Al Viro74e83122019-01-30 13:30:21 -05003419 }
3420 mnt->mnt_ns = ns;
3421 ns->root = mnt;
3422 ns->mounts++;
3423 list_add(&mnt->mnt_list, &ns->list);
Al Viroea441d12011-11-16 21:43:59 -05003424
Al Viro74e83122019-01-30 13:30:21 -05003425 err = vfs_path_lookup(m->mnt_root, m,
Al Viroea441d12011-11-16 21:43:59 -05003426 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3427
3428 put_mnt_ns(ns);
3429
3430 if (err)
3431 return ERR_PTR(err);
3432
3433 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05003434 s = path.mnt->mnt_sb;
3435 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05003436 mntput(path.mnt);
3437 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05003438 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05003439 /* ... and return the root of (sub)tree on it */
3440 return path.dentry;
3441}
3442EXPORT_SYMBOL(mount_subtree);
3443
Dominik Brodowskicccaa5e2018-10-23 22:41:09 +02003444SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3445 char __user *, type, unsigned long, flags, void __user *, data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
Vegard Nossumeca6f532009-09-18 13:05:45 -07003447 int ret;
3448 char *kernel_type;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003449 char *kernel_dev;
Al Virob40ef862015-12-14 18:44:44 -05003450 void *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Tim Gardnerb8850d12014-08-28 11:26:03 -06003452 kernel_type = copy_mount_string(type);
3453 ret = PTR_ERR(kernel_type);
3454 if (IS_ERR(kernel_type))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003455 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Tim Gardnerb8850d12014-08-28 11:26:03 -06003457 kernel_dev = copy_mount_string(dev_name);
3458 ret = PTR_ERR(kernel_dev);
3459 if (IS_ERR(kernel_dev))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003460 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Al Virob40ef862015-12-14 18:44:44 -05003462 options = copy_mount_options(data);
3463 ret = PTR_ERR(options);
3464 if (IS_ERR(options))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003465 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Al Virob40ef862015-12-14 18:44:44 -05003467 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003468
Al Virob40ef862015-12-14 18:44:44 -05003469 kfree(options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003470out_data:
3471 kfree(kernel_dev);
3472out_dev:
Vegard Nossumeca6f532009-09-18 13:05:45 -07003473 kfree(kernel_type);
3474out_type:
3475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Christian Brauner5b490502021-01-21 14:19:52 +01003478#define FSMOUNT_VALID_FLAGS \
3479 (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \
3480 MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME)
3481
Christian Brauner9caccd42021-01-21 14:19:54 +01003482#define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
Christian Brauner2a186722021-01-21 14:19:53 +01003483
3484#define MOUNT_SETATTR_PROPAGATION_FLAGS \
3485 (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
3486
Christian Brauner5b490502021-01-21 14:19:52 +01003487static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
3488{
3489 unsigned int mnt_flags = 0;
3490
3491 if (attr_flags & MOUNT_ATTR_RDONLY)
3492 mnt_flags |= MNT_READONLY;
3493 if (attr_flags & MOUNT_ATTR_NOSUID)
3494 mnt_flags |= MNT_NOSUID;
3495 if (attr_flags & MOUNT_ATTR_NODEV)
3496 mnt_flags |= MNT_NODEV;
3497 if (attr_flags & MOUNT_ATTR_NOEXEC)
3498 mnt_flags |= MNT_NOEXEC;
3499 if (attr_flags & MOUNT_ATTR_NODIRATIME)
3500 mnt_flags |= MNT_NODIRATIME;
3501
3502 return mnt_flags;
3503}
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505/*
David Howells93766fb2018-11-01 23:36:14 +00003506 * Create a kernel mount representation for a new, prepared superblock
3507 * (specified by fs_fd) and attach to an open_tree-like file descriptor.
3508 */
3509SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
3510 unsigned int, attr_flags)
3511{
3512 struct mnt_namespace *ns;
3513 struct fs_context *fc;
3514 struct file *file;
3515 struct path newmount;
3516 struct mount *mnt;
3517 struct fd f;
3518 unsigned int mnt_flags = 0;
3519 long ret;
3520
3521 if (!may_mount())
3522 return -EPERM;
3523
3524 if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
3525 return -EINVAL;
3526
Christian Brauner5b490502021-01-21 14:19:52 +01003527 if (attr_flags & ~FSMOUNT_VALID_FLAGS)
David Howells93766fb2018-11-01 23:36:14 +00003528 return -EINVAL;
3529
Christian Brauner5b490502021-01-21 14:19:52 +01003530 mnt_flags = attr_flags_to_mnt_flags(attr_flags);
David Howells93766fb2018-11-01 23:36:14 +00003531
3532 switch (attr_flags & MOUNT_ATTR__ATIME) {
3533 case MOUNT_ATTR_STRICTATIME:
3534 break;
3535 case MOUNT_ATTR_NOATIME:
3536 mnt_flags |= MNT_NOATIME;
3537 break;
3538 case MOUNT_ATTR_RELATIME:
3539 mnt_flags |= MNT_RELATIME;
3540 break;
3541 default:
3542 return -EINVAL;
3543 }
3544
3545 f = fdget(fs_fd);
3546 if (!f.file)
3547 return -EBADF;
3548
3549 ret = -EINVAL;
3550 if (f.file->f_op != &fscontext_fops)
3551 goto err_fsfd;
3552
3553 fc = f.file->private_data;
3554
3555 ret = mutex_lock_interruptible(&fc->uapi_mutex);
3556 if (ret < 0)
3557 goto err_fsfd;
3558
3559 /* There must be a valid superblock or we can't mount it */
3560 ret = -EINVAL;
3561 if (!fc->root)
3562 goto err_unlock;
3563
3564 ret = -EPERM;
3565 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
3566 pr_warn("VFS: Mount too revealing\n");
3567 goto err_unlock;
3568 }
3569
3570 ret = -EBUSY;
3571 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
3572 goto err_unlock;
3573
3574 ret = -EPERM;
3575 if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
3576 goto err_unlock;
3577
3578 newmount.mnt = vfs_create_mount(fc);
3579 if (IS_ERR(newmount.mnt)) {
3580 ret = PTR_ERR(newmount.mnt);
3581 goto err_unlock;
3582 }
3583 newmount.dentry = dget(fc->root);
3584 newmount.mnt->mnt_flags = mnt_flags;
3585
3586 /* We've done the mount bit - now move the file context into more or
3587 * less the same state as if we'd done an fspick(). We don't want to
3588 * do any memory allocation or anything like that at this point as we
3589 * don't want to have to handle any errors incurred.
3590 */
3591 vfs_clean_context(fc);
3592
3593 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
3594 if (IS_ERR(ns)) {
3595 ret = PTR_ERR(ns);
3596 goto err_path;
3597 }
3598 mnt = real_mount(newmount.mnt);
3599 mnt->mnt_ns = ns;
3600 ns->root = mnt;
3601 ns->mounts = 1;
3602 list_add(&mnt->mnt_list, &ns->list);
Eric Biggers1b0b9cc2019-06-12 11:43:13 -07003603 mntget(newmount.mnt);
David Howells93766fb2018-11-01 23:36:14 +00003604
3605 /* Attach to an apparent O_PATH fd with a note that we need to unmount
3606 * it, not just simply put it.
3607 */
3608 file = dentry_open(&newmount, O_PATH, fc->cred);
3609 if (IS_ERR(file)) {
3610 dissolve_on_fput(newmount.mnt);
3611 ret = PTR_ERR(file);
3612 goto err_path;
3613 }
3614 file->f_mode |= FMODE_NEED_UNMOUNT;
3615
3616 ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
3617 if (ret >= 0)
3618 fd_install(ret, file);
3619 else
3620 fput(file);
3621
3622err_path:
3623 path_put(&newmount);
3624err_unlock:
3625 mutex_unlock(&fc->uapi_mutex);
3626err_fsfd:
3627 fdput(f);
3628 return ret;
3629}
3630
3631/*
3632 * Move a mount from one place to another. In combination with
3633 * fsopen()/fsmount() this is used to install a new mount and in combination
3634 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
3635 * a mount subtree.
David Howells2db154b2018-11-05 17:40:30 +00003636 *
3637 * Note the flags value is a combination of MOVE_MOUNT_* flags.
3638 */
3639SYSCALL_DEFINE5(move_mount,
Ben Dooks2658ce02019-10-15 11:35:02 +01003640 int, from_dfd, const char __user *, from_pathname,
3641 int, to_dfd, const char __user *, to_pathname,
David Howells2db154b2018-11-05 17:40:30 +00003642 unsigned int, flags)
3643{
3644 struct path from_path, to_path;
3645 unsigned int lflags;
3646 int ret = 0;
3647
3648 if (!may_mount())
3649 return -EPERM;
3650
3651 if (flags & ~MOVE_MOUNT__MASK)
3652 return -EINVAL;
3653
3654 /* If someone gives a pathname, they aren't permitted to move
3655 * from an fd that requires unmount as we can't get at the flag
3656 * to clear it afterwards.
3657 */
3658 lflags = 0;
3659 if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3660 if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3661 if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3662
3663 ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
3664 if (ret < 0)
3665 return ret;
3666
3667 lflags = 0;
3668 if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3669 if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3670 if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3671
3672 ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
3673 if (ret < 0)
3674 goto out_from;
3675
3676 ret = security_move_mount(&from_path, &to_path);
3677 if (ret < 0)
3678 goto out_to;
3679
3680 ret = do_move_mount(&from_path, &to_path);
3681
3682out_to:
3683 path_put(&to_path);
3684out_from:
3685 path_put(&from_path);
3686 return ret;
3687}
3688
3689/*
Al Viroafac7cb2011-11-23 19:34:49 -05003690 * Return true if path is reachable from root
3691 *
Al Viro48a066e2013-09-29 22:06:07 -04003692 * namespace_sem or mount_lock is held
Al Viroafac7cb2011-11-23 19:34:49 -05003693 */
Al Viro643822b2011-11-24 22:00:28 -05003694bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05003695 const struct path *root)
3696{
Al Viro643822b2011-11-24 22:00:28 -05003697 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05003698 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05003699 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05003700 }
Al Viro643822b2011-11-24 22:00:28 -05003701 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05003702}
3703
Mickaël Salaün640eb7e2016-11-14 22:14:35 +01003704bool path_is_under(const struct path *path1, const struct path *path2)
Al Viroafac7cb2011-11-23 19:34:49 -05003705{
Yaowei Bai25ab4c92015-11-17 14:40:10 +08003706 bool res;
Al Viro48a066e2013-09-29 22:06:07 -04003707 read_seqlock_excl(&mount_lock);
Al Viro643822b2011-11-24 22:00:28 -05003708 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Al Viro48a066e2013-09-29 22:06:07 -04003709 read_sequnlock_excl(&mount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05003710 return res;
3711}
3712EXPORT_SYMBOL(path_is_under);
3713
3714/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 * pivot_root Semantics:
3716 * Moves the root file system of the current process to the directory put_old,
3717 * makes new_root as the new root file system of the current process, and sets
3718 * root/cwd of all processes which had them on the current root to new_root.
3719 *
3720 * Restrictions:
3721 * The new_root and put_old must be directories, and must not be on the
3722 * same file system as the current process root. The put_old must be
3723 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3724 * pointed to by put_old must yield the same directory as new_root. No other
3725 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3726 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08003727 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02003728 * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
Neil Brown4a0d11f2006-01-08 01:03:18 -08003729 * in this situation.
3730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 * Notes:
3732 * - we don't move root/cwd if they are not at the root (reason: if something
3733 * cared enough to change them, it's probably wrong to force them elsewhere)
3734 * - it's okay to pick a root that isn't the root of a file system, e.g.
3735 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3736 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3737 * first.
3738 */
Heiko Carstens3480b252009-01-14 14:14:16 +01003739SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3740 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
Al Viro2763d112019-06-30 19:18:53 -04003742 struct path new, old, root;
3743 struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
Al Viro84d17192013-03-15 10:53:28 -04003744 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 int error;
3746
Al Viro9b40bc92013-02-22 22:45:42 -05003747 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return -EPERM;
3749
Al Viroce6595a2019-07-14 16:42:44 -04003750 error = user_path_at(AT_FDCWD, new_root,
3751 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (error)
3753 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Al Viroce6595a2019-07-14 16:42:44 -04003755 error = user_path_at(AT_FDCWD, put_old,
3756 LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (error)
3758 goto out1;
3759
Al Viro2d8f3032008-07-22 09:59:21 -04003760 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04003761 if (error)
3762 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02003764 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04003765 old_mp = lock_mount(&old);
3766 error = PTR_ERR(old_mp);
3767 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04003768 goto out3;
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05003771 new_mnt = real_mount(new.mnt);
3772 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04003773 old_mnt = real_mount(old.mnt);
Al Viro2763d112019-06-30 19:18:53 -04003774 ex_parent = new_mnt->mnt_parent;
3775 root_parent = root_mnt->mnt_parent;
Al Viro84d17192013-03-15 10:53:28 -04003776 if (IS_MNT_SHARED(old_mnt) ||
Al Viro2763d112019-06-30 19:18:53 -04003777 IS_MNT_SHARED(ex_parent) ||
3778 IS_MNT_SHARED(root_parent))
Al Virob12cea92011-03-18 08:55:38 -04003779 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05003780 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003781 goto out4;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003782 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3783 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003785 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04003786 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04003788 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04003789 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04003791 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003792 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003793 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003794 goto out4; /* not attached */
Al Viro2d8f3032008-07-22 09:59:21 -04003795 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003796 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003797 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003798 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08003799 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04003800 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04003801 goto out4;
Eric W. Biederman0d082602014-10-08 10:42:27 -07003802 /* make certain new is below the root */
3803 if (!is_path_reachable(new_mnt, new.dentry, &root))
3804 goto out4;
Al Viro719ea2f2013-09-29 11:24:49 -04003805 lock_mount_hash();
Al Viro2763d112019-06-30 19:18:53 -04003806 umount_mnt(new_mnt);
3807 root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003808 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3809 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3810 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3811 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003812 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04003813 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08003814 /* mount new_root on / */
Al Viro2763d112019-06-30 19:18:53 -04003815 attach_mnt(new_mnt, root_parent, root_mp);
3816 mnt_add_count(root_parent, -1);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003817 touch_mnt_namespace(current->nsproxy->mnt_ns);
Eric W. Biederman4fed6552014-10-08 10:42:57 -07003818 /* A moved mount should not expire automatically */
3819 list_del_init(&new_mnt->mnt_expire);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13003820 put_mountpoint(root_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04003821 unlock_mount_hash();
Al Viro2d8f3032008-07-22 09:59:21 -04003822 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04003824out4:
Al Viro84d17192013-03-15 10:53:28 -04003825 unlock_mount(old_mp);
Al Viro2763d112019-06-30 19:18:53 -04003826 if (!error)
3827 mntput_no_expire(ex_parent);
Al Virob12cea92011-03-18 08:55:38 -04003828out3:
Al Viro8c3ee422008-03-22 18:00:39 -04003829 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04003830out2:
Al Viro2d8f3032008-07-22 09:59:21 -04003831 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832out1:
Al Viro2d8f3032008-07-22 09:59:21 -04003833 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Christian Brauner2a186722021-01-21 14:19:53 +01003838static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
3839{
3840 unsigned int flags = mnt->mnt.mnt_flags;
3841
3842 /* flags to clear */
3843 flags &= ~kattr->attr_clr;
3844 /* flags to raise */
3845 flags |= kattr->attr_set;
3846
3847 return flags;
3848}
3849
Christian Brauner9caccd42021-01-21 14:19:54 +01003850static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
3851{
3852 struct vfsmount *m = &mnt->mnt;
3853
3854 if (!kattr->mnt_userns)
3855 return 0;
3856
3857 /*
3858 * Once a mount has been idmapped we don't allow it to change its
3859 * mapping. It makes things simpler and callers can just create
3860 * another bind-mount they can idmap if they want to.
3861 */
3862 if (mnt_user_ns(m) != &init_user_ns)
3863 return -EPERM;
3864
3865 /* The underlying filesystem doesn't support idmapped mounts yet. */
3866 if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
3867 return -EINVAL;
3868
3869 /* We're not controlling the superblock. */
3870 if (!ns_capable(m->mnt_sb->s_user_ns, CAP_SYS_ADMIN))
3871 return -EPERM;
3872
3873 /* Mount has already been visible in the filesystem hierarchy. */
3874 if (!is_anon_ns(mnt->mnt_ns))
3875 return -EINVAL;
3876
3877 return 0;
3878}
3879
Christian Brauner2a186722021-01-21 14:19:53 +01003880static struct mount *mount_setattr_prepare(struct mount_kattr *kattr,
3881 struct mount *mnt, int *err)
3882{
3883 struct mount *m = mnt, *last = NULL;
3884
3885 if (!is_mounted(&m->mnt)) {
3886 *err = -EINVAL;
3887 goto out;
3888 }
3889
3890 if (!(mnt_has_parent(m) ? check_mnt(m) : is_anon_ns(m->mnt_ns))) {
3891 *err = -EINVAL;
3892 goto out;
3893 }
3894
3895 do {
3896 unsigned int flags;
3897
3898 flags = recalc_flags(kattr, m);
3899 if (!can_change_locked_flags(m, flags)) {
3900 *err = -EPERM;
3901 goto out;
3902 }
3903
Christian Brauner9caccd42021-01-21 14:19:54 +01003904 *err = can_idmap_mount(kattr, m);
3905 if (*err)
3906 goto out;
3907
Christian Brauner2a186722021-01-21 14:19:53 +01003908 last = m;
3909
3910 if ((kattr->attr_set & MNT_READONLY) &&
3911 !(m->mnt.mnt_flags & MNT_READONLY)) {
3912 *err = mnt_hold_writers(m);
3913 if (*err)
3914 goto out;
3915 }
3916 } while (kattr->recurse && (m = next_mnt(m, mnt)));
3917
3918out:
3919 return last;
3920}
3921
Christian Brauner9caccd42021-01-21 14:19:54 +01003922static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
3923{
3924 struct user_namespace *mnt_userns;
3925
3926 if (!kattr->mnt_userns)
3927 return;
3928
3929 mnt_userns = get_user_ns(kattr->mnt_userns);
3930 /* Pairs with smp_load_acquire() in mnt_user_ns(). */
3931 smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
3932}
3933
Christian Brauner2a186722021-01-21 14:19:53 +01003934static void mount_setattr_commit(struct mount_kattr *kattr,
3935 struct mount *mnt, struct mount *last,
3936 int err)
3937{
3938 struct mount *m = mnt;
3939
3940 do {
3941 if (!err) {
3942 unsigned int flags;
3943
Christian Brauner9caccd42021-01-21 14:19:54 +01003944 do_idmap_mount(kattr, m);
Christian Brauner2a186722021-01-21 14:19:53 +01003945 flags = recalc_flags(kattr, m);
3946 WRITE_ONCE(m->mnt.mnt_flags, flags);
3947 }
3948
3949 /*
3950 * We either set MNT_READONLY above so make it visible
3951 * before ~MNT_WRITE_HOLD or we failed to recursively
3952 * apply mount options.
3953 */
3954 if ((kattr->attr_set & MNT_READONLY) &&
3955 (m->mnt.mnt_flags & MNT_WRITE_HOLD))
3956 mnt_unhold_writers(m);
3957
3958 if (!err && kattr->propagation)
3959 change_mnt_propagation(m, kattr->propagation);
3960
3961 /*
3962 * On failure, only cleanup until we found the first mount
3963 * we failed to handle.
3964 */
3965 if (err && m == last)
3966 break;
3967 } while (kattr->recurse && (m = next_mnt(m, mnt)));
3968
3969 if (!err)
3970 touch_mnt_namespace(mnt->mnt_ns);
3971}
3972
3973static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
3974{
3975 struct mount *mnt = real_mount(path->mnt), *last = NULL;
3976 int err = 0;
3977
3978 if (path->dentry != mnt->mnt.mnt_root)
3979 return -EINVAL;
3980
3981 if (kattr->propagation) {
3982 /*
3983 * Only take namespace_lock() if we're actually changing
3984 * propagation.
3985 */
3986 namespace_lock();
3987 if (kattr->propagation == MS_SHARED) {
3988 err = invent_group_ids(mnt, kattr->recurse);
3989 if (err) {
3990 namespace_unlock();
3991 return err;
3992 }
3993 }
3994 }
3995
3996 lock_mount_hash();
3997
3998 /*
3999 * Get the mount tree in a shape where we can change mount
4000 * properties without failure.
4001 */
4002 last = mount_setattr_prepare(kattr, mnt, &err);
4003 if (last) /* Commit all changes or revert to the old state. */
4004 mount_setattr_commit(kattr, mnt, last, err);
4005
4006 unlock_mount_hash();
4007
4008 if (kattr->propagation) {
4009 namespace_unlock();
4010 if (err)
4011 cleanup_group_ids(mnt, NULL);
4012 }
4013
4014 return err;
4015}
4016
Christian Brauner9caccd42021-01-21 14:19:54 +01004017static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
4018 struct mount_kattr *kattr, unsigned int flags)
4019{
4020 int err = 0;
4021 struct ns_common *ns;
4022 struct user_namespace *mnt_userns;
4023 struct file *file;
4024
4025 if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
4026 return 0;
4027
4028 /*
4029 * We currently do not support clearing an idmapped mount. If this ever
4030 * is a use-case we can revisit this but for now let's keep it simple
4031 * and not allow it.
4032 */
4033 if (attr->attr_clr & MOUNT_ATTR_IDMAP)
4034 return -EINVAL;
4035
4036 if (attr->userns_fd > INT_MAX)
4037 return -EINVAL;
4038
4039 file = fget(attr->userns_fd);
4040 if (!file)
4041 return -EBADF;
4042
4043 if (!proc_ns_file(file)) {
4044 err = -EINVAL;
4045 goto out_fput;
4046 }
4047
4048 ns = get_proc_ns(file_inode(file));
4049 if (ns->ops->type != CLONE_NEWUSER) {
4050 err = -EINVAL;
4051 goto out_fput;
4052 }
4053
4054 /*
4055 * The init_user_ns is used to indicate that a vfsmount is not idmapped.
4056 * This is simpler than just having to treat NULL as unmapped. Users
4057 * wanting to idmap a mount to init_user_ns can just use a namespace
4058 * with an identity mapping.
4059 */
4060 mnt_userns = container_of(ns, struct user_namespace, ns);
4061 if (mnt_userns == &init_user_ns) {
4062 err = -EPERM;
4063 goto out_fput;
4064 }
4065 kattr->mnt_userns = get_user_ns(mnt_userns);
4066
4067out_fput:
4068 fput(file);
4069 return err;
4070}
4071
4072static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
Christian Brauner2a186722021-01-21 14:19:53 +01004073 struct mount_kattr *kattr, unsigned int flags)
4074{
4075 unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
4076
4077 if (flags & AT_NO_AUTOMOUNT)
4078 lookup_flags &= ~LOOKUP_AUTOMOUNT;
4079 if (flags & AT_SYMLINK_NOFOLLOW)
4080 lookup_flags &= ~LOOKUP_FOLLOW;
4081 if (flags & AT_EMPTY_PATH)
4082 lookup_flags |= LOOKUP_EMPTY;
4083
4084 *kattr = (struct mount_kattr) {
4085 .lookup_flags = lookup_flags,
4086 .recurse = !!(flags & AT_RECURSIVE),
4087 };
4088
4089 if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
4090 return -EINVAL;
4091 if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
4092 return -EINVAL;
4093 kattr->propagation = attr->propagation;
4094
4095 if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
4096 return -EINVAL;
4097
Christian Brauner2a186722021-01-21 14:19:53 +01004098 kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
4099 kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
4100
4101 /*
4102 * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
4103 * users wanting to transition to a different atime setting cannot
4104 * simply specify the atime setting in @attr_set, but must also
4105 * specify MOUNT_ATTR__ATIME in the @attr_clr field.
4106 * So ensure that MOUNT_ATTR__ATIME can't be partially set in
4107 * @attr_clr and that @attr_set can't have any atime bits set if
4108 * MOUNT_ATTR__ATIME isn't set in @attr_clr.
4109 */
4110 if (attr->attr_clr & MOUNT_ATTR__ATIME) {
4111 if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
4112 return -EINVAL;
4113
4114 /*
4115 * Clear all previous time settings as they are mutually
4116 * exclusive.
4117 */
4118 kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
4119 switch (attr->attr_set & MOUNT_ATTR__ATIME) {
4120 case MOUNT_ATTR_RELATIME:
4121 kattr->attr_set |= MNT_RELATIME;
4122 break;
4123 case MOUNT_ATTR_NOATIME:
4124 kattr->attr_set |= MNT_NOATIME;
4125 break;
4126 case MOUNT_ATTR_STRICTATIME:
4127 break;
4128 default:
4129 return -EINVAL;
4130 }
4131 } else {
4132 if (attr->attr_set & MOUNT_ATTR__ATIME)
4133 return -EINVAL;
4134 }
4135
Christian Brauner9caccd42021-01-21 14:19:54 +01004136 return build_mount_idmapped(attr, usize, kattr, flags);
4137}
4138
4139static void finish_mount_kattr(struct mount_kattr *kattr)
4140{
4141 put_user_ns(kattr->mnt_userns);
4142 kattr->mnt_userns = NULL;
Christian Brauner2a186722021-01-21 14:19:53 +01004143}
4144
4145SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
4146 unsigned int, flags, struct mount_attr __user *, uattr,
4147 size_t, usize)
4148{
4149 int err;
4150 struct path target;
4151 struct mount_attr attr;
4152 struct mount_kattr kattr;
4153
4154 BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
4155
4156 if (flags & ~(AT_EMPTY_PATH |
4157 AT_RECURSIVE |
4158 AT_SYMLINK_NOFOLLOW |
4159 AT_NO_AUTOMOUNT))
4160 return -EINVAL;
4161
4162 if (unlikely(usize > PAGE_SIZE))
4163 return -E2BIG;
4164 if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
4165 return -EINVAL;
4166
4167 if (!may_mount())
4168 return -EPERM;
4169
4170 err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
4171 if (err)
4172 return err;
4173
4174 /* Don't bother walking through the mounts if this is a nop. */
4175 if (attr.attr_set == 0 &&
4176 attr.attr_clr == 0 &&
4177 attr.propagation == 0)
4178 return 0;
4179
Christian Brauner9caccd42021-01-21 14:19:54 +01004180 err = build_mount_kattr(&attr, usize, &kattr, flags);
Christian Brauner2a186722021-01-21 14:19:53 +01004181 if (err)
4182 return err;
4183
4184 err = user_path_at(dfd, path, kattr.lookup_flags, &target);
4185 if (err)
4186 return err;
4187
4188 err = do_mount_setattr(&target, &kattr);
Christian Brauner9caccd42021-01-21 14:19:54 +01004189 finish_mount_kattr(&kattr);
Christian Brauner2a186722021-01-21 14:19:53 +01004190 path_put(&target);
4191 return err;
4192}
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194static void __init init_mount_tree(void)
4195{
4196 struct vfsmount *mnt;
Al Viro74e83122019-01-30 13:30:21 -05004197 struct mount *m;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08004198 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08004199 struct path root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Al Virofd3e0072019-05-30 17:48:35 -04004201 mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 if (IS_ERR(mnt))
4203 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11004204
Al Viro74e83122019-01-30 13:30:21 -05004205 ns = alloc_mnt_ns(&init_user_ns, false);
Trond Myklebust3b22edc2009-06-23 17:29:49 -04004206 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 panic("Can't allocate initial namespace");
Al Viro74e83122019-01-30 13:30:21 -05004208 m = real_mount(mnt);
4209 m->mnt_ns = ns;
4210 ns->root = m;
4211 ns->mounts = 1;
4212 list_add(&m->mnt_list, &ns->list);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08004213 init_task.nsproxy->mnt_ns = ns;
4214 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Al Virobe08d6d2011-12-06 13:32:36 -05004216 root.mnt = mnt;
4217 root.dentry = mnt->mnt_root;
Eric W. Biedermanda362b02014-10-07 12:19:53 -07004218 mnt->mnt_flags |= MNT_LOCKED;
Jan Blunckac748a02008-02-14 19:34:39 -08004219
4220 set_fs_pwd(current->fs, &root);
4221 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
Denis Cheng74bf17c2007-10-16 23:26:30 -07004224void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Randy Dunlap15a67dd2006-09-29 01:58:57 -07004226 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Al Viro7d6fec42011-11-23 12:14:10 -05004228 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09004229 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Al Viro0818bf22014-02-28 13:46:44 -05004231 mount_hashtable = alloc_large_system_hash("Mount-cache",
Al Viro38129a12014-03-20 21:10:51 -04004232 sizeof(struct hlist_head),
Al Viro0818bf22014-02-28 13:46:44 -05004233 mhash_entries, 19,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07004234 HASH_ZERO,
Al Viro0818bf22014-02-28 13:46:44 -05004235 &m_hash_shift, &m_hash_mask, 0, 0);
4236 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
4237 sizeof(struct hlist_head),
4238 mphash_entries, 19,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07004239 HASH_ZERO,
Al Viro0818bf22014-02-28 13:46:44 -05004240 &mp_hash_shift, &mp_hash_mask, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Al Viro84d17192013-03-15 10:53:28 -04004242 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 panic("Failed to allocate mount hash table\n");
4244
Tejun Heo4b93dc92013-11-28 14:54:43 -05004245 kernfs_init();
4246
Randy Dunlap15a67dd2006-09-29 01:58:57 -07004247 err = sysfs_init();
4248 if (err)
4249 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07004250 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06004251 fs_kobj = kobject_create_and_add("fs", NULL);
4252 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07004253 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Al Viro037f11b2019-06-01 18:09:44 -04004254 shmem_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 init_rootfs();
4256 init_mount_tree();
4257}
4258
Trond Myklebust616511d2009-06-22 15:09:13 -04004259void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Kirill Tkhai1a7b89692020-08-03 13:16:42 +03004261 if (!refcount_dec_and_test(&ns->ns.count))
Trond Myklebust616511d2009-06-22 15:09:13 -04004262 return;
Al Viro7b00ed62013-09-16 21:19:20 -04004263 drop_collected_mounts(&ns->root->mnt);
Eric W. Biederman771b1372012-07-26 21:08:32 -07004264 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
Al Viro9d412a42011-03-17 22:08:28 -04004266
David Howellsd911b452018-11-01 23:07:26 +00004267struct vfsmount *kern_mount(struct file_system_type *type)
Al Viro9d412a42011-03-17 22:08:28 -04004268{
Tim Chen423e0ab2011-07-19 09:32:38 -07004269 struct vfsmount *mnt;
David Howellsd911b452018-11-01 23:07:26 +00004270 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Tim Chen423e0ab2011-07-19 09:32:38 -07004271 if (!IS_ERR(mnt)) {
4272 /*
4273 * it is a longterm mount, don't release mnt until
4274 * we unmount before file sys is unregistered
4275 */
Al Virof7a99c52012-06-09 00:59:08 -04004276 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07004277 }
4278 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04004279}
David Howellsd911b452018-11-01 23:07:26 +00004280EXPORT_SYMBOL_GPL(kern_mount);
Tim Chen423e0ab2011-07-19 09:32:38 -07004281
4282void kern_unmount(struct vfsmount *mnt)
4283{
4284 /* release long term mount so mount point can be released */
4285 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04004286 real_mount(mnt)->mnt_ns = NULL;
Al Viro48a066e2013-09-29 22:06:07 -04004287 synchronize_rcu(); /* yecchhh... */
Tim Chen423e0ab2011-07-19 09:32:38 -07004288 mntput(mnt);
4289 }
4290}
4291EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05004292
Miklos Szeredidf820f82020-06-04 10:48:19 +02004293void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
4294{
4295 unsigned int i;
4296
4297 for (i = 0; i < num; i++)
4298 if (mnt[i])
4299 real_mount(mnt[i])->mnt_ns = NULL;
4300 synchronize_rcu_expedited();
4301 for (i = 0; i < num; i++)
4302 mntput(mnt[i]);
4303}
4304EXPORT_SYMBOL(kern_unmount_array);
4305
Al Viro02125a82011-12-05 08:43:34 -05004306bool our_mnt(struct vfsmount *mnt)
4307{
Al Viro143c8c92011-11-25 00:46:35 -05004308 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05004309}
Eric W. Biederman8823c072010-03-07 18:49:36 -08004310
Eric W. Biederman31515272013-03-15 01:45:51 -07004311bool current_chrooted(void)
4312{
4313 /* Does the current process have a non-standard root */
4314 struct path ns_root;
4315 struct path fs_root;
4316 bool chrooted;
4317
4318 /* Find the namespace root */
4319 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
4320 ns_root.dentry = ns_root.mnt->mnt_root;
4321 path_get(&ns_root);
4322 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
4323 ;
4324
4325 get_fs_root(current->fs, &fs_root);
4326
4327 chrooted = !path_equal(&fs_root, &ns_root);
4328
4329 path_put(&fs_root);
4330 path_put(&ns_root);
4331
4332 return chrooted;
4333}
4334
David Howells132e4602018-11-04 07:43:08 -05004335static bool mnt_already_visible(struct mnt_namespace *ns,
4336 const struct super_block *sb,
Eric W. Biederman8654df42016-06-09 16:06:06 -05004337 int *new_mnt_flags)
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004338{
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05004339 int new_flags = *new_mnt_flags;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004340 struct mount *mnt;
Eric W. Biedermane51db732013-03-30 19:57:41 -07004341 bool visible = false;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004342
Al Viro44bb4382013-09-16 21:37:36 -04004343 down_read(&namespace_sem);
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02004344 lock_ns_list(ns);
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004345 list_for_each_entry(mnt, &ns->list, mnt_list) {
Eric W. Biedermane51db732013-03-30 19:57:41 -07004346 struct mount *child;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004347 int mnt_flags;
4348
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02004349 if (mnt_is_cursor(mnt))
4350 continue;
4351
David Howells132e4602018-11-04 07:43:08 -05004352 if (mnt->mnt.mnt_sb->s_type != sb->s_type)
Eric W. Biedermane51db732013-03-30 19:57:41 -07004353 continue;
4354
Eric W. Biederman7e96c1b2015-05-08 16:36:50 -05004355 /* This mount is not fully visible if it's root directory
4356 * is not the root directory of the filesystem.
4357 */
4358 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
4359 continue;
4360
Eric W. Biedermana1935c12016-06-15 06:59:49 -05004361 /* A local view of the mount flags */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004362 mnt_flags = mnt->mnt.mnt_flags;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004363
Eric W. Biederman695e9df2016-06-10 12:21:40 -05004364 /* Don't miss readonly hidden in the superblock flags */
David Howellsbc98a422017-07-17 08:45:34 +01004365 if (sb_rdonly(mnt->mnt.mnt_sb))
Eric W. Biederman695e9df2016-06-10 12:21:40 -05004366 mnt_flags |= MNT_LOCK_READONLY;
4367
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05004368 /* Verify the mount flags are equal to or more permissive
4369 * than the proposed new mount.
4370 */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004371 if ((mnt_flags & MNT_LOCK_READONLY) &&
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05004372 !(new_flags & MNT_READONLY))
4373 continue;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004374 if ((mnt_flags & MNT_LOCK_ATIME) &&
4375 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05004376 continue;
4377
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06004378 /* This mount is not fully visible if there are any
4379 * locked child mounts that cover anything except for
4380 * empty directories.
Eric W. Biedermane51db732013-03-30 19:57:41 -07004381 */
4382 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
4383 struct inode *inode = child->mnt_mountpoint->d_inode;
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06004384 /* Only worry about locked mounts */
Eric W. Biedermand71ed6c2016-05-27 14:50:05 -05004385 if (!(child->mnt.mnt_flags & MNT_LOCKED))
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06004386 continue;
Eric W. Biederman7236c852015-05-13 20:51:09 -05004387 /* Is the directory permanetly empty? */
4388 if (!is_empty_dir_inode(inode))
Eric W. Biedermane51db732013-03-30 19:57:41 -07004389 goto next;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004390 }
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05004391 /* Preserve the locked attributes */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004392 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
Eric W. Biederman77b1a972015-06-04 09:43:11 -05004393 MNT_LOCK_ATIME);
Eric W. Biedermane51db732013-03-30 19:57:41 -07004394 visible = true;
4395 goto found;
4396 next: ;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004397 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07004398found:
Miklos Szeredi9f6c61f2020-05-14 16:44:24 +02004399 unlock_ns_list(ns);
Al Viro44bb4382013-09-16 21:37:36 -04004400 up_read(&namespace_sem);
Eric W. Biedermane51db732013-03-30 19:57:41 -07004401 return visible;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07004402}
4403
David Howells132e4602018-11-04 07:43:08 -05004404static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
Eric W. Biederman8654df42016-06-09 16:06:06 -05004405{
Eric W. Biedermana1935c12016-06-15 06:59:49 -05004406 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
Eric W. Biederman8654df42016-06-09 16:06:06 -05004407 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
4408 unsigned long s_iflags;
4409
4410 if (ns->user_ns == &init_user_ns)
4411 return false;
4412
4413 /* Can this filesystem be too revealing? */
David Howells132e4602018-11-04 07:43:08 -05004414 s_iflags = sb->s_iflags;
Eric W. Biederman8654df42016-06-09 16:06:06 -05004415 if (!(s_iflags & SB_I_USERNS_VISIBLE))
4416 return false;
4417
Eric W. Biedermana1935c12016-06-15 06:59:49 -05004418 if ((s_iflags & required_iflags) != required_iflags) {
4419 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
4420 required_iflags);
4421 return true;
4422 }
4423
David Howells132e4602018-11-04 07:43:08 -05004424 return !mnt_already_visible(ns, sb, new_mnt_flags);
Eric W. Biederman8654df42016-06-09 16:06:06 -05004425}
4426
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05004427bool mnt_may_suid(struct vfsmount *mnt)
4428{
4429 /*
4430 * Foreign mounts (accessed via fchdir or through /proc
4431 * symlinks) are always treated as if they are nosuid. This
4432 * prevents namespaces from trusting potentially unsafe
4433 * suid/sgid bits, file caps, or security labels that originate
4434 * in other namespaces.
4435 */
4436 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
4437 current_in_userns(mnt->mnt_sb->s_user_ns);
4438}
4439
Al Viro64964522014-11-01 00:37:32 -04004440static struct ns_common *mntns_get(struct task_struct *task)
Eric W. Biederman8823c072010-03-07 18:49:36 -08004441{
Al Viro58be28252014-11-01 00:00:23 -04004442 struct ns_common *ns = NULL;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004443 struct nsproxy *nsproxy;
4444
Eric W. Biederman728dba32014-02-03 19:13:49 -08004445 task_lock(task);
4446 nsproxy = task->nsproxy;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004447 if (nsproxy) {
Al Viro58be28252014-11-01 00:00:23 -04004448 ns = &nsproxy->mnt_ns->ns;
4449 get_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08004450 }
Eric W. Biederman728dba32014-02-03 19:13:49 -08004451 task_unlock(task);
Eric W. Biederman8823c072010-03-07 18:49:36 -08004452
4453 return ns;
4454}
4455
Al Viro64964522014-11-01 00:37:32 -04004456static void mntns_put(struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08004457{
Al Viro58be28252014-11-01 00:00:23 -04004458 put_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08004459}
4460
Christian Braunerf2a8d522020-05-05 16:04:30 +02004461static int mntns_install(struct nsset *nsset, struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08004462{
Christian Braunerf2a8d522020-05-05 16:04:30 +02004463 struct nsproxy *nsproxy = nsset->nsproxy;
4464 struct fs_struct *fs = nsset->fs;
Al Viro4f757f32017-04-15 17:31:22 -04004465 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
Christian Braunerf2a8d522020-05-05 16:04:30 +02004466 struct user_namespace *user_ns = nsset->cred->user_ns;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004467 struct path root;
Al Viro4f757f32017-04-15 17:31:22 -04004468 int err;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004469
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07004470 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Christian Braunerf2a8d522020-05-05 16:04:30 +02004471 !ns_capable(user_ns, CAP_SYS_CHROOT) ||
4472 !ns_capable(user_ns, CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08004473 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004474
Al Viro74e83122019-01-30 13:30:21 -05004475 if (is_anon_ns(mnt_ns))
4476 return -EINVAL;
4477
Eric W. Biederman8823c072010-03-07 18:49:36 -08004478 if (fs->users != 1)
4479 return -EINVAL;
4480
4481 get_mnt_ns(mnt_ns);
Al Viro4f757f32017-04-15 17:31:22 -04004482 old_mnt_ns = nsproxy->mnt_ns;
Eric W. Biederman8823c072010-03-07 18:49:36 -08004483 nsproxy->mnt_ns = mnt_ns;
4484
4485 /* Find the root */
Al Viro4f757f32017-04-15 17:31:22 -04004486 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
4487 "/", LOOKUP_DOWN, &root);
4488 if (err) {
4489 /* revert to old namespace */
4490 nsproxy->mnt_ns = old_mnt_ns;
4491 put_mnt_ns(mnt_ns);
4492 return err;
4493 }
Eric W. Biederman8823c072010-03-07 18:49:36 -08004494
Andrei Vagin40683672017-06-08 17:32:29 -07004495 put_mnt_ns(old_mnt_ns);
4496
Eric W. Biederman8823c072010-03-07 18:49:36 -08004497 /* Update the pwd and root */
4498 set_fs_pwd(fs, &root);
4499 set_fs_root(fs, &root);
4500
4501 path_put(&root);
4502 return 0;
4503}
4504
Andrey Vaginbcac25a2016-09-06 00:47:13 -07004505static struct user_namespace *mntns_owner(struct ns_common *ns)
4506{
4507 return to_mnt_ns(ns)->user_ns;
4508}
4509
Eric W. Biederman8823c072010-03-07 18:49:36 -08004510const struct proc_ns_operations mntns_operations = {
4511 .name = "mnt",
4512 .type = CLONE_NEWNS,
4513 .get = mntns_get,
4514 .put = mntns_put,
4515 .install = mntns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07004516 .owner = mntns_owner,
Eric W. Biederman8823c072010-03-07 18:49:36 -08004517};