/

CVE-2026-21878

Severity:

HIGH RISK

(7.5)

Path Traversal in BACnet Stack File Writing Functionality

CVE-2026-21878

Analyzed

Vulnerability type

Category:

Path Traversal / Directory Traversal

CWE:

CWE-22

Impact:

Arbitrary File Write

Severity:

HIGH RISK

(7.5)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

BACnet Stack's file writing functionality fails to validate user-supplied file paths, enabling path traversal when handling write operations.

In deployments where remote clients can influence BACnet file paths, an attacker may be able to write files to arbitrary directories on the underlying system.

Affected context

This vulnerability appears in BACnet Stack deployments that expose file-related BACnet services implemented via apps/readfile/main.c and ports/posix/bacfile-posix.c.

It is relevant when:

  • The application relies on BACnet file services for reading or writing files
  • File paths or filenames are derived from BACnet client input
  • The operating environment has a writable filesystem accessible to the BACnet process

The issue becomes more severe when the BACnet service runs with elevated privileges or has access to sensitive directories.

Exploit preconditions

  • Attack surface: BACnet protocol interface that processes file write or file-related requests
  • Required access: Ability to send crafted BACnet traffic to the affected device or service
  • Trigger point: Handling of BACnet operations that map user-controlled paths/filenames into filesystem write calls
  • Why exploitation is possible: The file writing logic does not constrain user-provided paths to an allowed directory, nor does it neutralize ../ sequences or absolute paths, allowing traversal outside the intended file root.

Why this happens

The core issue is a failure to enforce a safe file path boundary when translating BACnet file operations into POSIX file system actions.

User-controlled input is used directly as (or incorporated into) the target file path without:

  • Normalizing and canonicalizing the path
  • Verifying that the resolved path resides within an allowed directory
  • Rejecting absolute paths or traversal sequences such as ../

This aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where an application allows external input to escape intended directory constraints and manipulate arbitrary filesystem locations.

Potential impact

  • Creation of arbitrary files in directories writable by the BACnet process
  • Overwriting existing files that the service user can access
  • Planting or modifying configuration files that could alter system or application behavior
  • Assisting in code execution when combined with other weaknesses (e.g., writing scripts to interpreted directories)
  • Tampering with log files or data files used by other components on the system

General mitigation guidance

Effective mitigation focuses on enforcing strict file system boundaries and reducing the trust placed in client-supplied paths:

  • Introduce a designated root directory (chroot-like concept) for all BACnet file operations and ensure all paths are resolved relative to this root.
  • Normalize and canonicalize requested paths, then verify they remain within the approved directory before performing any file write.
  • Reject absolute paths and any path containing traversal sequences or unexpected characters according to a strict allowlist-based validation policy.
  • Use fixed filenames or controlled filename templates where possible, instead of allowing fully arbitrary names from clients.
  • Run the BACnet service under a dedicated, least-privileged account with write permission only to the directories strictly required for operation.
  • Consider mounting sensitive directories as read-only or inaccessible to the BACnet service context.

How teams detect issues like this

Detection typically combines code-centric review with runtime monitoring:

  • Static analysis / code review:
  • Search for file open or write calls (fopen, open, write, etc.) that use values derived from network input without intermediate validation.
  • Flag patterns where path normalization or boundary checks are missing before file operations.
  • Verify that any path-join helpers enforce a fixed base directory and reject traversal.
  • Configuration and policy checks:
  • Apply secure coding guidelines that prohibit direct use of user-supplied paths in file APIs.
  • Enforce code review checklists that explicitly question directory traversal handling for any file-service feature.
  • Runtime / operational monitoring:
  • Monitor file system activity for unexpected writes originating from the BACnet service user, especially outside designated data directories.
  • Use host-based intrusion detection to alert on file creations or modifications in sensitive paths initiated by the BACnet process.

Automated SAST or security-focused linters can help identify path traversal patterns early in the development lifecycle by enforcing these checks.

Sources

SourceLink
github.comgithub.com/bacnet-stack/bacnet-stack/commit/c5dc00a77b4bc2550befa67a930b333e299c18f3
github.comgithub.com/bacnet-stack/bacnet-stack/security/advisories/GHSA-p8rx-c26w-545j

Frequently asked

questions

Yes, in typical BACnet deployments where file operations are exposed over the network, a remote BACnet client that can communicate with the affected service may exploit the issue by sending crafted file path parameters.

The attacker needs network-level access to the BACnet interface that processes file-related requests.

No local shell access is required, but the attacker must be able to reach and interact with the BACnet service.

Constrain all file operations to a predefined directory tree, normalize and canonicalize incoming paths, then verify that the resolved path remains within that tree before opening or writing files.

Additionally, reject absolute paths and disallow traversal sequences and unexpected characters according to a strict validation policy.

Adopt a defensive file handling strategy that:

  • Treats all external path input as untrusted
  • Uses path-joining utilities that enforce a fixed base directory
  • Requires explicit boundary checks before file access
  • Integrates static checks and code review rules focused on path traversal and file permission handling into the development workflow.
Find out what's already
exploitable in your codebase.