[PATCH] coda: fix possible data race around sb->s_fs_info access

From: Tuo Li
Date: Mon Jun 12 2023 - 03:35:05 EST


The struct field s_fs_info is often protected by the lock vc_mutex when is
accessed. Here is an example in coda_put_super():

mutex_lock(&vcp->vc_mutex);
vcp->vc_sb = NULL;
sb->s_fs_info = NULL;
mutex_unlock(&vcp->vc_mutex);

However, sb->s_fs_info is accessed after the lock vc->vc_mutex is unlocked
in coda_fill_super()

vc->vc_sb = sb;
mutex_unlock(&vc->vc_mutex);

sb->s_fs_info = vc;

And thus can cause a data race when another thread access sb->s_fs_info
concurrently.

To fix this possible data race, the instruction to unlock vc->vc_mutex is
moved in front of the access to sb->s_fs_info.

Reported-by: BassCheck <bass@xxxxxxxxxxx>
Signed-off-by: Tuo Li <islituo@xxxxxxxxx>
---
fs/coda/inode.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/coda/inode.c b/fs/coda/inode.c
index d661e6cf17ac..743030ec5ef6 100644
--- a/fs/coda/inode.c
+++ b/fs/coda/inode.c
@@ -179,9 +179,9 @@ static int coda_fill_super(struct super_block *sb, void *data, int silent)
}

vc->vc_sb = sb;
+ sb->s_fs_info = vc;
mutex_unlock(&vc->vc_mutex);

- sb->s_fs_info = vc;
sb->s_flags |= SB_NOATIME;
sb->s_blocksize = 4096; /* XXXXX what do we put here?? */
sb->s_blocksize_bits = 12;
--
2.34.1