File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Coccinelle script to fix 'dtor' function pointer casts to use proper
2+ // types to prevent runtime CFI violations, e.g. under RAP.
3+ //
4+ // We need to create a thunk in any case as there may be calls to the strongly
5+ // typed function from other TUs, causing CFI violations if we'd just change
6+ // the function's signature to match the dtor function pointer prototype.
7+ //
8+ // typedef void (*NVOC_DYNAMIC_DTOR)(Dynamic*);
9+ //
10+ // (c) 2025,2026 Open Source Security, Inc. All Rights Reserved.
11+
12+ // replace function casts to (NVOC_DYNAMIC_DTOR) with its thunk
13+ @dtor_cast@
14+ identifier fn;
15+ fresh identifier fnthunk = "THUNK_" ## fn;
16+ @@
17+ - (NVOC_DYNAMIC_DTOR) &fn
18+ + &fnthunk
19+
20+ // add decl for the thunk, if needed, i.e. if the original func had one
21+ @thunk_decl@
22+ identifier dtor_cast.fn, dtor_cast.fnthunk, arg;
23+ typedef Dynamic;
24+ type T, R;
25+ @@
26+ // XXX: matching function decls is only poorly supported, so we need this hack
27+ (
28+ - R fn(T)
29+ + R fn(T);
30+ + static void fnthunk(Dynamic *)
31+ ;
32+ |
33+ - R fn(T arg)
34+ + R fn(T arg);
35+ + static void fnthunk(Dynamic *arg)
36+ ;
37+ )
38+
39+ // add thunk function
40+ @thunk_def@
41+ identifier dtor_cast.fn, dtor_cast.fnthunk, arg;
42+ typedef Dynamic;
43+ type T, R;
44+ @@
45+ R fn(T arg) { ... }
46+ +
47+ + static void fnthunk(Dynamic *arg) {
48+ + fn((T)arg);
49+ + }
You can’t perform that action at this time.
0 commit comments