From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v7 02/13] perf scripts: Add a coverage harness for perf
Date: Wed, 18 Nov 2026 22:08:34 +0000 [thread overview]
Message-ID: <sashiko-outbox-222085@kernel.org> (raw)
In-Reply-To: <20261118215716.114868-3-acme@kernel.org>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf scripts: abspath() symlink resolution and non-root
bypass in coverage.sh
- [Low] perf scripts: check_root_path() leaf sticky exemption and
unchecked state read
- [Low] perf scripts: fix_ownership() chowns existing root BUILD_DIR
without mindepth
--
commit f925befacbe87aac1cdf421770ce4909c03a1946
Author: Arnaldo Carvalho de Melo <acme@redhat.com>
perf scripts: Add a coverage harness for perf
This commit adds tools/perf/scripts/coverage.sh and corresponding make
targets to build perf with clang source-based coverage, run the perf test
suite, and report line and function coverage.
> diff --git a/tools/perf/scripts/coverage.sh b/tools/perf/scripts/coverage.sh
> new file mode 100755
> index 0000000000000..9c77644e77f91
> --- /dev/null
> +++ b/tools/perf/scripts/coverage.sh
> @@ -0,0 +1,1114 @@
[ ... ]
> +# The tests run from the kernel tree root, so keep these absolute. The
> +# Makefile passes FULL_O resolved with readlink -f; resolve here too so an
> +# in-tree build directory compares equal to the physical PERF_DIR below.
> +abspath()
> +{
> + realpath -m "$1"
> +}
> +BUILD_DIR=$(abspath "$BUILD_DIR")
> +
> +#
> +# Root executes the perf in the build directory and deletes and rewrites
> +# its cov/ directory, and that directory comes from a file the invoking
> +# user wrote or is the default under /tmp, so refuse one owned by somebody
> +# else or writable by another group or by others. Every parent is checked
> +# too: a directory another user can write lets them swap an entry between
> +# this check and the run. The trusted user is the one behind sudo, or the
> +# owner of the kernel tree when root runs the script directly, as in a
> +# container with the tree mounted from the host.
> +#
> +TRUST_UID=0
> +TRUST_GID=0
> +if [ "$ROOT" = 1 ]; then
> + if [ -n "${SUDO_UID:-}" ]; then
> + TRUST_UID=$SUDO_UID
> + TRUST_GID=${SUDO_GID:-$SUDO_UID}
> + else
> + read -r TRUST_UID TRUST_GID < \
> + <(stat -c '%u %g' "$PERF_DIR" 2>/dev/null) ||
> + die "could not stat $PERF_DIR"
> + fi
> +fi
> +
> +check_root_path()
> +{
> + local path=$1 uid gid mode owners=root
> + [ "$ROOT" = 1 ] || return 0
> + [ "$TRUST_UID" = 0 ] || owners="root or uid $TRUST_UID"
> + # abspath() resolves links such as /home -> /var/home on Silverblue
> + # and CoreOS, so the walk below checks their targets. A path not
> + # resolved that way, like $BUILD_DIR/perf, must not be a link.
> + [ -L "$path" ] && die "$path is a symbolic link, use its target"
[Severity: Medium]
Can another local user hijack the default /tmp/build/perf-coverage path
because abspath() resolves symlinks with realpath -m before
check_root_path() runs, and check_root_path() returns immediately when
ROOT=0?
During a root run, if another user creates /tmp/build and places a
symlink at /tmp/build/perf-coverage pointing to a root- or
developer-owned directory, realpath -m resolves the symlink to its
target before check_root_path() walks the path, bypassing validation of
the untrusted /tmp/build directory and redirecting rm -rf "$COV_DIR/raw"
or fix_ownership()'s chown -Rh. During a non-root run (such as make
coverage-prep), check_root_path() is skipped and mkdir_checked() accepts
any pre-existing directory, so a pre-created mode 0777
/tmp/build/perf-coverage lets another user replace $BUILD_DIR/perf
before tests or a subsequent root run execute it.
> + while :; do
> + if [ -e "$path" ]; then
> + if ! read -r uid gid mode < \
> + <(stat -c '%u %g %a' "$path" 2>/dev/null); then
> + die "could not stat $path"
> + fi
> + # A sticky directory owned by root, like /tmp, lets
> + # anybody create entries but not replace one that
> + # belongs to somebody else. Anyone can set the sticky
> + # bit on what they own, so nothing else is exempt.
> + if ! { [ -d "$path" ] && [ "$uid" = 0 ] &&
> + [ $(( 8#$mode & 8#1000 )) != 0 ]; }; then
> + [ "$uid" = 0 ] || [ "$uid" = "$TRUST_UID" ] ||
> + die "$path is owned by uid $uid, not by $owners"
> + # With user private groups, which most
> + # distributions set up, umask 002 gives
> + # 775 and only the developer's own group
> + # may write here.
> + [ $(( 8#$mode & 8#002 )) = 0 ] ||
> + die "$path is writable by others"
> + if [ $(( 8#$mode & 8#020 )) != 0 ] &&
> + [ "$gid" != "$TRUST_GID" ]; then
> + die "$path is writable by group $gid"
> + fi
> + fi
> + fi
> + [ "$path" = / ] && return 0
> + path=$(dirname "$path")
> + done
> +}
> +
> +# The state file is read below, so check BUILD_DIR before opening it: a
> +# directory another user can write lets them replace state with a fifo
> +# and hang root.
> +check_root_path "$BUILD_DIR"
> +
> +if [ "$COV_DIR_SET" = 0 ] && [ -f "$BUILD_DIR/cov/state" ]; then
> + COV_DIR=$(awk -F= '$1 == "COV_DIR" { print $2; exit }' \
> + "$BUILD_DIR/cov/state")
> +fi
[Severity: Low]
Does check_root_path() exempt a sticky directory even when it is the
target directory $1 itself rather than an ancestor, and should
check_root_path() also validate "$BUILD_DIR/cov/state" before awk reads
it?
Because the sticky-bit exemption applies on the first iteration of the
loop, passing /tmp or /var/tmp as BUILD_DIR or COV_DIR passes
check_root_path() and lets root read, write, and chown entries directly
inside a world-writable sticky directory. In addition, only $BUILD_DIR
is checked before reading $BUILD_DIR/cov/state, so an untrusted or
symlinked cov/state is opened before check_root_path "$COV_DIR" runs.
[ ... ]
> + if [ -n "$BUILD_STAMP" ] && [ -f "$BUILD_STAMP" ]; then
> + find "$BUILD_DIR" -user 0 -newer "$BUILD_STAMP" \
> + -exec chown -h "$uid:$gid" {} + ||
> + warn "could not give the files root built in $BUILD_DIR back to uid $uid"
> + rm -f "$BUILD_STAMP" || true
> + fi
[Severity: Low]
Without -mindepth 1, doesn't find "$BUILD_DIR" -user 0 -newer
"$BUILD_STAMP" also match and chown $BUILD_DIR itself when root builds
in a pre-existing root-owned directory?
Creating build outputs such as $BUILD_DIR/perf updates $BUILD_DIR's
mtime after $BUILD_STAMP is created, so find includes the top-level
directory and hands ownership of $BUILD_DIR to $uid:$gid despite
BUILD_CREATED=0.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118215716.114868-1-acme@kernel.org?part=2
next prev parent reply other threads:[~2026-11-18 22:08 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 21:57 [PATCH v7 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
2026-11-18 21:57 ` [PATCH v7 01/13] perf build: Link the jvmti agent and the GTK UI with EXTRA_CFLAGS Arnaldo Carvalho de Melo
2026-11-18 22:01 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
2026-11-18 22:08 ` sashiko-bot [this message]
2026-11-18 21:57 ` [PATCH v7 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
2026-11-18 22:14 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
2026-11-18 22:08 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
2026-11-18 22:01 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
2026-11-18 22:04 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
2026-11-18 22:03 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
2026-11-18 22:03 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
2026-11-18 22:00 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 10/13] perf build: Record the features checked outside FEATURE_TESTS Arnaldo Carvalho de Melo
2026-11-18 22:04 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
2026-11-18 22:03 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
2026-11-18 22:03 ` sashiko-bot
2026-11-18 21:57 ` [PATCH v7 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
2026-11-18 22:04 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-222085@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=acme@kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox