summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE21
-rw-r--r--Makefile8
-rw-r--r--include/string.h238
3 files changed, 267 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a7b0218
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
1MIT/X Consortium License
2
3© 2015 Dimitris Papastamos <sin@2f30.org>
4
5Permission is hereby granted, free of charge, to any person obtaining a
6copy of this software and associated documentation files (the "Software"),
7to deal in the Software without restriction, including without limitation
8the rights to use, copy, modify, merge, publish, distribute, sublicense,
9and/or sell copies of the Software, and to permit persons to whom the
10Software is furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21DEALINGS IN THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..78614d2
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
1PREFIX = /usr/local
2
3install:
4 mkdir -p $(DESTDIR)$(PREFIX)/include/fortify
5 cp -f include/* $(DESTDIR)$(PREFIX)/include/fortify
6
7.PHONY:
8 install
diff --git a/include/string.h b/include/string.h
new file mode 100644
index 0000000..944cf0b
--- /dev/null
+++ b/include/string.h
@@ -0,0 +1,238 @@
1#ifndef FORTIFY_STRING_H_
2#define FORTIFY_STRING_H_
3
4#include_next <string.h>
5
6#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
7
8static inline __attribute__ ((always_inline))
9void *__memcpy_chk(void *__restrict dest, const void *__restrict src, size_t ssize,
10 size_t dsize)
11{
12 if (ssize > dsize)
13 __builtin_trap();
14 return memcpy(dest, src, ssize);
15}
16
17static inline __attribute__ ((always_inline))
18void *__fortify_memcpy(void *__restrict dest, const void *__restrict src, size_t n)
19{
20 size_t bos = __builtin_object_size(dest, 0);
21
22 if (bos == (size_t)-1)
23 return memcpy(dest, src, n);
24 if (__builtin_constant_p(n))
25 if (n <= bos)
26 return memcpy(dest, src, n);
27 return __memcpy_chk(dest, src, n, bos);
28}
29
30#undef memcpy
31#define memcpy(dest, src, n) __fortify_memcpy(dest, src, n)
32
33static inline __attribute__ ((always_inline))
34void *__memmove_chk(void *__restrict dest, const void *__restrict src, size_t ssize,
35 size_t dsize)
36{
37 if (ssize > dsize)
38 __builtin_trap();
39 return memmove(dest, src, ssize);
40}
41
42static inline __attribute__ ((always_inline))
43void *__fortify_memmove(void *__restrict dest, const void *__restrict src, size_t n)
44{
45 size_t bos = __builtin_object_size(dest, 0);
46
47 if (bos == (size_t)-1)
48 return memmove(dest, src, n);
49 if (__builtin_constant_p(n))
50 if (n <= bos)
51 return memmove(dest, src, n);
52 return __memmove_chk(dest, src, n, bos);
53}
54
55#undef memmove
56#define memmove(dest, src, n) __fortify_memmove(dest, src, n)
57
58static inline __attribute__ ((always_inline))
59void *__memset_chk(void *dest, int c, size_t n, size_t dsize)
60{
61 if (n > dsize)
62 __builtin_trap();
63 return memset(dest, c, n);
64}
65
66static inline __attribute__ ((always_inline))
67void *__fortify_memset(void *dest, int c, size_t n)
68{
69 size_t bos = __builtin_object_size(dest, 0);
70
71 if (bos == (size_t)-1)
72 return memset(dest, c, n);
73 if (__builtin_constant_p(n))
74 if (n <= bos)
75 return memset(dest, c, n);
76 return __memset_chk(dest, c, n, bos);
77}
78
79#undef memset
80#define memset(dest, src, n) __fortify_memset(dest, src, n)
81
82static inline __attribute__ ((always_inline))
83char *__strcat_chk(char *__restrict dest, const char *__restrict src, size_t n)
84{
85 size_t slen = strlen(src);
86 size_t dlen = strlen(dest);
87
88 if (slen + dlen + 1 > n)
89 __builtin_trap();
90 return strcat(dest, src);
91}
92
93static inline __attribute__ ((always_inline))
94char *__fortify_strcat(char *__restrict dest, const char *__restrict src)
95{
96 size_t bos = __builtin_object_size(dest, 0);
97
98 if (bos == (size_t)-1)
99 return strcat(dest, src);
100 return __strcat_chk(dest, src, bos);
101}
102
103#undef strcat
104#define strcat(dest, src) __fortify_strcat(dest, src)
105
106static inline __attribute__ ((always_inline))
107char *__strcpy_chk(char *__restrict dest, const char *__restrict src, size_t n)
108{
109 size_t slen = strlen(src);
110
111 if (slen + 1 > n)
112 __builtin_trap();
113 return strcpy(dest, src);
114}
115
116static inline __attribute__ ((always_inline))
117char *__fortify_strcpy(char *__restrict dest, const char *__restrict src)
118{
119 size_t bos = __builtin_object_size(dest, 0);
120
121 if (bos == (size_t)-1)
122 return strcpy(dest, src);
123 return __strcpy_chk(dest, src, bos);
124}
125
126#undef strcpy
127#define strcpy(dest, src) __fortify_strcpy(dest, src)
128
129static inline __attribute__ ((always_inline))
130char *__strncat_chk(char *__restrict dest, const char *__restrict src, size_t n,
131 size_t dsize)
132{
133 size_t slen = strlen(src);
134 size_t dlen = strlen(dest);
135
136 if (slen > n) slen = n;
137 if (slen + dlen + 1 > dsize)
138 __builtin_trap();
139 return strncat(dest, src, n);
140}
141
142static inline __attribute__ ((always_inline))
143char *__fortify_strncat(char *__restrict dest, const char *__restrict src, size_t n)
144{
145 size_t bos = __builtin_object_size(dest, 0);
146
147 if (bos == (size_t)-1)
148 return strncat(dest, src, n);
149 if (__builtin_constant_p(n))
150 if (n <= bos)
151 return strncat(dest, src, n);
152 return __strncat_chk(dest, src, n, bos);
153}
154
155#undef strncat
156#define strncat(dest, src, n) __fortify_strcat(dest, src, n)
157
158static inline __attribute__ ((always_inline))
159char *__strncpy_chk(char *__restrict dest, const char *__restrict src, size_t n,
160 size_t dsize)
161{
162 if (n > dsize)
163 __builtin_trap();
164 return strncpy(dest, src, n);
165}
166
167static inline __attribute__ ((always_inline))
168char *__fortify_strncpy(char *__restrict dest, const char *__restrict src, size_t n)
169{
170 size_t bos = __builtin_object_size(dest, 0);
171
172 if (bos == (size_t)-1)
173 return strncpy(dest, src, n);
174 if (__builtin_constant_p(n))
175 if (n <= bos)
176 return strncpy(dest, src, n);
177 return __strncpy_chk(dest, src, n, bos);
178}
179
180#undef strncpy
181#define strncpy(dest, src, n) __fortify_strcpy(dest, src, n)
182
183#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
184static inline __attribute__ ((always_inline))
185size_t __strlcat_chk(char *__restrict dest, const char *__restrict src, size_t n,
186 size_t dsize)
187{
188 if (n > dsize)
189 __builtin_trap();
190 return strlcat(dest, src, n);
191}
192
193static inline __attribute__ ((always_inline))
194size_t __fortify_strlcat(char *__restrict dest, const char *__restrict src, size_t n)
195{
196 size_t bos = __builtin_object_size(dest, 0);
197
198 if (bos == (size_t)-1)
199 return strlcat(dest, src, n);
200 if (__builtin_constant_p(n)) {
201 if (n <= bos)
202 return strlcat(dest, src, n);
203 }
204 return __strlcat_chk(dest, src, n, bos);
205}
206
207#undef strlcat
208#define strlcat(dest, src, n) __fortify_strlcat(dest, src, n)
209
210static inline __attribute__ ((always_inline))
211size_t __strlcpy_chk(char *__restrict dest, const char *__restrict src, size_t n,
212 size_t dsize)
213{
214 if (n > dsize)
215 __builtin_trap();
216 return strlcpy(dest, src, n);
217}
218
219static inline __attribute__ ((always_inline))
220size_t __fortify_strlcpy(char *__restrict dest, const char *__restrict src, size_t n)
221{
222 size_t bos = __builtin_object_size(dest, 0);
223
224 if (bos == (size_t)-1)
225 return strlcpy(dest, src, n);
226 if (__builtin_constant_p(n)) {
227 if (n <= bos)
228 return strlcpy(dest, src, n);
229 }
230 return __strlcpy_chk(dest, src, n, bos);
231}
232
233#undef strlcpy
234#define strlcpy(dest, src, n) __fortify_strlcpy(dest, src, n)
235#endif
236#endif
237
238#endif