Compare
f0e48172a59e517e613b73f73cc35f24fd123aeb..906f04fb1ebfed7b00ef33faa98499fc4f0e609d
Merge patch series "namespace: a mount that copy_tree() leaves out can't take over a lock"
Christian Brauner <brauner@kernel.org> says:
Since commit c62a4766937e ("move_mount: transfer MNT_LOCKED") a mount
can be moved beneath a locked mount. The lock moves from the old cover
to the new mount which takes over the job of hiding what's under it and
the old cover can be unmounted. That works as long as the new mount
stays. But the caller picks the new mount and copy_tree() leaves a
mount of a mount namespace file out of every copy made without
CL_COPY_MNT_NS_FILE, together with everything mounted on it, and never
looks at MNT_LOCKED. unshare(CLONE_NEWNS) and mount propagation make
such copies.
So a task that is root in its own user namespace gets rid of a locked
cover on a file without a race and without touching the filesystem
below it. It makes a child with a mount namespace of its own, binds the
child's /proc/<pid>/ns/mnt beneath the cover with MOVE_MOUNT_BENEATH,
which hands the lock to that bind, and calls unshare(CLONE_NEWNS). The
new mount namespace has nothing on the file. That's the usual way to
mask a single file, a bind of /dev/null or of an empty file. With a
bind of an ordinary file the file stays covered.
copy_tree() already refuses to leave out a locked unbindable mount with
-EPERM since commit df7342b24018 ("mount: Don't allow copying
MNT_UNBINDABLE|MNT_LOCKED mounts"). Do the same for a locked mount of a
mount namespace file. That also covers a tree copied with
CL_COPY_MNT_NS_FILE and attached into a mount namespace of another user
namespace where lock_mnt_tree() locks it. Note that unshare(CLONE_NEWNS)
and clone(CLONE_NEWNS) fail with EPERM from such a mount namespace from
then on. The mount can't be left out and it can't be unmounted.
And refuse to move a mount of a mount namespace file beneath a locked
mount in the first place. Otherwise the task ends up with a mount it
can't unmount and an unshare() that fails for good.
* patches from https://patch.msgid.link/20261014-work-namei-locked-mnt_ns_file-v1-0-24035bd5d8e9@kernel.org:
selftests/filesystems: test that a mntns file can't take over a lock
namespace: a mount that copy_tree() leaves out can't take over a lock
Link: https://patch.msgid.link/20261014-work-namei-locked-mnt_ns_file-v1-0-24035bd5d8e9@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Christian Brauner <brauner@kernel.org> · 2026-10-14 19:46 UTC
15 files changed,
+265
−44
- fs/autofs/waitq.c
- fs/namei.c
- fs/namespace.c
- fs/notify/mark.c
- fs/ntfs3/dir.c
- fs/ntfs3/ntfs_fs.h
- fs/ntfs3/super.c
- fs/overlayfs/params.c
- fs/unicode/utf8-core.c
- include/linux/fsnotify_backend.h
- include/linux/unicode.h
- kernel/exit.c
- kernel/fork.c
- kernel/pid_namespace.c
- tools/testing/selftests/filesystems/umount_propagation/locked_mount_test.c
modified fs/autofs/waitq.c
| @@ -12,15 +12,13 @@ | |||
| 12 | 12 | */ | |
| 13 | 13 | static autofs_wqt_t autofs_next_wait_queue = 1; | |
| 14 | 14 | ||
| 15 | - | void autofs_catatonic_mode(struct autofs_sb_info *sbi) | |
| 15 | + | static void autofs_catatonic_mode_locked(struct autofs_sb_info *sbi) | |
| 16 | 16 | { | |
| 17 | 17 | struct autofs_wait_queue *wq, *nwq; | |
| 18 | 18 | ||
| 19 | - | mutex_lock(&sbi->wq_mutex); | |
| 20 | - | if (sbi->flags & AUTOFS_SBI_CATATONIC) { | |
| 21 | - | mutex_unlock(&sbi->wq_mutex); | |
| 19 | + | lockdep_assert_held(&sbi->wq_mutex); | |
| 20 | + | if (sbi->flags & AUTOFS_SBI_CATATONIC) | |
| 22 | 21 | return; | |
| 23 | - | } | |
| 24 | 22 | ||
| 25 | 23 | pr_debug("entering catatonic mode\n"); | |
| 26 | 24 | ||
| @@ -40,6 +38,21 @@ void autofs_catatonic_mode(struct autofs_sb_info *sbi) | |||
| 40 | 38 | fput(sbi->pipe); /* Close the pipe */ | |
| 41 | 39 | sbi->pipe = NULL; | |
| 42 | 40 | sbi->pipefd = -1; | |
| 41 | + | } | |
| 42 | + | ||
| 43 | + | void autofs_catatonic_mode(struct autofs_sb_info *sbi) | |
| 44 | + | { | |
| 45 | + | mutex_lock(&sbi->wq_mutex); | |
| 46 | + | autofs_catatonic_mode_locked(sbi); | |
| 47 | + | mutex_unlock(&sbi->wq_mutex); | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | static void autofs_catatonic_mode_for_pipe(struct autofs_sb_info *sbi, | |
| 51 | + | struct file *pipe) | |
| 52 | + | { | |
| 53 | + | mutex_lock(&sbi->wq_mutex); | |
| 54 | + | if (sbi->pipe == pipe) | |
| 55 | + | autofs_catatonic_mode_locked(sbi); | |
| 43 | 56 | mutex_unlock(&sbi->wq_mutex); | |
| 44 | 57 | } | |
| 45 | 58 | ||
| @@ -170,7 +183,7 @@ static void autofs_notify_daemon(struct autofs_sb_info *sbi, | |||
| 170 | 183 | autofs_wait_release(sbi, wq->wait_queue_token, ret); | |
| 171 | 184 | break; | |
| 172 | 185 | default: | |
| 173 | - | autofs_catatonic_mode(sbi); | |
| 186 | + | autofs_catatonic_mode_for_pipe(sbi, pipe); | |
| 174 | 187 | break; | |
| 175 | 188 | } | |
| 176 | 189 | fput(pipe); | |
modified fs/namei.c
| @@ -4750,8 +4750,7 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag) | |||
| 4750 | 4750 | return dentry; | |
| 4751 | 4751 | ||
| 4752 | 4752 | if (open_flag & O_CREAT) { | |
| 4753 | - | /* Discard negative dentries. Need inode_lock to do the create */ | |
| 4754 | - | if (!dentry->d_inode) { | |
| 4753 | + | if (d_is_negative(dentry)) { | |
| 4755 | 4754 | if (!(nd->flags & LOOKUP_RCU)) | |
| 4756 | 4755 | dput(dentry); | |
| 4757 | 4756 | dentry = NULL; | |
modified fs/namespace.c
| @@ -297,7 +297,11 @@ int mnt_get_count(struct mount *mnt) | |||
| 297 | 297 | /* puts first, so a put counted here has its get counted below */ | |
| 298 | 298 | for_each_possible_cpu(cpu) | |
| 299 | 299 | puts += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_puts; | |
| 300 | - | smp_mb(); /* pairs with the smp_wmb() in mntput_no_expire() */ | |
| 300 | + | /* | |
| 301 | + | * Pairs with the smp_wmb() in mntput_no_expire() and with the | |
| 302 | + | * smp_mb() in __legitimize_mnt(). | |
| 303 | + | */ | |
| 304 | + | smp_mb(); | |
| 301 | 305 | for_each_possible_cpu(cpu) | |
| 302 | 306 | gets += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_gets; | |
| 303 | 307 | ||
| @@ -782,7 +786,7 @@ int __legitimize_mnt(struct vfsmount *bastard, unsigned seq) | |||
| 782 | 786 | return 0; | |
| 783 | 787 | mnt = real_mount(bastard); | |
| 784 | 788 | mnt_inc_count(mnt); | |
| 785 | - | smp_mb(); /* see mntput_no_expire_slowpath() and do_umount() */ | |
| 789 | + | smp_mb(); /* pairs with the smp_mb() in mnt_get_count() */ | |
| 786 | 790 | if (likely(!read_seqretry(&mount_lock, seq))) | |
| 787 | 791 | return 0; | |
| 788 | 792 | lock_mount_hash(); | |
| @@ -1419,11 +1423,6 @@ static void noinline mntput_no_expire_slowpath(struct mount *mnt) | |||
| 1419 | 1423 | ||
| 1420 | 1424 | VFS_BUG_ON(mnt->mnt_ns); | |
| 1421 | 1425 | lock_mount_hash(); | |
| 1422 | - | /* | |
| 1423 | - | * make sure that if __legitimize_mnt() has not seen us grab | |
| 1424 | - | * mount_lock, we'll see their refcount increment here. | |
| 1425 | - | */ | |
| 1426 | - | smp_mb(); | |
| 1427 | 1426 | mnt_dec_count(mnt); | |
| 1428 | 1427 | count = mnt_get_count(mnt); | |
| 1429 | 1428 | if (count != 0) { | |
| @@ -2006,7 +2005,6 @@ static int do_umount(struct mount *mnt, int flags) | |||
| 2006 | 2005 | umount_tree(mnt, UMOUNT_PROPAGATE); | |
| 2007 | 2006 | retval = 0; | |
| 2008 | 2007 | } else { | |
| 2009 | - | smp_mb(); // paired with __legitimize_mnt() | |
| 2010 | 2008 | shrink_submounts(mnt); | |
| 2011 | 2009 | retval = -EBUSY; | |
| 2012 | 2010 | if (!propagate_mount_busy(mnt, 2)) { | |
| @@ -2254,6 +2252,11 @@ struct mount *copy_tree(struct mount *src_root, struct dentry *dentry, | |||
| 2254 | 2252 | } | |
| 2255 | 2253 | if (!(flag & CL_COPY_MNT_NS_FILE) && | |
| 2256 | 2254 | is_mnt_ns_file(src_mnt->mnt.mnt_root)) { | |
| 2255 | + | if (src_mnt->mnt.mnt_flags & MNT_LOCKED) { | |
| 2256 | + | /* Both a mount namespace file and locked. */ | |
| 2257 | + | dst_mnt = ERR_PTR(-EPERM); | |
| 2258 | + | goto out; | |
| 2259 | + | } | |
| 2257 | 2260 | src_mnt = skip_mnt_tree(src_mnt); | |
| 2258 | 2261 | continue; | |
| 2259 | 2262 | } | |
| @@ -3649,8 +3652,9 @@ static bool mount_is_ancestor(const struct mount *p1, const struct mount *p2) | |||
| 3649 | 3652 | * @mnt_to: mount under which to mount | |
| 3650 | 3653 | * @mp: mountpoint of @mnt_to | |
| 3651 | 3654 | * | |
| 3652 | - | * - Make sure that the caller can unmount the topmost mount ensuring | |
| 3653 | - | * that the caller could reveal the underlying mountpoint. | |
| 3655 | + | * - Make sure that @mnt_from can take over the lock of @mnt_to. A | |
| 3656 | + | * mount of a mount namespace file is left out of a copy of the tree | |
| 3657 | + | * and can't. | |
| 3654 | 3658 | * - Ensure that nothing has been mounted on top of @mnt_from before we | |
| 3655 | 3659 | * grabbed @namespace_sem to avoid creating pointless shadow mounts. | |
| 3656 | 3660 | * - Prevent mounting beneath a mount if the propagation relationship | |
| @@ -3666,6 +3670,15 @@ static int can_move_mount_beneath(const struct mount *mnt_from, | |||
| 3666 | 3670 | { | |
| 3667 | 3671 | struct mount *parent_mnt_to = mnt_to->mnt_parent; | |
| 3668 | 3672 | ||
| 3673 | + | /* | |
| 3674 | + | * @mnt_from takes over the lock of @mnt_to. copy_tree() leaves a | |
| 3675 | + | * mount of a mount namespace file out of every copy made without | |
| 3676 | + | * CL_COPY_MNT_NS_FILE, together with what is mounted on it, so it | |
| 3677 | + | * can't keep anything hidden. | |
| 3678 | + | */ | |
| 3679 | + | if (IS_MNT_LOCKED(mnt_to) && is_mnt_ns_file(mnt_from->mnt.mnt_root)) | |
| 3680 | + | return -EINVAL; | |
| 3681 | + | ||
| 3669 | 3682 | /* Avoid creating shadow mounts during mount propagation. */ | |
| 3670 | 3683 | if (mnt_from->overmount) | |
| 3671 | 3684 | return -EINVAL; | |
modified fs/notify/mark.c
| @@ -929,6 +929,11 @@ restart: | |||
| 929 | 929 | goto restart; | |
| 930 | 930 | } | |
| 931 | 931 | ||
| 932 | + | if (conn->flags & FSNOTIFY_CONN_FLAG_DESTROYING) { | |
| 933 | + | err = -ENOENT; | |
| 934 | + | goto out_err; | |
| 935 | + | } | |
| 936 | + | ||
| 932 | 937 | /* is mark the first mark? */ | |
| 933 | 938 | if (hlist_empty(&conn->list)) { | |
| 934 | 939 | hlist_add_head_rcu(&mark->obj_list, &conn->list); | |
| @@ -1116,6 +1121,12 @@ void fsnotify_destroy_marks(fsnotify_connp_t *connp) | |||
| 1116 | 1121 | conn = fsnotify_grab_connector(connp); | |
| 1117 | 1122 | if (!conn) | |
| 1118 | 1123 | return; | |
| 1124 | + | /* | |
| 1125 | + | * New marks could be inserted before our cursor and survive the final | |
| 1126 | + | * detachment of the connector from the object. Prevent additions to | |
| 1127 | + | * this connector before dropping its lock. | |
| 1128 | + | */ | |
| 1129 | + | conn->flags |= FSNOTIFY_CONN_FLAG_DESTROYING; | |
| 1119 | 1130 | /* | |
| 1120 | 1131 | * We have to be careful since we can race with e.g. | |
| 1121 | 1132 | * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the | |
modified fs/ntfs3/dir.c
| @@ -186,11 +186,20 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len, | |||
| 186 | 186 | { | |
| 187 | 187 | int ret, slen, i; | |
| 188 | 188 | const u8 *end; | |
| 189 | - | struct nls_table *nls = sbi->options->nls; | |
| 189 | + | struct ntfs_mount_options *opts; | |
| 190 | + | struct nls_table *nls; | |
| 191 | + | bool ads; | |
| 190 | 192 | u16 *uname = uni->name; | |
| 191 | 193 | ||
| 192 | 194 | static_assert(sizeof(wchar_t) == sizeof(u16)); | |
| 193 | 195 | ||
| 196 | + | /* ntfs_fs_reconfigure() may replace the options, but not ->nls */ | |
| 197 | + | rcu_read_lock(); | |
| 198 | + | opts = READ_ONCE(sbi->options); | |
| 199 | + | nls = opts->nls; | |
| 200 | + | ads = opts->ads; | |
| 201 | + | rcu_read_unlock(); | |
| 202 | + | ||
| 194 | 203 | if (!nls) { | |
| 195 | 204 | /* utf8 -> utf16 */ | |
| 196 | 205 | ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen); | |
| @@ -230,7 +239,7 @@ int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len, | |||
| 230 | 239 | ||
| 231 | 240 | uni->len = ret; | |
| 232 | 241 | uni->ads_len = 0; | |
| 233 | - | if (ret > 0 && sbi->options->ads) { | |
| 242 | + | if (ret > 0 && ads) { | |
| 234 | 243 | uname = uni->name; | |
| 235 | 244 | /* Find delimiter in range [1 : ret-2). */ | |
| 236 | 245 | for (i = 1; i + 1 < ret; i++) { | |
modified fs/ntfs3/ntfs_fs.h
| @@ -91,6 +91,8 @@ enum utf16_endian; | |||
| 91 | 91 | struct ntfs_mount_options { | |
| 92 | 92 | char *nls_name; | |
| 93 | 93 | struct nls_table *nls; | |
| 94 | + | /* ->d_hash() and ->d_compare() read the options in rcu pathwalk */ | |
| 95 | + | struct rcu_head rcu; | |
| 94 | 96 | ||
| 95 | 97 | kuid_t fs_uid; | |
| 96 | 98 | kgid_t fs_gid; | |
| @@ -211,6 +213,7 @@ struct ntfs_index { | |||
| 211 | 213 | /* Ntfs file system in-core superblock data. */ | |
| 212 | 214 | struct ntfs_sb_info { | |
| 213 | 215 | struct super_block *sb; | |
| 216 | + | struct rcu_head rcu; | |
| 214 | 217 | ||
| 215 | 218 | u32 discard_granularity; | |
| 216 | 219 | u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1) | |
modified fs/ntfs3/super.c
| @@ -196,7 +196,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes) | |||
| 196 | 196 | void *ret = NULL; | |
| 197 | 197 | int i, j = -1; | |
| 198 | 198 | ||
| 199 | - | spin_lock(&s_shared_lock); | |
| 199 | + | spin_lock_bh(&s_shared_lock); | |
| 200 | 200 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { | |
| 201 | 201 | if (!s_shared[i].cnt) { | |
| 202 | 202 | j = i; | |
| @@ -214,7 +214,7 @@ void *ntfs_set_shared(void *ptr, u32 bytes) | |||
| 214 | 214 | s_shared[j].cnt = 1; | |
| 215 | 215 | ret = ptr; | |
| 216 | 216 | } | |
| 217 | - | spin_unlock(&s_shared_lock); | |
| 217 | + | spin_unlock_bh(&s_shared_lock); | |
| 218 | 218 | ||
| 219 | 219 | return ret; | |
| 220 | 220 | } | |
| @@ -231,7 +231,7 @@ void *ntfs_put_shared(void *ptr) | |||
| 231 | 231 | void *ret = ptr; | |
| 232 | 232 | int i; | |
| 233 | 233 | ||
| 234 | - | spin_lock(&s_shared_lock); | |
| 234 | + | spin_lock_bh(&s_shared_lock); | |
| 235 | 235 | for (i = 0; i < ARRAY_SIZE(s_shared); i++) { | |
| 236 | 236 | if (s_shared[i].cnt && s_shared[i].ptr == ptr) { | |
| 237 | 237 | if (--s_shared[i].cnt) | |
| @@ -239,7 +239,7 @@ void *ntfs_put_shared(void *ptr) | |||
| 239 | 239 | break; | |
| 240 | 240 | } | |
| 241 | 241 | } | |
| 242 | - | spin_unlock(&s_shared_lock); | |
| 242 | + | spin_unlock_bh(&s_shared_lock); | |
| 243 | 243 | ||
| 244 | 244 | return ret; | |
| 245 | 245 | } | |
| @@ -251,6 +251,11 @@ static inline void put_mount_options(struct ntfs_mount_options *options) | |||
| 251 | 251 | kfree(options); | |
| 252 | 252 | } | |
| 253 | 253 | ||
| 254 | + | static void put_mount_options_rcu(struct rcu_head *head) | |
| 255 | + | { | |
| 256 | + | put_mount_options(container_of(head, struct ntfs_mount_options, rcu)); | |
| 257 | + | } | |
| 258 | + | ||
| 254 | 259 | enum Opt { | |
| 255 | 260 | Opt_uid, | |
| 256 | 261 | Opt_gid, | |
| @@ -444,6 +449,7 @@ static int ntfs_fs_reconfigure(struct fs_context *fc) | |||
| 444 | 449 | struct super_block *sb = fc->root->d_sb; | |
| 445 | 450 | struct ntfs_sb_info *sbi = sb->s_fs_info; | |
| 446 | 451 | struct ntfs_mount_options *new_opts = fc->fs_private; | |
| 452 | + | struct ntfs_mount_options *old_opts; | |
| 447 | 453 | int ro_rw; | |
| 448 | 454 | ||
| 449 | 455 | ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY); | |
| @@ -473,7 +479,15 @@ static int ntfs_fs_reconfigure(struct fs_context *fc) | |||
| 473 | 479 | } | |
| 474 | 480 | ||
| 475 | 481 | sync_filesystem(sb); | |
| 476 | - | swap(sbi->options, fc->fs_private); | |
| 482 | + | old_opts = sbi->options; | |
| 483 | + | /* pairs with the READ_ONCE() in ntfs_nls_to_utf16() */ | |
| 484 | + | smp_store_release(&sbi->options, new_opts); | |
| 485 | + | fc->fs_private = NULL; | |
| 486 | + | /* | |
| 487 | + | * ntfs_d_hash() and ntfs_d_compare() of a pathwalk in rcu mode may | |
| 488 | + | * still read the old options through the sbi. | |
| 489 | + | */ | |
| 490 | + | call_rcu(&old_opts->rcu, put_mount_options_rcu); | |
| 477 | 491 | ||
| 478 | 492 | return 0; | |
| 479 | 493 | } | |
| @@ -708,6 +722,8 @@ static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi) | |||
| 708 | 722 | ||
| 709 | 723 | static void ntfs3_free_sbi(struct ntfs_sb_info *sbi) | |
| 710 | 724 | { | |
| 725 | + | if (sbi->options) | |
| 726 | + | put_mount_options(sbi->options); | |
| 711 | 727 | kfree(sbi->new_rec); | |
| 712 | 728 | kvfree(ntfs_put_shared(sbi->upcase)); | |
| 713 | 729 | kvfree(sbi->def_table); | |
| @@ -719,6 +735,11 @@ static void ntfs3_free_sbi(struct ntfs_sb_info *sbi) | |||
| 719 | 735 | kfree(sbi); | |
| 720 | 736 | } | |
| 721 | 737 | ||
| 738 | + | static void ntfs3_free_sbi_rcu(struct rcu_head *head) | |
| 739 | + | { | |
| 740 | + | ntfs3_free_sbi(container_of(head, struct ntfs_sb_info, rcu)); | |
| 741 | + | } | |
| 742 | + | ||
| 722 | 743 | static void ntfs_put_super(struct super_block *sb) | |
| 723 | 744 | { | |
| 724 | 745 | struct ntfs_sb_info *sbi = sb->s_fs_info; | |
| @@ -728,11 +749,11 @@ static void ntfs_put_super(struct super_block *sb) | |||
| 728 | 749 | /* Mark rw ntfs as clear, if possible. */ | |
| 729 | 750 | ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); | |
| 730 | 751 | ||
| 731 | - | if (sbi->options) { | |
| 732 | - | put_mount_options(sbi->options); | |
| 733 | - | sbi->options = NULL; | |
| 734 | - | } | |
| 735 | - | ||
| 752 | + | /* | |
| 753 | + | * The mount options stay until the sbi is freed: ->d_hash() and | |
| 754 | + | * ->d_compare() of a pathwalk in rcu mode read the nls table through | |
| 755 | + | * them. | |
| 756 | + | */ | |
| 736 | 757 | ntfs3_put_sbi(sbi); | |
| 737 | 758 | } | |
| 738 | 759 | ||
| @@ -1939,9 +1960,11 @@ static void ntfs3_kill_sb(struct super_block *sb) | |||
| 1939 | 1960 | ||
| 1940 | 1961 | kill_block_super(sb); | |
| 1941 | 1962 | ||
| 1942 | - | if (sbi->options) | |
| 1943 | - | put_mount_options(sbi->options); | |
| 1944 | - | ntfs3_free_sbi(sbi); | |
| 1963 | + | /* | |
| 1964 | + | * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() | |
| 1965 | + | * and read the upcase table and the nls table through the sbi. | |
| 1966 | + | */ | |
| 1967 | + | call_rcu(&sbi->rcu, ntfs3_free_sbi_rcu); | |
| 1945 | 1968 | } | |
| 1946 | 1969 | ||
| 1947 | 1970 | // clang-format off | |
modified fs/overlayfs/params.c
| @@ -216,6 +216,9 @@ static ssize_t ovl_parse_param_split_lowerdirs(char *str) | |||
| 216 | 216 | if (*s == '\\') { | |
| 217 | 217 | /* keep esc chars in split lowerdir */ | |
| 218 | 218 | *d++ = *s++; | |
| 219 | + | /* A trailing backslash escapes nothing. */ | |
| 220 | + | if (!*s) | |
| 221 | + | return -EINVAL; | |
| 219 | 222 | } else if (*s == ':') { | |
| 220 | 223 | bool next_colon = (*(s + 1) == ':'); | |
| 221 | 224 | ||
modified fs/unicode/utf8-core.c
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | #include <linux/kernel.h> | |
| 4 | 4 | #include <linux/string.h> | |
| 5 | 5 | #include <linux/slab.h> | |
| 6 | + | #include <linux/rcupdate.h> | |
| 6 | 7 | #include <linux/parser.h> | |
| 7 | 8 | #include <linux/errno.h> | |
| 8 | 9 | #include <linux/stringhash.h> | |
| @@ -183,12 +184,24 @@ out_free_um: | |||
| 183 | 184 | } | |
| 184 | 185 | EXPORT_SYMBOL(utf8_load); | |
| 185 | 186 | ||
| 187 | + | static void utf8_unload_rcu(struct rcu_head *head) | |
| 188 | + | { | |
| 189 | + | struct unicode_map *um = container_of(head, struct unicode_map, rcu); | |
| 190 | + | ||
| 191 | + | symbol_put(utf8_data_table); | |
| 192 | + | kfree(um); | |
| 193 | + | } | |
| 194 | + | ||
| 195 | + | /* | |
| 196 | + | * A pathwalk in rcu mode may still be in ->d_hash() or ->d_compare() of a | |
| 197 | + | * filesystem that is being torn down and read the map through its | |
| 198 | + | * superblock, so the map and the tables it points to have to outlive the | |
| 199 | + | * grace period. | |
| 200 | + | */ | |
| 186 | 201 | void utf8_unload(struct unicode_map *um) | |
| 187 | 202 | { | |
| 188 | - | if (um) { | |
| 189 | - | symbol_put(utf8_data_table); | |
| 190 | - | kfree(um); | |
| 191 | - | } | |
| 203 | + | if (um) | |
| 204 | + | call_rcu(&um->rcu, utf8_unload_rcu); | |
| 192 | 205 | } | |
| 193 | 206 | EXPORT_SYMBOL(utf8_unload); | |
| 194 | 207 | ||
modified include/linux/fsnotify_backend.h
| @@ -566,6 +566,7 @@ struct fsnotify_mark_connector { | |||
| 566 | 566 | unsigned char prio; /* Highest priority group */ | |
| 567 | 567 | #define FSNOTIFY_CONN_FLAG_IS_WATCHED 0x01 | |
| 568 | 568 | #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02 | |
| 569 | + | #define FSNOTIFY_CONN_FLAG_DESTROYING 0x04 | |
| 569 | 570 | unsigned short flags; /* flags [lock] */ | |
| 570 | 571 | union { | |
| 571 | 572 | /* Object pointer [lock] */ | |
modified include/linux/unicode.h
| @@ -53,6 +53,7 @@ struct unicode_map { | |||
| 53 | 53 | unsigned int version; | |
| 54 | 54 | const struct utf8data *ntab[UTF8_NMAX]; | |
| 55 | 55 | const struct utf8data_table *tables; | |
| 56 | + | struct rcu_head rcu; | |
| 56 | 57 | }; | |
| 57 | 58 | ||
| 58 | 59 | int utf8_validate(const struct unicode_map *um, const struct qstr *str); | |
modified kernel/exit.c
| @@ -1729,7 +1729,7 @@ static long do_wait(struct wait_opts *wo) | |||
| 1729 | 1729 | add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); | |
| 1730 | 1730 | ||
| 1731 | 1731 | do { | |
| 1732 | - | set_current_state(TASK_INTERRUPTIBLE); | |
| 1732 | + | set_current_state(TASK_INTERRUPTIBLE | TASK_FREEZABLE); | |
| 1733 | 1733 | retval = __do_wait(wo); | |
| 1734 | 1734 | if (retval != -ERESTARTSYS) | |
| 1735 | 1735 | break; | |
modified kernel/fork.c
| @@ -1619,7 +1619,13 @@ static int copy_exec_state(u64 clone_flags, struct task_struct *tsk) | |||
| 1619 | 1619 | return task_exec_state_copy(tsk); | |
| 1620 | 1620 | } | |
| 1621 | 1621 | ||
| 1622 | - | static int copy_fs(u64 clone_flags, struct task_struct *tsk, bool umh) | |
| 1622 | + | enum copy_fs_flags_t { | |
| 1623 | + | COPY_FS_UMH = (1 << 0), | |
| 1624 | + | COPY_FS_IDLE = (1 << 1), | |
| 1625 | + | }; | |
| 1626 | + | ||
| 1627 | + | static int copy_fs(u64 clone_flags, struct task_struct *tsk, | |
| 1628 | + | enum copy_fs_flags_t flags) | |
| 1623 | 1629 | { | |
| 1624 | 1630 | struct fs_struct *fs; | |
| 1625 | 1631 | ||
| @@ -1629,19 +1635,20 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk, bool umh) | |||
| 1629 | 1635 | * filesystem state, or be started from a non-initial mount | |
| 1630 | 1636 | * namespace. | |
| 1631 | 1637 | */ | |
| 1632 | - | if (umh) { | |
| 1638 | + | if (flags & COPY_FS_UMH) { | |
| 1633 | 1639 | if (clone_flags & (CLONE_NEWNS | CLONE_FS)) | |
| 1634 | 1640 | return -EINVAL; | |
| 1635 | 1641 | if (current->nsproxy->mnt_ns != &init_mnt_ns) | |
| 1636 | 1642 | return -EINVAL; | |
| 1637 | 1643 | fs = userspace_init_fs; | |
| 1644 | + | } else if (flags & COPY_FS_IDLE) { | |
| 1645 | + | fs = &init_fs; | |
| 1638 | 1646 | } else { | |
| 1639 | 1647 | fs = current->fs; | |
| 1640 | 1648 | VFS_WARN_ON_ONCE(current->fs != current->real_fs); | |
| 1641 | 1649 | } | |
| 1642 | 1650 | ||
| 1643 | 1651 | if (clone_flags & CLONE_FS) { | |
| 1644 | - | /* tsk->fs is already what we want */ | |
| 1645 | 1652 | read_seqlock_excl(&fs->seq); | |
| 1646 | 1653 | /* "users" and "in_exec" locked for check_unsafe_exec() */ | |
| 1647 | 1654 | if (fs->in_exec) { | |
| @@ -1650,6 +1657,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk, bool umh) | |||
| 1650 | 1657 | } | |
| 1651 | 1658 | fs->users++; | |
| 1652 | 1659 | read_sequnlock_excl(&fs->seq); | |
| 1660 | + | tsk->real_fs = tsk->fs = fs; | |
| 1653 | 1661 | return 0; | |
| 1654 | 1662 | } | |
| 1655 | 1663 | tsk->real_fs = tsk->fs = copy_fs_struct(fs); | |
| @@ -2299,7 +2307,9 @@ __latent_entropy struct task_struct *copy_process( | |||
| 2299 | 2307 | retval = copy_files(clone_flags, p, args->no_files); | |
| 2300 | 2308 | if (retval) | |
| 2301 | 2309 | goto bad_fork_cleanup_semundo; | |
| 2302 | - | retval = copy_fs(clone_flags, p, args->umh); | |
| 2310 | + | retval = copy_fs(clone_flags, p, | |
| 2311 | + | (args->umh ? COPY_FS_UMH : 0) | | |
| 2312 | + | (args->idle ? COPY_FS_IDLE : 0)); | |
| 2303 | 2313 | if (retval) | |
| 2304 | 2314 | goto bad_fork_cleanup_files; | |
| 2305 | 2315 | retval = copy_sighand(clone_flags, p); | |
| @@ -2671,7 +2681,7 @@ struct task_struct * __init fork_idle(int cpu) | |||
| 2671 | 2681 | { | |
| 2672 | 2682 | struct task_struct *task; | |
| 2673 | 2683 | struct kernel_clone_args args = { | |
| 2674 | - | .flags = CLONE_VM, | |
| 2684 | + | .flags = CLONE_VM | CLONE_FS, | |
| 2675 | 2685 | .fn = &idle_dummy, | |
| 2676 | 2686 | .fn_arg = NULL, | |
| 2677 | 2687 | .kthread = 1, | |
modified kernel/pid_namespace.c
| @@ -268,7 +268,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns) | |||
| 268 | 268 | * free_pid() will awaken this task. | |
| 269 | 269 | */ | |
| 270 | 270 | for (;;) { | |
| 271 | - | set_current_state(TASK_INTERRUPTIBLE); | |
| 271 | + | set_current_state(TASK_IDLE | TASK_FREEZABLE); | |
| 272 | 272 | if (pid_ns->pid_allocated == init_pids) | |
| 273 | 273 | break; | |
| 274 | 274 | schedule(); | |
modified tools/testing/selftests/filesystems/umount_propagation/locked_mount_test.c
| @@ -9,6 +9,8 @@ | |||
| 9 | 9 | #include <errno.h> | |
| 10 | 10 | #include <fcntl.h> | |
| 11 | 11 | #include <sched.h> | |
| 12 | + | #include <stdbool.h> | |
| 13 | + | #include <signal.h> | |
| 12 | 14 | #include <stdio.h> | |
| 13 | 15 | #include <stdlib.h> | |
| 14 | 16 | #include <string.h> | |
| @@ -27,6 +29,12 @@ | |||
| 27 | 29 | #ifndef MOVE_MOUNT_BENEATH | |
| 28 | 30 | #define MOVE_MOUNT_BENEATH 0x00000200 | |
| 29 | 31 | #endif | |
| 32 | + | #ifndef OPEN_TREE_CLONE | |
| 33 | + | #define OPEN_TREE_CLONE 1 | |
| 34 | + | #endif | |
| 35 | + | #ifndef OPEN_TREE_CLOEXEC | |
| 36 | + | #define OPEN_TREE_CLOEXEC O_CLOEXEC | |
| 37 | + | #endif | |
| 30 | 38 | ||
| 31 | 39 | #define DIR_LEN 64 | |
| 32 | 40 | #define PATH_LEN 192 | |
| @@ -120,6 +128,35 @@ static bool covered(const char *path) | |||
| 120 | 128 | return fd < 0; | |
| 121 | 129 | } | |
| 122 | 130 | ||
| 131 | + | /* What @path reads, "" if it can't be read. */ | |
| 132 | + | static int read_word(const char *path, char *buf, size_t len) | |
| 133 | + | { | |
| 134 | + | ssize_t n = 0; | |
| 135 | + | int fd; | |
| 136 | + | ||
| 137 | + | fd = open(path, O_RDONLY | O_CLOEXEC); | |
| 138 | + | if (fd >= 0) { | |
| 139 | + | n = read(fd, buf, len - 1); | |
| 140 | + | close(fd); | |
| 141 | + | } | |
| 142 | + | buf[n > 0 ? n : 0] = 0; | |
| 143 | + | return fd < 0 ? -1 : 0; | |
| 144 | + | } | |
| 145 | + | ||
| 146 | + | /* Put a clone of the mount at @src beneath the top mount at @where. */ | |
| 147 | + | static int move_beneath(const char *src, const char *where) | |
| 148 | + | { | |
| 149 | + | int fd, ret; | |
| 150 | + | ||
| 151 | + | fd = syscall(__NR_open_tree, AT_FDCWD, src, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC); | |
| 152 | + | if (fd < 0) | |
| 153 | + | return -1; | |
| 154 | + | ret = syscall(__NR_move_mount, fd, "", AT_FDCWD, where, | |
| 155 | + | MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH); | |
| 156 | + | close(fd); | |
| 157 | + | return ret; | |
| 158 | + | } | |
| 159 | + | ||
| 123 | 160 | FIXTURE(locked_mount) { | |
| 124 | 161 | char base[DIR_LEN]; | |
| 125 | 162 | bool tracing; /* debugfs with the tracefs automount is there */ | |
| @@ -429,4 +466,89 @@ TEST_F(locked_mount, propagated_copy_keeps_the_cover_locked) | |||
| 429 | 466 | EXPECT_EQ(ret, CHILD_OK); | |
| 430 | 467 | } | |
| 431 | 468 | ||
| 469 | + | /* | |
| 470 | + | * A mount of a mount namespace file is left out of the copy that | |
| 471 | + | * unshare(CLONE_NEWNS) makes, together with everything mounted on it, so | |
| 472 | + | * it can't take over the lock of a cover it is moved beneath. The child | |
| 473 | + | * tries just that with the namespace file of a grandchild and gets EINVAL. | |
| 474 | + | * A bind of an ordinary file takes the lock over, and the file is still | |
| 475 | + | * covered in the next copy of the mount namespace. | |
| 476 | + | */ | |
| 477 | + | static int ns_file_child(const char *base) | |
| 478 | + | { | |
| 479 | + | char secret[PATH_LEN], mine[PATH_LEN], nsfile[PATH_LEN], buf[32]; | |
| 480 | + | int ret = CHILD_OK, pfd[2]; | |
| 481 | + | pid_t pid; | |
| 482 | + | ||
| 483 | + | snprintf(secret, sizeof(secret), "%s/P/secret", base); | |
| 484 | + | snprintf(mine, sizeof(mine), "%s/mine", base); | |
| 485 | + | ||
| 486 | + | if (enter_userns() || pipe(pfd)) | |
| 487 | + | return CHILD_SETUP; | |
| 488 | + | /* the cover is locked */ | |
| 489 | + | if (!umount2(secret, 0) || errno != EINVAL) | |
| 490 | + | return CHILD_STEP1; | |
| 491 | + | ||
| 492 | + | /* a mount namespace newer than our own, so that a bind of it is allowed */ | |
| 493 | + | pid = fork(); | |
| 494 | + | if (pid < 0) | |
| 495 | + | return CHILD_SETUP; | |
| 496 | + | if (pid == 0) { | |
| 497 | + | if (unshare(CLONE_NEWNS) || send_byte(pfd[1])) | |
| 498 | + | _exit(1); | |
| 499 | + | pause(); | |
| 500 | + | _exit(0); | |
| 501 | + | } | |
| 502 | + | if (wait_byte(pfd[0])) | |
| 503 | + | return CHILD_SETUP; | |
| 504 | + | snprintf(nsfile, sizeof(nsfile), "/proc/%d/ns/mnt", pid); | |
| 505 | + | if (!move_beneath(nsfile, secret) || errno != EINVAL) | |
| 506 | + | ret = CHILD_STEP2; | |
| 507 | + | kill(pid, SIGKILL); | |
| 508 | + | waitpid(pid, NULL, 0); | |
| 509 | + | if (ret) | |
| 510 | + | return ret; | |
| 511 | + | ||
| 512 | + | /* an ordinary file beneath the cover takes the lock over */ | |
| 513 | + | if (move_beneath(mine, secret)) | |
| 514 | + | return CHILD_STEP3; | |
| 515 | + | if (umount2(secret, 0)) | |
| 516 | + | return CHILD_STEP3; | |
| 517 | + | if (!umount2(secret, 0) || errno != EINVAL) | |
| 518 | + | return CHILD_STEP3; | |
| 519 | + | /* and it is in the next copy of the namespace */ | |
| 520 | + | if (unshare(CLONE_NEWNS)) | |
| 521 | + | return CHILD_SETUP; | |
| 522 | + | if (read_word(secret, buf, sizeof(buf)) || strcmp(buf, "mine")) | |
| 523 | + | return CHILD_STEP4; | |
| 524 | + | return CHILD_OK; | |
| 525 | + | } | |
| 526 | + | ||
| 527 | + | TEST_F(locked_mount, mount_namespace_file_cant_take_the_lock) | |
| 528 | + | { | |
| 529 | + | char p[PATH_LEN], q[PATH_LEN]; | |
| 530 | + | pid_t pid; | |
| 531 | + | int ret; | |
| 532 | + | ||
| 533 | + | snprintf(p, sizeof(p), "%s/P", self->base); | |
| 534 | + | ASSERT_EQ(mkdir(p, 0755), 0); | |
| 535 | + | ASSERT_EQ(mount("tmpfs", p, "tmpfs", 0, "mode=0755"), 0); | |
| 536 | + | snprintf(p, sizeof(p), "%s/P/secret", self->base); | |
| 537 | + | ASSERT_EQ(create_file(p, "covered-by-root\n"), 0); | |
| 538 | + | snprintf(q, sizeof(q), "%s/P/empty", self->base); | |
| 539 | + | ASSERT_EQ(create_file(q, ""), 0); | |
| 540 | + | /* the cover: an empty file over the secret */ | |
| 541 | + | ASSERT_EQ(mount(q, p, NULL, MS_BIND, NULL), 0); | |
| 542 | + | snprintf(q, sizeof(q), "%s/mine", self->base); | |
| 543 | + | ASSERT_EQ(create_file(q, "mine"), 0); | |
| 544 | + | ||
| 545 | + | pid = fork(); | |
| 546 | + | ASSERT_GE(pid, 0); | |
| 547 | + | if (pid == 0) | |
| 548 | + | _exit(ns_file_child(self->base)); | |
| 549 | + | ret = wait_child(pid); | |
| 550 | + | TH_LOG("child exit code %d", ret); | |
| 551 | + | EXPECT_EQ(ret, CHILD_OK); | |
| 552 | + | } | |
| 553 | + | ||
| 432 | 554 | TEST_HARNESS_MAIN | |