T09 · Insecure Skill Coding Practices
- Location
scripts/face_compare.py:24- Finding
Automatic Loading of Unverified ONNX Models from Mutable Locations
- Content
View full analysis
Vulnerability Details
File Location:
scripts/face_compare.py, lines 24–36 and 395–403
Vulnerability Type: Untrusted model loading from shared and working directories
Risk Level: MediumVulnerable Code
python YUNET_MODEL_PATHS = [ os.environ.get("YUNET_MODEL", ""), "/tmp/face_detection_yunet_2023mar.onnx", "./face_detection_yunet_2023mar.onnx", os.path.join(os.path.dirname(__file__), "face_detection_yunet_2023mar.onnx"), ] SFACE_MODEL_PATHS = [ os.environ.get("SFACE_MODEL", ""), "/tmp/face_recognition_sface_2021dec.onnx", "./face_recognition_sface_2021dec.onnx", os.path.join(os.path.dirname(__file__), "face_recognition_sface_2021dec.onnx"), ]python yunet = yunet_path or find_model_file(YUNET_MODEL_PATHS) sface = sface_path or find_model_file(SFACE_MODEL_PATHS) comparator = None if yunet and sface: try: comparator = FaceCompareYuNetSFace(yunet, sface) print(f"✓ 使用方案A:YuNet + SFace(高精度模式)") except Exception as e:Technical Analysis
The application automatically searches for and loads ONNX models from
/tmp, the current working directory, environment-controlled paths, and the script directory. Thefind_model_file()validation only establishes that a candidate is a file; it does not verify its cryptographic hash, trusted ownership, permissions, canonical path, or provenance./tmpis normally shared and writable by multiple local users. The current working directory may also be controlled by the caller or another process. Consequently, a local attacker can pre-position files with the expected model names and cause OpenCV to parse them when the user does not supply explicit trusted model paths.Although ONNX models are data rather than Python source code, a substituted model can manipulate facial recognition results. A malformed model also reaches OpenCV's native model parser, increasing exposure t ...[truncated 1434 chars]
- Remediation
View remediation
Remediation Suggestions
- Remove shared
/tmpand implicit current-working-directory locations from the automatic model search path. - Store models in a dedicated application directory that is not writable by untrusted users.
- Pin approved SHA-256 hashes for both ONNX files and verify each model before passing it to OpenCV.
- Resolve each path with
realpathand enforce that it remains inside an approved model directory. - Reject symbolic links, non-regular files, files with unexpected ownership, and files writable by group or other users.
- Where environment variables or command-line paths remain supported, treat them as explicit trust decisions but still apply hash and file-security validation.
- Open files in a race-resistant manner where possible and avoid separate check-then-use operations on shared directories.
- Keep OpenCV current with security patches and return a hard failure rather than silently accepting an unverified high-accuracy model.
- Remove shared
