From nobody Wed Dec 17 03:00:15 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F4E6C3DA66 for ; Thu, 24 Aug 2023 02:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239408AbjHXCTV (ORCPT ); Wed, 23 Aug 2023 22:19:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239335AbjHXCSg (ORCPT ); Wed, 23 Aug 2023 22:18:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3893CE6D for ; Wed, 23 Aug 2023 19:18:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C590B633C6 for ; Thu, 24 Aug 2023 02:18:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 462E0C433C8; Thu, 24 Aug 2023 02:18:34 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1qYzvx-001hWq-0g; Wed, 23 Aug 2023 22:18:53 -0400 Message-ID: <20230824021853.022987766@goodmis.org> User-Agent: quilt/0.66 Date: Wed, 23 Aug 2023 22:18:25 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Sishuai Gong Subject: [for-next][PATCH 13/14] tracefs: Avoid changing i_mode to a temp value References: <20230824021812.938245293@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sishuai Gong 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 &=3D ~S_IALLUGO; unsigned int mode =3D inode->i_mode; inode->i_mode |=3D 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/AB5B0A1C-75D9-4E82-A7F0-CF= 7D0715587B@gmail.com Signed-off-by: Sishuai Gong Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/inode.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index bb6de89eb446..c7a10f965602 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -310,6 +310,7 @@ static int tracefs_apply_options(struct super_block *sb= , bool remount) struct tracefs_fs_info *fsi =3D sb->s_fs_info; struct inode *inode =3D d_inode(sb->s_root); struct tracefs_mount_opts *opts =3D &fsi->mount_opts; + umode_t tmp_mode; =20 /* * On remount, only reset mode/uid/gid if they were provided as mount @@ -317,8 +318,9 @@ static int tracefs_apply_options(struct super_block *sb= , bool remount) */ =20 if (!remount || opts->opts & BIT(Opt_mode)) { - inode->i_mode &=3D ~S_IALLUGO; - inode->i_mode |=3D opts->mode; + tmp_mode =3D READ_ONCE(inode->i_mode) & ~S_IALLUGO; + tmp_mode |=3D opts->mode; + WRITE_ONCE(inode->i_mode, tmp_mode); } =20 if (!remount || opts->opts & BIT(Opt_uid)) --=20 2.40.1