T09 · Insecure Skill Coding Practices
Warning
- Location
- example.py:79
- Finding
- Predictable Temporary File Path Allows Local File Overwrite## Vulnerability Details **File Location**: `example.py`, lines 79–80 **Vulnerability Type**: Predictable temporary file and potential symbolic-link overwrite **Risk Level**: Medium ```python output_file = '/tmp/cn-font-test.png' plt.savefig(output_file, dpi=100, bbox_inches='tight', facecolor='white') ``` ### Technical Analysis The script writes an image to a fixed, predictable path in the shared `/tmp` directory. It neither creates the destination securely nor verifies that the path is a regular file rather than a symbolic link. On a system where relevant symbolic-link protections are absent, disabled, or inapplicable, another local user can create `/tmp/cn-font-test.png` as a symbolic link to a file writable by the victim. When `plt.savefig()` opens the predictable path, it may follow the link and truncate or overwrite the linked file. The issue is a time-of-check/time-of-use and unsafe temporary-file handling weakness. The earlier font existence check does not protect the output path. ### Attack Path 1. A local attacker determines that the script always writes to `/tmp/cn-font-test.png`. 2. Before the victim runs the script, the attacker creates that path as a symbolic link to a target file that the victim can modify. 3. The victim executes `example.py`. 4. `plt.savefig()` opens the predictable output path and may follow the attacker-controlled symbolic link. 5. The target file is overwritten with PNG data, potentially corrupting configuration, application data, or another victim-owned file. Exploitation depends on local filesystem permissions and operating-system symbolic-link protections. Running the script with elevated privileges would substantially increase the set of files potentially affected. ### Impact Assessment A successful attacker can overwrite or corrupt files writable by the account executing the script. The vulnerability does not independently grant code execution or additional privileges, but ...[truncated 306 chars]
- Remediation
- ## Remediation Suggestions Use Python's `tempfile` module to create an unpredictable file securely with restrictive permissions: ```python import os import tempfile fd, output_file = tempfile.mkstemp(prefix='cn-font-test-', suffix='.png') os.close(fd) try: plt.savefig( output_file, dpi=100, bbox_inches='tight', facecolor='white' ) print(f"Image generated successfully: {output_file}") finally: if os.path.exists(output_file): os.remove(output_file) ``` If the generated image must persist, create it inside a private directory owned by the current user and configured with mode `0700`. Do not construct temporary filenames from predictable constants or merely check whether a path exists before opening it, as that would remain vulnerable to race conditions. Run the example as an unprivileged user and keep system package installation separate from script execution. Where supported, retain operating-system protections against unsafe symbolic-link following as defense in depth.
