fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers.git
Log | Files | Refs | README | LICENSE

unistd.h (3713B)


      1 /*
      2  * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
      3  *
      4  * Permission to use, copy, modify, and/or distribute this software for any
      5  * purpose with or without fee is hereby granted.
      6  *
      7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     10  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     12  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     13  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     14  */
     15 
     16 #ifndef _FORTIFY_UNISTD_H
     17 #define _FORTIFY_UNISTD_H
     18 
     19 #ifndef __cplusplus
     20 __extension__
     21 #endif
     22 #include_next <unistd.h>
     23 
     24 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
     25 #include "fortify-headers.h"
     26 
     27 #ifdef __cplusplus
     28 extern "C" {
     29 #endif
     30 
     31 #undef confstr
     32 #undef getcwd
     33 #undef getgroups
     34 #undef gethostname
     35 #undef getlogin_r
     36 #undef pread
     37 #undef read
     38 #undef readlink
     39 #undef readlinkat
     40 #undef ttyname_r
     41 #undef write
     42 
     43 _FORTIFY_FN(confstr) size_t confstr(int __n, char *__s, size_t __l)
     44 {
     45 	size_t __b = __builtin_object_size(__s, 0);
     46 	size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b);
     47 
     48 	if (__l > __b && __r > __b)
     49 		__builtin_trap();
     50 	return __r;
     51 }
     52 
     53 _FORTIFY_FN(getcwd) char *getcwd(char *__s, size_t __l)
     54 {
     55 	size_t __b = __builtin_object_size(__s, 0);
     56 
     57 	if (__l > __b)
     58 		__builtin_trap();
     59 	return __orig_getcwd(__s, __l);
     60 }
     61 
     62 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
     63 #undef getdomainname
     64 _FORTIFY_FN(getdomainname) int getdomainname(char *__s, size_t __l)
     65 {
     66 	size_t __b = __builtin_object_size(__s, 0);
     67 
     68 	if (__l > __b)
     69 		__builtin_trap();
     70 	return __orig_getdomainname(__s, __l);
     71 }
     72 #endif
     73 
     74 _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t *__s)
     75 {
     76 	size_t __b = __builtin_object_size(__s, 0);
     77 
     78 	if (__l > 0 && (unsigned)__l > __b / sizeof(gid_t))
     79 		__builtin_trap();
     80 	return __orig_getgroups(__l, __s);
     81 }
     82 
     83 _FORTIFY_FN(gethostname) int gethostname(char *__s, size_t __l)
     84 {
     85 	size_t __b = __builtin_object_size(__s, 0);
     86 
     87 	if (__l > __b)
     88 		__builtin_trap();
     89 	return __orig_gethostname(__s, __l);
     90 }
     91 
     92 _FORTIFY_FN(getlogin_r) int getlogin_r(char *__s, size_t __l)
     93 {
     94 	size_t __b = __builtin_object_size(__s, 0);
     95 
     96 	if (__l > __b)
     97 		__builtin_trap();
     98 	return __orig_getlogin_r(__s, __l);
     99 }
    100 
    101 _FORTIFY_FN(pread) ssize_t pread(int __f, void *__s, size_t __n, off_t __o)
    102 {
    103 	size_t __b = __builtin_object_size(__s, 0);
    104 
    105 	if (__n > __b)
    106 		__builtin_trap();
    107 	return __orig_pread(__f, __s, __n, __o);
    108 }
    109 
    110 _FORTIFY_FN(read) ssize_t read(int __f, void *__s, size_t __n)
    111 {
    112 	size_t __b = __builtin_object_size(__s, 0);
    113 
    114 	if (__n > __b)
    115 		__builtin_trap();
    116 	return __orig_read(__f, __s, __n);
    117 }
    118 
    119 _FORTIFY_FN(readlink) ssize_t readlink(const char *__p, char *__s, size_t __n)
    120 {
    121 	size_t __b = __builtin_object_size(__s, 0);
    122 
    123 	if (__n > __b)
    124 		__builtin_trap();
    125 	return __orig_readlink(__p, __s, __n);
    126 }
    127 
    128 _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p, char *__s, size_t __n)
    129 {
    130 	size_t __b = __builtin_object_size(__s, 0);
    131 
    132 	if (__n > __b)
    133 		__builtin_trap();
    134 	return __orig_readlinkat(__f, __p, __s, __n);
    135 }
    136 
    137 _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char *__s, size_t __n)
    138 {
    139 	size_t __b = __builtin_object_size(__s, 0);
    140 
    141 	if (__n > __b)
    142 		__builtin_trap();
    143 	return __orig_ttyname_r(__f, __s, __n);
    144 }
    145 
    146 _FORTIFY_FN(write) ssize_t write(int __f, const void *__s, size_t __n)
    147 {
    148 	size_t __b = __builtin_object_size(__s, 0);
    149 
    150 	if (__n > __b)
    151 		__builtin_trap();
    152 	return __orig_write(__f, __s, __n);
    153 }
    154 
    155 #ifdef __cplusplus
    156 }
    157 #endif
    158 
    159 #endif
    160 
    161 #endif