fortify-headers

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

unistd.h (4637B)


      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 __access(write_only, 2, 3)
     45 _FORTIFY_FN(confstr) size_t confstr(int __n, char * _FORTIFY_POS0 __s, size_t __l)
     46 {
     47 	size_t __b = __bos(__s, 0);
     48 	size_t __r = __orig_confstr(__n, __s, __b > __l ? __l : __b);
     49 
     50 	if (__l > __b && __r > __b)
     51 		__builtin_trap();
     52 	return __r;
     53 }
     54 
     55 __access(write_only, 1, 2)
     56 _FORTIFY_FN(getcwd) char *getcwd(char * _FORTIFY_POS0 __s, size_t __l)
     57 {
     58 	size_t __b = __bos(__s, 0);
     59 
     60 	if (__l > __b)
     61 		__builtin_trap();
     62 	return __orig_getcwd(__s, __l);
     63 }
     64 
     65 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
     66 #undef getdomainname
     67 __access(write_only, 1, 2)
     68 _FORTIFY_FN(getdomainname) int getdomainname(char * _FORTIFY_POS0 __s, size_t __l)
     69 {
     70 	size_t __b = __bos(__s, 0);
     71 
     72 	if (__l > __b)
     73 		__builtin_trap();
     74 	return __orig_getdomainname(__s, __l);
     75 }
     76 #endif
     77 
     78 _FORTIFY_FN(getgroups) int getgroups(int __l, gid_t * _FORTIFY_POS0 __s)
     79 {
     80 	size_t __b = __bos(__s, 0);
     81 
     82 	if (__l > 0 && (unsigned)__l > __b / sizeof(gid_t))
     83 		__builtin_trap();
     84 	return __orig_getgroups(__l, __s);
     85 }
     86 
     87 __access(write_only, 1, 2)
     88 _FORTIFY_FN(gethostname) int gethostname(char * _FORTIFY_POS0 __s, size_t __l)
     89 {
     90 	size_t __b = __bos(__s, 0);
     91 
     92 	if (__l > __b)
     93 		__builtin_trap();
     94 	return __orig_gethostname(__s, __l);
     95 }
     96 
     97 __access(write_only, 1, 2)
     98 _FORTIFY_FN(getlogin_r) int getlogin_r(char * _FORTIFY_POS0 __s, size_t __l)
     99 {
    100 	size_t __b = __bos(__s, 0);
    101 
    102 	if (__l > __b)
    103 		__builtin_trap();
    104 	return __orig_getlogin_r(__s, __l);
    105 }
    106 
    107 _FORTIFY_FN(pread) ssize_t pread(int __f, void * _FORTIFY_POS0 __s,
    108                                  size_t __n, off_t __o)
    109 {
    110 	size_t __b = __bos(__s, 0);
    111 
    112 	if (__n > __b)
    113 		__builtin_trap();
    114 	return __orig_pread(__f, __s, __n, __o);
    115 }
    116 
    117 __access(write_only, 2, 3)
    118 _FORTIFY_FN(read) ssize_t read(int __f, void * _FORTIFY_POS0 __s, size_t __n)
    119 {
    120 	size_t __b = __bos(__s, 0);
    121 
    122 	if (__n > __b)
    123 		__builtin_trap();
    124 	return __orig_read(__f, __s, __n);
    125 }
    126 
    127 __access(write_only, 2, 3)
    128 _FORTIFY_FN(readlink) ssize_t readlink(const char *__p,
    129                                        char * _FORTIFY_POS0 __s, size_t __n)
    130 {
    131 	size_t __b = __bos(__s, 0);
    132 
    133 	if (__n > __b)
    134 		__builtin_trap();
    135 	return __orig_readlink(__p, __s, __n);
    136 }
    137 
    138 __access(write_only, 3, 4)
    139 _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p,
    140                                            char * _FORTIFY_POS0 __s, size_t __n)
    141 {
    142 	size_t __b = __bos(__s, 0);
    143 
    144 	if (__n > __b)
    145 		__builtin_trap();
    146 	return __orig_readlinkat(__f, __p, __s, __n);
    147 }
    148 
    149 __access(write_only, 2, 3)
    150 _FORTIFY_FN(ttyname_r) int ttyname_r(int __f, char * _FORTIFY_POS0 __s,
    151                                      size_t __n)
    152 {
    153 	size_t __b = __bos(__s, 0);
    154 
    155 	if (__n > __b)
    156 		__builtin_trap();
    157 	return __orig_ttyname_r(__f, __s, __n);
    158 }
    159 
    160 __access(read_only, 2, 3)
    161 _FORTIFY_FN(write) ssize_t write(int __f, const void * _FORTIFY_POS0 __s,
    162                                  size_t __n)
    163 {
    164 	size_t __b = __bos(__s, 0);
    165 
    166 	if (__n > __b)
    167 		__builtin_trap();
    168 	return __orig_write(__f, __s, __n);
    169 }
    170 
    171 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
    172 #undef swab
    173 
    174 _FORTIFY_FN(swab) void swab(const void * _FORTIFY_POS0 __os,
    175                                  void * _FORTIFY_POS0 __od, ssize_t __n)
    176 {
    177 	size_t __bs = __bos(__os, 0);
    178 	size_t __bd = __bos(__od, 0);
    179 
    180 	if ((size_t)__n > __bs || (size_t)__n > __bd)
    181 		__builtin_trap();
    182 	return __orig_swab(__os, __od, __n);
    183 }
    184 
    185 #endif
    186 
    187 #ifdef __cplusplus
    188 }
    189 #endif
    190 
    191 #endif
    192 
    193 #endif