Skip to content

Fix symlink escape vulnerability in hw_metrics perfspect data handling (CWE-59) - #1061

Merged
mlim19 merged 2 commits into
masterfrom
fix_hw_metrics_symlink_vulnerability
Oct 7, 2026
Merged

mlim19 merged 2 commits into
masterfrom
fix_hw_metrics_symlink_vulnerability

Conversation

@mlim19

@mlim19 mlim19 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Description

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>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 18:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Hardens HWMetricsMonitor perfspect data handling to prevent symlink-based arbitrary file write via /tmp/perfspect_data (CWE-59).

Changes:

  • Use mkdir_owned_root() to ensure /tmp/perfspect_data is root-owned (reject/recreate attacker-controlled directories).
  • Replace shutil.copy() with safe_copy() to prevent copying through symlinked destinations.
  • Add _safe_remove() and use it for cleanup/removal to avoid removing paths that are symlinks.
File Description
gprofiler/​hw_metrics.py Secures perfspect directory creation and replaces unsafe copy/remove operations with hardened primitives.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread gprofiler/hw_metrics.py Resolved
Comment thread gprofiler/hw_metrics.py
Comment on lines +118 to +128
def _safe_remove(self, path: str) -> None:
"""
Safely remove a file, refusing to follow symlinks.

Security: Checks if path is a symlink before removal.
If path is a symlink, raises an exception instead of removing the target.
"""
if os.path.islink(path):
raise Exception(f"Refusing to remove {path}: symlinks are not allowed for security reasons")
if os.path.exists(path):
os.remove(path)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 6, 2026 02:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Existing writable directories remain insecure, and planted symlinks can cause persistent cleanup failures.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity 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.

@gmunoz
gmunoz self-requested a review October 7, 2026 17:24
@mlim19
mlim19 merged commit 5a8fc16 into master Oct 7, 2026
79 of 81 checks passed
@mlim19
mlim19 deleted the fix_hw_metrics_symlink_vulnerability branch October 7, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants