Skip to content

Commit 0866297

Browse files
Sishuai Gongrostedt
authored andcommitted
tracefs: Avoid changing i_mode to a temp value
Right now inode->i_mode is updated twice to reach the desired value in tracefs_apply_options(). Because there is no lock protecting the two writes, other threads might read the intermediate value of inode->i_mode. Thread-1 Thread-2 // tracefs_apply_options() //e.g., acl_permission_check inode->i_mode &= ~S_IALLUGO; unsigned int mode = inode->i_mode; inode->i_mode |= opts->mode; I think there is no need to introduce a lock but it is better to only update inode->i_mode ONCE, so the readers will either see the old or latest value, rather than an intermediate/temporary value. Note, the race is not a security concern as the intermediate value is more locked down than either the start or end version. This is more just to do the conversion cleanly. Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Signed-off-by: Sishuai Gong <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent a943188 commit 0866297

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

fs/tracefs/inode.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,17 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
310310
struct tracefs_fs_info *fsi = sb->s_fs_info;
311311
struct inode *inode = d_inode(sb->s_root);
312312
struct tracefs_mount_opts *opts = &fsi->mount_opts;
313+
umode_t tmp_mode;
313314

314315
/*
315316
* On remount, only reset mode/uid/gid if they were provided as mount
316317
* options.
317318
*/
318319

319320
if (!remount || opts->opts & BIT(Opt_mode)) {
320-
inode->i_mode &= ~S_IALLUGO;
321-
inode->i_mode |= opts->mode;
321+
tmp_mode = READ_ONCE(inode->i_mode) & ~S_IALLUGO;
322+
tmp_mode |= opts->mode;
323+
WRITE_ONCE(inode->i_mode, tmp_mode);
322324
}
323325

324326
if (!remount || opts->opts & BIT(Opt_uid))

0 commit comments

Comments
 (0)