From 03886aa26ca739f8458a54c663602ad09dc0a5b1 Mon Sep 17 00:00:00 2001 From: sin Date: Fri, 30 Jan 2015 09:44:49 +0000 Subject: Add pread() and pwrite() checks --- include/unistd.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'include') 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 @@ #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 @@ -38,6 +68,10 @@ __fortify_write(int fd, void *buf, size_t n) 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 -- cgit v1.3