Skip to content

Commit 5253568

Browse files
mjguzikgregkh
authored andcommitted
fs: consistently deref the files table with rcu_dereference_raw()
[ Upstream commit f381640 ] ... except when the table is known to be only used by one thread. A file pointer can get installed at any moment despite the ->file_lock being held since the following: 8a81252 ("fs/file.c: don't acquire files->file_lock in fd_install()") Accesses subject to such a race can in principle suffer load tearing. While here redo the comment in dup_fd -- it only covered a race against files showing up, still assuming fd_install() takes the lock. Signed-off-by: Mateusz Guzik <mjguzik@gmail.com> Link: https://lore.kernel.org/r/20250313135725.1320914-1-mjguzik@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent fa1827f commit 5253568

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

fs/file.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,25 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho
367367
old_fds = old_fdt->fd;
368368
new_fds = new_fdt->fd;
369369

370+
/*
371+
* We may be racing against fd allocation from other threads using this
372+
* files_struct, despite holding ->file_lock.
373+
*
374+
* alloc_fd() might have already claimed a slot, while fd_install()
375+
* did not populate it yet. Note the latter operates locklessly, so
376+
* the file can show up as we are walking the array below.
377+
*
378+
* At the same time we know no files will disappear as all other
379+
* operations take the lock.
380+
*
381+
* Instead of trying to placate userspace racing with itself, we
382+
* ref the file if we see it and mark the fd slot as unused otherwise.
383+
*/
370384
for (i = open_files; i != 0; i--) {
371-
struct file *f = *old_fds++;
385+
struct file *f = rcu_dereference_raw(*old_fds++);
372386
if (f) {
373387
get_file(f);
374388
} else {
375-
/*
376-
* The fd may be claimed in the fd bitmap but not yet
377-
* instantiated in the files array if a sibling thread
378-
* is partway through open(). So make sure that this
379-
* fd is available to the new process.
380-
*/
381389
__clear_open_fd(open_files - i, new_fdt);
382390
}
383391
rcu_assign_pointer(*new_fds++, f);
@@ -637,7 +645,7 @@ struct file *file_close_fd_locked(struct files_struct *files, unsigned fd)
637645
return NULL;
638646

639647
fd = array_index_nospec(fd, fdt->max_fds);
640-
file = fdt->fd[fd];
648+
file = rcu_dereference_raw(fdt->fd[fd]);
641649
if (file) {
642650
rcu_assign_pointer(fdt->fd[fd], NULL);
643651
__put_unused_fd(files, fd);
@@ -1219,7 +1227,7 @@ __releases(&files->file_lock)
12191227
*/
12201228
fdt = files_fdtable(files);
12211229
fd = array_index_nospec(fd, fdt->max_fds);
1222-
tofree = fdt->fd[fd];
1230+
tofree = rcu_dereference_raw(fdt->fd[fd]);
12231231
if (!tofree && fd_is_open(fd, fdt))
12241232
goto Ebusy;
12251233
get_file(file);

0 commit comments

Comments
 (0)