[PATCH] gfs2: Fix possible data races in gfs2_show_options()

Tuo Li posted 1 patch 2 years, 8 months ago
fs/gfs2/super.c | 2 ++
1 file changed, 2 insertions(+)
[PATCH] gfs2: Fix possible data races in gfs2_show_options()
Posted by Tuo Li 2 years, 8 months ago
Some fields such as gt_logd_secs of the struct gfs2_tune are accessed 
without holding the lock gt_spin in gfs2_show_options():

  val = sdp->sd_tune.gt_logd_secs;
  if (val != 30)
    seq_printf(s, ",commit=%d", val);

And thus can cause data races when gfs2_show_options() and other functions
such as gfs2_reconfigure() are concurrently executed:

  spin_lock(&gt->gt_spin);
  gt->gt_logd_secs = newargs->ar_commit;

To fix these possible data races, the lock sdp->sd_tune.gt_spin is 
acquired before accessing the fields of gfs2_tune and released after these
accesses.

Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
 fs/gfs2/super.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index a84bf6444bba..671adf38fe03 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -1059,6 +1059,7 @@ static int gfs2_show_options(struct seq_file *s, struct dentry *root)
 	}
 	if (args->ar_discard)
 		seq_puts(s, ",discard");
+	spin_lock(&sdp->sd_tune.gt_spin);
 	val = sdp->sd_tune.gt_logd_secs;
 	if (val != 30)
 		seq_printf(s, ",commit=%d", val);
@@ -1070,6 +1071,7 @@ static int gfs2_show_options(struct seq_file *s, struct dentry *root)
 	val = sdp->sd_tune.gt_quota_quantum;
 	if (val != 60)
 		seq_printf(s, ",quota_quantum=%d", val);
+	spin_unlock(&sdp->sd_tune.gt_spin);
 	if (args->ar_statfs_percent)
 		seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
 	if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
-- 
2.34.1
Re: [PATCH] gfs2: Fix possible data races in gfs2_show_options()
Posted by Andreas Gruenbacher 2 years, 7 months ago
On Tue, Jun 13, 2023 at 5:07 AM Tuo Li <islituo@gmail.com> wrote:
> Some fields such as gt_logd_secs of the struct gfs2_tune are accessed
> without holding the lock gt_spin in gfs2_show_options():
>
>   val = sdp->sd_tune.gt_logd_secs;
>   if (val != 30)
>     seq_printf(s, ",commit=%d", val);
>
> And thus can cause data races when gfs2_show_options() and other functions
> such as gfs2_reconfigure() are concurrently executed:
>
>   spin_lock(&gt->gt_spin);
>   gt->gt_logd_secs = newargs->ar_commit;
>
> To fix these possible data races, the lock sdp->sd_tune.gt_spin is
> acquired before accessing the fields of gfs2_tune and released after these
> accesses.
>
> Reported-by: BassCheck <bass@buaa.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>
> ---
>  fs/gfs2/super.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
> index a84bf6444bba..671adf38fe03 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1059,6 +1059,7 @@ static int gfs2_show_options(struct seq_file *s, struct dentry *root)
>         }
>         if (args->ar_discard)
>                 seq_puts(s, ",discard");
> +       spin_lock(&sdp->sd_tune.gt_spin);
>         val = sdp->sd_tune.gt_logd_secs;
>         if (val != 30)
>                 seq_printf(s, ",commit=%d", val);
> @@ -1070,6 +1071,7 @@ static int gfs2_show_options(struct seq_file *s, struct dentry *root)
>         val = sdp->sd_tune.gt_quota_quantum;
>         if (val != 60)
>                 seq_printf(s, ",quota_quantum=%d", val);
> +       spin_unlock(&sdp->sd_tune.gt_spin);
>         if (args->ar_statfs_percent)
>                 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
>         if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
> --
> 2.34.1
>

Added to for-next (with minor adjustments).

Thanks,
Andreas