summaryrefslogtreecommitdiff
path: root/include/unistd.h
diff options
context:
space:
mode:
authorsin2015-01-30 09:44:49 +0000
committersin2015-01-30 09:44:49 +0000
commit03886aa26ca739f8458a54c663602ad09dc0a5b1 (patch)
treee13055685e4f1056b3e66e669ea10cdac8845ef5 /include/unistd.h
parent03289c3954f5a61643e0fbcbce901487c7c4037a (diff)
Add pread() and pwrite() checks
Diffstat (limited to 'include/unistd.h')
-rw-r--r--include/unistd.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/include/unistd.h b/include/unistd.h
index 4f0439b..205e31c 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -8,6 +8,36 @@
8 8
9#define __errordecl(name, msg) extern void name(void) __attribute__((__error__(msg))) 9#define __errordecl(name, msg) extern void name(void) __attribute__((__error__(msg)))
10 10
11__errordecl(__pread_error, "pread: buffer overflow detected");
12static inline __attribute__ ((always_inline))
13ssize_t
14__fortify_pread(int fd, void *buf, size_t n, off_t offset)
15{
16 size_t bos = __builtin_object_size(buf, 0);
17
18 if (__builtin_constant_p(n) && n > bos)
19 __pread_error();
20
21 if (n > bos)
22 __builtin_trap();
23 return pread(fd, buf, n, offset);
24}
25
26__errordecl(__pwrite_error, "pwrite: buffer overflow detected");
27static inline __attribute__ ((always_inline))
28ssize_t
29__fortify_pwrite(int fd, void *buf, size_t n, off_t offset)
30{
31 size_t bos = __builtin_object_size(buf, 0);
32
33 if (__builtin_constant_p(n) && n > bos)
34 __pwrite_error();
35
36 if (n > bos)
37 __builtin_trap();
38 return pwrite(fd, buf, n, offset);
39}
40
11__errordecl(__read_error, "read: buffer overflow detected"); 41__errordecl(__read_error, "read: buffer overflow detected");
12static inline __attribute__ ((always_inline)) 42static inline __attribute__ ((always_inline))
13ssize_t 43ssize_t
@@ -38,6 +68,10 @@ __fortify_write(int fd, void *buf, size_t n)
38 return write(fd, buf, n); 68 return write(fd, buf, n);
39} 69}
40 70
71#undef pread
72#define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset)
73#undef pwrite
74#define pwrite(fd, buf, n, offset) __fortify_pwrite(fd, buf, n, offset)
41#undef read 75#undef read
42#define read(fd, buf, n) __fortify_read(fd, buf, n) 76#define read(fd, buf, n) __fortify_read(fd, buf, n)
43#undef write 77#undef write