bcachefs: install fd later to avoid race with close
PATCH 1/1 bcachefs: install fd later to avoid race with close
Calling fd_install() makes a file reachable for userland, including the possibility to close the file descriptor, which leads to calling its 'release' hook. If that happens before the code had a chance to bump the reference of the newly created task struct, the release callback will call put_task_struct() too early, leading to the premature destruction of the kernel thread.
Avoid that race by calling fd_install() later, after all the setup is done.
--- fs/bcachefs/thread_with_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/thread_with_file.c +1 -1
@@ -53,9 +53,9 @@ int bch2_run_thread_with_file(struct thread_with_file *thr,if (ret) goto err; -fd_install (fd, file);get_task_struct (thr->task);wake_up_process (thr->task); +fd_install (fd, file);return fd; err:if (fd >=0 )
Signature · 1 line
2.39.2
Quoted 7 lines · Mathias Krause
Calling
fd_install()makes a file reachable for userland, including the possibility to close the file descriptor, which leads to calling its 'release' hook. If that happens before the code had a chance to bump the reference of the newly created task struct, the release callback will callput_task_struct()too early, leading to the premature destruction of the kernel thread.
Quoted 5 lines
Avoid that race by calling
fd_install()later, after all the setup is done.Fixes 1c6fdbd8f246 ("bcachefs: Initial commit")Signed-off-by Mathias Krause <[email protected]>
The fixes line is wrong, but I'll take care of that - thanks!
Quoted 5 lines · Kent Overstreet
Quoted 2 lines · Mathias Krause
]...]
Fixes 1c6fdbd8f246 ("bcachefs: Initial commit")The fixes line is wrong, but I'll take care of that - thanks!
No, it's not wrong. It's broken in the initial commit where this code was still living in fs/bcachefs/chardev.c:bch2_ioctl_data(). Commit 96f37eabe7a5 ("bcachefs: factor out thread_with_file, thread_with_stdio") only moved that code to where it is now, but it's still wrong before that. So I used the earlier one.
Thanks,
Mathias
Quoted 5 lines · Mathias Krause
Quoted 4 lines · Kent Overstreet
Quoted 2 lines · Mathias Krause
]...]
Fixes 1c6fdbd8f246 ("bcachefs: Initial commit")
Quoted 7 lines
Quoted 1 line
The fixes line is wrong, but I'll take care of that - thanks!
No, it's not wrong. It's broken in the initial commit where this code was still living in fs/bcachefs/chardev.c:bch2_ioctl_data(). Commit
96f37eabe7a5("bcachefs: factor outthread_with_file,thread_with_stdio") only moved that code to where it is now, but it's still wrong before that. So I used the earlier one.
Oh hey, you did some code archeology - nice :)
I'll queue this up for stable then.