1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! PyO3 buffer overflow vulnerability fix verification
//!
//! # Security Context
//!
//! **Vulnerability:** RUSTSEC-2025-0020 - Buffer overflow in `PyString::from_object`
//! **CVSS:** Not rated (memory-exposure)
//! **Affected Versions:** pyo3 < 0.24.1
//! **Fixed In:** pyo3 >= 0.24.1
//! **Current Version:** 0.24.2
//! **Version Constraint:** See Cargo.toml:238-245 (workspace.dependencies.pyo3)
//!
//! ## Vulnerability Details
//!
//! `PyString::from_object` in pyo3 < 0.24.1 took `&str` arguments and forwarded
//! them directly to the Python C API without checking for terminating nul bytes.
//! This could lead the Python interpreter to read beyond the end of the `&str`
//! data and potentially leak contents of the out-of-bounds read.
//!
//! ### Attack Vector
//!
//! 1. Attacker provides `&str` without nul termination
//! 2. pyo3 forwards to Python C API without validation
//! 3. Python interpreter reads past string boundary
//! 4. Out-of-bounds read data leaked via Python exception
//!
//! ### Fix (pyo3 0.24.1+)
//!
//! `PyString::from_object` now allocates a `CString` to guarantee terminating
//! nul bytes, preventing buffer overflow.
//!
//! ## Test Approach
//!
//! These tests verify the pyo3 version and document the security fix.
//! Direct exploitation tests are not possible because:
//! 1. The vulnerable function is internal to pyo3
//! 2. The fix is transparent to user code
//! 3. Regression would require downgrading pyo3 (caught by Cargo.toml version)
//!
//! Instead, we:
//! 1. Verify pyo3 version >= 0.24.1
//! 2. Test Python string creation with boundary cases
//! 3. Document the vulnerability for awareness
//!
//! # References
//!
//! - Advisory: https://github.com/PyO3/pyo3/issues/5005
//! - RUSTSEC: https://rustsec.org/advisories/RUSTSEC-2025-0020
// ## Fix Documentation
//
// **Fix(RUSTSEC-2025-0020):** Updated pyo3 from 0.22.6 to 0.24.2
//
// **Root Cause:** pyo3 < 0.24.1 did not validate nul termination when passing
// `&str` to Python C API, allowing buffer overflow if string lacked nul byte.
// Python interpreter could read past string boundary and leak out-of-bounds
// data via exceptions.
//
// **Pitfall:** Always keep pyo3 updated. Buffer overflow vulnerabilities in
// FFI code are critical because:
// 1. They bypass Rust's memory safety guarantees
// 2. Exploitation requires minimal attacker control
// 3. Out-of-bounds reads can leak sensitive data
// 4. Python exceptions make leaked data visible
//
// Never downgrade pyo3 below 0.24.1. Monitor RUSTSEC advisories for FFI crates.
//
// ## Verification
//
// ```bash
// # Verify fix with cargo audit
// cargo audit
// # Expected: 0 vulnerabilities for pyo3
//
// # Verify pyo3 version
// cargo tree -i pyo3
// # Expected: pyo3 v0.24.2 (or higher)
//
// # Run this test suite
// cargo nextest run --package iron_runtime pyo3_string_safety_test
// # Expected: All tests pass
// ```