From nobody Sun Nov 24 20:57:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BA001CDFCB; Fri, 1 Nov 2024 18:32:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; cv=none; b=L+0bSm9D76NdREx61U1AX4TWU5kVTzmBD2xQRRKie52pC9fuS1RrjwFTGnudmV+Yr8+7/Y9YG0oX0v/2sbgYSFdT1NXR6r8cfvWakOUSsj0Pwoi9/f5FhaWj9Rr8gpyHud4IrDRgTZL7aD1zSctJKOqbc8LQ0yqQ0musybdwOcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; c=relaxed/simple; bh=NG34A9Sh7NNpVGAXKxos3KmZWrxcYzJCmMqeulvMRx8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Ict2JkrznKAF0mtFd1K6MfKBmIlCqLjjKVXRsFWYaIo/p8GoUOYPIC5irtC1F/0BnDs1Pa5Ntk5cDjjw2AkeW9BlMunI+vqvqvptTRWGDywtrAZVmfqMHdmo9dhCEQId7/1navwlDjAvJ8z0CenUDSgDFFE5ag2rpf1L1cHX37M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0178C4CECD; Fri, 1 Nov 2024 18:32:56 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t6wT5-00000005X40-2Wim; Fri, 01 Nov 2024 14:33:55 -0400 Message-ID: <20241101183355.468402456@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 01 Nov 2024 14:33:28 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Eric Sandeen , Shuah Khan , Ali Zahraee , Christian Brauner , David Howells , stable@vger.kernel.org, Kalesh Singh Subject: [for-linus][PATCH 1/3] tracing: Fix tracefs mount options References: <20241101183327.693623203@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kalesh Singh Commit 78ff64081949 ("vfs: Convert tracefs to use the new mount API") converted tracefs to use the new mount APIs caused mount options (e.g. gid=3D) to not take effect. The tracefs superblock can be updated from multiple paths: - on fs_initcall() to init_trace_printk_function_export() - from a work queue to initialize eventfs tracer_init_tracefs_work_func() - fsconfig() syscall to mount or remount of tracefs The tracefs superblock root inode gets created early on in init_trace_printk_function_export(). With the new mount API, tracefs effectively uses get_tree_single() instead of the old API mount_single(). Previously, mount_single() ensured that the options are always applied to the superblock root inode: (1) If the root inode didn't exist, call fill_super() to create it and apply the options. (2) If the root inode exists, call reconfigure_single() which effectively calls tracefs_apply_options() to parse and apply options to the subperblock's fs_info and inode and remount eventfs (if necessary) On the other hand, get_tree_single() effectively calls vfs_get_super() which: (3) If the root inode doesn't exists, calls fill_super() to create it and apply the options. (4) If the root inode already exists, updates the fs_context root with the superblock's root inode. (4) above is always the case for tracefs mounts, since the super block's root inode will already be created by init_trace_printk_function_export(). This means that the mount options get ignored: - Since it isn't applied to the superblock's root inode, it doesn't get inherited by the children. - Since eventfs is initialized from a separate work queue and before call to mount with the options, and it doesn't get remounted for mount. Ensure that the mount options are applied to the super block and eventfs is remounted to respect the mount options. To understand this better, if fstab has the following: tracefs /sys/kernel/tracing tracefs nosuid,nodev,noexec,gid=3Dtracing = 0 0 On boot up, permissions look like: # ls -l /sys/kernel/tracing/trace -rw-r----- 1 root root 0 Nov 1 08:37 /sys/kernel/tracing/trace When it should look like: # ls -l /sys/kernel/tracing/trace -rw-r----- 1 root tracing 0 Nov 1 08:37 /sys/kernel/tracing/trace Link: https://lore.kernel.org/r/536e99d3-345c-448b-adee-a21389d7ab4b@redhat= .com/ Cc: Eric Sandeen Cc: Mathieu Desnoyers Cc: Shuah Khan Cc: Ali Zahraee Cc: Christian Brauner Cc: David Howells Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: stable@vger.kernel.org Fixes: 78ff64081949 ("vfs: Convert tracefs to use the new mount API") Link: https://lore.kernel.org/20241030171928.4168869-2-kaleshsingh@google.c= om Signed-off-by: Kalesh Singh Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/inode.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 1748dff58c3b..cfc614c638da 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -392,6 +392,9 @@ static int tracefs_reconfigure(struct fs_context *fc) struct tracefs_fs_info *sb_opts =3D sb->s_fs_info; struct tracefs_fs_info *new_opts =3D fc->s_fs_info; =20 + if (!new_opts) + return 0; + sync_filesystem(sb); /* structure copy of new mount options to sb */ *sb_opts =3D *new_opts; @@ -478,14 +481,17 @@ static int tracefs_fill_super(struct super_block *sb,= struct fs_context *fc) sb->s_op =3D &tracefs_super_operations; sb->s_d_op =3D &tracefs_dentry_operations; =20 - tracefs_apply_options(sb, false); - return 0; } =20 static int tracefs_get_tree(struct fs_context *fc) { - return get_tree_single(fc, tracefs_fill_super); + int err =3D get_tree_single(fc, tracefs_fill_super); + + if (err) + return err; + + return tracefs_reconfigure(fc); } =20 static void tracefs_free_fc(struct fs_context *fc) --=20 2.45.2 From nobody Sun Nov 24 20:57:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 303B2146592 for ; Fri, 1 Nov 2024 18:32:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; cv=none; b=YKtF3/2eofzLFtSPyElcM+k7zqgIdBe3p42MNy/Ni9XagngH0auWK+2qs/JZZwOezn4XZbJimzE1I/kvkhYUAlfy1aIXe0mx/VKIPc8sKmuOVSLHwvbh9Pzjn5s1AWxErSq2A630gyaiyjzQsMb0MAZlIXtSiel/vZXbZMoWbFM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; c=relaxed/simple; bh=JBoTxHTmIi/CjVr6fQEh4WJHfT5qMw0f1r9bmXMLxHA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=JBekGbCyU3eY/JZWOsFUqPZrvyNSoayWBz79rEiL9CrwIzAj5dMA8/4012Px//uR2KPQfAT4EoH6nDHfTzo1Vk2xxfLeYyYtFF9v2QSaelJuFTg9Vr4b3/ZuO0YSRSDWduzoyOTNC98UdJtRFvzK95pvsb5+tu5YLtgfsM5bKoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBE15C4CED3; Fri, 1 Nov 2024 18:32:56 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t6wT5-00000005X4V-3BJF; Fri, 01 Nov 2024 14:33:55 -0400 Message-ID: <20241101183355.620763892@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 01 Nov 2024 14:33:29 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Eric Sandeen , Shuah Khan , Ali Zahraee , Christian Brauner , David Howells , Kalesh Singh Subject: [for-linus][PATCH 2/3] tracing: Document tracefs gid mount option References: <20241101183327.693623203@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kalesh Singh Commit ee7f3666995d ("tracefs: Have new files inherit the ownership of their parent") and commit 48b27b6b5191 ("tracefs: Set all files to the same group ownership as the mount option") introduced a new gid mount option that allows specifying a group to apply to all entries in tracefs. Document this in the tracing readme. Cc: Eric Sandeen Cc: Mathieu Desnoyers Cc: Shuah Khan Cc: Ali Zahraee Cc: Christian Brauner Cc: David Howells Cc: Masami Hiramatsu Link: https://lore.kernel.org/20241030171928.4168869-3-kaleshsingh@google.c= om Signed-off-by: Kalesh Singh Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index a8f52b6527ca..2b64b3ec67d9 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -5501,6 +5501,10 @@ static const struct file_operations tracing_iter_fop= s =3D { =20 static const char readme_msg[] =3D "tracing mini-HOWTO:\n\n" + "By default tracefs removes all OTH file permission bits.\n" + "When mounting tracefs an optional group id can be specified\n" + "which adds the group to every directory and file in tracefs:\n\n" + "\t e.g. mount -t tracefs [-o [gid=3D]] nodev /sys/kernel/tracing\n\= n" "# echo 0 > tracing_on : quick way to disable tracing\n" "# echo 1 > tracing_on : quick way to re-enable tracing\n\n" " Important files:\n" --=20 2.45.2 From nobody Sun Nov 24 20:57:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6252B1CEAAC for ; Fri, 1 Nov 2024 18:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; cv=none; b=LJoKFG5sHg5nqGO6RHIx3A6/WeaRzfwQzPXpak/qsEjmhIZ9j1voT02mnO8/nz2ccfwSYp+e+A3HiaupcNH6DKpWj2Oz2Qf4K0Pi8vCPp2qJdg1/qYkZauibwJkn1EVKlkAvCHWRNr4B6nkxr4KlXz8kLtN5aeDxTJRK8ICPiA0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730485977; c=relaxed/simple; bh=oeCReh64l4m4+kFJ7b+1DM7GsvtpgHfmbM82h6AcaWw=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=CU7/MwsrZOquCCnSMe3sT02d/bk/Tecrt4zJClr2ja8E2F5YMCbNVgjrtT4iez0T1L0YW68IHbG8t3kSOzuRM3pgTpdfjIv2yi3fkiMxHGIU7LYCpIbMcpKIv64jdRaLiwGG9iEp0prowtcJZlbEPQkQ74EpdnWfURz9cF1GREs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E50A0C4CED4; Fri, 1 Nov 2024 18:32:56 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t6wT5-00000005X4z-3nkF; Fri, 01 Nov 2024 14:33:55 -0400 Message-ID: <20241101183355.781823434@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 01 Nov 2024 14:33:30 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Eric Sandeen , Shuah Khan , Ali Zahraee , Christian Brauner , David Howells , Kalesh Singh Subject: [for-linus][PATCH 3/3] tracing/selftests: Add tracefs mount options test References: <20241101183327.693623203@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kalesh Singh Add a selftest to check that the tracefs gid mount option is applied correctly. ./ftracetest test.d/00basic/mount_options.tc Use the new readme string "[gid=3D] as a requirement and also update test_ownership.tc requirements to use this. Cc: Eric Sandeen Cc: Mathieu Desnoyers Cc: Shuah Khan Cc: Ali Zahraee Cc: Christian Brauner Cc: David Howells Cc: Masami Hiramatsu Link: https://lore.kernel.org/20241030171928.4168869-4-kaleshsingh@google.c= om Signed-off-by: Kalesh Singh Signed-off-by: Steven Rostedt (Google) --- .../ftrace/test.d/00basic/mount_options.tc | 101 ++++++++++++++++++ .../ftrace/test.d/00basic/test_ownership.tc | 16 +-- .../testing/selftests/ftrace/test.d/functions | 25 +++++ 3 files changed, 129 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/ftrace/test.d/00basic/mount_opt= ions.tc diff --git a/tools/testing/selftests/ftrace/test.d/00basic/mount_options.tc= b/tools/testing/selftests/ftrace/test.d/00basic/mount_options.tc new file mode 100644 index 000000000000..35e8d47d6072 --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/00basic/mount_options.tc @@ -0,0 +1,101 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test tracefs GID mount option +# requires: "[gid=3D]":README + +fail() { + local msg=3D"$1" + + echo "FAILED: $msg" + exit_fail +} + +find_alternate_gid() { + local original_gid=3D"$1" + tac /etc/group | grep -v ":$original_gid:" | head -1 | cut -d: -f3 +} + +mount_tracefs_with_options() { + local mount_point=3D"$1" + local options=3D"$2" + + mount -t tracefs -o "$options" nodev "$mount_point" + + setup +} + +unmount_tracefs() { + local mount_point=3D"$1" + + # Need to make sure the mount isn't busy so that we can umount it + (cd $mount_point; finish_ftrace;) + + cleanup +} + +create_instance() { + local mount_point=3D"$1" + local instance=3D"$mount_point/instances/$(mktemp -u test-XXXXXX)" + + mkdir "$instance" + echo "$instance" +} + +remove_instance() { + local instance=3D"$1" + + rmdir "$instance" +} + +check_gid() { + local mount_point=3D"$1" + local expected_gid=3D"$2" + + echo "Checking permission group ..." + + cd "$mount_point" + + for file in "." "events" "events/sched" "events/sched/sched_switch" "even= ts/sched/sched_switch/enable"; do + local gid=3D`stat -c "%g" $file` + if [ "$gid" -ne "$expected_gid" ]; then + cd - # Return to the previous working directory (tracefs root) + fail "$(realpath $file): Expected group $expected_gid; Got group $gid" + fi + done + + cd - # Return to the previous working directory (tracefs root) +} + +test_gid_mount_option() { + local mount_point=3D$(get_mount_point) + local mount_options=3D$(get_mnt_options "$mount_point") + local original_group=3D$(stat -c "%g" .) + local other_group=3D$(find_alternate_gid "$original_group") + + # Set up mount options with new GID for testing + local new_options=3D`echo "$mount_options" | sed -e "s/gid=3D[0-9]*/gid= =3D$other_group/"` + if [ "$new_options" =3D "$mount_options" ]; then + new_options=3D"$mount_options,gid=3D$other_group" + mount_options=3D"$mount_options,gid=3D$original_group" + fi + + # Unmount existing tracefs instance and mount with new GID + unmount_tracefs "$mount_point" + mount_tracefs_with_options "$mount_point" "$new_options" + + check_gid "$mount_point" "$other_group" + + # Check that files created after the mount inherit the GID + local instance=3D$(create_instance "$mount_point") + check_gid "$instance" "$other_group" + remove_instance "$instance" + + # Unmount and remount with the original GID + unmount_tracefs "$mount_point" + mount_tracefs_with_options "$mount_point" "$mount_options" + check_gid "$mount_point" "$original_group" +} + +test_gid_mount_option + +exit 0 diff --git a/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.t= c b/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc index 094419e190c2..e71cc3ad0bdf 100644 --- a/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc +++ b/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc @@ -1,24 +1,14 @@ #!/bin/sh # SPDX-License-Identifier: GPL-2.0 # description: Test file and directory ownership changes for eventfs +# requires: "[gid=3D]":README =20 original_group=3D`stat -c "%g" .` original_owner=3D`stat -c "%u" .` =20 -mount_point=3D`stat -c '%m' .` +local mount_point=3D$(get_mount_point) =20 -# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the -# current working directory (which should be a tracefs) as the mount point. -if [ ! -d "$mount_point" ]; then - if mount | grep -qw $PWD ; then - mount_point=3D$PWD - else - # If PWD doesn't work, that is an environmental problem. - exit_unresolved - fi -fi - -mount_options=3D`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'` +mount_options=3D$(get_mnt_options "$mount_point") =20 # find another owner and group that is not the original other_group=3D`tac /etc/group | grep -v ":$original_group:" | head -1 | cu= t -d: -f3` diff --git a/tools/testing/selftests/ftrace/test.d/functions b/tools/testin= g/selftests/ftrace/test.d/functions index 779f3e62ec90..84d6a9c7ad67 100644 --- a/tools/testing/selftests/ftrace/test.d/functions +++ b/tools/testing/selftests/ftrace/test.d/functions @@ -193,3 +193,28 @@ ftrace_errlog_check() { # err-prefix command-with-erro= r-pos-by-^ command-file # " Command: " and "^\n" =3D> 13 test $(expr 13 + $pos) -eq $N } + +# Helper to get the tracefs mount point +get_mount_point() { + local mount_point=3D`stat -c '%m' .` + + # If stat -c '%m' does not work (e.g. busybox) or failed, try to use the + # current working directory (which should be a tracefs) as the mount poin= t. + if [ ! -d "$mount_point" ]; then + if mount | grep -qw "$PWD"; then + mount_point=3D$PWD + else + # If PWD doesn't work, that is an environmental problem. + exit_unresolved + fi + fi + echo "$mount_point" +} + +# Helper function to retrieve mount options for a given mount point +get_mnt_options() { + local mnt_point=3D"$1" + local opts=3D$(mount | grep -m1 "$mnt_point" | sed -e 's/.*(\(.*\)).*/\1/= ') + + echo "$opts" +} \ No newline at end of file --=20 2.45.2