Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaldo Carvalho de Melo" <acme@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v6 02/13] perf scripts: Add a coverage harness for perf
Date: Tue, 17 Nov 2026 20:15:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-219585@kernel.org> (raw)
In-Reply-To: <20261117200433.45174-3-acme@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf scripts: sticky-bit and root-shell bypass in
  check_root_path()
- [Low] perf scripts: symlink following via chown without -h in
  fix_ownership()
--

commit a0647612ea142c35ebab221e54c45ab482f8b7ad
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 coverage statistics.

> diff --git a/tools/perf/scripts/coverage.sh b/tools/perf/scripts/coverage.sh
> new file mode 100755
> index 0000000000000..073d665229697
> --- /dev/null
> +++ b/tools/perf/scripts/coverage.sh
> @@ -0,0 +1,984 @@

[ ... ]

> +DEFAULT_BUILD_DIR=/tmp/build/perf-coverage

[ ... ]

> +if [ "$ROOT" = 1 ] && [ "$PREP" = 0 ] && [ "$TABLE" = 0 ]; then
> +	if [ "$REPORT_ONLY" = 0 ] && [ "$SKIP_BUILD" = 0 ] &&
> +	   { [ "$FORCE_BUILD" = 1 ] ||
> +	     { [ ! -f "$PERF_DIR/.coverage-build-dir" ] &&
> +	       [ ! -x "$BUILD_DIR/perf" ]; }; }; then
> +		say "Running as root, building perf in $BUILD_DIR"
> +	else
> +		SKIP_BUILD=1
> +		say "Running as root, using the coverage build in $BUILD_DIR"
> +	fi
> +fi
> +
> +#
> +# Under sudo the build directory comes from a file the invoking user wrote;
> +# root would execute the perf found there and delete and rewrite its cov/
> +# directory, 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.
> +#
> +check_root_path()
> +{
> +	local path=$1 uid gid mode
> +	[ "$ROOT" = 1 ] && [ -n "${SUDO_UID:-}" ] || return 0
> +	# 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"
> +	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 (/tmp) lets anybody create entries
> +			# but not replace one that belongs to somebody else.
> +			if [ $(( 8#$mode & 8#1000 )) = 0 ]; then
> +				[ "$uid" = 0 ] || [ "$uid" = "$SUDO_UID" ] ||
> +					die "$path is owned by uid $uid, not by root or $SUDO_UID"
> +				# A user private group with umask 002
> +				# gives 775, so 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" != "${SUDO_GID:-}" ]; then
> +					die "$path is writable by group $gid"
> +				fi
> +			fi
> +		fi
> +		[ "$path" = / ] && return 0
> +		path=$(dirname "$path")
> +	done
> +}

[Severity: Medium]
Can an unprivileged local user bypass check_root_path() by setting the
sticky bit on /tmp/build/perf-coverage?

Any user can chmod +t directories and files they own, and the sticky bit
check skips all uid, gid and permission checks when $(( 8#$mode & 8#1000 ))
is non-zero instead of only allowing root-owned sticky directories.
Because DEFAULT_BUILD_DIR is /tmp/build/perf-coverage and running as root
automatically sets SKIP_BUILD=1 whenever $BUILD_DIR/perf is executable, a
local user can pre-create /tmp/build/perf-coverage/perf with mode 1755 (and
a dummy __llvm_profile symbol) so that root executes their binary.  In
addition, an owner of a sticky /tmp/build directory can still unlink and
replace subdirectories inside it, and check_root_path() returns early when
SUDO_UID is unset in a direct root shell.

[ ... ]

> +fix_ownership()
> +{
> +	local uid gid path
> +	[ "$ROOT" = 1 ] || return 0
> +	if [ -n "${SUDO_UID:-}" ]; then
> +		uid=$SUDO_UID
> +		gid=${SUDO_GID:-$SUDO_UID}
> +	else
> +		read -r uid gid < <(stat -c '%u %g' "$PERF_DIR" 2>/dev/null) || return 0
> +	fi
> +	[ "$uid" != 0 ] || return 0
> +
> +	for path in "${COV_WRITES[@]}"; do
> +		[ -e "$COV_DIR/$path" ] || continue
> +		chown -R "$uid:$gid" "$COV_DIR/$path" ||
> +			warn "could not give $COV_DIR/$path back to uid $uid"
> +	done
> +	if [ "$COV_CREATED" = 1 ]; then
> +		chown "$uid:$gid" "$COV_DIR" ||
> +			warn "could not give $COV_DIR back to uid $uid"
> +	fi
> +	if [ "$BUILD_CREATED" = 1 ]; then
> +		chown -R "$uid:$gid" "$BUILD_DIR" ||
> +			warn "could not give $BUILD_DIR back to uid $uid"
> +	fi
> +	if [ -n "$BUILD_STAMP" ] && [ -f "$BUILD_STAMP" ]; then
> +		# -h: find matches a symlink by its own owner, but chown would
> +		# otherwise follow it and change a file outside the build tree.
> +		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
> +	if [ "$STATE_DIR_CREATED" = 1 ] && [ -d "$STATE_DIR" ]; then
> +		chown "$uid:$gid" "$STATE_DIR" ||
> +			warn "could not give $STATE_DIR back to uid $uid"
> +	fi
> +	if [ -f "$STATE_FILE" ]; then
> +		chown "$uid:$gid" "$STATE_FILE" ||
> +			warn "could not give $STATE_FILE back to uid $uid"
> +	fi
> +	if [ -f "$POINTER" ]; then
> +		chown "$uid:$gid" "$POINTER" ||
> +			warn "could not give $POINTER back to uid $uid"
> +	fi
> +}

[Severity: Low]
Should the other chown calls in fix_ownership() also pass -h (and -RP when
recursive)?

Unlike the find ... -exec chown -h call on $BUILD_DIR, the chown calls on
$COV_DIR/$path, $STATE_FILE and $POINTER do not pass -h, and [ -e ] / [ -f ]
succeed on symlinks to existing files.  If any of those paths is a symlink
(for example during --report-only, a subset -t run where baseline.txt is
not overwritten, or an early exit after the EXIT trap is armed), root will
dereference the symlink and hand ownership of its target to $uid:$gid.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117200433.45174-1-acme@kernel.org?part=2

  reply	other threads:[~2026-11-17 20:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 20:04 [PATCH v6 0/13] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
2026-11-17 20:04 ` [PATCH v6 01/13] perf jvmti: Link the agent with EXTRA_CFLAGS Arnaldo Carvalho de Melo
2026-11-17 20:10   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 02/13] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
2026-11-17 20:15   ` sashiko-bot [this message]
2026-11-17 20:04 ` [PATCH v6 03/13] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
2026-11-17 20:35   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 04/13] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
2026-11-17 20:13   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 05/13] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
2026-11-17 20:11   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 06/13] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
2026-11-17 20:10   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 07/13] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
2026-11-17 20:20   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 08/13] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
2026-11-17 20:11   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 09/13] perf tests: Silence the libbfd probe in build-test Arnaldo Carvalho de Melo
2026-11-17 20:08   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 10/13] perf build: Record the display-only features in FEATURE-DUMP Arnaldo Carvalho de Melo
2026-11-17 20:15   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 11/13] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
2026-11-17 20:18   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 12/13] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
2026-11-17 20:12   ` sashiko-bot
2026-11-17 20:04 ` [PATCH v6 13/13] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
2026-11-17 20:29   ` 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-219585@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