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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/*
* Copyright © 2015-2018 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#undef _FILE_OFFSET_BITS /* prevent #define open open64 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/sysmacros.h>
#include <dlfcn.h>
#include <pthread.h>
#include "drm-uapi/i915_drm.h"
#include "util/hash_table.h"
#include "util/u_math.h"
#define INTEL_LOG_TAG "INTEL-SANITIZE-GPU"
#include "common/intel_log.h"
#include "common/gen_clflush.h"
static int (*libc_open)(const char *pathname, int flags, mode_t mode);
static int (*libc_close)(int fd);
static int (*libc_ioctl)(int fd, unsigned long request, void *argp);
static int (*libc_fcntl)(int fd, int cmd, int param);
#define DRM_MAJOR 226
/* TODO: we want to make sure that the padding forces
* the BO to take another page on the (PP)GTT; 4KB
* may or may not be the page size for the BO. Indeed,
* depending on GPU, kernel version and GEM size, the
* page size can be one of 4KB, 64KB or 2M.
*/
#define PADDING_SIZE 4096
struct refcnt_hash_table {
struct hash_table *t;
int refcnt;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define MUTEX_LOCK() do { \
if (unlikely(pthread_mutex_lock(&mutex))) { \
intel_loge("mutex_lock failed"); \
abort(); \
} \
} while (0)
#define MUTEX_UNLOCK() do { \
if (unlikely(pthread_mutex_unlock(&mutex))) { \
intel_loge("mutex_unlock failed"); \
abort(); \
} \
} while (0)
static struct hash_table *fds_to_bo_sizes = NULL;
static inline struct hash_table*
bo_size_table(int fd)
{
struct hash_entry *e = _mesa_hash_table_search(fds_to_bo_sizes,
(void*)(uintptr_t)fd);
return e ? ((struct refcnt_hash_table*)e->data)->t : NULL;
}
static inline uint64_t
bo_size(int fd, uint32_t handle)
{
struct hash_table *t = bo_size_table(fd);
if (!t)
return UINT64_MAX;
struct hash_entry *e = _mesa_hash_table_search(t, (void*)(uintptr_t)handle);
return e ? (uint64_t)e->data : UINT64_MAX;
}
static inline bool
is_drm_fd(int fd)
{
return !!bo_size_table(fd);
}
static inline void
add_drm_fd(int fd)
{
struct refcnt_hash_table *r = malloc(sizeof(*r));
r->refcnt = 1;
r->t = _mesa_pointer_hash_table_create(NULL);
_mesa_hash_table_insert(fds_to_bo_sizes, (void*)(uintptr_t)fd,
(void*)(uintptr_t)r);
}
static inline void
dup_drm_fd(int old_fd, int new_fd)
{
struct hash_entry *e = _mesa_hash_table_search(fds_to_bo_sizes,
(void*)(uintptr_t)old_fd);
struct refcnt_hash_table *r = e->data;
r->refcnt++;
_mesa_hash_table_insert(fds_to_bo_sizes, (void*)(uintptr_t)new_fd,
(void*)(uintptr_t)r);
}
static inline void
del_drm_fd(int fd)
{
struct hash_entry *e = _mesa_hash_table_search(fds_to_bo_sizes,
(void*)(uintptr_t)fd);
struct refcnt_hash_table *r = e->data;
if (!--r->refcnt) {
_mesa_hash_table_remove(fds_to_bo_sizes, e);
_mesa_hash_table_destroy(r->t, NULL);
free(r);
}
}
/* Our goal is not to have noise good enough for cryto,
* but instead values that are unique-ish enough that
* it is incredibly unlikely that a buffer overwrite
* will produce the exact same values.
*/
static uint8_t
next_noise_value(uint8_t prev_noise)
{
uint32_t v = prev_noise;
return (v * 103u + 227u) & 0xFF;
}
static void
fill_noise_buffer(uint8_t *dst, uint8_t start, uint32_t length)
{
for(uint32_t i = 0; i < length; ++i) {
dst[i] = start;
start = next_noise_value(start);
}
}
static bool
padding_is_good(int fd, uint32_t handle)
{
struct drm_i915_gem_mmap mmap_arg = {
.handle = handle,
.offset = align64(bo_size(fd, handle), 4096),
.size = PADDING_SIZE,
.flags = 0,
};
/* Unknown bo, maybe prime or userptr. Ignore */
if (mmap_arg.offset == UINT64_MAX)
return true;
uint8_t *mapped;
int ret;
uint8_t expected_value;
ret = libc_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
if (ret != 0) {
intel_logd("Unable to map buffer %d for pad checking.", handle);
return false;
}
mapped = (uint8_t*) (uintptr_t) mmap_arg.addr_ptr;
/* bah-humbug, we need to see the latest contents and
* if the bo is not cache coherent we likely need to
* invalidate the cache lines to get it.
*/
gen_invalidate_range(mapped, PADDING_SIZE);
expected_value = handle & 0xFF;
for (uint32_t i = 0; i < PADDING_SIZE; ++i) {
if (expected_value != mapped[i]) {
munmap(mapped, PADDING_SIZE);
return false;
}
expected_value = next_noise_value(expected_value);
}
munmap(mapped, PADDING_SIZE);
return true;
}
static int
create_with_padding(int fd, struct drm_i915_gem_create *create)
{
uint64_t original_size = create->size;
create->size = align64(original_size, 4096) + PADDING_SIZE;
int ret = libc_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, create);
create->size = original_size;
if (ret != 0)
return ret;
uint8_t *noise_values;
struct drm_i915_gem_mmap mmap_arg = {
.handle = create->handle,
.offset = align64(create->size, 4096),
.size = PADDING_SIZE,
.flags = 0,
};
ret = libc_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
if (ret != 0) {
intel_logd("Unable to map buffer %d for pad creation.\n", create->handle);
return 0;
}
noise_values = (uint8_t*) (uintptr_t) mmap_arg.addr_ptr;
fill_noise_buffer(noise_values, create->handle & 0xFF,
PADDING_SIZE);
munmap(noise_values, PADDING_SIZE);
_mesa_hash_table_insert(bo_size_table(fd), (void*)(uintptr_t)create->handle,
(void*)(uintptr_t)create->size);
return 0;
}
static int
exec_and_check_padding(int fd, unsigned long request,
struct drm_i915_gem_execbuffer2 *exec)
{
int ret = libc_ioctl(fd, request, exec);
if (ret != 0)
return ret;
struct drm_i915_gem_exec_object2 *objects =
(void*)(uintptr_t)exec->buffers_ptr;
uint32_t batch_bo = exec->flags & I915_EXEC_BATCH_FIRST ? objects[0].handle :
objects[exec->buffer_count - 1].handle;
struct drm_i915_gem_wait wait = {
.bo_handle = batch_bo,
.timeout_ns = -1,
};
ret = libc_ioctl(fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
if (ret != 0)
return ret;
bool detected_out_of_bounds_write = false;
for (int i = 0; i < exec->buffer_count; i++) {
uint32_t handle = objects[i].handle;
if (!padding_is_good(fd, handle)) {
detected_out_of_bounds_write = true;
intel_loge("Detected buffer out-of-bounds write in bo %d", handle);
}
}
if (unlikely(detected_out_of_bounds_write)) {
abort();
}
return 0;
}
static int
gem_close(int fd, struct drm_gem_close *close)
{
int ret = libc_ioctl(fd, DRM_IOCTL_GEM_CLOSE, close);
if (ret != 0)
return ret;
struct hash_table *t = bo_size_table(fd);
struct hash_entry *e =
_mesa_hash_table_search(t, (void*)(uintptr_t)close->handle);
if (e)
_mesa_hash_table_remove(t, e);
return 0;
}
static bool
is_i915(int fd) {
struct stat stat;
if (fstat(fd, &stat))
return false;
if (!S_ISCHR(stat.st_mode) || major(stat.st_rdev) != DRM_MAJOR)
return false;
char name[5] = "";
drm_version_t version = {
.name = name,
.name_len = sizeof(name) - 1,
};
if (libc_ioctl(fd, DRM_IOCTL_VERSION, &version))
return false;
return strcmp("i915", name) == 0;
}
__attribute__ ((visibility ("default"))) int
open(const char *path, int flags, ...)
{
va_list args;
mode_t mode;
va_start(args, flags);
mode = va_arg(args, int);
va_end(args);
int fd = libc_open(path, flags, mode);
MUTEX_LOCK();
if (fd >= 0 && is_i915(fd))
add_drm_fd(fd);
MUTEX_UNLOCK();
return fd;
}
__attribute__ ((visibility ("default"), alias ("open"))) int
open64(const char *path, int flags, ...);
__attribute__ ((visibility ("default"))) int
close(int fd)
{
MUTEX_LOCK();
if (is_drm_fd(fd))
del_drm_fd(fd);
MUTEX_UNLOCK();
return libc_close(fd);
}
__attribute__ ((visibility ("default"))) int
fcntl(int fd, int cmd, ...)
{
va_list args;
int param;
va_start(args, cmd);
param = va_arg(args, int);
va_end(args);
int res = libc_fcntl(fd, cmd, param);
MUTEX_LOCK();
if (is_drm_fd(fd) && cmd == F_DUPFD_CLOEXEC)
dup_drm_fd(fd, res);
MUTEX_UNLOCK();
return res;
}
__attribute__ ((visibility ("default"))) int
ioctl(int fd, unsigned long request, ...)
{
int res;
va_list args;
void *argp;
MUTEX_LOCK();
va_start(args, request);
argp = va_arg(args, void *);
va_end(args);
if (_IOC_TYPE(request) == DRM_IOCTL_BASE && !is_drm_fd(fd) && is_i915(fd)) {
intel_loge("missed drm fd %d", fd);
add_drm_fd(fd);
}
if (is_drm_fd(fd)) {
switch (request) {
case DRM_IOCTL_GEM_CLOSE:
res = gem_close(fd, (struct drm_gem_close*)argp);
goto out;
case DRM_IOCTL_I915_GEM_CREATE:
res = create_with_padding(fd, (struct drm_i915_gem_create*)argp);
goto out;
case DRM_IOCTL_I915_GEM_EXECBUFFER2:
case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR:
res = exec_and_check_padding(fd, request,
(struct drm_i915_gem_execbuffer2*)argp);
goto out;
default:
break;
}
}
res = libc_ioctl(fd, request, argp);
out:
MUTEX_UNLOCK();
return res;
}
static void __attribute__ ((constructor))
init(void)
{
fds_to_bo_sizes = _mesa_pointer_hash_table_create(NULL);
libc_open = dlsym(RTLD_NEXT, "open");
libc_close = dlsym(RTLD_NEXT, "close");
libc_fcntl = dlsym(RTLD_NEXT, "fcntl");
libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
}
|