fortify-headers

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

unistd.h (4368B)


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