summaryrefslogtreecommitdiff
path: root/include/string.h
blob: f738901ce292677bc698f327a2442ff65e006687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2015-2016 Dimitris Papastamos <sin@2f30.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _FORTIFY_STRING_H
#define _FORTIFY_STRING_H

#ifndef __cplusplus
__extension__
#endif
#include_next <string.h>

#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
#include "fortify-headers.h"

#ifdef __cplusplus
extern "C" {
#endif

#undef memcpy
#undef memmove
#undef memset
#undef strcat
#undef strcpy
#undef strncat
#undef strncpy

__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(memcpy) void *memcpy(void *__od, const void *__os, size_t __n)
{
	size_t __bd = __bos(__od, 0);
	size_t __bs = __bos(__os, 0);
	char *__d = (char *)__od;
	const char *__s = (const char *)__os;

	/* trap if pointers are overlapping but not if dst == src.
	 * gcc seems to like to generate code that relies on dst == src */
	if ((__d < __s && __d + __n > __s) ||
	    (__s < __d && __s + __n > __d))
		__builtin_trap();
	if (__n > __bd || __n > __bs)
		__builtin_trap();
	return __builtin_memcpy(__od, __os, __n);
}

__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(memmove) void *memmove(void *__d, const void *__s, size_t __n)
{
	size_t __bd = __bos(__d, 0);
	size_t __bs = __bos(__s, 0);

	if (__n > __bd || __n > __bs)
		__builtin_trap();
	return __orig_memmove(__d, __s, __n);
}

__access(write_only, 1)
_FORTIFY_FN(memset) void *memset(void *__d, int __c, size_t __n)
{
	size_t __b = __bos(__d, 0);

	if (__n > __b)
		__builtin_trap();
	return __builtin_memset(__d, __c, __n);
}

#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
 || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
 || defined(_BSD_SOURCE)
#undef stpcpy
__access(write_only, 1)
__access(read_only, 2)
_FORTIFY_FN(stpcpy) char *stpcpy(char *__d, const char *__s)
{
	size_t __n = strlen(__s) + 1;

	/* trap if pointers are overlapping but not if dst == src.
	 * gcc seems to like to generate code that relies on dst == src */
	if ((__d < __s && __d + __n > __s) ||
	    (__s < __d && __s + __n > __d))
		__builtin_trap();

	size_t __b = __bos(__d, 0);
	if (__n > __b)
		__builtin_trap();
	return __orig_stpcpy(__d, __s);
}

#undef stpncpy
__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(stpncpy) char *stpncpy(char *__d, const char *__s, size_t __n)
{
	size_t __b = __bos(__d, 0);

	if (__n > __b && strlen(__s) + 1 > __b)
		__builtin_trap();
	return __orig_stpncpy(__d, __s, __n);
}
#endif

__access (read_write, 1)
__access (read_only, 2)
_FORTIFY_FN(strcat) char *strcat(char *__d, const char *__s)
{
	size_t __b = __bos(__d, 0);

	if (strlen(__s) + strlen(__d) + 1 > __b)
		__builtin_trap();
	return __orig_strcat(__d, __s);
}

__access (write_only, 1)
__access (read_only, 2)
_FORTIFY_FN(strcpy) char *strcpy(char *__d, const char *__s)
{
	size_t __n = strlen(__s) + 1;

	/* trap if pointers are overlapping but not if dst == src.
	 * gcc seems to like to generate code that relies on dst == src */
	if ((__d < __s && __d + __n > __s) ||
	    (__s < __d && __s + __n > __d))
		__builtin_trap();

	size_t __b = __bos(__d, 0);
	if (__n > __b)
		__builtin_trap();
	return __orig_strcpy(__d, __s);
}

__access (read_write, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strncat) char *strncat(char *__d, const char *__s, size_t __n)
{
	size_t __b = __bos(__d, 0);
	size_t __sl, __dl;

	if (__n > __b) {
		__sl = strlen(__s);
		__dl = strlen(__d);
		if (__sl > __n)
			__sl = __n;
		if (__sl + __dl + 1 > __b)
			__builtin_trap();
	}
	return __orig_strncat(__d, __s, __n);
}

__access (write_only, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strncpy) char *strncpy(char *__d, const char *__s, size_t __n)
{
	/* trap if pointers are overlapping but not if dst == src.
	 * gcc seems to like to generate code that relies on dst == src */
	if ((__d < __s && __d + __n > __s) ||
	    (__s < __d && __s + __n > __d))
		__builtin_trap();

	size_t __b = __bos(__d, 0);
	if (__n > __b)
		__builtin_trap();
	return __orig_strncpy(__d, __s, __n);
}

#ifdef _GNU_SOURCE
#undef mempcpy
__access(write_only, 1)
__access(read_only, 2, 3)
_FORTIFY_FN(mempcpy) void *mempcpy(void *__d, const void *__s, size_t __n)
{
	size_t __bd = __bos(__d, 0);
	size_t __bs = __bos(__s, 0);

	if (__n > __bd || __n > __bs)
		__builtin_trap();
	return __orig_mempcpy(__d, __s, __n);
}
#endif

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#undef strlcat
#undef strlcpy
__access (read_write, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strlcat) size_t strlcat(char *__d, const char *__s, size_t __n)
{
	size_t __b = __bos(__d, 0);

	if (__n > __b)
		__builtin_trap();
	return __orig_strlcat(__d, __s, __n);
}

__access (write_only, 1)
__access (read_only, 2, 3)
_FORTIFY_FN(strlcpy) size_t strlcpy(char *__d, const char *__s, size_t __n)
{
	size_t __b = __bos(__d, 0);

	if (__n > __b)
		__builtin_trap();
	return __orig_strlcpy(__d, __s, __n);
}
#endif

#ifdef __cplusplus
}
#endif

#endif

#endif