fortify-headers/include/unistd.h

84 lines
1.9 KiB
C
Raw Normal View History

2015-01-29 20:31:30 +00:00
#ifndef FORTIFY_UNISTD_H_
#define FORTIFY_UNISTD_H_
#include_next <unistd.h>
#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
2015-02-04 15:12:50 +00:00
#define __errordecl(name, msg) extern void name(void) __attribute__ ((__error__(msg)))
2015-01-29 20:31:30 +00:00
2015-02-05 14:03:53 +00:00
__errordecl(__confstr_error, "confstr: buffer overflow detected");
static inline __attribute__ ((always_inline))
size_t
__fortify_confstr(int name, char *buf, size_t len)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(len) && len > bos)
__confstr_error();
if (len > bos)
__builtin_trap();
return confstr(name, buf, len);
}
2015-02-05 14:07:14 +00:00
__errordecl(__getcwd_error, "getcwd: buffer overflow detected");
static inline __attribute__ ((always_inline))
char *
__fortify_getcwd(char *buf, size_t len)
{
size_t bos = __builtin_object_size(buf, 0);
if (__builtin_constant_p(len) && len > bos)
__getcwd_error();
if (len > bos)
__builtin_trap();
return getcwd(buf, len);
}
2015-01-30 09:44:49 +00:00
__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);
}
2015-01-29 20:31:30 +00:00
__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);
}
2015-02-05 14:03:53 +00:00
#undef confstr
#define confstr(name, buf, len) __fortify_confstr(name, buf, len)
2015-02-05 14:07:14 +00:00
#undef getcwd
#define getcwd(buf, len) __fortify_getcwd(buf, len)
2015-01-30 09:44:49 +00:00
#undef pread
#define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset)
2015-01-29 20:31:30 +00:00
#undef read
#define read(fd, buf, n) __fortify_read(fd, buf, n)
#undef __errordecl
2015-01-29 20:31:30 +00:00
#endif
#endif