syscall-intercept 0.1.0

Userspace syscall intercepting library.
Documentation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * Copyright 2016-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *
 *     * Neither the name of the copyright holder nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "intercept_util.h"
#include "intercept.h"
#include "libsyscall_intercept_hook_point.h"

#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <ctype.h>
#include <stddef.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syscall.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <sched.h>
#include <linux/limits.h>

void
mprotect_no_intercept(void *addr, size_t len, int prot,
			const char *msg_on_error)
{
	long result = syscall_no_intercept(SYS_mprotect, addr, len, prot);

	xabort_on_syserror(result, msg_on_error);
}

void *
xmmap_anon(size_t size)
{
	long addr = syscall_no_intercept(SYS_mmap,
				NULL, size,
				PROT_READ | PROT_WRITE,
				MAP_PRIVATE | MAP_ANON, -1, (off_t)0);

	xabort_on_syserror(addr, __func__);

	return (void *) addr;
}

void *
xmremap(void *addr, size_t old, size_t new)
{
	long new_addr = syscall_no_intercept(SYS_mremap, addr,
				old, new, MREMAP_MAYMOVE);

	xabort_on_syserror(new_addr, __func__);

	return (void *) new_addr;
}

void
xmunmap(void *addr, size_t len)
{
	long result = syscall_no_intercept(SYS_munmap, addr, len);

	xabort_on_syserror(result, __func__);
}

long
xlseek(long fd, unsigned long off, int whence)
{
	long result = syscall_no_intercept(SYS_lseek, fd, off, whence);

	xabort_on_syserror(result, __func__);

	return result;
}

void
xread(long fd, void *buffer, size_t size)
{
	long result = syscall_no_intercept(SYS_read, fd, buffer, size);

	if (result != (long)size)
		xabort_errno(syscall_error_code(result), __func__);
}

/* BEGIN CSTYLED */
static const char *const error_strings[] = {
#ifdef EPERM
	[EPERM] = "EPERM (Operation not permitted)",
#endif
#ifdef ENOENT
	[ENOENT] = "ENOENT (No such file or directory)",
#endif
#ifdef ESRCH
	[ESRCH] = "ESRCH (No such process)",
#endif
#ifdef EINTR
	[EINTR] = "EINTR (Interrupted system call)",
#endif
#ifdef EIO
	[EIO] = "EIO (I/O error)",
#endif
#ifdef ENXIO
	[ENXIO] = "ENXIO (No such device or address)",
#endif
#ifdef E2BIG
	[E2BIG] = "E2BIG (Argument list too long)",
#endif
#ifdef ENOEXEC
	[ENOEXEC] = "ENOEXEC (Exec format error)",
#endif
#ifdef EBADF
	[EBADF] = "EBADF (Bad file number)",
#endif
#ifdef ECHILD
	[ECHILD] = "ECHILD (No child processes)",
#endif
#ifdef EAGAIN
	[EAGAIN] = "EAGAIN (Try again)",
#endif
#ifdef ENOMEM
	[ENOMEM] = "ENOMEM (Out of memory)",
#endif
#ifdef EACCES
	[EACCES] = "EACCES (Permission denied)",
#endif
#ifdef EFAULT
	[EFAULT] = "EFAULT (Bad address)",
#endif
#ifdef ENOTBLK
	[ENOTBLK] = "ENOTBLK (Block device required)",
#endif
#ifdef EBUSY
	[EBUSY] = "EBUSY (Device or resource busy)",
#endif
#ifdef EEXIST
	[EEXIST] = "EEXIST (File exists)",
#endif
#ifdef EXDEV
	[EXDEV] = "EXDEV (Cross-device link)",
#endif
#ifdef ENODEV
	[ENODEV] = "ENODEV (No such device)",
#endif
#ifdef ENOTDIR
	[ENOTDIR] = "ENOTDIR (Not a directory)",
#endif
#ifdef EISDIR
	[EISDIR] = "EISDIR (Is a directory)",
#endif
#ifdef EINVAL
	[EINVAL] = "EINVAL (Invalid argument)",
#endif
#ifdef ENFILE
	[ENFILE] = "ENFILE (File table overflow)",
#endif
#ifdef EMFILE
	[EMFILE] = "EMFILE (Too many open files)",
#endif
#ifdef ENOTTY
	[ENOTTY] = "ENOTTY (Not a typewriter)",
#endif
#ifdef ETXTBSY
	[ETXTBSY] = "ETXTBSY (Text file busy)",
#endif
#ifdef EFBIG
	[EFBIG] = "EFBIG (File too large)",
#endif
#ifdef ENOSPC
	[ENOSPC] = "ENOSPC (No space left on device)",
#endif
#ifdef ESPIPE
	[ESPIPE] = "ESPIPE (Illegal seek)",
#endif
#ifdef EROFS
	[EROFS] = "EROFS (Read-only file system)",
#endif
#ifdef EMLINK
	[EMLINK] = "EMLINK (Too many links)",
#endif
#ifdef EPIPE
	[EPIPE] = "EPIPE (Broken pipe)",
#endif
#ifdef EDOM
	[EDOM] = "EDOM (Math argument out of domain of func)",
#endif
#ifdef ERANGE
	[ERANGE] = "ERANGE (Math result not representable)",
#endif
#ifdef EDEADLK
	[EDEADLK] = "EDEADLK (Resource deadlock would occur)",
#endif
#ifdef ENAMETOOLONG
	[ENAMETOOLONG] = "ENAMETOOLONG (File name too long)",
#endif
#ifdef ENOLCK
	[ENOLCK] = "ENOLCK (No record locks available)",
#endif
#ifdef ENOSYS
	[ENOSYS] = "ENOSYS (Invalid system call number)",
#endif
#ifdef ENOTEMPTY
	[ENOTEMPTY] = "ENOTEMPTY (Directory not empty)",
#endif
#ifdef ELOOP
	[ELOOP] = "ELOOP (Too many symbolic links encountered)",
#endif
#ifdef ENOMSG
	[ENOMSG] = "ENOMSG (No message of desired type)",
#endif
#ifdef EIDRM
	[EIDRM] = "EIDRM (Identifier removed)",
#endif
#ifdef ECHRNG
	[ECHRNG] = "ECHRNG (Channel number out of range)",
#endif
#ifdef EL2NSYNC
	[EL2NSYNC] = "EL2NSYNC (Level 2 not synchronized)",
#endif
#ifdef EL3HLT
	[EL3HLT] = "EL3HLT (Level 3 halted)",
#endif
#ifdef EL3RST
	[EL3RST] = "EL3RST (Level 3 reset)",
#endif
#ifdef ELNRNG
	[ELNRNG] = "ELNRNG (Link number out of range)",
#endif
#ifdef EUNATCH
	[EUNATCH] = "EUNATCH (Protocol driver not attached)",
#endif
#ifdef ENOCSI
	[ENOCSI] = "ENOCSI (No CSI structure available)",
#endif
#ifdef EL2HLT
	[EL2HLT] = "EL2HLT (Level 2 halted)",
#endif
#ifdef EBADE
	[EBADE] = "EBADE (Invalid exchange)",
#endif
#ifdef EBADR
	[EBADR] = "EBADR (Invalid request descriptor)",
#endif
#ifdef EXFULL
	[EXFULL] = "EXFULL (Exchange full)",
#endif
#ifdef ENOANO
	[ENOANO] = "ENOANO (No anode)",
#endif
#ifdef EBADRQC
	[EBADRQC] = "EBADRQC (Invalid request code)",
#endif
#ifdef EBADSLT
	[EBADSLT] = "EBADSLT (Invalid slot)",
#endif
#ifdef EBFONT
	[EBFONT] = "EBFONT (Bad font file format)",
#endif
#ifdef ENOSTR
	[ENOSTR] = "ENOSTR (Device not a stream)",
#endif
#ifdef ENODATA
	[ENODATA] = "ENODATA (No data available)",
#endif
#ifdef ETIME
	[ETIME] = "ETIME (Timer expired)",
#endif
#ifdef ENOSR
	[ENOSR] = "ENOSR (Out of streams resources)",
#endif
#ifdef ENONET
	[ENONET] = "ENONET (Machine is not on the network)",
#endif
#ifdef ENOPKG
	[ENOPKG] = "ENOPKG (Package not installed)",
#endif
#ifdef EREMOTE
	[EREMOTE] = "EREMOTE (Object is remote)",
#endif
#ifdef ENOLINK
	[ENOLINK] = "ENOLINK (Link has been severed)",
#endif
#ifdef EADV
	[EADV] = "EADV (Advertise error)",
#endif
#ifdef ESRMNT
	[ESRMNT] = "ESRMNT (Srmount error)",
#endif
#ifdef ECOMM
	[ECOMM] = "ECOMM (Communication error on send)",
#endif
#ifdef EPROTO
	[EPROTO] = "EPROTO (Protocol error)",
#endif
#ifdef EMULTIHOP
	[EMULTIHOP] = "EMULTIHOP (Multihop attempted)",
#endif
#ifdef EDOTDOT
	[EDOTDOT] = "EDOTDOT (RFS specific error)",
#endif
#ifdef EBADMSG
	[EBADMSG] = "EBADMSG (Not a data message)",
#endif
#ifdef EOVERFLOW
	[EOVERFLOW] = "EOVERFLOW (Value too large for defined data type)",
#endif
#ifdef ENOTUNIQ
	[ENOTUNIQ] = "ENOTUNIQ (Name not unique on network)",
#endif
#ifdef EBADFD
	[EBADFD] = "EBADFD (File descriptor in bad state)",
#endif
#ifdef EREMCHG
	[EREMCHG] = "EREMCHG (Remote address changed)",
#endif
#ifdef ELIBACC
	[ELIBACC] = "ELIBACC (Can not access a needed shared library)",
#endif
#ifdef ELIBBAD
	[ELIBBAD] = "ELIBBAD (Accessing a corrupted shared library)",
#endif
#ifdef ELIBSCN
	[ELIBSCN] = "ELIBSCN (.lib section in a.out corrupted)",
#endif
#ifdef ELIBMAX
	[ELIBMAX] = "ELIBMAX (Attempting to link in too many shared libraries)",
#endif
#ifdef ELIBEXEC
	[ELIBEXEC] = "ELIBEXEC (Cannot exec a shared library directly)",
#endif
#ifdef EILSEQ
	[EILSEQ] = "EILSEQ (Illegal byte sequence)",
#endif
#ifdef ERESTART
	[ERESTART] = "ERESTART (Interrupted system call should be restarted)",
#endif
#ifdef ESTRPIPE
	[ESTRPIPE] = "ESTRPIPE (Streams pipe error)",
#endif
#ifdef EUSERS
	[EUSERS] = "EUSERS (Too many users)",
#endif
#ifdef ENOTSOCK
	[ENOTSOCK] = "ENOTSOCK (Socket operation on non-socket)",
#endif
#ifdef EDESTADDRREQ
	[EDESTADDRREQ] = "EDESTADDRREQ (Destination address required)",
#endif
#ifdef EMSGSIZE
	[EMSGSIZE] = "EMSGSIZE (Message too long)",
#endif
#ifdef EPROTOTYPE
	[EPROTOTYPE] = "EPROTOTYPE (Protocol wrong type for socket)",
#endif
#ifdef ENOPROTOOPT
	[ENOPROTOOPT] = "ENOPROTOOPT (Protocol not available)",
#endif
#ifdef EPROTONOSUPPORT
	[EPROTONOSUPPORT] = "EPROTONOSUPPORT (Protocol not supported)",
#endif
#ifdef ESOCKTNOSUPPORT
	[ESOCKTNOSUPPORT] = "ESOCKTNOSUPPORT (Socket type not supported)",
#endif
#ifdef EOPNOTSUPP
	[EOPNOTSUPP] = "EOPNOTSUPP (Operation not supported on transport endpoint)",
#endif
#ifdef EPFNOSUPPORT
	[EPFNOSUPPORT] = "EPFNOSUPPORT (Protocol family not supported)",
#endif
#ifdef EAFNOSUPPORT
	[EAFNOSUPPORT] = "EAFNOSUPPORT (Address family not supported by protocol)",
#endif
#ifdef EADDRINUSE
	[EADDRINUSE] = "EADDRINUSE (Address already in use)",
#endif
#ifdef EADDRNOTAVAIL
	[EADDRNOTAVAIL] = "EADDRNOTAVAIL (Cannot assign requested address)",
#endif
#ifdef ENETDOWN
	[ENETDOWN] = "ENETDOWN (Network is down)",
#endif
#ifdef ENETUNREACH
	[ENETUNREACH] = "ENETUNREACH (Network is unreachable)",
#endif
#ifdef ENETRESET
	[ENETRESET] = "ENETRESET (Network dropped connection because of reset)",
#endif
#ifdef ECONNABORTED
	[ECONNABORTED] = "ECONNABORTED (Software caused connection abort)",
#endif
#ifdef ECONNRESET
	[ECONNRESET] = "ECONNRESET (Connection reset by peer)",
#endif
#ifdef ENOBUFS
	[ENOBUFS] = "ENOBUFS (No buffer space available)",
#endif
#ifdef EISCONN
	[EISCONN] = "EISCONN (Transport endpoint is already connected)",
#endif
#ifdef ENOTCONN
	[ENOTCONN] = "ENOTCONN (Transport endpoint is not connected)",
#endif
#ifdef ESHUTDOWN
	[ESHUTDOWN] = "ESHUTDOWN (Cannot send after transport endpoint shutdown)",
#endif
#ifdef ETOOMANYREFS
	[ETOOMANYREFS] = "ETOOMANYREFS (Too many references: cannot splice)",
#endif
#ifdef ETIMEDOUT
	[ETIMEDOUT] = "ETIMEDOUT (Connection timed out)",
#endif
#ifdef ECONNREFUSED
	[ECONNREFUSED] = "ECONNREFUSED (Connection refused)",
#endif
#ifdef EHOSTDOWN
	[EHOSTDOWN] = "EHOSTDOWN (Host is down)",
#endif
#ifdef EHOSTUNREACH
	[EHOSTUNREACH] = "EHOSTUNREACH (No route to host)",
#endif
#ifdef EALREADY
	[EALREADY] = "EALREADY (Operation already in progress)",
#endif
#ifdef EINPROGRESS
	[EINPROGRESS] = "EINPROGRESS (Operation now in progress)",
#endif
#ifdef ESTALE
	[ESTALE] = "ESTALE (Stale file handle)",
#endif
#ifdef EUCLEAN
	[EUCLEAN] = "EUCLEAN (Structure needs cleaning)",
#endif
#ifdef ENOTNAM
	[ENOTNAM] = "ENOTNAM (Not a XENIX named type file)",
#endif
#ifdef ENAVAIL
	[ENAVAIL] = "ENAVAIL (No XENIX semaphores available)",
#endif
#ifdef EISNAM
	[EISNAM] = "EISNAM (Is a named type file)",
#endif
#ifdef EREMOTEIO
	[EREMOTEIO] = "EREMOTEIO (Remote I/O error)",
#endif
#ifdef EDQUOT
	[EDQUOT] = "EDQUOT (Quota exceeded)",
#endif
#ifdef ENOMEDIUM
	[ENOMEDIUM] = "ENOMEDIUM (No medium found)",
#endif
#ifdef EMEDIUMTYPE
	[EMEDIUMTYPE] = "EMEDIUMTYPE (Wrong medium type)",
#endif
#ifdef ECANCELED
	[ECANCELED] = "ECANCELED (Operation Canceled)",
#endif
#ifdef ENOKEY
	[ENOKEY] = "ENOKEY (Required key not available)",
#endif
#ifdef EKEYEXPIRED
	[EKEYEXPIRED] = "EKEYEXPIRED (Key has expired)",
#endif
#ifdef EKEYREVOKED
	[EKEYREVOKED] = "EKEYREVOKED (Key has been revoked)",
#endif
#ifdef EKEYREJECTED
	[EKEYREJECTED] = "EKEYREJECTED (Key was rejected by service)",
#endif
#ifdef EOWNERDEAD
	[EOWNERDEAD] = "EOWNERDEAD (Owner died)",
#endif
#ifdef ENOTRECOVERABLE
	[ENOTRECOVERABLE] = "ENOTRECOVERABLE (State not recoverable)",
#endif
#ifdef ERFKILL
	[ERFKILL] = "ERFKILL (Operation not possible due to RF-kill)",
#endif
#ifdef EHWPOISON
	[EHWPOISON] = "EHWPOISON (Memory page has hardware error)",
#endif
};
/* END CSTYLED */

const char *
strerror_no_intercept(long errnum)
{
	static const char unkown[] = "Unknown error";

	if (errnum < 0 || (size_t)errnum >= ARRAY_SIZE(error_strings))
		return unkown;

	if (error_strings[errnum] == NULL)
		return unkown;

	return error_strings[errnum];
}