summaryrefslogtreecommitdiff
path: root/include/unistd.h
diff options
context:
space:
mode:
authorsin2015-03-13 11:00:46 +0000
committersin2015-03-13 11:00:46 +0000
commit9f8c543dc81f0c4239acae6713f5414eb7dc681d (patch)
tree0c8dad17e27c510cc3c98502841aa1a75dfa3d1e /include/unistd.h
parentb211796d68c4a6b56f999534627791f3576b6135 (diff)
Rework fortify implementation to use extern inline
Overriding functions with macros is legal in C but a lot of software is not prepared for it. Use the extern inline method to achieve the same result.
Diffstat (limited to 'include/unistd.h')
-rw-r--r--include/unistd.h149
1 files changed, 72 insertions, 77 deletions
diff --git a/include/unistd.h b/include/unistd.h
index 5991155..aa4ddff 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -6,170 +6,165 @@
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0 6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7 7
8#ifndef __cplusplus 8#ifndef __cplusplus
9#undef confstr
10#undef getcwd
11#undef getgroups
12#undef gethostname
13#undef getlogin_r
14#undef pread
15#undef read
16#undef readlink
17#undef readlinkat
18#undef ttyname_r
19#undef write
9 20
10static inline __attribute__ ((always_inline)) 21extern size_t __confstr_orig(int, char *, size_t)
11size_t 22 __asm__(__USER_LABEL_PREFIX__ "confstr");
12__fortify_confstr(int name, char *buf, size_t len) 23extern __inline __attribute__((__always_inline__,__gnu_inline__))
24size_t confstr(int name, char *buf, size_t len)
13{ 25{
14 size_t bos = __builtin_object_size(buf, 0); 26 size_t bos = __builtin_object_size(buf, 0);
15 27
16 if (len > bos) 28 if (len > bos)
17 __builtin_trap(); 29 __builtin_trap();
18 return confstr(name, buf, len); 30 return __confstr_orig(name, buf, len);
19} 31}
20 32
21static inline __attribute__ ((always_inline)) 33extern char *__getcwd_orig(char *, size_t)
22char * 34 __asm__(__USER_LABEL_PREFIX__ "getcwd");
23__fortify_getcwd(char *buf, size_t len) 35extern __inline __attribute__((__always_inline__,__gnu_inline__))
36char *getcwd(char *buf, size_t len)
24{ 37{
25 size_t bos = __builtin_object_size(buf, 0); 38 size_t bos = __builtin_object_size(buf, 0);
26 39
27 if (len > bos) 40 if (len > bos)
28 __builtin_trap(); 41 __builtin_trap();
29 return getcwd(buf, len); 42 return __getcwd_orig(buf, len);
30} 43}
31 44
32#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 45#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
33static inline __attribute__ ((always_inline)) 46#undef getdomainname
34int 47extern int __getdomainname_orig(char *, size_t)
35__fortify_getdomainname(char *name, size_t len) 48 __asm__(__USER_LABEL_PREFIX__ "getdomainname");
49extern __inline __attribute__((__always_inline__,__gnu_inline__))
50int getdomainname(char *name, size_t len)
36{ 51{
37 size_t bos = __builtin_object_size(name, 0); 52 size_t bos = __builtin_object_size(name, 0);
38 53
39 if (len > bos) 54 if (len > bos)
40 __builtin_trap(); 55 __builtin_trap();
41 return getdomainname(name, len); 56 return __getdomainname_orig(name, len);
42} 57}
43#endif 58#endif
44 59
45static inline __attribute__ ((always_inline)) 60extern int __getgroups_orig(int, gid_t *)
46int 61 __asm__(__USER_LABEL_PREFIX__ "getgroups");
47__fortify_getgroups(int len, gid_t *set) 62extern __inline __attribute__((__always_inline__,__gnu_inline__))
63int getgroups(int len, gid_t *set)
48{ 64{
49 size_t bos = __builtin_object_size(set, 0); 65 size_t bos = __builtin_object_size(set, 0);
50 66
51 if (len > bos / sizeof(gid_t)) 67 if (len > bos / sizeof(gid_t))
52 __builtin_trap(); 68 __builtin_trap();
53 return getgroups(len, set); 69 return __getgroups_orig(len, set);
54} 70}
55 71
56static inline __attribute__ ((always_inline)) 72extern int __gethostname_orig(char *, size_t)
57int 73 __asm__(__USER_LABEL_PREFIX__ "gethostname");
58__fortify_gethostname(char *name, size_t len) 74extern __inline __attribute__((__always_inline__,__gnu_inline__))
75int gethostname(char *name, size_t len)
59{ 76{
60 size_t bos = __builtin_object_size(name, 0); 77 size_t bos = __builtin_object_size(name, 0);
61 78
62 if (len > bos) 79 if (len > bos)
63 __builtin_trap(); 80 __builtin_trap();
64 return gethostname(name, len); 81 return __gethostname_orig(name, len);
65} 82}
66 83
67static inline __attribute__ ((always_inline)) 84extern int __getlogin_r_orig(char *, size_t)
68int 85 __asm__(__USER_LABEL_PREFIX__ "getlogin_r");
69__fortify_getlogin_r(char *name, size_t len) 86extern __inline __attribute__((__always_inline__,__gnu_inline__))
87int getlogin_r(char *name, size_t len)
70{ 88{
71 size_t bos = __builtin_object_size(name, 0); 89 size_t bos = __builtin_object_size(name, 0);
72 90
73 if (len > bos) 91 if (len > bos)
74 __builtin_trap(); 92 __builtin_trap();
75 return getlogin_r(name, len); 93 return __getlogin_r_orig(name, len);
76} 94}
77 95
78static inline __attribute__ ((always_inline)) 96extern ssize_t __pread_orig(int, void *, size_t, off_t)
79ssize_t 97 __asm__(__USER_LABEL_PREFIX__ "pread");
80__fortify_pread(int fd, void *buf, size_t n, off_t offset) 98extern __inline __attribute__((__always_inline__,__gnu_inline__))
99ssize_t pread(int fd, void *buf, size_t n, off_t offset)
81{ 100{
82 size_t bos = __builtin_object_size(buf, 0); 101 size_t bos = __builtin_object_size(buf, 0);
83 102
84 if (n > bos) 103 if (n > bos)
85 __builtin_trap(); 104 __builtin_trap();
86 return pread(fd, buf, n, offset); 105 return __pread_orig(fd, buf, n, offset);
87} 106}
88 107
89static inline __attribute__ ((always_inline)) 108extern ssize_t __read_orig(int, void *, size_t)
90ssize_t 109 __asm__(__USER_LABEL_PREFIX__ "read");
91__fortify_read(int fd, void *buf, size_t n) 110extern __inline __attribute__((__always_inline__,__gnu_inline__))
111ssize_t read(int fd, void *buf, size_t n)
92{ 112{
93 size_t bos = __builtin_object_size(buf, 0); 113 size_t bos = __builtin_object_size(buf, 0);
94 114
95 if (n > bos) 115 if (n > bos)
96 __builtin_trap(); 116 __builtin_trap();
97 return read(fd, buf, n); 117 return __read_orig(fd, buf, n);
98} 118}
99 119
100static inline __attribute__ ((always_inline)) 120extern ssize_t __readlink_orig(const char *, char *, size_t)
101ssize_t 121 __asm__(__USER_LABEL_PREFIX__ "readlink");
102__fortify_readlink(const char *path, char *buf, size_t n) 122extern __inline __attribute__((__always_inline__,__gnu_inline__))
123ssize_t readlink(const char *path, char *buf, size_t n)
103{ 124{
104 size_t bos = __builtin_object_size(buf, 0); 125 size_t bos = __builtin_object_size(buf, 0);
105 126
106 if (n > bos) 127 if (n > bos)
107 __builtin_trap(); 128 __builtin_trap();
108 return readlink(path, buf, n); 129 return __readlink_orig(path, buf, n);
109} 130}
110 131
111static inline __attribute__ ((always_inline)) 132extern ssize_t __readlinkat_orig(int, const char *, char *, size_t)
112ssize_t 133 __asm__(__USER_LABEL_PREFIX__ "readlinkat");
113__fortify_readlinkat(int fd, const char *path, char *buf, size_t n) 134extern __inline __attribute__((__always_inline__,__gnu_inline__))
135ssize_t readlinkat(int fd, const char *path, char *buf, size_t n)
114{ 136{
115 size_t bos = __builtin_object_size(buf, 0); 137 size_t bos = __builtin_object_size(buf, 0);
116 138
117 if (n > bos) 139 if (n > bos)
118 __builtin_trap(); 140 __builtin_trap();
119 return readlinkat(fd, path, buf, n); 141 return __readlinkat_orig(fd, path, buf, n);
120} 142}
121 143
122static inline __attribute__ ((always_inline)) 144extern int __ttyname_r_orig(int, char *, size_t)
123int 145 __asm__(__USER_LABEL_PREFIX__ "ttyname_r");
124__fortify_ttyname_r(int fd, char *name, size_t n) 146extern __inline __attribute__((__always_inline__,__gnu_inline__))
147int ttyname_r(int fd, char *name, size_t n)
125{ 148{
126 size_t bos = __builtin_object_size(name, 0); 149 size_t bos = __builtin_object_size(name, 0);
127 150
128 if (n > bos) 151 if (n > bos)
129 __builtin_trap(); 152 __builtin_trap();
130 return ttyname_r(fd, name, n); 153 return __ttyname_r_orig(fd, name, n);
131} 154}
132 155
133static inline __attribute__ ((always_inline)) 156extern ssize_t __write_orig(int, const void *, size_t)
134ssize_t 157 __asm__(__USER_LABEL_PREFIX__ "write");
135__fortify_write(int fd, const void *buf, size_t n) 158extern __inline __attribute__((__always_inline__,__gnu_inline__))
159ssize_t write(int fd, const void *buf, size_t n)
136{ 160{
137 size_t bos = __builtin_object_size(buf, 0); 161 size_t bos = __builtin_object_size(buf, 0);
138 162
139 if (n > bos) 163 if (n > bos)
140 __builtin_trap(); 164 __builtin_trap();
141 return write(fd, buf, n); 165 return __write_orig(fd, buf, n);
142} 166}
143 167
144#undef confstr
145#define confstr(name, buf, len) __fortify_confstr(name, buf, len)
146#undef getcwd
147#define getcwd(buf, len) __fortify_getcwd(buf, len)
148
149#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
150#undef getdomainname
151#define getdomainname(name, len) __fortify_getdomainname(name, len)
152#endif
153
154#undef getgroups
155#define getgroups(len, set) __fortify_getgroups(len, set)
156#undef gethostname
157#define gethostname(name, len) __fortify_gethostname(name, len)
158#undef getlogin_r
159#define getlogin_r(name, len) __fortify_getlogin_r(name, len)
160#undef pread
161#define pread(fd, buf, n, offset) __fortify_pread(fd, buf, n, offset)
162#undef read
163#define read(fd, buf, n) __fortify_read(fd, buf, n)
164#undef readlink
165#define readlink(path, buf, n) __fortify_readlink(path, buf, n)
166#undef readlinkat
167#define readlinkat(fd, path, buf, n) __fortify_readlinkat(fd, path, buf, n)
168#undef ttyname_r
169#define ttyname_r(fd, name, n) __fortify_ttyname_r(fd, name, n)
170#undef write
171#define write(fd, buf, n) __fortify_write(fd, buf, n)
172
173#endif 168#endif
174 169
175#endif 170#endif