Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1942929rwn; Fri, 16 Sep 2022 03:20:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5fkd/Zp2UHptTSj1sSlbbflXC2gMgELF99cLnP0xR71qYREiv2UMly15IbvYxzAUFzrUu3 X-Received: by 2002:a63:de58:0:b0:439:41e9:dda2 with SMTP id y24-20020a63de58000000b0043941e9dda2mr3858017pgi.331.1663323615145; Fri, 16 Sep 2022 03:20:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663323615; cv=none; d=google.com; s=arc-20160816; b=BrBrhym0NVq4puF1OnqnCgs0kc1d9M5S2Drkcqq5b8WGAORB8eW/cXa+L4E2bXX8s8 mOqPNsQSpLeGvHYSBsEnNhrkxaUbQ9MK8es/2tm2sP6Mew3NOzjcAEp8bjMv2E21jBGb cidlor4oYLh2iHIgeEyLQEwWoGQPKS3TGRhPG9lAY3n6LsiYtp/e8TZpgF8onTPMVH2x oJ0h+K0dQL4kkw9S/A5EhlHCCGv4UV+sjD5xhMcVWXEvhPLMMMkRng2qK/K7BhGhl6JE hy2HbsisWL35PFZR/FzoNmnfEdMxqTsS7n3p0l3VEecbXl5CcxeEG0CmcNWHYdMjFkqu doRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0IfkqWpcAqo0uyKFcs4a8VpeVIImG1CcxjC81Pal+XM=; b=BjAN1SM1oQ3abuXBA9eTTnkW/1xwkRMvxZZQWkaicS/Mre0sthudq+4af+cH3mHK63 AtUlLcBvbRtHUPsgS94B+YX471sKRhx9q3EvpONm3MUFZvd1jzgAl4N3lp8y6opiGBeb 5LHVH7qhkmLBMGGVXHvIijXvsBnZOxV3j4WPCzMHpfWA+EzRja7Nc1A/fkp+rqbJPjOP VfopFtEp1pnyQgxqywX3+n9Zl0Y7UiY6MG1VPe1uvMjmp15kEm3Hrwf1smwtv4WmZIBU o6vEgAxdOqa2+5U5oQqJ96T46x8aXfReETgyjzxpr/F6RQTspqhnrRsmkqQIZkN7bbMF jDNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JZDKc2fX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q20-20020a656854000000b00428829af8e8si18454534pgt.663.2022.09.16.03.20.03; Fri, 16 Sep 2022 03:20:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JZDKc2fX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230489AbiIPKKU (ORCPT + 99 others); Fri, 16 Sep 2022 06:10:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231243AbiIPKJh (ORCPT ); Fri, 16 Sep 2022 06:09:37 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42CDEABF34; Fri, 16 Sep 2022 03:08:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id EFFDFB82503; Fri, 16 Sep 2022 10:08:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 529ABC433D6; Fri, 16 Sep 2022 10:08:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663322908; bh=LzV4F/MDDvs2PsNWQ4DkbtFiMj0IuYqEthpNVbJpyNo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JZDKc2fX8IZt5PxxPzceLzBfDVamiTYTPoK9pVo/WSmRiV8bGOAgXEr21WNiCa4WS NcJS6mIBfF6HnfjQSGshpxtP2GUrLs9NgdBS+zvw4Lb7BYyaBTGghOhT/A8bOQvKKt vjiaaWXWSuih9owibWqqOZYTAUsTrYCtrqzv1WNY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Brian Norris , "Steven Rostedt (Google)" Subject: [PATCH 4.19 11/11] tracefs: Only clobber mode/uid/gid on remount if asked Date: Fri, 16 Sep 2022 12:08:07 +0200 Message-Id: <20220916100443.217874115@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220916100442.662955946@linuxfoundation.org> References: <20220916100442.662955946@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Brian Norris commit 47311db8e8f33011d90dee76b39c8886120cdda4 upstream. Users may have explicitly configured their tracefs permissions; we shouldn't overwrite those just because a second mount appeared. Only clobber if the options were provided at mount time. Note: the previous behavior was especially surprising in the presence of automounted /sys/kernel/debug/tracing/. Existing behavior: ## Pre-existing status: tracefs is 0755. # stat -c '%A' /sys/kernel/tracing/ drwxr-xr-x ## (Re)trigger the automount. # umount /sys/kernel/debug/tracing # stat -c '%A' /sys/kernel/debug/tracing/. drwx------ ## Unexpected: the automount changed mode for other mount instances. # stat -c '%A' /sys/kernel/tracing/ drwx------ New behavior (after this change): ## Pre-existing status: tracefs is 0755. # stat -c '%A' /sys/kernel/tracing/ drwxr-xr-x ## (Re)trigger the automount. # umount /sys/kernel/debug/tracing # stat -c '%A' /sys/kernel/debug/tracing/. drwxr-xr-x ## Expected: the automount does not change other mount instances. # stat -c '%A' /sys/kernel/tracing/ drwxr-xr-x Link: https://lkml.kernel.org/r/20220826174353.2.Iab6e5ea57963d6deca5311b27fb7226790d44406@changeid Cc: stable@vger.kernel.org Fixes: 4282d60689d4f ("tracefs: Add new tracefs file system") Signed-off-by: Brian Norris Signed-off-by: Steven Rostedt (Google) Signed-off-by: Greg Kroah-Hartman --- fs/tracefs/inode.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -142,6 +142,8 @@ struct tracefs_mount_opts { kuid_t uid; kgid_t gid; umode_t mode; + /* Opt_* bitfield. */ + unsigned int opts; }; enum { @@ -242,6 +244,7 @@ static int tracefs_parse_options(char *d kgid_t gid; char *p; + opts->opts = 0; opts->mode = TRACEFS_DEFAULT_MODE; while ((p = strsep(&data, ",")) != NULL) { @@ -276,24 +279,36 @@ static int tracefs_parse_options(char *d * but traditionally tracefs has ignored all mount options */ } + + opts->opts |= BIT(token); } return 0; } -static int tracefs_apply_options(struct super_block *sb) +static int tracefs_apply_options(struct super_block *sb, bool remount) { struct tracefs_fs_info *fsi = sb->s_fs_info; struct inode *inode = sb->s_root->d_inode; struct tracefs_mount_opts *opts = &fsi->mount_opts; - inode->i_mode &= ~S_IALLUGO; - inode->i_mode |= opts->mode; + /* + * On remount, only reset mode/uid/gid if they were provided as mount + * options. + */ + + if (!remount || opts->opts & BIT(Opt_mode)) { + inode->i_mode &= ~S_IALLUGO; + inode->i_mode |= opts->mode; + } - inode->i_uid = opts->uid; + if (!remount || opts->opts & BIT(Opt_uid)) + inode->i_uid = opts->uid; - /* Set all the group ids to the mount option */ - set_gid(sb->s_root, opts->gid); + if (!remount || opts->opts & BIT(Opt_gid)) { + /* Set all the group ids to the mount option */ + set_gid(sb->s_root, opts->gid); + } return 0; } @@ -308,7 +323,7 @@ static int tracefs_remount(struct super_ if (err) goto fail; - tracefs_apply_options(sb); + tracefs_apply_options(sb, true); fail: return err; @@ -360,7 +375,7 @@ static int trace_fill_super(struct super sb->s_op = &tracefs_super_operations; - tracefs_apply_options(sb); + tracefs_apply_options(sb, false); return 0;