blob: 46316ba1561570491a6b641745bb95301421b994 [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>
Al Viro9ea459e2014-08-08 13:08:20 -040028#include <linux/task_work.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010029#include <linux/sched/task.h>
David Howellse262e32d2018-11-01 23:07:23 +000030#include <uapi/linux/mount.h>
David Howells9bc61ab2018-11-04 03:19:03 -050031#include <linux/fs_context.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +010032
Ram Pai07b20882005-11-07 17:19:07 -050033#include "pnode.h"
Adrian Bunk948730b2007-07-15 23:41:25 -070034#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric W. Biedermand2921682016-09-28 00:27:17 -050036/* Maximum number of mounts in a mount namespace */
37unsigned int sysctl_mount_max __read_mostly = 100000;
38
Al Viro0818bf22014-02-28 13:46:44 -050039static unsigned int m_hash_mask __read_mostly;
40static unsigned int m_hash_shift __read_mostly;
41static unsigned int mp_hash_mask __read_mostly;
42static unsigned int mp_hash_shift __read_mostly;
43
44static __initdata unsigned long mhash_entries;
45static int __init set_mhash_entries(char *str)
46{
47 if (!str)
48 return 0;
49 mhash_entries = simple_strtoul(str, &str, 0);
50 return 1;
51}
52__setup("mhash_entries=", set_mhash_entries);
53
54static __initdata unsigned long mphash_entries;
55static int __init set_mphash_entries(char *str)
56{
57 if (!str)
58 return 0;
59 mphash_entries = simple_strtoul(str, &str, 0);
60 return 1;
61}
62__setup("mphash_entries=", set_mphash_entries);
Eric Dumazet13f14b42008-02-06 01:37:57 -080063
Al Viroc7999c32014-02-27 14:40:10 -050064static u64 event;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +010065static DEFINE_IDA(mnt_id_ida);
Miklos Szeredi719f5d72008-03-27 13:06:23 +010066static DEFINE_IDA(mnt_group_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Al Viro38129a12014-03-20 21:10:51 -040068static struct hlist_head *mount_hashtable __read_mostly;
Al Viro0818bf22014-02-28 13:46:44 -050069static struct hlist_head *mountpoint_hashtable __read_mostly;
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *mnt_cache __read_mostly;
Al Viro59aa0da2013-09-16 21:34:53 -040071static DECLARE_RWSEM(namespace_sem);
Al Viro4edbe132019-06-30 10:39:08 -040072static HLIST_HEAD(unmounted); /* protected by namespace_sem */
73static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080075/* /sys/fs */
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -060076struct kobject *fs_kobj;
77EXPORT_SYMBOL_GPL(fs_kobj);
Miklos Szeredif87fd4c2006-01-16 22:14:23 -080078
Nick Piggin99b7db72010-08-18 04:37:39 +100079/*
80 * vfsmount lock may be taken for read to prevent changes to the
81 * vfsmount hash, ie. during mountpoint lookups or walking back
82 * up the tree.
83 *
84 * It should be taken for write in all cases where the vfsmount
85 * tree or hash is modified or when a vfsmount structure is modified.
86 */
Al Viro48a066e2013-09-29 22:06:07 -040087__cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
Nick Piggin99b7db72010-08-18 04:37:39 +100088
Al Viro38129a12014-03-20 21:10:51 -040089static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Ram Paib58fed82005-11-07 17:16:09 -050091 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
92 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Al Viro0818bf22014-02-28 13:46:44 -050093 tmp = tmp + (tmp >> m_hash_shift);
94 return &mount_hashtable[tmp & m_hash_mask];
95}
96
97static inline struct hlist_head *mp_hash(struct dentry *dentry)
98{
99 unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
100 tmp = tmp + (tmp >> mp_hash_shift);
101 return &mountpoint_hashtable[tmp & mp_hash_mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Al Virob105e272011-11-24 20:38:33 -0500104static int mnt_alloc_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100105{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400106 int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100107
Matthew Wilcox169b4802018-06-11 12:31:36 -0400108 if (res < 0)
109 return res;
110 mnt->mnt_id = res;
111 return 0;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100112}
113
Al Virob105e272011-11-24 20:38:33 -0500114static void mnt_free_id(struct mount *mnt)
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100115{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400116 ida_free(&mnt_id_ida, mnt->mnt_id);
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100117}
118
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100119/*
120 * Allocate a new peer group ID
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100121 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500122static int mnt_alloc_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100123{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400124 int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
Al Virof21f6222009-06-24 03:12:00 -0400125
Matthew Wilcox169b4802018-06-11 12:31:36 -0400126 if (res < 0)
127 return res;
128 mnt->mnt_group_id = res;
129 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100130}
131
132/*
133 * Release a peer group ID
134 */
Al Viro4b8b21f2011-11-24 19:54:23 -0500135void mnt_release_group_id(struct mount *mnt)
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100136{
Matthew Wilcox169b4802018-06-11 12:31:36 -0400137 ida_free(&mnt_group_ida, mnt->mnt_group_id);
Al Viro15169fe2011-11-25 00:50:41 -0500138 mnt->mnt_group_id = 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +0100139}
140
Nick Pigginb3e19d92011-01-07 17:50:11 +1100141/*
142 * vfsmount lock must be held for read
143 */
Al Viro83adc752011-11-24 22:37:54 -0500144static inline void mnt_add_count(struct mount *mnt, int n)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100145{
146#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500147 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100148#else
149 preempt_disable();
Al Viro68e8a9f2011-11-24 22:53:09 -0500150 mnt->mnt_count += n;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100151 preempt_enable();
152#endif
153}
154
Nick Pigginb3e19d92011-01-07 17:50:11 +1100155/*
156 * vfsmount lock must be held for write
157 */
Al Viro83adc752011-11-24 22:37:54 -0500158unsigned int mnt_get_count(struct mount *mnt)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100159{
160#ifdef CONFIG_SMP
Al Virof03c6592011-01-14 22:30:21 -0500161 unsigned int count = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100162 int cpu;
163
164 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500165 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100166 }
167
168 return count;
169#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500170 return mnt->mnt_count;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100171#endif
172}
173
Al Viro87b95ce2015-01-10 19:01:08 -0500174static void drop_mountpoint(struct fs_pin *p)
175{
176 struct mount *m = container_of(p, struct mount, mnt_umount);
Al Viro87b95ce2015-01-10 19:01:08 -0500177 pin_remove(p);
178 mntput(&m->mnt);
179}
180
Al Virob105e272011-11-24 20:38:33 -0500181static struct mount *alloc_vfsmnt(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Al Viroc63181e2011-11-25 02:35:16 -0500183 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
184 if (mnt) {
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100185 int err;
186
Al Viroc63181e2011-11-25 02:35:16 -0500187 err = mnt_alloc_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800188 if (err)
189 goto out_free_cache;
190
191 if (name) {
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800192 mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
Al Viroc63181e2011-11-25 02:35:16 -0500193 if (!mnt->mnt_devname)
Li Zefan88b38782008-07-21 18:06:36 +0800194 goto out_free_id;
Miklos Szeredi73cd49e2008-03-26 22:11:34 +0100195 }
196
Nick Pigginb3e19d92011-01-07 17:50:11 +1100197#ifdef CONFIG_SMP
Al Viroc63181e2011-11-25 02:35:16 -0500198 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
199 if (!mnt->mnt_pcp)
Nick Pigginb3e19d92011-01-07 17:50:11 +1100200 goto out_free_devname;
201
Al Viroc63181e2011-11-25 02:35:16 -0500202 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +1100203#else
Al Viroc63181e2011-11-25 02:35:16 -0500204 mnt->mnt_count = 1;
205 mnt->mnt_writers = 0;
Nick Pigginb3e19d92011-01-07 17:50:11 +1100206#endif
207
Al Viro38129a12014-03-20 21:10:51 -0400208 INIT_HLIST_NODE(&mnt->mnt_hash);
Al Viroc63181e2011-11-25 02:35:16 -0500209 INIT_LIST_HEAD(&mnt->mnt_child);
210 INIT_LIST_HEAD(&mnt->mnt_mounts);
211 INIT_LIST_HEAD(&mnt->mnt_list);
212 INIT_LIST_HEAD(&mnt->mnt_expire);
213 INIT_LIST_HEAD(&mnt->mnt_share);
214 INIT_LIST_HEAD(&mnt->mnt_slave_list);
215 INIT_LIST_HEAD(&mnt->mnt_slave);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700216 INIT_HLIST_NODE(&mnt->mnt_mp_list);
Eric W. Biederman99b19d12016-10-24 16:16:13 -0500217 INIT_LIST_HEAD(&mnt->mnt_umounting);
Al Viro87b95ce2015-01-10 19:01:08 -0500218 init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Al Viroc63181e2011-11-25 02:35:16 -0500220 return mnt;
Li Zefan88b38782008-07-21 18:06:36 +0800221
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000222#ifdef CONFIG_SMP
223out_free_devname:
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800224 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000225#endif
Li Zefan88b38782008-07-21 18:06:36 +0800226out_free_id:
Al Viroc63181e2011-11-25 02:35:16 -0500227 mnt_free_id(mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800228out_free_cache:
Al Viroc63181e2011-11-25 02:35:16 -0500229 kmem_cache_free(mnt_cache, mnt);
Li Zefan88b38782008-07-21 18:06:36 +0800230 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Dave Hansen83660252008-02-15 14:37:30 -0800233/*
234 * Most r/o checks on a fs are for operations that take
235 * discrete amounts of time, like a write() or unlink().
236 * We must keep track of when those operations start
237 * (for permission checks) and when they end, so that
238 * we can determine when writes are able to occur to
239 * a filesystem.
240 */
Dave Hansen3d733632008-02-15 14:37:59 -0800241/*
242 * __mnt_is_readonly: check whether a mount is read-only
243 * @mnt: the mount to check for its write status
244 *
245 * This shouldn't be used directly ouside of the VFS.
246 * It does not guarantee that the filesystem will stay
247 * r/w, just that it is right *now*. This can not and
248 * should not be used in place of IS_RDONLY(inode).
249 * mnt_want/drop_write() will _keep_ the filesystem
250 * r/w.
251 */
David Howells43f5e652018-11-01 23:07:25 +0000252bool __mnt_is_readonly(struct vfsmount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800253{
David Howells43f5e652018-11-01 23:07:25 +0000254 return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800255}
256EXPORT_SYMBOL_GPL(__mnt_is_readonly);
257
Al Viro83adc752011-11-24 22:37:54 -0500258static inline void mnt_inc_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800259{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000260#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500261 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000262#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500263 mnt->mnt_writers++;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000264#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800265}
Dave Hansen3d733632008-02-15 14:37:59 -0800266
Al Viro83adc752011-11-24 22:37:54 -0500267static inline void mnt_dec_writers(struct mount *mnt)
Dave Hansen3d733632008-02-15 14:37:59 -0800268{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000269#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500270 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000271#else
Al Viro68e8a9f2011-11-24 22:53:09 -0500272 mnt->mnt_writers--;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000273#endif
274}
275
Al Viro83adc752011-11-24 22:37:54 -0500276static unsigned int mnt_get_writers(struct mount *mnt)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000277{
278#ifdef CONFIG_SMP
279 unsigned int count = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800280 int cpu;
Dave Hansen3d733632008-02-15 14:37:59 -0800281
282 for_each_possible_cpu(cpu) {
Al Viro68e8a9f2011-11-24 22:53:09 -0500283 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
Dave Hansen3d733632008-02-15 14:37:59 -0800284 }
Dave Hansen3d733632008-02-15 14:37:59 -0800285
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000286 return count;
287#else
288 return mnt->mnt_writers;
289#endif
Dave Hansen3d733632008-02-15 14:37:59 -0800290}
291
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100292static int mnt_is_readonly(struct vfsmount *mnt)
293{
294 if (mnt->mnt_sb->s_readonly_remount)
295 return 1;
296 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
297 smp_rmb();
298 return __mnt_is_readonly(mnt);
299}
300
Dave Hansen3d733632008-02-15 14:37:59 -0800301/*
Jan Karaeb04c282012-06-12 16:20:35 +0200302 * Most r/o & frozen checks on a fs are for operations that take discrete
303 * amounts of time, like a write() or unlink(). We must keep track of when
304 * those operations start (for permission checks) and when they end, so that we
305 * can determine when writes are able to occur to a filesystem.
Dave Hansen3d733632008-02-15 14:37:59 -0800306 */
Dave Hansen83660252008-02-15 14:37:30 -0800307/**
Jan Karaeb04c282012-06-12 16:20:35 +0200308 * __mnt_want_write - get write access to a mount without freeze protection
Al Viro83adc752011-11-24 22:37:54 -0500309 * @m: the mount on which to take a write
Dave Hansen83660252008-02-15 14:37:30 -0800310 *
Jan Karaeb04c282012-06-12 16:20:35 +0200311 * This tells the low-level filesystem that a write is about to be performed to
312 * it, and makes sure that writes are allowed (mnt it read-write) before
313 * returning success. This operation does not protect against filesystem being
314 * frozen. When the write operation is finished, __mnt_drop_write() must be
315 * called. This is effectively a refcount.
Dave Hansen83660252008-02-15 14:37:30 -0800316 */
Jan Karaeb04c282012-06-12 16:20:35 +0200317int __mnt_want_write(struct vfsmount *m)
Dave Hansen83660252008-02-15 14:37:30 -0800318{
Al Viro83adc752011-11-24 22:37:54 -0500319 struct mount *mnt = real_mount(m);
Dave Hansen3d733632008-02-15 14:37:59 -0800320 int ret = 0;
Dave Hansen3d733632008-02-15 14:37:59 -0800321
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000322 preempt_disable();
Nick Pigginc6653a82011-01-07 17:50:10 +1100323 mnt_inc_writers(mnt);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000324 /*
Nick Pigginc6653a82011-01-07 17:50:10 +1100325 * The store to mnt_inc_writers must be visible before we pass
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000326 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
327 * incremented count after it has set MNT_WRITE_HOLD.
328 */
329 smp_mb();
Mark Rutland6aa7de02017-10-23 14:07:29 -0700330 while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000331 cpu_relax();
332 /*
333 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
334 * be set to match its requirements. So we must not load that until
335 * MNT_WRITE_HOLD is cleared.
336 */
337 smp_rmb();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100338 if (mnt_is_readonly(m)) {
Nick Pigginc6653a82011-01-07 17:50:10 +1100339 mnt_dec_writers(mnt);
Dave Hansen3d733632008-02-15 14:37:59 -0800340 ret = -EROFS;
Dave Hansen3d733632008-02-15 14:37:59 -0800341 }
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000342 preempt_enable();
Jan Karaeb04c282012-06-12 16:20:35 +0200343
344 return ret;
345}
346
347/**
348 * mnt_want_write - get write access to a mount
349 * @m: the mount on which to take a write
350 *
351 * This tells the low-level filesystem that a write is about to be performed to
352 * it, and makes sure that writes are allowed (mount is read-write, filesystem
353 * is not frozen) before returning success. When the write operation is
354 * finished, mnt_drop_write() must be called. This is effectively a refcount.
355 */
356int mnt_want_write(struct vfsmount *m)
357{
358 int ret;
359
360 sb_start_write(m->mnt_sb);
361 ret = __mnt_want_write(m);
362 if (ret)
363 sb_end_write(m->mnt_sb);
Dave Hansen3d733632008-02-15 14:37:59 -0800364 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800365}
366EXPORT_SYMBOL_GPL(mnt_want_write);
367
368/**
npiggin@suse.de96029c42009-04-26 20:25:55 +1000369 * mnt_clone_write - get write access to a mount
370 * @mnt: the mount on which to take a write
371 *
372 * This is effectively like mnt_want_write, except
373 * it must only be used to take an extra write reference
374 * on a mountpoint that we already know has a write reference
375 * on it. This allows some optimisation.
376 *
377 * After finished, mnt_drop_write must be called as usual to
378 * drop the reference.
379 */
380int mnt_clone_write(struct vfsmount *mnt)
381{
382 /* superblock may be r/o */
383 if (__mnt_is_readonly(mnt))
384 return -EROFS;
385 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500386 mnt_inc_writers(real_mount(mnt));
npiggin@suse.de96029c42009-04-26 20:25:55 +1000387 preempt_enable();
388 return 0;
389}
390EXPORT_SYMBOL_GPL(mnt_clone_write);
391
392/**
Jan Karaeb04c282012-06-12 16:20:35 +0200393 * __mnt_want_write_file - get write access to a file's mount
394 * @file: the file who's mount on which to take a write
395 *
396 * This is like __mnt_want_write, but it takes a file and can
397 * do some optimisations if the file is open for write already
398 */
399int __mnt_want_write_file(struct file *file)
400{
Al Viro83f936c2014-03-14 12:02:47 -0400401 if (!(file->f_mode & FMODE_WRITER))
Jan Karaeb04c282012-06-12 16:20:35 +0200402 return __mnt_want_write(file->f_path.mnt);
403 else
404 return mnt_clone_write(file->f_path.mnt);
405}
406
407/**
Miklos Szeredi7c6893e2017-09-05 12:53:12 +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
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200413 */
414int mnt_want_write_file(struct file *file)
415{
416 int ret;
417
Miklos Szeredia6795a52018-07-18 15:44:43 +0200418 sb_start_write(file_inode(file)->i_sb);
Jan Karaeb04c282012-06-12 16:20:35 +0200419 ret = __mnt_want_write_file(file);
420 if (ret)
Miklos Szeredia6795a52018-07-18 15:44:43 +0200421 sb_end_write(file_inode(file)->i_sb);
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200422 return ret;
423}
npiggin@suse.de96029c42009-04-26 20:25:55 +1000424EXPORT_SYMBOL_GPL(mnt_want_write_file);
425
426/**
Jan Karaeb04c282012-06-12 16:20:35 +0200427 * __mnt_drop_write - give up write access to a mount
Dave Hansen83660252008-02-15 14:37:30 -0800428 * @mnt: the mount on which to give up write access
429 *
430 * Tells the low-level filesystem that we are done
431 * performing writes to it. Must be matched with
Jan Karaeb04c282012-06-12 16:20:35 +0200432 * __mnt_want_write() call above.
Dave Hansen83660252008-02-15 14:37:30 -0800433 */
Jan Karaeb04c282012-06-12 16:20:35 +0200434void __mnt_drop_write(struct vfsmount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800435{
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000436 preempt_disable();
Al Viro83adc752011-11-24 22:37:54 -0500437 mnt_dec_writers(real_mount(mnt));
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000438 preempt_enable();
Dave Hansen83660252008-02-15 14:37:30 -0800439}
Jan Karaeb04c282012-06-12 16:20:35 +0200440
441/**
442 * mnt_drop_write - give up write access to a mount
443 * @mnt: the mount on which to give up write access
444 *
445 * Tells the low-level filesystem that we are done performing writes to it and
446 * also allows filesystem to be frozen again. Must be matched with
447 * mnt_want_write() call above.
448 */
449void mnt_drop_write(struct vfsmount *mnt)
450{
451 __mnt_drop_write(mnt);
452 sb_end_write(mnt->mnt_sb);
453}
Dave Hansen83660252008-02-15 14:37:30 -0800454EXPORT_SYMBOL_GPL(mnt_drop_write);
455
Jan Karaeb04c282012-06-12 16:20:35 +0200456void __mnt_drop_write_file(struct file *file)
457{
458 __mnt_drop_write(file->f_path.mnt);
459}
460
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200461void mnt_drop_write_file(struct file *file)
462{
Miklos Szeredia6795a52018-07-18 15:44:43 +0200463 __mnt_drop_write_file(file);
Miklos Szeredi7c6893e2017-09-05 12:53:12 +0200464 sb_end_write(file_inode(file)->i_sb);
465}
Al Viro2a79f172011-12-09 08:06:57 -0500466EXPORT_SYMBOL(mnt_drop_write_file);
467
Al Viro83adc752011-11-24 22:37:54 -0500468static int mnt_make_readonly(struct mount *mnt)
Dave Hansen83660252008-02-15 14:37:30 -0800469{
Dave Hansen3d733632008-02-15 14:37:59 -0800470 int ret = 0;
471
Al Viro719ea2f2013-09-29 11:24:49 -0400472 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500473 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000474 /*
475 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
476 * should be visible before we do.
477 */
478 smp_mb();
479
480 /*
481 * With writers on hold, if this value is zero, then there are
482 * definitely no active writers (although held writers may subsequently
483 * increment the count, they'll have to wait, and decrement it after
484 * seeing MNT_READONLY).
485 *
486 * It is OK to have counter incremented on one CPU and decremented on
487 * another: the sum will add up correctly. The danger would be when we
488 * sum up each counter, if we read a counter before it is incremented,
489 * but then read another CPU's count which it has been subsequently
490 * decremented from -- we would see more decrements than we should.
491 * MNT_WRITE_HOLD protects against this scenario, because
492 * mnt_want_write first increments count, then smp_mb, then spins on
493 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
494 * we're counting up here.
495 */
Nick Pigginc6653a82011-01-07 17:50:10 +1100496 if (mnt_get_writers(mnt) > 0)
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000497 ret = -EBUSY;
498 else
Al Viro83adc752011-11-24 22:37:54 -0500499 mnt->mnt.mnt_flags |= MNT_READONLY;
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000500 /*
501 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
502 * that become unheld will see MNT_READONLY.
503 */
504 smp_wmb();
Al Viro83adc752011-11-24 22:37:54 -0500505 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
Al Viro719ea2f2013-09-29 11:24:49 -0400506 unlock_mount_hash();
Dave Hansen3d733632008-02-15 14:37:59 -0800507 return ret;
Dave Hansen83660252008-02-15 14:37:30 -0800508}
Dave Hansen83660252008-02-15 14:37:30 -0800509
David Howells43f5e652018-11-01 23:07:25 +0000510static int __mnt_unmake_readonly(struct mount *mnt)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800511{
Al Viro719ea2f2013-09-29 11:24:49 -0400512 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -0500513 mnt->mnt.mnt_flags &= ~MNT_READONLY;
Al Viro719ea2f2013-09-29 11:24:49 -0400514 unlock_mount_hash();
David Howells43f5e652018-11-01 23:07:25 +0000515 return 0;
Dave Hansen2e4b7fc2008-02-15 14:38:00 -0800516}
517
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100518int sb_prepare_remount_readonly(struct super_block *sb)
519{
520 struct mount *mnt;
521 int err = 0;
522
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100523 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
524 if (atomic_long_read(&sb->s_remove_count))
525 return -EBUSY;
526
Al Viro719ea2f2013-09-29 11:24:49 -0400527 lock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100528 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
529 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
530 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
531 smp_mb();
532 if (mnt_get_writers(mnt) > 0) {
533 err = -EBUSY;
534 break;
535 }
536 }
537 }
Miklos Szeredi8e8b8792011-11-21 12:11:33 +0100538 if (!err && atomic_long_read(&sb->s_remove_count))
539 err = -EBUSY;
540
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100541 if (!err) {
542 sb->s_readonly_remount = 1;
543 smp_wmb();
544 }
545 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
546 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
547 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
548 }
Al Viro719ea2f2013-09-29 11:24:49 -0400549 unlock_mount_hash();
Miklos Szeredi4ed5e82f2011-11-21 12:11:31 +0100550
551 return err;
552}
553
Al Virob105e272011-11-24 20:38:33 -0500554static void free_vfsmnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Andrzej Hajdafcc139a2015-02-13 14:36:41 -0800556 kfree_const(mnt->mnt_devname);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000557#ifdef CONFIG_SMP
Al Viro68e8a9f2011-11-24 22:53:09 -0500558 free_percpu(mnt->mnt_pcp);
npiggin@suse.ded3ef3d72009-04-26 20:25:54 +1000559#endif
Al Virob105e272011-11-24 20:38:33 -0500560 kmem_cache_free(mnt_cache, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
David Howells8ffcb322014-01-24 12:17:54 +0000563static void delayed_free_vfsmnt(struct rcu_head *head)
564{
565 free_vfsmnt(container_of(head, struct mount, mnt_rcu));
566}
567
Al Viro48a066e2013-09-29 22:06:07 -0400568/* call under rcu_read_lock */
Al Viro294d71f2015-05-08 11:43:53 -0400569int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
Al Viro48a066e2013-09-29 22:06:07 -0400570{
571 struct mount *mnt;
572 if (read_seqretry(&mount_lock, seq))
Al Viro294d71f2015-05-08 11:43:53 -0400573 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400574 if (bastard == NULL)
Al Viro294d71f2015-05-08 11:43:53 -0400575 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400576 mnt = real_mount(bastard);
577 mnt_add_count(mnt, 1);
Al Viro119e1ef2018-08-09 17:51:32 -0400578 smp_mb(); // see mntput_no_expire()
Al Viro48a066e2013-09-29 22:06:07 -0400579 if (likely(!read_seqretry(&mount_lock, seq)))
Al Viro294d71f2015-05-08 11:43:53 -0400580 return 0;
Al Viro48a066e2013-09-29 22:06:07 -0400581 if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
582 mnt_add_count(mnt, -1);
Al Viro294d71f2015-05-08 11:43:53 -0400583 return 1;
Al Viro48a066e2013-09-29 22:06:07 -0400584 }
Al Viro119e1ef2018-08-09 17:51:32 -0400585 lock_mount_hash();
586 if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
587 mnt_add_count(mnt, -1);
588 unlock_mount_hash();
589 return 1;
590 }
591 unlock_mount_hash();
592 /* caller will mntput() */
Al Viro294d71f2015-05-08 11:43:53 -0400593 return -1;
594}
595
596/* call under rcu_read_lock */
597bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
598{
599 int res = __legitimize_mnt(bastard, seq);
600 if (likely(!res))
601 return true;
602 if (unlikely(res < 0)) {
603 rcu_read_unlock();
604 mntput(bastard);
605 rcu_read_lock();
606 }
Al Viro48a066e2013-09-29 22:06:07 -0400607 return false;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
Al Viro474279d2013-10-01 16:11:26 -0400611 * find the first mount at @dentry on vfsmount @mnt.
Al Viro48a066e2013-09-29 22:06:07 -0400612 * call under rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Al Viro474279d2013-10-01 16:11:26 -0400614struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Al Viro38129a12014-03-20 21:10:51 -0400616 struct hlist_head *head = m_hash(mnt, dentry);
Al Viro474279d2013-10-01 16:11:26 -0400617 struct mount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Al Viro38129a12014-03-20 21:10:51 -0400619 hlist_for_each_entry_rcu(p, head, mnt_hash)
Al Viro474279d2013-10-01 16:11:26 -0400620 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
621 return p;
622 return NULL;
623}
624
625/*
David Howellsf015f1262012-06-25 12:55:28 +0100626 * lookup_mnt - Return the first child mount mounted at path
627 *
628 * "First" means first mounted chronologically. If you create the
629 * following mounts:
630 *
631 * mount /dev/sda1 /mnt
632 * mount /dev/sda2 /mnt
633 * mount /dev/sda3 /mnt
634 *
635 * Then lookup_mnt() on the base /mnt dentry in the root mount will
636 * return successively the root dentry and vfsmount of /dev/sda1, then
637 * /dev/sda2, then /dev/sda3, then NULL.
638 *
639 * lookup_mnt takes a reference to the found vfsmount.
Ram Paia05964f2005-11-07 17:20:17 -0500640 */
Al Viroca71cf72016-11-20 19:45:28 -0500641struct vfsmount *lookup_mnt(const struct path *path)
Ram Paia05964f2005-11-07 17:20:17 -0500642{
Al Viroc7105362011-11-24 18:22:03 -0500643 struct mount *child_mnt;
Al Viro48a066e2013-09-29 22:06:07 -0400644 struct vfsmount *m;
645 unsigned seq;
Nick Piggin99b7db72010-08-18 04:37:39 +1000646
Al Viro48a066e2013-09-29 22:06:07 -0400647 rcu_read_lock();
648 do {
649 seq = read_seqbegin(&mount_lock);
650 child_mnt = __lookup_mnt(path->mnt, path->dentry);
651 m = child_mnt ? &child_mnt->mnt : NULL;
652 } while (!legitimize_mnt(m, seq));
653 rcu_read_unlock();
654 return m;
Ram Paia05964f2005-11-07 17:20:17 -0500655}
656
Eric W. Biederman7af13642013-10-04 19:15:13 -0700657/*
658 * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
659 * current mount namespace.
660 *
661 * The common case is dentries are not mountpoints at all and that
662 * test is handled inline. For the slow case when we are actually
663 * dealing with a mountpoint of some kind, walk through all of the
664 * mounts in the current mount namespace and test to see if the dentry
665 * is a mountpoint.
666 *
667 * The mount_hashtable is not usable in the context because we
668 * need to identify all mounts that may be in the current mount
669 * namespace not just a mount that happens to have some specified
670 * parent mount.
671 */
672bool __is_local_mountpoint(struct dentry *dentry)
673{
674 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
675 struct mount *mnt;
676 bool is_covered = false;
677
678 if (!d_mountpoint(dentry))
679 goto out;
680
681 down_read(&namespace_sem);
682 list_for_each_entry(mnt, &ns->list, mnt_list) {
683 is_covered = (mnt->mnt_mountpoint == dentry);
684 if (is_covered)
685 break;
686 }
687 up_read(&namespace_sem);
688out:
689 return is_covered;
690}
691
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800692static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
Al Viro84d17192013-03-15 10:53:28 -0400693{
Al Viro0818bf22014-02-28 13:46:44 -0500694 struct hlist_head *chain = mp_hash(dentry);
Al Viro84d17192013-03-15 10:53:28 -0400695 struct mountpoint *mp;
696
Al Viro0818bf22014-02-28 13:46:44 -0500697 hlist_for_each_entry(mp, chain, m_hash) {
Al Viro84d17192013-03-15 10:53:28 -0400698 if (mp->m_dentry == dentry) {
Al Viro84d17192013-03-15 10:53:28 -0400699 mp->m_count++;
700 return mp;
701 }
702 }
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800703 return NULL;
704}
705
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300706static struct mountpoint *get_mountpoint(struct dentry *dentry)
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800707{
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300708 struct mountpoint *mp, *new = NULL;
Eric W. Biedermane2dfa932014-02-24 17:32:34 -0800709 int ret;
Al Viro84d17192013-03-15 10:53:28 -0400710
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300711 if (d_mountpoint(dentry)) {
Benjamin Coddington1e9c75f2018-10-03 10:18:33 -0400712 /* might be worth a WARN_ON() */
713 if (d_unlinked(dentry))
714 return ERR_PTR(-ENOENT);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300715mountpoint:
716 read_seqlock_excl(&mount_lock);
717 mp = lookup_mountpoint(dentry);
718 read_sequnlock_excl(&mount_lock);
719 if (mp)
720 goto done;
Al Viro84d17192013-03-15 10:53:28 -0400721 }
Miklos Szeredieed81002013-09-05 14:39:11 +0200722
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300723 if (!new)
724 new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
725 if (!new)
726 return ERR_PTR(-ENOMEM);
727
728
729 /* Exactly one processes may set d_mounted */
730 ret = d_set_mounted(dentry);
731
732 /* Someone else set d_mounted? */
733 if (ret == -EBUSY)
734 goto mountpoint;
735
736 /* The dentry is not available as a mountpoint? */
737 mp = ERR_PTR(ret);
738 if (ret)
739 goto done;
740
741 /* Add the new mountpoint to the hash table */
742 read_seqlock_excl(&mount_lock);
Al Viro4edbe132019-06-30 10:39:08 -0400743 new->m_dentry = dget(dentry);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +1300744 new->m_count = 1;
745 hlist_add_head(&new->m_hash, mp_hash(dentry));
746 INIT_HLIST_HEAD(&new->m_list);
747 read_sequnlock_excl(&mount_lock);
748
749 mp = new;
750 new = NULL;
751done:
752 kfree(new);
Al Viro84d17192013-03-15 10:53:28 -0400753 return mp;
754}
755
Al Viro4edbe132019-06-30 10:39:08 -0400756/*
757 * vfsmount lock must be held. Additionally, the caller is responsible
758 * for serializing calls for given disposal list.
759 */
760static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
Al Viro84d17192013-03-15 10:53:28 -0400761{
762 if (!--mp->m_count) {
763 struct dentry *dentry = mp->m_dentry;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700764 BUG_ON(!hlist_empty(&mp->m_list));
Al Viro84d17192013-03-15 10:53:28 -0400765 spin_lock(&dentry->d_lock);
766 dentry->d_flags &= ~DCACHE_MOUNTED;
767 spin_unlock(&dentry->d_lock);
Al Viro4edbe132019-06-30 10:39:08 -0400768 dput_to_list(dentry, list);
Al Viro0818bf22014-02-28 13:46:44 -0500769 hlist_del(&mp->m_hash);
Al Viro84d17192013-03-15 10:53:28 -0400770 kfree(mp);
771 }
772}
773
Al Viro4edbe132019-06-30 10:39:08 -0400774/* called with namespace_lock and vfsmount lock */
775static void put_mountpoint(struct mountpoint *mp)
776{
777 __put_mountpoint(mp, &ex_mountpoints);
778}
779
Al Viro143c8c92011-11-25 00:46:35 -0500780static inline int check_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800782 return mnt->mnt_ns == current->nsproxy->mnt_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Nick Piggin99b7db72010-08-18 04:37:39 +1000785/*
786 * vfsmount lock must be held for write
787 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800788static void touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500789{
790 if (ns) {
791 ns->event = ++event;
792 wake_up_interruptible(&ns->poll);
793 }
794}
795
Nick Piggin99b7db72010-08-18 04:37:39 +1000796/*
797 * vfsmount lock must be held for write
798 */
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800799static void __touch_mnt_namespace(struct mnt_namespace *ns)
Al Viro5addc5d2005-11-07 17:15:49 -0500800{
801 if (ns && ns->event != event) {
802 ns->event = event;
803 wake_up_interruptible(&ns->poll);
804 }
805}
806
Nick Piggin99b7db72010-08-18 04:37:39 +1000807/*
808 * vfsmount lock must be held for write
809 */
Al Viroe4e59902019-06-29 12:58:42 -0400810static struct mountpoint *unhash_mnt(struct mount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Al Viroe4e59902019-06-29 12:58:42 -0400812 struct mountpoint *mp;
Al Viro0714a532011-11-24 22:19:58 -0500813 mnt->mnt_parent = mnt;
Al Viroa73324d2011-11-24 22:25:07 -0500814 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
Al Viro6b41d532011-11-24 23:24:33 -0500815 list_del_init(&mnt->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -0400816 hlist_del_init_rcu(&mnt->mnt_hash);
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700817 hlist_del_init(&mnt->mnt_mp_list);
Al Viroe4e59902019-06-29 12:58:42 -0400818 mp = mnt->mnt_mp;
Al Viro84d17192013-03-15 10:53:28 -0400819 mnt->mnt_mp = NULL;
Al Viroe4e59902019-06-29 12:58:42 -0400820 return mp;
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 */
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600826static void umount_mnt(struct mount *mnt)
827{
Al Viroe4e59902019-06-29 12:58:42 -0400828 put_mountpoint(unhash_mnt(mnt));
Eric W. Biederman6a46c572015-01-15 22:58:33 -0600829}
830
831/*
832 * vfsmount lock must be held for write
833 */
Al Viro84d17192013-03-15 10:53:28 -0400834void mnt_set_mountpoint(struct mount *mnt,
835 struct mountpoint *mp,
Al Viro44d964d62011-11-24 21:28:22 -0500836 struct mount *child_mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500837{
Al Viro84d17192013-03-15 10:53:28 -0400838 mp->m_count++;
Al Viro3a2393d2011-11-25 03:19:09 -0500839 mnt_add_count(mnt, 1); /* essentially, that's mntget */
Al Viro4edbe132019-06-30 10:39:08 -0400840 child_mnt->mnt_mountpoint = mp->m_dentry;
Al Viro3a2393d2011-11-25 03:19:09 -0500841 child_mnt->mnt_parent = mnt;
Al Viro84d17192013-03-15 10:53:28 -0400842 child_mnt->mnt_mp = mp;
Eric W. Biederman0a5eb7c2013-09-22 19:37:01 -0700843 hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
Ram Paib90fa9a2005-11-07 17:19:50 -0500844}
845
Eric W. Biederman1064f872017-01-20 18:28:35 +1300846static void __attach_mnt(struct mount *mnt, struct mount *parent)
847{
848 hlist_add_head_rcu(&mnt->mnt_hash,
849 m_hash(&parent->mnt, mnt->mnt_mountpoint));
850 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
851}
852
Nick Piggin99b7db72010-08-18 04:37:39 +1000853/*
854 * vfsmount lock must be held for write
855 */
Al Viro84d17192013-03-15 10:53:28 -0400856static void attach_mnt(struct mount *mnt,
857 struct mount *parent,
858 struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Al Viro84d17192013-03-15 10:53:28 -0400860 mnt_set_mountpoint(parent, mp, mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +1300861 __attach_mnt(mnt, parent);
Ram Paib90fa9a2005-11-07 17:19:50 -0500862}
863
Eric W. Biederman1064f872017-01-20 18:28:35 +1300864void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
Al Viro12a5b522014-08-10 03:44:55 -0400865{
Eric W. Biederman1064f872017-01-20 18:28:35 +1300866 struct mountpoint *old_mp = mnt->mnt_mp;
Eric W. Biederman1064f872017-01-20 18:28:35 +1300867 struct mount *old_parent = mnt->mnt_parent;
868
869 list_del_init(&mnt->mnt_child);
870 hlist_del_init(&mnt->mnt_mp_list);
871 hlist_del_init_rcu(&mnt->mnt_hash);
872
873 attach_mnt(mnt, parent, mp);
874
875 put_mountpoint(old_mp);
Eric W. Biederman1064f872017-01-20 18:28:35 +1300876 mnt_add_count(old_parent, -1);
Al Viro12a5b522014-08-10 03:44:55 -0400877}
878
Ram Paib90fa9a2005-11-07 17:19:50 -0500879/*
Nick Piggin99b7db72010-08-18 04:37:39 +1000880 * vfsmount lock must be held for write
Ram Paib90fa9a2005-11-07 17:19:50 -0500881 */
Eric W. Biederman1064f872017-01-20 18:28:35 +1300882static void commit_tree(struct mount *mnt)
Ram Paib90fa9a2005-11-07 17:19:50 -0500883{
Al Viro0714a532011-11-24 22:19:58 -0500884 struct mount *parent = mnt->mnt_parent;
Al Viro83adc752011-11-24 22:37:54 -0500885 struct mount *m;
Ram Paib90fa9a2005-11-07 17:19:50 -0500886 LIST_HEAD(head);
Al Viro143c8c92011-11-25 00:46:35 -0500887 struct mnt_namespace *n = parent->mnt_ns;
Ram Paib90fa9a2005-11-07 17:19:50 -0500888
Al Viro0714a532011-11-24 22:19:58 -0500889 BUG_ON(parent == mnt);
Ram Paib90fa9a2005-11-07 17:19:50 -0500890
Al Viro1a4eeaf2011-11-25 02:19:55 -0500891 list_add_tail(&head, &mnt->mnt_list);
Al Virof7a99c52012-06-09 00:59:08 -0400892 list_for_each_entry(m, &head, mnt_list)
Al Viro143c8c92011-11-25 00:46:35 -0500893 m->mnt_ns = n;
Al Virof03c6592011-01-14 22:30:21 -0500894
Ram Paib90fa9a2005-11-07 17:19:50 -0500895 list_splice(&head, n->list.prev);
896
Eric W. Biedermand2921682016-09-28 00:27:17 -0500897 n->mounts += n->pending_mounts;
898 n->pending_mounts = 0;
899
Eric W. Biederman1064f872017-01-20 18:28:35 +1300900 __attach_mnt(mnt, parent);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800901 touch_mnt_namespace(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Al Viro909b0a82011-11-25 03:06:56 -0500904static struct mount *next_mnt(struct mount *p, struct mount *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Al Viro6b41d532011-11-24 23:24:33 -0500906 struct list_head *next = p->mnt_mounts.next;
907 if (next == &p->mnt_mounts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 while (1) {
Al Viro909b0a82011-11-25 03:06:56 -0500909 if (p == root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return NULL;
Al Viro6b41d532011-11-24 23:24:33 -0500911 next = p->mnt_child.next;
912 if (next != &p->mnt_parent->mnt_mounts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 break;
Al Viro0714a532011-11-24 22:19:58 -0500914 p = p->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 }
Al Viro6b41d532011-11-24 23:24:33 -0500917 return list_entry(next, struct mount, mnt_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Al Viro315fc832011-11-24 18:57:30 -0500920static struct mount *skip_mnt_tree(struct mount *p)
Ram Pai9676f0c2005-11-07 17:21:20 -0500921{
Al Viro6b41d532011-11-24 23:24:33 -0500922 struct list_head *prev = p->mnt_mounts.prev;
923 while (prev != &p->mnt_mounts) {
924 p = list_entry(prev, struct mount, mnt_child);
925 prev = p->mnt_mounts.prev;
Ram Pai9676f0c2005-11-07 17:21:20 -0500926 }
927 return p;
928}
929
Al Viro8f2918892018-11-04 06:48:34 -0500930/**
931 * vfs_create_mount - Create a mount for a configured superblock
932 * @fc: The configuration context with the superblock attached
933 *
934 * Create a mount to an already configured superblock. If necessary, the
935 * caller should invoke vfs_get_tree() before calling this.
936 *
937 * Note that this does not attach the mount to anything.
938 */
939struct vfsmount *vfs_create_mount(struct fs_context *fc)
Al Viro9d412a42011-03-17 22:08:28 -0400940{
Al Virob105e272011-11-24 20:38:33 -0500941 struct mount *mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400942
Al Viro8f2918892018-11-04 06:48:34 -0500943 if (!fc->root)
944 return ERR_PTR(-EINVAL);
Al Viro9d412a42011-03-17 22:08:28 -0400945
Al Viro8f2918892018-11-04 06:48:34 -0500946 mnt = alloc_vfsmnt(fc->source ?: "none");
Al Viro9d412a42011-03-17 22:08:28 -0400947 if (!mnt)
948 return ERR_PTR(-ENOMEM);
949
Al Viro8f2918892018-11-04 06:48:34 -0500950 if (fc->sb_flags & SB_KERNMOUNT)
Al Virob105e272011-11-24 20:38:33 -0500951 mnt->mnt.mnt_flags = MNT_INTERNAL;
Al Viro9d412a42011-03-17 22:08:28 -0400952
Al Viro8f2918892018-11-04 06:48:34 -0500953 atomic_inc(&fc->root->d_sb->s_active);
954 mnt->mnt.mnt_sb = fc->root->d_sb;
955 mnt->mnt.mnt_root = dget(fc->root);
956 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
957 mnt->mnt_parent = mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400958
Al Viro719ea2f2013-09-29 11:24:49 -0400959 lock_mount_hash();
Al Viro8f2918892018-11-04 06:48:34 -0500960 list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -0400961 unlock_mount_hash();
Al Virob105e272011-11-24 20:38:33 -0500962 return &mnt->mnt;
Al Viro9d412a42011-03-17 22:08:28 -0400963}
Al Viro8f2918892018-11-04 06:48:34 -0500964EXPORT_SYMBOL(vfs_create_mount);
965
966struct vfsmount *fc_mount(struct fs_context *fc)
967{
968 int err = vfs_get_tree(fc);
969 if (!err) {
970 up_write(&fc->root->d_sb->s_umount);
971 return vfs_create_mount(fc);
972 }
973 return ERR_PTR(err);
974}
975EXPORT_SYMBOL(fc_mount);
976
David Howells9bc61ab2018-11-04 03:19:03 -0500977struct vfsmount *vfs_kern_mount(struct file_system_type *type,
978 int flags, const char *name,
979 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
David Howells9bc61ab2018-11-04 03:19:03 -0500981 struct fs_context *fc;
Al Viro8f2918892018-11-04 06:48:34 -0500982 struct vfsmount *mnt;
David Howells9bc61ab2018-11-04 03:19:03 -0500983 int ret = 0;
Al Viro9d412a42011-03-17 22:08:28 -0400984
985 if (!type)
David Howells3e1aeb02018-11-01 23:07:25 +0000986 return ERR_PTR(-EINVAL);
Al Viro9d412a42011-03-17 22:08:28 -0400987
David Howells9bc61ab2018-11-04 03:19:03 -0500988 fc = fs_context_for_mount(type, flags);
989 if (IS_ERR(fc))
990 return ERR_CAST(fc);
991
David Howells3e1aeb02018-11-01 23:07:25 +0000992 if (name)
993 ret = vfs_parse_fs_string(fc, "source",
994 name, strlen(name));
David Howells9bc61ab2018-11-04 03:19:03 -0500995 if (!ret)
996 ret = parse_monolithic_mount_data(fc, data);
997 if (!ret)
Al Viro8f2918892018-11-04 06:48:34 -0500998 mnt = fc_mount(fc);
999 else
1000 mnt = ERR_PTR(ret);
Al Viro9d412a42011-03-17 22:08:28 -04001001
David Howells9bc61ab2018-11-04 03:19:03 -05001002 put_fs_context(fc);
Al Viro8f2918892018-11-04 06:48:34 -05001003 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04001004}
1005EXPORT_SYMBOL_GPL(vfs_kern_mount);
1006
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13001007struct vfsmount *
1008vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
1009 const char *name, void *data)
1010{
1011 /* Until it is worked out how to pass the user namespace
1012 * through from the parent mount to the submount don't support
1013 * unprivileged mounts with submounts.
1014 */
1015 if (mountpoint->d_sb->s_user_ns != &init_user_ns)
1016 return ERR_PTR(-EPERM);
1017
David Howellse462ec52017-07-17 08:45:35 +01001018 return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
Eric W. Biederman93faccbb2017-02-01 06:06:16 +13001019}
1020EXPORT_SYMBOL_GPL(vfs_submount);
1021
Al Viro87129cc2011-11-24 21:24:27 -05001022static struct mount *clone_mnt(struct mount *old, struct dentry *root,
Ram Pai36341f62005-11-07 17:17:22 -05001023 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Al Viro87129cc2011-11-24 21:24:27 -05001025 struct super_block *sb = old->mnt.mnt_sb;
David Howellsbe34d1a2012-06-25 12:55:18 +01001026 struct mount *mnt;
1027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
David Howellsbe34d1a2012-06-25 12:55:18 +01001029 mnt = alloc_vfsmnt(old->mnt_devname);
1030 if (!mnt)
1031 return ERR_PTR(-ENOMEM);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001032
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001033 if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
David Howellsbe34d1a2012-06-25 12:55:18 +01001034 mnt->mnt_group_id = 0; /* not a peer of original */
1035 else
1036 mnt->mnt_group_id = old->mnt_group_id;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001037
David Howellsbe34d1a2012-06-25 12:55:18 +01001038 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
1039 err = mnt_alloc_group_id(mnt);
1040 if (err)
1041 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001043
Al Viro16a34ad2018-04-19 22:03:08 -04001044 mnt->mnt.mnt_flags = old->mnt.mnt_flags;
1045 mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001046
David Howellsbe34d1a2012-06-25 12:55:18 +01001047 atomic_inc(&sb->s_active);
1048 mnt->mnt.mnt_sb = sb;
1049 mnt->mnt.mnt_root = dget(root);
1050 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1051 mnt->mnt_parent = mnt;
Al Viro719ea2f2013-09-29 11:24:49 -04001052 lock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001053 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
Al Viro719ea2f2013-09-29 11:24:49 -04001054 unlock_mount_hash();
David Howellsbe34d1a2012-06-25 12:55:18 +01001055
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07001056 if ((flag & CL_SLAVE) ||
1057 ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
David Howellsbe34d1a2012-06-25 12:55:18 +01001058 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
1059 mnt->mnt_master = old;
1060 CLEAR_MNT_SHARED(mnt);
1061 } else if (!(flag & CL_PRIVATE)) {
1062 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
1063 list_add(&mnt->mnt_share, &old->mnt_share);
1064 if (IS_MNT_SLAVE(old))
1065 list_add(&mnt->mnt_slave, &old->mnt_slave);
1066 mnt->mnt_master = old->mnt_master;
Al Viro5235d442016-11-20 19:33:09 -05001067 } else {
1068 CLEAR_MNT_SHARED(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001069 }
1070 if (flag & CL_MAKE_SHARED)
1071 set_mnt_shared(mnt);
1072
1073 /* stick the duplicate mount on the same expiry list
1074 * as the original if that was on one */
1075 if (flag & CL_EXPIRE) {
1076 if (!list_empty(&old->mnt_expire))
1077 list_add(&mnt->mnt_expire, &old->mnt_expire);
1078 }
1079
Al Virocb338d02011-11-24 20:55:08 -05001080 return mnt;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001081
1082 out_free:
David Howells8ffcb322014-01-24 12:17:54 +00001083 mnt_free_id(mnt);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001084 free_vfsmnt(mnt);
David Howellsbe34d1a2012-06-25 12:55:18 +01001085 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Al Viro9ea459e2014-08-08 13:08:20 -04001088static void cleanup_mnt(struct mount *mnt)
1089{
1090 /*
1091 * This probably indicates that somebody messed
1092 * up a mnt_want/drop_write() pair. If this
1093 * happens, the filesystem was probably unable
1094 * to make r/w->r/o transitions.
1095 */
1096 /*
1097 * The locking used to deal with mnt_count decrement provides barriers,
1098 * so mnt_get_writers() below is safe.
1099 */
1100 WARN_ON(mnt_get_writers(mnt));
1101 if (unlikely(mnt->mnt_pins.first))
1102 mnt_pin_kill(mnt);
1103 fsnotify_vfsmount_delete(&mnt->mnt);
1104 dput(mnt->mnt.mnt_root);
1105 deactivate_super(mnt->mnt.mnt_sb);
1106 mnt_free_id(mnt);
1107 call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
1108}
1109
1110static void __cleanup_mnt(struct rcu_head *head)
1111{
1112 cleanup_mnt(container_of(head, struct mount, mnt_rcu));
1113}
1114
1115static LLIST_HEAD(delayed_mntput_list);
1116static void delayed_mntput(struct work_struct *unused)
1117{
1118 struct llist_node *node = llist_del_all(&delayed_mntput_list);
Byungchul Park29785732017-08-07 17:44:45 +09001119 struct mount *m, *t;
Al Viro9ea459e2014-08-08 13:08:20 -04001120
Byungchul Park29785732017-08-07 17:44:45 +09001121 llist_for_each_entry_safe(m, t, node, mnt_llist)
1122 cleanup_mnt(m);
Al Viro9ea459e2014-08-08 13:08:20 -04001123}
1124static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
1125
Al Viro900148d2011-11-25 00:33:11 -05001126static void mntput_no_expire(struct mount *mnt)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001127{
Al Viro4edbe132019-06-30 10:39:08 -04001128 LIST_HEAD(list);
1129
Al Viro48a066e2013-09-29 22:06:07 -04001130 rcu_read_lock();
Al Viro9ea0a462018-08-09 17:21:17 -04001131 if (likely(READ_ONCE(mnt->mnt_ns))) {
1132 /*
1133 * Since we don't do lock_mount_hash() here,
1134 * ->mnt_ns can change under us. However, if it's
1135 * non-NULL, then there's a reference that won't
1136 * be dropped until after an RCU delay done after
1137 * turning ->mnt_ns NULL. So if we observe it
1138 * non-NULL under rcu_read_lock(), the reference
1139 * we are dropping is not the final one.
1140 */
1141 mnt_add_count(mnt, -1);
Al Viro48a066e2013-09-29 22:06:07 -04001142 rcu_read_unlock();
Al Virof03c6592011-01-14 22:30:21 -05001143 return;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001144 }
Al Viro719ea2f2013-09-29 11:24:49 -04001145 lock_mount_hash();
Al Viro119e1ef2018-08-09 17:51:32 -04001146 /*
1147 * make sure that if __legitimize_mnt() has not seen us grab
1148 * mount_lock, we'll see their refcount increment here.
1149 */
1150 smp_mb();
Al Viro9ea0a462018-08-09 17:21:17 -04001151 mnt_add_count(mnt, -1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001152 if (mnt_get_count(mnt)) {
Al Viro48a066e2013-09-29 22:06:07 -04001153 rcu_read_unlock();
Al Viro719ea2f2013-09-29 11:24:49 -04001154 unlock_mount_hash();
Nick Pigginb3e19d92011-01-07 17:50:11 +11001155 return;
1156 }
Al Viro48a066e2013-09-29 22:06:07 -04001157 if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
1158 rcu_read_unlock();
1159 unlock_mount_hash();
1160 return;
1161 }
1162 mnt->mnt.mnt_flags |= MNT_DOOMED;
1163 rcu_read_unlock();
Andi Kleen962830d2012-05-08 13:32:02 +09301164
Miklos Szeredi39f7c4d2011-11-21 12:11:30 +01001165 list_del(&mnt->mnt_instance);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001166
1167 if (unlikely(!list_empty(&mnt->mnt_mounts))) {
1168 struct mount *p, *tmp;
1169 list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
Al Viro4edbe132019-06-30 10:39:08 -04001170 __put_mountpoint(unhash_mnt(p), &list);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001171 }
1172 }
Al Viro719ea2f2013-09-29 11:24:49 -04001173 unlock_mount_hash();
Al Viro4edbe132019-06-30 10:39:08 -04001174 shrink_dentry_list(&list);
Al Viro649a7952013-09-28 12:41:25 -04001175
Al Viro9ea459e2014-08-08 13:08:20 -04001176 if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
1177 struct task_struct *task = current;
1178 if (likely(!(task->flags & PF_KTHREAD))) {
1179 init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
1180 if (!task_work_add(task, &mnt->mnt_rcu, true))
1181 return;
1182 }
1183 if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
1184 schedule_delayed_work(&delayed_mntput_work, 1);
1185 return;
1186 }
1187 cleanup_mnt(mnt);
Al Viro7b7b1ac2005-11-07 17:13:39 -05001188}
Nick Pigginb3e19d92011-01-07 17:50:11 +11001189
1190void mntput(struct vfsmount *mnt)
1191{
1192 if (mnt) {
Al Viro863d6842011-11-25 00:57:42 -05001193 struct mount *m = real_mount(mnt);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001194 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
Al Viro863d6842011-11-25 00:57:42 -05001195 if (unlikely(m->mnt_expiry_mark))
1196 m->mnt_expiry_mark = 0;
1197 mntput_no_expire(m);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001198 }
1199}
1200EXPORT_SYMBOL(mntput);
1201
1202struct vfsmount *mntget(struct vfsmount *mnt)
1203{
1204 if (mnt)
Al Viro83adc752011-11-24 22:37:54 -05001205 mnt_add_count(real_mount(mnt), 1);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001206 return mnt;
1207}
1208EXPORT_SYMBOL(mntget);
1209
Ian Kentc6609c02016-11-24 08:03:41 +11001210/* path_is_mountpoint() - Check if path is a mount in the current
1211 * namespace.
1212 *
1213 * d_mountpoint() can only be used reliably to establish if a dentry is
1214 * not mounted in any namespace and that common case is handled inline.
1215 * d_mountpoint() isn't aware of the possibility there may be multiple
1216 * mounts using a given dentry in a different namespace. This function
1217 * checks if the passed in path is a mountpoint rather than the dentry
1218 * alone.
1219 */
1220bool path_is_mountpoint(const struct path *path)
1221{
1222 unsigned seq;
1223 bool res;
1224
1225 if (!d_mountpoint(path->dentry))
1226 return false;
1227
1228 rcu_read_lock();
1229 do {
1230 seq = read_seqbegin(&mount_lock);
1231 res = __path_is_mountpoint(path);
1232 } while (read_seqretry(&mount_lock, seq));
1233 rcu_read_unlock();
1234
1235 return res;
1236}
1237EXPORT_SYMBOL(path_is_mountpoint);
1238
Al Viroca71cf72016-11-20 19:45:28 -05001239struct vfsmount *mnt_clone_internal(const struct path *path)
Al Viro7b7b1ac2005-11-07 17:13:39 -05001240{
Al Viro3064c352014-08-07 09:12:31 -04001241 struct mount *p;
1242 p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
1243 if (IS_ERR(p))
1244 return ERR_CAST(p);
1245 p->mnt.mnt_flags |= MNT_INTERNAL;
1246 return &p->mnt;
Al Viro7b7b1ac2005-11-07 17:13:39 -05001247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001249#ifdef CONFIG_PROC_FS
Al Viro0226f492011-12-06 12:21:54 -05001250/* iterator; we want it to have access to namespace_sem, thus here... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251static void *m_start(struct seq_file *m, loff_t *pos)
1252{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001253 struct proc_mounts *p = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Ram Pai390c6842005-11-07 17:17:51 -05001255 down_read(&namespace_sem);
Al Viroc7999c32014-02-27 14:40:10 -05001256 if (p->cached_event == p->ns->event) {
1257 void *v = p->cached_mount;
1258 if (*pos == p->cached_index)
1259 return v;
1260 if (*pos == p->cached_index + 1) {
1261 v = seq_list_next(v, &p->ns->list, &p->cached_index);
1262 return p->cached_mount = v;
1263 }
1264 }
1265
1266 p->cached_event = p->ns->event;
1267 p->cached_mount = seq_list_start(&p->ns->list, *pos);
1268 p->cached_index = *pos;
1269 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static void *m_next(struct seq_file *m, void *v, loff_t *pos)
1273{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001274 struct proc_mounts *p = m->private;
Pavel Emelianovb0765fb2007-07-15 23:39:55 -07001275
Al Viroc7999c32014-02-27 14:40:10 -05001276 p->cached_mount = seq_list_next(v, &p->ns->list, pos);
1277 p->cached_index = *pos;
1278 return p->cached_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281static void m_stop(struct seq_file *m, void *v)
1282{
Ram Pai390c6842005-11-07 17:17:51 -05001283 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Al Viro0226f492011-12-06 12:21:54 -05001286static int m_show(struct seq_file *m, void *v)
Al Viro9f5596a2010-02-05 00:40:25 -05001287{
Yann Droneaudede1bf02015-06-30 14:57:30 -07001288 struct proc_mounts *p = m->private;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001289 struct mount *r = list_entry(v, struct mount, mnt_list);
Al Viro0226f492011-12-06 12:21:54 -05001290 return p->show(m, &r->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001293const struct seq_operations mounts_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .start = m_start,
1295 .next = m_next,
1296 .stop = m_stop,
Al Viro0226f492011-12-06 12:21:54 -05001297 .show = m_show,
Chuck Leverb4629fe2006-03-20 13:44:12 -05001298};
Miklos Szeredia1a2c402008-03-27 13:06:24 +01001299#endif /* CONFIG_PROC_FS */
Chuck Leverb4629fe2006-03-20 13:44:12 -05001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/**
1302 * may_umount_tree - check if a mount tree is busy
1303 * @mnt: root of mount tree
1304 *
1305 * This is called to check if a tree of mounts has any
1306 * open files, pwds, chroots or sub mounts that are
1307 * busy.
1308 */
Al Viro909b0a82011-11-25 03:06:56 -05001309int may_umount_tree(struct vfsmount *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Al Viro909b0a82011-11-25 03:06:56 -05001311 struct mount *mnt = real_mount(m);
Ram Pai36341f62005-11-07 17:17:22 -05001312 int actual_refs = 0;
1313 int minimum_refs = 0;
Al Viro315fc832011-11-24 18:57:30 -05001314 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05001315 BUG_ON(!m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Nick Pigginb3e19d92011-01-07 17:50:11 +11001317 /* write lock needed for mnt_get_count */
Al Viro719ea2f2013-09-29 11:24:49 -04001318 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05001319 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Viro83adc752011-11-24 22:37:54 -05001320 actual_refs += mnt_get_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Al Viro719ea2f2013-09-29 11:24:49 -04001323 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (actual_refs > minimum_refs)
Ian Kente3474a82006-03-27 01:14:51 -08001326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Ian Kente3474a82006-03-27 01:14:51 -08001328 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331EXPORT_SYMBOL(may_umount_tree);
1332
1333/**
1334 * may_umount - check if a mount point is busy
1335 * @mnt: root of mount
1336 *
1337 * This is called to check if a mount point has any
1338 * open files, pwds, chroots or sub mounts. If the
1339 * mount has sub mounts this will return busy
1340 * regardless of whether the sub mounts are busy.
1341 *
1342 * Doesn't take quota and stuff into account. IOW, in some cases it will
1343 * give false negatives. The main reason why it's here is that we need
1344 * a non-destructive way to look for easily umountable filesystems.
1345 */
1346int may_umount(struct vfsmount *mnt)
1347{
Ian Kente3474a82006-03-27 01:14:51 -08001348 int ret = 1;
Al Viro8ad08d82010-01-16 12:56:08 -05001349 down_read(&namespace_sem);
Al Viro719ea2f2013-09-29 11:24:49 -04001350 lock_mount_hash();
Al Viro1ab59732011-11-24 21:35:16 -05001351 if (propagate_mount_busy(real_mount(mnt), 2))
Ian Kente3474a82006-03-27 01:14:51 -08001352 ret = 0;
Al Viro719ea2f2013-09-29 11:24:49 -04001353 unlock_mount_hash();
Al Viro8ad08d82010-01-16 12:56:08 -05001354 up_read(&namespace_sem);
Ram Paia05964f2005-11-07 17:20:17 -05001355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358EXPORT_SYMBOL(may_umount);
1359
Al Viro97216be2013-03-16 15:12:40 -04001360static void namespace_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001362 struct hlist_head head;
Al Viro4edbe132019-06-30 10:39:08 -04001363 LIST_HEAD(list);
Al Viro97216be2013-03-16 15:12:40 -04001364
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001365 hlist_move_list(&unmounted, &head);
Al Viro4edbe132019-06-30 10:39:08 -04001366 list_splice_init(&ex_mountpoints, &list);
Al Viro97216be2013-03-16 15:12:40 -04001367
Al Viro97216be2013-03-16 15:12:40 -04001368 up_write(&namespace_sem);
1369
Al Viro4edbe132019-06-30 10:39:08 -04001370 shrink_dentry_list(&list);
1371
Eric W. Biedermana3b3c562015-04-02 20:33:53 -05001372 if (likely(hlist_empty(&head)))
1373 return;
1374
NeilBrown22cb7402018-11-30 10:33:18 +11001375 synchronize_rcu_expedited();
Al Viro48a066e2013-09-29 22:06:07 -04001376
Al Viro87b95ce2015-01-10 19:01:08 -05001377 group_pin_kill(&head);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001378}
1379
Al Viro97216be2013-03-16 15:12:40 -04001380static inline void namespace_lock(void)
Al Viroe3197d82013-03-16 14:35:16 -04001381{
Al Viro97216be2013-03-16 15:12:40 -04001382 down_write(&namespace_sem);
Al Viroe3197d82013-03-16 14:35:16 -04001383}
1384
Eric W. Biedermane819f152014-12-24 07:20:01 -06001385enum umount_tree_flags {
1386 UMOUNT_SYNC = 1,
1387 UMOUNT_PROPAGATE = 2,
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001388 UMOUNT_CONNECTED = 4,
Eric W. Biedermane819f152014-12-24 07:20:01 -06001389};
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001390
1391static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
1392{
1393 /* Leaving mounts connected is only valid for lazy umounts */
1394 if (how & UMOUNT_SYNC)
1395 return true;
1396
1397 /* A mount without a parent has nothing to be connected to */
1398 if (!mnt_has_parent(mnt))
1399 return true;
1400
1401 /* Because the reference counting rules change when mounts are
1402 * unmounted and connected, umounted mounts may not be
1403 * connected to mounted mounts.
1404 */
1405 if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
1406 return true;
1407
1408 /* Has it been requested that the mount remain connected? */
1409 if (how & UMOUNT_CONNECTED)
1410 return false;
1411
1412 /* Is the mount locked such that it needs to remain connected? */
1413 if (IS_MNT_LOCKED(mnt))
1414 return false;
1415
1416 /* By default disconnect the mount */
1417 return true;
1418}
1419
Nick Piggin99b7db72010-08-18 04:37:39 +10001420/*
Al Viro48a066e2013-09-29 22:06:07 -04001421 * mount_lock must be held
Nick Piggin99b7db72010-08-18 04:37:39 +10001422 * namespace_sem must be held for write
1423 */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001424static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
Ram Pai70fbcdf2005-11-07 17:17:04 -05001425{
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001426 LIST_HEAD(tmp_list);
Al Viro315fc832011-11-24 18:57:30 -05001427 struct mount *p;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001428
Eric W. Biederman5d884572015-01-03 05:39:35 -06001429 if (how & UMOUNT_PROPAGATE)
1430 propagate_mount_unlock(mnt);
1431
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001432 /* Gather the mounts to umount */
Eric W. Biederman590ce4bc2014-12-22 18:30:08 -06001433 for (p = mnt; p; p = next_mnt(p, mnt)) {
1434 p->mnt.mnt_flags |= MNT_UMOUNT;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001435 list_move(&p->mnt_list, &tmp_list);
Eric W. Biederman590ce4bc2014-12-22 18:30:08 -06001436 }
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001437
Eric W. Biederman411a9382014-12-22 19:12:07 -06001438 /* Hide the mounts from mnt_mounts */
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001439 list_for_each_entry(p, &tmp_list, mnt_list) {
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001440 list_del_init(&p->mnt_child);
Al Viro38129a12014-03-20 21:10:51 -04001441 }
Ram Pai70fbcdf2005-11-07 17:17:04 -05001442
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001443 /* Add propogated mounts to the tmp_list */
Eric W. Biedermane819f152014-12-24 07:20:01 -06001444 if (how & UMOUNT_PROPAGATE)
Al Viro7b8a53f2011-01-15 20:08:44 -05001445 propagate_umount(&tmp_list);
Ram Paia05964f2005-11-07 17:20:17 -05001446
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001447 while (!list_empty(&tmp_list)) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05001448 struct mnt_namespace *ns;
Eric W. Biedermance07d892014-12-23 21:37:03 -06001449 bool disconnect;
Eric W. Biedermanc003b262014-12-18 13:10:48 -06001450 p = list_first_entry(&tmp_list, struct mount, mnt_list);
Al Viro6776db3d2011-11-25 00:22:05 -05001451 list_del_init(&p->mnt_expire);
Al Viro1a4eeaf2011-11-25 02:19:55 -05001452 list_del_init(&p->mnt_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05001453 ns = p->mnt_ns;
1454 if (ns) {
1455 ns->mounts--;
1456 __touch_mnt_namespace(ns);
1457 }
Al Viro143c8c92011-11-25 00:46:35 -05001458 p->mnt_ns = NULL;
Eric W. Biedermane819f152014-12-24 07:20:01 -06001459 if (how & UMOUNT_SYNC)
Al Viro48a066e2013-09-29 22:06:07 -04001460 p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
Al Viro87b95ce2015-01-10 19:01:08 -05001461
Eric W. Biedermanf2d0a122015-07-17 14:15:30 -05001462 disconnect = disconnect_mount(p, how);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001463
1464 pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
1465 disconnect ? &unmounted : NULL);
Al Viro676da582011-11-24 21:47:05 -05001466 if (mnt_has_parent(p)) {
Al Viro81b6b062014-08-30 18:32:05 -04001467 mnt_add_count(p->mnt_parent, -1);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001468 if (!disconnect) {
1469 /* Don't forget about p */
1470 list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
1471 } else {
1472 umount_mnt(p);
1473 }
Al Viro7c4b93d2008-03-21 23:59:49 -04001474 }
Al Viro0f0afb12011-11-24 20:43:10 -05001475 change_mnt_propagation(p, MS_PRIVATE);
Al Viro38129a12014-03-20 21:10:51 -04001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Al Virob54b9be2013-03-16 14:39:34 -04001479static void shrink_submounts(struct mount *mnt);
Al Viroc35038b2008-03-22 00:46:23 -04001480
David Howells8d0347f2018-11-04 09:28:36 -05001481static int do_umount_root(struct super_block *sb)
1482{
1483 int ret = 0;
1484
1485 down_write(&sb->s_umount);
1486 if (!sb_rdonly(sb)) {
1487 struct fs_context *fc;
1488
1489 fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
1490 SB_RDONLY);
1491 if (IS_ERR(fc)) {
1492 ret = PTR_ERR(fc);
1493 } else {
1494 ret = parse_monolithic_mount_data(fc, NULL);
1495 if (!ret)
1496 ret = reconfigure_super(fc);
1497 put_fs_context(fc);
1498 }
1499 }
1500 up_write(&sb->s_umount);
1501 return ret;
1502}
1503
Al Viro1ab59732011-11-24 21:35:16 -05001504static int do_umount(struct mount *mnt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Al Viro1ab59732011-11-24 21:35:16 -05001506 struct super_block *sb = mnt->mnt.mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int retval;
1508
Al Viro1ab59732011-11-24 21:35:16 -05001509 retval = security_sb_umount(&mnt->mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (retval)
1511 return retval;
1512
1513 /*
1514 * Allow userspace to request a mountpoint be expired rather than
1515 * unmounting unconditionally. Unmount only happens if:
1516 * (1) the mark is already set (the mark is cleared by mntput())
1517 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1518 */
1519 if (flags & MNT_EXPIRE) {
Al Viro1ab59732011-11-24 21:35:16 -05001520 if (&mnt->mnt == current->fs->root.mnt ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 flags & (MNT_FORCE | MNT_DETACH))
1522 return -EINVAL;
1523
Nick Pigginb3e19d92011-01-07 17:50:11 +11001524 /*
1525 * probably don't strictly need the lock here if we examined
1526 * all race cases, but it's a slowpath.
1527 */
Al Viro719ea2f2013-09-29 11:24:49 -04001528 lock_mount_hash();
Al Viro83adc752011-11-24 22:37:54 -05001529 if (mnt_get_count(mnt) != 2) {
Al Viro719ea2f2013-09-29 11:24:49 -04001530 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return -EBUSY;
Nick Pigginb3e19d92011-01-07 17:50:11 +11001532 }
Al Viro719ea2f2013-09-29 11:24:49 -04001533 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Al Viro863d6842011-11-25 00:57:42 -05001535 if (!xchg(&mnt->mnt_expiry_mark, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EAGAIN;
1537 }
1538
1539 /*
1540 * If we may have to abort operations to get out of this
1541 * mount, and they will themselves hold resources we must
1542 * allow the fs to do things. In the Unix tradition of
1543 * 'Gee thats tricky lets do it in userspace' the umount_begin
1544 * might fail to complete on the first run through as other tasks
1545 * must return, and the like. Thats for the mount program to worry
1546 * about for the moment.
1547 */
1548
Al Viro42faad92008-04-24 07:21:56 -04001549 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
Al Viro42faad92008-04-24 07:21:56 -04001550 sb->s_op->umount_begin(sb);
Al Viro42faad92008-04-24 07:21:56 -04001551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /*
1554 * No sense to grab the lock for this test, but test itself looks
1555 * somewhat bogus. Suggestions for better replacement?
1556 * Ho-hum... In principle, we might treat that as umount + switch
1557 * to rootfs. GC would eventually take care of the old vfsmount.
1558 * Actually it makes sense, especially if rootfs would contain a
1559 * /reboot - static binary that would close all descriptors and
1560 * call reboot(9). Then init(8) could umount root and exec /reboot.
1561 */
Al Viro1ab59732011-11-24 21:35:16 -05001562 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * Special case for "unmounting" root ...
1565 * we just try to remount it readonly.
1566 */
Eric W. Biedermanbc6155d2017-09-18 17:58:08 -05001567 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
Andy Lutomirskia1480dc2014-10-08 12:32:47 -07001568 return -EPERM;
David Howells8d0347f2018-11-04 09:28:36 -05001569 return do_umount_root(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
Al Viro97216be2013-03-16 15:12:40 -04001572 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001573 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001575 /* Recheck MNT_LOCKED with the locks held */
1576 retval = -EINVAL;
1577 if (mnt->mnt.mnt_flags & MNT_LOCKED)
1578 goto out;
1579
1580 event++;
Al Viro48a066e2013-09-29 22:06:07 -04001581 if (flags & MNT_DETACH) {
Al Viro1a4eeaf2011-11-25 02:19:55 -05001582 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001583 umount_tree(mnt, UMOUNT_PROPAGATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 retval = 0;
Al Viro48a066e2013-09-29 22:06:07 -04001585 } else {
1586 shrink_submounts(mnt);
1587 retval = -EBUSY;
1588 if (!propagate_mount_busy(mnt, 2)) {
1589 if (!list_empty(&mnt->mnt_list))
Eric W. Biedermane819f152014-12-24 07:20:01 -06001590 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Viro48a066e2013-09-29 22:06:07 -04001591 retval = 0;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001594out:
Al Viro719ea2f2013-09-29 11:24:49 -04001595 unlock_mount_hash();
Al Viroe3197d82013-03-16 14:35:16 -04001596 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return retval;
1598}
1599
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001600/*
1601 * __detach_mounts - lazily unmount all mounts on the specified dentry
1602 *
1603 * During unlink, rmdir, and d_drop it is possible to loose the path
1604 * to an existing mountpoint, and wind up leaking the mount.
1605 * detach_mounts allows lazily unmounting those mounts instead of
1606 * leaking them.
1607 *
1608 * The caller may hold dentry->d_inode->i_mutex.
1609 */
1610void __detach_mounts(struct dentry *dentry)
1611{
1612 struct mountpoint *mp;
1613 struct mount *mnt;
1614
1615 namespace_lock();
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001616 lock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001617 mp = lookup_mountpoint(dentry);
Al Viroadc9b5c2019-06-29 12:06:51 -04001618 if (!mp)
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001619 goto out_unlock;
1620
Andrey Ulanove06b9332016-04-15 14:24:41 -07001621 event++;
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001622 while (!hlist_empty(&mp->m_list)) {
1623 mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001624 if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
Eric W. Biedermanfe78fcc2015-07-17 14:54:27 -05001625 hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
1626 umount_mnt(mnt);
Eric W. Biedermance07d892014-12-23 21:37:03 -06001627 }
Eric W. Biedermane0c9c0a2015-04-01 18:30:06 -05001628 else umount_tree(mnt, UMOUNT_CONNECTED);
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001629 }
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001630 put_mountpoint(mp);
1631out_unlock:
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13001632 unlock_mount_hash();
Eric W. Biederman80b5dce2013-10-03 01:31:18 -07001633 namespace_unlock();
1634}
1635
David Howellsdd111b32017-07-04 17:25:09 +01001636/*
Al Viro9b40bc92013-02-22 22:45:42 -05001637 * Is the caller allowed to modify his namespace?
1638 */
1639static inline bool may_mount(void)
1640{
1641 return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
1642}
1643
Jeff Layton9e8925b2015-11-16 09:49:34 -05001644static inline bool may_mandlock(void)
1645{
1646#ifndef CONFIG_MANDATORY_FILE_LOCKING
1647 return false;
1648#endif
Eric W. Biederman95ace752015-11-11 17:22:33 -06001649 return capable(CAP_SYS_ADMIN);
Jeff Layton9e8925b2015-11-16 09:49:34 -05001650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652/*
1653 * Now umount can handle mount points as well as block devices.
1654 * This is important for filesystems which use unnamed block devices.
1655 *
1656 * We now support a flag for forced unmount like the other 'big iron'
1657 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1658 */
1659
Dominik Brodowski3a18ef52018-03-11 11:34:40 +01001660int ksys_umount(char __user *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Al Viro2d8f3032008-07-22 09:59:21 -04001662 struct path path;
Al Viro900148d2011-11-25 00:33:11 -05001663 struct mount *mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int retval;
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001665 int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001667 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1668 return -EINVAL;
1669
Al Viro9b40bc92013-02-22 22:45:42 -05001670 if (!may_mount())
1671 return -EPERM;
1672
Miklos Szeredidb1f05b2010-02-10 12:15:53 +01001673 if (!(flags & UMOUNT_NOFOLLOW))
1674 lookup_flags |= LOOKUP_FOLLOW;
1675
Richard Guy Briggs57d46572019-01-23 13:35:00 -05001676 lookup_flags |= LOOKUP_NO_EVAL;
1677
Al Viro197df042013-09-08 14:03:27 -04001678 retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (retval)
1680 goto out;
Al Viro900148d2011-11-25 00:33:11 -05001681 mnt = real_mount(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 retval = -EINVAL;
Al Viro2d8f3032008-07-22 09:59:21 -04001683 if (path.dentry != path.mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto dput_and_out;
Al Viro143c8c92011-11-25 00:46:35 -05001685 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto dput_and_out;
Eric W. Biederman25d202e2018-10-22 10:21:38 -05001687 if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07001688 goto dput_and_out;
Eric W. Biedermanb2f5d4d2014-10-04 14:44:03 -07001689 retval = -EPERM;
1690 if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
1691 goto dput_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Al Viro900148d2011-11-25 00:33:11 -05001693 retval = do_umount(mnt, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694dput_and_out:
Jan Blunck429731b2008-02-14 19:34:31 -08001695 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
Al Viro2d8f3032008-07-22 09:59:21 -04001696 dput(path.dentry);
Al Viro900148d2011-11-25 00:33:11 -05001697 mntput_no_expire(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698out:
1699 return retval;
1700}
1701
Dominik Brodowski3a18ef52018-03-11 11:34:40 +01001702SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1703{
1704 return ksys_umount(name, flags);
1705}
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1708
1709/*
Ram Paib58fed82005-11-07 17:16:09 -05001710 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Heiko Carstensbdc480e2009-01-14 14:14:12 +01001712SYSCALL_DEFINE1(oldumount, char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Dominik Brodowski3a18ef52018-03-11 11:34:40 +01001714 return ksys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717#endif
1718
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001719static bool is_mnt_ns_file(struct dentry *dentry)
Eric W. Biederman8823c072010-03-07 18:49:36 -08001720{
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001721 /* Is this a proxy for a mount namespace? */
Al Viroe149ed22014-11-01 10:57:28 -04001722 return dentry->d_op == &ns_dentry_operations &&
1723 dentry->d_fsdata == &mntns_operations;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001724}
1725
Al Viro58be28252014-11-01 00:00:23 -04001726struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
1727{
1728 return container_of(ns, struct mnt_namespace, ns);
1729}
1730
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001731static bool mnt_ns_loop(struct dentry *dentry)
1732{
1733 /* Could bind mounting the mount namespace inode cause a
1734 * mount namespace loop?
1735 */
1736 struct mnt_namespace *mnt_ns;
1737 if (!is_mnt_ns_file(dentry))
1738 return false;
1739
Al Virof77c8012014-11-01 03:13:17 -04001740 mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
Eric W. Biederman8823c072010-03-07 18:49:36 -08001741 return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
1742}
1743
Al Viro87129cc2011-11-24 21:24:27 -05001744struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -05001745 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Al Viro84d17192013-03-15 10:53:28 -04001747 struct mount *res, *p, *q, *r, *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001749 if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
1750 return ERR_PTR(-EINVAL);
1751
1752 if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
David Howellsbe34d1a2012-06-25 12:55:18 +01001753 return ERR_PTR(-EINVAL);
Ram Pai9676f0c2005-11-07 17:21:20 -05001754
Ram Pai36341f62005-11-07 17:17:22 -05001755 res = q = clone_mnt(mnt, dentry, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001756 if (IS_ERR(q))
1757 return q;
1758
Al Viroa73324d2011-11-24 22:25:07 -05001759 q->mnt_mountpoint = mnt->mnt_mountpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 p = mnt;
Al Viro6b41d532011-11-24 23:24:33 -05001762 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Al Viro315fc832011-11-24 18:57:30 -05001763 struct mount *s;
Jan Blunck7ec02ef2008-04-29 00:59:40 -07001764 if (!is_subdir(r->mnt_mountpoint, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 continue;
1766
Al Viro909b0a82011-11-25 03:06:56 -05001767 for (s = r; s; s = next_mnt(s, r)) {
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001768 if (!(flag & CL_COPY_UNBINDABLE) &&
1769 IS_MNT_UNBINDABLE(s)) {
Eric W. Biedermandf7342b2018-10-25 09:04:18 -05001770 if (s->mnt.mnt_flags & MNT_LOCKED) {
1771 /* Both unbindable and locked. */
1772 q = ERR_PTR(-EPERM);
1773 goto out;
1774 } else {
1775 s = skip_mnt_tree(s);
1776 continue;
1777 }
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07001778 }
1779 if (!(flag & CL_COPY_MNT_NS_FILE) &&
1780 is_mnt_ns_file(s->mnt.mnt_root)) {
Ram Pai9676f0c2005-11-07 17:21:20 -05001781 s = skip_mnt_tree(s);
1782 continue;
1783 }
Al Viro0714a532011-11-24 22:19:58 -05001784 while (p != s->mnt_parent) {
1785 p = p->mnt_parent;
1786 q = q->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Al Viro87129cc2011-11-24 21:24:27 -05001788 p = s;
Al Viro84d17192013-03-15 10:53:28 -04001789 parent = q;
Al Viro87129cc2011-11-24 21:24:27 -05001790 q = clone_mnt(p, p->mnt.mnt_root, flag);
David Howellsbe34d1a2012-06-25 12:55:18 +01001791 if (IS_ERR(q))
1792 goto out;
Al Viro719ea2f2013-09-29 11:24:49 -04001793 lock_mount_hash();
Al Viro1a4eeaf2011-11-25 02:19:55 -05001794 list_add_tail(&q->mnt_list, &res->mnt_list);
Eric W. Biederman1064f872017-01-20 18:28:35 +13001795 attach_mnt(q, parent, p->mnt_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04001796 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 }
1799 return res;
David Howellsbe34d1a2012-06-25 12:55:18 +01001800out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (res) {
Al Viro719ea2f2013-09-29 11:24:49 -04001802 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06001803 umount_tree(res, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04001804 unlock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
David Howellsbe34d1a2012-06-25 12:55:18 +01001806 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
David Howellsbe34d1a2012-06-25 12:55:18 +01001809/* Caller should check returned pointer for errors */
1810
Al Viroca71cf72016-11-20 19:45:28 -05001811struct vfsmount *collect_mounts(const struct path *path)
Al Viro8aec0802007-06-07 12:20:32 -04001812{
Al Virocb338d02011-11-24 20:55:08 -05001813 struct mount *tree;
Al Viro97216be2013-03-16 15:12:40 -04001814 namespace_lock();
Eric W. Biedermancd4a4012015-01-07 14:28:26 -06001815 if (!check_mnt(real_mount(path->mnt)))
1816 tree = ERR_PTR(-EINVAL);
1817 else
1818 tree = copy_tree(real_mount(path->mnt), path->dentry,
1819 CL_COPY_ALL | CL_PRIVATE);
Al Viro328e6d92013-03-16 14:49:45 -04001820 namespace_unlock();
David Howellsbe34d1a2012-06-25 12:55:18 +01001821 if (IS_ERR(tree))
Dan Carpenter52e220d2013-08-14 12:44:39 +03001822 return ERR_CAST(tree);
David Howellsbe34d1a2012-06-25 12:55:18 +01001823 return &tree->mnt;
Al Viro8aec0802007-06-07 12:20:32 -04001824}
1825
Al Viroa07b2002018-11-05 17:40:30 +00001826static void free_mnt_ns(struct mnt_namespace *);
1827static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
1828
1829void dissolve_on_fput(struct vfsmount *mnt)
1830{
1831 struct mnt_namespace *ns;
1832 namespace_lock();
1833 lock_mount_hash();
1834 ns = real_mount(mnt)->mnt_ns;
David Howells44dfd842018-11-05 17:40:31 +00001835 if (ns) {
1836 if (is_anon_ns(ns))
1837 umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
1838 else
1839 ns = NULL;
1840 }
Al Viroa07b2002018-11-05 17:40:30 +00001841 unlock_mount_hash();
1842 namespace_unlock();
David Howells44dfd842018-11-05 17:40:31 +00001843 if (ns)
1844 free_mnt_ns(ns);
Al Viroa07b2002018-11-05 17:40:30 +00001845}
1846
Al Viro8aec0802007-06-07 12:20:32 -04001847void drop_collected_mounts(struct vfsmount *mnt)
1848{
Al Viro97216be2013-03-16 15:12:40 -04001849 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04001850 lock_mount_hash();
Eric W. Biederman9c8e0a12018-10-25 12:05:11 -05001851 umount_tree(real_mount(mnt), 0);
Al Viro719ea2f2013-09-29 11:24:49 -04001852 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04001853 namespace_unlock();
Al Viro8aec0802007-06-07 12:20:32 -04001854}
1855
Miklos Szeredic771d682014-10-24 00:14:36 +02001856/**
1857 * clone_private_mount - create a private clone of a path
1858 *
1859 * This creates a new vfsmount, which will be the clone of @path. The new will
1860 * not be attached anywhere in the namespace and will be private (i.e. changes
1861 * to the originating mount won't be propagated into this).
1862 *
1863 * Release with mntput().
1864 */
Al Viroca71cf72016-11-20 19:45:28 -05001865struct vfsmount *clone_private_mount(const struct path *path)
Miklos Szeredic771d682014-10-24 00:14:36 +02001866{
1867 struct mount *old_mnt = real_mount(path->mnt);
1868 struct mount *new_mnt;
1869
1870 if (IS_MNT_UNBINDABLE(old_mnt))
1871 return ERR_PTR(-EINVAL);
1872
Miklos Szeredic771d682014-10-24 00:14:36 +02001873 new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
Miklos Szeredic771d682014-10-24 00:14:36 +02001874 if (IS_ERR(new_mnt))
1875 return ERR_CAST(new_mnt);
1876
1877 return &new_mnt->mnt;
1878}
1879EXPORT_SYMBOL_GPL(clone_private_mount);
1880
Al Viro1f707132010-01-30 22:51:25 -05001881int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1882 struct vfsmount *root)
1883{
Al Viro1a4eeaf2011-11-25 02:19:55 -05001884 struct mount *mnt;
Al Viro1f707132010-01-30 22:51:25 -05001885 int res = f(root, arg);
1886 if (res)
1887 return res;
Al Viro1a4eeaf2011-11-25 02:19:55 -05001888 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1889 res = f(&mnt->mnt, arg);
Al Viro1f707132010-01-30 22:51:25 -05001890 if (res)
1891 return res;
1892 }
1893 return 0;
1894}
1895
Al Viro3bd045c2019-01-30 13:15:45 -05001896static void lock_mnt_tree(struct mount *mnt)
1897{
1898 struct mount *p;
1899
1900 for (p = mnt; p; p = next_mnt(p, mnt)) {
1901 int flags = p->mnt.mnt_flags;
1902 /* Don't allow unprivileged users to change mount flags */
1903 flags |= MNT_LOCK_ATIME;
1904
1905 if (flags & MNT_READONLY)
1906 flags |= MNT_LOCK_READONLY;
1907
1908 if (flags & MNT_NODEV)
1909 flags |= MNT_LOCK_NODEV;
1910
1911 if (flags & MNT_NOSUID)
1912 flags |= MNT_LOCK_NOSUID;
1913
1914 if (flags & MNT_NOEXEC)
1915 flags |= MNT_LOCK_NOEXEC;
1916 /* Don't allow unprivileged users to reveal what is under a mount */
1917 if (list_empty(&p->mnt_expire))
1918 flags |= MNT_LOCKED;
1919 p->mnt.mnt_flags = flags;
1920 }
1921}
1922
Al Viro4b8b21f2011-11-24 19:54:23 -05001923static void cleanup_group_ids(struct mount *mnt, struct mount *end)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001924{
Al Viro315fc832011-11-24 18:57:30 -05001925 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001926
Al Viro909b0a82011-11-25 03:06:56 -05001927 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05001928 if (p->mnt_group_id && !IS_MNT_SHARED(p))
Al Viro4b8b21f2011-11-24 19:54:23 -05001929 mnt_release_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001930 }
1931}
1932
Al Viro4b8b21f2011-11-24 19:54:23 -05001933static int invent_group_ids(struct mount *mnt, bool recurse)
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001934{
Al Viro315fc832011-11-24 18:57:30 -05001935 struct mount *p;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001936
Al Viro909b0a82011-11-25 03:06:56 -05001937 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
Al Virofc7be132011-11-25 01:05:37 -05001938 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001939 int err = mnt_alloc_group_id(p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001940 if (err) {
Al Viro4b8b21f2011-11-24 19:54:23 -05001941 cleanup_group_ids(mnt, p);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01001942 return err;
1943 }
1944 }
1945 }
1946
1947 return 0;
1948}
1949
Eric W. Biedermand2921682016-09-28 00:27:17 -05001950int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
1951{
1952 unsigned int max = READ_ONCE(sysctl_mount_max);
1953 unsigned int mounts = 0, old, pending, sum;
1954 struct mount *p;
1955
1956 for (p = mnt; p; p = next_mnt(p, mnt))
1957 mounts++;
1958
1959 old = ns->mounts;
1960 pending = ns->pending_mounts;
1961 sum = old + pending;
1962 if ((old > sum) ||
1963 (pending > sum) ||
1964 (max < sum) ||
1965 (mounts > (max - sum)))
1966 return -ENOSPC;
1967
1968 ns->pending_mounts = pending + mounts;
1969 return 0;
1970}
1971
Ram Paib90fa9a2005-11-07 17:19:50 -05001972/*
1973 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -05001974 * @nd : place the mount tree @source_mnt is attached
1975 * @parent_nd : if non-null, detach the source_mnt from its parent and
1976 * store the parent mount and mountpoint dentry.
1977 * (done when source_mnt is moved)
Ram Paib90fa9a2005-11-07 17:19:50 -05001978 *
1979 * NOTE: in the table below explains the semantics when a source mount
1980 * of a given type is attached to a destination mount of a given type.
Ram Pai9676f0c2005-11-07 17:21:20 -05001981 * ---------------------------------------------------------------------------
1982 * | BIND MOUNT OPERATION |
1983 * |**************************************************************************
1984 * | source-->| shared | private | slave | unbindable |
1985 * | dest | | | | |
1986 * | | | | | | |
1987 * | v | | | | |
1988 * |**************************************************************************
1989 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1990 * | | | | | |
1991 * |non-shared| shared (+) | private | slave (*) | invalid |
1992 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -05001993 * A bind operation clones the source mount and mounts the clone on the
1994 * destination mount.
1995 *
1996 * (++) the cloned mount is propagated to all the mounts in the propagation
1997 * tree of the destination mount and the cloned mount is added to
1998 * the peer group of the source mount.
1999 * (+) the cloned mount is created under the destination mount and is marked
2000 * as shared. The cloned mount is added to the peer group of the source
2001 * mount.
Ram Pai5afe0022005-11-07 17:21:01 -05002002 * (+++) the mount is propagated to all the mounts in the propagation tree
2003 * of the destination mount and the cloned mount is made slave
2004 * of the same master as that of the source mount. The cloned mount
2005 * is marked as 'shared and slave'.
2006 * (*) the cloned mount is made a slave of the same master as that of the
2007 * source mount.
2008 *
Ram Pai9676f0c2005-11-07 17:21:20 -05002009 * ---------------------------------------------------------------------------
2010 * | MOVE MOUNT OPERATION |
2011 * |**************************************************************************
2012 * | source-->| shared | private | slave | unbindable |
2013 * | dest | | | | |
2014 * | | | | | | |
2015 * | v | | | | |
2016 * |**************************************************************************
2017 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
2018 * | | | | | |
2019 * |non-shared| shared (+*) | private | slave (*) | unbindable |
2020 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -05002021 *
2022 * (+) the mount is moved to the destination. And is then propagated to
2023 * all the mounts in the propagation tree of the destination mount.
Ram Pai21444402005-11-07 17:20:03 -05002024 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -05002025 * (+++) the mount is moved to the destination and is then propagated to
2026 * all the mounts belonging to the destination mount's propagation tree.
2027 * the mount is marked as 'shared and slave'.
2028 * (*) the mount continues to be a slave at the new location.
Ram Paib90fa9a2005-11-07 17:19:50 -05002029 *
2030 * if the source mount is a tree, the operations explained above is
2031 * applied to each mount in the tree.
2032 * Must be called without spinlocks held, since this function can sleep
2033 * in allocations.
2034 */
Al Viro0fb54e52011-11-24 19:59:16 -05002035static int attach_recursive_mnt(struct mount *source_mnt,
Al Viro84d17192013-03-15 10:53:28 -04002036 struct mount *dest_mnt,
2037 struct mountpoint *dest_mp,
Al Viro2763d112019-06-30 19:18:53 -04002038 bool moving)
Ram Paib90fa9a2005-11-07 17:19:50 -05002039{
Al Viro3bd045c2019-01-30 13:15:45 -05002040 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
Al Viro38129a12014-03-20 21:10:51 -04002041 HLIST_HEAD(tree_list);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002042 struct mnt_namespace *ns = dest_mnt->mnt_ns;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002043 struct mountpoint *smp;
Al Viro315fc832011-11-24 18:57:30 -05002044 struct mount *child, *p;
Al Viro38129a12014-03-20 21:10:51 -04002045 struct hlist_node *n;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002046 int err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002047
Eric W. Biederman1064f872017-01-20 18:28:35 +13002048 /* Preallocate a mountpoint in case the new mounts need
2049 * to be tucked under other mounts.
2050 */
2051 smp = get_mountpoint(source_mnt->mnt.mnt_root);
2052 if (IS_ERR(smp))
2053 return PTR_ERR(smp);
2054
Eric W. Biedermand2921682016-09-28 00:27:17 -05002055 /* Is there space to add these mounts to the mount namespace? */
Al Viro2763d112019-06-30 19:18:53 -04002056 if (!moving) {
Eric W. Biedermand2921682016-09-28 00:27:17 -05002057 err = count_mounts(ns, source_mnt);
2058 if (err)
2059 goto out;
2060 }
2061
Al Virofc7be132011-11-25 01:05:37 -05002062 if (IS_MNT_SHARED(dest_mnt)) {
Al Viro0fb54e52011-11-24 19:59:16 -05002063 err = invent_group_ids(source_mnt, true);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002064 if (err)
2065 goto out;
Al Viro0b1b9012014-03-21 10:14:08 -04002066 err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
Al Virof2ebb3a2014-02-27 09:35:45 -05002067 lock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002068 if (err)
2069 goto out_cleanup_ids;
Al Viro909b0a82011-11-25 03:06:56 -05002070 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
Al Viro0f0afb12011-11-24 20:43:10 -05002071 set_mnt_shared(p);
Al Viro0b1b9012014-03-21 10:14:08 -04002072 } else {
2073 lock_mount_hash();
Ram Paib90fa9a2005-11-07 17:19:50 -05002074 }
Al Viro2763d112019-06-30 19:18:53 -04002075 if (moving) {
2076 unhash_mnt(source_mnt);
Al Viro84d17192013-03-15 10:53:28 -04002077 attach_mnt(source_mnt, dest_mnt, dest_mp);
Al Viro143c8c92011-11-25 00:46:35 -05002078 touch_mnt_namespace(source_mnt->mnt_ns);
Ram Pai21444402005-11-07 17:20:03 -05002079 } else {
David Howells44dfd842018-11-05 17:40:31 +00002080 if (source_mnt->mnt_ns) {
2081 /* move from anon - the caller will destroy */
2082 list_del_init(&source_mnt->mnt_ns->list);
2083 }
Al Viro84d17192013-03-15 10:53:28 -04002084 mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002085 commit_tree(source_mnt);
Ram Pai21444402005-11-07 17:20:03 -05002086 }
Ram Paib90fa9a2005-11-07 17:19:50 -05002087
Al Viro38129a12014-03-20 21:10:51 -04002088 hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
Al Viro1d6a32a2014-03-20 20:34:43 -04002089 struct mount *q;
Al Viro38129a12014-03-20 21:10:51 -04002090 hlist_del_init(&child->mnt_hash);
Eric W. Biederman1064f872017-01-20 18:28:35 +13002091 q = __lookup_mnt(&child->mnt_parent->mnt,
2092 child->mnt_mountpoint);
2093 if (q)
2094 mnt_change_mountpoint(child, smp, q);
Al Viro3bd045c2019-01-30 13:15:45 -05002095 /* Notice when we are propagating across user namespaces */
2096 if (child->mnt_parent->mnt_ns->user_ns != user_ns)
2097 lock_mnt_tree(child);
Christian Braunerd728cf72019-06-17 23:22:14 +02002098 child->mnt.mnt_flags &= ~MNT_LOCKED;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002099 commit_tree(child);
Ram Paib90fa9a2005-11-07 17:19:50 -05002100 }
Eric W. Biederman1064f872017-01-20 18:28:35 +13002101 put_mountpoint(smp);
Al Viro719ea2f2013-09-29 11:24:49 -04002102 unlock_mount_hash();
Nick Piggin99b7db72010-08-18 04:37:39 +10002103
Ram Paib90fa9a2005-11-07 17:19:50 -05002104 return 0;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002105
2106 out_cleanup_ids:
Al Virof2ebb3a2014-02-27 09:35:45 -05002107 while (!hlist_empty(&tree_list)) {
2108 child = hlist_entry(tree_list.first, struct mount, mnt_hash);
Eric W. Biedermand2921682016-09-28 00:27:17 -05002109 child->mnt_parent->mnt_ns->pending_mounts = 0;
Eric W. Biedermane819f152014-12-24 07:20:01 -06002110 umount_tree(child, UMOUNT_SYNC);
Al Virof2ebb3a2014-02-27 09:35:45 -05002111 }
2112 unlock_mount_hash();
Al Viro0b1b9012014-03-21 10:14:08 -04002113 cleanup_group_ids(source_mnt, NULL);
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002114 out:
Eric W. Biedermand2921682016-09-28 00:27:17 -05002115 ns->pending_mounts = 0;
Eric W. Biederman1064f872017-01-20 18:28:35 +13002116
2117 read_seqlock_excl(&mount_lock);
2118 put_mountpoint(smp);
2119 read_sequnlock_excl(&mount_lock);
2120
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002121 return err;
Ram Paib90fa9a2005-11-07 17:19:50 -05002122}
2123
Al Viro84d17192013-03-15 10:53:28 -04002124static struct mountpoint *lock_mount(struct path *path)
Al Virob12cea92011-03-18 08:55:38 -04002125{
2126 struct vfsmount *mnt;
Al Viro84d17192013-03-15 10:53:28 -04002127 struct dentry *dentry = path->dentry;
Al Virob12cea92011-03-18 08:55:38 -04002128retry:
Al Viro59551022016-01-22 15:40:57 -05002129 inode_lock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002130 if (unlikely(cant_mount(dentry))) {
Al Viro59551022016-01-22 15:40:57 -05002131 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002132 return ERR_PTR(-ENOENT);
Al Virob12cea92011-03-18 08:55:38 -04002133 }
Al Viro97216be2013-03-16 15:12:40 -04002134 namespace_lock();
Al Virob12cea92011-03-18 08:55:38 -04002135 mnt = lookup_mnt(path);
Al Viro84d17192013-03-15 10:53:28 -04002136 if (likely(!mnt)) {
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002137 struct mountpoint *mp = get_mountpoint(dentry);
Al Viro84d17192013-03-15 10:53:28 -04002138 if (IS_ERR(mp)) {
Al Viro97216be2013-03-16 15:12:40 -04002139 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002140 inode_unlock(dentry->d_inode);
Al Viro84d17192013-03-15 10:53:28 -04002141 return mp;
2142 }
2143 return mp;
2144 }
Al Viro97216be2013-03-16 15:12:40 -04002145 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002146 inode_unlock(path->dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002147 path_put(path);
2148 path->mnt = mnt;
Al Viro84d17192013-03-15 10:53:28 -04002149 dentry = path->dentry = dget(mnt->mnt_root);
Al Virob12cea92011-03-18 08:55:38 -04002150 goto retry;
2151}
2152
Al Viro84d17192013-03-15 10:53:28 -04002153static void unlock_mount(struct mountpoint *where)
Al Virob12cea92011-03-18 08:55:38 -04002154{
Al Viro84d17192013-03-15 10:53:28 -04002155 struct dentry *dentry = where->m_dentry;
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002156
2157 read_seqlock_excl(&mount_lock);
Al Viro84d17192013-03-15 10:53:28 -04002158 put_mountpoint(where);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13002159 read_sequnlock_excl(&mount_lock);
2160
Al Viro328e6d92013-03-16 14:49:45 -04002161 namespace_unlock();
Al Viro59551022016-01-22 15:40:57 -05002162 inode_unlock(dentry->d_inode);
Al Virob12cea92011-03-18 08:55:38 -04002163}
2164
Al Viro84d17192013-03-15 10:53:28 -04002165static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
David Howellse462ec52017-07-17 08:45:35 +01002167 if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return -EINVAL;
2169
David Howellse36cb0b2015-01-29 12:02:35 +00002170 if (d_is_dir(mp->m_dentry) !=
2171 d_is_dir(mnt->mnt.mnt_root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return -ENOTDIR;
2173
Al Viro2763d112019-06-30 19:18:53 -04002174 return attach_recursive_mnt(mnt, p, mp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177/*
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002178 * Sanity check the flags to change_mnt_propagation.
2179 */
2180
David Howellse462ec52017-07-17 08:45:35 +01002181static int flags_to_propagation_type(int ms_flags)
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002182{
David Howellse462ec52017-07-17 08:45:35 +01002183 int type = ms_flags & ~(MS_REC | MS_SILENT);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002184
2185 /* Fail if any non-propagation flags are set */
2186 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2187 return 0;
2188 /* Only one propagation flag should be set */
2189 if (!is_power_of_2(type))
2190 return 0;
2191 return type;
2192}
2193
2194/*
Ram Pai07b20882005-11-07 17:19:07 -05002195 * recursively change the type of the mountpoint.
2196 */
David Howellse462ec52017-07-17 08:45:35 +01002197static int do_change_type(struct path *path, int ms_flags)
Ram Pai07b20882005-11-07 17:19:07 -05002198{
Al Viro315fc832011-11-24 18:57:30 -05002199 struct mount *m;
Al Viro4b8b21f2011-11-24 19:54:23 -05002200 struct mount *mnt = real_mount(path->mnt);
David Howellse462ec52017-07-17 08:45:35 +01002201 int recurse = ms_flags & MS_REC;
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002202 int type;
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002203 int err = 0;
Ram Pai07b20882005-11-07 17:19:07 -05002204
Al Viro2d92ab32008-08-02 00:51:11 -04002205 if (path->dentry != path->mnt->mnt_root)
Ram Pai07b20882005-11-07 17:19:07 -05002206 return -EINVAL;
2207
David Howellse462ec52017-07-17 08:45:35 +01002208 type = flags_to_propagation_type(ms_flags);
Valerie Aurora7a2e8a82010-08-26 11:07:22 -07002209 if (!type)
2210 return -EINVAL;
2211
Al Viro97216be2013-03-16 15:12:40 -04002212 namespace_lock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002213 if (type == MS_SHARED) {
2214 err = invent_group_ids(mnt, recurse);
2215 if (err)
2216 goto out_unlock;
2217 }
2218
Al Viro719ea2f2013-09-29 11:24:49 -04002219 lock_mount_hash();
Al Viro909b0a82011-11-25 03:06:56 -05002220 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
Al Viro0f0afb12011-11-24 20:43:10 -05002221 change_mnt_propagation(m, type);
Al Viro719ea2f2013-09-29 11:24:49 -04002222 unlock_mount_hash();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002223
2224 out_unlock:
Al Viro97216be2013-03-16 15:12:40 -04002225 namespace_unlock();
Miklos Szeredi719f5d72008-03-27 13:06:23 +01002226 return err;
Ram Pai07b20882005-11-07 17:19:07 -05002227}
2228
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07002229static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
2230{
2231 struct mount *child;
2232 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
2233 if (!is_subdir(child->mnt_mountpoint, dentry))
2234 continue;
2235
2236 if (child->mnt.mnt_flags & MNT_LOCKED)
2237 return true;
2238 }
2239 return false;
2240}
2241
Al Viroa07b2002018-11-05 17:40:30 +00002242static struct mount *__do_loopback(struct path *old_path, int recurse)
2243{
2244 struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
2245
2246 if (IS_MNT_UNBINDABLE(old))
2247 return mnt;
2248
2249 if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
2250 return mnt;
2251
2252 if (!recurse && has_locked_children(old, old_path->dentry))
2253 return mnt;
2254
2255 if (recurse)
2256 mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
2257 else
2258 mnt = clone_mnt(old, old_path->dentry, 0);
2259
2260 if (!IS_ERR(mnt))
2261 mnt->mnt.mnt_flags &= ~MNT_LOCKED;
2262
2263 return mnt;
2264}
2265
Ram Pai07b20882005-11-07 17:19:07 -05002266/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * do loopback mount.
2268 */
Al Viro808d4e32012-10-11 11:42:01 -04002269static int do_loopback(struct path *path, const char *old_name,
Eric Sandeen2dafe1c2008-02-08 04:22:12 -08002270 int recurse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Al Viro2d92ab32008-08-02 00:51:11 -04002272 struct path old_path;
Al Viroa07b2002018-11-05 17:40:30 +00002273 struct mount *mnt = NULL, *parent;
Al Viro84d17192013-03-15 10:53:28 -04002274 struct mountpoint *mp;
Al Viro57eccb82013-02-22 22:49:10 -05002275 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (!old_name || !*old_name)
2277 return -EINVAL;
Trond Myklebust815d4052011-09-26 20:36:09 -04002278 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (err)
2280 return err;
2281
Eric W. Biederman8823c072010-03-07 18:49:36 -08002282 err = -EINVAL;
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07002283 if (mnt_ns_loop(old_path.dentry))
David Howellsdd111b32017-07-04 17:25:09 +01002284 goto out;
Eric W. Biederman8823c072010-03-07 18:49:36 -08002285
Al Viro84d17192013-03-15 10:53:28 -04002286 mp = lock_mount(path);
Al Viroa07b2002018-11-05 17:40:30 +00002287 if (IS_ERR(mp)) {
2288 err = PTR_ERR(mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08002289 goto out;
Al Viroa07b2002018-11-05 17:40:30 +00002290 }
Ram Pai9676f0c2005-11-07 17:21:20 -05002291
Al Viro84d17192013-03-15 10:53:28 -04002292 parent = real_mount(path->mnt);
Al Viroe149ed22014-11-01 10:57:28 -04002293 if (!check_mnt(parent))
2294 goto out2;
2295
Al Viroa07b2002018-11-05 17:40:30 +00002296 mnt = __do_loopback(&old_path, recurse);
David Howellsbe34d1a2012-06-25 12:55:18 +01002297 if (IS_ERR(mnt)) {
2298 err = PTR_ERR(mnt);
Andrey Vagine9c5d8a2013-04-09 17:33:29 +04002299 goto out2;
David Howellsbe34d1a2012-06-25 12:55:18 +01002300 }
Al Viroccd48bc2005-11-07 17:15:04 -05002301
Al Viro84d17192013-03-15 10:53:28 -04002302 err = graft_tree(mnt, parent, mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002303 if (err) {
Al Viro719ea2f2013-09-29 11:24:49 -04002304 lock_mount_hash();
Eric W. Biedermane819f152014-12-24 07:20:01 -06002305 umount_tree(mnt, UMOUNT_SYNC);
Al Viro719ea2f2013-09-29 11:24:49 -04002306 unlock_mount_hash();
Ram Pai5b83d2c5c2005-11-07 17:16:29 -05002307 }
Al Virob12cea92011-03-18 08:55:38 -04002308out2:
Al Viro84d17192013-03-15 10:53:28 -04002309 unlock_mount(mp);
Al Viroccd48bc2005-11-07 17:15:04 -05002310out:
Al Viro2d92ab32008-08-02 00:51:11 -04002311 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return err;
2313}
2314
Al Viroa07b2002018-11-05 17:40:30 +00002315static struct file *open_detached_copy(struct path *path, bool recursive)
2316{
2317 struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
2318 struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
2319 struct mount *mnt, *p;
2320 struct file *file;
2321
2322 if (IS_ERR(ns))
2323 return ERR_CAST(ns);
2324
2325 namespace_lock();
2326 mnt = __do_loopback(path, recursive);
2327 if (IS_ERR(mnt)) {
2328 namespace_unlock();
2329 free_mnt_ns(ns);
2330 return ERR_CAST(mnt);
2331 }
2332
2333 lock_mount_hash();
2334 for (p = mnt; p; p = next_mnt(p, mnt)) {
2335 p->mnt_ns = ns;
2336 ns->mounts++;
2337 }
2338 ns->root = mnt;
2339 list_add_tail(&ns->list, &mnt->mnt_list);
2340 mntget(&mnt->mnt);
2341 unlock_mount_hash();
2342 namespace_unlock();
2343
2344 mntput(path->mnt);
2345 path->mnt = &mnt->mnt;
2346 file = dentry_open(path, O_PATH, current_cred());
2347 if (IS_ERR(file))
2348 dissolve_on_fput(path->mnt);
2349 else
2350 file->f_mode |= FMODE_NEED_UNMOUNT;
2351 return file;
2352}
2353
2354SYSCALL_DEFINE3(open_tree, int, dfd, const char *, filename, unsigned, flags)
2355{
2356 struct file *file;
2357 struct path path;
2358 int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
2359 bool detached = flags & OPEN_TREE_CLONE;
2360 int error;
2361 int fd;
2362
2363 BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
2364
2365 if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
2366 AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
2367 OPEN_TREE_CLOEXEC))
2368 return -EINVAL;
2369
2370 if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
2371 return -EINVAL;
2372
2373 if (flags & AT_NO_AUTOMOUNT)
2374 lookup_flags &= ~LOOKUP_AUTOMOUNT;
2375 if (flags & AT_SYMLINK_NOFOLLOW)
2376 lookup_flags &= ~LOOKUP_FOLLOW;
2377 if (flags & AT_EMPTY_PATH)
2378 lookup_flags |= LOOKUP_EMPTY;
2379
2380 if (detached && !may_mount())
2381 return -EPERM;
2382
2383 fd = get_unused_fd_flags(flags & O_CLOEXEC);
2384 if (fd < 0)
2385 return fd;
2386
2387 error = user_path_at(dfd, filename, lookup_flags, &path);
2388 if (unlikely(error)) {
2389 file = ERR_PTR(error);
2390 } else {
2391 if (detached)
2392 file = open_detached_copy(&path, flags & AT_RECURSIVE);
2393 else
2394 file = dentry_open(&path, O_PATH, current_cred());
2395 path_put(&path);
2396 }
2397 if (IS_ERR(file)) {
2398 put_unused_fd(fd);
2399 return PTR_ERR(file);
2400 }
2401 fd_install(fd, file);
2402 return fd;
2403}
2404
David Howells43f5e652018-11-01 23:07:25 +00002405/*
2406 * Don't allow locked mount flags to be cleared.
2407 *
2408 * No locks need to be held here while testing the various MNT_LOCK
2409 * flags because those flags can never be cleared once they are set.
2410 */
2411static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002412{
David Howells43f5e652018-11-01 23:07:25 +00002413 unsigned int fl = mnt->mnt.mnt_flags;
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002414
David Howells43f5e652018-11-01 23:07:25 +00002415 if ((fl & MNT_LOCK_READONLY) &&
2416 !(mnt_flags & MNT_READONLY))
2417 return false;
2418
2419 if ((fl & MNT_LOCK_NODEV) &&
2420 !(mnt_flags & MNT_NODEV))
2421 return false;
2422
2423 if ((fl & MNT_LOCK_NOSUID) &&
2424 !(mnt_flags & MNT_NOSUID))
2425 return false;
2426
2427 if ((fl & MNT_LOCK_NOEXEC) &&
2428 !(mnt_flags & MNT_NOEXEC))
2429 return false;
2430
2431 if ((fl & MNT_LOCK_ATIME) &&
2432 ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
2433 return false;
2434
2435 return true;
2436}
2437
2438static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
2439{
2440 bool readonly_request = (mnt_flags & MNT_READONLY);
2441
2442 if (readonly_request == __mnt_is_readonly(&mnt->mnt))
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002443 return 0;
2444
2445 if (readonly_request)
David Howells43f5e652018-11-01 23:07:25 +00002446 return mnt_make_readonly(mnt);
2447
2448 return __mnt_unmake_readonly(mnt);
2449}
2450
2451/*
2452 * Update the user-settable attributes on a mount. The caller must hold
2453 * sb->s_umount for writing.
2454 */
2455static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
2456{
2457 lock_mount_hash();
2458 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
2459 mnt->mnt.mnt_flags = mnt_flags;
2460 touch_mnt_namespace(mnt->mnt_ns);
2461 unlock_mount_hash();
2462}
2463
2464/*
2465 * Handle reconfiguration of the mountpoint only without alteration of the
2466 * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
2467 * to mount(2).
2468 */
2469static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
2470{
2471 struct super_block *sb = path->mnt->mnt_sb;
2472 struct mount *mnt = real_mount(path->mnt);
2473 int ret;
2474
2475 if (!check_mnt(mnt))
2476 return -EINVAL;
2477
2478 if (path->dentry != mnt->mnt.mnt_root)
2479 return -EINVAL;
2480
2481 if (!can_change_locked_flags(mnt, mnt_flags))
2482 return -EPERM;
2483
2484 down_write(&sb->s_umount);
2485 ret = change_mount_ro_state(mnt, mnt_flags);
2486 if (ret == 0)
2487 set_mount_attributes(mnt, mnt_flags);
2488 up_write(&sb->s_umount);
2489 return ret;
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08002490}
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492/*
2493 * change filesystem flags. dir should be a physical root of filesystem.
2494 * If you've mounted a non-root directory somewhere and want to do remount
2495 * on it - tough luck.
2496 */
David Howellse462ec52017-07-17 08:45:35 +01002497static int do_remount(struct path *path, int ms_flags, int sb_flags,
2498 int mnt_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 int err;
Al Viro2d92ab32008-08-02 00:51:11 -04002501 struct super_block *sb = path->mnt->mnt_sb;
Al Viro143c8c92011-11-25 00:46:35 -05002502 struct mount *mnt = real_mount(path->mnt);
David Howells8d0347f2018-11-04 09:28:36 -05002503 struct fs_context *fc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Al Viro143c8c92011-11-25 00:46:35 -05002505 if (!check_mnt(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return -EINVAL;
2507
Al Viro2d92ab32008-08-02 00:51:11 -04002508 if (path->dentry != path->mnt->mnt_root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return -EINVAL;
2510
David Howells43f5e652018-11-01 23:07:25 +00002511 if (!can_change_locked_flags(mnt, mnt_flags))
Eric W. Biederman07b64552014-07-28 17:10:56 -07002512 return -EPERM;
Eric W. Biederman9566d672014-07-28 17:26:07 -07002513
David Howells8d0347f2018-11-04 09:28:36 -05002514 fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
2515 if (IS_ERR(fc))
2516 return PTR_ERR(fc);
Eric Parisff36fe22011-03-03 16:09:14 -05002517
David Howells8d0347f2018-11-04 09:28:36 -05002518 err = parse_monolithic_mount_data(fc, data);
2519 if (!err) {
2520 down_write(&sb->s_umount);
2521 err = -EPERM;
2522 if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
2523 err = reconfigure_super(fc);
2524 if (!err)
2525 set_mount_attributes(mnt, mnt_flags);
2526 }
2527 up_write(&sb->s_umount);
Dan Williams0e55a7c2008-09-26 19:01:20 -07002528 }
David Howells8d0347f2018-11-04 09:28:36 -05002529 put_fs_context(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return err;
2531}
2532
Al Virocbbe3622011-11-24 20:01:19 -05002533static inline int tree_contains_unbindable(struct mount *mnt)
Ram Pai9676f0c2005-11-07 17:21:20 -05002534{
Al Viro315fc832011-11-24 18:57:30 -05002535 struct mount *p;
Al Viro909b0a82011-11-25 03:06:56 -05002536 for (p = mnt; p; p = next_mnt(p, mnt)) {
Al Virofc7be132011-11-25 01:05:37 -05002537 if (IS_MNT_UNBINDABLE(p))
Ram Pai9676f0c2005-11-07 17:21:20 -05002538 return 1;
2539 }
2540 return 0;
2541}
2542
David Howells44dfd842018-11-05 17:40:31 +00002543/*
2544 * Check that there aren't references to earlier/same mount namespaces in the
2545 * specified subtree. Such references can act as pins for mount namespaces
2546 * that aren't checked by the mount-cycle checking code, thereby allowing
2547 * cycles to be made.
2548 */
2549static bool check_for_nsfs_mounts(struct mount *subtree)
2550{
2551 struct mount *p;
2552 bool ret = false;
2553
2554 lock_mount_hash();
2555 for (p = subtree; p; p = next_mnt(p, subtree))
2556 if (mnt_ns_loop(p->mnt.mnt_root))
2557 goto out;
2558
2559 ret = true;
2560out:
2561 unlock_mount_hash();
2562 return ret;
2563}
2564
David Howells2db154b2018-11-05 17:40:30 +00002565static int do_move_mount(struct path *old_path, struct path *new_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
David Howells44dfd842018-11-05 17:40:31 +00002567 struct mnt_namespace *ns;
Al Viro676da582011-11-24 21:47:05 -05002568 struct mount *p;
Al Viro0fb54e52011-11-24 19:59:16 -05002569 struct mount *old;
Al Viro2763d112019-06-30 19:18:53 -04002570 struct mount *parent;
2571 struct mountpoint *mp, *old_mp;
Al Viro57eccb82013-02-22 22:49:10 -05002572 int err;
David Howells44dfd842018-11-05 17:40:31 +00002573 bool attached;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
David Howells2db154b2018-11-05 17:40:30 +00002575 mp = lock_mount(new_path);
Al Viro84d17192013-03-15 10:53:28 -04002576 if (IS_ERR(mp))
David Howells2db154b2018-11-05 17:40:30 +00002577 return PTR_ERR(mp);
David Howellscc53ce52011-01-14 18:45:26 +00002578
David Howells2db154b2018-11-05 17:40:30 +00002579 old = real_mount(old_path->mnt);
2580 p = real_mount(new_path->mnt);
Al Viro2763d112019-06-30 19:18:53 -04002581 parent = old->mnt_parent;
David Howells44dfd842018-11-05 17:40:31 +00002582 attached = mnt_has_parent(old);
Al Viro2763d112019-06-30 19:18:53 -04002583 old_mp = old->mnt_mp;
David Howells44dfd842018-11-05 17:40:31 +00002584 ns = old->mnt_ns;
Al Viro143c8c92011-11-25 00:46:35 -05002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 err = -EINVAL;
David Howells44dfd842018-11-05 17:40:31 +00002587 /* The mountpoint must be in our namespace. */
2588 if (!check_mnt(p))
David Howells2db154b2018-11-05 17:40:30 +00002589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Eric Biggers570d7a92019-06-29 13:27:44 -07002591 /* The thing moved must be mounted... */
2592 if (!is_mounted(&old->mnt))
David Howells44dfd842018-11-05 17:40:31 +00002593 goto out;
2594
Eric Biggers570d7a92019-06-29 13:27:44 -07002595 /* ... and either ours or the root of anon namespace */
2596 if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
David Howells2db154b2018-11-05 17:40:30 +00002597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
David Howells2db154b2018-11-05 17:40:30 +00002599 if (old->mnt.mnt_flags & MNT_LOCKED)
2600 goto out;
2601
2602 if (old_path->dentry != old_path->mnt->mnt_root)
2603 goto out;
2604
2605 if (d_is_dir(new_path->dentry) !=
2606 d_is_dir(old_path->dentry))
2607 goto out;
Ram Pai21444402005-11-07 17:20:03 -05002608 /*
2609 * Don't move a mount residing in a shared parent.
2610 */
Al Viro2763d112019-06-30 19:18:53 -04002611 if (attached && IS_MNT_SHARED(parent))
David Howells2db154b2018-11-05 17:40:30 +00002612 goto out;
Ram Pai9676f0c2005-11-07 17:21:20 -05002613 /*
2614 * Don't move a mount tree containing unbindable mounts to a destination
2615 * mount which is shared.
2616 */
Al Virofc7be132011-11-25 01:05:37 -05002617 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
David Howells2db154b2018-11-05 17:40:30 +00002618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 err = -ELOOP;
David Howells44dfd842018-11-05 17:40:31 +00002620 if (!check_for_nsfs_mounts(old))
2621 goto out;
Al Virofc7be132011-11-25 01:05:37 -05002622 for (; mnt_has_parent(p); p = p->mnt_parent)
Al Viro676da582011-11-24 21:47:05 -05002623 if (p == old)
David Howells2db154b2018-11-05 17:40:30 +00002624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
David Howells2db154b2018-11-05 17:40:30 +00002626 err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
Al Viro2763d112019-06-30 19:18:53 -04002627 attached);
Jan Blunck4ac91372008-02-14 19:34:32 -08002628 if (err)
David Howells2db154b2018-11-05 17:40:30 +00002629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 /* if the mount is moved, it should no longer be expire
2632 * automatically */
Al Viro6776db3d2011-11-25 00:22:05 -05002633 list_del_init(&old->mnt_expire);
Al Viro2763d112019-06-30 19:18:53 -04002634 if (attached)
2635 put_mountpoint(old_mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636out:
David Howells2db154b2018-11-05 17:40:30 +00002637 unlock_mount(mp);
David Howells44dfd842018-11-05 17:40:31 +00002638 if (!err) {
Al Viro2763d112019-06-30 19:18:53 -04002639 if (attached)
2640 mntput_no_expire(parent);
2641 else
David Howells44dfd842018-11-05 17:40:31 +00002642 free_mnt_ns(ns);
2643 }
David Howells2db154b2018-11-05 17:40:30 +00002644 return err;
2645}
2646
2647static int do_move_mount_old(struct path *path, const char *old_name)
2648{
2649 struct path old_path;
2650 int err;
2651
2652 if (!old_name || !*old_name)
2653 return -EINVAL;
2654
2655 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
2656 if (err)
2657 return err;
2658
2659 err = do_move_mount(&old_path, path);
Al Viro2d92ab32008-08-02 00:51:11 -04002660 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return err;
2662}
2663
Al Viro9d412a42011-03-17 22:08:28 -04002664/*
2665 * add a mount into a namespace's mount tree
2666 */
Al Viro95bc5f22011-11-25 00:30:56 -05002667static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
Al Viro9d412a42011-03-17 22:08:28 -04002668{
Al Viro84d17192013-03-15 10:53:28 -04002669 struct mountpoint *mp;
2670 struct mount *parent;
Al Viro9d412a42011-03-17 22:08:28 -04002671 int err;
2672
Al Virof2ebb3a2014-02-27 09:35:45 -05002673 mnt_flags &= ~MNT_INTERNAL_FLAGS;
Al Viro9d412a42011-03-17 22:08:28 -04002674
Al Viro84d17192013-03-15 10:53:28 -04002675 mp = lock_mount(path);
2676 if (IS_ERR(mp))
2677 return PTR_ERR(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002678
Al Viro84d17192013-03-15 10:53:28 -04002679 parent = real_mount(path->mnt);
Al Viro9d412a42011-03-17 22:08:28 -04002680 err = -EINVAL;
Al Viro84d17192013-03-15 10:53:28 -04002681 if (unlikely(!check_mnt(parent))) {
Al Viro156cacb2012-09-21 08:19:02 -04002682 /* that's acceptable only for automounts done in private ns */
2683 if (!(mnt_flags & MNT_SHRINKABLE))
2684 goto unlock;
2685 /* ... and for those we'd better have mountpoint still alive */
Al Viro84d17192013-03-15 10:53:28 -04002686 if (!parent->mnt_ns)
Al Viro156cacb2012-09-21 08:19:02 -04002687 goto unlock;
2688 }
Al Viro9d412a42011-03-17 22:08:28 -04002689
2690 /* Refuse the same filesystem on the same mount point */
2691 err = -EBUSY;
Al Viro95bc5f22011-11-25 00:30:56 -05002692 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
Al Viro9d412a42011-03-17 22:08:28 -04002693 path->mnt->mnt_root == path->dentry)
2694 goto unlock;
2695
2696 err = -EINVAL;
David Howellse36cb0b2015-01-29 12:02:35 +00002697 if (d_is_symlink(newmnt->mnt.mnt_root))
Al Viro9d412a42011-03-17 22:08:28 -04002698 goto unlock;
2699
Al Viro95bc5f22011-11-25 00:30:56 -05002700 newmnt->mnt.mnt_flags = mnt_flags;
Al Viro84d17192013-03-15 10:53:28 -04002701 err = graft_tree(newmnt, parent, mp);
Al Viro9d412a42011-03-17 22:08:28 -04002702
2703unlock:
Al Viro84d17192013-03-15 10:53:28 -04002704 unlock_mount(mp);
Al Viro9d412a42011-03-17 22:08:28 -04002705 return err;
2706}
Al Virob1e75df2011-01-17 01:47:59 -05002707
David Howells132e4602018-11-04 07:43:08 -05002708static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
2709
2710/*
2711 * Create a new mount using a superblock configuration and request it
2712 * be added to the namespace tree.
2713 */
2714static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
2715 unsigned int mnt_flags)
2716{
2717 struct vfsmount *mnt;
2718 struct super_block *sb = fc->root->d_sb;
2719 int error;
2720
Al Viroc9ce29e2018-12-20 15:04:50 -05002721 error = security_sb_kern_mount(sb);
2722 if (!error && mount_too_revealing(sb, &mnt_flags))
2723 error = -EPERM;
2724
2725 if (unlikely(error)) {
2726 fc_drop_locked(fc);
2727 return error;
David Howells132e4602018-11-04 07:43:08 -05002728 }
2729
2730 up_write(&sb->s_umount);
2731
2732 mnt = vfs_create_mount(fc);
2733 if (IS_ERR(mnt))
2734 return PTR_ERR(mnt);
2735
2736 error = do_add_mount(real_mount(mnt), mountpoint, mnt_flags);
2737 if (error < 0)
2738 mntput(mnt);
2739 return error;
2740}
Eric W. Biederman1b852bc2015-05-08 23:22:29 -05002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
2743 * create a new mount for userspace and request it to be added into the
2744 * namespace's tree
2745 */
David Howellse462ec52017-07-17 08:45:35 +01002746static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
Al Viro808d4e32012-10-11 11:42:01 -04002747 int mnt_flags, const char *name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002749 struct file_system_type *type;
Al Viroa0c9a8b2018-11-04 07:18:51 -05002750 struct fs_context *fc;
2751 const char *subtype = NULL;
2752 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002754 if (!fstype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return -EINVAL;
2756
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002757 type = get_fs_type(fstype);
2758 if (!type)
2759 return -ENODEV;
2760
Al Viroa0c9a8b2018-11-04 07:18:51 -05002761 if (type->fs_flags & FS_HAS_SUBTYPE) {
2762 subtype = strchr(fstype, '.');
2763 if (subtype) {
2764 subtype++;
2765 if (!*subtype) {
2766 put_filesystem(type);
2767 return -EINVAL;
2768 }
2769 } else {
2770 subtype = "";
2771 }
Eric W. Biederman8654df42016-06-09 16:06:06 -05002772 }
2773
Al Viroa0c9a8b2018-11-04 07:18:51 -05002774 fc = fs_context_for_mount(type, sb_flags);
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07002775 put_filesystem(type);
Al Viroa0c9a8b2018-11-04 07:18:51 -05002776 if (IS_ERR(fc))
2777 return PTR_ERR(fc);
2778
David Howells3e1aeb02018-11-01 23:07:25 +00002779 if (subtype)
2780 err = vfs_parse_fs_string(fc, "subtype",
2781 subtype, strlen(subtype));
2782 if (!err && name)
2783 err = vfs_parse_fs_string(fc, "source", name, strlen(name));
Al Viroa0c9a8b2018-11-04 07:18:51 -05002784 if (!err)
2785 err = parse_monolithic_mount_data(fc, data);
2786 if (!err)
2787 err = vfs_get_tree(fc);
David Howells132e4602018-11-04 07:43:08 -05002788 if (!err)
2789 err = do_new_mount_fc(fc, path, mnt_flags);
Al Viroa0c9a8b2018-11-04 07:18:51 -05002790
Al Viroa0c9a8b2018-11-04 07:18:51 -05002791 put_fs_context(fc);
Al Viro15f9a3f2011-01-17 01:41:58 -05002792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Al Viro19a167a2011-01-17 01:35:23 -05002795int finish_automount(struct vfsmount *m, struct path *path)
2796{
Al Viro6776db3d2011-11-25 00:22:05 -05002797 struct mount *mnt = real_mount(m);
Al Viro19a167a2011-01-17 01:35:23 -05002798 int err;
2799 /* The new mount record should have at least 2 refs to prevent it being
2800 * expired before we get a chance to add it
2801 */
Al Viro6776db3d2011-11-25 00:22:05 -05002802 BUG_ON(mnt_get_count(mnt) < 2);
Al Viro19a167a2011-01-17 01:35:23 -05002803
2804 if (m->mnt_sb == path->mnt->mnt_sb &&
2805 m->mnt_root == path->dentry) {
Al Virob1e75df2011-01-17 01:47:59 -05002806 err = -ELOOP;
2807 goto fail;
Al Viro19a167a2011-01-17 01:35:23 -05002808 }
2809
Al Viro95bc5f22011-11-25 00:30:56 -05002810 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
Al Virob1e75df2011-01-17 01:47:59 -05002811 if (!err)
2812 return 0;
2813fail:
2814 /* remove m from any expiration list it may be on */
Al Viro6776db3d2011-11-25 00:22:05 -05002815 if (!list_empty(&mnt->mnt_expire)) {
Al Viro97216be2013-03-16 15:12:40 -04002816 namespace_lock();
Al Viro6776db3d2011-11-25 00:22:05 -05002817 list_del_init(&mnt->mnt_expire);
Al Viro97216be2013-03-16 15:12:40 -04002818 namespace_unlock();
Al Viro19a167a2011-01-17 01:35:23 -05002819 }
Al Virob1e75df2011-01-17 01:47:59 -05002820 mntput(m);
2821 mntput(m);
Al Viro19a167a2011-01-17 01:35:23 -05002822 return err;
2823}
2824
David Howellsea5b7782011-01-14 19:10:03 +00002825/**
2826 * mnt_set_expiry - Put a mount on an expiration list
2827 * @mnt: The mount to list.
2828 * @expiry_list: The list to add the mount to.
2829 */
2830void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
2831{
Al Viro97216be2013-03-16 15:12:40 -04002832 namespace_lock();
David Howellsea5b7782011-01-14 19:10:03 +00002833
Al Viro6776db3d2011-11-25 00:22:05 -05002834 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
David Howellsea5b7782011-01-14 19:10:03 +00002835
Al Viro97216be2013-03-16 15:12:40 -04002836 namespace_unlock();
David Howellsea5b7782011-01-14 19:10:03 +00002837}
2838EXPORT_SYMBOL(mnt_set_expiry);
2839
2840/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * process a list of expirable mountpoints with the intent of discarding any
2842 * mountpoints that aren't in use and haven't been touched since last we came
2843 * here
2844 */
2845void mark_mounts_for_expiry(struct list_head *mounts)
2846{
Al Viro761d5c32011-11-24 21:07:43 -05002847 struct mount *mnt, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 LIST_HEAD(graveyard);
2849
2850 if (list_empty(mounts))
2851 return;
2852
Al Viro97216be2013-03-16 15:12:40 -04002853 namespace_lock();
Al Viro719ea2f2013-09-29 11:24:49 -04002854 lock_mount_hash();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 /* extract from the expiration list every vfsmount that matches the
2857 * following criteria:
2858 * - only referenced by its parent vfsmount
2859 * - still marked for expiry (marked on the last call here; marks are
2860 * cleared by mntput())
2861 */
Al Viro6776db3d2011-11-25 00:22:05 -05002862 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Al Viro863d6842011-11-25 00:57:42 -05002863 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
Al Viro1ab59732011-11-24 21:35:16 -05002864 propagate_mount_busy(mnt, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 continue;
Al Viro6776db3d2011-11-25 00:22:05 -05002866 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
Al Virobcc5c7d2008-03-22 00:21:53 -04002868 while (!list_empty(&graveyard)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002869 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002870 touch_mnt_namespace(mnt->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002871 umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002872 }
Al Viro719ea2f2013-09-29 11:24:49 -04002873 unlock_mount_hash();
Al Viro3ab6abe2013-03-16 14:42:19 -04002874 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
2878
2879/*
Trond Myklebust5528f9112006-06-09 09:34:17 -04002880 * Ripoff of 'select_parent()'
2881 *
2882 * search the list of submounts for a given mountpoint, and move any
2883 * shrinkable submounts to the 'graveyard' list.
2884 */
Al Viro692afc32011-11-24 21:15:14 -05002885static int select_submounts(struct mount *parent, struct list_head *graveyard)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002886{
Al Viro692afc32011-11-24 21:15:14 -05002887 struct mount *this_parent = parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002888 struct list_head *next;
2889 int found = 0;
2890
2891repeat:
Al Viro6b41d532011-11-24 23:24:33 -05002892 next = this_parent->mnt_mounts.next;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002893resume:
Al Viro6b41d532011-11-24 23:24:33 -05002894 while (next != &this_parent->mnt_mounts) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002895 struct list_head *tmp = next;
Al Viro6b41d532011-11-24 23:24:33 -05002896 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002897
2898 next = tmp->next;
Al Viro692afc32011-11-24 21:15:14 -05002899 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
Trond Myklebust5528f9112006-06-09 09:34:17 -04002900 continue;
2901 /*
2902 * Descend a level if the d_mounts list is non-empty.
2903 */
Al Viro6b41d532011-11-24 23:24:33 -05002904 if (!list_empty(&mnt->mnt_mounts)) {
Trond Myklebust5528f9112006-06-09 09:34:17 -04002905 this_parent = mnt;
2906 goto repeat;
2907 }
2908
Al Viro1ab59732011-11-24 21:35:16 -05002909 if (!propagate_mount_busy(mnt, 1)) {
Al Viro6776db3d2011-11-25 00:22:05 -05002910 list_move_tail(&mnt->mnt_expire, graveyard);
Trond Myklebust5528f9112006-06-09 09:34:17 -04002911 found++;
2912 }
2913 }
2914 /*
2915 * All done at this level ... ascend and resume the search
2916 */
2917 if (this_parent != parent) {
Al Viro6b41d532011-11-24 23:24:33 -05002918 next = this_parent->mnt_child.next;
Al Viro0714a532011-11-24 22:19:58 -05002919 this_parent = this_parent->mnt_parent;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002920 goto resume;
2921 }
2922 return found;
2923}
2924
2925/*
2926 * process a list of expirable mountpoints with the intent of discarding any
2927 * submounts of a specific parent mountpoint
Nick Piggin99b7db72010-08-18 04:37:39 +10002928 *
Al Viro48a066e2013-09-29 22:06:07 -04002929 * mount_lock must be held for write
Trond Myklebust5528f9112006-06-09 09:34:17 -04002930 */
Al Virob54b9be2013-03-16 14:39:34 -04002931static void shrink_submounts(struct mount *mnt)
Trond Myklebust5528f9112006-06-09 09:34:17 -04002932{
2933 LIST_HEAD(graveyard);
Al Viro761d5c32011-11-24 21:07:43 -05002934 struct mount *m;
Trond Myklebust5528f9112006-06-09 09:34:17 -04002935
Trond Myklebust5528f9112006-06-09 09:34:17 -04002936 /* extract submounts of 'mountpoint' from the expiration list */
Al Viroc35038b2008-03-22 00:46:23 -04002937 while (select_submounts(mnt, &graveyard)) {
Al Virobcc5c7d2008-03-22 00:21:53 -04002938 while (!list_empty(&graveyard)) {
Al Viro761d5c32011-11-24 21:07:43 -05002939 m = list_first_entry(&graveyard, struct mount,
Al Viro6776db3d2011-11-25 00:22:05 -05002940 mnt_expire);
Al Viro143c8c92011-11-25 00:46:35 -05002941 touch_mnt_namespace(m->mnt_ns);
Eric W. Biedermane819f152014-12-24 07:20:01 -06002942 umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
Al Virobcc5c7d2008-03-22 00:21:53 -04002943 }
2944 }
Trond Myklebust5528f9112006-06-09 09:34:17 -04002945}
2946
Trond Myklebust5528f9112006-06-09 09:34:17 -04002947/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 * Some copy_from_user() implementations do not return the exact number of
2949 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2950 * Note that this function differs from copy_from_user() in that it will oops
2951 * on bad values of `to', rather than returning a short copy.
2952 */
Ram Paib58fed82005-11-07 17:16:09 -05002953static long exact_copy_from_user(void *to, const void __user * from,
2954 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 char *t = to;
2957 const char __user *f = from;
2958 char c;
2959
Linus Torvalds96d4f262019-01-03 18:57:57 -08002960 if (!access_ok(from, n))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return n;
2962
2963 while (n) {
2964 if (__get_user(c, f)) {
2965 memset(t, 0, n);
2966 break;
2967 }
2968 *t++ = c;
2969 f++;
2970 n--;
2971 }
2972 return n;
2973}
2974
Al Virob40ef862015-12-14 18:44:44 -05002975void *copy_mount_options(const void __user * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 unsigned long size;
Al Virob40ef862015-12-14 18:44:44 -05002979 char *copy;
Ram Paib58fed82005-11-07 17:16:09 -05002980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (!data)
Al Virob40ef862015-12-14 18:44:44 -05002982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Al Virob40ef862015-12-14 18:44:44 -05002984 copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
2985 if (!copy)
2986 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 /* We only care that *some* data at the address the user
2989 * gave us is valid. Just in case, we'll zero
2990 * the remainder of the page.
2991 */
2992 /* copy_from_user cannot cross TASK_SIZE ! */
2993 size = TASK_SIZE - (unsigned long)data;
2994 if (size > PAGE_SIZE)
2995 size = PAGE_SIZE;
2996
Al Virob40ef862015-12-14 18:44:44 -05002997 i = size - exact_copy_from_user(copy, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (!i) {
Al Virob40ef862015-12-14 18:44:44 -05002999 kfree(copy);
3000 return ERR_PTR(-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002 if (i != PAGE_SIZE)
Al Virob40ef862015-12-14 18:44:44 -05003003 memset(copy + i, 0, PAGE_SIZE - i);
3004 return copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Tim Gardnerb8850d12014-08-28 11:26:03 -06003007char *copy_mount_string(const void __user *data)
Vegard Nossumeca6f532009-09-18 13:05:45 -07003008{
Chandan Rajendrafbdb4402019-01-22 12:21:52 +05303009 return data ? strndup_user(data, PATH_MAX) : NULL;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003010}
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012/*
3013 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
3014 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
3015 *
3016 * data is a (void *) that can point to any structure up to
3017 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
3018 * information (or be NULL).
3019 *
3020 * Pre-0.97 versions of mount() didn't have a flags word.
3021 * When the flags word was introduced its top half was required
3022 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
3023 * Therefore, if this magic number is present, it carries no information
3024 * and must be discarded.
3025 */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09003026long do_mount(const char *dev_name, const char __user *dir_name,
Al Viro808d4e32012-10-11 11:42:01 -04003027 const char *type_page, unsigned long flags, void *data_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Al Viro2d92ab32008-08-02 00:51:11 -04003029 struct path path;
David Howellse462ec52017-07-17 08:45:35 +01003030 unsigned int mnt_flags = 0, sb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 /* Discard magic */
3034 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
3035 flags &= ~MS_MGC_MSK;
3036
3037 /* Basic sanity checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (data_page)
3039 ((char *)data_page)[PAGE_SIZE - 1] = 0;
3040
David Howellse462ec52017-07-17 08:45:35 +01003041 if (flags & MS_NOUSER)
3042 return -EINVAL;
3043
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09003044 /* ... and get the mountpoint */
Seunghun Lee5e6123f2014-09-14 22:15:10 +09003045 retval = user_path(dir_name, &path);
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09003046 if (retval)
3047 return retval;
3048
3049 retval = security_sb_mount(dev_name, &path,
3050 type_page, flags, data_page);
Al Viro0d5cadb2013-05-04 14:40:51 -04003051 if (!retval && !may_mount())
3052 retval = -EPERM;
David Howellse462ec52017-07-17 08:45:35 +01003053 if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
Jeff Layton9e8925b2015-11-16 09:49:34 -05003054 retval = -EPERM;
Tetsuo Handaa27ab9f22009-10-04 21:49:49 +09003055 if (retval)
3056 goto dput_out;
3057
Andi Kleen613cbe32009-04-19 18:40:43 +02003058 /* Default to relatime unless overriden */
3059 if (!(flags & MS_NOATIME))
3060 mnt_flags |= MNT_RELATIME;
Matthew Garrett0a1c01c2009-03-26 17:53:14 +00003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 /* Separate the per-mountpoint flags */
3063 if (flags & MS_NOSUID)
3064 mnt_flags |= MNT_NOSUID;
3065 if (flags & MS_NODEV)
3066 mnt_flags |= MNT_NODEV;
3067 if (flags & MS_NOEXEC)
3068 mnt_flags |= MNT_NOEXEC;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08003069 if (flags & MS_NOATIME)
3070 mnt_flags |= MNT_NOATIME;
3071 if (flags & MS_NODIRATIME)
3072 mnt_flags |= MNT_NODIRATIME;
Matthew Garrettd0adde52009-03-26 17:49:56 +00003073 if (flags & MS_STRICTATIME)
3074 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
David Howellsa9e5b732018-04-20 13:35:02 +01003075 if (flags & MS_RDONLY)
Dave Hansen2e4b7fc2008-02-15 14:38:00 -08003076 mnt_flags |= MNT_READONLY;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08003077
Eric W. Biedermanffbc6f02014-07-28 17:36:04 -07003078 /* The default atime for remount is preservation */
3079 if ((flags & MS_REMOUNT) &&
3080 ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
3081 MS_STRICTATIME)) == 0)) {
3082 mnt_flags &= ~MNT_ATIME_MASK;
3083 mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
3084 }
3085
David Howellse462ec52017-07-17 08:45:35 +01003086 sb_flags = flags & (SB_RDONLY |
3087 SB_SYNCHRONOUS |
3088 SB_MANDLOCK |
3089 SB_DIRSYNC |
3090 SB_SILENT |
Mimi Zohar917086f2017-10-08 00:28:21 -04003091 SB_POSIXACL |
Markus Trippelsdorfd7ee9462017-10-11 07:01:31 +02003092 SB_LAZYTIME |
Mimi Zohar917086f2017-10-08 00:28:21 -04003093 SB_I_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
David Howells43f5e652018-11-01 23:07:25 +00003095 if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
3096 retval = do_reconfigure_mnt(&path, mnt_flags);
3097 else if (flags & MS_REMOUNT)
David Howellse462ec52017-07-17 08:45:35 +01003098 retval = do_remount(&path, flags, sb_flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 data_page);
3100 else if (flags & MS_BIND)
Al Viro2d92ab32008-08-02 00:51:11 -04003101 retval = do_loopback(&path, dev_name, flags & MS_REC);
Ram Pai9676f0c2005-11-07 17:21:20 -05003102 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Al Viro2d92ab32008-08-02 00:51:11 -04003103 retval = do_change_type(&path, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 else if (flags & MS_MOVE)
David Howells2db154b2018-11-05 17:40:30 +00003105 retval = do_move_mount_old(&path, dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 else
David Howellse462ec52017-07-17 08:45:35 +01003107 retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 dev_name, data_page);
3109dput_out:
Al Viro2d92ab32008-08-02 00:51:11 -04003110 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 return retval;
3112}
3113
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003114static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
3115{
3116 return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
3117}
3118
3119static void dec_mnt_namespaces(struct ucounts *ucounts)
3120{
3121 dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
3122}
3123
Eric W. Biederman771b1372012-07-26 21:08:32 -07003124static void free_mnt_ns(struct mnt_namespace *ns)
3125{
Al Viro74e83122019-01-30 13:30:21 -05003126 if (!is_anon_ns(ns))
3127 ns_free_inum(&ns->ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003128 dec_mnt_namespaces(ns->ucounts);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003129 put_user_ns(ns->user_ns);
3130 kfree(ns);
3131}
3132
Eric W. Biederman8823c072010-03-07 18:49:36 -08003133/*
3134 * Assign a sequence number so we can detect when we attempt to bind
3135 * mount a reference to an older mount namespace into the current
3136 * mount namespace, preventing reference counting loops. A 64bit
3137 * number incrementing at 10Ghz will take 12,427 years to wrap which
3138 * is effectively never, so we can ignore the possibility.
3139 */
3140static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
3141
Al Viro74e83122019-01-30 13:30:21 -05003142static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003143{
3144 struct mnt_namespace *new_ns;
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003145 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -07003146 int ret;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003147
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003148 ucounts = inc_mnt_namespaces(user_ns);
3149 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -05003150 return ERR_PTR(-ENOSPC);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003151
Al Viro74e83122019-01-30 13:30:21 -05003152 new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003153 if (!new_ns) {
3154 dec_mnt_namespaces(ucounts);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003155 return ERR_PTR(-ENOMEM);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003156 }
Al Viro74e83122019-01-30 13:30:21 -05003157 if (!anon) {
3158 ret = ns_alloc_inum(&new_ns->ns);
3159 if (ret) {
3160 kfree(new_ns);
3161 dec_mnt_namespaces(ucounts);
3162 return ERR_PTR(ret);
3163 }
Eric W. Biederman98f842e2011-06-15 10:21:48 -07003164 }
Al Viro33c42942014-11-01 02:32:53 -04003165 new_ns->ns.ops = &mntns_operations;
Al Viro74e83122019-01-30 13:30:21 -05003166 if (!anon)
3167 new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003168 atomic_set(&new_ns->count, 1);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003169 INIT_LIST_HEAD(&new_ns->list);
3170 init_waitqueue_head(&new_ns->poll);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003171 new_ns->user_ns = get_user_ns(user_ns);
Eric W. Biederman537f7cc2016-08-08 14:37:37 -05003172 new_ns->ucounts = ucounts;
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003173 return new_ns;
3174}
3175
Emese Revfy0766f782016-06-20 20:42:34 +02003176__latent_entropy
Al Viro9559f682013-09-28 20:47:57 -04003177struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
3178 struct user_namespace *user_ns, struct fs_struct *new_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003180 struct mnt_namespace *new_ns;
Al Viro7f2da1e2008-05-10 20:44:54 -04003181 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
Al Viro315fc832011-11-24 18:57:30 -05003182 struct mount *p, *q;
Al Viro9559f682013-09-28 20:47:57 -04003183 struct mount *old;
Al Virocb338d02011-11-24 20:55:08 -05003184 struct mount *new;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003185 int copy_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Al Viro9559f682013-09-28 20:47:57 -04003187 BUG_ON(!ns);
3188
3189 if (likely(!(flags & CLONE_NEWNS))) {
3190 get_mnt_ns(ns);
3191 return ns;
3192 }
3193
3194 old = ns->root;
3195
Al Viro74e83122019-01-30 13:30:21 -05003196 new_ns = alloc_mnt_ns(user_ns, false);
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003197 if (IS_ERR(new_ns))
3198 return new_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Al Viro97216be2013-03-16 15:12:40 -04003200 namespace_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 /* First pass: copy the tree topology */
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003202 copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
Al Viro9559f682013-09-28 20:47:57 -04003203 if (user_ns != ns->user_ns)
Al Viro3bd045c2019-01-30 13:15:45 -05003204 copy_flags |= CL_SHARED_TO_SLAVE;
Eric W. Biederman7a472ef2012-07-31 13:13:04 -07003205 new = copy_tree(old, old->mnt.mnt_root, copy_flags);
David Howellsbe34d1a2012-06-25 12:55:18 +01003206 if (IS_ERR(new)) {
Al Viro328e6d92013-03-16 14:49:45 -04003207 namespace_unlock();
Eric W. Biederman771b1372012-07-26 21:08:32 -07003208 free_mnt_ns(new_ns);
David Howellsbe34d1a2012-06-25 12:55:18 +01003209 return ERR_CAST(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Al Viro3bd045c2019-01-30 13:15:45 -05003211 if (user_ns != ns->user_ns) {
3212 lock_mount_hash();
3213 lock_mnt_tree(new);
3214 unlock_mount_hash();
3215 }
Al Virobe08d6d2011-12-06 13:32:36 -05003216 new_ns->root = new;
Al Viro1a4eeaf2011-11-25 02:19:55 -05003217 list_add_tail(&new_ns->list, &new->mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 /*
3220 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
3221 * as belonging to new namespace. We have already acquired a private
3222 * fs_struct, so tsk->fs->lock is not needed.
3223 */
Al Viro909b0a82011-11-25 03:06:56 -05003224 p = old;
Al Virocb338d02011-11-24 20:55:08 -05003225 q = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 while (p) {
Al Viro143c8c92011-11-25 00:46:35 -05003227 q->mnt_ns = new_ns;
Eric W. Biedermand2921682016-09-28 00:27:17 -05003228 new_ns->mounts++;
Al Viro9559f682013-09-28 20:47:57 -04003229 if (new_fs) {
3230 if (&p->mnt == new_fs->root.mnt) {
3231 new_fs->root.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003232 rootmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
Al Viro9559f682013-09-28 20:47:57 -04003234 if (&p->mnt == new_fs->pwd.mnt) {
3235 new_fs->pwd.mnt = mntget(&q->mnt);
Al Viro315fc832011-11-24 18:57:30 -05003236 pwdmnt = &p->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Al Viro909b0a82011-11-25 03:06:56 -05003239 p = next_mnt(p, old);
3240 q = next_mnt(q, new);
Eric W. Biederman4ce5d2b2013-03-30 01:35:18 -07003241 if (!q)
3242 break;
3243 while (p->mnt.mnt_root != q->mnt.mnt_root)
3244 p = next_mnt(p, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Al Viro328e6d92013-03-16 14:49:45 -04003246 namespace_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (rootmnt)
Al Virof03c6592011-01-14 22:30:21 -05003249 mntput(rootmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (pwdmnt)
Al Virof03c6592011-01-14 22:30:21 -05003251 mntput(pwdmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
JANAK DESAI741a2952006-02-07 12:59:00 -08003253 return new_ns;
3254}
3255
Al Viro74e83122019-01-30 13:30:21 -05003256struct dentry *mount_subtree(struct vfsmount *m, const char *name)
Trond Myklebustcf8d2c12009-06-22 15:09:13 -04003257{
Al Viro74e83122019-01-30 13:30:21 -05003258 struct mount *mnt = real_mount(m);
Al Viroea441d12011-11-16 21:43:59 -05003259 struct mnt_namespace *ns;
Al Virod31da0f2011-11-22 12:31:21 -05003260 struct super_block *s;
Al Viroea441d12011-11-16 21:43:59 -05003261 struct path path;
3262 int err;
3263
Al Viro74e83122019-01-30 13:30:21 -05003264 ns = alloc_mnt_ns(&init_user_ns, true);
3265 if (IS_ERR(ns)) {
3266 mntput(m);
Al Viroea441d12011-11-16 21:43:59 -05003267 return ERR_CAST(ns);
Al Viro74e83122019-01-30 13:30:21 -05003268 }
3269 mnt->mnt_ns = ns;
3270 ns->root = mnt;
3271 ns->mounts++;
3272 list_add(&mnt->mnt_list, &ns->list);
Al Viroea441d12011-11-16 21:43:59 -05003273
Al Viro74e83122019-01-30 13:30:21 -05003274 err = vfs_path_lookup(m->mnt_root, m,
Al Viroea441d12011-11-16 21:43:59 -05003275 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
3276
3277 put_mnt_ns(ns);
3278
3279 if (err)
3280 return ERR_PTR(err);
3281
3282 /* trade a vfsmount reference for active sb one */
Al Virod31da0f2011-11-22 12:31:21 -05003283 s = path.mnt->mnt_sb;
3284 atomic_inc(&s->s_active);
Al Viroea441d12011-11-16 21:43:59 -05003285 mntput(path.mnt);
3286 /* lock the sucker */
Al Virod31da0f2011-11-22 12:31:21 -05003287 down_write(&s->s_umount);
Al Viroea441d12011-11-16 21:43:59 -05003288 /* ... and return the root of (sub)tree on it */
3289 return path.dentry;
3290}
3291EXPORT_SYMBOL(mount_subtree);
3292
Dominik Brodowski312db1a2018-03-11 11:34:39 +01003293int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
3294 unsigned long flags, void __user *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Vegard Nossumeca6f532009-09-18 13:05:45 -07003296 int ret;
3297 char *kernel_type;
Vegard Nossumeca6f532009-09-18 13:05:45 -07003298 char *kernel_dev;
Al Virob40ef862015-12-14 18:44:44 -05003299 void *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Tim Gardnerb8850d12014-08-28 11:26:03 -06003301 kernel_type = copy_mount_string(type);
3302 ret = PTR_ERR(kernel_type);
3303 if (IS_ERR(kernel_type))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003304 goto out_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Tim Gardnerb8850d12014-08-28 11:26:03 -06003306 kernel_dev = copy_mount_string(dev_name);
3307 ret = PTR_ERR(kernel_dev);
3308 if (IS_ERR(kernel_dev))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003309 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Al Virob40ef862015-12-14 18:44:44 -05003311 options = copy_mount_options(data);
3312 ret = PTR_ERR(options);
3313 if (IS_ERR(options))
Vegard Nossumeca6f532009-09-18 13:05:45 -07003314 goto out_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Al Virob40ef862015-12-14 18:44:44 -05003316 ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003317
Al Virob40ef862015-12-14 18:44:44 -05003318 kfree(options);
Vegard Nossumeca6f532009-09-18 13:05:45 -07003319out_data:
3320 kfree(kernel_dev);
3321out_dev:
Vegard Nossumeca6f532009-09-18 13:05:45 -07003322 kfree(kernel_type);
3323out_type:
3324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
Dominik Brodowski312db1a2018-03-11 11:34:39 +01003327SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
3328 char __user *, type, unsigned long, flags, void __user *, data)
3329{
3330 return ksys_mount(dev_name, dir_name, type, flags, data);
3331}
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333/*
David Howells93766fb2018-11-01 23:36:14 +00003334 * Create a kernel mount representation for a new, prepared superblock
3335 * (specified by fs_fd) and attach to an open_tree-like file descriptor.
3336 */
3337SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
3338 unsigned int, attr_flags)
3339{
3340 struct mnt_namespace *ns;
3341 struct fs_context *fc;
3342 struct file *file;
3343 struct path newmount;
3344 struct mount *mnt;
3345 struct fd f;
3346 unsigned int mnt_flags = 0;
3347 long ret;
3348
3349 if (!may_mount())
3350 return -EPERM;
3351
3352 if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
3353 return -EINVAL;
3354
3355 if (attr_flags & ~(MOUNT_ATTR_RDONLY |
3356 MOUNT_ATTR_NOSUID |
3357 MOUNT_ATTR_NODEV |
3358 MOUNT_ATTR_NOEXEC |
3359 MOUNT_ATTR__ATIME |
3360 MOUNT_ATTR_NODIRATIME))
3361 return -EINVAL;
3362
3363 if (attr_flags & MOUNT_ATTR_RDONLY)
3364 mnt_flags |= MNT_READONLY;
3365 if (attr_flags & MOUNT_ATTR_NOSUID)
3366 mnt_flags |= MNT_NOSUID;
3367 if (attr_flags & MOUNT_ATTR_NODEV)
3368 mnt_flags |= MNT_NODEV;
3369 if (attr_flags & MOUNT_ATTR_NOEXEC)
3370 mnt_flags |= MNT_NOEXEC;
3371 if (attr_flags & MOUNT_ATTR_NODIRATIME)
3372 mnt_flags |= MNT_NODIRATIME;
3373
3374 switch (attr_flags & MOUNT_ATTR__ATIME) {
3375 case MOUNT_ATTR_STRICTATIME:
3376 break;
3377 case MOUNT_ATTR_NOATIME:
3378 mnt_flags |= MNT_NOATIME;
3379 break;
3380 case MOUNT_ATTR_RELATIME:
3381 mnt_flags |= MNT_RELATIME;
3382 break;
3383 default:
3384 return -EINVAL;
3385 }
3386
3387 f = fdget(fs_fd);
3388 if (!f.file)
3389 return -EBADF;
3390
3391 ret = -EINVAL;
3392 if (f.file->f_op != &fscontext_fops)
3393 goto err_fsfd;
3394
3395 fc = f.file->private_data;
3396
3397 ret = mutex_lock_interruptible(&fc->uapi_mutex);
3398 if (ret < 0)
3399 goto err_fsfd;
3400
3401 /* There must be a valid superblock or we can't mount it */
3402 ret = -EINVAL;
3403 if (!fc->root)
3404 goto err_unlock;
3405
3406 ret = -EPERM;
3407 if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
3408 pr_warn("VFS: Mount too revealing\n");
3409 goto err_unlock;
3410 }
3411
3412 ret = -EBUSY;
3413 if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
3414 goto err_unlock;
3415
3416 ret = -EPERM;
3417 if ((fc->sb_flags & SB_MANDLOCK) && !may_mandlock())
3418 goto err_unlock;
3419
3420 newmount.mnt = vfs_create_mount(fc);
3421 if (IS_ERR(newmount.mnt)) {
3422 ret = PTR_ERR(newmount.mnt);
3423 goto err_unlock;
3424 }
3425 newmount.dentry = dget(fc->root);
3426 newmount.mnt->mnt_flags = mnt_flags;
3427
3428 /* We've done the mount bit - now move the file context into more or
3429 * less the same state as if we'd done an fspick(). We don't want to
3430 * do any memory allocation or anything like that at this point as we
3431 * don't want to have to handle any errors incurred.
3432 */
3433 vfs_clean_context(fc);
3434
3435 ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
3436 if (IS_ERR(ns)) {
3437 ret = PTR_ERR(ns);
3438 goto err_path;
3439 }
3440 mnt = real_mount(newmount.mnt);
3441 mnt->mnt_ns = ns;
3442 ns->root = mnt;
3443 ns->mounts = 1;
3444 list_add(&mnt->mnt_list, &ns->list);
Eric Biggers1b0b9cc2019-06-12 11:43:13 -07003445 mntget(newmount.mnt);
David Howells93766fb2018-11-01 23:36:14 +00003446
3447 /* Attach to an apparent O_PATH fd with a note that we need to unmount
3448 * it, not just simply put it.
3449 */
3450 file = dentry_open(&newmount, O_PATH, fc->cred);
3451 if (IS_ERR(file)) {
3452 dissolve_on_fput(newmount.mnt);
3453 ret = PTR_ERR(file);
3454 goto err_path;
3455 }
3456 file->f_mode |= FMODE_NEED_UNMOUNT;
3457
3458 ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
3459 if (ret >= 0)
3460 fd_install(ret, file);
3461 else
3462 fput(file);
3463
3464err_path:
3465 path_put(&newmount);
3466err_unlock:
3467 mutex_unlock(&fc->uapi_mutex);
3468err_fsfd:
3469 fdput(f);
3470 return ret;
3471}
3472
3473/*
3474 * Move a mount from one place to another. In combination with
3475 * fsopen()/fsmount() this is used to install a new mount and in combination
3476 * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
3477 * a mount subtree.
David Howells2db154b2018-11-05 17:40:30 +00003478 *
3479 * Note the flags value is a combination of MOVE_MOUNT_* flags.
3480 */
3481SYSCALL_DEFINE5(move_mount,
3482 int, from_dfd, const char *, from_pathname,
3483 int, to_dfd, const char *, to_pathname,
3484 unsigned int, flags)
3485{
3486 struct path from_path, to_path;
3487 unsigned int lflags;
3488 int ret = 0;
3489
3490 if (!may_mount())
3491 return -EPERM;
3492
3493 if (flags & ~MOVE_MOUNT__MASK)
3494 return -EINVAL;
3495
3496 /* If someone gives a pathname, they aren't permitted to move
3497 * from an fd that requires unmount as we can't get at the flag
3498 * to clear it afterwards.
3499 */
3500 lflags = 0;
3501 if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3502 if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3503 if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3504
3505 ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
3506 if (ret < 0)
3507 return ret;
3508
3509 lflags = 0;
3510 if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
3511 if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
3512 if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
3513
3514 ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
3515 if (ret < 0)
3516 goto out_from;
3517
3518 ret = security_move_mount(&from_path, &to_path);
3519 if (ret < 0)
3520 goto out_to;
3521
3522 ret = do_move_mount(&from_path, &to_path);
3523
3524out_to:
3525 path_put(&to_path);
3526out_from:
3527 path_put(&from_path);
3528 return ret;
3529}
3530
3531/*
Al Viroafac7cb2011-11-23 19:34:49 -05003532 * Return true if path is reachable from root
3533 *
Al Viro48a066e2013-09-29 22:06:07 -04003534 * namespace_sem or mount_lock is held
Al Viroafac7cb2011-11-23 19:34:49 -05003535 */
Al Viro643822b2011-11-24 22:00:28 -05003536bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
Al Viroafac7cb2011-11-23 19:34:49 -05003537 const struct path *root)
3538{
Al Viro643822b2011-11-24 22:00:28 -05003539 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
Al Viroa73324d2011-11-24 22:25:07 -05003540 dentry = mnt->mnt_mountpoint;
Al Viro0714a532011-11-24 22:19:58 -05003541 mnt = mnt->mnt_parent;
Al Viroafac7cb2011-11-23 19:34:49 -05003542 }
Al Viro643822b2011-11-24 22:00:28 -05003543 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
Al Viroafac7cb2011-11-23 19:34:49 -05003544}
3545
Mickaël Salaün640eb7e2016-11-14 22:14:35 +01003546bool path_is_under(const struct path *path1, const struct path *path2)
Al Viroafac7cb2011-11-23 19:34:49 -05003547{
Yaowei Bai25ab4c92015-11-17 14:40:10 +08003548 bool res;
Al Viro48a066e2013-09-29 22:06:07 -04003549 read_seqlock_excl(&mount_lock);
Al Viro643822b2011-11-24 22:00:28 -05003550 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
Al Viro48a066e2013-09-29 22:06:07 -04003551 read_sequnlock_excl(&mount_lock);
Al Viroafac7cb2011-11-23 19:34:49 -05003552 return res;
3553}
3554EXPORT_SYMBOL(path_is_under);
3555
3556/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 * pivot_root Semantics:
3558 * Moves the root file system of the current process to the directory put_old,
3559 * makes new_root as the new root file system of the current process, and sets
3560 * root/cwd of all processes which had them on the current root to new_root.
3561 *
3562 * Restrictions:
3563 * The new_root and put_old must be directories, and must not be on the
3564 * same file system as the current process root. The put_old must be
3565 * underneath new_root, i.e. adding a non-zero number of /.. to the string
3566 * pointed to by put_old must yield the same directory as new_root. No other
3567 * file system may be mounted on put_old. After all, new_root is a mountpoint.
3568 *
Neil Brown4a0d11f2006-01-08 01:03:18 -08003569 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
3570 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
3571 * in this situation.
3572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 * Notes:
3574 * - we don't move root/cwd if they are not at the root (reason: if something
3575 * cared enough to change them, it's probably wrong to force them elsewhere)
3576 * - it's okay to pick a root that isn't the root of a file system, e.g.
3577 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
3578 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
3579 * first.
3580 */
Heiko Carstens3480b252009-01-14 14:14:16 +01003581SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
3582 const char __user *, put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
Al Viro2763d112019-06-30 19:18:53 -04003584 struct path new, old, root;
3585 struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
Al Viro84d17192013-03-15 10:53:28 -04003586 struct mountpoint *old_mp, *root_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 int error;
3588
Al Viro9b40bc92013-02-22 22:45:42 -05003589 if (!may_mount())
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return -EPERM;
3591
Al Viro2d8f3032008-07-22 09:59:21 -04003592 error = user_path_dir(new_root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 if (error)
3594 goto out0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Al Viro2d8f3032008-07-22 09:59:21 -04003596 error = user_path_dir(put_old, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 if (error)
3598 goto out1;
3599
Al Viro2d8f3032008-07-22 09:59:21 -04003600 error = security_sb_pivotroot(&old, &new);
Al Virob12cea92011-03-18 08:55:38 -04003601 if (error)
3602 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02003604 get_fs_root(current->fs, &root);
Al Viro84d17192013-03-15 10:53:28 -04003605 old_mp = lock_mount(&old);
3606 error = PTR_ERR(old_mp);
3607 if (IS_ERR(old_mp))
Al Virob12cea92011-03-18 08:55:38 -04003608 goto out3;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 error = -EINVAL;
Al Viro419148d2011-11-24 19:41:16 -05003611 new_mnt = real_mount(new.mnt);
3612 root_mnt = real_mount(root.mnt);
Al Viro84d17192013-03-15 10:53:28 -04003613 old_mnt = real_mount(old.mnt);
Al Viro2763d112019-06-30 19:18:53 -04003614 ex_parent = new_mnt->mnt_parent;
3615 root_parent = root_mnt->mnt_parent;
Al Viro84d17192013-03-15 10:53:28 -04003616 if (IS_MNT_SHARED(old_mnt) ||
Al Viro2763d112019-06-30 19:18:53 -04003617 IS_MNT_SHARED(ex_parent) ||
3618 IS_MNT_SHARED(root_parent))
Al Virob12cea92011-03-18 08:55:38 -04003619 goto out4;
Al Viro143c8c92011-11-25 00:46:35 -05003620 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003621 goto out4;
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003622 if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
3623 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 error = -ENOENT;
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04003625 if (d_unlinked(new.dentry))
Al Virob12cea92011-03-18 08:55:38 -04003626 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 error = -EBUSY;
Al Viro84d17192013-03-15 10:53:28 -04003628 if (new_mnt == root_mnt || old_mnt == root_mnt)
Al Virob12cea92011-03-18 08:55:38 -04003629 goto out4; /* loop, on the same file system */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 error = -EINVAL;
Al Viro8c3ee422008-03-22 18:00:39 -04003631 if (root.mnt->mnt_root != root.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003632 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003633 if (!mnt_has_parent(root_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003634 goto out4; /* not attached */
Al Viro2d8f3032008-07-22 09:59:21 -04003635 if (new.mnt->mnt_root != new.dentry)
Al Virob12cea92011-03-18 08:55:38 -04003636 goto out4; /* not a mountpoint */
Al Viro676da582011-11-24 21:47:05 -05003637 if (!mnt_has_parent(new_mnt))
Al Virob12cea92011-03-18 08:55:38 -04003638 goto out4; /* not attached */
Jan Blunck4ac91372008-02-14 19:34:32 -08003639 /* make sure we can reach put_old from new_root */
Al Viro84d17192013-03-15 10:53:28 -04003640 if (!is_path_reachable(old_mnt, old.dentry, &new))
Al Virob12cea92011-03-18 08:55:38 -04003641 goto out4;
Eric W. Biederman0d082602014-10-08 10:42:27 -07003642 /* make certain new is below the root */
3643 if (!is_path_reachable(new_mnt, new.dentry, &root))
3644 goto out4;
Al Viro719ea2f2013-09-29 11:24:49 -04003645 lock_mount_hash();
Al Viro2763d112019-06-30 19:18:53 -04003646 umount_mnt(new_mnt);
3647 root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */
Eric W. Biederman5ff9d8a2013-03-29 21:04:39 -07003648 if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
3649 new_mnt->mnt.mnt_flags |= MNT_LOCKED;
3650 root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
3651 }
Jan Blunck4ac91372008-02-14 19:34:32 -08003652 /* mount old root on put_old */
Al Viro84d17192013-03-15 10:53:28 -04003653 attach_mnt(root_mnt, old_mnt, old_mp);
Jan Blunck4ac91372008-02-14 19:34:32 -08003654 /* mount new_root on / */
Al Viro2763d112019-06-30 19:18:53 -04003655 attach_mnt(new_mnt, root_parent, root_mp);
3656 mnt_add_count(root_parent, -1);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003657 touch_mnt_namespace(current->nsproxy->mnt_ns);
Eric W. Biederman4fed6552014-10-08 10:42:57 -07003658 /* A moved mount should not expire automatically */
3659 list_del_init(&new_mnt->mnt_expire);
Eric W. Biederman3895dbf2017-01-03 14:18:43 +13003660 put_mountpoint(root_mp);
Al Viro719ea2f2013-09-29 11:24:49 -04003661 unlock_mount_hash();
Al Viro2d8f3032008-07-22 09:59:21 -04003662 chroot_fs_refs(&root, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 error = 0;
Al Virob12cea92011-03-18 08:55:38 -04003664out4:
Al Viro84d17192013-03-15 10:53:28 -04003665 unlock_mount(old_mp);
Al Viro2763d112019-06-30 19:18:53 -04003666 if (!error)
3667 mntput_no_expire(ex_parent);
Al Virob12cea92011-03-18 08:55:38 -04003668out3:
Al Viro8c3ee422008-03-22 18:00:39 -04003669 path_put(&root);
Al Virob12cea92011-03-18 08:55:38 -04003670out2:
Al Viro2d8f3032008-07-22 09:59:21 -04003671 path_put(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672out1:
Al Viro2d8f3032008-07-22 09:59:21 -04003673 path_put(&new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674out0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
3678static void __init init_mount_tree(void)
3679{
3680 struct vfsmount *mnt;
Al Viro74e83122019-01-30 13:30:21 -05003681 struct mount *m;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003682 struct mnt_namespace *ns;
Jan Blunckac748a02008-02-14 19:34:39 -08003683 struct path root;
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003684 struct file_system_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003686 type = get_fs_type("rootfs");
3687 if (!type)
3688 panic("Can't find rootfs type");
3689 mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
3690 put_filesystem(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if (IS_ERR(mnt))
3692 panic("Can't create rootfs");
Nick Pigginb3e19d92011-01-07 17:50:11 +11003693
Al Viro74e83122019-01-30 13:30:21 -05003694 ns = alloc_mnt_ns(&init_user_ns, false);
Trond Myklebust3b22edc2009-06-23 17:29:49 -04003695 if (IS_ERR(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 panic("Can't allocate initial namespace");
Al Viro74e83122019-01-30 13:30:21 -05003697 m = real_mount(mnt);
3698 m->mnt_ns = ns;
3699 ns->root = m;
3700 ns->mounts = 1;
3701 list_add(&m->mnt_list, &ns->list);
Kirill Korotaev6b3286e2006-12-08 02:37:56 -08003702 init_task.nsproxy->mnt_ns = ns;
3703 get_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Al Virobe08d6d2011-12-06 13:32:36 -05003705 root.mnt = mnt;
3706 root.dentry = mnt->mnt_root;
Eric W. Biedermanda362b02014-10-07 12:19:53 -07003707 mnt->mnt_flags |= MNT_LOCKED;
Jan Blunckac748a02008-02-14 19:34:39 -08003708
3709 set_fs_pwd(current->fs, &root);
3710 set_fs_root(current->fs, &root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
Denis Cheng74bf17c2007-10-16 23:26:30 -07003713void __init mnt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003715 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Al Viro7d6fec42011-11-23 12:14:10 -05003717 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
Paul Mundt20c2df82007-07-20 10:11:58 +09003718 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Al Viro0818bf22014-02-28 13:46:44 -05003720 mount_hashtable = alloc_large_system_hash("Mount-cache",
Al Viro38129a12014-03-20 21:10:51 -04003721 sizeof(struct hlist_head),
Al Viro0818bf22014-02-28 13:46:44 -05003722 mhash_entries, 19,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003723 HASH_ZERO,
Al Viro0818bf22014-02-28 13:46:44 -05003724 &m_hash_shift, &m_hash_mask, 0, 0);
3725 mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
3726 sizeof(struct hlist_head),
3727 mphash_entries, 19,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07003728 HASH_ZERO,
Al Viro0818bf22014-02-28 13:46:44 -05003729 &mp_hash_shift, &mp_hash_mask, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Al Viro84d17192013-03-15 10:53:28 -04003731 if (!mount_hashtable || !mountpoint_hashtable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 panic("Failed to allocate mount hash table\n");
3733
Tejun Heo4b93dc92013-11-28 14:54:43 -05003734 kernfs_init();
3735
Randy Dunlap15a67dd2006-09-29 01:58:57 -07003736 err = sysfs_init();
3737 if (err)
3738 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003739 __func__, err);
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06003740 fs_kobj = kobject_create_and_add("fs", NULL);
3741 if (!fs_kobj)
Harvey Harrison8e24eea2008-04-30 00:55:09 -07003742 printk(KERN_WARNING "%s: kobj create error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 init_rootfs();
3744 init_mount_tree();
3745}
3746
Trond Myklebust616511d2009-06-22 15:09:13 -04003747void put_mnt_ns(struct mnt_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
Al Virod498b252010-02-05 02:21:06 -05003749 if (!atomic_dec_and_test(&ns->count))
Trond Myklebust616511d2009-06-22 15:09:13 -04003750 return;
Al Viro7b00ed62013-09-16 21:19:20 -04003751 drop_collected_mounts(&ns->root->mnt);
Eric W. Biederman771b1372012-07-26 21:08:32 -07003752 free_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
Al Viro9d412a42011-03-17 22:08:28 -04003754
David Howellsd911b452018-11-01 23:07:26 +00003755struct vfsmount *kern_mount(struct file_system_type *type)
Al Viro9d412a42011-03-17 22:08:28 -04003756{
Tim Chen423e0ab2011-07-19 09:32:38 -07003757 struct vfsmount *mnt;
David Howellsd911b452018-11-01 23:07:26 +00003758 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
Tim Chen423e0ab2011-07-19 09:32:38 -07003759 if (!IS_ERR(mnt)) {
3760 /*
3761 * it is a longterm mount, don't release mnt until
3762 * we unmount before file sys is unregistered
3763 */
Al Virof7a99c52012-06-09 00:59:08 -04003764 real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
Tim Chen423e0ab2011-07-19 09:32:38 -07003765 }
3766 return mnt;
Al Viro9d412a42011-03-17 22:08:28 -04003767}
David Howellsd911b452018-11-01 23:07:26 +00003768EXPORT_SYMBOL_GPL(kern_mount);
Tim Chen423e0ab2011-07-19 09:32:38 -07003769
3770void kern_unmount(struct vfsmount *mnt)
3771{
3772 /* release long term mount so mount point can be released */
3773 if (!IS_ERR_OR_NULL(mnt)) {
Al Virof7a99c52012-06-09 00:59:08 -04003774 real_mount(mnt)->mnt_ns = NULL;
Al Viro48a066e2013-09-29 22:06:07 -04003775 synchronize_rcu(); /* yecchhh... */
Tim Chen423e0ab2011-07-19 09:32:38 -07003776 mntput(mnt);
3777 }
3778}
3779EXPORT_SYMBOL(kern_unmount);
Al Viro02125a82011-12-05 08:43:34 -05003780
3781bool our_mnt(struct vfsmount *mnt)
3782{
Al Viro143c8c92011-11-25 00:46:35 -05003783 return check_mnt(real_mount(mnt));
Al Viro02125a82011-12-05 08:43:34 -05003784}
Eric W. Biederman8823c072010-03-07 18:49:36 -08003785
Eric W. Biederman31515272013-03-15 01:45:51 -07003786bool current_chrooted(void)
3787{
3788 /* Does the current process have a non-standard root */
3789 struct path ns_root;
3790 struct path fs_root;
3791 bool chrooted;
3792
3793 /* Find the namespace root */
3794 ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
3795 ns_root.dentry = ns_root.mnt->mnt_root;
3796 path_get(&ns_root);
3797 while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
3798 ;
3799
3800 get_fs_root(current->fs, &fs_root);
3801
3802 chrooted = !path_equal(&fs_root, &ns_root);
3803
3804 path_put(&fs_root);
3805 path_put(&ns_root);
3806
3807 return chrooted;
3808}
3809
David Howells132e4602018-11-04 07:43:08 -05003810static bool mnt_already_visible(struct mnt_namespace *ns,
3811 const struct super_block *sb,
Eric W. Biederman8654df42016-06-09 16:06:06 -05003812 int *new_mnt_flags)
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003813{
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003814 int new_flags = *new_mnt_flags;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003815 struct mount *mnt;
Eric W. Biedermane51db732013-03-30 19:57:41 -07003816 bool visible = false;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003817
Al Viro44bb4382013-09-16 21:37:36 -04003818 down_read(&namespace_sem);
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003819 list_for_each_entry(mnt, &ns->list, mnt_list) {
Eric W. Biedermane51db732013-03-30 19:57:41 -07003820 struct mount *child;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003821 int mnt_flags;
3822
David Howells132e4602018-11-04 07:43:08 -05003823 if (mnt->mnt.mnt_sb->s_type != sb->s_type)
Eric W. Biedermane51db732013-03-30 19:57:41 -07003824 continue;
3825
Eric W. Biederman7e96c1b2015-05-08 16:36:50 -05003826 /* This mount is not fully visible if it's root directory
3827 * is not the root directory of the filesystem.
3828 */
3829 if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
3830 continue;
3831
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003832 /* A local view of the mount flags */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003833 mnt_flags = mnt->mnt.mnt_flags;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003834
Eric W. Biederman695e9df2016-06-10 12:21:40 -05003835 /* Don't miss readonly hidden in the superblock flags */
David Howellsbc98a422017-07-17 08:45:34 +01003836 if (sb_rdonly(mnt->mnt.mnt_sb))
Eric W. Biederman695e9df2016-06-10 12:21:40 -05003837 mnt_flags |= MNT_LOCK_READONLY;
3838
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003839 /* Verify the mount flags are equal to or more permissive
3840 * than the proposed new mount.
3841 */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003842 if ((mnt_flags & MNT_LOCK_READONLY) &&
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003843 !(new_flags & MNT_READONLY))
3844 continue;
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003845 if ((mnt_flags & MNT_LOCK_ATIME) &&
3846 ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003847 continue;
3848
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003849 /* This mount is not fully visible if there are any
3850 * locked child mounts that cover anything except for
3851 * empty directories.
Eric W. Biedermane51db732013-03-30 19:57:41 -07003852 */
3853 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
3854 struct inode *inode = child->mnt_mountpoint->d_inode;
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003855 /* Only worry about locked mounts */
Eric W. Biedermand71ed6c2016-05-27 14:50:05 -05003856 if (!(child->mnt.mnt_flags & MNT_LOCKED))
Eric W. Biedermanceeb0e52015-01-07 08:10:09 -06003857 continue;
Eric W. Biederman7236c852015-05-13 20:51:09 -05003858 /* Is the directory permanetly empty? */
3859 if (!is_empty_dir_inode(inode))
Eric W. Biedermane51db732013-03-30 19:57:41 -07003860 goto next;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003861 }
Eric W. Biederman8c6cf9c2015-05-08 23:49:47 -05003862 /* Preserve the locked attributes */
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003863 *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
Eric W. Biederman77b1a972015-06-04 09:43:11 -05003864 MNT_LOCK_ATIME);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003865 visible = true;
3866 goto found;
3867 next: ;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003868 }
Eric W. Biedermane51db732013-03-30 19:57:41 -07003869found:
Al Viro44bb4382013-09-16 21:37:36 -04003870 up_read(&namespace_sem);
Eric W. Biedermane51db732013-03-30 19:57:41 -07003871 return visible;
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -07003872}
3873
David Howells132e4602018-11-04 07:43:08 -05003874static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
Eric W. Biederman8654df42016-06-09 16:06:06 -05003875{
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003876 const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
Eric W. Biederman8654df42016-06-09 16:06:06 -05003877 struct mnt_namespace *ns = current->nsproxy->mnt_ns;
3878 unsigned long s_iflags;
3879
3880 if (ns->user_ns == &init_user_ns)
3881 return false;
3882
3883 /* Can this filesystem be too revealing? */
David Howells132e4602018-11-04 07:43:08 -05003884 s_iflags = sb->s_iflags;
Eric W. Biederman8654df42016-06-09 16:06:06 -05003885 if (!(s_iflags & SB_I_USERNS_VISIBLE))
3886 return false;
3887
Eric W. Biedermana1935c12016-06-15 06:59:49 -05003888 if ((s_iflags & required_iflags) != required_iflags) {
3889 WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
3890 required_iflags);
3891 return true;
3892 }
3893
David Howells132e4602018-11-04 07:43:08 -05003894 return !mnt_already_visible(ns, sb, new_mnt_flags);
Eric W. Biederman8654df42016-06-09 16:06:06 -05003895}
3896
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05003897bool mnt_may_suid(struct vfsmount *mnt)
3898{
3899 /*
3900 * Foreign mounts (accessed via fchdir or through /proc
3901 * symlinks) are always treated as if they are nosuid. This
3902 * prevents namespaces from trusting potentially unsafe
3903 * suid/sgid bits, file caps, or security labels that originate
3904 * in other namespaces.
3905 */
3906 return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
3907 current_in_userns(mnt->mnt_sb->s_user_ns);
3908}
3909
Al Viro64964522014-11-01 00:37:32 -04003910static struct ns_common *mntns_get(struct task_struct *task)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003911{
Al Viro58be28252014-11-01 00:00:23 -04003912 struct ns_common *ns = NULL;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003913 struct nsproxy *nsproxy;
3914
Eric W. Biederman728dba32014-02-03 19:13:49 -08003915 task_lock(task);
3916 nsproxy = task->nsproxy;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003917 if (nsproxy) {
Al Viro58be28252014-11-01 00:00:23 -04003918 ns = &nsproxy->mnt_ns->ns;
3919 get_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003920 }
Eric W. Biederman728dba32014-02-03 19:13:49 -08003921 task_unlock(task);
Eric W. Biederman8823c072010-03-07 18:49:36 -08003922
3923 return ns;
3924}
3925
Al Viro64964522014-11-01 00:37:32 -04003926static void mntns_put(struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003927{
Al Viro58be28252014-11-01 00:00:23 -04003928 put_mnt_ns(to_mnt_ns(ns));
Eric W. Biederman8823c072010-03-07 18:49:36 -08003929}
3930
Al Viro64964522014-11-01 00:37:32 -04003931static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman8823c072010-03-07 18:49:36 -08003932{
3933 struct fs_struct *fs = current->fs;
Al Viro4f757f32017-04-15 17:31:22 -04003934 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003935 struct path root;
Al Viro4f757f32017-04-15 17:31:22 -04003936 int err;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003937
Eric W. Biederman0c55cfc2012-07-26 21:42:03 -07003938 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07003939 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
3940 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Zhao Hongjiangae11e0f2012-09-13 16:38:03 +08003941 return -EPERM;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003942
Al Viro74e83122019-01-30 13:30:21 -05003943 if (is_anon_ns(mnt_ns))
3944 return -EINVAL;
3945
Eric W. Biederman8823c072010-03-07 18:49:36 -08003946 if (fs->users != 1)
3947 return -EINVAL;
3948
3949 get_mnt_ns(mnt_ns);
Al Viro4f757f32017-04-15 17:31:22 -04003950 old_mnt_ns = nsproxy->mnt_ns;
Eric W. Biederman8823c072010-03-07 18:49:36 -08003951 nsproxy->mnt_ns = mnt_ns;
3952
3953 /* Find the root */
Al Viro4f757f32017-04-15 17:31:22 -04003954 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
3955 "/", LOOKUP_DOWN, &root);
3956 if (err) {
3957 /* revert to old namespace */
3958 nsproxy->mnt_ns = old_mnt_ns;
3959 put_mnt_ns(mnt_ns);
3960 return err;
3961 }
Eric W. Biederman8823c072010-03-07 18:49:36 -08003962
Andrei Vagin40683672017-06-08 17:32:29 -07003963 put_mnt_ns(old_mnt_ns);
3964
Eric W. Biederman8823c072010-03-07 18:49:36 -08003965 /* Update the pwd and root */
3966 set_fs_pwd(fs, &root);
3967 set_fs_root(fs, &root);
3968
3969 path_put(&root);
3970 return 0;
3971}
3972
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003973static struct user_namespace *mntns_owner(struct ns_common *ns)
3974{
3975 return to_mnt_ns(ns)->user_ns;
3976}
3977
Eric W. Biederman8823c072010-03-07 18:49:36 -08003978const struct proc_ns_operations mntns_operations = {
3979 .name = "mnt",
3980 .type = CLONE_NEWNS,
3981 .get = mntns_get,
3982 .put = mntns_put,
3983 .install = mntns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07003984 .owner = mntns_owner,
Eric W. Biederman8823c072010-03-07 18:49:36 -08003985};