Commit 98c2d52
committed
fix(iast): wrong memory address in subprocess in mcp servers (#15514)
IAST-enabled applications using Gunicorn/Uvicorn workers were
experiencing segmentation faults (~33% crash rate on MCP streaming
requests) due to memory corruption when processes fork.
- C++ global singletons (`taint_engine_context`, `initializer`)
initialized at module load
- Taint maps storing PyObject pointers by memory address
- Child processes after fork inherited stale pointers from parent
process memory
- Accessing these stale pointers → use-after-free → SIGSEGV crash
- Implemented `pthread_atfork` handler that automatically resets C++
global state in child processes after every fork:
- Added comprehensive null-check wrappers around all native functions to
prevent crashes when native state is
- Fixed test regression issues where context slots weren't being freed:
**[AddressSanitizer
(ASAN)](https://github.com/google/sanitizers/wiki/AddressSanitizer)** is
a fast memory error detector that catches use-after-free, buffer
overflows, and other memory corruption bugs at runtime.
**1. Runtime Environment (No Recompilation Required)**
The simplest way to test is using LD_PRELOAD with the system's libasan:
```bash
ASAN_LIB=$(gcc -print-file-name=libasan.so)
LD_PRELOAD=$ASAN_LIB \
ASAN_OPTIONS="detect_leaks=0:symbolize=1:abort_on_error=0" \
python3 -m pytest tests/appsec/iast/test_fork_handler_regression.py -v
```
**ASAN_OPTIONS explained:**
- `detect_leaks=0` - Disable leak detection (Python has many false
positives)
- `symbolize=1` - Show human-readable stack traces
- `abort_on_error=0` - Continue after first error (collect all errors)
**2. Build with ASAN (Optional, for deeper analysis)**
For more thorough testing, compile the native extension with ASAN:
```bash
export CFLAGS="-fsanitize=address -fno-omit-frame-pointer -g"
export CXXFLAGS="-fsanitize=address -fno-omit-frame-pointer -g"
export LDFLAGS="-fsanitize=address"
pip install --no-build-isolation --force-reinstall -e .
ASAN_OPTIONS="detect_leaks=0:symbolize=1:abort_on_error=0" \
python3 -m pytest tests/appsec/iast/test_fork_handler_regression.py -v
```
This script demonstrates the fork safety fix and can be used to verify
ASAN finds no errors:
```python
"""Minimal fork safety reproduction test for ASAN verification."""
import os
from ddtrace.appsec._iast._taint_tracking import OriginType
from ddtrace.appsec._iast._taint_tracking._native import initialize_native_state
from ddtrace.appsec._iast._taint_tracking._taint_objects import taint_pyobject
from ddtrace.appsec._iast._taint_tracking._context import (
start_request_context,
debug_context_array_free_slots_number,
debug_num_tainted_objects
)
def main():
print(f"[Parent PID {os.getpid()}] Initializing IAST...")
# Initialize native state
initialize_native_state()
# Create context and tainted objects in parent
ctx_id = start_request_context()
print(f"[Parent] Context created: {ctx_id}")
# Create some tainted objects (populates native maps)
for i in range(10):
taint_pyobject(f"data_{i}", "source", f"value_{i}", OriginType.PARAMETER)
tainted_count = debug_num_tainted_objects(ctx_id)
print(f"[Parent] Tainted objects: {tainted_count}")
# Fork
pid = os.fork()
if pid == 0:
# Child process
print(f"[Child PID {os.getpid()}] Started after fork")
# Verify pthread_atfork reset worked
free_slots = debug_context_array_free_slots_number()
print(f"[Child] Free slots: {free_slots}")
if free_slots > 0:
print("[Child] ✅ Context slots were reset (pthread_atfork worked!)")
os._exit(0) # Success
else:
print("[Child] ❌ Context slots NOT reset")
os._exit(1) # Failure
else:
# Parent waits for child
_, status = os.waitpid(pid, 0)
exit_code = os.WEXITSTATUS(status)
if exit_code == 0:
print(f"[Parent] ✅ Child exited cleanly - Fork safety verified!")
return 0
else:
print(f"[Parent] ❌ Child failed with exit code {exit_code}")
return 1
if __name__ == "__main__":
exit(main())
```
**Run with ASAN:**
```bash
LD_PRELOAD=$(gcc -print-file-name=libasan.so) \
ASAN_OPTIONS="detect_leaks=0:symbolize=1:abort_on_error=0" \
python3 test_fork_asan.py
```
**Expected output (success):**
```
[Parent PID 12345] Initializing IAST...
[Parent] Context created: 0
[Parent] Tainted objects: 10
[Child PID 12346] Started after fork
[Child] Free slots: 2
[Child] ✅ Context slots were reset (pthread_atfork worked!)
[Parent] ✅ Child exited cleanly - Fork safety verified!
```
**What ASAN would report WITHOUT this fix:**
```
==12346==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000040
==12346==The signal is caused by a READ memory access.
#0 0x7f... in get_tainted_object_map_by_ctx_id
#1 0x7f... in debug_context_array_free_slots_number
```
(cherry picked from commit d1b4fd8)1 parent cf60ea7 commit 98c2d52
File tree
58 files changed
+1478
-245
lines changed- ddtrace
- appsec/_iast
- _taint_tracking
- api
- aspects
- context
- taint_tracking
- tainted_ops
- tests
- settings
- releasenotes/notes
- tests/appsec
- iast_memcheck
- iast
- aspects
- taint_tracking
- integrations
- django_tests
- django_app
- fastapi_tests
- flask_tests
- packages_tests
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
58 files changed
+1478
-245
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
| 57 | + | |
57 | 58 | | |
58 | | - | |
59 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
60 | 64 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
64 | 69 | | |
65 | | - | |
66 | | - | |
67 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
68 | 74 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
73 | 78 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
78 | 82 | | |
79 | | - | |
80 | | - | |
| 83 | + | |
| 84 | + | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| |||
86 | 90 | | |
87 | 91 | | |
88 | 92 | | |
89 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
90 | 99 | | |
91 | 100 | | |
92 | 101 | | |
93 | 102 | | |
94 | 103 | | |
95 | | - | |
96 | 104 | | |
97 | 105 | | |
98 | 106 | | |
99 | 107 | | |
100 | 108 | | |
101 | 109 | | |
102 | | - | |
103 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
107 | | - | |
108 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
109 | 127 | | |
110 | | - | |
111 | | - | |
112 | 128 | | |
113 | 129 | | |
114 | 130 | | |
115 | | - | |
| 131 | + | |
116 | 132 | | |
117 | 133 | | |
118 | 134 | | |
| |||
182 | 198 | | |
183 | 199 | | |
184 | 200 | | |
| 201 | + | |
185 | 202 | | |
186 | 203 | | |
187 | 204 | | |
| |||
190 | 207 | | |
191 | 208 | | |
192 | 209 | | |
| 210 | + | |
193 | 211 | | |
194 | 212 | | |
195 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
65 | 67 | | |
66 | 68 | | |
67 | 69 | | |
68 | | - | |
| 70 | + | |
| 71 | + | |
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 46 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
0 commit comments