You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix symlink escape vulnerability in hw_metrics perfspect data handling (CWE-59) - #1061
HWMetricsMonitor writes perfspect data under the hardcoded, world-writable path /tmp/perfspect_data. A local unprivileged attacker can pre-create that directory and plant a symlink (e.g. <hostname>_metrics_summary_latest.csv -> /etc/cron.d/evil) before gProfiler starts. When gProfiler runs as root and calls shutil.copy(src, self._ps_latest_csv_filename), the copy follows the symlink and writes metrics CSV content to an attacker-chosen path — a root-privileged arbitrary file write.
Root causes in gprofiler/hw_metrics.py:
_cleanup() reused a pre-existing /tmp/perfspect_data directory without any ownership check.
shutil.copy() (lines ~137, ~153) and os.remove() follow a final symlink.
Fix
_cleanup() now calls mkdir_owned_root(), which refuses/recreates the directory unless it is root-owned, so an attacker-created directory (and any symlinks planted inside it) cannot be used.
Replace shutil.copy() with safe_copy() (atomic O_EXCL temp file + symlink check), which refuses to write through a symlinked destination.
Add _safe_remove() which refuses to unlink symlinks, replacing the bare os.remove() calls.
mkdir_owned_root() and safe_copy() are the same hardening primitives already used elsewhere in the codebase.
How Has This Been Tested?
Existing CI. The perfspect data directory is created and owned by gProfiler itself at runtime, so legitimate behavior is unchanged; only attacker-controlled directories/symlinks are now rejected.
The perfspect data directory /tmp/perfspect_data is in world-writable /tmp.
An attacker could pre-create this directory and place symlinks to redirect
file writes to arbitrary locations when gProfiler runs as root.
Fixes:
- Use mkdir_owned_root() to ensure directory is root-owned
- Use safe_copy() instead of shutil.copy() to reject symlinks on destination
- Add _safe_remove() helper that rejects symlinks before removal
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Harden permissions on existing root-owned directory
gprofiler/hw_metrics.py:134
mkdir_owned_root() only checks ownership and returns immediately for an existing root-owned directory; it does not correct its mode (gprofiler/utils/fs.py:226-231). The previous os.makedirs() creation inherited the caller's umask, so an existing root-owned but group/world-writable directory is possible and remains attacker-writable. In that case an attacker can still plant the summary source symlink (which safe_copy() follows when opening src) or repeatedly force failures. Normalize the directory permissions before inspecting its contents.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
HWMetricsMonitorwrites perfspect data under the hardcoded, world-writable path/tmp/perfspect_data. A local unprivileged attacker can pre-create that directory and plant a symlink (e.g.<hostname>_metrics_summary_latest.csv->/etc/cron.d/evil) before gProfiler starts. When gProfiler runs as root and callsshutil.copy(src, self._ps_latest_csv_filename), the copy follows the symlink and writes metrics CSV content to an attacker-chosen path — a root-privileged arbitrary file write.Root causes in
gprofiler/hw_metrics.py:_cleanup()reused a pre-existing/tmp/perfspect_datadirectory without any ownership check.shutil.copy()(lines ~137, ~153) andos.remove()follow a final symlink.Fix
_cleanup()now callsmkdir_owned_root(), which refuses/recreates the directory unless it is root-owned, so an attacker-created directory (and any symlinks planted inside it) cannot be used.shutil.copy()withsafe_copy()(atomicO_EXCLtemp file + symlink check), which refuses to write through a symlinked destination._safe_remove()which refuses to unlink symlinks, replacing the bareos.remove()calls.mkdir_owned_root()andsafe_copy()are the same hardening primitives already used elsewhere in the codebase.How Has This Been Tested?
Existing CI. The perfspect data directory is created and owned by gProfiler itself at runtime, so legitimate behavior is unchanged; only attacker-controlled directories/symlinks are now rejected.