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
|
/* See LICENSE file for copyright and license details. */
#ifndef FORTIFY_UNISTD_H_
#define FORTIFY_UNISTD_H_
#include_next <unistd.h>
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
#define __errordecl(name, msg) extern void name(void) __attribute__((__error__(msg)))
__errordecl(__pread_error, "pread: buffer overflow detected");
static inline __attribute__ ((always_inline))
ssize_t
__fortify_pread(int fd, void *buf, size_t n, off_t offset)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(n) && n > bos)
__pread_error();
if (n > bos)
__builtin_trap();
return pread(fd, buf, n, offset);
}
__errordecl(__pwrite_error, "pwrite: buffer overflow detected");
static inline __attribute__ ((always_inline))
ssize_t
__fortify_pwrite(int fd, void *buf, size_t n, off_t offset)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(n) && n > bos)
__pwrite_error();
if (n > bos)
__builtin_trap();
return pwrite(fd, buf, n, offset);
}
__errordecl(__read_error, "read: buffer overflow detected");
static inline __attribute__ ((always_inline))
ssize_t
__fortify_read(int fd, void *buf, size_t n)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(n) && n > bos)
__read_error();
if (n > bos)
__builtin_trap();
return read(fd, buf, n);
}
__errordecl(__write_error, "write: buffer overflow detected");
static inline __attribute__ ((always_inline))
ssize_t
__fortify_write(int fd, void *buf, size_t n)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(n) && n > bos)
__write_error();
if (n > bos)
__builtin_trap();
return write(fd, buf, n);
}
#undef pread
#define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset)
#undef pwrite
#define pwrite(fd, buf, n, offset) __fortify_pwrite(fd, buf, n, offset)
#undef read
#define read(fd, buf, n) __fortify_read(fd, buf, n)
#undef write
#define write(fd, buf, n) __fortify_write(fd, buf, n)
#endif
#endif
|