Summary
ONNX can follow a pre-planted symbolic link when saving model external data because it checks the destination and opens it through separate, non-atomic operations. A local attacker who can write to the serialization directory may redirect the victim's output into another file that the victim can modify, including sensitive configuration or authorization files.
Affected context
The vulnerability occurs when ONNX serializes external model data and derives the output path from the model's external-data location field. Risk is present when an attacker can create or modify directory entries in the destination directory and the victim process has write permission to a targeted file. The unsafe behavior involves a separate file-existence check followed by an output open operation that does not prevent symbolic-link following or enforce exclusive creation.
Exploit preconditions
- Attack surface: Local filesystem used for ONNX external-data serialization
- Required access: Ability to write entries in the directory where the victim saves external data
- Victim capability: The victim process must have write access to the redirected target file
- Trigger point: Model serialization through the external-data saving function
- Why exploitation is possible: A non-atomic existence check and subsequent file open can be influenced by a pre-existing symbolic link, allowing the output operation to affect a different writable file
Why this happens
The underlying bug combines unsafe symbolic-link handling with a time-of-check-to-time-of-use condition. The code checks whether a path is a file and later opens that path for writing without atomically binding the validation result to the opened file or rejecting symbolic links. This maps primarily to CWE-59: Improper Link Resolution Before File Access, with related aspects of CWE-367: Time-of-Check Time-of-Use Race Condition and CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
Potential impact
- An attacker could cause data to be written to an unintended file
- Sensitive authorization or configuration files could be altered
- The victim application's behavior could be changed through unauthorized file modification
- Scheduled tasks or startup configuration could be affected
- Local privilege escalation or broader compromise may occur depending on file permissions
General mitigation guidance
Mitigation generally focuses on making file creation resistant to symbolic-link substitution and eliminating check-then-use gaps.
Common defensive patterns include:
- Open output files atomically with exclusive-creation semantics
- Reject symbolic links and verify file identity using trusted directory descriptors
- Use filesystem APIs that prevent link following where available
- Keep model-output directories private, controlled, and non-writable by untrusted users
- Reduce the privileges of processes that serialize model data
- Validate external-data locations against controlled directories and avoid unnecessary path flexibility
- Apply restrictive permissions and separate temporary or staging areas from sensitive application files
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying file-existence checks followed by later opens or writes
- Code review searches for filesystem calls that omit exclusive creation or no-follow protections
- Review of path construction based on model, archive, or user-controlled metadata
- Runtime monitoring for unexpected writes through symbolic links or into sensitive paths
- Policy checks requiring private ownership and restrictive permissions on serialization directories
- Automated security review tools that flag TOCTOU patterns and unsafe link resolution

