Compare
c01b091b06412d3b1b53d8c2925f6de586c8a2fc..36ebddca56b9d8993d6dc984fdecc748ee1f5e06
fsnotify: prevent mark additions during connector teardown
fsnotify_destroy_marks() keeps a reference to its current mark while it
drops conn->lock to destroy the mark. This stabilizes the forward-walk
cursor, but does not prevent a new mark from being inserted before it.
For example, an inotify watch re-added after IN_IGNORED sorts before the
old, detached mark for the same group. The walk can miss the replacement
and then detach the entire connector from the object.
The replacement is still on its group's marks_list and, for inotify, in
the watch IDR. The group mutex no longer protects the connector's object
from disappearing. An unprivileged fdinfo read racing the final detach
can consequently call igrab(NULL):
BUG: KASAN: null-ptr-deref in igrab+0x24/0x100
Write of size 4 at addr 0000000000000148
Call Trace:
igrab+0x24/0x100
inotify_show_fdinfo+0x1dc/0x5e0
seq_show+0x397/0x690
Set a DESTROYING flag under conn->lock before starting the walk, and
reject new marks on that connector with -ENOENT. The existing add-mark
error paths undo the temporary group-list entry and the backend's watch
allocation. Do not wait for teardown while holding the group mutex,
which the destroyer needs. Once the old connector is detached, an add
can install a fresh connector as before.
This closes the insertion race for all object types using the common
connector teardown, without changing the mark reference or SRCU lifetime
rules.
Fixes: 6b3f05d24d35 ("fsnotify: Detach mark from object list when last reference is dropped")
Cc: stable@vger.kernel.org
Signed-off-by: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Kyle Zeng <kylebot@openai.com> · 2026-10-02 05:59 UTC · committed by Christian Brauner
9 files changed,
+67
−19
- fs/autofs/waitq.c
- fs/namespace.c
- fs/notify/mark.c
- fs/ntfs3/dir.c
- fs/ntfs3/ntfs_fs.h
- fs/ntfs3/super.c
- include/linux/fsnotify_backend.h
- kernel/exit.c
- kernel/pid_namespace.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/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)) { | |
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; | |
modified fs/ntfs3/super.c
| @@ -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 | } | |
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 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/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(); | |