Skip to content

Commit 568a7b9

Browse files
committed
fix_nvoc_dtor.cocci: fix NVOC_DYNAMIC_DTOR CFI violations
Signed-off-by: Mathias Krause <minipli@grsecurity.net>
1 parent 5f55948 commit 568a7b9

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

src/nvidia/fix_nvoc_dtor.cocci

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
+}

0 commit comments

Comments
 (0)