Skip to content

Security: Fix CWE-95 (Eval Injection) in _get_torch_dtype()#1964

Open
yoavkatz wants to merge 1 commit intomainfrom
security/fix-cwe95-eval-injection
Open

Security: Fix CWE-95 (Eval Injection) in _get_torch_dtype()#1964
yoavkatz wants to merge 1 commit intomainfrom
security/fix-cwe95-eval-injection

Conversation

@yoavkatz
Copy link
Copy Markdown
Member

Security Fix: CWE-95 (Eval Injection)

Summary

Fixed a HIGH severity security vulnerability (CVSS 7.8) in the _get_torch_dtype() method that could allow arbitrary code execution.

Vulnerability Details

  • CWE: CWE-95 (Eval Injection)
  • Severity: HIGH (CVSS 7.8)
  • Location: src/unitxt/inference.py:550 (before fix)
  • Reported by: External security researcher via IBM PSIRT

The _get_torch_dtype() method used Python's eval() function with insufficient input validation, which could be exploited to execute arbitrary code.

The Fix

Replaced the eval() call with a secure lookup table containing an explicit whitelist of 21 valid torch dtypes:

torch_dtypes = {
    "torch.float16": torch.float16,
    "torch.float32": torch.float32,
    "torch.bfloat16": torch.bfloat16,
    # ... 18 more valid dtypes
}
dtype = torch_dtypes.get(self.torch_dtype)

Changes

  • src/unitxt/inference.py: Replace eval() with explicit whitelist of valid torch dtypes
  • tests/inference/test_inference_engine.py: Add comprehensive security tests
    • test_torch_dtype_security_fix() - Full integration test
    • test_torch_dtype_security_fix_fast() - Fast unit test (1.7s)

Security Improvements

✅ Eliminates code injection vulnerability
✅ Validates input against explicit whitelist
✅ No breaking changes - all legitimate torch dtypes continue to work
✅ Better performance (dict lookup vs eval)
✅ Clearer error messages listing supported values

Testing

All tests pass including new security-specific tests:

pytest tests/inference/test_inference_engine.py -k test_torch_dtype_security -v
# PASSED

Impact Assessment

  • ✅ No breaking changes
  • ✅ No user action required
  • ✅ Performance improvement
  • ✅ Better UX with clearer error messages

Security Note

Detailed exploitation information has been shared privately with the security team and will be disclosed responsibly after the fix is released.

Replace dangerous eval() with secure lookup table to prevent arbitrary code execution.

- Vulnerability: The _get_torch_dtype() method used eval() with insufficient validation,
  allowing arbitrary code execution through the torch_dtype parameter via __globals__ chain
- Severity: HIGH (CVSS 7.8)
- CWE: CWE-95 (Eval Injection)

Changes:
- src/unitxt/inference.py: Replace eval() with explicit whitelist of 21 valid torch dtypes
- tests/inference/test_inference_engine.py: Add comprehensive security tests
  - test_torch_dtype_security_fix(): Full integration test
  - test_torch_dtype_security_fix_fast(): Fast unit test (1.7s)

Security improvements:
- Blocks arbitrary code execution via __globals__ chain
- Rejects malicious payloads without executing any code
- No breaking changes - all legitimate torch dtypes continue to work
- Better performance (dict lookup vs eval)
- Clearer error messages listing supported values

Reported by: External security researcher via IBM PSIRT

Signed-off-by: Yoav Katz <katz@il.ibm.com>
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.

1 participant