fortify-headers

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

unistd.h (5859B)


      1 /*
      2  * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
      3  * Copyright (C) 2022 q66 <q66@chimera-linux.org>
      4  *
      5  * Permission to use, copy, modify, and/or distribute this software for any
      6  * purpose with or without fee is hereby granted.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #ifndef _FORTIFY_UNISTD_H
     18 #define _FORTIFY_UNISTD_H
     19 
     20 #if !defined(__cplusplus) && !defined(__clang__)
     21 __extension__
     22 #endif
     23 #include_next <unistd.h>
     24 
     25 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
     26 #include "fortify-headers.h"
     27 
     28 #ifdef __cplusplus
     29 extern "C" {
     30 #endif
     31 
     32 #undef confstr
     33 #undef getcwd
     34 #undef getgroups
     35 #undef gethostname
     36 #undef getlogin_r
     37 #undef pread
     38 #undef read
     39 #undef readlink
     40 #undef readlinkat
     41 #undef ttyname_r
     42 #undef write
     43 
     44 __fh_access(write_only, 2, 3)
     45 #if __has_builtin(__builtin_confstr)
     46 __diagnose_as_builtin(__builtin_confstr, 1, 2, 3)
     47 #endif
     48 _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l)
     49 {
     50 	__fh_size_t __b = __fh_bos(__s, 0);
     51 	__fh_size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b);
     52 
     53 	if (__l > __b && __r > __b)
     54 		__builtin_trap();
     55 	return __r;
     56 }
     57 
     58 __fh_access(write_only, 1, 2)
     59 #if __has_builtin(__builtin_getcwd)
     60 __diagnose_as_builtin(__builtin_getcwd, 1, 2)
     61 #endif
     62 _FORTIFY_FN(getcwd) char *getcwd(char * _FORTIFY_POS0 __s, size_t __l)
     63 {
     64 	__fh_size_t __b = __fh_bos(__s, 0);
     65 
     66 	if (__l > __b)
     67 		__builtin_trap();
     68 	return __orig_getcwd(__s, __l);
     69 }
     70 
     71 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
     72 #undef getdomainname
     73 __fh_access(write_only, 1, 2)
     74 #if __has_builtin(__builtin_getdomainname)
     75 __diagnose_as_builtin(__builtin_getdomainname, 1, 2)
     76 #endif
     77 _FORTIFY_FN(getdomainname) int getdomainname(char * _FORTIFY_POS0 __s, size_t __l)
     78 {
     79 	__fh_size_t __b = __fh_bos(__s, 0);
     80 
     81 	if (__l > __b)
     82 		__builtin_trap();
     83 	return __orig_getdomainname(__s, __l);
     84 }
     85 #endif
     86 
     87 #if __has_builtin(__builtin_getgroups)
     88 __diagnose_as_builtin(__builtin_getgroups, 1, 2)
     89 #endif
     90 _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t * _FORTIFY_POS0 __s)
     91 {
     92 	__fh_size_t __b = __fh_bos(__s, 0);
     93 
     94 	if (__l > 0 && (unsigned)__l > __b / sizeof(gid_t))
     95 		__builtin_trap();
     96 	return __orig_getgroups(__l, __s);
     97 }
     98 
     99 __fh_access(write_only, 1, 2)
    100 #if __has_builtin(__builtin_gethostname)
    101 __diagnose_as_builtin(__builtin_gethostname, 1, 2)
    102 #endif
    103 _FORTIFY_FN(gethostname) int gethostname(char * _FORTIFY_POS0 __s, size_t __l)
    104 {
    105 	__fh_size_t __b = __fh_bos(__s, 0);
    106 
    107 	if (__l > __b)
    108 		__builtin_trap();
    109 	return __orig_gethostname(__s, __l);
    110 }
    111 
    112 __fh_access(write_only, 1, 2)
    113 #if __has_builtin(__builtin_getlogin_r)
    114 __diagnose_as_builtin(__builtin_getlogin_r, 1, 2)
    115 #endif
    116 _FORTIFY_FN(getlogin_r) int getlogin_r(char * _FORTIFY_POS0 __s, size_t __l)
    117 {
    118 	__fh_size_t __b = __fh_bos(__s, 0);
    119 
    120 	if (__l > __b)
    121 		__builtin_trap();
    122 	return __orig_getlogin_r(__s, __l);
    123 }
    124 
    125 #if __has_builtin(__builtin_pread)
    126 __diagnose_as_builtin(__builtin_pread, 1, 2, 3, 4)
    127 #endif
    128 _FORTIFY_FN(pread) ssize_t pread(int __f, void * _FORTIFY_POS0 __s,
    129                                  size_t __n, off_t __o)
    130 {
    131 	__fh_size_t __b = __fh_bos(__s, 0);
    132 
    133 	if (__n > __b)
    134 		__builtin_trap();
    135 	return __orig_pread(__f, __s, __n, __o);
    136 }
    137 
    138 __fh_access(write_only, 2, 3)
    139 #if __has_builtin(__builtin_read)
    140 __diagnose_as_builtin(__builtin_read, 1, 2, 3)
    141 #endif
    142 _FORTIFY_FN(read) ssize_t read(int __f, void * _FORTIFY_POS0 __s, size_t __n)
    143 {
    144 	__fh_size_t __b = __fh_bos(__s, 0);
    145 
    146 	if (__n > __b)
    147 		__builtin_trap();
    148 	return __orig_read(__f, __s, __n);
    149 }
    150 
    151 __fh_access(write_only, 2, 3)
    152 #if __has_builtin(__builtin_readlink)
    153 __diagnose_as_builtin(__builtin_readlink, 1, 2, 3)
    154 #endif
    155 _FORTIFY_FN(readlink) ssize_t readlink(const char *__p,
    156                                        char * _FORTIFY_POS0 __s, size_t __n)
    157 {
    158 	__fh_size_t __b = __fh_bos(__s, 0);
    159 
    160 	if (__n > __b)
    161 		__builtin_trap();
    162 	return __orig_readlink(__p, __s, __n);
    163 }
    164 
    165 __fh_access(write_only, 3, 4)
    166 #if __has_builtin(__builtin_readlinkat)
    167 __diagnose_as_builtin(__builtin_readlinkat, 1, 2, 3, 4)
    168 #endif
    169 _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p,
    170                                            char * _FORTIFY_POS0 __s, size_t __n)
    171 {
    172 	__fh_size_t __b = __fh_bos(__s, 0);
    173 
    174 	if (__n > __b)
    175 		__builtin_trap();
    176 	return __orig_readlinkat(__f, __p, __s, __n);
    177 }
    178 
    179 __fh_access(write_only, 2, 3)
    180 #if __has_builtin(__builtin_ttyname_r)
    181 __diagnose_as_builtin(__builtin_ttyname_r, 1, 2, 3)
    182 #endif
    183 _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char * _FORTIFY_POS0 __s,
    184                                      size_t __n)
    185 {
    186 	__fh_size_t __b = __fh_bos(__s, 0);
    187 
    188 	if (__n > __b)
    189 		__builtin_trap();
    190 	return __orig_ttyname_r(__f, __s, __n);
    191 }
    192 
    193 __fh_access(read_only, 2, 3)
    194 #if __has_builtin(__builtin_write)
    195 __diagnose_as_builtin(__builtin_write, 1, 2, 3)
    196 #endif
    197 _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s,
    198                                  size_t __n)
    199 {
    200 	__fh_size_t __b = __fh_bos(__s, 0);
    201 
    202 	if (__n > __b)
    203 		__builtin_trap();
    204 	return __orig_write(__f, __s, __n);
    205 }
    206 
    207 #if __has_builtin(__builtin_pwrite)
    208 __diagnose_as_builtin(__builtin_pwrite, 1, 2, 3, 4)
    209 #endif
    210 _FORTIFY_FN(pwrite) ssize_t pwrite(int __f, const void * _FORTIFY_POS0 __s,
    211                                  size_t __n, off_t __o)
    212 {
    213 	__fh_size_t __b = __fh_bos(__s, 0);
    214 
    215 	if (__n > __b)
    216 		__builtin_trap();
    217 	return __orig_pwrite(__f, __s, __n, __o);
    218 }
    219 
    220 #ifdef __cplusplus
    221 }
    222 #endif
    223 
    224 #endif
    225 
    226 #endif