Skip to content

Commit 51455bb

Browse files
committed
tests/type_confusion: add dav1d_{alloc,free} instead of IA2_SHARED_DATA
This lets us not have to explain `IA2_SHARED_DATA` and better showcases how an untrusted compartment could exploit completely opaque types from another compartment.
1 parent 1ce0acd commit 51455bb

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

tests/type_confusion/dav1d.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ RUN: cat dav1d_call_gates_1.ld | FileCheck --check-prefix=LINKARGS %s
88
#define IA2_COMPARTMENT 2
99
#include <ia2_compartment_init.inc>
1010

11+
/// Allocate memory in dav1d's compartment 2.
12+
void* dav1d_alloc(const size_t size) {
13+
return malloc(size);
14+
}
15+
16+
void dav1d_free(void* const memory) {
17+
free(memory);
18+
}
19+
1120
IA2_CONSTRUCTOR // Registers that ptr `this` has type `Dav1dContext` now.
1221
int dav1d_open(Dav1dContext *const this, const Dav1dSettings *const s) {
1322
// Initialize `this`; implementation omitted.

tests/type_confusion/include/dav1d.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ typedef struct {
1414
ptrdiff_t stride[2];
1515
} Dav1dPicture;
1616

17+
void* dav1d_alloc(size_t size);
18+
19+
void dav1d_free(void* memory);
20+
1721
int dav1d_open(Dav1dContext *this, const Dav1dSettings *s);
1822

1923
void dav1d_close(Dav1dContext *this);

tests/type_confusion/main.c

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,78 @@ INIT_RUNTIME(2);
1414
#define IA2_COMPARTMENT 1
1515
#include <ia2_compartment_init.inc>
1616

17-
Dav1dContext c IA2_SHARED_DATA;
18-
Dav1dSettings settings IA2_SHARED_DATA;
19-
Dav1dPicture pic IA2_SHARED_DATA;
20-
Dav1dContext c2 IA2_SHARED_DATA;
21-
2217
Test(type_confusion, normal) {
23-
dav1d_open(&c, &settings);
24-
dav1d_get_picture(&c, &pic);
25-
dav1d_close(&c);
18+
Dav1dContext *c = dav1d_alloc(sizeof(Dav1dContext));
19+
Dav1dSettings *settings = dav1d_alloc(sizeof(Dav1dSettings));
20+
Dav1dPicture *pic = dav1d_alloc(sizeof(Dav1dPicture));
21+
22+
dav1d_open(c, settings);
23+
dav1d_get_picture(c, pic);
24+
dav1d_close(c);
25+
26+
dav1d_free(pic);
27+
dav1d_free(settings);
28+
dav1d_free(c);
2629
}
2730

2831
Test(type_confusion, uninitialized, .signal = SIGABRT) {
29-
dav1d_open(&c, &settings);
32+
Dav1dContext *c = dav1d_alloc(sizeof(Dav1dContext));
33+
Dav1dContext *c2 = dav1d_alloc(sizeof(Dav1dContext));
34+
Dav1dSettings *settings = dav1d_alloc(sizeof(Dav1dSettings));
35+
Dav1dPicture *pic = dav1d_alloc(sizeof(Dav1dPicture));
36+
37+
dav1d_open(c, settings);
3038
// Try to use another `Dav1dContext` that hasn't been constructed/opened yet.
31-
dav1d_get_picture(&c2, &pic);
32-
dav1d_close(&c);
39+
dav1d_get_picture(c2, pic); // Will `SIGABRT`.
40+
dav1d_close(c);
41+
42+
dav1d_free(pic);
43+
dav1d_free(settings);
44+
dav1d_free(c2);
45+
dav1d_free(c);
3346
}
3447

3548
Test(type_confusion, wrong_type, .signal = SIGABRT) {
36-
dav1d_open(&c, &settings);
49+
Dav1dContext *c = dav1d_alloc(sizeof(Dav1dContext));
50+
Dav1dSettings *settings = dav1d_alloc(sizeof(Dav1dSettings));
51+
Dav1dPicture *pic = dav1d_alloc(sizeof(Dav1dPicture));
52+
53+
dav1d_open(c, settings);
3754
// Try to use another type (`Dav1dPicture`) instead.
38-
dav1d_get_picture((Dav1dContext *)&pic, &pic);
39-
dav1d_close(&c);
55+
dav1d_get_picture((Dav1dContext *)pic, pic); // Will `SIGABRT`.
56+
dav1d_close(c);
57+
58+
dav1d_free(pic);
59+
dav1d_free(settings);
60+
dav1d_free(c);
4061
}
4162

4263
Test(type_confusion, null, .signal = SIGABRT) {
43-
dav1d_open(&c, &settings);
64+
Dav1dContext *c = dav1d_alloc(sizeof(Dav1dContext));
65+
Dav1dSettings *settings = dav1d_alloc(sizeof(Dav1dSettings));
66+
Dav1dPicture *pic = dav1d_alloc(sizeof(Dav1dPicture));
67+
68+
dav1d_open(c, settings);
4469
// Try to `NULL`.
45-
dav1d_get_picture(NULL, &pic);
46-
dav1d_close(&c);
70+
dav1d_get_picture(NULL, pic); // Will `SIGABRT`.
71+
dav1d_close(c);
72+
73+
dav1d_free(pic);
74+
dav1d_free(settings);
75+
dav1d_free(c);
4776
}
4877

4978
Test(type_confusion, use_after_free, .signal = SIGABRT) {
50-
dav1d_open(&c, &settings);
51-
dav1d_close(&c);
79+
Dav1dContext *c = dav1d_alloc(sizeof(Dav1dContext));
80+
Dav1dSettings *settings = dav1d_alloc(sizeof(Dav1dSettings));
81+
Dav1dPicture *pic = dav1d_alloc(sizeof(Dav1dPicture));
82+
83+
dav1d_open(c, settings);
84+
dav1d_close(c);
5285
// Try to use an already destructed `Dav1dContext`.
53-
dav1d_get_picture(&c, &pic);
86+
dav1d_get_picture(c, pic); // Will `SIGABRT`.
87+
88+
dav1d_free(pic);
89+
dav1d_free(settings);
90+
dav1d_free(c);
5491
}

0 commit comments

Comments
 (0)